2 * Copyright 1995-2025 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
11 #include "internal/e_os.h"
14 #include <sys/types.h>
16 #include "internal/nelem.h"
17 #include "internal/o_dir.h"
18 #include <openssl/bio.h>
19 #include <openssl/pem.h>
20 #include <openssl/store.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/dh.h>
23 #include <openssl/bn.h>
24 #include <openssl/crypto.h>
25 #include "internal/refcount.h"
26 #include "ssl_local.h"
27 #include "ssl_cert_table.h"
28 #include "internal/thread_once.h"
29 #include "internal/ssl_unwrap.h"
30 #ifndef OPENSSL_NO_POSIX_IO
31 # include <sys/stat.h>
36 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
41 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
42 int op
, int bits
, int nid
, void *other
,
45 static CRYPTO_ONCE ssl_x509_store_ctx_once
= CRYPTO_ONCE_STATIC_INIT
;
46 static volatile int ssl_x509_store_ctx_idx
= -1;
48 DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init
)
50 ssl_x509_store_ctx_idx
= X509_STORE_CTX_get_ex_new_index(0,
51 "SSL for verify callback",
53 return ssl_x509_store_ctx_idx
>= 0;
56 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
59 if (!RUN_ONCE(&ssl_x509_store_ctx_once
, ssl_x509_store_ctx_init
))
61 return ssl_x509_store_ctx_idx
;
64 CERT
*ssl_cert_new(size_t ssl_pkey_num
)
68 /* Should never happen */
69 if (!ossl_assert(ssl_pkey_num
>= SSL_PKEY_NUM
))
72 ret
= OPENSSL_zalloc(sizeof(*ret
));
76 ret
->ssl_pkey_num
= ssl_pkey_num
;
77 ret
->pkeys
= OPENSSL_zalloc(ret
->ssl_pkey_num
* sizeof(CERT_PKEY
));
78 if (ret
->pkeys
== NULL
) {
83 ret
->key
= &(ret
->pkeys
[SSL_PKEY_RSA
]);
84 ret
->sec_cb
= ssl_security_default_callback
;
85 ret
->sec_level
= OPENSSL_TLS_SECURITY_LEVEL
;
87 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
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
) {
114 ret
->key
= &ret
->pkeys
[cert
->key
- cert
->pkeys
];
115 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
116 OPENSSL_free(ret
->pkeys
);
121 if (cert
->dh_tmp
!= NULL
) {
122 if (!EVP_PKEY_up_ref(cert
->dh_tmp
))
124 ret
->dh_tmp
= cert
->dh_tmp
;
127 ret
->dh_tmp_cb
= cert
->dh_tmp_cb
;
128 ret
->dh_tmp_auto
= cert
->dh_tmp_auto
;
130 for (i
= 0; i
< ret
->ssl_pkey_num
; i
++) {
131 CERT_PKEY
*cpk
= cert
->pkeys
+ i
;
132 CERT_PKEY
*rpk
= ret
->pkeys
+ i
;
134 if (cpk
->x509
!= NULL
) {
135 if (!X509_up_ref(cpk
->x509
))
137 rpk
->x509
= cpk
->x509
;
140 if (cpk
->privatekey
!= NULL
) {
141 if (!EVP_PKEY_up_ref(cpk
->privatekey
))
143 rpk
->privatekey
= cpk
->privatekey
;
147 rpk
->chain
= X509_chain_up_ref(cpk
->chain
);
149 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
153 if (cpk
->serverinfo
!= NULL
) {
154 /* Just copy everything. */
155 rpk
->serverinfo
= OPENSSL_memdup(cpk
->serverinfo
, cpk
->serverinfo_length
);
156 if (rpk
->serverinfo
== NULL
)
158 rpk
->serverinfo_length
= cpk
->serverinfo_length
;
160 #ifndef OPENSSL_NO_COMP_ALG
161 for (j
= TLSEXT_comp_cert_none
; j
< TLSEXT_comp_cert_limit
; j
++) {
162 if (cpk
->comp_cert
[j
] != NULL
) {
163 if (!OSSL_COMP_CERT_up_ref(cpk
->comp_cert
[j
]))
165 rpk
->comp_cert
[j
] = cpk
->comp_cert
[j
];
171 /* Configured sigalgs copied across */
172 if (cert
->conf_sigalgs
) {
173 ret
->conf_sigalgs
= OPENSSL_malloc(cert
->conf_sigalgslen
174 * sizeof(*cert
->conf_sigalgs
));
175 if (ret
->conf_sigalgs
== NULL
)
177 memcpy(ret
->conf_sigalgs
, cert
->conf_sigalgs
,
178 cert
->conf_sigalgslen
* sizeof(*cert
->conf_sigalgs
));
179 ret
->conf_sigalgslen
= cert
->conf_sigalgslen
;
181 ret
->conf_sigalgs
= NULL
;
183 if (cert
->client_sigalgs
) {
184 ret
->client_sigalgs
= OPENSSL_malloc(cert
->client_sigalgslen
185 * sizeof(*cert
->client_sigalgs
));
186 if (ret
->client_sigalgs
== NULL
)
188 memcpy(ret
->client_sigalgs
, cert
->client_sigalgs
,
189 cert
->client_sigalgslen
* sizeof(*cert
->client_sigalgs
));
190 ret
->client_sigalgslen
= cert
->client_sigalgslen
;
192 ret
->client_sigalgs
= NULL
;
193 /* Copy any custom client certificate types */
195 ret
->ctype
= OPENSSL_memdup(cert
->ctype
, cert
->ctype_len
);
196 if (ret
->ctype
== NULL
)
198 ret
->ctype_len
= cert
->ctype_len
;
201 ret
->cert_flags
= cert
->cert_flags
;
203 ret
->cert_cb
= cert
->cert_cb
;
204 ret
->cert_cb_arg
= cert
->cert_cb_arg
;
206 if (cert
->verify_store
) {
207 if (!X509_STORE_up_ref(cert
->verify_store
))
209 ret
->verify_store
= cert
->verify_store
;
212 if (cert
->chain_store
) {
213 if (!X509_STORE_up_ref(cert
->chain_store
))
215 ret
->chain_store
= cert
->chain_store
;
218 ret
->sec_cb
= cert
->sec_cb
;
219 ret
->sec_level
= cert
->sec_level
;
220 ret
->sec_ex
= cert
->sec_ex
;
222 if (!custom_exts_copy(&ret
->custext
, &cert
->custext
))
224 #ifndef OPENSSL_NO_PSK
225 if (cert
->psk_identity_hint
) {
226 ret
->psk_identity_hint
= OPENSSL_strdup(cert
->psk_identity_hint
);
227 if (ret
->psk_identity_hint
== NULL
)
239 /* Free up and clear all certificates and chains */
241 void ssl_cert_clear_certs(CERT
*c
)
244 #ifndef OPENSSL_NO_COMP_ALG
250 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
251 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
252 X509_free(cpk
->x509
);
254 EVP_PKEY_free(cpk
->privatekey
);
255 cpk
->privatekey
= NULL
;
256 OSSL_STACK_OF_X509_free(cpk
->chain
);
258 OPENSSL_free(cpk
->serverinfo
);
259 cpk
->serverinfo
= NULL
;
260 cpk
->serverinfo_length
= 0;
261 #ifndef OPENSSL_NO_COMP_ALG
262 for (j
= 0; j
< TLSEXT_comp_cert_limit
; j
++) {
263 OSSL_COMP_CERT_free(cpk
->comp_cert
[j
]);
264 cpk
->comp_cert
[j
] = NULL
;
265 cpk
->cert_comp_used
= 0;
271 void ssl_cert_free(CERT
*c
)
277 CRYPTO_DOWN_REF(&c
->references
, &i
);
278 REF_PRINT_COUNT("CERT", i
, c
);
281 REF_ASSERT_ISNT(i
< 0);
283 EVP_PKEY_free(c
->dh_tmp
);
285 ssl_cert_clear_certs(c
);
286 OPENSSL_free(c
->conf_sigalgs
);
287 OPENSSL_free(c
->client_sigalgs
);
288 OPENSSL_free(c
->ctype
);
289 X509_STORE_free(c
->verify_store
);
290 X509_STORE_free(c
->chain_store
);
291 custom_exts_free(&c
->custext
);
292 #ifndef OPENSSL_NO_PSK
293 OPENSSL_free(c
->psk_identity_hint
);
295 OPENSSL_free(c
->pkeys
);
296 CRYPTO_FREE_REF(&c
->references
);
300 int ssl_cert_set0_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, STACK_OF(X509
) *chain
)
303 CERT_PKEY
*cpk
= s
!= NULL
? s
->cert
->key
: ctx
->cert
->key
;
307 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
308 X509
*x
= sk_X509_value(chain
, i
);
310 r
= ssl_security_cert(s
, ctx
, x
, 0, 0);
312 ERR_raise(ERR_LIB_SSL
, r
);
316 OSSL_STACK_OF_X509_free(cpk
->chain
);
321 int ssl_cert_set1_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, STACK_OF(X509
) *chain
)
323 STACK_OF(X509
) *dchain
;
326 return ssl_cert_set0_chain(s
, ctx
, NULL
);
327 dchain
= X509_chain_up_ref(chain
);
330 if (!ssl_cert_set0_chain(s
, ctx
, dchain
)) {
331 OSSL_STACK_OF_X509_free(dchain
);
337 int ssl_cert_add0_chain_cert(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, X509
*x
)
340 CERT_PKEY
*cpk
= s
? s
->cert
->key
: ctx
->cert
->key
;
344 r
= ssl_security_cert(s
, ctx
, x
, 0, 0);
346 ERR_raise(ERR_LIB_SSL
, r
);
350 cpk
->chain
= sk_X509_new_null();
351 if (!cpk
->chain
|| !sk_X509_push(cpk
->chain
, x
))
356 int ssl_cert_add1_chain_cert(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, X509
*x
)
360 if (!ssl_cert_add0_chain_cert(s
, ctx
, x
)) {
367 int ssl_cert_select_current(CERT
*c
, X509
*x
)
373 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
374 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
375 if (cpk
->x509
== x
&& cpk
->privatekey
) {
381 for (i
= 0; i
< c
->ssl_pkey_num
; i
++) {
382 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
383 if (cpk
->privatekey
&& cpk
->x509
&& !X509_cmp(cpk
->x509
, x
)) {
391 int ssl_cert_set_current(CERT
*c
, long op
)
397 if (op
== SSL_CERT_SET_FIRST
)
399 else if (op
== SSL_CERT_SET_NEXT
) {
400 idx
= (size_t)(c
->key
- c
->pkeys
+ 1);
401 if (idx
>= c
->ssl_pkey_num
)
405 for (i
= idx
; i
< c
->ssl_pkey_num
; i
++) {
406 CERT_PKEY
*cpk
= c
->pkeys
+ i
;
407 if (cpk
->x509
&& cpk
->privatekey
) {
415 void ssl_cert_set_cert_cb(CERT
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
418 c
->cert_cb_arg
= arg
;
422 * Verify a certificate chain/raw public key
425 * 0: Verify failure or error
428 static int ssl_verify_internal(SSL_CONNECTION
*s
, STACK_OF(X509
) *sk
, EVP_PKEY
*rpk
)
432 X509_STORE
*verify_store
;
433 X509_STORE_CTX
*ctx
= NULL
;
434 X509_VERIFY_PARAM
*param
;
437 /* Something must be passed in */
438 if ((sk
== NULL
|| sk_X509_num(sk
) == 0) && rpk
== NULL
)
441 /* Only one can be set */
442 if (sk
!= NULL
&& rpk
!= NULL
)
445 sctx
= SSL_CONNECTION_GET_CTX(s
);
446 if (s
->cert
->verify_store
)
447 verify_store
= s
->cert
->verify_store
;
449 verify_store
= sctx
->cert_store
;
451 ctx
= X509_STORE_CTX_new_ex(sctx
->libctx
, sctx
->propq
);
453 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
458 x
= sk_X509_value(sk
, 0);
459 if (!X509_STORE_CTX_init(ctx
, verify_store
, x
, sk
)) {
460 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
464 if (!X509_STORE_CTX_init_rpk(ctx
, verify_store
, rpk
)) {
465 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
469 param
= X509_STORE_CTX_get0_param(ctx
);
471 * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
472 * point, for now a single @SECLEVEL sets the same policy for TLS crypto
473 * and PKI authentication.
475 X509_VERIFY_PARAM_set_auth_level(param
,
476 SSL_get_security_level(SSL_CONNECTION_GET_SSL(s
)));
478 /* Set suite B flags if needed */
479 X509_STORE_CTX_set_flags(ctx
, tls1_suiteb(s
));
480 if (!X509_STORE_CTX_set_ex_data(ctx
,
481 SSL_get_ex_data_X509_STORE_CTX_idx(),
482 SSL_CONNECTION_GET_USER_SSL(s
)))
485 /* Verify via DANE if enabled */
486 if (DANETLS_ENABLED(&s
->dane
))
487 X509_STORE_CTX_set0_dane(ctx
, &s
->dane
);
490 * We need to inherit the verify parameters. These can be determined by
491 * the context: if its a server it will verify SSL client certificates or
495 X509_STORE_CTX_set_default(ctx
, s
->server
? "ssl_client" : "ssl_server");
497 * Anything non-default in "s->param" should overwrite anything in the ctx.
499 X509_VERIFY_PARAM_set1(param
, s
->param
);
501 if (s
->verify_callback
)
502 X509_STORE_CTX_set_verify_cb(ctx
, s
->verify_callback
);
504 if (sctx
->app_verify_callback
!= NULL
) {
505 i
= sctx
->app_verify_callback(ctx
, sctx
->app_verify_arg
);
507 i
= X509_verify_cert(ctx
);
508 /* We treat an error in the same way as a failure to verify */
513 s
->verify_result
= X509_STORE_CTX_get_error(ctx
);
514 OSSL_STACK_OF_X509_free(s
->verified_chain
);
515 s
->verified_chain
= NULL
;
517 if (sk
!= NULL
&& X509_STORE_CTX_get0_chain(ctx
) != NULL
) {
518 s
->verified_chain
= X509_STORE_CTX_get1_chain(ctx
);
519 if (s
->verified_chain
== NULL
) {
520 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
525 /* Move peername from the store context params to the SSL handle's */
526 X509_VERIFY_PARAM_move_peername(s
->param
, param
);
529 X509_STORE_CTX_free(ctx
);
534 * Verify a raw public key
537 * 0: Verify failure or error
540 int ssl_verify_rpk(SSL_CONNECTION
*s
, EVP_PKEY
*rpk
)
542 return ssl_verify_internal(s
, NULL
, rpk
);
546 * Verify a certificate chain
549 * 0: Verify failure or error
552 int ssl_verify_cert_chain(SSL_CONNECTION
*s
, STACK_OF(X509
) *sk
)
554 return ssl_verify_internal(s
, sk
, NULL
);
557 static void set0_CA_list(STACK_OF(X509_NAME
) **ca_list
,
558 STACK_OF(X509_NAME
) *name_list
)
560 sk_X509_NAME_pop_free(*ca_list
, X509_NAME_free
);
561 *ca_list
= name_list
;
564 STACK_OF(X509_NAME
) *SSL_dup_CA_list(const STACK_OF(X509_NAME
) *sk
)
567 const int num
= sk_X509_NAME_num(sk
);
568 STACK_OF(X509_NAME
) *ret
;
571 ret
= sk_X509_NAME_new_reserve(NULL
, num
);
573 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
576 for (i
= 0; i
< num
; i
++) {
577 name
= X509_NAME_dup(sk_X509_NAME_value(sk
, i
));
579 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
580 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
583 sk_X509_NAME_push(ret
, name
); /* Cannot fail after reserve call */
588 void SSL_set0_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
590 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
595 set0_CA_list(&sc
->ca_names
, name_list
);
598 void SSL_CTX_set0_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
600 set0_CA_list(&ctx
->ca_names
, name_list
);
603 const STACK_OF(X509_NAME
) *SSL_CTX_get0_CA_list(const SSL_CTX
*ctx
)
605 return ctx
->ca_names
;
608 const STACK_OF(X509_NAME
) *SSL_get0_CA_list(const SSL
*s
)
610 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
615 return sc
->ca_names
!= NULL
? sc
->ca_names
: s
->ctx
->ca_names
;
618 void SSL_CTX_set_client_CA_list(SSL_CTX
*ctx
, STACK_OF(X509_NAME
) *name_list
)
620 set0_CA_list(&ctx
->client_ca_names
, name_list
);
623 STACK_OF(X509_NAME
) *SSL_CTX_get_client_CA_list(const SSL_CTX
*ctx
)
625 return ctx
->client_ca_names
;
628 void SSL_set_client_CA_list(SSL
*s
, STACK_OF(X509_NAME
) *name_list
)
630 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
635 set0_CA_list(&sc
->client_ca_names
, name_list
);
638 const STACK_OF(X509_NAME
) *SSL_get0_peer_CA_list(const SSL
*s
)
640 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
645 return sc
->s3
.tmp
.peer_ca_names
;
648 STACK_OF(X509_NAME
) *SSL_get_client_CA_list(const SSL
*s
)
650 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
656 return sc
->s3
.tmp
.peer_ca_names
;
657 return sc
->client_ca_names
!= NULL
? sc
->client_ca_names
658 : s
->ctx
->client_ca_names
;
661 static int add_ca_name(STACK_OF(X509_NAME
) **sk
, const X509
*x
)
667 if (*sk
== NULL
&& ((*sk
= sk_X509_NAME_new_null()) == NULL
))
670 if ((name
= X509_NAME_dup(X509_get_subject_name(x
))) == NULL
)
673 if (!sk_X509_NAME_push(*sk
, name
)) {
674 X509_NAME_free(name
);
680 int SSL_add1_to_CA_list(SSL
*ssl
, const X509
*x
)
682 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
687 return add_ca_name(&sc
->ca_names
, x
);
690 int SSL_CTX_add1_to_CA_list(SSL_CTX
*ctx
, const X509
*x
)
692 return add_ca_name(&ctx
->ca_names
, x
);
696 * The following two are older names are to be replaced with
697 * SSL(_CTX)_add1_to_CA_list
699 int SSL_add_client_CA(SSL
*ssl
, X509
*x
)
701 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
706 return add_ca_name(&sc
->client_ca_names
, x
);
709 int SSL_CTX_add_client_CA(SSL_CTX
*ctx
, X509
*x
)
711 return add_ca_name(&ctx
->client_ca_names
, x
);
714 static int xname_cmp(const X509_NAME
*a
, const X509_NAME
*b
)
716 unsigned char *abuf
= NULL
, *bbuf
= NULL
;
719 /* X509_NAME_cmp() itself casts away constness in this way, so
722 alen
= i2d_X509_NAME((X509_NAME
*)a
, &abuf
);
723 blen
= i2d_X509_NAME((X509_NAME
*)b
, &bbuf
);
725 if (alen
< 0 || blen
< 0)
727 else if (alen
!= blen
)
729 else /* alen == blen */
730 ret
= memcmp(abuf
, bbuf
, alen
);
738 static int xname_sk_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
740 return xname_cmp(*a
, *b
);
743 static unsigned long xname_hash(const X509_NAME
*a
)
745 /* This returns 0 also if SHA1 is not available */
746 return X509_NAME_hash_ex((X509_NAME
*)a
, NULL
, NULL
, NULL
);
749 STACK_OF(X509_NAME
) *SSL_load_client_CA_file_ex(const char *file
,
750 OSSL_LIB_CTX
*libctx
,
753 BIO
*in
= BIO_new(BIO_s_file());
755 X509_NAME
*xn
= NULL
;
756 STACK_OF(X509_NAME
) *ret
= NULL
;
757 LHASH_OF(X509_NAME
) *name_hash
= lh_X509_NAME_new(xname_hash
, xname_cmp
);
758 OSSL_LIB_CTX
*prev_libctx
= NULL
;
761 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
764 if (name_hash
== NULL
) {
765 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
769 ERR_raise(ERR_LIB_SSL
, ERR_R_BIO_LIB
);
773 x
= X509_new_ex(libctx
, propq
);
775 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
778 if (BIO_read_filename(in
, file
) <= 0)
781 /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
782 prev_libctx
= OSSL_LIB_CTX_set0_default(libctx
);
784 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
787 ret
= sk_X509_NAME_new_null();
789 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
793 if ((xn
= X509_get_subject_name(x
)) == NULL
)
795 /* check for duplicates */
796 xn
= X509_NAME_dup(xn
);
799 if (lh_X509_NAME_retrieve(name_hash
, xn
) != NULL
) {
804 lh_X509_NAME_insert(name_hash
, xn
);
805 if (!sk_X509_NAME_push(ret
, xn
))
813 sk_X509_NAME_pop_free(ret
, X509_NAME_free
);
816 /* restore the old libctx */
817 OSSL_LIB_CTX_set0_default(prev_libctx
);
820 lh_X509_NAME_free(name_hash
);
826 STACK_OF(X509_NAME
) *SSL_load_client_CA_file(const char *file
)
828 return SSL_load_client_CA_file_ex(file
, NULL
, NULL
);
831 static int add_file_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
833 LHASH_OF(X509_NAME
) *name_hash
)
837 X509_NAME
*xn
= NULL
;
840 in
= BIO_new(BIO_s_file());
843 ERR_raise(ERR_LIB_SSL
, ERR_R_BIO_LIB
);
847 if (BIO_read_filename(in
, file
) <= 0)
851 if (PEM_read_bio_X509(in
, &x
, NULL
, NULL
) == NULL
)
853 if ((xn
= X509_get_subject_name(x
)) == NULL
)
855 xn
= X509_NAME_dup(xn
);
858 if (lh_X509_NAME_retrieve(name_hash
, xn
) != NULL
) {
861 } else if (!sk_X509_NAME_push(stack
, xn
)) {
865 /* Successful insert, add to hash table */
866 lh_X509_NAME_insert(name_hash
, xn
);
881 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
884 X509_NAME
*xn
= NULL
;
888 LHASH_OF(X509_NAME
) *name_hash
= lh_X509_NAME_new(xname_hash
, xname_cmp
);
891 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
895 if (name_hash
== NULL
) {
896 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
901 * Pre-populate the lhash with the existing entries of the stack, since
902 * using the LHASH_OF is much faster for duplicate checking. That's because
903 * xname_cmp converts the X509_NAMEs to DER involving a memory allocation
904 * for every single invocation of the comparison function.
906 num
= sk_X509_NAME_num(stack
);
907 for (idx
= 0; idx
< num
; idx
++) {
908 xn
= sk_X509_NAME_value(stack
, idx
);
909 lh_X509_NAME_insert(name_hash
, xn
);
912 ret
= add_file_cert_subjects_to_stack(stack
, file
, name_hash
);
918 lh_X509_NAME_free(name_hash
);
922 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
925 OPENSSL_DIR_CTX
*d
= NULL
;
926 const char *filename
;
928 X509_NAME
*xn
= NULL
;
931 LHASH_OF(X509_NAME
) *name_hash
= lh_X509_NAME_new(xname_hash
, xname_cmp
);
933 if (name_hash
== NULL
) {
934 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
939 * Pre-populate the lhash with the existing entries of the stack, since
940 * using the LHASH_OF is much faster for duplicate checking. That's because
941 * xname_cmp converts the X509_NAMEs to DER involving a memory allocation
942 * for every single invocation of the comparison function.
944 num
= sk_X509_NAME_num(stack
);
945 for (idx
= 0; idx
< num
; idx
++) {
946 xn
= sk_X509_NAME_value(stack
, idx
);
947 lh_X509_NAME_insert(name_hash
, xn
);
950 while ((filename
= OPENSSL_DIR_read(&d
, dir
))) {
953 #ifndef OPENSSL_NO_POSIX_IO
957 /* Cannot use stat so just skip current and parent directories */
958 if (strcmp(filename
, ".") == 0 || strcmp(filename
, "..") == 0)
961 if (strlen(dir
) + strlen(filename
) + 2 > sizeof(buf
)) {
962 ERR_raise(ERR_LIB_SSL
, SSL_R_PATH_TOO_LONG
);
965 #ifdef OPENSSL_SYS_VMS
966 r
= BIO_snprintf(buf
, sizeof(buf
), "%s%s", dir
, filename
);
968 r
= BIO_snprintf(buf
, sizeof(buf
), "%s/%s", dir
, filename
);
970 #ifndef OPENSSL_NO_POSIX_IO
971 /* Skip subdirectories */
972 if (!stat(buf
, &st
) && S_ISDIR(st
.st_mode
))
975 if (r
<= 0 || r
>= (int)sizeof(buf
))
977 if (!add_file_cert_subjects_to_stack(stack
, buf
, name_hash
))
982 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
983 "calling OPENSSL_dir_read(%s)", dir
);
984 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
993 lh_X509_NAME_free(name_hash
);
998 static int add_uris_recursive(STACK_OF(X509_NAME
) *stack
,
999 const char *uri
, int depth
)
1002 OSSL_STORE_CTX
*ctx
= NULL
;
1004 X509_NAME
*xn
= NULL
;
1005 OSSL_STORE_INFO
*info
= NULL
;
1007 if ((ctx
= OSSL_STORE_open(uri
, NULL
, NULL
, NULL
, NULL
)) == NULL
)
1010 while (!OSSL_STORE_eof(ctx
) && !OSSL_STORE_error(ctx
)) {
1013 if ((info
= OSSL_STORE_load(ctx
)) == NULL
)
1015 infotype
= OSSL_STORE_INFO_get_type(info
);
1017 if (infotype
== OSSL_STORE_INFO_NAME
) {
1019 * This is an entry in the "directory" represented by the current
1020 * uri. if |depth| allows, dive into it.
1023 ok
= add_uris_recursive(stack
, OSSL_STORE_INFO_get0_NAME(info
),
1025 } else if (infotype
== OSSL_STORE_INFO_CERT
) {
1026 if ((x
= OSSL_STORE_INFO_get0_CERT(info
)) == NULL
1027 || (xn
= X509_get_subject_name(x
)) == NULL
1028 || (xn
= X509_NAME_dup(xn
)) == NULL
)
1030 if (sk_X509_NAME_find(stack
, xn
) >= 0) {
1033 } else if (!sk_X509_NAME_push(stack
, xn
)) {
1039 OSSL_STORE_INFO_free(info
);
1048 OSSL_STORE_INFO_free(info
);
1050 OSSL_STORE_close(ctx
);
1055 int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME
) *stack
,
1058 int (*oldcmp
) (const X509_NAME
*const *a
, const X509_NAME
*const *b
)
1059 = sk_X509_NAME_set_cmp_func(stack
, xname_sk_cmp
);
1060 int ret
= add_uris_recursive(stack
, store
, 1);
1062 (void)sk_X509_NAME_set_cmp_func(stack
, oldcmp
);
1066 /* Build a certificate chain for current certificate */
1067 int ssl_build_cert_chain(SSL_CONNECTION
*s
, SSL_CTX
*ctx
, int flags
)
1069 CERT
*c
= s
!= NULL
? s
->cert
: ctx
->cert
;
1070 CERT_PKEY
*cpk
= c
->key
;
1071 X509_STORE
*chain_store
= NULL
;
1072 X509_STORE_CTX
*xs_ctx
= NULL
;
1073 STACK_OF(X509
) *chain
= NULL
, *untrusted
= NULL
;
1075 SSL_CTX
*real_ctx
= (s
== NULL
) ? ctx
: SSL_CONNECTION_GET_CTX(s
);
1078 if (cpk
->x509
== NULL
) {
1079 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_SET
);
1082 /* Rearranging and check the chain: add everything to a store */
1083 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
) {
1084 chain_store
= X509_STORE_new();
1085 if (chain_store
== NULL
)
1087 for (i
= 0; i
< sk_X509_num(cpk
->chain
); i
++) {
1088 x
= sk_X509_value(cpk
->chain
, i
);
1089 if (!X509_STORE_add_cert(chain_store
, x
))
1092 /* Add EE cert too: it might be self signed */
1093 if (!X509_STORE_add_cert(chain_store
, cpk
->x509
))
1096 if (c
->chain_store
!= NULL
)
1097 chain_store
= c
->chain_store
;
1099 chain_store
= real_ctx
->cert_store
;
1101 if (flags
& SSL_BUILD_CHAIN_FLAG_UNTRUSTED
)
1102 untrusted
= cpk
->chain
;
1105 xs_ctx
= X509_STORE_CTX_new_ex(real_ctx
->libctx
, real_ctx
->propq
);
1106 if (xs_ctx
== NULL
) {
1107 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
1110 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, cpk
->x509
, untrusted
)) {
1111 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
1114 /* Set suite B flags if needed */
1115 X509_STORE_CTX_set_flags(xs_ctx
,
1116 c
->cert_flags
& SSL_CERT_FLAG_SUITEB_128_LOS
);
1118 i
= X509_verify_cert(xs_ctx
);
1119 if (i
<= 0 && flags
& SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
) {
1120 if (flags
& SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR
)
1126 chain
= X509_STORE_CTX_get1_chain(xs_ctx
);
1128 i
= X509_STORE_CTX_get_error(xs_ctx
);
1129 ERR_raise_data(ERR_LIB_SSL
, SSL_R_CERTIFICATE_VERIFY_FAILED
,
1130 "Verify error:%s", X509_verify_cert_error_string(i
));
1134 /* Remove EE certificate from chain */
1135 x
= sk_X509_shift(chain
);
1137 if (flags
& SSL_BUILD_CHAIN_FLAG_NO_ROOT
) {
1138 if (sk_X509_num(chain
) > 0) {
1139 /* See if last cert is self signed */
1140 x
= sk_X509_value(chain
, sk_X509_num(chain
) - 1);
1141 if (X509_get_extension_flags(x
) & EXFLAG_SS
) {
1142 x
= sk_X509_pop(chain
);
1148 * Check security level of all CA certificates: EE will have been checked
1151 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1152 x
= sk_X509_value(chain
, i
);
1153 rv
= ssl_security_cert(s
, ctx
, x
, 0, 0);
1155 ERR_raise(ERR_LIB_SSL
, rv
);
1156 OSSL_STACK_OF_X509_free(chain
);
1161 OSSL_STACK_OF_X509_free(cpk
->chain
);
1166 if (flags
& SSL_BUILD_CHAIN_FLAG_CHECK
)
1167 X509_STORE_free(chain_store
);
1168 X509_STORE_CTX_free(xs_ctx
);
1173 int ssl_cert_set_cert_store(CERT
*c
, X509_STORE
*store
, int chain
, int ref
)
1175 X509_STORE
**pstore
;
1177 if (ref
&& store
&& !X509_STORE_up_ref(store
))
1181 pstore
= &c
->chain_store
;
1183 pstore
= &c
->verify_store
;
1184 X509_STORE_free(*pstore
);
1190 int ssl_cert_get_cert_store(CERT
*c
, X509_STORE
**pstore
, int chain
)
1192 *pstore
= (chain
? c
->chain_store
: c
->verify_store
);
1196 int ssl_get_security_level_bits(const SSL
*s
, const SSL_CTX
*ctx
, int *levelp
)
1200 * note that there's a corresponding minbits_table
1201 * in crypto/x509/x509_vfy.c that's used for checking the security level
1202 * of RSA and DSA keys
1204 static const int minbits_table
[5 + 1] = { 0, 80, 112, 128, 192, 256 };
1207 level
= SSL_CTX_get_security_level(ctx
);
1209 level
= SSL_get_security_level(s
);
1219 return minbits_table
[level
];
1222 static int ssl_security_default_callback(const SSL
*s
, const SSL_CTX
*ctx
,
1223 int op
, int bits
, int nid
, void *other
,
1226 int level
, minbits
, pfs_mask
;
1227 const SSL_CONNECTION
*sc
;
1229 minbits
= ssl_get_security_level_bits(s
, ctx
, &level
);
1233 * No EDH keys weaker than 1024-bits even at level 0, otherwise,
1236 if (op
== SSL_SECOP_TMP_DH
&& bits
< 80)
1241 case SSL_SECOP_CIPHER_SUPPORTED
:
1242 case SSL_SECOP_CIPHER_SHARED
:
1243 case SSL_SECOP_CIPHER_CHECK
:
1245 const SSL_CIPHER
*c
= other
;
1246 /* No ciphers below security level */
1249 /* No unauthenticated ciphersuites */
1250 if (c
->algorithm_auth
& SSL_aNULL
)
1252 /* No MD5 mac ciphersuites */
1253 if (c
->algorithm_mac
& SSL_MD5
)
1255 /* SHA1 HMAC is 160 bits of security */
1256 if (minbits
> 160 && c
->algorithm_mac
& SSL_SHA1
)
1258 /* Level 3: forward secure ciphersuites only */
1259 pfs_mask
= SSL_kDHE
| SSL_kECDHE
| SSL_kDHEPSK
| SSL_kECDHEPSK
;
1260 if (level
>= 3 && c
->min_tls
!= TLS1_3_VERSION
&&
1261 !(c
->algorithm_mkey
& pfs_mask
))
1265 case SSL_SECOP_VERSION
:
1266 if ((sc
= SSL_CONNECTION_FROM_CONST_SSL(s
)) == NULL
)
1268 if (!SSL_CONNECTION_IS_DTLS(sc
)) {
1269 /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
1270 if (nid
<= TLS1_1_VERSION
&& level
> 0)
1273 /* DTLS v1.0 only allowed at level 0 */
1274 if (DTLS_VERSION_LT(nid
, DTLS1_2_VERSION
) && level
> 0)
1279 case SSL_SECOP_COMPRESSION
:
1283 case SSL_SECOP_TICKET
:
1294 int ssl_security(const SSL_CONNECTION
*s
, int op
, int bits
, int nid
, void *other
)
1296 return s
->cert
->sec_cb(SSL_CONNECTION_GET_USER_SSL(s
), NULL
, op
, bits
, nid
,
1297 other
, s
->cert
->sec_ex
);
1300 int ssl_ctx_security(const SSL_CTX
*ctx
, int op
, int bits
, int nid
, void *other
)
1302 return ctx
->cert
->sec_cb(NULL
, ctx
, op
, bits
, nid
, other
,
1306 int ssl_cert_lookup_by_nid(int nid
, size_t *pidx
, SSL_CTX
*ctx
)
1310 for (i
= 0; i
< OSSL_NELEM(ssl_cert_info
); i
++) {
1311 if (ssl_cert_info
[i
].nid
== nid
) {
1316 for (i
= 0; i
< ctx
->sigalg_list_len
; i
++) {
1317 if (ctx
->ssl_cert_info
[i
].nid
== nid
) {
1318 *pidx
= SSL_PKEY_NUM
+ i
;
1325 const SSL_CERT_LOOKUP
*ssl_cert_lookup_by_pkey(const EVP_PKEY
*pk
, size_t *pidx
, SSL_CTX
*ctx
)
1329 /* check classic pk types */
1330 for (i
= 0; i
< OSSL_NELEM(ssl_cert_info
); i
++) {
1331 const SSL_CERT_LOOKUP
*tmp_lu
= &ssl_cert_info
[i
];
1333 if (EVP_PKEY_is_a(pk
, OBJ_nid2sn(tmp_lu
->nid
))
1334 || EVP_PKEY_is_a(pk
, OBJ_nid2ln(tmp_lu
->nid
))) {
1340 /* check provider-loaded pk types */
1341 for (i
= 0; i
< ctx
->sigalg_list_len
; i
++) {
1342 SSL_CERT_LOOKUP
*tmp_lu
= &(ctx
->ssl_cert_info
[i
]);
1344 if (EVP_PKEY_is_a(pk
, OBJ_nid2sn(tmp_lu
->nid
))
1345 || EVP_PKEY_is_a(pk
, OBJ_nid2ln(tmp_lu
->nid
))) {
1347 *pidx
= SSL_PKEY_NUM
+ i
;
1348 return &ctx
->ssl_cert_info
[i
];
1355 const SSL_CERT_LOOKUP
*ssl_cert_lookup_by_idx(size_t idx
, SSL_CTX
*ctx
)
1357 if (idx
>= (OSSL_NELEM(ssl_cert_info
) + ctx
->sigalg_list_len
))
1359 else if (idx
>= (OSSL_NELEM(ssl_cert_info
)))
1360 return &(ctx
->ssl_cert_info
[idx
- SSL_PKEY_NUM
]);
1361 return &ssl_cert_info
[idx
];