The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
session.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: c78710ceb9b18f273c250c47de1ff8c6befa9c17 $
19 *
20 * @file tls/session.c
21 * @brief Initialise OpenSSL sessions, and read/write data to/from them.
22 *
23 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
24 */
25#ifdef WITH_TLS
26#define LOG_PREFIX "tls"
27#define _TLS_PRIVATE 1
28
29#include <freeradius-devel/server/pair.h>
30#include <freeradius-devel/server/log.h>
31
32#include <freeradius-devel/util/debug.h>
33#include <freeradius-devel/util/math.h>
34#include <freeradius-devel/util/base16.h>
35#include <freeradius-devel/util/skip.h>
36#include <freeradius-devel/util/pair_legacy.h>
37
38#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
39
40#include <freeradius-devel/unlang/action.h>
41#include <freeradius-devel/unlang/call.h>
42#include <freeradius-devel/unlang/interpret.h>
43#include <freeradius-devel/unlang/subrequest.h>
44
45#include <sys/stat.h>
46#include <fcntl.h>
47
48#include <freeradius-devel/protocol/tls/freeradius.h>
49
50#include "attrs.h"
51#include "base.h"
52#include "dtls.h"
53#include "log.h"
54
55#include <openssl/x509v3.h>
56#include <openssl/ssl.h>
57
58static char const *tls_version_str[] = {
59 [SSL2_VERSION] = "SSL 2.0",
60 [SSL3_VERSION] = "SSL 3.0",
61 [TLS1_VERSION] = "TLS 1.0",
62 [TLS1_1_VERSION] = "TLS 1.1",
63 [TLS1_2_VERSION] = "TLS 1.2",
64 [TLS1_3_VERSION] = "TLS 1.3",
65#ifdef TLS1_4_VERSION
66 [TLS1_4_VERSION] = "TLS 1.4",
67#endif
68};
69
70static char const *tls_content_type_str[] = {
71 [SSL3_RT_CHANGE_CIPHER_SPEC] = "change_cipher_spec",
72 [SSL3_RT_ALERT] = "alert",
73 [SSL3_RT_HANDSHAKE] = "handshake",
74 [SSL3_RT_APPLICATION_DATA] = "application_data",
75#ifdef SSL3_RT_HEADER
76 [SSL3_RT_HEADER] = "header",
77#endif
78#ifdef SSL3_RT_INNER_CONTENT_TYPE
79 [SSL3_RT_INNER_CONTENT_TYPE] = "inner_content_type",
80#endif
81};
82
83static char const *tls_alert_description_str[] = {
84 [SSL3_AD_CLOSE_NOTIFY] = "close_notify",
85 [SSL3_AD_UNEXPECTED_MESSAGE] = "unexpected_message",
86 [SSL3_AD_BAD_RECORD_MAC] = "bad_record_mac",
87 [TLS1_AD_DECRYPTION_FAILED] = "decryption_failed",
88 [TLS1_AD_RECORD_OVERFLOW] = "record_overflow",
89 [SSL3_AD_DECOMPRESSION_FAILURE] = "decompression_failure",
90 [SSL3_AD_HANDSHAKE_FAILURE] = "handshake_failure",
91 [SSL3_AD_BAD_CERTIFICATE] = "bad_certificate",
92 [SSL3_AD_UNSUPPORTED_CERTIFICATE] = "unsupported_certificate",
93 [SSL3_AD_CERTIFICATE_REVOKED] = "certificate_revoked",
94 [SSL3_AD_CERTIFICATE_EXPIRED] = "certificate_expired",
95 [SSL3_AD_CERTIFICATE_UNKNOWN] = "certificate_unknown",
96 [SSL3_AD_ILLEGAL_PARAMETER] = "illegal_parameter",
97 [TLS1_AD_UNKNOWN_CA] = "unknown_ca",
98 [TLS1_AD_ACCESS_DENIED] = "access_denied",
99 [TLS1_AD_DECODE_ERROR] = "decode_error",
100 [TLS1_AD_DECRYPT_ERROR] = "decrypt_error",
101 [TLS1_AD_EXPORT_RESTRICTION] = "export_restriction",
102 [TLS1_AD_PROTOCOL_VERSION] = "protocol_version",
103 [TLS1_AD_INSUFFICIENT_SECURITY] = "insufficient_security",
104 [TLS1_AD_INTERNAL_ERROR] = "internal_error",
105 [TLS1_AD_USER_CANCELLED] = "user_cancelled",
106 [TLS1_AD_NO_RENEGOTIATION] = "no_renegotiation",
107#ifdef TLS13_AD_MISSING_EXTENSION
108 [TLS13_AD_MISSING_EXTENSION] = "missing_extension",
109#endif
110#ifdef TLS13_AD_CERTIFICATE_REQUIRED
111 [TLS13_AD_CERTIFICATE_REQUIRED] = "certificate_required",
112#endif
113#ifdef TLS1_AD_UNSUPPORTED_EXTENSION
114 [TLS1_AD_UNSUPPORTED_EXTENSION] = "unsupported_extension",
115#endif
116#ifdef TLS1_AD_CERTIFICATE_UNOBTAINABLE
117 [TLS1_AD_CERTIFICATE_UNOBTAINABLE] = "certificate_unobtainable",
118#endif
119#ifdef TLS1_AD_UNRECOGNIZED_NAME
120 [TLS1_AD_UNRECOGNIZED_NAME] = "unrecognised_name",
121#endif
122#ifdef TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE
123 [TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE] = "bad_certificate_status_response",
124#endif
125#ifdef TLS1_AD_BAD_CERTIFICATE_HASH_VALUE
126 [TLS1_AD_BAD_CERTIFICATE_HASH_VALUE] = "bad_certificate_hash_value",
127#endif
128#ifdef TLS1_AD_UNKNOWN_PSK_IDENTITY
129 [TLS1_AD_UNKNOWN_PSK_IDENTITY] = "unknown_psk_identity",
130#endif
131#ifdef TLS1_AD_NO_APPLICATION_PROTOCOL
132 [TLS1_AD_NO_APPLICATION_PROTOCOL] = "no_application_protocol",
133#endif
134};
135
136static char const *tls_handshake_type_str[] = {
137 [SSL3_MT_HELLO_REQUEST] = "hello_request",
138 [SSL3_MT_CLIENT_HELLO] = "client_hello",
139 [SSL3_MT_SERVER_HELLO] = "server_hello",
140#ifdef SSL3_MT_NEWSESSION_TICKET
141 [SSL3_MT_NEWSESSION_TICKET] = "new_session_ticket",
142#endif
143#ifdef SSL3_MT_END_OF_EARLY_DATA
144 [SSL3_MT_END_OF_EARLY_DATA] = "end_of_early_data",
145#endif
146#ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
147 [SSL3_MT_ENCRYPTED_EXTENSIONS] = "encrypted_extensions",
148#endif
149 [SSL3_MT_CERTIFICATE] = "certificate",
150 [SSL3_MT_SERVER_KEY_EXCHANGE] = "server_key_exchange",
151 [SSL3_MT_CERTIFICATE_REQUEST] = "certificate_request",
152 [SSL3_MT_SERVER_DONE] = "server_hello_done",
153 [SSL3_MT_CERTIFICATE_VERIFY] = "certificate_verify",
154 [SSL3_MT_CLIENT_KEY_EXCHANGE] = "client_key_exchange",
155 [SSL3_MT_FINISHED] = "finished",
156#ifdef SSL3_MT_CERTIFICATE_URL
157 [SSL3_MT_CERTIFICATE_URL] = "certificate_url",
158#endif
159#ifdef SSL3_MT_CERTIFICATE_STATUS
160 [SSL3_MT_CERTIFICATE_STATUS] = "certificate_status",
161#endif
162#ifdef SSL3_MT_SUPPLEMENTAL_DATA
163 [SSL3_MT_SUPPLEMENTAL_DATA] = "supplemental_data",
164#endif
165#ifdef SSL3_MT_KEY_UPDATE
166 [SSL3_MT_KEY_UPDATE] = "key_update",
167#endif
168#ifdef SSL3_MT_NEXT_PROTO
169 [SSL3_MT_NEXT_PROTO] = "next_proto",
170#endif
171#ifdef SSL3_MT_MESSAGE_HASH
172 [SSL3_MT_MESSAGE_HASH] = "message_hash",
173#endif
174#ifdef DTLS1_MT_HELLO_VERIFY_REQUEST
175 [DTLS1_MT_HELLO_VERIFY_REQUEST] = "hello_verify_request",
176#endif
177#ifdef SSL3_MT_CHANGE_CIPHER_SPEC
178 [SSL3_MT_CHANGE_CIPHER_SPEC] = "change_cipher_spec",
179#endif
180};
181
182/** Return the static private key password we have configured
183 *
184 * @param[out] buf Where to write the password to.
185 * @param[in] size The length of buf.
186 * @param[in] rwflag
187 * - 0 if password used for decryption.
188 * - 1 if password used for encryption.
189 * @param[in] u The static password.
190 * @return
191 * - 0 on error.
192 * - >0 on success (the length of the password).
193 */
194int fr_tls_session_password_cb(char *buf, int size, int rwflag UNUSED, void *u)
195{
196 size_t len;
197
198 /*
199 * We do this instead of not registering the callback
200 * to ensure OpenSSL doesn't try and read a password
201 * from stdin (causes server to block).
202 */
203 if (!u) {
204 ERROR("Private key encrypted but no private_key_password configured");
205 return 0;
206 }
207
208 len = strlcpy(buf, (char *)u, size);
209 if (len >= (size_t)size) {
210 ERROR("Password too long. Maximum length is %i bytes", size - 1);
211 return 0;
212 }
213
214 return (int)len;
215}
216
217#ifdef PSK_MAX_IDENTITY_LEN
218/** Verify the PSK identity contains no reserved chars
219 *
220 * @param identity to check.
221 * @return
222 * - true identity does not contain reserved chars.
223 * - false identity contains reserved chars.
224 */
225static bool session_psk_identity_is_safe(const char *identity)
226{
227 char c;
228
229 if (!identity) return true;
230
231 while ((c = *(identity++)) != '\0') {
232 if (isalpha((uint8_t) c) || isdigit((uint8_t) c) || isspace((uint8_t) c) ||
233 (c == '@') || (c == '-') || (c == '_') || (c == '.')) {
234 continue;
235 }
236
237 return false;
238 }
239
240 return true;
241}
242
243/** Determine the PSK to use for an outgoing connection
244 *
245 * @param[in] ssl session.
246 * @param[in] identity The identity of the PSK to search for.
247 * @param[out] psk Where to write the PSK we found (if any).
248 * @param[in] max_psk_len The length of the buffer provided for PSK.
249 * @return
250 * - 0 if no PSK matching identity was found.
251 * - >0 if a PSK matching identity was found (the length of bytes written to psk).
252 */
253unsigned int fr_tls_session_psk_client_cb(SSL *ssl, UNUSED char const *hint,
254 char *identity, unsigned int max_identity_len,
255 unsigned char *psk, unsigned int max_psk_len)
256{
257 unsigned int psk_len;
258 fr_tls_conf_t *conf;
259
260 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
261 if (!conf) return 0;
262
263 psk_len = talloc_array_length(conf->psk.key);
264 if (psk_len > max_psk_len) return 0;
265
266 strlcpy(identity, conf->psk.identity, max_identity_len);
267 memcpy(psk, conf->psk.key, psk_len);
268
269 return psk_len;
270}
271
272/** Forget the result of `load psk`, so that the next handshake starts with no pending call
273 *
274 * @param[in] tls_session The TLS session that the section ran for.
275 */
276static void tls_session_psk_reset(fr_tls_session_t *tls_session)
277{
278 TALLOC_FREE(tls_session->psk.key);
279 tls_session->psk.key_len = 0;
280 tls_session->psk.state = FR_TLS_PSK_INIT;
281}
282
283/** Determine the PSK to use for an incoming connection
284 *
285 * When the virtual server has a `load psk` section, the callback pauses
286 * the handshake. tls_session_async_handshake_cont() then pushes the
287 * section with fr_tls_session_psk_pending_push(). Once the section has
288 * finished, the callback returns the key that the section placed in
289 * `reply.TLS-PSK-Key`.
290 *
291 * @param[in] ssl session.
292 * @param[in] identity The identity of the PSK to search for.
293 * @param[out] psk Where to write the PSK we found (if any).
294 * @param[in] max_psk_len The length of the buffer provided for PSK.
295 * @return
296 * - 0 if no PSK matching identity was found.
297 * - >0 if a PSK matching identity was found (the length of bytes written to psk).
298 */
299unsigned int fr_tls_session_psk_server_cb(SSL *ssl, const char *identity,
300 unsigned char *psk, unsigned int max_psk_len)
301{
302 size_t psk_len = 0;
303 fr_tls_conf_t *conf;
305
306 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
307 if (!conf) return 0;
308
309 request = fr_tls_session_request(ssl);
310 if (request && conf->load_psk) {
311 fr_tls_session_t *tls_session = fr_tls_session(ssl);
312 fr_pair_t *vp;
313
314 /*
315 * The passed identity is weird. Deny it.
316 */
317 if (!session_psk_identity_is_safe(identity)) {
318 RWDEBUG("(TLS) Invalid characters in PSK identity %s", identity);
319 fr_tls_session_error_add(request, FR_ERROR_VALUE_PSK_IDENTITY_INVALID);
320 return 0;
321 }
322
323 /*
324 * The `TLS-PSK-Identity` pair stays in the request after the handshake,
325 * so that policies can read the identity. fr_tls_session_psk_pending_push()
326 * copies the pair into the `load psk` subrequest.
327 */
329 if (fr_pair_value_from_str(vp, identity, strlen(identity), NULL, true) < 0) {
330 RPWDEBUG2("Failed parsing TLS PSK Identity");
332 return 0;
333 }
334
335 /*
336 * OpenSSL runs the handshake, including the PSK callback, on a fibre whose
337 * stack OpenSSL sizes. The unlang interpreter never runs on that stack, so
338 * the section runs on the request's own stack:
339 *
340 * 1. The callback records that `load psk` needs to run, and pauses the
341 * handshake.
342 * 2. Control returns to tls_session_async_handshake_cont(), which pushes the
343 * section with fr_tls_session_psk_pending_push().
344 * 3. Once the section has finished, tls_session_async_handshake_cont() resumes
345 * the handshake.
346 * 4. Execution continues after the ASYNC_pause_job() call below.
347 *
348 * The `verify certificate` and session cache sections pause the handshake the
349 * same way.
350 */
351 if (!fr_cond_assert_msg(tls_session->can_pause,
352 "Unexpected call to %s. tls_session_async_handshake_cont must be in call stack",
353 __FUNCTION__)) return 0;
354
355 fr_assert(tls_session->psk.state == FR_TLS_PSK_INIT);
356 tls_session->psk.state = FR_TLS_PSK_REQUESTED;
357 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_PSK);
358 tls_session->psk.max_psk_len = max_psk_len;
359
360 ASYNC_pause_job();
361
362 /*
363 * If the request was cancelled while the handshake was paused,
364 * the section may not have run.
365 */
367 tls_session_psk_reset(tls_session);
368 return 0;
369 }
370
371 switch (tls_session->psk.state) {
372 case FR_TLS_PSK_SUCCESS:
373 break;
374
375 case FR_TLS_PSK_FAILED:
376 tls_session_psk_reset(tls_session);
377 return 0;
378
379 case FR_TLS_PSK_INIT:
380 case FR_TLS_PSK_REQUESTED:
381 fr_assert_msg(0, "Handshake resumed with `load psk` in state %u",
382 tls_session->psk.state);
383 tls_session_psk_reset(tls_session);
384 return 0;
385 }
386
387 /*
388 * _tls_session_psk_load_resume() checked the key, so the
389 * callback only copies the key into the `psk` buffer that
390 * OpenSSL passed.
391 */
392 psk_len = tls_session->psk.key_len;
393 memcpy(psk, tls_session->psk.key, psk_len);
394 tls_session_psk_reset(tls_session);
395
396 return (unsigned int)psk_len;
397 }
398
399 if (!conf->psk.identity) {
400 DEBUG("Rejecting the client, no fixed psk.identity set");
401 return 0;
402 }
403
404 /*
405 * Compare the identity against the fixed `psk.identity`, then
406 * copy the fixed `psk.key` into the `psk` buffer that OpenSSL
407 * passed.
408 */
409 if (strcmp(identity, conf->psk.identity) != 0) {
410 fr_tls_log_error("Supplied PSK identity %s does not match configuration. Rejecting.",
411 identity);
412 fr_tls_session_error_add(request, FR_ERROR_VALUE_PSK_IDENTITY_UNKNOWN);
413 return 0;
414 }
415
416 psk_len = talloc_array_length(conf->psk.key);
417 if (psk_len > max_psk_len) return 0;
418
419 memcpy(psk, conf->psk.key, psk_len);
420
421 return (unsigned int)psk_len;
422}
423
424/** Process the result of `load psk`
425 *
426 * The function runs in the subrequest, once the section has finished.
427 * The interpreter frees the subrequest when the function returns, so the
428 * function copies the key out of the reply and into the session. The
429 * function logs every error, because the function runs on the request's
430 * own stack rather than on the OpenSSL fibre. fr_tls_session_psk_server_cb()
431 * then only copies the key into the `psk` buffer that OpenSSL passed.
432 *
433 * @param[in] request The subrequest that ran the section.
434 * @param[in] uctx The #fr_tls_session_t that the section ran for.
435 * @return UNLANG_ACTION_CALCULATE_RESULT
436 */
437static unlang_action_t _tls_session_psk_load_resume(request_t *request, void *uctx)
438{
439 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
440 fr_pair_t *vp;
441
442 fr_assert(tls_session->psk.state == FR_TLS_PSK_REQUESTED);
443
444 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_packet_type);
445 if (!vp || (vp->vp_uint32 != enum_tls_packet_type_success->vb_uint32)) {
446 RWDEBUG("(TLS) Failed loading the pre-shared key, `load psk` did not return success");
447
448 /*
449 * `notfound` says that the policy does not know the
450 * identity, and an unknown identity is the peer's error.
451 * Any other failure is the policy's error.
452 */
453 fr_tls_session_error_add(request->parent,
454 (vp && (vp->vp_uint32 == enum_tls_packet_type_notfound->vb_uint32)) ?
455 FR_ERROR_VALUE_PSK_IDENTITY_UNKNOWN :
456 FR_ERROR_VALUE_LOAD_PSK_FAILED);
457 failed:
458 tls_session->psk.state = FR_TLS_PSK_FAILED;
460 }
461
462 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_psk_key);
463 if (!vp || (vp->vp_length == 0)) {
464 RWDEBUG("(TLS) `load psk` returned success, but reply.TLS-PSK-Key is missing or empty");
465 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_PSK_EMPTY);
466 goto failed;
467 }
468
469 if (vp->vp_length > tls_session->psk.max_psk_len) {
470 RWDEBUG("(TLS) Rejecting reply.TLS-PSK-Key of %zu bytes, OpenSSL accepts at most %u bytes", vp->vp_length,
471 tls_session->psk.max_psk_len);
472 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_PSK_TOO_LONG);
473 goto failed;
474 }
475
476 MEM(tls_session->psk.key = talloc_memdup(tls_session, vp->vp_octets, vp->vp_length));
477 tls_session->psk.key_len = vp->vp_length;
478 tls_session->psk.state = FR_TLS_PSK_SUCCESS;
479
480 RDEBUG2("(TLS) Loaded a %zu-byte pre-shared key", vp->vp_length);
481
483}
484
485/** Push a `load psk` call into the current request, using a subrequest
486 *
487 * tls_session_async_handshake_cont() calls the function once the
488 * handshake has paused, after the pushes for pending session cache
489 * sections and before the push for a pending `verify certificate`
490 * section.
491 *
492 * @param[in] request The current request.
493 * @param[in] tls_session The current TLS session.
494 * @return
495 * - UNLANG_ACTION_CALCULATE_RESULT on noop.
496 * - UNLANG_ACTION_PUSHED_CHILD on success.
497 * - UNLANG_ACTION_FAIL on failure.
498 */
499unlang_action_t fr_tls_session_psk_pending_push(request_t *request, fr_tls_session_t *tls_session)
500{
501 fr_tls_conf_t *conf = tls_session->conf;
502 request_t *child;
503 fr_pair_t *identity;
505
506 if (tls_session->psk.state != FR_TLS_PSK_REQUESTED) return UNLANG_ACTION_CALCULATE_RESULT;
507
508 fr_assert(conf->virtual_server);
509
510 /*
511 * fr_tls_session_psk_server_cb() added the `TLS-PSK-Identity` pair to
512 * the request before pausing the handshake.
513 */
514 identity = fr_pair_find_by_da(&request->request_pairs, NULL, attr_tls_psk_identity);
515 fr_assert(identity);
516
517 RDEBUG2("(TLS) Loading the pre-shared key for identity \"%pV\"", &identity->data);
518
519 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_load_psk->vb_uint32,
520 &tls_session->session_id));
521
522 /*
523 * Add extra pairs to the subrequest
524 */
525 fr_tls_session_extra_pairs_copy_to_child(child, tls_session);
526
527 fr_pair_append(&child->request_pairs, fr_pair_copy(child->request_ctx, identity));
528
529 /*
530 * Push the call to the TLS virtual server into the child.
531 */
532 ua = fr_tls_call_push(child, _tls_session_psk_load_resume, conf, tls_session, false);
533 if (ua == UNLANG_ACTION_FAIL) {
534 REDEBUG("(TLS) Failed calling TLS virtual server");
535 talloc_free(child);
536 return UNLANG_ACTION_FAIL;
537 }
538
539 return ua;
540}
541#endif /* PSK_MAX_IDENTITY_LEN */
542
543/** Record a TLS alert in a pair list
544 *
545 * A TLS Alert says why the handshake ended, so we only keep the last one.
546 *
547 * A peer can send multiple alerts, and if we left earlier `Alert` in
548 * the list, the policies would be looking at the wrong one.
549 *
550 * When we receive a TLS Alert, it gets copied into the `request`
551 * list. When we send a TLS Alert, it gets copied into the `reply`
552 * list.
553 *
554 * We also update `session-state.Error` to describe the TLS Alert.
555 * See fr_tls_session_error_add().
556 *
557 * @param[in] request to log through. A NULL request is ignored,
558 * because several OpenSSL callbacks run without one.
559 * @param[in] list_ctx the pair which holds the list, either
560 * `request->request_ctx` or `request->reply_ctx`.
561 * @param[in] prefix names the list in the log line.
562 * @param[in] level of the alert, warning or fatal.
563 * @param[in] description of the alert.
564 */
565static void tls_session_alert_pair_add(request_t *request, fr_pair_t *list_ctx, char const *prefix,
566 uint8_t level, uint8_t description)
567{
568 fr_pair_t *alert, *vp;
569
570 if (!request) return;
571
572 fr_pair_delete_by_da(&list_ctx->vp_group, attr_tls_alert);
573
574 MEM(fr_pair_append_by_da(list_ctx, &alert, &list_ctx->vp_group, attr_tls_alert) >= 0);
575
576 MEM(fr_pair_append_by_da(alert, &vp, &alert->vp_group, attr_tls_alert_level) >= 0);
577 vp->vp_uint8 = level;
578
579 MEM(fr_pair_append_by_da(alert, &vp, &alert->vp_group, attr_tls_alert_description) >= 0);
580 vp->vp_uint8 = description;
581
582 RDEBUG2("(TLS) Adding %s%pP", prefix, alert);
583}
584
585
586DIAG_OFF_OPTIONAL(used-but-marked-unused) /* Fix spurious warnings for sk_ macros */
587/** Record session state changes
588 *
589 * Called by OpenSSL whenever the session state changes, an alert is received or an error occurs.
590 *
591 * @param[in] ssl session.
592 * @param[in] where Which context the callback is being called in.
593 * See https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_info_callback.html
594 * for additional info.
595 * @param[in] ret 0 if an error occurred, or the alert type if an alert was received.
596 */
597void fr_tls_session_info_cb(SSL const *ssl, int where, int ret)
598{
599 char const *role, *state;
600 request_t *request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
601
602 /*
603 * OpenSSL runs the info callback from inside a handshake round,
604 * and the library binds the request across a round.
605 */
606 fr_assert(request != NULL);
607
608 if ((where & ~SSL_ST_MASK) & SSL_ST_CONNECT) {
609 role = "Client ";
610 } else if (((where & ~SSL_ST_MASK)) & SSL_ST_ACCEPT) {
611 role = "Server ";
612 } else {
613 role = "";
614 }
615
616 state = SSL_state_string_long(ssl);
617 state = state ? state : "<INVALID>";
618
619 if ((where & SSL_CB_LOOP) || (where & SSL_CB_HANDSHAKE_START) || (where & SSL_CB_HANDSHAKE_DONE)) {
620 if (RDEBUG_ENABLED3) {
621 char const *abbrv = SSL_state_string(ssl);
622 size_t len;
623
624 /*
625 * Trim crappy OpenSSL state strings...
626 */
627 len = strlen(abbrv);
628 if ((len > 1) && (abbrv[len - 1] == ' ')) len--;
629
630 RDEBUG3("(TLS) Handshake state [%.*s] - %s%s", (int)len, abbrv, role, state);
631
632#ifdef OPENSSL_NO_SSL_TRACE
633 {
634 STACK_OF(SSL_CIPHER) *server_ciphers;
635 STACK_OF(SSL_CIPHER) *client_ciphers;
636
637 /*
638 * After a ClientHello, list all the proposed ciphers
639 * from the client.
640 *
641 * Only do this if we don't have SSL_trace() as
642 * SSL_trace() prints this information and we don't
643 * want to duplicate it.
644 */
645 if (SSL_get_state(ssl) == TLS_ST_SR_CLNT_HELLO &&
646 (client_ciphers = SSL_get_client_ciphers(ssl))) {
647 int i;
648 int num_ciphers;
649 const SSL_CIPHER *this_cipher;
650
651 server_ciphers = SSL_get_ciphers(ssl);
652 /*
653 * These are printed on startup, so not usually
654 * required.
655 */
656 RDEBUG4("(TLS) Our preferred ciphers (by priority)");
657 if (RDEBUG_ENABLED4) {
658 RINDENT();
659 num_ciphers = sk_SSL_CIPHER_num(server_ciphers);
660 for (i = 0; i < num_ciphers; i++) {
661 this_cipher = sk_SSL_CIPHER_value(server_ciphers, i);
662 RDEBUG4("(TLS) [%i] %s", i, SSL_CIPHER_get_name(this_cipher));
663 }
664 REXDENT();
665 }
666
667 /*
668 * Print information about the client's
669 * handshake message.
670 */
671 if (RDEBUG_ENABLED3) {
672 RDEBUG3("(TLS) Client's preferred ciphers (by priority)");
673 RINDENT();
674 num_ciphers = sk_SSL_CIPHER_num(client_ciphers);
675 for (i = 0; i < num_ciphers; i++) {
676 this_cipher = sk_SSL_CIPHER_value(client_ciphers, i);
677 RDEBUG3("(TLS) [%i] %s", i, SSL_CIPHER_get_name(this_cipher));
678 }
679 REXDENT();
680 }
681 }
682 }
683# endif
684 } else {
685 RDEBUG2("(TLS) Handshake state - %s%s (%u)", role, state, SSL_get_state(ssl));
686 }
687 return;
688 }
689
690 if (where & SSL_CB_ALERT) {
691 if ((ret & 0xff) == SSL_AD_CLOSE_NOTIFY) return;
692
693 /*
694 * We got an alert...
695 */
696 if (where & SSL_CB_READ) {
697 REDEBUG("(TLS) Client sent %s TLS alert (%i) - %s", SSL_alert_type_string_long(ret),
698 ret & 0xff, SSL_alert_desc_string_long(ret));
699
700 /*
701 * Offer helpful advice... Should be expanded.
702 */
703 switch (ret & 0xff) {
704 case TLS1_AD_UNKNOWN_CA:
705 REDEBUG("(TLS) Verify the client has a copy of the server's Certificate "
706 "Authority (CA) installed, and trusts that CA");
707 break;
708
709 default:
710 break;
711 }
712
713 /*
714 * The alert level is packed into the high octet of `ret`. The alert
715 * description is packed into the low octet.
716 *
717 * A peer can send alerts are multiple points in the handshake, so
718 * earlier alerts are removed before the latest one is added.
719 *
720 * We could just find an earlier alert and update its members, but that's
721 * more code.
722 */
723 tls_session_alert_pair_add(request, request->request_ctx, "",
724 (ret >> 8) & 0xff, ret & 0xff);
725 fr_tls_session_error_add(request, FR_ERROR_VALUE_RECEIVED_ALERT);
726 /*
727 * We're sending the client an alert.
728 */
729 } else {
730 REDEBUG("(TLS) Sending client %s TLS alert (%i) - %s",
731 SSL_alert_type_string_long(ret), ret & 0xff, SSL_alert_desc_string_long(ret));
732
733 /*
734 * Offer helpful advice... Should be expanded.
735 */
736 switch (ret & 0xff) {
737 case TLS1_AD_PROTOCOL_VERSION:
738 REDEBUG("(TLS) Client requested a TLS protocol version that is not "
739 "enabled or not supported. Upgrade FreeRADIUS + OpenSSL to their latest "
740 "versions and/or adjust 'tls_max_version'/'tls_min_version' if you want "
741 "authentication to succeed");
742 break;
743
744 default:
745 break;
746 }
747
748 /*
749 * A sent alert goes in the reply list, where a received one
750 * goes in the request list. `ret` packs the two octets the
751 * same way in both directions.
752 */
753 tls_session_alert_pair_add(request, request->reply_ctx, "reply.",
754 (ret >> 8) & 0xff, ret & 0xff);
755 fr_tls_session_error_add(request, FR_ERROR_VALUE_SENT_ALERT);
756 }
757 return;
758 }
759
760 if (where & SSL_CB_EXIT) {
761 if (ret == 0) {
762 REDEBUG("(TLS) Handshake exit state %s%s", role, state);
763 return;
764 }
765
766 if (ret < 0) {
767 if (SSL_want_read(ssl)) {
768 RDEBUG2("(TLS) Need more data from client"); /* State same as previous call, don't print */
769 return;
770 }
771 REDEBUG("(TLS) Handshake exit state %s%s", role, state);
772 }
773 }
774}
775DIAG_ON_OPTIONAL(used-but-marked-unused)
776
777/** Print a message to the request or global log detailing handshake state
778 *
779 * @param[in] request The current #request_t.
780 * @param[in] tls_session The current TLS session.
781 */
782static void session_msg_log(request_t *request, fr_tls_session_t *tls_session, uint8_t const *data, size_t data_len)
783{
784 char const *version, *content_type;
785 char const *str_details1 = NULL;
786 char const *str_details2 = NULL;
787 char unknown_version[32];
788 char unknown_content_type[32];
789 char unknown_alert_level[32];
790 char unknown_alert_description[32];
791 char unknown_handshake_type[32];
792
793 /*
794 * Both callers hold a request: fr_tls_session_msg_cb() takes it
795 * off the SSL *, and fr_tls_session_alert_send() is handed one.
796 */
797 fr_assert(request != NULL);
798
799 /*
800 * Don't print this out in the normal course of
801 * operations.
802 */
803 if (!RDEBUG_ENABLED2) return;
804
805 if (((size_t)tls_session->info.version >= NUM_ELEMENTS(tls_version_str)) ||
806 !tls_version_str[tls_session->info.version]) {
807 snprintf(unknown_version, sizeof(unknown_version), "unknown_tls_version_0x%04x",
808 (unsigned int) tls_session->info.version);
809 version = unknown_version;
810 } else {
811 version = tls_version_str[tls_session->info.version];
812 }
813
814 /*
815 * TLS 1.0, 1.1, 1.2 content types are the same as SSLv3
816 */
817 if (((size_t)tls_session->info.content_type >= NUM_ELEMENTS(tls_content_type_str)) ||
818 !tls_content_type_str[tls_session->info.content_type]) {
819 snprintf(unknown_content_type, sizeof(unknown_content_type),
820 "unknown_content_type_0x%04x", (unsigned int) tls_session->info.content_type);
821 content_type = unknown_content_type;
822 } else {
823 content_type = tls_content_type_str[tls_session->info.content_type];
824 }
825
826 if (tls_session->info.content_type == SSL3_RT_ALERT) {
827 if (tls_session->info.record_len == 2) {
828 switch (tls_session->info.alert_level) {
829 case SSL3_AL_WARNING:
830 str_details1 = "warning";
831 break;
832 case SSL3_AL_FATAL:
833 str_details1 = "fatal";
834 break;
835
836 default:
837 snprintf(unknown_alert_level, sizeof(unknown_alert_level),
838 "unknown_alert_level_0x%04x", tls_session->info.alert_level);
839 str_details1 = unknown_alert_level;
840 break;
841 }
842
843 if (((size_t)tls_session->info.alert_description >= NUM_ELEMENTS(tls_alert_description_str)) ||
844 !tls_alert_description_str[tls_session->info.alert_description]) {
845 snprintf(unknown_alert_description, sizeof(unknown_alert_description),
846 "unknown_alert_0x%04x", tls_session->info.alert_description);
847 str_details2 = unknown_alert_description;
848 } else {
849 str_details2 = tls_alert_description_str[tls_session->info.alert_description];
850 }
851 } else {
852 str_details1 = "unknown_alert_level";
853 str_details2 = "unknown_alert";
854 }
855 }
856
857 if ((size_t)tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
858 if (tls_session->info.record_len > 0) {
859 /*
860 * Range guard not needed due to size of array
861 * and underlying type.
862 */
863 if (!tls_handshake_type_str[tls_session->info.handshake_type]) {
864 snprintf(unknown_handshake_type, sizeof(unknown_handshake_type),
865 "unknown_handshake_type_0x%02x", tls_session->info.handshake_type);
866 str_details1 = unknown_handshake_type;
867 } else {
868 str_details1 = tls_handshake_type_str[tls_session->info.handshake_type];
869 }
870 }
871 }
872
873 snprintf(tls_session->info.info_description, sizeof(tls_session->info.info_description),
874 "%s %s, %s[length %lu]%s%s%s%s",
875 tls_session->info.origin ? ">>> send" : "<<< recv",
876 version,
877 content_type,
878 (unsigned long)tls_session->info.record_len,
879 str_details1 ? ", " : "",
880 str_details1 ? str_details1 : "",
881 str_details2 ? ", " : "",
882 str_details2 ? str_details2 : "");
883
884 /*
885 * Print out information about the record and print the
886 * data at higher debug levels.
887 */
888 if (RDEBUG_ENABLED4) {
889 RHEXDUMP4(data, data_len, "(TLS) %s", tls_session->info.info_description);
890 } else {
891 RDEBUG2("(TLS) %s", tls_session->info.info_description);
892 }
893}
894
895/** Record the progression of the TLS handshake
896 *
897 * This callback is called by OpenSSL whenever a protocol message relating to a handshake is sent
898 * or received.
899 *
900 * This function copies state information from the various arguments into the state->info
901 * structure of the #fr_tls_session_t, to allow us to track the progression of the handshake.
902 *
903 * @param[in] write_p
904 * - 0 when a message has been received.
905 * - 1 when a message has been sent.
906 *
907 * @param[in] msg_version The TLS version negotiated, should be one of:
908 * - TLS1_VERSION
909 * - TLS1_1_VERSION
910 * - TLS1_2_VERSION
911 * - TLS1_3_VERSION
912 *
913 * @param[in] content_type One of the contentType values defined for TLS:
914 * - SSL3_RT_CHANGE_CIPHER_SPEC (20)
915 * - SSL3_RT_ALERT (21)
916 * - SSL3_RT_HANDSHAKE (22)
917 * - TLS1_RT_HEARTBEAT (24)
918 *
919 * @param[in] inbuf The raw protocol message.
920 * @param[in] len Length of the raw protocol message.
921 * @param[in] ssl The SSL session.
922 * @param[in] arg The #fr_tls_session_t holding the SSL session.
923 */
924void fr_tls_session_msg_cb(int write_p, int msg_version, int content_type,
925 void const *inbuf, size_t len,
926 SSL *ssl, void *arg)
927{
928 uint8_t const *buf = inbuf;
929 fr_tls_session_t *tls_session = talloc_get_type_abort(arg, fr_tls_session_t);
930 request_t *request = fr_tls_session_request(tls_session->ssl);
931
932 /*
933 * OpenSSL runs the message callback for every record it reads
934 * or writes, and every one of those happens while the library
935 * has the request bound. _fr_dtls_timer_expired() binds across
936 * DTLSv1_handle_timeout() for exactly this reason.
937 */
938 fr_assert(request != NULL);
939
940 /*
941 * Mostly to check for memory corruption...
942 */
943 if (!fr_cond_assert(tls_session->ssl == ssl)) {
944 REDEBUG("(TLS) fr_tls_session_t and ssl arg do not match in fr_tls_session_msg_cb");
945 fr_tls_session_error_alert(request, tls_session,
946 FR_ERROR_VALUE_SESSION_MISMATCH, SSL_AD_INTERNAL_ERROR);
947 tls_session->invalid = true;
948 return;
949 }
950
951 /*
952 * As per https://tools.ietf.org/html/rfc7568
953 *
954 * We explicitly disable SSLv2/v3, hence the asserts.
955 */
956#ifdef SSL2_VERSION
957 if (!fr_cond_assert(msg_version != SSL2_VERSION)) {
958 REDEBUG("(TLS) Invalid version (SSLv2) in handshake");
959 fr_tls_session_error_alert(request, tls_session,
960 FR_ERROR_VALUE_INVALID_VERSION_SSLV2, SSL_AD_PROTOCOL_VERSION);
961 tls_session->invalid = true;
962 return;
963 }
964#endif
965
966#ifdef SSL3_VERSION
967 if (!fr_cond_assert(msg_version != SSL3_VERSION)) {
968 REDEBUG("(TLS) Invalid version (SSLv3) in handshake");
969 fr_tls_session_error_alert(request, tls_session,
970 FR_ERROR_VALUE_INVALID_VERSION_SSLV3, SSL_AD_PROTOCOL_VERSION);
971 tls_session->invalid = true;
972 return;
973 }
974#endif
975
976 /*
977 * OpenSSL >= 1.0.2 calls this function with 'pseudo'
978 * content types. Which breaks our tracking of
979 * the SSL Session state.
980 */
981 if ((msg_version == 0) && (content_type > UINT8_MAX)) {
982 REDEBUG4("(TLS) Ignoring fr_tls_session_msg_cb call with pseudo content type %i, version %i",
983 content_type, msg_version);
984 return;
985 }
986
987 if ((write_p != 0) && (write_p != 1)) {
988 REDEBUG4("(TLS) Ignoring fr_tls_session_msg_cb call with invalid write_p %d", write_p);
989 return;
990 }
991
992 /*
993 * 0 - received (from peer)
994 * 1 - sending (to peer)
995 */
996 tls_session->info.origin = write_p;
997 tls_session->info.content_type = content_type;
998 tls_session->info.record_len = len;
999 tls_session->info.version = msg_version;
1000 tls_session->info.initialized = true;
1001
1002 switch (content_type) {
1003 case SSL3_RT_ALERT:
1004 if (len < 2) {
1005 invalid_alert:
1006 REDEBUG("(TLS) Invalid TLS Alert. Closing connection");
1007 fr_tls_session_error_alert(request, tls_session,
1008 FR_ERROR_VALUE_INVALID_RECORD_LENGTH, SSL_AD_DECODE_ERROR);
1009 tls_session->invalid = true;
1010 return;
1011 }
1012
1013 tls_session->info.alert_level = buf[0];
1014 tls_session->info.alert_description = buf[1];
1015 tls_session->info.handshake_type = 0x00;
1016 break;
1017
1018 case SSL3_RT_HANDSHAKE:
1019 if (!len) goto invalid_alert;
1020
1021 tls_session->info.handshake_type = buf[0];
1022 tls_session->info.alert_level = 0x00;
1023 tls_session->info.alert_description = 0x00;
1024 break;
1025
1026#ifdef SSL3_RT_HEARTBEAT
1027 case TLS1_RT_HEARTBEAT:
1028 uint8_t *p = buf;
1029
1030 if ((len >= 3) && (p[0] == 1)) {
1031 size_t payload_len;
1032
1033 payload_len = fr_nbo_to_uint16(p + 1);
1034 if ((payload_len + 3) > len) {
1035 tls_session->invalid = true;
1036 REDEBUG("(TLS) OpenSSL Heartbeat attack detected. Closing connection");
1037 fr_tls_session_error_alert(request, tls_session,
1038 FR_ERROR_VALUE_HEARTBEAT_OVERFLOW, SSL_AD_DECODE_ERROR);
1039 return;
1040 }
1041 }
1042 break;
1043#endif
1044 default:
1045 break;
1046 }
1047
1048 /*
1049 * Manually track when records are encrypted. Once records are encrypted, we can't manually
1050 * create a TLS alert. OpenSSL doesn't have an API to send alerts. And it doesn't have an API
1051 * to tell us when the records are encrypted.
1052 */
1053 switch (content_type) {
1054 /*
1055 * Everything after ChangeCipherSpec is encrypted.
1056 */
1057 case SSL3_RT_CHANGE_CIPHER_SPEC:
1058 if (write_p != TLS_INFO_ORIGIN_RECORD_SENT) break;
1059 if (tls_session->write_encrypted) break;
1060
1061 tls_session->write_encrypted = true;
1062 RDEBUG3("(TLS) The records we write are encrypted, from the ChangeCipherSpec onwards");
1063 break;
1064
1065 /*
1066 * For TLS 1.3, everything after ServerHello is encrypted. After ServerHello, SSL_version()
1067 * finally returns the correct TLS version number which was negotiated. Before ServerHello,
1068 * SSL_version() returns the highest allowed TLS version.
1069 */
1070 case SSL3_RT_HANDSHAKE:
1071 if (tls_session->info.handshake_type != SSL3_MT_SERVER_HELLO) break;
1072 if (SSL_version(ssl) < TLS1_3_VERSION) break;
1073
1074 /*
1075 * We allow one session ticket, which is fine for TLS 1.2. The tickets are sent in the
1076 * header. For TLS 1.3 and stateful session tickets, the server also sends the ticket
1077 * immediately. For EAP, the `store session` code is delayed until after the inner
1078 * methods succeed. So it doesn't matter if the peer has a ticket.
1079 */
1080 if (tls_session->conf->delayed_tickets && (tls_session->conf->cache.mode & FR_TLS_TICKET_STATELESS) &&
1081 SSL_is_server(ssl)) {
1082 RDEBUG3("(TLS) TLS 1.3 session ticket is delayed until inner tunnel indicates success");
1083 SSL_set_num_tickets(ssl, 0);
1084 }
1085
1086 if (tls_session->write_encrypted) break;
1087
1088 tls_session->write_encrypted = true;
1089 RDEBUG3("(TLS) The records we write are encrypted, from the ServerHello onwards");
1090 break;
1091
1092 default:
1093 break;
1094 }
1095
1096 session_msg_log(request, tls_session, (uint8_t const *)inbuf, len);
1097
1098#ifndef OPENSSL_NO_SSL_TRACE
1099 if (RDEBUG_ENABLED3) {
1100 SSL_trace(tls_session->info.origin,
1101 tls_session->info.version,
1102 tls_session->info.content_type,
1103 inbuf, len,
1104 ssl,
1105 request ?
1106 fr_tls_request_log_bio(request, L_DBG, L_DBG_LVL_3) :
1107 fr_tls_global_log_bio(L_DBG, L_DBG_LVL_3));
1108 }
1109#endif
1110}
1111
1112/*
1113 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1114 * material will be exported that you may use with Wireshark to decode any
1115 * TLS flows. Please see the following for more details:
1116 *
1117 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1118 *
1119 * An example logging session is (you should delete the file on each run):
1120 *
1121 * rm -f /tmp/sslkey.log; env SSLKEYLOGFILE=/tmp/sslkey.log freeradius -X | tee /tmp/debug
1122 *
1123 * Note that we rely on the OS to check file permissions. If the
1124 * caller can run radiusd, then they can only write to files which
1125 * they own. If radiusd is running as root, then only root can
1126 * change the environment variables for radiusd.
1127 */
1128void fr_tls_session_keylog_cb(const SSL *ssl, const char *line)
1129{
1130 int fd;
1131 size_t len;
1132 const char *filename;
1133 char buffer[64 + 2*SSL3_RANDOM_SIZE + 2*SSL_MAX_MASTER_KEY_LENGTH];
1134
1135 /*
1136 * Prefer the environment variable definition to the
1137 * configuration file. This allows for "one-shot"
1138 * dumping of EAP keys when you know you're not using
1139 * RadSec, and you don't want to edit the configuration.
1140 */
1141 filename = getenv("SSLKEYLOGFILE");
1142 if (!filename) {
1143 fr_tls_conf_t *conf;
1144
1145 conf = (fr_tls_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
1146 if (!conf || !conf->keylog_file || !*conf->keylog_file) return;
1147
1148 filename = conf->keylog_file;
1149 }
1150
1151 /*
1152 * We write all of the data at once. This is *generally*
1153 * atomic on most systems.
1154 */
1155 len = strlen(line);
1156 if ((len + 1) > sizeof(buffer)) {
1157 DEBUG("SSLKEYLOGFILE buffer not large enough, max %lu, required %lu", sizeof(buffer), len + 1);
1158 return;
1159 }
1160
1161 /*
1162 * Add a \n, which means that in order to write() the
1163 * buffer AND the trailing \n, we have to place both
1164 * strings into the same buffer.
1165 */
1166 memcpy(buffer, line, len);
1167 buffer[len] = '\n';
1168
1169 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1170 if (fd < 0) {
1171 DEBUG("Failed to open file %s: %s", filename, strerror(errno));
1172 return;
1173 }
1174
1175 /*
1176 * This should work in most situations.
1177 */
1178 if (write(fd, buffer, len + 1) < 0) {
1179 DEBUG("Failed to write to file %s: %s", filename, strerror(errno));
1180 }
1181
1182 close(fd);
1183}
1184
1185/** Check if all TLS initialization has finished.
1186 *
1187 * SSL_is_init_finished() tells us if the TLS handshake has finished,
1188 * which is enough for TLS 1.2, or TLS 1.3 without session tickets, or
1189 * TLS 1.3 with stateful session tickets.
1190 *
1191 * For TLS 1.3 with stateless session tickets, the ticket is sent
1192 * after the handshake has completed. The application gets to
1193 * send/recv its application data only after the handshake has
1194 * finished, the stateless session ticket has been received, and after
1195 * the `load session` has been run.
1196 *
1197 * Callers outside this library MUST use this function rather than
1198 * SSL_is_init_finished().
1199 *
1200 * @param[in] tls_session to check.
1201 * @return
1202 * - true if every part of the setup has finished.
1203 * - false if OpenSSL is still handshaking, or a `load session` which
1204 * the caller has to wait for has not run yet.
1205 */
1206bool fr_tls_session_is_init_finished(fr_tls_session_t *tls_session)
1207{
1208 fr_tls_conf_t *conf;
1209
1210 if (!SSL_is_init_finished(tls_session->ssl)) return false;
1211
1212 /*
1213 * TLS 1.3 servers send the session ticket (stateless or stateful) after the handshake has
1214 * finished. That adds a step after the TLS handshake is done. If we're not doing TLS 1.3, or
1215 * we're not using tickets, then we're done all negotiation, and application data can flow.
1216 */
1217 if (tls_session->info.version != TLS1_3_VERSION) return true;
1218
1219 if (!tls_session->cache) return true;
1220
1221 /*
1222 * If we're not a server, then we're a client, and we need to receive a ticket. For TLS 1.3, the
1223 * TLS handshake is finished, and then we might fet a ticket (stateless or stateful).
1224 *
1225 * We can either wait for a ticket (with timeout), or notice that receiption of application data
1226 * means that the ticket was sent (or not).
1227 */
1228 if (!SSL_is_server(tls_session->ssl)) {
1229 return tls_session->session_ticket_received || tls_session->seen_application_data;
1230 }
1231
1232 /*
1233 * A server which keeps its sessions in a cache has no
1234 * `load session` to wait for after the handshake.
1235 */
1236 conf = tls_session->conf;
1237 if (!(conf->cache.mode & FR_TLS_TICKET_STATELESS)) return true;
1238
1239 /*
1240 * Something has requested `load session`, but it hasn't
1241 * yet been run. Once that section completes, its state
1242 * returns to INIT. A session which never sends a ticket
1243 * is therefore also marked as finished.
1244 */
1245 return (tls_session->cache->load.state != FR_TLS_TICKET_STATEFUL_REQUESTED);
1246}
1247
1248/** Decrypt application data
1249 *
1250 * @note Handshake must have completed before this function may be called.
1251 *
1252 * Feed data from dirty_in to OpenSSL, and read the clean data into clean_out.
1253 *
1254 * @param[in] request The current #request_t.
1255 * @param[in] tls_session The current TLS session.
1256 * @return
1257 * - -1 on error.
1258 * - 1 if more fragments are required to fully reassemble the record for decryption.
1259 * - 0 if we decrypted a complete record.
1260 */
1261int fr_tls_session_recv(request_t *request, fr_tls_session_t *tls_session)
1262{
1263 int ret;
1264
1265 if (!tls_session->seen_application_data) {
1266 if (!SSL_is_init_finished(tls_session->ssl)) {
1267 REDEBUG("(TLS) Attempted to read application data before handshake completed");
1268 fr_tls_session_error_add(request, FR_ERROR_VALUE_APPLICATION_DATA_TOO_EARLY);
1269 return -1;
1270 }
1271
1272 /*
1273 * The handshake is over and we are about to move application
1274 * data, so we are past the point where a session ticket is
1275 * wanted. tls_ticket_stateful_store_cb() refuses any which arrive from
1276 * here on.
1277 */
1278 tls_session->seen_application_data = true;
1279 }
1280
1281 fr_tls_session_request_bind(tls_session->ssl, request);
1282
1283 /*
1284 * Nothing is copied here. The caller filled dirty_in, which
1285 * is into_ssl's producer cursor, so OpenSSL can already see
1286 * the record, and the BIO read callback reclaims the space as
1287 * OpenSSL consumes it.
1288 *
1289 * Init the clean_out buffer to store the decrypted data.
1290 */
1291 fr_tls_record_init(&tls_session->clean_out);
1292
1293 /*
1294 * Prevent spurious errors on the thread local error
1295 * stack causing SSL_get_error() to return SSL_ERROR_SSL
1296 * instead of what of the SSL_ERROR_WANT_* values.
1297 */
1298 ERR_clear_error();
1299
1300 /*
1301 * Read (and decrypt) the tunneled data from the
1302 * SSL session, and put it into the decrypted
1303 * data buffer.
1304 */
1305 ret = SSL_read(tls_session->ssl, fr_dbuff_current(&tls_session->clean_out),
1306 (int)fr_dbuff_remaining(&tls_session->clean_out));
1307 if (ret < 0) {
1308 int code;
1309
1310 code = SSL_get_error(tls_session->ssl, ret);
1311 switch (code) {
1312 case SSL_ERROR_WANT_READ:
1313 RWDEBUG("(TLS) Peer indicated record was complete, but OpenSSL returned SSL_WANT_READ. "
1314 "Attempting to continue");
1315 ret = 1;
1316 goto finish;
1317
1318 case SSL_ERROR_WANT_WRITE:
1319 REDEBUG("(TLS) Error in fragmentation logic: SSL_WANT_WRITE");
1320 error:
1321 ret = -1;
1322 goto finish;
1323
1324 case SSL_ERROR_NONE:
1325 RDEBUG2("(TLS) No application data received. Assuming handshake is continuing...");
1326 ret = 0;
1327 break;
1328
1329 default:
1330 REDEBUG("(TLS) Error in fragmentation logic");
1331 fr_tls_log_io_error(request, code, "SSL_read (%s)", __FUNCTION__);
1332 goto error;
1333 }
1334 }
1335
1336 /*
1337 * Passed all checks, successfully decrypted data
1338 */
1339 fr_dbuff_advance(&tls_session->clean_out, (size_t) ret);
1340 ret = 0;
1341
1342 if (RDEBUG_ENABLED3) {
1343 RHEXDUMP3(fr_dbuff_start(&tls_session->clean_out), fr_dbuff_used(&tls_session->clean_out),
1344 "(TLS) Decrypted TLS application data (%zu bytes)", fr_dbuff_used(&tls_session->clean_out));
1345 } else {
1346 RDEBUG2("(TLS) Decrypted TLS application data (%zu bytes)", fr_dbuff_used(&tls_session->clean_out));
1347 }
1348finish:
1349 fr_tls_session_request_unbind(tls_session->ssl);
1350
1351 return ret;
1352}
1353
1354/** Encrypt application data
1355 *
1356 * @note Handshake must have completed before this function may be called.
1357 *
1358 * Take cleartext data from clean_in, and feed it to OpenSSL, reading
1359 * the encrypted data into dirty_out.
1360 *
1361 * @param[in] request The current request.
1362 * @param[in] tls_session The current TLS session.
1363 * @return
1364 * - -1 on failure.
1365 * - 0 on success.
1366 */
1367int fr_tls_session_send(request_t *request, fr_tls_session_t *tls_session)
1368{
1369 int ret = 0;
1370
1371 if (!tls_session->seen_application_data) {
1372 if (!SSL_is_init_finished(tls_session->ssl)) {
1373 REDEBUG("(TLS) Attempted to write application data before handshake completed");
1374 fr_tls_session_error_add(request, FR_ERROR_VALUE_APPLICATION_DATA_TOO_EARLY);
1375 return -1;
1376 }
1377
1378 tls_session->seen_application_data = true;
1379 }
1380
1381 fr_tls_session_request_bind(tls_session->ssl, request);
1382
1383 /*
1384 * If there's un-encrypted data in 'clean_in', then write
1385 * that data to the SSL session, and then call the BIO function
1386 * to get that encrypted data from the SSL session, into
1387 * a buffer which we can then package into an EAP packet.
1388 *
1389 * Based on Server's logic this clean_in is expected to
1390 * contain the data to send to the client.
1391 */
1392 if (fr_dbuff_used(&tls_session->clean_in) > 0) {
1393 size_t used = fr_dbuff_used(&tls_session->clean_in);
1394
1395 /*
1396 * Ensure spurious errors aren't printed
1397 */
1398 ERR_clear_error();
1399
1400 if (RDEBUG_ENABLED3) {
1401 RHEXDUMP3(fr_dbuff_start(&tls_session->clean_in), used,
1402 "(TLS) TLS application data to encrypt (%zu bytes)", used);
1403 } else {
1404 RDEBUG2("(TLS) TLS application data to encrypt (%zu bytes)", used);
1405 }
1406
1407 ret = SSL_write(tls_session->ssl, fr_dbuff_start(&tls_session->clean_in), (int)used);
1408 if (ret < 0) {
1409 /*
1410 * ret<0 means that we have a real error.
1411 *
1412 * ret=0 means the "error" is SSL_WANT_READ, SSL_WANT_WRITE, etc.
1413 */
1414 ret = fr_tls_log_io_error(request, SSL_get_error(tls_session->ssl, ret),
1415 "SSL_write (%s)", __FUNCTION__);
1416 goto finish;
1417 }
1418
1419 /*
1420 * SSL_MODE_ENABLE_PARTIAL_WRITE is not set, so SSL_write()
1421 * either takes everything or fails. The whole buffer is
1422 * therefore consumed.
1423 */
1424 fr_assert((size_t) ret == used);
1425 fr_tls_record_init(&tls_session->clean_in);
1426
1427 /*
1428 * SSL_write() passes the encrypted record in
1429 * dirty_in to the BIO callback, which put the
1430 * output data into from_ssl. We're therefore
1431 * done here.
1432 */
1433 ret = 0;
1434 }
1435
1436finish:
1437 fr_tls_session_request_unbind(tls_session->ssl);
1438
1439 return ret;
1440}
1441
1442/** Create a TLS 1.3 session ticket after the handshake, and queue the ticket for the peer
1443 *
1444 * SSL_new_session_ticket() queues a ticket, and we need to call SSL_do_handshake() to put the ticket into
1445 * dirty_out for writing.
1446 *
1447 * The ticket creation callback runs inside SSL_do_handshake(). It cannot pause the request, so the caller
1448 * MUST have run `encode session` already, and set tls_session->app_data_ready.
1449 *
1450 * @param[in] request bound to the session for the duration of the call.
1451 * @param[in] tls_session to create a ticket for. Must be a server, and TLS 1.3.
1452 * @return
1453 * - 0 on success. tls_session->session_ticket_created says if OpenSSL made a ticket.
1454 * - -1 on failure.
1455 */
1456int fr_tls_session_ticket_write(request_t *request, fr_tls_session_t *tls_session)
1457{
1458 int ret;
1459
1460 fr_tls_session_request_bind(tls_session->ssl, request);
1461
1462 ERR_clear_error();
1463
1464 tls_session->session_ticket_created = false;
1465
1466 if (SSL_new_session_ticket(tls_session->ssl) != 1) {
1467 fr_tls_log_perror(request, "SSL_new_session_ticket failed");
1468 tls_session->app_data_ready = false;
1469 fr_tls_session_request_unbind(tls_session->ssl);
1470 return -1;
1471 }
1472
1473 ret = SSL_do_handshake(tls_session->ssl);
1474 if (ret <= 0) {
1475 ret = fr_tls_log_io_error(request, SSL_get_error(tls_session->ssl, ret),
1476 "SSL_do_handshake (%s)", __FUNCTION__);
1477 if (ret < 0) {
1478 tls_session->app_data_ready = false;
1479 fr_tls_session_request_unbind(tls_session->ssl);
1480 return -1;
1481 }
1482 }
1483
1484 /*
1485 * The app_data_set() callback clears the flag when the callback uses the data. If the flag is
1486 * still set, then OpenSSL did not create a ticket. Don't leave stale data for the next ticket.
1487 */
1488 if (tls_session->app_data_ready) {
1489 RWDEBUG("(TLS) OpenSSL did not create a session ticket");
1490 tls_session->app_data_ready = false;
1491 }
1492
1493 fr_tls_session_request_unbind(tls_session->ssl);
1494 return 0;
1495}
1496
1497/** Record a failure which FreeRADIUS itself decided
1498 *
1499 * The `session-state` list is where these go, because the list is what
1500 * survives from one round of the handshake to the next, and because it is
1501 * the list which `fail session { ... }` is given.
1502 *
1503 * Errors append rather than replace. A record which FreeRADIUS refuses
1504 * marks the session invalid, and the round after that reports
1505 * `Session-Invalid`. Replacing would keep that second report and lose the
1506 * refusal which caused it, which is the part an administrator needs.
1507 *
1508 * @param[in] request to add the error to. A NULL request is ignored,
1509 * because several OpenSSL callbacks run without one.
1510 * @param[in] error an `FR_ERROR_VALUE_*` value from
1511 * `protocol/tls/freeradius.h`, which is generated from
1512 * `share/dictionary/tls/dictionary.freeradius`.
1513 */
1514void fr_tls_session_error_add(request_t *request, uint32_t error)
1515{
1516 fr_pair_t *vp;
1517
1518 if (!request) return;
1519
1521 vp->vp_uint32 = error;
1522
1523 RDEBUG2("(TLS) request.%pP", vp);
1524}
1525
1526/** Instruct fr_tls_session_async_handshake to create a synthesised TLS alert record and send it to the peer
1527 *
1528 */
1529int fr_tls_session_alert(request_t *request, fr_tls_session_t *session, uint8_t level, uint8_t description)
1530{
1531 fr_assert(request != NULL);
1532
1533 if (session->alerts_sent > 3) return -1; /* Some kind of state machine brokenness */
1534
1535 /*
1536 * Ignore an alert which has lower severity than the one already queued.
1537 *
1538 * Two alerts of the same severity means the first one is the cause, and the second one is a
1539 * later round noticing the damage. fr_tls_session_msg_cb() refuses a record and marks the
1540 * session invalid, and the round after that reports FR_ERROR_VALUE_SESSION_INVALID. The first
1541 * alert says what was wrong with the record, which is the part the peer can act on.
1542 */
1543 if (session->pending_alert && (level <= session->pending_alert_level)) return 0;
1544
1545 /*
1546 * Check our internal flag is write_encrypted. We track that above in fr_tls_session_msg_cb()
1547 *
1548 * Refusing here rather than when the alert is sent lets the caller act on the refusal. A caller
1549 * which queued the alert in order to reject the session has to fail the session some other way,
1550 * and both callers outside this file already check the return code.
1551 *
1552 * The caller still records the error, so `fail session { ... }` reports the same reason either
1553 * way. However, the peer never gets the alert.
1554 */
1555 if (session->write_encrypted) {
1556 RDEBUG2("(TLS) Not sending TLS alert (%u), the records we write are already encrypted",
1557 description);
1558 return -1;
1559 }
1560
1561 session->pending_alert = true;
1562 session->pending_alert_level = level;
1563 session->pending_alert_description = description;
1564
1565 return 0;
1566}
1567
1568/** Record an application error, and queue a fatal TLS alert for sending to the peer.
1569 *
1570 * We can refuse handshakes for a number of policy or application-layer reasons. RFC9846 Section 6.2 says
1571 * that we should send an alert to the peer, and then close the connection.
1572 *
1573 * This function should be used only when OpenSSL hasn't already sent an alert of its own.
1574 *
1575 * fr_tls_session_alert_send() builds the alert by hand, and overwrites whatever OpenSSL left in
1576 * the buffer.
1577 *
1578 * @param[in] request to add the error to.
1579 * @param[in] session to queue the alert on.
1580 * @param[in] error an `FR_ERROR_VALUE_*` value, as for
1581 * fr_tls_session_error_add().
1582 * @param[in] description the TLS alert description to send. Pass
1583 * `SSL_AD_INTERNAL_ERROR` where no more specific
1584 * description fits the rule which was broken.
1585 */
1586void fr_tls_session_error_alert(request_t *request, fr_tls_session_t *session,
1587 uint32_t error, uint8_t description)
1588{
1589 fr_tls_session_error_add(request, error);
1590
1591 (void) fr_tls_session_alert(request, session, SSL3_AL_FATAL, description);
1592}
1593
1594static void fr_tls_session_alert_send(request_t *request, fr_tls_session_t *session)
1595{
1596 ssize_t slen;
1597
1598 /*
1599 * Update our internal view of the session
1600 */
1601 session->info.origin = TLS_INFO_ORIGIN_RECORD_SENT;
1602 session->info.content_type = SSL3_RT_ALERT;
1603 session->info.alert_level = session->pending_alert_level;
1604 session->info.alert_description = session->pending_alert_description;
1605
1606 /*
1607 * session_msg_log() reads both of these, and decodes the
1608 * level and the description only when the length says two.
1609 * Without them the log line reports whatever record came
1610 * before this one, which is not the alert it claims to
1611 * describe. They have to agree with the record written
1612 * below.
1613 */
1614 session->info.version = TLS1_VERSION;
1615 session->info.record_len = 2;
1616
1617 /*
1618 * Sending an alert over-rides whatever was in the
1619 * buffer, waiting to be sent. But for nearly all cases,
1620 * the from_ssl buffer will already be empty.
1621 *
1622 * The application then sees the data, and writes it to
1623 * the peer.
1624 */
1625 fr_tls_bio_dbuff_clear(session->from_ssl);
1626
1627 slen = fr_dbuff_in_bytes(fr_tls_bio_dbuff_in(session->from_ssl),
1628 (uint8_t) session->info.content_type,
1629 (uint8_t) 3, (uint8_t) 1, (uint8_t) 0, (uint8_t) 2,
1630 session->pending_alert_level,
1631 session->pending_alert_description);
1632 if (!fr_cond_assert(slen == 7)) return;
1633
1634 /*
1635 * We're building the TLS Alert manually, so we have to
1636 * manually record it here.
1637 */
1638 tls_session_alert_pair_add(request, request->reply_ctx, "reply.",
1639 session->pending_alert_level, session->pending_alert_description);
1640 fr_tls_session_error_add(request, FR_ERROR_VALUE_SENT_ALERT);
1641
1642 session->pending_alert = false;
1643 session->alerts_sent++;
1644
1645 SSL_clear(session->ssl); /* Reset the SSL *, to allow the client to restart the session */
1646
1647 session_msg_log(request, session, fr_dbuff_current(session->dirty_out),
1648 fr_dbuff_remaining(session->dirty_out));
1649}
1650
1651/** Tell the peer about any pending errors before we close the connection.
1652 *
1653 * A connection which is about to close sends a `close_notify`, as RFC 9846 Section 6.1 says "Each party MUST
1654 * send a 'close_notify' alert before closing its write side of the connection, unless it has already sent
1655 * some error alert." If there's an error alert, then don't send that.
1656 *
1657 * - A queued fatal alert is an error, and we send that. The end of a handshake round sends any queued alert.
1658 * An error path does not reach the end of the round, so the "send alert" function has to be called
1659 * explicitly.
1660 *
1661 * - if there's no queued alert, then we just send a close_notify alert. OpenSSL doesn't provide any other
1662 * way to send a TLS alert to the peer.
1663 *
1664 * - If the handshake doesn't finish, then we can't send anything. The connection is likely not TLS, and
1665 * sending a TLS alert won't help.
1666 *
1667 * The application should send the remaining data, and then close the socket.
1668 *
1669 * @param[in] request to log against, and to bind while OpenSSL runs.
1670 * @param[in] session which is closing.
1671 */
1672void fr_tls_session_close_send(request_t *request, fr_tls_session_t *session)
1673{
1674 bool bound;
1675 int ret;
1676
1677 if (session->pending_alert) {
1678 fr_tls_session_alert_send(request, session);
1679 return;
1680 }
1681
1682 /*
1683 * We've already sent an error alert, and RFC 9846 Section 6.1 says we don't need to follow that
1684 * with a close_notify. OpenSSL alerts don't need to be counted, because they result in
1685 * handshake failures, and this function is never reached.
1686 */
1687 if (session->alerts_sent > 0) return;
1688
1689 if (!SSL_is_init_finished(session->ssl)) return;
1690
1691 /*
1692 * Make this function idempotent. The source of the fatal error calls us, and we might be called
1693 * again after `fail sessions { ... }` finishes. The application doesn't know what's up, so it
1694 * calls us just to be safe.
1695 */
1696 if (SSL_get_shutdown(session->ssl) & SSL_SENT_SHUTDOWN) return;
1697
1698 /*
1699 * A failure inside of a handshake results in fr_tls_session_msg_cb() being called, and it needs
1700 * the request bound. Do that here, if it wasn't already done/
1701 */
1702 bound = (fr_tls_session_request(session->ssl) != NULL);
1703 if (!bound) fr_tls_session_request_bind(session->ssl, request);
1704
1705 /*
1706 * Disable the OpenSSL quiet shutdown, so it sends close_notify. We need to do that on our
1707 * timeframe, so the quiet shutdown is on by default.
1708 *
1709 * OpenSSL drops a session from its internal cache (and maybe ours) when a connection closes
1710 * without seeing a `close_notify`.
1711 */
1712 SSL_set_quiet_shutdown(session->ssl, 0);
1713
1714 ret = SSL_shutdown(session->ssl);
1715
1716 /*
1717 * We're non-blocking, so a zero return is "we sent it, but we didn't see a response from the
1718 * peer", which is expected.
1719 */
1720 if (ret < 0) {
1721 RDEBUG2("(TLS) Failed sending close_notify");
1722 ERR_clear_error();
1723 } else {
1724 RDEBUG2("(TLS) Sending close_notify");
1725 }
1726
1727 if (!bound) fr_tls_session_request_unbind(session->ssl);
1728}
1729
1730/** Push an `establish session { ... }` call into the current request, using a subrequest
1731 *
1732 * @param[in] request The current request.
1733 * @param[in] conf TLS configuration.
1734 * @param[in] tls_session The current TLS session.
1735 * @return
1736 * - UNLANG_ACTION_PUSHED_CHILD on success.
1737 * - UNLANG_ACTION_FAIL on failure.
1738 */
1739static inline CC_HINT(always_inline)
1740unlang_action_t tls_establish_session_push(request_t *request, fr_tls_conf_t *conf, fr_tls_session_t *tls_session)
1741{
1742 request_t *child;
1743 unlang_action_t ua;
1744
1745 fr_assert(conf->virtual_server);
1746
1747 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_establish_session->vb_uint32,
1748 &tls_session->session_id));
1749
1750 /*
1751 * Allocate a child, and set it up to call
1752 * the TLS virtual server.
1753 */
1754 ua = fr_tls_call_push(child, NULL, conf, tls_session, false);
1755 if (ua == UNLANG_ACTION_FAIL) {
1756 talloc_free(child);
1757 return UNLANG_ACTION_FAIL;
1758 }
1759
1760 return ua;
1761}
1762
1763/** Discard the session, once `fail session { ... }` has run
1764 *
1765 */
1766static unlang_action_t tls_session_fail_clear(request_t *request, void *uctx)
1767{
1768 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1769
1770 return fr_tls_ticket_stateful_clear_session(request, tls_session);
1771}
1772
1773/** Discard the session without clearing the cached session, once `fail session { ... }` has run
1774 *
1775 */
1776static unlang_action_t tls_session_fail_discard(request_t *request, void *uctx)
1777{
1778 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1779
1780 return fr_tls_ticket_stateful_discard_session(request, tls_session);
1781}
1782
1783/** The TLS session failed
1784 *
1785 * Runs `fail session { ... }`, so that the admin can run policies on
1786 * TLS connection failure. When `fail session { ... }` has finished, the function runs
1787 * `clear session { ... }` to remove any cached session information, unless `clear` is false.
1788 *
1789 * Pass `clear` as false after a network failure. A network failure is normal, and shouldn't invalidate the
1790 * session ticket.
1791 *
1792 * When `clear` is false, the function cancels any pending `store session { ... }`. A `clear session { ... }`
1793 * that was already queued for the loaded session still runs to invalidate the previous ticket, and any other
1794 * queued clear is dropped.
1795 *
1796 * A session can fail without session tickets, and a session ticket can be removed without a TLS
1797 * negotiation failure.
1798 *
1799 * @note The caller MUST set the caller's own unlang result before calling
1800 * `fr_tls_session_fail_session()`. A pushed child returns to the caller's
1801 * caller with that result already in place.
1802 *
1803 * @param[in] request to run the sections in.
1804 * @param[in] tls_session which failed.
1805 * @param[in] clear true to run `clear session { ... }` for a loaded session, false to
1806 * keep the cached session.
1807 * @return
1808 * - UNLANG_ACTION_PUSHED_CHILD - the sections are running.
1809 * - UNLANG_ACTION_CALCULATE_RESULT - there is no `fail session { ... }` section, and no
1810 * `clear session { ... }` was pushed.
1811 * - UNLANG_ACTION_FAIL - a frame could not be pushed.
1812 */
1813unlang_action_t fr_tls_session_fail_session(request_t *request, fr_tls_session_t *tls_session, bool clear)
1814{
1815 fr_tls_conf_t *conf = tls_session->conf;
1816 request_t *child;
1817 unlang_action_t ua;
1818 unlang_function_no_result_t finish = clear ? tls_session_fail_clear : tls_session_fail_discard;
1819
1820 if (!conf->fail_session) return finish(request, tls_session);
1821
1823 NULL,
1824 finish,
1825 NULL, FR_SIGNAL_NONE, UNLANG_SUB_FRAME, tls_session) == UNLANG_ACTION_FAIL) {
1826 return UNLANG_ACTION_FAIL;
1827 }
1828
1829 /*
1830 * A session which failed before OpenSSL established one has
1831 * no ID to report, which tls_subrequest_alloc() handles.
1832 */
1833 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_fail_session->vb_uint32,
1834 &tls_session->session_id));
1835
1836 ua = fr_tls_call_push(child, NULL, conf, tls_session, false);
1838 talloc_free(child);
1840 return UNLANG_ACTION_FAIL;
1841 }
1842
1843 return ua;
1844}
1845
1846/** Finish off a handshake round, possibly adding attributes to the request
1847 *
1848 */
1849static unlang_action_t tls_session_async_handshake_done_round(request_t *request, void *uctx)
1850{
1851 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
1852
1853 RDEBUG3("(TLS) entered state %s", __FUNCTION__);
1854
1855 /*
1856 * This only occurs once per session, where calling
1857 * SSL_read updates the state of the SSL session, setting
1858 * this flag to true.
1859 *
1860 * Callbacks provide enough info so we don't need to
1861 * print debug statements when the handshake is in other
1862 * states.
1863 */
1864 if (SSL_is_init_finished(tls_session->ssl)) {
1865 SSL_CIPHER const *cipher;
1866 fr_pair_t *vp;
1867 char const *version;
1868
1869 char cipher_desc[256], cipher_desc_clean[256];
1870 char *p = cipher_desc, *q = cipher_desc_clean;
1871
1872 /*
1873 * Check if ALPN was successfully negotiated. We
1874 * can do this without caring about session
1875 * tickets.
1876 */
1877 if (fr_tls_session_alpn_check(request, tls_session) < 0) {
1878 fr_tls_session_close_send(request, tls_session);
1879 tls_session->result = FR_TLS_RESULT_ERROR;
1880 fr_tls_session_request_unbind(tls_session->ssl);
1882 }
1883
1884 cipher = SSL_get_current_cipher(tls_session->ssl);
1885 SSL_CIPHER_description(cipher, cipher_desc, sizeof(cipher_desc));
1886
1887 /*
1888 * Cleanup the output from OpenSSL
1889 * Seems to print info in a tabular format.
1890 */
1891 while (*p != '\0') {
1892 if (isspace((uint8_t) *p)) {
1893 p++;
1894 continue;
1895 }
1896 *q++ = *p++;
1897 }
1898 *q = '\0';
1899
1900 RDEBUG2("(TLS) Cipher suite: %s", cipher_desc_clean);
1901
1902 RDEBUG2("(TLS) Adding TLS session information to request");
1903 RINDENT();
1905 fr_pair_value_strdup(vp, SSL_CIPHER_get_name(cipher), false);
1906 RDEBUG2("(TLS) session-state.%pP", vp);
1907
1908 if (((size_t)tls_session->info.version >= NUM_ELEMENTS(tls_version_str)) ||
1909 !tls_version_str[tls_session->info.version]) {
1910 version = "UNKNOWN";
1911 } else {
1912 version = tls_version_str[tls_session->info.version];
1913 }
1914
1916 fr_pair_value_strdup(vp, version, false);
1917 RDEBUG2("(TLS) session-state.%pP", vp);
1918 REXDENT();
1919
1920 /*
1921 * Cache the SSL_SESSION pointer.
1922 *
1923 * Which contains all the data we need for session resumption.
1924 */
1925 if (!tls_session->session) {
1926 tls_session->session = SSL_get_session(tls_session->ssl);
1927 if (tls_session->session) {
1928 tls_session_id_cache(tls_session, tls_session->session);
1929 }
1930 if (!tls_session->session) {
1931 REDEBUG("(TLS) Failed getting TLS session");
1932 fr_tls_session_error_alert(request, tls_session,
1933 FR_ERROR_VALUE_SESSION_MISSING, SSL_AD_INTERNAL_ERROR);
1934 fr_tls_session_close_send(request, tls_session);
1935 tls_session->result = FR_TLS_RESULT_ERROR;
1936 fr_tls_session_request_unbind(tls_session->ssl);
1938 }
1939 }
1940
1941 if (RDEBUG_ENABLED3) {
1942 if (SSL_SESSION_print(fr_tls_request_log_bio(request, L_DBG, L_DBG_LVL_3),
1943 tls_session->session) != 1) {
1944 RDEBUG3("(TLS) Failed retrieving session data");
1945 }
1946 }
1947
1948 /*
1949 * Session was resumed, add attribute to mark it as such.
1950 */
1951 if (SSL_session_reused(tls_session->ssl)) {
1952 /*
1953 * Mark the request as resumed.
1954 */
1956 vp->vp_bool = true;
1957 }
1958 }
1959
1960 /*
1961 * Get the data to send to the peer. OpenSSL already did
1962 * it's magic, so we can just do ours here.
1963 *
1964 * No encrypted data means that OpenSSL produced no TLS
1965 * content, so the output is likely application data.
1966 */
1967 if (fr_dbuff_remaining(tls_session->dirty_out) == 0) {
1968 /* Its clean application data, do whatever we want */
1969 fr_tls_record_init(&tls_session->clean_out);
1970 }
1971
1972 /*
1973 * Trash the current data in dirty_out, and synthesize
1974 * a TLS error record.
1975 *
1976 * OpensSL annoyingly provides no mechanism for us to
1977 * send alerts, and we need to send alerts as part of
1978 * RFC 5216, so this is our only option.
1979 */
1980 if (tls_session->pending_alert) fr_tls_session_alert_send(request, tls_session);
1981
1982 tls_session->result = FR_TLS_RESULT_SUCCESS;
1983 fr_tls_session_request_unbind(tls_session->ssl);
1984 if (SSL_is_init_finished(tls_session->ssl)) {
1985 fr_tls_conf_t *conf = tls_session->conf;
1986 if (conf->establish_session) return tls_establish_session_push(request, conf, tls_session);
1987 }
1989}
1990
1991/** Try very hard to get the SSL * into a consistent state where it's not yielded
1992 *
1993 * ...because if it's yielded, we'll probably leak thread contexts and all kinds of memory.
1994 *
1995 * @param[in] request being cancelled.
1996 * @param[in] action we're being signalled with.
1997 * @param[in] uctx the SSL * to cancel.
1998 */
1999static void tls_session_async_handshake_signal(UNUSED request_t *request, UNUSED fr_signal_t action, void *uctx)
2000{
2001 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
2002 int ret;
2003
2004 /*
2005 * We might want to set can_pause = false here
2006 * but that would trigger asserts in the
2007 * cache code.
2008 */
2009
2010 /*
2011 * If SSL_get_error returns SSL_ERROR_WANT_ASYNC
2012 * it means we're yielded in the middle of a
2013 * callback.
2014 *
2015 * Keep calling SSL_read() in a loop until we
2016 * no longer get SSL_ERROR_WANT_ASYNC, then
2017 * shut it down so it's in a consistent state.
2018 *
2019 * It'll get freed later when the request is
2020 * freed.
2021 */
2022 for (ret = tls_session->last_ret;
2023 SSL_get_error(tls_session->ssl, ret) == SSL_ERROR_WANT_ASYNC;
2024 ret = SSL_read(tls_session->ssl, fr_dbuff_current(&tls_session->clean_out),
2025 (int)fr_dbuff_remaining(&tls_session->clean_out)));
2026
2027 /*
2028 * Unbind the cancelled request from the SSL *
2029 */
2030 fr_tls_session_request_unbind(tls_session->ssl);
2031}
2032
2033/** Push the section for one bit of #fr_tls_pending_t
2034 *
2035 */
2036typedef unlang_action_t (*fr_tls_pending_push_t)(request_t *request, fr_tls_session_t *tls_session);
2037
2038/** Push the section for every raised bit, then call SSL_read() to continue the TLS state machine
2039 *
2040 * This function may be called multiple times, once after every asynchronous request.
2041 *
2042 * @param[in] request The current request.
2043 * @param[in] uctx #fr_tls_session_t to continue.
2044 * @return
2045 * - UNLANG_ACTION_CALCULATE_RESULT - We're done with this round, or a push
2046 * failed and `tls_session->result` is FR_TLS_RESULT_ERROR.
2047 * - UNLANG_ACTION_PUSHED_CHILD - Need to perform more asynchronous actions.
2048 */
2049static unlang_action_t tls_session_async_handshake_cont(request_t *request, void *uctx)
2050{
2051 /*
2052 * `push[idx]` is the push function for bit `1 << idx`.
2053 */
2054 static fr_tls_pending_push_t const push[] = {
2055 [0] = fr_tls_ticket_stateful_pending_push, /* FR_TLS_PENDING_STATEFUL_TICKET */
2056 [1] = fr_tls_ticket_stateless_pending_push, /* FR_TLS_PENDING_STATELESS_TICKET */
2057#ifdef PSK_MAX_IDENTITY_LEN
2058 [2] = fr_tls_session_psk_pending_push, /* FR_TLS_PENDING_PSK */
2059#endif
2060 [3] = fr_tls_verify_cert_pending_push, /* FR_TLS_PENDING_VERIFY */
2061 };
2062 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
2063 int err;
2064
2065 RDEBUG3("(TLS) (re-)entered state %s", __FUNCTION__);
2066
2067again:
2068 while (tls_session->pending) {
2069 unsigned int idx = fr_low_bit_pos(tls_session->pending) - 1;
2070 fr_tls_pending_t bit = (fr_tls_pending_t)(UINT64_C(1) << idx);
2071 unlang_action_t ua;
2072
2073 fr_assert(idx < NUM_ELEMENTS(push));
2074 fr_assert(push[idx]);
2075
2076 /*
2077 * Each bit is one shot. The push function is called once for
2078 * the bit, and raises the bit again if the callback asks again.
2079 */
2080 TLS_PENDING_CLEAR(tls_session, bit);
2081
2082 /*
2083 * Set this function as the repeat function before the push, so
2084 * the frame runs this function again once the section has finished.
2085 */
2086 if (unlikely(unlang_function_repeat_set(request, tls_session_async_handshake_cont) < 0)) {
2087 error:
2088 tls_session->result = FR_TLS_RESULT_ERROR;
2089 goto finish;
2090 }
2091
2092 ua = push[idx](request, tls_session);
2093 switch (ua) {
2095 return ua;
2096
2097 case UNLANG_ACTION_FAIL:
2099 goto error;
2100
2101 default:
2102 /*
2103 * The push function found nothing to push for this bit, so the
2104 * repeat function never runs. Clear the repeat function and
2105 * service the next raised bit.
2106 */
2108 break;
2109 }
2110 }
2111
2112 /*
2113 * Clear OpenSSL's thread local error stack.
2114 *
2115 * Why do we need to do this here? Although SSL_get_error
2116 * takes an `SSL *`, which would make you _think_ it was
2117 * operating on the error stack for that SSL, it will also
2118 * return SSL_ERROR_SSL if there are any errors in the stack.
2119 *
2120 * When operating normally SSL_read() can return < 0, to
2121 * indicate it wants more data, or that we need to perform
2122 * asynchronous processing.
2123 *
2124 * If spurious errors have been left on the thread local
2125 * stack they MUST be cleared before we can call SSL_get_error
2126 * otherwise stale errors mask the async notifications
2127 * and cause random handshake failures.
2128 */
2129 ERR_clear_error();
2130
2131 /*
2132 * Magic/More magic? Although SSL_read is normally
2133 * used to read application data, it will also
2134 * continue the TLS handshake. Removing this call will
2135 * cause the handshake to fail.
2136 *
2137 * We don't ever expect to actually *receive* application
2138 * data here.
2139 *
2140 * The reason why we call SSL_read instead of SSL_accept,
2141 * or SSL_connect, as it allows this function
2142 * to be used, irrespective or whether we're acting
2143 * as a client or a server.
2144 *
2145 * If acting as a client SSL_set_connect_state must have
2146 * been called before this function.
2147 *
2148 * If acting as a server SSL_set_accept_state must have
2149 * been called before this function.
2150 */
2151 tls_session->can_pause = true;
2152 tls_session->last_ret = SSL_read(tls_session->ssl, fr_dbuff_current(&tls_session->clean_out),
2153 (int)fr_dbuff_remaining(&tls_session->clean_out));
2154 tls_session->can_pause = false;
2155 if (tls_session->last_ret > 0) {
2156 fr_dbuff_advance(&tls_session->clean_out, (size_t) tls_session->last_ret);
2157
2158 /*
2159 * We've received application data. This is a positive
2160 * signal that all session tickets have been sent.
2161 * This flag informs fr_tls_session_is_init_finished().
2162 */
2163 tls_session->seen_application_data = true;
2164
2165 /*
2166 * Round successful, and we don't need to do any
2167 * further processing.
2168 */
2169 tls_session->result = FR_TLS_RESULT_SUCCESS;
2170 finish:
2171 /*
2172 * Was bound by caller
2173 */
2174 fr_tls_session_request_unbind(tls_session->ssl);
2176 }
2177
2178 /*
2179 * Bug in OpenSSL 3.0 - Normal handshaking behaviour
2180 * results in spurious "BIO_R_UNSUPPORTED_METHOD"
2181 * errors.
2182 *
2183 * SSL_get_error apparently returns SSL_ERROR_SSL if
2184 * there are any errors in the stack. This masks
2185 * SSL_ERROR_WANT_ASYNC and causes the handshake to
2186 * fail.
2187 *
2188 * Note: We may want some version of this code
2189 * included for all OpenSSL versions.
2190 *
2191 * It would call ERR_GET_FATAL() on each of the errors
2192 * in the stack to drain non-fatal errors and prevent
2193 * the other (more useful) return codes of
2194 * SSL_get_error from being masked. I guess we'll see
2195 * if this occurs in other scenarios.
2196 */
2197 if (SSL_get_error(tls_session->ssl, tls_session->last_ret) == SSL_ERROR_SSL) {
2198 unsigned long ssl_err;
2199
2200 /*
2201 * Some weird OpenSSL thing marking ERR_GET_REASON
2202 * as "unused". Unclear why this is done as it's
2203 * not deprecated.
2204 */
2205DIAG_OFF_OPTIONAL(used-but-marked-unused)
2206 while ((ssl_err = ERR_peek_error()) && (ERR_GET_REASON(ssl_err) == BIO_R_UNSUPPORTED_METHOD)) {
2207 (void) ERR_get_error();
2208 }
2209DIAG_ON_OPTIONAL(used-but-marked-unused)
2210 }
2211
2212 /*
2213 * Deal with asynchronous requests from OpenSSL.
2214 * These aren't actually errors, they're the
2215 * result of one of our callbacks indicating that
2216 * it'd like to perform the operation
2217 * asynchronously.
2218 */
2219 switch (err = SSL_get_error(tls_session->ssl, tls_session->last_ret)) {
2220 case SSL_ERROR_WANT_ASYNC: /* Certification validation or cache loads */
2221 goto again;
2222
2223 case SSL_ERROR_WANT_ASYNC_JOB:
2224 REDEBUG("(TLS) No async jobs available in pool, increase thread.openssl_async_pool_max");
2225 goto error;
2226
2227 default:
2228 /*
2229 * Returns 0 if we can continue processing the handshake
2230 * Returns -1 if we encountered a fatal error.
2231 */
2232 if (fr_tls_log_io_error(request,
2233 err, "SSL_read (%s)", __FUNCTION__) < 0) goto error;
2234 return tls_session_async_handshake_done_round(request, uctx);
2235 }
2236}
2237
2238/** Ingest data for another handshake round
2239 *
2240 * Advance the TLS handshake by feeding OpenSSL data from dirty_in,
2241 * and reading data from OpenSSL into dirty_out.
2242 *
2243 * Calls #tls_session_async_handshake_read to perform the actual ingestion.
2244 * #tls_session_async_handshake_read is split out because we may need to call
2245 * it multiple times, once after every async action.
2246 *
2247 * @param[in] request The current request.
2248 * @param[in] uctx #fr_tls_session_t to continue.
2249 * @return
2250 * - UNLANG_ACTION_CALCULATE_RESULT - We're done with this round.
2251 * - UNLANG_ACTION_PUSHED_CHILD - Need to perform more asynchronous actions.
2252 */
2253static unlang_action_t tls_session_handshake_round(request_t *request, void *uctx)
2254{
2255 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
2256
2257 RDEBUG3("(TLS) entered state %s", __FUNCTION__);
2258
2259 tls_session->result = FR_TLS_RESULT_IN_PROGRESS;
2260
2261 fr_tls_session_request_bind(tls_session->ssl, request); /* May be unbound in this function or asynchronously */
2262
2263 /*
2264 * This is a logic error.
2265 * fr_tls_session_async_handshake() must not be called if
2266 * the handshake is complete. fr_tls_session_recv() must
2267 * be called instead.
2268 *
2269 * Check for full init finished, including TLS 1.3 for a
2270 * client with stateless session tickets. Those arrive
2271 * after the TLS handshake has completed.
2272 */
2273 if (fr_tls_session_is_init_finished(tls_session)) {
2274 REDEBUG("(TLS) Attempted to continue TLS handshake, but handshake has completed");
2275 fr_tls_session_error_alert(request, tls_session,
2276 FR_ERROR_VALUE_HANDSHAKE_FINISHED, SSL_AD_INTERNAL_ERROR);
2277 error:
2278 /*
2279 * The end of the round is what normally sends a
2280 * queued alert, and this exit does not reach it.
2281 * EAP has no other chance: it pushes the handshake
2282 * itself and never touches fr_tls_connection_t.
2283 */
2284 fr_tls_session_close_send(request, tls_session);
2285 tls_session->result = FR_TLS_RESULT_ERROR;
2286 fr_tls_session_request_unbind(tls_session->ssl); /* Was bound in this function */
2288 }
2289
2290 if (tls_session->invalid) {
2291 REDEBUG("(TLS) Preventing invalid session from continuing");
2292 fr_tls_session_error_alert(request, tls_session,
2293 FR_ERROR_VALUE_SESSION_INVALID, SSL_AD_INTERNAL_ERROR);
2294 goto error;
2295 }
2296
2297 /*
2298 * This function runs once per handshake message, and
2299 * only for handshake messages. Count the number of
2300 * handshakes, and if there are too many, then forcibly
2301 * stop the TLS negotiation.
2302 */
2303 tls_session->rounds++;
2304 if (tls_session->rounds >= FR_TLS_MAX_ROUNDS) {
2305 REDEBUG("(TLS) Failing TLS session due to too many handshake rounds (limit %u)", FR_TLS_MAX_ROUNDS);
2306 fr_tls_session_error_alert(request, tls_session,
2307 FR_ERROR_VALUE_TOO_MANY_ROUNDS, SSL_AD_INTERNAL_ERROR);
2308 goto error;
2309 }
2310
2311 /*
2312 * OpenSSL does it's magic to convert the encrypted
2313 * dirty_in data to clean_out application data.
2314 */
2315 return tls_session_async_handshake_cont(request, uctx); /* Must unbind request, possibly asynchronously */
2316}
2317
2318/** Ask the virtual server for a session to resume, then start the handshake
2319 *
2320 * Only a client picks a session to resume before the handshake starts. A
2321 * server is asked for one by OpenSSL, part way through the handshake, and
2322 * answers from tls_ticket_stateful_load_cb().
2323 *
2324 * Nothing is bound to the SSL * yet, so the `load session { ... }` section can
2325 * push a subrequest without the handshake holding the SSL * while that
2326 * subrequest runs.
2327 */
2328static unlang_action_t tls_session_load_session(request_t *request, void *uctx)
2329{
2330 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
2331 unlang_action_t ua;
2332
2333 /*
2334 * The round runs next whatever happens here, so arm it before
2335 * pushing anything. unlang_function_repeat_set() only works
2336 * while this frame is on top of the stack, and pushing a child
2337 * puts another frame on top.
2338 */
2339 if (unlikely(unlang_function_repeat_set(request, tls_session_handshake_round) < 0)) {
2340 error:
2341 tls_session->result = FR_TLS_RESULT_ERROR;
2343 }
2344
2345 ua = fr_tls_ticket_stateful_load_client_push(request, tls_session);
2346 if (ua == UNLANG_ACTION_PUSHED_CHILD) return ua;
2347 if (ua == UNLANG_ACTION_FAIL) goto error;
2348
2349 return tls_session_handshake_round(request, uctx);
2350}
2351
2352/** Run one round of the handshake, and the policy which precedes the first one
2353 *
2354 * The caller pushes this once per round, so the work which happens only once
2355 * is keyed off the round counter. `tls_session->rounds` is incremented by
2356 * tls_session_handshake_round(), so a zero here means no round has run yet.
2357 *
2358 * `new session { ... }` is not run here. That section runs before the
2359 * session is allocated, so that it can set control attributes which decide how
2360 * the session is allocated, such as EAP-TLS-Require-Client-Cert. By the time
2361 * a handshake round runs, the session already exists and the decision has been
2362 * taken. Running `new session { ... }` from here would be too late, so the
2363 * caller runs it.
2364 */
2365static unlang_action_t tls_session_async_handshake(request_t *request, void *uctx)
2366{
2367 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
2368
2369 if ((tls_session->rounds == 0) && !SSL_is_server(tls_session->ssl)) {
2370 return tls_session_load_session(request, uctx);
2371 }
2372
2373 return tls_session_handshake_round(request, uctx);
2374}
2375
2376/** Push a handshake call onto the stack
2377 *
2378 * We push the handshake frame (as opposed to having the caller do it),
2379 * so that we guarantee there's a frame that the handshake function can
2380 * manipulate to manage its own state.
2381 *
2382 * The result of processing this handshake round can be found in
2383 * tls_session->result.
2384 *
2385 * @param[in] request The current request.
2386 * @param[in] tls_session to continue handshaking.
2387 * @return
2388 * - UNLANG_ACTION_PUSHED_CHILD on success.
2389 * - UNLANG_ACTION_FAIL on failure.
2390 */
2391unlang_action_t fr_tls_session_async_handshake_push(request_t *request, fr_tls_session_t *tls_session)
2392{
2394 tls_session_async_handshake,
2395 NULL,
2396 tls_session_async_handshake_signal,
2399 tls_session);
2400}
2401
2402/** Will this handshake negotiate TLS 1.3?
2403 *
2404 * OpenSSL decides whether to send a TLS 1.2 ticket while it parses the ClientHello extensions, so which means that
2405 * we need to check TLS versions in the ClientHello callback.
2406 *
2407 * TLS 1.3 is negotiated when the server allows it, and the client lists it in `supported_versions`.
2408 * A client which sends no `supported_versions` extension (including a DTLS client) cannot do TLS 1.3.
2409 *
2410 * @param[in] ssl to check. Must be inside the ClientHello callback.
2411 * @return
2412 * - true if TLS 1.3 will be negotiated.
2413 * - false if TLS 1.2 or earlier will be negotiated.
2414 */
2415static bool tls_client_hello_is_tls13(SSL *ssl)
2416{
2417 uint8_t const *p;
2418 size_t len, i;
2419 int max = SSL_get_max_proto_version(ssl);
2420
2421 if (max && (max < TLS1_3_VERSION)) return false;
2422
2423 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_supported_versions, &p, &len) != 1) return false;
2424
2425 /*
2426 * One byte of length, then a list of two byte versions.
2427 */
2428 if ((len < 1) || (p[0] != (len - 1)) || (p[0] & 0x01)) return false;
2429
2430 for (i = 1; (i + 1) < len; i += 2) {
2431 if (((p[i] << 8) | p[i + 1]) == TLS1_3_VERSION) return true;
2432 }
2433
2434 return false;
2435}
2436
2437/** Callback run during ClientHello phase of handshake
2438 *
2439 * The SSL_client_hello* functions can only be called inside a callback like this.
2440 */
2441static int fr_tls_session_client_hello_cb(SSL *ssl, UNUSED int *al, UNUSED void *arg)
2442{
2443 fr_tls_conf_t *conf = fr_tls_session_conf(ssl);
2444 request_t *request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
2445
2446 /*
2447 * Is there a session_ticket extension in the CLientHello?
2448 * If not, the client doesn't support session tickets - disable them.
2449 */
2450 if (conf->cache.mode != FR_TLS_TICKET_DISABLED) {
2451 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_session_ticket, NULL, NULL) == 1) {
2452 fr_tls_session_t *tls_session = fr_tls_session(ssl);
2453 tls_session->session_ticket_supported = true;
2454 } else {
2455 long ssl_options = SSL_get_options(ssl);
2456 RDEBUG2("(TLS) Session ticket extension not found in ClientHello");
2457 ssl_options |= SSL_OP_NO_TICKET;
2458 SSL_set_options(ssl, ssl_options);
2459 }
2460
2461 /*
2462 * Session tickets interact badly with EAP. For EAP success, we would ideally like to
2463 * send the session ticket only after the inner-tunnel method has succeeded. However,
2464 * for TLS 1.2, the ticket it sent during the TLS handshake, and there's nothing we can
2465 * do about that.
2466 *
2467 * So for stateful session tickets, we just skip `store session` if the inner-tunnel
2468 * method fails. The peer has a ticket (with an ID), but the server doesn't know about
2469 * that ticket or ID, and therefore won't be able to resume the session.
2470 *
2471 * For TLS 1.3, the session ticket is sent after the TLS handshake has completed.
2472 * HOWEVER, the EAP peers will process that ticket only in a few situations. (1) if they
2473 * see the ticket immediately after the TLS handshake has completed, or (2) when the
2474 * ticket arrives with expected inner-tunnel "success" from the server.
2475 *
2476 * i.e. for EAP-TLS, with the inner "protected success indicator". For PEAP / TTLS /
2477 * FAST, with the MS-CHAPv2 success. For TTLS with PAP or CHAP, you're out of luck.
2478 *
2479 * Overall, stateless session tickets are poorly tested with TLS 1.2, even outside of EAP.
2480 *
2481 * All of the above menas that We allow stateless session tickets only when TLS 1.3 has
2482 * been negotiated. In `auto` mode with TLS 1.2, we forced the use of stateful session
2483 * IDs.
2484 */
2485 if ((conf->cache.mode & FR_TLS_TICKET_STATELESS) && !tls_client_hello_is_tls13(ssl)) {
2486 fr_tls_session_t *tls_session = fr_tls_session(ssl);
2487
2488 RDEBUG2("(TLS) Stateless session tickets are not used below TLS 1.3");
2489 SSL_set_options(ssl, SSL_get_options(ssl) | SSL_OP_NO_TICKET);
2490 tls_session->session_ticket_supported = false;
2491 }
2492 }
2493
2494 if (conf->client_hello_parse) return fr_tls_session_client_hello_pairs(ssl, request);
2495
2496 return SSL_CLIENT_HELLO_SUCCESS;
2497}
2498
2499/** Free a TLS session and any associated OpenSSL data
2500 *
2501 * @param session to free.
2502 * @return 0.
2503 */
2504static int _fr_tls_session_free(fr_tls_session_t *session)
2505{
2506 /*
2507 * The application can free the session before the cache operations have run, for example
2508 * when the peer closes the connection after the handshake has finished. A cache section
2509 * needs a request to run, and no request exists in this destructor. Because of that, the
2510 * cache destructor discards the queued operations.
2511 *
2512 * A discarded `store session` is harmless. Because the server never stored the session
2513 * that the ticket names, the peer cannot resume from the ticket. A discarded
2514 * `clear session` leaves the session in the cache until the cache entry for the session
2515 * expires.
2516 */
2517 if (fr_tls_ticket_stateful_pending(session->cache)) {
2518 DEBUG2("(TLS) Discarding queued cache operations, session freed before the operations ran");
2519 }
2520
2521 if (session->ssl) {
2522 /*
2523 * The OpenSSL docs state:
2524 *
2525 * SSL_shutdown() can be modified to only set the
2526 * connection to "shutdown" state but not actually
2527 * send the "close notify" alert messages, see
2528 * SSL_CTX_set_quiet_shutdown(3). When "quiet shutdown"
2529 * is enabled, SSL_shutdown() will always succeed and
2530 * return 1.
2531 *
2532 * This is only partially correct. This call does mean
2533 * we don't notify the other party, but the SSL_shutdown
2534 * call can still fail if the handshake hasn't begun, leaving
2535 * spurious errors on the thread local error stack.
2536 */
2537 SSL_set_quiet_shutdown(session->ssl, 1);
2538
2539 /*
2540 * Don't leave spurious errors raised by SSL_shutdown on
2541 * the error stack.
2542 */
2543 if (SSL_shutdown(session->ssl) != 1) ERR_clear_error();
2544
2545 SSL_free(session->ssl);
2546 session->ssl = NULL;
2547 }
2548
2549 return 0;
2550}
2551
2552/** Allocate a new #fr_tls_session_t and initialize it
2553 *
2554 */
2555static fr_tls_session_t *tls_session_alloc(TALLOC_CTX *ctx, request_t *request, SSL_CTX *ssl_ctx)
2556{
2557 fr_tls_session_t *tls_session;
2558
2559 tls_session = talloc_zero(ctx, fr_tls_session_t);
2560 if (!tls_session) return NULL;
2561
2562 /*
2563 * The socket type determines whether we use TLS or DTLS.
2564 */
2565 tls_session->socket_type = (SSL_CTX_get_ssl_method(ssl_ctx) == DTLS_method()) ? SOCK_DGRAM : SOCK_STREAM;
2566
2567 /*
2568 * Cache the configuration pointer, because it doesn't change during the lifetime of the
2569 * connection.
2570 */
2571 tls_session->conf = fr_tls_ctx_conf(ssl_ctx);
2572
2573 tls_session->ctx = ssl_ctx;
2574
2575 tls_session->ssl = SSL_new(ssl_ctx);
2576 if (!tls_session->ssl) {
2577 talloc_free(tls_session);
2578 fr_tls_log_perror(request, "Error creating new TLS session");
2579 return NULL;
2580 }
2581
2582 talloc_set_destructor(tls_session, _fr_tls_session_free);
2583 fr_pair_list_init(&tls_session->extra_pairs);
2584
2585 fr_value_box_init_null(&tls_session->session_id);
2586
2587 /*
2588 * Add the message callback to identify what type of
2589 * message/handshake is passed
2590 */
2591 SSL_set_msg_callback(tls_session->ssl, fr_tls_session_msg_cb);
2592 SSL_set_msg_callback_arg(tls_session->ssl, tls_session);
2593 SSL_set_info_callback(tls_session->ssl, fr_tls_session_info_cb);
2594
2595 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_TLS_SESSION, (void *)tls_session);
2596
2597 /*
2598 * The client sometimes doesn't have a request. The server always has one.
2599 */
2600 if (!request) return tls_session;
2601
2602 /*
2603 * If there is a request, then allocate the record buffers and
2604 * create the BIOs. Otherwise leave the record buffers empty and
2605 * the bio handles as NULL, so that any accidental use
2606 * causes problems.
2607 */
2608 {
2609 fr_dbuff_t *record[] = { &tls_session->clean_in, &tls_session->clean_out };
2610 size_t i;
2611
2612 for (i = 0; i < NUM_ELEMENTS(record); i++) {
2613 uint8_t *buff;
2614
2615 MEM(buff = talloc_array(tls_session, uint8_t, SSL3_RT_MAX_PLAIN_LENGTH));
2616 fr_dbuff_init(record[i], buff, (size_t) SSL3_RT_MAX_PLAIN_LENGTH);
2617 }
2618 }
2619
2620 /*
2621 * Create & hook the BIOs to handle the dirty side of the
2622 * SSL. The caller writes encrypted data into the
2623 * "dirty_in" side via some external mechanism, and
2624 * writes encrypted data out of the "dirty_out" side via
2625 * an external mechanism. This design allows us to use
2626 * the same TLS code for both RadSec, and for all
2627 * TLS-enabled EAP methods.
2628 *
2629 * OpenSSL either reads "dirty_in" and writes
2630 * "clean_out", or reads "clean_in" and writes
2631 * "dirty_out".
2632 *
2633 * We allocate a fixed size buffer going into_ssl,
2634 * because an extensible buffer could allow the peer to
2635 * send us unbounded data. A fixed size buffer allows
2636 * this code to limit that usage.
2637 *
2638 * @todo - make the input buffer extendable, but with
2639 * a maximum limit, and only after all TLS negotiation
2640 * has completed, and the session is fully authenticated.
2641 *
2642 * The size is FR_TLS_MAX_PACKET_SIZE and not
2643 * SSL3_RT_MAX_PLAIN_LENGTH. Data OpenSSL has not used
2644 * stay in the buffer, as they are generally partial TLS
2645 * records. But the buffer has to be large enough to
2646 * hold an entire TLS record.
2647 *
2648 * init and max are the same, so the buffer is allocated
2649 * once and never moves.
2650 *
2651 * We don't limit the data coming out of from_ssl,
2652 * because OpenSSL decides how much it writes. Multiple
2653 * handshakes could carry a long certificate chain, which
2654 * exceeds one record in size.
2655 */
2656 {
2657 BIO *rbio, *wbio;
2658
2659 /*
2660 * free_buff is false because tls_session is the talloc
2661 * parent of the buffer as well as of the
2662 * fr_tls_bio_dbuff_t. talloc frees the buffer with the
2663 * session, and a destructor which freed it too would
2664 * free it twice.
2665 */
2666 MEM(rbio = fr_tls_bio_dbuff_alloc(&tls_session->into_ssl, tls_session, tls_session,
2667 FR_TLS_MAX_PACKET_SIZE, FR_TLS_MAX_PACKET_SIZE, false));
2668 MEM(wbio = fr_tls_bio_dbuff_alloc(&tls_session->from_ssl, tls_session, tls_session,
2669 SSL3_RT_MAX_PLAIN_LENGTH, 0, false));
2670
2671 tls_session->dirty_in = fr_tls_bio_dbuff_in(tls_session->into_ssl);
2672 tls_session->dirty_out = fr_tls_bio_dbuff_out(tls_session->from_ssl);
2673
2674 /*
2675 * SSL_set_bio() takes ownership of both BIOs, and the
2676 * fr_tls_bio_dbuff_t destructor frees its own BIO. Give
2677 * each owner a reference to drop.
2678 */
2679 BIO_up_ref(rbio);
2680 BIO_up_ref(wbio);
2681 SSL_set_bio(tls_session->ssl, rbio, wbio);
2682 }
2683
2684 return tls_session;
2685}
2686
2687/** Create a new client TLS session
2688 *
2689 * Configures a new client TLS session, configuring options, setting callbacks etc...
2690 *
2691 * If request is not passed, the caller gets a bare SSL*, and owns
2692 * the transport itself. The session has no memory BIOs, and OpenSSL
2693 * does the certificate validation on its own. This API is used by
2694 * hiredis, which needs direct access to the bare SSL*
2695 *
2696 * If a request is passed, the session management follows the server
2697 * session path. Data goes through memory BIOs,
2698 * fr_tls_session_async_handshake_push() is run for each round, and
2699 * certificate verification calls the `verify certificate` section of
2700 * the configured virtual_server.
2701 *
2702 * @param[in] ctx to alloc session data in. Should usually be NULL unless the lifetime of the
2703 * session is tied to another talloc'd object.
2704 * @param[in] ssl_ctx containing the base configuration for this session.
2705 * @param[in] request which the handshake runs under, or NULL for a session whose transport
2706 * is owned by the caller.
2707 * @return
2708 * - A new session on success.
2709 * - NULL on error.
2710 */
2711fr_tls_session_t *fr_tls_session_alloc_client(TALLOC_CTX *ctx, SSL_CTX *ssl_ctx, request_t *request)
2712{
2713 int verify_mode;
2714 fr_tls_session_t *tls_session;
2715 fr_tls_conf_t *conf = fr_tls_ctx_conf(ssl_ctx);
2716
2717 tls_session = tls_session_alloc(ctx, request, ssl_ctx);
2718 if (!tls_session) return NULL;
2719
2720 /*
2721 * In Client mode we only connect.
2722 *
2723 * This sets up the SSL session to work correctly with
2724 * fr_tls_session_handshake.
2725 */
2726 SSL_set_connect_state(tls_session->ssl);
2727
2728 /*
2729 * Always verify the peer, but only require a certificate
2730 * when we're doing certificate authentication.
2731 *
2732 * PSK doesn't use certs, so we can't require certs if
2733 * we're using PSK.
2734 */
2735 verify_mode = SSL_VERIFY_PEER;
2736 if (!conf->psk.identity) {
2737 ROPTIONAL(RDEBUG2, DEBUG2, "Requiring Server certificate");
2738 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2739 }
2740
2741 if (request) {
2742 /*
2743 * fr_tls_verify_cert_cb() pauses the handshake and runs
2744 * the `verify certificate` section. It needs a request,
2745 * and it needs to be called from inside
2746 * tls_session_async_handshake_cont().
2747 */
2748 tls_session->verify_peer_cert = true;
2749 SSL_set_verify(tls_session->ssl, verify_mode, fr_tls_verify_cert_cb);
2750 } else {
2751 /*
2752 * With no request there is nowhere to run a policy, so
2753 * OpenSSL does the validating. When the callback is
2754 * NULL OpenSSL uses its own validation function, and the
2755 * flags modify that function's behaviour.
2756 */
2757 SSL_set_verify(tls_session->ssl, verify_mode, NULL);
2758 }
2759
2760 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
2761
2762 /*
2763 * Datagrams need to be limited to a pre-configured MTU. So we remember that, and tell OpenSSL
2764 * to limit the MTU.
2765 */
2766 if ((tls_session->socket_type == SOCK_DGRAM) && (fr_dtls_session_init(tls_session, conf) < 0)) {
2767 talloc_free(tls_session);
2768 return NULL;
2769 }
2770
2771 /*
2772 * Session resumption needs somewhere to keep its state, and is
2773 * driven by the caller. See fr_tls_ticket_stateful_load_client_push() and
2774 * fr_tls_ticket_stateful_pending_push().
2775 */
2776 if (request && (conf->cache.mode != FR_TLS_TICKET_DISABLED)) {
2777 tls_session->allow_session_resumption = true; /* otherwise it's false */
2778 fr_tls_ticket_stateful_session_alloc(tls_session);
2779 }
2780
2781 return tls_session;
2782}
2783
2784/** Create a new server TLS session
2785 *
2786 * Configures a new server TLS session, configuring options, setting callbacks etc...
2787 *
2788 * @param[in] ctx to alloc session data in. Should usually be NULL
2789 * unless the lifetime of the session is tied to another
2790 * talloc'd object.
2791 * @param[in] ssl_ctx containing the base configuration for this session.
2792 * @param[in] request The current #request_t.
2793 * @param[in] client_cert Whether to require a client_cert.
2794 * @return
2795 * - A new session on success.
2796 * - NULL on error.
2797 */
2798fr_tls_session_t *fr_tls_session_alloc_server(TALLOC_CTX *ctx, SSL_CTX *ssl_ctx, request_t *request, bool client_cert)
2799{
2800 fr_tls_session_t *tls_session;
2801 int verify_mode = 0;
2802 fr_pair_t *vp;
2803 fr_tls_conf_t *conf = fr_tls_ctx_conf(ssl_ctx);
2804
2805 RDEBUG2("(TLS) Initiating new TLS session");
2806
2807 tls_session = tls_session_alloc(ctx, request, ssl_ctx);
2808 if (!tls_session) return NULL;
2809
2810 fr_tls_session_request_bind(tls_session->ssl, request); /* Is unbound in this function */
2811
2812 /*
2813 * This sets the context sessions can be resumed in.
2814 * This is to prevent sessions being created by one application
2815 * and used by another. In our case it prevents sessions being
2816 * reused between modules, or TLS server components such as
2817 * RADSEC.
2818 *
2819 * A context must always be set when doing session resumption
2820 * otherwise session resumption will fail.
2821 *
2822 * As the context ID must be <= 32, we digest the context
2823 * data with sha256.
2824 *
2825 * This seems to only be used for stateful session resumption
2826 * not session-tickets
2827 */
2828 if (conf->cache.mode != FR_TLS_TICKET_DISABLED) {
2829 char *context_id;
2830 EVP_MD_CTX *md_ctx;
2831 uint8_t digest[SHA256_DIGEST_LENGTH];
2832
2833 static_assert(sizeof(digest) <= SSL_MAX_SSL_SESSION_ID_LENGTH,
2834 "SSL_MAX_SSL_SESSION_ID_LENGTH must be >= SHA256_DIGEST_LENGTH");
2835 fr_assert(conf->cache.id_name);
2836
2837 if (tmpl_aexpand(tls_session, &context_id, request, conf->cache.id_name, NULL, NULL) < 0) {
2838 RPEDEBUG("(TLS) Failed expanding session ID");
2839 error:
2840 fr_tls_session_request_unbind(tls_session->ssl); /* Was bound in this function */
2841 talloc_free(tls_session);
2842 return NULL;
2843 }
2844
2845 MEM(md_ctx = EVP_MD_CTX_create());
2846 EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL);
2847 EVP_DigestUpdate(md_ctx, context_id, talloc_strlen(context_id));
2848 EVP_DigestFinal_ex(md_ctx, digest, NULL);
2849 EVP_MD_CTX_destroy(md_ctx);
2850 talloc_free(context_id);
2851
2852 if (!fr_cond_assert(SSL_set_session_id_context(tls_session->ssl,
2853 digest, sizeof(digest)) == 1)) goto error;
2854 }
2855
2856 /*
2857 * Add the session certificate to the session.
2858 */
2859 vp = fr_pair_find_by_da(&request->control_pairs, NULL, attr_tls_session_cert_file);
2860 if (vp) {
2861 RDEBUG2("(TLS) Loading TLS session certificate \"%pV\"", &vp->data);
2862
2863 if (SSL_use_certificate_file(tls_session->ssl, vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
2864 fr_tls_log_perror(request, "Failed loading TLS session certificate \"%s\"",
2865 vp->vp_strvalue);
2866 goto error;
2867 }
2868
2869 if (SSL_use_PrivateKey_file(tls_session->ssl, vp->vp_strvalue, SSL_FILETYPE_PEM) != 1) {
2870 fr_tls_log_perror(request, "Failed loading TLS session certificate \"%s\"",
2871 vp->vp_strvalue);
2872 goto error;
2873 }
2874
2875 if (SSL_check_private_key(tls_session->ssl) != 1) {
2876 fr_tls_log_perror(request, "Failed validating TLS session certificate \"%s\"",
2877 vp->vp_strvalue);
2878 goto error;
2879 }
2880 /*
2881 * Better to perform explicit checks, than rely
2882 * on OpenSSL's opaque error messages.
2883 */
2884 } else {
2885 if (!conf->chains || !conf->chains[0]->private_key_file) {
2886 REDEBUG("(TLS) Server requires a private key file");
2887 fr_tls_session_error_add(request, FR_ERROR_VALUE_NO_SERVER_PRIVATE_KEY);
2888 goto error;
2889 }
2890
2891 if (!conf->chains || !conf->chains[0]->certificate_file) {
2892 REDEBUG("(TLS) Server requires a certificate file");
2893 fr_tls_session_error_add(request, FR_ERROR_VALUE_NO_SERVER_CERTIFICATE);
2894 goto error;
2895 }
2896 }
2897
2898 /** Dynamic toggle for allowing disallowing client certs
2899 *
2900 * This is mainly used for testing in environments where we can't
2901 * get test credentials for the host.
2902 */
2904 if (vp) client_cert = vp->vp_bool;
2905
2906 /*
2907 * In Server mode we only accept.
2908 *
2909 * This sets up the SSL session to work correctly with
2910 * fr_tls_session_handshake.
2911 */
2912 SSL_set_accept_state(tls_session->ssl);
2913
2914 /*
2915 * Verify the peer certificate, if asked.
2916 */
2917 if (client_cert) {
2918 RDEBUG2("(TLS) Setting verify mode to require certificate from client");
2919 verify_mode = SSL_VERIFY_PEER;
2920 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2921 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
2922 }
2923 tls_session->verify_peer_cert = client_cert;
2924
2925 SSL_set_verify(tls_session->ssl, verify_mode, fr_tls_verify_cert_cb);
2926 SSL_set_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
2927
2928 /*
2929 * Datagrams need to be limited to a pre-configured MTU. So we remember that, and tell OpenSSL
2930 * to limit the MTU.
2931 */
2932 if ((tls_session->socket_type == SOCK_DGRAM) && (fr_dtls_session_init(tls_session, conf) < 0)) {
2933 goto error;
2934 }
2935
2936 SSL_CTX_set_client_hello_cb(ssl_ctx, fr_tls_session_client_hello_cb, NULL);
2937
2938 if (conf->cache.mode != FR_TLS_TICKET_DISABLED) {
2939 tls_session->allow_session_resumption = true; /* otherwise it's false */
2940 if (conf->cache.mode & FR_TLS_TICKET_STATEFUL) fr_tls_ticket_stateful_session_alloc(tls_session);
2941 }
2942
2943 fr_tls_session_request_unbind(tls_session->ssl); /* Was bound in this function */
2944
2945 return tls_session;
2946}
2947
2948static unlang_action_t tls_new_session_result(request_t *request, UNUSED void *uctx)
2949{
2950 request_t *parent = request->parent;
2951
2953
2954 /*
2955 * Copy control attributes back to the parent.
2956 */
2957 if (fr_pair_list_copy(parent->control_ctx, &parent->control_pairs, &request->control_pairs) < 0) return UNLANG_ACTION_FAIL;
2958
2960}
2961
2962unlang_action_t fr_tls_new_session_push(request_t *request, fr_tls_conf_t const *tls_conf)
2963{
2964 request_t *child;
2965 fr_value_box_t id;
2966
2967 /*
2968 * There is no session yet, so there is no ID to report.
2969 */
2971
2972 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_new_session->vb_uint32, &id));
2973
2974 if (unlang_subrequest_child_push(NULL, child, request, true, UNLANG_SUB_FRAME) < 0) {
2975 /*
2976 * The child hasn't run, so we shouldn't call
2977 * unlang_subrequest_detach_and_free().
2978 */
2980 talloc_free(child);
2981 return UNLANG_ACTION_FAIL;
2982 }
2983
2985 NULL,
2986 tls_new_session_result,
2987 NULL, FR_SIGNAL_NONE,
2989 /*
2990 * We just pushed the child frame onto the stack.
2991 * We don't want the subrequest to execute, if we then
2992 * failed to push the function that executes after it
2993 * returns.
2994 */
2995 error:
2998 talloc_free(child);
2999 return UNLANG_ACTION_FAIL;
3000 }
3001
3002 /*
3003 * unlang_call_push() fails when the virtual server was never
3004 * compiled, not only when memory runs out. So this is a
3005 * configuration error, and is reachable.
3006 */
3007 if (unlang_call_push(NULL, child, tls_conf->virtual_server, UNLANG_SUB_FRAME) < 0) goto error;
3008
3010}
3011#endif /* WITH_TLS */
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition action.h:40
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:38
static int const char char buffer[256]
Definition acutest.h:641
void int line
Definition acutest.h:810
static uint8_t request[4]
#define IGNORE(_expr, _type)
Definition build.h:627
#define DIAG_OFF_OPTIONAL(_x)
Definition build.h:577
#define unlikely(_x)
Definition build.h:486
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:437
#define DIAG_ON_OPTIONAL(_x)
Definition build.h:578
unlang_action_t unlang_call_push(unlang_result_t *p_result, request_t *request, CONF_SECTION *server_cs, bool top_frame)
Push a virtual server CONF_SECTION as a call frame onto the stack.
Definition call.c:151
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1121
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:963
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:387
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition dbuff.h:795
#define fr_dbuff_in_bytes(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker.
Definition dbuff.h:1518
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:248
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:194
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
int unlang_function_clear(request_t *request)
Clear pending repeat function calls, and remove the signal handler.
Definition function.c:275
int unlang_function_repeat_clear(request_t *request)
Clear the pending repeat function, and keep the signal handler.
Definition function.c:309
unlang_action_t(* unlang_function_no_result_t)(request_t *request, void *uctx)
A generic function pushed by a module or xlat to functions deeper in the C call stack to create resum...
Definition function.h:68
#define unlang_function_repeat_set(_request, _repeat)
Set a new repeat function for an existing function frame.
Definition function.h:110
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
Definition function.h:181
talloc_free(hp)
void unlang_interpet_frame_discard(request_t *request)
Discard the bottom most frame on the request's stack.
Definition interpret.c:2751
bool unlang_request_is_cancelled(request_t const *request)
Return whether a request has been cancelled.
Definition interpret.c:1985
#define UNLANG_SUB_FRAME
Definition interpret.h:37
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:460
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
Definition log.h:545
#define RWDEBUG(fmt,...)
Definition log.h:378
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
Definition log.h:352
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RHEXDUMP4(_data, _len, _fmt,...)
Definition log.h:725
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define RHEXDUMP3(_data, _len, _fmt,...)
Definition log.h:724
#define RDEBUG4(fmt,...)
Definition log.h:361
#define RDEBUG_ENABLED4
True if request debug level 1-4 messages are enabled.
Definition log.h:353
#define RPWDEBUG2(fmt,...)
Definition log.h:384
#define REDEBUG4(fmt,...)
Definition log.h:391
#define RINDENT()
Indent R* messages by one level.
Definition log.h:447
fr_value_box_t const * enum_tls_packet_type_load_psk
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_alert_level
fr_value_box_t const * enum_tls_packet_type_success
HIDDEN fr_dict_attr_t const * attr_tls_psk_key
HIDDEN fr_dict_attr_t const * attr_tls_session_require_client_cert
fr_value_box_t const * enum_tls_packet_type_new_session
fr_value_box_t const * enum_tls_packet_type_establish_session
HIDDEN fr_dict_attr_t const * attr_tls_error
fr_value_box_t const * enum_tls_packet_type_fail_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_alert_description
fr_value_box_t const * enum_tls_packet_type_notfound
HIDDEN fr_dict_attr_t const * attr_tls_alert
HIDDEN fr_dict_attr_t const * attr_tls_session_cert_file
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
Definition log.h:69
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:56
static uint8_t fr_low_bit_pos(uint64_t num)
Find the lowest order high bit in an unsigned 64 bit integer.
Definition math.h:113
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
#define UINT8_MAX
static size_t used
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)
Definition nbo.h:146
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1425
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2312
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2607
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2561
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.
Definition pair.c:716
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1306
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1650
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:47
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:509
#define fr_assert(_expr)
Definition rad_assert.h:37
#define pair_update_request(_attr, _da)
#define REDEBUG(fmt,...)
#define RDEBUG_ENABLED2()
#define RDEBUG2(fmt,...)
#define DEBUG2(fmt,...)
static rs_t * conf
Definition radsniff.c:52
#define request_is_detachable(_x)
Definition request.h:188
#define pair_append_request(_attr, _da)
Allocate and append a fr_pair_t to the request list.
Definition pair.h:37
#define pair_update_session_state(_attr, _da)
Return or allocate a fr_pair_t in the session_state list.
Definition pair.h:151
#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.
Definition tmpl.h:1089
#define FR_SIGNAL_ALLOW(_signals)
Signal mask which blocks every signal except the ones in _signals.
Definition signal.h:58
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_NONE
No signals.
Definition signal.h:39
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
fr_pair_t * vp
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
int unlang_subrequest_child_push(unlang_result_t *p_result, request_t *child, void const *unique_session_ptr, bool free_child, bool top_frame)
Push a pre-existing child back onto the stack as a subrequest.
Definition subrequest.c:440
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
static fr_slen_t parent
Definition pair.h:864
static fr_sbuff_err_t char size_t fr_sbuff_t size_t max
Definition value.h:1061
static fr_slen_t data
Definition value.h:1366
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition value.h:640
static fr_sbuff_err_t char size_t * len
Definition value.h:1061