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