2 * Copyright 1995-2020 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));
56 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
60 ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
62 ret->sec_cb = ssl_security_default_callback;
63 ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
65 ret->lock = CRYPTO_THREAD_lock_new();
66 if (ret->lock == NULL) {
67 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
75 CERT *ssl_cert_dup(CERT *cert)
77 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
81 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
86 ret->key = &ret->pkeys[cert->key - cert->pkeys];
87 ret->lock = CRYPTO_THREAD_lock_new();
88 if (ret->lock == NULL) {
89 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
94 if (cert->dh_tmp != NULL) {
95 ret->dh_tmp = cert->dh_tmp;
96 EVP_PKEY_up_ref(ret->dh_tmp);
99 ret->dh_tmp_cb = cert->dh_tmp_cb;
101 ret->dh_tmp_auto = cert->dh_tmp_auto;
103 for (i = 0; i < SSL_PKEY_NUM; i++) {
104 CERT_PKEY *cpk = cert->pkeys + i;
105 CERT_PKEY *rpk = ret->pkeys + i;
106 if (cpk->x509 != NULL) {
107 rpk->x509 = cpk->x509;
108 X509_up_ref(rpk->x509);
111 if (cpk->privatekey != NULL) {
112 rpk->privatekey = cpk->privatekey;
113 EVP_PKEY_up_ref(cpk->privatekey);
117 rpk->chain = X509_chain_up_ref(cpk->chain);
119 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
123 if (cert->pkeys[i].serverinfo != NULL) {
124 /* Just copy everything. */
125 ret->pkeys[i].serverinfo =
126 OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
127 if (ret->pkeys[i].serverinfo == NULL) {
128 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
131 ret->pkeys[i].serverinfo_length = cert->pkeys[i].serverinfo_length;
132 memcpy(ret->pkeys[i].serverinfo,
133 cert->pkeys[i].serverinfo, cert->pkeys[i].serverinfo_length);
137 /* Configured sigalgs copied across */
138 if (cert->conf_sigalgs) {
139 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
140 * sizeof(*cert->conf_sigalgs));
141 if (ret->conf_sigalgs == NULL)
143 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
144 cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
145 ret->conf_sigalgslen = cert->conf_sigalgslen;
147 ret->conf_sigalgs = NULL;
149 if (cert->client_sigalgs) {
150 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
151 * sizeof(*cert->client_sigalgs));
152 if (ret->client_sigalgs == NULL)
154 memcpy(ret->client_sigalgs, cert->client_sigalgs,
155 cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
156 ret->client_sigalgslen = cert->client_sigalgslen;
158 ret->client_sigalgs = NULL;
159 /* Copy any custom client certificate types */
161 ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
162 if (ret->ctype == NULL)
164 ret->ctype_len = cert->ctype_len;
167 ret->cert_flags = cert->cert_flags;
169 ret->cert_cb = cert->cert_cb;
170 ret->cert_cb_arg = cert->cert_cb_arg;
172 if (cert->verify_store) {
173 X509_STORE_up_ref(cert->verify_store);
174 ret->verify_store = cert->verify_store;
177 if (cert->chain_store) {
178 X509_STORE_up_ref(cert->chain_store);
179 ret->chain_store = cert->chain_store;
182 ret->sec_cb = cert->sec_cb;
183 ret->sec_level = cert->sec_level;
184 ret->sec_ex = cert->sec_ex;
186 if (!custom_exts_copy(&ret->custext, &cert->custext))
188 #ifndef OPENSSL_NO_PSK
189 if (cert->psk_identity_hint) {
190 ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
191 if (ret->psk_identity_hint == NULL)
203 /* Free up and clear all certificates and chains */
205 void ssl_cert_clear_certs(CERT *c)
210 for (i = 0; i < SSL_PKEY_NUM; i++) {
211 CERT_PKEY *cpk = c->pkeys + i;
212 X509_free(cpk->x509);
214 EVP_PKEY_free(cpk->privatekey);
215 cpk->privatekey = NULL;
216 sk_X509_pop_free(cpk->chain, X509_free);
218 OPENSSL_free(cpk->serverinfo);
219 cpk->serverinfo = NULL;
220 cpk->serverinfo_length = 0;
224 void ssl_cert_free(CERT *c)
230 CRYPTO_DOWN_REF(&c->references, &i, c->lock);
231 REF_PRINT_COUNT("CERT", c);
234 REF_ASSERT_ISNT(i < 0);
236 EVP_PKEY_free(c->dh_tmp);
238 ssl_cert_clear_certs(c);
239 OPENSSL_free(c->conf_sigalgs);
240 OPENSSL_free(c->client_sigalgs);
241 OPENSSL_free(c->ctype);
242 X509_STORE_free(c->verify_store);
243 X509_STORE_free(c->chain_store);
244 custom_exts_free(&c->custext);
245 #ifndef OPENSSL_NO_PSK
246 OPENSSL_free(c->psk_identity_hint);
248 CRYPTO_THREAD_lock_free(c->lock);
252 int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
255 CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
259 for (i = 0; i < sk_X509_num(chain); i++) {
260 X509 *x = sk_X509_value(chain, i);
262 r = ssl_security_cert(s, ctx, x, 0, 0);
264 ERR_raise(ERR_LIB_SSL, r);
268 sk_X509_pop_free(cpk->chain, X509_free);
273 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
275 STACK_OF(X509) *dchain;
277 return ssl_cert_set0_chain(s, ctx, NULL);
278 dchain = X509_chain_up_ref(chain);
281 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
282 sk_X509_pop_free(dchain, X509_free);
288 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
291 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
294 r = ssl_security_cert(s, ctx, x, 0, 0);
296 ERR_raise(ERR_LIB_SSL, r);
300 cpk->chain = sk_X509_new_null();
301 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
306 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
308 if (!ssl_cert_add0_chain_cert(s, ctx, x))
314 int ssl_cert_select_current(CERT *c, X509 *x)
319 for (i = 0; i < SSL_PKEY_NUM; i++) {
320 CERT_PKEY *cpk = c->pkeys + i;
321 if (cpk->x509 == x && cpk->privatekey) {
327 for (i = 0; i < SSL_PKEY_NUM; i++) {
328 CERT_PKEY *cpk = c->pkeys + i;
329 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
337 int ssl_cert_set_current(CERT *c, long op)
342 if (op == SSL_CERT_SET_FIRST)
344 else if (op == SSL_CERT_SET_NEXT) {
345 idx = (int)(c->key - c->pkeys + 1);
346 if (idx >= SSL_PKEY_NUM)
350 for (i = idx; i < SSL_PKEY_NUM; i++) {
351 CERT_PKEY *cpk = c->pkeys + i;
352 if (cpk->x509 && cpk->privatekey) {
360 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
363 c->cert_cb_arg = arg;
366 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
370 X509_STORE *verify_store;
371 X509_STORE_CTX *ctx = NULL;
372 X509_VERIFY_PARAM *param;
374 if ((sk == NULL) || (sk_X509_num(sk) == 0))
377 if (s->cert->verify_store)
378 verify_store = s->cert->verify_store;
380 verify_store = s->ctx->cert_store;
382 ctx = X509_STORE_CTX_new_ex(s->ctx->libctx, s->ctx->propq);
384 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
388 x = sk_X509_value(sk, 0);
389 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
390 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
393 param = X509_STORE_CTX_get0_param(ctx);
395 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
396 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
397 * and PKI authentication.
399 X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));
401 /* Set suite B flags if needed */
402 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
403 if (!X509_STORE_CTX_set_ex_data
404 (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
408 /* Verify via DANE if enabled */
409 if (DANETLS_ENABLED(&s->dane))
410 X509_STORE_CTX_set0_dane(ctx, &s->dane);
413 * We need to inherit the verify parameters. These can be determined by
414 * the context: if its a server it will verify SSL client certificates or
418 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
420 * Anything non-default in "s->param" should overwrite anything in the ctx.
422 X509_VERIFY_PARAM_set1(param, s->param);
424 if (s->verify_callback)
425 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
427 if (s->ctx->app_verify_callback != NULL)
428 i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);
430 i = X509_verify_cert(ctx);
432 s->verify_result = X509_STORE_CTX_get_error(ctx);
433 sk_X509_pop_free(s->verified_chain, X509_free);
434 s->verified_chain = NULL;
435 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
436 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
437 if (s->verified_chain == NULL) {
438 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
443 /* Move peername from the store context params to the SSL handle's */
444 X509_VERIFY_PARAM_move_peername(s->param, param);
447 X509_STORE_CTX_free(ctx);
451 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
452 STACK_OF(X509_NAME) *name_list)
454 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
455 *ca_list = name_list;
458 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
461 const int num = sk_X509_NAME_num(sk);
462 STACK_OF(X509_NAME) *ret;
465 ret = sk_X509_NAME_new_reserve(NULL, num);
467 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
470 for (i = 0; i < num; i++) {
471 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
473 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
474 sk_X509_NAME_pop_free(ret, X509_NAME_free);
477 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
482 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
484 set0_CA_list(&s->ca_names, name_list);
487 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
489 set0_CA_list(&ctx->ca_names, name_list);
492 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
494 return ctx->ca_names;
497 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
499 return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;
502 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
504 set0_CA_list(&ctx->client_ca_names, name_list);
507 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
509 return ctx->client_ca_names;
512 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
514 set0_CA_list(&s->client_ca_names, name_list);
517 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
519 return s->s3.tmp.peer_ca_names;
522 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
525 return s->s3.tmp.peer_ca_names;
526 return s->client_ca_names != NULL ? s->client_ca_names
527 : s->ctx->client_ca_names;
530 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
536 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
539 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
542 if (!sk_X509_NAME_push(*sk, name)) {
543 X509_NAME_free(name);
549 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
551 return add_ca_name(&ssl->ca_names, x);
554 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
556 return add_ca_name(&ctx->ca_names, x);
560 * The following two are older names are to be replaced with
561 * SSL(_CTX)_add1_to_CA_list
563 int SSL_add_client_CA(SSL *ssl, X509 *x)
565 return add_ca_name(&ssl->client_ca_names, x);
568 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
570 return add_ca_name(&ctx->client_ca_names, x);
573 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
575 unsigned char *abuf = NULL, *bbuf = NULL;
578 /* X509_NAME_cmp() itself casts away constness in this way, so
581 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
582 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
584 if (alen < 0 || blen < 0)
586 else if (alen != blen)
588 else /* alen == blen */
589 ret = memcmp(abuf, bbuf, alen);
597 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
599 return xname_cmp(*a, *b);
602 static unsigned long xname_hash(const X509_NAME *a)
604 /* This returns 0 also if SHA1 is not available */
605 return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
608 STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
609 OSSL_LIB_CTX *libctx,
612 BIO *in = BIO_new(BIO_s_file());
614 X509_NAME *xn = NULL;
615 STACK_OF(X509_NAME) *ret = NULL;
616 LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
617 OSSL_LIB_CTX *prev_libctx = NULL;
619 if ((name_hash == NULL) || (in == NULL)) {
620 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
624 x = X509_new_ex(libctx, propq);
626 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
629 if (!BIO_read_filename(in, file))
632 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
633 prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
635 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
638 ret = sk_X509_NAME_new_null();
640 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
644 if ((xn = X509_get_subject_name(x)) == NULL)
646 /* check for duplicates */
647 xn = X509_NAME_dup(xn);
650 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
655 lh_X509_NAME_insert(name_hash, xn);
656 if (!sk_X509_NAME_push(ret, xn))
664 sk_X509_NAME_pop_free(ret, X509_NAME_free);
667 /* restore the old libctx */
668 OSSL_LIB_CTX_set0_default(prev_libctx);
671 lh_X509_NAME_free(name_hash);
677 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
679 return SSL_load_client_CA_file_ex(file, NULL, NULL);
682 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
687 X509_NAME *xn = NULL;
689 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
691 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
693 in = BIO_new(BIO_s_file());
696 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
700 if (!BIO_read_filename(in, file))
704 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
706 if ((xn = X509_get_subject_name(x)) == NULL)
708 xn = X509_NAME_dup(xn);
711 if (sk_X509_NAME_find(stack, xn) >= 0) {
714 } else if (!sk_X509_NAME_push(stack, xn)) {
728 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
732 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
735 OPENSSL_DIR_CTX *d = NULL;
736 const char *filename;
739 /* Note that a side effect is that the CAs will be sorted by name */
741 while ((filename = OPENSSL_DIR_read(&d, dir))) {
745 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
746 ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
749 #ifdef OPENSSL_SYS_VMS
750 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
752 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
754 if (r <= 0 || r >= (int)sizeof(buf))
756 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
761 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
762 "calling OPENSSL_dir_read(%s)", dir);
763 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
776 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
777 const char *uri, int depth)
780 OSSL_STORE_CTX *ctx = NULL;
782 X509_NAME *xn = NULL;
784 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
787 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
788 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
789 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
794 if (infotype == OSSL_STORE_INFO_NAME) {
796 * This is an entry in the "directory" represented by the current
797 * uri. if |depth| allows, dive into it.
800 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
802 } else if (infotype == OSSL_STORE_INFO_CERT) {
803 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
804 || (xn = X509_get_subject_name(x)) == NULL
805 || (xn = X509_NAME_dup(xn)) == NULL)
807 if (sk_X509_NAME_find(stack, xn) >= 0) {
810 } else if (!sk_X509_NAME_push(stack, xn)) {
816 OSSL_STORE_INFO_free(info);
825 OSSL_STORE_close(ctx);
830 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
833 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
834 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
835 int ret = add_uris_recursive(stack, store, 1);
837 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
841 /* Build a certificate chain for current certificate */
842 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
844 CERT *c = s ? s->cert : ctx->cert;
845 CERT_PKEY *cpk = c->key;
846 X509_STORE *chain_store = NULL;
847 X509_STORE_CTX *xs_ctx = NULL;
848 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
850 SSL_CTX *real_ctx = (s == NULL) ? ctx : s->ctx;
854 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
857 /* Rearranging and check the chain: add everything to a store */
858 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
859 chain_store = X509_STORE_new();
860 if (chain_store == NULL)
862 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
863 x = sk_X509_value(cpk->chain, i);
864 if (!X509_STORE_add_cert(chain_store, x))
867 /* Add EE cert too: it might be self signed */
868 if (!X509_STORE_add_cert(chain_store, cpk->x509))
872 chain_store = c->chain_store;
874 chain_store = s->ctx->cert_store;
876 chain_store = ctx->cert_store;
878 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
879 untrusted = cpk->chain;
882 xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, ctx->propq);
883 if (xs_ctx == NULL) {
884 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
887 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
888 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
891 /* Set suite B flags if needed */
892 X509_STORE_CTX_set_flags(xs_ctx,
893 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
895 i = X509_verify_cert(xs_ctx);
896 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
897 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
903 chain = X509_STORE_CTX_get1_chain(xs_ctx);
905 i = X509_STORE_CTX_get_error(xs_ctx);
906 ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
907 "Verify error:%s", X509_verify_cert_error_string(i));
911 /* Remove EE certificate from chain */
912 x = sk_X509_shift(chain);
914 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
915 if (sk_X509_num(chain) > 0) {
916 /* See if last cert is self signed */
917 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
918 if (X509_get_extension_flags(x) & EXFLAG_SS) {
919 x = sk_X509_pop(chain);
925 * Check security level of all CA certificates: EE will have been checked
928 for (i = 0; i < sk_X509_num(chain); i++) {
929 x = sk_X509_value(chain, i);
930 rv = ssl_security_cert(s, ctx, x, 0, 0);
932 ERR_raise(ERR_LIB_SSL, rv);
933 sk_X509_pop_free(chain, X509_free);
938 sk_X509_pop_free(cpk->chain, X509_free);
943 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
944 X509_STORE_free(chain_store);
945 X509_STORE_CTX_free(xs_ctx);
950 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
954 pstore = &c->chain_store;
956 pstore = &c->verify_store;
957 X509_STORE_free(*pstore);
960 X509_STORE_up_ref(store);
964 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
965 int op, int bits, int nid, void *other,
969 static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
971 level = SSL_CTX_get_security_level(ctx);
973 level = SSL_get_security_level(s);
977 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
980 if (op == SSL_SECOP_TMP_DH && bits < 80)
986 minbits = minbits_table[level - 1];
988 case SSL_SECOP_CIPHER_SUPPORTED:
989 case SSL_SECOP_CIPHER_SHARED:
990 case SSL_SECOP_CIPHER_CHECK:
992 const SSL_CIPHER *c = other;
993 /* No ciphers below security level */
996 /* No unauthenticated ciphersuites */
997 if (c->algorithm_auth & SSL_aNULL)
999 /* No MD5 mac ciphersuites */
1000 if (c->algorithm_mac & SSL_MD5)
1002 /* SHA1 HMAC is 160 bits of security */
1003 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1005 /* Level 2: no RC4 */
1006 if (level >= 2 && c->algorithm_enc == SSL_RC4)
1008 /* Level 3: forward secure ciphersuites only */
1009 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1010 !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1014 case SSL_SECOP_VERSION:
1015 if (!SSL_IS_DTLS(s)) {
1016 /* SSLv3 not allowed at level 2 */
1017 if (nid <= SSL3_VERSION && level >= 2)
1019 /* TLS v1.1 and above only for level 3 */
1020 if (nid <= TLS1_VERSION && level >= 3)
1022 /* TLS v1.2 only for level 4 and above */
1023 if (nid <= TLS1_1_VERSION && level >= 4)
1026 /* DTLS v1.2 only for level 4 and above */
1027 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
1032 case SSL_SECOP_COMPRESSION:
1036 case SSL_SECOP_TICKET:
1047 int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
1049 return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1052 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1054 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1058 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1062 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1063 if (ssl_cert_info[i].nid == nid) {
1072 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1076 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1077 const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
1079 if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1080 || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1090 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1092 if (idx >= OSSL_NELEM(ssl_cert_info))
1094 return &ssl_cert_info[idx];