2 * Copyright 1995-2019 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 "internal/refcount.h"
13 #include <openssl/x509.h>
14 #include "crypto/x509.h"
15 #include <openssl/x509v3.h>
16 #include "x509_local.h"
18 X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method)
20 X509_LOOKUP *ret = OPENSSL_zalloc(sizeof(*ret));
23 X509err(X509_F_X509_LOOKUP_NEW, ERR_R_MALLOC_FAILURE);
28 if (method->new_item != NULL && method->new_item(ret) == 0) {
35 void X509_LOOKUP_free(X509_LOOKUP *ctx)
39 if ((ctx->method != NULL) && (ctx->method->free != NULL))
40 (*ctx->method->free) (ctx);
44 int X509_STORE_lock(X509_STORE *s)
46 return CRYPTO_THREAD_write_lock(s->lock);
49 int X509_STORE_unlock(X509_STORE *s)
51 return CRYPTO_THREAD_unlock(s->lock);
54 int X509_LOOKUP_init(X509_LOOKUP *ctx)
56 if (ctx->method == NULL)
58 if (ctx->method->init != NULL)
59 return ctx->method->init(ctx);
64 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
66 if (ctx->method == NULL)
68 if (ctx->method->shutdown != NULL)
69 return ctx->method->shutdown(ctx);
74 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
77 if (ctx->method == NULL)
79 if (ctx->method->ctrl != NULL)
80 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
85 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
86 X509_NAME *name, X509_OBJECT *ret)
88 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
92 return ctx->method->get_by_subject(ctx, type, name, ret);
95 int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
96 X509_NAME *name, ASN1_INTEGER *serial,
99 if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
101 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
104 int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
105 const unsigned char *bytes, int len,
108 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
110 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
113 int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
114 const char *str, int len, X509_OBJECT *ret)
116 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
118 return ctx->method->get_by_alias(ctx, type, str, len, ret);
121 int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data)
123 ctx->method_data = data;
127 void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx)
129 return ctx->method_data;
132 X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx)
134 return ctx->store_ctx;
138 static int x509_object_cmp(const X509_OBJECT *const *a,
139 const X509_OBJECT *const *b)
143 ret = ((*a)->type - (*b)->type);
146 switch ((*a)->type) {
148 ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
151 ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
160 X509_STORE *X509_STORE_new(void)
162 X509_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
165 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
168 if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL) {
169 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
173 if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL) {
174 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
178 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL) {
179 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
182 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data)) {
183 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
187 ret->lock = CRYPTO_THREAD_lock_new();
188 if (ret->lock == NULL) {
189 X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE);
197 X509_VERIFY_PARAM_free(ret->param);
198 sk_X509_OBJECT_free(ret->objs);
199 sk_X509_LOOKUP_free(ret->get_cert_methods);
204 void X509_STORE_free(X509_STORE *vfy)
207 STACK_OF(X509_LOOKUP) *sk;
212 CRYPTO_DOWN_REF(&vfy->references, &i, vfy->lock);
213 REF_PRINT_COUNT("X509_STORE", vfy);
216 REF_ASSERT_ISNT(i < 0);
218 sk = vfy->get_cert_methods;
219 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
220 lu = sk_X509_LOOKUP_value(sk, i);
221 X509_LOOKUP_shutdown(lu);
222 X509_LOOKUP_free(lu);
224 sk_X509_LOOKUP_free(sk);
225 sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free);
227 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
228 X509_VERIFY_PARAM_free(vfy->param);
229 CRYPTO_THREAD_lock_free(vfy->lock);
233 int X509_STORE_up_ref(X509_STORE *vfy)
237 if (CRYPTO_UP_REF(&vfy->references, &i, vfy->lock) <= 0)
240 REF_PRINT_COUNT("X509_STORE", a);
241 REF_ASSERT_ISNT(i < 2);
242 return ((i > 1) ? 1 : 0);
245 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
248 STACK_OF(X509_LOOKUP) *sk;
251 sk = v->get_cert_methods;
252 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
253 lu = sk_X509_LOOKUP_value(sk, i);
254 if (m == lu->method) {
259 lu = X509_LOOKUP_new(m);
261 X509err(X509_F_X509_STORE_ADD_LOOKUP, ERR_R_MALLOC_FAILURE);
266 if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
269 X509err(X509_F_X509_STORE_ADD_LOOKUP, ERR_R_MALLOC_FAILURE);
270 X509_LOOKUP_free(lu);
274 X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs,
275 X509_LOOKUP_TYPE type,
278 X509_OBJECT *ret = X509_OBJECT_new();
282 if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) {
283 X509_OBJECT_free(ret);
289 int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
290 X509_NAME *name, X509_OBJECT *ret)
292 X509_STORE *store = vs->ctx;
294 X509_OBJECT stmp, *tmp;
300 stmp.type = X509_LU_NONE;
301 stmp.data.ptr = NULL;
304 X509_STORE_lock(store);
305 tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name);
306 X509_STORE_unlock(store);
308 if (tmp == NULL || type == X509_LU_CRL) {
309 for (i = 0; i < sk_X509_LOOKUP_num(store->get_cert_methods); i++) {
310 lu = sk_X509_LOOKUP_value(store->get_cert_methods, i);
311 j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
321 if (!X509_OBJECT_up_ref_count(tmp))
324 ret->type = tmp->type;
325 ret->data.ptr = tmp->data.ptr;
330 static int x509_store_add(X509_STORE *store, void *x, int crl) {
332 int ret = 0, added = 0;
336 obj = X509_OBJECT_new();
341 obj->type = X509_LU_CRL;
342 obj->data.crl = (X509_CRL *)x;
344 obj->type = X509_LU_X509;
345 obj->data.x509 = (X509 *)x;
347 if (!X509_OBJECT_up_ref_count(obj)) {
348 obj->type = X509_LU_NONE;
349 X509_OBJECT_free(obj);
353 X509_STORE_lock(store);
354 if (X509_OBJECT_retrieve_match(store->objs, obj)) {
357 added = sk_X509_OBJECT_push(store->objs, obj);
360 X509_STORE_unlock(store);
362 if (added == 0) /* obj not pushed */
363 X509_OBJECT_free(obj);
368 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
370 if (!x509_store_add(ctx, x, 0)) {
371 X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
377 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
379 if (!x509_store_add(ctx, x, 1)) {
380 X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
386 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
392 return X509_up_ref(a->data.x509);
394 return X509_CRL_up_ref(a->data.crl);
399 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
401 if (a == NULL || a->type != X509_LU_X509)
406 X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
408 if (a == NULL || a->type != X509_LU_CRL)
413 X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
418 X509_OBJECT *X509_OBJECT_new(void)
420 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
423 X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
426 ret->type = X509_LU_NONE;
430 static void x509_object_free_internal(X509_OBJECT *a)
438 X509_free(a->data.x509);
441 X509_CRL_free(a->data.crl);
446 int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj)
448 if (a == NULL || !X509_up_ref(obj))
451 x509_object_free_internal(a);
452 a->type = X509_LU_X509;
457 int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj)
459 if (a == NULL || !X509_CRL_up_ref(obj))
462 x509_object_free_internal(a);
463 a->type = X509_LU_CRL;
468 void X509_OBJECT_free(X509_OBJECT *a)
470 x509_object_free_internal(a);
474 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
475 X509_NAME *name, int *pnmatch)
485 stmp.data.x509 = &x509_s;
486 x509_s.cert_info.subject = name;
489 stmp.data.crl = &crl_s;
490 crl_s.crl.issuer = name;
497 idx = sk_X509_OBJECT_find(h, &stmp);
498 if (idx >= 0 && pnmatch) {
500 const X509_OBJECT *tobj, *pstmp;
503 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
504 tobj = sk_X509_OBJECT_value(h, tidx);
505 if (x509_object_cmp(&tobj, &pstmp))
513 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
516 return x509_object_idx_cnt(h, type, name, NULL);
519 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
520 X509_LOOKUP_TYPE type,
524 idx = X509_OBJECT_idx_by_subject(h, type, name);
527 return sk_X509_OBJECT_value(h, idx);
530 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
535 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
538 STACK_OF(X509) *sk = NULL;
541 X509_STORE *store = ctx->ctx;
546 X509_STORE_lock(store);
547 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
550 * Nothing found in cache: do lookup to possibly add new objects to
553 X509_OBJECT *xobj = X509_OBJECT_new();
555 X509_STORE_unlock(store);
559 if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
560 X509_OBJECT_free(xobj);
563 X509_OBJECT_free(xobj);
564 X509_STORE_lock(store);
565 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
567 X509_STORE_unlock(store);
572 sk = sk_X509_new_null();
573 for (i = 0; i < cnt; i++, idx++) {
574 obj = sk_X509_OBJECT_value(store->objs, idx);
576 if (!X509_up_ref(x)) {
577 X509_STORE_unlock(store);
578 sk_X509_pop_free(sk, X509_free);
581 if (!sk_X509_push(sk, x)) {
582 X509_STORE_unlock(store);
584 sk_X509_pop_free(sk, X509_free);
588 X509_STORE_unlock(store);
592 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
595 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
597 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
598 X509_STORE *store = ctx->ctx;
600 /* Always do lookup to possibly add new CRLs to cache */
604 || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
605 X509_OBJECT_free(xobj);
606 sk_X509_CRL_free(sk);
609 X509_OBJECT_free(xobj);
610 X509_STORE_lock(store);
611 idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt);
613 X509_STORE_unlock(store);
614 sk_X509_CRL_free(sk);
618 for (i = 0; i < cnt; i++, idx++) {
619 obj = sk_X509_OBJECT_value(store->objs, idx);
621 if (!X509_CRL_up_ref(x)) {
622 X509_STORE_unlock(store);
623 sk_X509_CRL_pop_free(sk, X509_CRL_free);
626 if (!sk_X509_CRL_push(sk, x)) {
627 X509_STORE_unlock(store);
629 sk_X509_CRL_pop_free(sk, X509_CRL_free);
633 X509_STORE_unlock(store);
637 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
643 idx = sk_X509_OBJECT_find(h, x);
646 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
647 return sk_X509_OBJECT_value(h, idx);
648 for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
649 obj = sk_X509_OBJECT_value(h, i);
650 if (x509_object_cmp((const X509_OBJECT **)&obj,
651 (const X509_OBJECT **)&x))
653 if (x->type == X509_LU_X509) {
654 if (!X509_cmp(obj->data.x509, x->data.x509))
656 } else if (x->type == X509_LU_CRL) {
657 if (!X509_CRL_match(obj->data.crl, x->data.crl))
666 * Try to get issuer certificate from store. Due to limitations
667 * of the API this can only retrieve a single certificate matching
668 * a given subject name. However it will fill the cache with all
669 * matching certificates, so we can examine the cache for all
673 * 1 lookup successful.
674 * 0 certificate not found.
675 * -1 some other error.
677 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
680 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
681 X509_STORE *store = ctx->ctx;
687 xn = X509_get_issuer_name(x);
688 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
690 X509_OBJECT_free(obj);
693 /* If certificate matches all OK */
694 if (ctx->check_issued(ctx, x, obj->data.x509)) {
695 if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
696 *issuer = obj->data.x509;
697 if (!X509_up_ref(*issuer)) {
701 X509_OBJECT_free(obj);
705 X509_OBJECT_free(obj);
710 /* Else find index of first cert accepted by 'check_issued' */
712 X509_STORE_lock(store);
713 idx = X509_OBJECT_idx_by_subject(store->objs, X509_LU_X509, xn);
714 if (idx != -1) { /* should be true as we've had at least one
716 /* Look through all matching certs for suitable issuer */
717 for (i = idx; i < sk_X509_OBJECT_num(store->objs); i++) {
718 pobj = sk_X509_OBJECT_value(store->objs, i);
719 /* See if we've run past the matches */
720 if (pobj->type != X509_LU_X509)
722 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
724 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
725 *issuer = pobj->data.x509;
728 * If times check, exit with match,
729 * otherwise keep looking. Leave last
730 * match in issuer so we return nearest
731 * match if no certificate time is OK.
734 if (x509_check_cert_time(ctx, *issuer, -1))
739 if (*issuer && !X509_up_ref(*issuer)) {
743 X509_STORE_unlock(store);
747 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
749 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
752 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
754 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
758 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
760 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
763 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
765 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
768 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
770 return X509_VERIFY_PARAM_set1(ctx->param, param);
773 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
778 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
780 ctx->verify = verify;
783 X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx)
788 void X509_STORE_set_verify_cb(X509_STORE *ctx,
789 X509_STORE_CTX_verify_cb verify_cb)
791 ctx->verify_cb = verify_cb;
794 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx)
796 return ctx->verify_cb;
799 void X509_STORE_set_get_issuer(X509_STORE *ctx,
800 X509_STORE_CTX_get_issuer_fn get_issuer)
802 ctx->get_issuer = get_issuer;
805 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx)
807 return ctx->get_issuer;
810 void X509_STORE_set_check_issued(X509_STORE *ctx,
811 X509_STORE_CTX_check_issued_fn check_issued)
813 ctx->check_issued = check_issued;
816 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx)
818 return ctx->check_issued;
821 void X509_STORE_set_check_revocation(X509_STORE *ctx,
822 X509_STORE_CTX_check_revocation_fn check_revocation)
824 ctx->check_revocation = check_revocation;
827 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx)
829 return ctx->check_revocation;
832 void X509_STORE_set_get_crl(X509_STORE *ctx,
833 X509_STORE_CTX_get_crl_fn get_crl)
835 ctx->get_crl = get_crl;
838 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx)
843 void X509_STORE_set_check_crl(X509_STORE *ctx,
844 X509_STORE_CTX_check_crl_fn check_crl)
846 ctx->check_crl = check_crl;
849 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx)
851 return ctx->check_crl;
854 void X509_STORE_set_cert_crl(X509_STORE *ctx,
855 X509_STORE_CTX_cert_crl_fn cert_crl)
857 ctx->cert_crl = cert_crl;
860 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx)
862 return ctx->cert_crl;
865 void X509_STORE_set_check_policy(X509_STORE *ctx,
866 X509_STORE_CTX_check_policy_fn check_policy)
868 ctx->check_policy = check_policy;
871 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx)
873 return ctx->check_policy;
876 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
877 X509_STORE_CTX_lookup_certs_fn lookup_certs)
879 ctx->lookup_certs = lookup_certs;
882 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx)
884 return ctx->lookup_certs;
887 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
888 X509_STORE_CTX_lookup_crls_fn lookup_crls)
890 ctx->lookup_crls = lookup_crls;
893 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx)
895 return ctx->lookup_crls;
898 void X509_STORE_set_cleanup(X509_STORE *ctx,
899 X509_STORE_CTX_cleanup_fn ctx_cleanup)
901 ctx->cleanup = ctx_cleanup;
904 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx)
909 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
911 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
914 void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
916 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
919 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)