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)
308 obj = X509_OBJECT_new();
311 obj->type = X509_LU_X509;
314 CRYPTO_THREAD_write_lock(ctx->lock);
316 X509_OBJECT_up_ref_count(obj);
318 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
319 X509_OBJECT_free(obj);
320 X509err(X509_F_X509_STORE_ADD_CERT,
321 X509_R_CERT_ALREADY_IN_HASH_TABLE);
324 sk_X509_OBJECT_push(ctx->objs, obj);
326 CRYPTO_THREAD_unlock(ctx->lock);
331 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
338 obj = X509_OBJECT_new();
341 obj->type = X509_LU_CRL;
344 CRYPTO_THREAD_write_lock(ctx->lock);
346 X509_OBJECT_up_ref_count(obj);
348 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
349 X509_OBJECT_free(obj);
350 X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
353 sk_X509_OBJECT_push(ctx->objs, obj);
355 CRYPTO_THREAD_unlock(ctx->lock);
360 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
366 return X509_up_ref(a->data.x509);
368 return X509_CRL_up_ref(a->data.crl);
373 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
375 if (a == NULL || a->type != X509_LU_X509)
380 X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a)
382 if (a == NULL || a->type != X509_LU_CRL)
387 int X509_OBJECT_get_type(const X509_OBJECT *a)
392 X509_OBJECT *X509_OBJECT_new()
394 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
397 X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
400 ret->type = X509_LU_FAIL;
405 void X509_OBJECT_free(X509_OBJECT *a)
413 X509_free(a->data.x509);
416 X509_CRL_free(a->data.crl);
422 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
423 X509_NAME *name, int *pnmatch)
433 stmp.data.x509 = &x509_s;
434 x509_s.cert_info.subject = name;
437 stmp.data.crl = &crl_s;
438 crl_s.crl.issuer = name;
445 idx = sk_X509_OBJECT_find(h, &stmp);
446 if (idx >= 0 && pnmatch) {
448 const X509_OBJECT *tobj, *pstmp;
451 for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
452 tobj = sk_X509_OBJECT_value(h, tidx);
453 if (x509_object_cmp(&tobj, &pstmp))
461 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
464 return x509_object_idx_cnt(h, type, name, NULL);
467 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
468 int type, X509_NAME *name)
471 idx = X509_OBJECT_idx_by_subject(h, type, name);
474 return sk_X509_OBJECT_value(h, idx);
477 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v)
482 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)
485 STACK_OF(X509) *sk = NULL;
489 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
490 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
493 * Nothing found in cache: do lookup to possibly add new objects to
496 X509_OBJECT *xobj = X509_OBJECT_new();
498 CRYPTO_THREAD_unlock(ctx->ctx->lock);
501 if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
502 X509_OBJECT_free(xobj);
505 X509_OBJECT_free(xobj);
506 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
507 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
509 CRYPTO_THREAD_unlock(ctx->ctx->lock);
514 sk = sk_X509_new_null();
515 for (i = 0; i < cnt; i++, idx++) {
516 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
519 if (!sk_X509_push(sk, x)) {
520 CRYPTO_THREAD_unlock(ctx->ctx->lock);
522 sk_X509_pop_free(sk, X509_free);
526 CRYPTO_THREAD_unlock(ctx->ctx->lock);
530 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm)
533 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
535 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
537 /* Always do lookup to possibly add new CRLs to cache */
538 if (sk == NULL || xobj == NULL ||
539 !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
540 X509_OBJECT_free(xobj);
541 sk_X509_CRL_free(sk);
544 X509_OBJECT_free(xobj);
545 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
546 idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
548 CRYPTO_THREAD_unlock(ctx->ctx->lock);
549 sk_X509_CRL_free(sk);
553 for (i = 0; i < cnt; i++, idx++) {
554 obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
557 if (!sk_X509_CRL_push(sk, x)) {
558 CRYPTO_THREAD_unlock(ctx->ctx->lock);
560 sk_X509_CRL_pop_free(sk, X509_CRL_free);
564 CRYPTO_THREAD_unlock(ctx->ctx->lock);
568 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
573 idx = sk_X509_OBJECT_find(h, x);
576 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
577 return sk_X509_OBJECT_value(h, idx);
578 for (i = idx; i < sk_X509_OBJECT_num(h); i++) {
579 obj = sk_X509_OBJECT_value(h, i);
581 ((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x))
583 if (x->type == X509_LU_X509) {
584 if (!X509_cmp(obj->data.x509, x->data.x509))
586 } else if (x->type == X509_LU_CRL) {
587 if (!X509_CRL_match(obj->data.crl, x->data.crl))
596 * Try to get issuer certificate from store. Due to limitations
597 * of the API this can only retrieve a single certificate matching
598 * a given subject name. However it will fill the cache with all
599 * matching certificates, so we can examine the cache for all
603 * 1 lookup successful.
604 * 0 certificate not found.
605 * -1 some other error.
607 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
610 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
616 xn = X509_get_issuer_name(x);
617 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
618 if (ok != X509_LU_X509) {
619 X509_OBJECT_free(obj);
620 if (ok == X509_LU_RETRY) {
621 X509err(X509_F_X509_STORE_CTX_GET1_ISSUER, X509_R_SHOULD_RETRY);
624 if (ok != X509_LU_FAIL) {
625 /* not good :-(, break anyway */
630 /* If certificate matches all OK */
631 if (ctx->check_issued(ctx, x, obj->data.x509)) {
632 if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
633 *issuer = obj->data.x509;
634 X509_up_ref(*issuer);
635 X509_OBJECT_free(obj);
639 X509_OBJECT_free(obj);
641 /* Else find index of first cert accepted by 'check_issued' */
643 CRYPTO_THREAD_write_lock(ctx->ctx->lock);
644 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
645 if (idx != -1) { /* should be true as we've had at least one
647 /* Look through all matching certs for suitable issuer */
648 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
649 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
650 /* See if we've run past the matches */
651 if (pobj->type != X509_LU_X509)
653 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
655 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
656 *issuer = pobj->data.x509;
659 * If times check, exit with match,
660 * otherwise keep looking. Leave last
661 * match in issuer so we return nearest
662 * match if no certificate time is OK.
665 if (x509_check_cert_time(ctx, *issuer, -1))
670 CRYPTO_THREAD_unlock(ctx->ctx->lock);
672 X509_up_ref(*issuer);
676 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
678 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
681 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
683 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
687 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
689 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
692 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
694 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
697 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param)
699 return X509_VERIFY_PARAM_set1(ctx->param, param);
702 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx)
707 void X509_STORE_set_verify_cb(X509_STORE *ctx,
708 int (*verify_cb) (int, X509_STORE_CTX *))
710 ctx->verify_cb = verify_cb;
713 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify verify)
715 ctx->verify = verify;
718 void X509_STORE_set_lookup_crls_cb(X509_STORE *ctx,
719 STACK_OF(X509_CRL) *(*cb) (X509_STORE_CTX
723 ctx->lookup_crls = cb;
726 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
728 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
731 void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx)
733 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
736 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx)