The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
tls.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 (at
5 * 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: a037dd41483e857308d478c7bd40230287952fb5 $
19 * @file lib/bio/tls.c
20 * @brief Binary IO abstractions for TLS and DTLS connections
21 *
22 * The TLS bio sits between the application and an fd bio, which carries the encrypted records. The TLS bio
23 * owns the whole life of the TLS connection:
24 *
25 * 1. `fr_bio_tls_alloc()` allocates the bio and the `fr_tls_connection_t` from the request, and sets
26 * `conn->request`. The request must already use an interpreter.
27 * 2. `fr_bio_tls_connect()` takes the connection through every state which comes before application data:
28 * the connect() of the fd bio, the TLS or DTLS handshake, and the policies which run before and after
29 * the handshake. During these states the TLS bio reads and writes the fd bio, and owns the events of
30 * the fd in the event list of the interpreter.
31 * 3. When the connection can carry application data, the TLS bio removes the events of the fd, and calls
32 * the `connected` callback. From then on the application reads and writes the TLS bio in the same way as
33 * any other bio. A read takes encrypted data from the fd bio, passes the encrypted data through
34 * OpenSSL, and returns the decrypted application data. A write encrypts the application data, and
35 * writes the encrypted records to the fd bio.
36 *
37 * The next bio MUST be a fd bio. If the connection fails at any point, the TLS bio removes the events of
38 * the fd, and calls the `failed` callback. The `failed` callback must not free the bio, as the failure can
39 * happen inside a read or a write on the bio.
40 *
41 * The bio and the `fr_tls_connection_t` are allocated from the request, so freeing the request frees both.
42 * Freeing the bio cancels the request, as the request has a frame which points to the connection.
43 *
44 * After each read or write to the fd bio, the TLS bio calls `fr_tls_connection_io_error()` to decide what to
45 * do next: try again, wait, or stop. `fr_tls_connection_io_error()` also tells the TLS connection about EOF
46 * and fatal errors.
47 *
48 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
49 */
50
51#include <freeradius-devel/server/base.h>
52
53#include <freeradius-devel/tls/base.h>
54#include <freeradius-devel/tls/connection.h>
55#include <freeradius-devel/tls/dtls.h>
56
57#include <freeradius-devel/unlang/interpret.h>
58
59#include <freeradius-devel/bio/bio_priv.h>
60#include <freeradius-devel/bio/fd.h>
61#include <freeradius-devel/bio/tls.h>
62
63/** The TLS bio
64 *
65 */
68
69 fr_tls_connection_t *conn; //!< The TLS connection, which is a child of the bio.
70 ///< `conn->request` is the request which the bio came from.
71
72 fr_event_list_t *el; //!< The event list of the interpreter.
73 bool events; //!< The bio owns the events of the fd, which it has until the
74 ///< connection is connected. The event loop can remove the
75 ///< events by itself, e.g. at EOF, so the bio keeps no pointer.
76 int fd; //!< The fd of the next bio.
77
78 SSL_CTX *ssl_ctx; //!< Used to create the TLS session.
79 bool client; //!< Act as the client, and connect to a server.
80 bool client_cert; //!< For a server, require a client certificate.
81
82 bool write_wait; //!< The write event of the fd is active.
83 bool read_paused; //!< The read event of the fd paused
84
85 void *packet_ctx; //!< Passed to the next bio when writing encrypted records.
86 ///< Set only while `fr_bio_tls_write()` runs. Records which
87 ///< are written later, by `fr_bio_tls_write_resume()`, have
88 ///< no `packet_ctx`, as the caller's `packet_ctx` may be gone.
89
90 size_t clean_len; //!< How much decrypted data is in `clean_out`.
91 size_t clean_offset; //!< How much of the decrypted data the caller has read.
92
93 ssize_t next_error; //!< The error which the next bio returned from a write.
94
95 bool write_blocked; //!< The next bio is blocked. Encrypted records wait in the
96 ///< TLS connection until `fr_bio_tls_write_resume()` runs.
97
98 uint8_t *clienthello; //!< For DTLS, the datagram which started the connection.
99 size_t clienthello_len; //!< Length of `clienthello`.
100};
101
104 { 0 }
105};
106
109 { 0 }
110};
111
112/** Start or stop waiting for the fd to have room for more records
113 *
114 * The event of the fd is inserted with a write callback, and then the write event is suspended, as an fd
115 * which has room fires the write event on every pass of the event loop. The write event is active only
116 * while encrypted records are waiting for room.
117 *
118 * A suspend reads the active callback and a resume reads the suspended callback, so a suspend and a resume
119 * have to alternate. `write_wait` records whether the write event is active.
120 *
121 * @return
122 * - 0 on success, or when the bio does not own the events of the fd
123 * - <0 on error
124 */
125static int fr_bio_tls_write_wait(fr_bio_tls_t *my, bool wait)
126{
127 if (!my->events || (my->write_wait == wait)) return 0;
128
129 if (fr_event_filter_update(my->el, my->fd, FR_EVENT_FILTER_IO, wait ? resume_write : pause_write) < 0) {
130 fr_strerror_const_push("Failed updating the write event of the TLS connection");
131 return -1;
132 }
133
134 my->write_wait = wait;
135
136 return 0;
137}
138
139/** Stop owning the events of the fd
140 *
141 */
142static void fr_bio_tls_events_delete(fr_bio_tls_t *my)
143{
144 if (!my->events) return;
145
146 /*
147 * Fails harmlessly when the event loop has already removed the events.
148 */
149 (void) fr_event_fd_delete(my->el, my->fd, FR_EVENT_FILTER_IO);
150 my->events = false;
151 my->write_wait = false;
152 my->read_paused = false;
153}
154
155/** Tell the next bio that it can write again
156 *
157 * The next bio does not know that the event loop saw the fd become writable.
158 */
160{
161 fr_bio_common_t *this = (fr_bio_common_t *) next;
162
163 if (this->priv_cb.write_resume) (void) this->priv_cb.write_resume(next);
164}
165
166/** Stop the connection, and tell the application
167 *
168 * This function is for failures which the TLS connection does not know about, i.e. before the connection
169 * has started.
170 */
171static void fr_bio_tls_failed(fr_bio_tls_t *my, fr_tls_connection_fail_t reason, int error)
172{
174
175 my->conn->failed = reason;
176 my->conn->error = error;
177
178 my->bio.read = fr_bio_shutdown_read;
179 my->bio.write = fr_bio_shutdown_write;
180
181 if (my->cb.failed) my->cb.failed(&my->bio);
182}
183
184/** Write encrypted records to the next bio
185 *
186 * `fr_tls_connection_write()` calls this function once for each block of encrypted data in the TLS
187 * connection. For datagrams, each block is one datagram.
188 *
189 * A partial write means that the fd bio is out of room, so we assume that writes are blocked, in the same
190 * way as `fr_bio_mem_write_next()`. For every other result, `fr_tls_connection_io_error()` decides what
191 * to do.
192 *
193 * On EOF or a fatal error, `fr_tls_connection_io_error()` tells the TLS connection, which runs the
194 * `failed` callback of the application.
195 *
196 * @return
197 * - >0 the amount of data which was written
198 * - 0 writes are blocked. The data stays in the TLS connection, and `fr_bio_tls_write_resume()`
199 * writes the data.
200 * - <0 the write failed. The error is saved in `next_error`.
201 */
202static ssize_t fr_bio_tls_conn_write(fr_tls_connection_t *conn, uint8_t const *data, size_t size)
203{
204 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
205 fr_bio_t *next;
206 ssize_t slen;
207
208 /*
209 * An earlier write was partial, or blocked. Leave the data in the TLS connection until
210 * `fr_bio_tls_write_resume()` runs.
211 */
212 if (my->write_blocked) return 0;
213
214 next = fr_bio_next(&my->bio);
215 fr_assert(next != NULL);
216
217retry:
218 slen = next->write(next, my->packet_ctx, data, size);
219 if (slen > 0) {
220 if ((size_t) slen == size) {
221 /*
222 * Something went out, so the fd had room. Stop waiting for room, which does
223 * nothing when the bio is not waiting.
224 */
225 if (fr_bio_tls_write_wait(my, false) < 0) return -1;
226 return slen;
227 }
228
229 /*
230 * Tell the previous bios that writes are blocked. The data has been written, so we
231 * return how much was written, even if a previous bio returns an error. Otherwise the
232 * TLS connection would write the same data again.
233 */
234 my->write_blocked = true;
235 (void) fr_bio_write_blocked(&my->bio);
236 if (fr_bio_tls_write_wait(my, true) < 0) return -1;
237 return slen;
238 }
239
240 /*
241 * The fd bio returns 0 after it shuts the chain down for an error such as `EPIPE`.
242 */
243 my->next_error = (slen < 0) ? slen : fr_bio_error(SHUTDOWN);
244
245 /*
246 * The fd bio calls its `write_blocked` callback before returning `IO_WOULD_BLOCK`, and the
247 * callback may change `errno`.
248 */
249 if (slen == fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
250
251 switch (fr_tls_connection_io_error(conn, slen)) {
252 case FR_TLS_CONNECTION_IO_RETRY:
253 goto retry;
254
255 case FR_TLS_CONNECTION_IO_BLOCKED:
256 my->write_blocked = true;
257 if (fr_bio_tls_write_wait(my, true) < 0) return -1;
258 return 0;
259
260 /*
261 * `write()` failed, so the result cannot be "no error". Treat the impossible value as fatal.
262 */
263 case FR_TLS_CONNECTION_IO_OK:
264 case FR_TLS_CONNECTION_IO_EOF:
265 case FR_TLS_CONNECTION_IO_FATAL:
266 break;
267 }
268
269 return -1;
270}
271
272static ssize_t fr_bio_tls_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size);
273static ssize_t fr_bio_tls_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size);
274
275/** The TLS connection can carry application data
276 *
277 * The TLS connection calls this function once, when the connection enters the `TLS_CONNECTION_CONNECTED`
278 * state. The application owns the fd from now on, so the events of the fd go away before the application
279 * hears about the connection. The application can then read and write the bio.
280 *
281 * The peer can send application data in the same TCP segment as the end of the handshake. The last
282 * handshake round then decrypts that data into `clean_out`. The application needs to try to read the BIO
283 * after the `connected` callback, in order to drain any pending application data.
284 */
285static void fr_bio_tls_conn_connected(fr_tls_connection_t *conn)
286{
287 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
288
290
291 my->bio.read = fr_bio_tls_read;
292 my->bio.write = fr_bio_tls_write;
293
294 my->clean_len = fr_dbuff_used(&conn->tls_session->clean_out);
295 my->clean_offset = 0;
296
297 if (my->cb.connected) my->cb.connected(&my->bio);
298}
299
300/** The TLS connection failed
301 *
302 * The TLS connection runs the `close` callback only after a failure. After a failure the bio cannot read
303 * or write, and the application decides when to free the bio.
304 */
305static void fr_bio_tls_conn_close(fr_tls_connection_t *conn)
306{
307 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
308
310
311 my->bio.read = fr_bio_shutdown_read;
312 my->bio.write = fr_bio_shutdown_write;
313
314 if (my->cb.failed) my->cb.failed(&my->bio);
315}
316
317/** Write any pending encrypted records to the next bio
318 *
319 * @return
320 * - 1 nothing is left to write
321 * - 0 the next bio is blocked, and some data is still waiting
322 * - <0 the error which the next bio returned
323 */
324static int fr_bio_tls_flush(fr_bio_tls_t *my)
325{
326 fr_tls_session_t *tls_session = my->conn->tls_session;
327
328 if (fr_tls_connection_write(my->conn) < 0) return (int) my->next_error;
329
330 if (fr_dbuff_remaining(tls_session->dirty_out) > 0) return 0;
331
332 return 1;
333}
334
335/** Record that the next bio is blocked
336 *
337 */
339{
340 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
341
342 /*
343 * If we're blocked, then either we're waiting for connect() to finish, or there's pending
344 * application data.
345 */
346 fr_assert((my->conn->state != TLS_CONNECTION_CONNECTED) ||
347 (fr_dbuff_remaining(my->conn->tls_session->dirty_out) > 0));
348
349 my->write_blocked = true;
350
351 return 1;
352}
353
354/** Write the encrypted records which were waiting for the next bio
355 *
356 * @return
357 * - 1 writes can resume
358 * - 0 still blocked
359 * - <0 on error
360 */
362{
363 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
364 int rcode;
365
366 /*
367 * `fr_bio_tls_conn_write()` sets `write_blocked` again if the next bio is still blocked.
368 */
369 my->write_blocked = false;
370
371 rcode = fr_bio_tls_flush(my);
372 if (rcode <= 0) return rcode;
373
374 if (!my->cb.write_resume) return 1;
375
376 return my->cb.write_resume(bio);
377}
378
379/** Encrypt application data, and write the encrypted records to the next bio
380 *
381 * `SSL_MODE_ENABLE_PARTIAL_WRITE` is not set, so OpenSSL encrypts all of the data passed to
382 * `SSL_write()`. The application data therefore counts as written as soon as OpenSSL encrypts the data.
383 * If the next bio blocks, the encrypted records wait in the TLS connection, and the next write or
384 * `fr_bio_tls_write_resume()` writes the records later. Until the waiting records reach the next bio,
385 * the bio returns `fr_bio_error(IO_WOULD_BLOCK)` for new application data. The amount of waiting data
386 * therefore cannot grow without limit.
387 *
388 * @return
389 * - >0 the amount of application data which was written
390 * - `fr_bio_error(IO_WOULD_BLOCK)` when encrypted records are still waiting for the next bio
391 * - <0 on error
392 */
393static ssize_t fr_bio_tls_write_data(fr_bio_tls_t *my, void const *buffer, size_t size)
394{
395 fr_tls_session_t *tls_session = my->conn->tls_session;
396 uint8_t const *p = buffer;
397 size_t written = 0;
398 int rcode;
399
400 /*
401 * Writes are blocked. `fr_bio_tls_write_resume()` writes the waiting records.
402 *
403 * A blocked bio always has encrypted records which have not reached the next bio.
404 */
405 if (my->write_blocked) {
406 fr_assert(fr_dbuff_remaining(tls_session->dirty_out) > 0);
407 return fr_bio_error(IO_WOULD_BLOCK);
408 }
409
410 /*
411 * A read can leave encrypted records waiting. `fr_bio_tls_flush()` writes the waiting
412 * records first, so that the next bio receives the records in order.
413 */
414 if (fr_dbuff_remaining(tls_session->dirty_out) > 0) {
415 rcode = fr_bio_tls_flush(my);
416 if (rcode < 0) return rcode;
417 if (rcode == 0) return fr_bio_error(IO_WOULD_BLOCK);
418 }
419
420 while (written < size) {
421 size_t chunk = size - written;
422
423 if (chunk > fr_dbuff_remaining(&tls_session->clean_in)) chunk = fr_dbuff_remaining(&tls_session->clean_in);
424
425 (void) fr_dbuff_in_memcpy(&tls_session->clean_in, p + written, chunk);
426
427 /*
428 * After a failure inside OpenSSL, the TLS connection cannot continue. The TLS bio is the
429 * bio which failed, so the TLS bio shuts the chain down.
430 */
431 if (fr_tls_session_send(my->conn->request, tls_session) < 0) {
432 fr_strerror_const("Failed encrypting TLS application data");
433 (void) fr_bio_shutdown(&my->bio);
434 return fr_bio_error(GENERIC);
435 }
436
437 /*
438 * OpenSSL has taken the data, so the data has been written, even if the encrypted
439 * records have not yet reached the next bio.
440 */
441 written += chunk;
442
443 rcode = fr_bio_tls_flush(my);
444 if (rcode == 1) continue;
445
446 /*
447 * Writes are blocked, or the write failed. OpenSSL has already taken the application
448 * data, so we return how much data OpenSSL took, and not the error. If the caller wrote
449 * the same data again, the peer would get the data twice.
450 */
451 break;
452 }
453
454 return written;
455}
456
457static ssize_t fr_bio_tls_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
458{
459 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
460 ssize_t rcode;
461
462 /*
463 * If we're not writing actual data, then flush the buffer.
464 *
465 * A blocked TLS BIO is either waiting on connect(), or it has waiting records. We clear the
466 * blocked flag so that the write() tries the next BIO. The write function will take care of
467 * either writing all of the data, or setting the write_blocked flag again.
468 */
469 if (!buffer) {
470 my->write_blocked = false;
471
472 rcode = fr_bio_tls_flush(my);
473 if (rcode < 0) {
474 fr_assert((rcode != fr_bio_error(IO_WOULD_BLOCK)) || my->write_blocked);
475 return rcode;
476 }
477 if (rcode > 0) {
478 fr_assert(!my->write_blocked);
479 return 0;
480 }
481
482 fr_assert(my->write_blocked);
483 return fr_bio_error(IO_WOULD_BLOCK);
484 }
485
486 my->packet_ctx = packet_ctx;
487 rcode = fr_bio_tls_write_data(my, buffer, size);
488 my->packet_ctx = NULL;
489
490 return rcode;
491}
492
493/** Check whether OpenSSL holds data which the bio has not yet processed
494 *
495 */
496static bool fr_bio_tls_pending(fr_bio_tls_t *my)
497{
498 fr_tls_session_t *tls_session = my->conn->tls_session;
499
500 return (fr_dbuff_remaining(fr_tls_bio_dbuff_out(tls_session->into_ssl)) > 0) ||
501 (SSL_pending(tls_session->ssl) > 0);
502}
503
504/** Decrypt one record which is waiting in OpenSSL
505 *
506 * @return
507 * - 1 one record was processed. The record may not contain application data.
508 * - 0 OpenSSL needs more data for a complete record
509 * - <0 on error
510 */
511static int fr_bio_tls_decrypt(fr_bio_tls_t *my)
512{
513 fr_tls_session_t *tls_session = my->conn->tls_session;
514 int rcode;
515
516 rcode = fr_tls_session_recv(my->conn->request, tls_session);
517 if (rcode < 0) {
518 /*
519 * After a failure inside OpenSSL, the TLS connection cannot continue. The TLS bio is the
520 * bio which failed, so the TLS bio shuts the chain down.
521 */
522 fr_strerror_const("Failed decrypting TLS application data");
523 (void) fr_bio_shutdown(&my->bio);
524 return fr_bio_error(GENERIC);
525 }
526
527 if (rcode == 1) return 0;
528
529 my->clean_len = fr_dbuff_used(&tls_session->clean_out);
530 my->clean_offset = 0;
531
532 /*
533 * `fr_tls_session_recv()` can leave encrypted records in `dirty_out`. When writes are
534 * blocked, `fr_bio_tls_write_resume()` writes the records later.
535 */
536 if (fr_dbuff_remaining(tls_session->dirty_out) > 0) {
537 rcode = fr_bio_tls_flush(my);
538 if (rcode < 0) return rcode;
539 }
540
541 return 1;
542}
543
544/** Copy decrypted data which the caller has not yet read into the caller's buffer
545 *
546 */
547static size_t fr_bio_tls_clean_read(fr_bio_tls_t *my, void *buffer, size_t size)
548{
549 size_t used = my->clean_len - my->clean_offset;
550
551 if (size > used) size = used;
552
553 memcpy(buffer, fr_dbuff_start(&my->conn->tls_session->clean_out) + my->clean_offset, size);
554 my->clean_offset += size;
555
556 return size;
557}
558
559/** Read encrypted data from the next bio, and return the decrypted application data
560 *
561 * One record can hold more application data than the caller asked for. The rest of the data stays in
562 * the TLS connection, and the next read returns the rest. The bio asks OpenSSL to decrypt the next
563 * record only after the caller has read all of the data from the previous record.
564 *
565 * @return
566 * - >0 the amount of application data which was read
567 * - 0 no application data is available, or the peer closed the TLS connection
568 * - <0 on error
569 */
570static ssize_t fr_bio_tls_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
571{
572 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
573 fr_tls_connection_t *conn = my->conn;
574 fr_tls_session_t *tls_session = conn->tls_session;
575 fr_bio_t *next;
576
577 next = fr_bio_next(&my->bio);
578 fr_assert(next != NULL);
579
580 while (true) {
581 size_t room;
582 ssize_t rcode;
583
584 if (my->clean_offset < my->clean_len) return fr_bio_tls_clean_read(my, buffer, size);
585
586 /*
587 * Decrypt the records which OpenSSL already has. A record which holds no application
588 * data, such as a session ticket, produces no data, and the loop then decrypts the next
589 * record.
590 */
591 if (fr_bio_tls_pending(my)) {
592 rcode = fr_bio_tls_decrypt(my);
593 if (rcode < 0) return rcode;
594
595 if (rcode == 1) {
596 if (my->clean_len > 0) continue;
597
598 /*
599 * The peer sent a TLS `close_notify` alert, which ends the TLS connection.
600 * No more application data will arrive.
601 */
602 if ((SSL_get_shutdown(tls_session->ssl) & SSL_RECEIVED_SHUTDOWN) != 0) {
603 fr_bio_eof(&my->bio);
604 return 0;
605 }
606
607 if (fr_bio_tls_pending(my)) continue;
608 }
609 }
610
611 /*
612 * OpenSSL needs more data. When `dirty_in` is full, and OpenSSL still needs more data,
613 * the peer sent a record which is too large.
614 */
615 room = fr_dbuff_extend_lowat(NULL, tls_session->dirty_in, tls_session->dirty_size);
616 if (room == 0) {
617 fr_strerror_const("TLS record is too large");
618 (void) fr_bio_shutdown(&my->bio);
619 return fr_bio_error(BUFFER_FULL);
620 }
621
622 /*
623 * Because the socket truncates a datagram which does not fit in the free space, fail
624 * rather than read part of a datagram. See `fr_tls_connection_recv_room()`.
625 */
626 if ((tls_session->socket_type == SOCK_DGRAM) && (room < tls_session->conf->max_datagram_len)) {
627 fr_strerror_printf("Only %zu bytes are free for the next datagram, and max_datagram_len is %u",
628 room, tls_session->conf->max_datagram_len);
629 (void) fr_bio_shutdown(&my->bio);
630 return fr_bio_error(BUFFER_FULL);
631 }
632
633 /*
634 * Read straight into `dirty_in`, the buffer which OpenSSL reads the encrypted data from.
635 * Nothing else writes to `dirty_in` before the `fr_dbuff_advance()` call below.
636 */
637 rcode = next->read(next, packet_ctx, fr_dbuff_current(tls_session->dirty_in), room);
638 if (rcode <= 0) {
639 /*
640 * The fd bio has already shut the chain down for EOF, and for fatal errors. That
641 * shutdown may have run `fr_bio_tls_shutdown()`. On EOF or a fatal error,
642 * `fr_tls_connection_io_error()` tells the TLS connection, which runs the `failed`
643 * callback.
644 *
645 * The fd bio calls its `read_blocked` callback before returning `IO_WOULD_BLOCK`,
646 * and the callback may change `errno`.
647 */
648 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
649
650 switch (fr_tls_connection_io_error(conn, rcode)) {
651 case FR_TLS_CONNECTION_IO_RETRY:
652 continue;
653
654 /*
655 * Blocked, or a datagram socket with no data. Wait for the next read event.
656 */
657 case FR_TLS_CONNECTION_IO_BLOCKED:
658 case FR_TLS_CONNECTION_IO_OK:
659 case FR_TLS_CONNECTION_IO_EOF:
660 case FR_TLS_CONNECTION_IO_FATAL:
661 break;
662 }
663
664 return rcode;
665 }
666
667 /*
668 * `room` limits the read to the free space in `dirty_in`.
669 */
670 fr_dbuff_advance(tls_session->dirty_in, (size_t) rcode);
671 }
672}
673
674/** Return the decrypted data which remains after the next bio reached EOF, and then return EOF
675 *
676 */
677static ssize_t fr_bio_tls_read_eof(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
678{
679 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
680 int rcode;
681
682 while (true) {
683 if (my->clean_offset < my->clean_len) return fr_bio_tls_clean_read(my, buffer, size);
684
685 if (!fr_bio_tls_pending(my)) break;
686
687 /*
688 * The next bio is at EOF, so an incomplete record never completes.
689 */
690 rcode = fr_bio_tls_decrypt(my);
691 if (rcode < 0) return rcode;
692 if (rcode == 0) break;
693 }
694
695 /*
696 * Clear `priv_cb.eof`, so that `fr_bio_eof()` does not call `fr_bio_tls_eof()` again.
697 * `fr_bio_eof()` then tells the previous bios that the TLS bio is at EOF.
698 */
699 my->priv_cb.eof = NULL;
700 fr_bio_eof(&my->bio);
701 return 0;
702}
703
704/** Handle EOF from the next bio
705 *
706 * When the TLS bio still holds data for the application, this function sets the read function to
707 * `fr_bio_tls_read_eof()`. `fr_bio_tls_read_eof()` returns the remaining data, and then signals EOF.
708 *
709 * @return
710 * - 1 the TLS bio holds no data for the application, so `fr_bio_eof()` continues to the previous bios
711 * - 0 the TLS bio still holds data for the application
712 */
713static int fr_bio_tls_eof(fr_bio_t *bio)
714{
715 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
716
717 /*
718 * Before the connection is connected, there is no application data.
719 */
720 if (my->conn->state != TLS_CONNECTION_CONNECTED) return 1;
721
722 if ((my->clean_offset == my->clean_len) && !fr_bio_tls_pending(my)) return 1;
723
724 my->bio.read = fr_bio_tls_read_eof;
725
726 return 0;
727}
728
729
730/** Disconnect the bio from the fd
731 *
732 * This function runs when a shutdown of the chain completes, and when the bio is freed. The events of the
733 * fd go away, and the application can no longer read or write. The TLS connection is a child of the bio, so
734 * the TLS connection stays valid until the bio is freed.
735 */
737{
738 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
739
741
742 my->bio.read = fr_bio_shutdown_read;
743 my->bio.write = fr_bio_shutdown_write;
744
745 return 0;
746}
747
748/** Application data cannot move until the connection is connected
749 *
750 * A read or write before the `connected` callback runs gets "would block". The write is a flush request from
751 * the fd bio, which has nothing to flush.
752 */
754{
755 return fr_bio_error(IO_WOULD_BLOCK);
756}
757
759{
760 if (size == SIZE_MAX) return 0;
761
762 return fr_bio_error(IO_WOULD_BLOCK);
763}
764
767 { 0 }
768};
769
772 { 0 }
773};
774
775/** Resume reading from the connection
776 *
777 * If a connection has been paused because of a full buffer, then mark it as resumable.
778 */
779static void fr_bio_tls_conn_recv_resume(fr_tls_connection_t *conn)
780{
781 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
782
783 if (!my->events || !my->read_paused) return;
784
785 if (fr_event_filter_update(my->el, my->fd, FR_EVENT_FILTER_IO, resume_read) < 0) {
786 fr_tls_connection_failed(conn, TLS_CONNECTION_FAIL_APPLICATION);
787 return;
788 }
789
790 my->read_paused = false;
791}
792
793/** The fd is ready for reads
794 *
795 * This function only reads the fd. The read goes straight into `dirty_in`, and `fr_tls_connection_received()`
796 * passes the octets to the TLS connection.
797 *
798 * `fr_tls_connection_recv_room()` limits each read to the free space in `dirty_in`. When `dirty_in` has no
799 * free space, we pause reads, so that the event loop does not keep reporting that the fd is readable. For
800 * DTLS, `fr_tls_connection_recv_room()` fails when the free space is less than `max_datagram_len`.
801 */
802static void fr_bio_tls_el_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
803{
804 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
805 fr_bio_t *next = fr_bio_next(&my->bio);
806 fr_dbuff_t *dirty_in;
807 size_t room;
808 ssize_t slen;
809
810 room = fr_tls_connection_recv_room(my->conn);
811 dirty_in = my->conn->tls_session->dirty_in;
812 if (room == 0) {
813 if (fr_event_filter_update(my->el, my->fd, FR_EVENT_FILTER_IO, pause_read) < 0) {
814 fr_tls_connection_failed(my->conn, TLS_CONNECTION_FAIL_APPLICATION);
815 return;
816 }
817
818 my->read_paused = true;
819 return;
820 }
821
822 while (true) {
823 slen = next->read(next, NULL, fr_dbuff_current(dirty_in), room);
824 if (slen > 0) {
825 fr_dbuff_advance(dirty_in, (size_t) slen);
826 fr_tls_connection_received(my->conn, (size_t) slen);
827 return;
828 }
829
830 /*
831 * The fd bio calls its `read_blocked` callback before returning `IO_WOULD_BLOCK`, and the
832 * callback may change `errno`.
833 */
834 if (slen == fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
835
836 /*
837 * The connection records what happened, and ends itself when the result ends it. An
838 * interrupted read is run again. A blocked read waits for the next read event, as an fd
839 * which still holds a record fires the read event again.
840 */
841 if (fr_tls_connection_io_error(my->conn, slen) != FR_TLS_CONNECTION_IO_RETRY) return;
842 }
843}
844
845/** The fd is ready for writes
846 *
847 * Run the connection state machine, which either flushes pending data, or keeps going with the next TLS
848 * state. The event loop calls this function only after a write blocked, see `fr_bio_tls_write_wait()`.
849 *
850 * `fr_tls_connection_process()` writes the rest of the encrypted records, and then checks the connection
851 * again. The check ends a connection whose handshake finished while a record was still waiting.
852 */
853static void fr_bio_tls_el_write(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
854{
855 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
856
857 /*
858 * Stop the event first, as an fd which has room fires the event on every pass of the event
859 * loop. Writing the records which are waiting starts the event again if the fd is still full.
860 */
861 if (fr_bio_tls_write_wait(my, false) < 0) {
862 fr_bio_tls_failed(my, TLS_CONNECTION_FAIL_APPLICATION, 0);
863 return;
864 }
865
866 my->write_blocked = false;
868
869 fr_tls_connection_process(my->conn);
870}
871
872/** The fd failed
873 *
874 */
875static void fr_bio_tls_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
876{
877 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
878
879 if (fd_errno == 0) {
880 (void) fr_tls_connection_io_error(my->conn, 0);
881 return;
882 }
883
884 /*
885 * The event loop reports the error rather than leaving it in `errno`, so put it back where
886 * `fr_tls_connection_io_error()` reads it.
887 */
888 errno = fd_errno;
889 (void) fr_tls_connection_io_error(my->conn, -1);
890}
891
892/** The fd is connected, so start the TLS connection
893 *
894 * Create the TLS session, put the events of the fd in the event list, and start the connection state
895 * machine. The state machine runs in one frame of the request, which `fr_tls_connection_push()` pushes.
896 * The TLS bio does the IO for the state machine.
897 *
898 * @return
899 * - 0 on success
900 * - <0 on error
901 */
902static int fr_bio_tls_handshake_start(fr_bio_tls_t *my)
903{
904 fr_tls_connection_t *conn = my->conn;
905 request_t *request = conn->request;
906 fr_bio_t *next = fr_bio_next(&my->bio);
907 fr_bio_fd_info_t const *info = fr_bio_fd_info(next);
908
909 my->fd = info->socket.fd;
910
911 /*
912 * The application knows more about what identifies a connection, so it can set the name before it
913 * calls fr_bio_tls_connect().
914 */
915 if (!conn->name) {
916 conn->name = talloc_asprintf(conn, "%s %s", (info->socket.type == SOCK_DGRAM) ? "DTLS" : "TLS",
917 info->name ? info->name : "");
918 if (!conn->name) return -1;
919 }
920
921 if (conn->client) {
922 conn->tls_session = fr_tls_session_alloc_client(request, my->ssl_ctx, request);
923 } else {
924 conn->tls_session = fr_tls_session_alloc_server(request, my->ssl_ctx, request, my->client_cert);
925 }
926 if (!conn->tls_session) return -1;
927
928 /*
929 * Bind the DTLS cookie to the socket which the connection runs on. The library hashes the whole
930 * structure with a key of its own, and never reads it.
931 */
932 if (info->socket.type == SOCK_DGRAM) conn->tls_session->cookie = &info->socket;
933
934 /*
935 * Copy the ClientHello datagram which started the connection into `dirty_in`. The first handshake
936 * round will then read the datagram from `dirty_in`.
937 */
938 if (my->clienthello) {
939 if (fr_dbuff_in_memcpy_partial(conn->tls_session->dirty_in, my->clienthello, my->clienthello_len) != my->clienthello_len) {
940 fr_strerror_printf("The ClientHello packet (%zu octets) does not fit in the TLS buffer", my->clienthello_len);
941 return -1;
942 }
943 TALLOC_FREE(my->clienthello);
944 my->clienthello_len = 0;
945 }
946
947 /*
948 * The events exist before the connection starts, so that a write which blocks during the first
949 * round can ask for the write event. A fd which has room fires the write event on every pass of
950 * the event loop, so suspend the write event until it is needed.
951 */
952 if (fr_event_fd_insert(my, NULL, my->el, my->fd,
954 fr_strerror_const_push("Failed adding the TLS connection to the event loop");
955 return -1;
956 }
957 my->events = true;
958
959 my->write_wait = true;
960 if (fr_bio_tls_write_wait(my, false) < 0) {
962 return -1;
963 }
964
965 if (fr_tls_connection_push(conn) < 0) {
967 fr_strerror_const_push("Failed starting the TLS connection");
968 return -1;
969 }
970
971 /*
972 * Run the interpreter once, so that the first frame yields. `unlang_interpret_mark_runnable()`
973 * acts only on a frame which has yielded.
974 */
976
977 /*
978 * Run `new session { ... }` and, for a client, `load session`. A server then waits for the
979 * ClientHello. A client has to send the ClientHello.
980 */
981 fr_tls_connection_wake(conn);
982
983 return 0;
984}
985
986/** Find the TLS bio from the fd bio
987 *
988 * The TLS bio is always the bio before the fd bio.
989 */
990static fr_bio_tls_t *fr_bio_tls_from_fd(fr_bio_t *fd_bio)
991{
992 fr_bio_t *prev = fr_bio_prev(fd_bio);
993
994 if (!prev) return NULL;
995
996 return talloc_get_type_abort(prev, fr_bio_tls_t);
997}
998
999/** The fd is connected
1000 *
1001 */
1003{
1004 fr_bio_tls_t *my = fr_bio_tls_from_fd(fd_bio);
1005
1006 if (!my) return;
1007
1008 if (fr_bio_tls_handshake_start(my) < 0) fr_bio_tls_failed(my, TLS_CONNECTION_FAIL_APPLICATION, 0);
1009}
1010
1011/** The fd failed to connect
1012 *
1013 * The fd bio has recorded the error, and shuts down.
1014 */
1015static void fr_bio_tls_fd_error(fr_bio_t *fd_bio)
1016{
1017 fr_bio_tls_t *my = fr_bio_tls_from_fd(fd_bio);
1018
1019 if (!my) return;
1020
1021 fr_bio_tls_failed(my, TLS_CONNECTION_FAIL_SYSCALL, fr_bio_fd_info(fd_bio)->connect_errno);
1022}
1023
1024/** Send a TLS `close_notify` alert to the peer
1025 *
1026 * RFC 9846 Section 6.1 requires that a party sends a `close_notify` alert before closing its write side
1027 * of the connection. A TCP connection can be closed without writing, but a TLS connection cannot.
1028 *
1029 * The write is best effort. If the next bio is blocked, the alert stays in the TLS connection buffer, and
1030 * the fd is closed anyway.
1031 *
1032 * The alert is not sent when the TLS connection is not connected, when the TLS connection failed, when
1033 * the bio chain already shut down, or when the fd is at EOF or is not open.
1034 *
1035 * An application which frees the fd bio before the TLS bio MUST call this function first. Freeing the
1036 * fd bio shuts down the whole chain, which stops writes in the TLS bio, so the TLS bio destructor can no
1037 * longer send the alert.
1038 *
1039 * @param[in] my the TLS bio.
1040 */
1041void fr_bio_tls_close_send(fr_bio_tls_t *my)
1042{
1043 fr_bio_t *next = fr_bio_next(&my->bio);
1044 fr_bio_fd_info_t const *info;
1045
1046 if (my->conn->state != TLS_CONNECTION_CONNECTED) return;
1047 if (my->conn->failed != TLS_CONNECTION_FAIL_NONE) return;
1048 if (my->bio.write == fr_bio_shutdown_write) return;
1049
1050 info = fr_bio_fd_info(next);
1051 if (info->eof || (info->state != FR_BIO_FD_STATE_OPEN)) return;
1052
1053 fr_tls_session_close_send(my->conn->request, my->conn->tls_session);
1054
1055 /*
1056 * Nothing can write to the bio after the alert. Setting the write function also stops a
1057 * blocked write from running the `write_blocked` callbacks while the bio closes.
1058 */
1059 my->bio.write = fr_bio_shutdown_write;
1060
1061 (void) fr_bio_tls_flush(my);
1062}
1063
1064/** Send any close_notify, stop the fd events, cancel the request, and then run the common bio destructor
1065 *
1066 * The application is freeing the bio, so the application does not care if there is a failed write when
1067 * sending the `close_notify`. Delete the FD events before any writes.
1068 *
1069 * A failed write in the FD BIO causes it to close the FD.
1070 *
1071 * The request has a frame which points at the TLS connection, and the TLS connection is freed with the bio.
1072 * Cancelling the request unwinds the frames nad frees the connection. The interpreter can then free the
1073 * request.
1074 */
1075static int fr_bio_tls_destructor(fr_bio_tls_t *my)
1076{
1077 my->cb.failed = NULL;
1078
1079 (void) fr_event_fd_delete(my->el, my->fd, FR_EVENT_FILTER_IO);
1080 my->events = false;
1081 my->write_wait = false;
1082
1084
1085 unlang_interpret_signal(my->conn->request, FR_SIGNAL_CANCEL);
1086
1087 return fr_bio_destructor((fr_bio_t *) my);
1088}
1089
1090/** Allocate a TLS bio
1091 *
1092 * The bio and the TLS connection are allocated from the request, so they cannot outlive the request. The
1093 * request must already use an interpreter, as the connection state machine runs in a frame of the request,
1094 * and the bio takes the event list from the interpreter. After allocating the bio, call `fr_bio_tls_connect()`.
1095 *
1096 * Use `fr_bio_cb_set()` to set the callbacks:
1097 *
1098 * - `connected` is called when the connection can carry application data. The fd events are gone, and the
1099 * application can read and write the bio.
1100 * - `failed` is called when the connection fails, for any reason, at any time. The reason is in
1101 * `conn->failed`, and `conn->error` holds the errno for system call failures. The callback must not free
1102 * the bio, as the callback can run from inside a read or a write on the bio. The callback can instead
1103 * arrange for the application to free the bio later, for example from the event loop.
1104 *
1105 * @param[in] request to allocate the bio and the connection from, and which runs the connection.
1106 * @param[in] next the fd bio which carries the encrypted records.
1107 * @param[in] ssl_ctx to create the TLS session from.
1108 * @param[in] client true to connect to a server, false to accept a connection.
1109 * @param[in] client_cert for a server, whether to require a client certificate. Ignored for a client.
1110 * @return
1111 * - the TLS bio
1112 * - `NULL` when the next bio is not an fd bio, when the request has no event list, or when an allocation
1113 * fails
1114 */
1115fr_bio_tls_t *fr_bio_tls_alloc(request_t *request, fr_bio_t *next, SSL_CTX *ssl_ctx, bool client, bool client_cert)
1116{
1117 fr_bio_tls_t *my;
1119
1120 if (!el) {
1121 fr_strerror_const("The request does not have an event list");
1122 return NULL;
1123 }
1124
1125 if (!talloc_check_name(next, "fr_bio_fd_t")) {
1126 fr_strerror_const("The next bio must be an fd bio");
1127 return NULL;
1128 }
1129
1130 my = talloc_zero(request, fr_bio_tls_t);
1131 if (!my) return NULL;
1132
1133 my->el = el;
1134 my->ssl_ctx = ssl_ctx;
1135 my->client = client;
1136 my->client_cert = client_cert;
1137 my->fd = fr_bio_fd_info(next)->socket.fd;
1138
1139 my->conn = fr_tls_connection_alloc(my, el->tl, fr_bio_tls_conn_write,
1141 if (!my->conn) {
1142 talloc_free(my);
1143 return NULL;
1144 }
1145 my->conn->uctx = my;
1146 my->conn->client = client;
1147 my->conn->recv_resume = fr_bio_tls_conn_recv_resume;
1148 my->conn->request = request;
1149
1150 my->bio.read = fr_bio_tls_read_wait;
1151 my->bio.write = fr_bio_tls_write_wait_connected;
1152
1153 my->priv_cb.eof = fr_bio_tls_eof;
1154 my->priv_cb.shutdown = fr_bio_tls_shutdown;
1155 my->priv_cb.write_blocked = fr_bio_tls_write_blocked;
1156 my->priv_cb.write_resume = fr_bio_tls_write_resume;
1157
1158 fr_bio_chain(&my->bio, next);
1159
1160 talloc_set_destructor(my, fr_bio_tls_destructor);
1161 return my;
1162}
1163
1164/** The TLS connection, so the caller can set `conn->name`
1165 *
1166 */
1167fr_tls_connection_t *fr_bio_tls_conn(fr_bio_tls_t *my)
1168{
1169 return my->conn;
1170}
1171
1172/** Give a DTLS server the datagram which started the connection
1173 *
1174 * A DTLS server which accepts a connection with fr_bio_fd_accept() has already read the first datagram from
1175 * the socket, which must be a ClientHello. We need to cache it somewhere until the underlying TLS
1176 * connection is set up.
1177 *
1178 * Call this function after `fr_bio_tls_alloc()`, and before `fr_bio_tls_connect()`. We therefore cache it,
1179 * until it can be read in fr_bio_tls_handshake_start().
1180 *
1181 * @param[in] my the TLS bio.
1182 * @param[in] data the datagram.
1183 * @param[in] data_len the length of the datagram.
1184 * @return
1185 * - 0 on success.
1186 * - <0 the bio is not a DTLS server which has yet to start, or the datagram is empty or too large.
1187 */
1188int fr_bio_tls_dtls_clienthello(fr_bio_tls_t *my, uint8_t const *data, size_t data_len)
1189{
1190 fr_bio_fd_info_t const *info = fr_bio_fd_info(fr_bio_next(&my->bio));
1191
1192 if (my->client || (info->socket.type != SOCK_DGRAM) || my->conn->tls_session) {
1193 fr_strerror_const("The first datagram can only be given to a DTLS server before the connection starts");
1194 return -1;
1195 }
1196
1197 if (data_len <= DTLS1_RT_HEADER_LENGTH) {
1198 fr_strerror_printf("Invalid length %zu for ClientHello, expected at least %d",
1199 data_len, DTLS1_RT_HEADER_LENGTH + 1);
1200 return -1;
1201 }
1202
1203 if (data_len > FR_TLS_MAX_PACKET_SIZE) {
1204 fr_strerror_printf("Invalid length %zu for ClientHello, expected no more than %d",
1205 data_len, FR_TLS_MAX_PACKET_SIZE);
1206 return -1;
1207 }
1208
1209 if (data[0] != SSL3_RT_HANDSHAKE) {
1210 fr_strerror_printf("Invalid type %u, expected Handshake (%d)", data[0], SSL3_RT_HANDSHAKE);
1211 return -1;
1212 }
1213
1214 if ((data[3] != 0) || (data[4] != 0)) {
1215 fr_strerror_printf("Invalid epoch %u for ClientHello, expected 0",
1216 fr_nbo_to_uint16(data + 3));
1217 return -1;
1218 }
1219
1220 if (fr_nbo_to_uint16(data + 11) > FR_TLS_DTLS_CLIENTHELLO_MAX) {
1221 fr_strerror_printf("Invalid length (%u) for ClientHello, expected no more than %d",
1222 fr_nbo_to_uint16(data + 11), FR_TLS_DTLS_CLIENTHELLO_MAX);
1223 return -1;
1224 }
1225
1226 if (data[DTLS1_RT_HEADER_LENGTH] != SSL3_MT_CLIENT_HELLO) {
1227 fr_strerror_printf("Invalid message type (%u), expected ClientHello (%d)",
1228 data[DTLS1_RT_HEADER_LENGTH], SSL3_MT_CLIENT_HELLO);
1229 return -1;
1230 }
1231
1232 /*
1233 * @todo - set up the TLS session, and move the code from fr_bio_tls_handshake_start() to here.
1234 */
1235 TALLOC_FREE(my->clienthello);
1236
1237 my->clienthello = talloc_memdup(my, data, data_len);
1238 if (!my->clienthello) {
1239 fr_strerror_const("Out of memory");
1240 return -1;
1241 }
1242 my->clienthello_len = data_len;
1243
1244 return 0;
1245}
1246
1247/** Take the connection through every state which comes before application data
1248 *
1249 * The connection connects the fd bio, runs the TLS handshake, and runs
1250 * the policies which go with the handshake. A stream socket can take some time to connect, and the TLS bio
1251 * waits for the connect in the event list. A datagram socket, or a socket which the caller accepted, is
1252 * already connected, so the handshake starts before this function returns.
1253 *
1254 * The caller must wait for the `connected` or `failed` callback. A callback can run before this function
1255 * returns.
1256 *
1257 * @param[in] my the TLS bio.
1258 * @return
1259 * - 0 the connection has started.
1260 * - <0 the fd bio failed to connect. The `failed` callback has already run.
1261 */
1262int fr_bio_tls_connect(fr_bio_tls_t *my)
1263{
1264 if (fr_bio_fd_connect_full(fr_bio_next(&my->bio), my->el,
1265 fr_bio_tls_fd_connected, fr_bio_tls_fd_error, NULL, NULL) < 0) {
1266 return -1;
1267 }
1268
1269 return 0;
1270}
static int const char char buffer[256]
Definition acutest.h:641
fr_bio_write_t _CONST write
write to the underlying bio
Definition base.h:117
static fr_bio_t * fr_bio_prev(fr_bio_t *bio)
Definition base.h:122
fr_bio_read_t _CONST read
read from the underlying bio
Definition base.h:116
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
Definition base.h:131
#define fr_bio_error(_x)
Definition base.h:200
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
Definition bio_priv.h:86
#define UNUSED
Definition build.h:415
#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_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition dbuff.h:712
#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_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1403
#define fr_dbuff_in_memcpy_partial(_out, _in, _inlen)
Copy at most _inlen bytes into the dbuff.
Definition dbuff.h:1491
#define fr_event_fd_insert(...)
Definition event.h:251
#define fr_event_filter_update(...)
Definition event.h:243
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
Definition event.h:135
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
Definition event.h:119
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:87
Callbacks for the FR_EVENT_FILTER_IO filter.
Definition event.h:192
Structure describing a modification to a filter's state.
Definition event.h:100
int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb, fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
Finalize a connect()
Definition fd.c:1349
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
Definition fd.c:1446
fr_socket_t socket
as connected socket
Definition fd.h:145
char const * name
printable name of this BIO
Definition fd.h:150
bool eof
are we at EOF?
Definition fd.h:154
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:148
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:54
Run-time status of the socket.
Definition fd.h:144
talloc_free(hp)
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:1302
#define SHUTDOWN
Definition interpret.c:2530
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1794
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2778
#define UNLANG_REQUEST_RESUME
Definition interpret.h:48
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
Definition base.c:354
ssize_t fr_bio_shutdown_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Definition base.c:74
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
ssize_t fr_bio_shutdown_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Definition base.c:79
fr_bio_t * fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
Definition base.c:280
int fr_bio_shutdown(fr_bio_t *bio)
Shut down a set of BIOs.
Definition base.c:158
static void fr_bio_tls_events_delete(fr_bio_tls_t *my)
Stop owning the events of the fd.
Definition tls.c:142
void fr_bio_tls_close_send(fr_bio_tls_t *my)
Send a TLS close_notify alert to the peer.
Definition tls.c:1041
static void fr_bio_tls_el_write(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
The fd is ready for writes.
Definition tls.c:853
static void fr_bio_tls_conn_connected(fr_tls_connection_t *conn)
The TLS connection can carry application data.
Definition tls.c:285
size_t clean_offset
How much of the decrypted data the caller has read.
Definition tls.c:91
static ssize_t fr_bio_tls_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read encrypted data from the next bio, and return the decrypted application data.
Definition tls.c:570
static void fr_bio_tls_conn_close(fr_tls_connection_t *conn)
The TLS connection failed.
Definition tls.c:305
static ssize_t fr_bio_tls_read_wait(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Application data cannot move until the connection is connected.
Definition tls.c:753
bool client
Act as the client, and connect to a server.
Definition tls.c:79
bool read_paused
The read event of the fd paused.
Definition tls.c:83
fr_tls_connection_t * fr_bio_tls_conn(fr_bio_tls_t *my)
The TLS connection, so the caller can set conn->name
Definition tls.c:1167
void * packet_ctx
Passed to the next bio when writing encrypted records.
Definition tls.c:85
bool events
The bio owns the events of the fd, which it has until the connection is connected.
Definition tls.c:73
static int fr_bio_tls_write_blocked(fr_bio_t *bio)
Record that the next bio is blocked.
Definition tls.c:338
static int fr_bio_tls_write_wait(fr_bio_tls_t *my, bool wait)
Start or stop waiting for the fd to have room for more records.
Definition tls.c:125
static ssize_t fr_bio_tls_write_data(fr_bio_tls_t *my, void const *buffer, size_t size)
Encrypt application data, and write the encrypted records to the next bio.
Definition tls.c:393
size_t clean_len
How much decrypted data is in clean_out.
Definition tls.c:90
static bool fr_bio_tls_pending(fr_bio_tls_t *my)
Check whether OpenSSL holds data which the bio has not yet processed.
Definition tls.c:496
static int fr_bio_tls_decrypt(fr_bio_tls_t *my)
Decrypt one record which is waiting in OpenSSL.
Definition tls.c:511
static void fr_bio_tls_fd_connected(fr_bio_t *fd_bio)
The fd is connected.
Definition tls.c:1002
static fr_event_update_t const resume_write[]
Definition tls.c:107
static fr_event_update_t const pause_read[]
Definition tls.c:765
FR_BIO_COMMON
Definition tls.c:67
ssize_t next_error
The error which the next bio returned from a write.
Definition tls.c:93
bool write_wait
The write event of the fd is active.
Definition tls.c:82
static ssize_t fr_bio_tls_write_wait_connected(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, size_t size)
Definition tls.c:758
int fd
The fd of the next bio.
Definition tls.c:76
static void fr_bio_tls_failed(fr_bio_tls_t *my, fr_tls_connection_fail_t reason, int error)
Stop the connection, and tell the application.
Definition tls.c:171
static void fr_bio_tls_fd_error(fr_bio_t *fd_bio)
The fd failed to connect.
Definition tls.c:1015
SSL_CTX * ssl_ctx
Used to create the TLS session.
Definition tls.c:78
static fr_event_update_t const resume_read[]
Definition tls.c:770
static void fr_bio_tls_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
The fd failed.
Definition tls.c:875
static int fr_bio_tls_write_resume(fr_bio_t *bio)
Write the encrypted records which were waiting for the next bio.
Definition tls.c:361
bool write_blocked
The next bio is blocked.
Definition tls.c:95
static ssize_t fr_bio_tls_read_eof(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Return the decrypted data which remains after the next bio reached EOF, and then return EOF.
Definition tls.c:677
static int fr_bio_tls_eof(fr_bio_t *bio)
Handle EOF from the next bio.
Definition tls.c:713
int fr_bio_tls_connect(fr_bio_tls_t *my)
Take the connection through every state which comes before application data.
Definition tls.c:1262
static void fr_bio_tls_el_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
The fd is ready for reads.
Definition tls.c:802
fr_bio_tls_t * fr_bio_tls_alloc(request_t *request, fr_bio_t *next, SSL_CTX *ssl_ctx, bool client, bool client_cert)
Allocate a TLS bio.
Definition tls.c:1115
int fr_bio_tls_dtls_clienthello(fr_bio_tls_t *my, uint8_t const *data, size_t data_len)
Give a DTLS server the datagram which started the connection.
Definition tls.c:1188
bool client_cert
For a server, require a client certificate.
Definition tls.c:80
static void fr_bio_tls_conn_recv_resume(fr_tls_connection_t *conn)
Resume reading from the connection.
Definition tls.c:779
static int fr_bio_tls_flush(fr_bio_tls_t *my)
Write any pending encrypted records to the next bio.
Definition tls.c:324
uint8_t * clienthello
For DTLS, the datagram which started the connection.
Definition tls.c:98
static int fr_bio_tls_shutdown(fr_bio_t *bio)
Disconnect the bio from the fd.
Definition tls.c:736
fr_tls_connection_t * conn
The TLS connection, which is a child of the bio.
Definition tls.c:69
static size_t fr_bio_tls_clean_read(fr_bio_tls_t *my, void *buffer, size_t size)
Copy decrypted data which the caller has not yet read into the caller's buffer.
Definition tls.c:547
size_t clienthello_len
Length of clienthello.
Definition tls.c:99
static int fr_bio_tls_destructor(fr_bio_tls_t *my)
Send any close_notify, stop the fd events, cancel the request, and then run the common bio destructor...
Definition tls.c:1075
static int fr_bio_tls_handshake_start(fr_bio_tls_t *my)
The fd is connected, so start the TLS connection.
Definition tls.c:902
static ssize_t fr_bio_tls_conn_write(fr_tls_connection_t *conn, uint8_t const *data, size_t size)
Write encrypted records to the next bio.
Definition tls.c:202
static fr_bio_tls_t * fr_bio_tls_from_fd(fr_bio_t *fd_bio)
Find the TLS bio from the fd bio.
Definition tls.c:990
static void fr_bio_tls_next_resume(fr_bio_t *next)
Tell the next bio that it can write again.
Definition tls.c:159
fr_event_list_t * el
The event list of the interpreter.
Definition tls.c:72
static fr_event_update_t const pause_write[]
Definition tls.c:102
static ssize_t fr_bio_tls_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Definition tls.c:457
The TLS bio.
Definition tls.c:66
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1244
Stores all information relating to an event list.
Definition event.c:415
long int ssize_t
unsigned char uint8_t
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
#define fr_assert(_expr)
Definition rad_assert.h:37
static rs_t * conf
Definition radsniff.c:52
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
#define talloc_asprintf
Definition talloc.h:151
static fr_event_list_t * el
int fd
File descriptor if this is a live socket.
Definition socket.h:86
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Definition socket.h:84
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
static fr_slen_t data
Definition value.h:1366