The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
unit_test_bio.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 * $Id: 0d75f1fc3e5bb6721bc092530dcffd8dd193bb42 $
18 *
19 * @file unit_test_bio.c
20 * @brief TLS server test framework, using the TLS bio
21 *
22 * This program loads the "tls" protocol and reads two top-level sections
23 * from unit_test_tls.conf. The "tls" section supplies the TLS server
24 * context, and takes exactly the items that a TLS configuration takes
25 * anywhere else in the server. The "unit_test_tls" section supplies what
26 * steers the test program itself, which is the address of the listening TCP
27 * socket and whether a client certificate is required.
28 *
29 * One connection is accepted, and the TLS handshake for that connection is
30 * run to completion.
31 *
32 * This program is unit_test_tls.c reworked to use the TLS bio in
33 * src/lib/bio/tls.c. The program opens the sockets as fd bios, and the
34 * TLS bio runs everything else for a connection: the connect, the handshake,
35 * the IO, and the policies. The program supplies the request, the
36 * interpreter, and two callbacks, `connected` and `failed`. The section is
37 * still called "unit_test_tls", so the configuration files of unit_test_tls
38 * work here too. Setting `drop_datagram` is an error, as the TLS bio does
39 * not offer a place to drop a datagram.
40 *
41 * The handshake runs on a persistent asynchronous interpreter, the same way
42 * radiusd runs one. A persistent interpreter keeps the handshake state
43 * across the several rounds of a single connection.
44 *
45 * The "virtual_server" item in the "tls" section names a virtual server.
46 * That virtual server supplies the "verify certificate", "load session",
47 * "store session" and "clear session" sections which the handshake calls.
48 *
49 * To run the test, start unit_test_tls with the test configuration, then
50 * connect to the listening socket:
51 *
52 * @code
53 * ./scripts/bin/unit_test_tls -d src/tests/tls -X
54 *
55 * openssl s_client -connect 127.0.0.1:2083 \
56 * -cert raddb/certs/rsa/client.pem \
57 * -key raddb/certs/rsa/client.key -pass pass:whatever \
58 * -CAfile raddb/certs/rsa/ca.pem
59 * @endcode
60 *
61 * src/tests/tls/unit_test_tls.conf documents every configuration item.
62 *
63 * @copyright 2025 The FreeRADIUS server project
64 */
65RCSID("$Id: 0d75f1fc3e5bb6721bc092530dcffd8dd193bb42 $")
66
67#include <freeradius-devel/server/base.h>
68#include <freeradius-devel/server/module_rlm.h>
69
70#include <freeradius-devel/io/listen.h>
71#include <freeradius-devel/io/thread.h>
72
73#include <freeradius-devel/tls/base.h>
74#include <freeradius-devel/tls/dtls.h>
75#include <freeradius-devel/tls/strerror.h>
76#include <freeradius-devel/tls/version.h>
77#include <freeradius-devel/tls/connection.h>
78#include <freeradius-devel/bio/base.h>
79#include <freeradius-devel/bio/fd.h>
80#include <freeradius-devel/bio/tls.h>
81#include <freeradius-devel/util/misc.h>
82
83#include <freeradius-devel/unlang/base.h>
84#include <freeradius-devel/unlang/function.h>
85#include <freeradius-devel/unlang/interpret.h>
86
87#include <freeradius-devel/util/socket.h>
88
89#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
90
91#ifdef HAVE_GETOPT_H
92# include <getopt.h>
93#endif
94
95#include <sys/wait.h>
96
97#define EXIT_WITH_FAILURE \
98do { \
99 ret = EXIT_FAILURE; \
100 goto cleanup; \
101} while (0)
102
103char const *radiusd_version = RADIUSD_VERSION_BUILD("unit_test_bio");
104
106static fr_dict_t const *dict_tls;
107
110 { .out = &dict_freeradius, .proto = "freeradius" },
111 { .out = &dict_tls, .proto = "tls" },
113};
114
115/** The "unit_test_tls" section
116 *
117 * The items at the top level steer the test program and do not configure
118 * TLS. The TLS configuration for each role is a subsection.
119 * `fr_tls_server_config` and `fr_tls_client_config` parse the subsections,
120 * so each subsection takes exactly the items that a `tls { ... }` section
121 * takes anywhere else in the server.
122 */
123/** One transport's socket, named by `transport` in the configuration
124 *
125 */
126typedef struct {
127 fr_ipaddr_t ipaddr; //!< Address of the listening socket.
128 uint16_t port; //!< Port of the listening socket.
130
131typedef struct {
132 char const *transport; //!< "tcp" or "udp", naming the subsection below
133 ///< which holds the socket.
134 int socket_type; //!< SOCK_STREAM or SOCK_DGRAM, from `transport`.
135
136 unit_test_bio_socket_t tcp; //!< The `tcp { ... }` subsection.
137 unit_test_bio_socket_t udp; //!< The `udp { ... }` subsection.
138
139 fr_ipaddr_t ipaddr; //!< Address of the listening socket, copied from
140 ///< whichever subsection `transport` named.
141 uint16_t port; //!< Port of the listening socket, and the default
142 ///< port for -s.
143 uint32_t drop_datagram; //!< Drop this datagram rather than sending it,
144 ///< counting from one. Zero drops nothing.
145 bool require_client_certificate; //!< Whether the client has to present a certificate.
146
147 fr_tls_conf_t *server; //!< The `server { ... }` subsection.
148 fr_tls_conf_t *client; //!< The `client { ... }` subsection, used with `-s`.
150
151/** The socket items, parsed once per transport subsection
152 *
153 */
163
164/** Protocols which the test program can negotiate
165 *
166 * The names are the ones that RFC 9765 defines for RADIUS, plus one name that no entry of the RFC matches, which
167 * the tests use as a peer that shares nothing with the other end.
168 */
174
175static fr_tls_alpn_t const unit_test_bio_alpn_table[] = {
176 { .id = UTB_ALPN_RADIUS_1_1, .name = "radius/1.1", .conf_name = "radius_v1_1", .flags = FR_TLS_ALPN_STICKY },
177 { .id = UTB_ALPN_RADIUS_1_0, .name = "radius/1.0", .conf_name = "radius_v1_0", .flags = FR_TLS_ALPN_DEFAULT },
178 { .id = UTB_ALPN_HTTP_1_1, .name = "http/1.1", .conf_name = "http_1_1" },
179 FR_TLS_ALPN_TABLE_TERMINATOR
180};
181
182/** The `alpn` subsection has no rules of its own
183 *
184 * fr_tls_alpn_parse() reads the items of the section itself, so the section needs no rules to parse them.
185 */
189
190/** The rules for the `server` and `client` subsections
191 *
192 * Each includes the rules of the TLS library, and adds the `alpn` subsection, which only the application can
193 * add because only the application has the table. `virtual_server` is at offset 0 of #fr_tls_conf_t, so the
194 * rules of the library keep the offsets that they were written with.
195 */
197 { FR_CONF_OFFSET_REF(fr_tls_conf_t, virtual_server, fr_tls_server_config) },
199 .func = fr_tls_alpn_parse, .uctx = unit_test_bio_alpn_table },
201};
202
204 { FR_CONF_OFFSET_REF(fr_tls_conf_t, virtual_server, fr_tls_client_config) },
206 .func = fr_tls_alpn_parse, .uctx = unit_test_bio_alpn_table },
208};
209
211 /*
212 * `transport` names a subsection, the way a listener's
213 * `transport` does, see common_transport_parse() in
214 * src/lib/bio/fd_config.c. The socket items live in that
215 * subsection rather than here, so that a configuration which
216 * names one transport cannot carry another one's settings.
217 */
218 { FR_CONF_OFFSET("transport", unit_test_bio_conf_t, transport), .dflt = "tcp" },
219
224
225 /*
226 * Lose a datagram on purpose, so that a test can see the
227 * retransmission which recovers from it. Zero loses nothing.
228 */
229 { FR_CONF_OFFSET("drop_datagram", unit_test_bio_conf_t, drop_datagram) },
230
231 { FR_CONF_OFFSET("require_client_certificate", unit_test_bio_conf_t, require_client_certificate),
232 .dflt = "no" },
233
234 /*
235 * A test file may hold only the role that the test runs, so
236 * either subsection may be missing.
237 */
239 .subcs_type = "fr_tls_conf_t" },
241 .subcs_type = "fr_tls_conf_t" },
242
244};
245
246/** State of the test program
247 *
248 * The interpreter and the runnable heap live for the whole of the connection.
249 * A TLS handshake takes several rounds, and each round may yield while a
250 * virtual server section runs. The interpreter and the runnable heap
251 * therefore cannot be allocated once per round.
252 */
253typedef struct {
254 fr_event_list_t *el; //!< Event list everything runs on.
255 unlang_interpret_t *intp; //!< Interpreter for the connection.
256 fr_heap_t *runnable; //!< Requests the interpreter has marked runnable.
257 int yielded; //!< How many requests are currently yielded.
258
259 unit_test_bio_conf_t conf; //!< Parsed "unit_test_tls" section.
260 SSL_CTX *ssl_ctx; //!< Context built from the "tls" section.
261
262 fr_bio_t *listen_bio; //!< Listening fd bio.
263 fr_bio_fd_config_t listen_cfg; //!< Config of `listen_bio`. The bio keeps the pointer.
264 fr_bio_t *fd_bio; //!< The fd bio of the connection being run.
265 fr_bio_fd_config_t fd_cfg; //!< Config of `fd_bio`. The bio keeps the pointer.
266 fr_bio_tls_t *bio; //!< The TLS bio of the connection being run.
267 fr_tls_connection_t *conn; //!< The connection of `bio`. The hooks of this
268 ///< program read this connection.
269 request_t *request; //!< The request the connection runs under.
270
271
272 unsigned int count; //!< How many connections to run.
273 bool alert; //!< Reject the peer with a TLS alert, see -A.
274 bool invalid; //!< Mark the session invalid mid-handshake, see -I.
275 bool nonblock; //!< Put the connection socket in non-blocking mode, see -B.
276 bool client; //!< Act as the client and connect to a server,
277 ///< rather than accept a connection. Copied into
278 ///< every connection this program allocates.
279
280 bool app_data; //!< Send one byte of application data once the
281 ///< handshake is done, see -P.
282
283 bool hold; //!< Once connected, send nothing and stay
284 ///< connected, see -H.
285
286 bool reject; //!< Reject the session once the handshake
287 ///< succeeds, see -R.
288 bool network_fail; //!< Fail the session with a network error once
289 ///< the handshake succeeds, see -N.
290 bool abort_pending; //!< Free the connection once the handshake
291 ///< succeeds, while `store session` is still
292 ///< queued, see -Q.
293 unsigned int connections; //!< How many connections have been run so far.
294 fr_ipaddr_t server_ipaddr; //!< Server named by -s.
295 uint16_t server_port; //!< Port from -s, or from the configuration.
296
297 int sockfd; //!< Listening socket.
298 fr_event_fd_t *accept_ef; //!< Read event for sockfd, while waiting in
299 ///< tls_socket_accept().
300 bool accepting; //!< Waiting in tls_socket_accept().
301
302 char const *command; //!< Command to run, see -e.
303 pid_t command_pid; //!< Command started by -e, or 0.
304 fr_event_pid_t const *command_ev; //!< Waits for the command to exit.
305 bool command_exited; //!< The command has exited.
306 bool command_waiting; //!< Waiting in tls_command_wait().
307
308 int fd; //!< Accepted or connected socket.
309
310 uint8_t *first; //!< The datagram which named the peer, read from
311 ///< the listening socket before the connection's
312 ///< socket existed. Handed to the connection once
313 ///< the connection is running.
314 size_t first_len; //!< Length of `first`.
315 unsigned char *alpn; //!< Protocol list built by -L, in the wire format
316 ///< fr_tls_conf_t expects. NULL means no ALPN.
317 bool alpn_required; //!< Set by -l.
318
319 bool connected; //!< The `connected` callback has run.
320 bool done; //!< Set once the connection has a result.
321 int ret; //!< Exit status.
323
324static void usage(main_config_t const *config, int status);
325static void tls_request_failed(unit_test_bio_t *utt);
326
327/*
328 * Interpreter callbacks.
329 *
330 * The callbacks below mirror the set in
331 * src/lib/unlang/interpret_synchronous.c and src/lib/io/coord_pair.c. The
332 * one difference is that a post-event handler drains the runnable heap,
333 * rather than a loop inside the caller, so the event loop decides when
334 * each request runs.
335 */
336
337/** Schedule a request, once
338 *
339 * A request reaches the heap from two directions: the interpreter creating it,
340 * and the interpreter marking it runnable again. Inserting a request which is
341 * already on the heap puts it there twice, and popping it then returns a
342 * request which the heap still holds, which the interpreter refuses to run.
343 */
345{
346 if (fr_heap_entry_inserted(request->runnable)) return;
347
349}
350
351/** An internal request created by the interpreter has to run on ours
352 *
353 * The subrequests created for the "verify certificate" section and for the
354 * session cache sections reach this callback.
355 *
356 * fr_tls_call_push() pushes each of them as a detachable subrequest, which
357 * the parent waits on rather than running inline, so the subrequest has to be
358 * scheduled here or nothing ever runs it.
359 */
360static void _request_init_internal(request_t *request, void *uctx)
361{
362 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
363
364 RDEBUG3("Initialising internal request");
365
367
368 /*
369 * interpret_child_init() calls this, and nothing else schedules
370 * the child, so a subrequest which is not put on the heap here
371 * never runs at all.
372 */
374}
375
377{
378 RDEBUG3("Done external request");
379
380 /*
381 * main() allocated the request, and the request has to survive
382 * until the connection is done with, so this callback does not
383 * free the request.
384 */
385}
386
388{
389 RDEBUG3("Done internal request");
390
391 /* The code which created the internal request frees the request */
392}
393
395{
396 RDEBUG3("Done detached request");
397
398 /*
399 * Nothing else can free a detached request, so this callback
400 * frees the detached request.
401 */
403}
404
405static void _request_detach(request_t *request, UNUSED void *uctx)
406{
407 RDEBUG3("Request detached");
408
409 if (request_detach(request) < 0) RPEDEBUG("Failed detaching request");
410}
411
412static void _request_yield(request_t *request, void *uctx)
413{
414 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
415
416 utt->yielded++;
417
418 RDEBUG3("Request yielded");
419}
420
421static void _request_resume(request_t *request, UNUSED void *uctx)
422{
423 RDEBUG3("Request resumed");
424}
425
426static void _request_runnable(request_t *request, void *uctx)
427{
428 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
429
430 fr_assert(utt->yielded > 0);
431 utt->yielded--;
432
434}
435
436static bool _request_scheduled(request_t const *request, UNUSED void *uctx)
437{
438 return fr_heap_entry_inserted(request->runnable);
439}
440
441/** Stop the event loop, recording why
442 *
443 * The connection carries the result, so the exit status is decided here
444 * rather than by the TLS bio.
445 */
446static void tls_done(unit_test_bio_t *utt)
447{
448 if (utt->done) return;
449
450 utt->done = true;
451 /*
452 * With -H the peer ends a connection which was already up, and the end is
453 * not a failure.
454 */
455 utt->ret = (utt->conn && utt->conn->failed && !(utt->hold && utt->connected)) ? EXIT_FAILURE : EXIT_SUCCESS;
456
457 fr_event_loop_exit(utt->el, 1);
458}
459
460/** The TLS bio says the connection failed, at any point
461 *
462 */
463static void tls_bio_failed(fr_bio_t *bio)
464{
465 unit_test_bio_t *utt = talloc_get_type_abort(bio->uctx, unit_test_bio_t);
466
467 tls_done(utt);
468}
469
470/** Record a failure this program cannot recover from, and stop
471 *
472 * tls_done() reads the result from the connection, so a failure has to reach
473 * the connection before the program stops.
474 */
476{
477 if (utt->conn) utt->conn->failed = TLS_CONNECTION_FAIL_APPLICATION;
478 tls_done(utt);
479}
480
481/** Something happened on a connection which -H holds open
482 *
483 * The TLS bio removed its fd events when the connection connected, so the
484 * program watches the fd itself. Reading the bio is how the program learns
485 * that the peer has gone.
486 */
487static void _tls_hold_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
488{
489 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
490 uint8_t buffer[256];
491 ssize_t slen;
492
493 do {
494 slen = fr_bio_read((fr_bio_t *) utt->bio, NULL, buffer, sizeof(buffer));
495 } while (slen > 0);
496
497 if (slen == fr_bio_error(IO_WOULD_BLOCK)) return;
498
499 if ((slen < 0) || fr_bio_fd_info(utt->fd_bio)->eof) {
500 DEBUG2("The peer ended the held connection");
501 tls_done(utt);
502 }
503}
504
505static void _tls_hold_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags,
506 UNUSED int fd_errno, void *uctx)
507{
508 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
509
510 DEBUG2("The held connection failed");
511 tls_done(utt);
512}
513
514/** The connection is up, and can carry application data
515 *
516 * The TLS bio removed its fd events before calling this function. A program
517 * which had data to send would send it with fr_bio_write(), and close when it
518 * was done.
519 */
521{
522 unit_test_bio_t *utt = talloc_get_type_abort(bio->uctx, unit_test_bio_t);
523
524 DEBUG2("Connection is up and can carry application data");
525
526 /*
527 * Send one byte of application data, which tells the peer that
528 * every session ticket we were going to send has already gone.
529 * The connection only reaches TLS_CONNECTION_CONNECTED when every
530 * ticket section has run.
531 */
532 if (utt->app_data && !utt->client) {
533 uint8_t zero = 0x00;
534
535 if (fr_bio_write(bio, NULL, &zero, sizeof(zero)) != (ssize_t) sizeof(zero)) {
536 PERROR("Failed sending the application data");
538 return;
539 }
540
541 DEBUG("Sent one byte of application data");
542 }
543
544 /*
545 * -H holds the connection open with nothing more to say. A
546 * peer which is still owed a session ticket or a byte of
547 * application data then waits for something which never
548 * arrives, which is what the post-handshake timer is for.
549 *
550 * The connection still ends: the peer gives up, closes, and
551 * the TLS bio fails the connection when it sees EOF.
552 */
553 utt->connected = true;
554
555 if (utt->hold) {
556 DEBUG("Holding the connection open, sending nothing");
557
558 if (fr_event_fd_insert(utt, NULL, utt->el, utt->fd, _tls_hold_read, NULL, _tls_hold_error, utt) < 0) {
559 PERROR("Failed watching the held connection");
561 }
562 return;
563 }
564
565 tls_done(utt);
566}
567
568/** Drain the runnable heap once per pass of the event loop
569 *
570 * A handshake round yields whenever the round runs a virtual server section.
571 * When that section finishes, unlang_interpret_mark_runnable() puts the
572 * request on the runnable heap, and this drains the heap again.
573 */
575{
576 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
578
579 /*
580 * Stand in for an application which rejects a peer outright.
581 *
582 * `pending` says a record has arrived and the handshake has not
583 * read it yet. This hook used to live in _tls_connection_read(),
584 * where its position said the same thing, and the comment there
585 * warned that raising the alert before a record arrives sends it
586 * into an empty handshake, where it is lost. The test cannot
587 * tell: removing this test passes six suite runs out of six,
588 * because a server runs no handshake round until a record
589 * arrives. A client runs one straight away, so keep the test.
590 *
591 * Raising the alert later, with the other two hooks below, is
592 * too late. Those run after the round, and the round which
593 * would have carried the alert has gone.
594 */
595 /*
596 * Stand in for a record which FreeRADIUS itself refuses.
597 *
598 * fr_tls_session_msg_cb() marks such a session invalid and
599 * lets the round finish. The round after that refuses to
600 * continue, records FR_ERROR_VALUE_SESSION_INVALID, and sends
601 * the peer a fatal alert. Setting the flag here reaches that
602 * second round without having to feed the handshake a
603 * malformed record.
604 *
605 * `pending` places this before a round, for the same reason
606 * the alert hook below is placed there.
607 */
608 if (utt->invalid && utt->conn->tls_session && utt->conn->pending &&
609 (utt->connections == utt->count)) {
610 utt->invalid = false;
611 utt->conn->tls_session->invalid = true;
612 }
613
614 if (utt->alert && utt->conn->tls_session && utt->conn->pending &&
615 (utt->connections == utt->count)) {
616 utt->alert = false;
617
618 if (fr_tls_session_alert(utt->conn->request, utt->conn->tls_session,
619 SSL3_AL_FATAL, SSL_AD_ACCESS_DENIED) < 0) {
620 ERROR("Failed raising the TLS alert");
622 return;
623 }
624 }
625
626 while (fr_heap_pop((void **)&request, &utt->runnable) == 0) {
627 if (!request) break;
628
630
631 /*
632 * Only the connection's own request advances the
633 * handshake. A subrequest returns the subrequest's
634 * result through the interpreter.
635 *
636 * The connection's own IO is not done here any more.
637 * tls_connection_frame_process() does it, inside
638 * src/lib/tls, at the point the connection is about to
639 * wait for the peer. A real listener has no hook here,
640 * so nothing which a real listener cannot do belongs
641 * here either.
642 */
643 if (request == utt->conn->request) {
644 /*
645 * Stand in for an application which rejects a
646 * session after the handshake succeeded. The
647 * handshake has finished, so OpenSSL has asked
648 * for the session to be cached, and the
649 * connection is about to run its cache
650 * operations. Failing it here makes those
651 * operations a deny and a clear rather than a
652 * store, which is what rlm_eap_tls does when
653 * policy rejects.
654 */
655 if ((utt->reject || utt->network_fail) && (utt->connections == utt->count) &&
656 fr_tls_session_is_init_finished(utt->conn->tls_session)) {
657 INFO("%s the session after a successful handshake",
658 utt->network_fail ? "Failing, with a network error," : "Rejecting");
659
660 /*
661 * Record the reason, and nothing else.
662 * The handshake has finished, so OpenSSL
663 * has asked for the session to be cached,
664 * and the connection frame is about to run
665 * that cache work. The frame reads
666 * `failed` to decide whether the work is a
667 * store or a deny and a clear, which is
668 * what rlm_eap_tls does when policy
669 * rejects.
670 *
671 * fr_tls_connection_failed() would instead
672 * report the connection finished here, and
673 * the queued cache work would never be
674 * drained. src/lib/tls/session.c asserts
675 * on exactly that.
676 */
677 utt->conn->failed = utt->network_fail ? TLS_CONNECTION_FAIL_SYSCALL :
678 TLS_CONNECTION_FAIL_APPLICATION;
679 }
680
681 /*
682 * Free the connection while `store session` is
683 * still queued. The handshake has finished, so
684 * OpenSSL has asked for the session to be cached
685 * and the connection is about to run that work,
686 * but it has not run yet. Closing now frees the
687 * session with the operation still pending, which
688 * is the path session.c used to assert on and now
689 * discards. This is deterministic: it does not
690 * depend on when the peer closes.
691 */
692 if (utt->abort_pending && (utt->connections == utt->count) &&
693 fr_tls_session_is_init_finished(utt->conn->tls_session)) {
694 INFO("Aborting the connection with cache operations still queued");
695 tls_done(utt);
696 return;
697 }
698 }
699
700 if (utt->done) return;
701 }
702}
703
704/** Open the listening socket described by the "unit_test_tls" section
705 *
706 * The listening socket is an fd bio for both transports. fr_bio_fd_accept()
707 * accepts a stream connection, or takes the first datagram of a peer as a
708 * connection, see tls_socket_accept_one().
709 */
711{
712 fr_ipaddr_t ipaddr = utt->conf.ipaddr;
713 uint16_t port = utt->conf.port;
714 bool dgram = (utt->conf.socket_type == SOCK_DGRAM);
715
716 /*
717 * The items are not marked as required, because a client has no
718 * listening socket, and so needs neither of them.
719 */
720 if ((ipaddr.af == AF_UNSPEC) || !port) {
721 ERROR("Both 'ipaddr' and 'port' must be set in the 'unit_test_tls' section "
722 "when listening for a connection");
723 return -1;
724 }
725
728 .socket_type = utt->conf.socket_type,
729 .transport = dgram ? "udp" : "tcp",
730 .transport_type = dgram ? FR_BIO_FD_TRANSPORT_UDP : FR_BIO_FD_TRANSPORT_TCP,
731 .server = true,
732 .src_ipaddr = ipaddr,
733 .src_port = port,
734 .backlog = 8,
735 .backlog_is_set = true,
736 .reuse_port = dgram, /* the connection's socket shares the port */
737 .async = true, /* so that accepting does not block */
738 };
739
740 utt->listen_bio = fr_bio_fd_alloc(utt, &utt->listen_cfg, 0);
741 if (!utt->listen_bio) {
742 PERROR("Failed opening %s socket", utt->conf.transport);
743 return -1;
744 }
745
747
748 INFO("Listening on %pV port %u using %s", fr_box_ipaddr(ipaddr), port, utt->conf.transport);
749 return 0;
750}
751
752/** Accept a connection, if the listening socket has one queued
753 *
754 * @return
755 * - 1 if a connection was accepted.
756 * - 0 if no connection is queued.
757 * - -1 on error.
758 */
760{
761 int fd, rcode;
762
763 /*
764 * A datagram socket is not listening, so there is nothing to
765 * accept. The first ClientHello names the peer, and the
766 * connection runs on a second socket which is connect()ed to
767 * that peer: the kernel then delivers that peer's datagrams to
768 * the connected socket in preference to this one, and read()
769 * and write() work as they do on a stream.
770 *
771 * The connection cannot run on this socket. connect() is a
772 * property of the socket rather than of the descriptor, so
773 * connecting this one, or any descriptor duplicated from it,
774 * would leave the listening socket bound to one peer for the
775 * rest of the program.
776 */
777 if (utt->conf.socket_type == SOCK_DGRAM) {
778 uint8_t buf[FR_TLS_MAX_PACKET_SIZE];
779 ssize_t slen;
780
781 /*
782 * fr_bio_fd_accept() takes the first datagram of a peer as a connection: it opens
783 * a socket which is connect()ed to that peer, and the kernel then delivers that
784 * peer's datagrams to the connected socket in preference to the listening socket.
785 * The datagram which named the peer is no longer in the socket, and the first read
786 * of the new bio returns the datagram. The program reads the datagram now,
787 * because the datagram decides whether there is a connection at all.
788 *
789 * It is given to the connection once the connection is running, see
790 * _tls_connection_start().
791 */
792 for (;;) {
793 int accepted = fr_bio_fd_accept(utt, &utt->fd_bio, utt->listen_bio);
794
795 if (accepted == 0) return 0;
796
797 if (accepted < 0) {
798 PERROR("Failed accepting the first datagram");
799 return -1;
800 }
801
802 slen = fr_bio_read(utt->fd_bio, NULL, buf, sizeof(buf));
803 if (slen < 0) {
804 PERROR("Failed reading the first datagram");
805 TALLOC_FREE(utt->fd_bio);
806 return -1;
807 }
808
809 /*
810 * Filter input packets by ClientHello.
811 *
812 * Even if we connect() sockets, the connected socket could go away. Packets for
813 * that (old) socket are then received by the original accept socket. We don't
814 * know what to do with them, as the DTLS session goes away when the socket goes
815 * away.
816 *
817 * The main accept socket doesn't read its FD while the DTLS session is
818 * running, so packets might pile up here. This is only a problem for
819 * unit_test_bio, as a real server would keep reading the accept socket.
820 *
821 * A server which takes one of those old datagrams as a new connection request
822 * will try to start a connection to a peer which has already gone. The real
823 * peer's handshake stays queued here, and the handshake never completes.
824 *
825 * The solution (hack) is to just discard anything that doesn't look like a
826 * ClientHello.
827 *
828 * A ClientHello is a handshake record in epoch 0 whose first message is a
829 * client_hello. RFC 6347 Section 4.1 gives the record header, which is
830 * DTLS1_RT_HEADER_LENGTH octets of type, version, epoch, sequence number, and
831 * length. Section 4.3.2 gives the handshake message types. A discarded
832 * datagram looks to the peer like a lost datagram, and the peer retransmits, see
833 * Section 4.2.4.
834 */
835 if ((slen > (ssize_t) DTLS1_RT_HEADER_LENGTH) &&
836 (buf[0] == SSL3_RT_HANDSHAKE) &&
837 (buf[3] == 0) && (buf[4] == 0) &&
838 (buf[DTLS1_RT_HEADER_LENGTH] == SSL3_MT_CLIENT_HELLO)) break;
839
840 DEBUG2("Discarding a %zd byte datagram which does not start a connection", slen);
841 TALLOC_FREE(utt->fd_bio);
842 }
843
844 MEM(utt->first = talloc_memdup(utt, buf, (size_t) slen));
845 utt->first_len = (size_t) slen;
846
847 utt->fd = fr_bio_fd_info(utt->fd_bio)->socket.fd;
848
849 return 1;
850 }
851
852 rcode = fr_bio_fd_accept(utt, &utt->fd_bio, utt->listen_bio);
853 if (rcode == 0) return 0;
854
855 if (rcode < 0) {
856 PERROR("Failed accepting connection");
857 return -1;
858 }
859
860 fd = fr_bio_fd_info(utt->fd_bio)->socket.fd;
861
862 /*
863 * Some systems give the accepted socket the O_NONBLOCK flag of
864 * the listening socket, and some don't. Set the flag to what
865 * -B asks for.
866 */
867 if (utt->nonblock ? (fr_nonblock(fd) < 0) : (fr_blocking(fd) < 0)) {
868 PERROR("Failed setting the connection socket mode");
869 TALLOC_FREE(utt->fd_bio);
870 return -1;
871 }
872
873 utt->fd = fd;
874
875 return 1;
876}
877
878/** Accept the connection once the listening socket is readable
879 *
880 */
881static void _tls_socket_accept(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
882{
883 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
884
885 if (tls_socket_accept_one(utt) == 0) return;
886
888}
889
890/** Stop waiting for a connection if the listening socket fails
891 *
892 */
893static void _tls_socket_error(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, UNUSED void *uctx)
894{
895 ERROR("Listening socket failed: %s", fr_syserror(fd_errno));
896
898}
899
900/** Start waiting for a connection, from inside the event loop
901 *
902 * See tls_connection_run() for why this runs from inside the event loop.
903 */
905{
906 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
907
908 /*
909 * The command exited before this connection, so the only
910 * connection which can arrive is one already queued.
911 */
912 if (utt->command_exited) {
913 if (tls_socket_accept_one(utt) == 0) ERROR("The command exited without connecting");
914
916 return;
917 }
918
919 if (fr_event_fd_insert(utt, &utt->accept_ef, el, utt->sockfd,
920 _tls_socket_accept, NULL, _tls_socket_error, utt) < 0) {
921 PERROR("Failed adding the listening socket to the event loop");
923 }
924}
925
926/** Wait for a connection, or for the command started by -e to exit without connecting
927 *
928 * @return
929 * - 0 with utt->fd set to the accepted connection.
930 * - -1 on failure.
931 */
933{
934 fr_event_user_t *ev = NULL;
935
936 INFO("Waiting for a connection");
937
938 if (fr_event_user_insert(utt, utt->el, &ev, true, _tls_socket_accept_start, utt) < 0) {
939 PERROR("Failed scheduling the wait for a connection");
940 return -1;
941 }
942
943 utt->accepting = true;
944 (void) fr_event_loop(utt->el);
945 utt->accepting = false;
946
947 if (utt->accept_ef) {
949 utt->accept_ef = NULL;
950 }
951 TALLOC_FREE(ev);
952
953 return utt->fd_bio ? 0 : -1;
954}
955
956/** Record that the command started by -e has exited
957 *
958 * If the server is waiting for a connection, a command which exits has either
959 * connected already, and the connection is queued, or never will.
960 */
961static void _tls_command_exit(fr_event_list_t *el, pid_t pid, int status, void *uctx)
962{
963 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
964
965 utt->command_exited = true;
966
967 if (WIFEXITED(status)) {
968 INFO("Command (PID %ld) exited with status %d", (long)pid, WEXITSTATUS(status));
969 } else if (WIFSIGNALED(status)) {
970 INFO("Command (PID %ld) was killed by signal %d", (long)pid, WTERMSIG(status));
971 }
972
973 if (utt->accepting) {
974 if (tls_socket_accept_one(utt) == 0) ERROR("The command exited without connecting");
975
977 return;
978 }
979
981}
982
983/** Start the command named by -e
984 *
985 * A server starts the command once it is listening. A command which connects
986 * to the server connects to a socket which is already listening, so it can't
987 * be refused, and the server sees the command exit, so it never waits for a
988 * peer which has gone.
989 *
990 * A client starts the command once its first connection succeeds.
991 */
992static int tls_command_start(unit_test_bio_t *utt, char const *command)
993{
994 pid_t pid;
995 sigset_t set;
996
997 pid = fork();
998 if (pid < 0) {
999 ERROR("Failed forking the command: %s", fr_syserror(errno));
1000 return -1;
1001 }
1002
1003 if (pid == 0) {
1004 /*
1005 * Only async-signal-safe calls between fork() and exec.
1006 * The command gets neither of our sockets, so the peer
1007 * sees the connection close when we close it, and it
1008 * gets an empty signal mask, not ours.
1009 *
1010 * Only close() the fds. Freeing the listen bio would
1011 * shutdown() the listening socket, which is shared
1012 * with the parent after fork().
1013 */
1014 if (utt->sockfd >= 0) close(utt->sockfd);
1015 if (utt->fd >= 0) close(utt->fd);
1016 sigemptyset(&set);
1017 sigprocmask(SIG_SETMASK, &set, NULL);
1018 execl("/bin/sh", "sh", "-c", command, (char *)NULL);
1019 _exit(127);
1020 }
1021
1022 INFO("Started command (PID %ld): %s", (long)pid, command);
1023
1024 utt->command_pid = pid;
1025
1026 if (fr_event_pid_wait(utt, utt->el, &utt->command_ev, pid, _tls_command_exit, utt) < 0) {
1027 PERROR("Failed waiting for the command to exit");
1028 return -1;
1029 }
1030
1031 return 0;
1032}
1033
1034/** Wait for the command started by -e to exit, so its output is complete
1035 *
1036 */
1038{
1039 if (!utt->command_pid || utt->command_exited) return;
1040
1041 utt->command_waiting = true;
1042 (void) fr_event_loop(utt->el);
1043 utt->command_waiting = false;
1044}
1045
1046/** Connect to the server named by -s
1047 *
1048 * With -B the fd bio connects without blocking, and the TLS bio waits for
1049 * the connect in the event loop.
1050 */
1052{
1054
1055 fr_inet_ntop(buffer, sizeof(buffer), &utt->server_ipaddr);
1056
1057 utt->fd_cfg = (fr_bio_fd_config_t) {
1059 .socket_type = utt->conf.socket_type,
1060 .transport = (utt->conf.socket_type == SOCK_DGRAM) ? "udp" : "tcp",
1061 .transport_type = (utt->conf.socket_type == SOCK_DGRAM) ?
1063 .dst_ipaddr = utt->server_ipaddr,
1064 .dst_port = utt->server_port,
1065 .async = utt->nonblock,
1066 };
1067
1068 utt->fd_bio = fr_bio_fd_alloc(utt, &utt->fd_cfg, 0);
1069 if (!utt->fd_bio) {
1070 PERROR("Failed connecting to %s port %u", buffer, utt->server_port);
1071 return -1;
1072 }
1073
1074 utt->fd = fr_bio_fd_info(utt->fd_bio)->socket.fd;
1075
1076 INFO("Opened a connection to %s port %u on fd %d", buffer, utt->server_port, utt->fd);
1077
1078 return 0;
1079}
1080
1081/** Build an internal client.
1082 *
1083 * unit_test_tls accepts packets from anywhere (for now), and doesn't read "client" configuration sections.
1084 *
1085 * The client is built by allocating a CONF_SECTION rather than by filling in #fr_client_t manually, which
1086 * allows fields to be examined by `%request.client()`.
1087 *
1088 * "proto = tls" sets both the protocol and tls_required. It also fixes the
1089 * secret at "radsec", so that is the secret set here.
1090 */
1091static fr_client_t *tls_client_alloc(TALLOC_CTX *ctx, fr_ipaddr_t const *ipaddr)
1092{
1093 CONF_SECTION *cs;
1094 fr_client_t *client;
1096
1097 /*
1098 * Written without a prefix, the way a person would write it in
1099 * a "client" section. The "ipaddr" item is a COMBO_IP_PREFIX,
1100 * so a bare host address parses as a /32 or a /128.
1101 */
1102 fr_inet_ntop(buffer, sizeof(buffer), ipaddr);
1103
1104 MEM(cs = cf_section_alloc(ctx, NULL, "client", "unit_test_tls"));
1106 MEM(cf_pair_alloc(cs, "proto", "tls", T_OP_EQ, T_BARE_WORD, T_BARE_WORD));
1107 MEM(cf_pair_alloc(cs, "secret", "radsec", T_OP_EQ, T_BARE_WORD, T_DOUBLE_QUOTED_STRING));
1108 MEM(cf_pair_alloc(cs, "shortname", "unit_test_tls", T_OP_EQ, T_BARE_WORD, T_DOUBLE_QUOTED_STRING));
1109 MEM(cf_pair_alloc(cs, "nas_type", "test", T_OP_EQ, T_BARE_WORD, T_DOUBLE_QUOTED_STRING));
1110
1111 client = client_afrom_cs(ctx, cs, NULL, 0);
1112 if (!client) {
1113 PERROR("Failed creating the client for %s", buffer);
1114 talloc_free(cs);
1115 return NULL;
1116 }
1117
1118 /*
1119 * The client has to outlive the section it was built from,
1120 * because %request.client() reads the section.
1121 */
1122 talloc_steal(client, cs);
1123
1124 return client;
1125}
1126
1127/** Build the request the handshake runs under
1128 *
1129 * The TLS code reads the control list for TLS-Session-Cert-File and
1130 * TLS-Session-Require-Client-Certificate, and writes the negotiated version
1131 * and cipher suite into the session-state list, so a real request is needed.
1132 */
1133static request_t *tls_request_alloc(TALLOC_CTX *ctx, fr_bio_t *fd_bio, int socket_type)
1134{
1135 fr_bio_fd_info_t const *info = fr_bio_fd_info(fd_bio);
1136 int fd = info->socket.fd;
1138 struct sockaddr_storage sa;
1139 socklen_t salen;
1140
1141 static uint64_t number = 0;
1142
1144 if (!request) return NULL;
1145
1146 if (!request->packet) request->packet = fr_packet_alloc(request, false);
1147 if (!request->reply) request->reply = fr_packet_alloc(request, false);
1148
1149 request->packet->timestamp = fr_time();
1150
1151 request->packet->socket.type = socket_type;
1152 request->packet->socket.fd = fd;
1153
1154 /*
1155 * A stream socket which is still connecting has no peer yet, so
1156 * the fd bio gives the address which the socket is connecting to.
1157 */
1158 salen = sizeof(sa);
1159 if (getpeername(fd, (struct sockaddr *) &sa, &salen) == 0) {
1160 (void) fr_ipaddr_from_sockaddr(&request->packet->socket.inet.src_ipaddr,
1161 &request->packet->socket.inet.src_port, &sa, salen);
1162 } else {
1163 request->packet->socket.inet.src_ipaddr = info->socket.inet.dst_ipaddr;
1164 request->packet->socket.inet.src_port = info->socket.inet.dst_port;
1165 }
1166 request->packet->socket.af = request->packet->socket.inet.src_ipaddr.af;
1167
1168 salen = sizeof(sa);
1169 if (getsockname(fd, (struct sockaddr *) &sa, &salen) == 0) {
1170 (void) fr_ipaddr_from_sockaddr(&request->packet->socket.inet.dst_ipaddr,
1171 &request->packet->socket.inet.dst_port, &sa, salen);
1172 }
1173
1174 /*
1175 * The client is the far end of the connection we just accepted.
1176 */
1177 request->client = tls_client_alloc(request, &request->packet->socket.inet.src_ipaddr);
1178 if (!request->client) {
1180 return NULL;
1181 }
1182
1183 request->number = number++;
1184 request->name = talloc_typed_asprintf(request, "%" PRIu64, request->number);
1185 request->master_state = REQUEST_ACTIVE;
1186
1187 request->log.dst = talloc_zero(request, log_dst_t);
1188 request->log.dst->func = vlog_request;
1189 request->log.dst->uctx = &default_log;
1190 request->log.dst->lvl = fr_debug_lvl;
1191
1192 request->log.lvl = fr_debug_lvl;
1193 request->async = talloc_zero(request, fr_async_t);
1194 request->async->request = request;
1195
1196 if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
1197 ERROR("Failed converting connection addresses to attributes");
1199 return NULL;
1200 }
1201
1202 return request;
1203}
1204
1205/** Start the TLS bio, from inside the event loop
1206 *
1207 * See tls_connection_run() for why this runs from inside the event loop.
1208 * The TLS bio adds the fd to the event loop, runs the handshake, and calls
1209 * tls_bio_connected() or tls_bio_failed().
1210 */
1212{
1213 unit_test_bio_t *utt = talloc_get_type_abort(uctx, unit_test_bio_t);
1214
1215 if (fr_bio_tls_connect(utt->bio) < 0) {
1216 PERROR("Failed starting the TLS connection");
1217 tls_request_failed(utt);
1218 return;
1219 }
1220
1221 /*
1222 * A datagram connection starts with a datagram which was read
1223 * from the listening socket, because that is what named the
1224 * peer. The connection's socket never saw it, so hand it over
1225 * now that there is a connection to hand it to.
1226 */
1227 if (utt->first && !utt->done) {
1228 uint8_t *first = utt->first;
1229 size_t first_len = utt->first_len;
1230
1231 utt->first = NULL;
1232 utt->first_len = 0;
1233
1234 DEBUG3("Handing the connection the %zu byte datagram which named the peer", first_len);
1235 fr_tls_connection_recv(utt->conn, first, first_len);
1236 talloc_free(first);
1237 }
1238}
1239
1240/** Run one connection from the first byte to the last
1241 *
1242 * Everything which belongs to a single connection is allocated here and freed
1243 * again at the end, so that the next connection starts clean. What survives
1244 * is what the cache needs: the interpreter, the modules, and so the sessions
1245 * a policy stored.
1246 *
1247 * The program opens the fd bio, allocates the request, and allocates the TLS
1248 * bio from the request. The TLS bio does the rest.
1249 */
1251{
1252 int ret = -1;
1253 fr_event_user_t *ev = NULL;
1254 request_t *stale;
1255
1256 utt->connections++;
1257
1258 utt->done = false;
1259 utt->connected = false;
1260 utt->ret = EXIT_SUCCESS;
1261 utt->fd = -1;
1262
1263 /*
1264 * Get a connection, one way or the other.
1265 */
1266 if (utt->client) {
1267 if (tls_socket_connect(utt) < 0) return -1;
1268
1269 if (utt->command && !utt->command_pid && (tls_command_start(utt, utt->command) < 0)) goto finish;
1270 } else {
1271 if (tls_socket_accept(utt) < 0) return -1;
1272 }
1273
1274 utt->request = tls_request_alloc(utt, utt->fd_bio, utt->conf.socket_type);
1275 if (!utt->request) goto finish;
1276
1277 unlang_interpret_set(utt->request, utt->intp);
1278
1279 utt->bio = fr_bio_tls_alloc(utt->request, utt->fd_bio, utt->ssl_ctx, utt->client,
1281 if (!utt->bio) {
1282 PERROR("Failed creating the TLS bio");
1283 goto finish;
1284 }
1285
1286 utt->conn = fr_bio_tls_conn(utt->bio);
1287
1288 ((fr_bio_t *) utt->bio)->uctx = utt;
1290 .connected = tls_bio_connected,
1291 .failed = tls_bio_failed,
1292 });
1293
1294 /*
1295 * Name the connection for the log. Only the application knows
1296 * what identifies a connection, which is why the library does
1297 * not do this itself.
1298 */
1299 {
1300 fr_socket_t *socket = &utt->request->packet->socket;
1301
1302 utt->conn->name = fr_asprintf(utt->conn, "%s from %pV:%u to %pV:%u",
1303 (socket->type == SOCK_DGRAM) ? "DTLS" : "TLS",
1304 fr_box_ipaddr(socket->inet.src_ipaddr), socket->inet.src_port,
1305 fr_box_ipaddr(socket->inet.dst_ipaddr), socket->inet.dst_port);
1306 if (!utt->conn->name) goto finish;
1307
1308 DEBUG2("Connection is %s", utt->conn->name);
1309 }
1310
1311 if (!utt->client) {
1312 INFO("Accepted connection from %pV on fd %d",
1313 fr_box_ipaddr(utt->request->packet->socket.inet.src_ipaddr), utt->fd);
1314 }
1315
1316 if (fr_event_post_insert(utt->el, _tls_runnable, utt) < 0) {
1317 PERROR("Failed adding the runnable handler to the event loop");
1318 goto finish;
1319 }
1320
1321 /*
1322 * The connection is started from inside the event loop rather
1323 * than here. Ending the previous connection left the loop
1324 * flagged as exiting, and fr_event_fd_insert() refuses to add a
1325 * socket to a loop in that state. fr_event_loop() clears the
1326 * flag as it starts, so a user event which fires immediately is
1327 * the first point at which the TLS bio can add the socket.
1328 */
1329 if (fr_event_user_insert(utt, utt->el, &ev, true, _tls_connection_start, utt) < 0) {
1330 PERROR("Failed scheduling the start of the connection");
1331 goto finish;
1332 }
1333
1334 (void) fr_event_loop(utt->el);
1335
1336 ret = 0;
1337
1338finish:
1339 (void) fr_event_post_delete(utt->el, _tls_runnable, utt);
1340 if (utt->fd >= 0) (void) fr_event_fd_delete(utt->el, utt->fd, FR_EVENT_FILTER_IO);
1341
1342 /*
1343 * The user event is allocated in utt, which outlives the event
1344 * list. The destructor of the user event removes it from the
1345 * event list, so free it while the event list still exists.
1346 */
1347 TALLOC_FREE(ev);
1348
1349 /*
1350 * Freeing the TLS bio cancels the request, which unwinds the
1351 * connection frame, and then frees the connection. The fd bio
1352 * closes the socket.
1353 */
1354 utt->conn = NULL;
1355 TALLOC_FREE(utt->bio);
1356 TALLOC_FREE(utt->fd_bio);
1357
1358 /*
1359 * Empty the heap before the requests on it are freed. Popping
1360 * clears each entry's index, so nothing is left pointing at
1361 * memory the request pool is about to hand out again.
1362 */
1363 while (fr_heap_pop((void **)&stale, &utt->runnable) == 0) {
1364 if (!stale) break;
1365 }
1366 utt->yielded = 0;
1367
1368 TALLOC_FREE(utt->request);
1369 TALLOC_FREE(utt->first);
1370 utt->fd = -1;
1371
1372 return ret;
1373}
1374
1375/** Turn a comma separated list of names into the wire format ALPN uses
1376 *
1377 * Each name becomes one length octet followed by the name. There is no
1378 * terminator, so the caller takes the length from the array itself.
1379 *
1380 * @param[in] ctx to allocate in.
1381 * @param[in] names comma separated, in order of preference.
1382 * @return
1383 * - the list, whose talloc array length is the length of the list.
1384 * - NULL if a name is empty or longer than 255 octets.
1385 */
1386static unsigned char *tls_alpn_build(TALLOC_CTX *ctx, char const *names)
1387{
1388 unsigned char *alpn, *p;
1389 char const *q;
1390 size_t len;
1391
1392 /*
1393 * One length octet per name, and the names themselves. The
1394 * separators become those length octets, and the first name has
1395 * no separator before it, so one more octet than the string.
1396 */
1397 MEM(alpn = talloc_zero_array(ctx, unsigned char, strlen(names) + 1));
1398 p = alpn;
1399
1400 for (q = names; *q != '\0'; q += len) {
1401 len = strcspn(q, ",");
1402 if ((len == 0) || (len > 255)) {
1403 ERROR("Each ALPN protocol name must be between 1 and 255 characters");
1404 talloc_free(alpn);
1405 return NULL;
1406 }
1407
1408 *p++ = (unsigned char) len;
1409 memcpy(p, q, len);
1410 p += len;
1411
1412 if (q[len] == ',') len++; /* step over the separator */
1413 }
1414
1415 /*
1416 * A trailing separator, or a repeated one, leaves the array
1417 * longer than what was written into it. Shrink it so the array
1418 * length is the length of the list.
1419 */
1420 MEM(alpn = talloc_realloc(ctx, alpn, unsigned char, (size_t) (p - alpn)));
1421
1422 return alpn;
1423}
1424
1425/** Turn a comma separated list of names into ids of the test table
1426 *
1427 * @param[in] ctx to allocate the array in.
1428 * @param[in] names comma separated, in the order of preference.
1429 * @param[out] num number of ids returned.
1430 * @return
1431 * - the ids.
1432 * - NULL if a name is not in the table.
1433 */
1434static int *tls_alpn_ids(TALLOC_CTX *ctx, char const *names, size_t *num)
1435{
1436 int *ids;
1437 char const *q;
1438 size_t len, n = 0;
1439
1440 MEM(ids = talloc_zero_array(ctx, int, strlen(names) + 1));
1441
1442 for (q = names; *q != '\0'; q += len) {
1443 fr_tls_alpn_t const *entry;
1444
1445 len = strcspn(q, ",");
1446
1447 for (entry = unit_test_bio_alpn_table; entry->name; entry++) {
1448 if ((strlen(entry->name) == len) && (strncmp(entry->name, q, len) == 0)) break;
1449 }
1450
1451 if (!entry->name) {
1452 ERROR("ALPN name \"%.*s\" is not in the table of the test program", (int) len, q);
1453 talloc_free(ids);
1454 return NULL;
1455 }
1456
1457 ids[n++] = entry->id;
1458
1459 if (q[len] == ',') len++; /* step over the separator */
1460 }
1461
1462 *num = n;
1463
1464 return ids;
1465}
1466
1467int main(int argc, char *argv[])
1468{
1469 int ret = EXIT_SUCCESS;
1470 int c;
1471 char const *receipt_file = NULL;
1472 char const *command = NULL;
1473 char const *server = NULL;
1474 unsigned int count = 1;
1475 bool alert = false;
1476 bool invalid = false;
1477 bool nonblock = false;
1478 bool reject = false;
1479 bool network_fail = false;
1480 bool abort_pending = false;
1481 bool app_data = false;
1482 bool delay_tickets = false;
1483 bool hold = false;
1484 bool client = false;
1485 char const *alpn_names = NULL;
1486 bool alpn_required = false;
1487 bool alpn_raw = false;
1488 unsigned int i;
1489
1490 TALLOC_CTX *autofree;
1491 TALLOC_CTX *thread_ctx;
1492
1493 char *p;
1495
1496 fr_dict_t *dict = NULL;
1497 fr_dict_t const *dict_check;
1498
1499 virtual_server_t const *vs;
1500 CONF_SECTION *utt_cs;
1501
1502 unit_test_bio_t *utt = NULL;
1503 fr_tls_conf_t *conf = NULL;
1504
1505 /*
1506 * Must be called first, so the handler is called last
1507 */
1509
1511 thread_ctx = talloc_new(autofree);
1512
1514 if (!config) {
1515 fr_perror("unit_test_bio");
1516 fr_exit_now(EXIT_FAILURE);
1517 }
1518
1519 p = strrchr(argv[0], FR_DIR_SEP);
1520 if (!p) {
1521 main_config_name_set_default(config, argv[0], false);
1522 } else {
1523 main_config_name_set_default(config, p + 1, false);
1524 }
1525
1527
1528 if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0], PANIC_ACTION_SIGNALS) < 0) {
1529 fr_perror("%s", config->name);
1530 fr_exit_now(EXIT_FAILURE);
1531 }
1532#ifdef NDEBUG
1534#endif
1535
1536 fr_debug_lvl = 0;
1537 fr_time_start();
1538
1539 /*
1540 * The tests should have only IPs, not host names.
1541 */
1543
1544 /*
1545 * We always log to stdout.
1546 */
1548 default_log.fd = STDOUT_FILENO;
1549 default_log.print_level = true;
1550
1551 /* Process the options. */
1552 while ((c = getopt(argc, argv, "aABc:Cd:D:e:hHIlL:Mn:NPQr:Rs:TxX")) != -1) {
1553 switch (c) {
1554 case 'A':
1555 alert = true;
1556 break;
1557
1558 case 'B':
1559 nonblock = true;
1560 break;
1561
1562 case 'N':
1563 network_fail = true;
1564 break;
1565
1566 case 'R':
1567 reject = true;
1568 break;
1569
1570 case 'Q':
1571 abort_pending = true;
1572 break;
1573
1574 case 'c':
1575 count = (unsigned int) atoi(optarg);
1576 if (!count) {
1577 fprintf(stderr, "Invalid value \"%s\" for -c\n", optarg);
1578 fr_exit_now(EXIT_FAILURE);
1579 }
1580 break;
1581
1582 case 'C':
1583 check_config = true;
1584 break;
1585
1586 case 'd':
1588 break;
1589
1590 case 'D':
1592 break;
1593
1594 case 'h':
1595 usage(config, EXIT_SUCCESS);
1596 break;
1597
1598 case 'I':
1599 invalid = true;
1600 break;
1601
1602 case 'a':
1603 alpn_raw = true;
1604 break;
1605
1606 case 'l':
1607 alpn_required = true;
1608 break;
1609
1610 case 'L':
1611 alpn_names = optarg;
1612 break;
1613
1614 case 'M':
1615 talloc_enable_leak_report();
1616 break;
1617
1618 case 'n':
1619 config->name = optarg;
1620 break;
1621
1622 case 'H':
1623 hold = true;
1624 break;
1625
1626 case 'P':
1627 app_data = true;
1628 break;
1629
1630 case 'T':
1631 delay_tickets = true;
1632 break;
1633
1634 case 'e':
1635 command = optarg;
1636 break;
1637
1638 case 'r':
1639 receipt_file = optarg;
1640 break;
1641
1642 case 's':
1643 server = optarg;
1644 break;
1645
1646 case 'X':
1647 fr_debug_lvl += 2;
1648 default_log.print_level = true;
1649 break;
1650
1651 case 'x':
1652 fr_debug_lvl++;
1653 if (fr_debug_lvl > 2) default_log.print_level = true;
1654 break;
1655
1656 default:
1657 usage(config, EXIT_FAILURE);
1658 break;
1659 }
1660 }
1661
1662 if (receipt_file && (fr_unlink(receipt_file) < 0)) {
1663 fr_perror("%s", config->name);
1665 }
1666
1667 /*
1668 * A mismatch between the OpenSSL headers used at build time and the
1669 * linked OpenSSL library makes this program exit now, rather than
1670 * crash later.
1671 */
1673
1674 /*
1675 * fr_openssl_init() must be called before *ANY* OpenSSL functions are
1676 * used, which is why
1677 * fr_openssl_init() is called so early.
1678 */
1679 if (fr_openssl_init() < 0) EXIT_WITH_FAILURE;
1680
1682
1683 /*
1684 * Mismatch between the binary and the libraries it links against
1685 */
1687 fr_perror("%s", config->name);
1689 }
1690
1691 /*
1692 * Initialise the dynamic loader infrastructure, which the config
1693 * file parser uses.
1694 */
1695 modules_init(config->lib_dir);
1696
1697 if (!fr_dict_global_ctx_init(NULL, true, config->dict_dir)) {
1698 fr_perror("%s", config->name);
1700 }
1701
1703 fr_perror("%s", config->name);
1705 }
1706
1707 if (fr_tls_dict_init() < 0) EXIT_WITH_FAILURE;
1708
1709 /*
1710 * Load the custom dictionary
1711 */
1712 if (fr_dict_read(dict, config->confdir, FR_DICTIONARY_FILE) == -1) {
1713 PERROR("Failed to initialize the dictionaries");
1715 }
1716
1718 fr_perror("%s", config->name);
1720 }
1721
1722 if (request_global_init() < 0) {
1723 fr_perror("%s", config->name);
1725 }
1726
1727 /*
1728 * The triggers are run-time expansions, so the triggers need the
1729 * main event loop.
1730 */
1731 if (main_loop_init() < 0) {
1732 PERROR("Failed initialising main event loop");
1734 }
1735
1736 if (unlang_global_init() < 0) {
1737 fr_perror("%s", config->name);
1739 }
1740
1741 if (modules_rlm_init() < 0) {
1742 fr_perror("%s", config->name);
1744 }
1745
1746 if (virtual_servers_init() < 0) {
1747 fr_perror("%s", config->name);
1749 }
1750
1752
1753 /*
1754 * The TLS bio writes a close_notify alert when the bio closes, and the peer may have closed the
1755 * socket already. The write must fail with an error, as it does in radiusd, and must not kill
1756 * the process.
1757 */
1758#ifdef SIGPIPE
1759 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
1760 fr_perror("%s: Failed ignoring SIGPIPE", config->name);
1762 }
1763#endif
1764
1765 MEM(utt = talloc_zero(autofree, unit_test_bio_t));
1766 utt->sockfd = utt->fd = -1;
1767 utt->ret = EXIT_SUCCESS;
1768 utt->count = count;
1769 utt->alert = alert;
1770 utt->invalid = invalid;
1771 utt->nonblock = nonblock;
1772 utt->app_data = app_data;
1773 utt->hold = hold;
1774 utt->reject = reject;
1775 utt->network_fail = network_fail;
1776 utt->abort_pending = abort_pending;
1777
1778
1779 /*
1780 * Bootstrap and instantiate the virtual servers and the modules
1781 * that the virtual servers use. The TLS subsections name a
1782 * virtual server, so server_init() has to run before
1783 * cf_section_parse() reads the `unit_test_tls` section.
1784 */
1785 if (server_init(config->root_cs, config->confdir, dict) < 0) EXIT_WITH_FAILURE;
1786
1787 vs = virtual_server_find("tls");
1788 if (!vs) {
1789 ERROR("Cannot find virtual server 'tls'");
1791 }
1792
1793 dict_check = virtual_server_dict_by_name("tls");
1794 if (!dict_check || !fr_dict_compatible(dict_check, dict_tls)) {
1795 ERROR("Virtual server 'tls' must have 'namespace = tls'");
1797 }
1798
1799 /*
1800 * Parse the settings that steer the test program, with the TLS
1801 * configuration for each role as a subsection. The TLS rows
1802 * resolve the `virtual_server` item through
1803 * virtual_server_cf_parse(), so server_init() had to run first.
1804 * cf_section_parse_pass2() then resolves the expansions in the
1805 * TLS subsections.
1806 */
1807 utt_cs = cf_section_find(config->root_cs, "unit_test_tls", NULL);
1808 if (!utt_cs) {
1809 ERROR("Cannot find a top-level 'unit_test_tls { ... }' section in %s.conf", config->name);
1811 }
1812
1814
1815 if ((cf_section_parse(utt, &utt->conf, utt_cs) < 0) ||
1816 (cf_section_parse_pass2(&utt->conf, utt_cs) < 0)) {
1817 cf_log_perr(utt_cs, "Failed parsing the 'unit_test_tls' section");
1819 }
1820
1821 /*
1822 * Resolve `transport` into the socket it names, and into the
1823 * socket type which the TLS configuration below is given.
1824 */
1825 if (strcmp(utt->conf.transport, "tcp") == 0) {
1826 utt->conf.socket_type = SOCK_STREAM;
1827 utt->conf.ipaddr = utt->conf.tcp.ipaddr;
1828 utt->conf.port = utt->conf.tcp.port;
1829
1830 } else if (strcmp(utt->conf.transport, "udp") == 0) {
1831 utt->conf.socket_type = SOCK_DGRAM;
1832 utt->conf.ipaddr = utt->conf.udp.ipaddr;
1833 utt->conf.port = utt->conf.udp.port;
1834
1835 } else {
1836 cf_log_err(utt_cs, "Invalid transport name \"%s\", expected \"tcp\" or \"udp\"",
1837 utt->conf.transport);
1839 }
1840
1841 /*
1842 * -s turns the program around: instead of listening for a
1843 * connection, it makes one.
1844 */
1845 if (server) {
1846 utt->client = client = true;
1847
1848 if (fr_inet_pton_port(&utt->server_ipaddr, &utt->server_port, server,
1849 -1, AF_UNSPEC, true, false) < 0) {
1850 PERROR("Invalid value \"%s\" for -s", server);
1852 }
1853
1854 /*
1855 * fr_inet_pton_port() clears the port before it starts,
1856 * so a missing port is zero here, and not the default.
1857 */
1858 if (!utt->server_port) utt->server_port = utt->conf.port;
1859
1860 if (!utt->server_port) {
1861 ERROR("No port given in -s, and no 'port' in the 'unit_test_tls' section");
1863 }
1864 }
1865
1866 if (utt->conf.drop_datagram) {
1867 cf_log_err(utt_cs, "'drop_datagram' is not supported by unit_test_bio, use unit_test_tls");
1869 }
1870
1871 conf = client ? utt->conf.client : utt->conf.server;
1872 if (!conf) {
1873 cf_log_err(utt_cs, "Cannot run as %s, the 'unit_test_tls' section has no '%s { ... }' subsection",
1874 client ? "client" : "server", client ? "client" : "server");
1876 }
1877
1878 conf->delayed_tickets = delay_tickets;
1879
1880 /*
1881 * -L sets the ALPN list from names in the table of the test program, which is how an application
1882 * that uses the library's table sets it. The `alpn` section of the configuration file does the same
1883 * for the names it lists. -a sets the raw fields instead, which is how an application sets them
1884 * when it does not use a table. See src/lib/tls/alpn.c.
1885 *
1886 * -l sets `alpn_required` for the raw fields. With a table, the library derives `alpn_required`
1887 * from the entries.
1888 */
1889 if (alpn_names && !alpn_raw) {
1890 int *ids;
1891 size_t num = 0;
1892
1893 ids = tls_alpn_ids(utt, alpn_names, &num);
1894 if (!ids) EXIT_WITH_FAILURE;
1895
1896 if (fr_tls_alpn_set(conf, conf, unit_test_bio_alpn_table, ids, num) < 0) EXIT_WITH_FAILURE;
1897
1898 talloc_free(ids);
1899 } else if (alpn_names) {
1900 utt->alpn = tls_alpn_build(utt, alpn_names);
1901 if (!utt->alpn) EXIT_WITH_FAILURE;
1902
1903 conf->alpn = utt->alpn;
1904 conf->sizeof_alpn = talloc_array_length(utt->alpn);
1905 }
1906 if (!conf->alpn_table) conf->alpn_required = alpn_required;
1907
1908 /*
1909 * The transport is set by the application for the same reason
1910 * the ALPN strings are: one `tls { ... }` section is parsed by
1911 * the same rules everywhere, and only the application knows
1912 * what it opened the socket as.
1913 */
1914 conf->socket_type = utt->conf.socket_type;
1915
1916 utt->ssl_ctx = fr_tls_ctx_alloc(conf, client);
1917 if (!utt->ssl_ctx) {
1918 cf_log_perr(utt_cs, "Failed creating the TLS context");
1920 }
1921
1922 /*
1923 * The configuration parsed without error, so exit with success.
1924 */
1925 if (check_config) {
1926 DEBUG("Configuration appears to be OK");
1927 goto cleanup;
1928 }
1929
1930 utt->el = main_loop_event_list();
1931 fr_assert(utt->el != NULL);
1932
1933
1935
1936 /*
1937 * Simulate thread-specific instantiation
1938 */
1940 if (fr_thread_instantiate(thread_ctx, utt->el) < 0) {
1941 fr_perror("%s", config->name);
1943 }
1944
1945 if (modules_rlm_coord_attach(utt->el) < 0) {
1946 fr_perror("%s", config->name);
1948 }
1949
1950 if (fr_coord_pre_event_insert(utt->el) < 0) {
1951 fr_strerror_const("Failed adding coordinator pre-check to event list");
1953 }
1954
1955 if (fr_coord_post_event_insert(utt->el) < 0) {
1956 fr_strerror_const("Failed adding coordinator post-check to event list");
1958 }
1959
1960 /*
1961 * Set the panic action (if required)
1962 */
1963 {
1964 char const *panic_action = NULL;
1965
1966 panic_action = getenv("PANIC_ACTION");
1967 if (!panic_action) panic_action = config->panic_action;
1968
1970 fr_perror("%s", config->name);
1972 }
1973 }
1974
1975 setlinebuf(stdout); /* line buffered output */
1976
1977 /*
1978 * One interpreter, one runnable heap and one request for the
1979 * whole connection. The unit_test_bio_t documentation says why.
1980 */
1981 MEM(utt->runnable = fr_heap_talloc_alloc(utt, fr_pointer_cmp, request_t, runnable, 0));
1982
1983 utt->intp = unlang_interpret_init(utt, utt->el,
1985 .init_internal = _request_init_internal,
1986
1987 .done_external = _request_done_external,
1988 .done_internal = _request_done_internal,
1989 .done_detached = _request_done_detached,
1990
1991 .detach = _request_detach,
1992 .yield = _request_yield,
1993 .resume = _request_resume,
1994 .mark_runnable = _request_runnable,
1995 .scheduled = _request_scheduled,
1996 }, utt);
1997 if (!utt->intp) {
1998 fr_perror("%s", config->name);
2000 }
2001
2002 /*
2003 * Subrequests created by the TLS code inherit the thread default,
2004 * so the thread default has to point at the connection's
2005 * interpreter.
2006 */
2008
2009 /*
2010 * A server has one listening socket for every connection it
2011 * accepts, so it is opened once, here.
2012 */
2013 if (!utt->client && (tls_socket_open(utt) < 0)) EXIT_WITH_FAILURE;
2014
2015 /*
2016 * A server starts the command once it is listening. A client
2017 * starts it once the first connection succeeds, see
2018 * tls_connection_run().
2019 */
2020 utt->command = command;
2021 if (!utt->client && command && (tls_command_start(utt, command) < 0)) EXIT_WITH_FAILURE;
2022
2023 for (i = 0; i < utt->count; i++) {
2025
2026 if (utt->ret != EXIT_SUCCESS) break;
2027 }
2028
2029 tls_command_wait(utt);
2030
2031 ret = utt->ret;
2032
2033cleanup:
2034 if (utt) {
2035 /*
2036 * tls_connection_run() cleans up everything which belongs
2037 * to one connection. What is left here is what outlives
2038 * them.
2039 */
2040 if (utt->ssl_ctx) SSL_CTX_free(utt->ssl_ctx);
2041
2042 if (utt->sockfd >= 0) close(utt->sockfd);
2043
2044 /*
2045 * A failure left the client running. The reap below
2046 * signals it if it doesn't exit by itself.
2047 */
2048 if (utt->command_pid && !utt->command_exited) {
2050 (void) fr_event_pid_reap(utt->el, utt->command_pid, NULL, NULL);
2051 }
2052 }
2053
2055
2056 /*
2057 * Detach from coordinators.
2058 */
2059 if (utt && utt->el && (modules_rlm_coord_detach() > 0)) {
2060 if (unlikely(fr_coord_close_event_insert(utt->el) < 0)) {
2061 ERROR("Failed setting up coordinator close events");
2062 }
2063 fr_event_loop(utt->el);
2064 }
2065
2066 /*
2067 * Free thread data
2068 */
2069 talloc_free(thread_ctx);
2070
2072
2074
2075 if (utt && utt->el) fr_event_list_reap_signal(utt->el, fr_time_delta_from_sec(5), SIGKILL);
2076
2078
2080
2081 server_free();
2082
2083 /*
2084 * Virtual servers need to be freed before modules
2085 * as state entries containing data with module-specific
2086 * destructors may exist.
2087 */
2089
2091
2093
2094 fr_tls_dict_free();
2095
2097
2098 if (fr_dict_free(&dict, __FILE__) < 0) {
2099 fr_perror("unit_test_bio - dict");
2100 ret = EXIT_FAILURE;
2101 }
2102
2103 fr_openssl_free();
2104
2105 if (receipt_file && (ret == EXIT_SUCCESS) && (fr_touch(NULL, receipt_file, 0644, true, 0755) <= 0)) {
2106 fr_perror("unit_test_bio");
2107 ret = EXIT_FAILURE;
2108 }
2109
2110 if (talloc_free(autofree) < 0) {
2111 fr_perror("unit_test_bio - autofree");
2112 ret = EXIT_FAILURE;
2113 }
2114
2115 /*
2116 * Ensure our atexit handlers run before any other
2117 * atexit handlers registered by third party libraries.
2118 */
2120
2121 return ret;
2122}
2123
2124/*
2125 * Display the syntax for starting this program.
2126 */
2127static NEVER_RETURNS void usage(main_config_t const *config, int status)
2128{
2129 FILE *output = status ? stderr : stdout;
2130
2131 fprintf(output, "Usage: %s [options]\n", config->name);
2132 fprintf(output, "Options:\n");
2133 fprintf(output, " -A Reject the peer with a fatal TLS alert on the last connection,\n");
2134 fprintf(output, " rather than completing the handshake. With -c 2 the first\n");
2135 fprintf(output, " connection fills the session cache, so the alert then has a\n");
2136 fprintf(output, " session to clear. Used to test the alert and clear paths.\n");
2137 fprintf(output, " -B Put the connection socket in non-blocking mode, so that a read()\n");
2138 fprintf(output, " or a write() which would wait returns EAGAIN instead. The event\n");
2139 fprintf(output, " loop already waits for the socket to be readable or writable, so\n");
2140 fprintf(output, " this exercises the EAGAIN paths rather than changing what runs.\n");
2141 fprintf(output, " -I Mark the session invalid part way through the handshake on the\n");
2142 fprintf(output, " last connection, the way fr_tls_session_msg_cb() marks a record\n");
2143 fprintf(output, " FreeRADIUS refuses. The next round then refuses to continue, and\n");
2144 fprintf(output, " sends the peer a fatal alert saying so.\n");
2145 fprintf(output, " -R Reject the session once the handshake has succeeded on the last\n");
2146 fprintf(output, " connection, as policy would. The cached session is then cleared\n");
2147 fprintf(output, " rather than stored. With -c 2 the first connection fills the\n");
2148 fprintf(output, " cache and the second resumes from it before being rejected, so\n");
2149 fprintf(output, " the clear then has a loaded session to remove.\n");
2150 fprintf(output, " -Q Free the connection on the last connection once the handshake\n");
2151 fprintf(output, " has succeeded, while `store session` is still queued, to\n");
2152 fprintf(output, " exercise the path that discards queued cache operations.\n");
2153 fprintf(output, " -N Fail the session with a network error on the last connection,\n");
2154 fprintf(output, " once the handshake succeeds, to simulate the peer closing the\n");
2155 fprintf(output, " connection. With -c 2 the second connection loads a session\n");
2156 fprintf(output, " from the cache. A network error does not clear the loaded\n");
2157 fprintf(output, " session, and the session stays in the cache.\n");
2158 fprintf(output, " -H Once the connection is up, send nothing and hold it open.\n");
2159 fprintf(output, " A peer which is still owed a session ticket or a byte of\n");
2160 fprintf(output, " application data then waits for something which never\n");
2161 fprintf(output, " arrives, which is what exercises the post-handshake timer.\n");
2162 fprintf(output, " -c <count> Run <count> connections, one after another. Session resumption\n");
2163 fprintf(output, " needs two: one to fill the cache, one to resume from it.\n");
2164 fprintf(output, " -C Check configuration and exit.\n");
2165 fprintf(output, " -d <confdir> Configuration file directory. (defaults to " CONFDIR ").\n");
2166 fprintf(output, " -D <dict_dir> Dictionary files are in \"dict_dir/*\".\n");
2167 fprintf(output, " -e <command> Run <command> in a shell. A server runs it once it is\n");
2168 fprintf(output, " listening, so <command> can be what connects to the server,\n");
2169 fprintf(output, " such as openssl s_client. If <command> exits without\n");
2170 fprintf(output, " connecting, the server fails instead of waiting. With -s,\n");
2171 fprintf(output, " it runs once the first connection succeeds. Either way,\n");
2172 fprintf(output, " unit_test_bio waits for <command> to exit before it exits.\n");
2173 fprintf(output, " -h Print this help message.\n");
2174 fprintf(output, " -M Enable talloc leak reporting.\n");
2175 fprintf(output, " -n <name> Read ${confdir}/name.conf instead of ${confdir}/unit_test_bio.conf.\n");
2176 fprintf(output, " -P Server only. Send one byte of application data, 0x00, once the\n");
2177 fprintf(output, " handshake is done. A server sends every session ticket before\n");
2178 fprintf(output, " its first byte of application data, so this tells a client that\n");
2179 fprintf(output, " no ticket is still on its way.\n");
2180 fprintf(output, " -T Delay stateless TLS 1.3 session tickets, as EAP does. The server\n");
2181 fprintf(output, " sends none in the handshake.\n");
2182 fprintf(output, " -r <receipt_file> Create <receipt_file> when the program exits successfully.\n");
2183 fprintf(output, " -s <server[:port]> Connect to <server> as a TLS client, instead of listening\n");
2184 fprintf(output, " for a connection. Reads the 'tls client' section. The port\n");
2185 fprintf(output, " defaults to 'port' from the 'unit_test_tls' section.\n");
2186 fprintf(output, " -X Turn on full debugging.\n");
2187 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
2188
2189 fr_exit_now(status);
2190}
static int const char char buffer[256]
Definition acutest.h:641
int n
Definition acutest.h:642
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:179
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:310
#define fr_atexit_thread_trigger_all(...)
Definition atexit.h:236
static ssize_t fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to a bio.
Definition base.h:184
static ssize_t fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read raw data from a bio.
Definition base.h:161
void * uctx
user ctx, caller can manually set it.
Definition base.h:114
#define fr_bio_error(_x)
Definition base.h:200
static uint8_t request[4]
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define RCSID(id)
Definition build.h:609
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:413
#define unlikely(_x)
Definition build.h:492
#define UNUSED
Definition build.h:415
bool check_config
Definition cf_file.c:61
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1360
int cf_section_parse_pass2(void *base, CONF_SECTION *cs)
Fixup xlat expansions and attributes.
Definition cf_parse.c:1680
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:666
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
conf_parser_flags_t flags
Flags which control parsing behaviour.
Definition cf_parse.h:655
@ CONF_FLAG_OK_MISSING
OK if it's missing.
Definition cf_parse.h:491
#define FR_CONF_SUBSECTION_ALLOC(_name, _flags, _struct, _field, _subcs)
conf_parser_t which allocates a struct and populates it from a subsection
Definition cf_parse.h:384
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:754
#define FR_CONF_OFFSET_REF(_struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:337
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:309
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:238
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_PAIR * cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t op, fr_token_t lhs_quote, fr_token_t rhs_quote)
Allocate a CONF_PAIR.
Definition cf_util.c:1487
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1247
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:347
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:198
TALLOC_CTX * autofree
Definition common.c:29
fr_dict_t * dict
Definition common.c:31
int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
Start coordinators in single threaded mode.
Definition coord.c:626
void fr_coords_destroy(void)
Clean up coordinators in single threaded mode.
Definition coord.c:608
int fr_coord_close_event_insert(fr_event_list_t *el)
Definition coord.c:907
int fr_coord_post_event_insert(fr_event_list_t *el)
Insert instance specific post-event callbacks.
Definition coord.c:863
int fr_coord_pre_event_insert(fr_event_list_t *el)
Insert instance specific pre-event callbacks.
Definition coord.c:838
static char panic_action[512]
The command to execute when panicking.
Definition debug.c:66
void fr_disable_null_tracking_on_free(TALLOC_CTX *ctx)
Disable the null tracking context when a talloc chunk is freed.
Definition debug.c:1031
void fr_talloc_fault_setup(void)
Register talloc fault handlers.
Definition debug.c:1047
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program, unsigned long fault_signals)
Registers signal handlers to execute panic_action on fatal signal.
Definition debug.c:1071
#define PANIC_ACTION_SIGNALS
Definition debug.h:133
#define MEM(x)
Definition debug.h:38
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:272
void dependency_version_print(void)
Definition dependency.c:364
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
static NEVER_RETURNS void usage(void)
Definition dhcpclient.c:113
#define fr_dict_autofree(_to_free)
Definition dict.h:942
bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
See if two dictionaries have the same end parent.
Definition dict_util.c:2956
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename))
Read supplementary attribute definitions into an existing dictionary.
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4474
#define fr_dict_autoload(_to_load)
Definition dict.h:939
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir))
Initialise the global protocol hashes.
Definition dict_util.c:4858
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent)
(Re-)Initialize the special internal dictionary
#define fr_event_fd_insert(...)
Definition event.h:251
#define fr_event_pid_reap(...)
Definition event.h:278
#define fr_event_pid_wait(...)
Definition event.h:272
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:87
#define fr_event_user_insert(_ctx, _el, _ev_p, _trigger, _callback, _uctx)
Definition event.h:285
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
Definition fd.c:1142
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
Accept a new connection on a socket.
Definition fd.c:1901
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:1472
fr_socket_t socket
as connected socket
Definition fd.h:150
bool eof
are we at EOF?
Definition fd.h:159
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition fd.h:94
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:64
@ FR_BIO_FD_LISTEN
call fr_bio_fd_accept() for each new connection
Definition fd.h:65
@ FR_BIO_FD_TRANSPORT_TCP
TCP over IPv4 or IPv6.
Definition fd.h:80
@ FR_BIO_FD_TRANSPORT_UDP
UDP over IPv4 or IPv6.
Definition fd.h:81
Configuration for sockets.
Definition fd.h:93
Run-time status of the socket.
Definition fd.h:149
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:149
int fr_heap_pop(void **out, fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:359
static bool fr_heap_entry_inserted(fr_heap_index_t heap_idx)
Check if an entry is inserted into a heap.
Definition heap.h:126
#define fr_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init)
Creates a heap that verifies elements are of a specific talloc type.
Definition heap.h:117
The main heap structure.
Definition heap.h:68
talloc_free(hp)
int fr_ipaddr_from_sockaddr(fr_ipaddr_t *ipaddr, uint16_t *port, struct sockaddr_storage const *sa, socklen_t salen)
Convert sockaddr to our internal ip address representation.
Definition inet.c:1448
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:52
int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
Parses IPv4/6 address + port, to fr_ipaddr_t and integer (port)
Definition inet.c:944
char * fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
Print the address portion of a fr_ipaddr_t.
Definition inet.c:1025
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:51
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition inet.h:89
int af
Address family.
Definition inet.h:64
IPv4/6 prefix.
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:1314
void unlang_interpret_set(request_t *request, unlang_interpret_t *intp)
Set a specific interpreter for a request.
Definition interpret.c:2771
void unlang_interpret_set_thread_default(unlang_interpret_t *intp)
Set the default interpreter for this thread.
Definition interpret.c:2802
unlang_interpret_t * unlang_interpret_init(TALLOC_CTX *ctx, fr_event_list_t *el, unlang_request_func_t *funcs, void *uctx)
Initialize a unlang compiler / interpret.
Definition interpret.c:2722
#define UNLANG_REQUEST_RESUME
Definition interpret.h:48
External functions provided by the owner of the interpret.
Definition interpret.h:116
el_self ties the lifetime of the event list to the interpreter
void fr_bio_cb_set(fr_bio_t *bio, fr_bio_cb_funcs_t const *cb)
Definition base.c:260
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
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
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 server_init(CONF_SECTION *cs, char const *conf_dir, fr_dict_t *dict)
Initialize src/lib/server/.
Definition base.c:42
void server_free(void)
Free src/lib/server/.
Definition base.c:137
Describes a host allowed to send packets to the server.
Definition client.h:80
void vlog_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt, va_list ap, void *uctx)
Send a log message to its destination, possibly including fields from the request.
Definition log.c:293
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RPEDEBUG(fmt,...)
Definition log.h:393
Definition log.h:70
int fr_packet_pairs_from_packet(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_packet_t const *packet)
Allocate a "Net." struct with src/dst host and port.
Definition packet.c:91
int unlang_global_init(void)
Definition base.c:158
int fr_event_post_delete(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Delete a post-event callback from the event list.
Definition event.c:2106
#define fr_time()
Definition event.c:61
unsigned int fr_event_list_reap_signal(fr_event_list_t *el, fr_time_delta_t timeout, int signal)
Send a signal to all the processes we have in our reap list, and reap them.
Definition event.c:1759
void fr_event_loop_exit(fr_event_list_t *el, int code)
Signal an event loop exit with the specified code.
Definition event.c:2463
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
int fr_event_loop(fr_event_list_t *el)
Run an event loop.
Definition event.c:2485
int fr_event_post_insert(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Add a post-event callback to the event list.
Definition event.c:2083
A file descriptor/filter event.
Definition event.c:282
Stores all information relating to an event list.
Definition event.c:415
Callbacks for kevent() user events.
Definition event.c:363
int fr_unlink(char const *filename)
Remove a regular file from the filesystem.
Definition file.c:366
ssize_t fr_touch(int *fd_out, char const *filename, mode_t mode, bool mkdir, mode_t dir_mode)
Create an empty file.
Definition file.c:322
int fr_debug_lvl
Definition log.c:41
fr_log_t default_log
Definition log.c:308
@ L_DST_STDOUT
Log to stdout.
Definition log.h:75
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
Minimal data structure to use the new code.
Definition listen.h:80
int main_config_free(main_config_t **config)
main_config_t * main_config_alloc(TALLOC_CTX *ctx)
Allocate a main_config_t struct, setting defaults.
void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
Set the server name.
int main_config_init(main_config_t *config)
void main_config_confdir_set(main_config_t *config, char const *name)
Set the global radius config directory.
void main_config_dict_dir_set(main_config_t *config, char const *name)
Set the global dictionary directory.
Main server configuration.
Definition main_config.h:59
fr_event_list_t * main_loop_event_list(void)
Return the main loop event list.
Definition main_loop.c:173
int main_loop_init(void)
Initialise the main event loop, setting up signal handlers.
Definition main_loop.c:262
void main_loop_free(void)
Definition main_loop.c:198
static const uint8_t * zero
Definition md4.c:359
unsigned short uint16_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
fr_cmp_ret_t fr_pointer_cmp(void const *a, void const *b)
Compares two pointers.
Definition misc.c:449
int fr_nonblock(UNUSED int fd)
Definition misc.c:293
int fr_blocking(UNUSED int fd)
Definition misc.c:298
int modules_rlm_coord_attach(fr_event_list_t *el)
Runs the coord_attach method of all registered backend modules.
Definition module_rlm.c:992
int modules_rlm_free(void)
Cleanup all global structures.
int modules_rlm_coord_detach(void)
Runs the coord_detach method of all registered backend modules.
Definition module_rlm.c:999
int modules_rlm_init(void)
Initialise the module list structure.
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
Definition print.c:883
static const conf_parser_t config[]
Definition base.c:164
#define fr_assert(_expr)
Definition rad_assert.h:37
#define DEBUG2(fmt,...)
#define INFO(fmt,...)
Definition radict.c:67
#define WIFEXITED(stat_val)
Definition radiusd.c:66
#define WEXITSTATUS(stat_val)
Definition radiusd.c:63
static bool cleanup
Definition radsniff.c:59
static rs_t * conf
Definition radsniff.c:52
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
int request_global_init(void)
Definition request.c:614
int request_detach(request_t *child)
Unlink a subrequest from its parent.
Definition request.c:560
#define request_local_alloc_internal(_ctx, _args)
Allocate a new internal request outside of the request pool.
Definition request.h:344
@ REQUEST_ACTIVE
Request is active (running or runnable)
Definition request.h:89
void fr_schedule_worker_id_set(int id)
Explicitly set the worker id for the current thread.
Definition schedule.c:122
fr_client_t * client_afrom_cs(TALLOC_CTX *ctx, CONF_SECTION *cs, CONF_SECTION *server_cs, size_t extra)
Allocate a new client from a config section.
Definition client.c:735
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
Definition module.c:1981
return count
Definition module.c:155
fr_log_dst_t dst
Log destination.
Definition log.h:94
int fd
File descriptor to write messages to.
Definition log.h:109
bool print_level
sometimes we don't want log levels printed
Definition log.h:103
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:621
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
int fr_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el)
Instantiate thread-specific data for modules, virtual servers, xlats, unlang, and TLS.
Definition thread.c:177
static const char * names[8]
Definition time.c:618
int fr_time_start(void)
Initialize the local time.
Definition time.c:157
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
"server local" time.
Definition time.h:69
int fr_openssl_version_consistent(void)
Definition version.c:246
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
static char const * receipt_file
static fr_event_list_t * el
static void _request_yield(request_t *request, void *uctx)
static void _request_done_external(request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx)
fr_heap_t * runnable
Requests the interpreter has marked runnable.
fr_tls_connection_t * conn
The connection of bio.
static const conf_parser_t unit_test_bio_client_config[]
static const conf_parser_t unit_test_bio_alpn_config[]
The alpn subsection has no rules of its own.
fr_ipaddr_t server_ipaddr
Server named by -s.
int main(int argc, char *argv[])
static void _tls_runnable(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
Drain the runnable heap once per pass of the event loop.
char const * transport
"tcp" or "udp", naming the subsection below which holds the socket.
fr_event_list_t * el
Event list everything runs on.
static void _request_resume(request_t *request, UNUSED void *uctx)
static void tls_runnable_insert(unit_test_bio_t *utt, request_t *request)
Schedule a request, once.
static void _tls_socket_accept_start(fr_event_list_t *el, void *uctx)
Start waiting for a connection, from inside the event loop.
static void _tls_connection_start(UNUSED fr_event_list_t *el, void *uctx)
Start the TLS bio, from inside the event loop.
bool invalid
Mark the session invalid mid-handshake, see -I.
static int tls_connection_run(unit_test_bio_t *utt)
Run one connection from the first byte to the last.
static void _request_runnable(request_t *request, void *uctx)
uint32_t drop_datagram
Drop this datagram rather than sending it, counting from one.
bool alert
Reject the peer with a TLS alert, see -A.
static fr_client_t * tls_client_alloc(TALLOC_CTX *ctx, fr_ipaddr_t const *ipaddr)
Build an internal client.
uint16_t port
Port of the listening socket.
static void _request_done_internal(request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx)
bool abort_pending
Free the connection once the handshake succeeds, while store session is still queued,...
static bool _request_scheduled(request_t const *request, UNUSED void *uctx)
static fr_dict_t const * dict_freeradius
static const conf_parser_t unit_test_bio_config[]
int yielded
How many requests are currently yielded.
static fr_dict_t const * dict_tls
fr_bio_tls_t * bio
The TLS bio of the connection being run.
unsigned char * alpn
Protocol list built by -L, in the wire format fr_tls_conf_t expects.
static request_t * tls_request_alloc(TALLOC_CTX *ctx, fr_bio_t *fd_bio, int socket_type)
Build the request the handshake runs under.
static int tls_command_start(unit_test_bio_t *utt, char const *command)
Start the command named by -e.
static void _request_detach(request_t *request, UNUSED void *uctx)
fr_tls_conf_t * server
The server { ... } subsection.
static const conf_parser_t unit_test_bio_socket_config[]
The socket items, parsed once per transport subsection.
int socket_type
SOCK_STREAM or SOCK_DGRAM, from transport.
unit_test_bio_socket_t udp
The udp { ... } subsection.
fr_bio_t * fd_bio
The fd bio of the connection being run.
bool app_data
Send one byte of application data once the handshake is done, see -P.
int ret
Exit status.
static int tls_socket_open(unit_test_bio_t *utt)
Open the listening socket described by the "unit_test_tls" section.
static void _tls_hold_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Something happened on a connection which -H holds open.
static void tls_done(unit_test_bio_t *utt)
Stop the event loop, recording why.
static void tls_bio_connected(fr_bio_t *bio)
The connection is up, and can carry application data.
bool done
Set once the connection has a result.
static int * tls_alpn_ids(TALLOC_CTX *ctx, char const *names, size_t *num)
Turn a comma separated list of names into ids of the test table.
fr_tls_conf_t * client
The client { ... } subsection, used with -s.
bool command_exited
The command has exited.
unsigned int connections
How many connections have been run so far.
fr_dict_autoload_t unit_test_bio_dict[]
fr_bio_fd_config_t listen_cfg
Config of listen_bio. The bio keeps the pointer.
static void tls_bio_failed(fr_bio_t *bio)
The TLS bio says the connection failed, at any point.
char const * radiusd_version
bool nonblock
Put the connection socket in non-blocking mode, see -B.
fr_ipaddr_t ipaddr
Address of the listening socket.
int sockfd
Listening socket.
bool hold
Once connected, send nothing and stay connected, see -H.
bool client
Act as the client and connect to a server, rather than accept a connection.
bool network_fail
Fail the session with a network error once the handshake succeeds, see -N.
fr_event_fd_t * accept_ef
Read event for sockfd, while waiting in tls_socket_accept().
SSL_CTX * ssl_ctx
Context built from the "tls" section.
static void tls_request_failed(unit_test_bio_t *utt)
Record a failure this program cannot recover from, and stop.
static void _tls_socket_error(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, UNUSED void *uctx)
Stop waiting for a connection if the listening socket fails.
bool alpn_required
Set by -l.
fr_ipaddr_t ipaddr
Address of the listening socket, copied from whichever subsection transport named.
request_t * request
The request the connection runs under.
bool require_client_certificate
Whether the client has to present a certificate.
#define EXIT_WITH_FAILURE
static void _tls_command_exit(fr_event_list_t *el, pid_t pid, int status, void *uctx)
Record that the command started by -e has exited.
static int tls_socket_connect(unit_test_bio_t *utt)
Connect to the server named by -s.
static void _tls_socket_accept(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Accept the connection once the listening socket is readable.
fr_bio_fd_config_t fd_cfg
Config of fd_bio. The bio keeps the pointer.
pid_t command_pid
Command started by -e, or 0.
static int tls_socket_accept(unit_test_bio_t *utt)
Wait for a connection, or for the command started by -e to exit without connecting.
char const * command
Command to run, see -e.
unit_test_bio_socket_t tcp
The tcp { ... } subsection.
static const conf_parser_t unit_test_bio_server_config[]
The rules for the server and client subsections.
bool command_waiting
Waiting in tls_command_wait().
static int tls_socket_accept_one(unit_test_bio_t *utt)
Accept a connection, if the listening socket has one queued.
unit_test_bio_alpn_id_t
Protocols which the test program can negotiate.
@ UTB_ALPN_HTTP_1_1
@ UTB_ALPN_RADIUS_1_0
@ UTB_ALPN_RADIUS_1_1
int fd
Accepted or connected socket.
fr_bio_t * listen_bio
Listening fd bio.
static fr_tls_alpn_t const unit_test_bio_alpn_table[]
static void _request_done_detached(request_t *request, UNUSED rlm_rcode_t rcode, UNUSED void *uctx)
static void _tls_hold_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, UNUSED int fd_errno, void *uctx)
uint16_t server_port
Port from -s, or from the configuration.
unit_test_bio_conf_t conf
Parsed "unit_test_tls" section.
bool reject
Reject the session once the handshake succeeds, see -R.
size_t first_len
Length of first.
uint8_t * first
The datagram which named the peer, read from the listening socket before the connection's socket exis...
unlang_interpret_t * intp
Interpreter for the connection.
static void _request_init_internal(request_t *request, void *uctx)
An internal request created by the interpreter has to run on ours.
fr_event_pid_t const * command_ev
Waits for the command to exit.
bool connected
The connected callback has run.
bool accepting
Waiting in tls_socket_accept().
static void tls_command_wait(unit_test_bio_t *utt)
Wait for the command started by -e to exit, so its output is complete.
static unsigned char * tls_alpn_build(TALLOC_CTX *ctx, char const *names)
Turn a comma separated list of names into the wire format ALPN uses.
uint16_t port
Port of the listening socket, and the default port for -s.
unsigned int count
How many connections to run.
The "unit_test_tls" section.
State of the test program.
#define FR_DICTIONARY_FILE
Definition conf.h:6
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:7
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:83
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
Holds information necessary for binding or connecting to a socket.
Definition socket.h:60
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
#define fr_strerror_const(_msg)
Definition strerror.h:223
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition version.c:40
#define RADIUSD_VERSION_BUILD(_x)
Create a version string for a utility in the suite of FreeRADIUS utilities.
Definition version.h:58
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81
#define fr_box_ipaddr(_val)
Definition value.h:348
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
fr_dict_t const * virtual_server_dict_by_name(char const *virtual_server)
Return the namespace for the named virtual server.
virtual_server_t const * virtual_server_find(char const *name)
Return virtual server matching the specified name.
int virtual_servers_init(void)
Performs global initialisation for the virtual server code.
int virtual_servers_free(void)