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"
29 DEFINE_STACK_OF(X509_NAME)
31 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
32 int op, int bits, int nid, void *other,
35 static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
36 static volatile int ssl_x509_store_ctx_idx = -1;
38 DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
40 ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
41 "SSL for verify callback",
43 return ssl_x509_store_ctx_idx >= 0;
46 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
49 if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
51 return ssl_x509_store_ctx_idx;
54 CERT *ssl_cert_new(void)
56 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
59 SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
63 ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
65 ret->sec_cb = ssl_security_default_callback;
66 ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
68 ret->lock = CRYPTO_THREAD_lock_new();
69 if (ret->lock == NULL) {
70 SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
78 CERT *ssl_cert_dup(CERT *cert)
80 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
84 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
89 ret->key = &ret->pkeys[cert->key - cert->pkeys];
90 ret->lock = CRYPTO_THREAD_lock_new();
91 if (ret->lock == NULL) {
92 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
97 if (cert->dh_tmp != NULL) {
98 ret->dh_tmp = cert->dh_tmp;
99 EVP_PKEY_up_ref(ret->dh_tmp);
101 ret->dh_tmp_cb = cert->dh_tmp_cb;
102 ret->dh_tmp_auto = cert->dh_tmp_auto;
105 for (i = 0; i < SSL_PKEY_NUM; i++) {
106 CERT_PKEY *cpk = cert->pkeys + i;
107 CERT_PKEY *rpk = ret->pkeys + i;
108 if (cpk->x509 != NULL) {
109 rpk->x509 = cpk->x509;
110 X509_up_ref(rpk->x509);
113 if (cpk->privatekey != NULL) {
114 rpk->privatekey = cpk->privatekey;
115 EVP_PKEY_up_ref(cpk->privatekey);
119 rpk->chain = X509_chain_up_ref(cpk->chain);
121 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
125 if (cert->pkeys[i].serverinfo != NULL) {
126 /* Just copy everything. */
127 ret->pkeys[i].serverinfo =
128 OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
129 if (ret->pkeys[i].serverinfo == NULL) {
130 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
133 ret->pkeys[i].serverinfo_length = cert->pkeys[i].serverinfo_length;
134 memcpy(ret->pkeys[i].serverinfo,
135 cert->pkeys[i].serverinfo, cert->pkeys[i].serverinfo_length);
139 /* Configured sigalgs copied across */
140 if (cert->conf_sigalgs) {
141 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
142 * sizeof(*cert->conf_sigalgs));
143 if (ret->conf_sigalgs == NULL)
145 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
146 cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
147 ret->conf_sigalgslen = cert->conf_sigalgslen;
149 ret->conf_sigalgs = NULL;
151 if (cert->client_sigalgs) {
152 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
153 * sizeof(*cert->client_sigalgs));
154 if (ret->client_sigalgs == NULL)
156 memcpy(ret->client_sigalgs, cert->client_sigalgs,
157 cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
158 ret->client_sigalgslen = cert->client_sigalgslen;
160 ret->client_sigalgs = NULL;
161 /* Copy any custom client certificate types */
163 ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
164 if (ret->ctype == NULL)
166 ret->ctype_len = cert->ctype_len;
169 ret->cert_flags = cert->cert_flags;
171 ret->cert_cb = cert->cert_cb;
172 ret->cert_cb_arg = cert->cert_cb_arg;
174 if (cert->verify_store) {
175 X509_STORE_up_ref(cert->verify_store);
176 ret->verify_store = cert->verify_store;
179 if (cert->chain_store) {
180 X509_STORE_up_ref(cert->chain_store);
181 ret->chain_store = cert->chain_store;
184 ret->sec_cb = cert->sec_cb;
185 ret->sec_level = cert->sec_level;
186 ret->sec_ex = cert->sec_ex;
188 if (!custom_exts_copy(&ret->custext, &cert->custext))
190 #ifndef OPENSSL_NO_PSK
191 if (cert->psk_identity_hint) {
192 ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
193 if (ret->psk_identity_hint == NULL)
205 /* Free up and clear all certificates and chains */
207 void ssl_cert_clear_certs(CERT *c)
212 for (i = 0; i < SSL_PKEY_NUM; i++) {
213 CERT_PKEY *cpk = c->pkeys + i;
214 X509_free(cpk->x509);
216 EVP_PKEY_free(cpk->privatekey);
217 cpk->privatekey = NULL;
218 sk_X509_pop_free(cpk->chain, X509_free);
220 OPENSSL_free(cpk->serverinfo);
221 cpk->serverinfo = NULL;
222 cpk->serverinfo_length = 0;
226 void ssl_cert_free(CERT *c)
232 CRYPTO_DOWN_REF(&c->references, &i, c->lock);
233 REF_PRINT_COUNT("CERT", c);
236 REF_ASSERT_ISNT(i < 0);
238 #ifndef OPENSSL_NO_DH
239 EVP_PKEY_free(c->dh_tmp);
242 ssl_cert_clear_certs(c);
243 OPENSSL_free(c->conf_sigalgs);
244 OPENSSL_free(c->client_sigalgs);
245 OPENSSL_free(c->ctype);
246 X509_STORE_free(c->verify_store);
247 X509_STORE_free(c->chain_store);
248 custom_exts_free(&c->custext);
249 #ifndef OPENSSL_NO_PSK
250 OPENSSL_free(c->psk_identity_hint);
252 CRYPTO_THREAD_lock_free(c->lock);
256 int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
259 CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
263 for (i = 0; i < sk_X509_num(chain); i++) {
264 X509 *x = sk_X509_value(chain, i);
266 r = ssl_security_cert(s, ctx, x, 0, 0);
268 SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r);
272 sk_X509_pop_free(cpk->chain, X509_free);
277 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
279 STACK_OF(X509) *dchain;
281 return ssl_cert_set0_chain(s, ctx, NULL);
282 dchain = X509_chain_up_ref(chain);
285 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
286 sk_X509_pop_free(dchain, X509_free);
292 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
295 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
298 r = ssl_security_cert(s, ctx, x, 0, 0);
300 SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r);
304 cpk->chain = sk_X509_new_null();
305 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
310 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
312 if (!ssl_cert_add0_chain_cert(s, ctx, x))
318 int ssl_cert_select_current(CERT *c, X509 *x)
323 for (i = 0; i < SSL_PKEY_NUM; i++) {
324 CERT_PKEY *cpk = c->pkeys + i;
325 if (cpk->x509 == x && cpk->privatekey) {
331 for (i = 0; i < SSL_PKEY_NUM; i++) {
332 CERT_PKEY *cpk = c->pkeys + i;
333 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
341 int ssl_cert_set_current(CERT *c, long op)
346 if (op == SSL_CERT_SET_FIRST)
348 else if (op == SSL_CERT_SET_NEXT) {
349 idx = (int)(c->key - c->pkeys + 1);
350 if (idx >= SSL_PKEY_NUM)
354 for (i = idx; i < SSL_PKEY_NUM; i++) {
355 CERT_PKEY *cpk = c->pkeys + i;
356 if (cpk->x509 && cpk->privatekey) {
364 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
367 c->cert_cb_arg = arg;
370 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
374 X509_STORE *verify_store;
375 X509_STORE_CTX *ctx = NULL;
376 X509_VERIFY_PARAM *param;
378 if ((sk == NULL) || (sk_X509_num(sk) == 0))
381 if (s->cert->verify_store)
382 verify_store = s->cert->verify_store;
384 verify_store = s->ctx->cert_store;
386 ctx = X509_STORE_CTX_new_with_libctx(s->ctx->libctx, s->ctx->propq);
388 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
392 x = sk_X509_value(sk, 0);
393 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
394 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
397 param = X509_STORE_CTX_get0_param(ctx);
399 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
400 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
401 * and PKI authentication.
403 X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));
405 /* Set suite B flags if needed */
406 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
407 if (!X509_STORE_CTX_set_ex_data
408 (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
412 /* Verify via DANE if enabled */
413 if (DANETLS_ENABLED(&s->dane))
414 X509_STORE_CTX_set0_dane(ctx, &s->dane);
417 * We need to inherit the verify parameters. These can be determined by
418 * the context: if its a server it will verify SSL client certificates or
422 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
424 * Anything non-default in "s->param" should overwrite anything in the ctx.
426 X509_VERIFY_PARAM_set1(param, s->param);
428 if (s->verify_callback)
429 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
431 if (s->ctx->app_verify_callback != NULL)
432 i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);
434 i = X509_verify_cert(ctx);
436 s->verify_result = X509_STORE_CTX_get_error(ctx);
437 sk_X509_pop_free(s->verified_chain, X509_free);
438 s->verified_chain = NULL;
439 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
440 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
441 if (s->verified_chain == NULL) {
442 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
447 /* Move peername from the store context params to the SSL handle's */
448 X509_VERIFY_PARAM_move_peername(s->param, param);
451 X509_STORE_CTX_free(ctx);
455 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
456 STACK_OF(X509_NAME) *name_list)
458 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
459 *ca_list = name_list;
462 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
465 const int num = sk_X509_NAME_num(sk);
466 STACK_OF(X509_NAME) *ret;
469 ret = sk_X509_NAME_new_reserve(NULL, num);
471 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
474 for (i = 0; i < num; i++) {
475 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
477 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
478 sk_X509_NAME_pop_free(ret, X509_NAME_free);
481 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
486 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
488 set0_CA_list(&s->ca_names, name_list);
491 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
493 set0_CA_list(&ctx->ca_names, name_list);
496 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
498 return ctx->ca_names;
501 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
503 return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;
506 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
508 set0_CA_list(&ctx->client_ca_names, name_list);
511 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
513 return ctx->client_ca_names;
516 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
518 set0_CA_list(&s->client_ca_names, name_list);
521 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
523 return s->s3.tmp.peer_ca_names;
526 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
529 return s->s3.tmp.peer_ca_names;
530 return s->client_ca_names != NULL ? s->client_ca_names
531 : s->ctx->client_ca_names;
534 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
540 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
543 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
546 if (!sk_X509_NAME_push(*sk, name)) {
547 X509_NAME_free(name);
553 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
555 return add_ca_name(&ssl->ca_names, x);
558 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
560 return add_ca_name(&ctx->ca_names, x);
564 * The following two are older names are to be replaced with
565 * SSL(_CTX)_add1_to_CA_list
567 int SSL_add_client_CA(SSL *ssl, X509 *x)
569 return add_ca_name(&ssl->client_ca_names, x);
572 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
574 return add_ca_name(&ctx->client_ca_names, x);
577 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
579 unsigned char *abuf = NULL, *bbuf = NULL;
582 /* X509_NAME_cmp() itself casts away constness in this way, so
585 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
586 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
588 if (alen < 0 || blen < 0)
590 else if (alen != blen)
592 else /* alen == blen */
593 ret = memcmp(abuf, bbuf, alen);
601 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
603 return xname_cmp(*a, *b);
606 static unsigned long xname_hash(const X509_NAME *a)
608 return X509_NAME_hash((X509_NAME *)a);
611 STACK_OF(X509_NAME) *SSL_load_client_CA_file_with_libctx(const char *file,
615 BIO *in = BIO_new(BIO_s_file());
617 X509_NAME *xn = NULL;
618 STACK_OF(X509_NAME) *ret = NULL;
619 LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
620 OPENSSL_CTX *prev_libctx = NULL;
622 if ((name_hash == NULL) || (in == NULL)) {
623 SSLerr(0, ERR_R_MALLOC_FAILURE);
627 x = X509_new_with_libctx(libctx, propq);
629 SSLerr(0, ERR_R_MALLOC_FAILURE);
632 if (!BIO_read_filename(in, file))
635 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
636 prev_libctx = OPENSSL_CTX_set0_default(libctx);
638 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
641 ret = sk_X509_NAME_new_null();
643 SSLerr(0, ERR_R_MALLOC_FAILURE);
647 if ((xn = X509_get_subject_name(x)) == NULL)
649 /* check for duplicates */
650 xn = X509_NAME_dup(xn);
653 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
658 lh_X509_NAME_insert(name_hash, xn);
659 if (!sk_X509_NAME_push(ret, xn))
667 sk_X509_NAME_pop_free(ret, X509_NAME_free);
670 /* restore the old libctx */
671 OPENSSL_CTX_set0_default(prev_libctx);
674 lh_X509_NAME_free(name_hash);
680 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
682 return SSL_load_client_CA_file_with_libctx(file, NULL, NULL);
685 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
690 X509_NAME *xn = NULL;
692 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
694 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
696 in = BIO_new(BIO_s_file());
699 SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, ERR_R_MALLOC_FAILURE);
703 if (!BIO_read_filename(in, file))
707 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
709 if ((xn = X509_get_subject_name(x)) == NULL)
711 xn = X509_NAME_dup(xn);
714 if (sk_X509_NAME_find(stack, xn) >= 0) {
717 } else if (!sk_X509_NAME_push(stack, xn)) {
731 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
735 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
738 OPENSSL_DIR_CTX *d = NULL;
739 const char *filename;
742 /* Note that a side effect is that the CAs will be sorted by name */
744 while ((filename = OPENSSL_DIR_read(&d, dir))) {
748 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
749 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
750 SSL_R_PATH_TOO_LONG);
753 #ifdef OPENSSL_SYS_VMS
754 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
756 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
758 if (r <= 0 || r >= (int)sizeof(buf))
760 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
765 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
766 "calling OPENSSL_dir_read(%s)",
768 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
781 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
782 const char *uri, int depth)
785 OSSL_STORE_CTX *ctx = NULL;
787 X509_NAME *xn = NULL;
789 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
792 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
793 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
794 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
799 if (infotype == OSSL_STORE_INFO_NAME) {
801 * This is an entry in the "directory" represented by the current
802 * uri. if |depth| allows, dive into it.
805 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
807 } else if (infotype == OSSL_STORE_INFO_CERT) {
808 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
809 || (xn = X509_get_subject_name(x)) == NULL
810 || (xn = X509_NAME_dup(xn)) == NULL)
812 if (sk_X509_NAME_find(stack, xn) >= 0) {
815 } else if (!sk_X509_NAME_push(stack, xn)) {
821 OSSL_STORE_INFO_free(info);
830 OSSL_STORE_close(ctx);
835 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
838 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
839 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
840 int ret = add_uris_recursive(stack, store, 1);
842 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
846 /* Build a certificate chain for current certificate */
847 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
849 CERT *c = s ? s->cert : ctx->cert;
850 CERT_PKEY *cpk = c->key;
851 X509_STORE *chain_store = NULL;
852 X509_STORE_CTX *xs_ctx = NULL;
853 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
855 SSL_CTX *real_ctx = (s == NULL) ? ctx : s->ctx;
859 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
862 /* Rearranging and check the chain: add everything to a store */
863 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
864 chain_store = X509_STORE_new();
865 if (chain_store == NULL)
867 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
868 x = sk_X509_value(cpk->chain, i);
869 if (!X509_STORE_add_cert(chain_store, x))
872 /* Add EE cert too: it might be self signed */
873 if (!X509_STORE_add_cert(chain_store, cpk->x509))
877 chain_store = c->chain_store;
879 chain_store = s->ctx->cert_store;
881 chain_store = ctx->cert_store;
883 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
884 untrusted = cpk->chain;
887 xs_ctx = X509_STORE_CTX_new_with_libctx(real_ctx->libctx, ctx->propq);
888 if (xs_ctx == NULL) {
889 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
892 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
893 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
896 /* Set suite B flags if needed */
897 X509_STORE_CTX_set_flags(xs_ctx,
898 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
900 i = X509_verify_cert(xs_ctx);
901 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
902 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
908 chain = X509_STORE_CTX_get1_chain(xs_ctx);
910 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
911 i = X509_STORE_CTX_get_error(xs_ctx);
912 ERR_add_error_data(2, "Verify error:",
913 X509_verify_cert_error_string(i));
917 /* Remove EE certificate from chain */
918 x = sk_X509_shift(chain);
920 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
921 if (sk_X509_num(chain) > 0) {
922 /* See if last cert is self signed */
923 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
924 if (X509_get_extension_flags(x) & EXFLAG_SS) {
925 x = sk_X509_pop(chain);
931 * Check security level of all CA certificates: EE will have been checked
934 for (i = 0; i < sk_X509_num(chain); i++) {
935 x = sk_X509_value(chain, i);
936 rv = ssl_security_cert(s, ctx, x, 0, 0);
938 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv);
939 sk_X509_pop_free(chain, X509_free);
944 sk_X509_pop_free(cpk->chain, X509_free);
949 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
950 X509_STORE_free(chain_store);
951 X509_STORE_CTX_free(xs_ctx);
956 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
960 pstore = &c->chain_store;
962 pstore = &c->verify_store;
963 X509_STORE_free(*pstore);
966 X509_STORE_up_ref(store);
970 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
971 int op, int bits, int nid, void *other,
975 static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
977 level = SSL_CTX_get_security_level(ctx);
979 level = SSL_get_security_level(s);
983 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
986 if (op == SSL_SECOP_TMP_DH && bits < 80)
992 minbits = minbits_table[level - 1];
994 case SSL_SECOP_CIPHER_SUPPORTED:
995 case SSL_SECOP_CIPHER_SHARED:
996 case SSL_SECOP_CIPHER_CHECK:
998 const SSL_CIPHER *c = other;
999 /* No ciphers below security level */
1002 /* No unauthenticated ciphersuites */
1003 if (c->algorithm_auth & SSL_aNULL)
1005 /* No MD5 mac ciphersuites */
1006 if (c->algorithm_mac & SSL_MD5)
1008 /* SHA1 HMAC is 160 bits of security */
1009 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1011 /* Level 2: no RC4 */
1012 if (level >= 2 && c->algorithm_enc == SSL_RC4)
1014 /* Level 3: forward secure ciphersuites only */
1015 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1016 !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1020 case SSL_SECOP_VERSION:
1021 if (!SSL_IS_DTLS(s)) {
1022 /* SSLv3 not allowed at level 2 */
1023 if (nid <= SSL3_VERSION && level >= 2)
1025 /* TLS v1.1 and above only for level 3 */
1026 if (nid <= TLS1_VERSION && level >= 3)
1028 /* TLS v1.2 only for level 4 and above */
1029 if (nid <= TLS1_1_VERSION && level >= 4)
1032 /* DTLS v1.2 only for level 4 and above */
1033 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
1038 case SSL_SECOP_COMPRESSION:
1042 case SSL_SECOP_TICKET:
1053 int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
1055 return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1058 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1060 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1064 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1068 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1069 if (ssl_cert_info[i].nid == nid) {
1078 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1082 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1083 const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
1085 if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1086 || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1096 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1098 if (idx >= OSSL_NELEM(ssl_cert_info))
1100 return &ssl_cert_info[idx];