/src/openssl/ssl/quic/quic_port.c
Line  | Count  | Source  | 
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  | 0  | #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  | 0  | DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL); Unexecuted instantiation: quic_port.c:ossl_list_ch_head Unexecuted instantiation: quic_port.c:ossl_list_ch_next  | 
97  | 0  | DEFINE_LIST_OF_IMPL(incoming_ch, QUIC_CHANNEL); Unexecuted instantiation: quic_port.c:ossl_list_incoming_ch_insert_tail Unexecuted instantiation: quic_port.c:ossl_list_incoming_ch_head Unexecuted instantiation: quic_port.c:ossl_list_incoming_ch_remove  | 
98  | 0  | DEFINE_LIST_OF_IMPL(port, QUIC_PORT); Unexecuted instantiation: quic_port.c:ossl_list_port_insert_tail Unexecuted instantiation: quic_port.c:ossl_list_port_remove  | 
99  |  |  | 
100  |  | QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args)  | 
101  | 0  | { | 
102  | 0  |     QUIC_PORT *port;  | 
103  |  | 
  | 
104  | 0  |     if ((port = OPENSSL_zalloc(sizeof(QUIC_PORT))) == NULL)  | 
105  | 0  |         return NULL;  | 
106  |  |  | 
107  | 0  |     port->engine        = args->engine;  | 
108  | 0  |     port->channel_ctx   = args->channel_ctx;  | 
109  | 0  |     port->is_multi_conn = args->is_multi_conn;  | 
110  | 0  |     port->validate_addr = args->do_addr_validation;  | 
111  | 0  |     port->get_conn_user_ssl = args->get_conn_user_ssl;  | 
112  | 0  |     port->user_ssl_arg = args->user_ssl_arg;  | 
113  |  | 
  | 
114  | 0  |     if (!port_init(port)) { | 
115  | 0  |         OPENSSL_free(port);  | 
116  | 0  |         return NULL;  | 
117  | 0  |     }  | 
118  |  |  | 
119  | 0  |     return port;  | 
120  | 0  | }  | 
121  |  |  | 
122  |  | void ossl_quic_port_free(QUIC_PORT *port)  | 
123  | 0  | { | 
124  | 0  |     if (port == NULL)  | 
125  | 0  |         return;  | 
126  |  |  | 
127  | 0  |     port_cleanup(port);  | 
128  | 0  |     OPENSSL_free(port);  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static int port_init(QUIC_PORT *port)  | 
132  | 0  | { | 
133  | 0  |     size_t rx_short_dcid_len = (port->is_multi_conn ? INIT_DCID_LEN : 0);  | 
134  | 0  |     int key_len = -1;  | 
135  | 0  |     EVP_CIPHER *cipher = NULL;  | 
136  | 0  |     unsigned char *token_key = NULL;  | 
137  | 0  |     int ret = 0;  | 
138  |  | 
  | 
139  | 0  |     if (port->engine == NULL || port->channel_ctx == NULL)  | 
140  | 0  |         goto err;  | 
141  |  |  | 
142  | 0  |     if ((port->err_state = OSSL_ERR_STATE_new()) == NULL)  | 
143  | 0  |         goto err;  | 
144  |  |  | 
145  | 0  |     if ((port->demux = ossl_quic_demux_new(/*BIO=*/NULL,  | 
146  | 0  |                                            /*Short CID Len=*/rx_short_dcid_len,  | 
147  | 0  |                                            get_time, port)) == NULL)  | 
148  | 0  |         goto err;  | 
149  |  |  | 
150  | 0  |     ossl_quic_demux_set_default_handler(port->demux,  | 
151  | 0  |                                         port_default_packet_handler,  | 
152  | 0  |                                         port);  | 
153  |  | 
  | 
154  | 0  |     if ((port->srtm = ossl_quic_srtm_new(port->engine->libctx,  | 
155  | 0  |                                          port->engine->propq)) == NULL)  | 
156  | 0  |         goto err;  | 
157  |  |  | 
158  | 0  |     if ((port->lcidm = ossl_quic_lcidm_new(port->engine->libctx,  | 
159  | 0  |                                            rx_short_dcid_len)) == NULL)  | 
160  | 0  |         goto err;  | 
161  |  |  | 
162  | 0  |     port->rx_short_dcid_len = (unsigned char)rx_short_dcid_len;  | 
163  | 0  |     port->tx_init_dcid_len  = INIT_DCID_LEN;  | 
164  | 0  |     port->state             = QUIC_PORT_STATE_RUNNING;  | 
165  |  | 
  | 
166  | 0  |     ossl_list_port_insert_tail(&port->engine->port_list, port);  | 
167  | 0  |     port->on_engine_list    = 1;  | 
168  | 0  |     port->bio_changed       = 1;  | 
169  |  |  | 
170  |  |     /* Generate random key for token encryption */  | 
171  | 0  |     if ((port->token_ctx = EVP_CIPHER_CTX_new()) == NULL  | 
172  | 0  |         || (cipher = EVP_CIPHER_fetch(port->engine->libctx,  | 
173  | 0  |                                       "AES-256-GCM", NULL)) == NULL  | 
174  | 0  |         || !EVP_EncryptInit_ex(port->token_ctx, cipher, NULL, NULL, NULL)  | 
175  | 0  |         || (key_len = EVP_CIPHER_CTX_get_key_length(port->token_ctx)) <= 0  | 
176  | 0  |         || (token_key = OPENSSL_malloc(key_len)) == NULL  | 
177  | 0  |         || !RAND_priv_bytes_ex(port->engine->libctx, token_key, key_len, 0)  | 
178  | 0  |         || !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, token_key, NULL))  | 
179  | 0  |         goto err;  | 
180  |  |  | 
181  | 0  |     ret = 1;  | 
182  | 0  | err:  | 
183  | 0  |     EVP_CIPHER_free(cipher);  | 
184  | 0  |     if (key_len >= 1)  | 
185  | 0  |         OPENSSL_clear_free(token_key, key_len);  | 
186  | 0  |     else  | 
187  | 0  |         OPENSSL_free(token_key);  | 
188  | 0  |     if (!ret)  | 
189  | 0  |         port_cleanup(port);  | 
190  | 0  |     return ret;  | 
191  | 0  | }  | 
192  |  |  | 
193  |  | static void port_cleanup(QUIC_PORT *port)  | 
194  | 0  | { | 
195  | 0  |     assert(ossl_list_ch_num(&port->channel_list) == 0);  | 
196  |  | 
  | 
197  | 0  |     ossl_quic_demux_free(port->demux);  | 
198  | 0  |     port->demux = NULL;  | 
199  |  | 
  | 
200  | 0  |     ossl_quic_srtm_free(port->srtm);  | 
201  | 0  |     port->srtm = NULL;  | 
202  |  | 
  | 
203  | 0  |     ossl_quic_lcidm_free(port->lcidm);  | 
204  | 0  |     port->lcidm = NULL;  | 
205  |  | 
  | 
206  | 0  |     OSSL_ERR_STATE_free(port->err_state);  | 
207  | 0  |     port->err_state = NULL;  | 
208  |  | 
  | 
209  | 0  |     if (port->on_engine_list) { | 
210  | 0  |         ossl_list_port_remove(&port->engine->port_list, port);  | 
211  | 0  |         port->on_engine_list = 0;  | 
212  | 0  |     }  | 
213  |  | 
  | 
214  | 0  |     EVP_CIPHER_CTX_free(port->token_ctx);  | 
215  | 0  |     port->token_ctx = NULL;  | 
216  | 0  | }  | 
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  | 0  | { | 
228  | 0  |     return port->state == QUIC_PORT_STATE_RUNNING;  | 
229  | 0  | }  | 
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  | 0  | { | 
238  | 0  |     return ossl_quic_engine_get0_reactor(port->engine);  | 
239  | 0  | }  | 
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  | 0  | { | 
253  | 0  |     return ossl_quic_engine_get_time(port->engine);  | 
254  | 0  | }  | 
255  |  |  | 
256  |  | static OSSL_TIME get_time(void *port)  | 
257  | 0  | { | 
258  | 0  |     return ossl_quic_port_get_time((QUIC_PORT *)port);  | 
259  | 0  | }  | 
260  |  |  | 
261  |  | int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT *port)  | 
262  | 0  | { | 
263  | 0  |     return port->rx_short_dcid_len;  | 
264  | 0  | }  | 
265  |  |  | 
266  |  | int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT *port)  | 
267  | 0  | { | 
268  | 0  |     return port->tx_init_dcid_len;  | 
269  | 0  | }  | 
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  | 0  | { | 
284  | 0  |     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  | 0  |     return 1;  | 
290  | 0  | }  | 
291  |  |  | 
292  |  | BIO *ossl_quic_port_get_net_rbio(QUIC_PORT *port)  | 
293  | 0  | { | 
294  | 0  |     return port->net_rbio;  | 
295  | 0  | }  | 
296  |  |  | 
297  |  | BIO *ossl_quic_port_get_net_wbio(QUIC_PORT *port)  | 
298  | 0  | { | 
299  | 0  |     return port->net_wbio;  | 
300  | 0  | }  | 
301  |  |  | 
302  |  | static int port_update_poll_desc(QUIC_PORT *port, BIO *net_bio, int for_write)  | 
303  | 0  | { | 
304  | 0  |     BIO_POLL_DESCRIPTOR d = {0}; | 
305  |  | 
  | 
306  | 0  |     if (net_bio == NULL  | 
307  | 0  |         || (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d))  | 
308  | 0  |         || (for_write && !BIO_get_wpoll_descriptor(net_bio, &d)))  | 
309  |  |         /* Non-pollable BIO */  | 
310  | 0  |         d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;  | 
311  |  | 
  | 
312  | 0  |     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  | 0  |     if (for_write)  | 
325  | 0  |         ossl_quic_reactor_set_poll_w(&port->engine->rtor, &d);  | 
326  | 0  |     else  | 
327  | 0  |         ossl_quic_reactor_set_poll_r(&port->engine->rtor, &d);  | 
328  |  | 
  | 
329  | 0  |     return 1;  | 
330  | 0  | }  | 
331  |  |  | 
332  |  | int ossl_quic_port_update_poll_descriptors(QUIC_PORT *port, int force)  | 
333  | 0  | { | 
334  | 0  |     int ok = 1;  | 
335  |  | 
  | 
336  | 0  |     if (!force && !port->bio_changed)  | 
337  | 0  |         return 0;  | 
338  |  |  | 
339  | 0  |     if (!port_update_poll_desc(port, port->net_rbio, /*for_write=*/0))  | 
340  | 0  |         ok = 0;  | 
341  |  | 
  | 
342  | 0  |     if (!port_update_poll_desc(port, port->net_wbio, /*for_write=*/1))  | 
343  | 0  |         ok = 0;  | 
344  |  | 
  | 
345  | 0  |     port->bio_changed = 0;  | 
346  | 0  |     return ok;  | 
347  | 0  | }  | 
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  | 0  | { | 
380  | 0  |     long rcaps = 0, wcaps = 0;  | 
381  |  | 
  | 
382  | 0  |     if (port->net_rbio != NULL)  | 
383  | 0  |         rcaps = BIO_dgram_get_effective_caps(port->net_rbio);  | 
384  |  | 
  | 
385  | 0  |     if (port->net_wbio != NULL)  | 
386  | 0  |         wcaps = BIO_dgram_get_effective_caps(port->net_wbio);  | 
387  |  | 
  | 
388  | 0  |     port->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);  | 
389  | 0  |     port->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);  | 
390  | 0  |     port->bio_changed = 1;  | 
391  | 0  | }  | 
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  | 0  | { | 
400  | 0  |     return port->addressed_mode_w;  | 
401  | 0  | }  | 
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  | 0  | { | 
416  | 0  |     if (port->net_rbio == net_rbio)  | 
417  | 0  |         return 1;  | 
418  |  |  | 
419  | 0  |     if (!port_update_poll_desc(port, net_rbio, /*for_write=*/0))  | 
420  | 0  |         return 0;  | 
421  |  |  | 
422  | 0  |     ossl_quic_demux_set_bio(port->demux, net_rbio);  | 
423  | 0  |     port->net_rbio = net_rbio;  | 
424  | 0  |     port_update_addressing_mode(port);  | 
425  | 0  |     return 1;  | 
426  | 0  | }  | 
427  |  |  | 
428  |  | int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio)  | 
429  | 0  | { | 
430  | 0  |     QUIC_CHANNEL *ch;  | 
431  |  | 
  | 
432  | 0  |     if (port->net_wbio == net_wbio)  | 
433  | 0  |         return 1;  | 
434  |  |  | 
435  | 0  |     if (!port_update_poll_desc(port, net_wbio, /*for_write=*/1))  | 
436  | 0  |         return 0;  | 
437  |  |  | 
438  | 0  |     OSSL_LIST_FOREACH(ch, ch, &port->channel_list)  | 
439  | 0  |         ossl_qtx_set_bio(ch->qtx, net_wbio);  | 
440  |  | 
  | 
441  | 0  |     port->net_wbio = net_wbio;  | 
442  | 0  |     port_update_addressing_mode(port);  | 
443  | 0  |     return 1;  | 
444  | 0  | }  | 
445  |  |  | 
446  |  | SSL_CTX *ossl_quic_port_get_channel_ctx(QUIC_PORT *port)  | 
447  | 0  | { | 
448  | 0  |     return port->channel_ctx;  | 
449  | 0  | }  | 
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  | 0  | { | 
511  | 0  |     QUIC_CHANNEL_ARGS args = {0}; | 
512  | 0  |     QUIC_CHANNEL *ch;  | 
513  |  | 
  | 
514  | 0  |     args.port          = port;  | 
515  | 0  |     args.is_server     = is_server;  | 
516  | 0  |     args.lcidm         = port->lcidm;  | 
517  | 0  |     args.srtm          = port->srtm;  | 
518  | 0  |     args.qrx           = qrx;  | 
519  | 0  |     args.is_tserver_ch = is_tserver;  | 
520  |  |  | 
521  |  |     /*  | 
522  |  |      * Creating a new channel is made a bit tricky here as there is a  | 
523  |  |      * bit of a circular dependency.  Initializing a channel requires that  | 
524  |  |      * the ch->tls and optionally the qlog_title be configured prior to  | 
525  |  |      * initialization, 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  | 0  |     ch = ossl_quic_channel_alloc(&args);  | 
533  | 0  |     if (ch == NULL)  | 
534  | 0  |         return NULL;  | 
535  |  |  | 
536  |  |     /*  | 
537  |  |      * Fixup the channel tls connection here before we init the channel  | 
538  |  |      */  | 
539  | 0  |     ch->tls = (tls != NULL) ? tls : port_new_handshake_layer(port, ch);  | 
540  |  | 
  | 
541  | 0  |     if (ch->tls == NULL) { | 
542  | 0  |         OPENSSL_free(ch);  | 
543  | 0  |         return NULL;  | 
544  | 0  |     }  | 
545  |  |  | 
546  | 0  | #ifndef OPENSSL_NO_QLOG  | 
547  |  |     /*  | 
548  |  |      * If we're using qlog, make sure the tls get further configured properly  | 
549  |  |      */  | 
550  | 0  |     ch->use_qlog = 1;  | 
551  | 0  |     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  | 0  | #endif  | 
558  |  |  | 
559  |  |     /*  | 
560  |  |      * And finally init the channel struct  | 
561  |  |      */  | 
562  | 0  |     if (!ossl_quic_channel_init(ch)) { | 
563  | 0  |         OPENSSL_free(ch);  | 
564  | 0  |         return NULL;  | 
565  | 0  |     }  | 
566  |  |  | 
567  | 0  |     ossl_qtx_set_bio(ch->qtx, port->net_wbio);  | 
568  | 0  |     return ch;  | 
569  | 0  | }  | 
570  |  |  | 
571  |  | QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls)  | 
572  | 0  | { | 
573  | 0  |     return port_make_channel(port, tls, NULL, /* is_server= */ 0,  | 
574  | 0  |                              /* is_tserver= */ 0);  | 
575  | 0  | }  | 
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  | 0  | { | 
663  | 0  |     QUIC_CHANNEL *ch;  | 
664  |  | 
  | 
665  | 0  |     res->net_read_desired       = ossl_quic_port_is_running(port);  | 
666  | 0  |     res->net_write_desired      = 0;  | 
667  | 0  |     res->notify_other_threads   = 0;  | 
668  | 0  |     res->tick_deadline          = ossl_time_infinite();  | 
669  |  | 
  | 
670  | 0  |     if (!port->engine->inhibit_tick) { | 
671  |  |         /* Handle any incoming data from network. */  | 
672  | 0  |         if (ossl_quic_port_is_running(port))  | 
673  | 0  |             port_rx_pre(port);  | 
674  |  |  | 
675  |  |         /* Iterate through all channels and service them. */  | 
676  | 0  |         OSSL_LIST_FOREACH(ch, ch, &port->channel_list) { | 
677  | 0  |             QUIC_TICK_RESULT subr = {0}; | 
678  |  | 
  | 
679  | 0  |             ossl_quic_channel_subtick(ch, &subr, flags);  | 
680  | 0  |             ossl_quic_tick_result_merge_into(res, &subr);  | 
681  | 0  |         }  | 
682  | 0  |     }  | 
683  | 0  | }  | 
684  |  |  | 
685  |  | /* Process incoming datagrams, if any. */  | 
686  |  | static void port_rx_pre(QUIC_PORT *port)  | 
687  | 0  | { | 
688  | 0  |     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  | 0  |     if (!port->allow_incoming && !port->have_sent_any_pkt)  | 
707  | 0  |         return;  | 
708  |  |  | 
709  |  |     /*  | 
710  |  |      * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams  | 
711  |  |      * to the appropriate QRX instances.  | 
712  |  |      */  | 
713  | 0  |     ret = ossl_quic_demux_pump(port->demux);  | 
714  | 0  |     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  | 0  | }  | 
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  |         if (peer != NULL && BIO_ADDR_family(peer) != AF_UNSPEC)  | 
744  | 0  |             ossl_quic_channel_set_peer_addr(ch, peer);  | 
745  |  | 
  | 
746  | 0  |         ossl_quic_channel_bind_qrx(ch, qrx);  | 
747  | 0  |         ossl_qrx_set_msg_callback(ch->qrx, ch->msg_callback,  | 
748  | 0  |                                   ch->msg_callback_ssl);  | 
749  | 0  |         ossl_qrx_set_msg_callback_arg(ch->qrx, ch->msg_callback_arg);  | 
750  | 0  |     } else { | 
751  | 0  |         ch = port_make_channel(port, NULL, qrx, /* is_server= */ 1,  | 
752  | 0  |                                /* is_tserver */ 0);  | 
753  | 0  |     }  | 
754  |  | 
  | 
755  | 0  |     if (ch == NULL)  | 
756  | 0  |         return;  | 
757  |  |  | 
758  |  |     /*  | 
759  |  |      * If we didn't provide a qrx here that means we need to set our initial  | 
760  |  |      * secret here, since we just created a qrx  | 
761  |  |      * Normally its not needed, as the initial secret gets added when we send  | 
762  |  |      * our first server hello, but if we get a huge client hello, crossing  | 
763  |  |      * multiple datagrams, we don't have a chance to do that, and datagrams  | 
764  |  |      * after the first won't get decoded properly, for lack of secrets  | 
765  |  |      */  | 
766  | 0  |     if (qrx == NULL)  | 
767  | 0  |         if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,  | 
768  | 0  |                                               ch->port->engine->propq,  | 
769  | 0  |                                               dcid, /* is_server */ 1,  | 
770  | 0  |                                               ch->qrx, NULL))  | 
771  | 0  |             return;  | 
772  |  |  | 
773  | 0  |     if (odcid->id_len != 0) { | 
774  |  |         /*  | 
775  |  |          * If we have an odcid, then we went through server address validation  | 
776  |  |          * and as such, this channel need not conform to the 3x validation cap  | 
777  |  |          * See RFC 9000 s. 8.1  | 
778  |  |          */  | 
779  | 0  |         ossl_quic_tx_packetiser_set_validated(ch->txp);  | 
780  | 0  |         if (!ossl_quic_bind_channel(ch, peer, scid, dcid, odcid)) { | 
781  | 0  |             ossl_quic_channel_free(ch);  | 
782  | 0  |             return;  | 
783  | 0  |         }  | 
784  | 0  |     } else { | 
785  |  |         /*  | 
786  |  |          * No odcid means we didn't do server validation, so we need to  | 
787  |  |          * generate a cid via ossl_quic_channel_on_new_conn  | 
788  |  |          */  | 
789  | 0  |         if (!ossl_quic_channel_on_new_conn(ch, peer, scid, dcid)) { | 
790  | 0  |             ossl_quic_channel_free(ch);  | 
791  | 0  |             return;  | 
792  | 0  |         }  | 
793  | 0  |     }  | 
794  |  |  | 
795  | 0  |     ossl_list_incoming_ch_insert_tail(&port->incoming_channel_list, ch);  | 
796  | 0  |     *new_ch = ch;  | 
797  | 0  | }  | 
798  |  |  | 
799  |  | static int port_try_handle_stateless_reset(QUIC_PORT *port, const QUIC_URXE *e)  | 
800  | 0  | { | 
801  | 0  |     size_t i;  | 
802  | 0  |     const unsigned char *data = ossl_quic_urxe_data(e);  | 
803  | 0  |     void *opaque = NULL;  | 
804  |  |  | 
805  |  |     /*  | 
806  |  |      * Perform some fast and cheap checks for a packet not being a stateless  | 
807  |  |      * reset token.  RFC 9000 s. 10.3 specifies this layout for stateless  | 
808  |  |      * reset packets:  | 
809  |  |      *  | 
810  |  |      *  Stateless Reset { | 
811  |  |      *      Fixed Bits (2) = 1,  | 
812  |  |      *      Unpredictable Bits (38..),  | 
813  |  |      *      Stateless Reset Token (128),  | 
814  |  |      *  }  | 
815  |  |      *  | 
816  |  |      * It also specifies:  | 
817  |  |      *      However, endpoints MUST treat any packet ending in a valid  | 
818  |  |      *      stateless reset token as a Stateless Reset, as other QUIC  | 
819  |  |      *      versions might allow the use of a long header.  | 
820  |  |      *  | 
821  |  |      * We can rapidly check for the minimum length and that the first pair  | 
822  |  |      * of bits in the first byte are 01 or 11.  | 
823  |  |      *  | 
824  |  |      * The function returns 1 if it is a stateless reset packet, 0 if it isn't  | 
825  |  |      * and -1 if an error was encountered.  | 
826  |  |      */  | 
827  | 0  |     if (e->data_len < QUIC_STATELESS_RESET_TOKEN_LEN + 5  | 
828  | 0  |         || (0100 & *data) != 0100)  | 
829  | 0  |         return 0;  | 
830  |  |  | 
831  | 0  |     for (i = 0;; ++i) { | 
832  | 0  |         if (!ossl_quic_srtm_lookup(port->srtm,  | 
833  | 0  |                                    (QUIC_STATELESS_RESET_TOKEN *)(data + e->data_len  | 
834  | 0  |                                    - sizeof(QUIC_STATELESS_RESET_TOKEN)),  | 
835  | 0  |                                    i, &opaque, NULL))  | 
836  | 0  |             break;  | 
837  |  |  | 
838  | 0  |         assert(opaque != NULL);  | 
839  | 0  |         ossl_quic_channel_on_stateless_reset((QUIC_CHANNEL *)opaque);  | 
840  | 0  |     }  | 
841  |  | 
  | 
842  | 0  |     return i > 0;  | 
843  | 0  | }  | 
844  |  |  | 
845  |  | static void cleanup_validation_token(QUIC_VALIDATION_TOKEN *token)  | 
846  | 0  | { | 
847  | 0  |     OPENSSL_free(token->remote_addr);  | 
848  | 0  | }  | 
849  |  |  | 
850  |  | /**  | 
851  |  |  * @brief Generates a validation token for a RETRY/NEW_TOKEN packet.  | 
852  |  |  *  | 
853  |  |  *  | 
854  |  |  * @param peer  Address of the client peer receiving the packet.  | 
855  |  |  * @param odcid DCID of the connection attempt.  | 
856  |  |  * @param rscid Retry source connection ID of the connection attempt.  | 
857  |  |  * @param token Address of token to fill data.  | 
858  |  |  *  | 
859  |  |  * @return 1 if validation token is filled successfully, 0 otherwise.  | 
860  |  |  */  | 
861  |  | static int generate_token(BIO_ADDR *peer, QUIC_CONN_ID odcid,  | 
862  |  |                           QUIC_CONN_ID rscid, QUIC_VALIDATION_TOKEN *token,  | 
863  |  |                           int is_retry)  | 
864  | 0  | { | 
865  | 0  |     token->is_retry = is_retry;  | 
866  | 0  |     token->timestamp = ossl_time_now();  | 
867  | 0  |     token->remote_addr = NULL;  | 
868  | 0  |     token->odcid = odcid;  | 
869  | 0  |     token->rscid = rscid;  | 
870  |  | 
  | 
871  | 0  |     if (!BIO_ADDR_rawaddress(peer, NULL, &token->remote_addr_len)  | 
872  | 0  |         || token->remote_addr_len == 0  | 
873  | 0  |         || (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL  | 
874  | 0  |         || !BIO_ADDR_rawaddress(peer, token->remote_addr,  | 
875  | 0  |                                 &token->remote_addr_len)) { | 
876  | 0  |         cleanup_validation_token(token);  | 
877  | 0  |         return 0;  | 
878  | 0  |     }  | 
879  |  |  | 
880  | 0  |     return 1;  | 
881  | 0  | }  | 
882  |  |  | 
883  |  | /**  | 
884  |  |  * @brief Marshals a validation token into a new buffer.  | 
885  |  |  *  | 
886  |  |  * |buffer| should already be allocated and at least MARSHALLED_TOKEN_MAX_LEN  | 
887  |  |  * bytes long. Stores the length of data stored in |buffer| in |buffer_len|.  | 
888  |  |  *  | 
889  |  |  * @param token      Validation token.  | 
890  |  |  * @param buffer     Address to store the marshalled token.  | 
891  |  |  * @param buffer_len Size of data stored in |buffer|.  | 
892  |  |  */  | 
893  |  | static int marshal_validation_token(QUIC_VALIDATION_TOKEN *token,  | 
894  |  |                                     unsigned char *buffer, size_t *buffer_len)  | 
895  | 0  | { | 
896  | 0  |     WPACKET wpkt = {0}; | 
897  | 0  |     BUF_MEM *buf_mem = BUF_MEM_new();  | 
898  |  | 
  | 
899  | 0  |     if (buffer == NULL || buf_mem == NULL  | 
900  | 0  |         || (token->is_retry != 0 && token->is_retry != 1)) { | 
901  | 0  |         BUF_MEM_free(buf_mem);  | 
902  | 0  |         return 0;  | 
903  | 0  |     }  | 
904  |  |  | 
905  | 0  |     if (!WPACKET_init(&wpkt, buf_mem)  | 
906  | 0  |         || !WPACKET_put_bytes_u8(&wpkt, token->is_retry)  | 
907  | 0  |         || !WPACKET_memcpy(&wpkt, &token->timestamp,  | 
908  | 0  |                            sizeof(token->timestamp))  | 
909  | 0  |         || (token->is_retry  | 
910  | 0  |             && (!WPACKET_sub_memcpy_u8(&wpkt, &token->odcid.id,  | 
911  | 0  |                                        token->odcid.id_len)  | 
912  | 0  |                 || !WPACKET_sub_memcpy_u8(&wpkt, &token->rscid.id,  | 
913  | 0  |                                           token->rscid.id_len)))  | 
914  | 0  |         || !WPACKET_sub_memcpy_u8(&wpkt, token->remote_addr, token->remote_addr_len)  | 
915  | 0  |         || !WPACKET_get_total_written(&wpkt, buffer_len)  | 
916  | 0  |         || *buffer_len > MARSHALLED_TOKEN_MAX_LEN  | 
917  | 0  |         || !WPACKET_finish(&wpkt)) { | 
918  | 0  |         WPACKET_cleanup(&wpkt);  | 
919  | 0  |         BUF_MEM_free(buf_mem);  | 
920  | 0  |         return 0;  | 
921  | 0  |     }  | 
922  |  |  | 
923  | 0  |     memcpy(buffer, buf_mem->data, *buffer_len);  | 
924  | 0  |     BUF_MEM_free(buf_mem);  | 
925  | 0  |     return 1;  | 
926  | 0  | }  | 
927  |  |  | 
928  |  | /**  | 
929  |  |  * @brief Encrypts a validation token using AES-256-GCM  | 
930  |  |  *  | 
931  |  |  * @param port       The QUIC port containing the encryption key  | 
932  |  |  * @param plaintext  The data to encrypt  | 
933  |  |  * @param pt_len     Length of the plaintext  | 
934  |  |  * @param ciphertext Buffer to receive encrypted data. If NULL, ct_len will be  | 
935  |  |  *                   set to the required buffer size and function returns  | 
936  |  |  *                   immediately.  | 
937  |  |  * @param ct_len     Pointer to size_t that will receive the ciphertext length.  | 
938  |  |  *                   This also includes bytes for QUIC_RETRY_INTEGRITY_TAG_LEN.  | 
939  |  |  *  | 
940  |  |  * @return 1 on success, 0 on failure  | 
941  |  |  *  | 
942  |  |  * The ciphertext format is:  | 
943  |  |  * [EVP_GCM_IV_LEN bytes IV][encrypted data][EVP_GCM_TAG_LEN bytes tag]  | 
944  |  |  */  | 
945  |  | static int encrypt_validation_token(const QUIC_PORT *port,  | 
946  |  |                                     const unsigned char *plaintext,  | 
947  |  |                                     size_t pt_len,  | 
948  |  |                                     unsigned char *ciphertext,  | 
949  |  |                                     size_t *ct_len)  | 
950  | 0  | { | 
951  | 0  |     int iv_len, len, ret = 0;  | 
952  | 0  |     int tag_len;  | 
953  | 0  |     unsigned char *iv = ciphertext, *data, *tag;  | 
954  |  | 
  | 
955  | 0  |     if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) <= 0  | 
956  | 0  |         || (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0)  | 
957  | 0  |         goto err;  | 
958  |  |  | 
959  | 0  |     *ct_len = iv_len + pt_len + tag_len + QUIC_RETRY_INTEGRITY_TAG_LEN;  | 
960  | 0  |     if (ciphertext == NULL) { | 
961  | 0  |         ret = 1;  | 
962  | 0  |         goto err;  | 
963  | 0  |     }  | 
964  |  |  | 
965  | 0  |     data = ciphertext + iv_len;  | 
966  | 0  |     tag = data + pt_len;  | 
967  |  | 
  | 
968  | 0  |     if (!RAND_bytes_ex(port->engine->libctx, ciphertext, iv_len, 0)  | 
969  | 0  |         || !EVP_EncryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv)  | 
970  | 0  |         || !EVP_EncryptUpdate(port->token_ctx, data, &len, plaintext, (int)pt_len)  | 
971  | 0  |         || !EVP_EncryptFinal_ex(port->token_ctx, data + pt_len, &len)  | 
972  | 0  |         || !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_GET_TAG, tag_len, tag))  | 
973  | 0  |         goto err;  | 
974  |  |  | 
975  | 0  |     ret = 1;  | 
976  | 0  | err:  | 
977  | 0  |     return ret;  | 
978  | 0  | }  | 
979  |  |  | 
980  |  | /**  | 
981  |  |  * @brief Decrypts a validation token using AES-256-GCM  | 
982  |  |  *  | 
983  |  |  * @param port       The QUIC port containing the decryption key  | 
984  |  |  * @param ciphertext The encrypted data (including IV and tag)  | 
985  |  |  * @param ct_len     Length of the ciphertext  | 
986  |  |  * @param plaintext  Buffer to receive decrypted data. If NULL, pt_len will be  | 
987  |  |  *                   set to the required buffer size.  | 
988  |  |  * @param pt_len     Pointer to size_t that will receive the plaintext length  | 
989  |  |  *  | 
990  |  |  * @return 1 on success, 0 on failure  | 
991  |  |  *  | 
992  |  |  * Expected ciphertext format:  | 
993  |  |  * [EVP_GCM_IV_LEN bytes IV][encrypted data][EVP_GCM_TAG_LEN bytes tag]  | 
994  |  |  */  | 
995  |  | static int decrypt_validation_token(const QUIC_PORT *port,  | 
996  |  |                                     const unsigned char *ciphertext,  | 
997  |  |                                     size_t ct_len,  | 
998  |  |                                     unsigned char *plaintext,  | 
999  |  |                                     size_t *pt_len)  | 
1000  | 0  | { | 
1001  | 0  |     int iv_len, len = 0, ret = 0;  | 
1002  | 0  |     int tag_len;  | 
1003  | 0  |     const unsigned char *iv = ciphertext, *data, *tag;  | 
1004  |  | 
  | 
1005  | 0  |     if ((tag_len = EVP_CIPHER_CTX_get_tag_length(port->token_ctx)) <= 0  | 
1006  | 0  |         || (iv_len = EVP_CIPHER_CTX_get_iv_length(port->token_ctx)) <= 0)  | 
1007  | 0  |         goto err;  | 
1008  |  |  | 
1009  |  |     /* Prevent decryption of a buffer that is not within reasonable bounds */  | 
1010  | 0  |     if (ct_len < (size_t)(iv_len + tag_len) || ct_len > ENCRYPTED_TOKEN_MAX_LEN)  | 
1011  | 0  |         goto err;  | 
1012  |  |  | 
1013  | 0  |     *pt_len = ct_len - iv_len - tag_len;  | 
1014  | 0  |     if (plaintext == NULL) { | 
1015  | 0  |         ret = 1;  | 
1016  | 0  |         goto err;  | 
1017  | 0  |     }  | 
1018  |  |  | 
1019  | 0  |     data = ciphertext + iv_len;  | 
1020  | 0  |     tag = ciphertext + ct_len - tag_len;  | 
1021  |  | 
  | 
1022  | 0  |     if (!EVP_DecryptInit_ex(port->token_ctx, NULL, NULL, NULL, iv)  | 
1023  | 0  |         || !EVP_DecryptUpdate(port->token_ctx, plaintext, &len, data,  | 
1024  | 0  |                               (int)(ct_len - iv_len - tag_len))  | 
1025  | 0  |         || !EVP_CIPHER_CTX_ctrl(port->token_ctx, EVP_CTRL_GCM_SET_TAG, tag_len,  | 
1026  | 0  |                                 (void *)tag)  | 
1027  | 0  |         || !EVP_DecryptFinal_ex(port->token_ctx, plaintext + len, &len))  | 
1028  | 0  |         goto err;  | 
1029  |  |  | 
1030  | 0  |     ret = 1;  | 
1031  |  | 
  | 
1032  | 0  | err:  | 
1033  | 0  |     return ret;  | 
1034  | 0  | }  | 
1035  |  |  | 
1036  |  | /**  | 
1037  |  |  * @brief Parses contents of a buffer into a validation token.  | 
1038  |  |  *  | 
1039  |  |  * VALIDATION_TOKEN should already be initialized. Does some basic sanity checks.  | 
1040  |  |  *  | 
1041  |  |  * @param token   Validation token to fill data in.  | 
1042  |  |  * @param buf     Buffer of previously marshaled validation token.  | 
1043  |  |  * @param buf_len Length of |buf|.  | 
1044  |  |  */  | 
1045  |  | static int parse_validation_token(QUIC_VALIDATION_TOKEN *token,  | 
1046  |  |                                   const unsigned char *buf, size_t buf_len)  | 
1047  | 0  | { | 
1048  | 0  |     PACKET pkt, subpkt;  | 
1049  |  | 
  | 
1050  | 0  |     if (buf == NULL || token == NULL)  | 
1051  | 0  |         return 0;  | 
1052  |  |  | 
1053  | 0  |     token->remote_addr = NULL;  | 
1054  |  | 
  | 
1055  | 0  |     if (!PACKET_buf_init(&pkt, buf, buf_len)  | 
1056  | 0  |         || !PACKET_copy_bytes(&pkt, &token->is_retry, sizeof(token->is_retry))  | 
1057  | 0  |         || !(token->is_retry == 0 || token->is_retry == 1)  | 
1058  | 0  |         || !PACKET_copy_bytes(&pkt, (unsigned char *)&token->timestamp,  | 
1059  | 0  |                               sizeof(token->timestamp))  | 
1060  | 0  |         || (token->is_retry  | 
1061  | 0  |             && (!PACKET_get_length_prefixed_1(&pkt, &subpkt)  | 
1062  | 0  |                 || (token->odcid.id_len = (unsigned char)PACKET_remaining(&subpkt))  | 
1063  | 0  |                     > QUIC_MAX_CONN_ID_LEN  | 
1064  | 0  |                 || !PACKET_copy_bytes(&subpkt,  | 
1065  | 0  |                                       (unsigned char *)&token->odcid.id,  | 
1066  | 0  |                                       token->odcid.id_len)  | 
1067  | 0  |                 || !PACKET_get_length_prefixed_1(&pkt, &subpkt)  | 
1068  | 0  |                 || (token->rscid.id_len = (unsigned char)PACKET_remaining(&subpkt))  | 
1069  | 0  |                     > QUIC_MAX_CONN_ID_LEN  | 
1070  | 0  |                 || !PACKET_copy_bytes(&subpkt, (unsigned char *)&token->rscid.id,  | 
1071  | 0  |                                       token->rscid.id_len)))  | 
1072  | 0  |         || !PACKET_get_length_prefixed_1(&pkt, &subpkt)  | 
1073  | 0  |         || (token->remote_addr_len = PACKET_remaining(&subpkt)) == 0  | 
1074  | 0  |         || (token->remote_addr = OPENSSL_malloc(token->remote_addr_len)) == NULL  | 
1075  | 0  |         || !PACKET_copy_bytes(&subpkt, token->remote_addr, token->remote_addr_len)  | 
1076  | 0  |         || PACKET_remaining(&pkt) != 0) { | 
1077  | 0  |         cleanup_validation_token(token);  | 
1078  | 0  |         return 0;  | 
1079  | 0  |     }  | 
1080  |  |  | 
1081  | 0  |     return 1;  | 
1082  | 0  | }  | 
1083  |  |  | 
1084  |  | /**  | 
1085  |  |  * @brief Sends a QUIC Retry packet to a client.  | 
1086  |  |  *  | 
1087  |  |  * This function constructs and sends a Retry packet to the specified client  | 
1088  |  |  * using the provided connection header information. The Retry packet  | 
1089  |  |  * includes a generated validation token and a new connection ID, following  | 
1090  |  |  * the QUIC protocol specifications for connection establishment.  | 
1091  |  |  *  | 
1092  |  |  * @param port        Pointer to the QUIC port from which to send the packet.  | 
1093  |  |  * @param peer        Address of the client peer receiving the packet.  | 
1094  |  |  * @param client_hdr  Header of the client's initial packet, containing  | 
1095  |  |  *                    connection IDs and other relevant information.  | 
1096  |  |  *  | 
1097  |  |  * This function performs the following steps:  | 
1098  |  |  * - Generates a validation token for the client.  | 
1099  |  |  * - Sets the destination and source connection IDs.  | 
1100  |  |  * - Calculates the integrity tag and sets the token length.  | 
1101  |  |  * - Encodes and sends the packet via the BIO network interface.  | 
1102  |  |  *  | 
1103  |  |  * Error handling is included for failures in CID generation, encoding, and  | 
1104  |  |  * network transmiss  | 
1105  |  |  */  | 
1106  |  | static void port_send_retry(QUIC_PORT *port,  | 
1107  |  |                             BIO_ADDR *peer,  | 
1108  |  |                             QUIC_PKT_HDR *client_hdr)  | 
1109  | 0  | { | 
1110  | 0  |     BIO_MSG msg[1];  | 
1111  |  |     /*  | 
1112  |  |      * Buffer is used for both marshalling the token as well as for the RETRY  | 
1113  |  |      * packet. The size of buffer should not be less than  | 
1114  |  |      * MARSHALLED_TOKEN_MAX_LEN.  | 
1115  |  |      */  | 
1116  | 0  |     unsigned char buffer[512];  | 
1117  | 0  |     unsigned char ct_buf[ENCRYPTED_TOKEN_MAX_LEN];  | 
1118  | 0  |     WPACKET wpkt;  | 
1119  | 0  |     size_t written, token_buf_len, ct_len;  | 
1120  | 0  |     QUIC_PKT_HDR hdr = {0}; | 
1121  | 0  |     QUIC_VALIDATION_TOKEN token = {0}; | 
1122  | 0  |     int ok;  | 
1123  |  | 
  | 
1124  | 0  |     if (!ossl_assert(sizeof(buffer) >= MARSHALLED_TOKEN_MAX_LEN))  | 
1125  | 0  |         return;  | 
1126  |  |     /*  | 
1127  |  |      * 17.2.5.1 Sending a Retry packet  | 
1128  |  |      *   dst ConnId is src ConnId we got from client  | 
1129  |  |      *   src ConnId comes from local conn ID manager  | 
1130  |  |      */  | 
1131  | 0  |     memset(&hdr, 0, sizeof(QUIC_PKT_HDR));  | 
1132  | 0  |     hdr.dst_conn_id = client_hdr->src_conn_id;  | 
1133  |  |     /*  | 
1134  |  |      * this is the random connection ID, we expect client is  | 
1135  |  |      * going to send the ID with next INITIAL packet which  | 
1136  |  |      * will also come with token we generate here.  | 
1137  |  |      */  | 
1138  | 0  |     ok = ossl_quic_lcidm_get_unused_cid(port->lcidm, &hdr.src_conn_id);  | 
1139  | 0  |     if (ok == 0)  | 
1140  | 0  |         goto err;  | 
1141  |  |  | 
1142  | 0  |     memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN));  | 
1143  |  |  | 
1144  |  |     /* Generate retry validation token */  | 
1145  | 0  |     if (!generate_token(peer, client_hdr->dst_conn_id,  | 
1146  | 0  |                         hdr.src_conn_id, &token, 1)  | 
1147  | 0  |         || !marshal_validation_token(&token, buffer, &token_buf_len)  | 
1148  | 0  |         || !encrypt_validation_token(port, buffer, token_buf_len, NULL,  | 
1149  | 0  |                                      &ct_len)  | 
1150  | 0  |         || ct_len > ENCRYPTED_TOKEN_MAX_LEN  | 
1151  | 0  |         || !encrypt_validation_token(port, buffer, token_buf_len, ct_buf,  | 
1152  | 0  |                                      &ct_len)  | 
1153  | 0  |         || !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN))  | 
1154  | 0  |         goto err;  | 
1155  |  |  | 
1156  | 0  |     hdr.dst_conn_id = client_hdr->src_conn_id;  | 
1157  | 0  |     hdr.type = QUIC_PKT_TYPE_RETRY;  | 
1158  | 0  |     hdr.fixed = 1;  | 
1159  | 0  |     hdr.version = 1;  | 
1160  | 0  |     hdr.len = ct_len;  | 
1161  | 0  |     hdr.data = ct_buf;  | 
1162  | 0  |     ok = ossl_quic_calculate_retry_integrity_tag(port->engine->libctx,  | 
1163  | 0  |                                                  port->engine->propq, &hdr,  | 
1164  | 0  |                                                  &client_hdr->dst_conn_id,  | 
1165  | 0  |                                                  ct_buf + ct_len  | 
1166  | 0  |                                                  - QUIC_RETRY_INTEGRITY_TAG_LEN);  | 
1167  | 0  |     if (ok == 0)  | 
1168  | 0  |         goto err;  | 
1169  |  |  | 
1170  | 0  |     hdr.token = hdr.data;  | 
1171  | 0  |     hdr.token_len = hdr.len;  | 
1172  |  | 
  | 
1173  | 0  |     msg[0].data = buffer;  | 
1174  | 0  |     msg[0].peer = peer;  | 
1175  | 0  |     msg[0].local = NULL;  | 
1176  | 0  |     msg[0].flags = 0;  | 
1177  |  | 
  | 
1178  | 0  |     ok = WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0);  | 
1179  | 0  |     if (ok == 0)  | 
1180  | 0  |         goto err;  | 
1181  |  |  | 
1182  | 0  |     ok = ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len,  | 
1183  | 0  |                                        &hdr, NULL);  | 
1184  | 0  |     if (ok == 0)  | 
1185  | 0  |         goto err;  | 
1186  |  |  | 
1187  | 0  |     ok = WPACKET_get_total_written(&wpkt, &msg[0].data_len);  | 
1188  | 0  |     if (ok == 0)  | 
1189  | 0  |         goto err;  | 
1190  |  |  | 
1191  | 0  |     ok = WPACKET_finish(&wpkt);  | 
1192  | 0  |     if (ok == 0)  | 
1193  | 0  |         goto err;  | 
1194  |  |  | 
1195  |  |     /*  | 
1196  |  |      * TODO(QUIC FUTURE) need to retry this in the event it return EAGAIN  | 
1197  |  |      * on a non-blocking BIO  | 
1198  |  |      */  | 
1199  | 0  |     if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written))  | 
1200  | 0  |         ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,  | 
1201  | 0  |                        "port retry send failed due to network BIO I/O error");  | 
1202  |  | 
  | 
1203  | 0  | err:  | 
1204  | 0  |     cleanup_validation_token(&token);  | 
1205  | 0  | }  | 
1206  |  |  | 
1207  |  | /**  | 
1208  |  |  * @brief Sends a QUIC Version Negotiation packet to the specified peer.  | 
1209  |  |  *  | 
1210  |  |  * This function constructs and sends a Version Negotiation packet using  | 
1211  |  |  * the connection IDs from the client's initial packet header. The  | 
1212  |  |  * Version Negotiation packet indicates support for QUIC version 1.  | 
1213  |  |  *  | 
1214  |  |  * @param port      Pointer to the QUIC_PORT structure representing the port  | 
1215  |  |  *                  context used for network communication.  | 
1216  |  |  * @param peer      Pointer to the BIO_ADDR structure specifying the address  | 
1217  |  |  *                  of the peer to which the Version Negotiation packet  | 
1218  |  |  *                  will be sent.  | 
1219  |  |  * @param client_hdr Pointer to the QUIC_PKT_HDR structure containing the  | 
1220  |  |  *                  client's packet header used to extract connection IDs.  | 
1221  |  |  *  | 
1222  |  |  * @note The function will raise an error if sending the message fails.  | 
1223  |  |  */  | 
1224  |  | static void port_send_version_negotiation(QUIC_PORT *port, BIO_ADDR *peer,  | 
1225  |  |                                           QUIC_PKT_HDR *client_hdr)  | 
1226  | 0  | { | 
1227  | 0  |     BIO_MSG msg[1];  | 
1228  | 0  |     unsigned char buffer[1024];  | 
1229  | 0  |     QUIC_PKT_HDR hdr;  | 
1230  | 0  |     WPACKET wpkt;  | 
1231  | 0  |     uint32_t supported_versions[1];  | 
1232  | 0  |     size_t written;  | 
1233  | 0  |     size_t i;  | 
1234  |  | 
  | 
1235  | 0  |     memset(&hdr, 0, sizeof(QUIC_PKT_HDR));  | 
1236  |  |     /*  | 
1237  |  |      * Reverse the source and dst conn ids  | 
1238  |  |      */  | 
1239  | 0  |     hdr.dst_conn_id = client_hdr->src_conn_id;  | 
1240  | 0  |     hdr.src_conn_id = client_hdr->dst_conn_id;  | 
1241  |  |  | 
1242  |  |     /*  | 
1243  |  |      * This is our list of supported protocol versions  | 
1244  |  |      * Currently only QUIC_VERSION_1  | 
1245  |  |      */  | 
1246  | 0  |     supported_versions[0] = QUIC_VERSION_1;  | 
1247  |  |  | 
1248  |  |     /*  | 
1249  |  |      * Fill out the header fields  | 
1250  |  |      * Note: Version negotiation packets, must, unlike  | 
1251  |  |      * other packet types have a version of 0  | 
1252  |  |      */  | 
1253  | 0  |     hdr.type = QUIC_PKT_TYPE_VERSION_NEG;  | 
1254  | 0  |     hdr.version = 0;  | 
1255  | 0  |     hdr.token = 0;  | 
1256  | 0  |     hdr.token_len = 0;  | 
1257  | 0  |     hdr.len = sizeof(supported_versions);  | 
1258  | 0  |     hdr.data = (unsigned char *)supported_versions;  | 
1259  |  | 
  | 
1260  | 0  |     msg[0].data = buffer;  | 
1261  | 0  |     msg[0].peer = peer;  | 
1262  | 0  |     msg[0].local = NULL;  | 
1263  | 0  |     msg[0].flags = 0;  | 
1264  |  | 
  | 
1265  | 0  |     if (!WPACKET_init_static_len(&wpkt, buffer, sizeof(buffer), 0))  | 
1266  | 0  |         return;  | 
1267  |  |  | 
1268  | 0  |     if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, client_hdr->dst_conn_id.id_len,  | 
1269  | 0  |                                        &hdr, NULL))  | 
1270  | 0  |         return;  | 
1271  |  |  | 
1272  |  |     /*  | 
1273  |  |      * Add the array of supported versions to the end of the packet  | 
1274  |  |      */  | 
1275  | 0  |     for (i = 0; i < OSSL_NELEM(supported_versions); i++) { | 
1276  | 0  |         if (!WPACKET_put_bytes_u32(&wpkt, supported_versions[i]))  | 
1277  | 0  |             return;  | 
1278  | 0  |     }  | 
1279  |  |  | 
1280  | 0  |     if (!WPACKET_get_total_written(&wpkt, &msg[0].data_len))  | 
1281  | 0  |         return;  | 
1282  |  |  | 
1283  | 0  |     if (!WPACKET_finish(&wpkt))  | 
1284  | 0  |         return;  | 
1285  |  |  | 
1286  |  |     /*  | 
1287  |  |      * Send it back to the client attempting to connect  | 
1288  |  |      * TODO(QUIC FUTURE): Need to handle the EAGAIN case here, if the  | 
1289  |  |      * BIO_sendmmsg call falls in a retryable manner  | 
1290  |  |      */  | 
1291  | 0  |     if (!BIO_sendmmsg(port->net_wbio, msg, sizeof(BIO_MSG), 1, 0, &written))  | 
1292  | 0  |         ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,  | 
1293  | 0  |                        "port version negotiation send failed");  | 
1294  | 0  | }  | 
1295  |  |  | 
1296  |  | /**  | 
1297  |  |  * @brief definitions of token lifetimes  | 
1298  |  |  *  | 
1299  |  |  * RETRY tokens are only valid for 10 seconds  | 
1300  |  |  * NEW_TOKEN tokens have a lifetime of 3600 sec (1 hour)  | 
1301  |  |  */  | 
1302  |  |  | 
1303  | 0  | #define RETRY_LIFETIME 10  | 
1304  | 0  | #define NEW_TOKEN_LIFETIME 3600  | 
1305  |  | /**  | 
1306  |  |  * @brief Validates a received token in a QUIC packet header.  | 
1307  |  |  *  | 
1308  |  |  * This function checks the validity of a token contained in the provided  | 
1309  |  |  * QUIC packet header (`QUIC_PKT_HDR *hdr`). The validation process involves  | 
1310  |  |  * verifying that the token matches an expected format and value. If the  | 
1311  |  |  * token is from a RETRY packet, the function extracts the original connection  | 
1312  |  |  * ID (ODCID)/original source connection ID (SCID) and stores it in the provided  | 
1313  |  |  * parameters. If the token is from a NEW_TOKEN packet, the values will be  | 
1314  |  |  * derived instead.  | 
1315  |  |  *  | 
1316  |  |  * @param hdr   Pointer to the QUIC packet header containing the token.  | 
1317  |  |  * @param port  Pointer to the QUIC port from which to send the packet.  | 
1318  |  |  * @param peer  Address of the client peer receiving the packet.  | 
1319  |  |  * @param odcid Pointer to the connection ID structure to store the ODCID if the  | 
1320  |  |  *              token is valid.  | 
1321  |  |  * @param scid  Pointer to the connection ID structure to store the SCID if the  | 
1322  |  |  *              token is valid.  | 
1323  |  |  *  | 
1324  |  |  * @return      1 if the token is valid and ODCID/SCID are successfully set.  | 
1325  |  |  *              0 otherwise.  | 
1326  |  |  *  | 
1327  |  |  * The function performs the following checks:  | 
1328  |  |  * - Token length meets the required minimum.  | 
1329  |  |  * - Buffer matches expected format.  | 
1330  |  |  * - Peer address matches previous connection address.  | 
1331  |  |  * - Token has not expired. Currently set to 10 seconds for tokens from RETRY  | 
1332  |  |  *   packets and 60 minutes for tokens from NEW_TOKEN packets. This may be  | 
1333  |  |  *   configurable in the future.  | 
1334  |  |  */  | 
1335  |  | static int port_validate_token(QUIC_PKT_HDR *hdr, QUIC_PORT *port,  | 
1336  |  |                                BIO_ADDR *peer, QUIC_CONN_ID *odcid,  | 
1337  |  |                                QUIC_CONN_ID *scid, uint8_t *gen_new_token)  | 
1338  | 0  | { | 
1339  | 0  |     int ret = 0;  | 
1340  | 0  |     QUIC_VALIDATION_TOKEN token = { 0 }; | 
1341  | 0  |     uint64_t time_diff;  | 
1342  | 0  |     size_t remote_addr_len, dec_token_len;  | 
1343  | 0  |     unsigned char *remote_addr = NULL, dec_token[MARSHALLED_TOKEN_MAX_LEN];  | 
1344  | 0  |     OSSL_TIME now = ossl_time_now();  | 
1345  |  | 
  | 
1346  | 0  |     *gen_new_token = 0;  | 
1347  |  | 
  | 
1348  | 0  |     if (!decrypt_validation_token(port, hdr->token, hdr->token_len, NULL,  | 
1349  | 0  |                                   &dec_token_len)  | 
1350  | 0  |         || dec_token_len > MARSHALLED_TOKEN_MAX_LEN  | 
1351  | 0  |         || !decrypt_validation_token(port, hdr->token, hdr->token_len,  | 
1352  | 0  |                                      dec_token, &dec_token_len)  | 
1353  | 0  |         || !parse_validation_token(&token, dec_token, dec_token_len))  | 
1354  | 0  |         goto err;  | 
1355  |  |  | 
1356  |  |     /*  | 
1357  |  |      * Validate token timestamp. Current time should not be before the token  | 
1358  |  |      * timestamp.  | 
1359  |  |      */  | 
1360  | 0  |     if (ossl_time_compare(now, token.timestamp) < 0)  | 
1361  | 0  |         goto err;  | 
1362  | 0  |     time_diff = ossl_time2seconds(ossl_time_abs_difference(token.timestamp,  | 
1363  | 0  |                                                            now));  | 
1364  | 0  |     if ((token.is_retry && time_diff > RETRY_LIFETIME)  | 
1365  | 0  |         || (!token.is_retry && time_diff > NEW_TOKEN_LIFETIME))  | 
1366  | 0  |         goto err;  | 
1367  |  |  | 
1368  |  |     /* Validate remote address */  | 
1369  | 0  |     if (!BIO_ADDR_rawaddress(peer, NULL, &remote_addr_len)  | 
1370  | 0  |         || remote_addr_len != token.remote_addr_len  | 
1371  | 0  |         || (remote_addr = OPENSSL_malloc(remote_addr_len)) == NULL  | 
1372  | 0  |         || !BIO_ADDR_rawaddress(peer, remote_addr, &remote_addr_len)  | 
1373  | 0  |         || memcmp(remote_addr, token.remote_addr, remote_addr_len) != 0)  | 
1374  | 0  |         goto err;  | 
1375  |  |  | 
1376  |  |     /*  | 
1377  |  |      * Set ODCID and SCID. If the token is from a RETRY packet, retrieve both  | 
1378  |  |      * from the token. Otherwise, generate a new ODCID and use the header's  | 
1379  |  |      * source connection ID for SCID.  | 
1380  |  |      */  | 
1381  | 0  |     if (token.is_retry) { | 
1382  |  |         /*  | 
1383  |  |          * We're parsing a packet header before its gone through AEAD validation  | 
1384  |  |          * here, so there is a chance we are dealing with corrupted data. Make  | 
1385  |  |          * Sure the dcid encoded in the token matches the headers dcid to  | 
1386  |  |          * mitigate that.  | 
1387  |  |          * TODO(QUIC FUTURE): Consider handling AEAD validation at the port  | 
1388  |  |          * level rather than the QRX/channel level to eliminate the need for  | 
1389  |  |          * this.  | 
1390  |  |          */  | 
1391  | 0  |         if (token.rscid.id_len != hdr->dst_conn_id.id_len  | 
1392  | 0  |             || memcmp(&token.rscid.id, &hdr->dst_conn_id.id,  | 
1393  | 0  |                       token.rscid.id_len) != 0)  | 
1394  | 0  |             goto err;  | 
1395  | 0  |         *odcid = token.odcid;  | 
1396  | 0  |         *scid = token.rscid;  | 
1397  | 0  |     } else { | 
1398  | 0  |         if (!ossl_quic_lcidm_get_unused_cid(port->lcidm, odcid))  | 
1399  | 0  |             goto err;  | 
1400  | 0  |         *scid = hdr->src_conn_id;  | 
1401  | 0  |     }  | 
1402  |  |  | 
1403  |  |     /*  | 
1404  |  |      * Determine if we need to send a NEW_TOKEN frame  | 
1405  |  |      * If we validated a retry token, we should always  | 
1406  |  |      * send a NEW_TOKEN frame to the client  | 
1407  |  |      *  | 
1408  |  |      * If however, we validated a NEW_TOKEN, which may be  | 
1409  |  |      * reused multiple times, only send a NEW_TOKEN frame  | 
1410  |  |      * if the existing received token has less than 10% of its lifetime  | 
1411  |  |      * remaining.  This prevents us from constantly sending  | 
1412  |  |      * NEW_TOKEN frames on every connection when not needed  | 
1413  |  |      */  | 
1414  | 0  |     if (token.is_retry) { | 
1415  | 0  |         *gen_new_token = 1;  | 
1416  | 0  |     } else { | 
1417  | 0  |         if (time_diff > ((NEW_TOKEN_LIFETIME * 9) / 10))  | 
1418  | 0  |             *gen_new_token = 1;  | 
1419  | 0  |     }  | 
1420  |  | 
  | 
1421  | 0  |     ret = 1;  | 
1422  | 0  | err:  | 
1423  | 0  |     cleanup_validation_token(&token);  | 
1424  | 0  |     OPENSSL_free(remote_addr);  | 
1425  | 0  |     return ret;  | 
1426  | 0  | }  | 
1427  |  |  | 
1428  |  | static void generate_new_token(QUIC_CHANNEL *ch, BIO_ADDR *peer)  | 
1429  | 0  | { | 
1430  | 0  |     QUIC_CONN_ID rscid = { 0 }; | 
1431  | 0  |     QUIC_VALIDATION_TOKEN token;  | 
1432  | 0  |     unsigned char buffer[ENCRYPTED_TOKEN_MAX_LEN];  | 
1433  | 0  |     unsigned char *ct_buf;  | 
1434  | 0  |     size_t ct_len;  | 
1435  | 0  |     size_t token_buf_len = 0;  | 
1436  |  |  | 
1437  |  |     /* Clients never send a NEW_TOKEN */  | 
1438  | 0  |     if (!ch->is_server)  | 
1439  | 0  |         return;  | 
1440  |  |  | 
1441  | 0  |     ct_buf = OPENSSL_zalloc(ENCRYPTED_TOKEN_MAX_LEN);  | 
1442  | 0  |     if (ct_buf == NULL)  | 
1443  | 0  |         return;  | 
1444  |  |  | 
1445  |  |     /*  | 
1446  |  |      * NEW_TOKEN tokens may be used for multiple subsequent connections  | 
1447  |  |      * within their timeout period, so don't reserve an rscid here  | 
1448  |  |      * like we do for retry tokens, instead, just fill it with random  | 
1449  |  |      * data, as we won't use it anyway  | 
1450  |  |      */  | 
1451  | 0  |     rscid.id_len = 8;  | 
1452  | 0  |     if (!RAND_bytes_ex(ch->port->engine->libctx, rscid.id, 8, 0)) { | 
1453  | 0  |         OPENSSL_free(ct_buf);  | 
1454  | 0  |         return;  | 
1455  | 0  |     }  | 
1456  |  |  | 
1457  | 0  |     memset(&token, 0, sizeof(QUIC_VALIDATION_TOKEN));  | 
1458  |  | 
  | 
1459  | 0  |     if (!generate_token(peer, ch->init_dcid, rscid, &token, 0)  | 
1460  | 0  |         || !marshal_validation_token(&token, buffer, &token_buf_len)  | 
1461  | 0  |         || !encrypt_validation_token(ch->port, buffer, token_buf_len, NULL,  | 
1462  | 0  |                                      &ct_len)  | 
1463  | 0  |         || ct_len > ENCRYPTED_TOKEN_MAX_LEN  | 
1464  | 0  |         || !encrypt_validation_token(ch->port, buffer, token_buf_len, ct_buf,  | 
1465  | 0  |                                      &ct_len)  | 
1466  | 0  |         || !ossl_assert(ct_len >= QUIC_RETRY_INTEGRITY_TAG_LEN)) { | 
1467  | 0  |         OPENSSL_free(ct_buf);  | 
1468  | 0  |         cleanup_validation_token(&token);  | 
1469  | 0  |         return;  | 
1470  | 0  |     }  | 
1471  |  |  | 
1472  | 0  |     ch->pending_new_token = ct_buf;  | 
1473  | 0  |     ch->pending_new_token_len = ct_len;  | 
1474  |  | 
  | 
1475  | 0  |     cleanup_validation_token(&token);  | 
1476  | 0  | }  | 
1477  |  |  | 
1478  |  | /*  | 
1479  |  |  * This is called by the demux when we get a packet not destined for any known  | 
1480  |  |  * DCID.  | 
1481  |  |  */  | 
1482  |  | static void port_default_packet_handler(QUIC_URXE *e, void *arg,  | 
1483  |  |                                         const QUIC_CONN_ID *dcid)  | 
1484  | 0  | { | 
1485  | 0  |     QUIC_PORT *port = arg;  | 
1486  | 0  |     PACKET pkt;  | 
1487  | 0  |     QUIC_PKT_HDR hdr;  | 
1488  | 0  |     QUIC_CHANNEL *ch = NULL, *new_ch = NULL;  | 
1489  | 0  |     QUIC_CONN_ID odcid, scid;  | 
1490  | 0  |     uint8_t gen_new_token = 0;  | 
1491  | 0  |     OSSL_QRX *qrx = NULL;  | 
1492  | 0  |     OSSL_QRX *qrx_src = NULL;  | 
1493  | 0  |     OSSL_QRX_ARGS qrx_args = {0}; | 
1494  | 0  |     uint64_t cause_flags = 0;  | 
1495  | 0  |     OSSL_QRX_PKT *qrx_pkt = NULL;  | 
1496  |  |  | 
1497  |  |     /* Don't handle anything if we are no longer running. */  | 
1498  | 0  |     if (!ossl_quic_port_is_running(port))  | 
1499  | 0  |         goto undesirable;  | 
1500  |  |  | 
1501  | 0  |     if (port_try_handle_stateless_reset(port, e))  | 
1502  | 0  |         goto undesirable;  | 
1503  |  |  | 
1504  | 0  |     if (dcid != NULL  | 
1505  | 0  |         && ossl_quic_lcidm_lookup(port->lcidm, dcid, NULL,  | 
1506  | 0  |                                   (void **)&ch)) { | 
1507  | 0  |         assert(ch != NULL);  | 
1508  | 0  |         ossl_quic_channel_inject(ch, e);  | 
1509  | 0  |         return;  | 
1510  | 0  |     }  | 
1511  |  |  | 
1512  |  |     /*  | 
1513  |  |      * If we have an incoming packet which doesn't match any existing connection  | 
1514  |  |      * we assume this is an attempt to make a new connection.  | 
1515  |  |      */  | 
1516  | 0  |     if (!port->allow_incoming)  | 
1517  | 0  |         goto undesirable;  | 
1518  |  |  | 
1519  |  |     /*  | 
1520  |  |      * We have got a packet for an unknown DCID. This might be an attempt to  | 
1521  |  |      * open a new connection.  | 
1522  |  |      */  | 
1523  | 0  |     if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN)  | 
1524  | 0  |         goto undesirable;  | 
1525  |  |  | 
1526  | 0  |     if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len))  | 
1527  | 0  |         goto undesirable;  | 
1528  |  |  | 
1529  |  |     /*  | 
1530  |  |      * We set short_conn_id_len to SIZE_MAX here which will cause the decode  | 
1531  |  |      * operation to fail if we get a 1-RTT packet. This is fine since we only  | 
1532  |  |      * care about Initial packets.  | 
1533  |  |      */  | 
1534  | 0  |     if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL,  | 
1535  | 0  |                                        &cause_flags)) { | 
1536  |  |         /*  | 
1537  |  |          * If we fail due to a bad version, we know the packet up to the version  | 
1538  |  |          * number was decoded, and we use it below to send a version  | 
1539  |  |          * negotiation packet  | 
1540  |  |          */  | 
1541  | 0  |         if ((cause_flags & QUIC_PKT_HDR_DECODE_BAD_VERSION) == 0)  | 
1542  | 0  |             goto undesirable;  | 
1543  | 0  |     }  | 
1544  |  |  | 
1545  | 0  |     switch (hdr.version) { | 
1546  | 0  |     case QUIC_VERSION_1:  | 
1547  | 0  |         break;  | 
1548  |  |  | 
1549  | 0  |     case QUIC_VERSION_NONE:  | 
1550  | 0  |     default:  | 
1551  |  |  | 
1552  |  |         /*  | 
1553  |  |          * If we get here, then we have a bogus version, and might need  | 
1554  |  |          * to send a version negotiation packet.  According to  | 
1555  |  |          * RFC 9000 s. 6 and 14.1, we only do so however, if the UDP datagram  | 
1556  |  |          * is a minimum of 1200 bytes in size  | 
1557  |  |          */  | 
1558  | 0  |         if (e->data_len < 1200)  | 
1559  | 0  |             goto undesirable;  | 
1560  |  |  | 
1561  |  |         /*  | 
1562  |  |          * If we don't get a supported version, respond with a ver  | 
1563  |  |          * negotiation packet, and discard  | 
1564  |  |          * TODO(QUIC FUTURE): Rate limit the reception of these  | 
1565  |  |          */  | 
1566  | 0  |         port_send_version_negotiation(port, &e->peer, &hdr);  | 
1567  | 0  |         goto undesirable;  | 
1568  | 0  |     }  | 
1569  |  |  | 
1570  |  |     /*  | 
1571  |  |      * We only care about Initial packets which might be trying to establish a  | 
1572  |  |      * connection.  | 
1573  |  |      */  | 
1574  | 0  |     if (hdr.type != QUIC_PKT_TYPE_INITIAL)  | 
1575  | 0  |         goto undesirable;  | 
1576  |  |  | 
1577  | 0  |     odcid.id_len = 0;  | 
1578  |  |  | 
1579  |  |     /*  | 
1580  |  |      * Create qrx now so we can check integrity of packet  | 
1581  |  |      * which does not belong to any channel.  | 
1582  |  |      */  | 
1583  | 0  |     qrx_args.libctx             = port->engine->libctx;  | 
1584  | 0  |     qrx_args.demux              = port->demux;  | 
1585  | 0  |     qrx_args.short_conn_id_len  = dcid->id_len;  | 
1586  | 0  |     qrx_args.max_deferred       = 32;  | 
1587  | 0  |     qrx = ossl_qrx_new(&qrx_args);  | 
1588  | 0  |     if (qrx == NULL)  | 
1589  | 0  |         goto undesirable;  | 
1590  |  |  | 
1591  |  |     /*  | 
1592  |  |      * Derive secrets for qrx only.  | 
1593  |  |      */  | 
1594  | 0  |     if (!ossl_quic_provide_initial_secret(port->engine->libctx,  | 
1595  | 0  |                                           port->engine->propq,  | 
1596  | 0  |                                           &hdr.dst_conn_id,  | 
1597  | 0  |                                           /* is_server */ 1,  | 
1598  | 0  |                                           qrx, NULL))  | 
1599  | 0  |         goto undesirable;  | 
1600  |  |  | 
1601  | 0  |     if (ossl_qrx_validate_initial_packet(qrx, e, (const QUIC_CONN_ID *)dcid) == 0)  | 
1602  | 0  |         goto undesirable;  | 
1603  |  |  | 
1604  | 0  |     if (port->validate_addr == 0) { | 
1605  |  |         /*  | 
1606  |  |          * Forget qrx, because it becomes (almost) useless here. We must let  | 
1607  |  |          * channel to create a new QRX for connection ID server chooses. The  | 
1608  |  |          * validation keys for new DCID will be derived by  | 
1609  |  |          * ossl_quic_channel_on_new_conn() when we will be creating channel.  | 
1610  |  |          * See RFC 9000 section 7.2 negotiating connection id to better  | 
1611  |  |          * understand what's going on here.  | 
1612  |  |          *  | 
1613  |  |          * Did we say qrx is almost useless? Why? Because qrx remembers packets  | 
1614  |  |          * we just validated. Those packets must be injected to channel we are  | 
1615  |  |          * going to create. We use qrx_src alias so we can read packets from  | 
1616  |  |          * qrx and inject them to channel.  | 
1617  |  |          */  | 
1618  | 0  |          qrx_src = qrx;  | 
1619  | 0  |          qrx = NULL;  | 
1620  | 0  |     }  | 
1621  |  |     /*  | 
1622  |  |      * TODO(QUIC FUTURE): there should be some logic similar to accounting half-open  | 
1623  |  |      * states in TCP. If we reach certain threshold, then we want to  | 
1624  |  |      * validate clients.  | 
1625  |  |      */  | 
1626  | 0  |     if (port->validate_addr == 1 && hdr.token == NULL) { | 
1627  | 0  |         port_send_retry(port, &e->peer, &hdr);  | 
1628  | 0  |         goto undesirable;  | 
1629  | 0  |     }  | 
1630  |  |  | 
1631  |  |     /*  | 
1632  |  |      * Note, even if we don't enforce the sending of retry frames for  | 
1633  |  |      * server address validation, we may still get a token if we sent  | 
1634  |  |      * a NEW_TOKEN frame during a prior connection, which we should still  | 
1635  |  |      * validate here  | 
1636  |  |      */  | 
1637  | 0  |     if (hdr.token != NULL  | 
1638  | 0  |         && port_validate_token(&hdr, port, &e->peer,  | 
1639  | 0  |                                &odcid, &scid,  | 
1640  | 0  |                                &gen_new_token) == 0) { | 
1641  |  |         /*  | 
1642  |  |          * RFC 9000 s 8.1.3  | 
1643  |  |          * When a server receives an Initial packet with an address  | 
1644  |  |          * validation token, it MUST attempt to validate the token,  | 
1645  |  |          * unless it has already completed address validation.  | 
1646  |  |          * If the token is invalid, then the server SHOULD proceed as  | 
1647  |  |          * if the client did not have a validated address,  | 
1648  |  |          * including potentially sending a Retry packet  | 
1649  |  |          * Note: If address validation is disabled, just act like  | 
1650  |  |          * the request is valid  | 
1651  |  |          */  | 
1652  | 0  |         if (port->validate_addr == 1) { | 
1653  |  |             /*  | 
1654  |  |              * Again: we should consider saving initial encryption level  | 
1655  |  |              * secrets to token here to save some CPU cycles.  | 
1656  |  |              */  | 
1657  | 0  |             port_send_retry(port, &e->peer, &hdr);  | 
1658  | 0  |             goto undesirable;  | 
1659  | 0  |         }  | 
1660  |  |  | 
1661  |  |         /*  | 
1662  |  |          * client is under amplification limit, until it completes  | 
1663  |  |          * handshake.  | 
1664  |  |          *  | 
1665  |  |          * forget qrx so channel can create a new one  | 
1666  |  |          * with valid initial encryption level keys.  | 
1667  |  |          */  | 
1668  | 0  |         qrx_src = qrx;  | 
1669  | 0  |         qrx = NULL;  | 
1670  | 0  |     }  | 
1671  |  |  | 
1672  | 0  |     port_bind_channel(port, &e->peer, &scid, &hdr.dst_conn_id,  | 
1673  | 0  |                       &odcid, qrx, &new_ch);  | 
1674  |  |  | 
1675  |  |     /*  | 
1676  |  |      * if packet validates it gets moved to channel, we've just bound  | 
1677  |  |      * to port.  | 
1678  |  |      */  | 
1679  | 0  |     if (new_ch == NULL)  | 
1680  | 0  |         goto undesirable;  | 
1681  |  |  | 
1682  |  |     /*  | 
1683  |  |      * Generate a token for sending in a later NEW_TOKEN frame  | 
1684  |  |      */  | 
1685  | 0  |     if (gen_new_token == 1)  | 
1686  | 0  |         generate_new_token(new_ch, &e->peer);  | 
1687  |  | 
  | 
1688  | 0  |     if (qrx != NULL) { | 
1689  |  |         /*  | 
1690  |  |          * The qrx belongs to channel now, so don't free it.  | 
1691  |  |          */  | 
1692  | 0  |         qrx = NULL;  | 
1693  | 0  |     } else { | 
1694  |  |         /*  | 
1695  |  |          * We still need to salvage packets from almost forgotten qrx  | 
1696  |  |          * and pass them to channel.  | 
1697  |  |          */  | 
1698  | 0  |         while (ossl_qrx_read_pkt(qrx_src, &qrx_pkt) == 1)  | 
1699  | 0  |             ossl_quic_channel_inject_pkt(new_ch, qrx_pkt);  | 
1700  | 0  |         ossl_qrx_update_pn_space(qrx_src, new_ch->qrx);  | 
1701  | 0  |     }  | 
1702  |  |  | 
1703  |  |     /*  | 
1704  |  |      * If function reaches this place, then packet got validated in  | 
1705  |  |      * ossl_qrx_validate_initial_packet(). Keep in mind the function  | 
1706  |  |      * ossl_qrx_validate_initial_packet() decrypts the packet to validate it.  | 
1707  |  |      * If packet validation was successful (and it was because we are here),  | 
1708  |  |      * then the function puts the packet to qrx->rx_pending. We must not call  | 
1709  |  |      * ossl_qrx_inject_urxe() here now, because we don't want to insert  | 
1710  |  |      * the packet to qrx->urx_pending which keeps packet waiting for decryption.  | 
1711  |  |      *  | 
1712  |  |      * We are going to call ossl_quic_demux_release_urxe() to dispose buffer  | 
1713  |  |      * which still holds encrypted data.  | 
1714  |  |      */  | 
1715  |  | 
  | 
1716  | 0  | undesirable:  | 
1717  | 0  |     ossl_qrx_free(qrx);  | 
1718  | 0  |     ossl_qrx_free(qrx_src);  | 
1719  | 0  |     ossl_quic_demux_release_urxe(port->demux, e);  | 
1720  | 0  | }  | 
1721  |  |  | 
1722  |  | void ossl_quic_port_raise_net_error(QUIC_PORT *port,  | 
1723  |  |                                     QUIC_CHANNEL *triggering_ch)  | 
1724  | 0  | { | 
1725  | 0  |     QUIC_CHANNEL *ch;  | 
1726  |  | 
  | 
1727  | 0  |     if (!ossl_quic_port_is_running(port))  | 
1728  | 0  |         return;  | 
1729  |  |  | 
1730  |  |     /*  | 
1731  |  |      * Immediately capture any triggering error on the error stack, with a  | 
1732  |  |      * cover error.  | 
1733  |  |      */  | 
1734  | 0  |     ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,  | 
1735  | 0  |                    "port failed due to network BIO I/O error");  | 
1736  | 0  |     OSSL_ERR_STATE_save(port->err_state);  | 
1737  |  | 
  | 
1738  | 0  |     port_transition_failed(port);  | 
1739  |  |  | 
1740  |  |     /* Give the triggering channel (if any) the first notification. */  | 
1741  | 0  |     if (triggering_ch != NULL)  | 
1742  | 0  |         ossl_quic_channel_raise_net_error(triggering_ch);  | 
1743  |  | 
  | 
1744  | 0  |     OSSL_LIST_FOREACH(ch, ch, &port->channel_list)  | 
1745  | 0  |         if (ch != triggering_ch)  | 
1746  | 0  |             ossl_quic_channel_raise_net_error(ch);  | 
1747  | 0  | }  | 
1748  |  |  | 
1749  |  | void ossl_quic_port_restore_err_state(const QUIC_PORT *port)  | 
1750  | 0  | { | 
1751  | 0  |     ERR_clear_error();  | 
1752  | 0  |     OSSL_ERR_STATE_restore(port->err_state);  | 
1753  | 0  | }  |