The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
ticket_stateful.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: 2f4e74f4aea9c0b576e5c84d85f6a2c83f23f7f7 $
19 *
20 * @file tls/ticket_stateful.c
21 * @brief Stateful TLS session resumption, the session cache and its `load session`, `store session` and `clear session` sections
22 *
23 * @copyright 2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
25 */
26RCSID("$Id: 2f4e74f4aea9c0b576e5c84d85f6a2c83f23f7f7 $")
27USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
28
29#ifdef WITH_TLS
30#define LOG_PREFIX "tls"
31#define _TLS_PRIVATE 1
32
33#include <freeradius-devel/internal/internal.h>
34#include <freeradius-devel/server/pair.h>
35#include <freeradius-devel/unlang/function.h>
36#include <freeradius-devel/unlang/subrequest.h>
37#include <freeradius-devel/util/debug.h>
38
39#include <freeradius-devel/protocol/tls/freeradius.h>
40
41#include "attrs.h"
42#include "base.h"
43#include "log.h"
44#include "strerror.h"
45#include "ticket.h"
46#include "verify.h"
47
48#include <openssl/ssl.h>
49
50#define TLS_TICKET_STATEFUL_DISABLED (!tls_cache || !conf->virtual_server)
51
52static inline CC_HINT(always_inline, nonnull(2))
53void _tls_ticket_stateful_load_state_reset(request_t *request, fr_tls_ticket_stateful_t *cache, char const *func)
54{
55 if (cache->load.sess) {
57 if (cache->session_id) {
58 ROPTIONAL(RDEBUG3, DEBUG3, "(TLS) Session ID %pV - Freeing loaded session in %s",
59 cache->session_id, func);
60 } else {
61 ROPTIONAL(RDEBUG3, DEBUG3, "(TLS) Freeing loaded session in %s", func);
62 }
63 }
64
65 SSL_SESSION_free(cache->load.sess);
66 cache->load.sess = NULL;
67 }
68 cache->load.returned = false;
69 cache->load.state = FR_TLS_TICKET_STATEFUL_INIT;
70}
71#define tls_ticket_stateful_load_state_reset(_request, _cache) _tls_ticket_stateful_load_state_reset(_request, _cache, __FUNCTION__)
72
73static inline CC_HINT(always_inline, nonnull(2))
74void _tls_ticket_stateful_store_state_reset(request_t *request, fr_tls_ticket_stateful_t *cache, char const *func)
75{
76 if (cache->store.sess) {
78 ROPTIONAL(RDEBUG3, DEBUG3, "(TLS) Session ID %pV - Freeing session to store in %s", &cache->store.id, func);
79 }
80 SSL_SESSION_free(cache->store.sess);
81 cache->store.sess = NULL;
82 fr_value_box_clear(&cache->store.id);
83 }
84 cache->store.state = FR_TLS_TICKET_STATEFUL_INIT;
85}
86#define tls_ticket_stateful_store_state_reset(_request, _cache) _tls_ticket_stateful_store_state_reset(_request, _cache, __FUNCTION__)
87
88static inline CC_HINT(always_inline)
89void _tls_ticket_stateful_clear_state_reset(request_t *request, fr_tls_ticket_stateful_t *cache, char const *func)
90{
91 if (!fr_type_is_null(cache->clear.id.type)) {
93 ROPTIONAL(RDEBUG3, DEBUG3, "(TLS) Session ID %pV - Freeing session ID to clear in %s",
94 &cache->clear.id, func);
95 }
96 fr_value_box_clear(&cache->clear.id);
97 }
98 cache->clear.state = FR_TLS_TICKET_STATEFUL_INIT;
99}
100#define tls_ticket_stateful_clear_state_reset(_request, _cache) _tls_ticket_stateful_clear_state_reset(_request, _cache, __FUNCTION__)
101
102/** Request `load session { ... }`, and tell the handshake a cache section is pending
103 */
104static inline CC_HINT(always_inline) void tls_ticket_stateful_load_state_request(fr_tls_session_t *tls_session)
105{
106 tls_session->cache->load.state = FR_TLS_TICKET_STATEFUL_REQUESTED;
107 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_STATEFUL_TICKET);
108}
109
110/** Request `store session { ... }`, and tell the handshake a cache section is pending
111 */
112static inline CC_HINT(always_inline) void tls_ticket_stateful_store_state_request(fr_tls_session_t *tls_session)
113{
114 tls_session->cache->store.state = FR_TLS_TICKET_STATEFUL_REQUESTED;
115 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_STATEFUL_TICKET);
116}
117
118/** Request `clear session { ... }`, and tell the handshake a cache section is pending
119 */
120static inline CC_HINT(always_inline) void tls_ticket_stateful_clear_state_request(fr_tls_session_t *tls_session)
121{
122 tls_session->cache->clear.state = FR_TLS_TICKET_STATEFUL_REQUESTED;
123 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_STATEFUL_TICKET);
124}
125
126/** Has `load session { ... }` returned a session which the handshake is still using?
127 *
128 * `load.id` is set when the load is requested, and stays set after a successful load. Every failed
129 * load clears `load.id`. Because every failed load clears `load.id`, a set `load.id` with no load
130 * pending means that the session cache holds an entry for the session.
131 *
132 * @param[in] tls_cache to check.
133 * @return
134 * - true if a loaded session exists, and `load.id` names the cache entry.
135 * - false otherwise.
136 */
137static inline CC_HINT(always_inline) bool tls_ticket_stateful_loaded(fr_tls_ticket_stateful_t const *tls_cache)
138{
139 return !fr_type_is_null(tls_cache->load.id.type) && (tls_cache->load.state != FR_TLS_TICKET_STATEFUL_REQUESTED);
140}
141
142/** Delete session data be deleted from the cache
143 *
144 * @param[in] sess to be deleted.
145 */
146static void tls_ticket_stateful_delete_request(fr_tls_session_t *tls_session, SSL_SESSION *sess)
147{
148 fr_tls_ticket_stateful_t *tls_cache;
150
151 if (!tls_session->cache) return;
152
153 request = fr_tls_session_request(tls_session->ssl);
154 tls_cache = tls_session->cache;
155
156 /*
157 * Request was cancelled just return without doing any work.
158 */
160
161 fr_assert(tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_INIT);
162
163 /*
164 * Record the session to delete
165 */
166 if (tls_ticket_id_to_box(tls_cache, &tls_cache->clear.id, sess) < 0) {
167 RWDEBUG("(TLS) Error retrieving Session ID");
168 return;
169 }
170
171 RDEBUG3("(TLS) Session ID %pV - Requested session clear", &tls_cache->clear.id);
172
173 tls_ticket_stateful_clear_state_request(tls_session);
174
175 /*
176 * Reset any pending `store session`, so that we skip
177 * unnecessary work.
178 */
179 tls_ticket_stateful_store_state_reset(request, tls_cache);
180
181 /*
182 * We _usually_ store a copy of the SSL_SESSION in tls_session->session. If the
183 * session is being freed, then we invalidate the cached SSL_SESSION. Note that
184 * tls_session->session can be NULL sometimes, see tls_ticket_stateful_delete_cb().
185 *
186 * In any case, if the SSL_SESSION pointer exists, we clear it here to avoid leaving a dangling
187 * pointer.
188 */
189 if (tls_session->session) {
190 fr_assert(tls_session->session == sess);
191 tls_session->session = NULL;
192 }
193
194 /*
195 * Previously the code called ASYNC_pause_job();
196 * assuming this callback would always be called
197 * from SSL_read() or another SSL function.
198 *
199 * Unfortunately it appears that the call path
200 * can also be triggered with SSL_CTX_remove_session
201 * if the reference count on the SSL_SESSION
202 * drops to zero.
203 *
204 * We now check the 'can_pause' flag to determine
205 * if we're inside a yieldable SSL_read call.
206 */
207 if (tls_session->can_pause) ASYNC_pause_job();
208}
209
210/** Process the result of `load session { ... }`
211 */
212static unlang_action_t tls_ticket_stateful_load_resume(request_t *request, void *uctx)
213{
214 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
215 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
216 fr_pair_t *vp;
217 uint8_t const *q, **p;
218 SSL_SESSION *sess;
219
220 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_packet_type);
221 if (!vp || (vp->vp_uint32 != enum_tls_packet_type_success->vb_uint32)) {
222 RWDEBUG("(TLS) Failed acquiring session data");
223
224 /*
225 * A cache miss answers `notfound`, and a miss is the
226 * normal answer on a first connection. Recording it as
227 * a failure would put an `Error` in the session-state
228 * list of every healthy handshake.
229 */
230 fr_tls_session_error_add(request->parent,
231 (vp && (vp->vp_uint32 == enum_tls_packet_type_notfound->vb_uint32)) ?
232 FR_ERROR_VALUE_LOAD_SESSION_NOT_FOUND :
233 FR_ERROR_VALUE_LOAD_SESSION_FAILED);
234 error:
235 tls_cache->load.state = FR_TLS_TICKET_STATEFUL_FAILED;
237 }
238
239 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_session_data);
240 if (!vp) {
241 RWDEBUG("(TLS) No cached session found");
242 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_LOAD_SESSION_NOT_FOUND);
243 goto error;
244 }
245
246 q = vp->vp_octets; /* openssl will mutate q, so we can't use vp_octets directly */
247 p = (unsigned char const **)&q;
248
249 sess = d2i_SSL_SESSION(NULL, p, vp->vp_length);
250 if (!sess) {
251 fr_tls_log_perror(request, "Failed loading persisted session");
252 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_LOAD_SESSION_MALFORMED);
253 goto error;
254 }
255
256 tls_session_id_cache(tls_session, sess); /* the client path has no ID until now */
257
258 if (RDEBUG_ENABLED3) {
259 RDEBUG3("(TLS) Session ID %pV - Read %zu bytes of data. "
260 "Session de-serialized successfully", &tls_session->session_id, vp->vp_length);
261 SSL_SESSION_print(fr_tls_request_log_bio(request, L_DBG, L_DBG_LVL_3), sess);
262 }
263
264 /*
265 * Enforce session timeouts. We don't resume sessions which have exceeded their lifetime.
266 *
267 * The lifetime is already enforced for `store session`. The session may exist on disk (or in a
268 * DB) for long enough that it expires.
269 *
270 * tls_ticket_session_lifetime() clamps to the lowest of the session's own lifetime, the
271 * configured `lifetime`, and the RFC maximum. Clamping to the configured value is what lets
272 * a policy update reach sessions which were stored before it.
273 */
274 {
275 fr_tls_conf_t *conf = tls_session->conf;
276
277 if (!tls_ticket_session_resumable(request, &tls_session->session_id, conf, sess)) {
278 RWDEBUG("(TLS) Session ID %pV - Cached session has too little life left, not resuming",
279 &tls_session->session_id);
280 fr_tls_session_error_add(request, FR_ERROR_VALUE_LOAD_SESSION_EXPIRED);
281
282 /*
283 * The session was allocated by d2i_SSL_SESSION(), and it is not yet saved in
284 * tls_cache->load.sess. We're the only one who knows about it, so we have to
285 * free it.
286 */
287 SSL_SESSION_free(sess);
288 goto error;
289 }
290 }
291
292 /*
293 * OpenSSL's API is very inconsistent.
294 *
295 * We need to set external data here, so it can be
296 * retrieved in tls_ticket_stateful_delete_cb().
297 *
298 * ex_data is not serialised in i2d_SSL_SESSION
299 * so we don't have to bother unsetting it.
300 */
301 SSL_SESSION_set_ex_data(sess, fr_tls_session_ex_index, fr_tls_session(tls_session->ssl));
302
303 tls_cache->load.state = FR_TLS_TICKET_STATEFUL_SUCCESS;
304 tls_cache->load.sess = sess; /* This is consumed in tls_ticket_stateful_load_cb */
305
307}
308
309/** Push a `load session { ... }` call into the current request, using a subrequest
310 *
311 * @param[in] request The current request.
312 * @param[in] tls_session The current TLS session.
313 * @return
314 * - UNLANG_ACTION_CALCULATE_RESULT on noop.
315 * - UNLANG_ACTION_PUSHED_CHILD on success.
316 * - UNLANG_ACTION_FAIL on failure.
317 */
318static unlang_action_t tls_ticket_stateful_load_push(request_t *request, fr_tls_session_t *tls_session)
319{
320 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
321 fr_tls_conf_t *conf = tls_session->conf;
322 request_t *child;
324
325 if (TLS_TICKET_STATEFUL_DISABLED) return UNLANG_ACTION_CALCULATE_RESULT;
326
327 if (tls_cache->load.state != FR_TLS_TICKET_STATEFUL_REQUESTED) return UNLANG_ACTION_CALCULATE_RESULT;
328
329 /*
330 * Reset any pending `load session` if there is also a
331 * pending `clear session`, and mark up the load as failed.
332 *
333 * The load is stuck in an async callback via
334 * ASYNC_pause_job(), so we can't reset the load state
335 * here. When it runs, the callback sees that the load
336 * failed, tells OpenSSL that there is no session, and the
337 * peer does a full handshake instead.
338 */
339 if (tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
340 RDEBUG3("(TLS) Session ID %pV - Clear is pending, skipping `load session { ... }`",
341 &tls_cache->load.id);
342 tls_cache->load.state = FR_TLS_TICKET_STATEFUL_FAILED;
344 }
345
346 fr_assert(!fr_type_is_null(tls_cache->load.id.type));
347
348 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_load_session->vb_uint32,
349 &tls_cache->load.id));
350
351 /*
352 * Allocate a child, and set it up to call
353 * the TLS virtual server.
354 */
355 ua = fr_tls_call_push(child, tls_ticket_stateful_load_resume, conf, tls_session, true);
356 if (ua == UNLANG_ACTION_FAIL) {
357 talloc_free(child);
358 tls_ticket_stateful_load_state_reset(request, tls_cache);
359 return UNLANG_ACTION_FAIL;
360 }
361
362 return ua;
363}
364
365/** Process the result of `store session { ... }`
366 */
367static unlang_action_t tls_ticket_stateful_store_resume(request_t *request, void *uctx)
368{
369 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
370 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
371 fr_pair_t *vp;
372
373 tls_ticket_stateful_store_state_reset(request, tls_cache);
374
375 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_packet_type);
376 if (vp && (vp->vp_uint32 == enum_tls_packet_type_success->vb_uint32)) {
377 tls_cache->store.state = FR_TLS_TICKET_STATEFUL_SUCCESS; /* Avoid spurious clear calls */
378 } else {
379 RWDEBUG("(TLS) Failed storing session data");
380 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_STORE_SESSION_FAILED);
381 tls_cache->store.state = FR_TLS_TICKET_STATEFUL_INIT;
382 }
383
385}
386
387/** Push a `store session { ... }` call into the current request, using a subrequest
388 *
389 * @param[in] request The current request.
390 * @param[in] conf TLS configuration.
391 * @param[in] tls_session The current TLS session.
392 * @return
393 * - UNLANG_ACTION_CALCULATE_RESULT on noop.
394 * - UNLANG_ACTION_PUSHED_CHILD on success.
395 * - UNLANG_ACTION_FAIL on failure.
396 */
397static inline CC_HINT(always_inline)
398unlang_action_t tls_ticket_stateful_store_push(request_t *request, fr_tls_conf_t *conf, fr_tls_session_t *tls_session)
399{
400 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
401 size_t len, ret;
402 int rcode;
403
404 uint8_t *p, *data = NULL;
405
406 request_t *child;
407 fr_pair_t *vp;
408 SSL_SESSION *sess = tls_session->cache->store.sess;
410 fr_time_delta_t ttl;
411
412 if (TLS_TICKET_STATEFUL_DISABLED) return UNLANG_ACTION_CALCULATE_RESULT;
413
414 fr_assert(tls_cache->store.sess);
415 fr_assert(tls_cache->store.state == FR_TLS_TICKET_STATEFUL_REQUESTED);
416
417 /*
418 * If there's a pending clear, then don't push any load /
419 * save / etc. This isn't strictly necessary, but is
420 * good "defense in depth" for any future code changes.
421 * It also documents / enforces our expectations.
422 */
423 if (tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
424 RWDEBUG("(TLS) Session ID %pV - Clear is pending, not storing", &tls_cache->store.id);
425 fr_tls_session_error_add(request, FR_ERROR_VALUE_STORE_CANCELLED_BY_CLEAR);
426 tls_ticket_stateful_store_state_reset(request, tls_cache);
428 }
429
430 ttl = tls_ticket_session_lifetime(request, &tls_cache->store.id, conf, sess);
431 if (!fr_time_delta_ispos(ttl)) {
432 RWDEBUG("(TLS) Session ID %pV - Session has already expired, not storing", &tls_cache->store.id);
433 fr_tls_session_error_add(request, FR_ERROR_VALUE_STORE_SESSION_EXPIRED);
435 }
436
437 /*
438 * Add the current session-state list
439 * contents to the ssl-data
440 */
441 rcode = tls_ticket_app_data_set(request, sess, &tls_cache->store.id);
442 if (rcode < 0) {
443 tls_ticket_stateful_store_state_reset(request, tls_cache);
444 return UNLANG_ACTION_FAIL;
445 }
446
447 if (rcode == 0) return UNLANG_ACTION_CALCULATE_RESULT;
448
449 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_store_session->vb_uint32,
450 &tls_cache->store.id));
451 request = child;
452
453 /*
454 * How long the session has to live. Already clamped, the RFC
455 * maximum included, by tls_ticket_session_lifetime() above.
456 */
458 vp->vp_time_delta = ttl;
459
460 /*
461 * Serialize the session
462 */
463 ret = i2d_SSL_SESSION(sess, NULL); /* find out what length data we need */
464 if (ret < 1) {
465 /* something went wrong */
466 fr_tls_strerror_printf(NULL); /* Drain the OpenSSL error stack */
467 RPWDEBUG("(TLS) Session ID %pV - Serialisation failed, couldn't determine "
468 "required buffer length", &tls_cache->store.id);
469 error:
470 tls_ticket_stateful_store_state_reset(request, tls_cache);
471 talloc_free(child);
472 return UNLANG_ACTION_FAIL;
473 }
474 len = ret;
475
477 MEM(data = talloc_array(vp, uint8_t, len));
478
479 /* openssl mutates &p */
480 p = data;
481 ret = i2d_SSL_SESSION(sess, &p); /* Serialize as ASN.1 */
482 if (ret != len) {
483 fr_tls_strerror_printf(NULL); /* Drain the OpenSSL error stack */
484 RPWDEBUG("(TLS) Session ID %pV - Serialisation failed", &tls_cache->store.id);
486 goto error;
487 }
489
490 /*
491 * Allocate a child, and set it up to call
492 * the TLS virtual server.
493 */
494 ua = fr_tls_call_push(child, tls_ticket_stateful_store_resume, conf, tls_session, true);
495 if (ua == UNLANG_ACTION_FAIL) goto error;
496
497 return ua;
498}
499
500/** Process the result of `clear session { ... }`
501 */
502static unlang_action_t tls_ticket_stateful_clear_resume(request_t *request, void *uctx)
503{
504 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
505 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
506 fr_pair_t *vp;
507
508 tls_ticket_stateful_clear_state_reset(request, tls_cache);
509
510 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_tls_packet_type);
511 if (vp &&
512 ((vp->vp_uint32 == enum_tls_packet_type_success->vb_uint32) ||
513 (vp->vp_uint32 == enum_tls_packet_type_notfound->vb_uint32))) {
515 }
516
517 RWDEBUG("(TLS) Failed deleting session data - security may be compromised");
518 fr_tls_session_error_add(request->parent, FR_ERROR_VALUE_CLEAR_SESSION_FAILED);
520}
521
522/** Push a `clear session { ... }` call into the current request, using a subrequest
523 *
524 * @param[in] request The current request.
525 * @param[in] conf TLS configuration.
526 * @param[in] tls_session The current TLS session.
527 * @return
528 * - UNLANG_ACTION_CALCULATE_RESULT on noop.
529 * - UNLANG_ACTION_PUSHED_CHILD on success.
530 * - UNLANG_ACTION_FAIL on failure.
531 */
532static inline CC_HINT(always_inline)
533unlang_action_t tls_ticket_stateful_clear_push(request_t *request, fr_tls_conf_t *conf, fr_tls_session_t *tls_session)
534{
535 request_t *child;
536 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
538
539 if (TLS_TICKET_STATEFUL_DISABLED) return UNLANG_ACTION_CALCULATE_RESULT;
540
541 fr_assert(tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED);
542 fr_assert(!fr_type_is_null(tls_cache->clear.id.type));
543
544 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_clear_session->vb_uint32,
545 &tls_cache->clear.id));
546
547 /*
548 * Allocate a child, and set it up to call
549 * the TLS virtual server.
550 */
551 ua = fr_tls_call_push(child, tls_ticket_stateful_clear_resume, conf, tls_session, true);
552 if (ua == UNLANG_ACTION_FAIL) {
553 talloc_free(child);
554 tls_ticket_stateful_clear_state_reset(request, tls_cache);
555 return UNLANG_ACTION_FAIL;
556 }
557
558 return ua;
559}
560
561/** Process the result of a client's `load session { ... }` call
562 *
563 * A client hands the session to OpenSSL before the handshake starts. The
564 * server path differs: OpenSSL hands a server the session ID part way through
565 * a handshake, and the server returns the matching session.
566 */
567static unlang_action_t tls_ticket_stateful_load_client_resume(request_t *request, void *uctx)
568{
569 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
570 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
571
572 (void) tls_ticket_stateful_load_resume(request, uctx);
573
574 if (tls_cache->load.state != FR_TLS_TICKET_STATEFUL_SUCCESS) {
575 RDEBUG2("(TLS) No session to resume");
576 fr_value_box_clear(&tls_cache->load.id);
578 }
579
580 if (SSL_set_session(tls_session->ssl, tls_cache->load.sess) != 1) {
581 fr_tls_log_perror(request, "Failed setting the session to resume");
582 tls_ticket_stateful_load_state_reset(request, tls_cache);
583 fr_value_box_clear(&tls_cache->load.id);
585 }
586
587 RDEBUG2("(TLS) Offering the cached session for resumption");
588
589 /*
590 * SSL_set_session() takes its own reference, and nothing else
591 * consumes this one, unlike the server path where the session is
592 * handed back to OpenSSL from tls_ticket_stateful_load_cb.
593 */
594 tls_ticket_stateful_load_state_reset(request, tls_cache);
595
597}
598
599/** Ask the virtual server for a session to resume, as a client
600 *
601 * Call this before the handshake starts. A client chooses which session to
602 * offer, so the client looks a session up under a key the client already
603 * knows, the expansion of `session { name = ... }`. A server makes no such
604 * choice. OpenSSL hands the server the session ID the peer asked for, so the
605 * server path lives in tls_ticket_stateful_load_cb().
606 *
607 * The policy decides what the key means. `TLS-Session-Id` holds the expanded
608 * name, and the `load session { ... }` section may look the session up by
609 * `TLS-Session-Id`, or by any other attribute in the request.
610 *
611 * @param[in] request The current request.
612 * @param[in] tls_session The current TLS session.
613 * @return
614 * - UNLANG_ACTION_CALCULATE_RESULT if there is nothing to do.
615 * - UNLANG_ACTION_PUSHED_CHILD on success.
616 * - UNLANG_ACTION_FAIL on failure.
617 */
618unlang_action_t fr_tls_ticket_stateful_load_client_push(request_t *request, fr_tls_session_t *tls_session)
619{
620 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
621 fr_tls_conf_t *conf = tls_session->conf;
622 char *name;
623 request_t *child;
625
626 if (TLS_TICKET_STATEFUL_DISABLED) return UNLANG_ACTION_CALCULATE_RESULT;
627 if (!tls_session->allow_session_resumption) return UNLANG_ACTION_CALCULATE_RESULT;
628
629 fr_assert(conf->cache.id_name);
630
631 if (tmpl_aexpand(tls_session, &name, request, conf->cache.id_name, NULL, NULL) < 0) {
632 RPEDEBUG("(TLS) Failed expanding the session name");
633 return UNLANG_ACTION_FAIL;
634 }
635
636 /*
637 * Keep the session ID in `load.id`. If the session later fails authentication, we need to run
638 * `clear session` on the session we wanted to load.
639 * i.e. `fr_tls_ticket_stateful_clear_session()` clears a failed session by that ID.
640 */
641 fr_assert(fr_type_is_null(tls_cache->load.id.type));
642 MEM(fr_value_box_memdup(tls_cache, &tls_cache->load.id, NULL,
643 (uint8_t const *) name, talloc_strlen(name), false) == 0);
644
645 MEM(child = tls_subrequest_alloc(request, enum_tls_packet_type_load_session->vb_uint32, &tls_cache->load.id));
646
648
649 ua = fr_tls_call_push(child, tls_ticket_stateful_load_client_resume, conf, tls_session, true);
650 if (ua == UNLANG_ACTION_FAIL) {
651 talloc_free(child);
652 fr_value_box_clear(&tls_cache->load.id);
653 return UNLANG_ACTION_FAIL;
654 }
655
656 return ua;
657}
658
659/** Push a `store session { ... }` or `clear session { ... }` or `load session { ... }`
660 *
661 * Depending on what operation is needed.
662 *
663 * @param[in] request The current request.
664 * @param[in] tls_session The current TLS session.
665 * @return
666 * - UNLANG_ACTION_CALCULATE_RESULT - No pending actions
667 * - UNLANG_ACTION_PUSHED_CHILD - Pending operations to evaluate.
668 */
669unlang_action_t fr_tls_ticket_stateful_pending_push(request_t *request, fr_tls_session_t *tls_session)
670{
671 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
672 fr_tls_conf_t *conf = tls_session->conf;
674
675 if (!tls_cache) return UNLANG_ACTION_CALCULATE_RESULT; /* No caching allowed, nothing to discard */
676
677 /*
678 * The caller is asking us to push load / store / etc. Since the cache is disabled, we just
679 * reset the state (and free resources), then return. The callers can then check
680 * fr_tls_ticket_stateful_pending(), which will now return "nope".
681 */
682 if (TLS_TICKET_STATEFUL_DISABLED) {
683 if (tls_cache->load.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
684 tls_ticket_stateful_load_state_reset(request, tls_cache);
685 }
686 if (tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
687 tls_ticket_stateful_clear_state_reset(request, tls_cache);
688 }
689 if (tls_cache->store.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
690 tls_ticket_stateful_store_state_reset(request, tls_cache);
691 }
693 }
694
695 /*
696 * Load stateful session data.
697 *
698 * tls_ticket_stateful_load_push() may return
699 * UNLANG_ACTION_CALCULATE_RESULT there's a pending
700 * `clear session`. When that happens, we skip the load,
701 * and run the clear instead.
702 */
703 if (tls_cache->load.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
704 ua = tls_ticket_stateful_load_push(request, tls_session);
705 if (ua != UNLANG_ACTION_CALCULATE_RESULT) return ua;
706 }
707
708 /*
709 * We only support a single session
710 * ticket currently...
711 */
712 if (tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
713 /*
714 * Enforce that there's no queued store, as it
715 * should have been cancelled.
716 */
717 fr_assert(tls_cache->store.state != FR_TLS_TICKET_STATEFUL_REQUESTED);
718 tls_ticket_stateful_store_state_reset(request, tls_cache);
719
720 return tls_ticket_stateful_clear_push(request, conf, tls_session);
721 }
722
723 if (tls_cache->store.state == FR_TLS_TICKET_STATEFUL_REQUESTED) {
724 return tls_ticket_stateful_store_push(request, conf, tls_session);
725 }
726
728}
729
730/** Run all queued cache operations
731 *
732 * Multiple operations may be pushed at the same time. We set
733 * ourselves as the resume function before each push, so that when the
734 * queued operation is done, we can check for another one, and run it.
735 *
736 * @param[in] request to run the cache sections in.
737 * @param[in] uctx the #fr_tls_session_t whose queued operations to run.
738 * @return
739 * - UNLANG_ACTION_PUSHED_CHILD - a cache section is running.
740 * - UNLANG_ACTION_CALCULATE_RESULT - no operation is left.
741 */
742static unlang_action_t tls_ticket_stateful_drain(request_t *request, void *uctx)
743{
744 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
746
747 if (!fr_tls_ticket_stateful_pending(tls_session->cache)) return UNLANG_ACTION_CALCULATE_RESULT;
748
749 /*
750 * Set our repeat before any child is pushed.
751 */
752 if (unlikely(unlang_function_repeat_set(request, tls_ticket_stateful_drain) < 0)) return UNLANG_ACTION_FAIL;
753
754 ua = fr_tls_ticket_stateful_pending_push(request, tls_session);
755 if (ua == UNLANG_ACTION_PUSHED_CHILD) return ua;
756
757 /*
758 * We didn't push anything. Clear the repeat.
759 */
761
762 /*
763 * On error, log the failure and continue.
764 */
765 if (ua == UNLANG_ACTION_FAIL) {
766 REDEBUG("(TLS) Failed running session cache operations");
768 }
769
770 return ua;
771}
772
773/** Push a frame which runs every queued cache operation
774 *
775 * @param[in] request to run the cache sections in.
776 * @param[in] tls_session whose queued operations to run.
777 * @return
778 * - UNLANG_ACTION_PUSHED_CHILD - the queued operations are running.
779 * - UNLANG_ACTION_CALCULATE_RESULT - no operation was queued.
780 * - UNLANG_ACTION_FAIL - the frame could not be pushed.
781 */
782static unlang_action_t tls_ticket_stateful_drain_push(request_t *request, fr_tls_session_t *tls_session)
783{
784 if (!fr_tls_ticket_stateful_pending(tls_session->cache)) return UNLANG_ACTION_CALCULATE_RESULT;
785
786 return unlang_function_push(request, tls_ticket_stateful_drain, NULL, NULL, FR_SIGNAL_NONE, UNLANG_SUB_FRAME, tls_session);
787}
788
789/** Store a session after a successful authentication
790 *
791 * Remember to run `store session { ... }`.
792 *
793 * Just finishing the TLS handshake is not always enough. EAP runs
794 * inner methods inside of the TLS tunnel which can fail. So we only
795 * run `store session` after the inner method succeeds.
796 *
797 * @note The caller MUST set the caller's own unlang result before calling
798 * fr_tls_ticket_stateful_store_session(). A pushed child returns to the caller's
799 * caller with that result already in place.
800 *
801 * @param[in] request to run the cache sections in.
802 * @param[in] tls_session to keep.
803 * @return
804 * - UNLANG_ACTION_PUSHED_CHILD - cache sections are running.
805 * - UNLANG_ACTION_CALCULATE_RESULT - there was nothing to do.
806 * - UNLANG_ACTION_FAIL - the frame could not be pushed.
807 */
808unlang_action_t fr_tls_ticket_stateful_store_session(request_t *request, fr_tls_session_t *tls_session)
809{
810 return tls_ticket_stateful_drain_push(request, tls_session);
811}
812
813/** Clear a session after a failed authentication
814 *
815 * Cancels any pending `store session { ... }`, because a failed session is never stored. If we did a `load
816 * session`, then call `clear session { ... }` for that session. `load session { ... }` just loads, and does not
817 * remove entries.
818 *
819 * A session which was not loaded is not in the cache, so we don't need to run `clear session { ... }`.
820 *
821 * @note The caller MUST set the unlang result of the caller before calling
822 * `fr_tls_ticket_stateful_clear_session()`. A pushed child returns to the caller's caller with
823 * the result that the caller set.
824 *
825 * @param[in] request to run the cache sections in.
826 * @param[in] tls_session to discard.
827 * @return
828 * - UNLANG_ACTION_PUSHED_CHILD - cache sections are running.
829 * - UNLANG_ACTION_CALCULATE_RESULT - there was nothing to do.
830 * - UNLANG_ACTION_FAIL - a frame could not be pushed.
831 */
832unlang_action_t fr_tls_ticket_stateful_clear_session(request_t *request, fr_tls_session_t *tls_session)
833{
834 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
835
836 /*
837 * Caching is not allowed for the session. The session has no cache entry to clear.
838 */
839 if (!tls_cache) return UNLANG_ACTION_CALCULATE_RESULT;
840
841 tls_session->allow_session_resumption = false;
842
843 /*
844 * Cancel any pending `store session { ... }`, because a failed session is never stored.
845 */
846 tls_ticket_stateful_store_state_reset(request, tls_cache);
847
848 /*
849 * Queue a clear for the loaded session using `load.id`, unless OpenSSL already queued a clear.
850 * A queued clear has the ID in `load.id`, because OpenSSL asked for the clear when the loaded
851 * session was invalidated.
852 */
853 if (tls_ticket_stateful_loaded(tls_cache) && (tls_cache->clear.state != FR_TLS_TICKET_STATEFUL_REQUESTED)) {
854 if (fr_value_box_copy(tls_cache, &tls_cache->clear.id, &tls_cache->load.id) < 0) {
855 RPERROR("(TLS) Failed recording the session ID to clear");
856 return UNLANG_ACTION_FAIL;
857 }
858
859 RDEBUG3("(TLS) Session ID %pV - Requested session clear", &tls_cache->clear.id);
860 tls_ticket_stateful_clear_state_request(tls_session);
861 }
862
863 return tls_ticket_stateful_drain_push(request, tls_session);
864}
865
866/** Discard a session after a network failure, without clearing the cache.
867 *
868 * See fr_tls_session_fail_session() for more information.
869 *
870 * @note The caller MUST set the caller's own unlang result before calling
871 * `fr_tls_ticket_stateful_discard_session()`. A pushed child returns to the caller's caller with
872 * that result already in place.
873 *
874 * @param[in] request to run the queued `clear session { ... }` in.
875 * @param[in] tls_session to discard.
876 * @return
877 * - UNLANG_ACTION_PUSHED_CHILD - `clear session { ... }` is running.
878 * - UNLANG_ACTION_CALCULATE_RESULT - no section was pushed.
879 * - UNLANG_ACTION_FAIL - a frame could not be pushed.
880 */
881unlang_action_t fr_tls_ticket_stateful_discard_session(request_t *request, fr_tls_session_t *tls_session)
882{
883 fr_tls_ticket_stateful_t *tls_cache = tls_session->cache;
884
885 if (!tls_cache) return UNLANG_ACTION_CALCULATE_RESULT;
886
887 tls_ticket_stateful_store_state_reset(request, tls_cache);
888
889 /*
890 * Do `clear session ` only if it matches a previous `load session`.
891 */
892 if ((tls_cache->clear.state == FR_TLS_TICKET_STATEFUL_REQUESTED) &&
893 (!tls_ticket_stateful_loaded(tls_cache) ||
894 (fr_value_box_cmp(&tls_cache->clear.id, &tls_cache->load.id) != 0))) {
895 tls_ticket_stateful_clear_state_reset(request, tls_cache);
896 }
897
898 return tls_ticket_stateful_drain_push(request, tls_session);
899}
900
901/** Write a newly created session data to the tls_session->cache structure
902 *
903 * @note If you hit an assert in this function, it was likely called twice, which shouldn't happen
904 * so blame OpenSSL.
905 *
906 * @param[in] ssl session state.
907 * @param[in] sess to serialise and write to the cache.
908 * @return
909 * - 1. What we return is not used by OpenSSL to indicate success
910 * or failure, but to indicate whether it should free its copy of
911 * the session data.
912 * In this case we tell it not to free the session data, as we
913 */
914static int tls_ticket_stateful_store_cb(SSL *ssl, SSL_SESSION *sess)
915{
917 fr_tls_session_t *tls_session;
918 fr_tls_ticket_stateful_t *tls_cache;
919
920 /*
921 * This functions should only be called once during the lifetime
922 * of the tls_session, as the fields aren't re-populated on
923 * resumption.
924 */
925 tls_session = fr_tls_session(ssl);
926 request = fr_tls_session_request(tls_session->ssl);
927
928 /*
929 * OpenSSL runs this callback when a session is established or
930 * when a NewSessionTicket arrives. Both happen inside
931 * fr_tls_session_recv(), fr_tls_session_send() or a handshake
932 * round, and each of those binds the request across the call.
933 */
934 fr_assert(request != NULL);
935
936 /*
937 * When handling TLS 1.3, if they are enabled OpenSSL calls both the callback to create stateless
938 * session tickets and this one. If a stateless ticket has been created, we don't need to store
939 * the data in a stateful store as well.
940 * TLS 1.2 handling does the right thing.
941 */
942 if (tls_session->session_ticket_created) {
943 RDEBUG2("(TLS) Stateless ticket already created, not performing stateful store");
944 return 0;
945 }
946
947 /*
948 * RFC 8446 Section 4.6.1 lets a TLS 1.3 server send NewSessionTicket at any point after its
949 * Finished message, including part way through application data. We do not support that.
950 *
951 * Once application data is moving, we discard any session tickets that we receive. TLS doesn't
952 * require us to do this, but we don't know what else to do. The application is processing it's
953 * data, and we do not (as yet) have the code to pause the application, and run a subrequest to
954 * store the new ticket.
955 *
956 * The current implementation runs the cache sections from the handshake, which is the only place
957 * the interpreter can yield. tls_ticket_stateful_store_cb() reaches them through ASYNC_pause_job(), and
958 * that is only legal while tls_session->can_pause is set. Fixing this is an issue for the
959 * future.
960 *
961 * One possible solution is to spawn a _detached_ subrequest which processes the ticket, and only
962 * the ticket. This has to be a subrequest in order to avoid confusing the main application
963 * request (and state machine) with TLS data. We don't care if the `store session` succeeded or
964 * failed, because there's nothing we can really do with that result. The connection is likely
965 * to stay up, so we might as well just ignore errors on `store session`.
966 *
967 * For those reasons and more, we just discard the session ticket. All this means is that the
968 * peer (client here) is unable to use that ticket for session resumption.
969 */
970 if (tls_session->seen_application_data) {
971 RDEBUG2("(TLS) Ignoring session ticket received after application data started");
972 return 0;
973 }
974
975 /*
976 * We have a session ticket. See fr_tls_session_is_init_finished()
977 */
978 tls_session->session_ticket_received = true;
979
980 /*
981 * A server sees this callback once, before anything else has
982 * recorded a session.
983 *
984 * A client has already recorded one: SSL_get_session() runs
985 * when OpenSSL finishes the handshake, which is before the
986 * NewSessionTicket arrives here. The ticket's session is the
987 * one worth keeping, because it is what the client offers
988 * back, so it supersedes.
989 */
990 fr_assert(!tls_session->session || !SSL_is_server(ssl));
991 tls_session->session = sess;
992 tls_session_id_cache(tls_session, sess);
993
994 tls_cache = tls_session->cache;
995
996 fr_assert(tls_cache);
997 fr_assert(tls_session->conf->virtual_server);
998
999 /*
1000 * Request was cancelled, just get OpenSSL to
1001 * free the session data, and don't do any work.
1002 */
1003 if (unlang_request_is_cancelled(request)) return 0;
1004
1005 /*
1006 * Take a copy of the ID, because "return 0" tells
1007 * OpenSSL that it can delete the session. Which means
1008 * that the cached pointer to the session ID could be
1009 * freed.
1010 *
1011 * We may need to use the ID in the `store session` even
1012 * after the session is gone.
1013 */
1014 if (tls_ticket_id_to_box(tls_cache, &tls_cache->store.id, sess) < 0) {
1015 RDEBUG3("(TLS) No Session ID to store");
1016 return 0;
1017 }
1018
1019 RDEBUG3("(TLS) Session ID %pV - Requested store", &tls_cache->store.id);
1020
1021 /*
1022 * Store the session blob and session id for writing
1023 * later, once all the authentication phases have completed.
1024 */
1025 tls_cache->store.sess = sess;
1026 tls_ticket_stateful_store_state_request(tls_session);
1027
1028 return 1;
1029}
1030
1031/** Read session data from the cache
1032 *
1033 * @param[in] ssl session state.
1034 * @param[in] key to retrieve session data for.
1035 * @param[in] key_len The length of the key.
1036 * @param[out] copy Indicates whether OpenSSL should increment the reference
1037 * count on SSL_SESSION to prevent it being automatically freed. We always
1038 * set this to 0.
1039 * @return
1040 * - Deserialised session data on success.
1041 * - NULL on error.
1042 */
1043static SSL_SESSION *tls_ticket_stateful_load_cb(SSL *ssl,
1044 unsigned char const *key,
1045 int key_len, int *copy)
1046{
1047 fr_tls_session_t *tls_session;
1048 fr_tls_ticket_stateful_t *tls_cache;
1050
1051 tls_session = fr_tls_session(ssl);
1052 request = fr_tls_session_request(tls_session->ssl);
1053 tls_cache = tls_session->cache;
1054
1055 fr_assert(tls_cache);
1056 fr_assert(tls_session->conf->virtual_server);
1057
1058 /*
1059 * The request was cancelled. Do not return a session, and
1060 * let OpenSSL fall back to a full handshake.
1061 */
1062 if (unlang_request_is_cancelled(request)) return NULL;
1063
1064 /*
1065 * Never return a session when session resumption is
1066 * disallowed.
1067 */
1068 if (!tls_cache || !tls_session->allow_session_resumption) return NULL;
1069
1070 /*
1071 * OpenSSL runs the handshake, including this callback, on
1072 * one of its fibers (a 32K micro stack by default).
1073 *
1074 * So that we don't have silent memory corruption we ensure
1075 * all the heavy lifting and unlang execution occurs on the
1076 * main thread, and never on the fiber.
1077 *
1078 * 1. On the first call, the callback records the session
1079 * ID, marks the section as pending, and pauses the
1080 * handshake.
1081 * 2. Control returns to
1082 * tls_session_async_handshake_cont(), which pushes the
1083 * section with fr_tls_ticket_stateful_pending_push().
1084 * 3. Once the section has finished,
1085 * tls_session_async_handshake_cont() resumes the
1086 * handshake, and execution continues after the first
1087 * ASYNC_pause_job() call below.
1088 * 4. The callback jumps back to the switch, where the load
1089 * state records whether the section found a session.
1090 * When the section found a session, the callback pauses
1091 * the handshake a second time, and
1092 * `verify certificate { ... }` re-validates the peer
1093 * certificate.
1094 */
1095again:
1096 switch (tls_cache->load.state) {
1097 case FR_TLS_TICKET_STATEFUL_INIT:
1098 fr_assert(fr_type_is_null(tls_cache->load.id.type));
1099
1100 tls_ticket_stateful_load_state_request(tls_session);
1101 MEM(fr_value_box_memdup(tls_cache, &tls_cache->load.id, NULL,
1102 (uint8_t const *)key, key_len, true) == 0);
1103
1104 /*
1105 * The key is the ID of the session that the peer
1106 * asks to resume, and the key stays the session ID
1107 * for the rest of the handshake. Caching the key
1108 * here keeps the log lines that print the ID
1109 * working after load.id is cleared.
1110 */
1111 if (fr_type_is_null(tls_session->session_id.type)) {
1112 MEM(fr_value_box_memdup(tls_session, &tls_session->session_id, NULL,
1113 (uint8_t const *)key, key_len, true) == 0);
1114 if (tls_cache) tls_cache->session_id = &tls_session->session_id;
1115 }
1116
1117 RDEBUG3("(TLS) Requested session load - ID %pV", &tls_cache->load.id);
1118
1119 /*
1120 * Cache functions are only allowed during the
1121 * handshake.
1122 *
1123 * FIXME: With TLS 1.3 session tickets can be sent
1124 * later. Technically every point where we call
1125 * SSL_read() may need to be a yield point.
1126 */
1127 if (unlikely(!tls_session->can_pause)) {
1128 cant_pause:
1129 fr_assert_msg("Unexpected call to %s. "
1130 "tls_session_async_handshake_cont must be in call stack", __FUNCTION__);
1131 return NULL;
1132 }
1133 /*
1134 * Jumps back to SSL_read() in session.c.
1135 *
1136 * If the request is cancelled, whatever was meant
1137 * to be done while the handshake was paused may
1138 * not have been completed.
1139 */
1140 ASYNC_pause_job();
1141
1142 /*
1143 * `load session { ... }` finished, but the request
1144 * was cancelled. Free any loaded session, reset
1145 * the load state, and tell OpenSSL that the load
1146 * failed.
1147 */
1149 tls_ticket_stateful_load_state_reset(request, tls_cache); /* Clears any loaded session data */
1150 fr_value_box_clear(&tls_cache->load.id);
1151 return NULL;
1152
1153 }
1154 goto again;
1155
1156 case FR_TLS_TICKET_STATEFUL_REQUESTED:
1157 fr_assert(0); /* Called twice without attempting the load?! */
1158 tls_cache->load.state = FR_TLS_TICKET_STATEFUL_FAILED;
1159 break;
1160
1161 case FR_TLS_TICKET_STATEFUL_SUCCESS:
1162 {
1163 fr_pair_t *vp;
1164
1165 /*
1166 * OpenSSL calls this callback once for each ClientHello that it receives. A TLS 1.3
1167 * server which sends a HelloRetryRequest receives a second ClientHello, and we don't
1168 * want to run the full `load session` or `verify certificate` sections twice.
1169 *
1170 * The second time we're called, just return the previously found session.
1171 */
1172 if (tls_cache->load.returned) goto return_session;
1173
1174 /*
1175 * The handshake continues with the loaded session. Cache the ID of the loaded session
1176 * in `tls_session->session_id`. `load.id` stays set after a successful load, because we
1177 * need to clear a failed session by that ID.
1178 */
1179 tls_session_id_cache(tls_session, tls_cache->load.sess);
1180
1181 RDEBUG3("(TLS) Setting session data");
1182
1183 /*
1184 * The SSL_SESSION holds a copy of the peer's
1185 * certificate, but not the peer's certificate
1186 * chain. Re-validation needs the chain.
1187 * tls_ticket_app_data_get() therefore restores the
1188 * session-state list, which holds the certificate
1189 * pairs, from the application data stored with the
1190 * session.
1191 */
1192 if (tls_ticket_app_data_get(request, tls_cache->load.sess, &tls_session->session_id) < 0) {
1193 REDEBUG("(TLS) Denying session resumption via session-id");
1194 verify_error:
1195 /*
1196 * Request the `delete session { ... }`
1197 * section, which runs the next time the
1198 * handshake pauses.
1199 */
1200 tls_ticket_stateful_delete_request(tls_session, tls_cache->load.sess);
1201 tls_ticket_stateful_load_state_reset(request, tls_session->cache); /* Free the session */
1202 fr_value_box_clear(&tls_cache->load.id); /* Nothing is loaded, the clear above has the ID */
1203 return NULL;
1204 }
1205
1206 /*
1207 * Add TLS-Session-Resume-Type to report how the session was resumed.
1208 */
1210 RWDEBUG("(TLS) Session ID %pV - Failed adding %s attribute", &tls_session->session_id,
1212 } else {
1213 vp->vp_uint32 = enum_tls_session_resumed_stateful->vb_uint32;
1214 }
1215
1216 /*
1217 * Mark `verify certificate { ... }` as pending,
1218 * for tls_session_async_handshake_cont() to push
1219 * once the handshake pauses below.
1220 */
1221 fr_tls_verify_resumed_request(tls_session);
1222
1223 if (unlikely(!tls_session->can_pause)) goto cant_pause;
1224
1225 /*
1226 * Jumps back to SSL_read() in session.c.
1227 *
1228 * If the request is cancelled, whatever was meant
1229 * to be done while the handshake was paused may
1230 * not have been completed.
1231 */
1232 ASYNC_pause_job();
1233
1234 /*
1235 * `verify certificate { ... }` finished, but the
1236 * request was cancelled. Free any loaded session,
1237 * reset the load and verify states, and tell
1238 * OpenSSL that the load failed.
1239 */
1241 tls_ticket_stateful_load_state_reset(request, tls_cache); /* Clears any loaded session data */
1242 fr_value_box_clear(&tls_cache->load.id);
1243 fr_tls_verify_cert_reset(tls_session);
1244 return NULL;
1245
1246 }
1247
1248 /*
1249 * The callback denies resumption when the peer
1250 * certificate fails re-validation.
1251 */
1252 if (!fr_tls_verify_cert_result(tls_session)) {
1253 RDEBUG2("(TLS) Certificate re-validation failed, denying session resumption via session-id");
1254 goto verify_error;
1255 }
1256 tls_cache->load.returned = true;
1257
1258 return_session:
1259 /*
1260 * OpenSSL frees the reference which the callback returns, so the callback takes a
1261 * reference for OpenSSL, and tells OpenSSL that the callback has done so with
1262 * *copy = 0. `load.sess` keeps the original reference for a second call, and
1263 * tls_ticket_stateful_load_state_reset() frees the original reference.
1264 */
1265 RDEBUG3("(TLS) Session ID %pV - Giving a reference to the session to libssl", &tls_session->session_id);
1266 SSL_SESSION_up_ref(tls_cache->load.sess);
1267 *copy = 0;
1268 return tls_cache->load.sess;
1269 }
1270
1271
1272 case FR_TLS_TICKET_STATEFUL_FAILED:
1273 RDEBUG3("(TLS) Session data load failed");
1274 break;
1275 }
1276
1277 fr_value_box_clear(&tls_cache->load.id);
1278 fr_assert(!tls_cache->load.sess);
1279
1280 return NULL;
1281}
1282
1283/** Delete session data from the cache
1284 *
1285 * @param[in] ctx Current ssl context.
1286 * @param[in] sess to be deleted.
1287 */
1288static void tls_ticket_stateful_delete_cb(UNUSED SSL_CTX *ctx, SSL_SESSION *sess)
1289{
1290 fr_tls_session_t *tls_session;
1291
1292 /*
1293 * Not sure why this happens, but sometimes SSL_SESSION *s
1294 * make it here without the correct ex data.
1295 *
1296 * Maybe it's one OpenSSL created internally?
1297 */
1298 tls_session = SSL_SESSION_get_ex_data(sess, fr_tls_session_ex_index);
1299 if (!tls_session) return;
1300
1301 (void) talloc_get_type_abort(tls_session, fr_tls_session_t);
1302
1303 /*
1304 * Note that tls_session->session CAN be NULL here. That's because that field is set during the
1305 * TLS negotiation. If we get a rejection part way though the TLS negotiation, then the field
1306 * isn't set. But OpenSSL passes the SSL_SESSION to us here, so we use that.
1307 */
1308 tls_ticket_stateful_delete_request(tls_session, sess);
1309}
1310
1311/** Cleanup any memory allocated by OpenSSL
1312 */
1313static int _tls_ticket_stateful_free(fr_tls_ticket_stateful_t *tls_cache)
1314{
1315 /*
1316 * `session_id` points at the session ID in the TLS session, which owns `tls_cache`. Talloc
1317 * frees the children of the TLS session in the reverse order of their allocation, so the
1318 * octets of the session ID are already freed when this destructor runs. The reset functions
1319 * log the session ID, so they must not see the pointer.
1320 */
1321 tls_cache->session_id = NULL;
1322
1323 tls_ticket_stateful_load_state_reset(NULL, tls_cache);
1324 tls_ticket_stateful_store_state_reset(NULL, tls_cache);
1325 tls_ticket_stateful_clear_state_reset(NULL, tls_cache);
1326
1327 return 0;
1328}
1329
1330/** Allocate a session cache state structure, and assign it to a tls_session
1331 *
1332 * @note This must be called if session caching is enabled for a tls session.
1333 *
1334 * @param[in] tls_session to assign cache structure to.
1335 */
1336void fr_tls_ticket_stateful_session_alloc(fr_tls_session_t *tls_session)
1337{
1338 fr_assert(!tls_session->cache);
1339
1340 MEM(tls_session->cache = talloc_zero(tls_session, fr_tls_ticket_stateful_t));
1341 talloc_set_destructor(tls_session->cache, _tls_ticket_stateful_free);
1342}
1343
1344/** Disable stateful session resumption for a given TLS ctx
1345 *
1346 * @param[in] ctx to disable stateful session resumption for.
1347 */
1348void fr_tls_ticket_stateful_disable(SSL_CTX *ctx)
1349{
1350 /*
1351 * Only disables stateful session-resumption.
1352 *
1353 * As per Matt Caswell:
1354 *
1355 * SSL_SESS_CACHE_OFF, when called on the server,
1356 * disables caching of server side sessions.
1357 * It does not switch off resumption. Resumption can
1358 * still occur if a stateless session ticket is used
1359 * (even in TLSv1.2).
1360 */
1361 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1362}
1363
1364/** Install the stateful session cache callbacks on an SSL_CTX
1365 *
1366 * The callbacks run `store session`, `load session` and `clear session`.
1367 * Internal lookups are disabled, so every lookup goes through the
1368 * callbacks. OpenSSL calls the store callback only for the role the
1369 * cache mode names, so a client context sets SSL_SESS_CACHE_CLIENT
1370 * rather than SSL_SESS_CACHE_SERVER.
1371 *
1372 * @param[in] ctx to install the callbacks on.
1373 * @param[in] cache_conf Session caching configuration, for the lifetime.
1374 * @param[in] client true when the context is for a client.
1375 */
1376void fr_tls_ticket_stateful_ctx_init(SSL_CTX *ctx, fr_tls_ticket_conf_t const *cache_conf, bool client)
1377{
1378 SSL_CTX_sess_set_new_cb(ctx, tls_ticket_stateful_store_cb);
1379 SSL_CTX_sess_set_get_cb(ctx, tls_ticket_stateful_load_cb);
1380 SSL_CTX_sess_set_remove_cb(ctx, tls_ticket_stateful_delete_cb);
1381
1382 SSL_CTX_set_session_cache_mode(ctx, (client ? SSL_SESS_CACHE_CLIENT : SSL_SESS_CACHE_SERVER) |
1383 SSL_SESS_CACHE_NO_INTERNAL);
1384
1385 /*
1386 * Controls the validity period of the stateful cache.
1387 */
1388 SSL_CTX_set_timeout(ctx, fr_time_delta_to_sec(cache_conf->lifetime));
1389}
1390#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 uint8_t request[4]
#define USES_APPLE_DEPRECATED_API
Definition build.h:590
#define RCSID(id)
Definition build.h:603
#define IGNORE(_expr, _type)
Definition build.h:627
#define unlikely(_x)
Definition build.h:486
#define UNUSED
Definition build.h:415
#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 MEM(x)
Definition debug.h:38
int unlang_function_clear(request_t *request)
Clear pending repeat function calls, and remove the signal handler.
Definition function.c:275
#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)
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 DEBUG3(_fmt,...)
Definition log.h:271
#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 ROPTIONAL_ENABLED(_e_request, _e_global)
Check if a debug level is set by the request (if !NULL) or by the global log.
Definition log.h:559
#define RPERROR(fmt,...)
Definition log.h:319
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define RPWDEBUG(fmt,...)
Definition log.h:383
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
Definition log.h:264
fr_value_box_t const * enum_tls_packet_type_store_session
HIDDEN fr_dict_attr_t const * attr_tls_packet_type
fr_value_box_t const * enum_tls_packet_type_success
HIDDEN fr_dict_attr_t const * attr_tls_session_ttl
fr_value_box_t const * enum_tls_session_resumed_stateful
HIDDEN fr_dict_attr_t const * attr_tls_session_data
fr_value_box_t const * enum_tls_packet_type_load_session
fr_value_box_t const * enum_tls_packet_type_clear_session
fr_value_box_t const * enum_tls_packet_type_notfound
HIDDEN fr_dict_attr_t const * attr_tls_session_resume_type
@ 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
unsigned char uint8_t
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_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted)
Assign a talloced buffer to a "octets" type value pair.
Definition pair.c:2981
#define fr_assert(_expr)
Definition rad_assert.h:37
#define pair_update_request(_attr, _da)
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
static rs_t * conf
Definition radsniff.c:52
static char const * name
#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
@ FR_SIGNAL_NONE
No signals.
Definition signal.h:39
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
#define fr_time_delta_ispos(_a)
Definition time.h:290
static int64_t fr_time_delta_to_sec(fr_time_delta_t delta)
Definition time.h:656
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
#define fr_type_is_null(_x)
Definition types.h:347
fr_cmp_ret_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:770
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4526
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4509
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:5255
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
int nonnull(2, 5))