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/x509v3.h>
19 #include <openssl/dh.h>
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include "internal/refcount.h"
24 #include "ssl_cert_table.h"
25 #include "internal/thread_once.h"
27 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
28 int op
, int bits
, int nid
, void *other
,
31 static CRYPTO_ONCE ssl_x509_store_ctx_once
= CRYPTO_ONCE_STATIC_INIT
;
32 static volatile int ssl_x509_store_ctx_idx
= -1;
34 DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init
)
36 ssl_x509_store_ctx_idx
= X509_STORE_CTX_get_ex_new_index(0,
37 "SSL for verify callback",
39 return ssl_x509_store_ctx_idx
>= 0;
42 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
45 if (!RUN_ONCE(&ssl_x509_store_ctx_once
, ssl_x509_store_ctx_init
))
47 return ssl_x509_store_ctx_idx
;
50 CERT
*ssl_cert_new(void)
52 CERT
*ret
= OPENSSL_zalloc(sizeof(*ret
));
55 SSLerr(SSL_F_SSL_CERT_NEW
, ERR_R_MALLOC_FAILURE
);
59 ret
->key
= &(ret
->pkeys
[SSL_PKEY_RSA
]);
61 ret
->sec_cb
= ssl_security_default_callback
;
62 ret
->sec_level
= OPENSSL_TLS_SECURITY_LEVEL
;
64 ret
->lock
= CRYPTO_THREAD_lock_new();
65 if (ret
->lock
== NULL
) {
66 SSLerr(SSL_F_SSL_CERT_NEW
, ERR_R_MALLOC_FAILURE
);
74 CERT
*ssl_cert_dup(CERT
*cert
)
76 CERT
*ret
= OPENSSL_zalloc(sizeof(*ret
));
80 SSLerr(SSL_F_SSL_CERT_DUP
, ERR_R_MALLOC_FAILURE
);
85 ret
->key
= &ret
->pkeys
[cert
->key
- cert
->pkeys
];
86 ret
->lock
= CRYPTO_THREAD_lock_new();
87 if (ret
->lock
== NULL
) {
88 SSLerr(SSL_F_SSL_CERT_DUP
, ERR_R_MALLOC_FAILURE
);
93 if (cert
->dh_tmp
!= NULL
) {
94 ret
->dh_tmp
= cert
->dh_tmp
;
95 EVP_PKEY_up_ref(ret
->dh_tmp
);
97 ret
->dh_tmp_cb
= cert
->dh_tmp_cb
;
98 ret
->dh_tmp_auto
= cert
->dh_tmp_auto
;
101 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
102 CERT_PKEY
*cpk
= cert
->pkeys
+ i
;
103 CERT_PKEY
*rpk
= ret
->pkeys
+ i
;
104 if (cpk
->x509
!= NULL
) {
105 rpk
->x509
= cpk
->x509
;
106 X509_up_ref(rpk
->x509
);
109 if (cpk
->privatekey
!= NULL
) {
110 rpk
->privatekey
= cpk
->privatekey
;
111 EVP_PKEY_up_ref(cpk
->privatekey
);
115 rpk
->chain
= X509_chain_up_ref(cpk
->chain
);
117 SSLerr(SSL_F_SSL_CERT_DUP
, ERR_R_MALLOC_FAILURE
);
121 if (cert
->pkeys
[i
].serverinfo
!= NULL
) {
122 /* Just copy everything. */
123 ret
->pkeys
[i
].serverinfo
=
124 OPENSSL_malloc(cert
->pkeys
[i
].serverinfo_length
);
125 if (ret
->pkeys
[i
].serverinfo
== NULL
) {
126 SSLerr(SSL_F_SSL_CERT_DUP
, ERR_R_MALLOC_FAILURE
);
129 ret
->pkeys
[i
].serverinfo_length
= cert
->pkeys
[i
].serverinfo_length
;
130 memcpy(ret
->pkeys
[i
].serverinfo
,
131 cert
->pkeys
[i
].serverinfo
, cert
->pkeys
[i
].serverinfo_length
);
135 /* Configured sigalgs copied across */
136 if (cert
->conf_sigalgs
) {
137 ret
->conf_sigalgs
= OPENSSL_malloc(cert
->conf_sigalgslen
138 * sizeof(*cert
->conf_sigalgs
));
139 if (ret
->conf_sigalgs
== NULL
)
141 memcpy(ret
->conf_sigalgs
, cert
->conf_sigalgs
,
142 cert
->conf_sigalgslen
* sizeof(*cert
->conf_sigalgs
));
143 ret
->conf_sigalgslen
= cert
->conf_sigalgslen
;
145 ret
->conf_sigalgs
= NULL
;
147 if (cert
->client_sigalgs
) {
148 ret
->client_sigalgs
= OPENSSL_malloc(cert
->client_sigalgslen
149 * sizeof(*cert
->client_sigalgs
));
150 if (ret
->client_sigalgs
== NULL
)
152 memcpy(ret
->client_sigalgs
, cert
->client_sigalgs
,
153 cert
->client_sigalgslen
* sizeof(*cert
->client_sigalgs
));
154 ret
->client_sigalgslen
= cert
->client_sigalgslen
;
156 ret
->client_sigalgs
= NULL
;
157 /* Copy any custom client certificate types */
159 ret
->ctype
= OPENSSL_memdup(cert
->ctype
, cert
->ctype_len
);
160 if (ret
->ctype
== NULL
)
162 ret
->ctype_len
= cert
->ctype_len
;
165 ret
->cert_flags
= cert
->cert_flags
;
167 ret
->cert_cb
= cert
->cert_cb
;
168 ret
->cert_cb_arg
= cert
->cert_cb_arg
;
170 if (cert
->verify_store
) {
171 X509_STORE_up_ref(cert
->verify_store
);
172 ret
->verify_store
= cert
->verify_store
;
175 if (cert
->chain_store
) {
176 X509_STORE_up_ref(cert
->chain_store
);
177 ret
->chain_store
= cert
->chain_store
;
180 ret
->sec_cb
= cert
->sec_cb
;
181 ret
->sec_level
= cert
->sec_level
;
182 ret
->sec_ex
= cert
->sec_ex
;
184 if (!custom_exts_copy(&ret
->custext
, &cert
->custext
))
186 #ifndef OPENSSL_NO_PSK
187 if (cert
->psk_identity_hint
) {
188 ret
->psk_identity_hint
= OPENSSL_strdup(cert
->psk_identity_hint
);
189 if (ret
->psk_identity_hint
== NULL
)
201 /* Free up and clear all certificates and chains */
203 void ssl_cert_clear_certs(CERT
*c
)
208 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
209 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
210 X509_free(cpk
->x509
);
212 EVP_PKEY_free(cpk
->privatekey
);
213 cpk
->privatekey
= NULL
;
214 sk_X509_pop_free(cpk
->chain
, X509_free
);
216 OPENSSL_free(cpk
->serverinfo
);
217 cpk
->serverinfo
= NULL
;
218 cpk
->serverinfo_length
= 0;
222 void ssl_cert_free(CERT
*c
)
228 CRYPTO_DOWN_REF(&c
->references
, &i
, c
->lock
);
229 REF_PRINT_COUNT("CERT", c
);
232 REF_ASSERT_ISNT(i
< 0);
234 #ifndef OPENSSL_NO_DH
235 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
? s
->cert
->key
: ctx
->cert
->key
;
258 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
259 r
= ssl_security_cert(s
, ctx
, sk_X509_value(chain
, i
), 0, 0);
261 SSLerr(SSL_F_SSL_CERT_SET0_CHAIN
, r
);
265 sk_X509_pop_free(cpk
->chain
, X509_free
);
270 int ssl_cert_set1_chain(SSL
*s
, SSL_CTX
*ctx
, STACK_OF(X509
) *chain
)
272 STACK_OF(X509
) *dchain
;
274 return ssl_cert_set0_chain(s
, ctx
, NULL
);
275 dchain
= X509_chain_up_ref(chain
);
278 if (!ssl_cert_set0_chain(s
, ctx
, dchain
)) {
279 sk_X509_pop_free(dchain
, X509_free
);
285 int ssl_cert_add0_chain_cert(SSL
*s
, SSL_CTX
*ctx
, X509
*x
)
288 CERT_PKEY
*cpk
= s
? s
->cert
->key
: ctx
->cert
->key
;
291 r
= ssl_security_cert(s
, ctx
, x
, 0, 0);
293 SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT
, r
);
297 cpk
->chain
= sk_X509_new_null();
298 if (!cpk
->chain
|| !sk_X509_push(cpk
->chain
, x
))
303 int ssl_cert_add1_chain_cert(SSL
*s
, SSL_CTX
*ctx
, X509
*x
)
305 if (!ssl_cert_add0_chain_cert(s
, ctx
, x
))
311 int ssl_cert_select_current(CERT
*c
, X509
*x
)
316 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
317 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
318 if (cpk
->x509
== x
&& cpk
->privatekey
) {
324 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
325 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
326 if (cpk
->privatekey
&& cpk
->x509
&& !X509_cmp(cpk
->x509
, x
)) {
334 int ssl_cert_set_current(CERT
*c
, long op
)
339 if (op
== SSL_CERT_SET_FIRST
)
341 else if (op
== SSL_CERT_SET_NEXT
) {
342 idx
= (int)(c
->key
- c
->pkeys
+ 1);
343 if (idx
>= SSL_PKEY_NUM
)
347 for (i
= idx
; i
< SSL_PKEY_NUM
; i
++) {
348 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
349 if (cpk
->x509
&& cpk
->privatekey
) {
357 void ssl_cert_set_cert_cb(CERT
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
360 c
->cert_cb_arg
= arg
;
363 int ssl_verify_cert_chain(SSL
*s
, STACK_OF(X509
) *sk
)
367 X509_STORE
*verify_store
;
368 X509_STORE_CTX
*ctx
= NULL
;
369 X509_VERIFY_PARAM
*param
;
371 if ((sk
== NULL
) || (sk_X509_num(sk
) == 0))
374 if (s
->cert
->verify_store
)
375 verify_store
= s
->cert
->verify_store
;
377 verify_store
= s
->ctx
->cert_store
;
379 ctx
= X509_STORE_CTX_new();
381 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN
, ERR_R_MALLOC_FAILURE
);
385 x
= sk_X509_value(sk
, 0);
386 if (!X509_STORE_CTX_init(ctx
, verify_store
, x
, sk
)) {
387 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN
, ERR_R_X509_LIB
);
390 param
= X509_STORE_CTX_get0_param(ctx
);
392 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
393 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
394 * and PKI authentication.
396 X509_VERIFY_PARAM_set_auth_level(param
, SSL_get_security_level(s
));
398 /* Set suite B flags if needed */
399 X509_STORE_CTX_set_flags(ctx
, tls1_suiteb(s
));
400 if (!X509_STORE_CTX_set_ex_data
401 (ctx
, SSL_get_ex_data_X509_STORE_CTX_idx(), s
)) {
405 /* Verify via DANE if enabled */
406 if (DANETLS_ENABLED(&s
->dane
))
407 X509_STORE_CTX_set0_dane(ctx
, &s
->dane
);
410 * We need to inherit the verify parameters. These can be determined by
411 * the context: if its a server it will verify SSL client certificates or
415 X509_STORE_CTX_set_default(ctx
, s
->server
? "ssl_client" : "ssl_server");
417 * Anything non-default in "s->param" should overwrite anything in the ctx.
419 X509_VERIFY_PARAM_set1(param
, s
->param
);
421 if (s
->verify_callback
)
422 X509_STORE_CTX_set_verify_cb(ctx
, s
->verify_callback
);
424 if (s
->ctx
->app_verify_callback
!= NULL
)
425 i
= s
->ctx
->app_verify_callback(ctx
, s
->ctx
->app_verify_arg
);
427 i
= X509_verify_cert(ctx
);
429 s
->verify_result
= X509_STORE_CTX_get_error(ctx
);
430 sk_X509_pop_free(s
->verified_chain
, X509_free
);
431 s
->verified_chain
= NULL
;
432 if (X509_STORE_CTX_get0_chain(ctx
) != NULL
) {
433 s
->verified_chain
= X509_STORE_CTX_get1_chain(ctx
);
434 if (s
->verified_chain
== NULL
) {
435 SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN
, ERR_R_MALLOC_FAILURE
);
440 /* Move peername from the store context params to the SSL handle's */
441 X509_VERIFY_PARAM_move_peername(s
->param
, param
);
444 X509_STORE_CTX_free(ctx
);
448 static void set0_CA_list(STACK_OF(X509_NAME
) **ca_list
,
449 STACK_OF(X509_NAME
) *name_list
)
451 sk_X509_NAME_pop_free(*ca_list
, X509_NAME_free
);
452 *ca_list
= name_list
;
455 STACK_OF(X509_NAME
) *SSL_dup_CA_list(const STACK_OF(X509_NAME
) *sk
)
458 const int num
= sk_X509_NAME_num(sk
);
459 STACK_OF(X509_NAME
) *ret
;
462 ret
= sk_X509_NAME_new_reserve(NULL
, num
);
464 SSLerr(SSL_F_SSL_DUP_CA_LIST
, ERR_R_MALLOC_FAILURE
);
467 for (i
= 0; i
< num
; i
++) {
468 name
= X509_NAME_dup(sk_X509_NAME_value(sk
, i
));
470 SSLerr(SSL_F_SSL_DUP_CA_LIST
, ERR_R_MALLOC_FAILURE
);
471 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
474 sk_X509_NAME_push(ret
, name
); /* Cannot fail after reserve call */
479 void SSL_set0_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
481 set0_CA_list(&s
->ca_names
, name_list
);
484 void SSL_CTX_set0_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
486 set0_CA_list(&ctx
->ca_names
, name_list
);
489 const STACK_OF(X509_NAME
) *SSL_CTX_get0_CA_list(const SSL_CTX
*ctx
)
491 return ctx
->ca_names
;
494 const STACK_OF(X509_NAME
) *SSL_get0_CA_list(const SSL
*s
)
496 return s
->ca_names
!= NULL
? s
->ca_names
: s
->ctx
->ca_names
;
499 void SSL_CTX_set_client_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
501 set0_CA_list(&ctx
->client_ca_names
, name_list
);
504 STACK_OF(X509_NAME
) *SSL_CTX_get_client_CA_list(const SSL_CTX
*ctx
)
506 return ctx
->client_ca_names
;
509 void SSL_set_client_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
511 set0_CA_list(&s
->client_ca_names
, name_list
);
514 const STACK_OF(X509_NAME
) *SSL_get0_peer_CA_list(const SSL
*s
)
516 return s
->s3
.tmp
.peer_ca_names
;
519 STACK_OF(X509_NAME
) *SSL_get_client_CA_list(const SSL
*s
)
522 return s
->s3
.tmp
.peer_ca_names
;
523 return s
->client_ca_names
!= NULL
? s
->client_ca_names
524 : s
->ctx
->client_ca_names
;
527 static int add_ca_name(STACK_OF(X509_NAME
) **sk
, const X509
*x
)
533 if (*sk
== NULL
&& ((*sk
= sk_X509_NAME_new_null()) == NULL
))
536 if ((name
= X509_NAME_dup(X509_get_subject_name(x
))) == NULL
)
539 if (!sk_X509_NAME_push(*sk
, name
)) {
540 X509_NAME_free(name
);
546 int SSL_add1_to_CA_list(SSL
*ssl
, const X509
*x
)
548 return add_ca_name(&ssl
->ca_names
, x
);
551 int SSL_CTX_add1_to_CA_list(SSL_CTX
*ctx
, const X509
*x
)
553 return add_ca_name(&ctx
->ca_names
, x
);
557 * The following two are older names are to be replaced with
558 * SSL(_CTX)_add1_to_CA_list
560 int SSL_add_client_CA(SSL
*ssl
, X509
*x
)
562 return add_ca_name(&ssl
->client_ca_names
, x
);
565 int SSL_CTX_add_client_CA(SSL_CTX
*ctx
, X509
*x
)
567 return add_ca_name(&ctx
->client_ca_names
, x
);
570 static int xname_cmp(const X509_NAME
*a
, const X509_NAME
*b
)
572 unsigned char *abuf
= NULL
, *bbuf
= NULL
;
575 /* X509_NAME_cmp() itself casts away constness in this way, so
578 alen
= i2d_X509_NAME((X509_NAME
*)a
, &abuf
);
579 blen
= i2d_X509_NAME((X509_NAME
*)b
, &bbuf
);
581 if (alen
< 0 || blen
< 0)
583 else if (alen
!= blen
)
585 else /* alen == blen */
586 ret
= memcmp(abuf
, bbuf
, alen
);
594 static int xname_sk_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
596 return xname_cmp(*a
, *b
);
599 static unsigned long xname_hash(const X509_NAME
*a
)
601 return X509_NAME_hash((X509_NAME
*)a
);
605 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
606 * it doesn't really have anything to do with clients (except that a common use
607 * for a stack of CAs is to send it to the client). Actually, it doesn't have
608 * much to do with CAs, either, since it will load any old cert.
609 * \param file the file containing one or more certs.
610 * \return a ::STACK containing the certs.
612 STACK_OF(X509_NAME
) *SSL_load_client_CA_file(const char *file
)
614 BIO
*in
= BIO_new(BIO_s_file());
616 X509_NAME
*xn
= NULL
;
617 STACK_OF(X509_NAME
) *ret
= NULL
;
618 LHASH_OF(X509_NAME
) *name_hash
= lh_X509_NAME_new(xname_hash
, xname_cmp
);
620 if ((name_hash
== NULL
) || (in
== NULL
)) {
621 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE
, ERR_R_MALLOC_FAILURE
);
625 if (!BIO_read_filename(in
, file
))
629 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
632 ret
= sk_X509_NAME_new_null();
634 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE
, ERR_R_MALLOC_FAILURE
);
638 if ((xn
= X509_get_subject_name(x
)) == NULL
)
640 /* check for duplicates */
641 xn
= X509_NAME_dup(xn
);
644 if (lh_X509_NAME_retrieve(name_hash
, xn
) != NULL
) {
649 lh_X509_NAME_insert(name_hash
, xn
);
650 if (!sk_X509_NAME_push(ret
, xn
))
658 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
663 lh_X509_NAME_free(name_hash
);
670 * Add a file of certs to a stack.
671 * \param stack the stack to add to.
672 * \param file the file to add from. All certs in this file that are not
673 * already in the stack will be added.
674 * \return 1 for success, 0 for failure. Note that in the case of failure some
675 * certs may have been added to \c stack.
678 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
683 X509_NAME
*xn
= NULL
;
685 int (*oldcmp
) (const X509_NAME
*const *a
, const X509_NAME
*const *b
);
687 oldcmp
= sk_X509_NAME_set_cmp_func(stack
, xname_sk_cmp
);
689 in
= BIO_new(BIO_s_file());
692 SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK
, ERR_R_MALLOC_FAILURE
);
696 if (!BIO_read_filename(in
, file
))
700 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
702 if ((xn
= X509_get_subject_name(x
)) == NULL
)
704 xn
= X509_NAME_dup(xn
);
707 if (sk_X509_NAME_find(stack
, xn
) >= 0) {
710 } else if (!sk_X509_NAME_push(stack
, xn
)) {
724 (void)sk_X509_NAME_set_cmp_func(stack
, oldcmp
);
729 * Add a directory of certs to a stack.
730 * \param stack the stack to append to.
731 * \param dir the directory to append from. All files in this directory will be
732 * examined as potential certs. Any that are acceptable to
733 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
735 * \return 1 for success, 0 for failure. Note that in the case of failure some
736 * certs may have been added to \c stack.
739 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
742 OPENSSL_DIR_CTX
*d
= NULL
;
743 const char *filename
;
746 /* Note that a side effect is that the CAs will be sorted by name */
748 while ((filename
= OPENSSL_DIR_read(&d
, dir
))) {
752 if (strlen(dir
) + strlen(filename
) + 2 > sizeof(buf
)) {
753 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK
,
754 SSL_R_PATH_TOO_LONG
);
757 #ifdef OPENSSL_SYS_VMS
758 r
= BIO_snprintf(buf
, sizeof(buf
), "%s%s", dir
, filename
);
760 r
= BIO_snprintf(buf
, sizeof(buf
), "%s/%s", dir
, filename
);
762 if (r
<= 0 || r
>= (int)sizeof(buf
))
764 if (!SSL_add_file_cert_subjects_to_stack(stack
, buf
))
769 FUNCerr("readdir", get_last_sys_error());
770 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir
, "')");
771 SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK
, ERR_R_SYS_LIB
);
784 /* Build a certificate chain for current certificate */
785 int ssl_build_cert_chain(SSL
*s
, SSL_CTX
*ctx
, int flags
)
787 CERT
*c
= s
? s
->cert
: ctx
->cert
;
788 CERT_PKEY
*cpk
= c
->key
;
789 X509_STORE
*chain_store
= NULL
;
790 X509_STORE_CTX
*xs_ctx
= NULL
;
791 STACK_OF(X509
) *chain
= NULL
, *untrusted
= NULL
;
796 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN
, SSL_R_NO_CERTIFICATE_SET
);
799 /* Rearranging and check the chain: add everything to a store */
800 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
) {
801 chain_store
= X509_STORE_new();
802 if (chain_store
== NULL
)
804 for (i
= 0; i
< sk_X509_num(cpk
->chain
); i
++) {
805 x
= sk_X509_value(cpk
->chain
, i
);
806 if (!X509_STORE_add_cert(chain_store
, x
))
809 /* Add EE cert too: it might be self signed */
810 if (!X509_STORE_add_cert(chain_store
, cpk
->x509
))
814 chain_store
= c
->chain_store
;
816 chain_store
= s
->ctx
->cert_store
;
818 chain_store
= ctx
->cert_store
;
820 if (flags
& SSL_BUILD_CHAIN_FLAG_UNTRUSTED
)
821 untrusted
= cpk
->chain
;
824 xs_ctx
= X509_STORE_CTX_new();
825 if (xs_ctx
== NULL
) {
826 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN
, ERR_R_MALLOC_FAILURE
);
829 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, cpk
->x509
, untrusted
)) {
830 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN
, ERR_R_X509_LIB
);
833 /* Set suite B flags if needed */
834 X509_STORE_CTX_set_flags(xs_ctx
,
835 c
->cert_flags
& SSL_CERT_FLAG_SUITEB_128_LOS
);
837 i
= X509_verify_cert(xs_ctx
);
838 if (i
<= 0 && flags
& SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
) {
839 if (flags
& SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR
)
845 chain
= X509_STORE_CTX_get1_chain(xs_ctx
);
847 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN
, SSL_R_CERTIFICATE_VERIFY_FAILED
);
848 i
= X509_STORE_CTX_get_error(xs_ctx
);
849 ERR_add_error_data(2, "Verify error:",
850 X509_verify_cert_error_string(i
));
854 /* Remove EE certificate from chain */
855 x
= sk_X509_shift(chain
);
857 if (flags
& SSL_BUILD_CHAIN_FLAG_NO_ROOT
) {
858 if (sk_X509_num(chain
) > 0) {
859 /* See if last cert is self signed */
860 x
= sk_X509_value(chain
, sk_X509_num(chain
) - 1);
861 if (X509_get_extension_flags(x
) & EXFLAG_SS
) {
862 x
= sk_X509_pop(chain
);
868 * Check security level of all CA certificates: EE will have been checked
871 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
872 x
= sk_X509_value(chain
, i
);
873 rv
= ssl_security_cert(s
, ctx
, x
, 0, 0);
875 SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN
, rv
);
876 sk_X509_pop_free(chain
, X509_free
);
881 sk_X509_pop_free(cpk
->chain
, X509_free
);
886 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
)
887 X509_STORE_free(chain_store
);
888 X509_STORE_CTX_free(xs_ctx
);
893 int ssl_cert_set_cert_store(CERT
*c
, X509_STORE
*store
, int chain
, int ref
)
897 pstore
= &c
->chain_store
;
899 pstore
= &c
->verify_store
;
900 X509_STORE_free(*pstore
);
903 X509_STORE_up_ref(store
);
907 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
908 int op
, int bits
, int nid
, void *other
,
912 static const int minbits_table
[5] = { 80, 112, 128, 192, 256 };
914 level
= SSL_CTX_get_security_level(ctx
);
916 level
= SSL_get_security_level(s
);
920 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
923 if (op
== SSL_SECOP_TMP_DH
&& bits
< 80)
929 minbits
= minbits_table
[level
- 1];
931 case SSL_SECOP_CIPHER_SUPPORTED
:
932 case SSL_SECOP_CIPHER_SHARED
:
933 case SSL_SECOP_CIPHER_CHECK
:
935 const SSL_CIPHER
*c
= other
;
936 /* No ciphers below security level */
939 /* No unauthenticated ciphersuites */
940 if (c
->algorithm_auth
& SSL_aNULL
)
942 /* No MD5 mac ciphersuites */
943 if (c
->algorithm_mac
& SSL_MD5
)
945 /* SHA1 HMAC is 160 bits of security */
946 if (minbits
> 160 && c
->algorithm_mac
& SSL_SHA1
)
948 /* Level 2: no RC4 */
949 if (level
>= 2 && c
->algorithm_enc
== SSL_RC4
)
951 /* Level 3: forward secure ciphersuites only */
952 if (level
>= 3 && c
->min_tls
!= TLS1_3_VERSION
&&
953 !(c
->algorithm_mkey
& (SSL_kEDH
| SSL_kEECDH
)))
957 case SSL_SECOP_VERSION
:
958 if (!SSL_IS_DTLS(s
)) {
959 /* SSLv3 not allowed at level 2 */
960 if (nid
<= SSL3_VERSION
&& level
>= 2)
962 /* TLS v1.1 and above only for level 3 */
963 if (nid
<= TLS1_VERSION
&& level
>= 3)
965 /* TLS v1.2 only for level 4 and above */
966 if (nid
<= TLS1_1_VERSION
&& level
>= 4)
969 /* DTLS v1.2 only for level 4 and above */
970 if (DTLS_VERSION_LT(nid
, DTLS1_2_VERSION
) && level
>= 4)
975 case SSL_SECOP_COMPRESSION
:
979 case SSL_SECOP_TICKET
:
990 int ssl_security(const SSL
*s
, int op
, int bits
, int nid
, void *other
)
992 return s
->cert
->sec_cb(s
, NULL
, op
, bits
, nid
, other
, s
->cert
->sec_ex
);
995 int ssl_ctx_security(const SSL_CTX
*ctx
, int op
, int bits
, int nid
, void *other
)
997 return ctx
->cert
->sec_cb(NULL
, ctx
, op
, bits
, nid
, other
,
1001 int ssl_cert_lookup_by_nid(int nid
, size_t *pidx
)
1005 for (i
= 0; i
< OSSL_NELEM(ssl_cert_info
); i
++) {
1006 if (ssl_cert_info
[i
].nid
== nid
) {
1015 const SSL_CERT_LOOKUP
*ssl_cert_lookup_by_pkey(const EVP_PKEY
*pk
, size_t *pidx
)
1017 int nid
= EVP_PKEY_id(pk
);
1020 if (nid
== NID_undef
)
1023 if (!ssl_cert_lookup_by_nid(nid
, &tmpidx
))
1029 return &ssl_cert_info
[tmpidx
];
1032 const SSL_CERT_LOOKUP
*ssl_cert_lookup_by_idx(size_t idx
)
1034 if (idx
>= OSSL_NELEM(ssl_cert_info
))
1036 return &ssl_cert_info
[idx
];