/src/openssl36/ssl/quic/quic_port.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/quic_port.h" |
11 | | #include "internal/quic_channel.h" |
12 | | #include "internal/quic_lcidm.h" |
13 | | #include "internal/quic_srtm.h" |
14 | | #include "internal/quic_txp.h" |
15 | | #include "internal/ssl_unwrap.h" |
16 | | #include "quic_port_local.h" |
17 | | #include "quic_channel_local.h" |
18 | | #include "quic_engine_local.h" |
19 | | #include "quic_local.h" |
20 | | #include "../ssl_local.h" |
21 | | #include <openssl/rand.h> |
22 | | |
23 | | /* |
24 | | * QUIC Port Structure |
25 | | * =================== |
26 | | */ |
27 | 24.9k | #define INIT_DCID_LEN 8 |
28 | | |
29 | | static int port_init(QUIC_PORT *port); |
30 | | static void port_cleanup(QUIC_PORT *port); |
31 | | static OSSL_TIME get_time(void *arg); |
32 | | static void port_default_packet_handler(QUIC_URXE *e, void *arg, |
33 | | const QUIC_CONN_ID *dcid); |
34 | | static void port_rx_pre(QUIC_PORT *port); |
35 | | |
36 | | /** |
37 | | * @struct validation_token |
38 | | * @brief Represents a validation token for secure connection handling. |
39 | | * |
40 | | * This struct is used to store information related to a validation token. |
41 | | * |
42 | | * @var validation_token::is_retry |
43 | | * True iff this validation token is for a token sent in a RETRY packet. |
44 | | * Otherwise, this token is from a NEW_TOKEN_packet. Iff this value is true, |
45 | | * then ODCID and RSCID are set. |
46 | | * |
47 | | * @var validation_token::timestamp |
48 | | * Time that the validation token was minted. |
49 | | * |
50 | | * @var validation_token::odcid |
51 | | * An original connection ID (`QUIC_CONN_ID`) used to identify the QUIC |
52 | | * connection. This ID helps associate the token with a specific connection. |
53 | | * This will only be valid for validation tokens from RETRY packets. |
54 | | * |
55 | | * @var validation_token::rscid |
56 | | * DCID that the client will use as the DCID of the subsequent initial packet |
57 | | * i.e the "new" DCID. |
58 | | * This will only be valid for validation tokens from RETRY packets. |
59 | | * |
60 | | * @var validation_token::remote_addr_len |
61 | | * Length of the following character array. |
62 | | * |
63 | | * @var validation_token::remote_addr |
64 | | * A character array holding the raw address of the client requesting the |
65 | | * connection. |
66 | | */ |
67 | | typedef struct validation_token { |
68 | | OSSL_TIME timestamp; |
69 | | QUIC_CONN_ID odcid; |
70 | | QUIC_CONN_ID rscid; |
71 | | size_t remote_addr_len; |
72 | | unsigned char *remote_addr; |
73 | | unsigned char is_retry; |
74 | | } QUIC_VALIDATION_TOKEN; |
75 | | |
76 | | /* |
77 | | * Maximum length of a marshalled validation token. |
78 | | * |
79 | | * - timestamp is 8 bytes |
80 | | * - odcid and rscid are maximally 42 bytes in total |
81 | | * - remote_addr_len is a size_t (8 bytes) |
82 | | * - remote_addr is in the worst case 110 bytes (in the case of using a |
83 | | * maximally sized AF_UNIX socket) |
84 | | * - is_retry is a single byte |
85 | | */ |
86 | 0 | #define MARSHALLED_TOKEN_MAX_LEN 169 |
87 | | |
88 | | /* |
89 | | * Maximum length of an encrypted marshalled validation token. |
90 | | * |
91 | | * This will include the size of the marshalled validation token plus a 16 byte |
92 | | * tag and a 12 byte IV, so in total 197 bytes. |
93 | | */ |
94 | 0 | #define ENCRYPTED_TOKEN_MAX_LEN (MARSHALLED_TOKEN_MAX_LEN + 16 + 12) |
95 | | |
96 | 42.0k | #define DEFAULT_MAX_PENDING_CONNS 256 |
97 | | |
98 | 98.4M | DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL); quic_port.c:ossl_list_ch_head Line | Count | Source | 98 | | DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL); |
quic_port.c:ossl_list_ch_next Line | Count | Source | 98 | | DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL); |
|
99 | 1.28k | DEFINE_LIST_OF_IMPL(incoming_ch, QUIC_CHANNEL); Unexecuted instantiation: quic_port.c:ossl_list_incoming_ch_insert_tail quic_port.c:ossl_list_incoming_ch_head Line | Count | Source | 99 | | DEFINE_LIST_OF_IMPL(incoming_ch, QUIC_CHANNEL); |
Unexecuted instantiation: quic_port.c:ossl_list_incoming_ch_remove |
100 | 84.0k | DEFINE_LIST_OF_IMPL(port, QUIC_PORT); quic_port.c:ossl_list_port_insert_tail Line | Count | Source | 100 | | DEFINE_LIST_OF_IMPL(port, QUIC_PORT); |
quic_port.c:ossl_list_port_remove Line | Count | Source | 100 | | DEFINE_LIST_OF_IMPL(port, QUIC_PORT); |
|
101 | | |
102 | | QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args) |
103 | 42.0k | { |
104 | 42.0k | QUIC_PORT *port; |
105 | | |
106 | 42.0k | if ((port = OPENSSL_zalloc(sizeof(QUIC_PORT))) == NULL) |
107 | 0 | return NULL; |
108 | | |
109 | 42.0k | port->engine = args->engine; |
110 | 42.0k | port->channel_ctx = args->channel_ctx; |
111 | 42.0k | port->is_multi_conn = args->is_multi_conn; |
112 | 42.0k | port->validate_addr = args->do_addr_validation; |
113 | 42.0k | port->get_conn_user_ssl = args->get_conn_user_ssl; |
114 | 42.0k | port->user_ssl_arg = args->user_ssl_arg; |
115 | 42.0k | port->max_pending_channels = DEFAULT_MAX_PENDING_CONNS; |
116 | | |
117 | 42.0k | if (!port_init(port)) { |
118 | 0 | OPENSSL_free(port); |
119 | 0 | return NULL; |
120 | 0 | } |
121 | | |
122 | 42.0k | return port; |
123 | 42.0k | } |
124 | | |
125 | | void ossl_quic_port_free(QUIC_PORT *port) |
126 | 42.0k | { |
127 | 42.0k | if (port == NULL) |
128 | 0 | return; |
129 | | |
130 | 42.0k | port_cleanup(port); |
131 | 42.0k | OPENSSL_free(port); |
132 | 42.0k | } |
133 | | |
134 | | static int port_init(QUIC_PORT *port) |
135 | 24.7k | { |
136 | 24.7k | size_t rx_short_dcid_len = (port->is_multi_conn ? INIT_DCID_LEN : 0); |
137 | 24.7k | int key_len = -1; |
138 | 24.7k | EVP_CIPHER *cipher = NULL; |
139 | 24.7k | unsigned char *token_key = NULL; |
140 | 24.7k | int ret = 0; |
141 | | |
142 | 24.7k | if (port->engine == NULL || port->channel_ctx == NULL) |
143 | 0 | goto err; |
144 | | |
145 | 24.7k | if ((port->err_state = OSSL_ERR_STATE_new()) == NULL) |
146 | 0 | goto err; |
147 | | |
148 | 24.7k | if ((port->demux = ossl_quic_demux_new(/*BIO=*/NULL, |
149 | 24.7k | /*Short CID Len=*/rx_short_dcid_len, |
150 | 24.7k | get_time, port)) |
151 | 24.7k | == NULL) |
152 | 0 | goto err; |
153 | | |
154 | 24.7k | ossl_quic_demux_set_default_handler(port->demux, |
155 | 24.7k | port_default_packet_handler, |
156 | 24.7k | port); |
157 | | |
158 | 24.7k | if ((port->srtm = ossl_quic_srtm_new(port->engine->libctx, |
159 | 24.7k | port->engine->propq)) |
160 | 24.7k | == NULL) |
161 | 0 | goto err; |
162 | | |
163 | 24.7k | if ((port->lcidm = ossl_quic_lcidm_new(port->engine->libctx, |
164 | 24.7k | rx_short_dcid_len)) |
165 | 24.7k | == NULL) |
166 | 0 | goto err; |
167 | | |
168 | 24.7k | port->rx_short_dcid_len = (unsigned char)rx_short_dcid_len; |
169 | 24.7k | port->tx_init_dcid_len = INIT_DCID_LEN; |
170 | 24.7k | port->state = QUIC_PORT_STATE_RUNNING; |
171 | | |
172 | 24.7k | ossl_list_port_insert_tail(&port->engine->port_list, port); |
173 | 24.7k | port->on_engine_list = 1; |
174 | 24.7k | port->bio_changed = 1; |
175 | | |
176 | | /* Generate random key for token encryption */ |
177 | 24.7k | if ((port->token_ctx = EVP_CIPHER_CTX_new()) == NULL |
178 | 24.7k | || (cipher = EVP_CIPHER_fetch(port->engine->libctx, |
179 | 24.7k | "AES-256-GCM", NULL)) |
180 | 24.7k | == NULL |
181 | 24.7k | || !EVP_EncryptInit_ex(port->token_ctx, cipher, NULL, NULL, NULL) |
182 | 24.7k | || (key_len = EVP_CIPHER_CTX_get_key_length(port->token_ctx)) <= 0 |
183 | 24.7k | || (token_key = OPENSSL_malloc(key_len)) == NULL |
184 | 24.7k | || !RAND_priv_bytes_ex(port->engine->libctx, token_key, key_len, 0) |
185 | 24.7k | || !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, token_key, NULL)) |
186 | 0 | goto err; |
187 | | |
188 | 24.7k | ret = 1; |
189 | 24.7k | err: |
190 | 24.7k | EVP_CIPHER_free(cipher); |
191 | 24.7k | if (key_len >= 1) |
192 | 24.7k | OPENSSL_clear_free(token_key, key_len); |
193 | 0 | else |
194 | 0 | OPENSSL_free(token_key); |
195 | 24.7k | if (!ret) |
196 | 0 | port_cleanup(port); |
197 | 24.7k | return ret; |
198 | 24.7k | } |
199 | | |
200 | | static void port_cleanup(QUIC_PORT *port) |
201 | 42.0k | { |
202 | 42.0k | assert(ossl_list_ch_num(&port->channel_list) == 0); |
203 | | |
204 | 42.0k | ossl_quic_demux_free(port->demux); |
205 | 42.0k | port->demux = NULL; |
206 | | |
207 | 42.0k | ossl_quic_srtm_free(port->srtm); |
208 | 42.0k | port->srtm = NULL; |
209 | | |
210 | 42.0k | ossl_quic_lcidm_free(port->lcidm); |
211 | 42.0k | port->lcidm = NULL; |
212 | | |
213 | 42.0k | OSSL_ERR_STATE_free(port->err_state); |
214 | 42.0k | port->err_state = NULL; |
215 | | |
216 | 42.0k | if (port->on_engine_list) { |
217 | 42.0k | ossl_list_port_remove(&port->engine->port_list, port); |
218 | 42.0k | port->on_engine_list = 0; |
219 | 42.0k | } |
220 | | |
221 | 42.0k | EVP_CIPHER_CTX_free(port->token_ctx); |
222 | 42.0k | port->token_ctx = NULL; |
223 | 42.0k | } |
224 | | |
225 | | static void port_transition_failed(QUIC_PORT *port) |
226 | 0 | { |
227 | 0 | if (port->state == QUIC_PORT_STATE_FAILED) |
228 | 0 | return; |
229 | | |
230 | 0 | port->state = QUIC_PORT_STATE_FAILED; |
231 | 0 | } |
232 | | |
233 | | int ossl_quic_port_is_running(const QUIC_PORT *port) |
234 | 98.8M | { |
235 | 98.8M | return port->state == QUIC_PORT_STATE_RUNNING; |
236 | 98.8M | } |
237 | | |
238 | | QUIC_ENGINE *ossl_quic_port_get0_engine(QUIC_PORT *port) |
239 | 0 | { |
240 | 0 | return port->engine; |
241 | 0 | } |
242 | | |
243 | | QUIC_REACTOR *ossl_quic_port_get0_reactor(QUIC_PORT *port) |
244 | 18.1M | { |
245 | 18.1M | return ossl_quic_engine_get0_reactor(port->engine); |
246 | 18.1M | } |
247 | | |
248 | | QUIC_DEMUX *ossl_quic_port_get0_demux(QUIC_PORT *port) |
249 | 0 | { |
250 | 0 | return port->demux; |
251 | 0 | } |
252 | | |
253 | | CRYPTO_MUTEX *ossl_quic_port_get0_mutex(QUIC_PORT *port) |
254 | 0 | { |
255 | 0 | return ossl_quic_engine_get0_mutex(port->engine); |
256 | 0 | } |
257 | | |
258 | | OSSL_TIME ossl_quic_port_get_time(QUIC_PORT *port) |
259 | 123M | { |
260 | 123M | return ossl_quic_engine_get_time(port->engine); |
261 | 123M | } |
262 | | |
263 | | static OSSL_TIME get_time(void *port) |
264 | 5.81M | { |
265 | 5.81M | return ossl_quic_port_get_time((QUIC_PORT *)port); |
266 | 5.81M | } |
267 | | |
268 | | int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT *port) |
269 | 41.7k | { |
270 | 41.7k | return port->rx_short_dcid_len; |
271 | 41.7k | } |
272 | | |
273 | | int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT *port) |
274 | 41.7k | { |
275 | 41.7k | return port->tx_init_dcid_len; |
276 | 41.7k | } |
277 | | |
278 | | size_t ossl_quic_port_get_num_incoming_channels(const QUIC_PORT *port) |
279 | 0 | { |
280 | 0 | return ossl_list_incoming_ch_num(&port->incoming_channel_list); |
281 | 0 | } |
282 | | |
283 | | /* |
284 | | * QUIC Port: Network BIO Configuration |
285 | | * ==================================== |
286 | | */ |
287 | | |
288 | | /* Determines whether we can support a given poll descriptor. */ |
289 | | static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d) |
290 | 64.0M | { |
291 | 64.0M | if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) { |
292 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
293 | 0 | return 0; |
294 | 0 | } |
295 | | |
296 | 64.0M | return 1; |
297 | 64.0M | } |
298 | | |
299 | | BIO *ossl_quic_port_get_net_rbio(QUIC_PORT *port) |
300 | 29.4M | { |
301 | 29.4M | return port->net_rbio; |
302 | 29.4M | } |
303 | | |
304 | | BIO *ossl_quic_port_get_net_wbio(QUIC_PORT *port) |
305 | 29.4M | { |
306 | 29.4M | return port->net_wbio; |
307 | 29.4M | } |
308 | | |
309 | | static int port_update_poll_desc(QUIC_PORT *port, BIO *net_bio, int for_write) |
310 | 64.0M | { |
311 | 64.0M | BIO_POLL_DESCRIPTOR d = { 0 }; |
312 | | |
313 | 64.0M | if (net_bio == NULL |
314 | 64.0M | || (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d)) |
315 | 31.9M | || (for_write && !BIO_get_wpoll_descriptor(net_bio, &d))) |
316 | | /* Non-pollable BIO */ |
317 | 64.0M | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; |
318 | | |
319 | 64.0M | if (!validate_poll_descriptor(&d)) |
320 | 0 | return 0; |
321 | | |
322 | | /* |
323 | | * TODO(QUIC MULTIPORT): We currently only support one port per |
324 | | * engine/domain. This is necessitated because QUIC_REACTOR only supports a |
325 | | * single pollable currently. In the future, once complete polling |
326 | | * infrastructure has been implemented, this limitation can be removed. |
327 | | * |
328 | | * For now, just update the descriptor on the engine's reactor as we are |
329 | | * guaranteed to be the only port under it. |
330 | | */ |
331 | 64.0M | if (for_write) |
332 | 32.0M | ossl_quic_reactor_set_poll_w(&port->engine->rtor, &d); |
333 | 32.0M | else |
334 | 32.0M | ossl_quic_reactor_set_poll_r(&port->engine->rtor, &d); |
335 | | |
336 | 64.0M | return 1; |
337 | 64.0M | } |
338 | | |
339 | | int ossl_quic_port_update_poll_descriptors(QUIC_PORT *port, int force) |
340 | 100M | { |
341 | 100M | int ok = 1; |
342 | | |
343 | 100M | if (!force && !port->bio_changed) |
344 | 71.3M | return 0; |
345 | | |
346 | 29.3M | if (!port_update_poll_desc(port, port->net_rbio, /*for_write=*/0)) |
347 | 0 | ok = 0; |
348 | | |
349 | 29.3M | if (!port_update_poll_desc(port, port->net_wbio, /*for_write=*/1)) |
350 | 0 | ok = 0; |
351 | | |
352 | 29.3M | port->bio_changed = 0; |
353 | 29.3M | return ok; |
354 | 100M | } |
355 | | |
356 | | /* |
357 | | * We need to determine our addressing mode. There are basically two ways we can |
358 | | * use L4 addresses: |
359 | | * |
360 | | * - Addressed mode, in which our BIO_sendmmsg calls have destination |
361 | | * addresses attached to them which we expect the underlying network BIO to |
362 | | * handle; |
363 | | * |
364 | | * - Unaddressed mode, in which the BIO provided to us on the network side |
365 | | * neither provides us with L4 addresses nor is capable of honouring ones we |
366 | | * provide. We don't know where the QUIC traffic we send ends up exactly and |
367 | | * trust the application to know what it is doing. |
368 | | * |
369 | | * Addressed mode is preferred because it enables support for connection |
370 | | * migration, multipath, etc. in the future. Addressed mode is automatically |
371 | | * enabled if we are using e.g. BIO_s_datagram, with or without BIO_s_connect. |
372 | | * |
373 | | * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to use |
374 | | * unaddressed mode unless that BIO supports capability flags indicating it can |
375 | | * provide and honour L4 addresses. |
376 | | * |
377 | | * Our strategy for determining address mode is simple: we probe the underlying |
378 | | * network BIOs for their capabilities. If the network BIOs support what we |
379 | | * need, we use addressed mode. Otherwise, we use unaddressed mode. |
380 | | * |
381 | | * If addressed mode is chosen, we require an initial peer address to be set. If |
382 | | * this is not set, we fail. If unaddressed mode is used, we do not require |
383 | | * this, as such an address is superfluous, though it can be set if desired. |
384 | | */ |
385 | | static void port_update_addressing_mode(QUIC_PORT *port) |
386 | 66.4k | { |
387 | 66.4k | long rcaps = 0, wcaps = 0; |
388 | | |
389 | 66.4k | if (port->net_rbio != NULL) |
390 | 66.4k | rcaps = BIO_dgram_get_effective_caps(port->net_rbio); |
391 | | |
392 | 66.4k | if (port->net_wbio != NULL) |
393 | 33.2k | wcaps = BIO_dgram_get_effective_caps(port->net_wbio); |
394 | | |
395 | 66.4k | port->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0); |
396 | 66.4k | port->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0); |
397 | 66.4k | port->bio_changed = 1; |
398 | 66.4k | } |
399 | | |
400 | | int ossl_quic_port_is_addressed_r(const QUIC_PORT *port) |
401 | 0 | { |
402 | 0 | return port->addressed_mode_r; |
403 | 0 | } |
404 | | |
405 | | int ossl_quic_port_is_addressed_w(const QUIC_PORT *port) |
406 | 65.7k | { |
407 | 65.7k | return port->addressed_mode_w; |
408 | 65.7k | } |
409 | | |
410 | | int ossl_quic_port_is_addressed(const QUIC_PORT *port) |
411 | 0 | { |
412 | 0 | return ossl_quic_port_is_addressed_r(port) && ossl_quic_port_is_addressed_w(port); |
413 | 0 | } |
414 | | |
415 | | /* |
416 | | * QUIC_PORT does not ref any BIO it is provided with, nor is any ref |
417 | | * transferred to it. The caller (e.g., QUIC_CONNECTION) is responsible for |
418 | | * ensuring the BIO lasts until the channel is freed or the BIO is switched out |
419 | | * for another BIO by a subsequent successful call to this function. |
420 | | */ |
421 | | int ossl_quic_port_set_net_rbio(QUIC_PORT *port, BIO *net_rbio) |
422 | 50.8k | { |
423 | 50.8k | if (port->net_rbio == net_rbio) |
424 | 8.82k | return 1; |
425 | | |
426 | 42.0k | if (!port_update_poll_desc(port, net_rbio, /*for_write=*/0)) |
427 | 0 | return 0; |
428 | | |
429 | 42.0k | ossl_quic_demux_set_bio(port->demux, net_rbio); |
430 | 42.0k | port->net_rbio = net_rbio; |
431 | 42.0k | port_update_addressing_mode(port); |
432 | 42.0k | return 1; |
433 | 42.0k | } |
434 | | |
435 | | int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio) |
436 | 50.8k | { |
437 | 50.8k | QUIC_CHANNEL *ch; |
438 | | |
439 | 50.8k | if (port->net_wbio == net_wbio) |
440 | 8.82k | return 1; |
441 | | |
442 | 42.0k | if (!port_update_poll_desc(port, net_wbio, /*for_write=*/1)) |
443 | 0 | return 0; |
444 | | |
445 | 42.0k | OSSL_LIST_FOREACH(ch, ch, &port->channel_list) |
446 | 41.7k | ossl_qtx_set_bio(ch->qtx, net_wbio); |
447 | | |
448 | 42.0k | port->net_wbio = net_wbio; |
449 | 42.0k | port_update_addressing_mode(port); |
450 | 42.0k | return 1; |
451 | 42.0k | } |
452 | | |
453 | | SSL_CTX *ossl_quic_port_get_channel_ctx(QUIC_PORT *port) |
454 | 1.57k | { |
455 | 1.57k | return port->channel_ctx; |
456 | 1.57k | } |
457 | | |
458 | | /* |
459 | | * QUIC Port: Channel Lifecycle |
460 | | * ============================ |
461 | | */ |
462 | | |
463 | | static SSL *port_new_handshake_layer(QUIC_PORT *port, QUIC_CHANNEL *ch) |
464 | 0 | { |
465 | 0 | SSL *tls = NULL; |
466 | 0 | SSL_CONNECTION *tls_conn = NULL; |
467 | 0 | SSL *user_ssl = NULL; |
468 | 0 | QUIC_CONNECTION *qc = NULL; |
469 | 0 | QUIC_LISTENER *ql = NULL; |
470 | | |
471 | | /* |
472 | | * It only makes sense to call this function if we know how to associate |
473 | | * the handshake layer we are about to create with some user_ssl object. |
474 | | */ |
475 | 0 | if (!ossl_assert(port->get_conn_user_ssl != NULL)) |
476 | 0 | return NULL; |
477 | 0 | user_ssl = port->get_conn_user_ssl(ch, port->user_ssl_arg); |
478 | 0 | if (user_ssl == NULL) |
479 | 0 | return NULL; |
480 | 0 | qc = (QUIC_CONNECTION *)user_ssl; |
481 | 0 | ql = (QUIC_LISTENER *)port->user_ssl_arg; |
482 | | |
483 | | /* |
484 | | * We expect the user_ssl to be newly created so it must not have an |
485 | | * existing qc->tls |
486 | | */ |
487 | 0 | if (!ossl_assert(qc->tls == NULL)) { |
488 | 0 | SSL_free(user_ssl); |
489 | 0 | return NULL; |
490 | 0 | } |
491 | | |
492 | 0 | tls = ossl_ssl_connection_new_int(port->channel_ctx, user_ssl, TLS_method()); |
493 | 0 | qc->tls = tls; |
494 | 0 | if (tls == NULL || (tls_conn = SSL_CONNECTION_FROM_SSL(tls)) == NULL) { |
495 | 0 | SSL_free(user_ssl); |
496 | 0 | return NULL; |
497 | 0 | } |
498 | | |
499 | 0 | if (ql != NULL && ql->obj.ssl.ctx->new_pending_conn_cb != NULL) |
500 | 0 | if (!ql->obj.ssl.ctx->new_pending_conn_cb(ql->obj.ssl.ctx, user_ssl, |
501 | 0 | ql->obj.ssl.ctx->new_pending_conn_arg)) { |
502 | 0 | SSL_free(user_ssl); |
503 | 0 | return NULL; |
504 | 0 | } |
505 | | |
506 | | /* Override the user_ssl of the inner connection. */ |
507 | 0 | tls_conn->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL; |
508 | | |
509 | | /* Restrict options derived from the SSL_CTX. */ |
510 | 0 | tls_conn->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN; |
511 | 0 | tls_conn->pha_enabled = 0; |
512 | 0 | return tls; |
513 | 0 | } |
514 | | |
515 | | static QUIC_CHANNEL *port_make_channel(QUIC_PORT *port, SSL *tls, OSSL_QRX *qrx, |
516 | | int is_server, int is_tserver) |
517 | 16.6k | { |
518 | 16.6k | QUIC_CHANNEL_ARGS args = { 0 }; |
519 | 16.6k | QUIC_CHANNEL *ch; |
520 | | |
521 | 16.6k | args.port = port; |
522 | 16.6k | args.is_server = is_server; |
523 | 16.6k | args.lcidm = port->lcidm; |
524 | 16.6k | args.srtm = port->srtm; |
525 | 16.6k | args.qrx = qrx; |
526 | 16.6k | args.is_tserver_ch = is_tserver; |
527 | | |
528 | | /* |
529 | | * Creating a a new channel is made a bit tricky here as there is a |
530 | | * bit of a circular dependency. Initializing a channel requires that |
531 | | * the ch->tls and optionally the qlog_title be configured prior to |
532 | | * initialization, but we need the channel at least partially configured |
533 | | * to create the new handshake layer, so we have to do this in a few steps. |
534 | | */ |
535 | | |
536 | | /* |
537 | | * start by allocation and provisioning as much of the channel as we can |
538 | | */ |
539 | 16.6k | ch = ossl_quic_channel_alloc(&args); |
540 | 16.6k | if (ch == NULL) { |
541 | 0 | ossl_qrx_free(qrx); |
542 | 0 | return NULL; |
543 | 0 | } |
544 | | |
545 | | /* |
546 | | * Fixup the channel tls connection here before we init the channel |
547 | | */ |
548 | 16.6k | ch->tls = (tls != NULL) ? tls : port_new_handshake_layer(port, ch); |
549 | | |
550 | 16.6k | if (ch->tls == NULL) { |
551 | 0 | OPENSSL_free(ch); |
552 | 0 | return NULL; |
553 | 0 | } |
554 | | |
555 | 16.6k | #ifndef OPENSSL_NO_QLOG |
556 | | /* |
557 | | * If we're using qlog, make sure the tls get further configured properly |
558 | | */ |
559 | 16.6k | ch->use_qlog = 1; |
560 | 16.6k | if (ch->tls != NULL && ch->tls->ctx->qlog_title != NULL) { |
561 | 0 | OPENSSL_free(ch->qlog_title); |
562 | 0 | if ((ch->qlog_title = OPENSSL_strdup(ch->tls->ctx->qlog_title)) == NULL) { |
563 | 0 | ossl_quic_channel_free(ch); |
564 | 0 | return NULL; |
565 | 0 | } |
566 | 0 | } |
567 | 16.6k | #endif |
568 | | |
569 | | /* |
570 | | * And finally init the channel struct |
571 | | */ |
572 | 16.6k | if (!ossl_quic_channel_init(ch)) { |
573 | 0 | OPENSSL_free(ch); |
574 | 0 | return NULL; |
575 | 0 | } |
576 | | |
577 | 16.6k | ossl_qtx_set_bio(ch->qtx, port->net_wbio); |
578 | 16.6k | return ch; |
579 | 16.6k | } |
580 | | |
581 | | QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls) |
582 | 41.7k | { |
583 | 41.7k | return port_make_channel(port, tls, NULL, /* is_server= */ 0, |
584 | 41.7k | /* is_tserver= */ 0); |
585 | 41.7k | } |
586 | | |
587 | | QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls) |
588 | 0 | { |
589 | 0 | QUIC_CHANNEL *ch; |
590 | |
|
591 | 0 | assert(port->tserver_ch == NULL); |
592 | | |
593 | | /* |
594 | | * pass -1 for qrx to indicate port will create qrx |
595 | | * later in port_default_packet_handler() when calling port_bind_channel(). |
596 | | */ |
597 | 0 | ch = port_make_channel(port, tls, NULL, /* is_server= */ 1, |
598 | 0 | /* is_tserver_ch */ 1); |
599 | 0 | port->tserver_ch = ch; |
600 | 0 | port->allow_incoming = 1; |
601 | 0 | return ch; |
602 | 0 | } |
603 | | |
604 | | QUIC_CHANNEL *ossl_quic_port_pop_incoming(QUIC_PORT *port) |
605 | 1.28k | { |
606 | 1.28k | QUIC_CHANNEL *ch; |
607 | | |
608 | 1.28k | ch = ossl_list_incoming_ch_head(&port->incoming_channel_list); |
609 | 1.28k | if (ch == NULL) |
610 | 1.28k | return NULL; |
611 | | |
612 | 0 | ossl_list_incoming_ch_remove(&port->incoming_channel_list, ch); |
613 | 0 | return ch; |
614 | 1.28k | } |
615 | | |
616 | | int ossl_quic_port_have_incoming(QUIC_PORT *port) |
617 | 0 | { |
618 | 0 | return ossl_list_incoming_ch_head(&port->incoming_channel_list) != NULL; |
619 | 0 | } |
620 | | |
621 | | void ossl_quic_port_drop_incoming(QUIC_PORT *port) |
622 | 242 | { |
623 | 242 | QUIC_CHANNEL *ch; |
624 | 242 | SSL *tls; |
625 | 242 | SSL *user_ssl; |
626 | 242 | SSL_CONNECTION *sc; |
627 | | |
628 | 242 | for (;;) { |
629 | 242 | ch = ossl_quic_port_pop_incoming(port); |
630 | 242 | if (ch == NULL) |
631 | 242 | break; |
632 | | |
633 | 0 | tls = ossl_quic_channel_get0_tls(ch); |
634 | | /* |
635 | | * The user ssl may or may not have been created via the |
636 | | * get_conn_user_ssl callback in the QUIC stack. The |
637 | | * differentiation being if the user_ssl pointer and tls pointer |
638 | | * are different. If they are, then the user_ssl needs freeing here |
639 | | * which sends us through ossl_quic_free, which then drops the actual |
640 | | * ch->tls ref and frees the channel |
641 | | */ |
642 | 0 | sc = SSL_CONNECTION_FROM_SSL(tls); |
643 | 0 | if (sc == NULL) |
644 | 0 | break; |
645 | | |
646 | 0 | user_ssl = SSL_CONNECTION_GET_USER_SSL(sc); |
647 | 0 | if (user_ssl == tls) { |
648 | 0 | ossl_quic_channel_free(ch); |
649 | 0 | SSL_free(tls); |
650 | 0 | } else { |
651 | 0 | SSL_free(user_ssl); |
652 | 0 | } |
653 | 0 | } |
654 | 242 | } |
655 | | |
656 | | void ossl_quic_port_set_allow_incoming(QUIC_PORT *port, int allow_incoming) |
657 | 640 | { |
658 | 640 | port->allow_incoming = allow_incoming; |
659 | 640 | } |
660 | | |
661 | | /* |
662 | | * QUIC Port: Ticker-Mutator |
663 | | * ========================= |
664 | | */ |
665 | | |
666 | | /* |
667 | | * Tick function for this port. This does everything related to network I/O for |
668 | | * this port's network BIOs, and services child channels. |
669 | | */ |
670 | | void ossl_quic_port_subtick(QUIC_PORT *port, QUIC_TICK_RESULT *res, |
671 | | uint32_t flags) |
672 | 49.1M | { |
673 | 49.1M | QUIC_CHANNEL *ch; |
674 | | |
675 | 49.1M | res->net_read_desired = ossl_quic_port_is_running(port); |
676 | 49.1M | res->net_write_desired = 0; |
677 | 49.1M | res->notify_other_threads = 0; |
678 | 49.1M | res->tick_deadline = ossl_time_infinite(); |
679 | | |
680 | 49.1M | if (!port->engine->inhibit_tick) { |
681 | | /* Handle any incoming data from network. */ |
682 | 49.1M | if (ossl_quic_port_is_running(port)) |
683 | 49.1M | port_rx_pre(port); |
684 | | |
685 | | /* Iterate through all channels and service them. */ |
686 | 49.1M | OSSL_LIST_FOREACH(ch, ch, &port->channel_list) |
687 | 49.1M | { |
688 | 49.1M | QUIC_TICK_RESULT subr = { 0 }; |
689 | | |
690 | 49.1M | ossl_quic_channel_subtick(ch, &subr, flags); |
691 | 49.1M | ossl_quic_tick_result_merge_into(res, &subr); |
692 | 49.1M | } |
693 | 49.1M | } |
694 | 49.1M | } |
695 | | |
696 | | /* Process incoming datagrams, if any. */ |
697 | | static void port_rx_pre(QUIC_PORT *port) |
698 | 49.1M | { |
699 | 49.1M | int ret; |
700 | | |
701 | | /* |
702 | | * Originally, this check (don't RX before we have sent anything if we are |
703 | | * not a server, because there can't be anything) was just intended as a |
704 | | * minor optimisation. However, it is actually required on Windows, and |
705 | | * removing this check will cause Windows to break. |
706 | | * |
707 | | * The reason is that under Win32, recvfrom() does not work on a UDP socket |
708 | | * which has not had bind() called (???). However, calling sendto() will |
709 | | * automatically bind an unbound UDP socket. Therefore, if we call a Winsock |
710 | | * recv-type function before calling a Winsock send-type function, that call |
711 | | * will fail with WSAEINVAL, which we will regard as a permanent network |
712 | | * error. |
713 | | * |
714 | | * Therefore, this check is essential as we do not require our API users to |
715 | | * bind a socket first when using the API in client mode. |
716 | | */ |
717 | 49.1M | if (!port->allow_incoming && !port->have_sent_any_pkt) |
718 | 41.7k | return; |
719 | | |
720 | | /* |
721 | | * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams |
722 | | * to the appropriate QRX instances. |
723 | | */ |
724 | 49.1M | ret = ossl_quic_demux_pump(port->demux); |
725 | 49.1M | if (ret == QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL) |
726 | | /* |
727 | | * We don't care about transient failure, but permanent failure means we |
728 | | * should tear down the port. All connections skip straight to the |
729 | | * Terminated state as there is no point trying to send CONNECTION_CLOSE |
730 | | * frames if the network BIO is not operating correctly. |
731 | | */ |
732 | 0 | ossl_quic_port_raise_net_error(port, NULL); |
733 | 49.1M | } |
734 | | |
735 | | /* |
736 | | * Handles an incoming connection request and potentially decides to make a |
737 | | * connection from it. If a new connection is made, the new channel is written |
738 | | * to *new_ch. |
739 | | */ |
740 | | static void port_bind_channel(QUIC_PORT *port, const BIO_ADDR *peer, |
741 | | const QUIC_CONN_ID *dcid, |
742 | | const QUIC_CONN_ID *odcid, OSSL_QRX *qrx, |
743 | | QUIC_CHANNEL **new_ch) |
744 | 0 | { |
745 | 0 | QUIC_CHANNEL *ch; |
746 | | |
747 | | /* |
748 | | * If we're running with a simulated tserver, it will already have |
749 | | * a dummy channel created, use that instead |
750 | | */ |
751 | 0 | if (port->tserver_ch != NULL) { |
752 | 0 | ch = port->tserver_ch; |
753 | 0 | port->tserver_ch = NULL; |
754 | 0 | ossl_quic_channel_bind_qrx(ch, qrx); |
755 | 0 | ossl_qrx_set_msg_callback(ch->qrx, ch->msg_callback, |
756 | 0 | ch->msg_callback_ssl); |
757 | 0 | ossl_qrx_set_msg_callback_arg(ch->qrx, ch->msg_callback_arg); |
758 | 0 | } else { |
759 | 0 | ch = port_make_channel(port, NULL, qrx, /* is_server= */ 1, |
760 | 0 | /* is_tserver */ 0); |
761 | 0 | } |
762 | |
|
763 | 0 | if (ch == NULL) |
764 | 0 | return; |
765 | | |
766 | | /* |
767 | | * If we didn't provide a qrx here that means we need to set our initial |
768 | | * secret here, since we just created a qrx |
769 | | * Normally its not needed, as the initial secret gets added when we send |
770 | | * our first server hello, but if we get a huge client hello, crossing |
771 | | * multiple datagrams, we don't have a chance to do that, and datagrams |
772 | | * after the first won't get decoded properly, for lack of secrets |
773 | | */ |
774 | 0 | if (qrx == NULL) |
775 | 0 | if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx, |
776 | 0 | ch->port->engine->propq, |
777 | 0 | dcid, /* is_server */ 1, |
778 | 0 | ch->qrx, NULL)) { |
779 | 0 | ossl_quic_channel_free(ch); |
780 | 0 | return; |
781 | 0 | } |
782 | | |
783 | 0 | if (odcid->id_len != 0) { |
784 | | /* |
785 | | * If we have an odcid, then we went through server address validation |
786 | | * and as such, this channel need not conform to the 3x validation cap |
787 | | * See RFC 9000 s. 8.1 |
788 | | */ |
789 | 0 | ossl_quic_tx_packetiser_set_validated(ch->txp); |
790 | 0 | if (!ossl_quic_bind_channel(ch, peer, dcid, odcid)) { |
791 | 0 | ossl_quic_channel_free(ch); |
792 | 0 | return; |
793 | 0 | } |
794 | 0 | } else { |
795 | | /* |
796 | | * No odcid means we didn't do server validation, so we need to |
797 | | * generate a cid via ossl_quic_channel_on_new_conn |
798 | | */ |
799 | 0 | if (!ossl_quic_channel_on_new_conn(ch, peer, dcid)) { |
800 | 0 | ossl_quic_channel_free(ch); |
801 | 0 | return; |
802 | 0 | } |
803 | 0 | } |
804 | | |
805 | 0 | ossl_list_incoming_ch_insert_tail(&port->incoming_channel_list, ch); |
806 | 0 | *new_ch = ch; |
807 | 0 | } |
808 | | |
809 | | static int port_try_handle_stateless_reset(QUIC_PORT *port, const QUIC_URXE *e) |
810 | 7.43M | { |
811 | 7.43M | size_t i; |
812 | 7.43M | const unsigned char *data = ossl_quic_urxe_data(e); |
813 | 7.43M | void *opaque = NULL; |
814 | | |
815 | | /* |
816 | | * Perform some fast and cheap checks for a packet not being a stateless |
817 | | * reset token. RFC 9000 s. 10.3 specifies this layout for stateless |
818 | | * reset packets: |
819 | | * |
820 | | * Stateless Reset { |
821 | | * Fixed Bits (2) = 1, |
822 | | * Unpredictable Bits (38..), |
823 | | * Stateless Reset Token (128), |
824 | | * } |
825 | | * |
826 | | * It also specifies: |
827 | | * However, endpoints MUST treat any packet ending in a valid |
828 | | * stateless reset token as a Stateless Reset, as other QUIC |
829 | | * versions might allow the use of a long header. |
830 | | * |
831 | | * We can rapidly check for the minimum length and that the first pair |
832 | | * of bits in the first byte are 01 or 11. |
833 | | * |
834 | | * The function returns 1 if it is a stateless reset packet, 0 if it isn't |
835 | | * and -1 if an error was encountered. |
836 | | */ |
837 | 7.43M | if (e->data_len < QUIC_STATELESS_RESET_TOKEN_LEN + 5 |
838 | 2.69M | || (0100 & *data) != 0100) |
839 | 4.98M | return 0; |
840 | | |
841 | 2.45M | for (i = 0;; ++i) { |
842 | 2.45M | if (!ossl_quic_srtm_lookup(port->srtm, |
843 | 2.45M | (QUIC_STATELESS_RESET_TOKEN *)(data + e->data_len |
844 | 2.45M | - sizeof(QUIC_STATELESS_RESET_TOKEN)), |
845 | 2.45M | i, &opaque, NULL)) |
846 | 2.45M | break; |
847 | | |
848 | 2.45M | assert(opaque != NULL); |
849 | 27 | ossl_quic_channel_on_stateless_reset((QUIC_CHANNEL *)opaque); |
850 | 27 | } |
851 | | |
852 | 2.45M | return i > 0; |
853 | 2.45M | } |
854 | | |
855 | | static void cleanup_validation_token(QUIC_VALIDATION_TOKEN *token) |
856 | 0 | { |
857 | 0 | OPENSSL_free(token->remote_addr); |
858 | 0 | } |
859 | | |
860 | | /** |
861 | | * @brief Generates a validation token for a RETRY/NEW_TOKEN packet. |
862 | | * |
863 | | * |
864 | | * @param peer Address of the client peer receiving the packet. |
865 | | * @param odcid DCID of the connection attempt. |
866 | | * @param rscid Retry source connection ID of the connection attempt. |
867 | | * @param token Address of token to fill data. |
868 | | * |
869 | | * @return 1 if validation token is filled successfully, 0 otherwise. |
870 | | */ |
871 | | static int generate_token(BIO_ADDR *peer, QUIC_CONN_ID odcid, |
872 | | QUIC_CONN_ID rscid, QUIC_VALIDATION_TOKEN *token, |
873 | | int is_retry) |
874 | 0 | { |
875 | 0 | token->is_retry = is_retry; |
876 | 0 | token->timestamp = ossl_time_now(); |
877 | 0 | token->remote_addr = NULL; |
878 | 0 | token->odcid = odcid; |
879 | 0 | token->rscid = rscid; |
880 | |
|
881 | 0 | if (!BIO_ADDR_rawaddress(peer, NULL, &token->remote_addr_len) |
882 | 0 | || token->remote_addr_len == 0 |
883 | 0 | || (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL |
884 | 0 | || !BIO_ADDR_rawaddress(peer, token->remote_addr, |
885 | 0 | &token->remote_addr_len)) { |
886 | 0 | cleanup_validation_token(token); |
887 | 0 | return 0; |
888 | 0 | } |
889 | | |
890 | 0 | return 1; |
891 | 0 | } |
892 | | |
893 | | /** |
894 | | * @brief Marshals a validation token into a new buffer. |
895 | | * |
896 | | * |buffer| should already be allocated and at least MARSHALLED_TOKEN_MAX_LEN |
897 | | * bytes long. Stores the length of data stored in |buffer| in |buffer_len|. |
898 | | * |
899 | | * @param token Validation token. |
900 | | * @param buffer Address to store the marshalled token. |
901 | | * @param buffer_len Size of data stored in |buffer|. |
902 | | */ |
903 | | static int marshal_validation_token(QUIC_VALIDATION_TOKEN *token, |
904 | | unsigned char *buffer, size_t *buffer_len) |
905 | 0 | { |
906 | 0 | WPACKET wpkt = { 0 }; |
907 | 0 | BUF_MEM *buf_mem = BUF_MEM_new(); |
908 | |
|
909 | 0 | if (buffer == NULL || buf_mem == NULL |
910 | 0 | || (token->is_retry != 0 && token->is_retry != 1)) { |
911 | 0 | BUF_MEM_free(buf_mem); |
912 | 0 | return 0; |
913 | 0 | } |
914 | | |
915 | 0 | if (!WPACKET_init(&wpkt, buf_mem) |
916 | 0 | || !WPACKET_memset(&wpkt, token->is_retry, 1) |
917 | 0 | || !WPACKET_memcpy(&wpkt, &token->timestamp, |
918 | 0 | sizeof(token->timestamp)) |
919 | 0 | || (token->is_retry |
920 | 0 | && (!WPACKET_sub_memcpy_u8(&wpkt, &token->odcid.id, |
921 | 0 | token->odcid.id_len) |
922 | 0 | || !WPACKET_sub_memcpy_u8(&wpkt, &token->rscid.id, |
923 | 0 | token->rscid.id_len))) |
924 | 0 | || !WPACKET_sub_memcpy_u8(&wpkt, token->remote_addr, token->remote_addr_len) |
925 | 0 | || !WPACKET_get_total_written(&wpkt, buffer_len) |
926 | 0 | || *buffer_len > MARSHALLED_TOKEN_MAX_LEN |
927 | 0 | || !WPACKET_finish(&wpkt)) { |
928 | 0 | WPACKET_cleanup(&wpkt); |
929 | 0 | BUF_MEM_free(buf_mem); |
930 | 0 | return 0; |
931 | 0 | } |
932 | | |
933 | 0 | memcpy(buffer, buf_mem->data, *buffer_len); |
934 | 0 | BUF_MEM_free(buf_mem); |
935 | 0 | return 1; |
936 | 0 | } |
937 | | |
938 | | /** |
939 | | * @brief Encrypts a validation token using AES-256-GCM |
940 | | * |
941 | | * @param port The QUIC port containing the encryption key |
942 | | * @param plaintext The data to encrypt |
943 | | * @param pt_len Length of the plaintext |
944 | | * @param ciphertext Buffer to receive encrypted data. If NULL, ct_len will be |
945 | | * set to the required buffer size and function returns |
946 | | * immediately. |
947 | | * @param ct_len Pointer to size_t that will receive the ciphertext length. |
948 | | * This also includes bytes for QUIC_RETRY_INTEGRITY_TAG_LEN. |
949 | | * |
950 | | * @return 1 on success, 0 on failure |
951 | | * |
952 | | * The ciphertext format is: |
953 | | * [EVP_GCM_IV_LEN bytes IV][encrypted data][EVP_GCM_TAG_LEN bytes tag] |
954 | | */ |
955 | | static int encrypt_validation_token(const QUIC_PORT *port, |
956 | | const unsigned char *plaintext, |
957 | | size_t pt_len, |
958 | | unsigned char *ciphertext, |
959 | | size_t *ct_len) |
960 | 0 | { |
961 | 0 | int iv_len, len, ret = 0; |
962 | 0 | int tag_len; |
963 | 0 | unsigned char *iv = ciphertext, *data, *tag; |
964 | |
|
965 | 0 | if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) <= 0 |
966 | 0 | || (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0) |
967 | 0 | goto err; |
968 | | |
969 | 0 | *ct_len = iv_len + pt_len + tag_len + QUIC_RETRY_INTEGRITY_TAG_LEN; |
970 | 0 | if (ciphertext == NULL) { |
971 | 0 | ret = 1; |
972 | 0 | goto err; |
973 | 0 | } |
974 | | |
975 | 0 | data = ciphertext + iv_len; |
976 | 0 | tag = data + pt_len; |
977 | |
|
978 | 0 | if (!RAND_bytes_ex(port->engine->libctx, ciphertext, iv_len, 0) |
979 | 0 | || !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv) |
980 | 0 | || !EVP_EncryptUpdate(port->token_ctx, data, &len, plaintext, (int)pt_len) |
981 | 0 | || !EVP_EncryptFinal_ex(port->token_ctx, data + pt_len, &len) |
982 | 0 | || !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_GET_TAG, tag_len, tag)) |
983 | 0 | goto err; |
984 | | |
985 | 0 | ret = 1; |
986 | 0 | err: |
987 | 0 | return ret; |
988 | 0 | } |
989 | | |
990 | | /** |
991 | | * @brief Decrypts a validation token using AES-256-GCM |
992 | | * |
993 | | * @param port The QUIC port containing the decryption key |
994 | | * @param ciphertext The encrypted data (including IV and tag) |
995 | | * @param ct_len Length of the ciphertext |
996 | | * @param plaintext Buffer to receive decrypted data. If NULL, pt_len will be |
997 | | * set to the required buffer size. |
998 | | * @param pt_len Pointer to size_t that will receive the plaintext length |
999 | | * |
1000 | | * @return 1 on success, 0 on failure |
1001 | | * |
1002 | | * Expected ciphertext format: |
1003 | | * [EVP_GCM_IV_LEN bytes IV][encrypted data][EVP_GCM_TAG_LEN bytes tag] |
1004 | | */ |
1005 | | static int decrypt_validation_token(const QUIC_PORT *port, |
1006 | | const unsigned char *ciphertext, |
1007 | | size_t ct_len, |
1008 | | unsigned char *plaintext, |
1009 | | size_t *pt_len) |
1010 | 0 | { |
1011 | 0 | int iv_len, len = 0, ret = 0; |
1012 | 0 | int tag_len; |
1013 | 0 | const unsigned char *iv = ciphertext, *data, *tag; |
1014 | |
|
1015 | 0 | if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) <= 0 |
1016 | 0 | || (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0) |
1017 | 0 | goto err; |
1018 | | |
1019 | | /* Prevent decryption of a buffer that is not within reasonable bounds */ |
1020 | 0 | if (ct_len < (size_t)(iv_len + tag_len) || ct_len > ENCRYPTED_TOKEN_MAX_LEN) |
1021 | 0 | goto err; |
1022 | | |
1023 | 0 | *pt_len = ct_len - iv_len - tag_len; |
1024 | 0 | if (plaintext == NULL) { |
1025 | 0 | ret = 1; |
1026 | 0 | goto err; |
1027 | 0 | } |
1028 | | |
1029 | 0 | data = ciphertext + iv_len; |
1030 | 0 | tag = ciphertext + ct_len - tag_len; |
1031 | |
|
1032 | 0 | if (!EVP_DecryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv) |
1033 | 0 | || !EVP_DecryptUpdate(port->token_ctx, plaintext, &len, data, |
1034 | 0 | (int)(ct_len - iv_len - tag_len)) |
1035 | 0 | || !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_SET_TAG, tag_len, |
1036 | 0 | (void *)tag) |
1037 | 0 | || !EVP_DecryptFinal_ex(port->token_ctx, plaintext + len, &len)) |
1038 | 0 | goto err; |
1039 | | |
1040 | 0 | ret = 1; |
1041 | |
|
1042 | 0 | err: |
1043 | 0 | return ret; |
1044 | 0 | } |
1045 | | |
1046 | | /** |
1047 | | * @brief Parses contents of a buffer into a validation token. |
1048 | | * |
1049 | | * VALIDATION_TOKEN should already be initialized. Does some basic sanity checks. |
1050 | | * |
1051 | | * @param token Validation token to fill data in. |
1052 | | * @param buf Buffer of previously marshaled validation token. |
1053 | | * @param buf_len Length of |buf|. |
1054 | | */ |
1055 | | static int parse_validation_token(QUIC_VALIDATION_TOKEN *token, |
1056 | | const unsigned char *buf, size_t buf_len) |
1057 | 0 | { |
1058 | 0 | PACKET pkt, subpkt; |
1059 | |
|
1060 | 0 | if (buf == NULL || token == NULL) |
1061 | 0 | return 0; |
1062 | | |
1063 | 0 | token->remote_addr = NULL; |
1064 | |
|
1065 | 0 | if (!PACKET_buf_init(&pkt, buf, buf_len) |
1066 | 0 | || !PACKET_copy_bytes(&pkt, &token->is_retry, sizeof(token->is_retry)) |
1067 | 0 | || !(token->is_retry == 0 || token->is_retry == 1) |
1068 | 0 | || !PACKET_copy_bytes(&pkt, (unsigned char *)&token->timestamp, |
1069 | 0 | sizeof(token->timestamp)) |
1070 | 0 | || (token->is_retry |
1071 | 0 | && (!PACKET_get_length_prefixed_1(&pkt, &subpkt) |
1072 | 0 | || (token->odcid.id_len = (unsigned char)PACKET_remaining(&subpkt)) |
1073 | 0 | > QUIC_MAX_CONN_ID_LEN |
1074 | 0 | || !PACKET_copy_bytes(&subpkt, |
1075 | 0 | (unsigned char *)&token->odcid.id, |
1076 | 0 | token->odcid.id_len) |
1077 | 0 | || !PACKET_get_length_prefixed_1(&pkt, &subpkt) |
1078 | 0 | || (token->rscid.id_len = (unsigned char)PACKET_remaining(&subpkt)) |
1079 | 0 | > QUIC_MAX_CONN_ID_LEN |
1080 | 0 | || !PACKET_copy_bytes(&subpkt, (unsigned char *)&token->rscid.id, |
1081 | 0 | token->rscid.id_len))) |
1082 | 0 | || !PACKET_get_length_prefixed_1(&pkt, &subpkt) |
1083 | 0 | || (token->remote_addr_len = PACKET_remaining(&subpkt)) == 0 |
1084 | 0 | || (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL |
1085 | 0 | || !PACKET_copy_bytes(&subpkt, token->remote_addr, token->remote_addr_len) |
1086 | 0 | || PACKET_remaining(&pkt) != 0) { |
1087 | 0 | cleanup_validation_token(token); |
1088 | 0 | return 0; |
1089 | 0 | } |
1090 | | |
1091 | 0 | return 1; |
1092 | 0 | } |
1093 | | |
1094 | | /** |
1095 | | * @brief Sends a QUIC Retry packet to a client. |
1096 | | * |
1097 | | * This function constructs and sends a Retry packet to the specified client |
1098 | | * using the provided connection header information. The Retry packet |
1099 | | * includes a generated validation token and a new connection ID, following |
1100 | | * the QUIC protocol specifications for connection establishment. |
1101 | | * |
1102 | | * @param port Pointer to the QUIC port from which to send the packet. |
1103 | | * @param peer Address of the client peer receiving the packet. |
1104 | | * @param client_hdr Header of the client's initial packet, containing |
1105 | | * connection IDs and other relevant information. |
1106 | | * |
1107 | | * This function performs the following steps: |
1108 | | * - Generates a validation token for the client. |
1109 | | * - Sets the destination and source connection IDs. |
1110 | | * - Calculates the integrity tag and sets the token length. |
1111 | | * - Encodes and sends the packet via the BIO network interface. |
1112 | | * |
1113 | | * Error handling is included for failures in CID generation, encoding, and |
1114 | | * network transmiss |
1115 | | */ |
1116 | | static void port_send_retry(QUIC_PORT *port, |
1117 | | BIO_ADDR *peer, |
1118 | | QUIC_PKT_HDR *client_hdr) |
1119 | 0 | { |
1120 | 0 | BIO_MSG msg[1]; |
1121 | | /* |
1122 | | * Buffer is used for both marshalling the token as well as for the RETRY |
1123 | | * packet. The size of buffer should not be less than |
1124 | | * MARSHALLED_TOKEN_MAX_LEN. |
1125 | | */ |
1126 | 0 | unsigned char buffer[512]; |
1127 | 0 | unsigned char ct_buf[ENCRYPTED_TOKEN_MAX_LEN]; |
1128 | 0 | WPACKET wpkt; |
1129 | 0 | size_t written, token_buf_len, ct_len; |
1130 | 0 | QUIC_PKT_HDR hdr = { 0 }; |
1131 | 0 | QUIC_VALIDATION_TOKEN token = { 0 }; |
1132 | 0 | int ok; |
1133 | |
|
1134 | 0 | if (!ossl_assert(sizeof(buffer) >= MARSHALLED_TOKEN_MAX_LEN)) |
1135 | 0 | return; |
1136 | | /* |
1137 | | * 17.2.5.1 Sending a Retry packet |
1138 | | * dst ConnId is src ConnId we got from client |
1139 | | * src ConnId comes from local conn ID manager |
1140 | | */ |
1141 | 0 | memset(&hdr, 0, sizeof(QUIC_PKT_HDR)); |
1142 | 0 | hdr.dst_conn_id = client_hdr->src_conn_id; |
1143 | | /* |
1144 | | * this is the random connection ID, we expect client is |
1145 | | * going to send the ID with next INITIAL packet which |
1146 | | * will also come with token we generate here. |
1147 | | */ |
1148 | 0 | ok = ossl_quic_lcidm_get_unused_cid(port->lcidm, &hdr.src_conn_id); |
1149 | 0 | if (ok == 0) |
1150 | 0 | goto err; |
1151 | | |
1152 | 0 | memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN)); |
1153 | | |
1154 | | /* Generate retry validation token */ |
1155 | 0 | if (!generate_token(peer, client_hdr->dst_conn_id, |
1156 | 0 | hdr.src_conn_id, &token, 1) |
1157 | 0 | || !marshal_validation_token(&token, buffer, &token_buf_len) |
1158 | 0 | || !encrypt_validation_token(port, buffer, token_buf_len, NULL, |
1159 | 0 | &ct_len) |
1160 | 0 | || ct_len > ENCRYPTED_TOKEN_MAX_LEN |
1161 | 0 | || !encrypt_validation_token(port, buffer, token_buf_len, ct_buf, |
1162 | 0 | &ct_len) |
1163 | 0 | || !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN)) |
1164 | 0 | goto err; |
1165 | | |
1166 | 0 | hdr.dst_conn_id = client_hdr->src_conn_id; |
1167 | 0 | hdr.type = QUIC_PKT_TYPE_RETRY; |
1168 | 0 | hdr.fixed = 1; |
1169 | 0 | hdr.version = 1; |
1170 | 0 | hdr.len = ct_len; |
1171 | 0 | hdr.data = ct_buf; |
1172 | 0 | ok = ossl_quic_calculate_retry_integrity_tag(port->engine->libctx, |
1173 | 0 | port->engine->propq, &hdr, |
1174 | 0 | &client_hdr->dst_conn_id, |
1175 | 0 | ct_buf + ct_len |
1176 | 0 | - QUIC_RETRY_INTEGRITY_TAG_LEN); |
1177 | 0 | if (ok == 0) |
1178 | 0 | goto err; |
1179 | | |
1180 | 0 | hdr.token = hdr.data; |
1181 | 0 | hdr.token_len = hdr.len; |
1182 | |
|
1183 | 0 | msg[0].data = buffer; |
1184 | 0 | msg[0].peer = peer; |
1185 | 0 | msg[0].local = NULL; |
1186 | 0 | msg[0].flags = 0; |
1187 | |
|
1188 | 0 | ok = WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0); |
1189 | 0 | if (ok == 0) |
1190 | 0 | goto err; |
1191 | | |
1192 | 0 | ok = ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len, |
1193 | 0 | &hdr, NULL); |
1194 | 0 | if (ok == 0) |
1195 | 0 | goto err; |
1196 | | |
1197 | 0 | ok = WPACKET_get_total_written(&wpkt, &msg[0].data_len); |
1198 | 0 | if (ok == 0) |
1199 | 0 | goto err; |
1200 | | |
1201 | 0 | ok = WPACKET_finish(&wpkt); |
1202 | 0 | if (ok == 0) |
1203 | 0 | goto err; |
1204 | | |
1205 | | /* |
1206 | | * TODO(QUIC FUTURE) need to retry this in the event it return EAGAIN |
1207 | | * on a non-blocking BIO |
1208 | | */ |
1209 | 0 | if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written)) |
1210 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR, |
1211 | 0 | "port retry send failed due to network BIO I/O error"); |
1212 | |
|
1213 | 0 | err: |
1214 | 0 | cleanup_validation_token(&token); |
1215 | 0 | } |
1216 | | |
1217 | | /** |
1218 | | * @brief Sends a QUIC Version Negotiation packet to the specified peer. |
1219 | | * |
1220 | | * This function constructs and sends a Version Negotiation packet using |
1221 | | * the connection IDs from the client's initial packet header. The |
1222 | | * Version Negotiation packet indicates support for QUIC version 1. |
1223 | | * |
1224 | | * @param port Pointer to the QUIC_PORT structure representing the port |
1225 | | * context used for network communication. |
1226 | | * @param peer Pointer to the BIO_ADDR structure specifying the address |
1227 | | * of the peer to which the Version Negotiation packet |
1228 | | * will be sent. |
1229 | | * @param client_hdr Pointer to the QUIC_PKT_HDR structure containing the |
1230 | | * client's packet header used to extract connection IDs. |
1231 | | * |
1232 | | * @note The function will raise an error if sending the message fails. |
1233 | | */ |
1234 | | static void port_send_version_negotiation(QUIC_PORT *port, BIO_ADDR *peer, |
1235 | | QUIC_PKT_HDR *client_hdr) |
1236 | 0 | { |
1237 | 0 | BIO_MSG msg[1]; |
1238 | 0 | unsigned char buffer[1024]; |
1239 | 0 | QUIC_PKT_HDR hdr; |
1240 | 0 | WPACKET wpkt; |
1241 | 0 | uint32_t supported_versions[1]; |
1242 | 0 | size_t written; |
1243 | 0 | size_t i; |
1244 | |
|
1245 | 0 | memset(&hdr, 0, sizeof(QUIC_PKT_HDR)); |
1246 | | /* |
1247 | | * Reverse the source and dst conn ids |
1248 | | */ |
1249 | 0 | hdr.dst_conn_id = client_hdr->src_conn_id; |
1250 | 0 | hdr.src_conn_id = client_hdr->dst_conn_id; |
1251 | | |
1252 | | /* |
1253 | | * This is our list of supported protocol versions |
1254 | | * Currently only QUIC_VERSION_1 |
1255 | | */ |
1256 | 0 | supported_versions[0] = QUIC_VERSION_1; |
1257 | | |
1258 | | /* |
1259 | | * Fill out the header fields |
1260 | | * Note: Version negotiation packets, must, unlike |
1261 | | * other packet types have a version of 0 |
1262 | | */ |
1263 | 0 | hdr.type = QUIC_PKT_TYPE_VERSION_NEG; |
1264 | 0 | hdr.version = 0; |
1265 | 0 | hdr.token = 0; |
1266 | 0 | hdr.token_len = 0; |
1267 | 0 | hdr.len = sizeof(supported_versions); |
1268 | 0 | hdr.data = (unsigned char *)supported_versions; |
1269 | |
|
1270 | 0 | msg[0].data = buffer; |
1271 | 0 | msg[0].peer = peer; |
1272 | 0 | msg[0].local = NULL; |
1273 | 0 | msg[0].flags = 0; |
1274 | |
|
1275 | 0 | if (!WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0)) |
1276 | 0 | return; |
1277 | | |
1278 | 0 | if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len, |
1279 | 0 | &hdr, NULL)) |
1280 | 0 | return; |
1281 | | |
1282 | | /* |
1283 | | * Add the array of supported versions to the end of the packet |
1284 | | */ |
1285 | 0 | for (i = 0; i < OSSL_NELEM(supported_versions); i++) { |
1286 | 0 | if (!WPACKET_put_bytes_u32(&wpkt, supported_versions[i])) |
1287 | 0 | return; |
1288 | 0 | } |
1289 | | |
1290 | 0 | if (!WPACKET_get_total_written(&wpkt, &msg[0].data_len)) |
1291 | 0 | return; |
1292 | | |
1293 | 0 | if (!WPACKET_finish(&wpkt)) |
1294 | 0 | return; |
1295 | | |
1296 | | /* |
1297 | | * Send it back to the client attempting to connect |
1298 | | * TODO(QUIC FUTURE): Need to handle the EAGAIN case here, if the |
1299 | | * BIO_sendmmsg call falls in a retryable manner |
1300 | | */ |
1301 | 0 | if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written)) |
1302 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR, |
1303 | 0 | "port version negotiation send failed"); |
1304 | 0 | } |
1305 | | |
1306 | | /** |
1307 | | * @brief definitions of token lifetimes |
1308 | | * |
1309 | | * RETRY tokens are only valid for 10 seconds |
1310 | | * NEW_TOKEN tokens have a lifetime of 3600 sec (1 hour) |
1311 | | */ |
1312 | | |
1313 | 0 | #define RETRY_LIFETIME 10 |
1314 | 0 | #define NEW_TOKEN_LIFETIME 3600 |
1315 | | /** |
1316 | | * @brief Validates a received token in a QUIC packet header. |
1317 | | * |
1318 | | * This function checks the validity of a token contained in the provided |
1319 | | * QUIC packet header (`QUIC_PKT_HDR *hdr`). The validation process involves |
1320 | | * verifying that the token matches an expected format and value. If the |
1321 | | * token is from a RETRY packet, the function extracts the original connection |
1322 | | * ID (ODCID)/original source connection ID (SCID) and stores it in the provided |
1323 | | * parameters. If the token is from a NEW_TOKEN packet, the values will be |
1324 | | * derived instead. |
1325 | | * |
1326 | | * @param hdr Pointer to the QUIC packet header containing the token. |
1327 | | * @param port Pointer to the QUIC port from which to send the packet. |
1328 | | * @param peer Address of the client peer receiving the packet. |
1329 | | * @param odcid Pointer to the connection ID structure to store the ODCID if the |
1330 | | * token is valid. |
1331 | | * @param scid Pointer to the connection ID structure to store the SCID if the |
1332 | | * token is valid. |
1333 | | * |
1334 | | * @return 1 if the token is valid and ODCID/SCID are successfully set. |
1335 | | * 0 otherwise. |
1336 | | * |
1337 | | * The function performs the following checks: |
1338 | | * - Token length meets the required minimum. |
1339 | | * - Buffer matches expected format. |
1340 | | * - Peer address matches previous connection address. |
1341 | | * - Token has not expired. Currently set to 10 seconds for tokens from RETRY |
1342 | | * packets and 60 minutes for tokens from NEW_TOKEN packets. This may be |
1343 | | * configurable in the future. |
1344 | | */ |
1345 | | static int port_validate_token(QUIC_PKT_HDR *hdr, QUIC_PORT *port, |
1346 | | BIO_ADDR *peer, QUIC_CONN_ID *odcid, uint8_t *gen_new_token) |
1347 | 0 | { |
1348 | 0 | int ret = 0; |
1349 | 0 | QUIC_VALIDATION_TOKEN token = { 0 }; |
1350 | 0 | uint64_t time_diff; |
1351 | 0 | size_t remote_addr_len, dec_token_len; |
1352 | 0 | unsigned char *remote_addr = NULL, dec_token[MARSHALLED_TOKEN_MAX_LEN]; |
1353 | 0 | OSSL_TIME now = ossl_time_now(); |
1354 | |
|
1355 | 0 | *gen_new_token = 0; |
1356 | |
|
1357 | 0 | if (!decrypt_validation_token(port, hdr->token, hdr->token_len, NULL, |
1358 | 0 | &dec_token_len) |
1359 | 0 | || dec_token_len > MARSHALLED_TOKEN_MAX_LEN |
1360 | 0 | || !decrypt_validation_token(port, hdr->token, hdr->token_len, |
1361 | 0 | dec_token, &dec_token_len) |
1362 | 0 | || !parse_validation_token(&token, dec_token, dec_token_len)) |
1363 | 0 | goto err; |
1364 | | |
1365 | | /* |
1366 | | * Validate token timestamp. Current time should not be before the token |
1367 | | * timestamp. |
1368 | | */ |
1369 | 0 | if (ossl_time_compare(now, token.timestamp) < 0) |
1370 | 0 | goto err; |
1371 | 0 | time_diff = ossl_time2seconds(ossl_time_abs_difference(token.timestamp, |
1372 | 0 | now)); |
1373 | 0 | if ((token.is_retry && time_diff > RETRY_LIFETIME) |
1374 | 0 | || (!token.is_retry && time_diff > NEW_TOKEN_LIFETIME)) |
1375 | 0 | goto err; |
1376 | | |
1377 | | /* Validate remote address */ |
1378 | 0 | if (!BIO_ADDR_rawaddress(peer, NULL, &remote_addr_len) |
1379 | 0 | || remote_addr_len != token.remote_addr_len |
1380 | 0 | || (remote_addr = OPENSSL_malloc(remote_addr_len)) == NULL |
1381 | 0 | || !BIO_ADDR_rawaddress(peer, remote_addr, &remote_addr_len) |
1382 | 0 | || memcmp(remote_addr, token.remote_addr, remote_addr_len) != 0) |
1383 | 0 | goto err; |
1384 | | |
1385 | | /* |
1386 | | * Set ODCID and SCID. If the token is from a RETRY packet, retrieve both |
1387 | | * from the token. Otherwise, generate a new ODCID and use the header's |
1388 | | * source connection ID for SCID. |
1389 | | */ |
1390 | 0 | if (token.is_retry) { |
1391 | | /* |
1392 | | * We're parsing a packet header before its gone through AEAD validation |
1393 | | * here, so there is a chance we are dealing with corrupted data. Make |
1394 | | * Sure the dcid encoded in the token matches the headers dcid to |
1395 | | * mitigate that. |
1396 | | * TODO(QUIC FUTURE): Consider handling AEAD validation at the port |
1397 | | * level rather than the QRX/channel level to eliminate the need for |
1398 | | * this. |
1399 | | */ |
1400 | 0 | if (token.rscid.id_len != hdr->dst_conn_id.id_len |
1401 | 0 | || memcmp(&token.rscid.id, &hdr->dst_conn_id.id, |
1402 | 0 | token.rscid.id_len) |
1403 | 0 | != 0) |
1404 | 0 | goto err; |
1405 | 0 | *odcid = token.odcid; |
1406 | 0 | } else { |
1407 | 0 | if (!ossl_quic_lcidm_get_unused_cid(port->lcidm, odcid)) |
1408 | 0 | goto err; |
1409 | 0 | } |
1410 | | |
1411 | | /* |
1412 | | * Determine if we need to send a NEW_TOKEN frame |
1413 | | * If we validated a retry token, we should always |
1414 | | * send a NEW_TOKEN frame to the client |
1415 | | * |
1416 | | * If however, we validated a NEW_TOKEN, which may be |
1417 | | * reused multiple times, only send a NEW_TOKEN frame |
1418 | | * if the existing received token has less than 10% of its lifetime |
1419 | | * remaining. This prevents us from constantly sending |
1420 | | * NEW_TOKEN frames on every connection when not needed |
1421 | | */ |
1422 | 0 | if (token.is_retry) { |
1423 | 0 | *gen_new_token = 1; |
1424 | 0 | } else { |
1425 | 0 | if (time_diff > ((NEW_TOKEN_LIFETIME * 9) / 10)) |
1426 | 0 | *gen_new_token = 1; |
1427 | 0 | } |
1428 | |
|
1429 | 0 | ret = 1; |
1430 | 0 | err: |
1431 | 0 | cleanup_validation_token(&token); |
1432 | 0 | OPENSSL_free(remote_addr); |
1433 | 0 | return ret; |
1434 | 0 | } |
1435 | | |
1436 | | static void generate_new_token(QUIC_CHANNEL *ch, BIO_ADDR *peer) |
1437 | 0 | { |
1438 | 0 | QUIC_CONN_ID rscid = { 0 }; |
1439 | 0 | QUIC_VALIDATION_TOKEN token; |
1440 | 0 | unsigned char buffer[ENCRYPTED_TOKEN_MAX_LEN]; |
1441 | 0 | unsigned char *ct_buf; |
1442 | 0 | size_t ct_len; |
1443 | 0 | size_t token_buf_len = 0; |
1444 | | |
1445 | | /* Clients never send a NEW_TOKEN */ |
1446 | 0 | if (!ch->is_server) |
1447 | 0 | return; |
1448 | | |
1449 | 0 | ct_buf = OPENSSL_zalloc(ENCRYPTED_TOKEN_MAX_LEN); |
1450 | 0 | if (ct_buf == NULL) |
1451 | 0 | return; |
1452 | | |
1453 | | /* |
1454 | | * NEW_TOKEN tokens may be used for multiple subsequent connections |
1455 | | * within their timeout period, so don't reserve an rscid here |
1456 | | * like we do for retry tokens, instead, just fill it with random |
1457 | | * data, as we won't use it anyway |
1458 | | */ |
1459 | 0 | rscid.id_len = 8; |
1460 | 0 | if (!RAND_bytes_ex(ch->port->engine->libctx, rscid.id, 8, 0)) { |
1461 | 0 | OPENSSL_free(ct_buf); |
1462 | 0 | return; |
1463 | 0 | } |
1464 | | |
1465 | 0 | memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN)); |
1466 | |
|
1467 | 0 | if (!generate_token(peer, ch->init_dcid, rscid, &token, 0) |
1468 | 0 | || !marshal_validation_token(&token, buffer, &token_buf_len) |
1469 | 0 | || !encrypt_validation_token(ch->port, buffer, token_buf_len, NULL, |
1470 | 0 | &ct_len) |
1471 | 0 | || ct_len > ENCRYPTED_TOKEN_MAX_LEN |
1472 | 0 | || !encrypt_validation_token(ch->port, buffer, token_buf_len, ct_buf, |
1473 | 0 | &ct_len) |
1474 | 0 | || !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN)) { |
1475 | 0 | OPENSSL_free(ct_buf); |
1476 | 0 | cleanup_validation_token(&token); |
1477 | 0 | return; |
1478 | 0 | } |
1479 | | |
1480 | 0 | ch->pending_new_token = ct_buf; |
1481 | 0 | ch->pending_new_token_len = ct_len; |
1482 | |
|
1483 | 0 | cleanup_validation_token(&token); |
1484 | 0 | } |
1485 | | |
1486 | | /* |
1487 | | * This is called by the demux when we get a packet not destined for any known |
1488 | | * DCID. |
1489 | | */ |
1490 | | static void port_default_packet_handler(QUIC_URXE *e, void *arg, |
1491 | | const QUIC_CONN_ID *dcid) |
1492 | 5.64M | { |
1493 | 5.64M | QUIC_PORT *port = arg; |
1494 | 5.64M | PACKET pkt; |
1495 | 5.64M | QUIC_PKT_HDR hdr; |
1496 | 5.64M | QUIC_CHANNEL *ch = NULL, *new_ch = NULL; |
1497 | 5.64M | QUIC_CONN_ID odcid; |
1498 | 5.64M | uint8_t gen_new_token = 0; |
1499 | 5.64M | OSSL_QRX *qrx = NULL, *qrx_ref; |
1500 | 5.64M | OSSL_QRX *qrx_src = NULL; |
1501 | 5.64M | OSSL_QRX_ARGS qrx_args = { 0 }; |
1502 | 5.64M | uint64_t cause_flags = 0; |
1503 | 5.64M | OSSL_QRX_PKT *qrx_pkt = NULL; |
1504 | | |
1505 | | /* Don't handle anything if we are no longer running. */ |
1506 | 5.64M | if (!ossl_quic_port_is_running(port)) |
1507 | 0 | goto undesirable; |
1508 | | |
1509 | 5.64M | if (port_try_handle_stateless_reset(port, e)) |
1510 | 22 | goto undesirable; |
1511 | | |
1512 | 5.64M | if (dcid != NULL |
1513 | 1.95M | && ossl_quic_lcidm_lookup(port->lcidm, dcid, NULL, |
1514 | 1.95M | (void **)&ch)) { |
1515 | 1.94M | assert(ch != NULL); |
1516 | 1.94M | ossl_quic_channel_inject(ch, e); |
1517 | 1.94M | return; |
1518 | 1.94M | } |
1519 | | |
1520 | | /* |
1521 | | * If we have an incoming packet which doesn't match any existing connection |
1522 | | * we assume this is an attempt to make a new connection. |
1523 | | */ |
1524 | 3.70M | if (!port->allow_incoming) |
1525 | 3.70M | goto undesirable; |
1526 | | |
1527 | | /* |
1528 | | * packet without destination connection id is invalid/corrupted here. |
1529 | | * stop wasting CPU cycles now. |
1530 | | */ |
1531 | 0 | if (dcid == NULL) |
1532 | 0 | goto undesirable; |
1533 | | |
1534 | | /* |
1535 | | * We have got a packet for an unknown DCID. This might be an attempt to |
1536 | | * open a new connection. |
1537 | | */ |
1538 | 0 | if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN) |
1539 | 0 | goto undesirable; |
1540 | | |
1541 | 0 | if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len)) |
1542 | 0 | goto undesirable; |
1543 | | |
1544 | | /* |
1545 | | * We set short_conn_id_len to SIZE_MAX here which will cause the decode |
1546 | | * operation to fail if we get a 1-RTT packet. This is fine since we only |
1547 | | * care about Initial packets. |
1548 | | */ |
1549 | 0 | if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL, |
1550 | 0 | &cause_flags)) { |
1551 | | /* |
1552 | | * If we fail due to a bad version, we know the packet up to the version |
1553 | | * number was decoded, and we use it below to send a version |
1554 | | * negotiation packet |
1555 | | */ |
1556 | 0 | if ((cause_flags & QUIC_PKT_HDR_DECODE_BAD_VERSION) == 0) |
1557 | 0 | goto undesirable; |
1558 | 0 | } |
1559 | | |
1560 | 0 | switch (hdr.version) { |
1561 | 0 | case QUIC_VERSION_1: |
1562 | 0 | break; |
1563 | | |
1564 | 0 | case QUIC_VERSION_NONE: |
1565 | 0 | default: |
1566 | | |
1567 | | /* |
1568 | | * If we get here, then we have a bogus version, and might need |
1569 | | * to send a version negotiation packet. According to |
1570 | | * RFC 9000 s. 6 and 14.1, we only do so however, if the UDP datagram |
1571 | | * is a minimum of 1200 bytes in size |
1572 | | */ |
1573 | 0 | if (e->data_len < 1200) |
1574 | 0 | goto undesirable; |
1575 | | |
1576 | | /* |
1577 | | * If we don't get a supported version, respond with a ver |
1578 | | * negotiation packet, and discard |
1579 | | * TODO(QUIC FUTURE): Rate limit the reception of these |
1580 | | */ |
1581 | 0 | port_send_version_negotiation(port, &e->peer, &hdr); |
1582 | 0 | goto undesirable; |
1583 | 0 | } |
1584 | | |
1585 | | /* |
1586 | | * We only care about Initial packets which might be trying to establish a |
1587 | | * connection. |
1588 | | */ |
1589 | 0 | if (hdr.type != QUIC_PKT_TYPE_INITIAL) |
1590 | 0 | goto undesirable; |
1591 | | |
1592 | 0 | if (port->max_pending_channels > 0 && ossl_list_incoming_ch_num(&port->incoming_channel_list) >= port->max_pending_channels) |
1593 | 0 | goto undesirable; |
1594 | | |
1595 | 0 | odcid.id_len = 0; |
1596 | | |
1597 | | /* |
1598 | | * Create qrx now so we can check integrity of packet |
1599 | | * which does not belong to any channel. |
1600 | | */ |
1601 | 0 | qrx_args.libctx = port->engine->libctx; |
1602 | 0 | qrx_args.demux = port->demux; |
1603 | 0 | qrx_args.short_conn_id_len = dcid->id_len; |
1604 | 0 | qrx_args.max_deferred = 32; |
1605 | 0 | qrx = ossl_qrx_new(&qrx_args); |
1606 | 0 | if (qrx == NULL) |
1607 | 0 | goto undesirable; |
1608 | | |
1609 | | /* |
1610 | | * Derive secrets for qrx only. |
1611 | | */ |
1612 | 0 | if (!ossl_quic_provide_initial_secret(port->engine->libctx, |
1613 | 0 | port->engine->propq, |
1614 | 0 | &hdr.dst_conn_id, |
1615 | 0 | /* is_server */ 1, |
1616 | 0 | qrx, NULL)) |
1617 | 0 | goto undesirable; |
1618 | | |
1619 | 0 | if (ossl_qrx_validate_initial_packet(qrx, e, (const QUIC_CONN_ID *)dcid) == 0) |
1620 | 0 | goto undesirable; |
1621 | | |
1622 | 0 | if (port->validate_addr == 0) { |
1623 | | /* |
1624 | | * Forget qrx, because it becomes (almost) useless here. We must let |
1625 | | * channel to create a new QRX for connection ID server chooses. The |
1626 | | * validation keys for new DCID will be derived by |
1627 | | * ossl_quic_channel_on_new_conn() when we will be creating channel. |
1628 | | * See RFC 9000 section 7.2 negotiating connection id to better |
1629 | | * understand what's going on here. |
1630 | | * |
1631 | | * Did we say qrx is almost useless? Why? Because qrx remembers packets |
1632 | | * we just validated. Those packets must be injected to channel we are |
1633 | | * going to create. We use qrx_src alias so we can read packets from |
1634 | | * qrx and inject them to channel. |
1635 | | */ |
1636 | 0 | qrx_src = qrx; |
1637 | 0 | qrx = NULL; |
1638 | 0 | } |
1639 | | /* |
1640 | | * TODO(QUIC FUTURE): there should be some logic similar to accounting half-open |
1641 | | * states in TCP. If we reach certain threshold, then we want to |
1642 | | * validate clients. |
1643 | | */ |
1644 | 0 | if (port->validate_addr == 1 && hdr.token == NULL) { |
1645 | 0 | port_send_retry(port, &e->peer, &hdr); |
1646 | 0 | goto undesirable; |
1647 | 0 | } |
1648 | | |
1649 | | /* |
1650 | | * Note, even if we don't enforce the sending of retry frames for |
1651 | | * server address validation, we may still get a token if we sent |
1652 | | * a NEW_TOKEN frame during a prior connection, which we should still |
1653 | | * validate here |
1654 | | */ |
1655 | 0 | if (hdr.token != NULL |
1656 | 0 | && port_validate_token(&hdr, port, &e->peer, |
1657 | 0 | &odcid, &gen_new_token) |
1658 | 0 | == 0) { |
1659 | | /* |
1660 | | * RFC 9000 s 8.1.3 |
1661 | | * When a server receives an Initial packet with an address |
1662 | | * validation token, it MUST attempt to validate the token, |
1663 | | * unless it has already completed address validation. |
1664 | | * If the token is invalid, then the server SHOULD proceed as |
1665 | | * if the client did not have a validated address, |
1666 | | * including potentially sending a Retry packet |
1667 | | * Note: If address validation is disabled, just act like |
1668 | | * the request is valid |
1669 | | */ |
1670 | 0 | if (port->validate_addr == 1) { |
1671 | | /* |
1672 | | * Again: we should consider saving initial encryption level |
1673 | | * secrets to token here to save some CPU cycles. |
1674 | | */ |
1675 | 0 | port_send_retry(port, &e->peer, &hdr); |
1676 | 0 | goto undesirable; |
1677 | 0 | } |
1678 | | |
1679 | | /* |
1680 | | * client is under amplification limit, until it completes |
1681 | | * handshake. |
1682 | | * |
1683 | | * forget qrx so channel can create a new one |
1684 | | * with valid initial encryption level keys. |
1685 | | */ |
1686 | 0 | if (qrx != NULL) { |
1687 | 0 | qrx_src = qrx; |
1688 | 0 | qrx = NULL; |
1689 | 0 | } |
1690 | 0 | } |
1691 | | |
1692 | 0 | qrx_ref = NULL; |
1693 | 0 | if (qrx != NULL) { |
1694 | | /* |
1695 | | * if we are here, then client is validated via retry packet |
1696 | | * (client sent a valid token). In this case the qrx has valid |
1697 | | * secrets set for QUIC initial level encryption. We can pass |
1698 | | * reference to qrx to newly created channel. |
1699 | | * |
1700 | | * Note: port_bind_channel()/channel becomes owner of qrx_ref. |
1701 | | */ |
1702 | 0 | qrx_ref = ossl_qrx_newref(qrx); |
1703 | 0 | if (qrx_ref == NULL) |
1704 | 0 | goto undesirable; |
1705 | 0 | } |
1706 | 0 | port_bind_channel(port, &e->peer, &hdr.dst_conn_id, |
1707 | 0 | &odcid, qrx_ref, &new_ch); |
1708 | | |
1709 | | /* |
1710 | | * if packet validates it gets moved to channel, we've just bound |
1711 | | * to port. |
1712 | | */ |
1713 | 0 | if (new_ch == NULL) |
1714 | 0 | goto undesirable; |
1715 | | |
1716 | | /* |
1717 | | * Generate a token for sending in a later NEW_TOKEN frame |
1718 | | */ |
1719 | 0 | if (gen_new_token == 1) |
1720 | 0 | generate_new_token(new_ch, &e->peer); |
1721 | |
|
1722 | 0 | if (qrx_src != NULL) { |
1723 | | /* |
1724 | | * Time to reinject packets from qrx to channel before |
1725 | | * qrx will be destroyed here. |
1726 | | */ |
1727 | 0 | while (ossl_qrx_read_pkt(qrx_src, &qrx_pkt) == 1) |
1728 | 0 | ossl_quic_channel_inject_pkt(new_ch, qrx_pkt); |
1729 | 0 | ossl_qrx_update_pn_space(qrx_src, new_ch->qrx); |
1730 | | /* |
1731 | | * transfer ownership back to qrx; |
1732 | | */ |
1733 | 0 | qrx = qrx_src; |
1734 | 0 | qrx_src = NULL; |
1735 | 0 | } |
1736 | | |
1737 | | /* |
1738 | | * If function reaches this place, then packet got validated in |
1739 | | * ossl_qrx_validate_initial_packet(). Keep in mind the function |
1740 | | * ossl_qrx_validate_initial_packet() decrypts the packet to validate it. |
1741 | | * If packet validation was successful (and it was because we are here), |
1742 | | * then the function puts the packet to qrx->rx_pending. We must not call |
1743 | | * ossl_qrx_inject_urxe() here now, because we don't want to insert |
1744 | | * the packet to qrx->urx_pending which keeps packet waiting for decryption. |
1745 | | * |
1746 | | * We are going to call ossl_quic_demux_release_urxe() to dispose buffer |
1747 | | * which still holds encrypted data. |
1748 | | */ |
1749 | |
|
1750 | 3.70M | undesirable: |
1751 | 3.70M | ossl_qrx_free(qrx); /* releases reference */ |
1752 | 3.70M | ossl_qrx_free(qrx_src); |
1753 | 3.70M | ossl_quic_demux_release_urxe(port->demux, e); |
1754 | 3.70M | } |
1755 | | |
1756 | | void ossl_quic_port_raise_net_error(QUIC_PORT *port, |
1757 | | QUIC_CHANNEL *triggering_ch) |
1758 | 0 | { |
1759 | 0 | QUIC_CHANNEL *ch; |
1760 | |
|
1761 | 0 | if (!ossl_quic_port_is_running(port)) |
1762 | 0 | return; |
1763 | | |
1764 | | /* |
1765 | | * Immediately capture any triggering error on the error stack, with a |
1766 | | * cover error. |
1767 | | */ |
1768 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR, |
1769 | 0 | "port failed due to network BIO I/O error"); |
1770 | 0 | OSSL_ERR_STATE_save(port->err_state); |
1771 | |
|
1772 | 0 | port_transition_failed(port); |
1773 | | |
1774 | | /* Give the triggering channel (if any) the first notification. */ |
1775 | 0 | if (triggering_ch != NULL) |
1776 | 0 | ossl_quic_channel_raise_net_error(triggering_ch); |
1777 | |
|
1778 | 0 | OSSL_LIST_FOREACH(ch, ch, &port->channel_list) |
1779 | 0 | if (ch != triggering_ch) |
1780 | 0 | ossl_quic_channel_raise_net_error(ch); |
1781 | 0 | } |
1782 | | |
1783 | | void ossl_quic_port_restore_err_state(const QUIC_PORT *port) |
1784 | 0 | { |
1785 | 0 | ERR_clear_error(); |
1786 | 0 | OSSL_ERR_STATE_restore(port->err_state); |
1787 | 0 | } |
1788 | | |
1789 | | uint64_t ossl_quic_port_get_max_pending_channels(const QUIC_PORT *port) |
1790 | 0 | { |
1791 | 0 | return port->max_pending_channels; |
1792 | 0 | } |
1793 | | |
1794 | | void ossl_quic_port_set_max_pending_channels(QUIC_PORT *port, uint64_t max_pending_channels) |
1795 | 0 | { |
1796 | 0 | port->max_pending_channels = max_pending_channels; |
1797 | 0 | } |