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_CTX_get_obj_by_subject(X509_STORE_CTX *vs, int type,
252 X509_OBJECT *ret = X509_OBJECT_new();
256 if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) {
257 X509_OBJECT_free(ret);
263 int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
264 X509_NAME *name, X509_OBJECT *ret)
266 X509_STORE *ctx = vs->ctx;
268 X509_OBJECT stmp, *tmp;
271 CRYPTO_THREAD_write_lock(ctx->lock);
272 tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
273 CRYPTO_THREAD_unlock(ctx->lock);
275 if (tmp == NULL || type == X509_LU_CRL) {
276 for (i = vs->current_method;
277 i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
278 lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
279 j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
281 vs->current_method = j;
288 vs->current_method = 0;
293 ret->type = tmp->type;
294 ret->data.ptr = tmp->data.ptr;
296 X509_OBJECT_up_ref_count(ret);
301 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
304 int ret = 1, added = 1;
308 obj = X509_OBJECT_new();
311 obj->type = X509_LU_X509;
313 X509_OBJECT_up_ref_count(obj);
315 CRYPTO_THREAD_write_lock(ctx->lock);
317 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
318 X509err(X509_F_X509_STORE_ADD_CERT,
319 X509_R_CERT_ALREADY_IN_HASH_TABLE);
322 added = sk_X509_OBJECT_push(ctx->objs, obj);
326 CRYPTO_THREAD_unlock(ctx->lock);
328 if (!ret) /* obj not pushed */
329 X509_OBJECT_free(obj);
330 if (!added) /* on push failure */
331 X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
336 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
339 int ret = 1, added = 1;
343 obj = X509_OBJECT_new();
346 obj->type = X509_LU_CRL;
348 X509_OBJECT_up_ref_count(obj);
350 CRYPTO_THREAD_write_lock(ctx->lock);
352 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
353 X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
356 added = sk_X509_OBJECT_push(ctx->objs, obj);
360 CRYPTO_THREAD_unlock(ctx->lock);
362 if (!ret) /* obj not pushed */
363 X509_OBJECT_free(obj);
364 if (!added) /* on push failure */
365 X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
370 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
376 return X509_up_ref(a->data.x509);
378 return X509_CRL_up_ref(a->data.crl);
383 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
385 if (a == NULL || a->type != X509_LU_X509)
390 X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
392 if (a == NULL || a->type != X509_LU_CRL)
397 int X509_OBJECT_get_type(const X509_OBJECT *a)
402 X509_OBJECT *X509_OBJECT_new()
404 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
407 X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
410 ret->type = X509_LU_FAIL;
415 void X509_OBJECT_free(X509_OBJECT *a)
423 X509_free(a->data.x509);
426 X509_CRL_free(a->data.crl);
432 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
433 X509_NAME *name, int *pnmatch)
443 stmp.data.x509 = &x509_s;
444 x509_s.cert_info.subject = name;
447 stmp.data.crl = &crl_s;
448 crl_s.crl.issuer = name;
455 idx = sk_X509_OBJECT_find(h, &stmp);
456 if (idx >= 0 && pnmatch) {
458 const X509_OBJECT *tobj, *pstmp;
461 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
462 tobj = sk_X509_OBJECT_value(h, tidx);
463 if (x509_object_cmp(&tobj, &pstmp))
471 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
474 return x509_object_idx_cnt(h, type, name, NULL);
477 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
478 int type, X509_NAME *name)
481 idx = X509_OBJECT_idx_by_subject(h, type, name);
484 return sk_X509_OBJECT_value(h, idx);
487 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
492 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
495 STACK_OF(X509) *sk = NULL;
499 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
500 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
503 * Nothing found in cache: do lookup to possibly add new objects to
506 X509_OBJECT *xobj = X509_OBJECT_new();
508 CRYPTO_THREAD_unlock(ctx->ctx->lock);
511 if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
512 X509_OBJECT_free(xobj);
515 X509_OBJECT_free(xobj);
516 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
517 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
519 CRYPTO_THREAD_unlock(ctx->ctx->lock);
524 sk = sk_X509_new_null();
525 for (i = 0; i < cnt; i++, idx++) {
526 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
529 if (!sk_X509_push(sk, x)) {
530 CRYPTO_THREAD_unlock(ctx->ctx->lock);
532 sk_X509_pop_free(sk, X509_free);
536 CRYPTO_THREAD_unlock(ctx->ctx->lock);
540 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
543 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
545 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
547 /* Always do lookup to possibly add new CRLs to cache */
548 if (sk == NULL || xobj == NULL ||
549 !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
550 X509_OBJECT_free(xobj);
551 sk_X509_CRL_free(sk);
554 X509_OBJECT_free(xobj);
555 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
556 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
558 CRYPTO_THREAD_unlock(ctx->ctx->lock);
559 sk_X509_CRL_free(sk);
563 for (i = 0; i < cnt; i++, idx++) {
564 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
567 if (!sk_X509_CRL_push(sk, x)) {
568 CRYPTO_THREAD_unlock(ctx->ctx->lock);
570 sk_X509_CRL_pop_free(sk, X509_CRL_free);
574 CRYPTO_THREAD_unlock(ctx->ctx->lock);
578 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
583 idx = sk_X509_OBJECT_find(h, x);
586 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
587 return sk_X509_OBJECT_value(h, idx);
588 for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
589 obj = sk_X509_OBJECT_value(h, i);
591 ((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x))
593 if (x->type == X509_LU_X509) {
594 if (!X509_cmp(obj->data.x509, x->data.x509))
596 } else if (x->type == X509_LU_CRL) {
597 if (!X509_CRL_match(obj->data.crl, x->data.crl))
606 * Try to get issuer certificate from store. Due to limitations
607 * of the API this can only retrieve a single certificate matching
608 * a given subject name. However it will fill the cache with all
609 * matching certificates, so we can examine the cache for all
613 * 1 lookup successful.
614 * 0 certificate not found.
615 * -1 some other error.
617 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
620 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
626 xn = X509_get_issuer_name(x);
627 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
628 if (ok != X509_LU_X509) {
629 X509_OBJECT_free(obj);
630 if (ok == X509_LU_RETRY) {
631 X509err(X509_F_X509_STORE_CTX_GET1_ISSUER, X509_R_SHOULD_RETRY);
634 if (ok != X509_LU_FAIL) {
635 /* not good :-(, break anyway */
640 /* If certificate matches all OK */
641 if (ctx->check_issued(ctx, x, obj->data.x509)) {
642 if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
643 *issuer = obj->data.x509;
644 X509_up_ref(*issuer);
645 X509_OBJECT_free(obj);
649 X509_OBJECT_free(obj);
651 /* Else find index of first cert accepted by 'check_issued' */
653 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
654 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
655 if (idx != -1) { /* should be true as we've had at least one
657 /* Look through all matching certs for suitable issuer */
658 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
659 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
660 /* See if we've run past the matches */
661 if (pobj->type != X509_LU_X509)
663 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
665 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
666 *issuer = pobj->data.x509;
669 * If times check, exit with match,
670 * otherwise keep looking. Leave last
671 * match in issuer so we return nearest
672 * match if no certificate time is OK.
675 if (x509_check_cert_time(ctx, *issuer, -1))
680 CRYPTO_THREAD_unlock(ctx->ctx->lock);
682 X509_up_ref(*issuer);
686 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
688 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
691 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
693 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
697 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
699 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
702 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
704 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
707 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
709 return X509_VERIFY_PARAM_set1(ctx->param, param);
712 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
717 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
719 ctx->verify = verify;
722 X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx)
727 void X509_STORE_set_verify_cb(X509_STORE *ctx,
728 X509_STORE_CTX_verify_cb verify_cb)
730 ctx->verify_cb = verify_cb;
733 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx)
735 return ctx->verify_cb;
738 void X509_STORE_set_get_issuer(X509_STORE *ctx,
739 X509_STORE_CTX_get_issuer_fn get_issuer)
741 ctx->get_issuer = get_issuer;
744 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx)
746 return ctx->get_issuer;
749 void X509_STORE_set_check_issued(X509_STORE *ctx,
750 X509_STORE_CTX_check_issued_fn check_issued)
752 ctx->check_issued = check_issued;
755 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx)
757 return ctx->check_issued;
760 void X509_STORE_set_check_revocation(X509_STORE *ctx,
761 X509_STORE_CTX_check_revocation_fn check_revocation)
763 ctx->check_revocation = check_revocation;
766 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx)
768 return ctx->check_revocation;
771 void X509_STORE_set_get_crl(X509_STORE *ctx,
772 X509_STORE_CTX_get_crl_fn get_crl)
774 ctx->get_crl = get_crl;
777 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx)
782 void X509_STORE_set_check_crl(X509_STORE *ctx,
783 X509_STORE_CTX_check_crl_fn check_crl)
785 ctx->check_crl = check_crl;
788 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx)
790 return ctx->check_crl;
793 void X509_STORE_set_cert_crl(X509_STORE *ctx,
794 X509_STORE_CTX_cert_crl_fn cert_crl)
796 ctx->cert_crl = cert_crl;
799 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx)
801 return ctx->cert_crl;
804 void X509_STORE_set_check_policy(X509_STORE *ctx,
805 X509_STORE_CTX_check_policy_fn check_policy)
807 ctx->check_policy = check_policy;
810 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx)
812 return ctx->check_policy;
815 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
816 X509_STORE_CTX_lookup_certs_fn lookup_certs)
818 ctx->lookup_certs = lookup_certs;
821 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx)
823 return ctx->lookup_certs;
826 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
827 X509_STORE_CTX_lookup_crls_fn lookup_crls)
829 ctx->lookup_crls = lookup_crls;
832 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx)
834 return ctx->lookup_crls;
837 void X509_STORE_set_cleanup(X509_STORE *ctx,
838 X509_STORE_CTX_cleanup_fn ctx_cleanup)
840 ctx->cleanup = ctx_cleanup;
843 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx)
848 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
850 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
853 void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
855 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
858 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)