2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/lhash.h>
13 #include <openssl/x509.h>
14 #include "internal/x509_int.h"
15 #include <openssl/x509v3.h>
18 X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method)
22 ret = OPENSSL_zalloc(sizeof(*ret));
27 if ((method->new_item != NULL) && !method->new_item(ret)) {
34 void X509_LOOKUP_free(X509_LOOKUP *ctx)
38 if ((ctx->method != NULL) && (ctx->method->free != NULL))
39 (*ctx->method->free) (ctx);
43 int X509_LOOKUP_init(X509_LOOKUP *ctx)
45 if (ctx->method == NULL)
47 if (ctx->method->init != NULL)
48 return ctx->method->init(ctx);
53 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
55 if (ctx->method == NULL)
57 if (ctx->method->shutdown != NULL)
58 return ctx->method->shutdown(ctx);
63 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
66 if (ctx->method == NULL)
68 if (ctx->method->ctrl != NULL)
69 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
74 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
77 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
81 return ctx->method->get_by_subject(ctx, type, name, ret);
84 int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
85 ASN1_INTEGER *serial, X509_OBJECT *ret)
87 if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
89 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
92 int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type,
93 unsigned char *bytes, int len,
96 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
98 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
101 int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str, int len,
104 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
106 return ctx->method->get_by_alias(ctx, type, str, len, ret);
109 static int x509_object_cmp(const X509_OBJECT *const *a,
110 const X509_OBJECT *const *b)
114 ret = ((*a)->type - (*b)->type);
117 switch ((*a)->type) {
119 ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
122 ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
131 X509_STORE *X509_STORE_new(void)
135 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
137 if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL)
140 if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL)
143 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL)
146 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data))
149 ret->lock = CRYPTO_THREAD_lock_new();
150 if (ret->lock == NULL)
157 X509_VERIFY_PARAM_free(ret->param);
158 sk_X509_OBJECT_free(ret->objs);
159 sk_X509_LOOKUP_free(ret->get_cert_methods);
164 static void cleanup(X509_OBJECT *a)
168 if (a->type == X509_LU_X509) {
169 X509_free(a->data.x509);
170 } else if (a->type == X509_LU_CRL) {
171 X509_CRL_free(a->data.crl);
179 void X509_STORE_free(X509_STORE *vfy)
182 STACK_OF(X509_LOOKUP) *sk;
188 CRYPTO_atomic_add(&vfy->references, -1, &i, vfy->lock);
189 REF_PRINT_COUNT("X509_STORE", vfy);
192 REF_ASSERT_ISNT(i < 0);
194 sk = vfy->get_cert_methods;
195 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
196 lu = sk_X509_LOOKUP_value(sk, i);
197 X509_LOOKUP_shutdown(lu);
198 X509_LOOKUP_free(lu);
200 sk_X509_LOOKUP_free(sk);
201 sk_X509_OBJECT_pop_free(vfy->objs, cleanup);
203 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
204 X509_VERIFY_PARAM_free(vfy->param);
205 CRYPTO_THREAD_lock_free(vfy->lock);
209 int X509_STORE_up_ref(X509_STORE *vfy)
213 if (CRYPTO_atomic_add(&vfy->references, 1, &i, vfy->lock) <= 0)
216 REF_PRINT_COUNT("X509_STORE", a);
217 REF_ASSERT_ISNT(i < 2);
218 return ((i > 1) ? 1 : 0);
221 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
224 STACK_OF(X509_LOOKUP) *sk;
227 sk = v->get_cert_methods;
228 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
229 lu = sk_X509_LOOKUP_value(sk, i);
230 if (m == lu->method) {
235 lu = X509_LOOKUP_new(m);
240 if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
243 X509_LOOKUP_free(lu);
249 X509_OBJECT *X509_STORE_get_X509_by_subject(X509_STORE_CTX *vs, int type,
254 ret = OPENSSL_malloc(sizeof (*ret));
256 X509err(X509_F_X509_STORE_GET_X509_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
259 if (!X509_STORE_get_by_subject(vs, type, name, ret)) {
266 int X509_STORE_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
267 X509_NAME *name, X509_OBJECT *ret)
269 X509_STORE *ctx = vs->ctx;
271 X509_OBJECT stmp, *tmp;
274 CRYPTO_THREAD_write_lock(ctx->lock);
275 tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
276 CRYPTO_THREAD_unlock(ctx->lock);
278 if (tmp == NULL || type == X509_LU_CRL) {
279 for (i = vs->current_method;
280 i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
281 lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
282 j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
284 vs->current_method = j;
291 vs->current_method = 0;
296 /*- if (ret->data.ptr != NULL)
297 X509_OBJECT_free_contents(ret); */
299 ret->type = tmp->type;
300 ret->data.ptr = tmp->data.ptr;
302 X509_OBJECT_up_ref_count(ret);
307 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
314 obj = OPENSSL_malloc(sizeof(*obj));
316 X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
319 obj->type = X509_LU_X509;
322 CRYPTO_THREAD_write_lock(ctx->lock);
324 X509_OBJECT_up_ref_count(obj);
326 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
327 X509_OBJECT_free_contents(obj);
329 X509err(X509_F_X509_STORE_ADD_CERT,
330 X509_R_CERT_ALREADY_IN_HASH_TABLE);
333 sk_X509_OBJECT_push(ctx->objs, obj);
335 CRYPTO_THREAD_unlock(ctx->lock);
340 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
347 obj = OPENSSL_malloc(sizeof(*obj));
349 X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
352 obj->type = X509_LU_CRL;
355 CRYPTO_THREAD_write_lock(ctx->lock);
357 X509_OBJECT_up_ref_count(obj);
359 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
360 X509_OBJECT_free_contents(obj);
362 X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
365 sk_X509_OBJECT_push(ctx->objs, obj);
367 CRYPTO_THREAD_unlock(ctx->lock);
372 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
378 return X509_up_ref(a->data.x509);
380 return X509_CRL_up_ref(a->data.crl);
385 X509 *X509_OBJECT_get0_X509(X509_OBJECT *a)
390 int X509_OBJECT_get_type(X509_OBJECT *a)
395 void X509_OBJECT_free(X509_OBJECT *a)
399 X509_OBJECT_free_contents(a);
403 void X509_OBJECT_free_contents(X509_OBJECT *a)
411 X509_free(a->data.x509);
414 X509_CRL_free(a->data.crl);
419 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
420 X509_NAME *name, int *pnmatch)
430 stmp.data.x509 = &x509_s;
431 x509_s.cert_info.subject = name;
434 stmp.data.crl = &crl_s;
435 crl_s.crl.issuer = name;
442 idx = sk_X509_OBJECT_find(h, &stmp);
443 if (idx >= 0 && pnmatch) {
445 const X509_OBJECT *tobj, *pstmp;
448 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
449 tobj = sk_X509_OBJECT_value(h, tidx);
450 if (x509_object_cmp(&tobj, &pstmp))
458 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
461 return x509_object_idx_cnt(h, type, name, NULL);
464 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
465 int type, X509_NAME *name)
468 idx = X509_OBJECT_idx_by_subject(h, type, name);
471 return sk_X509_OBJECT_value(h, idx);
474 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
479 STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
485 sk = sk_X509_new_null();
486 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
487 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
490 * Nothing found in cache: do lookup to possibly add new objects to
494 CRYPTO_THREAD_unlock(ctx->ctx->lock);
495 if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) {
499 X509_OBJECT_free_contents(&xobj);
500 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
501 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
503 CRYPTO_THREAD_unlock(ctx->ctx->lock);
508 for (i = 0; i < cnt; i++, idx++) {
509 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
512 if (!sk_X509_push(sk, x)) {
513 CRYPTO_THREAD_unlock(ctx->ctx->lock);
515 sk_X509_pop_free(sk, X509_free);
519 CRYPTO_THREAD_unlock(ctx->ctx->lock);
524 STACK_OF(X509_CRL) *X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
527 STACK_OF(X509_CRL) *sk;
529 X509_OBJECT *obj, xobj;
530 sk = sk_X509_CRL_new_null();
533 * Always do lookup to possibly add new CRLs to cache
535 if (!X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) {
536 sk_X509_CRL_free(sk);
539 X509_OBJECT_free_contents(&xobj);
540 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
541 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
543 CRYPTO_THREAD_unlock(ctx->ctx->lock);
544 sk_X509_CRL_free(sk);
548 for (i = 0; i < cnt; i++, idx++) {
549 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
552 if (!sk_X509_CRL_push(sk, x)) {
553 CRYPTO_THREAD_unlock(ctx->ctx->lock);
555 sk_X509_CRL_pop_free(sk, X509_CRL_free);
559 CRYPTO_THREAD_unlock(ctx->ctx->lock);
563 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
568 idx = sk_X509_OBJECT_find(h, x);
571 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
572 return sk_X509_OBJECT_value(h, idx);
573 for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
574 obj = sk_X509_OBJECT_value(h, i);
576 ((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x))
578 if (x->type == X509_LU_X509) {
579 if (!X509_cmp(obj->data.x509, x->data.x509))
581 } else if (x->type == X509_LU_CRL) {
582 if (!X509_CRL_match(obj->data.crl, x->data.crl))
591 * Try to get issuer certificate from store. Due to limitations
592 * of the API this can only retrieve a single certificate matching
593 * a given subject name. However it will fill the cache with all
594 * matching certificates, so we can examine the cache for all
598 * 1 lookup successful.
599 * 0 certificate not found.
600 * -1 some other error.
602 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
605 X509_OBJECT obj, *pobj;
609 xn = X509_get_issuer_name(x);
610 ok = X509_STORE_get_by_subject(ctx, X509_LU_X509, xn, &obj);
611 if (ok != X509_LU_X509) {
612 if (ok == X509_LU_RETRY) {
613 X509_OBJECT_free_contents(&obj);
614 X509err(X509_F_X509_STORE_CTX_GET1_ISSUER, X509_R_SHOULD_RETRY);
616 } else if (ok != X509_LU_FAIL) {
617 X509_OBJECT_free_contents(&obj);
618 /* not good :-(, break anyway */
623 /* If certificate matches all OK */
624 if (ctx->check_issued(ctx, x, obj.data.x509)) {
625 if (x509_check_cert_time(ctx, obj.data.x509, -1)) {
626 *issuer = obj.data.x509;
630 X509_OBJECT_free_contents(&obj);
632 /* Else find index of first cert accepted by 'check_issued' */
634 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
635 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
636 if (idx != -1) { /* should be true as we've had at least one
638 /* Look through all matching certs for suitable issuer */
639 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
640 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
641 /* See if we've run past the matches */
642 if (pobj->type != X509_LU_X509)
644 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
646 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
647 *issuer = pobj->data.x509;
650 * If times check, exit with match,
651 * otherwise keep looking. Leave last
652 * match in issuer so we return nearest
653 * match if no certificate time is OK.
656 if (x509_check_cert_time(ctx, *issuer, -1))
661 CRYPTO_THREAD_unlock(ctx->ctx->lock);
663 X509_up_ref(*issuer);
667 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
669 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
672 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
674 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
678 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
680 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
683 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
685 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
688 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
690 return X509_VERIFY_PARAM_set1(ctx->param, param);
693 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
698 void X509_STORE_set_verify_cb(X509_STORE *ctx,
699 int (*verify_cb) (int, X509_STORE_CTX *))
701 ctx->verify_cb = verify_cb;
704 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify verify)
706 ctx->verify = verify;
709 void X509_STORE_set_lookup_crls_cb(X509_STORE *ctx,
710 STACK_OF(X509_CRL) *(*cb) (X509_STORE_CTX
714 ctx->lookup_crls = cb;
717 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
719 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
722 void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
724 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
727 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)