28#define LOG_PREFIX "tls"
30#include <freeradius-devel/server/pair.h>
31#include <freeradius-devel/server/log.h>
33#include <freeradius-devel/util/debug.h>
34#include <freeradius-devel/util/base16.h>
35#include <freeradius-devel/util/misc.h>
36#include <freeradius-devel/util/pair_legacy.h>
38#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
40#include <freeradius-devel/unlang/call.h>
41#include <freeradius-devel/unlang/subrequest.h>
42#include <freeradius-devel/unlang/interpret.h>
52#include <openssl/x509v3.h>
53#include <openssl/ssl.h>
55static char const *tls_version_str[] = {
56 [SSL2_VERSION] =
"SSL 2.0",
57 [SSL3_VERSION] =
"SSL 3.0",
58 [TLS1_VERSION] =
"TLS 1.0",
60 [TLS1_1_VERSION] =
"TLS 1.1",
63 [TLS1_2_VERSION] =
"TLS 1.2",
66 [TLS1_3_VERSION] =
"TLS 1.3",
69 [TLS1_4_VERSION] =
"TLS 1.4",
73static char const *tls_content_type_str[] = {
74 [SSL3_RT_CHANGE_CIPHER_SPEC] =
"change_cipher_spec",
75 [SSL3_RT_ALERT] =
"alert",
76 [SSL3_RT_HANDSHAKE] =
"handshake",
77 [SSL3_RT_APPLICATION_DATA] =
"application_data",
79 [SSL3_RT_HEADER] =
"header",
81#ifdef SSL3_RT_INNER_CONTENT_TYPE
82 [SSL3_RT_INNER_CONTENT_TYPE] =
"inner_content_type",
86static char const *tls_alert_description_str[] = {
87 [SSL3_AD_CLOSE_NOTIFY] =
"close_notify",
88 [SSL3_AD_UNEXPECTED_MESSAGE] =
"unexpected_message",
89 [SSL3_AD_BAD_RECORD_MAC] =
"bad_record_mac",
90 [TLS1_AD_DECRYPTION_FAILED] =
"decryption_failed",
91 [TLS1_AD_RECORD_OVERFLOW] =
"record_overflow",
92 [SSL3_AD_DECOMPRESSION_FAILURE] =
"decompression_failure",
93 [SSL3_AD_HANDSHAKE_FAILURE] =
"handshake_failure",
94 [SSL3_AD_BAD_CERTIFICATE] =
"bad_certificate",
95 [SSL3_AD_UNSUPPORTED_CERTIFICATE] =
"unsupported_certificate",
96 [SSL3_AD_CERTIFICATE_REVOKED] =
"certificate_revoked",
97 [SSL3_AD_CERTIFICATE_EXPIRED] =
"certificate_expired",
98 [SSL3_AD_CERTIFICATE_UNKNOWN] =
"certificate_unknown",
99 [SSL3_AD_ILLEGAL_PARAMETER] =
"illegal_parameter",
100 [TLS1_AD_UNKNOWN_CA] =
"unknown_ca",
101 [TLS1_AD_ACCESS_DENIED] =
"access_denied",
102 [TLS1_AD_DECODE_ERROR] =
"decode_error",
103 [TLS1_AD_DECRYPT_ERROR] =
"decrypt_error",
104 [TLS1_AD_EXPORT_RESTRICTION] =
"export_restriction",
105 [TLS1_AD_PROTOCOL_VERSION] =
"protocol_version",
106 [TLS1_AD_INSUFFICIENT_SECURITY] =
"insufficient_security",
107 [TLS1_AD_INTERNAL_ERROR] =
"internal_error",
108 [TLS1_AD_USER_CANCELLED] =
"user_cancelled",
109 [TLS1_AD_NO_RENEGOTIATION] =
"no_renegotiation",
110#ifdef TLS13_AD_MISSING_EXTENSION
111 [TLS13_AD_MISSING_EXTENSION] =
"missing_extension",
113#ifdef TLS13_AD_CERTIFICATE_REQUIRED
114 [TLS13_AD_CERTIFICATE_REQUIRED] =
"certificate_required",
116#ifdef TLS1_AD_UNSUPPORTED_EXTENSION
117 [TLS1_AD_UNSUPPORTED_EXTENSION] =
"unsupported_extension",
119#ifdef TLS1_AD_CERTIFICATE_UNOBTAINABLE
120 [TLS1_AD_CERTIFICATE_UNOBTAINABLE] =
"certificate_unobtainable",
122#ifdef TLS1_AD_UNRECOGNIZED_NAME
123 [TLS1_AD_UNRECOGNIZED_NAME] =
"unrecognised_name",
125#ifdef TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE
126 [TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE] =
"bad_certificate_status_response",
128#ifdef TLS1_AD_BAD_CERTIFICATE_HASH_VALUE
129 [TLS1_AD_BAD_CERTIFICATE_HASH_VALUE] =
"bad_certificate_hash_value",
131#ifdef TLS1_AD_UNKNOWN_PSK_IDENTITY
132 [TLS1_AD_UNKNOWN_PSK_IDENTITY] =
"unknown_psk_identity",
134#ifdef TLS1_AD_NO_APPLICATION_PROTOCOL
135 [TLS1_AD_NO_APPLICATION_PROTOCOL] =
"no_application_protocol",
139static char const *tls_handshake_type_str[] = {
140 [SSL3_MT_HELLO_REQUEST] =
"hello_request",
141 [SSL3_MT_CLIENT_HELLO] =
"client_hello",
142 [SSL3_MT_SERVER_HELLO] =
"server_hello",
143#ifdef SSL3_MT_NEWSESSION_TICKET
144 [SSL3_MT_NEWSESSION_TICKET] =
"new_session_ticket",
146#ifdef SSL3_MT_END_OF_EARLY_DATA
147 [SSL3_MT_END_OF_EARLY_DATA] =
"end_of_early_data",
149#ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
150 [SSL3_MT_ENCRYPTED_EXTENSIONS] =
"encrypted_extensions",
152 [SSL3_MT_CERTIFICATE] =
"certificate",
153 [SSL3_MT_SERVER_KEY_EXCHANGE] =
"server_key_exchange",
154 [SSL3_MT_CERTIFICATE_REQUEST] =
"certificate_request",
155 [SSL3_MT_SERVER_DONE] =
"server_hello_done",
156 [SSL3_MT_CERTIFICATE_VERIFY] =
"certificate_verify",
157 [SSL3_MT_CLIENT_KEY_EXCHANGE] =
"client_key_exchange",
158 [SSL3_MT_FINISHED] =
"finished",
159#ifdef SSL3_MT_CERTIFICATE_URL
160 [SSL3_MT_CERTIFICATE_URL] =
"certificate_url",
162#ifdef SSL3_MT_CERTIFICATE_STATUS
163 [SSL3_MT_CERTIFICATE_STATUS] =
"certificate_status",
165#ifdef SSL3_MT_SUPPLEMENTAL_DATA
166 [SSL3_MT_SUPPLEMENTAL_DATA] =
"supplemental_data",
168#ifdef SSL3_MT_KEY_UPDATE
169 [SSL3_MT_KEY_UPDATE] =
"key_update",
171#ifdef SSL3_MT_NEXT_PROTO
172 [SSL3_MT_NEXT_PROTO] =
"next_proto",
174#ifdef SSL3_MT_MESSAGE_HASH
175 [SSL3_MT_MESSAGE_HASH] =
"message_hash",
177#ifdef DTLS1_MT_HELLO_VERIFY_REQUEST
178 [DTLS1_MT_HELLO_VERIFY_REQUEST] =
"hello_verify_request",
180#ifdef SSL3_MT_CHANGE_CIPHER_SPEC
181 [SSL3_MT_CHANGE_CIPHER_SPEC] =
"change_cipher_spec",
189inline static void record_init(fr_tls_record_t *record)
198inline static void record_close(fr_tls_record_t *record)
210inline static unsigned int record_from_buff(fr_tls_record_t *record,
void const *
in,
unsigned int inlen)
212 unsigned int added = FR_TLS_MAX_RECORD_SIZE - record->used;
215 if (added == 0)
return 0;
217 memcpy(record->data + record->used,
in, added);
218 record->used += added;
230inline static unsigned int record_to_buff(fr_tls_record_t *record,
void *
out,
unsigned int outlen)
232 unsigned int taken = record->used;
234 if (taken > outlen) taken = outlen;
235 if (taken == 0)
return 0;
236 if (
out) memcpy(
out, record->data, taken);
238 record->used -= taken;
243 if (record->used > 0) memmove(record->data, record->data + taken, record->used);
260int fr_tls_session_password_cb(
char *buf,
int size,
int rwflag
UNUSED,
void *u)
270 ERROR(
"Private key encrypted but no private_key_password configured");
274 len =
strlcpy(buf, (
char *)u, size);
275 if (len > (
size_t)size) {
276 ERROR(
"Password too long. Maximum length is %i bytes", size - 1);
283#ifdef PSK_MAX_IDENTITY_LEN
291static bool session_psk_identity_is_safe(
const char *identity)
295 if (!identity)
return true;
297 while ((c = *(identity++)) !=
'\0') {
299 (c ==
'@') || (c ==
'-') || (c ==
'_') || (c ==
'.')) {
319unsigned int fr_tls_session_psk_client_cb(SSL *ssl,
UNUSED char const *hint,
320 char *identity,
unsigned int max_identity_len,
321 unsigned char *psk,
unsigned int max_psk_len)
323 unsigned int psk_len;
326 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
329 psk_len = strlen(
conf->psk_password);
330 if (psk_len > (2 * max_psk_len))
return 0;
332 strlcpy(identity,
conf->psk_identity, max_identity_len);
349unsigned int fr_tls_session_psk_server_cb(SSL *ssl,
const char *identity,
350 unsigned char *psk,
unsigned int max_psk_len)
356 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
359 request = fr_tls_session_request(ssl);
360 if (request &&
conf->psk_query) {
363 char buffer[2 * PSK_MAX_PSK_LEN + 4];
368 if (!session_psk_identity_is_safe(identity)) {
369 RWDEBUG(
"Invalid characters in PSK identity %s", identity);
375 RPWDEBUG2(
"Failed parsing TLS PSK Identity");
382 RWDEBUG(
"PSK expansion returned an empty string.");
391 if (hex_len > (2 * max_psk_len)) {
392 RWDEBUG(
"Returned PSK is too long (%u > %u)", (
unsigned int) hex_len, 2 * max_psk_len);
406 if (!
conf->psk_identity) {
407 DEBUG(
"No static PSK identity set. Rejecting the user");
415 if (strcmp(identity,
conf->psk_identity) != 0) {
416 ERROR(
"Supplied PSK identity %s does not match configuration. Rejecting.",
421 psk_len = strlen(
conf->psk_password);
422 if (psk_len > (2 * max_psk_len))
return 0;
442void fr_tls_session_info_cb(SSL
const *ssl,
int where,
int ret)
444 char const *role, *state;
445 request_t *request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
447 if ((where & ~SSL_ST_MASK) & SSL_ST_CONNECT) {
449 }
else if (((where & ~SSL_ST_MASK)) & SSL_ST_ACCEPT) {
455 state = SSL_state_string_long(ssl);
456 state = state ? state :
"<INVALID>";
458 if ((where & SSL_CB_LOOP) || (where & SSL_CB_HANDSHAKE_START) || (where & SSL_CB_HANDSHAKE_DONE)) {
460 char const *abbrv = SSL_state_string(ssl);
467 if ((len > 1) && (abbrv[len - 1] ==
' ')) len--;
471#ifdef OPENSSL_NO_SSL_TRACE
473 STACK_OF(SSL_CIPHER) *server_ciphers;
474 STACK_OF(SSL_CIPHER) *client_ciphers;
484 if (SSL_get_state(ssl) == TLS_ST_SR_CLNT_HELLO &&
485 (client_ciphers = SSL_get_client_ciphers(ssl))) {
488 const SSL_CIPHER *this_cipher;
490 server_ciphers = SSL_get_ciphers(ssl);
498 num_ciphers = sk_SSL_CIPHER_num(server_ciphers);
499 for (i = 0; i < num_ciphers; i++) {
500 this_cipher = sk_SSL_CIPHER_value(server_ciphers, i);
513 num_ciphers = sk_SSL_CIPHER_num(client_ciphers);
514 for (i = 0; i < num_ciphers; i++) {
515 this_cipher = sk_SSL_CIPHER_value(client_ciphers, i);
529 if (where & SSL_CB_ALERT) {
530 if ((ret & 0xff) == SSL_AD_CLOSE_NOTIFY)
return;
535 if (where & SSL_CB_READ) {
539 ret & 0xff, SSL_alert_desc_string_long(ret));
544 switch (ret & 0xff) {
545 case TLS1_AD_UNKNOWN_CA:
546 REDEBUG(
"Verify the client has a copy of the server's Certificate "
547 "Authority (CA) installed, and trusts that CA");
555 vp->vp_uint8 = ret & 0xff;
562 SSL_alert_type_string_long(ret), ret & 0xff, SSL_alert_desc_string_long(ret));
567 switch (ret & 0xff) {
568 case TLS1_AD_PROTOCOL_VERSION:
570 "enabled or not supported. Upgrade FreeRADIUS + OpenSSL to their latest "
571 "versions and/or adjust 'tls_max_version'/'tls_min_version' if you want "
572 "authentication to succeed");
582 if (where & SSL_CB_EXIT) {
589 if (SSL_want_read(ssl)) {
590 RDEBUG2(
"Need more data from client");
605static void session_msg_log(
request_t *request, fr_tls_session_t *tls_session,
uint8_t const *
data,
size_t data_len)
607 char const *version, *content_type;
608 char const *str_details1 = NULL;
609 char const *str_details2 = NULL;
610 char unknown_version[32];
611 char unknown_content_type[32];
612 char unknown_alert_level[32];
613 char unknown_alert_description[32];
614 char unknown_handshake_type[32];
622 if (((
size_t)tls_session->info.version >=
NUM_ELEMENTS(tls_version_str)) ||
623 !tls_version_str[tls_session->info.version]) {
624 snprintf(unknown_version,
sizeof(unknown_version),
"unknown_tls_version_0x%04x",
625 (
unsigned int) tls_session->info.version);
626 version = unknown_version;
628 version = tls_version_str[tls_session->info.version];
634 if (((
size_t)tls_session->info.content_type >=
NUM_ELEMENTS(tls_content_type_str)) ||
635 !tls_content_type_str[tls_session->info.content_type]) {
636 snprintf(unknown_content_type,
sizeof(unknown_content_type),
637 "unknown_content_type_0x%04x", (
unsigned int) tls_session->info.content_type);
638 content_type = unknown_content_type;
640 content_type = tls_content_type_str[tls_session->info.content_type];
643 if (tls_session->info.content_type == SSL3_RT_ALERT) {
644 if (tls_session->info.record_len == 2) {
645 switch (tls_session->info.alert_level) {
646 case SSL3_AL_WARNING:
647 str_details1 =
"warning";
650 str_details1 =
"fatal";
654 snprintf(unknown_alert_level,
sizeof(unknown_alert_level),
655 "unknown_alert_level_0x%04x", tls_session->info.alert_level);
656 str_details1 = unknown_alert_level;
660 if (((
size_t)tls_session->info.alert_description >=
NUM_ELEMENTS(tls_alert_description_str)) ||
661 !tls_alert_description_str[tls_session->info.alert_description]) {
662 snprintf(unknown_alert_description,
sizeof(unknown_alert_description),
663 "unknown_alert_0x%04x", tls_session->info.alert_description);
664 str_details2 = unknown_alert_description;
666 str_details2 = tls_alert_description_str[tls_session->info.alert_description];
669 str_details1 =
"unknown_alert_level";
670 str_details2 =
"unknown_alert";
674 if ((
size_t)tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
675 if (tls_session->info.record_len > 0) {
680 if (!tls_handshake_type_str[tls_session->info.handshake_type]) {
681 snprintf(unknown_handshake_type,
sizeof(unknown_handshake_type),
682 "unknown_handshake_type_0x%02x", tls_session->info.handshake_type);
683 str_details1 = unknown_handshake_type;
685 str_details1 = tls_handshake_type_str[tls_session->info.handshake_type];
690 snprintf(tls_session->info.info_description,
sizeof(tls_session->info.info_description),
691 "%s %s, %s[length %lu]%s%s%s%s",
692 tls_session->info.origin ?
">>> send" :
"<<< recv",
695 (unsigned long)tls_session->info.record_len,
696 str_details1 ?
", " :
"",
697 str_details1 ? str_details1 :
"",
698 str_details2 ?
", " :
"",
699 str_details2 ? str_details2 :
"");
741void fr_tls_session_msg_cb(
int write_p,
int msg_version,
int content_type,
742 void const *inbuf,
size_t len,
746 fr_tls_session_t *tls_session = talloc_get_type_abort(arg, fr_tls_session_t);
747 request_t *request = fr_tls_session_request(tls_session->ssl);
754 tls_session->invalid =
true;
766 tls_session->invalid =
true;
774 tls_session->invalid =
true;
784 if ((msg_version == 0) && (content_type >
UINT8_MAX)) {
786 content_type, msg_version);
790 if ((write_p != 0) && (write_p != 1)) {
799 tls_session->info.origin = write_p;
800 tls_session->info.content_type = content_type;
801 tls_session->info.record_len = len;
802 tls_session->info.version = msg_version;
803 tls_session->info.initialized =
true;
805 switch (content_type) {
807 tls_session->info.alert_level = buf[0];
808 tls_session->info.alert_description = buf[1];
809 tls_session->info.handshake_type = 0x00;
812 case SSL3_RT_HANDSHAKE:
813 tls_session->info.handshake_type = buf[0];
814 tls_session->info.alert_level = 0x00;
815 tls_session->info.alert_description = 0x00;
818#ifdef SSL3_RT_HEARTBEAT
819 case TLS1_RT_HEARTBEAT:
822 if ((len >= 3) && (p[0] == 1)) {
826 if ((payload_len + 3) > len) {
827 tls_session->invalid =
true;
838 session_msg_log(request, tls_session, (
uint8_t const *)inbuf, len);
840#ifndef OPENSSL_NO_SSL_TRACE
842 SSL_trace(tls_session->info.origin,
843 tls_session->info.version,
844 tls_session->info.content_type,
870void fr_tls_session_keylog_cb(
const SSL *ssl,
const char *
line)
874 const char *filename;
875 char buffer[64 + 2*SSL3_RANDOM_SIZE + 2*SSL_MAX_MASTER_KEY_LENGTH];
883 filename = getenv(
"SSLKEYLOGFILE");
887 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
888 if (!
conf || !
conf->keylog_file || !*
conf->keylog_file)
return;
890 filename =
conf->keylog_file;
898 if ((len + 1) >
sizeof(
buffer)) {
899 DEBUG(
"SSLKEYLOGFILE buffer not large enough, max %lu, required %lu",
sizeof(
buffer), len + 1);
911 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
913 DEBUG(
"Failed to open file %s: %s", filename, strerror(errno));
920 if (write(fd,
buffer, len + 1) < 0) {
921 DEBUG(
"Failed to write to file %s: %s", filename, strerror(errno));
940int fr_tls_session_recv(
request_t *request, fr_tls_session_t *tls_session)
944 fr_tls_session_request_bind(tls_session->ssl, request);
946 if (!SSL_is_init_finished(tls_session->ssl)) {
947 REDEBUG(
"Attempted to read application data before handshake completed");
956 if (tls_session->dirty_in.used) {
957 ret = BIO_write(tls_session->into_ssl, tls_session->dirty_in.data, tls_session->dirty_in.used);
958 if (ret != (
int) tls_session->dirty_in.used) {
959 record_init(&tls_session->dirty_in);
960 REDEBUG(
"Failed writing %zu bytes to SSL BIO: %d", tls_session->dirty_in.used, ret);
964 record_init(&tls_session->dirty_in);
971 record_init(&tls_session->clean_out);
978 ret = SSL_read(tls_session->ssl, tls_session->clean_out.data,
sizeof(tls_session->clean_out.data));
982 code = SSL_get_error(tls_session->ssl, ret);
984 case SSL_ERROR_WANT_READ:
985 RWDEBUG(
"Peer indicated record was complete, but OpenSSL returned SSL_WANT_READ. "
986 "Attempting to continue");
990 case SSL_ERROR_WANT_WRITE:
991 REDEBUG(
"Error in fragmentation logic: SSL_WANT_WRITE");
995 RDEBUG2(
"No application data received. Assuming handshake is continuing...");
1000 REDEBUG(
"Error in fragmentation logic");
1001 fr_tls_log_io_error(request, code,
"SSL_read (%s)", __FUNCTION__);
1009 tls_session->clean_out.used = ret;
1013 RHEXDUMP3(tls_session->clean_out.data, tls_session->clean_out.used,
1014 "Decrypted TLS application data (%zu bytes)", tls_session->clean_out.used);
1016 RDEBUG2(
"Decrypted TLS application data (%zu bytes)", tls_session->clean_out.used);
1019 fr_tls_session_request_unbind(tls_session->ssl);
1037int fr_tls_session_send(
request_t *request, fr_tls_session_t *tls_session)
1041 fr_tls_session_request_bind(tls_session->ssl, request);
1043 if (!SSL_is_init_finished(tls_session->ssl)) {
1044 REDEBUG(
"Attempted to write application data before handshake completed");
1058 if (tls_session->clean_in.used > 0) {
1060 RHEXDUMP3(tls_session->clean_in.data, tls_session->clean_in.used,
1061 "TLS application data to encrypt (%zu bytes)", tls_session->clean_in.used);
1063 RDEBUG2(
"TLS application data to encrypt (%zu bytes)", tls_session->clean_in.used);
1066 ret = SSL_write(tls_session->ssl, tls_session->clean_in.data, tls_session->clean_in.used);
1067 record_to_buff(&tls_session->clean_in, NULL, ret);
1070 ret = BIO_read(tls_session->from_ssl, tls_session->dirty_out.data,
1071 sizeof(tls_session->dirty_out.data));
1073 tls_session->dirty_out.used = ret;
1076 if (fr_tls_log_io_error(request, SSL_get_error(tls_session->ssl, ret),
1077 "SSL_write (%s)", __FUNCTION__) < 0) ret = -1;
1082 fr_tls_session_request_unbind(tls_session->ssl);
1092 if (session->alerts_sent > 3)
return -1;
1097 if (session->pending_alert && (level < session->pending_alert_level))
return 0;
1099 session->pending_alert =
true;
1100 session->pending_alert_level = level;
1101 session->pending_alert_description = description;
1106static void fr_tls_session_alert_send(
request_t *request, fr_tls_session_t *session)
1111 session->info.origin = TLS_INFO_ORIGIN_RECORD_SENT;
1112 session->info.content_type = SSL3_RT_ALERT;
1113 session->info.alert_level = session->pending_alert_level;
1114 session->info.alert_description = session->pending_alert_description;
1116 session->dirty_out.data[0] = session->info.content_type;
1117 session->dirty_out.data[1] = 3;
1118 session->dirty_out.data[2] = 1;
1119 session->dirty_out.data[3] = 0;
1120 session->dirty_out.data[4] = 2;
1121 session->dirty_out.data[5] = session->pending_alert_level;
1122 session->dirty_out.data[6] = session->pending_alert_description;
1124 session->dirty_out.used = 7;
1126 session->pending_alert =
false;
1127 session->alerts_sent++;
1129 SSL_clear(session->ssl);
1131 session_msg_log(request, session, session->dirty_out.data, session->dirty_out.used);
1153static inline CC_HINT(always_inline)
1173 ua = fr_tls_call_push(child, tls_establish_session_result,
conf, tls_session);
1188 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1191 RDEBUG3(
"entered state %s", __FUNCTION__);
1202 if (SSL_is_init_finished(tls_session->ssl)) {
1203 SSL_CIPHER
const *cipher;
1205 char const *version;
1207 char cipher_desc[256], cipher_desc_clean[256];
1208 char *p = cipher_desc, *q = cipher_desc_clean;
1210 cipher = SSL_get_current_cipher(tls_session->ssl);
1211 SSL_CIPHER_description(cipher, cipher_desc,
sizeof(cipher_desc));
1217 while (*p !=
'\0') {
1227 RDEBUG2(
"Cipher suite: %s", cipher_desc_clean);
1229 RDEBUG2(
"Adding TLS session information to request");
1235 if (((
size_t)tls_session->info.version >=
NUM_ELEMENTS(tls_version_str)) ||
1236 !tls_version_str[tls_session->info.version]) {
1237 version =
"UNKNOWN";
1239 version = tls_version_str[tls_session->info.version];
1252 if (!tls_session->session) {
1253 tls_session->session = SSL_get_session(tls_session->ssl);
1254 if (!tls_session->session) {
1255 REDEBUG(
"Failed getting TLS session");
1257 tls_session->result = FR_TLS_RESULT_ERROR;
1258 fr_tls_session_request_unbind(tls_session->ssl);
1265 tls_session->session) != 1) {
1267 RDEBUG3(
"Failed retrieving session data");
1274 if (SSL_session_reused(tls_session->ssl)) {
1286 ret = BIO_ctrl_pending(tls_session->from_ssl);
1288 ret = BIO_read(tls_session->from_ssl, tls_session->dirty_out.data,
1289 sizeof(tls_session->dirty_out.data));
1291 tls_session->dirty_out.used = ret;
1292 }
else if (BIO_should_retry(tls_session->from_ssl)) {
1293 record_init(&tls_session->dirty_in);
1294 RDEBUG2(
"Asking for more data in tunnel");
1297 fr_tls_log(NULL, NULL);
1298 record_init(&tls_session->dirty_in);
1303 record_init(&tls_session->clean_out);
1314 if (tls_session->pending_alert) fr_tls_session_alert_send(request, tls_session);
1317 record_init(&tls_session->dirty_in);
1319 tls_session->result = FR_TLS_RESULT_SUCCESS;
1320 fr_tls_session_request_unbind(tls_session->ssl);
1321 if (SSL_is_init_finished(tls_session->ssl)) {
1322 fr_tls_conf_t *
conf = fr_tls_session_conf(tls_session->ssl);
1323 if (
conf->establish_session)
return tls_establish_session_push(request,
conf, tls_session);
1338 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1359 for (ret = tls_session->last_ret;
1360 SSL_get_error(tls_session->ssl, ret) == SSL_ERROR_WANT_ASYNC;
1361 ret = SSL_read(tls_session->ssl, tls_session->clean_out.data + tls_session->clean_out.used,
1362 sizeof(tls_session->clean_out.data) - tls_session->clean_out.used));
1367 fr_tls_session_request_unbind(tls_session->ssl);
1385 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1388 RDEBUG3(
"(re-)entered state %s", __FUNCTION__);
1410 tls_session->can_pause =
true;
1411 tls_session->last_ret = SSL_read(tls_session->ssl, tls_session->clean_out.data + tls_session->clean_out.used,
1412 sizeof(tls_session->clean_out.data) - tls_session->clean_out.used);
1413 tls_session->can_pause =
false;
1414 if (tls_session->last_ret > 0) {
1415 tls_session->clean_out.used += tls_session->last_ret;
1421 tls_session->result = FR_TLS_RESULT_SUCCESS;
1426 fr_tls_session_request_unbind(tls_session->ssl);
1449 if (SSL_get_error(tls_session->ssl, tls_session->last_ret) == SSL_ERROR_SSL) {
1450 unsigned long ssl_err;
1459 while ((ssl_err = ERR_peek_error()) && (ERR_GET_REASON(ssl_err) == BIO_R_UNSUPPORTED_METHOD)) {
1460 (void) ERR_get_error();
1473 switch (
err = SSL_get_error(tls_session->ssl, tls_session->last_ret)) {
1474 case SSL_ERROR_WANT_ASYNC:
1478 RDEBUG3(
"Performing async action for libssl");
1486 tls_session->result = FR_TLS_RESULT_ERROR;
1493 ua = fr_tls_cache_pending_push(request, tls_session);
1510 ua = fr_tls_verify_cert_pending_push(request, tls_session);
1521 case SSL_ERROR_WANT_ASYNC_JOB:
1522 RERROR(
"No async jobs available in pool, increase thread.openssl_async_pool_max");
1530 if (fr_tls_log_io_error(request,
1531 err,
"SSL_read (%s)", __FUNCTION__) < 0)
goto error;
1532 return tls_session_async_handshake_done_round(p_result, priority, request, uctx);
1556 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1559 RDEBUG3(
"entered state %s", __FUNCTION__);
1561 tls_session->result = FR_TLS_RESULT_IN_PROGRESS;
1563 fr_tls_session_request_bind(tls_session->ssl, request);
1571 if (SSL_is_init_finished(tls_session->ssl)) {
1572 REDEBUG(
"Attempted to continue TLS handshake, but handshake has completed");
1574 tls_session->result = FR_TLS_RESULT_ERROR;
1575 fr_tls_session_request_unbind(tls_session->ssl);
1579 if (tls_session->invalid) {
1580 REDEBUG(
"Preventing invalid session from continuing");
1589 if (tls_session->dirty_in.used) {
1590 ret = BIO_write(tls_session->into_ssl, tls_session->dirty_in.data, tls_session->dirty_in.used);
1591 if (ret != (
int)tls_session->dirty_in.used) {
1592 REDEBUG(
"Failed writing %zu bytes to TLS BIO: %d", tls_session->dirty_in.used, ret);
1593 record_init(&tls_session->dirty_in);
1596 record_init(&tls_session->dirty_in);
1599 return tls_session_async_handshake_cont(p_result, priority, request, uctx);
1620 tls_session_async_handshake,
1622 tls_session_async_handshake_signal,
1633static int _fr_tls_session_free(fr_tls_session_t *session)
1636 SSL_set_quiet_shutdown(session->ssl, 1);
1637 SSL_shutdown(session->ssl);
1638 SSL_free(session->ssl);
1639 session->ssl = NULL;
1645static void session_init(fr_tls_session_t *session)
1647 session->ssl = NULL;
1648 session->into_ssl = session->from_ssl = NULL;
1649 record_init(&session->clean_in);
1650 record_init(&session->clean_out);
1651 record_init(&session->dirty_in);
1652 record_init(&session->dirty_out);
1654 memset(&session->info, 0,
sizeof(session->info));
1657 session->opaque = NULL;
1671fr_tls_session_t *fr_tls_session_alloc_client(TALLOC_CTX *ctx, SSL_CTX *ssl_ctx)
1674 fr_tls_session_t *tls_session = NULL;
1675 fr_tls_conf_t *
conf = fr_tls_ctx_conf(ssl_ctx);
1677 MEM(tls_session = talloc_zero(ctx, fr_tls_session_t));
1678 talloc_set_destructor(tls_session, _fr_tls_session_free);
1681 tls_session->ssl = SSL_new(ssl_ctx);
1682 if (!tls_session->ssl) {
1691 SSL_set_msg_callback(tls_session->ssl, fr_tls_session_msg_cb);
1692 SSL_set_msg_callback_arg(tls_session->ssl, tls_session);
1693 SSL_set_info_callback(tls_session->ssl, fr_tls_session_info_cb);
1701 SSL_set_connect_state(tls_session->ssl);
1706 DEBUG2(
"Requiring Server certificate");
1707 verify_mode = SSL_VERIFY_PEER;
1708 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1719 SSL_set_verify(tls_session->ssl, verify_mode, NULL);
1720 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (
void *)
conf);
1721 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_TLS_SESSION, (
void *)tls_session);
1723 tls_session->mtu =
conf->fragment_size;
1743fr_tls_session_t *fr_tls_session_alloc_server(TALLOC_CTX *ctx, SSL_CTX *ssl_ctx,
request_t *request,
size_t dynamic_mtu,
bool client_cert)
1745 fr_tls_session_t *tls_session = NULL;
1747 int verify_mode = 0;
1749 fr_tls_conf_t *
conf = fr_tls_ctx_conf(ssl_ctx);
1751 RDEBUG2(
"Initiating new TLS session");
1753 MEM(tls_session = talloc_zero(ctx, fr_tls_session_t));
1755 ssl = SSL_new(ssl_ctx);
1757 fr_tls_log(request,
"Error creating new TLS session");
1762 session_init(tls_session);
1763 tls_session->ctx = ssl_ctx;
1764 tls_session->ssl = ssl;
1765 talloc_set_destructor(tls_session, _fr_tls_session_free);
1767 fr_tls_session_request_bind(tls_session->ssl, request);
1772 tls_session->record_init = record_init;
1773 tls_session->record_close = record_close;
1774 tls_session->record_from_buff = record_from_buff;
1775 tls_session->record_to_buff = record_to_buff;
1787 MEM(tls_session->into_ssl = BIO_new(BIO_s_mem()));
1788 MEM(tls_session->from_ssl = BIO_new(BIO_s_mem()));
1789 SSL_set_bio(tls_session->ssl, tls_session->into_ssl, tls_session->from_ssl);
1795 SSL_set_msg_callback(ssl, fr_tls_session_msg_cb);
1796 SSL_set_msg_callback_arg(ssl, tls_session);
1797 SSL_set_info_callback(ssl, fr_tls_session_info_cb);
1815 if (
conf->cache.mode != FR_TLS_CACHE_DISABLED) {
1818 uint8_t digest[SHA256_DIGEST_LENGTH];
1820 static_assert(
sizeof(digest) <= SSL_MAX_SSL_SESSION_ID_LENGTH,
1821 "SSL_MAX_SSL_SESSION_ID_LENGTH must be >= SHA256_DIGEST_LENGTH");
1824 if (
tmpl_aexpand(tls_session, &context_id, request,
conf->cache.id_name, NULL, NULL) < 0) {
1825 RPEDEBUG(
"Failed expanding session ID");
1827 fr_tls_session_request_unbind(tls_session->ssl);
1832 MEM(md_ctx = EVP_MD_CTX_create());
1833 EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL);
1834 EVP_DigestUpdate(md_ctx, context_id, talloc_array_length(context_id) - 1);
1835 EVP_DigestFinal_ex(md_ctx, digest, NULL);
1836 EVP_MD_CTX_destroy(md_ctx);
1840 digest,
sizeof(digest)) == 1))
goto error;
1848 RDEBUG2(
"Loading TLS session certificate \"%pV\"", &
vp->data);
1850 if (SSL_use_certificate_file(tls_session->ssl,
vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
1851 fr_tls_log(request,
"Failed loading TLS session certificate \"%s\"",
1856 if (SSL_use_PrivateKey_file(tls_session->ssl,
vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
1857 fr_tls_log(request,
"Failed loading TLS session certificate \"%s\"",
1862 if (SSL_check_private_key(tls_session->ssl) != 1) {
1863 fr_tls_log(request,
"Failed validating TLS session certificate \"%s\"",
1872 if (!
conf->chains || !
conf->chains[0]->private_key_file) {
1873 ERROR(
"TLS Server requires a private key file");
1877 if (!
conf->chains || !
conf->chains[0]->certificate_file) {
1878 ERROR(
"TLS Server requires a certificate file");
1889 if (
vp) client_cert =
vp->vp_bool;
1897 SSL_set_accept_state(tls_session->ssl);
1903 RDEBUG2(
"Setting verify mode to require certificate from client");
1904 verify_mode = SSL_VERIFY_PEER;
1905 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1906 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
1908 tls_session->verify_client_cert = client_cert;
1910 SSL_set_verify(tls_session->ssl, verify_mode, fr_tls_verify_cert_cb);
1911 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (
void *)
conf);
1912 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_TLS_SESSION, (
void *)tls_session);
1914 tls_session->mtu =
conf->fragment_size;
1915 if (dynamic_mtu > 100 && dynamic_mtu < tls_session->mtu) {
1916 RDEBUG2(
"Setting fragment_len to %zu from dynamic_mtu", dynamic_mtu);
1917 tls_session->mtu = dynamic_mtu;
1920 if (
conf->cache.mode != FR_TLS_CACHE_DISABLED) {
1921 tls_session->allow_session_resumption =
true;
1922 fr_tls_cache_session_alloc(tls_session);
1925 fr_tls_session_request_unbind(tls_session->ssl);
1957 .unique_ptr = child->parent
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
static int const char char buffer[256]
#define fr_base16_decode(_err, _out, _in, _no_trailing)
#define IGNORE(_expr, _type)
#define DIAG_UNKNOWN_PRAGMAS
unlang_action_t unlang_call_push(request_t *request, CONF_SECTION *server_cs, bool top_frame)
Push a call frame onto the stack.
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
int unlang_function_clear(request_t *request)
Clear pending repeat function calls, and remove the signal handler.
#define unlang_function_repeat_set(_request, _repeat)
Set a new repeat function for an existing function frame.
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
fr_dict_t const * dict_tls
#define REXDENT()
Exdent (unindent) R* messages by one level.
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
#define DEBUG_ENABLED4
True if global debug level 1-3 messages are enabled.
#define RHEXDUMP4(_data, _len, _fmt,...)
#define ROPTIONAL_ENABLED(_e_request, _e_global)
Check if a debug level is set by the request (if !NULL) or by the global log.
#define RPEDEBUG(fmt,...)
#define RHEXDUMP3(_data, _len, _fmt,...)
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
#define RDEBUG_ENABLED4
True if request debug level 1-4 messages are enabled.
#define HEXDUMP4(_data, _len, _fmt,...)
#define RPWDEBUG2(fmt,...)
#define REDEBUG4(fmt,...)
#define RINDENT()
Indent R* messages by one level.
HIDDEN fr_dict_attr_t const * attr_tls_packet_type
HIDDEN fr_dict_attr_t const * attr_session_resumed
HIDDEN fr_dict_attr_t const * attr_tls_session_version
HIDDEN fr_dict_attr_t const * attr_tls_session_require_client_cert
fr_value_box_t const * enum_tls_packet_type_new_session
HIDDEN fr_dict_attr_t const * attr_tls_client_error_code
fr_value_box_t const * enum_tls_packet_type_establish_session
HIDDEN fr_dict_attr_t const * attr_tls_session_cipher_suite
HIDDEN fr_dict_attr_t const * attr_tls_psk_identity
HIDDEN fr_dict_attr_t const * attr_tls_session_cert_file
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
@ L_DBG
Only displayed when debugging is enabled.
ssize_t xlat_eval(char *out, size_t outlen, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx)
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, bool tainted)
Convert string value to native attribute value.
#define pair_update_request(_attr, _da)
#define RDEBUG_ENABLED2()
rlm_rcode_t
Return codes indicating the result of the module call.
#define FR_SBUFF_IN(_start, _len_or_end)
#define pair_prepend_request(_attr, _da)
Allocate and prepend a fr_pair_t to the request list.
#define pair_update_session_state(_attr, _da)
Return or allocate a fr_pair_t in the session_state list.
#define tmpl_aexpand(_ctx, _out, _request, _vpt, _escape, _escape_ctx)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
fr_signal_t
Signals that can be generated/processed by request signal handlers.
@ FR_SIGNAL_CANCEL
Request has been cancelled.
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
size_t strlcpy(char *dst, char const *src, size_t siz)
Stores an attribute, a value and various bits of other data.
request_t * unlang_subrequest_alloc(request_t *parent, fr_dict_t const *namespace)
Allocate a subrequest to run through a virtual server at some point in the future.
int unlang_subrequest_child_push(rlm_rcode_t *out, request_t *child, unlang_subrequest_session_t const *session, bool free_child, bool top_frame)
Push a pre-existing child back onto the stack as a subrequest.
static size_t char fr_sbuff_t size_t inlen
static size_t char ** out