2 * Copyright 1995-2022 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"
27 #ifndef OPENSSL_NO_POSIX_IO
28 # include <sys/stat.h>
35 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
38 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
39 int op
, int bits
, int nid
, void *other
,
42 static CRYPTO_ONCE ssl_x509_store_ctx_once
= CRYPTO_ONCE_STATIC_INIT
;
43 static volatile int ssl_x509_store_ctx_idx
= -1;
45 DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init
)
47 ssl_x509_store_ctx_idx
= X509_STORE_CTX_get_ex_new_index(0,
48 "SSL for verify callback",
50 return ssl_x509_store_ctx_idx
>= 0;
53 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
56 if (!RUN_ONCE(&ssl_x509_store_ctx_once
, ssl_x509_store_ctx_init
))
58 return ssl_x509_store_ctx_idx
;
61 CERT
*ssl_cert_new(size_t ssl_pkey_num
)
65 /* Should never happen */
66 if (!ossl_assert(ssl_pkey_num
>= SSL_PKEY_NUM
))
69 ret
= OPENSSL_zalloc(sizeof(*ret
));
73 ret
->ssl_pkey_num
= ssl_pkey_num
;
74 ret
->pkeys
= OPENSSL_zalloc(ret
->ssl_pkey_num
* sizeof(CERT_PKEY
));
75 if (ret
->pkeys
== NULL
) {
80 ret
->key
= &(ret
->pkeys
[SSL_PKEY_RSA
]);
82 ret
->sec_cb
= ssl_security_default_callback
;
83 ret
->sec_level
= OPENSSL_TLS_SECURITY_LEVEL
;
85 ret
->lock
= CRYPTO_THREAD_lock_new();
86 if (ret
->lock
== NULL
) {
87 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
88 OPENSSL_free(ret
->pkeys
);
96 CERT
*ssl_cert_dup(CERT
*cert
)
98 CERT
*ret
= OPENSSL_zalloc(sizeof(*ret
));
100 #ifndef OPENSSL_NO_COMP_ALG
107 ret
->ssl_pkey_num
= cert
->ssl_pkey_num
;
108 ret
->pkeys
= OPENSSL_zalloc(ret
->ssl_pkey_num
* sizeof(CERT_PKEY
));
109 if (ret
->pkeys
== NULL
)
113 ret
->key
= &ret
->pkeys
[cert
->key
- cert
->pkeys
];
114 ret
->lock
= CRYPTO_THREAD_lock_new();
115 if (ret
->lock
== NULL
) {
116 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
121 if (cert
->dh_tmp
!= NULL
) {
122 ret
->dh_tmp
= cert
->dh_tmp
;
123 EVP_PKEY_up_ref(ret
->dh_tmp
);
126 ret
->dh_tmp_cb
= cert
->dh_tmp_cb
;
127 ret
->dh_tmp_auto
= cert
->dh_tmp_auto
;
129 for (i
= 0; i
< ret
->ssl_pkey_num
; i
++) {
130 CERT_PKEY
*cpk
= cert
->pkeys
+ i
;
131 CERT_PKEY
*rpk
= ret
->pkeys
+ i
;
133 if (cpk
->x509
!= NULL
) {
134 rpk
->x509
= cpk
->x509
;
135 X509_up_ref(rpk
->x509
);
138 if (cpk
->privatekey
!= NULL
) {
139 rpk
->privatekey
= cpk
->privatekey
;
140 EVP_PKEY_up_ref(cpk
->privatekey
);
144 rpk
->chain
= X509_chain_up_ref(cpk
->chain
);
146 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
150 if (cpk
->serverinfo
!= NULL
) {
151 /* Just copy everything. */
152 rpk
->serverinfo
= OPENSSL_memdup(cpk
->serverinfo
, cpk
->serverinfo_length
);
153 if (rpk
->serverinfo
== NULL
)
155 rpk
->serverinfo_length
= cpk
->serverinfo_length
;
157 #ifndef OPENSSL_NO_COMP_ALG
158 for (j
= TLSEXT_comp_cert_none
; j
< TLSEXT_comp_cert_limit
; j
++) {
159 if (cpk
->comp_cert
[j
] != NULL
) {
160 if (!OSSL_COMP_CERT_up_ref(cpk
->comp_cert
[j
]))
162 rpk
->comp_cert
[j
] = cpk
->comp_cert
[j
];
168 /* Configured sigalgs copied across */
169 if (cert
->conf_sigalgs
) {
170 ret
->conf_sigalgs
= OPENSSL_malloc(cert
->conf_sigalgslen
171 * sizeof(*cert
->conf_sigalgs
));
172 if (ret
->conf_sigalgs
== NULL
)
174 memcpy(ret
->conf_sigalgs
, cert
->conf_sigalgs
,
175 cert
->conf_sigalgslen
* sizeof(*cert
->conf_sigalgs
));
176 ret
->conf_sigalgslen
= cert
->conf_sigalgslen
;
178 ret
->conf_sigalgs
= NULL
;
180 if (cert
->client_sigalgs
) {
181 ret
->client_sigalgs
= OPENSSL_malloc(cert
->client_sigalgslen
182 * sizeof(*cert
->client_sigalgs
));
183 if (ret
->client_sigalgs
== NULL
)
185 memcpy(ret
->client_sigalgs
, cert
->client_sigalgs
,
186 cert
->client_sigalgslen
* sizeof(*cert
->client_sigalgs
));
187 ret
->client_sigalgslen
= cert
->client_sigalgslen
;
189 ret
->client_sigalgs
= NULL
;
190 /* Copy any custom client certificate types */
192 ret
->ctype
= OPENSSL_memdup(cert
->ctype
, cert
->ctype_len
);
193 if (ret
->ctype
== NULL
)
195 ret
->ctype_len
= cert
->ctype_len
;
198 ret
->cert_flags
= cert
->cert_flags
;
200 ret
->cert_cb
= cert
->cert_cb
;
201 ret
->cert_cb_arg
= cert
->cert_cb_arg
;
203 if (cert
->verify_store
) {
204 X509_STORE_up_ref(cert
->verify_store
);
205 ret
->verify_store
= cert
->verify_store
;
208 if (cert
->chain_store
) {
209 X509_STORE_up_ref(cert
->chain_store
);
210 ret
->chain_store
= cert
->chain_store
;
213 ret
->sec_cb
= cert
->sec_cb
;
214 ret
->sec_level
= cert
->sec_level
;
215 ret
->sec_ex
= cert
->sec_ex
;
217 if (!custom_exts_copy(&ret
->custext
, &cert
->custext
))
219 #ifndef OPENSSL_NO_PSK
220 if (cert
->psk_identity_hint
) {
221 ret
->psk_identity_hint
= OPENSSL_strdup(cert
->psk_identity_hint
);
222 if (ret
->psk_identity_hint
== NULL
)
234 /* Free up and clear all certificates and chains */
236 void ssl_cert_clear_certs(CERT
*c
)
239 #ifndef OPENSSL_NO_COMP_ALG
245 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
246 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
247 X509_free(cpk
->x509
);
249 EVP_PKEY_free(cpk
->privatekey
);
250 cpk
->privatekey
= NULL
;
251 OSSL_STACK_OF_X509_free(cpk
->chain
);
253 OPENSSL_free(cpk
->serverinfo
);
254 cpk
->serverinfo
= NULL
;
255 cpk
->serverinfo_length
= 0;
256 #ifndef OPENSSL_NO_COMP_ALG
257 for (j
= 0; j
< TLSEXT_comp_cert_limit
; j
++) {
258 OSSL_COMP_CERT_free(cpk
->comp_cert
[j
]);
259 cpk
->comp_cert
[j
] = NULL
;
260 cpk
->cert_comp_used
= 0;
266 void ssl_cert_free(CERT
*c
)
272 CRYPTO_DOWN_REF(&c
->references
, &i
, c
->lock
);
273 REF_PRINT_COUNT("CERT", c
);
276 REF_ASSERT_ISNT(i
< 0);
278 EVP_PKEY_free(c
->dh_tmp
);
280 ssl_cert_clear_certs(c
);
281 OPENSSL_free(c
->conf_sigalgs
);
282 OPENSSL_free(c
->client_sigalgs
);
283 OPENSSL_free(c
->ctype
);
284 X509_STORE_free(c
->verify_store
);
285 X509_STORE_free(c
->chain_store
);
286 custom_exts_free(&c
->custext
);
287 #ifndef OPENSSL_NO_PSK
288 OPENSSL_free(c
->psk_identity_hint
);
290 OPENSSL_free(c
->pkeys
);
291 CRYPTO_THREAD_lock_free(c
->lock
);
295 int ssl_cert_set0_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, STACK_OF(X509
) *chain
)
298 CERT_PKEY
*cpk
= s
!= NULL
? s
->cert
->key
: ctx
->cert
->key
;
302 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
303 X509
*x
= sk_X509_value(chain
, i
);
305 r
= ssl_security_cert(s
, ctx
, x
, 0, 0);
307 ERR_raise(ERR_LIB_SSL
, r
);
311 OSSL_STACK_OF_X509_free(cpk
->chain
);
316 int ssl_cert_set1_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, STACK_OF(X509
) *chain
)
318 STACK_OF(X509
) *dchain
;
321 return ssl_cert_set0_chain(s
, ctx
, NULL
);
322 dchain
= X509_chain_up_ref(chain
);
325 if (!ssl_cert_set0_chain(s
, ctx
, dchain
)) {
326 OSSL_STACK_OF_X509_free(dchain
);
332 int ssl_cert_add0_chain_cert(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, X509
*x
)
335 CERT_PKEY
*cpk
= s
? s
->cert
->key
: ctx
->cert
->key
;
339 r
= ssl_security_cert(s
, ctx
, x
, 0, 0);
341 ERR_raise(ERR_LIB_SSL
, r
);
345 cpk
->chain
= sk_X509_new_null();
346 if (!cpk
->chain
|| !sk_X509_push(cpk
->chain
, x
))
351 int ssl_cert_add1_chain_cert(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, X509
*x
)
353 if (!ssl_cert_add0_chain_cert(s
, ctx
, x
))
359 int ssl_cert_select_current(CERT
*c
, X509
*x
)
365 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
366 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
367 if (cpk
->x509
== x
&& cpk
->privatekey
) {
373 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
374 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
375 if (cpk
->privatekey
&& cpk
->x509
&& !X509_cmp(cpk
->x509
, x
)) {
383 int ssl_cert_set_current(CERT
*c
, long op
)
389 if (op
== SSL_CERT_SET_FIRST
)
391 else if (op
== SSL_CERT_SET_NEXT
) {
392 idx
= (size_t)(c
->key
- c
->pkeys
+ 1);
393 if (idx
>= c
->ssl_pkey_num
)
397 for (i
= idx
; i
< c
->ssl_pkey_num
; i
++) {
398 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
399 if (cpk
->x509
&& cpk
->privatekey
) {
407 void ssl_cert_set_cert_cb(CERT
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
410 c
->cert_cb_arg
= arg
;
414 * Verify a certificate chain
417 * 0: Verify failure or error
420 int ssl_verify_cert_chain(SSL_CONNECTION
*s
, STACK_OF(X509
) *sk
)
424 X509_STORE
*verify_store
;
425 X509_STORE_CTX
*ctx
= NULL
;
426 X509_VERIFY_PARAM
*param
;
429 if ((sk
== NULL
) || (sk_X509_num(sk
) == 0))
432 sctx
= SSL_CONNECTION_GET_CTX(s
);
433 if (s
->cert
->verify_store
)
434 verify_store
= s
->cert
->verify_store
;
436 verify_store
= sctx
->cert_store
;
438 ctx
= X509_STORE_CTX_new_ex(sctx
->libctx
, sctx
->propq
);
440 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
444 x
= sk_X509_value(sk
, 0);
445 if (!X509_STORE_CTX_init(ctx
, verify_store
, x
, sk
)) {
446 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
449 param
= X509_STORE_CTX_get0_param(ctx
);
451 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
452 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
453 * and PKI authentication.
455 X509_VERIFY_PARAM_set_auth_level(param
,
456 SSL_get_security_level(SSL_CONNECTION_GET_SSL(s
)));
458 /* Set suite B flags if needed */
459 X509_STORE_CTX_set_flags(ctx
, tls1_suiteb(s
));
460 if (!X509_STORE_CTX_set_ex_data(ctx
,
461 SSL_get_ex_data_X509_STORE_CTX_idx(), s
)) {
465 /* Verify via DANE if enabled */
466 if (DANETLS_ENABLED(&s
->dane
))
467 X509_STORE_CTX_set0_dane(ctx
, &s
->dane
);
470 * We need to inherit the verify parameters. These can be determined by
471 * the context: if its a server it will verify SSL client certificates or
475 X509_STORE_CTX_set_default(ctx
, s
->server
? "ssl_client" : "ssl_server");
477 * Anything non-default in "s->param" should overwrite anything in the ctx.
479 X509_VERIFY_PARAM_set1(param
, s
->param
);
481 if (s
->verify_callback
)
482 X509_STORE_CTX_set_verify_cb(ctx
, s
->verify_callback
);
484 if (sctx
->app_verify_callback
!= NULL
) {
485 i
= sctx
->app_verify_callback(ctx
, sctx
->app_verify_arg
);
487 i
= X509_verify_cert(ctx
);
488 /* We treat an error in the same way as a failure to verify */
493 s
->verify_result
= X509_STORE_CTX_get_error(ctx
);
494 OSSL_STACK_OF_X509_free(s
->verified_chain
);
495 s
->verified_chain
= NULL
;
496 if (X509_STORE_CTX_get0_chain(ctx
) != NULL
) {
497 s
->verified_chain
= X509_STORE_CTX_get1_chain(ctx
);
498 if (s
->verified_chain
== NULL
) {
499 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
504 /* Move peername from the store context params to the SSL handle's */
505 X509_VERIFY_PARAM_move_peername(s
->param
, param
);
508 X509_STORE_CTX_free(ctx
);
512 static void set0_CA_list(STACK_OF(X509_NAME
) **ca_list
,
513 STACK_OF(X509_NAME
) *name_list
)
515 sk_X509_NAME_pop_free(*ca_list
, X509_NAME_free
);
516 *ca_list
= name_list
;
519 STACK_OF(X509_NAME
) *SSL_dup_CA_list(const STACK_OF(X509_NAME
) *sk
)
522 const int num
= sk_X509_NAME_num(sk
);
523 STACK_OF(X509_NAME
) *ret
;
526 ret
= sk_X509_NAME_new_reserve(NULL
, num
);
528 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
531 for (i
= 0; i
< num
; i
++) {
532 name
= X509_NAME_dup(sk_X509_NAME_value(sk
, i
));
534 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
535 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
538 sk_X509_NAME_push(ret
, name
); /* Cannot fail after reserve call */
543 void SSL_set0_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
545 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
550 set0_CA_list(&sc
->ca_names
, name_list
);
553 void SSL_CTX_set0_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
555 set0_CA_list(&ctx
->ca_names
, name_list
);
558 const STACK_OF(X509_NAME
) *SSL_CTX_get0_CA_list(const SSL_CTX
*ctx
)
560 return ctx
->ca_names
;
563 const STACK_OF(X509_NAME
) *SSL_get0_CA_list(const SSL
*s
)
565 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
570 return sc
->ca_names
!= NULL
? sc
->ca_names
: s
->ctx
->ca_names
;
573 void SSL_CTX_set_client_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
575 set0_CA_list(&ctx
->client_ca_names
, name_list
);
578 STACK_OF(X509_NAME
) *SSL_CTX_get_client_CA_list(const SSL_CTX
*ctx
)
580 return ctx
->client_ca_names
;
583 void SSL_set_client_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
585 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
590 set0_CA_list(&sc
->client_ca_names
, name_list
);
593 const STACK_OF(X509_NAME
) *SSL_get0_peer_CA_list(const SSL
*s
)
595 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
600 return sc
->s3
.tmp
.peer_ca_names
;
603 STACK_OF(X509_NAME
) *SSL_get_client_CA_list(const SSL
*s
)
605 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
611 return sc
->s3
.tmp
.peer_ca_names
;
612 return sc
->client_ca_names
!= NULL
? sc
->client_ca_names
613 : s
->ctx
->client_ca_names
;
616 static int add_ca_name(STACK_OF(X509_NAME
) **sk
, const X509
*x
)
622 if (*sk
== NULL
&& ((*sk
= sk_X509_NAME_new_null()) == NULL
))
625 if ((name
= X509_NAME_dup(X509_get_subject_name(x
))) == NULL
)
628 if (!sk_X509_NAME_push(*sk
, name
)) {
629 X509_NAME_free(name
);
635 int SSL_add1_to_CA_list(SSL
*ssl
, const X509
*x
)
637 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
642 return add_ca_name(&sc
->ca_names
, x
);
645 int SSL_CTX_add1_to_CA_list(SSL_CTX
*ctx
, const X509
*x
)
647 return add_ca_name(&ctx
->ca_names
, x
);
651 * The following two are older names are to be replaced with
652 * SSL(_CTX)_add1_to_CA_list
654 int SSL_add_client_CA(SSL
*ssl
, X509
*x
)
656 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
661 return add_ca_name(&sc
->client_ca_names
, x
);
664 int SSL_CTX_add_client_CA(SSL_CTX
*ctx
, X509
*x
)
666 return add_ca_name(&ctx
->client_ca_names
, x
);
669 static int xname_cmp(const X509_NAME
*a
, const X509_NAME
*b
)
671 unsigned char *abuf
= NULL
, *bbuf
= NULL
;
674 /* X509_NAME_cmp() itself casts away constness in this way, so
677 alen
= i2d_X509_NAME((X509_NAME
*)a
, &abuf
);
678 blen
= i2d_X509_NAME((X509_NAME
*)b
, &bbuf
);
680 if (alen
< 0 || blen
< 0)
682 else if (alen
!= blen
)
684 else /* alen == blen */
685 ret
= memcmp(abuf
, bbuf
, alen
);
693 static int xname_sk_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
695 return xname_cmp(*a
, *b
);
698 static unsigned long xname_hash(const X509_NAME
*a
)
700 /* This returns 0 also if SHA1 is not available */
701 return X509_NAME_hash_ex((X509_NAME
*)a
, NULL
, NULL
, NULL
);
704 STACK_OF(X509_NAME
) *SSL_load_client_CA_file_ex(const char *file
,
705 OSSL_LIB_CTX
*libctx
,
708 BIO
*in
= BIO_new(BIO_s_file());
710 X509_NAME
*xn
= NULL
;
711 STACK_OF(X509_NAME
) *ret
= NULL
;
712 LHASH_OF(X509_NAME
) *name_hash
= lh_X509_NAME_new(xname_hash
, xname_cmp
);
713 OSSL_LIB_CTX
*prev_libctx
= NULL
;
715 if (name_hash
== NULL
) {
716 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
720 ERR_raise(ERR_LIB_SSL
, ERR_R_BIO_LIB
);
724 x
= X509_new_ex(libctx
, propq
);
726 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
729 if (BIO_read_filename(in
, file
) <= 0)
732 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
733 prev_libctx
= OSSL_LIB_CTX_set0_default(libctx
);
735 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
738 ret
= sk_X509_NAME_new_null();
740 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
744 if ((xn
= X509_get_subject_name(x
)) == NULL
)
746 /* check for duplicates */
747 xn
= X509_NAME_dup(xn
);
750 if (lh_X509_NAME_retrieve(name_hash
, xn
) != NULL
) {
755 lh_X509_NAME_insert(name_hash
, xn
);
756 if (!sk_X509_NAME_push(ret
, xn
))
764 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
767 /* restore the old libctx */
768 OSSL_LIB_CTX_set0_default(prev_libctx
);
771 lh_X509_NAME_free(name_hash
);
777 STACK_OF(X509_NAME
) *SSL_load_client_CA_file(const char *file
)
779 return SSL_load_client_CA_file_ex(file
, NULL
, NULL
);
782 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
787 X509_NAME
*xn
= NULL
;
789 int (*oldcmp
) (const X509_NAME
*const *a
, const X509_NAME
*const *b
);
791 oldcmp
= sk_X509_NAME_set_cmp_func(stack
, xname_sk_cmp
);
793 in
= BIO_new(BIO_s_file());
796 ERR_raise(ERR_LIB_SSL
, ERR_R_BIO_LIB
);
800 if (BIO_read_filename(in
, file
) <= 0)
804 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
806 if ((xn
= X509_get_subject_name(x
)) == NULL
)
808 xn
= X509_NAME_dup(xn
);
811 if (sk_X509_NAME_find(stack
, xn
) >= 0) {
814 } else if (!sk_X509_NAME_push(stack
, xn
)) {
828 (void)sk_X509_NAME_set_cmp_func(stack
, oldcmp
);
832 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
835 OPENSSL_DIR_CTX
*d
= NULL
;
836 const char *filename
;
839 /* Note that a side effect is that the CAs will be sorted by name */
841 while ((filename
= OPENSSL_DIR_read(&d
, dir
))) {
846 if (strlen(dir
) + strlen(filename
) + 2 > sizeof(buf
)) {
847 ERR_raise(ERR_LIB_SSL
, SSL_R_PATH_TOO_LONG
);
850 #ifdef OPENSSL_SYS_VMS
851 r
= BIO_snprintf(buf
, sizeof(buf
), "%s%s", dir
, filename
);
853 r
= BIO_snprintf(buf
, sizeof(buf
), "%s/%s", dir
, filename
);
855 /* Skip subdirectories */
856 if (!stat(buf
, &st
) && S_ISDIR(st
.st_mode
))
858 if (r
<= 0 || r
>= (int)sizeof(buf
))
860 if (!SSL_add_file_cert_subjects_to_stack(stack
, buf
))
865 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
866 "calling OPENSSL_dir_read(%s)", dir
);
867 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
880 static int add_uris_recursive(STACK_OF(X509_NAME
) *stack
,
881 const char *uri
, int depth
)
884 OSSL_STORE_CTX
*ctx
= NULL
;
886 X509_NAME
*xn
= NULL
;
888 if ((ctx
= OSSL_STORE_open(uri
, NULL
, NULL
, NULL
, NULL
)) == NULL
)
891 while (!OSSL_STORE_eof(ctx
) && !OSSL_STORE_error(ctx
)) {
892 OSSL_STORE_INFO
*info
= OSSL_STORE_load(ctx
);
893 int infotype
= info
== 0 ? 0 : OSSL_STORE_INFO_get_type(info
);
898 if (infotype
== OSSL_STORE_INFO_NAME
) {
900 * This is an entry in the "directory" represented by the current
901 * uri. if |depth| allows, dive into it.
904 ok
= add_uris_recursive(stack
, OSSL_STORE_INFO_get0_NAME(info
),
906 } else if (infotype
== OSSL_STORE_INFO_CERT
) {
907 if ((x
= OSSL_STORE_INFO_get0_CERT(info
)) == NULL
908 || (xn
= X509_get_subject_name(x
)) == NULL
909 || (xn
= X509_NAME_dup(xn
)) == NULL
)
911 if (sk_X509_NAME_find(stack
, xn
) >= 0) {
914 } else if (!sk_X509_NAME_push(stack
, xn
)) {
920 OSSL_STORE_INFO_free(info
);
929 OSSL_STORE_close(ctx
);
934 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
937 int (*oldcmp
) (const X509_NAME
*const *a
, const X509_NAME
*const *b
)
938 = sk_X509_NAME_set_cmp_func(stack
, xname_sk_cmp
);
939 int ret
= add_uris_recursive(stack
, store
, 1);
941 (void)sk_X509_NAME_set_cmp_func(stack
, oldcmp
);
945 /* Build a certificate chain for current certificate */
946 int ssl_build_cert_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, int flags
)
948 CERT
*c
= s
!= NULL
? s
->cert
: ctx
->cert
;
949 CERT_PKEY
*cpk
= c
->key
;
950 X509_STORE
*chain_store
= NULL
;
951 X509_STORE_CTX
*xs_ctx
= NULL
;
952 STACK_OF(X509
) *chain
= NULL
, *untrusted
= NULL
;
954 SSL_CTX
*real_ctx
= (s
== NULL
) ? ctx
: SSL_CONNECTION_GET_CTX(s
);
957 if (cpk
->x509
== NULL
) {
958 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_SET
);
961 /* Rearranging and check the chain: add everything to a store */
962 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
) {
963 chain_store
= X509_STORE_new();
964 if (chain_store
== NULL
)
966 for (i
= 0; i
< sk_X509_num(cpk
->chain
); i
++) {
967 x
= sk_X509_value(cpk
->chain
, i
);
968 if (!X509_STORE_add_cert(chain_store
, x
))
971 /* Add EE cert too: it might be self signed */
972 if (!X509_STORE_add_cert(chain_store
, cpk
->x509
))
975 if (c
->chain_store
!= NULL
)
976 chain_store
= c
->chain_store
;
978 chain_store
= real_ctx
->cert_store
;
980 if (flags
& SSL_BUILD_CHAIN_FLAG_UNTRUSTED
)
981 untrusted
= cpk
->chain
;
984 xs_ctx
= X509_STORE_CTX_new_ex(real_ctx
->libctx
, real_ctx
->propq
);
985 if (xs_ctx
== NULL
) {
986 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
989 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, cpk
->x509
, untrusted
)) {
990 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
993 /* Set suite B flags if needed */
994 X509_STORE_CTX_set_flags(xs_ctx
,
995 c
->cert_flags
& SSL_CERT_FLAG_SUITEB_128_LOS
);
997 i
= X509_verify_cert(xs_ctx
);
998 if (i
<= 0 && flags
& SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
) {
999 if (flags
& SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR
)
1005 chain
= X509_STORE_CTX_get1_chain(xs_ctx
);
1007 i
= X509_STORE_CTX_get_error(xs_ctx
);
1008 ERR_raise_data(ERR_LIB_SSL
, SSL_R_CERTIFICATE_VERIFY_FAILED
,
1009 "Verify error:%s", X509_verify_cert_error_string(i
));
1013 /* Remove EE certificate from chain */
1014 x
= sk_X509_shift(chain
);
1016 if (flags
& SSL_BUILD_CHAIN_FLAG_NO_ROOT
) {
1017 if (sk_X509_num(chain
) > 0) {
1018 /* See if last cert is self signed */
1019 x
= sk_X509_value(chain
, sk_X509_num(chain
) - 1);
1020 if (X509_get_extension_flags(x
) & EXFLAG_SS
) {
1021 x
= sk_X509_pop(chain
);
1027 * Check security level of all CA certificates: EE will have been checked
1030 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1031 x
= sk_X509_value(chain
, i
);
1032 rv
= ssl_security_cert(s
, ctx
, x
, 0, 0);
1034 ERR_raise(ERR_LIB_SSL
, rv
);
1035 OSSL_STACK_OF_X509_free(chain
);
1040 OSSL_STACK_OF_X509_free(cpk
->chain
);
1045 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
)
1046 X509_STORE_free(chain_store
);
1047 X509_STORE_CTX_free(xs_ctx
);
1052 int ssl_cert_set_cert_store(CERT
*c
, X509_STORE
*store
, int chain
, int ref
)
1054 X509_STORE
**pstore
;
1056 pstore
= &c
->chain_store
;
1058 pstore
= &c
->verify_store
;
1059 X509_STORE_free(*pstore
);
1062 X509_STORE_up_ref(store
);
1066 int ssl_cert_get_cert_store(CERT
*c
, X509_STORE
**pstore
, int chain
)
1068 *pstore
= (chain
? c
->chain_store
: c
->verify_store
);
1072 int ssl_get_security_level_bits(const SSL
*s
, const SSL_CTX
*ctx
, int *levelp
)
1076 * note that there's a corresponding minbits_table
1077 * in crypto/x509/x509_vfy.c that's used for checking the security level
1078 * of RSA and DSA keys
1080 static const int minbits_table
[5 + 1] = { 0, 80, 112, 128, 192, 256 };
1083 level
= SSL_CTX_get_security_level(ctx
);
1085 level
= SSL_get_security_level(s
);
1095 return minbits_table
[level
];
1098 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
1099 int op
, int bits
, int nid
, void *other
,
1102 int level
, minbits
, pfs_mask
;
1103 const SSL_CONNECTION
*sc
;
1105 minbits
= ssl_get_security_level_bits(s
, ctx
, &level
);
1109 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
1112 if (op
== SSL_SECOP_TMP_DH
&& bits
< 80)
1117 case SSL_SECOP_CIPHER_SUPPORTED
:
1118 case SSL_SECOP_CIPHER_SHARED
:
1119 case SSL_SECOP_CIPHER_CHECK
:
1121 const SSL_CIPHER
*c
= other
;
1122 /* No ciphers below security level */
1125 /* No unauthenticated ciphersuites */
1126 if (c
->algorithm_auth
& SSL_aNULL
)
1128 /* No MD5 mac ciphersuites */
1129 if (c
->algorithm_mac
& SSL_MD5
)
1131 /* SHA1 HMAC is 160 bits of security */
1132 if (minbits
> 160 && c
->algorithm_mac
& SSL_SHA1
)
1134 /* Level 3: forward secure ciphersuites only */
1135 pfs_mask
= SSL_kDHE
| SSL_kECDHE
| SSL_kDHEPSK
| SSL_kECDHEPSK
;
1136 if (level
>= 3 && c
->min_tls
!= TLS1_3_VERSION
&&
1137 !(c
->algorithm_mkey
& pfs_mask
))
1141 case SSL_SECOP_VERSION
:
1142 if ((sc
= SSL_CONNECTION_FROM_CONST_SSL(s
)) == NULL
)
1144 if (!SSL_CONNECTION_IS_DTLS(sc
)) {
1145 /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
1146 if (nid
<= TLS1_1_VERSION
&& level
> 0)
1149 /* DTLS v1.0 only allowed at level 0 */
1150 if (DTLS_VERSION_LT(nid
, DTLS1_2_VERSION
) && level
> 0)
1155 case SSL_SECOP_COMPRESSION
:
1159 case SSL_SECOP_TICKET
:
1170 int ssl_security(const SSL_CONNECTION
*s
, int op
, int bits
, int nid
, void *other
)
1172 return s
->cert
->sec_cb(SSL_CONNECTION_GET_SSL(s
), NULL
, op
, bits
, nid
,
1173 other
, s
->cert
->sec_ex
);
1176 int ssl_ctx_security(const SSL_CTX
*ctx
, int op
, int bits
, int nid
, void *other
)
1178 return ctx
->cert
->sec_cb(NULL
, ctx
, op
, bits
, nid
, other
,
1182 int ssl_cert_lookup_by_nid(int nid
, size_t *pidx
, SSL_CTX
*ctx
)
1186 for (i
= 0; i
< OSSL_NELEM(ssl_cert_info
); i
++) {
1187 if (ssl_cert_info
[i
].nid
== nid
) {
1192 for (i
= 0; i
< ctx
->sigalg_list_len
; i
++) {
1193 if (ctx
->ssl_cert_info
[i
].nid
== nid
) {
1194 *pidx
= SSL_PKEY_NUM
+ i
;
1201 SSL_CERT_LOOKUP
*ssl_cert_lookup_by_pkey(const EVP_PKEY
*pk
, size_t *pidx
, SSL_CTX
*ctx
)
1205 /* check classic pk types */
1206 for (i
= 0; i
< OSSL_NELEM(ssl_cert_info
); i
++) {
1207 SSL_CERT_LOOKUP
*tmp_lu
= &ssl_cert_info
[i
];
1209 if (EVP_PKEY_is_a(pk
, OBJ_nid2sn(tmp_lu
->nid
))
1210 || EVP_PKEY_is_a(pk
, OBJ_nid2ln(tmp_lu
->nid
))) {
1216 /* check provider-loaded pk types */
1217 for (i
= 0; ctx
->sigalg_list_len
; i
++) {
1218 SSL_CERT_LOOKUP
*tmp_lu
= &(ctx
->ssl_cert_info
[i
]);
1220 if (EVP_PKEY_is_a(pk
, OBJ_nid2sn(tmp_lu
->nid
))
1221 || EVP_PKEY_is_a(pk
, OBJ_nid2ln(tmp_lu
->nid
))) {
1223 *pidx
= SSL_PKEY_NUM
+ i
;
1224 return &ctx
->ssl_cert_info
[i
];
1231 SSL_CERT_LOOKUP
*ssl_cert_lookup_by_idx(size_t idx
, SSL_CTX
*ctx
)
1233 if (idx
>= (OSSL_NELEM(ssl_cert_info
) + ctx
->sigalg_list_len
))
1235 else if (idx
>= (OSSL_NELEM(ssl_cert_info
)))
1236 return &(ctx
->ssl_cert_info
[idx
- SSL_PKEY_NUM
]);
1237 return &ssl_cert_info
[idx
];