2 * Copyright 1995-2021 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;
100 ret->dh_tmp_auto = cert->dh_tmp_auto;
102 for (i = 0; i < SSL_PKEY_NUM; i++) {
103 CERT_PKEY *cpk = cert->pkeys + i;
104 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_MALLOC_FAILURE);
122 if (cert->pkeys[i].serverinfo != NULL) {
123 /* Just copy everything. */
124 ret->pkeys[i].serverinfo =
125 OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
126 if (ret->pkeys[i].serverinfo == NULL) {
127 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
130 ret->pkeys[i].serverinfo_length = cert->pkeys[i].serverinfo_length;
131 memcpy(ret->pkeys[i].serverinfo,
132 cert->pkeys[i].serverinfo, cert->pkeys[i].serverinfo_length);
136 /* Configured sigalgs copied across */
137 if (cert->conf_sigalgs) {
138 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
139 * sizeof(*cert->conf_sigalgs));
140 if (ret->conf_sigalgs == NULL)
142 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
143 cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
144 ret->conf_sigalgslen = cert->conf_sigalgslen;
146 ret->conf_sigalgs = NULL;
148 if (cert->client_sigalgs) {
149 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
150 * sizeof(*cert->client_sigalgs));
151 if (ret->client_sigalgs == NULL)
153 memcpy(ret->client_sigalgs, cert->client_sigalgs,
154 cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
155 ret->client_sigalgslen = cert->client_sigalgslen;
157 ret->client_sigalgs = NULL;
158 /* Copy any custom client certificate types */
160 ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
161 if (ret->ctype == NULL)
163 ret->ctype_len = cert->ctype_len;
166 ret->cert_flags = cert->cert_flags;
168 ret->cert_cb = cert->cert_cb;
169 ret->cert_cb_arg = cert->cert_cb_arg;
171 if (cert->verify_store) {
172 X509_STORE_up_ref(cert->verify_store);
173 ret->verify_store = cert->verify_store;
176 if (cert->chain_store) {
177 X509_STORE_up_ref(cert->chain_store);
178 ret->chain_store = cert->chain_store;
181 ret->sec_cb = cert->sec_cb;
182 ret->sec_level = cert->sec_level;
183 ret->sec_ex = cert->sec_ex;
185 if (!custom_exts_copy(&ret->custext, &cert->custext))
187 #ifndef OPENSSL_NO_PSK
188 if (cert->psk_identity_hint) {
189 ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
190 if (ret->psk_identity_hint == NULL)
202 /* Free up and clear all certificates and chains */
204 void ssl_cert_clear_certs(CERT *c)
209 for (i = 0; i < SSL_PKEY_NUM; i++) {
210 CERT_PKEY *cpk = c->pkeys + i;
211 X509_free(cpk->x509);
213 EVP_PKEY_free(cpk->privatekey);
214 cpk->privatekey = NULL;
215 sk_X509_pop_free(cpk->chain, X509_free);
217 OPENSSL_free(cpk->serverinfo);
218 cpk->serverinfo = NULL;
219 cpk->serverinfo_length = 0;
223 void ssl_cert_free(CERT *c)
229 CRYPTO_DOWN_REF(&c->references, &i, c->lock);
230 REF_PRINT_COUNT("CERT", c);
233 REF_ASSERT_ISNT(i < 0);
235 EVP_PKEY_free(c->dh_tmp);
237 ssl_cert_clear_certs(c);
238 OPENSSL_free(c->conf_sigalgs);
239 OPENSSL_free(c->client_sigalgs);
240 OPENSSL_free(c->ctype);
241 X509_STORE_free(c->verify_store);
242 X509_STORE_free(c->chain_store);
243 custom_exts_free(&c->custext);
244 #ifndef OPENSSL_NO_PSK
245 OPENSSL_free(c->psk_identity_hint);
247 CRYPTO_THREAD_lock_free(c->lock);
251 int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
254 CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
258 for (i = 0; i < sk_X509_num(chain); i++) {
259 X509 *x = sk_X509_value(chain, i);
261 r = ssl_security_cert(s, ctx, x, 0, 0);
263 ERR_raise(ERR_LIB_SSL, r);
267 sk_X509_pop_free(cpk->chain, X509_free);
272 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
274 STACK_OF(X509) *dchain;
276 return ssl_cert_set0_chain(s, ctx, NULL);
277 dchain = X509_chain_up_ref(chain);
280 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
281 sk_X509_pop_free(dchain, X509_free);
287 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
290 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
293 r = ssl_security_cert(s, ctx, x, 0, 0);
295 ERR_raise(ERR_LIB_SSL, r);
299 cpk->chain = sk_X509_new_null();
300 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
305 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
307 if (!ssl_cert_add0_chain_cert(s, ctx, x))
313 int ssl_cert_select_current(CERT *c, X509 *x)
318 for (i = 0; i < SSL_PKEY_NUM; i++) {
319 CERT_PKEY *cpk = c->pkeys + i;
320 if (cpk->x509 == x && cpk->privatekey) {
326 for (i = 0; i < SSL_PKEY_NUM; i++) {
327 CERT_PKEY *cpk = c->pkeys + i;
328 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
336 int ssl_cert_set_current(CERT *c, long op)
341 if (op == SSL_CERT_SET_FIRST)
343 else if (op == SSL_CERT_SET_NEXT) {
344 idx = (int)(c->key - c->pkeys + 1);
345 if (idx >= SSL_PKEY_NUM)
349 for (i = idx; i < SSL_PKEY_NUM; i++) {
350 CERT_PKEY *cpk = c->pkeys + i;
351 if (cpk->x509 && cpk->privatekey) {
359 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
362 c->cert_cb_arg = arg;
365 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
369 X509_STORE *verify_store;
370 X509_STORE_CTX *ctx = NULL;
371 X509_VERIFY_PARAM *param;
373 if ((sk == NULL) || (sk_X509_num(sk) == 0))
376 if (s->cert->verify_store)
377 verify_store = s->cert->verify_store;
379 verify_store = s->ctx->cert_store;
381 ctx = X509_STORE_CTX_new_ex(s->ctx->libctx, s->ctx->propq);
383 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
387 x = sk_X509_value(sk, 0);
388 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
389 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
392 param = X509_STORE_CTX_get0_param(ctx);
394 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
395 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
396 * and PKI authentication.
398 X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));
400 /* Set suite B flags if needed */
401 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
402 if (!X509_STORE_CTX_set_ex_data
403 (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
407 /* Verify via DANE if enabled */
408 if (DANETLS_ENABLED(&s->dane))
409 X509_STORE_CTX_set0_dane(ctx, &s->dane);
412 * We need to inherit the verify parameters. These can be determined by
413 * the context: if its a server it will verify SSL client certificates or
417 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
419 * Anything non-default in "s->param" should overwrite anything in the ctx.
421 X509_VERIFY_PARAM_set1(param, s->param);
423 if (s->verify_callback)
424 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
426 if (s->ctx->app_verify_callback != NULL)
427 i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);
429 i = X509_verify_cert(ctx);
431 s->verify_result = X509_STORE_CTX_get_error(ctx);
432 sk_X509_pop_free(s->verified_chain, X509_free);
433 s->verified_chain = NULL;
434 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
435 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
436 if (s->verified_chain == NULL) {
437 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
442 /* Move peername from the store context params to the SSL handle's */
443 X509_VERIFY_PARAM_move_peername(s->param, param);
446 X509_STORE_CTX_free(ctx);
450 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
451 STACK_OF(X509_NAME) *name_list)
453 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
454 *ca_list = name_list;
457 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
460 const int num = sk_X509_NAME_num(sk);
461 STACK_OF(X509_NAME) *ret;
464 ret = sk_X509_NAME_new_reserve(NULL, num);
466 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
469 for (i = 0; i < num; i++) {
470 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
472 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
473 sk_X509_NAME_pop_free(ret, X509_NAME_free);
476 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
481 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
483 set0_CA_list(&s->ca_names, name_list);
486 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
488 set0_CA_list(&ctx->ca_names, name_list);
491 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
493 return ctx->ca_names;
496 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
498 return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;
501 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
503 set0_CA_list(&ctx->client_ca_names, name_list);
506 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
508 return ctx->client_ca_names;
511 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
513 set0_CA_list(&s->client_ca_names, name_list);
516 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
518 return s->s3.tmp.peer_ca_names;
521 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
524 return s->s3.tmp.peer_ca_names;
525 return s->client_ca_names != NULL ? s->client_ca_names
526 : s->ctx->client_ca_names;
529 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
535 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
538 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
541 if (!sk_X509_NAME_push(*sk, name)) {
542 X509_NAME_free(name);
548 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
550 return add_ca_name(&ssl->ca_names, x);
553 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
555 return add_ca_name(&ctx->ca_names, x);
559 * The following two are older names are to be replaced with
560 * SSL(_CTX)_add1_to_CA_list
562 int SSL_add_client_CA(SSL *ssl, X509 *x)
564 return add_ca_name(&ssl->client_ca_names, x);
567 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
569 return add_ca_name(&ctx->client_ca_names, x);
572 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
574 unsigned char *abuf = NULL, *bbuf = NULL;
577 /* X509_NAME_cmp() itself casts away constness in this way, so
580 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
581 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
583 if (alen < 0 || blen < 0)
585 else if (alen != blen)
587 else /* alen == blen */
588 ret = memcmp(abuf, bbuf, alen);
596 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
598 return xname_cmp(*a, *b);
601 static unsigned long xname_hash(const X509_NAME *a)
603 /* This returns 0 also if SHA1 is not available */
604 return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
607 STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
608 OSSL_LIB_CTX *libctx,
611 BIO *in = BIO_new(BIO_s_file());
613 X509_NAME *xn = NULL;
614 STACK_OF(X509_NAME) *ret = NULL;
615 LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
616 OSSL_LIB_CTX *prev_libctx = NULL;
618 if ((name_hash == NULL) || (in == NULL)) {
619 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
623 x = X509_new_ex(libctx, propq);
625 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
628 if (BIO_read_filename(in, file) <= 0)
631 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
632 prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
634 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
637 ret = sk_X509_NAME_new_null();
639 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
643 if ((xn = X509_get_subject_name(x)) == NULL)
645 /* check for duplicates */
646 xn = X509_NAME_dup(xn);
649 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
654 lh_X509_NAME_insert(name_hash, xn);
655 if (!sk_X509_NAME_push(ret, xn))
663 sk_X509_NAME_pop_free(ret, X509_NAME_free);
666 /* restore the old libctx */
667 OSSL_LIB_CTX_set0_default(prev_libctx);
670 lh_X509_NAME_free(name_hash);
676 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
678 return SSL_load_client_CA_file_ex(file, NULL, NULL);
681 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
686 X509_NAME *xn = NULL;
688 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
690 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
692 in = BIO_new(BIO_s_file());
695 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
699 if (BIO_read_filename(in, file) <= 0)
703 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
705 if ((xn = X509_get_subject_name(x)) == NULL)
707 xn = X509_NAME_dup(xn);
710 if (sk_X509_NAME_find(stack, xn) >= 0) {
713 } else if (!sk_X509_NAME_push(stack, xn)) {
727 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
731 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
734 OPENSSL_DIR_CTX *d = NULL;
735 const char *filename;
738 /* Note that a side effect is that the CAs will be sorted by name */
740 while ((filename = OPENSSL_DIR_read(&d, dir))) {
744 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
745 ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
748 #ifdef OPENSSL_SYS_VMS
749 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
751 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
753 if (r <= 0 || r >= (int)sizeof(buf))
755 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
760 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
761 "calling OPENSSL_dir_read(%s)", dir);
762 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
775 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
776 const char *uri, int depth)
779 OSSL_STORE_CTX *ctx = NULL;
781 X509_NAME *xn = NULL;
783 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
786 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
787 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
788 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
793 if (infotype == OSSL_STORE_INFO_NAME) {
795 * This is an entry in the "directory" represented by the current
796 * uri. if |depth| allows, dive into it.
799 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
801 } else if (infotype == OSSL_STORE_INFO_CERT) {
802 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
803 || (xn = X509_get_subject_name(x)) == NULL
804 || (xn = X509_NAME_dup(xn)) == NULL)
806 if (sk_X509_NAME_find(stack, xn) >= 0) {
809 } else if (!sk_X509_NAME_push(stack, xn)) {
815 OSSL_STORE_INFO_free(info);
824 OSSL_STORE_close(ctx);
829 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
832 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
833 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
834 int ret = add_uris_recursive(stack, store, 1);
836 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
840 /* Build a certificate chain for current certificate */
841 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
843 CERT *c = s ? s->cert : ctx->cert;
844 CERT_PKEY *cpk = c->key;
845 X509_STORE *chain_store = NULL;
846 X509_STORE_CTX *xs_ctx = NULL;
847 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
849 SSL_CTX *real_ctx = (s == NULL) ? ctx : s->ctx;
853 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
856 /* Rearranging and check the chain: add everything to a store */
857 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
858 chain_store = X509_STORE_new();
859 if (chain_store == NULL)
861 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
862 x = sk_X509_value(cpk->chain, i);
863 if (!X509_STORE_add_cert(chain_store, x))
866 /* Add EE cert too: it might be self signed */
867 if (!X509_STORE_add_cert(chain_store, cpk->x509))
871 chain_store = c->chain_store;
873 chain_store = s->ctx->cert_store;
875 chain_store = ctx->cert_store;
877 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
878 untrusted = cpk->chain;
881 xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
882 if (xs_ctx == NULL) {
883 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
886 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
887 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
890 /* Set suite B flags if needed */
891 X509_STORE_CTX_set_flags(xs_ctx,
892 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
894 i = X509_verify_cert(xs_ctx);
895 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
896 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
902 chain = X509_STORE_CTX_get1_chain(xs_ctx);
904 i = X509_STORE_CTX_get_error(xs_ctx);
905 ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
906 "Verify error:%s", X509_verify_cert_error_string(i));
910 /* Remove EE certificate from chain */
911 x = sk_X509_shift(chain);
913 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
914 if (sk_X509_num(chain) > 0) {
915 /* See if last cert is self signed */
916 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
917 if (X509_get_extension_flags(x) & EXFLAG_SS) {
918 x = sk_X509_pop(chain);
924 * Check security level of all CA certificates: EE will have been checked
927 for (i = 0; i < sk_X509_num(chain); i++) {
928 x = sk_X509_value(chain, i);
929 rv = ssl_security_cert(s, ctx, x, 0, 0);
931 ERR_raise(ERR_LIB_SSL, rv);
932 sk_X509_pop_free(chain, X509_free);
937 sk_X509_pop_free(cpk->chain, X509_free);
942 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
943 X509_STORE_free(chain_store);
944 X509_STORE_CTX_free(xs_ctx);
949 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
953 pstore = &c->chain_store;
955 pstore = &c->verify_store;
956 X509_STORE_free(*pstore);
959 X509_STORE_up_ref(store);
963 int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
967 * note that there's a corresponding minbits_table
968 * in crypto/x509/x509_vfy.c that's used for checking the security level
969 * of RSA and DSA keys
971 static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
974 level = SSL_CTX_get_security_level(ctx);
976 level = SSL_get_security_level(s);
986 return minbits_table[level];
989 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
990 int op, int bits, int nid, void *other,
995 minbits = ssl_get_security_level_bits(s, ctx, &level);
999 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
1002 if (op == SSL_SECOP_TMP_DH && bits < 80)
1007 case SSL_SECOP_CIPHER_SUPPORTED:
1008 case SSL_SECOP_CIPHER_SHARED:
1009 case SSL_SECOP_CIPHER_CHECK:
1011 const SSL_CIPHER *c = other;
1012 /* No ciphers below security level */
1015 /* No unauthenticated ciphersuites */
1016 if (c->algorithm_auth & SSL_aNULL)
1018 /* No MD5 mac ciphersuites */
1019 if (c->algorithm_mac & SSL_MD5)
1021 /* SHA1 HMAC is 160 bits of security */
1022 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1024 /* Level 3: forward secure ciphersuites only */
1025 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1026 !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1030 case SSL_SECOP_VERSION:
1031 if (!SSL_IS_DTLS(s)) {
1032 /* SSLv3 not allowed at level 2 */
1033 if (nid <= SSL3_VERSION && level >= 2)
1035 /* TLS v1.1 and above only for level 3 */
1036 if (nid <= TLS1_VERSION && level >= 3)
1038 /* TLS v1.2 only for level 4 and above */
1039 if (nid <= TLS1_1_VERSION && level >= 4)
1042 /* DTLS v1.2 only for level 4 and above */
1043 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
1048 case SSL_SECOP_COMPRESSION:
1052 case SSL_SECOP_TICKET:
1063 int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
1065 return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1068 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1070 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1074 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1078 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1079 if (ssl_cert_info[i].nid == nid) {
1088 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1092 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1093 const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
1095 if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1096 || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1106 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1108 if (idx >= OSSL_NELEM(ssl_cert_info))
1110 return &ssl_cert_info[idx];