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_STORE_lock(X509_STORE *s)
45 return CRYPTO_THREAD_write_lock(s->lock);
48 int X509_STORE_unlock(X509_STORE *s)
50 return CRYPTO_THREAD_unlock(s->lock);
53 int X509_LOOKUP_init(X509_LOOKUP *ctx)
55 if (ctx->method == NULL)
57 if (ctx->method->init != NULL)
58 return ctx->method->init(ctx);
63 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
65 if (ctx->method == NULL)
67 if (ctx->method->shutdown != NULL)
68 return ctx->method->shutdown(ctx);
73 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
76 if (ctx->method == NULL)
78 if (ctx->method->ctrl != NULL)
79 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
84 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
87 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
91 return ctx->method->get_by_subject(ctx, type, name, ret);
94 int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
95 ASN1_INTEGER *serial, X509_OBJECT *ret)
97 if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
99 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
102 int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type,
103 unsigned char *bytes, int len,
106 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
108 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
111 int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str, int len,
114 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
116 return ctx->method->get_by_alias(ctx, type, str, len, ret);
119 static int x509_object_cmp(const X509_OBJECT *const *a,
120 const X509_OBJECT *const *b)
124 ret = ((*a)->type - (*b)->type);
127 switch ((*a)->type) {
129 ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
132 ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
141 X509_STORE *X509_STORE_new(void)
145 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
147 if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL)
150 if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL)
153 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL)
156 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data))
159 ret->lock = CRYPTO_THREAD_lock_new();
160 if (ret->lock == NULL)
167 X509_VERIFY_PARAM_free(ret->param);
168 sk_X509_OBJECT_free(ret->objs);
169 sk_X509_LOOKUP_free(ret->get_cert_methods);
174 static void cleanup(X509_OBJECT *a)
178 if (a->type == X509_LU_X509) {
179 X509_free(a->data.x509);
180 } else if (a->type == X509_LU_CRL) {
181 X509_CRL_free(a->data.crl);
189 void X509_STORE_free(X509_STORE *vfy)
192 STACK_OF(X509_LOOKUP) *sk;
198 CRYPTO_atomic_add(&vfy->references, -1, &i, vfy->lock);
199 REF_PRINT_COUNT("X509_STORE", vfy);
202 REF_ASSERT_ISNT(i < 0);
204 sk = vfy->get_cert_methods;
205 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
206 lu = sk_X509_LOOKUP_value(sk, i);
207 X509_LOOKUP_shutdown(lu);
208 X509_LOOKUP_free(lu);
210 sk_X509_LOOKUP_free(sk);
211 sk_X509_OBJECT_pop_free(vfy->objs, cleanup);
213 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
214 X509_VERIFY_PARAM_free(vfy->param);
215 CRYPTO_THREAD_lock_free(vfy->lock);
219 int X509_STORE_up_ref(X509_STORE *vfy)
223 if (CRYPTO_atomic_add(&vfy->references, 1, &i, vfy->lock) <= 0)
226 REF_PRINT_COUNT("X509_STORE", a);
227 REF_ASSERT_ISNT(i < 2);
228 return ((i > 1) ? 1 : 0);
231 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
234 STACK_OF(X509_LOOKUP) *sk;
237 sk = v->get_cert_methods;
238 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
239 lu = sk_X509_LOOKUP_value(sk, i);
240 if (m == lu->method) {
245 lu = X509_LOOKUP_new(m);
250 if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
253 X509_LOOKUP_free(lu);
259 X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs, int type,
262 X509_OBJECT *ret = X509_OBJECT_new();
266 if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) {
267 X509_OBJECT_free(ret);
273 int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
274 X509_NAME *name, X509_OBJECT *ret)
276 X509_STORE *ctx = vs->ctx;
278 X509_OBJECT stmp, *tmp;
281 CRYPTO_THREAD_write_lock(ctx->lock);
282 tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
283 CRYPTO_THREAD_unlock(ctx->lock);
285 if (tmp == NULL || type == X509_LU_CRL) {
286 for (i = vs->current_method;
287 i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
288 lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
289 j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
291 vs->current_method = j;
298 vs->current_method = 0;
303 ret->type = tmp->type;
304 ret->data.ptr = tmp->data.ptr;
306 X509_OBJECT_up_ref_count(ret);
311 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
314 int ret = 1, added = 1;
318 obj = X509_OBJECT_new();
321 obj->type = X509_LU_X509;
323 X509_OBJECT_up_ref_count(obj);
325 CRYPTO_THREAD_write_lock(ctx->lock);
327 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
328 X509err(X509_F_X509_STORE_ADD_CERT,
329 X509_R_CERT_ALREADY_IN_HASH_TABLE);
332 added = sk_X509_OBJECT_push(ctx->objs, obj);
336 CRYPTO_THREAD_unlock(ctx->lock);
338 if (!ret) /* obj not pushed */
339 X509_OBJECT_free(obj);
340 if (!added) /* on push failure */
341 X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
346 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
349 int ret = 1, added = 1;
353 obj = X509_OBJECT_new();
356 obj->type = X509_LU_CRL;
358 X509_OBJECT_up_ref_count(obj);
360 CRYPTO_THREAD_write_lock(ctx->lock);
362 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
363 X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
366 added = sk_X509_OBJECT_push(ctx->objs, obj);
370 CRYPTO_THREAD_unlock(ctx->lock);
372 if (!ret) /* obj not pushed */
373 X509_OBJECT_free(obj);
374 if (!added) /* on push failure */
375 X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
380 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
386 return X509_up_ref(a->data.x509);
388 return X509_CRL_up_ref(a->data.crl);
393 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
395 if (a == NULL || a->type != X509_LU_X509)
400 X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
402 if (a == NULL || a->type != X509_LU_CRL)
407 int X509_OBJECT_get_type(const X509_OBJECT *a)
412 X509_OBJECT *X509_OBJECT_new()
414 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
417 X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
420 ret->type = X509_LU_FAIL;
425 void X509_OBJECT_free(X509_OBJECT *a)
433 X509_free(a->data.x509);
436 X509_CRL_free(a->data.crl);
442 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
443 X509_NAME *name, int *pnmatch)
453 stmp.data.x509 = &x509_s;
454 x509_s.cert_info.subject = name;
457 stmp.data.crl = &crl_s;
458 crl_s.crl.issuer = name;
465 idx = sk_X509_OBJECT_find(h, &stmp);
466 if (idx >= 0 && pnmatch) {
468 const X509_OBJECT *tobj, *pstmp;
471 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
472 tobj = sk_X509_OBJECT_value(h, tidx);
473 if (x509_object_cmp(&tobj, &pstmp))
481 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
484 return x509_object_idx_cnt(h, type, name, NULL);
487 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
488 int type, X509_NAME *name)
491 idx = X509_OBJECT_idx_by_subject(h, type, name);
494 return sk_X509_OBJECT_value(h, idx);
497 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
502 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
505 STACK_OF(X509) *sk = NULL;
509 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
510 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
513 * Nothing found in cache: do lookup to possibly add new objects to
516 X509_OBJECT *xobj = X509_OBJECT_new();
518 CRYPTO_THREAD_unlock(ctx->ctx->lock);
521 if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
522 X509_OBJECT_free(xobj);
525 X509_OBJECT_free(xobj);
526 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
527 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
529 CRYPTO_THREAD_unlock(ctx->ctx->lock);
534 sk = sk_X509_new_null();
535 for (i = 0; i < cnt; i++, idx++) {
536 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
539 if (!sk_X509_push(sk, x)) {
540 CRYPTO_THREAD_unlock(ctx->ctx->lock);
542 sk_X509_pop_free(sk, X509_free);
546 CRYPTO_THREAD_unlock(ctx->ctx->lock);
550 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
553 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
555 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
557 /* Always do lookup to possibly add new CRLs to cache */
558 if (sk == NULL || xobj == NULL ||
559 !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
560 X509_OBJECT_free(xobj);
561 sk_X509_CRL_free(sk);
564 X509_OBJECT_free(xobj);
565 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
566 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
568 CRYPTO_THREAD_unlock(ctx->ctx->lock);
569 sk_X509_CRL_free(sk);
573 for (i = 0; i < cnt; i++, idx++) {
574 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
577 if (!sk_X509_CRL_push(sk, x)) {
578 CRYPTO_THREAD_unlock(ctx->ctx->lock);
580 sk_X509_CRL_pop_free(sk, X509_CRL_free);
584 CRYPTO_THREAD_unlock(ctx->ctx->lock);
588 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
593 idx = sk_X509_OBJECT_find(h, x);
596 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
597 return sk_X509_OBJECT_value(h, idx);
598 for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
599 obj = sk_X509_OBJECT_value(h, i);
601 ((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x))
603 if (x->type == X509_LU_X509) {
604 if (!X509_cmp(obj->data.x509, x->data.x509))
606 } else if (x->type == X509_LU_CRL) {
607 if (!X509_CRL_match(obj->data.crl, x->data.crl))
616 * Try to get issuer certificate from store. Due to limitations
617 * of the API this can only retrieve a single certificate matching
618 * a given subject name. However it will fill the cache with all
619 * matching certificates, so we can examine the cache for all
623 * 1 lookup successful.
624 * 0 certificate not found.
625 * -1 some other error.
627 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
630 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
636 xn = X509_get_issuer_name(x);
637 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
638 if (ok != X509_LU_X509) {
639 X509_OBJECT_free(obj);
640 if (ok == X509_LU_RETRY) {
641 X509err(X509_F_X509_STORE_CTX_GET1_ISSUER, X509_R_SHOULD_RETRY);
644 if (ok != X509_LU_FAIL) {
645 /* not good :-(, break anyway */
650 /* If certificate matches all OK */
651 if (ctx->check_issued(ctx, x, obj->data.x509)) {
652 if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
653 *issuer = obj->data.x509;
654 X509_up_ref(*issuer);
655 X509_OBJECT_free(obj);
659 X509_OBJECT_free(obj);
661 /* Else find index of first cert accepted by 'check_issued' */
663 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
664 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
665 if (idx != -1) { /* should be true as we've had at least one
667 /* Look through all matching certs for suitable issuer */
668 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
669 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
670 /* See if we've run past the matches */
671 if (pobj->type != X509_LU_X509)
673 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
675 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
676 *issuer = pobj->data.x509;
679 * If times check, exit with match,
680 * otherwise keep looking. Leave last
681 * match in issuer so we return nearest
682 * match if no certificate time is OK.
685 if (x509_check_cert_time(ctx, *issuer, -1))
690 CRYPTO_THREAD_unlock(ctx->ctx->lock);
692 X509_up_ref(*issuer);
696 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
698 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
701 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
703 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
707 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
709 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
712 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
714 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
717 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
719 return X509_VERIFY_PARAM_set1(ctx->param, param);
722 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
727 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
729 ctx->verify = verify;
732 X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx)
737 void X509_STORE_set_verify_cb(X509_STORE *ctx,
738 X509_STORE_CTX_verify_cb verify_cb)
740 ctx->verify_cb = verify_cb;
743 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx)
745 return ctx->verify_cb;
748 void X509_STORE_set_get_issuer(X509_STORE *ctx,
749 X509_STORE_CTX_get_issuer_fn get_issuer)
751 ctx->get_issuer = get_issuer;
754 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx)
756 return ctx->get_issuer;
759 void X509_STORE_set_check_issued(X509_STORE *ctx,
760 X509_STORE_CTX_check_issued_fn check_issued)
762 ctx->check_issued = check_issued;
765 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx)
767 return ctx->check_issued;
770 void X509_STORE_set_check_revocation(X509_STORE *ctx,
771 X509_STORE_CTX_check_revocation_fn check_revocation)
773 ctx->check_revocation = check_revocation;
776 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx)
778 return ctx->check_revocation;
781 void X509_STORE_set_get_crl(X509_STORE *ctx,
782 X509_STORE_CTX_get_crl_fn get_crl)
784 ctx->get_crl = get_crl;
787 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx)
792 void X509_STORE_set_check_crl(X509_STORE *ctx,
793 X509_STORE_CTX_check_crl_fn check_crl)
795 ctx->check_crl = check_crl;
798 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx)
800 return ctx->check_crl;
803 void X509_STORE_set_cert_crl(X509_STORE *ctx,
804 X509_STORE_CTX_cert_crl_fn cert_crl)
806 ctx->cert_crl = cert_crl;
809 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx)
811 return ctx->cert_crl;
814 void X509_STORE_set_check_policy(X509_STORE *ctx,
815 X509_STORE_CTX_check_policy_fn check_policy)
817 ctx->check_policy = check_policy;
820 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx)
822 return ctx->check_policy;
825 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
826 X509_STORE_CTX_lookup_certs_fn lookup_certs)
828 ctx->lookup_certs = lookup_certs;
831 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx)
833 return ctx->lookup_certs;
836 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
837 X509_STORE_CTX_lookup_crls_fn lookup_crls)
839 ctx->lookup_crls = lookup_crls;
842 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx)
844 return ctx->lookup_crls;
847 void X509_STORE_set_cleanup(X509_STORE *ctx,
848 X509_STORE_CTX_cleanup_fn ctx_cleanup)
850 ctx->cleanup = ctx_cleanup;
853 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx)
858 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
860 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
863 void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
865 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
868 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)