]>
Commit | Line | Data |
---|---|---|
f538b421 HL |
1 | /* |
2 | * Copyright 2022 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 | ||
9c3ea4e1 TM |
10 | #include <openssl/rand.h> |
11 | #include <openssl/err.h> | |
f538b421 HL |
12 | #include "internal/quic_channel.h" |
13 | #include "internal/quic_error.h" | |
14 | #include "internal/quic_rx_depack.h" | |
15 | #include "../ssl_local.h" | |
16 | #include "quic_channel_local.h" | |
f538b421 | 17 | |
b1b06da2 HL |
18 | /* |
19 | * NOTE: While this channel implementation currently has basic server support, | |
20 | * this functionality has been implemented for internal testing purposes and is | |
21 | * not suitable for network use. In particular, it does not implement address | |
22 | * validation, anti-amplification or retry logic. | |
23 | * | |
44cb36d0 TM |
24 | * TODO(QUIC SERVER): Implement address validation and anti-amplification |
25 | * TODO(QUIC SERVER): Implement retry logic | |
b1b06da2 HL |
26 | */ |
27 | ||
f538b421 HL |
28 | #define INIT_DCID_LEN 8 |
29 | #define INIT_CRYPTO_BUF_LEN 8192 | |
30 | #define INIT_APP_BUF_LEN 8192 | |
31 | ||
9cf091a3 HL |
32 | /* |
33 | * Interval before we force a PING to ensure NATs don't timeout. This is based | |
0815b725 | 34 | * on the lowest commonly seen value of 30 seconds as cited in RFC 9000 s. |
9cf091a3 HL |
35 | * 10.1.2. |
36 | */ | |
37 | #define MAX_NAT_INTERVAL (ossl_ms2time(25000)) | |
38 | ||
f13868de HL |
39 | /* |
40 | * Our maximum ACK delay on the TX side. This is up to us to choose. Note that | |
41 | * this could differ from QUIC_DEFAULT_MAX_DELAY in future as that is a protocol | |
42 | * value which determines the value of the maximum ACK delay if the | |
43 | * max_ack_delay transport parameter is not set. | |
44 | */ | |
45 | #define DEFAULT_MAX_ACK_DELAY QUIC_DEFAULT_MAX_ACK_DELAY | |
46 | ||
3bf4dc8c | 47 | static void ch_rx_pre(QUIC_CHANNEL *ch); |
f538b421 HL |
48 | static int ch_rx(QUIC_CHANNEL *ch); |
49 | static int ch_tx(QUIC_CHANNEL *ch); | |
ccd31037 | 50 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg, uint32_t flags); |
f538b421 HL |
51 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch); |
52 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch); | |
53 | static int ch_retry(QUIC_CHANNEL *ch, | |
54 | const unsigned char *retry_token, | |
55 | size_t retry_token_len, | |
56 | const QUIC_CONN_ID *retry_scid); | |
57 | static void ch_cleanup(QUIC_CHANNEL *ch); | |
58 | static int ch_generate_transport_params(QUIC_CHANNEL *ch); | |
59 | static int ch_on_transport_params(const unsigned char *params, | |
60 | size_t params_len, | |
61 | void *arg); | |
62 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code); | |
63 | static int ch_on_handshake_complete(void *arg); | |
64 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
65 | uint32_t suite_id, EVP_MD *md, | |
66 | const unsigned char *secret, | |
67 | size_t secret_len, | |
68 | void *arg); | |
7257188b MC |
69 | static int ch_on_crypto_recv_record(const unsigned char **buf, |
70 | size_t *bytes_read, void *arg); | |
71 | static int ch_on_crypto_release_record(size_t bytes_read, void *arg); | |
f538b421 HL |
72 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream); |
73 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
74 | size_t *consumed, void *arg); | |
75 | static OSSL_TIME get_time(void *arg); | |
76 | static uint64_t get_stream_limit(int uni, void *arg); | |
dfe5e7fa | 77 | static int rx_late_validate(QUIC_PN pn, int pn_space, void *arg); |
8a65e7a5 | 78 | static void rxku_detected(QUIC_PN pn, void *arg); |
f538b421 HL |
79 | static int ch_retry(QUIC_CHANNEL *ch, |
80 | const unsigned char *retry_token, | |
81 | size_t retry_token_len, | |
82 | const QUIC_CONN_ID *retry_scid); | |
83 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
84 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
85 | uint32_t enc_level); | |
86 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch); | |
87 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
3b1ab5a3 | 88 | static void ch_update_ping_deadline(QUIC_CHANNEL *ch); |
cdd91631 | 89 | static void ch_stateless_reset(QUIC_CHANNEL *ch); |
df15e990 | 90 | static void ch_raise_net_error(QUIC_CHANNEL *ch); |
f538b421 HL |
91 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch); |
92 | static void ch_start_terminating(QUIC_CHANNEL *ch, | |
df15e990 HL |
93 | const QUIC_TERMINATE_CAUSE *tcause, |
94 | int force_immediate); | |
cdd91631 | 95 | static int ch_stateless_reset_token_handler(const unsigned char *data, size_t datalen, void *arg); |
b1b06da2 HL |
96 | static void ch_default_packet_handler(QUIC_URXE *e, void *arg); |
97 | static int ch_server_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer, | |
98 | const QUIC_CONN_ID *peer_scid, | |
99 | const QUIC_CONN_ID *peer_dcid); | |
8a65e7a5 HL |
100 | static void ch_on_txp_ack_tx(const OSSL_QUIC_FRAME_ACK *ack, uint32_t pn_space, |
101 | void *arg); | |
777a8a7f HL |
102 | static void ch_rx_handle_version_neg(QUIC_CHANNEL *ch, OSSL_QRX_PKT *pkt); |
103 | static void ch_raise_version_neg_failure(QUIC_CHANNEL *ch); | |
f538b421 | 104 | |
cdd91631 P |
105 | DEFINE_LHASH_OF_EX(QUIC_SRT_ELEM); |
106 | ||
f538b421 HL |
107 | static int gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len, QUIC_CONN_ID *cid) |
108 | { | |
109 | if (len > QUIC_MAX_CONN_ID_LEN) | |
110 | return 0; | |
111 | ||
112 | cid->id_len = (unsigned char)len; | |
113 | ||
114 | if (RAND_bytes_ex(libctx, cid->id, len, len * 8) != 1) { | |
96014840 | 115 | ERR_raise(ERR_LIB_SSL, ERR_R_RAND_LIB); |
f538b421 HL |
116 | cid->id_len = 0; |
117 | return 0; | |
118 | } | |
119 | ||
120 | return 1; | |
121 | } | |
122 | ||
cdd91631 P |
123 | static unsigned long chan_reset_token_hash(const QUIC_SRT_ELEM *a) |
124 | { | |
125 | unsigned long h; | |
126 | ||
127 | assert(sizeof(h) <= sizeof(a->token)); | |
128 | memcpy(&h, &a->token, sizeof(h)); | |
129 | return h; | |
130 | } | |
131 | ||
132 | static int chan_reset_token_cmp(const QUIC_SRT_ELEM *a, const QUIC_SRT_ELEM *b) | |
133 | { | |
134 | /* RFC 9000 s. 10.3.1: | |
135 | * When comparing a datagram to stateless reset token values, | |
136 | * endpoints MUST perform the comparison without leaking | |
137 | * information about the value of the token. For example, | |
138 | * performing this comparison in constant time protects the | |
139 | * value of individual stateless reset tokens from information | |
140 | * leakage through timing side channels. | |
141 | * | |
142 | * TODO(QUIC FUTURE): make this a memcmp when obfuscation is done and update | |
143 | * comment above. | |
144 | */ | |
145 | return CRYPTO_memcmp(&a->token, &b->token, sizeof(a->token)); | |
146 | } | |
147 | ||
148 | static int reset_token_obfuscate(QUIC_SRT_ELEM *out, const unsigned char *in) | |
149 | { | |
150 | /* | |
151 | * TODO(QUIC FUTURE): update this to AES encrypt the token in ECB mode with a | |
152 | * random (per channel) key. | |
153 | */ | |
154 | memcpy(&out->token, in, sizeof(out->token)); | |
155 | return 1; | |
156 | } | |
157 | ||
158 | /* | |
159 | * Add a stateless reset token to the channel | |
160 | */ | |
161 | static int chan_add_reset_token(QUIC_CHANNEL *ch, const unsigned char *new, | |
162 | uint64_t seq_num) | |
163 | { | |
164 | QUIC_SRT_ELEM *srte; | |
165 | int err; | |
166 | ||
167 | /* Add to list by sequence number (always the tail) */ | |
168 | if ((srte = OPENSSL_malloc(sizeof(*srte))) == NULL) | |
169 | return 0; | |
170 | ||
171 | ossl_list_stateless_reset_tokens_init_elem(srte); | |
172 | ossl_list_stateless_reset_tokens_insert_tail(&ch->srt_list_seq, srte); | |
173 | reset_token_obfuscate(srte, new); | |
174 | srte->seq_num = seq_num; | |
175 | ||
176 | lh_QUIC_SRT_ELEM_insert(ch->srt_hash_tok, srte); | |
177 | err = lh_QUIC_SRT_ELEM_error(ch->srt_hash_tok); | |
178 | if (err > 0) { | |
179 | ossl_list_stateless_reset_tokens_remove(&ch->srt_list_seq, srte); | |
180 | OPENSSL_free(srte); | |
181 | return 0; | |
182 | } | |
183 | return 1; | |
184 | } | |
185 | ||
186 | /* | |
187 | * Remove a stateless reset token from the channel | |
188 | * If the token isn't known, we just ignore the remove request which is safe. | |
189 | */ | |
190 | static void chan_remove_reset_token(QUIC_CHANNEL *ch, uint64_t seq_num) | |
191 | { | |
192 | QUIC_SRT_ELEM *srte; | |
193 | ||
194 | /* | |
195 | * Because the list is ordered and we only ever remove CIDs in order, | |
196 | * this loop should never iterate, but safer to provide the option. | |
197 | */ | |
198 | for (srte = ossl_list_stateless_reset_tokens_head(&ch->srt_list_seq); | |
199 | srte != NULL; | |
200 | srte = ossl_list_stateless_reset_tokens_next(srte)) { | |
201 | if (srte->seq_num > seq_num) | |
202 | return; | |
203 | if (srte->seq_num == seq_num) { | |
204 | ossl_list_stateless_reset_tokens_remove(&ch->srt_list_seq, srte); | |
205 | (void)lh_QUIC_SRT_ELEM_delete(ch->srt_hash_tok, srte); | |
206 | OPENSSL_free(srte); | |
207 | return; | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | /* | |
213 | * This is called by the demux whenever a new datagram arrives | |
214 | * | |
215 | * TODO(QUIC FUTURE): optimise this to only be called for unparsable packets | |
216 | */ | |
217 | static int ch_stateless_reset_token_handler(const unsigned char *data, | |
218 | size_t datalen, void *arg) | |
219 | { | |
220 | QUIC_SRT_ELEM srte; | |
221 | QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg; | |
222 | ||
223 | /* | |
224 | * Perform some fast and cheap checks for a packet not being a stateless | |
225 | * reset token. RFC 9000 s. 10.3 specifies this layout for stateless | |
226 | * reset packets: | |
227 | * | |
228 | * Stateless Reset { | |
229 | * Fixed Bits (2) = 1, | |
230 | * Unpredictable Bits (38..), | |
231 | * Stateless Reset Token (128), | |
232 | * } | |
233 | * | |
234 | * It also specifies: | |
235 | * However, endpoints MUST treat any packet ending in a valid | |
236 | * stateless reset token as a Stateless Reset, as other QUIC | |
237 | * versions might allow the use of a long header. | |
238 | * | |
239 | * We can rapidly check for the minimum length and that the first pair | |
240 | * of bits in the first byte are 01 or 11. | |
241 | * | |
242 | * The function returns 1 if it is a stateless reset packet, 0 if it isn't | |
243 | * and -1 if an error was encountered. | |
244 | */ | |
245 | if (datalen < QUIC_STATELESS_RESET_TOKEN_LEN + 5 || (0100 & *data) != 0100) | |
246 | return 0; | |
247 | memset(&srte, 0, sizeof(srte)); | |
248 | if (!reset_token_obfuscate(&srte, data + datalen - sizeof(srte.token))) | |
249 | return -1; | |
250 | return lh_QUIC_SRT_ELEM_retrieve(ch->srt_hash_tok, &srte) != NULL; | |
251 | } | |
252 | ||
f538b421 HL |
253 | /* |
254 | * QUIC Channel Initialization and Teardown | |
255 | * ======================================== | |
256 | */ | |
89b0948e HL |
257 | #define DEFAULT_INIT_CONN_RXFC_WND (768 * 1024) |
258 | #define DEFAULT_CONN_RXFC_MAX_WND_MUL 20 | |
0815b725 | 259 | |
89b0948e HL |
260 | #define DEFAULT_INIT_STREAM_RXFC_WND (512 * 1024) |
261 | #define DEFAULT_STREAM_RXFC_MAX_WND_MUL 12 | |
0815b725 | 262 | |
a6b6ea17 HL |
263 | #define DEFAULT_INIT_CONN_MAX_STREAMS 100 |
264 | ||
f538b421 HL |
265 | static int ch_init(QUIC_CHANNEL *ch) |
266 | { | |
267 | OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0}; | |
268 | OSSL_QTX_ARGS qtx_args = {0}; | |
269 | OSSL_QRX_ARGS qrx_args = {0}; | |
2723d705 | 270 | QUIC_TLS_ARGS tls_args = {0}; |
f538b421 | 271 | uint32_t pn_space; |
b1b06da2 | 272 | size_t rx_short_cid_len = ch->is_server ? INIT_DCID_LEN : 0; |
f538b421 | 273 | |
cdd91631 P |
274 | ossl_list_stateless_reset_tokens_init(&ch->srt_list_seq); |
275 | ch->srt_hash_tok = lh_QUIC_SRT_ELEM_new(&chan_reset_token_hash, | |
276 | &chan_reset_token_cmp); | |
277 | if (ch->srt_hash_tok == NULL) | |
278 | goto err; | |
279 | ||
b1b06da2 HL |
280 | /* For clients, generate our initial DCID. */ |
281 | if (!ch->is_server | |
282 | && !gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, &ch->init_dcid)) | |
f538b421 HL |
283 | goto err; |
284 | ||
285 | /* We plug in a network write BIO to the QTX later when we get one. */ | |
c2212dc1 | 286 | qtx_args.libctx = ch->libctx; |
f538b421 HL |
287 | qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN; |
288 | ch->rx_max_udp_payload_size = qtx_args.mdpl; | |
289 | ||
27195689 MC |
290 | ch->ping_deadline = ossl_time_infinite(); |
291 | ||
f538b421 HL |
292 | ch->qtx = ossl_qtx_new(&qtx_args); |
293 | if (ch->qtx == NULL) | |
294 | goto err; | |
295 | ||
296 | ch->txpim = ossl_quic_txpim_new(); | |
297 | if (ch->txpim == NULL) | |
298 | goto err; | |
299 | ||
300 | ch->cfq = ossl_quic_cfq_new(); | |
301 | if (ch->cfq == NULL) | |
302 | goto err; | |
303 | ||
304 | if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL)) | |
305 | goto err; | |
306 | ||
0815b725 HL |
307 | /* |
308 | * Note: The TP we transmit governs what the peer can transmit and thus | |
309 | * applies to the RXFC. | |
310 | */ | |
311 | ch->tx_init_max_stream_data_bidi_local = DEFAULT_INIT_STREAM_RXFC_WND; | |
312 | ch->tx_init_max_stream_data_bidi_remote = DEFAULT_INIT_STREAM_RXFC_WND; | |
313 | ch->tx_init_max_stream_data_uni = DEFAULT_INIT_STREAM_RXFC_WND; | |
314 | ||
f538b421 | 315 | if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL, |
0815b725 | 316 | DEFAULT_INIT_CONN_RXFC_WND, |
e8fe7a21 HL |
317 | DEFAULT_CONN_RXFC_MAX_WND_MUL * |
318 | DEFAULT_INIT_CONN_RXFC_WND, | |
b212d554 | 319 | get_time, ch)) |
f538b421 HL |
320 | goto err; |
321 | ||
098914d0 HL |
322 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) |
323 | if (!ossl_quic_rxfc_init_standalone(&ch->crypto_rxfc[pn_space], | |
324 | INIT_CRYPTO_BUF_LEN, | |
325 | get_time, ch)) | |
326 | goto err; | |
327 | ||
1051b4a0 HL |
328 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_bidi_rxfc, |
329 | DEFAULT_INIT_CONN_MAX_STREAMS, | |
330 | get_time, ch)) | |
a6b6ea17 HL |
331 | goto err; |
332 | ||
1051b4a0 HL |
333 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_uni_rxfc, |
334 | DEFAULT_INIT_CONN_MAX_STREAMS, | |
335 | get_time, ch)) | |
a6b6ea17 HL |
336 | goto err; |
337 | ||
f538b421 HL |
338 | if (!ossl_statm_init(&ch->statm)) |
339 | goto err; | |
340 | ||
341 | ch->have_statm = 1; | |
f6f45c55 | 342 | ch->cc_method = &ossl_cc_newreno_method; |
66ec5348 | 343 | if ((ch->cc_data = ch->cc_method->new(get_time, ch)) == NULL) |
f538b421 HL |
344 | goto err; |
345 | ||
b212d554 | 346 | if ((ch->ackm = ossl_ackm_new(get_time, ch, &ch->statm, |
f538b421 HL |
347 | ch->cc_method, ch->cc_data)) == NULL) |
348 | goto err; | |
349 | ||
a6b6ea17 HL |
350 | if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch, |
351 | &ch->max_streams_bidi_rxfc, | |
5915a900 HL |
352 | &ch->max_streams_uni_rxfc, |
353 | ch->is_server)) | |
f538b421 HL |
354 | goto err; |
355 | ||
356 | ch->have_qsm = 1; | |
357 | ||
358 | /* We use a zero-length SCID. */ | |
a6b6ea17 HL |
359 | txp_args.cur_dcid = ch->init_dcid; |
360 | txp_args.ack_delay_exponent = 3; | |
361 | txp_args.qtx = ch->qtx; | |
362 | txp_args.txpim = ch->txpim; | |
363 | txp_args.cfq = ch->cfq; | |
364 | txp_args.ackm = ch->ackm; | |
365 | txp_args.qsm = &ch->qsm; | |
366 | txp_args.conn_txfc = &ch->conn_txfc; | |
367 | txp_args.conn_rxfc = &ch->conn_rxfc; | |
368 | txp_args.max_streams_bidi_rxfc = &ch->max_streams_bidi_rxfc; | |
369 | txp_args.max_streams_uni_rxfc = &ch->max_streams_uni_rxfc; | |
370 | txp_args.cc_method = ch->cc_method; | |
371 | txp_args.cc_data = ch->cc_data; | |
372 | txp_args.now = get_time; | |
373 | txp_args.now_arg = ch; | |
45454ccc | 374 | |
f538b421 HL |
375 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { |
376 | ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_BUF_LEN); | |
377 | if (ch->crypto_send[pn_space] == NULL) | |
378 | goto err; | |
379 | ||
380 | txp_args.crypto[pn_space] = ch->crypto_send[pn_space]; | |
381 | } | |
382 | ||
383 | ch->txp = ossl_quic_tx_packetiser_new(&txp_args); | |
384 | if (ch->txp == NULL) | |
385 | goto err; | |
386 | ||
8a65e7a5 HL |
387 | ossl_quic_tx_packetiser_set_ack_tx_cb(ch->txp, ch_on_txp_ack_tx, ch); |
388 | ||
b1b06da2 HL |
389 | if ((ch->demux = ossl_quic_demux_new(/*BIO=*/NULL, |
390 | /*Short CID Len=*/rx_short_cid_len, | |
b212d554 | 391 | get_time, ch)) == NULL) |
f538b421 HL |
392 | goto err; |
393 | ||
cdd91631 P |
394 | /* |
395 | * Setup a handler to detect stateless reset tokens. | |
396 | */ | |
397 | ossl_quic_demux_set_stateless_reset_handler(ch->demux, | |
398 | &ch_stateless_reset_token_handler, | |
399 | ch); | |
400 | ||
b1b06da2 HL |
401 | /* |
402 | * If we are a server, setup our handler for packets not corresponding to | |
403 | * any known DCID on our end. This is for handling clients establishing new | |
404 | * connections. | |
405 | */ | |
406 | if (ch->is_server) | |
407 | ossl_quic_demux_set_default_handler(ch->demux, | |
408 | ch_default_packet_handler, | |
409 | ch); | |
410 | ||
c2212dc1 | 411 | qrx_args.libctx = ch->libctx; |
f538b421 | 412 | qrx_args.demux = ch->demux; |
b1b06da2 | 413 | qrx_args.short_conn_id_len = rx_short_cid_len; |
f538b421 HL |
414 | qrx_args.max_deferred = 32; |
415 | ||
416 | if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL) | |
417 | goto err; | |
418 | ||
dfe5e7fa HL |
419 | if (!ossl_qrx_set_late_validation_cb(ch->qrx, |
420 | rx_late_validate, | |
421 | ch)) | |
f538b421 HL |
422 | goto err; |
423 | ||
8a65e7a5 HL |
424 | if (!ossl_qrx_set_key_update_cb(ch->qrx, |
425 | rxku_detected, | |
426 | ch)) | |
427 | goto err; | |
428 | ||
b1b06da2 | 429 | if (!ch->is_server && !ossl_qrx_add_dst_conn_id(ch->qrx, &txp_args.cur_scid)) |
f538b421 HL |
430 | goto err; |
431 | ||
432 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
2113ea58 | 433 | ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL, 0); |
f538b421 HL |
434 | if (ch->crypto_recv[pn_space] == NULL) |
435 | goto err; | |
436 | } | |
437 | ||
2723d705 MC |
438 | /* Plug in the TLS handshake layer. */ |
439 | tls_args.s = ch->tls; | |
440 | tls_args.crypto_send_cb = ch_on_crypto_send; | |
441 | tls_args.crypto_send_cb_arg = ch; | |
7257188b MC |
442 | tls_args.crypto_recv_rcd_cb = ch_on_crypto_recv_record; |
443 | tls_args.crypto_recv_rcd_cb_arg = ch; | |
444 | tls_args.crypto_release_rcd_cb = ch_on_crypto_release_record; | |
445 | tls_args.crypto_release_rcd_cb_arg = ch; | |
2723d705 MC |
446 | tls_args.yield_secret_cb = ch_on_handshake_yield_secret; |
447 | tls_args.yield_secret_cb_arg = ch; | |
448 | tls_args.got_transport_params_cb = ch_on_transport_params; | |
449 | tls_args.got_transport_params_cb_arg= ch; | |
450 | tls_args.handshake_complete_cb = ch_on_handshake_complete; | |
451 | tls_args.handshake_complete_cb_arg = ch; | |
452 | tls_args.alert_cb = ch_on_handshake_alert; | |
453 | tls_args.alert_cb_arg = ch; | |
454 | tls_args.is_server = ch->is_server; | |
455 | ||
456 | if ((ch->qtls = ossl_quic_tls_new(&tls_args)) == NULL) | |
f538b421 HL |
457 | goto err; |
458 | ||
f13868de | 459 | ch->tx_max_ack_delay = DEFAULT_MAX_ACK_DELAY; |
4648eac5 HL |
460 | ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY; |
461 | ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP; | |
462 | ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT; | |
463 | ch->max_idle_timeout = QUIC_DEFAULT_IDLE_TIMEOUT; | |
464 | ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL; | |
465 | ch->rx_enc_level = QUIC_ENC_LEVEL_INITIAL; | |
16f3b542 | 466 | ch->txku_threshold_override = UINT64_MAX; |
4648eac5 | 467 | |
f13868de HL |
468 | ossl_ackm_set_tx_max_ack_delay(ch->ackm, ossl_ms2time(ch->tx_max_ack_delay)); |
469 | ossl_ackm_set_rx_max_ack_delay(ch->ackm, ossl_ms2time(ch->rx_max_ack_delay)); | |
470 | ||
f538b421 HL |
471 | /* |
472 | * Determine the QUIC Transport Parameters and serialize the transport | |
473 | * parameters block. (For servers, we do this later as we must defer | |
474 | * generation until we have received the client's transport parameters.) | |
475 | */ | |
476 | if (!ch->is_server && !ch_generate_transport_params(ch)) | |
477 | goto err; | |
478 | ||
f538b421 HL |
479 | ch_update_idle(ch); |
480 | ossl_quic_reactor_init(&ch->rtor, ch_tick, ch, | |
481 | ch_determine_next_tick_deadline(ch)); | |
482 | return 1; | |
483 | ||
484 | err: | |
485 | ch_cleanup(ch); | |
486 | return 0; | |
487 | } | |
488 | ||
489 | static void ch_cleanup(QUIC_CHANNEL *ch) | |
490 | { | |
cdd91631 | 491 | QUIC_SRT_ELEM *srte, *srte_next; |
f538b421 HL |
492 | uint32_t pn_space; |
493 | ||
494 | if (ch->ackm != NULL) | |
495 | for (pn_space = QUIC_PN_SPACE_INITIAL; | |
496 | pn_space < QUIC_PN_SPACE_NUM; | |
497 | ++pn_space) | |
498 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
499 | ||
500 | ossl_quic_tx_packetiser_free(ch->txp); | |
501 | ossl_quic_txpim_free(ch->txpim); | |
502 | ossl_quic_cfq_free(ch->cfq); | |
503 | ossl_qtx_free(ch->qtx); | |
504 | if (ch->cc_data != NULL) | |
505 | ch->cc_method->free(ch->cc_data); | |
506 | if (ch->have_statm) | |
507 | ossl_statm_destroy(&ch->statm); | |
508 | ossl_ackm_free(ch->ackm); | |
509 | ||
f538b421 HL |
510 | if (ch->have_qsm) |
511 | ossl_quic_stream_map_cleanup(&ch->qsm); | |
512 | ||
513 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
514 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
515 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
516 | } | |
517 | ||
518 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
519 | ch->qrx_pkt = NULL; | |
520 | ||
2723d705 | 521 | ossl_quic_tls_free(ch->qtls); |
f538b421 HL |
522 | ossl_qrx_free(ch->qrx); |
523 | ossl_quic_demux_free(ch->demux); | |
524 | OPENSSL_free(ch->local_transport_params); | |
40c8c756 | 525 | OPENSSL_free((char *)ch->terminate_cause.reason); |
9c3ea4e1 | 526 | OSSL_ERR_STATE_free(ch->err_state); |
cdd91631 P |
527 | |
528 | /* Free the stateless reset tokens */ | |
529 | for (srte = ossl_list_stateless_reset_tokens_head(&ch->srt_list_seq); | |
530 | srte != NULL; | |
531 | srte = srte_next) { | |
532 | srte_next = ossl_list_stateless_reset_tokens_next(srte); | |
533 | ossl_list_stateless_reset_tokens_remove(&ch->srt_list_seq, srte); | |
534 | (void)lh_QUIC_SRT_ELEM_delete(ch->srt_hash_tok, srte); | |
535 | OPENSSL_free(srte); | |
536 | } | |
537 | lh_QUIC_SRT_ELEM_free(ch->srt_hash_tok); | |
f538b421 HL |
538 | } |
539 | ||
540 | QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args) | |
541 | { | |
542 | QUIC_CHANNEL *ch = NULL; | |
543 | ||
544 | if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL) | |
545 | return NULL; | |
546 | ||
5cf99b40 MC |
547 | ch->libctx = args->libctx; |
548 | ch->propq = args->propq; | |
549 | ch->is_server = args->is_server; | |
550 | ch->tls = args->tls; | |
551 | ch->mutex = args->mutex; | |
552 | ch->now_cb = args->now_cb; | |
553 | ch->now_cb_arg = args->now_cb_arg; | |
f538b421 HL |
554 | |
555 | if (!ch_init(ch)) { | |
556 | OPENSSL_free(ch); | |
557 | return NULL; | |
558 | } | |
559 | ||
560 | return ch; | |
561 | } | |
562 | ||
563 | void ossl_quic_channel_free(QUIC_CHANNEL *ch) | |
564 | { | |
565 | if (ch == NULL) | |
566 | return; | |
567 | ||
568 | ch_cleanup(ch); | |
569 | OPENSSL_free(ch); | |
570 | } | |
571 | ||
14e31409 MC |
572 | /* Set mutator callbacks for test framework support */ |
573 | int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch, | |
574 | ossl_mutate_packet_cb mutatecb, | |
575 | ossl_finish_mutate_cb finishmutatecb, | |
576 | void *mutatearg) | |
577 | { | |
578 | if (ch->qtx == NULL) | |
579 | return 0; | |
580 | ||
581 | ossl_qtx_set_mutator(ch->qtx, mutatecb, finishmutatecb, mutatearg); | |
582 | return 1; | |
583 | } | |
584 | ||
f538b421 HL |
585 | int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr) |
586 | { | |
617b459d HL |
587 | if (!ch->addressed_mode) |
588 | return 0; | |
589 | ||
f538b421 HL |
590 | *peer_addr = ch->cur_peer_addr; |
591 | return 1; | |
592 | } | |
593 | ||
594 | int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr) | |
595 | { | |
617b459d HL |
596 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) |
597 | return 0; | |
598 | ||
599 | if (peer_addr == NULL || BIO_ADDR_family(peer_addr) == AF_UNSPEC) { | |
600 | BIO_ADDR_clear(&ch->cur_peer_addr); | |
601 | ch->addressed_mode = 0; | |
602 | return 1; | |
603 | } | |
604 | ||
605 | ch->cur_peer_addr = *peer_addr; | |
606 | ch->addressed_mode = 1; | |
f538b421 HL |
607 | return 1; |
608 | } | |
609 | ||
610 | QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch) | |
611 | { | |
612 | return &ch->rtor; | |
613 | } | |
614 | ||
615 | QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch) | |
616 | { | |
617 | return &ch->qsm; | |
618 | } | |
619 | ||
620 | OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch) | |
621 | { | |
622 | return &ch->statm; | |
623 | } | |
624 | ||
625 | QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch, | |
626 | uint64_t stream_id) | |
627 | { | |
628 | return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id); | |
629 | } | |
630 | ||
631 | int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch) | |
632 | { | |
633 | return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE; | |
634 | } | |
635 | ||
afe4a797 | 636 | static int ossl_quic_channel_is_closing(const QUIC_CHANNEL *ch) |
f538b421 | 637 | { |
afe4a797 P |
638 | return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING; |
639 | } | |
c12e1113 | 640 | |
afe4a797 P |
641 | static int ossl_quic_channel_is_draining(const QUIC_CHANNEL *ch) |
642 | { | |
643 | return ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING; | |
f538b421 HL |
644 | } |
645 | ||
afe4a797 | 646 | static int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch) |
f538b421 | 647 | { |
afe4a797 P |
648 | return ossl_quic_channel_is_closing(ch) |
649 | || ossl_quic_channel_is_draining(ch); | |
650 | } | |
c12e1113 | 651 | |
afe4a797 P |
652 | int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch) |
653 | { | |
654 | return ch->state == QUIC_CHANNEL_STATE_TERMINATED; | |
f538b421 HL |
655 | } |
656 | ||
c12e1113 MC |
657 | int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch) |
658 | { | |
659 | return ossl_quic_channel_is_terminating(ch) | |
660 | || ossl_quic_channel_is_terminated(ch); | |
661 | } | |
662 | ||
723cbe8a HL |
663 | const QUIC_TERMINATE_CAUSE * |
664 | ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch) | |
f538b421 | 665 | { |
723cbe8a | 666 | return ossl_quic_channel_is_term_any(ch) ? &ch->terminate_cause : NULL; |
f538b421 HL |
667 | } |
668 | ||
669 | int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch) | |
670 | { | |
671 | return ch->handshake_complete; | |
672 | } | |
673 | ||
ce8f20b6 MC |
674 | int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch) |
675 | { | |
676 | return ch->handshake_confirmed; | |
677 | } | |
678 | ||
553a4e00 HL |
679 | QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch) |
680 | { | |
681 | return ch->demux; | |
682 | } | |
683 | ||
fb2245c4 HL |
684 | CRYPTO_MUTEX *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch) |
685 | { | |
686 | return ch->mutex; | |
687 | } | |
688 | ||
9280d26a HL |
689 | int ossl_quic_channel_has_pending(const QUIC_CHANNEL *ch) |
690 | { | |
691 | return ossl_quic_demux_has_pending(ch->demux) | |
692 | || ossl_qrx_processed_read_pending(ch->qrx); | |
693 | } | |
694 | ||
f538b421 HL |
695 | /* |
696 | * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components | |
697 | * ================================================================ | |
698 | */ | |
699 | ||
700 | /* Used by various components. */ | |
701 | static OSSL_TIME get_time(void *arg) | |
702 | { | |
b212d554 HL |
703 | QUIC_CHANNEL *ch = arg; |
704 | ||
705 | if (ch->now_cb == NULL) | |
706 | return ossl_time_now(); | |
707 | ||
708 | return ch->now_cb(ch->now_cb_arg); | |
f538b421 HL |
709 | } |
710 | ||
711 | /* Used by QSM. */ | |
712 | static uint64_t get_stream_limit(int uni, void *arg) | |
713 | { | |
714 | QUIC_CHANNEL *ch = arg; | |
715 | ||
716 | return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi; | |
717 | } | |
718 | ||
719 | /* | |
720 | * Called by QRX to determine if a packet is potentially invalid before trying | |
721 | * to decrypt it. | |
722 | */ | |
dfe5e7fa | 723 | static int rx_late_validate(QUIC_PN pn, int pn_space, void *arg) |
f538b421 HL |
724 | { |
725 | QUIC_CHANNEL *ch = arg; | |
726 | ||
727 | /* Potential duplicates should not be processed. */ | |
728 | if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space)) | |
729 | return 0; | |
730 | ||
731 | return 1; | |
732 | } | |
733 | ||
8a65e7a5 HL |
734 | /* |
735 | * Triggers a TXKU (whether spontaneous or solicited). Does not check whether | |
736 | * spontaneous TXKU is currently allowed. | |
737 | */ | |
738 | QUIC_NEEDS_LOCK | |
739 | static void ch_trigger_txku(QUIC_CHANNEL *ch) | |
740 | { | |
741 | uint64_t next_pn | |
742 | = ossl_quic_tx_packetiser_get_next_pn(ch->txp, QUIC_PN_SPACE_APP); | |
743 | ||
744 | if (!ossl_quic_pn_valid(next_pn) | |
745 | || !ossl_qtx_trigger_key_update(ch->qtx)) { | |
746 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
747 | "key update"); | |
748 | return; | |
749 | } | |
750 | ||
751 | ch->txku_in_progress = 1; | |
752 | ch->txku_pn = next_pn; | |
753 | ch->rxku_expected = ch->ku_locally_initiated; | |
754 | } | |
755 | ||
756 | QUIC_NEEDS_LOCK | |
757 | static int txku_in_progress(QUIC_CHANNEL *ch) | |
758 | { | |
759 | if (ch->txku_in_progress | |
760 | && ossl_ackm_get_largest_acked(ch->ackm, QUIC_PN_SPACE_APP) >= ch->txku_pn) { | |
761 | OSSL_TIME pto = ossl_ackm_get_pto_duration(ch->ackm); | |
762 | ||
763 | /* | |
764 | * RFC 9001 s. 6.5: Endpoints SHOULD wait three times the PTO before | |
765 | * initiating a key update after receiving an acknowledgment that | |
766 | * confirms that the previous key update was received. | |
767 | * | |
768 | * Note that by the above wording, this period starts from when we get | |
769 | * the ack for a TXKU-triggering packet, not when the TXKU is initiated. | |
770 | * So we defer TXKU cooldown deadline calculation to this point. | |
771 | */ | |
772 | ch->txku_in_progress = 0; | |
773 | ch->txku_cooldown_deadline = ossl_time_add(get_time(ch), | |
774 | ossl_time_multiply(pto, 3)); | |
775 | } | |
776 | ||
777 | return ch->txku_in_progress; | |
778 | } | |
779 | ||
780 | QUIC_NEEDS_LOCK | |
781 | static int txku_allowed(QUIC_CHANNEL *ch) | |
782 | { | |
783 | return ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT /* Sanity check. */ | |
784 | /* Strict RFC 9001 criterion for TXKU. */ | |
785 | && ch->handshake_confirmed | |
786 | && !txku_in_progress(ch); | |
787 | } | |
788 | ||
789 | QUIC_NEEDS_LOCK | |
790 | static int txku_recommendable(QUIC_CHANNEL *ch) | |
791 | { | |
792 | if (!txku_allowed(ch)) | |
793 | return 0; | |
794 | ||
795 | return | |
796 | /* Recommended RFC 9001 criterion for TXKU. */ | |
797 | ossl_time_compare(get_time(ch), ch->txku_cooldown_deadline) >= 0 | |
798 | /* Some additional sensible criteria. */ | |
799 | && !ch->rxku_in_progress | |
800 | && !ch->rxku_pending_confirm; | |
801 | } | |
802 | ||
803 | QUIC_NEEDS_LOCK | |
804 | static int txku_desirable(QUIC_CHANNEL *ch) | |
805 | { | |
16f3b542 | 806 | uint64_t cur_pkt_count, max_pkt_count, thresh_pkt_count; |
8a65e7a5 HL |
807 | const uint32_t enc_level = QUIC_ENC_LEVEL_1RTT; |
808 | ||
809 | /* Check AEAD limit to determine if we should perform a spontaneous TXKU. */ | |
810 | cur_pkt_count = ossl_qtx_get_cur_epoch_pkt_count(ch->qtx, enc_level); | |
811 | max_pkt_count = ossl_qtx_get_max_epoch_pkt_count(ch->qtx, enc_level); | |
812 | ||
16f3b542 HL |
813 | thresh_pkt_count = max_pkt_count / 2; |
814 | if (ch->txku_threshold_override != UINT64_MAX) | |
815 | thresh_pkt_count = ch->txku_threshold_override; | |
816 | ||
817 | return cur_pkt_count >= thresh_pkt_count; | |
8a65e7a5 HL |
818 | } |
819 | ||
820 | QUIC_NEEDS_LOCK | |
821 | static void ch_maybe_trigger_spontaneous_txku(QUIC_CHANNEL *ch) | |
822 | { | |
823 | if (!txku_recommendable(ch) || !txku_desirable(ch)) | |
824 | return; | |
825 | ||
826 | ch->ku_locally_initiated = 1; | |
827 | ch_trigger_txku(ch); | |
828 | } | |
829 | ||
830 | QUIC_NEEDS_LOCK | |
831 | static int rxku_allowed(QUIC_CHANNEL *ch) | |
832 | { | |
833 | /* | |
834 | * RFC 9001 s. 6.1: An endpoint MUST NOT initiate a key update prior to | |
835 | * having confirmed the handshake (Section 4.1.2). | |
836 | * | |
837 | * RFC 9001 s. 6.1: An endpoint MUST NOT initiate a subsequent key update | |
838 | * unless it has received an acknowledgment for a packet that was sent | |
839 | * protected with keys from the current key phase. | |
840 | * | |
841 | * RFC 9001 s. 6.2: If an endpoint detects a second update before it has | |
842 | * sent any packets with updated keys containing an acknowledgment for the | |
843 | * packet that initiated the key update, it indicates that its peer has | |
844 | * updated keys twice without awaiting confirmation. An endpoint MAY treat | |
845 | * such consecutive key updates as a connection error of type | |
846 | * KEY_UPDATE_ERROR. | |
847 | */ | |
848 | return ch->handshake_confirmed && !ch->rxku_pending_confirm; | |
849 | } | |
850 | ||
851 | /* | |
852 | * Called when the QRX detects a new RX key update event. | |
853 | */ | |
854 | enum rxku_decision { | |
855 | DECISION_RXKU_ONLY, | |
856 | DECISION_PROTOCOL_VIOLATION, | |
857 | DECISION_SOLICITED_TXKU | |
858 | }; | |
859 | ||
860 | /* Called when the QRX detects a key update has occurred. */ | |
861 | QUIC_NEEDS_LOCK | |
862 | static void rxku_detected(QUIC_PN pn, void *arg) | |
863 | { | |
864 | QUIC_CHANNEL *ch = arg; | |
865 | enum rxku_decision decision; | |
866 | OSSL_TIME pto; | |
867 | ||
868 | /* | |
869 | * Note: rxku_in_progress is always 0 here as an RXKU cannot be detected | |
870 | * when we are still in UPDATING or COOLDOWN (see quic_record_rx.h). | |
871 | */ | |
872 | assert(!ch->rxku_in_progress); | |
873 | ||
874 | if (!rxku_allowed(ch)) | |
875 | /* Is RXKU even allowed at this time? */ | |
876 | decision = DECISION_PROTOCOL_VIOLATION; | |
877 | ||
878 | else if (ch->ku_locally_initiated) | |
879 | /* | |
880 | * If this key update was locally initiated (meaning that this detected | |
881 | * RXKU event is a result of our own spontaneous TXKU), we do not | |
882 | * trigger another TXKU; after all, to do so would result in an infinite | |
883 | * ping-pong of key updates. We still process it as an RXKU. | |
884 | */ | |
885 | decision = DECISION_RXKU_ONLY; | |
886 | ||
887 | else | |
888 | /* | |
889 | * Otherwise, a peer triggering a KU means we have to trigger a KU also. | |
890 | */ | |
891 | decision = DECISION_SOLICITED_TXKU; | |
892 | ||
893 | if (decision == DECISION_PROTOCOL_VIOLATION) { | |
894 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_KEY_UPDATE_ERROR, | |
895 | 0, "RX key update again too soon"); | |
896 | return; | |
897 | } | |
898 | ||
899 | pto = ossl_ackm_get_pto_duration(ch->ackm); | |
900 | ||
901 | ch->ku_locally_initiated = 0; | |
902 | ch->rxku_in_progress = 1; | |
903 | ch->rxku_pending_confirm = 1; | |
904 | ch->rxku_trigger_pn = pn; | |
905 | ch->rxku_update_end_deadline = ossl_time_add(get_time(ch), pto); | |
c93f7668 | 906 | ch->rxku_expected = 0; |
8a65e7a5 HL |
907 | |
908 | if (decision == DECISION_SOLICITED_TXKU) | |
909 | /* NOT gated by usual txku_allowed() */ | |
910 | ch_trigger_txku(ch); | |
37ba2bc7 HL |
911 | |
912 | /* | |
913 | * Ordinarily, we only generate ACK when some ACK-eliciting frame has been | |
914 | * received. In some cases, this may not occur for a long time, for example | |
915 | * if transmission of application data is going in only one direction and | |
916 | * nothing else is happening with the connection. However, since the peer | |
917 | * cannot initiate a subsequent (spontaneous) TXKU until its prior | |
692a3cab | 918 | * (spontaneous or solicited) TXKU has completed - meaning that prior |
37ba2bc7 HL |
919 | * TXKU's trigger packet (or subsequent packet) has been acknowledged, this |
920 | * can lead to very long times before a TXKU is considered 'completed'. | |
921 | * Optimise this by forcing ACK generation after triggering TXKU. | |
922 | * (Basically, we consider a RXKU event something that is 'ACK-eliciting', | |
923 | * which it more or less should be; it is necessarily separate from ordinary | |
924 | * processing of ACK-eliciting frames as key update is not indicated via a | |
925 | * frame.) | |
926 | */ | |
927 | ossl_quic_tx_packetiser_schedule_ack(ch->txp, QUIC_PN_SPACE_APP); | |
8a65e7a5 HL |
928 | } |
929 | ||
930 | /* Called per tick to handle RXKU timer events. */ | |
931 | QUIC_NEEDS_LOCK | |
932 | static void ch_rxku_tick(QUIC_CHANNEL *ch) | |
933 | { | |
934 | if (!ch->rxku_in_progress | |
935 | || ossl_time_compare(get_time(ch), ch->rxku_update_end_deadline) < 0) | |
936 | return; | |
937 | ||
938 | ch->rxku_update_end_deadline = ossl_time_infinite(); | |
939 | ch->rxku_in_progress = 0; | |
940 | ||
941 | if (!ossl_qrx_key_update_timeout(ch->qrx, /*normal=*/1)) | |
942 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
943 | "RXKU cooldown internal error"); | |
944 | } | |
945 | ||
946 | QUIC_NEEDS_LOCK | |
947 | static void ch_on_txp_ack_tx(const OSSL_QUIC_FRAME_ACK *ack, uint32_t pn_space, | |
948 | void *arg) | |
949 | { | |
950 | QUIC_CHANNEL *ch = arg; | |
951 | ||
952 | if (pn_space != QUIC_PN_SPACE_APP || !ch->rxku_pending_confirm | |
953 | || !ossl_quic_frame_ack_contains_pn(ack, ch->rxku_trigger_pn)) | |
954 | return; | |
955 | ||
956 | /* | |
957 | * Defer clearing rxku_pending_confirm until TXP generate call returns | |
958 | * successfully. | |
959 | */ | |
960 | ch->rxku_pending_confirm_done = 1; | |
961 | } | |
962 | ||
f538b421 HL |
963 | /* |
964 | * QUIC Channel: Handshake Layer Event Handling | |
965 | * ============================================ | |
966 | */ | |
967 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
968 | size_t *consumed, void *arg) | |
969 | { | |
970 | int ret; | |
971 | QUIC_CHANNEL *ch = arg; | |
972 | uint32_t enc_level = ch->tx_enc_level; | |
973 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
974 | QUIC_SSTREAM *sstream = ch->crypto_send[pn_space]; | |
975 | ||
976 | if (!ossl_assert(sstream != NULL)) | |
977 | return 0; | |
978 | ||
979 | ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed); | |
980 | return ret; | |
981 | } | |
982 | ||
983 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream) | |
984 | { | |
985 | size_t avail = 0; | |
986 | int is_fin = 0; | |
987 | ||
988 | if (rstream == NULL) | |
989 | return 1; | |
990 | ||
991 | if (!ossl_quic_rstream_available(rstream, &avail, &is_fin)) | |
992 | return 0; | |
993 | ||
994 | return avail == 0; | |
995 | } | |
996 | ||
7257188b MC |
997 | static int ch_on_crypto_recv_record(const unsigned char **buf, |
998 | size_t *bytes_read, void *arg) | |
f538b421 HL |
999 | { |
1000 | QUIC_CHANNEL *ch = arg; | |
1001 | QUIC_RSTREAM *rstream; | |
1002 | int is_fin = 0; /* crypto stream is never finished, so we don't use this */ | |
1003 | uint32_t i; | |
1004 | ||
1005 | /* | |
1006 | * After we move to a later EL we must not allow our peer to send any new | |
1007 | * bytes in the crypto stream on a previous EL. Retransmissions of old bytes | |
1008 | * are allowed. | |
1009 | * | |
1010 | * In practice we will only move to a new EL when we have consumed all bytes | |
1011 | * which should be sent on the crypto stream at a previous EL. For example, | |
1012 | * the Handshake EL should not be provisioned until we have completely | |
1013 | * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output | |
1014 | * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a | |
1015 | * given EL is available we simply ensure we have not received any further | |
1016 | * bytes at a lower EL. | |
1017 | */ | |
45ecfc9b | 1018 | for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->rx_enc_level; ++i) |
f538b421 HL |
1019 | if (i != QUIC_ENC_LEVEL_0RTT && |
1020 | !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { | |
1021 | /* Protocol violation (RFC 9001 s. 4.1.3) */ | |
1022 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
1023 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
1024 | "crypto stream data in wrong EL"); | |
1025 | return 0; | |
1026 | } | |
1027 | ||
45ecfc9b | 1028 | rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->rx_enc_level)]; |
f538b421 HL |
1029 | if (rstream == NULL) |
1030 | return 0; | |
1031 | ||
7257188b MC |
1032 | return ossl_quic_rstream_get_record(rstream, buf, bytes_read, |
1033 | &is_fin); | |
1034 | } | |
1035 | ||
1036 | static int ch_on_crypto_release_record(size_t bytes_read, void *arg) | |
1037 | { | |
1038 | QUIC_CHANNEL *ch = arg; | |
1039 | QUIC_RSTREAM *rstream; | |
098914d0 HL |
1040 | OSSL_RTT_INFO rtt_info; |
1041 | uint32_t rx_pn_space = ossl_quic_enc_level_to_pn_space(ch->rx_enc_level); | |
7257188b | 1042 | |
098914d0 | 1043 | rstream = ch->crypto_recv[rx_pn_space]; |
7257188b MC |
1044 | if (rstream == NULL) |
1045 | return 0; | |
1046 | ||
098914d0 HL |
1047 | ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ch), &rtt_info); |
1048 | if (!ossl_quic_rxfc_on_retire(&ch->crypto_rxfc[rx_pn_space], bytes_read, | |
1049 | rtt_info.smoothed_rtt)) | |
1050 | return 0; | |
1051 | ||
7257188b | 1052 | return ossl_quic_rstream_release_record(rstream, bytes_read); |
f538b421 HL |
1053 | } |
1054 | ||
1055 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
1056 | uint32_t suite_id, EVP_MD *md, | |
1057 | const unsigned char *secret, | |
1058 | size_t secret_len, | |
1059 | void *arg) | |
1060 | { | |
1061 | QUIC_CHANNEL *ch = arg; | |
1062 | uint32_t i; | |
1063 | ||
1064 | if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM) | |
1065 | /* Invalid EL. */ | |
1066 | return 0; | |
1067 | ||
f538b421 HL |
1068 | |
1069 | if (direction) { | |
1070 | /* TX */ | |
45ecfc9b MC |
1071 | if (enc_level <= ch->tx_enc_level) |
1072 | /* | |
9f0ade7c HL |
1073 | * Does not make sense for us to try and provision an EL we have already |
1074 | * attained. | |
1075 | */ | |
45ecfc9b MC |
1076 | return 0; |
1077 | ||
f538b421 HL |
1078 | if (!ossl_qtx_provide_secret(ch->qtx, enc_level, |
1079 | suite_id, md, | |
1080 | secret, secret_len)) | |
1081 | return 0; | |
1082 | ||
1083 | ch->tx_enc_level = enc_level; | |
1084 | } else { | |
1085 | /* RX */ | |
45ecfc9b MC |
1086 | if (enc_level <= ch->rx_enc_level) |
1087 | /* | |
9f0ade7c HL |
1088 | * Does not make sense for us to try and provision an EL we have already |
1089 | * attained. | |
1090 | */ | |
45ecfc9b MC |
1091 | return 0; |
1092 | ||
1093 | /* | |
9f0ade7c HL |
1094 | * Ensure all crypto streams for previous ELs are now empty of available |
1095 | * data. | |
1096 | */ | |
45ecfc9b | 1097 | for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i) |
e28f512f | 1098 | if (!crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { |
45ecfc9b MC |
1099 | /* Protocol violation (RFC 9001 s. 4.1.3) */ |
1100 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
1101 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
1102 | "crypto stream data in wrong EL"); | |
1103 | return 0; | |
1104 | } | |
1105 | ||
f538b421 HL |
1106 | if (!ossl_qrx_provide_secret(ch->qrx, enc_level, |
1107 | suite_id, md, | |
1108 | secret, secret_len)) | |
1109 | return 0; | |
92282a17 HL |
1110 | |
1111 | ch->have_new_rx_secret = 1; | |
45ecfc9b | 1112 | ch->rx_enc_level = enc_level; |
f538b421 HL |
1113 | } |
1114 | ||
1115 | return 1; | |
1116 | } | |
1117 | ||
1118 | static int ch_on_handshake_complete(void *arg) | |
1119 | { | |
1120 | QUIC_CHANNEL *ch = arg; | |
1121 | ||
e28f512f | 1122 | if (!ossl_assert(!ch->handshake_complete)) |
f538b421 HL |
1123 | return 0; /* this should not happen twice */ |
1124 | ||
1125 | if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT)) | |
1126 | return 0; | |
1127 | ||
62d0da12 | 1128 | if (!ch->got_remote_transport_params) { |
f538b421 HL |
1129 | /* |
1130 | * Was not a valid QUIC handshake if we did not get valid transport | |
1131 | * params. | |
1132 | */ | |
3ad5711e | 1133 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_MISSING_EXT, |
62d0da12 MC |
1134 | OSSL_QUIC_FRAME_TYPE_CRYPTO, |
1135 | "no transport parameters received"); | |
f538b421 | 1136 | return 0; |
62d0da12 | 1137 | } |
f538b421 HL |
1138 | |
1139 | /* Don't need transport parameters anymore. */ | |
1140 | OPENSSL_free(ch->local_transport_params); | |
1141 | ch->local_transport_params = NULL; | |
1142 | ||
2a6f1f2f HL |
1143 | /* Tell the QRX it can now process 1-RTT packets. */ |
1144 | ossl_qrx_allow_1rtt_processing(ch->qrx); | |
1145 | ||
f538b421 HL |
1146 | /* Tell TXP the handshake is complete. */ |
1147 | ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp); | |
1148 | ||
1149 | ch->handshake_complete = 1; | |
b1b06da2 HL |
1150 | |
1151 | if (ch->is_server) { | |
1152 | /* | |
1153 | * On the server, the handshake is confirmed as soon as it is complete. | |
1154 | */ | |
1155 | ossl_quic_channel_on_handshake_confirmed(ch); | |
1156 | ||
1157 | ossl_quic_tx_packetiser_schedule_handshake_done(ch->txp); | |
1158 | } | |
1159 | ||
f538b421 HL |
1160 | return 1; |
1161 | } | |
1162 | ||
1163 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code) | |
1164 | { | |
1165 | QUIC_CHANNEL *ch = arg; | |
1166 | ||
b644a932 MC |
1167 | /* |
1168 | * RFC 9001 s. 4.4: More specifically, servers MUST NOT send post-handshake | |
1169 | * TLS CertificateRequest messages, and clients MUST treat receipt of such | |
1170 | * messages as a connection error of type PROTOCOL_VIOLATION. | |
1171 | */ | |
04c7fb53 | 1172 | if (alert_code == SSL_AD_UNEXPECTED_MESSAGE |
b644a932 MC |
1173 | && ch->handshake_complete |
1174 | && ossl_quic_tls_is_cert_request(ch->qtls)) | |
1175 | ossl_quic_channel_raise_protocol_error(ch, | |
1176 | QUIC_ERR_PROTOCOL_VIOLATION, | |
1177 | 0, | |
1178 | "Post-handshake TLS " | |
1179 | "CertificateRequest received"); | |
04c7fb53 MC |
1180 | /* |
1181 | * RFC 9001 s. 4.6.1: Servers MUST NOT send the early_data extension with a | |
1182 | * max_early_data_size field set to any value other than 0xffffffff. A | |
1183 | * client MUST treat receipt of a NewSessionTicket that contains an | |
1184 | * early_data extension with any other value as a connection error of type | |
1185 | * PROTOCOL_VIOLATION. | |
1186 | */ | |
1187 | else if (alert_code == SSL_AD_ILLEGAL_PARAMETER | |
1188 | && ch->handshake_complete | |
1189 | && ossl_quic_tls_has_bad_max_early_data(ch->qtls)) | |
1190 | ossl_quic_channel_raise_protocol_error(ch, | |
1191 | QUIC_ERR_PROTOCOL_VIOLATION, | |
1192 | 0, | |
1193 | "Bad max_early_data received"); | |
b644a932 MC |
1194 | else |
1195 | ossl_quic_channel_raise_protocol_error(ch, | |
1196 | QUIC_ERR_CRYPTO_ERR_BEGIN | |
1197 | + alert_code, | |
1198 | 0, "handshake alert"); | |
1199 | ||
f538b421 HL |
1200 | return 1; |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * QUIC Channel: Transport Parameter Handling | |
1205 | * ========================================== | |
1206 | */ | |
1207 | ||
1208 | /* | |
1209 | * Called by handshake layer when we receive QUIC Transport Parameters from the | |
1210 | * peer. Note that these are not authenticated until the handshake is marked | |
1211 | * as complete. | |
1212 | */ | |
3c567a52 HL |
1213 | #define TP_REASON_SERVER_ONLY(x) \ |
1214 | x " may not be sent by a client" | |
1215 | #define TP_REASON_DUP(x) \ | |
1216 | x " appears multiple times" | |
1217 | #define TP_REASON_MALFORMED(x) \ | |
1218 | x " is malformed" | |
1219 | #define TP_REASON_EXPECTED_VALUE(x) \ | |
1220 | x " does not match expected value" | |
1221 | #define TP_REASON_NOT_RETRY(x) \ | |
1222 | x " sent when not performing a retry" | |
1223 | #define TP_REASON_REQUIRED(x) \ | |
1224 | x " was not sent but is required" | |
cdd91631 P |
1225 | #define TP_REASON_INTERNAL_ERROR(x) \ |
1226 | x " encountered internal error" | |
3c567a52 | 1227 | |
26ad16ea HL |
1228 | static void txfc_bump_cwm_bidi(QUIC_STREAM *s, void *arg) |
1229 | { | |
1230 | if (!ossl_quic_stream_is_bidi(s) | |
1231 | || ossl_quic_stream_is_server_init(s)) | |
1232 | return; | |
1233 | ||
1234 | ossl_quic_txfc_bump_cwm(&s->txfc, *(uint64_t *)arg); | |
1235 | } | |
1236 | ||
1237 | static void txfc_bump_cwm_uni(QUIC_STREAM *s, void *arg) | |
1238 | { | |
1239 | if (ossl_quic_stream_is_bidi(s) | |
1240 | || ossl_quic_stream_is_server_init(s)) | |
1241 | return; | |
1242 | ||
1243 | ossl_quic_txfc_bump_cwm(&s->txfc, *(uint64_t *)arg); | |
1244 | } | |
1245 | ||
1246 | static void do_update(QUIC_STREAM *s, void *arg) | |
1247 | { | |
1248 | QUIC_CHANNEL *ch = arg; | |
1249 | ||
1250 | ossl_quic_stream_map_update_state(&ch->qsm, s); | |
1251 | } | |
1252 | ||
f538b421 HL |
1253 | static int ch_on_transport_params(const unsigned char *params, |
1254 | size_t params_len, | |
1255 | void *arg) | |
1256 | { | |
1257 | QUIC_CHANNEL *ch = arg; | |
1258 | PACKET pkt; | |
1259 | uint64_t id, v; | |
1260 | size_t len; | |
1261 | const unsigned char *body; | |
1262 | int got_orig_dcid = 0; | |
1263 | int got_initial_scid = 0; | |
1264 | int got_retry_scid = 0; | |
1265 | int got_initial_max_data = 0; | |
1266 | int got_initial_max_stream_data_bidi_local = 0; | |
1267 | int got_initial_max_stream_data_bidi_remote = 0; | |
1268 | int got_initial_max_stream_data_uni = 0; | |
1269 | int got_initial_max_streams_bidi = 0; | |
1270 | int got_initial_max_streams_uni = 0; | |
1271 | int got_ack_delay_exp = 0; | |
1272 | int got_max_ack_delay = 0; | |
1273 | int got_max_udp_payload_size = 0; | |
1274 | int got_max_idle_timeout = 0; | |
1275 | int got_active_conn_id_limit = 0; | |
0911cb4a | 1276 | int got_disable_active_migration = 0; |
f538b421 | 1277 | QUIC_CONN_ID cid; |
3c567a52 | 1278 | const char *reason = "bad transport parameter"; |
f538b421 HL |
1279 | |
1280 | if (ch->got_remote_transport_params) | |
1281 | goto malformed; | |
1282 | ||
96014840 TM |
1283 | if (!PACKET_buf_init(&pkt, params, params_len)) { |
1284 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
1285 | "internal error (packet buf init)"); | |
f538b421 | 1286 | return 0; |
96014840 | 1287 | } |
f538b421 HL |
1288 | |
1289 | while (PACKET_remaining(&pkt) > 0) { | |
1290 | if (!ossl_quic_wire_peek_transport_param(&pkt, &id)) | |
1291 | goto malformed; | |
1292 | ||
1293 | switch (id) { | |
75b2920a | 1294 | case QUIC_TPARAM_ORIG_DCID: |
3c567a52 HL |
1295 | if (got_orig_dcid) { |
1296 | reason = TP_REASON_DUP("ORIG_DCID"); | |
1297 | goto malformed; | |
1298 | } | |
1299 | ||
1300 | if (ch->is_server) { | |
1301 | reason = TP_REASON_SERVER_ONLY("ORIG_DCID"); | |
75b2920a | 1302 | goto malformed; |
3c567a52 | 1303 | } |
75b2920a | 1304 | |
3c567a52 HL |
1305 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1306 | reason = TP_REASON_MALFORMED("ORIG_DCID"); | |
75b2920a | 1307 | goto malformed; |
3c567a52 | 1308 | } |
75b2920a HL |
1309 | |
1310 | /* Must match our initial DCID. */ | |
3c567a52 HL |
1311 | if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid)) { |
1312 | reason = TP_REASON_EXPECTED_VALUE("ORIG_DCID"); | |
75b2920a | 1313 | goto malformed; |
3c567a52 | 1314 | } |
75b2920a HL |
1315 | |
1316 | got_orig_dcid = 1; | |
1317 | break; | |
1318 | ||
1319 | case QUIC_TPARAM_RETRY_SCID: | |
3c567a52 HL |
1320 | if (ch->is_server) { |
1321 | reason = TP_REASON_SERVER_ONLY("RETRY_SCID"); | |
1322 | goto malformed; | |
1323 | } | |
1324 | ||
1325 | if (got_retry_scid) { | |
1326 | reason = TP_REASON_DUP("RETRY_SCID"); | |
75b2920a | 1327 | goto malformed; |
3c567a52 HL |
1328 | } |
1329 | ||
1330 | if (!ch->doing_retry) { | |
1331 | reason = TP_REASON_NOT_RETRY("RETRY_SCID"); | |
1332 | goto malformed; | |
1333 | } | |
75b2920a | 1334 | |
3c567a52 HL |
1335 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1336 | reason = TP_REASON_MALFORMED("RETRY_SCID"); | |
75b2920a | 1337 | goto malformed; |
3c567a52 | 1338 | } |
75b2920a HL |
1339 | |
1340 | /* Must match Retry packet SCID. */ | |
3c567a52 HL |
1341 | if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid)) { |
1342 | reason = TP_REASON_EXPECTED_VALUE("RETRY_SCID"); | |
75b2920a | 1343 | goto malformed; |
3c567a52 | 1344 | } |
75b2920a HL |
1345 | |
1346 | got_retry_scid = 1; | |
1347 | break; | |
1348 | ||
1349 | case QUIC_TPARAM_INITIAL_SCID: | |
3c567a52 | 1350 | if (got_initial_scid) { |
75b2920a | 1351 | /* must not appear more than once */ |
3c567a52 | 1352 | reason = TP_REASON_DUP("INITIAL_SCID"); |
75b2920a | 1353 | goto malformed; |
3c567a52 | 1354 | } |
75b2920a | 1355 | |
3c567a52 HL |
1356 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1357 | reason = TP_REASON_MALFORMED("INITIAL_SCID"); | |
75b2920a | 1358 | goto malformed; |
3c567a52 | 1359 | } |
75b2920a HL |
1360 | |
1361 | /* Must match SCID of first Initial packet from server. */ | |
3c567a52 HL |
1362 | if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) { |
1363 | reason = TP_REASON_EXPECTED_VALUE("INITIAL_SCID"); | |
75b2920a | 1364 | goto malformed; |
3c567a52 | 1365 | } |
75b2920a HL |
1366 | |
1367 | got_initial_scid = 1; | |
1368 | break; | |
1369 | ||
1370 | case QUIC_TPARAM_INITIAL_MAX_DATA: | |
3c567a52 | 1371 | if (got_initial_max_data) { |
75b2920a | 1372 | /* must not appear more than once */ |
3c567a52 | 1373 | reason = TP_REASON_DUP("INITIAL_MAX_DATA"); |
75b2920a | 1374 | goto malformed; |
3c567a52 | 1375 | } |
75b2920a | 1376 | |
3c567a52 HL |
1377 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1378 | reason = TP_REASON_MALFORMED("INITIAL_MAX_DATA"); | |
75b2920a | 1379 | goto malformed; |
3c567a52 | 1380 | } |
75b2920a HL |
1381 | |
1382 | ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v); | |
1383 | got_initial_max_data = 1; | |
1384 | break; | |
1385 | ||
1386 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: | |
3c567a52 | 1387 | if (got_initial_max_stream_data_bidi_local) { |
75b2920a | 1388 | /* must not appear more than once */ |
3c567a52 | 1389 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); |
75b2920a | 1390 | goto malformed; |
3c567a52 | 1391 | } |
75b2920a | 1392 | |
3c567a52 HL |
1393 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1394 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); | |
75b2920a | 1395 | goto malformed; |
3c567a52 | 1396 | } |
f538b421 HL |
1397 | |
1398 | /* | |
75b2920a HL |
1399 | * This is correct; the BIDI_LOCAL TP governs streams created by |
1400 | * the endpoint which sends the TP, i.e., our peer. | |
f538b421 | 1401 | */ |
54562e89 | 1402 | ch->rx_init_max_stream_data_bidi_remote = v; |
75b2920a HL |
1403 | got_initial_max_stream_data_bidi_local = 1; |
1404 | break; | |
1405 | ||
1406 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: | |
3c567a52 | 1407 | if (got_initial_max_stream_data_bidi_remote) { |
75b2920a | 1408 | /* must not appear more than once */ |
3c567a52 | 1409 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); |
75b2920a | 1410 | goto malformed; |
3c567a52 | 1411 | } |
75b2920a | 1412 | |
3c567a52 HL |
1413 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1414 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); | |
75b2920a | 1415 | goto malformed; |
3c567a52 | 1416 | } |
75b2920a HL |
1417 | |
1418 | /* | |
1419 | * This is correct; the BIDI_REMOTE TP governs streams created | |
1420 | * by the endpoint which receives the TP, i.e., us. | |
1421 | */ | |
54562e89 | 1422 | ch->rx_init_max_stream_data_bidi_local = v; |
75b2920a | 1423 | |
26ad16ea HL |
1424 | /* Apply to all existing streams. */ |
1425 | ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_bidi, &v); | |
75b2920a HL |
1426 | got_initial_max_stream_data_bidi_remote = 1; |
1427 | break; | |
1428 | ||
1429 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI: | |
3c567a52 | 1430 | if (got_initial_max_stream_data_uni) { |
75b2920a | 1431 | /* must not appear more than once */ |
3c567a52 | 1432 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_UNI"); |
75b2920a | 1433 | goto malformed; |
3c567a52 | 1434 | } |
75b2920a | 1435 | |
3c567a52 HL |
1436 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1437 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_UNI"); | |
75b2920a | 1438 | goto malformed; |
3c567a52 | 1439 | } |
75b2920a | 1440 | |
e8fe7a21 | 1441 | ch->rx_init_max_stream_data_uni = v; |
26ad16ea HL |
1442 | |
1443 | /* Apply to all existing streams. */ | |
1444 | ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_uni, &v); | |
75b2920a HL |
1445 | got_initial_max_stream_data_uni = 1; |
1446 | break; | |
1447 | ||
1448 | case QUIC_TPARAM_ACK_DELAY_EXP: | |
3c567a52 | 1449 | if (got_ack_delay_exp) { |
75b2920a | 1450 | /* must not appear more than once */ |
3c567a52 | 1451 | reason = TP_REASON_DUP("ACK_DELAY_EXP"); |
75b2920a | 1452 | goto malformed; |
3c567a52 | 1453 | } |
75b2920a HL |
1454 | |
1455 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
1456 | || v > QUIC_MAX_ACK_DELAY_EXP) { |
1457 | reason = TP_REASON_MALFORMED("ACK_DELAY_EXP"); | |
75b2920a | 1458 | goto malformed; |
3c567a52 | 1459 | } |
75b2920a HL |
1460 | |
1461 | ch->rx_ack_delay_exp = (unsigned char)v; | |
1462 | got_ack_delay_exp = 1; | |
1463 | break; | |
1464 | ||
1465 | case QUIC_TPARAM_MAX_ACK_DELAY: | |
3c567a52 | 1466 | if (got_max_ack_delay) { |
75b2920a | 1467 | /* must not appear more than once */ |
3c567a52 | 1468 | reason = TP_REASON_DUP("MAX_ACK_DELAY"); |
96014840 | 1469 | goto malformed; |
3c567a52 | 1470 | } |
75b2920a HL |
1471 | |
1472 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
1473 | || v >= (((uint64_t)1) << 14)) { |
1474 | reason = TP_REASON_MALFORMED("MAX_ACK_DELAY"); | |
75b2920a | 1475 | goto malformed; |
3c567a52 | 1476 | } |
75b2920a HL |
1477 | |
1478 | ch->rx_max_ack_delay = v; | |
f13868de HL |
1479 | ossl_ackm_set_rx_max_ack_delay(ch->ackm, |
1480 | ossl_ms2time(ch->rx_max_ack_delay)); | |
1481 | ||
75b2920a HL |
1482 | got_max_ack_delay = 1; |
1483 | break; | |
1484 | ||
1485 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI: | |
3c567a52 | 1486 | if (got_initial_max_streams_bidi) { |
75b2920a | 1487 | /* must not appear more than once */ |
3c567a52 | 1488 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_BIDI"); |
96014840 | 1489 | goto malformed; |
3c567a52 | 1490 | } |
75b2920a HL |
1491 | |
1492 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
1493 | || v > (((uint64_t)1) << 60)) { |
1494 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_BIDI"); | |
75b2920a | 1495 | goto malformed; |
3c567a52 | 1496 | } |
75b2920a HL |
1497 | |
1498 | assert(ch->max_local_streams_bidi == 0); | |
1499 | ch->max_local_streams_bidi = v; | |
1500 | got_initial_max_streams_bidi = 1; | |
1501 | break; | |
1502 | ||
1503 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI: | |
3c567a52 | 1504 | if (got_initial_max_streams_uni) { |
75b2920a | 1505 | /* must not appear more than once */ |
3c567a52 | 1506 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_UNI"); |
75b2920a | 1507 | goto malformed; |
3c567a52 | 1508 | } |
75b2920a HL |
1509 | |
1510 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
1511 | || v > (((uint64_t)1) << 60)) { |
1512 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_UNI"); | |
75b2920a | 1513 | goto malformed; |
3c567a52 | 1514 | } |
75b2920a HL |
1515 | |
1516 | assert(ch->max_local_streams_uni == 0); | |
1517 | ch->max_local_streams_uni = v; | |
1518 | got_initial_max_streams_uni = 1; | |
1519 | break; | |
1520 | ||
1521 | case QUIC_TPARAM_MAX_IDLE_TIMEOUT: | |
3c567a52 | 1522 | if (got_max_idle_timeout) { |
75b2920a | 1523 | /* must not appear more than once */ |
3c567a52 | 1524 | reason = TP_REASON_DUP("MAX_IDLE_TIMEOUT"); |
75b2920a | 1525 | goto malformed; |
3c567a52 | 1526 | } |
75b2920a | 1527 | |
3c567a52 HL |
1528 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1529 | reason = TP_REASON_MALFORMED("MAX_IDLE_TIMEOUT"); | |
75b2920a | 1530 | goto malformed; |
3c567a52 | 1531 | } |
75b2920a | 1532 | |
4648eac5 | 1533 | if (v > 0 && v < ch->max_idle_timeout) |
75b2920a HL |
1534 | ch->max_idle_timeout = v; |
1535 | ||
1536 | ch_update_idle(ch); | |
1537 | got_max_idle_timeout = 1; | |
1538 | break; | |
f538b421 | 1539 | |
75b2920a | 1540 | case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE: |
3c567a52 | 1541 | if (got_max_udp_payload_size) { |
75b2920a | 1542 | /* must not appear more than once */ |
3c567a52 | 1543 | reason = TP_REASON_DUP("MAX_UDP_PAYLOAD_SIZE"); |
75b2920a | 1544 | goto malformed; |
3c567a52 | 1545 | } |
f538b421 | 1546 | |
75b2920a | 1547 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
3c567a52 HL |
1548 | || v < QUIC_MIN_INITIAL_DGRAM_LEN) { |
1549 | reason = TP_REASON_MALFORMED("MAX_UDP_PAYLOAD_SIZE"); | |
75b2920a | 1550 | goto malformed; |
3c567a52 | 1551 | } |
75b2920a HL |
1552 | |
1553 | ch->rx_max_udp_payload_size = v; | |
1554 | got_max_udp_payload_size = 1; | |
1555 | break; | |
1556 | ||
1557 | case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT: | |
3c567a52 | 1558 | if (got_active_conn_id_limit) { |
75b2920a | 1559 | /* must not appear more than once */ |
3c567a52 | 1560 | reason = TP_REASON_DUP("ACTIVE_CONN_ID_LIMIT"); |
75b2920a | 1561 | goto malformed; |
3c567a52 | 1562 | } |
75b2920a HL |
1563 | |
1564 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
1565 | || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT) { |
1566 | reason = TP_REASON_MALFORMED("ACTIVE_CONN_ID_LIMIT"); | |
75b2920a | 1567 | goto malformed; |
3c567a52 | 1568 | } |
75b2920a HL |
1569 | |
1570 | ch->rx_active_conn_id_limit = v; | |
1571 | got_active_conn_id_limit = 1; | |
1572 | break; | |
1573 | ||
3c567a52 | 1574 | case QUIC_TPARAM_STATELESS_RESET_TOKEN: |
3c567a52 | 1575 | /* |
cdd91631 P |
1576 | * We must ensure a client doesn't send them because we don't have |
1577 | * processing for them. | |
1578 | * | |
1579 | * TODO(QUIC SERVER): remove this restriction | |
3c567a52 HL |
1580 | */ |
1581 | if (ch->is_server) { | |
1582 | reason = TP_REASON_SERVER_ONLY("STATELESS_RESET_TOKEN"); | |
1583 | goto malformed; | |
1584 | } | |
1585 | ||
1586 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); | |
1587 | if (body == NULL || len != QUIC_STATELESS_RESET_TOKEN_LEN) { | |
1588 | reason = TP_REASON_MALFORMED("STATELESS_RESET_TOKEN"); | |
1589 | goto malformed; | |
1590 | } | |
cdd91631 P |
1591 | if (!chan_add_reset_token(ch, body, ch->cur_remote_seq_num)) { |
1592 | reason = TP_REASON_INTERNAL_ERROR("STATELESS_RESET_TOKEN"); | |
1593 | goto malformed; | |
1594 | } | |
3c567a52 HL |
1595 | |
1596 | break; | |
1597 | ||
1598 | case QUIC_TPARAM_PREFERRED_ADDR: | |
54bd1f24 | 1599 | { |
44cb36d0 | 1600 | /* TODO(QUIC FUTURE): Handle preferred address. */ |
54bd1f24 HL |
1601 | QUIC_PREFERRED_ADDR pfa; |
1602 | ||
1603 | /* | |
1604 | * RFC 9000 s. 18.2: "A server that chooses a zero-length | |
1605 | * connection ID MUST NOT provide a preferred address. | |
1606 | * Similarly, a server MUST NOT include a zero-length connection | |
1607 | * ID in this transport parameter. A client MUST treat a | |
1608 | * violation of these requirements as a connection error of type | |
1609 | * TRANSPORT_PARAMETER_ERROR." | |
1610 | */ | |
1611 | if (ch->is_server) { | |
1612 | reason = TP_REASON_SERVER_ONLY("PREFERRED_ADDR"); | |
1613 | goto malformed; | |
1614 | } | |
1615 | ||
1616 | if (ch->cur_remote_dcid.id_len == 0) { | |
1617 | reason = "PREFERRED_ADDR provided for zero-length CID"; | |
1618 | goto malformed; | |
1619 | } | |
1620 | ||
1621 | if (!ossl_quic_wire_decode_transport_param_preferred_addr(&pkt, &pfa)) { | |
1622 | reason = TP_REASON_MALFORMED("PREFERRED_ADDR"); | |
1623 | goto malformed; | |
1624 | } | |
1625 | ||
1626 | if (pfa.cid.id_len == 0) { | |
1627 | reason = "zero-length CID in PREFERRED_ADDR"; | |
1628 | goto malformed; | |
1629 | } | |
3c567a52 | 1630 | } |
3c567a52 | 1631 | break; |
75b2920a HL |
1632 | |
1633 | case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION: | |
1634 | /* We do not currently handle migration, so nothing to do. */ | |
0911cb4a HL |
1635 | if (got_disable_active_migration) { |
1636 | /* must not appear more than once */ | |
1637 | reason = TP_REASON_DUP("DISABLE_ACTIVE_MIGRATION"); | |
1638 | goto malformed; | |
1639 | } | |
1640 | ||
1641 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); | |
1642 | if (body == NULL || len > 0) { | |
1643 | reason = TP_REASON_MALFORMED("DISABLE_ACTIVE_MIGRATION"); | |
1644 | goto malformed; | |
1645 | } | |
1646 | ||
1647 | got_disable_active_migration = 1; | |
1648 | break; | |
1649 | ||
75b2920a | 1650 | default: |
0911cb4a HL |
1651 | /* |
1652 | * Skip over and ignore. | |
1653 | * | |
1654 | * RFC 9000 s. 7.4: We SHOULD treat duplicated transport parameters | |
1655 | * as a connection error, but we are not required to. Currently, | |
1656 | * handle this programmatically by checking for duplicates in the | |
1657 | * parameters that we recognise, as above, but don't bother | |
1658 | * maintaining a list of duplicates for anything we don't recognise. | |
1659 | */ | |
75b2920a HL |
1660 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, |
1661 | &len); | |
1662 | if (body == NULL) | |
1663 | goto malformed; | |
1664 | ||
1665 | break; | |
f538b421 HL |
1666 | } |
1667 | } | |
1668 | ||
3c567a52 HL |
1669 | if (!got_initial_scid) { |
1670 | reason = TP_REASON_REQUIRED("INITIAL_SCID"); | |
f538b421 | 1671 | goto malformed; |
3c567a52 HL |
1672 | } |
1673 | ||
1674 | if (!ch->is_server) { | |
1675 | if (!got_orig_dcid) { | |
1676 | reason = TP_REASON_REQUIRED("ORIG_DCID"); | |
1677 | goto malformed; | |
1678 | } | |
1679 | ||
1680 | if (ch->doing_retry && !got_retry_scid) { | |
1681 | reason = TP_REASON_REQUIRED("RETRY_SCID"); | |
1682 | goto malformed; | |
1683 | } | |
1684 | } | |
f538b421 HL |
1685 | |
1686 | ch->got_remote_transport_params = 1; | |
1687 | ||
1688 | if (got_initial_max_data || got_initial_max_stream_data_bidi_remote | |
1689 | || got_initial_max_streams_bidi || got_initial_max_streams_uni) | |
26ad16ea HL |
1690 | /* |
1691 | * If FC credit was bumped, we may now be able to send. Update all | |
1692 | * streams. | |
1693 | */ | |
1694 | ossl_quic_stream_map_visit(&ch->qsm, do_update, ch); | |
f538b421 HL |
1695 | |
1696 | /* If we are a server, we now generate our own transport parameters. */ | |
1697 | if (ch->is_server && !ch_generate_transport_params(ch)) { | |
1698 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
1699 | "internal error"); | |
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | return 1; | |
1704 | ||
1705 | malformed: | |
1706 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_TRANSPORT_PARAMETER_ERROR, | |
3c567a52 | 1707 | 0, reason); |
f538b421 HL |
1708 | return 0; |
1709 | } | |
1710 | ||
1711 | /* | |
1712 | * Called when we want to generate transport parameters. This is called | |
1713 | * immediately at instantiation time for a client and after we receive the | |
1714 | * client's transport parameters for a server. | |
1715 | */ | |
1716 | static int ch_generate_transport_params(QUIC_CHANNEL *ch) | |
1717 | { | |
1718 | int ok = 0; | |
1719 | BUF_MEM *buf_mem = NULL; | |
1720 | WPACKET wpkt; | |
1721 | int wpkt_valid = 0; | |
1722 | size_t buf_len = 0; | |
1723 | ||
1724 | if (ch->local_transport_params != NULL) | |
1725 | goto err; | |
1726 | ||
1727 | if ((buf_mem = BUF_MEM_new()) == NULL) | |
1728 | goto err; | |
1729 | ||
1730 | if (!WPACKET_init(&wpkt, buf_mem)) | |
1731 | goto err; | |
1732 | ||
1733 | wpkt_valid = 1; | |
1734 | ||
1735 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION, | |
1736 | NULL, 0) == NULL) | |
1737 | goto err; | |
1738 | ||
3c567a52 HL |
1739 | if (ch->is_server) { |
1740 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_ORIG_DCID, | |
1741 | &ch->init_dcid)) | |
1742 | goto err; | |
1743 | ||
1744 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_INITIAL_SCID, | |
bbc97540 | 1745 | &ch->cur_local_cid)) |
3c567a52 HL |
1746 | goto err; |
1747 | } else { | |
1748 | /* Client always uses an empty SCID. */ | |
1749 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_INITIAL_SCID, | |
1750 | NULL, 0) == NULL) | |
1751 | goto err; | |
1752 | } | |
f538b421 HL |
1753 | |
1754 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT, | |
1755 | ch->max_idle_timeout)) | |
1756 | goto err; | |
1757 | ||
1758 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE, | |
1759 | QUIC_MIN_INITIAL_DGRAM_LEN)) | |
1760 | goto err; | |
1761 | ||
1762 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT, | |
54bd1f24 | 1763 | QUIC_MIN_ACTIVE_CONN_ID_LIMIT)) |
f538b421 HL |
1764 | goto err; |
1765 | ||
f13868de HL |
1766 | if (ch->tx_max_ack_delay != QUIC_DEFAULT_MAX_ACK_DELAY |
1767 | && !ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_ACK_DELAY, | |
1768 | ch->tx_max_ack_delay)) | |
1769 | goto err; | |
1770 | ||
f538b421 HL |
1771 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA, |
1772 | ossl_quic_rxfc_get_cwm(&ch->conn_rxfc))) | |
1773 | goto err; | |
1774 | ||
0815b725 | 1775 | /* Send the default CWM for a new RXFC. */ |
f538b421 | 1776 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, |
0815b725 | 1777 | ch->tx_init_max_stream_data_bidi_local)) |
f538b421 HL |
1778 | goto err; |
1779 | ||
1780 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, | |
0815b725 | 1781 | ch->tx_init_max_stream_data_bidi_remote)) |
f538b421 HL |
1782 | goto err; |
1783 | ||
1784 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI, | |
0815b725 | 1785 | ch->tx_init_max_stream_data_uni)) |
f538b421 HL |
1786 | goto err; |
1787 | ||
1788 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI, | |
a6b6ea17 | 1789 | ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc))) |
f538b421 HL |
1790 | goto err; |
1791 | ||
1792 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI, | |
a6b6ea17 | 1793 | ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc))) |
f538b421 HL |
1794 | goto err; |
1795 | ||
eff04652 TM |
1796 | if (!WPACKET_finish(&wpkt)) |
1797 | goto err; | |
1798 | ||
1799 | wpkt_valid = 0; | |
1800 | ||
f538b421 HL |
1801 | if (!WPACKET_get_total_written(&wpkt, &buf_len)) |
1802 | goto err; | |
1803 | ||
1804 | ch->local_transport_params = (unsigned char *)buf_mem->data; | |
1805 | buf_mem->data = NULL; | |
1806 | ||
f538b421 | 1807 | |
2723d705 | 1808 | if (!ossl_quic_tls_set_transport_params(ch->qtls, ch->local_transport_params, |
f538b421 HL |
1809 | buf_len)) |
1810 | goto err; | |
1811 | ||
1812 | ok = 1; | |
1813 | err: | |
1814 | if (wpkt_valid) | |
1815 | WPACKET_cleanup(&wpkt); | |
1816 | BUF_MEM_free(buf_mem); | |
1817 | return ok; | |
1818 | } | |
1819 | ||
1820 | /* | |
1821 | * QUIC Channel: Ticker-Mutator | |
1822 | * ============================ | |
1823 | */ | |
1824 | ||
1825 | /* | |
1826 | * The central ticker function called by the reactor. This does everything, or | |
1827 | * at least everything network I/O related. Best effort - not allowed to fail | |
1828 | * "loudly". | |
1829 | */ | |
ccd31037 | 1830 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg, uint32_t flags) |
f538b421 HL |
1831 | { |
1832 | OSSL_TIME now, deadline; | |
1833 | QUIC_CHANNEL *ch = arg; | |
9cf091a3 | 1834 | int channel_only = (flags & QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY) != 0; |
80bcc4f1 | 1835 | uint64_t error_code; |
7a2bb210 HL |
1836 | const char *error_msg; |
1837 | ERR_STATE *error_state = NULL; | |
f538b421 HL |
1838 | |
1839 | /* | |
1840 | * When we tick the QUIC connection, we do everything we need to do | |
1841 | * periodically. In order, we: | |
1842 | * | |
1843 | * - handle any incoming data from the network; | |
1844 | * - handle any timer events which are due to fire (ACKM, etc.) | |
1845 | * - write any data to the network due to be sent, to the extent | |
1846 | * possible; | |
1847 | * - determine the time at which we should next be ticked. | |
1848 | */ | |
1849 | ||
1850 | /* If we are in the TERMINATED state, there is nothing to do. */ | |
c12e1113 | 1851 | if (ossl_quic_channel_is_terminated(ch)) { |
b639475a HL |
1852 | res->net_read_desired = 0; |
1853 | res->net_write_desired = 0; | |
1854 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1855 | return; |
1856 | } | |
1857 | ||
1858 | /* | |
1859 | * If we are in the TERMINATING state, check if the terminating timer has | |
1860 | * expired. | |
1861 | */ | |
c12e1113 | 1862 | if (ossl_quic_channel_is_terminating(ch)) { |
b212d554 | 1863 | now = get_time(ch); |
f538b421 HL |
1864 | |
1865 | if (ossl_time_compare(now, ch->terminate_deadline) >= 0) { | |
1866 | ch_on_terminating_timeout(ch); | |
b639475a HL |
1867 | res->net_read_desired = 0; |
1868 | res->net_write_desired = 0; | |
1869 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1870 | return; /* abort normal processing, nothing to do */ |
1871 | } | |
1872 | } | |
1873 | ||
03b38595 HL |
1874 | if (!ch->inhibit_tick) { |
1875 | /* Handle RXKU timeouts. */ | |
1876 | ch_rxku_tick(ch); | |
8a65e7a5 | 1877 | |
03b38595 HL |
1878 | /* Handle any incoming data from network. */ |
1879 | ch_rx_pre(ch); | |
3bf4dc8c | 1880 | |
03b38595 HL |
1881 | do { |
1882 | /* Process queued incoming packets. */ | |
1883 | ch_rx(ch); | |
f538b421 | 1884 | |
03b38595 HL |
1885 | /* |
1886 | * Allow the handshake layer to check for any new incoming data and | |
1887 | * generate new outgoing data. | |
1888 | */ | |
1889 | ch->have_new_rx_secret = 0; | |
80bcc4f1 | 1890 | if (!channel_only) { |
03b38595 | 1891 | ossl_quic_tls_tick(ch->qtls); |
4e64437a | 1892 | |
741170be | 1893 | if (ossl_quic_tls_get_error(ch->qtls, &error_code, &error_msg, |
7a2bb210 HL |
1894 | &error_state)) |
1895 | ossl_quic_channel_raise_protocol_error_state(ch, error_code, 0, | |
1896 | error_msg, error_state); | |
80bcc4f1 HL |
1897 | } |
1898 | ||
03b38595 HL |
1899 | /* |
1900 | * If the handshake layer gave us a new secret, we need to do RX | |
1901 | * again because packets that were not previously processable and | |
1902 | * were deferred might now be processable. | |
1903 | * | |
44cb36d0 | 1904 | * TODO(QUIC FUTURE): Consider handling this in the yield_secret callback. |
03b38595 HL |
1905 | */ |
1906 | } while (ch->have_new_rx_secret); | |
1907 | } | |
f538b421 HL |
1908 | |
1909 | /* | |
03b38595 HL |
1910 | * Handle any timer events which are due to fire; namely, the loss |
1911 | * detection deadline and the idle timeout. | |
f538b421 | 1912 | * |
03b38595 HL |
1913 | * ACKM ACK generation deadline is polled by TXP, so we don't need to |
1914 | * handle it here. | |
f538b421 | 1915 | */ |
b212d554 | 1916 | now = get_time(ch); |
f538b421 HL |
1917 | if (ossl_time_compare(now, ch->idle_deadline) >= 0) { |
1918 | /* | |
03b38595 HL |
1919 | * Idle timeout differs from normal protocol violation because we do |
1920 | * not send a CONN_CLOSE frame; go straight to TERMINATED. | |
f538b421 | 1921 | */ |
03b38595 HL |
1922 | if (!ch->inhibit_tick) |
1923 | ch_on_idle_timeout(ch); | |
1924 | ||
b639475a HL |
1925 | res->net_read_desired = 0; |
1926 | res->net_write_desired = 0; | |
1927 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1928 | return; |
1929 | } | |
1930 | ||
03b38595 HL |
1931 | if (!ch->inhibit_tick) { |
1932 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); | |
1933 | if (!ossl_time_is_zero(deadline) | |
1934 | && ossl_time_compare(now, deadline) >= 0) | |
1935 | ossl_ackm_on_timeout(ch->ackm); | |
f538b421 | 1936 | |
03b38595 HL |
1937 | /* If a ping is due, inform TXP. */ |
1938 | if (ossl_time_compare(now, ch->ping_deadline) >= 0) { | |
1939 | int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level); | |
3b1ab5a3 | 1940 | |
03b38595 HL |
1941 | ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space); |
1942 | } | |
3b1ab5a3 | 1943 | |
03b38595 HL |
1944 | /* Write any data to the network due to be sent. */ |
1945 | ch_tx(ch); | |
f538b421 | 1946 | |
03b38595 HL |
1947 | /* Do stream GC. */ |
1948 | ossl_quic_stream_map_gc(&ch->qsm); | |
1949 | } | |
0847e63e | 1950 | |
f538b421 HL |
1951 | /* Determine the time at which we should next be ticked. */ |
1952 | res->tick_deadline = ch_determine_next_tick_deadline(ch); | |
1953 | ||
df15e990 HL |
1954 | /* |
1955 | * Always process network input unless we are now terminated. | |
1956 | * Although we had not terminated at the beginning of this tick, network | |
1957 | * errors in ch_rx_pre() or ch_tx() may have caused us to transition to the | |
1958 | * Terminated state. | |
1959 | */ | |
c12e1113 | 1960 | res->net_read_desired = !ossl_quic_channel_is_terminated(ch); |
f538b421 HL |
1961 | |
1962 | /* We want to write to the network if we have any in our queue. */ | |
b639475a | 1963 | res->net_write_desired |
c12e1113 | 1964 | = (!ossl_quic_channel_is_terminated(ch) |
df15e990 | 1965 | && ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0); |
f538b421 HL |
1966 | } |
1967 | ||
3bf4dc8c HL |
1968 | /* Process incoming datagrams, if any. */ |
1969 | static void ch_rx_pre(QUIC_CHANNEL *ch) | |
1970 | { | |
df15e990 HL |
1971 | int ret; |
1972 | ||
b1b06da2 | 1973 | if (!ch->is_server && !ch->have_sent_any_pkt) |
3bf4dc8c HL |
1974 | return; |
1975 | ||
1976 | /* | |
1977 | * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams | |
1978 | * to the appropriate QRX instance. | |
1979 | */ | |
df15e990 | 1980 | ret = ossl_quic_demux_pump(ch->demux); |
cdd91631 P |
1981 | if (ret == QUIC_DEMUX_PUMP_RES_STATELESS_RESET) |
1982 | ch_stateless_reset(ch); | |
1983 | else if (ret == QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL) | |
df15e990 HL |
1984 | /* |
1985 | * We don't care about transient failure, but permanent failure means we | |
1986 | * should tear down the connection as though a protocol violation | |
1987 | * occurred. Skip straight to the Terminating state as there is no point | |
1988 | * trying to send CONNECTION_CLOSE frames if the network BIO is not | |
1989 | * operating correctly. | |
1990 | */ | |
1991 | ch_raise_net_error(ch); | |
3bf4dc8c HL |
1992 | } |
1993 | ||
48120ea5 HL |
1994 | /* Check incoming forged packet limit and terminate connection if needed. */ |
1995 | static void ch_rx_check_forged_pkt_limit(QUIC_CHANNEL *ch) | |
1996 | { | |
1997 | uint32_t enc_level; | |
1998 | uint64_t limit = UINT64_MAX, l; | |
1999 | ||
2000 | for (enc_level = QUIC_ENC_LEVEL_INITIAL; | |
2001 | enc_level < QUIC_ENC_LEVEL_NUM; | |
2002 | ++enc_level) | |
2003 | { | |
2004 | /* | |
2005 | * Different ELs can have different AEADs which can in turn impose | |
2006 | * different limits, so use the lowest value of any currently valid EL. | |
2007 | */ | |
2008 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
2009 | continue; | |
2010 | ||
2011 | if (enc_level > ch->rx_enc_level) | |
2012 | break; | |
2013 | ||
2014 | l = ossl_qrx_get_max_forged_pkt_count(ch->qrx, enc_level); | |
2015 | if (l < limit) | |
2016 | limit = l; | |
2017 | } | |
2018 | ||
2019 | if (ossl_qrx_get_cur_forged_pkt_count(ch->qrx) < limit) | |
2020 | return; | |
2021 | ||
2022 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_AEAD_LIMIT_REACHED, 0, | |
2023 | "forgery limit"); | |
2024 | } | |
2025 | ||
3bf4dc8c | 2026 | /* Process queued incoming packets and handle frames, if any. */ |
f538b421 HL |
2027 | static int ch_rx(QUIC_CHANNEL *ch) |
2028 | { | |
2029 | int handled_any = 0; | |
50e76846 | 2030 | const int closing = ossl_quic_channel_is_closing(ch); |
f538b421 | 2031 | |
b1b06da2 | 2032 | if (!ch->is_server && !ch->have_sent_any_pkt) |
f538b421 HL |
2033 | /* |
2034 | * We have not sent anything yet, therefore there is no need to check | |
75b2920a | 2035 | * for incoming data. |
f538b421 HL |
2036 | */ |
2037 | return 1; | |
2038 | ||
f538b421 HL |
2039 | for (;;) { |
2040 | assert(ch->qrx_pkt == NULL); | |
2041 | ||
2042 | if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt)) | |
2043 | break; | |
2044 | ||
50e76846 P |
2045 | /* Track the amount of data received while in the closing state */ |
2046 | if (closing) | |
2047 | ossl_quic_tx_packetiser_record_received_closing_bytes( | |
2048 | ch->txp, ch->qrx_pkt->hdr->len); | |
2049 | ||
8fd32a0e | 2050 | if (!handled_any) { |
f538b421 | 2051 | ch_update_idle(ch); |
8fd32a0e TM |
2052 | ch_update_ping_deadline(ch); |
2053 | } | |
f538b421 HL |
2054 | |
2055 | ch_rx_handle_packet(ch); /* best effort */ | |
2056 | ||
2057 | /* | |
2058 | * Regardless of the outcome of frame handling, unref the packet. | |
2059 | * This will free the packet unless something added another | |
2060 | * reference to it during frame processing. | |
2061 | */ | |
2062 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
2063 | ch->qrx_pkt = NULL; | |
2064 | ||
3b1ab5a3 | 2065 | ch->have_sent_ack_eliciting_since_rx = 0; |
f538b421 HL |
2066 | handled_any = 1; |
2067 | } | |
2068 | ||
48120ea5 HL |
2069 | ch_rx_check_forged_pkt_limit(ch); |
2070 | ||
f538b421 HL |
2071 | /* |
2072 | * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we | |
2073 | * process one or more incoming packets. | |
2074 | */ | |
50e76846 | 2075 | if (handled_any && closing) |
f538b421 HL |
2076 | ch->conn_close_queued = 1; |
2077 | ||
2078 | return 1; | |
2079 | } | |
2080 | ||
3ffb7d10 HL |
2081 | static int bio_addr_eq(const BIO_ADDR *a, const BIO_ADDR *b) |
2082 | { | |
2083 | if (BIO_ADDR_family(a) != BIO_ADDR_family(b)) | |
2084 | return 0; | |
2085 | ||
2086 | switch (BIO_ADDR_family(a)) { | |
2087 | case AF_INET: | |
2088 | return !memcmp(&a->s_in.sin_addr, | |
2089 | &b->s_in.sin_addr, | |
2090 | sizeof(a->s_in.sin_addr)) | |
2091 | && a->s_in.sin_port == b->s_in.sin_port; | |
9c8d04db | 2092 | #if OPENSSL_USE_IPV6 |
3ffb7d10 HL |
2093 | case AF_INET6: |
2094 | return !memcmp(&a->s_in6.sin6_addr, | |
2095 | &b->s_in6.sin6_addr, | |
2096 | sizeof(a->s_in6.sin6_addr)) | |
2097 | && a->s_in6.sin6_port == b->s_in6.sin6_port; | |
9c8d04db | 2098 | #endif |
3ffb7d10 HL |
2099 | default: |
2100 | return 0; /* not supported */ | |
2101 | } | |
2102 | ||
2103 | return 1; | |
2104 | } | |
2105 | ||
f538b421 HL |
2106 | /* Handles the packet currently in ch->qrx_pkt->hdr. */ |
2107 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch) | |
2108 | { | |
2109 | uint32_t enc_level; | |
777a8a7f | 2110 | int old_have_processed_any_pkt = ch->have_processed_any_pkt; |
f538b421 HL |
2111 | |
2112 | assert(ch->qrx_pkt != NULL); | |
2113 | ||
50e76846 P |
2114 | /* |
2115 | * RFC 9000 s. 10.2.1 Closing Connection State: | |
2116 | * An endpoint that is closing is not required to process any | |
2117 | * received frame. | |
2118 | */ | |
8a6a00e3 | 2119 | if (!ossl_quic_channel_is_active(ch)) |
8a6a00e3 HL |
2120 | return; |
2121 | ||
f538b421 HL |
2122 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) { |
2123 | if (!ch->have_received_enc_pkt) { | |
eff04652 | 2124 | ch->cur_remote_dcid = ch->init_scid = ch->qrx_pkt->hdr->src_conn_id; |
f538b421 HL |
2125 | ch->have_received_enc_pkt = 1; |
2126 | ||
2127 | /* | |
2128 | * We change to using the SCID in the first Initial packet as the | |
2129 | * DCID. | |
2130 | */ | |
2131 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid); | |
2132 | } | |
2133 | ||
2134 | enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type); | |
2135 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
2136 | /* Do not process packets from ELs we have already discarded. */ | |
2137 | return; | |
2138 | } | |
2139 | ||
3ffb7d10 HL |
2140 | /* |
2141 | * RFC 9000 s. 9.6: "If a client receives packets from a new server address | |
2142 | * when the client has not initiated a migration to that address, the client | |
2143 | * SHOULD discard these packets." | |
2144 | * | |
2145 | * We need to be a bit careful here as due to the BIO abstraction layer an | |
2146 | * application is liable to be weird and lie to us about peer addresses. | |
96b7df60 HL |
2147 | * Only apply this check if we actually are using a real AF_INET or AF_INET6 |
2148 | * address. | |
3ffb7d10 HL |
2149 | */ |
2150 | if (!ch->is_server | |
2151 | && ch->qrx_pkt->peer != NULL | |
9c8d04db TC |
2152 | && ( |
2153 | BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET | |
2154 | #if OPENSSL_USE_IPV6 | |
2155 | || BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET6 | |
2156 | #endif | |
2157 | ) | |
3ffb7d10 HL |
2158 | && !bio_addr_eq(ch->qrx_pkt->peer, &ch->cur_peer_addr)) |
2159 | return; | |
2160 | ||
0911cb4a HL |
2161 | if (!ch->is_server |
2162 | && ch->have_received_enc_pkt | |
2163 | && ossl_quic_pkt_type_has_scid(ch->qrx_pkt->hdr->type)) { | |
2164 | /* | |
3ffb7d10 | 2165 | * RFC 9000 s. 7.2: "Once a client has received a valid Initial packet |
0911cb4a HL |
2166 | * from the server, it MUST discard any subsequent packet it receives on |
2167 | * that connection with a different SCID." | |
2168 | */ | |
2169 | if (!ossl_quic_conn_id_eq(&ch->qrx_pkt->hdr->src_conn_id, | |
2170 | &ch->init_scid)) | |
2171 | return; | |
2172 | } | |
2173 | ||
2174 | if (ossl_quic_pkt_type_has_version(ch->qrx_pkt->hdr->type) | |
2175 | && ch->qrx_pkt->hdr->version != QUIC_VERSION_1) | |
2176 | /* | |
2177 | * RFC 9000 s. 5.2.1: If a client receives a packet that uses a | |
2178 | * different version than it initially selected, it MUST discard the | |
2179 | * packet. We only ever use v1, so require it. | |
2180 | */ | |
2181 | return; | |
2182 | ||
777a8a7f HL |
2183 | ch->have_processed_any_pkt = 1; |
2184 | ||
08cb9a83 HL |
2185 | /* |
2186 | * RFC 9000 s. 17.2: "An endpoint MUST treat receipt of a packet that has a | |
2187 | * non-zero value for [the reserved bits] after removing both packet and | |
2188 | * header protection as a connection error of type PROTOCOL_VIOLATION." | |
2189 | */ | |
2190 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type) | |
2191 | && ch->qrx_pkt->hdr->reserved != 0) { | |
2192 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
2193 | 0, "packet header reserved bits"); | |
2194 | return; | |
2195 | } | |
2196 | ||
f538b421 HL |
2197 | /* Handle incoming packet. */ |
2198 | switch (ch->qrx_pkt->hdr->type) { | |
75b2920a | 2199 | case QUIC_PKT_TYPE_RETRY: |
b1b06da2 | 2200 | if (ch->doing_retry || ch->is_server) |
75b2920a HL |
2201 | /* |
2202 | * It is not allowed to ask a client to do a retry more than | |
b1b06da2 | 2203 | * once. Clients may not send retries. |
75b2920a HL |
2204 | */ |
2205 | return; | |
f538b421 | 2206 | |
75b2920a HL |
2207 | if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN) |
2208 | /* Packets with zero-length Retry Tokens are invalid. */ | |
2209 | return; | |
f538b421 | 2210 | |
75b2920a | 2211 | /* |
44cb36d0 | 2212 | * TODO(QUIC FUTURE): Theoretically this should probably be in the QRX. |
75b2920a HL |
2213 | * However because validation is dependent on context (namely the |
2214 | * client's initial DCID) we can't do this cleanly. In the future we | |
2215 | * should probably add a callback to the QRX to let it call us (via | |
2216 | * the DEMUX) and ask us about the correct original DCID, rather | |
2217 | * than allow the QRX to emit a potentially malformed packet to the | |
2218 | * upper layers. However, special casing this will do for now. | |
2219 | */ | |
2220 | if (!ossl_quic_validate_retry_integrity_tag(ch->libctx, | |
2221 | ch->propq, | |
2222 | ch->qrx_pkt->hdr, | |
2223 | &ch->init_dcid)) | |
2224 | /* Malformed retry packet, ignore. */ | |
2225 | return; | |
f538b421 | 2226 | |
96014840 TM |
2227 | if (!ch_retry(ch, ch->qrx_pkt->hdr->data, |
2228 | ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN, | |
2229 | &ch->qrx_pkt->hdr->src_conn_id)) | |
2230 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, | |
2231 | 0, "handling retry packet"); | |
75b2920a | 2232 | break; |
f538b421 | 2233 | |
75b2920a | 2234 | case QUIC_PKT_TYPE_0RTT: |
b1b06da2 HL |
2235 | if (!ch->is_server) |
2236 | /* Clients should never receive 0-RTT packets. */ | |
2237 | return; | |
2238 | ||
2239 | /* | |
44cb36d0 TM |
2240 | * TODO(QUIC 0RTT): Implement 0-RTT on the server side. We currently |
2241 | * do not need to implement this as a client can only do 0-RTT if we | |
b1b06da2 HL |
2242 | * have given it permission to in a previous session. |
2243 | */ | |
75b2920a HL |
2244 | break; |
2245 | ||
b1b06da2 HL |
2246 | case QUIC_PKT_TYPE_INITIAL: |
2247 | case QUIC_PKT_TYPE_HANDSHAKE: | |
2248 | case QUIC_PKT_TYPE_1RTT: | |
b6125b54 | 2249 | if (ch->is_server && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE) |
75b2920a HL |
2250 | /* |
2251 | * We automatically drop INITIAL EL keys when first successfully | |
2252 | * decrypting a HANDSHAKE packet, as per the RFC. | |
2253 | */ | |
2254 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); | |
2255 | ||
54fb0072 HL |
2256 | if (ch->rxku_in_progress |
2257 | && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_1RTT | |
2258 | && ch->qrx_pkt->pn >= ch->rxku_trigger_pn | |
2259 | && ch->qrx_pkt->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)) { | |
2260 | /* | |
2261 | * RFC 9001 s. 6.4: Packets with higher packet numbers MUST be | |
2262 | * protected with either the same or newer packet protection keys | |
2263 | * than packets with lower packet numbers. An endpoint that | |
2264 | * successfully removes protection with old keys when newer keys | |
2265 | * were used for packets with lower packet numbers MUST treat this | |
2266 | * as a connection error of type KEY_UPDATE_ERROR. | |
2267 | */ | |
2268 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_KEY_UPDATE_ERROR, | |
2269 | 0, "new packet with old keys"); | |
2270 | break; | |
2271 | } | |
2272 | ||
fd0d5932 HL |
2273 | if (!ch->is_server |
2274 | && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL | |
2275 | && ch->qrx_pkt->hdr->token_len > 0) { | |
2276 | /* | |
2277 | * RFC 9000 s. 17.2.2: Clients that receive an Initial packet with a | |
2278 | * non-zero Token Length field MUST either discard the packet or | |
2279 | * generate a connection error of type PROTOCOL_VIOLATION. | |
bed20874 P |
2280 | * |
2281 | * TODO(QUIC): consider the implications of RFC 9000 s. 10.2.3 | |
2282 | * Immediate Close during the Handshake: | |
2283 | * However, at the cost of reducing feedback about | |
2284 | * errors for legitimate peers, some forms of denial of | |
2285 | * service can be made more difficult for an attacker | |
2286 | * if endpoints discard illegal packets rather than | |
2287 | * terminating a connection with CONNECTION_CLOSE. For | |
2288 | * this reason, endpoints MAY discard packets rather | |
2289 | * than immediately close if errors are detected in | |
2290 | * packets that lack authentication. | |
2291 | * I.e. should we drop this packet instead of closing the connection? | |
fd0d5932 HL |
2292 | */ |
2293 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
2294 | 0, "client received initial token"); | |
2295 | break; | |
2296 | } | |
2297 | ||
75b2920a HL |
2298 | /* This packet contains frames, pass to the RXDP. */ |
2299 | ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */ | |
2300 | break; | |
b1b06da2 | 2301 | |
777a8a7f HL |
2302 | case QUIC_PKT_TYPE_VERSION_NEG: |
2303 | /* | |
2304 | * "A client MUST discard any Version Negotiation packet if it has | |
2305 | * received and successfully processed any other packet." | |
2306 | */ | |
2307 | if (!old_have_processed_any_pkt) | |
2308 | ch_rx_handle_version_neg(ch, ch->qrx_pkt); | |
2309 | ||
2310 | break; | |
2311 | ||
b1b06da2 HL |
2312 | default: |
2313 | assert(0); | |
2314 | break; | |
2315 | } | |
2316 | } | |
2317 | ||
777a8a7f HL |
2318 | static void ch_rx_handle_version_neg(QUIC_CHANNEL *ch, OSSL_QRX_PKT *pkt) |
2319 | { | |
2320 | /* | |
2321 | * We do not support version negotiation at this time. As per RFC 9000 s. | |
2322 | * 6.2., we MUST abandon the connection attempt if we receive a Version | |
2323 | * Negotiation packet, unless we have already successfully processed another | |
2324 | * incoming packet, or the packet lists the QUIC version we want to use. | |
2325 | */ | |
2326 | PACKET vpkt; | |
2327 | unsigned long v; | |
2328 | ||
2329 | if (!PACKET_buf_init(&vpkt, pkt->hdr->data, pkt->hdr->len)) | |
2330 | return; | |
2331 | ||
2332 | while (PACKET_remaining(&vpkt) > 0) { | |
2333 | if (!PACKET_get_net_4(&vpkt, &v)) | |
2334 | break; | |
2335 | ||
2336 | if ((uint32_t)v == QUIC_VERSION_1) | |
2337 | return; | |
2338 | } | |
2339 | ||
2340 | /* No match, this is a failure case. */ | |
2341 | ch_raise_version_neg_failure(ch); | |
2342 | } | |
2343 | ||
2344 | static void ch_raise_version_neg_failure(QUIC_CHANNEL *ch) | |
2345 | { | |
2346 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
2347 | ||
2348 | tcause.error_code = QUIC_ERR_CONNECTION_REFUSED; | |
2349 | tcause.reason = "version negotiation failure"; | |
2350 | tcause.reason_len = strlen(tcause.reason); | |
2351 | ||
2352 | /* | |
2353 | * Skip TERMINATING state; this is not considered a protocol error and we do | |
2354 | * not send CONNECTION_CLOSE. | |
2355 | */ | |
2356 | ch_start_terminating(ch, &tcause, 1); | |
2357 | } | |
2358 | ||
b1b06da2 HL |
2359 | /* |
2360 | * This is called by the demux when we get a packet not destined for any known | |
2361 | * DCID. | |
2362 | */ | |
2363 | static void ch_default_packet_handler(QUIC_URXE *e, void *arg) | |
2364 | { | |
2365 | QUIC_CHANNEL *ch = arg; | |
2366 | PACKET pkt; | |
2367 | QUIC_PKT_HDR hdr; | |
2368 | ||
2369 | if (!ossl_assert(ch->is_server)) | |
2370 | goto undesirable; | |
2371 | ||
2372 | /* | |
2373 | * We only support one connection to our server currently, so if we already | |
2374 | * started one, ignore any new connection attempts. | |
2375 | */ | |
2376 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) | |
2377 | goto undesirable; | |
2378 | ||
2379 | /* | |
2380 | * We have got a packet for an unknown DCID. This might be an attempt to | |
2381 | * open a new connection. | |
2382 | */ | |
2383 | if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN) | |
2384 | goto undesirable; | |
2385 | ||
2386 | if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len)) | |
091f532e | 2387 | goto err; |
b1b06da2 HL |
2388 | |
2389 | /* | |
2390 | * We set short_conn_id_len to SIZE_MAX here which will cause the decode | |
2391 | * operation to fail if we get a 1-RTT packet. This is fine since we only | |
2392 | * care about Initial packets. | |
2393 | */ | |
e8528c95 | 2394 | if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL)) |
b1b06da2 HL |
2395 | goto undesirable; |
2396 | ||
2397 | switch (hdr.version) { | |
2398 | case QUIC_VERSION_1: | |
2399 | break; | |
2400 | ||
2401 | case QUIC_VERSION_NONE: | |
2402 | default: | |
2403 | /* Unknown version or proactive version negotiation request, bail. */ | |
44cb36d0 | 2404 | /* TODO(QUIC SERVER): Handle version negotiation on server side */ |
b1b06da2 | 2405 | goto undesirable; |
f538b421 | 2406 | } |
b1b06da2 HL |
2407 | |
2408 | /* | |
2409 | * We only care about Initial packets which might be trying to establish a | |
2410 | * connection. | |
2411 | */ | |
2412 | if (hdr.type != QUIC_PKT_TYPE_INITIAL) | |
2413 | goto undesirable; | |
2414 | ||
2415 | /* | |
2416 | * Assume this is a valid attempt to initiate a connection. | |
2417 | * | |
2418 | * We do not register the DCID in the initial packet we received and that | |
2419 | * DCID is not actually used again, thus after provisioning the correct | |
2420 | * Initial keys derived from it (which is done in the call below) we pass | |
2421 | * the received packet directly to the QRX so that it can process it as a | |
2422 | * one-time thing, instead of going through the usual DEMUX DCID-based | |
2423 | * routing. | |
2424 | */ | |
2425 | if (!ch_server_on_new_conn(ch, &e->peer, | |
2426 | &hdr.src_conn_id, | |
2427 | &hdr.dst_conn_id)) | |
091f532e | 2428 | goto err; |
b1b06da2 HL |
2429 | |
2430 | ossl_qrx_inject_urxe(ch->qrx, e); | |
2431 | return; | |
2432 | ||
091f532e HL |
2433 | err: |
2434 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
2435 | "internal error"); | |
b1b06da2 HL |
2436 | undesirable: |
2437 | ossl_quic_demux_release_urxe(ch->demux, e); | |
f538b421 HL |
2438 | } |
2439 | ||
2440 | /* Try to generate packets and if possible, flush them to the network. */ | |
2441 | static int ch_tx(QUIC_CHANNEL *ch) | |
2442 | { | |
a3a51d6e | 2443 | QUIC_TXP_STATUS status; |
64fd6991 | 2444 | int res; |
3b1ab5a3 | 2445 | |
afe4a797 P |
2446 | /* |
2447 | * RFC 9000 s. 10.2.2: Draining Connection State: | |
2448 | * While otherwise identical to the closing state, an endpoint | |
2449 | * in the draining state MUST NOT send any packets. | |
2450 | * and: | |
2451 | * An endpoint MUST NOT send further packets. | |
2452 | */ | |
2453 | if (ossl_quic_channel_is_draining(ch)) | |
2454 | return 0; | |
2455 | ||
2456 | if (ossl_quic_channel_is_closing(ch)) { | |
f538b421 HL |
2457 | /* |
2458 | * While closing, only send CONN_CLOSE if we've received more traffic | |
2459 | * from the peer. Once we tell the TXP to generate CONN_CLOSE, all | |
2460 | * future calls to it generate CONN_CLOSE frames, so otherwise we would | |
2461 | * just constantly generate CONN_CLOSE frames. | |
6b3b5f9d | 2462 | * |
afe4a797 | 2463 | * Confirming to RFC 9000 s. 10.2.1 Closing Connection State: |
6b3b5f9d | 2464 | * An endpoint SHOULD limit the rate at which it generates |
afe4a797 | 2465 | * packets in the closing state. |
f538b421 HL |
2466 | */ |
2467 | if (!ch->conn_close_queued) | |
2468 | return 0; | |
2469 | ||
2470 | ch->conn_close_queued = 0; | |
2471 | } | |
2472 | ||
8a65e7a5 HL |
2473 | /* Do TXKU if we need to. */ |
2474 | ch_maybe_trigger_spontaneous_txku(ch); | |
2475 | ||
2476 | ch->rxku_pending_confirm_done = 0; | |
2477 | ||
aa433014 MC |
2478 | /* Loop until we stop generating packets to send */ |
2479 | do { | |
3b1ab5a3 | 2480 | /* |
aa433014 MC |
2481 | * Send packet, if we need to. Best effort. The TXP consults the CC and |
2482 | * applies any limitations imposed by it, so we don't need to do it here. | |
2483 | * | |
2484 | * Best effort. In particular if TXP fails for some reason we should | |
2485 | * still flush any queued packets which we already generated. | |
2486 | */ | |
2487 | res = ossl_quic_tx_packetiser_generate(ch->txp, &status); | |
2488 | if (status.sent_pkt > 0) { | |
2489 | ch->have_sent_any_pkt = 1; /* Packet(s) were sent */ | |
3b1ab5a3 | 2490 | |
3eb0f9a7 | 2491 | /* |
aa433014 MC |
2492 | * RFC 9000 s. 10.1. 'An endpoint also restarts its idle timer when |
2493 | * sending an ack-eliciting packet if no other ack-eliciting packets | |
2494 | * have been sent since last receiving and processing a packet.' | |
2495 | */ | |
2496 | if (status.sent_ack_eliciting | |
2497 | && !ch->have_sent_ack_eliciting_since_rx) { | |
2498 | ch_update_idle(ch); | |
2499 | ch->have_sent_ack_eliciting_since_rx = 1; | |
2500 | } | |
3eb0f9a7 | 2501 | |
aa433014 MC |
2502 | if (!ch->is_server && status.sent_handshake) |
2503 | /* | |
2504 | * RFC 9001 s. 4.9.1: A client MUST discard Initial keys when it | |
2505 | * first sends a Handshake packet. | |
2506 | */ | |
2507 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); | |
8a65e7a5 | 2508 | |
aa433014 MC |
2509 | if (ch->rxku_pending_confirm_done) |
2510 | ch->rxku_pending_confirm = 0; | |
5a1b1d2b | 2511 | |
aa433014 MC |
2512 | ch_update_ping_deadline(ch); |
2513 | } | |
2514 | ||
2515 | if (!res) { | |
2516 | /* | |
2517 | * One case where TXP can fail is if we reach a TX PN of 2**62 - 1. | |
2518 | * As per RFC 9000 s. 12.3, if this happens we MUST close the | |
2519 | * connection without sending a CONNECTION_CLOSE frame. This is | |
2520 | * actually handled as an emergent consequence of our design, as the | |
2521 | * TX packetiser will never transmit another packet when the TX PN | |
2522 | * reaches the limit. | |
2523 | * | |
2524 | * Calling the below function terminates the connection; its attempt | |
2525 | * to schedule a CONNECTION_CLOSE frame will not actually cause a | |
2526 | * packet to be transmitted for this reason. | |
2527 | */ | |
2528 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, | |
2529 | 0, | |
2530 | "internal error (txp generate)"); | |
2531 | break; | |
2532 | } | |
2533 | } while (status.sent_pkt > 0); | |
df15e990 HL |
2534 | |
2535 | /* Flush packets to network. */ | |
2536 | switch (ossl_qtx_flush_net(ch->qtx)) { | |
2537 | case QTX_FLUSH_NET_RES_OK: | |
2538 | case QTX_FLUSH_NET_RES_TRANSIENT_FAIL: | |
2539 | /* Best effort, done for now. */ | |
2540 | break; | |
2541 | ||
2542 | case QTX_FLUSH_NET_RES_PERMANENT_FAIL: | |
2543 | default: | |
2544 | /* Permanent underlying network BIO, start terminating. */ | |
2545 | ch_raise_net_error(ch); | |
2546 | break; | |
2547 | } | |
f538b421 | 2548 | |
f538b421 HL |
2549 | return 1; |
2550 | } | |
2551 | ||
2552 | /* Determine next tick deadline. */ | |
2553 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch) | |
2554 | { | |
2555 | OSSL_TIME deadline; | |
ca711651 | 2556 | int i; |
f538b421 | 2557 | |
c12e1113 | 2558 | if (ossl_quic_channel_is_terminated(ch)) |
df15e990 HL |
2559 | return ossl_time_infinite(); |
2560 | ||
f538b421 HL |
2561 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); |
2562 | if (ossl_time_is_zero(deadline)) | |
2563 | deadline = ossl_time_infinite(); | |
2564 | ||
ca711651 MC |
2565 | /* |
2566 | * If the CC will let us send acks, check the ack deadline for all | |
2567 | * enc_levels that are actually provisioned | |
2568 | */ | |
2569 | if (ch->cc_method->get_tx_allowance(ch->cc_data) > 0) { | |
2570 | for (i = 0; i < QUIC_ENC_LEVEL_NUM; i++) { | |
2571 | if (ossl_qtx_is_enc_level_provisioned(ch->qtx, i)) { | |
2572 | deadline = ossl_time_min(deadline, | |
2573 | ossl_ackm_get_ack_deadline(ch->ackm, | |
2574 | ossl_quic_enc_level_to_pn_space(i))); | |
2575 | } | |
2576 | } | |
2577 | } | |
f538b421 | 2578 | |
c206f2aa HL |
2579 | /* Apply TXP wakeup deadline. */ |
2580 | deadline = ossl_time_min(deadline, | |
2581 | ossl_quic_tx_packetiser_get_deadline(ch->txp)); | |
f538b421 HL |
2582 | |
2583 | /* Is the terminating timer armed? */ | |
c12e1113 | 2584 | if (ossl_quic_channel_is_terminating(ch)) |
f538b421 HL |
2585 | deadline = ossl_time_min(deadline, |
2586 | ch->terminate_deadline); | |
2587 | else if (!ossl_time_is_infinite(ch->idle_deadline)) | |
2588 | deadline = ossl_time_min(deadline, | |
2589 | ch->idle_deadline); | |
2590 | ||
3b1ab5a3 HL |
2591 | /* |
2592 | * When do we need to send an ACK-eliciting packet to reset the idle | |
2593 | * deadline timer for the peer? | |
2594 | */ | |
2595 | if (!ossl_time_is_infinite(ch->ping_deadline)) | |
2596 | deadline = ossl_time_min(deadline, | |
2597 | ch->ping_deadline); | |
2598 | ||
8a65e7a5 HL |
2599 | /* When does the RXKU process complete? */ |
2600 | if (ch->rxku_in_progress) | |
2601 | deadline = ossl_time_min(deadline, ch->rxku_update_end_deadline); | |
2602 | ||
f538b421 HL |
2603 | return deadline; |
2604 | } | |
2605 | ||
2606 | /* | |
2607 | * QUIC Channel: Network BIO Configuration | |
2608 | * ======================================= | |
2609 | */ | |
2610 | ||
2611 | /* Determines whether we can support a given poll descriptor. */ | |
2612 | static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d) | |
2613 | { | |
96014840 TM |
2614 | if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) { |
2615 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); | |
f538b421 | 2616 | return 0; |
96014840 | 2617 | } |
f538b421 HL |
2618 | |
2619 | return 1; | |
2620 | } | |
2621 | ||
2622 | BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch) | |
2623 | { | |
2624 | return ch->net_rbio; | |
2625 | } | |
2626 | ||
2627 | BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch) | |
2628 | { | |
2629 | return ch->net_wbio; | |
2630 | } | |
2631 | ||
be96180a HL |
2632 | static int ch_update_poll_desc(QUIC_CHANNEL *ch, BIO *net_bio, int for_write) |
2633 | { | |
2634 | BIO_POLL_DESCRIPTOR d = {0}; | |
2635 | ||
2636 | if (net_bio == NULL | |
2637 | || (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d)) | |
2638 | || (for_write && !BIO_get_wpoll_descriptor(net_bio, &d))) | |
2639 | /* Non-pollable BIO */ | |
2640 | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; | |
2641 | ||
2642 | if (!validate_poll_descriptor(&d)) | |
2643 | return 0; | |
2644 | ||
2645 | if (for_write) | |
2646 | ossl_quic_reactor_set_poll_w(&ch->rtor, &d); | |
2647 | else | |
2648 | ossl_quic_reactor_set_poll_r(&ch->rtor, &d); | |
2649 | ||
2650 | return 1; | |
2651 | } | |
2652 | ||
2653 | int ossl_quic_channel_update_poll_descriptors(QUIC_CHANNEL *ch) | |
2654 | { | |
2655 | int ok = 1; | |
2656 | ||
2657 | if (!ch_update_poll_desc(ch, ch->net_rbio, /*for_write=*/0)) | |
2658 | ok = 0; | |
2659 | ||
2660 | if (!ch_update_poll_desc(ch, ch->net_wbio, /*for_write=*/1)) | |
2661 | ok = 0; | |
2662 | ||
2663 | return ok; | |
2664 | } | |
2665 | ||
d1ac77b1 HL |
2666 | /* |
2667 | * QUIC_CHANNEL does not ref any BIO it is provided with, nor is any ref | |
2668 | * transferred to it. The caller (i.e., QUIC_CONNECTION) is responsible for | |
2669 | * ensuring the BIO lasts until the channel is freed or the BIO is switched out | |
2670 | * for another BIO by a subsequent successful call to this function. | |
2671 | */ | |
2672 | int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio) | |
f538b421 | 2673 | { |
f538b421 HL |
2674 | if (ch->net_rbio == net_rbio) |
2675 | return 1; | |
2676 | ||
be96180a HL |
2677 | if (!ch_update_poll_desc(ch, net_rbio, /*for_write=*/0)) |
2678 | return 0; | |
f538b421 | 2679 | |
f538b421 HL |
2680 | ossl_quic_demux_set_bio(ch->demux, net_rbio); |
2681 | ch->net_rbio = net_rbio; | |
2682 | return 1; | |
2683 | } | |
2684 | ||
d1ac77b1 | 2685 | int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio) |
f538b421 | 2686 | { |
f538b421 HL |
2687 | if (ch->net_wbio == net_wbio) |
2688 | return 1; | |
2689 | ||
be96180a HL |
2690 | if (!ch_update_poll_desc(ch, net_wbio, /*for_write=*/1)) |
2691 | return 0; | |
f538b421 | 2692 | |
f538b421 HL |
2693 | ossl_qtx_set_bio(ch->qtx, net_wbio); |
2694 | ch->net_wbio = net_wbio; | |
2695 | return 1; | |
2696 | } | |
2697 | ||
2698 | /* | |
2699 | * QUIC Channel: Lifecycle Events | |
2700 | * ============================== | |
2701 | */ | |
f538b421 HL |
2702 | int ossl_quic_channel_start(QUIC_CHANNEL *ch) |
2703 | { | |
b1b06da2 HL |
2704 | if (ch->is_server) |
2705 | /* | |
2706 | * This is not used by the server. The server moves to active | |
2707 | * automatically on receiving an incoming connection. | |
2708 | */ | |
2709 | return 0; | |
2710 | ||
f538b421 HL |
2711 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) |
2712 | /* Calls to connect are idempotent */ | |
2713 | return 1; | |
2714 | ||
2715 | /* Inform QTX of peer address. */ | |
2716 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) | |
2717 | return 0; | |
2718 | ||
2719 | /* Plug in secrets for the Initial EL. */ | |
2720 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
2721 | ch->propq, | |
2722 | &ch->init_dcid, | |
091f532e | 2723 | ch->is_server, |
f538b421 HL |
2724 | ch->qrx, ch->qtx)) |
2725 | return 0; | |
2726 | ||
2727 | /* Change state. */ | |
2728 | ch->state = QUIC_CHANNEL_STATE_ACTIVE; | |
2729 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ | |
2730 | ||
2731 | /* Handshake layer: start (e.g. send CH). */ | |
2723d705 | 2732 | if (!ossl_quic_tls_tick(ch->qtls)) |
f538b421 HL |
2733 | return 0; |
2734 | ||
ccd31037 | 2735 | ossl_quic_reactor_tick(&ch->rtor, 0); /* best effort */ |
f538b421 HL |
2736 | return 1; |
2737 | } | |
2738 | ||
2739 | /* Start a locally initiated connection shutdown. */ | |
40c8c756 HL |
2740 | void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code, |
2741 | const char *app_reason) | |
f538b421 HL |
2742 | { |
2743 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
2744 | ||
c12e1113 | 2745 | if (ossl_quic_channel_is_term_any(ch)) |
f538b421 HL |
2746 | return; |
2747 | ||
e8043229 HL |
2748 | tcause.app = 1; |
2749 | tcause.error_code = app_error_code; | |
40c8c756 HL |
2750 | tcause.reason = app_reason; |
2751 | tcause.reason_len = app_reason != NULL ? strlen(app_reason) : 0; | |
df15e990 | 2752 | ch_start_terminating(ch, &tcause, 0); |
f538b421 HL |
2753 | } |
2754 | ||
2755 | static void free_token(const unsigned char *buf, size_t buf_len, void *arg) | |
2756 | { | |
2757 | OPENSSL_free((unsigned char *)buf); | |
2758 | } | |
2759 | ||
2760 | /* Called when a server asks us to do a retry. */ | |
2761 | static int ch_retry(QUIC_CHANNEL *ch, | |
2762 | const unsigned char *retry_token, | |
2763 | size_t retry_token_len, | |
2764 | const QUIC_CONN_ID *retry_scid) | |
2765 | { | |
2766 | void *buf; | |
2767 | ||
212616ed HL |
2768 | /* |
2769 | * RFC 9000 s. 17.2.5.1: "A client MUST discard a Retry packet that contains | |
2770 | * a SCID field that is identical to the DCID field of its initial packet." | |
2771 | */ | |
2772 | if (ossl_quic_conn_id_eq(&ch->init_dcid, retry_scid)) | |
96014840 | 2773 | return 1; |
212616ed | 2774 | |
f538b421 HL |
2775 | /* We change to using the SCID in the Retry packet as the DCID. */ |
2776 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid)) | |
2777 | return 0; | |
2778 | ||
2779 | /* | |
2780 | * Now we retry. We will release the Retry packet immediately, so copy | |
2781 | * the token. | |
2782 | */ | |
e28f512f | 2783 | if ((buf = OPENSSL_memdup(retry_token, retry_token_len)) == NULL) |
f538b421 HL |
2784 | return 0; |
2785 | ||
f538b421 HL |
2786 | ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, retry_token_len, |
2787 | free_token, NULL); | |
2788 | ||
2789 | ch->retry_scid = *retry_scid; | |
2790 | ch->doing_retry = 1; | |
2791 | ||
2792 | /* | |
2793 | * We need to stimulate the Initial EL to generate the first CRYPTO frame | |
2794 | * again. We can do this most cleanly by simply forcing the ACKM to consider | |
2795 | * the first Initial packet as lost, which it effectively was as the server | |
2796 | * hasn't processed it. This also maintains the desired behaviour with e.g. | |
2797 | * PNs not resetting and so on. | |
2798 | * | |
2799 | * The PN we used initially is always zero, because QUIC does not allow | |
2800 | * repeated retries. | |
2801 | */ | |
2802 | if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL, | |
2803 | /*PN=*/0)) | |
2804 | return 0; | |
2805 | ||
2806 | /* | |
2807 | * Plug in new secrets for the Initial EL. This is the only time we change | |
2808 | * the secrets for an EL after we already provisioned it. | |
2809 | */ | |
2810 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
2811 | ch->propq, | |
2812 | &ch->retry_scid, | |
2813 | /*is_server=*/0, | |
2814 | ch->qrx, ch->qtx)) | |
2815 | return 0; | |
2816 | ||
2817 | return 1; | |
2818 | } | |
2819 | ||
2820 | /* Called when an EL is to be discarded. */ | |
2821 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
2822 | uint32_t enc_level) | |
2823 | { | |
2824 | if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT)) | |
2825 | return 0; | |
2826 | ||
2827 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
2828 | /* Already done. */ | |
2829 | return 1; | |
2830 | ||
2831 | /* Best effort for all of these. */ | |
2832 | ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level); | |
2833 | ossl_qrx_discard_enc_level(ch->qrx, enc_level); | |
2834 | ossl_qtx_discard_enc_level(ch->qtx, enc_level); | |
2835 | ||
2836 | if (enc_level != QUIC_ENC_LEVEL_0RTT) { | |
2837 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
2838 | ||
2839 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
2840 | ||
2841 | /* We should still have crypto streams at this point. */ | |
79534440 HL |
2842 | if (!ossl_assert(ch->crypto_send[pn_space] != NULL) |
2843 | || !ossl_assert(ch->crypto_recv[pn_space] != NULL)) | |
2844 | return 0; | |
f538b421 HL |
2845 | |
2846 | /* Get rid of the crypto stream state for the EL. */ | |
2847 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
2848 | ch->crypto_send[pn_space] = NULL; | |
2849 | ||
2850 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
2851 | ch->crypto_recv[pn_space] = NULL; | |
2852 | } | |
2853 | ||
2854 | ch->el_discarded |= (1U << enc_level); | |
2855 | return 1; | |
2856 | } | |
2857 | ||
2858 | /* Intended to be called by the RXDP. */ | |
2859 | int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch) | |
2860 | { | |
2861 | if (ch->handshake_confirmed) | |
2862 | return 1; | |
2863 | ||
2864 | if (!ch->handshake_complete) { | |
2865 | /* | |
2866 | * Does not make sense for handshake to be confirmed before it is | |
2867 | * completed. | |
2868 | */ | |
2869 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
2870 | OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, | |
2871 | "handshake cannot be confirmed " | |
2872 | "before it is completed"); | |
2873 | return 0; | |
2874 | } | |
2875 | ||
2876 | ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE); | |
2877 | ch->handshake_confirmed = 1; | |
29a541fe | 2878 | ossl_ackm_on_handshake_confirmed(ch->ackm); |
f538b421 HL |
2879 | return 1; |
2880 | } | |
2881 | ||
2882 | /* | |
2883 | * Master function used when we want to start tearing down a connection: | |
2884 | * | |
2885 | * - If the connection is still IDLE we can go straight to TERMINATED; | |
2886 | * | |
2887 | * - If we are already TERMINATED this is a no-op. | |
2888 | * | |
2889 | * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE | |
9bbc5b54 | 2890 | * from the peer (tcause->remote == 1), we move to TERMINATING - DRAINING. |
f538b421 HL |
2891 | * |
2892 | * - If we are TERMINATING - DRAINING, we remain here until the terminating | |
2893 | * timer expires. | |
2894 | * | |
2895 | * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING. | |
2896 | * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note | |
2897 | * that we are considered to have caused a termination if we sent the first | |
2898 | * CONNECTION_CLOSE frame, even if it is caused by a peer protocol | |
2899 | * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to | |
2900 | * TERMINATING - DRAINING. | |
2901 | * | |
2902 | * We record the termination cause structure passed on the first call only. | |
2903 | * Any successive calls have their termination cause data discarded; | |
2904 | * once we start sending a CONNECTION_CLOSE frame, we don't change the details | |
2905 | * in it. | |
6b3b5f9d P |
2906 | * |
2907 | * This conforms to RFC 9000 s. 10.2.1: Closing Connection State: | |
2908 | * To minimize the state that an endpoint maintains for a closing | |
2909 | * connection, endpoints MAY send the exact same packet in response | |
2910 | * to any received packet. | |
2911 | * | |
2912 | * We don't drop any connection state (specifically packet protection keys) | |
2913 | * even though we are permitted to. This conforms to RFC 9000 s. 10.2.1: | |
2914 | * Closing Connection State: | |
2915 | * An endpoint MAY retain packet protection keys for incoming | |
2916 | * packets to allow it to read and process a CONNECTION_CLOSE frame. | |
2917 | * | |
2918 | * Note that we do not conform to these two from the same section: | |
2919 | * An endpoint's selected connection ID and the QUIC version | |
2920 | * are sufficient information to identify packets for a closing | |
2921 | * connection; the endpoint MAY discard all other connection state. | |
2922 | * and: | |
2923 | * An endpoint MAY drop packet protection keys when entering the | |
2924 | * closing state and send a packet containing a CONNECTION_CLOSE | |
2925 | * frame in response to any UDP datagram that is received. | |
f538b421 | 2926 | */ |
40c8c756 HL |
2927 | static void copy_tcause(QUIC_TERMINATE_CAUSE *dst, |
2928 | const QUIC_TERMINATE_CAUSE *src) | |
2929 | { | |
2930 | dst->error_code = src->error_code; | |
2931 | dst->frame_type = src->frame_type; | |
2932 | dst->app = src->app; | |
2933 | dst->remote = src->remote; | |
2934 | ||
2935 | dst->reason = NULL; | |
2936 | dst->reason_len = 0; | |
2937 | ||
2938 | if (src->reason != NULL && src->reason_len > 0) { | |
2939 | size_t l = src->reason_len; | |
2940 | char *r; | |
2941 | ||
2942 | if (l >= SIZE_MAX) | |
2943 | --l; | |
2944 | ||
2945 | /* | |
2946 | * If this fails, dst->reason becomes NULL and we simply do not use a | |
2947 | * reason. This ensures termination is infallible. | |
2948 | */ | |
2949 | dst->reason = r = OPENSSL_memdup(src->reason, l + 1); | |
2950 | if (r == NULL) | |
2951 | return; | |
2952 | ||
2953 | r[l] = '\0'; | |
2954 | dst->reason_len = l; | |
2955 | } | |
2956 | } | |
2957 | ||
f538b421 | 2958 | static void ch_start_terminating(QUIC_CHANNEL *ch, |
df15e990 HL |
2959 | const QUIC_TERMINATE_CAUSE *tcause, |
2960 | int force_immediate) | |
f538b421 | 2961 | { |
549d0a70 HL |
2962 | /* No point sending anything if we haven't sent anything yet. */ |
2963 | if (!ch->have_sent_any_pkt) | |
2964 | force_immediate = 1; | |
2965 | ||
f538b421 | 2966 | switch (ch->state) { |
75b2920a HL |
2967 | default: |
2968 | case QUIC_CHANNEL_STATE_IDLE: | |
40c8c756 | 2969 | copy_tcause(&ch->terminate_cause, tcause); |
75b2920a HL |
2970 | ch_on_terminating_timeout(ch); |
2971 | break; | |
2972 | ||
2973 | case QUIC_CHANNEL_STATE_ACTIVE: | |
40c8c756 | 2974 | copy_tcause(&ch->terminate_cause, tcause); |
df15e990 HL |
2975 | |
2976 | if (!force_immediate) { | |
2977 | ch->state = tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING | |
2978 | : QUIC_CHANNEL_STATE_TERMINATING_CLOSING; | |
a441d08b P |
2979 | /* |
2980 | * RFC 9000 s. 10.2 Immediate Close | |
2981 | * These states SHOULD persist for at least three times | |
2982 | * the current PTO interval as defined in [QUIC-RECOVERY]. | |
2983 | */ | |
df15e990 | 2984 | ch->terminate_deadline |
b212d554 | 2985 | = ossl_time_add(get_time(ch), |
df15e990 HL |
2986 | ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm), |
2987 | 3)); | |
2988 | ||
2989 | if (!tcause->remote) { | |
2990 | OSSL_QUIC_FRAME_CONN_CLOSE f = {0}; | |
2991 | ||
2992 | /* best effort */ | |
2993 | f.error_code = ch->terminate_cause.error_code; | |
2994 | f.frame_type = ch->terminate_cause.frame_type; | |
2995 | f.is_app = ch->terminate_cause.app; | |
40c8c756 HL |
2996 | f.reason = (char *)ch->terminate_cause.reason; |
2997 | f.reason_len = ch->terminate_cause.reason_len; | |
df15e990 | 2998 | ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f); |
afe4a797 P |
2999 | /* |
3000 | * RFC 9000 s. 10.2.2 Draining Connection State: | |
3001 | * An endpoint that receives a CONNECTION_CLOSE frame MAY | |
3002 | * send a single packet containing a CONNECTION_CLOSE | |
3003 | * frame before entering the draining state, using a | |
3004 | * NO_ERROR code if appropriate | |
3005 | */ | |
df15e990 HL |
3006 | ch->conn_close_queued = 1; |
3007 | } | |
3008 | } else { | |
3009 | ch_on_terminating_timeout(ch); | |
75b2920a HL |
3010 | } |
3011 | break; | |
f538b421 | 3012 | |
75b2920a | 3013 | case QUIC_CHANNEL_STATE_TERMINATING_CLOSING: |
df15e990 HL |
3014 | if (force_immediate) |
3015 | ch_on_terminating_timeout(ch); | |
3016 | else if (tcause->remote) | |
afe4a797 P |
3017 | /* |
3018 | * RFC 9000 s. 10.2.2 Draining Connection State: | |
3019 | * An endpoint MAY enter the draining state from the | |
3020 | * closing state if it receives a CONNECTION_CLOSE frame, | |
3021 | * which indicates that the peer is also closing or draining. | |
3022 | */ | |
75b2920a | 3023 | ch->state = QUIC_CHANNEL_STATE_TERMINATING_DRAINING; |
f538b421 | 3024 | |
75b2920a | 3025 | break; |
f538b421 | 3026 | |
75b2920a | 3027 | case QUIC_CHANNEL_STATE_TERMINATING_DRAINING: |
df15e990 HL |
3028 | /* |
3029 | * Other than in the force-immediate case, we remain here until the | |
eb4129e1 | 3030 | * timeout expires. |
df15e990 HL |
3031 | */ |
3032 | if (force_immediate) | |
3033 | ch_on_terminating_timeout(ch); | |
3034 | ||
75b2920a | 3035 | break; |
f538b421 | 3036 | |
75b2920a HL |
3037 | case QUIC_CHANNEL_STATE_TERMINATED: |
3038 | /* No-op. */ | |
3039 | break; | |
f538b421 HL |
3040 | } |
3041 | } | |
3042 | ||
3043 | /* For RXDP use. */ | |
3044 | void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch, | |
3045 | OSSL_QUIC_FRAME_CONN_CLOSE *f) | |
3046 | { | |
3047 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
3048 | ||
3049 | if (!ossl_quic_channel_is_active(ch)) | |
3050 | return; | |
3051 | ||
3052 | tcause.remote = 1; | |
3053 | tcause.app = f->is_app; | |
3054 | tcause.error_code = f->error_code; | |
3055 | tcause.frame_type = f->frame_type; | |
40c8c756 HL |
3056 | tcause.reason = f->reason; |
3057 | tcause.reason_len = f->reason_len; | |
df15e990 HL |
3058 | ch_start_terminating(ch, &tcause, 0); |
3059 | } | |
3060 | ||
eff04652 TM |
3061 | static void free_frame_data(unsigned char *buf, size_t buf_len, void *arg) |
3062 | { | |
3063 | OPENSSL_free(buf); | |
3064 | } | |
3065 | ||
3066 | static int ch_enqueue_retire_conn_id(QUIC_CHANNEL *ch, uint64_t seq_num) | |
3067 | { | |
96014840 | 3068 | BUF_MEM *buf_mem = NULL; |
eff04652 TM |
3069 | WPACKET wpkt; |
3070 | size_t l; | |
3071 | ||
cdd91631 P |
3072 | chan_remove_reset_token(ch, seq_num); |
3073 | ||
eff04652 | 3074 | if ((buf_mem = BUF_MEM_new()) == NULL) |
96014840 | 3075 | goto err; |
eff04652 TM |
3076 | |
3077 | if (!WPACKET_init(&wpkt, buf_mem)) | |
3078 | goto err; | |
3079 | ||
3080 | if (!ossl_quic_wire_encode_frame_retire_conn_id(&wpkt, seq_num)) { | |
3081 | WPACKET_cleanup(&wpkt); | |
3082 | goto err; | |
3083 | } | |
3084 | ||
3085 | WPACKET_finish(&wpkt); | |
3086 | if (!WPACKET_get_total_written(&wpkt, &l)) | |
3087 | goto err; | |
3088 | ||
3089 | if (ossl_quic_cfq_add_frame(ch->cfq, 1, QUIC_PN_SPACE_APP, | |
371c2958 | 3090 | OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, 0, |
eff04652 TM |
3091 | (unsigned char *)buf_mem->data, l, |
3092 | free_frame_data, NULL) == NULL) | |
3093 | goto err; | |
3094 | ||
3095 | buf_mem->data = NULL; | |
3096 | BUF_MEM_free(buf_mem); | |
3097 | return 1; | |
3098 | ||
3099 | err: | |
3100 | ossl_quic_channel_raise_protocol_error(ch, | |
3101 | QUIC_ERR_INTERNAL_ERROR, | |
3102 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, | |
3103 | "internal error enqueueing retire conn id"); | |
3104 | BUF_MEM_free(buf_mem); | |
3105 | return 0; | |
3106 | } | |
3107 | ||
3108 | void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL *ch, | |
3109 | OSSL_QUIC_FRAME_NEW_CONN_ID *f) | |
3110 | { | |
3111 | uint64_t new_remote_seq_num = ch->cur_remote_seq_num; | |
3112 | uint64_t new_retire_prior_to = ch->cur_retire_prior_to; | |
3113 | ||
3114 | if (!ossl_quic_channel_is_active(ch)) | |
3115 | return; | |
3116 | ||
3117 | /* We allow only two active connection ids; first check some constraints */ | |
eff04652 TM |
3118 | if (ch->cur_remote_dcid.id_len == 0) { |
3119 | /* Changing from 0 length connection id is disallowed */ | |
3120 | ossl_quic_channel_raise_protocol_error(ch, | |
3121 | QUIC_ERR_PROTOCOL_VIOLATION, | |
3122 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, | |
3123 | "zero length connection id in use"); | |
3124 | ||
3125 | return; | |
3126 | } | |
3127 | ||
3128 | if (f->seq_num > new_remote_seq_num) | |
3129 | new_remote_seq_num = f->seq_num; | |
3130 | if (f->retire_prior_to > new_retire_prior_to) | |
3131 | new_retire_prior_to = f->retire_prior_to; | |
3132 | ||
985429f4 P |
3133 | /* |
3134 | * RFC 9000-5.1.1: An endpoint MUST NOT provide more connection IDs | |
3135 | * than the peer's limit. | |
3136 | * | |
3137 | * After processing a NEW_CONNECTION_ID frame and adding and retiring | |
3138 | * active connection IDs, if the number of active connection IDs exceeds | |
3139 | * the value advertised in its active_connection_id_limit transport | |
3140 | * parameter, an endpoint MUST close the connection with an error of | |
3141 | * type CONNECTION_ID_LIMIT_ERROR. | |
3142 | */ | |
3143 | if (new_remote_seq_num - new_retire_prior_to > 1) { | |
eff04652 TM |
3144 | ossl_quic_channel_raise_protocol_error(ch, |
3145 | QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, | |
3146 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, | |
3147 | "active_connection_id limit violated"); | |
985429f4 P |
3148 | return; |
3149 | } | |
3150 | ||
3151 | /* | |
3152 | * RFC 9000-5.1.1: An endpoint MAY send connection IDs that temporarily | |
3153 | * exceed a peer's limit if the NEW_CONNECTION_ID frame also requires | |
3154 | * the retirement of any excess, by including a sufficiently large | |
3155 | * value in the Retire Prior To field. | |
3156 | * | |
3157 | * RFC 9000-5.1.2: An endpoint SHOULD allow for sending and tracking | |
3158 | * a number of RETIRE_CONNECTION_ID frames of at least twice the value | |
3159 | * of the active_connection_id_limit transport parameter. An endpoint | |
3160 | * MUST NOT forget a connection ID without retiring it, though it MAY | |
3161 | * choose to treat having connection IDs in need of retirement that | |
3162 | * exceed this limit as a connection error of type CONNECTION_ID_LIMIT_ERROR. | |
3163 | * | |
3164 | * We are a little bit more liberal than the minimum mandated. | |
3165 | */ | |
3166 | if (new_retire_prior_to - ch->cur_retire_prior_to > 10) { | |
3167 | ossl_quic_channel_raise_protocol_error(ch, | |
3168 | QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, | |
3169 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, | |
3170 | "retiring connection id limit violated"); | |
eff04652 TM |
3171 | |
3172 | return; | |
3173 | } | |
3174 | ||
3175 | if (new_remote_seq_num > ch->cur_remote_seq_num) { | |
cdd91631 P |
3176 | /* Add new stateless reset token */ |
3177 | if (!chan_add_reset_token(ch, f->stateless_reset.token, | |
3178 | new_remote_seq_num)) { | |
3179 | ossl_quic_channel_raise_protocol_error( | |
3180 | ch, QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, | |
3181 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, | |
3182 | "unable to store stateless reset token"); | |
3183 | ||
3184 | return; | |
3185 | } | |
eff04652 TM |
3186 | ch->cur_remote_seq_num = new_remote_seq_num; |
3187 | ch->cur_remote_dcid = f->conn_id; | |
3188 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid); | |
3189 | } | |
5cc73695 | 3190 | |
985429f4 P |
3191 | /* |
3192 | * RFC 9000-5.1.2: Upon receipt of an increased Retire Prior To | |
3193 | * field, the peer MUST stop using the corresponding connection IDs | |
3194 | * and retire them with RETIRE_CONNECTION_ID frames before adding the | |
3195 | * newly provided connection ID to the set of active connection IDs. | |
3196 | */ | |
5cc73695 HL |
3197 | |
3198 | /* | |
3199 | * Note: RFC 9000 s. 19.15 says: | |
3200 | * "An endpoint that receives a NEW_CONNECTION_ID frame with a sequence | |
3201 | * number smaller than the Retire Prior To field of a previously received | |
7c793cd3 | 3202 | * NEW_CONNECTION_ID frame MUST send a corresponding |
5cc73695 HL |
3203 | * RETIRE_CONNECTION_ID frame that retires the newly received connection |
3204 | * ID, unless it has already done so for that sequence number." | |
3205 | * | |
3206 | * Since we currently always queue RETIRE_CONN_ID frames based on the Retire | |
3207 | * Prior To field of a NEW_CONNECTION_ID frame immediately upon receiving | |
3208 | * that NEW_CONNECTION_ID frame, by definition this will always be met. | |
3209 | * This may change in future when we change our CID handling. | |
3210 | */ | |
eff04652 TM |
3211 | while (new_retire_prior_to > ch->cur_retire_prior_to) { |
3212 | if (!ch_enqueue_retire_conn_id(ch, ch->cur_retire_prior_to)) | |
3213 | break; | |
3214 | ++ch->cur_retire_prior_to; | |
3215 | } | |
3216 | } | |
3217 | ||
9c3ea4e1 TM |
3218 | static void ch_save_err_state(QUIC_CHANNEL *ch) |
3219 | { | |
3220 | if (ch->err_state == NULL) | |
3221 | ch->err_state = OSSL_ERR_STATE_new(); | |
3222 | ||
3223 | if (ch->err_state == NULL) | |
3224 | return; | |
3225 | ||
3226 | OSSL_ERR_STATE_save(ch->err_state); | |
3227 | } | |
3228 | ||
cdd91631 P |
3229 | static void ch_stateless_reset(QUIC_CHANNEL *ch) |
3230 | { | |
3231 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
3232 | ||
3233 | tcause.error_code = QUIC_ERR_NO_ERROR; | |
3234 | ch_start_terminating(ch, &tcause, 1); | |
3235 | } | |
3236 | ||
df15e990 HL |
3237 | static void ch_raise_net_error(QUIC_CHANNEL *ch) |
3238 | { | |
3239 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
3240 | ||
5c3474ea | 3241 | ch->net_error = 1; |
741170be HL |
3242 | |
3243 | ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR, | |
3244 | "connection terminated due to network error"); | |
9c3ea4e1 | 3245 | ch_save_err_state(ch); |
5c3474ea | 3246 | |
df15e990 HL |
3247 | tcause.error_code = QUIC_ERR_INTERNAL_ERROR; |
3248 | ||
3249 | /* | |
3250 | * Skip Terminating state and go directly to Terminated, no point trying to | |
3251 | * send CONNECTION_CLOSE if we cannot communicate. | |
3252 | */ | |
3253 | ch_start_terminating(ch, &tcause, 1); | |
f538b421 HL |
3254 | } |
3255 | ||
5c3474ea TM |
3256 | int ossl_quic_channel_net_error(QUIC_CHANNEL *ch) |
3257 | { | |
3258 | return ch->net_error; | |
3259 | } | |
3260 | ||
9c3ea4e1 TM |
3261 | void ossl_quic_channel_restore_err_state(QUIC_CHANNEL *ch) |
3262 | { | |
3263 | if (ch == NULL) | |
3264 | return; | |
3265 | ||
3266 | OSSL_ERR_STATE_restore(ch->err_state); | |
3267 | } | |
3268 | ||
741170be HL |
3269 | void ossl_quic_channel_raise_protocol_error_loc(QUIC_CHANNEL *ch, |
3270 | uint64_t error_code, | |
3271 | uint64_t frame_type, | |
3272 | const char *reason, | |
7a2bb210 | 3273 | ERR_STATE *err_state, |
741170be HL |
3274 | const char *src_file, |
3275 | int src_line, | |
3276 | const char *src_func) | |
f538b421 HL |
3277 | { |
3278 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
2b8126d8 TM |
3279 | int err_reason = error_code == QUIC_ERR_INTERNAL_ERROR |
3280 | ? ERR_R_INTERNAL_ERROR : SSL_R_QUIC_PROTOCOL_ERROR; | |
741170be HL |
3281 | const char *err_str = ossl_quic_err_to_string(error_code); |
3282 | const char *err_str_pfx = " (", *err_str_sfx = ")"; | |
3283 | const char *ft_str = NULL; | |
3284 | const char *ft_str_pfx = " (", *ft_str_sfx = ")"; | |
3285 | ||
549d0a70 HL |
3286 | if (ch->protocol_error) |
3287 | /* Only the first call to this function matters. */ | |
3288 | return; | |
3289 | ||
741170be HL |
3290 | if (err_str == NULL) { |
3291 | err_str = ""; | |
3292 | err_str_pfx = ""; | |
3293 | err_str_sfx = ""; | |
3294 | } | |
3295 | ||
7a2bb210 HL |
3296 | /* |
3297 | * If we were provided an underlying error state, restore it and then append | |
3298 | * our ERR on top as a "cover letter" error. | |
3299 | */ | |
3300 | if (err_state != NULL) | |
3301 | OSSL_ERR_STATE_restore(err_state); | |
3302 | ||
741170be HL |
3303 | if (frame_type != 0) { |
3304 | ft_str = ossl_quic_frame_type_to_string(frame_type); | |
3305 | if (ft_str == NULL) { | |
3306 | ft_str = ""; | |
3307 | ft_str_pfx = ""; | |
3308 | ft_str_sfx = ""; | |
3309 | } | |
3310 | ||
3311 | ERR_raise_data(ERR_LIB_SSL, err_reason, | |
3312 | "QUIC error code: 0x%llx%s%s%s " | |
3313 | "(triggered by frame type: 0x%llx%s%s%s), reason: \"%s\"", | |
3314 | (unsigned long long) error_code, | |
3315 | err_str_pfx, err_str, err_str_sfx, | |
3316 | (unsigned long long) frame_type, | |
3317 | ft_str_pfx, ft_str, ft_str_sfx, | |
3318 | reason); | |
3319 | } else { | |
3320 | ERR_raise_data(ERR_LIB_SSL, err_reason, | |
3321 | "QUIC error code: 0x%llx%s%s%s, reason: \"%s\"", | |
3322 | (unsigned long long) error_code, | |
3323 | err_str_pfx, err_str, err_str_sfx, | |
3324 | reason); | |
3325 | } | |
3326 | ||
3327 | if (src_file != NULL) | |
3328 | ERR_set_debug(src_file, src_line, src_func); | |
f538b421 | 3329 | |
2b8126d8 | 3330 | ch_save_err_state(ch); |
9c3ea4e1 | 3331 | |
f538b421 HL |
3332 | tcause.error_code = error_code; |
3333 | tcause.frame_type = frame_type; | |
40c8c756 | 3334 | tcause.reason = reason; |
f2609004 | 3335 | tcause.reason_len = strlen(reason); |
f538b421 | 3336 | |
549d0a70 | 3337 | ch->protocol_error = 1; |
df15e990 | 3338 | ch_start_terminating(ch, &tcause, 0); |
f538b421 HL |
3339 | } |
3340 | ||
3341 | /* | |
3342 | * Called once the terminating timer expires, meaning we move from TERMINATING | |
3343 | * to TERMINATED. | |
3344 | */ | |
3345 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch) | |
3346 | { | |
3347 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
3348 | } | |
3349 | ||
3350 | /* | |
3351 | * Updates our idle deadline. Called when an event happens which should bump the | |
3352 | * idle timeout. | |
3353 | */ | |
3354 | static void ch_update_idle(QUIC_CHANNEL *ch) | |
3355 | { | |
3356 | if (ch->max_idle_timeout == 0) | |
3357 | ch->idle_deadline = ossl_time_infinite(); | |
b056e9fc P |
3358 | else { |
3359 | /* RFC 9000 s. 10.1: Idle Timeout | |
3360 | * To avoid excessively small idle timeout periods, endpoints | |
3361 | * MUST increase the idle timeout period to be at least three | |
3362 | * times the current Probe Timeout (PTO). This allows for | |
3363 | * multiple PTOs to expire, and therefore multiple probes to | |
3364 | * be sent and lost, prior to idle timeout. | |
3365 | */ | |
3366 | OSSL_TIME pto = ossl_ackm_get_pto_duration(ch->ackm); | |
3367 | OSSL_TIME timeout = ossl_time_max(ossl_ms2time(ch->max_idle_timeout), | |
3368 | ossl_time_multiply(pto, 3)); | |
3369 | ||
3370 | ch->idle_deadline = ossl_time_add(get_time(ch), timeout); | |
3371 | } | |
f538b421 HL |
3372 | } |
3373 | ||
3b1ab5a3 HL |
3374 | /* |
3375 | * Updates our ping deadline, which determines when we next generate a ping if | |
3376 | * we don't have any other ACK-eliciting frames to send. | |
3377 | */ | |
3378 | static void ch_update_ping_deadline(QUIC_CHANNEL *ch) | |
3379 | { | |
3380 | if (ch->max_idle_timeout > 0) { | |
3381 | /* | |
9cf091a3 HL |
3382 | * Maximum amount of time without traffic before we send a PING to keep |
3383 | * the connection open. Usually we use max_idle_timeout/2, but ensure | |
3384 | * the period never exceeds the assumed NAT interval to ensure NAT | |
3385 | * devices don't have their state time out (RFC 9000 s. 10.1.2). | |
3b1ab5a3 HL |
3386 | */ |
3387 | OSSL_TIME max_span | |
3388 | = ossl_time_divide(ossl_ms2time(ch->max_idle_timeout), 2); | |
3389 | ||
9cf091a3 | 3390 | max_span = ossl_time_min(max_span, MAX_NAT_INTERVAL); |
3b1ab5a3 HL |
3391 | |
3392 | ch->ping_deadline = ossl_time_add(get_time(ch), max_span); | |
3393 | } else { | |
3394 | ch->ping_deadline = ossl_time_infinite(); | |
3395 | } | |
3396 | } | |
3397 | ||
f538b421 HL |
3398 | /* Called when the idle timeout expires. */ |
3399 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch) | |
3400 | { | |
3401 | /* | |
3402 | * Idle timeout does not have an error code associated with it because a | |
3403 | * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach | |
3404 | * TERMINATED anyway. | |
3405 | */ | |
3406 | ch->terminate_cause.app = 0; | |
3407 | ch->terminate_cause.error_code = UINT64_MAX; | |
3408 | ch->terminate_cause.frame_type = 0; | |
3409 | ||
3410 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
3411 | } | |
b1b06da2 HL |
3412 | |
3413 | /* Called when we, as a server, get a new incoming connection. */ | |
3414 | static int ch_server_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer, | |
3415 | const QUIC_CONN_ID *peer_scid, | |
3416 | const QUIC_CONN_ID *peer_dcid) | |
3417 | { | |
3418 | if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server)) | |
3419 | return 0; | |
3420 | ||
3421 | /* Generate a SCID we will use for the connection. */ | |
3422 | if (!gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, | |
bbc97540 | 3423 | &ch->cur_local_cid)) |
b1b06da2 HL |
3424 | return 0; |
3425 | ||
3426 | /* Note our newly learnt peer address and CIDs. */ | |
3427 | ch->cur_peer_addr = *peer; | |
3428 | ch->init_dcid = *peer_dcid; | |
3429 | ch->cur_remote_dcid = *peer_scid; | |
3430 | ||
3431 | /* Inform QTX of peer address. */ | |
3432 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) | |
3433 | return 0; | |
3434 | ||
3435 | /* Inform TXP of desired CIDs. */ | |
3436 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid)) | |
3437 | return 0; | |
3438 | ||
bbc97540 | 3439 | if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid)) |
b1b06da2 HL |
3440 | return 0; |
3441 | ||
3442 | /* Plug in secrets for the Initial EL. */ | |
3443 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
3444 | ch->propq, | |
3445 | &ch->init_dcid, | |
3446 | /*is_server=*/1, | |
3447 | ch->qrx, ch->qtx)) | |
3448 | return 0; | |
3449 | ||
bbc97540 TM |
3450 | /* Register our local CID in the DEMUX. */ |
3451 | if (!ossl_qrx_add_dst_conn_id(ch->qrx, &ch->cur_local_cid)) | |
b1b06da2 HL |
3452 | return 0; |
3453 | ||
3454 | /* Change state. */ | |
3455 | ch->state = QUIC_CHANNEL_STATE_ACTIVE; | |
3456 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ | |
3457 | return 1; | |
3458 | } | |
d03fe5de MC |
3459 | |
3460 | SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch) | |
3461 | { | |
3462 | return ch->tls; | |
3463 | } | |
2dbc39de | 3464 | |
e8fe7a21 HL |
3465 | static int ch_init_new_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs, |
3466 | int can_send, int can_recv) | |
3467 | { | |
3468 | uint64_t rxfc_wnd; | |
3469 | int server_init = ossl_quic_stream_is_server_init(qs); | |
3470 | int local_init = (ch->is_server == server_init); | |
3471 | int is_uni = !ossl_quic_stream_is_bidi(qs); | |
3472 | ||
db2f98c4 | 3473 | if (can_send) |
6ba2edb7 TM |
3474 | if ((qs->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL) |
3475 | goto err; | |
e8fe7a21 | 3476 | |
db2f98c4 | 3477 | if (can_recv) |
a02571a0 TM |
3478 | if ((qs->rstream = ossl_quic_rstream_new(NULL, NULL, 0)) == NULL) |
3479 | goto err; | |
e8fe7a21 HL |
3480 | |
3481 | /* TXFC */ | |
3482 | if (!ossl_quic_txfc_init(&qs->txfc, &ch->conn_txfc)) | |
3483 | goto err; | |
3484 | ||
3485 | if (ch->got_remote_transport_params) { | |
3486 | /* | |
3487 | * If we already got peer TPs we need to apply the initial CWM credit | |
3488 | * now. If we didn't already get peer TPs this will be done | |
3489 | * automatically for all extant streams when we do. | |
3490 | */ | |
3491 | if (can_send) { | |
3492 | uint64_t cwm; | |
3493 | ||
3494 | if (is_uni) | |
3495 | cwm = ch->rx_init_max_stream_data_uni; | |
3496 | else if (local_init) | |
3497 | cwm = ch->rx_init_max_stream_data_bidi_local; | |
3498 | else | |
3499 | cwm = ch->rx_init_max_stream_data_bidi_remote; | |
3500 | ||
3501 | ossl_quic_txfc_bump_cwm(&qs->txfc, cwm); | |
3502 | } | |
3503 | } | |
3504 | ||
3505 | /* RXFC */ | |
3506 | if (!can_recv) | |
3507 | rxfc_wnd = 0; | |
3508 | else if (is_uni) | |
3509 | rxfc_wnd = ch->tx_init_max_stream_data_uni; | |
3510 | else if (local_init) | |
3511 | rxfc_wnd = ch->tx_init_max_stream_data_bidi_local; | |
3512 | else | |
3513 | rxfc_wnd = ch->tx_init_max_stream_data_bidi_remote; | |
3514 | ||
3515 | if (!ossl_quic_rxfc_init(&qs->rxfc, &ch->conn_rxfc, | |
3516 | rxfc_wnd, | |
3517 | DEFAULT_STREAM_RXFC_MAX_WND_MUL * rxfc_wnd, | |
3518 | get_time, ch)) | |
3519 | goto err; | |
3520 | ||
3521 | return 1; | |
3522 | ||
3523 | err: | |
3524 | ossl_quic_sstream_free(qs->sstream); | |
3525 | qs->sstream = NULL; | |
3526 | ossl_quic_rstream_free(qs->rstream); | |
3527 | qs->rstream = NULL; | |
3528 | return 0; | |
3529 | } | |
3530 | ||
9d6bd3d3 HL |
3531 | static uint64_t *ch_get_local_stream_next_ordinal_ptr(QUIC_CHANNEL *ch, |
3532 | int is_uni) | |
3533 | { | |
3534 | return is_uni ? &ch->next_local_stream_ordinal_uni | |
3535 | : &ch->next_local_stream_ordinal_bidi; | |
3536 | } | |
3537 | ||
3538 | int ossl_quic_channel_is_new_local_stream_admissible(QUIC_CHANNEL *ch, | |
3539 | int is_uni) | |
3540 | { | |
3541 | uint64_t *p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni); | |
3542 | ||
3543 | return ossl_quic_stream_map_is_local_allowed_by_stream_limit(&ch->qsm, | |
3544 | *p_next_ordinal, | |
3545 | is_uni); | |
3546 | } | |
3547 | ||
f20fdd16 | 3548 | QUIC_STREAM *ossl_quic_channel_new_stream_local(QUIC_CHANNEL *ch, int is_uni) |
2dbc39de HL |
3549 | { |
3550 | QUIC_STREAM *qs; | |
22b1a96f | 3551 | int type; |
2dbc39de HL |
3552 | uint64_t stream_id, *p_next_ordinal; |
3553 | ||
22b1a96f HL |
3554 | type = ch->is_server ? QUIC_STREAM_INITIATOR_SERVER |
3555 | : QUIC_STREAM_INITIATOR_CLIENT; | |
2dbc39de | 3556 | |
9d6bd3d3 HL |
3557 | p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni); |
3558 | ||
3559 | if (is_uni) | |
2dbc39de | 3560 | type |= QUIC_STREAM_DIR_UNI; |
9d6bd3d3 | 3561 | else |
2dbc39de | 3562 | type |= QUIC_STREAM_DIR_BIDI; |
2dbc39de HL |
3563 | |
3564 | if (*p_next_ordinal >= ((uint64_t)1) << 62) | |
3565 | return NULL; | |
3566 | ||
3567 | stream_id = ((*p_next_ordinal) << 2) | type; | |
3568 | ||
3569 | if ((qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id, type)) == NULL) | |
3570 | return NULL; | |
3571 | ||
e8fe7a21 HL |
3572 | /* Locally-initiated stream, so we always want a send buffer. */ |
3573 | if (!ch_init_new_stream(ch, qs, /*can_send=*/1, /*can_recv=*/!is_uni)) | |
3574 | goto err; | |
3575 | ||
2dbc39de HL |
3576 | ++*p_next_ordinal; |
3577 | return qs; | |
e8fe7a21 HL |
3578 | |
3579 | err: | |
3580 | ossl_quic_stream_map_release(&ch->qsm, qs); | |
3581 | return NULL; | |
2dbc39de | 3582 | } |
f20fdd16 HL |
3583 | |
3584 | QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch, | |
3585 | uint64_t stream_id) | |
3586 | { | |
3587 | uint64_t peer_role; | |
e8fe7a21 | 3588 | int is_uni; |
f20fdd16 HL |
3589 | QUIC_STREAM *qs; |
3590 | ||
3591 | peer_role = ch->is_server | |
3592 | ? QUIC_STREAM_INITIATOR_CLIENT | |
3593 | : QUIC_STREAM_INITIATOR_SERVER; | |
3594 | ||
3595 | if ((stream_id & QUIC_STREAM_INITIATOR_MASK) != peer_role) | |
3596 | return NULL; | |
3597 | ||
e8fe7a21 HL |
3598 | is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI); |
3599 | ||
f20fdd16 HL |
3600 | qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id, |
3601 | stream_id & (QUIC_STREAM_INITIATOR_MASK | |
3602 | | QUIC_STREAM_DIR_MASK)); | |
3603 | if (qs == NULL) | |
3604 | return NULL; | |
3605 | ||
e8fe7a21 HL |
3606 | if (!ch_init_new_stream(ch, qs, /*can_send=*/!is_uni, /*can_recv=*/1)) |
3607 | goto err; | |
3608 | ||
995ff282 HL |
3609 | if (ch->incoming_stream_auto_reject) |
3610 | ossl_quic_channel_reject_stream(ch, qs); | |
3611 | else | |
3612 | ossl_quic_stream_map_push_accept_queue(&ch->qsm, qs); | |
3613 | ||
f20fdd16 | 3614 | return qs; |
e8fe7a21 HL |
3615 | |
3616 | err: | |
3617 | ossl_quic_stream_map_release(&ch->qsm, qs); | |
3618 | return NULL; | |
f20fdd16 | 3619 | } |
995ff282 HL |
3620 | |
3621 | void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL *ch, | |
3622 | int enable, | |
3623 | uint64_t aec) | |
3624 | { | |
3625 | ch->incoming_stream_auto_reject = (enable != 0); | |
3626 | ch->incoming_stream_auto_reject_aec = aec; | |
3627 | } | |
3628 | ||
3629 | void ossl_quic_channel_reject_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs) | |
3630 | { | |
e8b9f632 HL |
3631 | ossl_quic_stream_map_stop_sending_recv_part(&ch->qsm, qs, |
3632 | ch->incoming_stream_auto_reject_aec); | |
995ff282 | 3633 | |
e8b9f632 HL |
3634 | ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, qs, |
3635 | ch->incoming_stream_auto_reject_aec); | |
995ff282 HL |
3636 | qs->deleted = 1; |
3637 | ||
3638 | ossl_quic_stream_map_update_state(&ch->qsm, qs); | |
3639 | } | |
bbc97540 TM |
3640 | |
3641 | /* Replace local connection ID in TXP and DEMUX for testing purposes. */ | |
3642 | int ossl_quic_channel_replace_local_cid(QUIC_CHANNEL *ch, | |
3643 | const QUIC_CONN_ID *conn_id) | |
3644 | { | |
3645 | /* Remove the current local CID from the DEMUX. */ | |
3646 | if (!ossl_qrx_remove_dst_conn_id(ch->qrx, &ch->cur_local_cid)) | |
3647 | return 0; | |
3648 | ch->cur_local_cid = *conn_id; | |
3649 | /* Set in the TXP, used only for long header packets. */ | |
3650 | if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid)) | |
3651 | return 0; | |
3652 | /* Register our new local CID in the DEMUX. */ | |
3653 | if (!ossl_qrx_add_dst_conn_id(ch->qrx, &ch->cur_local_cid)) | |
3654 | return 0; | |
3655 | return 1; | |
3656 | } | |
5cf99b40 MC |
3657 | |
3658 | void ossl_quic_channel_set_msg_callback(QUIC_CHANNEL *ch, | |
3659 | ossl_msg_cb msg_callback, | |
c2786c8e | 3660 | SSL *msg_callback_ssl) |
5cf99b40 MC |
3661 | { |
3662 | ch->msg_callback = msg_callback; | |
c2786c8e MC |
3663 | ch->msg_callback_ssl = msg_callback_ssl; |
3664 | ossl_qtx_set_msg_callback(ch->qtx, msg_callback, msg_callback_ssl); | |
5cf99b40 | 3665 | ossl_quic_tx_packetiser_set_msg_callback(ch->txp, msg_callback, |
c2786c8e MC |
3666 | msg_callback_ssl); |
3667 | ossl_qrx_set_msg_callback(ch->qrx, msg_callback, msg_callback_ssl); | |
5cf99b40 MC |
3668 | } |
3669 | ||
3670 | void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL *ch, | |
3671 | void *msg_callback_arg) | |
3672 | { | |
3673 | ch->msg_callback_arg = msg_callback_arg; | |
3674 | ossl_qtx_set_msg_callback_arg(ch->qtx, msg_callback_arg); | |
3675 | ossl_quic_tx_packetiser_set_msg_callback_arg(ch->txp, msg_callback_arg); | |
3676 | ossl_qrx_set_msg_callback_arg(ch->qrx, msg_callback_arg); | |
3677 | } | |
16f3b542 HL |
3678 | |
3679 | void ossl_quic_channel_set_txku_threshold_override(QUIC_CHANNEL *ch, | |
3680 | uint64_t tx_pkt_threshold) | |
3681 | { | |
3682 | ch->txku_threshold_override = tx_pkt_threshold; | |
3683 | } | |
3684 | ||
3685 | uint64_t ossl_quic_channel_get_tx_key_epoch(QUIC_CHANNEL *ch) | |
3686 | { | |
3687 | return ossl_qtx_get_key_epoch(ch->qtx); | |
3688 | } | |
3689 | ||
3690 | uint64_t ossl_quic_channel_get_rx_key_epoch(QUIC_CHANNEL *ch) | |
3691 | { | |
3692 | return ossl_qrx_get_key_epoch(ch->qrx); | |
3693 | } | |
692a3cab HL |
3694 | |
3695 | int ossl_quic_channel_trigger_txku(QUIC_CHANNEL *ch) | |
3696 | { | |
3697 | if (!txku_allowed(ch)) | |
3698 | return 0; | |
3699 | ||
3700 | ch->ku_locally_initiated = 1; | |
3701 | ch_trigger_txku(ch); | |
3702 | return 1; | |
3703 | } | |
9ff3a99e HL |
3704 | |
3705 | int ossl_quic_channel_ping(QUIC_CHANNEL *ch) | |
3706 | { | |
3707 | int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level); | |
3708 | ||
3709 | ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space); | |
3710 | ||
3711 | return 1; | |
3712 | } | |
03b38595 HL |
3713 | |
3714 | void ossl_quic_channel_set_inhibit_tick(QUIC_CHANNEL *ch, int inhibit) | |
3715 | { | |
3716 | ch->inhibit_tick = (inhibit != 0); | |
3717 | } | |
17340e87 HL |
3718 | |
3719 | uint16_t ossl_quic_channel_get_diag_num_rx_ack(QUIC_CHANNEL *ch) | |
3720 | { | |
3721 | return ch->diag_num_rx_ack; | |
3722 | } | |
ed75eb32 HL |
3723 | |
3724 | void ossl_quic_channel_get_diag_local_cid(QUIC_CHANNEL *ch, QUIC_CONN_ID *cid) | |
3725 | { | |
3726 | *cid = ch->cur_local_cid; | |
3727 | } |