25RCSID(
"$Id: e4a6285f6eba36fc4f9fcc611214eed931b43e1e $")
29#define LOG_PREFIX "tls"
32#include <freeradius-devel/tls/log.h>
33#include <freeradius-devel/tls/strerror.h>
34#include <freeradius-devel/util/base16.h>
35#include <freeradius-devel/util/debug.h>
36#include <freeradius-devel/util/misc.h>
37#include <freeradius-devel/util/syserror.h>
45#include <openssl/rand.h>
46#include <openssl/dh.h>
47#include <openssl/x509v3.h>
48#include <openssl/provider.h>
50#ifndef OPENSSL_NO_ECDH
51static int ctx_ecdh_curve_set(SSL_CTX *ctx,
char const *ecdh_curve,
bool disable_single_dh_use)
55 if (!disable_single_dh_use) {
56 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
59 if (!ecdh_curve || !*ecdh_curve)
return 0;
61 list = strdup(ecdh_curve);
62 if (SSL_CTX_set1_curves_list(ctx, list) == 0) {
64 ERROR(
"Unknown ecdh_curve \"%s\"", ecdh_curve);
75static int ctx_dh_params_load(SSL_CTX *ctx,
char *file)
94 if (EVP_default_properties_is_fips_enabled(NULL)) {
95 WARN(
LOG_PREFIX ": Ignoring user-selected DH parameters in FIPS mode. Using defaults.");
99 if ((bio = BIO_new_file(file,
"r")) == NULL) {
100 ERROR(
"Unable to open DH file - %s", file);
104 dh = PEM_read_bio_Parameters(bio, &dh);
107 WARN(
"Unable to set DH parameters. DH cipher suites may not work!");
108 WARN(
"Fix this by generating the DH parameter file");
112 ret = SSL_CTX_set0_tmp_dh_pkey(ctx, dh);
115 ERROR(
"Unable to set DH parameters");
122static int tls_ctx_verify_chain_member(X509 **self_signed, SSL_CTX *ctx, X509 *to_verify,
123 fr_tls_chain_verify_mode_t verify_mode)
127 STACK_OF(X509) *chain;
130 leaf = SSL_CTX_get0_certificate(ctx);
132 ERROR(
"Chain does not contain a valid leaf certificate");
136 if (!SSL_CTX_get0_chain_certs(ctx, &chain)) {
137 fr_tls_log_perror(NULL,
"Failed retrieving chain certificates");
141 switch (fr_tls_cert_is_valid(NULL, ¬_after, to_verify)) {
143 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
144 PERROR(
"Malformed certificate");
149 switch (verify_mode) {
150 case FR_TLS_CHAIN_VERIFY_SOFT:
151 fr_tls_chain_marker_log(NULL,
L_WARN, chain, leaf, to_verify);
152 PWARN(
"Certificate validation failed");
155 case FR_TLS_CHAIN_VERIFY_HARD:
156 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
157 PERROR(
"Certificate validation failed");
169 switch (verify_mode) {
170 case FR_TLS_CHAIN_VERIFY_SOFT:
171 case FR_TLS_CHAIN_VERIFY_HARD:
187 if (X509_name_cmp(X509_get_subject_name(to_verify),
188 X509_get_issuer_name(to_verify)) == 0) {
190 switch (verify_mode) {
191 case FR_TLS_CHAIN_VERIFY_SOFT:
192 WARN(
"Found multiple self-signed certificates in chain");
193 WARN(
"First certificate was:");
194 fr_tls_chain_marker_log(NULL,
L_WARN,
195 chain, leaf, *self_signed);
197 WARN(
"Second certificate was:");
198 fr_tls_chain_marker_log(NULL,
L_WARN,
199 chain, leaf, to_verify);
202 case FR_TLS_CHAIN_VERIFY_HARD:
203 ERROR(
"Found multiple self-signed certificates in chain");
204 ERROR(
"First certificate was:");
205 fr_tls_chain_marker_log(NULL,
L_ERR,
206 chain, leaf, *self_signed);
208 ERROR(
"Second certificate was:");
209 fr_tls_chain_marker_log(NULL,
L_ERR,
210 chain, leaf, to_verify);
217 *self_signed = to_verify;
228static int tls_ctx_load_cert_chain(SSL_CTX *ctx, fr_tls_chain_conf_t *chain,
bool allow_multi_self_signed)
236 fr_assert(chain->leaf && chain->private_key);
238 if (SSL_CTX_use_certificate(ctx, chain->leaf) != 1) {
239 fr_tls_log_perror(NULL,
"Failed setting certificate from \"%s\"", chain->certificate_file);
244 for (i = 0; i < sk_X509_num(chain->extra); i++) {
245 if (SSL_CTX_add1_chain_cert(ctx, sk_X509_value(chain->extra, i)) != 1) {
246 fr_tls_log_perror(NULL,
"Failed adding certificate %d to the chain for \"%s\"",
247 i + 1, chain->certificate_file);
253 if (SSL_CTX_use_PrivateKey(ctx, chain->private_key) != 1) {
254 fr_tls_log_perror(NULL,
"Failed setting private key from \"%s\"", chain->private_key_file);
264 X509 *self_signed = NULL;
265 STACK_OF(X509) *our_chain;
267 if (tls_ctx_verify_chain_member(&self_signed, ctx, SSL_CTX_get0_certificate(ctx),
268 chain->verify_mode) < 0)
return -1;
270 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
271 fr_tls_log_perror(NULL,
"Failed retrieving chain certificates");
275 if (allow_multi_self_signed) self_signed = NULL;
278 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
284 if (tls_ctx_verify_chain_member(&self_signed, ctx, sk_X509_value(our_chain, i - 1),
285 chain->verify_mode) < 0)
return -1;
287 if (allow_multi_self_signed) self_signed = NULL;
293 int mode = SSL_BUILD_CHAIN_FLAG_CHECK;
295 if (!chain->include_root_ca) mode |= SSL_BUILD_CHAIN_FLAG_NO_ROOT;
303 switch (chain->verify_mode) {
304 case FR_TLS_CHAIN_VERIFY_NONE:
305 mode |= SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR;
306 (void)SSL_CTX_build_cert_chain(ctx, mode);
314 case FR_TLS_CHAIN_VERIFY_SOFT:
315 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
316 fr_tls_strerror_printf(NULL);
317 PWARN(
"Failed verifying chain");
321 case FR_TLS_CHAIN_VERIFY_HARD:
322 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
323 fr_tls_strerror_printf(NULL);
324 PERROR(
"Failed verifying chain");
352static inline CC_HINT(always_inline)
353int tls_ctx_version_set_dtls(SSL_CTX *ctx, fr_tls_conf_t
const *
conf)
357 if (
conf->tls_max_version > (
float) 0.0) {
358 if (
conf->tls_min_version >
conf->tls_max_version) {
359 ERROR(
"tls_min_version (%f) must be <= tls_max_version (%f)",
360 (
double)
conf->tls_min_version, (
double)
conf->tls_max_version);
368 if (
conf->tls_max_version >= (
float) 1.2) {
369 version = DTLS1_2_VERSION;
371 }
else if (
conf->tls_max_version >= (
float) 1.1) {
372 ERROR(
"There is no DTLS 1.1. Set tls_max_version to 1.0 or 1.2");
375 }
else if (
conf->tls_max_version >= (
float) 1.0) {
376 version = DTLS1_VERSION;
377 WARN(
"DTLS 1.0 is insecure and SHOULD NOT be used");
378 WARN(
"tls_max_version SHOULD be 1.2");
381 ERROR(
"tls_max_version must be >= 1.0");
385 if (!SSL_CTX_set_max_proto_version(ctx, version)) {
386 fr_tls_log_perror(NULL,
"Failed setting DTLS maximum version");
396 if (
conf->tls_min_version >= (
float) 1.3) {
397 ERROR(
"DTLS 1.3 is not supported by this version of OpenSSL");
400 }
else if (
conf->tls_min_version >= (
float) 1.2) {
401 version = DTLS1_2_VERSION;
403 }
else if (
conf->tls_min_version >= (
float) 1.1) {
404 ERROR(
"There is no DTLS 1.1. Set tls_min_version to 1.0 or 1.2");
407 }
else if (
conf->tls_min_version >= (
float) 1.0) {
408 version = DTLS1_VERSION;
409 WARN(
"DTLS 1.0 is insecure and SHOULD NOT be used");
410 WARN(
"tls_min_version SHOULD be 1.2");
413 ERROR(
"tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
417 if (!SSL_CTX_set_min_proto_version(ctx, version)) {
418 fr_tls_log_perror(NULL,
"Failed setting DTLS minimum version");
425static inline CC_HINT(always_inline)
426int tls_ctx_version_set(
UNUSED int *ctx_options, SSL_CTX *ctx, fr_tls_conf_t
const *
conf)
428 if (
conf->socket_type == SOCK_DGRAM)
return tls_ctx_version_set_dtls(ctx,
conf);
442 if (
conf->tls_max_version > (
float) 0.0) {
445 if (
conf->tls_min_version >
conf->tls_max_version) {
450 ERROR(
"tls_min_version (%f) must be <= tls_max_version (%f)",
451 (
double)
conf->tls_min_version, (
double)
conf->tls_max_version);
456 if (
conf->tls_max_version < (
float) 1.0) {
457 ERROR(
"tls_max_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
461# ifdef TLS1_4_VERSION
462 else if (
conf->tls_max_version >= (
float) 1.4) max_version = TLS1_4_VERSION;
464 else if (
conf->tls_max_version >= (
float) 1.3) max_version = TLS1_3_VERSION;
465 else if (
conf->tls_max_version >= (
float) 1.2) max_version = TLS1_2_VERSION;
466 else if (
conf->tls_max_version >= (
float) 1.1) max_version = TLS1_1_VERSION;
467 else max_version = TLS1_VERSION;
472 if (max_version < TLS1_2_VERSION) {
473 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
474 WARN(
"tls_max_version SHOULD be 1.2 or greater");
477 if (!SSL_CTX_set_max_proto_version(ctx, max_version)) {
478 fr_tls_log_perror(NULL,
"Failed setting TLS maximum version");
486 if (
conf->tls_min_version < (
float) 1.0) {
487 ERROR(
"tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
490# ifdef TLS1_4_VERSION
491 else if (
conf->tls_min_version >= (
float) 1.4) min_version = TLS1_4_VERSION;
493 else if (
conf->tls_min_version >= (
float) 1.3) min_version = TLS1_3_VERSION;
494 else if (
conf->tls_min_version >= (
float) 1.2) min_version = TLS1_2_VERSION;
495 else if (
conf->tls_min_version >= (
float) 1.1) min_version = TLS1_1_VERSION;
496 else min_version = TLS1_VERSION;
501 if (min_version < TLS1_2_VERSION) {
502 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
503 WARN(
"tls_min_version SHOULD be 1.2 or greater");
506 if (!SSL_CTX_set_min_proto_version(ctx, min_version)) {
507 fr_tls_log_perror(NULL,
"Failed setting TLS minimum version");
527SSL_CTX *fr_tls_ctx_alloc(fr_tls_conf_t
const *
conf,
bool client)
530 X509_STORE *verify_store = NULL;
532 int mode= SSL_MODE_ASYNC;
540 switch (
conf->socket_type) {
552 ERROR(
"Unsupported socket type %d for a TLS context, expected SOCK_STREAM (%d) "
553 "or SOCK_DGRAM (%d)",
conf->socket_type, SOCK_STREAM, SOCK_DGRAM);
557 ctx = SSL_CTX_new((
conf->socket_type == SOCK_DGRAM) ? DTLS_method() : TLS_method());
559 fr_tls_log_perror(NULL,
"Failed creating TLS context");
567 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_INDEX_CONF,
UNCONST(
void *,
conf));
572#ifdef PSK_MAX_IDENTITY_LEN
577 if (
conf->load_psk) {
578 if (
conf->psk.identity ||
conf->psk.key) {
579 ERROR(
"Invalid PSK Configuration: a fixed key is set in the psk section and the virtual server "
580 "has a \"load psk\" section. Remove one of the two");
589 }
else if (
conf->psk.identity) {
590 size_t key_len =
conf->psk.key ? talloc_array_length(
conf->psk.key) : 0;
592 if (!*
conf->psk.identity) {
593 ERROR(
"Invalid PSK Configuration: psk.identity is empty");
598 ERROR(
"Invalid PSK Configuration: psk.identity is set but psk.key is missing or empty");
602 if (key_len > PSK_MAX_PSK_LEN) {
603 ERROR(
"Invalid PSK Configuration: psk.key is %zu bytes, OpenSSL accepts at most %d bytes",
604 key_len, PSK_MAX_PSK_LEN);
608 }
else if (
conf->psk.key) {
609 ERROR(
"Invalid PSK Configuration: psk.key is set but psk.identity is not set");
616 if (!client && (
conf->psk.identity ||
conf->load_psk)) {
617 SSL_CTX_set_psk_server_callback(ctx, fr_tls_session_psk_server_cb);
624 if (
conf->psk.identity) {
626 SSL_CTX_set_psk_client_callback(ctx, fr_tls_session_psk_client_cb);
629 SSL_CTX_set_mode(ctx, mode);
643 mode |= SSL_MODE_NO_AUTO_CHAIN;
646 mode |= SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
647 mode |= SSL_MODE_AUTO_RETRY;
650 SSL_CTX_set_mode(ctx, mode);
659 MEM(verify_store = X509_STORE_new());
662 SSL_CTX_set0_verify_cert_store(ctx, verify_store);
665 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE, verify_store);
670 if (
conf->ca_file ||
conf->ca_path) {
677 if (!X509_STORE_load_locations(verify_store,
conf->ca_file,
conf->ca_path)) {
678 fr_tls_log_perror(NULL,
"Failed reading Trusted root CA list \"%s\"",
694 X509_STORE_set_purpose(verify_store, client ? X509_PURPOSE_SSL_SERVER : X509_PURPOSE_SSL_CLIENT);
703 if (
conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(
conf->ca_file));
705 X509_STORE_set_default_paths(verify_store);
712 size_t chains_conf = talloc_array_length(
conf->chains);
726 for (i = 0; i < chains_conf; i++) {
727 if (tls_ctx_load_cert_chain(ctx,
conf->chains[i],
false) < 0)
goto error;
739 size_t chains_set = 0;
746 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
748 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) chains_set++;
754 DEBUG3(
"Found %zu server certificate chain(s)", chains_set);
756 if (chains_set != chains_conf) {
757 WARN(
"Number of chains configured (%zu) does not match chains set (%zu)",
758 chains_conf, chains_set);
759 if (chains_conf > chains_set)
WARN(
"Only one chain per key type is allowed, "
760 "check config for duplicates");
763 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
765 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) {
766 STACK_OF(X509) *our_chain;
769 our_cert = SSL_CTX_get0_certificate(ctx);
777 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
778 fr_tls_log_perror(NULL,
"Failed retrieving chain certificates");
784 (void)SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
788#ifdef PSK_MAX_IDENTITY_LEN
791 if (tls_ctx_version_set(&ctx_options, ctx,
conf) < 0)
goto error;
800 if (!
conf->disable_single_dh_use) {
801 ctx_options |= SSL_OP_SINGLE_DH_USE;
804#ifdef SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
808 if (
conf->allow_renegotiation) {
809 ctx_options |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
819 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
826 if (
conf->cipher_server_preference) ctx_options |= SSL_OP_CIPHER_SERVER_PREFERENCE;
828 SSL_CTX_set_options(ctx, ctx_options);
840 if (
conf->padding_block_size) SSL_CTX_set_block_padding(ctx,
conf->padding_block_size);
845#ifndef OPENSSL_NO_ECDH
846 if (ctx_ecdh_curve_set(ctx,
conf->ecdh_curve,
conf->disable_single_dh_use) < 0)
goto error;
851 SSL_CTX_set_info_callback(ctx, fr_tls_session_info_cb);
859 if (
conf->verify_depth) {
860 SSL_CTX_set_verify_depth(ctx,
conf->verify_depth);
863#ifdef HAVE_OPENSSL_OCSP_H
867 if (
conf->staple.enable) {
868 SSL_CTX_set_tlsext_status_cb(ctx, fr_tls_ocsp_staple_cb);
869 SSL_CTX_set_tlsext_status_arg(ctx,
UNCONST(fr_tls_ocsp_conf_t *, &(
conf->staple)));
887 if (
conf->cipher_list &&
888 !SSL_CTX_set_cipher_list(ctx,
conf->cipher_list)) {
889 unsigned long err = ERR_peek_last_error();
892 if (ERR_GET_LIB(err) != ERR_LIB_SSL ||
893 ERR_GET_REASON(err) != SSL_R_NO_CIPHER_MATCH) {
894 fr_tls_log_perror(NULL,
"Failed setting cipher_list");
906 if (
conf->cipher_suites &&
907 !SSL_CTX_set_ciphersuites(ctx,
conf->cipher_suites)) {
908 unsigned long err = ERR_peek_last_error();
911 if (ERR_GET_LIB(err) != ERR_LIB_SSL ||
912 ERR_GET_REASON(err) != SSL_R_NO_CIPHER_MATCH) {
913 fr_tls_log_perror(NULL,
"Failed setting cipher_suites");
925 if (
conf->group_list) {
926 if (!SSL_CTX_set1_groups_list(ctx,
UNCONST(
char *,
conf->group_list))) {
927 fr_tls_log_perror(NULL,
"Failed setting group list");
942 fr_tls_log_perror(NULL,
"Failed creating temporary SSL session");
946 DEBUG3(
"Configured ciphers (by priority)");
948 while ((cipher = SSL_get_cipher_list(ssl, i))) {
949 DEBUG3(
"[%u] %s", i, cipher);
960 if (ctx_dh_params_load(ctx,
UNCONST(
char *,
conf->dh_file)) < 0)
goto error;
968 SSL_CTX_set_dh_auto(ctx, 1);
979 if (
conf->socket_type == SOCK_DGRAM) {
980 if (
conf->cache.mode == FR_TLS_TICKET_STATELESS) {
981 ERROR(
"session { mode = \"stateless\" } is not supported with DTLS. "
982 "Use \"stateful\" or \"disabled\"");
986 if (
conf->cache.mode == FR_TLS_TICKET_AUTO) {
987 UNCONST(fr_tls_conf_t *,
conf)->cache.mode = FR_TLS_TICKET_STATEFUL;
998 SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE);
999 SSL_CTX_set_cookie_generate_cb(ctx, fr_dtls_cookie_generate_cb);
1000 SSL_CTX_set_cookie_verify_cb(ctx, fr_dtls_cookie_verify_cb);
1007 if (fr_tls_ticket_ctx_init(ctx, &
conf->cache, client) < 0)
goto error;
1012 if (fr_tls_ctx_alpn_set(ctx,
conf, client) < 0)
goto error;
1017 if ((getenv(
"SSLKEYLOGFILE") != NULL) || (
conf->keylog_file && *
conf->keylog_file)) {
1018 SSL_CTX_set_keylog_callback(ctx, fr_tls_session_keylog_cb);
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define USES_APPLE_DEPRECATED_API
#define DIAG_OFF_OPTIONAL(_x)
#define DIAG_ON_OPTIONAL(_x)
#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.
char const * fr_tls_utils_x509_pkey_type(X509 *cert)
Returns a friendly identifier for the public key type of a certificate.