27 RCSID(
"$Id: a18c2f3112e793ee25bb4b21556b3090bee87d28 $")
31 #define LOG_PREFIX "tls"
33 #include <freeradius-devel/tls/log.h>
34 #include <freeradius-devel/tls/strerror.h>
35 #include <freeradius-devel/util/base16.h>
36 #include <freeradius-devel/util/debug.h>
37 #include <freeradius-devel/util/misc.h>
38 #include <freeradius-devel/util/strerror.h>
39 #include <freeradius-devel/util/syserror.h>
46 #include <openssl/rand.h>
47 #include <openssl/dh.h>
48 #include <openssl/x509v3.h>
49 #include <openssl/provider.h>
51 #ifndef OPENSSL_NO_ECDH
52 static int ctx_ecdh_curve_set(SSL_CTX *ctx,
char const *ecdh_curve,
bool disable_single_dh_use)
56 if (!disable_single_dh_use) {
57 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
60 if (!ecdh_curve || !*ecdh_curve)
return 0;
62 list = strdup(ecdh_curve);
63 if (SSL_CTX_set1_curves_list(ctx, list) == 0) {
65 ERROR(
"Unknown ecdh_curve \"%s\"", ecdh_curve);
76 static int ctx_dh_params_load(SSL_CTX *ctx,
char *
file)
95 if (EVP_default_properties_is_fips_enabled(NULL)) {
96 WARN(
LOG_PREFIX ": Ignoring user-selected DH parameters in FIPS mode. Using defaults.");
100 if ((bio = BIO_new_file(
file,
"r")) == NULL) {
101 ERROR(
"Unable to open DH file - %s",
file);
105 dh = PEM_read_bio_Parameters(bio, &dh);
108 WARN(
"Unable to set DH parameters. DH cipher suites may not work!");
109 WARN(
"Fix this by generating the DH parameter file");
113 ret = SSL_CTX_set0_tmp_dh_pkey(ctx, dh);
115 ERROR(
"Unable to set DH parameters");
122 static int tls_ctx_verify_chain_member(
fr_unix_time_t *expires_first, X509 **self_signed,
123 SSL_CTX *ctx, X509 *to_verify,
124 fr_tls_chain_verify_mode_t verify_mode)
128 STACK_OF(X509) *chain;
131 leaf = SSL_CTX_get0_certificate(ctx);
133 ERROR(
"Chain does not contain a valid leaf certificate");
137 if (!SSL_CTX_get0_chain_certs(ctx, &chain)) {
138 fr_tls_log(NULL,
"Failed retrieving chain certificates");
142 switch (fr_tls_cert_is_valid(NULL, ¬_after, to_verify)) {
144 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
145 PERROR(
"Malformed certificate");
150 switch (verify_mode) {
151 case FR_TLS_CHAIN_VERIFY_SOFT:
152 fr_tls_chain_marker_log(NULL,
L_WARN, chain, leaf, to_verify);
153 PWARN(
"Certificate validation failed");
156 case FR_TLS_CHAIN_VERIFY_HARD:
157 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
158 PERROR(
"Certificate validation failed");
170 switch (verify_mode) {
171 case FR_TLS_CHAIN_VERIFY_SOFT:
172 case FR_TLS_CHAIN_VERIFY_HARD:
188 if (X509_name_cmp(X509_get_subject_name(to_verify),
189 X509_get_issuer_name(to_verify)) == 0) {
191 switch (verify_mode) {
192 case FR_TLS_CHAIN_VERIFY_SOFT:
193 WARN(
"Found multiple self-signed certificates in chain");
194 WARN(
"First certificate was:");
195 fr_tls_chain_marker_log(NULL,
L_WARN,
196 chain, leaf, *self_signed);
198 WARN(
"Second certificate was:");
199 fr_tls_chain_marker_log(NULL,
L_WARN,
200 chain, leaf, to_verify);
203 case FR_TLS_CHAIN_VERIFY_HARD:
204 ERROR(
"Found multiple self-signed certificates in chain");
205 ERROR(
"First certificate was:");
206 fr_tls_chain_marker_log(NULL,
L_ERR,
207 chain, leaf, *self_signed);
209 ERROR(
"Second certificate was:");
210 fr_tls_chain_marker_log(NULL,
L_ERR,
211 chain, leaf, to_verify);
218 *self_signed = to_verify;
232 (
fr_unix_time_gt(*expires_first, not_after))) *expires_first = not_after;
237 static int tls_ctx_load_cert_chain(SSL_CTX *ctx, fr_tls_chain_conf_t *chain,
bool allow_multi_self_signed)
244 fr_assert(chain->certificate_file && chain->private_key_file);
249 memcpy(&password, &chain->password,
sizeof(password));
250 SSL_CTX_set_default_passwd_cb_userdata(ctx, password);
256 SSL_CTX_set_default_passwd_cb(ctx, fr_tls_session_password_cb);
258 switch (chain->file_format) {
259 case SSL_FILETYPE_PEM:
260 if (!(SSL_CTX_use_certificate_chain_file(ctx, chain->certificate_file))) {
261 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"",
262 chain->certificate_file);
267 case SSL_FILETYPE_ASN1:
268 if (!(SSL_CTX_use_certificate_file(ctx, chain->certificate_file, chain->file_format))) {
269 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"",
270 chain->certificate_file);
280 if (!(SSL_CTX_use_PrivateKey_file(ctx, chain->private_key_file, chain->file_format))) {
281 fr_tls_log(NULL,
"Failed reading private key file \"%s\"",
282 chain->private_key_file);
294 extra_cnt = talloc_array_length(chain->ca_files);
295 for (i = 0; i < extra_cnt; i++) {
298 char const *filename = chain->ca_files[i];
300 fp = fopen(filename,
"r");
309 switch (chain->file_format) {
310 case SSL_FILETYPE_PEM:
311 cert = PEM_read_X509(fp, NULL, NULL, NULL);
314 case SSL_FILETYPE_ASN1:
315 cert = d2i_X509_fp(fp, NULL);
326 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"", filename);
329 SSL_CTX_add0_chain_cert(ctx, cert);
340 if (!SSL_CTX_check_private_key(ctx)) {
341 ERROR(
"Private key does not match the certificate public key");
355 X509 *self_signed = NULL;
356 STACK_OF(X509) *our_chain;
359 if (tls_ctx_verify_chain_member(&expires_first, &self_signed,
360 ctx, SSL_CTX_get0_certificate(ctx),
361 chain->verify_mode) < 0)
return -1;
363 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
364 fr_tls_log(NULL,
"Failed retrieving chain certificates");
368 if (allow_multi_self_signed) self_signed = NULL;
372 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
378 if (tls_ctx_verify_chain_member(&expires_first, &self_signed,
379 ctx, sk_X509_value(our_chain, i - 1),
380 chain->verify_mode) < 0)
return -1;
382 if (allow_multi_self_signed) self_signed = NULL;
391 chain->valid_until = expires_first;
395 int mode = SSL_BUILD_CHAIN_FLAG_CHECK;
397 if (!chain->include_root_ca) mode |= SSL_BUILD_CHAIN_FLAG_NO_ROOT;
405 switch (chain->verify_mode) {
406 case FR_TLS_CHAIN_VERIFY_NONE:
407 mode |= SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR;
408 (void)SSL_CTX_build_cert_chain(ctx, mode);
416 case FR_TLS_CHAIN_VERIFY_SOFT:
417 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
418 fr_tls_strerror_printf(NULL);
419 PWARN(
"Failed verifying chain");
423 case FR_TLS_CHAIN_VERIFY_HARD:
424 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
425 fr_tls_strerror_printf(NULL);
426 PERROR(
"Failed verifying chain");
439 static inline CC_HINT(always_inline)
440 int tls_ctx_version_set(
442 int *ctx_options, SSL_CTX *ctx, fr_tls_conf_t
const *
conf)
456 if (
conf->tls_max_version > (
float) 0.0) {
459 if (
conf->tls_min_version >
conf->tls_max_version) {
464 ERROR(
"tls_min_version (%f) must be <= tls_max_version (%f)",
465 (
double)
conf->tls_min_version, (
double)
conf->tls_max_version);
470 if (
conf->tls_max_version < (
float) 1.0) {
471 ERROR(
"tls_max_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
475 # ifdef TLS1_4_VERSION
476 else if (
conf->tls_max_version >= (
float) 1.4) max_version = TLS1_4_VERSION;
478 # ifdef TLS1_3_VERSION
479 else if (
conf->tls_max_version >= (
float) 1.3) max_version = TLS1_3_VERSION;
481 else if (
conf->tls_max_version >= (
float) 1.2) max_version = TLS1_2_VERSION;
482 else if (
conf->tls_max_version >= (
float) 1.1) max_version = TLS1_1_VERSION;
483 else max_version = TLS1_VERSION;
488 if (max_version < TLS1_2_VERSION) {
489 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
490 WARN(
"tls_max_version SHOULD be 1.2 or greater");
493 if (!SSL_CTX_set_max_proto_version(ctx, max_version)) {
494 fr_tls_log(NULL,
"Failed setting TLS maximum version");
502 if (
conf->tls_min_version < (
float) 1.0) {
503 ERROR(
"tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
506 # ifdef TLS1_4_VERSION
507 else if (
conf->tls_min_version >= (
float) 1.4) min_version = TLS1_4_VERSION;
509 # ifdef TLS1_3_VERSION
510 else if (
conf->tls_min_version >= (
float) 1.3) min_version = TLS1_3_VERSION;
512 else if (
conf->tls_min_version >= (
float) 1.2) min_version = TLS1_2_VERSION;
513 else if (
conf->tls_min_version >= (
float) 1.1) min_version = TLS1_1_VERSION;
514 else min_version = TLS1_VERSION;
519 if (min_version < TLS1_2_VERSION) {
520 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
521 WARN(
"tls_min_version SHOULD be 1.2 or greater");
524 if (!SSL_CTX_set_min_proto_version(ctx, min_version)) {
525 fr_tls_log(NULL,
"Failed setting TLS minimum version");
545 SSL_CTX *fr_tls_ctx_alloc(fr_tls_conf_t
const *
conf,
bool client)
548 X509_STORE *cert_vpstore;
549 X509_STORE *verify_store;
552 ctx = SSL_CTX_new(TLS_method());
554 fr_tls_log(NULL,
"Failed creating TLS context");
562 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_INDEX_CONF,
UNCONST(
void *,
conf));
567 #ifdef PSK_MAX_IDENTITY_LEN
572 if (
conf->psk_query) {
573 if (!*
conf->psk_query) {
574 ERROR(
"Invalid PSK Configuration: psk_query cannot be empty");
580 if (
conf->psk_identity && *
conf->psk_identity) {
581 ERROR(
"Invalid PSK Configuration: psk_identity and psk_query cannot be used at the same time.");
585 if (
conf->psk_password && *
conf->psk_password) {
586 ERROR(
"Invalid PSK Configuration: psk_password and psk_query cannot be used at the same time.");
591 ERROR(
"Invalid PSK Configuration: psk_query cannot be used for outgoing connections");
598 }
else if (
conf->psk_identity) {
599 if (!*
conf->psk_identity) {
600 ERROR(
"Invalid PSK Configuration: psk_identity is empty");
605 if (!
conf->psk_password || !*
conf->psk_password) {
606 ERROR(
"Invalid PSK Configuration: psk_identity is set, but there is no psk_password");
610 }
else if (
conf->psk_password) {
611 ERROR(
"Invalid PSK Configuration: psk_password is set, but there is no psk_identity");
618 if (!client && (
conf->psk_identity ||
conf->psk_query)) {
619 SSL_CTX_set_psk_server_callback(ctx, fr_tls_session_psk_server_cb);
626 if (
conf->psk_identity && *
conf->psk_identity) {
627 size_t psk_len, hex_len;
631 SSL_CTX_set_psk_client_callback(ctx, fr_tls_session_psk_client_cb);
634 if (!
conf->psk_password)
goto error;
636 psk_len = strlen(
conf->psk_password);
637 if (strlen(
conf->psk_password) > (2 * PSK_MAX_PSK_LEN)) {
638 ERROR(
"psk_hexphrase is too long (max %d)", PSK_MAX_PSK_LEN);
649 if (psk_len != (2 * hex_len)) {
650 ERROR(
"psk_hexphrase is not all hex");
664 int mode = SSL_MODE_ASYNC;
675 mode |= SSL_MODE_NO_AUTO_CHAIN;
678 mode |= SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
679 mode |= SSL_MODE_AUTO_RETRY;
682 if (mode) SSL_CTX_set_mode(ctx, mode);
692 MEM(verify_store = X509_STORE_new());
695 SSL_CTX_set0_verify_cert_store(ctx, verify_store);
698 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE, verify_store);
703 if (
conf->ca_file ||
conf->ca_path) {
710 if (!X509_STORE_load_locations(verify_store,
conf->ca_file,
conf->ca_path)) {
711 fr_tls_log(NULL,
"Failed reading Trusted root CA list \"%s\"",
724 X509_STORE_set_purpose(verify_store, X509_PURPOSE_SSL_CLIENT);
733 if (
conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(
conf->ca_file));
735 X509_STORE_set_default_paths(verify_store);
742 size_t chains_conf = talloc_array_length(
conf->chains);
756 for (i = 0; i < chains_conf; i++) {
757 if (tls_ctx_load_cert_chain(ctx,
conf->chains[i],
false) < 0)
goto error;
769 size_t chains_set = 0;
776 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
778 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) chains_set++;
784 DEBUG3(
"Found %zu server certificate chain(s)", chains_set);
786 if (chains_set != chains_conf) {
787 WARN(
"Number of chains configured (%zu) does not match chains set (%zu)",
788 chains_conf, chains_set);
789 if (chains_conf > chains_set)
WARN(
"Only one chain per key type is allowed, "
790 "check config for duplicates");
793 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
795 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) {
796 STACK_OF(X509) *our_chain;
799 our_cert = SSL_CTX_get0_certificate(ctx);
807 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
808 fr_tls_log(NULL,
"Failed retrieving chain certificates");
814 (void)SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
818 #ifdef PSK_MAX_IDENTITY_LEN
821 if (tls_ctx_version_set(&ctx_options, ctx,
conf) < 0)
goto error;
830 if (!
conf->disable_single_dh_use) {
831 ctx_options |= SSL_OP_SINGLE_DH_USE;
834 #ifdef SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
838 if (
conf->allow_renegotiation) {
839 ctx_options |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
849 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
856 if (
conf->cipher_server_preference) ctx_options |= SSL_OP_CIPHER_SERVER_PREFERENCE;
858 SSL_CTX_set_options(ctx, ctx_options);
870 if (
conf->padding_block_size) SSL_CTX_set_block_padding(ctx,
conf->padding_block_size);
875 #ifndef OPENSSL_NO_ECDH
876 if (ctx_ecdh_curve_set(ctx,
conf->ecdh_curve,
conf->disable_single_dh_use) < 0)
goto error;
881 SSL_CTX_set_info_callback(ctx, fr_tls_session_info_cb);
886 #ifdef X509_V_FLAG_CRL_CHECK_ALL
887 if (
conf->verify.check_crl) {
888 cert_vpstore = SSL_CTX_get_cert_store(ctx);
889 if (cert_vpstore == NULL) {
890 fr_tls_log(NULL,
"Error reading Certificate Store");
893 X509_STORE_set_flags(cert_vpstore, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
894 #ifdef X509_V_FLAG_USE_DELTAS
902 X509_STORE_set_flags(cert_vpstore, X509_V_FLAG_USE_DELTAS);
913 if (
conf->verify_depth) {
914 SSL_CTX_set_verify_depth(ctx,
conf->verify_depth);
917 #ifdef HAVE_OPENSSL_OCSP_H
921 if (
conf->staple.enable) {
930 if (
conf->cipher_list) {
931 if (!SSL_CTX_set_cipher_list(ctx,
conf->cipher_list)) {
932 fr_tls_log(NULL,
"Failed setting cipher list");
947 fr_tls_log(NULL,
"Failed creating temporary SSL session");
951 DEBUG3(
"Configured ciphers (by priority)");
953 while ((cipher = SSL_get_cipher_list(ssl, i))) {
954 DEBUG3(
"[%i] %s", i, cipher);
964 if (
conf->dh_file && (ctx_dh_params_load(ctx,
UNCONST(
char *,
conf->dh_file)) < 0))
goto error;
969 if (fr_tls_cache_ctx_init(ctx, &
conf->cache) < 0)
goto error;
974 if ((getenv(
"SSLKEYLOGFILE") != NULL) || (
conf->keylog_file && *
conf->keylog_file)) {
975 SSL_CTX_set_keylog_callback(ctx, fr_tls_session_keylog_cb);
static int const char char buffer[256]
#define fr_base16_decode(_err, _out, _in, _no_trailing)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define USES_APPLE_DEPRECATED_API
#define DIAG_UNKNOWN_PRAGMAS
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
@ L_DBG
Only displayed when debugging is enabled.
int fr_tls_ocsp_staple_cb(SSL *ssl, void *data)
#define FR_SBUFF_IN(_start, _len_or_end)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define fr_unix_time_ispos(_a)
#define fr_unix_time_gt(_a, _b)
#define fr_unix_time_wrap(_time)
char const * fr_tls_utils_x509_pkey_type(X509 *cert)
Returns a friendly identifier for the public key type of a certificate.