2 * Copyright 1995-2018 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 SSLerr(SSL_F_SSL_CERT_NEW, 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 SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
75 CERT *ssl_cert_dup(CERT *cert)
77 CERT *ret = OPENSSL_zalloc(sizeof(*ret));
81 SSLerr(SSL_F_SSL_CERT_DUP, 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 SSLerr(SSL_F_SSL_CERT_DUP, 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);
98 ret->dh_tmp_cb = cert->dh_tmp_cb;
99 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 SSLerr(SSL_F_SSL_CERT_DUP, 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 SSLerr(SSL_F_SSL_CERT_DUP, 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 #ifndef OPENSSL_NO_DH
236 EVP_PKEY_free(c->dh_tmp);
239 ssl_cert_clear_certs(c);
240 OPENSSL_free(c->conf_sigalgs);
241 OPENSSL_free(c->client_sigalgs);
242 OPENSSL_free(c->ctype);
243 X509_STORE_free(c->verify_store);
244 X509_STORE_free(c->chain_store);
245 custom_exts_free(&c->custext);
246 #ifndef OPENSSL_NO_PSK
247 OPENSSL_free(c->psk_identity_hint);
249 CRYPTO_THREAD_lock_free(c->lock);
253 int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
256 CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
257 SSL_CTX *realctx = s != NULL ? s->ctx : ctx;
261 for (i = 0; i < sk_X509_num(chain); i++) {
262 X509 *x = sk_X509_value(chain, i);
264 if (!X509v3_cache_extensions(x, realctx->libctx, realctx->propq)) {
265 SSLerr(0, ERR_LIB_X509);
269 r = ssl_security_cert(s, ctx, x, 0, 0);
271 SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r);
275 sk_X509_pop_free(cpk->chain, X509_free);
280 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
282 STACK_OF(X509) *dchain;
284 return ssl_cert_set0_chain(s, ctx, NULL);
285 dchain = X509_chain_up_ref(chain);
288 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
289 sk_X509_pop_free(dchain, X509_free);
295 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
298 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
301 r = ssl_security_cert(s, ctx, x, 0, 0);
303 SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r);
307 cpk->chain = sk_X509_new_null();
308 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
313 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
315 if (!ssl_cert_add0_chain_cert(s, ctx, x))
321 int ssl_cert_select_current(CERT *c, X509 *x)
326 for (i = 0; i < SSL_PKEY_NUM; i++) {
327 CERT_PKEY *cpk = c->pkeys + i;
328 if (cpk->x509 == x && cpk->privatekey) {
334 for (i = 0; i < SSL_PKEY_NUM; i++) {
335 CERT_PKEY *cpk = c->pkeys + i;
336 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
344 int ssl_cert_set_current(CERT *c, long op)
349 if (op == SSL_CERT_SET_FIRST)
351 else if (op == SSL_CERT_SET_NEXT) {
352 idx = (int)(c->key - c->pkeys + 1);
353 if (idx >= SSL_PKEY_NUM)
357 for (i = idx; i < SSL_PKEY_NUM; i++) {
358 CERT_PKEY *cpk = c->pkeys + i;
359 if (cpk->x509 && cpk->privatekey) {
367 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
370 c->cert_cb_arg = arg;
373 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
377 X509_STORE *verify_store;
378 X509_STORE_CTX *ctx = NULL;
379 X509_VERIFY_PARAM *param;
381 if ((sk == NULL) || (sk_X509_num(sk) == 0))
384 if (s->cert->verify_store)
385 verify_store = s->cert->verify_store;
387 verify_store = s->ctx->cert_store;
389 ctx = X509_STORE_CTX_new_with_libctx(s->ctx->libctx, s->ctx->propq);
391 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
395 x = sk_X509_value(sk, 0);
396 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
397 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
400 param = X509_STORE_CTX_get0_param(ctx);
402 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
403 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
404 * and PKI authentication.
406 X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));
408 /* Set suite B flags if needed */
409 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
410 if (!X509_STORE_CTX_set_ex_data
411 (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
415 /* Verify via DANE if enabled */
416 if (DANETLS_ENABLED(&s->dane))
417 X509_STORE_CTX_set0_dane(ctx, &s->dane);
420 * We need to inherit the verify parameters. These can be determined by
421 * the context: if its a server it will verify SSL client certificates or
425 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
427 * Anything non-default in "s->param" should overwrite anything in the ctx.
429 X509_VERIFY_PARAM_set1(param, s->param);
431 if (s->verify_callback)
432 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
434 if (s->ctx->app_verify_callback != NULL)
435 i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);
437 i = X509_verify_cert(ctx);
439 s->verify_result = X509_STORE_CTX_get_error(ctx);
440 sk_X509_pop_free(s->verified_chain, X509_free);
441 s->verified_chain = NULL;
442 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
443 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
444 if (s->verified_chain == NULL) {
445 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
450 /* Move peername from the store context params to the SSL handle's */
451 X509_VERIFY_PARAM_move_peername(s->param, param);
454 X509_STORE_CTX_free(ctx);
458 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
459 STACK_OF(X509_NAME) *name_list)
461 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
462 *ca_list = name_list;
465 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
468 const int num = sk_X509_NAME_num(sk);
469 STACK_OF(X509_NAME) *ret;
472 ret = sk_X509_NAME_new_reserve(NULL, num);
474 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
477 for (i = 0; i < num; i++) {
478 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
480 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
481 sk_X509_NAME_pop_free(ret, X509_NAME_free);
484 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
489 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
491 set0_CA_list(&s->ca_names, name_list);
494 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
496 set0_CA_list(&ctx->ca_names, name_list);
499 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
501 return ctx->ca_names;
504 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
506 return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;
509 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
511 set0_CA_list(&ctx->client_ca_names, name_list);
514 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
516 return ctx->client_ca_names;
519 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
521 set0_CA_list(&s->client_ca_names, name_list);
524 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
526 return s->s3.tmp.peer_ca_names;
529 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
532 return s->s3.tmp.peer_ca_names;
533 return s->client_ca_names != NULL ? s->client_ca_names
534 : s->ctx->client_ca_names;
537 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
543 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
546 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
549 if (!sk_X509_NAME_push(*sk, name)) {
550 X509_NAME_free(name);
556 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
558 return add_ca_name(&ssl->ca_names, x);
561 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
563 return add_ca_name(&ctx->ca_names, x);
567 * The following two are older names are to be replaced with
568 * SSL(_CTX)_add1_to_CA_list
570 int SSL_add_client_CA(SSL *ssl, X509 *x)
572 return add_ca_name(&ssl->client_ca_names, x);
575 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
577 return add_ca_name(&ctx->client_ca_names, x);
580 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
582 unsigned char *abuf = NULL, *bbuf = NULL;
585 /* X509_NAME_cmp() itself casts away constness in this way, so
588 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
589 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
591 if (alen < 0 || blen < 0)
593 else if (alen != blen)
595 else /* alen == blen */
596 ret = memcmp(abuf, bbuf, alen);
604 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
606 return xname_cmp(*a, *b);
609 static unsigned long xname_hash(const X509_NAME *a)
611 return X509_NAME_hash((X509_NAME *)a);
614 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
616 BIO *in = BIO_new(BIO_s_file());
618 X509_NAME *xn = NULL;
619 STACK_OF(X509_NAME) *ret = NULL;
620 LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
622 if ((name_hash == NULL) || (in == NULL)) {
623 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
627 if (!BIO_read_filename(in, file))
631 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
634 ret = sk_X509_NAME_new_null();
636 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
640 if ((xn = X509_get_subject_name(x)) == NULL)
642 /* check for duplicates */
643 xn = X509_NAME_dup(xn);
646 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
651 lh_X509_NAME_insert(name_hash, xn);
652 if (!sk_X509_NAME_push(ret, xn))
660 sk_X509_NAME_pop_free(ret, X509_NAME_free);
665 lh_X509_NAME_free(name_hash);
671 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
676 X509_NAME *xn = NULL;
678 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
680 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
682 in = BIO_new(BIO_s_file());
685 SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, ERR_R_MALLOC_FAILURE);
689 if (!BIO_read_filename(in, file))
693 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
695 if ((xn = X509_get_subject_name(x)) == NULL)
697 xn = X509_NAME_dup(xn);
700 if (sk_X509_NAME_find(stack, xn) >= 0) {
703 } else if (!sk_X509_NAME_push(stack, xn)) {
717 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
721 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
724 OPENSSL_DIR_CTX *d = NULL;
725 const char *filename;
728 /* Note that a side effect is that the CAs will be sorted by name */
730 while ((filename = OPENSSL_DIR_read(&d, dir))) {
734 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
735 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
736 SSL_R_PATH_TOO_LONG);
739 #ifdef OPENSSL_SYS_VMS
740 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
742 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
744 if (r <= 0 || r >= (int)sizeof(buf))
746 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
751 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
752 "calling OPENSSL_dir_read(%s)",
754 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
767 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
768 const char *uri, int depth)
771 OSSL_STORE_CTX *ctx = NULL;
773 X509_NAME *xn = NULL;
775 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
778 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
779 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
780 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
785 if (infotype == OSSL_STORE_INFO_NAME) {
787 * This is an entry in the "directory" represented by the current
788 * uri. if |depth| allows, dive into it.
791 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
793 } else if (infotype == OSSL_STORE_INFO_CERT) {
794 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
795 || (xn = X509_get_subject_name(x)) == NULL
796 || (xn = X509_NAME_dup(xn)) == NULL)
798 if (sk_X509_NAME_find(stack, xn) >= 0) {
801 } else if (!sk_X509_NAME_push(stack, xn)) {
807 OSSL_STORE_INFO_free(info);
816 OSSL_STORE_close(ctx);
821 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
824 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
825 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
826 int ret = add_uris_recursive(stack, store, 1);
828 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
832 /* Build a certificate chain for current certificate */
833 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
835 CERT *c = s ? s->cert : ctx->cert;
836 CERT_PKEY *cpk = c->key;
837 X509_STORE *chain_store = NULL;
838 X509_STORE_CTX *xs_ctx = NULL;
839 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
844 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
847 /* Rearranging and check the chain: add everything to a store */
848 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
849 chain_store = X509_STORE_new();
850 if (chain_store == NULL)
852 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
853 x = sk_X509_value(cpk->chain, i);
854 if (!X509_STORE_add_cert(chain_store, x))
857 /* Add EE cert too: it might be self signed */
858 if (!X509_STORE_add_cert(chain_store, cpk->x509))
862 chain_store = c->chain_store;
864 chain_store = s->ctx->cert_store;
866 chain_store = ctx->cert_store;
868 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
869 untrusted = cpk->chain;
872 xs_ctx = X509_STORE_CTX_new_with_libctx(s->ctx->libctx, s->ctx->propq);
873 if (xs_ctx == NULL) {
874 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
877 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
878 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
881 /* Set suite B flags if needed */
882 X509_STORE_CTX_set_flags(xs_ctx,
883 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
885 i = X509_verify_cert(xs_ctx);
886 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
887 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
893 chain = X509_STORE_CTX_get1_chain(xs_ctx);
895 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
896 i = X509_STORE_CTX_get_error(xs_ctx);
897 ERR_add_error_data(2, "Verify error:",
898 X509_verify_cert_error_string(i));
902 /* Remove EE certificate from chain */
903 x = sk_X509_shift(chain);
905 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
906 if (sk_X509_num(chain) > 0) {
907 /* See if last cert is self signed */
908 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
909 if (X509_get_extension_flags(x) & EXFLAG_SS) {
910 x = sk_X509_pop(chain);
916 * Check security level of all CA certificates: EE will have been checked
919 for (i = 0; i < sk_X509_num(chain); i++) {
920 x = sk_X509_value(chain, i);
921 rv = ssl_security_cert(s, ctx, x, 0, 0);
923 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv);
924 sk_X509_pop_free(chain, X509_free);
929 sk_X509_pop_free(cpk->chain, X509_free);
934 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
935 X509_STORE_free(chain_store);
936 X509_STORE_CTX_free(xs_ctx);
941 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
945 pstore = &c->chain_store;
947 pstore = &c->verify_store;
948 X509_STORE_free(*pstore);
951 X509_STORE_up_ref(store);
955 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
956 int op, int bits, int nid, void *other,
960 static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
962 level = SSL_CTX_get_security_level(ctx);
964 level = SSL_get_security_level(s);
968 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
971 if (op == SSL_SECOP_TMP_DH && bits < 80)
977 minbits = minbits_table[level - 1];
979 case SSL_SECOP_CIPHER_SUPPORTED:
980 case SSL_SECOP_CIPHER_SHARED:
981 case SSL_SECOP_CIPHER_CHECK:
983 const SSL_CIPHER *c = other;
984 /* No ciphers below security level */
987 /* No unauthenticated ciphersuites */
988 if (c->algorithm_auth & SSL_aNULL)
990 /* No MD5 mac ciphersuites */
991 if (c->algorithm_mac & SSL_MD5)
993 /* SHA1 HMAC is 160 bits of security */
994 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
996 /* Level 2: no RC4 */
997 if (level >= 2 && c->algorithm_enc == SSL_RC4)
999 /* Level 3: forward secure ciphersuites only */
1000 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1001 !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1005 case SSL_SECOP_VERSION:
1006 if (!SSL_IS_DTLS(s)) {
1007 /* SSLv3 not allowed at level 2 */
1008 if (nid <= SSL3_VERSION && level >= 2)
1010 /* TLS v1.1 and above only for level 3 */
1011 if (nid <= TLS1_VERSION && level >= 3)
1013 /* TLS v1.2 only for level 4 and above */
1014 if (nid <= TLS1_1_VERSION && level >= 4)
1017 /* DTLS v1.2 only for level 4 and above */
1018 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
1023 case SSL_SECOP_COMPRESSION:
1027 case SSL_SECOP_TICKET:
1038 int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
1040 return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1043 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1045 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1049 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1053 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1054 if (ssl_cert_info[i].nid == nid) {
1063 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1065 int nid = EVP_PKEY_id(pk);
1068 if (nid == NID_undef)
1071 if (!ssl_cert_lookup_by_nid(nid, &tmpidx))
1077 return &ssl_cert_info[tmpidx];
1080 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1082 if (idx >= OSSL_NELEM(ssl_cert_info))
1084 return &ssl_cert_info[idx];