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 ? s->cert->key : ctx->cert->key;
259 for (i = 0; i < sk_X509_num(chain); i++) {
260 r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0);
262 SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r);
266 sk_X509_pop_free(cpk->chain, X509_free);
271 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
273 STACK_OF(X509) *dchain;
275 return ssl_cert_set0_chain(s, ctx, NULL);
276 dchain = X509_chain_up_ref(chain);
279 if (!ssl_cert_set0_chain(s, ctx, dchain)) {
280 sk_X509_pop_free(dchain, X509_free);
286 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
289 CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
292 r = ssl_security_cert(s, ctx, x, 0, 0);
294 SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r);
298 cpk->chain = sk_X509_new_null();
299 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
304 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
306 if (!ssl_cert_add0_chain_cert(s, ctx, x))
312 int ssl_cert_select_current(CERT *c, X509 *x)
317 for (i = 0; i < SSL_PKEY_NUM; i++) {
318 CERT_PKEY *cpk = c->pkeys + i;
319 if (cpk->x509 == x && cpk->privatekey) {
325 for (i = 0; i < SSL_PKEY_NUM; i++) {
326 CERT_PKEY *cpk = c->pkeys + i;
327 if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
335 int ssl_cert_set_current(CERT *c, long op)
340 if (op == SSL_CERT_SET_FIRST)
342 else if (op == SSL_CERT_SET_NEXT) {
343 idx = (int)(c->key - c->pkeys + 1);
344 if (idx >= SSL_PKEY_NUM)
348 for (i = idx; i < SSL_PKEY_NUM; i++) {
349 CERT_PKEY *cpk = c->pkeys + i;
350 if (cpk->x509 && cpk->privatekey) {
358 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
361 c->cert_cb_arg = arg;
364 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
368 X509_STORE *verify_store;
369 X509_STORE_CTX *ctx = NULL;
370 X509_VERIFY_PARAM *param;
372 if ((sk == NULL) || (sk_X509_num(sk) == 0))
375 if (s->cert->verify_store)
376 verify_store = s->cert->verify_store;
378 verify_store = s->ctx->cert_store;
380 ctx = X509_STORE_CTX_new();
382 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
386 x = sk_X509_value(sk, 0);
387 if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
388 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
391 param = X509_STORE_CTX_get0_param(ctx);
393 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
394 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
395 * and PKI authentication.
397 X509_VERIFY_PARAM_set_auth_level(param, SSL_get_security_level(s));
399 /* Set suite B flags if needed */
400 X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
401 if (!X509_STORE_CTX_set_ex_data
402 (ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
406 /* Verify via DANE if enabled */
407 if (DANETLS_ENABLED(&s->dane))
408 X509_STORE_CTX_set0_dane(ctx, &s->dane);
411 * We need to inherit the verify parameters. These can be determined by
412 * the context: if its a server it will verify SSL client certificates or
416 X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
418 * Anything non-default in "s->param" should overwrite anything in the ctx.
420 X509_VERIFY_PARAM_set1(param, s->param);
422 if (s->verify_callback)
423 X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
425 if (s->ctx->app_verify_callback != NULL)
426 i = s->ctx->app_verify_callback(ctx, s->ctx->app_verify_arg);
428 i = X509_verify_cert(ctx);
430 s->verify_result = X509_STORE_CTX_get_error(ctx);
431 sk_X509_pop_free(s->verified_chain, X509_free);
432 s->verified_chain = NULL;
433 if (X509_STORE_CTX_get0_chain(ctx) != NULL) {
434 s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
435 if (s->verified_chain == NULL) {
436 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
441 /* Move peername from the store context params to the SSL handle's */
442 X509_VERIFY_PARAM_move_peername(s->param, param);
445 X509_STORE_CTX_free(ctx);
449 static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
450 STACK_OF(X509_NAME) *name_list)
452 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
453 *ca_list = name_list;
456 STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
459 const int num = sk_X509_NAME_num(sk);
460 STACK_OF(X509_NAME) *ret;
463 ret = sk_X509_NAME_new_reserve(NULL, num);
465 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
468 for (i = 0; i < num; i++) {
469 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
471 SSLerr(SSL_F_SSL_DUP_CA_LIST, ERR_R_MALLOC_FAILURE);
472 sk_X509_NAME_pop_free(ret, X509_NAME_free);
475 sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
480 void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
482 set0_CA_list(&s->ca_names, name_list);
485 void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
487 set0_CA_list(&ctx->ca_names, name_list);
490 const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
492 return ctx->ca_names;
495 const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
497 return s->ca_names != NULL ? s->ca_names : s->ctx->ca_names;
500 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
502 set0_CA_list(&ctx->client_ca_names, name_list);
505 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
507 return ctx->client_ca_names;
510 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
512 set0_CA_list(&s->client_ca_names, name_list);
515 const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
517 return s->s3.tmp.peer_ca_names;
520 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
523 return s->s3.tmp.peer_ca_names;
524 return s->client_ca_names != NULL ? s->client_ca_names
525 : s->ctx->client_ca_names;
528 static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
534 if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
537 if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
540 if (!sk_X509_NAME_push(*sk, name)) {
541 X509_NAME_free(name);
547 int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
549 return add_ca_name(&ssl->ca_names, x);
552 int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
554 return add_ca_name(&ctx->ca_names, x);
558 * The following two are older names are to be replaced with
559 * SSL(_CTX)_add1_to_CA_list
561 int SSL_add_client_CA(SSL *ssl, X509 *x)
563 return add_ca_name(&ssl->client_ca_names, x);
566 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
568 return add_ca_name(&ctx->client_ca_names, x);
571 static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
573 unsigned char *abuf = NULL, *bbuf = NULL;
576 /* X509_NAME_cmp() itself casts away constness in this way, so
579 alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
580 blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
582 if (alen < 0 || blen < 0)
584 else if (alen != blen)
586 else /* alen == blen */
587 ret = memcmp(abuf, bbuf, alen);
595 static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
597 return xname_cmp(*a, *b);
600 static unsigned long xname_hash(const X509_NAME *a)
602 return X509_NAME_hash((X509_NAME *)a);
606 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
607 * it doesn't really have anything to do with clients (except that a common use
608 * for a stack of CAs is to send it to the client). Actually, it doesn't have
609 * much to do with CAs, either, since it will load any old cert.
610 * \param file the file containing one or more certs.
611 * \return a ::STACK containing the certs.
613 STACK_OF(X509_NAME) *SSL_load_client_CA_file(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);
621 if ((name_hash == NULL) || (in == NULL)) {
622 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
626 if (!BIO_read_filename(in, file))
630 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
633 ret = sk_X509_NAME_new_null();
635 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
639 if ((xn = X509_get_subject_name(x)) == NULL)
641 /* check for duplicates */
642 xn = X509_NAME_dup(xn);
645 if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
650 lh_X509_NAME_insert(name_hash, xn);
651 if (!sk_X509_NAME_push(ret, xn))
659 sk_X509_NAME_pop_free(ret, X509_NAME_free);
664 lh_X509_NAME_free(name_hash);
671 * Add a file of certs to a stack.
672 * \param stack the stack to add to.
673 * \param file the file to add from. All certs in this file that are not
674 * already in the stack will be added.
675 * \return 1 for success, 0 for failure. Note that in the case of failure some
676 * certs may have been added to \c stack.
679 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
684 X509_NAME *xn = NULL;
686 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
688 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
690 in = BIO_new(BIO_s_file());
693 SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, ERR_R_MALLOC_FAILURE);
697 if (!BIO_read_filename(in, file))
701 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
703 if ((xn = X509_get_subject_name(x)) == NULL)
705 xn = X509_NAME_dup(xn);
708 if (sk_X509_NAME_find(stack, xn) >= 0) {
711 } else if (!sk_X509_NAME_push(stack, xn)) {
725 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
730 * Add a directory of certs to a stack.
731 * \param stack the stack to append to.
732 * \param dir the directory to append from. All files in this directory will be
733 * examined as potential certs. Any that are acceptable to
734 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
736 * \return 1 for success, 0 for failure. Note that in the case of failure some
737 * certs may have been added to \c stack.
740 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
743 OPENSSL_DIR_CTX *d = NULL;
744 const char *filename;
747 /* Note that a side effect is that the CAs will be sorted by name */
749 while ((filename = OPENSSL_DIR_read(&d, dir))) {
753 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
754 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
755 SSL_R_PATH_TOO_LONG);
758 #ifdef OPENSSL_SYS_VMS
759 r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
761 r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
763 if (r <= 0 || r >= (int)sizeof(buf))
765 if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
770 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
771 "calling OPENSSL_dir_read(%s)",
773 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
787 * Add a container of certs to a stack.
788 * \param stack the stack to add to.
789 * \param file the file to add from. All certs in this file that are not
790 * already in the stack will be added.
791 * \return 1 for success, 0 for failure. Note that in the case of failure some
792 * certs may have been added to \c stack.
795 static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
796 const char *uri, int depth)
799 OSSL_STORE_CTX *ctx = NULL;
801 X509_NAME *xn = NULL;
803 if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
806 while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
807 OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
808 int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
813 if (infotype == OSSL_STORE_INFO_NAME) {
815 * This is an entry in the "directory" represented by the current
816 * uri. if |depth| allows, dive into it.
819 ok = add_uris_recursive(stack, uri, depth - 1);
820 } else if (infotype == OSSL_STORE_INFO_CERT) {
821 if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
822 || (xn = X509_get_subject_name(x)) == NULL
823 || (xn = X509_NAME_dup(xn)) == NULL)
825 if (sk_X509_NAME_find(stack, xn) >= 0) {
828 } else if (!sk_X509_NAME_push(stack, xn)) {
834 OSSL_STORE_INFO_free(info);
843 OSSL_STORE_close(ctx);
848 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
851 int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
852 = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
853 int ret = add_uris_recursive(stack, store, 1);
855 (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
859 /* Build a certificate chain for current certificate */
860 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
862 CERT *c = s ? s->cert : ctx->cert;
863 CERT_PKEY *cpk = c->key;
864 X509_STORE *chain_store = NULL;
865 X509_STORE_CTX *xs_ctx = NULL;
866 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
871 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
874 /* Rearranging and check the chain: add everything to a store */
875 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
876 chain_store = X509_STORE_new();
877 if (chain_store == NULL)
879 for (i = 0; i < sk_X509_num(cpk->chain); i++) {
880 x = sk_X509_value(cpk->chain, i);
881 if (!X509_STORE_add_cert(chain_store, x))
884 /* Add EE cert too: it might be self signed */
885 if (!X509_STORE_add_cert(chain_store, cpk->x509))
889 chain_store = c->chain_store;
891 chain_store = s->ctx->cert_store;
893 chain_store = ctx->cert_store;
895 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
896 untrusted = cpk->chain;
899 xs_ctx = X509_STORE_CTX_new();
900 if (xs_ctx == NULL) {
901 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
904 if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
905 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
908 /* Set suite B flags if needed */
909 X509_STORE_CTX_set_flags(xs_ctx,
910 c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
912 i = X509_verify_cert(xs_ctx);
913 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
914 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
920 chain = X509_STORE_CTX_get1_chain(xs_ctx);
922 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
923 i = X509_STORE_CTX_get_error(xs_ctx);
924 ERR_add_error_data(2, "Verify error:",
925 X509_verify_cert_error_string(i));
929 /* Remove EE certificate from chain */
930 x = sk_X509_shift(chain);
932 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
933 if (sk_X509_num(chain) > 0) {
934 /* See if last cert is self signed */
935 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
936 if (X509_get_extension_flags(x) & EXFLAG_SS) {
937 x = sk_X509_pop(chain);
943 * Check security level of all CA certificates: EE will have been checked
946 for (i = 0; i < sk_X509_num(chain); i++) {
947 x = sk_X509_value(chain, i);
948 rv = ssl_security_cert(s, ctx, x, 0, 0);
950 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv);
951 sk_X509_pop_free(chain, X509_free);
956 sk_X509_pop_free(cpk->chain, X509_free);
961 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
962 X509_STORE_free(chain_store);
963 X509_STORE_CTX_free(xs_ctx);
968 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
972 pstore = &c->chain_store;
974 pstore = &c->verify_store;
975 X509_STORE_free(*pstore);
978 X509_STORE_up_ref(store);
982 static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
983 int op, int bits, int nid, void *other,
987 static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
989 level = SSL_CTX_get_security_level(ctx);
991 level = SSL_get_security_level(s);
995 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
998 if (op == SSL_SECOP_TMP_DH && bits < 80)
1004 minbits = minbits_table[level - 1];
1006 case SSL_SECOP_CIPHER_SUPPORTED:
1007 case SSL_SECOP_CIPHER_SHARED:
1008 case SSL_SECOP_CIPHER_CHECK:
1010 const SSL_CIPHER *c = other;
1011 /* No ciphers below security level */
1014 /* No unauthenticated ciphersuites */
1015 if (c->algorithm_auth & SSL_aNULL)
1017 /* No MD5 mac ciphersuites */
1018 if (c->algorithm_mac & SSL_MD5)
1020 /* SHA1 HMAC is 160 bits of security */
1021 if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1023 /* Level 2: no RC4 */
1024 if (level >= 2 && c->algorithm_enc == SSL_RC4)
1026 /* Level 3: forward secure ciphersuites only */
1027 if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1028 !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1032 case SSL_SECOP_VERSION:
1033 if (!SSL_IS_DTLS(s)) {
1034 /* SSLv3 not allowed at level 2 */
1035 if (nid <= SSL3_VERSION && level >= 2)
1037 /* TLS v1.1 and above only for level 3 */
1038 if (nid <= TLS1_VERSION && level >= 3)
1040 /* TLS v1.2 only for level 4 and above */
1041 if (nid <= TLS1_1_VERSION && level >= 4)
1044 /* DTLS v1.2 only for level 4 and above */
1045 if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
1050 case SSL_SECOP_COMPRESSION:
1054 case SSL_SECOP_TICKET:
1065 int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
1067 return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1070 int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1072 return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1076 int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
1080 for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1081 if (ssl_cert_info[i].nid == nid) {
1090 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
1092 int nid = EVP_PKEY_id(pk);
1095 if (nid == NID_undef)
1098 if (!ssl_cert_lookup_by_nid(nid, &tmpidx))
1104 return &ssl_cert_info[tmpidx];
1107 const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
1109 if (idx >= OSSL_NELEM(ssl_cert_info))
1111 return &ssl_cert_info[idx];