27RCSID(
"$Id: 5cf0fa44efabbe69bcd4f1cca7ede0e44859287d $")
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
52static 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);
76static 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);
116 ERROR(
"Unable to set DH parameters");
123static int tls_ctx_verify_chain_member(
fr_unix_time_t *expires_first, X509 **self_signed,
124 SSL_CTX *ctx, X509 *to_verify,
125 fr_tls_chain_verify_mode_t verify_mode)
129 STACK_OF(X509) *chain;
132 leaf = SSL_CTX_get0_certificate(ctx);
134 ERROR(
"Chain does not contain a valid leaf certificate");
138 if (!SSL_CTX_get0_chain_certs(ctx, &chain)) {
139 fr_tls_log(NULL,
"Failed retrieving chain certificates");
143 switch (fr_tls_cert_is_valid(NULL, ¬_after, to_verify)) {
145 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
146 PERROR(
"Malformed certificate");
151 switch (verify_mode) {
152 case FR_TLS_CHAIN_VERIFY_SOFT:
153 fr_tls_chain_marker_log(NULL,
L_WARN, chain, leaf, to_verify);
154 PWARN(
"Certificate validation failed");
157 case FR_TLS_CHAIN_VERIFY_HARD:
158 fr_tls_chain_marker_log(NULL,
L_ERR, chain, leaf, to_verify);
159 PERROR(
"Certificate validation failed");
171 switch (verify_mode) {
172 case FR_TLS_CHAIN_VERIFY_SOFT:
173 case FR_TLS_CHAIN_VERIFY_HARD:
189 if (X509_name_cmp(X509_get_subject_name(to_verify),
190 X509_get_issuer_name(to_verify)) == 0) {
192 switch (verify_mode) {
193 case FR_TLS_CHAIN_VERIFY_SOFT:
194 WARN(
"Found multiple self-signed certificates in chain");
195 WARN(
"First certificate was:");
196 fr_tls_chain_marker_log(NULL,
L_WARN,
197 chain, leaf, *self_signed);
199 WARN(
"Second certificate was:");
200 fr_tls_chain_marker_log(NULL,
L_WARN,
201 chain, leaf, to_verify);
204 case FR_TLS_CHAIN_VERIFY_HARD:
205 ERROR(
"Found multiple self-signed certificates in chain");
206 ERROR(
"First certificate was:");
207 fr_tls_chain_marker_log(NULL,
L_ERR,
208 chain, leaf, *self_signed);
210 ERROR(
"Second certificate was:");
211 fr_tls_chain_marker_log(NULL,
L_ERR,
212 chain, leaf, to_verify);
219 *self_signed = to_verify;
233 (
fr_unix_time_gt(*expires_first, not_after))) *expires_first = not_after;
238static int tls_ctx_load_cert_chain(SSL_CTX *ctx, fr_tls_chain_conf_t *chain,
bool allow_multi_self_signed)
245 fr_assert(chain->certificate_file && chain->private_key_file);
250 memcpy(&password, &chain->password,
sizeof(password));
251 SSL_CTX_set_default_passwd_cb_userdata(ctx, password);
257 SSL_CTX_set_default_passwd_cb(ctx, fr_tls_session_password_cb);
259 switch (chain->file_format) {
260 case SSL_FILETYPE_PEM:
261 if (!(SSL_CTX_use_certificate_chain_file(ctx, chain->certificate_file))) {
262 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"",
263 chain->certificate_file);
268 case SSL_FILETYPE_ASN1:
269 if (!(SSL_CTX_use_certificate_file(ctx, chain->certificate_file, chain->file_format))) {
270 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"",
271 chain->certificate_file);
281 if (!(SSL_CTX_use_PrivateKey_file(ctx, chain->private_key_file, chain->file_format))) {
282 fr_tls_log(NULL,
"Failed reading private key file \"%s\"",
283 chain->private_key_file);
295 extra_cnt = talloc_array_length(chain->ca_files);
296 for (i = 0; i < extra_cnt; i++) {
299 char const *filename = chain->ca_files[i];
301 fp = fopen(filename,
"r");
310 switch (chain->file_format) {
311 case SSL_FILETYPE_PEM:
312 cert = PEM_read_X509(fp, NULL, NULL, NULL);
315 case SSL_FILETYPE_ASN1:
316 cert = d2i_X509_fp(fp, NULL);
327 fr_tls_log(NULL,
"Failed reading certificate file \"%s\"", filename);
331 if (SSL_CTX_add0_chain_cert(ctx, cert) != 1) {
332 fr_tls_log(NULL,
"Failed adding certificate to chain for \"%s\"", filename);
346 if (!SSL_CTX_check_private_key(ctx)) {
347 ERROR(
"Private key does not match the certificate public key");
361 X509 *self_signed = NULL;
362 STACK_OF(X509) *our_chain;
365 if (tls_ctx_verify_chain_member(&expires_first, &self_signed,
366 ctx, SSL_CTX_get0_certificate(ctx),
367 chain->verify_mode) < 0)
return -1;
369 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
370 fr_tls_log(NULL,
"Failed retrieving chain certificates");
374 if (allow_multi_self_signed) self_signed = NULL;
378 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
384 if (tls_ctx_verify_chain_member(&expires_first, &self_signed,
385 ctx, sk_X509_value(our_chain, i - 1),
386 chain->verify_mode) < 0)
return -1;
388 if (allow_multi_self_signed) self_signed = NULL;
397 chain->valid_until = expires_first;
401 int mode = SSL_BUILD_CHAIN_FLAG_CHECK;
403 if (!chain->include_root_ca) mode |= SSL_BUILD_CHAIN_FLAG_NO_ROOT;
411 switch (chain->verify_mode) {
412 case FR_TLS_CHAIN_VERIFY_NONE:
413 mode |= SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR;
414 (void)SSL_CTX_build_cert_chain(ctx, mode);
422 case FR_TLS_CHAIN_VERIFY_SOFT:
423 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
424 fr_tls_strerror_printf(NULL);
425 PWARN(
"Failed verifying chain");
429 case FR_TLS_CHAIN_VERIFY_HARD:
430 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
431 fr_tls_strerror_printf(NULL);
432 PERROR(
"Failed verifying chain");
445static inline CC_HINT(always_inline)
446int tls_ctx_version_set(
448 int *ctx_options, SSL_CTX *ctx, fr_tls_conf_t
const *
conf)
462 if (
conf->tls_max_version > (
float) 0.0) {
465 if (
conf->tls_min_version >
conf->tls_max_version) {
470 ERROR(
"tls_min_version (%f) must be <= tls_max_version (%f)",
471 (
double)
conf->tls_min_version, (
double)
conf->tls_max_version);
476 if (
conf->tls_max_version < (
float) 1.0) {
477 ERROR(
"tls_max_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
481# ifdef TLS1_4_VERSION
482 else if (
conf->tls_max_version >= (
float) 1.4) max_version = TLS1_4_VERSION;
484# ifdef TLS1_3_VERSION
485 else if (
conf->tls_max_version >= (
float) 1.3) max_version = TLS1_3_VERSION;
487 else if (
conf->tls_max_version >= (
float) 1.2) max_version = TLS1_2_VERSION;
488 else if (
conf->tls_max_version >= (
float) 1.1) max_version = TLS1_1_VERSION;
489 else max_version = TLS1_VERSION;
494 if (max_version < TLS1_2_VERSION) {
495 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
496 WARN(
"tls_max_version SHOULD be 1.2 or greater");
499 if (!SSL_CTX_set_max_proto_version(ctx, max_version)) {
500 fr_tls_log(NULL,
"Failed setting TLS maximum version");
508 if (
conf->tls_min_version < (
float) 1.0) {
509 ERROR(
"tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
512# ifdef TLS1_4_VERSION
513 else if (
conf->tls_min_version >= (
float) 1.4) min_version = TLS1_4_VERSION;
515# ifdef TLS1_3_VERSION
516 else if (
conf->tls_min_version >= (
float) 1.3) min_version = TLS1_3_VERSION;
518 else if (
conf->tls_min_version >= (
float) 1.2) min_version = TLS1_2_VERSION;
519 else if (
conf->tls_min_version >= (
float) 1.1) min_version = TLS1_1_VERSION;
520 else min_version = TLS1_VERSION;
525 if (min_version < TLS1_2_VERSION) {
526 WARN(
"TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
527 WARN(
"tls_min_version SHOULD be 1.2 or greater");
530 if (!SSL_CTX_set_min_proto_version(ctx, min_version)) {
531 fr_tls_log(NULL,
"Failed setting TLS minimum version");
551SSL_CTX *fr_tls_ctx_alloc(fr_tls_conf_t
const *
conf,
bool client)
554 X509_STORE *cert_vpstore;
555 X509_STORE *verify_store;
558 ctx = SSL_CTX_new(TLS_method());
560 fr_tls_log(NULL,
"Failed creating TLS context");
568 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_INDEX_CONF,
UNCONST(
void *,
conf));
573#ifdef PSK_MAX_IDENTITY_LEN
578 if (
conf->psk_query) {
579 if (!*
conf->psk_query) {
580 ERROR(
"Invalid PSK Configuration: psk_query cannot be empty");
586 if (
conf->psk_identity && *
conf->psk_identity) {
587 ERROR(
"Invalid PSK Configuration: psk_identity and psk_query cannot be used at the same time.");
591 if (
conf->psk_password && *
conf->psk_password) {
592 ERROR(
"Invalid PSK Configuration: psk_hexphrase and psk_query cannot be used at the same time.");
597 ERROR(
"Invalid PSK Configuration: psk_query cannot be used for outgoing connections");
604 }
else if (
conf->psk_identity) {
605 if (!*
conf->psk_identity) {
606 ERROR(
"Invalid PSK Configuration: psk_identity is empty");
611 if (!
conf->psk_password || !*
conf->psk_password) {
612 ERROR(
"Invalid PSK Configuration: psk_identity is set, but there is no psk_hexphrase");
616 }
else if (
conf->psk_password) {
617 ERROR(
"Invalid PSK Configuration: psk_hexphrase is set, but there is no psk_identity");
624 if (!client && (
conf->psk_identity ||
conf->psk_query)) {
625 SSL_CTX_set_psk_server_callback(ctx, fr_tls_session_psk_server_cb);
632 if (
conf->psk_identity && *
conf->psk_identity) {
633 size_t psk_len, hex_len;
637 SSL_CTX_set_psk_client_callback(ctx, fr_tls_session_psk_client_cb);
640 if (!
conf->psk_password)
goto error;
642 psk_len = strlen(
conf->psk_password);
643 if (strlen(
conf->psk_password) > (2 * PSK_MAX_PSK_LEN)) {
644 ERROR(
"psk_hexphrase is too long (max %d)", PSK_MAX_PSK_LEN);
655 if (psk_len != (2 * hex_len)) {
656 ERROR(
"psk_hexphrase is not all hex");
670 int mode = SSL_MODE_ASYNC;
681 mode |= SSL_MODE_NO_AUTO_CHAIN;
684 mode |= SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
685 mode |= SSL_MODE_AUTO_RETRY;
688 if (mode) SSL_CTX_set_mode(ctx, mode);
698 MEM(verify_store = X509_STORE_new());
701 SSL_CTX_set0_verify_cert_store(ctx, verify_store);
704 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE, verify_store);
709 if (
conf->ca_file ||
conf->ca_path) {
716 if (!X509_STORE_load_locations(verify_store,
conf->ca_file,
conf->ca_path)) {
717 fr_tls_log(NULL,
"Failed reading Trusted root CA list \"%s\"",
730 X509_STORE_set_purpose(verify_store, X509_PURPOSE_SSL_CLIENT);
739 if (
conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(
conf->ca_file));
741 X509_STORE_set_default_paths(verify_store);
748 size_t chains_conf = talloc_array_length(
conf->chains);
762 for (i = 0; i < chains_conf; i++) {
763 if (tls_ctx_load_cert_chain(ctx,
conf->chains[i],
false) < 0)
goto error;
775 size_t chains_set = 0;
782 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
784 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) chains_set++;
790 DEBUG3(
"Found %zu server certificate chain(s)", chains_set);
792 if (chains_set != chains_conf) {
793 WARN(
"Number of chains configured (%zu) does not match chains set (%zu)",
794 chains_conf, chains_set);
795 if (chains_conf > chains_set)
WARN(
"Only one chain per key type is allowed, "
796 "check config for duplicates");
799 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
801 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) {
802 STACK_OF(X509) *our_chain;
805 our_cert = SSL_CTX_get0_certificate(ctx);
813 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
814 fr_tls_log(NULL,
"Failed retrieving chain certificates");
820 (void)SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
824#ifdef PSK_MAX_IDENTITY_LEN
827 if (tls_ctx_version_set(&ctx_options, ctx,
conf) < 0)
goto error;
836 if (!
conf->disable_single_dh_use) {
837 ctx_options |= SSL_OP_SINGLE_DH_USE;
840#ifdef SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
844 if (
conf->allow_renegotiation) {
845 ctx_options |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
855 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
862 if (
conf->cipher_server_preference) ctx_options |= SSL_OP_CIPHER_SERVER_PREFERENCE;
864 SSL_CTX_set_options(ctx, ctx_options);
876 if (
conf->padding_block_size) SSL_CTX_set_block_padding(ctx,
conf->padding_block_size);
881#ifndef OPENSSL_NO_ECDH
882 if (ctx_ecdh_curve_set(ctx,
conf->ecdh_curve,
conf->disable_single_dh_use) < 0)
goto error;
887 SSL_CTX_set_info_callback(ctx, fr_tls_session_info_cb);
892#ifdef X509_V_FLAG_CRL_CHECK_ALL
893 if (
conf->verify.check_crl) {
894 cert_vpstore = SSL_CTX_get_cert_store(ctx);
895 if (cert_vpstore == NULL) {
896 fr_tls_log(NULL,
"Error reading Certificate Store");
899 X509_STORE_set_flags(cert_vpstore, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
900#ifdef X509_V_FLAG_USE_DELTAS
908 X509_STORE_set_flags(cert_vpstore, X509_V_FLAG_USE_DELTAS);
919 if (
conf->verify_depth) {
920 SSL_CTX_set_verify_depth(ctx,
conf->verify_depth);
923#ifdef HAVE_OPENSSL_OCSP_H
927 if (
conf->staple.enable) {
936 if (
conf->cipher_list) {
937 if (!SSL_CTX_set_cipher_list(ctx,
conf->cipher_list)) {
938 fr_tls_log(NULL,
"Failed setting cipher list");
953 fr_tls_log(NULL,
"Failed creating temporary SSL session");
957 DEBUG3(
"Configured ciphers (by priority)");
959 while ((cipher = SSL_get_cipher_list(ssl, i))) {
960 DEBUG3(
"[%u] %s", i, cipher);
971 if (ctx_dh_params_load(ctx,
UNCONST(
char *,
conf->dh_file)) < 0)
goto error;
979 SSL_CTX_set_dh_auto(ctx, 1);
985 if (fr_tls_cache_ctx_init(ctx, &
conf->cache) < 0)
goto error;
990 if ((getenv(
"SSLKEYLOGFILE") != NULL) || (
conf->keylog_file && *
conf->keylog_file)) {
991 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)
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.