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/interpret.h>
50 #include <openssl/x509v3.h>
51 #include <openssl/ssl.h>
53 static char const *tls_version_str[] = {
54 [SSL2_VERSION] =
"SSL 2.0",
55 [SSL3_VERSION] =
"SSL 3.0",
56 [TLS1_VERSION] =
"TLS 1.0",
58 [TLS1_1_VERSION] =
"TLS 1.1",
61 [TLS1_2_VERSION] =
"TLS 1.2",
64 [TLS1_3_VERSION] =
"TLS 1.3",
67 [TLS1_4_VERSION] =
"TLS 1.4",
71 static char const *tls_content_type_str[] = {
72 [SSL3_RT_CHANGE_CIPHER_SPEC] =
"change_cipher_spec",
73 [SSL3_RT_ALERT] =
"alert",
74 [SSL3_RT_HANDSHAKE] =
"handshake",
75 [SSL3_RT_APPLICATION_DATA] =
"application_data",
77 [SSL3_RT_HEADER] =
"header",
79 #ifdef SSL3_RT_INNER_CONTENT_TYPE
80 [SSL3_RT_INNER_CONTENT_TYPE] =
"inner_content_type",
84 static char const *tls_alert_description_str[] = {
85 [SSL3_AD_CLOSE_NOTIFY] =
"close_notify",
86 [SSL3_AD_UNEXPECTED_MESSAGE] =
"unexpected_message",
87 [SSL3_AD_BAD_RECORD_MAC] =
"bad_record_mac",
88 [TLS1_AD_DECRYPTION_FAILED] =
"decryption_failed",
89 [TLS1_AD_RECORD_OVERFLOW] =
"record_overflow",
90 [SSL3_AD_DECOMPRESSION_FAILURE] =
"decompression_failure",
91 [SSL3_AD_HANDSHAKE_FAILURE] =
"handshake_failure",
92 [SSL3_AD_BAD_CERTIFICATE] =
"bad_certificate",
93 [SSL3_AD_UNSUPPORTED_CERTIFICATE] =
"unsupported_certificate",
94 [SSL3_AD_CERTIFICATE_REVOKED] =
"certificate_revoked",
95 [SSL3_AD_CERTIFICATE_EXPIRED] =
"certificate_expired",
96 [SSL3_AD_CERTIFICATE_UNKNOWN] =
"certificate_unknown",
97 [SSL3_AD_ILLEGAL_PARAMETER] =
"illegal_parameter",
98 [TLS1_AD_UNKNOWN_CA] =
"unknown_ca",
99 [TLS1_AD_ACCESS_DENIED] =
"access_denied",
100 [TLS1_AD_DECODE_ERROR] =
"decode_error",
101 [TLS1_AD_DECRYPT_ERROR] =
"decrypt_error",
102 [TLS1_AD_EXPORT_RESTRICTION] =
"export_restriction",
103 [TLS1_AD_PROTOCOL_VERSION] =
"protocol_version",
104 [TLS1_AD_INSUFFICIENT_SECURITY] =
"insufficient_security",
105 [TLS1_AD_INTERNAL_ERROR] =
"internal_error",
106 [TLS1_AD_USER_CANCELLED] =
"user_cancelled",
107 [TLS1_AD_NO_RENEGOTIATION] =
"no_renegotiation",
108 #ifdef TLS13_AD_MISSING_EXTENSION
109 [TLS13_AD_MISSING_EXTENSION] =
"missing_extension",
111 #ifdef TLS13_AD_CERTIFICATE_REQUIRED
112 [TLS13_AD_CERTIFICATE_REQUIRED] =
"certificate_required",
114 #ifdef TLS1_AD_UNSUPPORTED_EXTENSION
115 [TLS1_AD_UNSUPPORTED_EXTENSION] =
"unsupported_extension",
117 #ifdef TLS1_AD_CERTIFICATE_UNOBTAINABLE
118 [TLS1_AD_CERTIFICATE_UNOBTAINABLE] =
"certificate_unobtainable",
120 #ifdef TLS1_AD_UNRECOGNIZED_NAME
121 [TLS1_AD_UNRECOGNIZED_NAME] =
"unrecognised_name",
123 #ifdef TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE
124 [TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE] =
"bad_certificate_status_response",
126 #ifdef TLS1_AD_BAD_CERTIFICATE_HASH_VALUE
127 [TLS1_AD_BAD_CERTIFICATE_HASH_VALUE] =
"bad_certificate_hash_value",
129 #ifdef TLS1_AD_UNKNOWN_PSK_IDENTITY
130 [TLS1_AD_UNKNOWN_PSK_IDENTITY] =
"unknown_psk_identity",
132 #ifdef TLS1_AD_NO_APPLICATION_PROTOCOL
133 [TLS1_AD_NO_APPLICATION_PROTOCOL] =
"no_application_protocol",
137 static char const *tls_handshake_type_str[] = {
138 [SSL3_MT_HELLO_REQUEST] =
"hello_request",
139 [SSL3_MT_CLIENT_HELLO] =
"client_hello",
140 [SSL3_MT_SERVER_HELLO] =
"server_hello",
141 #ifdef SSL3_MT_NEWSESSION_TICKET
142 [SSL3_MT_NEWSESSION_TICKET] =
"new_session_ticket",
144 #ifdef SSL3_MT_END_OF_EARLY_DATA
145 [SSL3_MT_END_OF_EARLY_DATA] =
"end_of_early_data",
147 #ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
148 [SSL3_MT_ENCRYPTED_EXTENSIONS] =
"encrypted_extensions",
150 [SSL3_MT_CERTIFICATE] =
"certificate",
151 [SSL3_MT_SERVER_KEY_EXCHANGE] =
"server_key_exchange",
152 [SSL3_MT_CERTIFICATE_REQUEST] =
"certificate_request",
153 [SSL3_MT_SERVER_DONE] =
"server_hello_done",
154 [SSL3_MT_CERTIFICATE_VERIFY] =
"certificate_verify",
155 [SSL3_MT_CLIENT_KEY_EXCHANGE] =
"client_key_exchange",
156 [SSL3_MT_FINISHED] =
"finished",
157 #ifdef SSL3_MT_CERTIFICATE_URL
158 [SSL3_MT_CERTIFICATE_URL] =
"certificate_url",
160 #ifdef SSL3_MT_CERTIFICATE_STATUS
161 [SSL3_MT_CERTIFICATE_STATUS] =
"certificate_status",
163 #ifdef SSL3_MT_SUPPLEMENTAL_DATA
164 [SSL3_MT_SUPPLEMENTAL_DATA] =
"supplemental_data",
166 #ifdef SSL3_MT_KEY_UPDATE
167 [SSL3_MT_KEY_UPDATE] =
"key_update",
169 #ifdef SSL3_MT_NEXT_PROTO
170 [SSL3_MT_NEXT_PROTO] =
"next_proto",
172 #ifdef SSL3_MT_MESSAGE_HASH
173 [SSL3_MT_MESSAGE_HASH] =
"message_hash",
175 #ifdef DTLS1_MT_HELLO_VERIFY_REQUEST
176 [DTLS1_MT_HELLO_VERIFY_REQUEST] =
"hello_verify_request",
178 #ifdef SSL3_MT_CHANGE_CIPHER_SPEC
179 [SSL3_MT_CHANGE_CIPHER_SPEC] =
"change_cipher_spec",
187 inline static void record_init(fr_tls_record_t *record)
196 inline static void record_close(fr_tls_record_t *record)
208 inline static unsigned int record_from_buff(fr_tls_record_t *record,
void const *
in,
unsigned int inlen)
210 unsigned int added = FR_TLS_MAX_RECORD_SIZE - record->used;
213 if (added == 0)
return 0;
215 memcpy(record->data + record->used,
in, added);
216 record->used += added;
228 inline static unsigned int record_to_buff(fr_tls_record_t *record,
void *
out,
unsigned int outlen)
230 unsigned int taken = record->used;
232 if (taken > outlen) taken = outlen;
233 if (taken == 0)
return 0;
234 if (
out) memcpy(
out, record->data, taken);
236 record->used -= taken;
241 if (record->used > 0) memmove(record->data, record->data + taken, record->used);
258 int fr_tls_session_password_cb(
char *buf,
int size,
int rwflag
UNUSED,
void *u)
268 ERROR(
"Private key encrypted but no private_key_password configured");
272 len =
strlcpy(buf, (
char *)u, size);
273 if (len > (
size_t)size) {
274 ERROR(
"Password too long. Maximum length is %i bytes", size - 1);
281 #ifdef PSK_MAX_IDENTITY_LEN
289 static bool session_psk_identity_is_safe(
const char *identity)
293 if (!identity)
return true;
295 while ((c = *(identity++)) !=
'\0') {
297 (c ==
'@') || (c ==
'-') || (c ==
'_') || (c ==
'.')) {
317 unsigned int fr_tls_session_psk_client_cb(SSL *ssl,
UNUSED char const *hint,
318 char *identity,
unsigned int max_identity_len,
319 unsigned char *psk,
unsigned int max_psk_len)
321 unsigned int psk_len;
324 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
327 psk_len = strlen(
conf->psk_password);
328 if (psk_len > (2 * max_psk_len))
return 0;
330 strlcpy(identity,
conf->psk_identity, max_identity_len);
347 unsigned int fr_tls_session_psk_server_cb(SSL *ssl,
const char *identity,
348 unsigned char *psk,
unsigned int max_psk_len)
354 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
357 request = fr_tls_session_request(ssl);
358 if (request &&
conf->psk_query) {
361 char buffer[2 * PSK_MAX_PSK_LEN + 4];
366 if (!session_psk_identity_is_safe(identity)) {
367 RWDEBUG(
"Invalid characters in PSK identity %s", identity);
373 RPWDEBUG2(
"Failed parsing TLS PSK Identity");
380 RWDEBUG(
"PSK expansion returned an empty string.");
389 if (hex_len > (2 * max_psk_len)) {
390 RWDEBUG(
"Returned PSK is too long (%u > %u)", (
unsigned int) hex_len, 2 * max_psk_len);
404 if (!
conf->psk_identity) {
405 DEBUG(
"No static PSK identity set. Rejecting the user");
413 if (strcmp(identity,
conf->psk_identity) != 0) {
414 ERROR(
"Supplied PSK identity %s does not match configuration. Rejecting.",
419 psk_len = strlen(
conf->psk_password);
420 if (psk_len > (2 * max_psk_len))
return 0;
440 void fr_tls_session_info_cb(SSL
const *ssl,
int where,
int ret)
442 char const *role, *state;
443 request_t *request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
445 if ((where & ~SSL_ST_MASK) & SSL_ST_CONNECT) {
447 }
else if (((where & ~SSL_ST_MASK)) & SSL_ST_ACCEPT) {
453 state = SSL_state_string_long(ssl);
454 state = state ? state :
"<INVALID>";
456 if ((where & SSL_CB_LOOP) || (where & SSL_CB_HANDSHAKE_START) || (where & SSL_CB_HANDSHAKE_DONE)) {
458 char const *abbrv = SSL_state_string(ssl);
465 if ((len > 1) && (abbrv[len - 1] ==
' ')) len--;
469 #ifdef OPENSSL_NO_SSL_TRACE
471 STACK_OF(SSL_CIPHER) *server_ciphers;
472 STACK_OF(SSL_CIPHER) *client_ciphers;
482 if (SSL_get_state(ssl) == TLS_ST_SR_CLNT_HELLO &&
483 (client_ciphers = SSL_get_client_ciphers(ssl))) {
486 const SSL_CIPHER *this_cipher;
488 server_ciphers = SSL_get_ciphers(ssl);
496 num_ciphers = sk_SSL_CIPHER_num(server_ciphers);
497 for (i = 0; i < num_ciphers; i++) {
498 this_cipher = sk_SSL_CIPHER_value(server_ciphers, i);
511 num_ciphers = sk_SSL_CIPHER_num(client_ciphers);
512 for (i = 0; i < num_ciphers; i++) {
513 this_cipher = sk_SSL_CIPHER_value(client_ciphers, i);
527 if (where & SSL_CB_ALERT) {
528 if ((ret & 0xff) == SSL_AD_CLOSE_NOTIFY)
return;
533 if (where & SSL_CB_READ) {
537 ret & 0xff, SSL_alert_desc_string_long(ret));
542 switch (ret & 0xff) {
543 case TLS1_AD_UNKNOWN_CA:
544 REDEBUG(
"Verify the client has a copy of the server's Certificate "
545 "Authority (CA) installed, and trusts that CA");
553 vp->vp_uint8 = ret & 0xff;
560 SSL_alert_type_string_long(ret), ret & 0xff, SSL_alert_desc_string_long(ret));
565 switch (ret & 0xff) {
566 case TLS1_AD_PROTOCOL_VERSION:
568 "enabled or not supported. Upgrade FreeRADIUS + OpenSSL to their latest "
569 "versions and/or adjust 'tls_max_version'/'tls_min_version' if you want "
570 "authentication to succeed");
580 if (where & SSL_CB_EXIT) {
587 if (SSL_want_read(ssl)) {
588 RDEBUG2(
"Need more data from client");
603 static void session_msg_log(
request_t *request, fr_tls_session_t *tls_session,
uint8_t const *
data,
size_t data_len)
605 char const *version, *content_type;
606 char const *str_details1 = NULL;
607 char const *str_details2 = NULL;
608 char unknown_version[32];
609 char unknown_content_type[32];
610 char unknown_alert_level[32];
611 char unknown_alert_description[32];
612 char unknown_handshake_type[32];
620 if (((
size_t)tls_session->info.version >=
NUM_ELEMENTS(tls_version_str)) ||
621 !tls_version_str[tls_session->info.version]) {
622 snprintf(unknown_version,
sizeof(unknown_version),
"unknown_tls_version_0x%04x", tls_session->info.version);
623 version = unknown_version;
625 version = tls_version_str[tls_session->info.version];
631 if (((
size_t)tls_session->info.content_type >=
NUM_ELEMENTS(tls_content_type_str)) ||
632 !tls_content_type_str[tls_session->info.content_type]) {
633 snprintf(unknown_content_type,
sizeof(unknown_content_type),
634 "unknown_content_type_0x%04x", tls_session->info.content_type);
635 content_type = unknown_content_type;
637 content_type = tls_content_type_str[tls_session->info.content_type];
640 if (tls_session->info.content_type == SSL3_RT_ALERT) {
641 if (tls_session->info.record_len == 2) {
642 switch (tls_session->info.alert_level) {
643 case SSL3_AL_WARNING:
644 str_details1 =
"warning";
647 str_details1 =
"fatal";
651 snprintf(unknown_alert_level,
sizeof(unknown_alert_level),
652 "unknown_alert_level_0x%04x", tls_session->info.alert_level);
653 str_details1 = unknown_alert_level;
657 if (((
size_t)tls_session->info.alert_description >=
NUM_ELEMENTS(tls_alert_description_str)) ||
658 !tls_alert_description_str[tls_session->info.alert_description]) {
659 snprintf(unknown_alert_description,
sizeof(unknown_alert_description),
660 "unknown_alert_0x%04x", tls_session->info.alert_description);
661 str_details2 = unknown_alert_description;
663 str_details2 = tls_alert_description_str[tls_session->info.alert_description];
666 str_details1 =
"unknown_alert_level";
667 str_details2 =
"unknown_alert";
671 if ((
size_t)tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
672 if (tls_session->info.record_len > 0) {
677 if (!tls_handshake_type_str[tls_session->info.handshake_type]) {
678 snprintf(unknown_handshake_type,
sizeof(unknown_handshake_type),
679 "unknown_handshake_type_0x%02x", tls_session->info.handshake_type);
680 str_details1 = unknown_handshake_type;
682 str_details1 = tls_handshake_type_str[tls_session->info.handshake_type];
687 snprintf(tls_session->info.info_description,
sizeof(tls_session->info.info_description),
688 "%s %s, %s[length %lu]%s%s%s%s",
689 tls_session->info.origin ?
">>> send" :
"<<< recv",
692 (
unsigned long)tls_session->info.record_len,
693 str_details1 ?
", " :
"",
694 str_details1 ? str_details1 :
"",
695 str_details2 ?
", " :
"",
696 str_details2 ? str_details2 :
"");
738 void fr_tls_session_msg_cb(
int write_p,
int msg_version,
int content_type,
739 void const *inbuf,
size_t len,
743 fr_tls_session_t *tls_session = talloc_get_type_abort(arg, fr_tls_session_t);
744 request_t *request = fr_tls_session_request(tls_session->ssl);
751 tls_session->invalid =
true;
763 tls_session->invalid =
true;
771 tls_session->invalid =
true;
781 if ((msg_version == 0) && (content_type >
UINT8_MAX)) {
783 content_type, msg_version);
787 if ((write_p != 0) && (write_p != 1)) {
796 tls_session->info.origin = write_p;
797 tls_session->info.content_type = content_type;
798 tls_session->info.record_len = len;
799 tls_session->info.version = msg_version;
800 tls_session->info.initialized =
true;
802 switch (content_type) {
804 tls_session->info.alert_level = buf[0];
805 tls_session->info.alert_description = buf[1];
806 tls_session->info.handshake_type = 0x00;
809 case SSL3_RT_HANDSHAKE:
810 tls_session->info.handshake_type = buf[0];
811 tls_session->info.alert_level = 0x00;
812 tls_session->info.alert_description = 0x00;
815 #ifdef SSL3_RT_HEARTBEAT
816 case TLS1_RT_HEARTBEAT:
819 if ((len >= 3) && (p[0] == 1)) {
823 if ((payload_len + 3) > len) {
824 tls_session->invalid =
true;
835 session_msg_log(request, tls_session, (
uint8_t const *)inbuf, len);
837 #ifndef OPENSSL_NO_SSL_TRACE
839 SSL_trace(tls_session->info.origin,
840 tls_session->info.version,
841 tls_session->info.content_type,
867 void fr_tls_session_keylog_cb(
const SSL *ssl,
const char *
line)
871 const char *filename;
872 char buffer[64 + 2*SSL3_RANDOM_SIZE + 2*SSL_MAX_MASTER_KEY_LENGTH];
880 filename = getenv(
"SSLKEYLOGFILE");
884 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
885 if (!
conf || !
conf->keylog_file || !*
conf->keylog_file)
return;
887 filename =
conf->keylog_file;
895 if ((len + 1) >
sizeof(
buffer)) {
896 DEBUG(
"SSLKEYLOGFILE buffer not large enough, max %lu, required %lu",
sizeof(
buffer), len + 1);
908 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
910 DEBUG(
"Failed to open file %s: %s", filename, strerror(errno));
917 if (write(fd,
buffer, len + 1) < 0) {
918 DEBUG(
"Failed to write to file %s: %s", filename, strerror(errno));
937 int fr_tls_session_recv(
request_t *request, fr_tls_session_t *tls_session)
941 fr_tls_session_request_bind(tls_session->ssl, request);
943 if (!SSL_is_init_finished(tls_session->ssl)) {
944 REDEBUG(
"Attempted to read application data before handshake completed");
953 if (tls_session->dirty_in.used) {
954 ret = BIO_write(tls_session->into_ssl, tls_session->dirty_in.data, tls_session->dirty_in.used);
955 if (ret != (
int) tls_session->dirty_in.used) {
956 record_init(&tls_session->dirty_in);
957 REDEBUG(
"Failed writing %zd bytes to SSL BIO: %d", tls_session->dirty_in.used, ret);
961 record_init(&tls_session->dirty_in);
968 record_init(&tls_session->clean_out);
975 ret = SSL_read(tls_session->ssl, tls_session->clean_out.data,
sizeof(tls_session->clean_out.data));
979 code = SSL_get_error(tls_session->ssl, ret);
981 case SSL_ERROR_WANT_READ:
982 RWDEBUG(
"Peer indicated record was complete, but OpenSSL returned SSL_WANT_READ. "
983 "Attempting to continue");
987 case SSL_ERROR_WANT_WRITE:
988 REDEBUG(
"Error in fragmentation logic: SSL_WANT_WRITE");
992 RDEBUG2(
"No application data received. Assuming handshake is continuing...");
997 REDEBUG(
"Error in fragmentation logic");
998 fr_tls_log_io_error(request, code,
"SSL_read (%s)", __FUNCTION__);
1006 tls_session->clean_out.used = ret;
1010 RHEXDUMP3(tls_session->clean_out.data, tls_session->clean_out.used,
1011 "Decrypted TLS application data (%zu bytes)", tls_session->clean_out.used);
1013 RDEBUG2(
"Decrypted TLS application data (%zu bytes)", tls_session->clean_out.used);
1016 fr_tls_session_request_unbind(tls_session->ssl);
1034 int fr_tls_session_send(
request_t *request, fr_tls_session_t *tls_session)
1038 fr_tls_session_request_bind(tls_session->ssl, request);
1040 if (!SSL_is_init_finished(tls_session->ssl)) {
1041 REDEBUG(
"Attempted to write application data before handshake completed");
1055 if (tls_session->clean_in.used > 0) {
1057 RHEXDUMP3(tls_session->clean_in.data, tls_session->clean_in.used,
1058 "TLS application data to encrypt (%zu bytes)", tls_session->clean_in.used);
1060 RDEBUG2(
"TLS application data to encrypt (%zu bytes)", tls_session->clean_in.used);
1063 ret = SSL_write(tls_session->ssl, tls_session->clean_in.data, tls_session->clean_in.used);
1064 record_to_buff(&tls_session->clean_in, NULL, ret);
1067 ret = BIO_read(tls_session->from_ssl, tls_session->dirty_out.data,
1068 sizeof(tls_session->dirty_out.data));
1070 tls_session->dirty_out.used = ret;
1073 if (fr_tls_log_io_error(request, SSL_get_error(tls_session->ssl, ret),
1074 "SSL_write (%s)", __FUNCTION__) < 0) ret = -1;
1079 fr_tls_session_request_unbind(tls_session->ssl);
1089 if (session->alerts_sent > 3)
return -1;
1094 if (session->pending_alert && (level < session->pending_alert_level))
return 0;
1096 session->pending_alert =
true;
1097 session->pending_alert_level = level;
1098 session->pending_alert_description = description;
1103 static void fr_tls_session_alert_send(
request_t *request, fr_tls_session_t *session)
1108 session->info.origin = TLS_INFO_ORIGIN_RECORD_SENT;
1109 session->info.content_type = SSL3_RT_ALERT;
1110 session->info.alert_level = session->pending_alert_level;
1111 session->info.alert_description = session->pending_alert_description;
1113 session->dirty_out.data[0] = session->info.content_type;
1114 session->dirty_out.data[1] = 3;
1115 session->dirty_out.data[2] = 1;
1116 session->dirty_out.data[3] = 0;
1117 session->dirty_out.data[4] = 2;
1118 session->dirty_out.data[5] = session->pending_alert_level;
1119 session->dirty_out.data[6] = session->pending_alert_description;
1121 session->dirty_out.used = 7;
1123 session->pending_alert =
false;
1124 session->alerts_sent++;
1126 SSL_clear(session->ssl);
1128 session_msg_log(request, session, session->dirty_out.data, session->dirty_out.used);
1137 fr_tls_session_t *tls_session = talloc_get_type_abort(
uctx, fr_tls_session_t);
1140 RDEBUG3(
"entered state %s", __FUNCTION__);
1151 if (SSL_is_init_finished(tls_session->ssl)) {
1152 SSL_CIPHER
const *cipher;
1154 char const *version;
1156 char cipher_desc[256], cipher_desc_clean[256];
1157 char *p = cipher_desc, *q = cipher_desc_clean;
1159 cipher = SSL_get_current_cipher(tls_session->ssl);
1160 SSL_CIPHER_description(cipher, cipher_desc,
sizeof(cipher_desc));
1166 while (*p !=
'\0') {
1176 RDEBUG2(
"Cipher suite: %s", cipher_desc_clean);
1178 RDEBUG2(
"Adding TLS session information to request");
1188 if (((
size_t)tls_session->info.version >=
NUM_ELEMENTS(tls_version_str)) ||
1189 !tls_version_str[tls_session->info.version]) {
1190 version =
"UNKNOWN";
1192 version = tls_version_str[tls_session->info.version];
1200 RDEBUG2(
"&session-state.TLS-Session-Version := \"%s\"", version);
1209 if (!tls_session->session) {
1210 tls_session->session = SSL_get_session(tls_session->ssl);
1211 if (!tls_session->session) {
1212 REDEBUG(
"Failed getting TLS session");
1214 tls_session->result = FR_TLS_RESULT_ERROR;
1215 fr_tls_session_request_unbind(tls_session->ssl);
1222 tls_session->session) != 1) {
1224 RDEBUG3(
"Failed retrieving session data");
1231 if (SSL_session_reused(tls_session->ssl)) {
1243 ret = BIO_ctrl_pending(tls_session->from_ssl);
1245 ret = BIO_read(tls_session->from_ssl, tls_session->dirty_out.data,
1246 sizeof(tls_session->dirty_out.data));
1248 tls_session->dirty_out.used = ret;
1249 }
else if (BIO_should_retry(tls_session->from_ssl)) {
1250 record_init(&tls_session->dirty_in);
1251 RDEBUG2(
"Asking for more data in tunnel");
1254 fr_tls_log(NULL, NULL);
1255 record_init(&tls_session->dirty_in);
1260 record_init(&tls_session->clean_out);
1271 if (tls_session->pending_alert) fr_tls_session_alert_send(request, tls_session);
1274 record_init(&tls_session->dirty_in);
1276 tls_session->result = FR_TLS_RESULT_SUCCESS;
1277 fr_tls_session_request_unbind(tls_session->ssl);
1291 fr_tls_session_t *tls_session = talloc_get_type_abort(
uctx, fr_tls_session_t);
1312 for (ret = tls_session->last_ret;
1313 SSL_get_error(tls_session->ssl, ret) == SSL_ERROR_WANT_ASYNC;
1314 ret = SSL_read(tls_session->ssl, tls_session->clean_out.data + tls_session->clean_out.used,
1315 sizeof(tls_session->clean_out.data) - tls_session->clean_out.used));
1320 fr_tls_session_request_unbind(tls_session->ssl);
1338 fr_tls_session_t *tls_session = talloc_get_type_abort(
uctx, fr_tls_session_t);
1341 RDEBUG3(
"(re-)entered state %s", __FUNCTION__);
1363 tls_session->can_pause =
true;
1364 tls_session->last_ret = SSL_read(tls_session->ssl, tls_session->clean_out.data + tls_session->clean_out.used,
1365 sizeof(tls_session->clean_out.data) - tls_session->clean_out.used);
1366 tls_session->can_pause =
false;
1367 if (tls_session->last_ret > 0) {
1368 tls_session->clean_out.used += tls_session->last_ret;
1374 tls_session->result = FR_TLS_RESULT_SUCCESS;
1379 fr_tls_session_request_unbind(tls_session->ssl);
1402 if (SSL_get_error(tls_session->ssl, tls_session->last_ret) == SSL_ERROR_SSL) {
1403 unsigned long ssl_err;
1412 while ((ssl_err = ERR_peek_error()) && (ERR_GET_REASON(ssl_err) == BIO_R_UNSUPPORTED_METHOD)) {
1413 (void) ERR_get_error();
1426 switch (
err = SSL_get_error(tls_session->ssl, tls_session->last_ret)) {
1427 case SSL_ERROR_WANT_ASYNC:
1431 RDEBUG3(
"Performing async action for libssl");
1439 tls_session->result = FR_TLS_RESULT_ERROR;
1446 ua = fr_tls_cache_pending_push(request, tls_session);
1463 ua = fr_tls_verify_cert_pending_push(request, tls_session);
1474 case SSL_ERROR_WANT_ASYNC_JOB:
1475 RERROR(
"No async jobs available in pool, increase thread.openssl_async_pool_max");
1483 if (fr_tls_log_io_error(request,
1484 err,
"SSL_read (%s)", __FUNCTION__) < 0)
goto error;
1485 return tls_session_async_handshake_done_round(p_result, priority, request,
uctx);
1509 fr_tls_session_t *tls_session = talloc_get_type_abort(
uctx, fr_tls_session_t);
1512 RDEBUG3(
"entered state %s", __FUNCTION__);
1514 tls_session->result = FR_TLS_RESULT_IN_PROGRESS;
1516 fr_tls_session_request_bind(tls_session->ssl, request);
1524 if (SSL_is_init_finished(tls_session->ssl)) {
1525 REDEBUG(
"Attempted to continue TLS handshake, but handshake has completed");
1527 tls_session->result = FR_TLS_RESULT_ERROR;
1528 fr_tls_session_request_unbind(tls_session->ssl);
1532 if (tls_session->invalid) {
1533 REDEBUG(
"Preventing invalid session from continuing");
1542 if (tls_session->dirty_in.used) {
1543 ret = BIO_write(tls_session->into_ssl, tls_session->dirty_in.data, tls_session->dirty_in.used);
1544 if (ret != (
int)tls_session->dirty_in.used) {
1545 REDEBUG(
"Failed writing %zd bytes to TLS BIO: %d", tls_session->dirty_in.used, ret);
1546 record_init(&tls_session->dirty_in);
1549 record_init(&tls_session->dirty_in);
1552 return tls_session_async_handshake_cont(p_result, priority, request,
uctx);
1573 tls_session_async_handshake,
1575 tls_session_async_handshake_signal,
1586 static int _fr_tls_session_free(fr_tls_session_t *session)
1589 SSL_set_quiet_shutdown(session->ssl, 1);
1590 SSL_shutdown(session->ssl);
1591 SSL_free(session->ssl);
1592 session->ssl = NULL;
1598 static void session_init(fr_tls_session_t *session)
1600 session->ssl = NULL;
1601 session->into_ssl = session->from_ssl = NULL;
1602 record_init(&session->clean_in);
1603 record_init(&session->clean_out);
1604 record_init(&session->dirty_in);
1605 record_init(&session->dirty_out);
1607 memset(&session->info, 0,
sizeof(session->info));
1610 session->opaque = NULL;
1624 fr_tls_session_t *fr_tls_session_alloc_client(TALLOC_CTX *ctx, SSL_CTX *ssl_ctx)
1627 fr_tls_session_t *tls_session = NULL;
1628 fr_tls_conf_t *
conf = fr_tls_ctx_conf(ssl_ctx);
1630 MEM(tls_session = talloc_zero(ctx, fr_tls_session_t));
1631 talloc_set_destructor(tls_session, _fr_tls_session_free);
1634 tls_session->ssl = SSL_new(ssl_ctx);
1635 if (!tls_session->ssl) {
1644 SSL_set_msg_callback(tls_session->ssl, fr_tls_session_msg_cb);
1645 SSL_set_msg_callback_arg(tls_session->ssl, tls_session);
1646 SSL_set_info_callback(tls_session->ssl, fr_tls_session_info_cb);
1651 DEBUG2(
"Requiring Server certificate");
1652 verify_mode = SSL_VERIFY_PEER;
1653 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1664 SSL_set_verify(tls_session->ssl, verify_mode, NULL);
1665 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (
void *)
conf);
1666 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_TLS_SESSION, (
void *)tls_session);
1668 tls_session->mtu =
conf->fragment_size;
1688 fr_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)
1690 fr_tls_session_t *tls_session = NULL;
1692 int verify_mode = 0;
1694 fr_tls_conf_t *
conf = fr_tls_ctx_conf(ssl_ctx);
1696 RDEBUG2(
"Initiating new TLS session");
1698 MEM(tls_session = talloc_zero(ctx, fr_tls_session_t));
1700 ssl = SSL_new(ssl_ctx);
1702 fr_tls_log(request,
"Error creating new TLS session");
1707 session_init(tls_session);
1708 tls_session->ctx = ssl_ctx;
1709 tls_session->ssl = ssl;
1710 talloc_set_destructor(tls_session, _fr_tls_session_free);
1712 fr_tls_session_request_bind(tls_session->ssl, request);
1717 tls_session->record_init = record_init;
1718 tls_session->record_close = record_close;
1719 tls_session->record_from_buff = record_from_buff;
1720 tls_session->record_to_buff = record_to_buff;
1732 MEM(tls_session->into_ssl = BIO_new(BIO_s_mem()));
1733 MEM(tls_session->from_ssl = BIO_new(BIO_s_mem()));
1734 SSL_set_bio(tls_session->ssl, tls_session->into_ssl, tls_session->from_ssl);
1740 SSL_set_msg_callback(ssl, fr_tls_session_msg_cb);
1741 SSL_set_msg_callback_arg(ssl, tls_session);
1742 SSL_set_info_callback(ssl, fr_tls_session_info_cb);
1760 if (
conf->cache.mode != FR_TLS_CACHE_DISABLED) {
1763 uint8_t digest[SHA256_DIGEST_LENGTH];
1765 static_assert(
sizeof(digest) <= SSL_MAX_SSL_SESSION_ID_LENGTH,
1766 "SSL_MAX_SSL_SESSION_ID_LENGTH must be >= SHA256_DIGEST_LENGTH");
1769 if (
tmpl_aexpand(tls_session, &context_id, request,
conf->cache.id_name, NULL, NULL) < 0) {
1770 RPEDEBUG(
"Failed expanding session ID");
1772 fr_tls_session_request_unbind(tls_session->ssl);
1777 MEM(md_ctx = EVP_MD_CTX_create());
1778 EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL);
1779 EVP_DigestUpdate(md_ctx, context_id, talloc_array_length(context_id) - 1);
1780 EVP_DigestFinal_ex(md_ctx, digest, NULL);
1781 EVP_MD_CTX_destroy(md_ctx);
1785 digest,
sizeof(digest)) == 1))
goto error;
1793 RDEBUG2(
"Loading TLS session certificate \"%pV\"", &
vp->data);
1795 if (SSL_use_certificate_file(tls_session->ssl,
vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
1796 fr_tls_log(request,
"Failed loading TLS session certificate \"%s\"",
1801 if (SSL_use_PrivateKey_file(tls_session->ssl,
vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
1802 fr_tls_log(request,
"Failed loading TLS session certificate \"%s\"",
1807 if (SSL_check_private_key(tls_session->ssl) != 1) {
1808 fr_tls_log(request,
"Failed validating TLS session certificate \"%s\"",
1817 if (!
conf->chains || !
conf->chains[0]->private_key_file) {
1818 ERROR(
"TLS Server requires a private key file");
1822 if (!
conf->chains || !
conf->chains[0]->certificate_file) {
1823 ERROR(
"TLS Server requires a certificate file");
1834 if (
vp) client_cert =
vp->vp_bool;
1842 SSL_set_accept_state(tls_session->ssl);
1848 RDEBUG2(
"Setting verify mode to require certificate from client");
1849 verify_mode = SSL_VERIFY_PEER;
1850 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1851 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
1853 tls_session->verify_client_cert = client_cert;
1855 SSL_set_verify(tls_session->ssl, verify_mode, fr_tls_verify_cert_cb);
1856 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (
void *)
conf);
1857 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_TLS_SESSION, (
void *)tls_session);
1859 tls_session->mtu =
conf->fragment_size;
1860 if (dynamic_mtu > 100 && dynamic_mtu < tls_session->mtu) {
1861 RDEBUG2(
"Setting fragment_len to %zu from dynamic_mtu", dynamic_mtu);
1862 tls_session->mtu = dynamic_mtu;
1865 if (
conf->cache.mode != FR_TLS_CACHE_DISABLED) {
1866 tls_session->allow_session_resumption =
true;
1867 fr_tls_cache_session_alloc(tls_session);
1870 fr_tls_session_request_unbind(tls_session->ssl);
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
#define static_assert
For systems with an old version libc, define static_assert.
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
fr_dcursor_eval_t void const * uctx
#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.
#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_session_resumed
HIDDEN fr_dict_attr_t const * attr_tls_session_version
HIDDEN fr_dict_attr_t const * attr_tls_session_require_client_cert
HIDDEN fr_dict_attr_t const * attr_tls_client_error_code
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)
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.
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
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 tmpl_aexpand(_ctx, _out, _request, _vpt, _escape, _escape_ctx)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
size_t strlcpy(char *dst, char const *src, size_t siz)
Stores an attribute, a value and various bits of other data.
static size_t char fr_sbuff_t size_t inlen
static size_t char ** out