2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <sys/types.h>
14 #include "internal/nelem.h"
15 #include "internal/o_dir.h"
16 #include <openssl/bio.h>
17 #include <openssl/pem.h>
18 #include <openssl/store.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/dh.h>
21 #include <openssl/bn.h>
22 #include <openssl/crypto.h>
23 #include "internal/refcount.h"
24 #include "ssl_local.h"
25 #include "ssl_cert_table.h"
26 #include "internal/thread_once.h"
28 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
29 int op, int bits, int nid, void *other,
32 static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
33 static volatile int ssl_x509_store_ctx_idx = -1;
35 DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
37 ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
38 "SSL for verify callback",
40 return ssl_x509_store_ctx_idx >= 0;
43 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
46 if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
48 return ssl_x509_store_ctx_idx;
51 CERT *ssl_cert_new(void)
53 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
58 ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
60 ret->sec_cb = ssl_security_default_callback;
61 ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
63 ret->lock = CRYPTO_THREAD_lock_new();
64 if (ret->lock == NULL) {
65 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
73 CERT *ssl_cert_dup(CERT *cert)
75 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
77 #ifndef OPENSSL_NO_COMP_ALG
85 ret->key = &ret->pkeys[cert->key - cert->pkeys];
86 ret->lock = CRYPTO_THREAD_lock_new();
87 if (ret->lock == NULL) {
88 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
93 if (cert->dh_tmp != NULL) {
94 ret->dh_tmp = cert->dh_tmp;
95 EVP_PKEY_up_ref(ret->dh_tmp);
98 ret->dh_tmp_cb = cert->dh_tmp_cb;
99 ret->dh_tmp_auto = cert->dh_tmp_auto;
101 for (i = 0; i < SSL_PKEY_NUM; i++) {
102 CERT_PKEY *cpk = cert->pkeys + i;
103 CERT_PKEY *rpk = ret->pkeys + i;
105 if (cpk->x509 != NULL) {
106 rpk->x509 = cpk->x509;
107 X509_up_ref(rpk->x509);
110 if (cpk->privatekey != NULL) {
111 rpk->privatekey = cpk->privatekey;
112 EVP_PKEY_up_ref(cpk->privatekey);
116 rpk->chain = X509_chain_up_ref(cpk->chain);
118 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
122 if (cpk->serverinfo != NULL) {
123 /* Just copy everything. */
124 rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length);
125 if (rpk->serverinfo == NULL)
127 rpk->serverinfo_length = cpk->serverinfo_length;
129 #ifndef OPENSSL_NO_COMP_ALG
130 for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) {
131 if (cpk->comp_cert[j] != NULL) {
132 if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j]))
134 rpk->comp_cert[j] = cpk->comp_cert[j];
140 /* Configured sigalgs copied across */
141 if (cert->conf_sigalgs) {
142 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
143 * sizeof(*cert->conf_sigalgs));
144 if (ret->conf_sigalgs == NULL)
146 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
147 cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
148 ret->conf_sigalgslen = cert->conf_sigalgslen;
150 ret->conf_sigalgs = NULL;
152 if (cert->client_sigalgs) {
153 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
154 * sizeof(*cert->client_sigalgs));
155 if (ret->client_sigalgs == NULL)
157 memcpy(ret->client_sigalgs, cert->client_sigalgs,
158 cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
159 ret->client_sigalgslen = cert->client_sigalgslen;
161 ret->client_sigalgs = NULL;
162 /* Copy any custom client certificate types */
164 ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
165 if (ret->ctype == NULL)
167 ret->ctype_len = cert->ctype_len;
170 ret->cert_flags = cert->cert_flags;
172 ret->cert_cb = cert->cert_cb;
173 ret->cert_cb_arg = cert->cert_cb_arg;
175 if (cert->verify_store) {
176 X509_STORE_up_ref(cert->verify_store);
177 ret->verify_store = cert->verify_store;
180 if (cert->chain_store) {
181 X509_STORE_up_ref(cert->chain_store);
182 ret->chain_store = cert->chain_store;
185 ret->sec_cb = cert->sec_cb;
186 ret->sec_level = cert->sec_level;
187 ret->sec_ex = cert->sec_ex;
189 if (!custom_exts_copy(&ret->custext, &cert->custext))
191 #ifndef OPENSSL_NO_PSK
192 if (cert->psk_identity_hint) {
193 ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
194 if (ret->psk_identity_hint == NULL)
206 /* Free up and clear all certificates and chains */
208 void ssl_cert_clear_certs(CERT *c)
211 #ifndef OPENSSL_NO_COMP_ALG
217 for (i = 0; i < SSL_PKEY_NUM; i++) {
218 CERT_PKEY *cpk = c->pkeys + i;
219 X509_free(cpk->x509);
221 EVP_PKEY_free(cpk->privatekey);
222 cpk->privatekey = NULL;
223 OSSL_STACK_OF_X509_free(cpk->chain);
225 OPENSSL_free(cpk->serverinfo);
226 cpk->serverinfo = NULL;
227 cpk->serverinfo_length = 0;
228 #ifndef OPENSSL_NO_COMP_ALG
229 for (j = 0; j < TLSEXT_comp_cert_limit; j++) {
230 OSSL_COMP_CERT_free(cpk->comp_cert[j]);
231 cpk->comp_cert[j] = NULL;
232 cpk->cert_comp_used = 0;
238 void ssl_cert_free(CERT *c)
244 CRYPTO_DOWN_REF(&c->references, &i, c->lock);
245 REF_PRINT_COUNT("CERT", c);
248 REF_ASSERT_ISNT(i < 0);
250 EVP_PKEY_free(c->dh_tmp);
252 ssl_cert_clear_certs(c);
253 OPENSSL_free(c->conf_sigalgs);
254 OPENSSL_free(c->client_sigalgs);
255 OPENSSL_free(c->ctype);
256 X509_STORE_free(c->verify_store);
257 X509_STORE_free(c->chain_store);
258 custom_exts_free(&c->custext);
259 #ifndef OPENSSL_NO_PSK
260 OPENSSL_free(c->psk_identity_hint);
262 CRYPTO_THREAD_lock_free(c->lock);
266 int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
269 CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
273 for (i = 0; i < sk_X509_num(chain); i++) {
274 X509 *x = sk_X509_value(chain, i);
276 r = ssl_security_cert(s, ctx, x, 0, 0);
278 ERR_raise(ERR_LIB_SSL, r);
282 OSSL_STACK_OF_X509_free(cpk->chain);
287 int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
289 STACK_OF(X509) *dchain;
291 return ssl_cert_set0_chain(s, ctx, NULL);
292 dchain = X509_chain_up_ref(chain);
295 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
296 OSSL_STACK_OF_X509_free(dchain);
302 int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
305 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
308 r = ssl_security_cert(s, ctx, x, 0, 0);
310 ERR_raise(ERR_LIB_SSL, r);
314 cpk->chain = sk_X509_new_null();
315 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
320 int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
322 if (!ssl_cert_add0_chain_cert(s, ctx, x))
328 int ssl_cert_select_current(CERT *c, X509 *x)
333 for (i = 0; i < SSL_PKEY_NUM; i++) {
334 CERT_PKEY *cpk = c->pkeys + i;
335 if (cpk->x509 == x && cpk->privatekey) {
341 for (i = 0; i < SSL_PKEY_NUM; i++) {
342 CERT_PKEY *cpk = c->pkeys + i;
343 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
351 int ssl_cert_set_current(CERT *c, long op)
356 if (op == SSL_CERT_SET_FIRST)
358 else if (op == SSL_CERT_SET_NEXT) {
359 idx = (int)(c->key - c->pkeys + 1);
360 if (idx >= SSL_PKEY_NUM)
364 for (i = idx; i < SSL_PKEY_NUM; i++) {
365 CERT_PKEY *cpk = c->pkeys + i;
366 if (cpk->x509 && cpk->privatekey) {
374 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
377 c->cert_cb_arg = arg;
381 * Verify a certificate chain
384 * 0: Verify failure or error
387 int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk)
391 X509_STORE *verify_store;
392 X509_STORE_CTX *ctx = NULL;
393 X509_VERIFY_PARAM *param;
396 if ((sk == NULL) || (sk_X509_num(sk) == 0))
399 sctx = SSL_CONNECTION_GET_CTX(s);
400 if (s->cert->verify_store)
401 verify_store = s->cert->verify_store;
403 verify_store = sctx->cert_store;
405 ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
407 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
411 x = sk_X509_value(sk, 0);
412 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
413 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
416 param = X509_STORE_CTX_get0_param(ctx);
418 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
419 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
420 * and PKI authentication.
422 X509_VERIFY_PARAM_set_auth_level(param,
423 SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
425 /* Set suite B flags if needed */
426 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
427 if (!X509_STORE_CTX_set_ex_data(ctx,
428 SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
432 /* Verify via DANE if enabled */
433 if (DANETLS_ENABLED(&s->dane))
434 X509_STORE_CTX_set0_dane(ctx, &s->dane);
437 * We need to inherit the verify parameters. These can be determined by
438 * the context: if its a server it will verify SSL client certificates or
442 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
444 * Anything non-default in "s->param" should overwrite anything in the ctx.
446 X509_VERIFY_PARAM_set1(param, s->param);
448 if (s->verify_callback)
449 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
451 if (sctx->app_verify_callback != NULL) {
452 i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
454 i = X509_verify_cert(ctx);
455 /* We treat an error in the same way as a failure to verify */
460 s->verify_result = X509_STORE_CTX_get_error(ctx);
461 OSSL_STACK_OF_X509_free(s->verified_chain);
462 s->verified_chain = NULL;
463 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
464 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
465 if (s->verified_chain == NULL) {
466 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
471 /* Move peername from the store context params to the SSL handle's */
472 X509_VERIFY_PARAM_move_peername(s->param, param);
475 X509_STORE_CTX_free(ctx);
479 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
480 STACK_OF(X509_NAME) *name_list)
482 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
483 *ca_list = name_list;
486 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
489 const int num = sk_X509_NAME_num(sk);
490 STACK_OF(X509_NAME) *ret;
493 ret = sk_X509_NAME_new_reserve(NULL, num);
495 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
498 for (i = 0; i < num; i++) {
499 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
501 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
502 sk_X509_NAME_pop_free(ret, X509_NAME_free);
505 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
510 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
512 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
517 set0_CA_list(&sc->ca_names, name_list);
520 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
522 set0_CA_list(&ctx->ca_names, name_list);
525 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
527 return ctx->ca_names;
530 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
532 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
537 return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
540 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
542 set0_CA_list(&ctx->client_ca_names, name_list);
545 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
547 return ctx->client_ca_names;
550 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
552 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
557 set0_CA_list(&sc->client_ca_names, name_list);
560 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
562 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
567 return sc->s3.tmp.peer_ca_names;
570 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
572 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
578 return sc->s3.tmp.peer_ca_names;
579 return sc->client_ca_names != NULL ? sc->client_ca_names
580 : s->ctx->client_ca_names;
583 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
589 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
592 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
595 if (!sk_X509_NAME_push(*sk, name)) {
596 X509_NAME_free(name);
602 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
604 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
609 return add_ca_name(&sc->ca_names, x);
612 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
614 return add_ca_name(&ctx->ca_names, x);
618 * The following two are older names are to be replaced with
619 * SSL(_CTX)_add1_to_CA_list
621 int SSL_add_client_CA(SSL *ssl, X509 *x)
623 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
628 return add_ca_name(&sc->client_ca_names, x);
631 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
633 return add_ca_name(&ctx->client_ca_names, x);
636 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
638 unsigned char *abuf = NULL, *bbuf = NULL;
641 /* X509_NAME_cmp() itself casts away constness in this way, so
644 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
645 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
647 if (alen < 0 || blen < 0)
649 else if (alen != blen)
651 else /* alen == blen */
652 ret = memcmp(abuf, bbuf, alen);
660 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
662 return xname_cmp(*a, *b);
665 static unsigned long xname_hash(const X509_NAME *a)
667 /* This returns 0 also if SHA1 is not available */
668 return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
671 STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
672 OSSL_LIB_CTX *libctx,
675 BIO *in = BIO_new(BIO_s_file());
677 X509_NAME *xn = NULL;
678 STACK_OF(X509_NAME) *ret = NULL;
679 LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
680 OSSL_LIB_CTX *prev_libctx = NULL;
682 if (name_hash == NULL) {
683 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
687 ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
691 x = X509_new_ex(libctx, propq);
693 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
696 if (BIO_read_filename(in, file) <= 0)
699 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
700 prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
702 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
705 ret = sk_X509_NAME_new_null();
707 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
711 if ((xn = X509_get_subject_name(x)) == NULL)
713 /* check for duplicates */
714 xn = X509_NAME_dup(xn);
717 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
722 lh_X509_NAME_insert(name_hash, xn);
723 if (!sk_X509_NAME_push(ret, xn))
731 sk_X509_NAME_pop_free(ret, X509_NAME_free);
734 /* restore the old libctx */
735 OSSL_LIB_CTX_set0_default(prev_libctx);
738 lh_X509_NAME_free(name_hash);
744 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
746 return SSL_load_client_CA_file_ex(file, NULL, NULL);
749 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
754 X509_NAME *xn = NULL;
756 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
758 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
760 in = BIO_new(BIO_s_file());
763 ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
767 if (BIO_read_filename(in, file) <= 0)
771 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
773 if ((xn = X509_get_subject_name(x)) == NULL)
775 xn = X509_NAME_dup(xn);
778 if (sk_X509_NAME_find(stack, xn) >= 0) {
781 } else if (!sk_X509_NAME_push(stack, xn)) {
795 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
799 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
802 OPENSSL_DIR_CTX *d = NULL;
803 const char *filename;
806 /* Note that a side effect is that the CAs will be sorted by name */
808 while ((filename = OPENSSL_DIR_read(&d, dir))) {
812 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
813 ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
816 #ifdef OPENSSL_SYS_VMS
817 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
819 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
821 if (r <= 0 || r >= (int)sizeof(buf))
823 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
828 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
829 "calling OPENSSL_dir_read(%s)", dir);
830 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
843 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
844 const char *uri, int depth)
847 OSSL_STORE_CTX *ctx = NULL;
849 X509_NAME *xn = NULL;
851 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
854 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
855 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
856 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
861 if (infotype == OSSL_STORE_INFO_NAME) {
863 * This is an entry in the "directory" represented by the current
864 * uri. if |depth| allows, dive into it.
867 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
869 } else if (infotype == OSSL_STORE_INFO_CERT) {
870 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
871 || (xn = X509_get_subject_name(x)) == NULL
872 || (xn = X509_NAME_dup(xn)) == NULL)
874 if (sk_X509_NAME_find(stack, xn) >= 0) {
877 } else if (!sk_X509_NAME_push(stack, xn)) {
883 OSSL_STORE_INFO_free(info);
892 OSSL_STORE_close(ctx);
897 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
900 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
901 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
902 int ret = add_uris_recursive(stack, store, 1);
904 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
908 /* Build a certificate chain for current certificate */
909 int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
911 CERT *c = s != NULL ? s->cert : ctx->cert;
912 CERT_PKEY *cpk = c->key;
913 X509_STORE *chain_store = NULL;
914 X509_STORE_CTX *xs_ctx = NULL;
915 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
917 SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
920 if (cpk->x509 == NULL) {
921 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
924 /* Rearranging and check the chain: add everything to a store */
925 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
926 chain_store = X509_STORE_new();
927 if (chain_store == NULL)
929 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
930 x = sk_X509_value(cpk->chain, i);
931 if (!X509_STORE_add_cert(chain_store, x))
934 /* Add EE cert too: it might be self signed */
935 if (!X509_STORE_add_cert(chain_store, cpk->x509))
938 if (c->chain_store != NULL)
939 chain_store = c->chain_store;
941 chain_store = real_ctx->cert_store;
943 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
944 untrusted = cpk->chain;
947 xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
948 if (xs_ctx == NULL) {
949 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
952 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
953 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
956 /* Set suite B flags if needed */
957 X509_STORE_CTX_set_flags(xs_ctx,
958 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
960 i = X509_verify_cert(xs_ctx);
961 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
962 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
968 chain = X509_STORE_CTX_get1_chain(xs_ctx);
970 i = X509_STORE_CTX_get_error(xs_ctx);
971 ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
972 "Verify error:%s", X509_verify_cert_error_string(i));
976 /* Remove EE certificate from chain */
977 x = sk_X509_shift(chain);
979 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
980 if (sk_X509_num(chain) > 0) {
981 /* See if last cert is self signed */
982 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
983 if (X509_get_extension_flags(x) & EXFLAG_SS) {
984 x = sk_X509_pop(chain);
990 * Check security level of all CA certificates: EE will have been checked
993 for (i = 0; i < sk_X509_num(chain); i++) {
994 x = sk_X509_value(chain, i);
995 rv = ssl_security_cert(s, ctx, x, 0, 0);
997 ERR_raise(ERR_LIB_SSL, rv);
998 OSSL_STACK_OF_X509_free(chain);
1003 OSSL_STACK_OF_X509_free(cpk->chain);
1008 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1009 X509_STORE_free(chain_store);
1010 X509_STORE_CTX_free(xs_ctx);
1015 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1017 X509_STORE **pstore;
1019 pstore = &c->chain_store;
1021 pstore = &c->verify_store;
1022 X509_STORE_free(*pstore);
1025 X509_STORE_up_ref(store);
1029 int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
1031 *pstore = (chain ? c->chain_store : c->verify_store);
1035 int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
1039 * note that there's a corresponding minbits_table
1040 * in crypto/x509/x509_vfy.c that's used for checking the security level
1041 * of RSA and DSA keys
1043 static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
1046 level = SSL_CTX_get_security_level(ctx);
1048 level = SSL_get_security_level(s);
1058 return minbits_table[level];
1061 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
1062 int op, int bits, int nid, void *other,
1065 int level, minbits, pfs_mask;
1066 const SSL_CONNECTION *sc;
1068 minbits = ssl_get_security_level_bits(s, ctx, &level);
1072 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
1075 if (op == SSL_SECOP_TMP_DH && bits < 80)
1080 case SSL_SECOP_CIPHER_SUPPORTED:
1081 case SSL_SECOP_CIPHER_SHARED:
1082 case SSL_SECOP_CIPHER_CHECK:
1084 const SSL_CIPHER *c = other;
1085 /* No ciphers below security level */
1088 /* No unauthenticated ciphersuites */
1089 if (c->algorithm_auth & SSL_aNULL)
1091 /* No MD5 mac ciphersuites */
1092 if (c->algorithm_mac & SSL_MD5)
1094 /* SHA1 HMAC is 160 bits of security */
1095 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1097 /* Level 3: forward secure ciphersuites only */
1098 pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
1099 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1100 !(c->algorithm_mkey & pfs_mask))
1104 case SSL_SECOP_VERSION:
1105 if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
1107 if (!SSL_CONNECTION_IS_DTLS(sc)) {
1108 /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
1109 if (nid <= TLS1_1_VERSION && level > 0)
1112 /* DTLS v1.0 only allowed at level 0 */
1113 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
1118 case SSL_SECOP_COMPRESSION:
1122 case SSL_SECOP_TICKET:
1133 int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
1135 return s->cert->sec_cb(SSL_CONNECTION_GET_SSL(s), NULL, op, bits, nid,
1136 other, s->cert->sec_ex);
1139 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1141 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1145 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1149 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1150 if (ssl_cert_info[i].nid == nid) {
1159 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1163 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1164 const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
1166 if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1167 || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1177 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1179 if (idx >= OSSL_NELEM(ssl_cert_info))
1181 return &ssl_cert_info[idx];