The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
alpn.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 69c44c8f412c41380d188b4832beabeebf43c2e0 $
19 *
20 * @file lib/tls/alpn.c
21 * @brief Set up Application Layer Protocol Negotiation (ALPN)
22 *
23 * ALPN allows TLS connections to signal what protocol they are carrying. This lets applications use multiple
24 * different protocols on the same UDP or TCP port.
25 *
26 * The client sends a list of ALPN strings that it accepts. The server either chooses one and sends it back,
27 * or else sends back nothing to indicate that none have been chosen. Either end can allow no ALPN, or
28 * require that some ALPN string was negotiated.
29 *
30 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
31 */
32#ifdef WITH_TLS
33#define LOG_PREFIX "tls"
34
35#define _TLS_PRIVATE 1
36
37#include <freeradius-devel/util/debug.h>
38#include <freeradius-devel/server/pair.h>
39
40#include <freeradius-devel/protocol/tls/freeradius.h>
41
42#include "attrs.h"
43#include "base.h"
44#include "log.h"
45
46/** Validate the ALPN list.
47 *
48 * The client has not necessarily been authenticated, so we don't trust the data that they sent.
49 *
50 * The list is in wire format:
51 * - one octet length (1..255)
52 * - 'length' octets data.
53 *
54 * Bounded by 'size'. There is no terminator.
55 *
56 * @param[in] alpn list to check.
57 * @param[in] len length of the list.
58 * @return
59 * - true if the list is well formed.
60 * - false if the list is empty, holds an empty name, or does not end
61 * where `len` says it ends.
62 */
63static bool tls_alpn_valid(unsigned char const *alpn, size_t len)
64{
65 size_t i = 0;
66
67 if (len == 0) return false;
68
69 while (i < len) {
70 if (alpn[i] == 0) return false;
71
72 i += alpn[i] + 1;
73 }
74
75 return i == len;
76}
77
78/** Return the identity of the TLS connection.
79 *
80 * Both the connection code and EAP store the identity in #FR_TLS_EX_INDEX_IDENTITY.
81 *
82 * @param[in] ssl to get the name of.
83 * @return
84 * - the name which the application gave the connection.
85 * - "(unknown)" if no name was stored, or the name is not set.
86 */
87static char const *tls_alpn_identity(SSL const *ssl)
88{
89 char const * const *identity = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_IDENTITY);
90
91 if (!identity || !*identity) return "(unknown)";
92
93 return *identity;
94}
95
96/** Find an ALPN table entry by name
97 *
98 * @param[in] conf holding the ids.
99 * @param[in] name to find, with no length octet.
100 * @param[in] len length of `name`.
101 * @return
102 * - the entry on the list with that name.
103 * - NULL if the list has no such entry, or the application set `alpn` itself.
104 */
105static fr_tls_alpn_t const *tls_alpn_entry_by_name(fr_tls_conf_t const *conf, unsigned char const *name, size_t len)
106{
107 size_t i;
108
109 for (i = 0; i < conf->alpn_num; i++) {
110 bool found = true;
111 size_t j;
112 fr_tls_alpn_t const *entry = &conf->alpn_table[conf->alpn_ids[i] - 1];
113
114 /*
115 * Check the name we got from OpenSSL against the configured list of names.
116 */
117 for (j = 0; entry->name[j] != '\0'; j++) {
118 if ((j >= len) ||
119 ((uint8_t) entry->name[j] != (uint8_t) name[j])) {
120 found = false;
121 break;
122 }
123 }
124
125 if (found && (j == len)) return entry;
126 }
127
128 return NULL;
129}
130
131/** Find the default ALPN ID when the TLS session doesn't use ALPN.
132 *
133 * @param[in] conf holding the ids.
134 * @return
135 * - the entry which has #FR_TLS_ALPN_DEFAULT set.
136 * - NULL if no entry on the list has the flag.
137 */
138static fr_tls_alpn_t const *tls_alpn_default(fr_tls_conf_t const *conf)
139{
140 size_t i;
141
142 for (i = 0; i < conf->alpn_num; i++) {
143 fr_tls_alpn_t const *entry = &conf->alpn_table[conf->alpn_ids[i] - 1];
144
145 if ((entry->flags & FR_TLS_ALPN_DEFAULT) != 0) return entry;
146 }
147
148 return NULL;
149}
150
151/** Convert internal ALPN structures to the format needed by TLS.
152 *
153 * The order of `ids` is the order of preference, highest first. The function writes the fields `alpn`,
154 * `sizeof_alpn` and `alpn_required`, and saves `table` and `ids`. `alpn_required` is true when no entry has
155 * #FR_TLS_ALPN_DEFAULT set, because then a peer which uses no ALPN has nothing to be assumed to speak.
156 *
157 * The entries of `table` must have the ids 1, 2, 3 and so on, in that order, so that the entry with a given
158 * `id` is `table[id - 1]`.
159 *
160 * @param[in] ctx for list allocation. Must live as long as `conf`.
161 * @param[in] conf to update.
162 * @param[in] table the application's table. Must outlive `conf`.
163 * @param[in] ids ids of the entries of `table` to offer, in the order of preference.
164 * @param[in] num number of ids.
165 * @return
166 * - 0 on success.
167 * - -1 if the list is empty, or an id is not in the table, or an entry has an invalid name or id.
168 */
169int fr_tls_alpn_set(TALLOC_CTX *ctx, fr_tls_conf_t *conf, fr_tls_alpn_t const *table,
170 int const *ids, size_t num)
171{
172 size_t i, table_len = 0, total = 0;
173 unsigned char *alpn, *p;
174 int *alpn_ids;
175 bool default_found = false;
176 fr_tls_alpn_t const *entry;
177
178 if (num == 0) {
179 ERROR("ALPN list cannot be empty");
180 return -1;
181 }
182
183 /*
184 * Find the length of the table, and sanity check the IDs.
185 */
186 for (entry = table; entry->name; entry++) {
187 if (entry->id != (int) (table_len + 1)) {
188 ERROR("ALPN name \"%s\" has id %d, but the id of table entry %zu must be %zu",
189 entry->name, entry->id, table_len, table_len + 1);
190 return -1;
191 }
192 table_len++;
193 }
194
195 /*
196 * Walk over the list of IDs which have been taken from the configuration file. Double-check
197 * them, and figure out how much memory we need for the wire format of the ALPN string.
198 */
199 for (i = 0; i < num; i++) {
200 size_t len;
201
202 if ((ids[i] < 1) || ((size_t) ids[i] > table_len)) {
203 ERROR("ALPN id %d is not in the table", ids[i]);
204 return -1;
205 }
206
207 entry = &table[ids[i] - 1];
208
209 len = strlen(entry->name);
210 if ((len == 0) || (len > UINT8_MAX)) {
211 ERROR("ALPN name \"%s\" length must be between 1 and 255 octets", entry->name);
212 return -1;
213 }
214
215 total += len + 1;
216 if (entry->flags & FR_TLS_ALPN_DEFAULT) default_found = true;
217 }
218
219 MEM(alpn = talloc_array(ctx, unsigned char, total));
220 MEM(alpn_ids = talloc_array(ctx, int, num));
221
222 /*
223 * Build the wire-format string from the list of the IDs, in the order that the IDs are given in.
224 */
225 for (i = 0, p = alpn; i < num; i++) {
226 size_t len;
227
228 entry = &table[ids[i] - 1];
229
230 len = strlen(entry->name);
231 *p++ = (unsigned char) len;
232 memcpy(p, entry->name, len);
233 p += len;
234
235 alpn_ids[i] = ids[i];
236 }
237
238 conf->alpn = alpn;
239 conf->sizeof_alpn = total;
240 conf->alpn_table = table;
241 conf->alpn_ids = alpn_ids;
242 conf->alpn_num = num;
243 conf->alpn_required = !default_found;
244
245 return 0;
246}
247
248/** Parse an `alpn` section into the ALPN list of a configuration
249 *
250 * The application puts this function on the `func` of an `alpn` subsection rule, and puts its table in the
251 * `uctx` of the rule. Each item in the section is the `conf_name` of a table entry, with the value `yes` or
252 * `no`. The order of the items is the order of preference, highest first.
253 *
254 * The rule sets CONF_FLAG_OK_MISSING, so a missing section never reaches the function, and ALPN stays unset as
255 * RFC 9765 Section 3.3 Table 1 describes. A section which is present but empty is an error, as is a section
256 * which disables every entry.
257 *
258 * @param[in] ctx unused.
259 * @param[in] out unused.
260 * @param[in] parent the #fr_tls_conf_t being parsed.
261 * @param[in] ci the `alpn` section.
262 * @param[in] rule holding the table in `uctx`.
263 * @return
264 * - 0 on success.
265 * - -1 on an empty section, an unknown item, a duplicate item, a value which is not `yes` or `no`, or when no
266 * entry is enabled.
267 */
268int fr_tls_alpn_parse(UNUSED TALLOC_CTX *ctx, UNUSED void *out, void *parent, CONF_ITEM *ci,
269 conf_parser_t const *rule)
270{
271 fr_tls_conf_t *conf = talloc_get_type_abort(parent, fr_tls_conf_t);
272 fr_tls_alpn_t const *table = rule->uctx, *entry;
274 int *list = NULL;
275 bool *seen = NULL;
276 size_t num = 0, table_len = 0;
277 int ret = -1;
278
279 fr_assert(table != NULL);
280
281 for (entry = table; entry->name; entry++) table_len++;
282
283 MEM(list = talloc_zero_array(conf, int, table_len));
284 MEM(seen = talloc_zero_array(conf, bool, table_len));
285
286 cf_item_foreach(cs, item) {
287 CONF_PAIR *cp;
288 char const *value;
289 size_t i;
290
291 if (!cf_item_is_pair(item)) {
292 cf_log_err(item, "Unexpected subsection in 'alpn'");
293 goto done;
294 }
295
296 cp = cf_item_to_pair(item);
297 value = cf_pair_value(cp);
298
299 for (i = 0; i < table_len; i++) {
300 if (strcmp(table[i].conf_name, cf_pair_attr(cp)) == 0) break;
301 }
302
303 if (i == table_len) {
304 cf_log_err(cp, "Unknown ALPN item '%s'", cf_pair_attr(cp));
305 goto done;
306 }
307
308 if (seen[i]) {
309 cf_log_err(cp, "Duplicate ALPN item '%s'", cf_pair_attr(cp));
310 goto done;
311 }
312 seen[i] = true;
313
314 if (value && ((strcasecmp(value, "no") == 0) || (strcasecmp(value, "false") == 0))) continue;
315
316 if (!value || ((strcasecmp(value, "yes") != 0) && (strcasecmp(value, "true") != 0))) {
317 cf_log_err(cp, "Invalid value for ALPN item '%s', expected 'yes' or 'no'", cf_pair_attr(cp));
318 goto done;
319 }
320
321 list[num++] = table[i].id;
322 }
323
324 if (!cf_item_next(cs, NULL)) {
325 cf_log_err(ci, "The 'alpn' section is empty");
326 goto done;
327 }
328
329 if (num == 0) {
330 cf_log_err(ci, "The 'alpn' section does not enable any protocol");
331 goto done;
332 }
333
334 if (fr_tls_alpn_set(conf, conf, table, list, num) < 0) {
335 cf_log_err(ci, "Failed setting the ALPN list");
336 goto done;
337 }
338
339 ret = 0;
340
341done:
342 talloc_free(list);
343 talloc_free(seen);
344 return ret;
345}
346
347/** Check if we can use ALPN on this session.
348 *
349 * RFC 9765 Section 3.4 requires TLS 1.3. OpenSSL has no DTLS 1.3, so DTLS 1.2 is allowed as well.
350 *
351 * @param[in] ssl whose handshake has finished.
352 * @return
353 * - true for TLS 1.3 and DTLS 1.2.
354 * - false for any earlier version.
355 */
356static bool tls_alpn_version_ok(SSL const *ssl)
357{
358 int version = SSL_version(ssl);
359
360 if (version == TLS1_3_VERSION) return true;
361
362 return SSL_is_dtls(ssl) && (version == DTLS1_2_VERSION);
363}
364
365/** Record the negotiated name in `session-state`
366 *
367 * The TLS library writes `session-state` into a ticket when it issues the ticket. A server issues the ticket
368 * during handshake negotiation, which is before fr_tls_session_alpn_check() runs. The select callback
369 * therefore records the name as soon as the name is chosen. A resumed session may already hold the name from
370 * the ticket, so this function replaces that value rather than adding a second pair.
371 *
372 * @param[in] request holding `session-state`.
373 * @param[in] name to record, with no length octet.
374 * @param[in] len length of `name`.
375 */
376static void tls_alpn_session_state_set(request_t *request, unsigned char const *name, size_t len)
377{
378 fr_pair_t *vp;
379
380 vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, attr_tls_alpn);
382
383 MEM(fr_pair_value_bstrndup(vp, (char const *) name, len, false) == 0);
384}
385
386/** Find the previous ALPN negotiation string, if it's set as part of the TLS session ticket.
387 *
388 * RFC 9765 Section 3.5 says a session which negotiated ALPN can be resumed only with the same ALPN. The
389 * previous ALPN string is stored in `session-state`, so we check that.
390 *
391 * @param[in] conf holding the table.
392 * @param[in] request holding `session-state`.
393 * @param[in] ssl being negotiated.
394 * @param[in] entry the entry that this handshake chose, or NULL if the handshake chose none.
395 * @return
396 * - the sticky entry, if the session was resumed and the handshake chose a different entry.
397 * - NULL if the handshake follows the rule.
398 */
399static fr_tls_alpn_t const *tls_alpn_sticky_conflict(fr_tls_conf_t const *conf, request_t *request, SSL *ssl,
400 fr_tls_alpn_t const *entry)
401{
402 fr_pair_t *vp;
403 fr_tls_alpn_t const *old;
404
405 if (!conf->alpn_table || !SSL_session_reused(ssl)) return NULL;
406
407 vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, attr_tls_alpn);
408 if (!vp) return NULL;
409
410 RDEBUG2("(TLS) ALPN - session-state.%pP from the resumed session", vp);
411
412 old = tls_alpn_entry_by_name(conf, (unsigned char const *) vp->vp_strvalue, vp->vp_length);
413
414 if (old && (old->flags & FR_TLS_ALPN_STICKY) && (old != entry)) return old;
415
416 return NULL;
417}
418
419/** Select a matching ALPN string.
420 *
421 * OpenSSL calls this only when the client offered a list, so a client which does not use ALPN never reaches
422 * here. That case is caught after the handshake instead, by fr_tls_session_alpn_check().
423 *
424 * The servers preference is set by the list order. SSL_select_next_proto() walks the list, and stops on the
425 * first matching name offered by the client.
426 *
427 * @param[in] ssl session being negotiated.
428 * @param[out] out the name chosen, pointing into one of the two lists.
429 * @param[out] outlen length of that name.
430 * @param[in] in the client's list.
431 * @param[in] inlen length of the client's list.
432 * @param[in] arg unused, the configuration comes from the SSL *.
433 * @return
434 * - SSL_TLSEXT_ERR_OK if a name was chosen.
435 * - SSL_TLSEXT_ERR_ALERT_FATAL if the two lists have nothing in common.
436 */
437static int tls_session_alpn_select_cb(SSL *ssl, unsigned char const **out, unsigned char *outlen,
438 unsigned char const *in, unsigned int inlen, UNUSED void *arg)
439{
440 request_t *request = fr_tls_session_request(ssl);
441 fr_tls_conf_t const *conf = fr_tls_session_conf(ssl);
442 fr_tls_session_t *tls_session = fr_tls_session(ssl);
443 unsigned int i;
444
445 /*
446 * OpenSSL runs this callback while it parses the ClientHello,
447 * which happens inside a handshake round, and the library binds
448 * the request across a round.
449 */
450 fr_assert(request != NULL);
451
452 /*
453 * Check that the list wasn't mangled after the application set it.
454 */
455 fr_assert(tls_alpn_valid(conf->alpn, conf->sizeof_alpn));
456
457 if (tls_session->alpn_ignore) {
458 RDEBUG2("(TLS) ALPN - not used with TLS 1.2 or earlier, choosing no name");
459 return SSL_TLSEXT_ERR_NOACK;
460 }
461
462 if (RDEBUG_ENABLED2) {
463 for (i = 0; i < inlen; i += in[i] + 1) {
464 RDEBUG2("(TLS) ALPN - peer suggests \"%.*s\"", in[i], &in[i + 1]);
465 }
466 }
467
468 /*
469 * SSL_select_next_proto() writes through a non-const pointer,
470 * while the callback is handed a const one. The data it writes
471 * is a pointer into one of the two lists, not the list itself,
472 * so nothing const is written through.
473 */
474 if (SSL_select_next_proto(UNCONST(unsigned char **, out), outlen,
475 conf->alpn, (unsigned int) conf->sizeof_alpn,
476 in, inlen) != OPENSSL_NPN_NEGOTIATED) {
477 REDEBUG("(TLS) ALPN - no protocol in common with the peer (%s)", tls_alpn_identity(ssl));
478 return SSL_TLSEXT_ERR_ALERT_FATAL;
479 }
480
481 RDEBUG2("(TLS) ALPN - chose \"%.*s\"", (int) *outlen, *out);
482
483 /*
484 * A resumed session which negotiated a sticky entry cannot choose another entry.
485 */
486 if (conf->alpn_table) {
487 fr_tls_alpn_t const *old = tls_alpn_sticky_conflict(conf, request, ssl,
488 tls_alpn_entry_by_name(conf, *out, *outlen));
489
490 if (old) {
491 REDEBUG("(TLS) ALPN - session was negotiated as \"%s\" and cannot resume as \"%.*s\" (%s)",
492 old->name, (int) *outlen, *out, tls_alpn_identity(ssl));
493 return SSL_TLSEXT_ERR_ALERT_FATAL;
494 }
495 }
496
497 tls_alpn_session_state_set(request, *out, *outlen);
498
499 return SSL_TLSEXT_ERR_OK;
500}
501
502/** Set ALPN (or not) for a connection.
503 *
504 * This is called for both client and server. A client sends it's list, and verifies that the reply sent back
505 * from the server matches one of the ALPN strings it sent. A server reads the client list, and compares the
506 * ALPN strings to the ones that it accepts.
507 *
508 * @param[in] ctx to configure.
509 * @param[in] conf holding the protocol list.
510 * @param[in] client true for the client role.
511 * @return
512 * - 0 on success, including the case where the application wants no ALPN.
513 * - -1 if the list is malformed, or OpenSSL refused it.
514 */
515int fr_tls_ctx_alpn_set(SSL_CTX *ctx, fr_tls_conf_t const *conf, bool client)
516{
517 /*
518 * No ALPN string. If ALPN is required, that's a code error.
519 */
520 if (!conf->alpn) {
521 if (conf->alpn_required) {
522 ERROR("ALPN is required, but no protocols were set");
523 return -1;
524 }
525
526 return 0;
527 }
528
529 if (!tls_alpn_valid(conf->alpn, conf->sizeof_alpn)) {
530 ERROR("ALPN protocol list is malformed, or does not match sizeof_alpn (%zu)",
531 conf->sizeof_alpn);
532 return -1;
533 }
534
535 if (client) {
536 /*
537 * Returns zero on success, which is the opposite of
538 * most of the SSL_CTX_set_* calls around it.
539 */
540 if (SSL_CTX_set_alpn_protos(ctx, conf->alpn, (unsigned int) conf->sizeof_alpn) != 0) {
541 fr_tls_log_perror(NULL, "Failed setting the ALPN protocol list");
542 return -1;
543 }
544 } else {
545 SSL_CTX_set_alpn_select_cb(ctx, tls_session_alpn_select_cb, NULL);
546 }
547
548 return 0;
549}
550
551/** Get the ALPN string which was selected.
552 *
553 * @param[in] tls_session to check
554 * @return
555 * - true if the two ends agreed on a protocol.
556 * - false if they did not, which includes the case where ALPN was not used.
557 */
558static bool tls_session_alpn(fr_tls_session_t *tls_session)
559{
560 unsigned char const *data;
561 unsigned int len = 0;
562
563 SSL_get0_alpn_selected(tls_session->ssl, &data, &len);
564 if (!data || (len == 0)) {
565 tls_session->alpn = NULL;
566 tls_session->sizeof_alpn = 0;
567 return false;
568 }
569
570 MEM(tls_session->alpn = talloc_memdup(tls_session, data, len));
571 tls_session->sizeof_alpn = len;
572
573 return true;
574}
575
576/** Does the wire list hold a name
577 *
578 * @param[in] conf holding the list.
579 * @param[in] name to find, with no length octet.
580 * @param[in] len length of `name`.
581 * @return
582 * - true if the list holds the name.
583 * - false otherwise.
584 */
585static bool tls_alpn_listed(fr_tls_conf_t const *conf, unsigned char const *name, size_t len)
586{
587 size_t i = 0;
588
589 while (i < conf->sizeof_alpn) {
590 if ((conf->alpn[i] == len) && (memcmp(&conf->alpn[i + 1], name, len) == 0)) return true;
591
592 i += conf->alpn[i] + 1;
593 }
594
595 return false;
596}
597
598/** Check that we agreed on an ALPN string (or not).
599 *
600 * Runs once the handshake is done, for both roles.
601 *
602 * The client sends a list of proposed ALPN strings. The server receives it, and checks that against its
603 * local list. If there's no overlap, the server sends a fatal alert. The fatal alert is also sent when the
604 * client sends no ALPN, and the server isn't configured with a `default` entry.
605 *
606 * Once the server has found a match, it sends back the chosen ALPN string to inform the client.
607 *
608 * With a table, a peer which agrees on no name is treated as the entry flagged `default`.
609 *
610 * @param[in] request to log through.
611 * @param[in] tls_session to check.
612 * @return
613 * - 0 if a protocol was agreed, or if none was needed.
614 * - -1 if one was needed and none was agreed.
615 */
616int fr_tls_session_alpn_check(request_t *request, fr_tls_session_t *tls_session)
617{
618 fr_tls_conf_t const *conf = tls_session->conf;
619 fr_tls_alpn_t const *entry = NULL;
620 fr_pair_t *vp;
621 bool negotiated;
622 int alert = SSL_AD_NO_APPLICATION_PROTOCOL;
623
624 /*
625 * The only caller is tls_session_async_handshake_done_round(),
626 * which the interpreter runs with a request.
627 */
628 fr_assert(request != NULL);
629
630 /*
631 * @todo - figure out why we get called twice. For now, just suppress duplicate calls.
632 */
633 if (tls_session->alpn_checked) return 0;
634 tls_session->alpn_checked = true;
635
636 if (!conf->alpn) return 0;
637
638 /*
639 * SSL_get0_alpn_selected() hands back the name on its own, with
640 * no leading length octet, so the whole of what it gave us is
641 * the name.
642 */
643 negotiated = tls_session_alpn(tls_session);
644
645 if (negotiated) {
646 /*
647 * RFC 9765 Section 3.3 B says a client MUST check that the server chose one of the names
648 * which the client sent. OpenSSL may check, and the check here does not depend on that.
649 */
650 if (!tls_alpn_listed(conf, tls_session->alpn, tls_session->sizeof_alpn)) {
651 REDEBUG("(TLS) ALPN - peer chose \"%.*s\", which is not on our list (%s)",
652 (int) tls_session->sizeof_alpn, tls_session->alpn, tls_alpn_identity(tls_session->ssl));
653 alert = SSL_AD_ILLEGAL_PARAMETER;
654 goto fail;
655 }
656
657 if (conf->alpn_table) {
658 if (!tls_alpn_version_ok(tls_session->ssl)) {
659 REDEBUG("(TLS) ALPN - peer chose \"%.*s\" with TLS 1.2 or earlier (%s)",
660 (int) tls_session->sizeof_alpn, tls_session->alpn,
661 tls_alpn_identity(tls_session->ssl));
662 alert = SSL_AD_ILLEGAL_PARAMETER;
663 goto fail;
664 }
665
666 entry = tls_alpn_entry_by_name(conf, tls_session->alpn, tls_session->sizeof_alpn);
667 fr_assert(entry != NULL);
668 }
669 } else if (conf->alpn_table) {
670 entry = tls_alpn_default(conf);
671 }
672
673 if (conf->alpn_table) {
674 /*
675 * The select callback enforces the rule when the handshake chose a name. A handshake which
676 * chose no name never reached the select callback, so the check enforces the rule here.
677 */
678 if (!negotiated) {
679 fr_tls_alpn_t const *old = tls_alpn_sticky_conflict(conf, request, tls_session->ssl, entry);
680
681 if (old) {
682 REDEBUG("(TLS) ALPN - session was negotiated as \"%s\" and cannot resume as \"%s\" (%s)",
683 old->name, entry ? entry->name : "no ALPN", tls_alpn_identity(tls_session->ssl));
684 goto fail;
685 }
686 }
687
688 if (entry) {
689 tls_session->alpn_id = entry->id;
690 RDEBUG2("(TLS) ALPN - table id %d", entry->id);
691 }
692 }
693
694 if (negotiated) {
695 RDEBUG2("(TLS) ALPN - agreed on \"%.*s\"",
696 (int) tls_session->sizeof_alpn, tls_session->alpn);
697
698 /*
699 * A client has no select callback, and a list which the application set itself may have
700 * reached no callback either, so record the name here too.
701 */
702 tls_alpn_session_state_set(request, tls_session->alpn, tls_session->sizeof_alpn);
703
704 vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, attr_tls_alpn);
705 RDEBUG2("(TLS) session-state.%pP", vp);
706
707 return 0;
708 }
709
710 if (entry) {
711 RDEBUG2("(TLS) ALPN - peer used no ALPN, assuming \"%s\"", entry->name);
712 return 0;
713 }
714
715 if (!conf->alpn_required) {
716 RDEBUG2("(TLS) ALPN - No protocols in common, not using ALPN");
717 return 0;
718 }
719
720 REDEBUG("(TLS) ALPN - Failure, no protocols in common (%s)", tls_alpn_identity(tls_session->ssl));
721
722fail:
723 fr_tls_session_error_alert(request, tls_session, FR_ERROR_VALUE_ALPN_FAILED, alert);
724
725 return -1;
726}
727
728/** Check ALPN while the server parses the ClientHello
729 *
730 * A server can send the alert while it processes the ClientHello, in order to close the connection early on
731 * failure. This function checks the ALPN strings from the configuration. If the application manually sets a
732 * list, that's checked after the handshake in fr_tls_session_alpn_check().
733 *
734 * We need to correlate ALPN with TLS version, but TLS hasn't finished negotiating the TLS version when
735 * tls_session_alpn_select_cb() runs.
736 *
737 * @param[in] ssl inside the ClientHello callback.
738 * @param[in] request to log through.
739 * @param[out] alert set when the function returns SSL_CLIENT_HELLO_ERROR.
740 * @param[in] tls13 true if TLS 1.3 will be negotiated. DTLS needs no test, because the function allows DTLS.
741 * @return
742 * - SSL_CLIENT_HELLO_SUCCESS to continue the handshake.
743 * - SSL_CLIENT_HELLO_ERROR to send `alert`, because no ALPN will be negotiated and the list has no default entry.
744 */
745int fr_tls_session_alpn_client_hello(SSL *ssl, request_t *request, int *alert, bool tls13)
746{
747 fr_tls_conf_t const *conf = fr_tls_session_conf(ssl);
748 fr_tls_session_t *tls_session = fr_tls_session(ssl);
749 bool usable;
750
751 if (!conf->alpn_table) return SSL_CLIENT_HELLO_SUCCESS;
752
753 usable = tls13 || SSL_is_dtls(ssl);
754 tls_session->alpn_ignore = !usable;
755
756 if (usable && (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_application_layer_protocol_negotiation,
757 NULL, NULL) == 1)) {
758 return SSL_CLIENT_HELLO_SUCCESS;
759 }
760
761 if (!conf->alpn_required) return SSL_CLIENT_HELLO_SUCCESS;
762
763 REDEBUG("(TLS) ALPN - Failure, %s and no default ALPN string was configured (%s)",
764 usable ? "client offered no ALPN" : "TLS 1.2 or earlier will be negotiated",
765 tls_alpn_identity(ssl));
766
767 *alert = SSL_AD_NO_APPLICATION_PROTOCOL;
768
769 return SSL_CLIENT_HELLO_ERROR;
770}
771
772/** Offer only a sticky protocol when a client resumes a session which negotiated it
773 *
774 * RFC 9765 Section 3.5 says a client that resumes a session which negotiated `radius/1.1` MUST advertise only
775 * `radius/1.1`. The negotiated name is stored on the `SSL_SESSION`, which OpenSSL exposes from release 3.3. An
776 * earlier release leaves the list unchanged, and the server enforces the rule.
777 *
778 * @param[in] request to log through.
779 * @param[in] ssl about to resume `sess`.
780 * @param[in] sess the session being resumed.
781 * @param[in] conf holding the table.
782 */
783void fr_tls_alpn_client_resume(request_t *request, SSL *ssl, SSL_SESSION const *sess, fr_tls_conf_t const *conf)
784{
785#if OPENSSL_VERSION_NUMBER >= 0x30300000L
786 unsigned char const *name;
787 size_t len;
788 fr_tls_alpn_t const *entry;
789 unsigned char list[UINT8_MAX + 1];
790
791 if (!conf->alpn_table) return;
792
793 SSL_SESSION_get0_alpn_selected(sess, &name, &len);
794 if (!name || (len == 0) || (len > UINT8_MAX)) return;
795
796 entry = tls_alpn_entry_by_name(conf, name, len);
797 if (!entry || !(entry->flags & FR_TLS_ALPN_STICKY)) return;
798
799 list[0] = (unsigned char) len;
800 memcpy(&list[1], name, len);
801
802 if (SSL_set_alpn_protos(ssl, list, (unsigned int) (len + 1)) != 0) {
803 RPERROR("(TLS) ALPN - Failed offering only \"%s\" for the resumed session", entry->name);
804 return;
805 }
806
807 RDEBUG2("(TLS) ALPN - resuming a session which negotiated \"%s\", offering only that name", entry->name);
808#else
809 (void) ssl;
810 (void) sess;
811
812 if (!conf->alpn_table) return;
813
814 RWDEBUG2("(TLS) ALPN - OpenSSL before 3.3 does not provide the ALPN string for resumed sessions, "
815 "allowing all ALPNs");
816#endif
817}
818#endif /* WITH_TLS */
static uint8_t request[4]
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define UNUSED
Definition build.h:415
void const * uctx
User data accessible by the cf_parse_t func.
Definition cf_parse.h:672
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
Common header for all CONF_* types.
Definition cf_priv.h:54
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:619
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:671
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:651
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1758
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1742
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
#define cf_item_next(_parent, _curr)
Definition cf_util.h:91
#define cf_item_foreach(_parent, _iter)
Iterate over every child item of a CONF_SECTION (or any CONF_ITEM that has children).
Definition cf_util.h:106
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
Test enumeration values.
Definition dict_test.h:92
talloc_free(hp)
#define RWDEBUG2(fmt,...)
Definition log.h:379
#define RPERROR(fmt,...)
Definition log.h:319
HIDDEN fr_dict_attr_t const * attr_tls_alpn
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:121
unsigned char uint8_t
#define UINT8_MAX
int strcasecmp(char *s1, char *s2)
Definition missing.c:65
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2756
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG_ENABLED2()
Definition radclient.h:50
#define RDEBUG2(fmt,...)
Definition radclient.h:54
static rs_t * conf
Definition radsniff.c:52
static char const * name
#define pair_append_session_state(_attr, _da)
Allocate and append a fr_pair_t to session-state list.
Definition pair.h:67
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
static fr_slen_t parent
Definition pair.h:864
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061