]>
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 | ||
10 | #include "internal/quic_channel.h" | |
11 | #include "internal/quic_error.h" | |
12 | #include "internal/quic_rx_depack.h" | |
13 | #include "../ssl_local.h" | |
14 | #include "quic_channel_local.h" | |
15 | #include <openssl/rand.h> | |
16 | ||
17 | #define INIT_DCID_LEN 8 | |
18 | #define INIT_CRYPTO_BUF_LEN 8192 | |
19 | #define INIT_APP_BUF_LEN 8192 | |
20 | ||
21 | static int ch_rx(QUIC_CHANNEL *ch); | |
22 | static int ch_tx(QUIC_CHANNEL *ch); | |
23 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg); | |
24 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch); | |
25 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch); | |
26 | static int ch_retry(QUIC_CHANNEL *ch, | |
27 | const unsigned char *retry_token, | |
28 | size_t retry_token_len, | |
29 | const QUIC_CONN_ID *retry_scid); | |
30 | static void ch_cleanup(QUIC_CHANNEL *ch); | |
31 | static int ch_generate_transport_params(QUIC_CHANNEL *ch); | |
32 | static int ch_on_transport_params(const unsigned char *params, | |
33 | size_t params_len, | |
34 | void *arg); | |
35 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code); | |
36 | static int ch_on_handshake_complete(void *arg); | |
37 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
38 | uint32_t suite_id, EVP_MD *md, | |
39 | const unsigned char *secret, | |
40 | size_t secret_len, | |
41 | void *arg); | |
42 | static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len, | |
43 | size_t *bytes_read, void *arg); | |
44 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream); | |
45 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
46 | size_t *consumed, void *arg); | |
47 | static OSSL_TIME get_time(void *arg); | |
48 | static uint64_t get_stream_limit(int uni, void *arg); | |
49 | static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg); | |
50 | static int ch_retry(QUIC_CHANNEL *ch, | |
51 | const unsigned char *retry_token, | |
52 | size_t retry_token_len, | |
53 | const QUIC_CONN_ID *retry_scid); | |
54 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
55 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
56 | uint32_t enc_level); | |
57 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch); | |
58 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
59 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch); | |
60 | static void ch_start_terminating(QUIC_CHANNEL *ch, | |
61 | const QUIC_TERMINATE_CAUSE *tcause); | |
62 | ||
63 | static int gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len, QUIC_CONN_ID *cid) | |
64 | { | |
65 | if (len > QUIC_MAX_CONN_ID_LEN) | |
66 | return 0; | |
67 | ||
68 | cid->id_len = (unsigned char)len; | |
69 | ||
70 | if (RAND_bytes_ex(libctx, cid->id, len, len * 8) != 1) { | |
71 | cid->id_len = 0; | |
72 | return 0; | |
73 | } | |
74 | ||
75 | return 1; | |
76 | } | |
77 | ||
78 | /* | |
79 | * QUIC Channel Initialization and Teardown | |
80 | * ======================================== | |
81 | */ | |
82 | static int ch_init(QUIC_CHANNEL *ch) | |
83 | { | |
84 | OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0}; | |
85 | OSSL_QTX_ARGS qtx_args = {0}; | |
86 | OSSL_QRX_ARGS qrx_args = {0}; | |
87 | QUIC_DHS_ARGS dhs_args = {0}; | |
88 | uint32_t pn_space; | |
89 | ||
6292519c | 90 | /* TODO(QUIC): This is only applicable to clients. */ |
f538b421 HL |
91 | if (!gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, &ch->init_dcid)) |
92 | goto err; | |
93 | ||
94 | /* We plug in a network write BIO to the QTX later when we get one. */ | |
95 | qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN; | |
96 | ch->rx_max_udp_payload_size = qtx_args.mdpl; | |
97 | ||
98 | ch->qtx = ossl_qtx_new(&qtx_args); | |
99 | if (ch->qtx == NULL) | |
100 | goto err; | |
101 | ||
102 | ch->txpim = ossl_quic_txpim_new(); | |
103 | if (ch->txpim == NULL) | |
104 | goto err; | |
105 | ||
106 | ch->cfq = ossl_quic_cfq_new(); | |
107 | if (ch->cfq == NULL) | |
108 | goto err; | |
109 | ||
110 | if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL)) | |
111 | goto err; | |
112 | ||
113 | if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL, | |
114 | 2 * 1024 * 1024, | |
115 | 10 * 1024 * 1024, | |
116 | get_time, NULL)) | |
117 | goto err; | |
118 | ||
119 | if (!ossl_statm_init(&ch->statm)) | |
120 | goto err; | |
121 | ||
122 | ch->have_statm = 1; | |
123 | ch->cc_method = &ossl_cc_dummy_method; | |
124 | if ((ch->cc_data = ch->cc_method->new(NULL, NULL, NULL)) == NULL) | |
125 | goto err; | |
126 | ||
127 | if ((ch->ackm = ossl_ackm_new(get_time, NULL, &ch->statm, | |
128 | ch->cc_method, ch->cc_data)) == NULL) | |
129 | goto err; | |
130 | ||
131 | if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch)) | |
132 | goto err; | |
133 | ||
134 | ch->have_qsm = 1; | |
135 | ||
136 | /* We use a zero-length SCID. */ | |
137 | txp_args.cur_dcid = ch->init_dcid; | |
138 | txp_args.ack_delay_exponent = 3; | |
139 | txp_args.qtx = ch->qtx; | |
140 | txp_args.txpim = ch->txpim; | |
141 | txp_args.cfq = ch->cfq; | |
142 | txp_args.ackm = ch->ackm; | |
143 | txp_args.qsm = &ch->qsm; | |
144 | txp_args.conn_txfc = &ch->conn_txfc; | |
145 | txp_args.conn_rxfc = &ch->conn_rxfc; | |
146 | txp_args.cc_method = ch->cc_method; | |
147 | txp_args.cc_data = ch->cc_data; | |
148 | txp_args.now = get_time; | |
149 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
150 | ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_BUF_LEN); | |
151 | if (ch->crypto_send[pn_space] == NULL) | |
152 | goto err; | |
153 | ||
154 | txp_args.crypto[pn_space] = ch->crypto_send[pn_space]; | |
155 | } | |
156 | ||
157 | ch->txp = ossl_quic_tx_packetiser_new(&txp_args); | |
158 | if (ch->txp == NULL) | |
159 | goto err; | |
160 | ||
161 | if ((ch->demux = ossl_quic_demux_new(/*BIO=*/NULL, /*Short CID Len=*/0, | |
d7668ff2 | 162 | get_time, NULL)) == NULL) |
f538b421 HL |
163 | goto err; |
164 | ||
165 | qrx_args.demux = ch->demux; | |
166 | qrx_args.short_conn_id_len = 0; /* We use a zero-length SCID. */ | |
167 | qrx_args.max_deferred = 32; | |
168 | ||
169 | if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL) | |
170 | goto err; | |
171 | ||
172 | if (!ossl_qrx_set_early_validation_cb(ch->qrx, | |
173 | rx_early_validate, | |
174 | ch)) | |
175 | goto err; | |
176 | ||
177 | if (!ossl_qrx_add_dst_conn_id(ch->qrx, &txp_args.cur_scid)) | |
178 | goto err; | |
179 | ||
180 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
181 | ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL); | |
182 | if (ch->crypto_recv[pn_space] == NULL) | |
183 | goto err; | |
184 | } | |
185 | ||
186 | if ((ch->stream0 = ossl_quic_stream_map_alloc(&ch->qsm, 0, | |
187 | QUIC_STREAM_INITIATOR_CLIENT | |
188 | | QUIC_STREAM_DIR_BIDI)) == NULL) | |
189 | goto err; | |
190 | ||
191 | if ((ch->stream0->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL) | |
192 | goto err; | |
193 | ||
194 | if ((ch->stream0->rstream = ossl_quic_rstream_new(NULL, NULL)) == NULL) | |
195 | goto err; | |
196 | ||
197 | if (!ossl_quic_txfc_init(&ch->stream0->txfc, &ch->conn_txfc)) | |
198 | goto err; | |
199 | ||
200 | if (!ossl_quic_rxfc_init(&ch->stream0->rxfc, &ch->conn_rxfc, | |
201 | 1 * 1024 * 1024, | |
202 | 5 * 1024 * 1024, | |
203 | get_time, NULL)) | |
204 | goto err; | |
205 | ||
206 | /* Plug in the dummy handshake layer. */ | |
207 | dhs_args.crypto_send_cb = ch_on_crypto_send; | |
208 | dhs_args.crypto_send_cb_arg = ch; | |
209 | dhs_args.crypto_recv_cb = ch_on_crypto_recv; | |
210 | dhs_args.crypto_recv_cb_arg = ch; | |
211 | dhs_args.yield_secret_cb = ch_on_handshake_yield_secret; | |
212 | dhs_args.yield_secret_cb_arg = ch; | |
213 | dhs_args.got_transport_params_cb = ch_on_transport_params; | |
214 | dhs_args.got_transport_params_cb_arg= ch; | |
215 | dhs_args.handshake_complete_cb = ch_on_handshake_complete; | |
216 | dhs_args.handshake_complete_cb_arg = ch; | |
217 | dhs_args.alert_cb = ch_on_handshake_alert; | |
218 | dhs_args.alert_cb_arg = ch; | |
219 | ||
220 | if ((ch->dhs = ossl_quic_dhs_new(&dhs_args)) == NULL) | |
221 | goto err; | |
222 | ||
223 | /* | |
224 | * Determine the QUIC Transport Parameters and serialize the transport | |
225 | * parameters block. (For servers, we do this later as we must defer | |
226 | * generation until we have received the client's transport parameters.) | |
227 | */ | |
228 | if (!ch->is_server && !ch_generate_transport_params(ch)) | |
229 | goto err; | |
230 | ||
231 | ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY; | |
232 | ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP; | |
233 | ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT; | |
234 | ch->max_idle_timeout = QUIC_DEFAULT_IDLE_TIMEOUT; | |
235 | ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL; | |
236 | ch_update_idle(ch); | |
237 | ossl_quic_reactor_init(&ch->rtor, ch_tick, ch, | |
238 | ch_determine_next_tick_deadline(ch)); | |
239 | return 1; | |
240 | ||
241 | err: | |
242 | ch_cleanup(ch); | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static void ch_cleanup(QUIC_CHANNEL *ch) | |
247 | { | |
248 | uint32_t pn_space; | |
249 | ||
250 | if (ch->ackm != NULL) | |
251 | for (pn_space = QUIC_PN_SPACE_INITIAL; | |
252 | pn_space < QUIC_PN_SPACE_NUM; | |
253 | ++pn_space) | |
254 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
255 | ||
256 | ossl_quic_tx_packetiser_free(ch->txp); | |
257 | ossl_quic_txpim_free(ch->txpim); | |
258 | ossl_quic_cfq_free(ch->cfq); | |
259 | ossl_qtx_free(ch->qtx); | |
260 | if (ch->cc_data != NULL) | |
261 | ch->cc_method->free(ch->cc_data); | |
262 | if (ch->have_statm) | |
263 | ossl_statm_destroy(&ch->statm); | |
264 | ossl_ackm_free(ch->ackm); | |
265 | ||
266 | if (ch->stream0 != NULL) { | |
267 | assert(ch->have_qsm); | |
268 | ossl_quic_stream_map_release(&ch->qsm, ch->stream0); /* frees sstream */ | |
269 | } | |
270 | ||
271 | if (ch->have_qsm) | |
272 | ossl_quic_stream_map_cleanup(&ch->qsm); | |
273 | ||
274 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
275 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
276 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
277 | } | |
278 | ||
279 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
280 | ch->qrx_pkt = NULL; | |
281 | ||
282 | ossl_quic_dhs_free(ch->dhs); | |
283 | ossl_qrx_free(ch->qrx); | |
284 | ossl_quic_demux_free(ch->demux); | |
285 | OPENSSL_free(ch->local_transport_params); | |
f538b421 HL |
286 | } |
287 | ||
288 | QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args) | |
289 | { | |
290 | QUIC_CHANNEL *ch = NULL; | |
291 | ||
292 | if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL) | |
293 | return NULL; | |
294 | ||
295 | ch->libctx = args->libctx; | |
296 | ch->propq = args->propq; | |
297 | ch->is_server = args->is_server; | |
298 | ||
299 | if (!ch_init(ch)) { | |
300 | OPENSSL_free(ch); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | return ch; | |
305 | } | |
306 | ||
307 | void ossl_quic_channel_free(QUIC_CHANNEL *ch) | |
308 | { | |
309 | if (ch == NULL) | |
310 | return; | |
311 | ||
312 | ch_cleanup(ch); | |
313 | OPENSSL_free(ch); | |
314 | } | |
315 | ||
316 | int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr) | |
317 | { | |
318 | *peer_addr = ch->cur_peer_addr; | |
319 | return 1; | |
320 | } | |
321 | ||
322 | int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr) | |
323 | { | |
324 | ch->cur_peer_addr = *peer_addr; | |
325 | return 1; | |
326 | } | |
327 | ||
328 | QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch) | |
329 | { | |
330 | return &ch->rtor; | |
331 | } | |
332 | ||
333 | QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch) | |
334 | { | |
335 | return &ch->qsm; | |
336 | } | |
337 | ||
338 | OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch) | |
339 | { | |
340 | return &ch->statm; | |
341 | } | |
342 | ||
343 | QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch, | |
344 | uint64_t stream_id) | |
345 | { | |
346 | return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id); | |
347 | } | |
348 | ||
349 | int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch) | |
350 | { | |
351 | return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE; | |
352 | } | |
353 | ||
354 | int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch) | |
355 | { | |
356 | return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING | |
357 | || ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING; | |
358 | } | |
359 | ||
360 | int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch) | |
361 | { | |
362 | return ch->state == QUIC_CHANNEL_STATE_TERMINATED; | |
363 | } | |
364 | ||
365 | int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch) | |
366 | { | |
367 | return ossl_quic_channel_is_terminating(ch) | |
368 | || ossl_quic_channel_is_terminated(ch); | |
369 | } | |
370 | ||
371 | int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch) | |
372 | { | |
373 | return ch->handshake_complete; | |
374 | } | |
375 | ||
376 | /* | |
377 | * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components | |
378 | * ================================================================ | |
379 | */ | |
380 | ||
381 | /* Used by various components. */ | |
382 | static OSSL_TIME get_time(void *arg) | |
383 | { | |
384 | return ossl_time_now(); | |
385 | } | |
386 | ||
387 | /* Used by QSM. */ | |
388 | static uint64_t get_stream_limit(int uni, void *arg) | |
389 | { | |
390 | QUIC_CHANNEL *ch = arg; | |
391 | ||
392 | return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi; | |
393 | } | |
394 | ||
395 | /* | |
396 | * Called by QRX to determine if a packet is potentially invalid before trying | |
397 | * to decrypt it. | |
398 | */ | |
399 | static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg) | |
400 | { | |
401 | QUIC_CHANNEL *ch = arg; | |
402 | ||
403 | /* Potential duplicates should not be processed. */ | |
404 | if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space)) | |
405 | return 0; | |
406 | ||
407 | return 1; | |
408 | } | |
409 | ||
410 | /* | |
411 | * QUIC Channel: Handshake Layer Event Handling | |
412 | * ============================================ | |
413 | */ | |
414 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
415 | size_t *consumed, void *arg) | |
416 | { | |
417 | int ret; | |
418 | QUIC_CHANNEL *ch = arg; | |
419 | uint32_t enc_level = ch->tx_enc_level; | |
420 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
421 | QUIC_SSTREAM *sstream = ch->crypto_send[pn_space]; | |
422 | ||
423 | if (!ossl_assert(sstream != NULL)) | |
424 | return 0; | |
425 | ||
426 | ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed); | |
427 | return ret; | |
428 | } | |
429 | ||
430 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream) | |
431 | { | |
432 | size_t avail = 0; | |
433 | int is_fin = 0; | |
434 | ||
435 | if (rstream == NULL) | |
436 | return 1; | |
437 | ||
438 | if (!ossl_quic_rstream_available(rstream, &avail, &is_fin)) | |
439 | return 0; | |
440 | ||
441 | return avail == 0; | |
442 | } | |
443 | ||
444 | static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len, | |
445 | size_t *bytes_read, void *arg) | |
446 | { | |
447 | QUIC_CHANNEL *ch = arg; | |
448 | QUIC_RSTREAM *rstream; | |
449 | int is_fin = 0; /* crypto stream is never finished, so we don't use this */ | |
450 | uint32_t i; | |
451 | ||
452 | /* | |
453 | * After we move to a later EL we must not allow our peer to send any new | |
454 | * bytes in the crypto stream on a previous EL. Retransmissions of old bytes | |
455 | * are allowed. | |
456 | * | |
457 | * In practice we will only move to a new EL when we have consumed all bytes | |
458 | * which should be sent on the crypto stream at a previous EL. For example, | |
459 | * the Handshake EL should not be provisioned until we have completely | |
460 | * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output | |
461 | * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a | |
462 | * given EL is available we simply ensure we have not received any further | |
463 | * bytes at a lower EL. | |
464 | */ | |
465 | for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->tx_enc_level; ++i) | |
466 | if (i != QUIC_ENC_LEVEL_0RTT && | |
467 | !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { | |
468 | /* Protocol violation (RFC 9001 s. 4.1.3) */ | |
469 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
470 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
471 | "crypto stream data in wrong EL"); | |
472 | return 0; | |
473 | } | |
474 | ||
475 | rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->tx_enc_level)]; | |
476 | if (rstream == NULL) | |
477 | return 0; | |
478 | ||
479 | return ossl_quic_rstream_read(rstream, buf, buf_len, bytes_read, | |
480 | &is_fin); | |
481 | } | |
482 | ||
483 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
484 | uint32_t suite_id, EVP_MD *md, | |
485 | const unsigned char *secret, | |
486 | size_t secret_len, | |
487 | void *arg) | |
488 | { | |
489 | QUIC_CHANNEL *ch = arg; | |
490 | uint32_t i; | |
491 | ||
492 | if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM) | |
493 | /* Invalid EL. */ | |
494 | return 0; | |
495 | ||
496 | if (enc_level <= ch->tx_enc_level) | |
497 | /* | |
498 | * Does not make sense for us to try and provision an EL we have already | |
499 | * attained. | |
500 | */ | |
501 | return 0; | |
502 | ||
503 | /* | |
504 | * Ensure all crypto streams for previous ELs are now empty of available | |
505 | * data. | |
506 | */ | |
507 | for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i) | |
508 | if (!crypto_ensure_empty(ch->crypto_recv[i])) { | |
509 | /* Protocol violation (RFC 9001 s. 4.1.3) */ | |
510 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
511 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
512 | "crypto stream data in wrong EL"); | |
513 | return 0; | |
514 | } | |
515 | ||
516 | if (direction) { | |
517 | /* TX */ | |
518 | if (!ossl_qtx_provide_secret(ch->qtx, enc_level, | |
519 | suite_id, md, | |
520 | secret, secret_len)) | |
521 | return 0; | |
522 | ||
523 | ch->tx_enc_level = enc_level; | |
524 | } else { | |
525 | /* RX */ | |
526 | if (!ossl_qrx_provide_secret(ch->qrx, enc_level, | |
527 | suite_id, md, | |
528 | secret, secret_len)) | |
529 | return 0; | |
92282a17 HL |
530 | |
531 | ch->have_new_rx_secret = 1; | |
f538b421 HL |
532 | } |
533 | ||
534 | return 1; | |
535 | } | |
536 | ||
537 | static int ch_on_handshake_complete(void *arg) | |
538 | { | |
539 | QUIC_CHANNEL *ch = arg; | |
540 | ||
541 | if (ch->handshake_complete) | |
542 | return 0; /* this should not happen twice */ | |
543 | ||
544 | if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT)) | |
545 | return 0; | |
546 | ||
547 | if (!ch->got_remote_transport_params) | |
548 | /* | |
549 | * Was not a valid QUIC handshake if we did not get valid transport | |
550 | * params. | |
551 | */ | |
552 | return 0; | |
553 | ||
554 | /* Don't need transport parameters anymore. */ | |
555 | OPENSSL_free(ch->local_transport_params); | |
556 | ch->local_transport_params = NULL; | |
557 | ||
558 | /* Tell TXP the handshake is complete. */ | |
559 | ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp); | |
560 | ||
561 | ch->handshake_complete = 1; | |
562 | return 1; | |
563 | } | |
564 | ||
565 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code) | |
566 | { | |
567 | QUIC_CHANNEL *ch = arg; | |
568 | ||
569 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_ERR_BEGIN + alert_code, | |
570 | 0, "handshake alert"); | |
571 | return 1; | |
572 | } | |
573 | ||
574 | /* | |
575 | * QUIC Channel: Transport Parameter Handling | |
576 | * ========================================== | |
577 | */ | |
578 | ||
579 | /* | |
580 | * Called by handshake layer when we receive QUIC Transport Parameters from the | |
581 | * peer. Note that these are not authenticated until the handshake is marked | |
582 | * as complete. | |
583 | */ | |
584 | static int ch_on_transport_params(const unsigned char *params, | |
585 | size_t params_len, | |
586 | void *arg) | |
587 | { | |
588 | QUIC_CHANNEL *ch = arg; | |
589 | PACKET pkt; | |
590 | uint64_t id, v; | |
591 | size_t len; | |
592 | const unsigned char *body; | |
593 | int got_orig_dcid = 0; | |
594 | int got_initial_scid = 0; | |
595 | int got_retry_scid = 0; | |
596 | int got_initial_max_data = 0; | |
597 | int got_initial_max_stream_data_bidi_local = 0; | |
598 | int got_initial_max_stream_data_bidi_remote = 0; | |
599 | int got_initial_max_stream_data_uni = 0; | |
600 | int got_initial_max_streams_bidi = 0; | |
601 | int got_initial_max_streams_uni = 0; | |
602 | int got_ack_delay_exp = 0; | |
603 | int got_max_ack_delay = 0; | |
604 | int got_max_udp_payload_size = 0; | |
605 | int got_max_idle_timeout = 0; | |
606 | int got_active_conn_id_limit = 0; | |
607 | QUIC_CONN_ID cid; | |
608 | ||
609 | if (ch->got_remote_transport_params) | |
610 | goto malformed; | |
611 | ||
612 | if (!PACKET_buf_init(&pkt, params, params_len)) | |
613 | return 0; | |
614 | ||
615 | while (PACKET_remaining(&pkt) > 0) { | |
616 | if (!ossl_quic_wire_peek_transport_param(&pkt, &id)) | |
617 | goto malformed; | |
618 | ||
619 | switch (id) { | |
620 | case QUIC_TPARAM_ORIG_DCID: | |
621 | if (got_orig_dcid) | |
622 | /* must not appear more than once */ | |
623 | goto malformed; | |
624 | ||
625 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) | |
626 | goto malformed; | |
627 | ||
628 | /* Must match our initial DCID. */ | |
629 | if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid)) | |
630 | goto malformed; | |
631 | ||
632 | got_orig_dcid = 1; | |
633 | break; | |
634 | ||
635 | case QUIC_TPARAM_RETRY_SCID: | |
636 | if (got_retry_scid || !ch->doing_retry) | |
637 | /* must not appear more than once or if retry not done */ | |
638 | goto malformed; | |
639 | ||
640 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) | |
641 | goto malformed; | |
642 | ||
643 | /* Must match Retry packet SCID. */ | |
644 | if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid)) | |
645 | goto malformed; | |
646 | ||
647 | got_retry_scid = 1; | |
648 | break; | |
649 | ||
650 | case QUIC_TPARAM_INITIAL_SCID: | |
651 | if (got_initial_scid) | |
652 | /* must not appear more than once */ | |
653 | goto malformed; | |
654 | ||
655 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) | |
656 | goto malformed; | |
657 | ||
658 | /* Must match SCID of first Initial packet from server. */ | |
659 | if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) | |
660 | goto malformed; | |
661 | ||
662 | got_initial_scid = 1; | |
663 | break; | |
664 | ||
665 | case QUIC_TPARAM_INITIAL_MAX_DATA: | |
666 | if (got_initial_max_data) | |
667 | /* must not appear more than once */ | |
668 | goto malformed; | |
669 | ||
670 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) | |
671 | goto malformed; | |
672 | ||
673 | ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v); | |
674 | got_initial_max_data = 1; | |
675 | break; | |
676 | ||
677 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: | |
678 | if (got_initial_max_stream_data_bidi_local) | |
679 | /* must not appear more than once */ | |
680 | goto malformed; | |
681 | ||
682 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) | |
683 | goto malformed; | |
684 | ||
685 | /* | |
686 | * This is correct; the BIDI_LOCAL TP governs streams created by | |
687 | * the endpoint which sends the TP, i.e., our peer. | |
688 | */ | |
689 | ch->init_max_stream_data_bidi_remote = v; | |
690 | got_initial_max_stream_data_bidi_local = 1; | |
691 | break; | |
692 | ||
693 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: | |
694 | if (got_initial_max_stream_data_bidi_remote) | |
695 | /* must not appear more than once */ | |
696 | goto malformed; | |
697 | ||
698 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) | |
699 | goto malformed; | |
700 | ||
701 | /* | |
702 | * This is correct; the BIDI_REMOTE TP governs streams created | |
703 | * by the endpoint which receives the TP, i.e., us. | |
704 | */ | |
705 | ch->init_max_stream_data_bidi_local = v; | |
706 | ||
707 | /* Apply to stream 0. */ | |
708 | ossl_quic_txfc_bump_cwm(&ch->stream0->txfc, v); | |
709 | got_initial_max_stream_data_bidi_remote = 1; | |
710 | break; | |
711 | ||
712 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI: | |
713 | if (got_initial_max_stream_data_uni) | |
714 | /* must not appear more than once */ | |
715 | goto malformed; | |
716 | ||
717 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) | |
718 | goto malformed; | |
719 | ||
720 | ch->init_max_stream_data_uni_remote = v; | |
721 | got_initial_max_stream_data_uni = 1; | |
722 | break; | |
723 | ||
724 | case QUIC_TPARAM_ACK_DELAY_EXP: | |
725 | if (got_ack_delay_exp) | |
726 | /* must not appear more than once */ | |
727 | goto malformed; | |
728 | ||
729 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
730 | || v > QUIC_MAX_ACK_DELAY_EXP) | |
731 | goto malformed; | |
732 | ||
733 | ch->rx_ack_delay_exp = (unsigned char)v; | |
734 | got_ack_delay_exp = 1; | |
735 | break; | |
736 | ||
737 | case QUIC_TPARAM_MAX_ACK_DELAY: | |
738 | if (got_max_ack_delay) | |
739 | /* must not appear more than once */ | |
740 | return 0; | |
741 | ||
742 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
743 | || v >= (((uint64_t)1) << 14)) | |
744 | goto malformed; | |
745 | ||
746 | ch->rx_max_ack_delay = v; | |
747 | got_max_ack_delay = 1; | |
748 | break; | |
749 | ||
750 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI: | |
751 | if (got_initial_max_streams_bidi) | |
752 | /* must not appear more than once */ | |
753 | return 0; | |
754 | ||
755 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
756 | || v > (((uint64_t)1) << 60)) | |
757 | goto malformed; | |
758 | ||
759 | assert(ch->max_local_streams_bidi == 0); | |
760 | ch->max_local_streams_bidi = v; | |
761 | got_initial_max_streams_bidi = 1; | |
762 | break; | |
763 | ||
764 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI: | |
765 | if (got_initial_max_streams_uni) | |
766 | /* must not appear more than once */ | |
767 | goto malformed; | |
768 | ||
769 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
770 | || v > (((uint64_t)1) << 60)) | |
771 | goto malformed; | |
772 | ||
773 | assert(ch->max_local_streams_uni == 0); | |
774 | ch->max_local_streams_uni = v; | |
775 | got_initial_max_streams_uni = 1; | |
776 | break; | |
777 | ||
778 | case QUIC_TPARAM_MAX_IDLE_TIMEOUT: | |
779 | if (got_max_idle_timeout) | |
780 | /* must not appear more than once */ | |
781 | goto malformed; | |
782 | ||
783 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) | |
784 | goto malformed; | |
785 | ||
786 | if (v < ch->max_idle_timeout) | |
787 | ch->max_idle_timeout = v; | |
788 | ||
789 | ch_update_idle(ch); | |
790 | got_max_idle_timeout = 1; | |
791 | break; | |
792 | ||
793 | case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE: | |
794 | if (got_max_udp_payload_size) | |
795 | /* must not appear more than once */ | |
796 | goto malformed; | |
797 | ||
798 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
799 | || v < QUIC_MIN_INITIAL_DGRAM_LEN) | |
800 | goto malformed; | |
801 | ||
802 | ch->rx_max_udp_payload_size = v; | |
803 | got_max_udp_payload_size = 1; | |
804 | break; | |
805 | ||
806 | case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT: | |
807 | if (got_active_conn_id_limit) | |
808 | /* must not appear more than once */ | |
809 | goto malformed; | |
810 | ||
811 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
812 | || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT) | |
813 | goto malformed; | |
814 | ||
815 | ch->rx_active_conn_id_limit = v; | |
816 | got_active_conn_id_limit = 1; | |
817 | break; | |
818 | ||
819 | /* | |
820 | * TODO(QUIC): Handle: | |
821 | * QUIC_TPARAM_STATELESS_RESET_TOKEN | |
822 | * QUIC_TPARAM_PREFERRED_ADDR | |
823 | */ | |
824 | ||
825 | case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION: | |
826 | /* We do not currently handle migration, so nothing to do. */ | |
827 | default: | |
828 | /* Skip over and ignore. */ | |
829 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, | |
830 | &len); | |
831 | if (body == NULL) | |
832 | goto malformed; | |
833 | ||
834 | break; | |
835 | } | |
836 | } | |
837 | ||
838 | if (!got_orig_dcid || !got_initial_scid || got_retry_scid != ch->doing_retry) | |
839 | /* Transport parameters were not valid. */ | |
840 | goto malformed; | |
841 | ||
842 | ch->got_remote_transport_params = 1; | |
843 | ||
844 | if (got_initial_max_data || got_initial_max_stream_data_bidi_remote | |
845 | || got_initial_max_streams_bidi || got_initial_max_streams_uni) | |
846 | /* If FC credit was bumped, we may now be able to send. */ | |
847 | ossl_quic_stream_map_update_state(&ch->qsm, ch->stream0); | |
848 | ||
849 | /* If we are a server, we now generate our own transport parameters. */ | |
850 | if (ch->is_server && !ch_generate_transport_params(ch)) { | |
851 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
852 | "internal error"); | |
853 | return 0; | |
854 | } | |
855 | ||
856 | return 1; | |
857 | ||
858 | malformed: | |
859 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_TRANSPORT_PARAMETER_ERROR, | |
860 | 0, "bad transport parameter"); | |
861 | return 0; | |
862 | } | |
863 | ||
864 | /* | |
865 | * Called when we want to generate transport parameters. This is called | |
866 | * immediately at instantiation time for a client and after we receive the | |
867 | * client's transport parameters for a server. | |
868 | */ | |
869 | static int ch_generate_transport_params(QUIC_CHANNEL *ch) | |
870 | { | |
871 | int ok = 0; | |
872 | BUF_MEM *buf_mem = NULL; | |
873 | WPACKET wpkt; | |
874 | int wpkt_valid = 0; | |
875 | size_t buf_len = 0; | |
876 | ||
877 | if (ch->local_transport_params != NULL) | |
878 | goto err; | |
879 | ||
880 | if ((buf_mem = BUF_MEM_new()) == NULL) | |
881 | goto err; | |
882 | ||
883 | if (!WPACKET_init(&wpkt, buf_mem)) | |
884 | goto err; | |
885 | ||
886 | wpkt_valid = 1; | |
887 | ||
888 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION, | |
889 | NULL, 0) == NULL) | |
890 | goto err; | |
891 | ||
892 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_INITIAL_SCID, | |
893 | NULL, 0) == NULL) | |
894 | goto err; | |
895 | ||
896 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT, | |
897 | ch->max_idle_timeout)) | |
898 | goto err; | |
899 | ||
900 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE, | |
901 | QUIC_MIN_INITIAL_DGRAM_LEN)) | |
902 | goto err; | |
903 | ||
904 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT, | |
905 | 4)) | |
906 | goto err; | |
907 | ||
908 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA, | |
909 | ossl_quic_rxfc_get_cwm(&ch->conn_rxfc))) | |
910 | goto err; | |
911 | ||
912 | /* | |
913 | * We actually want the default CWM for a new RXFC, but here we just use | |
914 | * stream0 as a representative specimen. TODO(QUIC): revisit this when we | |
915 | * support multiple streams. | |
916 | */ | |
917 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, | |
918 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
919 | goto err; | |
920 | ||
921 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, | |
922 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
923 | goto err; | |
924 | ||
925 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI, | |
926 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
927 | goto err; | |
928 | ||
929 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI, | |
930 | 0)) | |
931 | goto err; | |
932 | ||
933 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI, | |
934 | 0)) | |
935 | goto err; | |
936 | ||
937 | if (!WPACKET_get_total_written(&wpkt, &buf_len)) | |
938 | goto err; | |
939 | ||
940 | ch->local_transport_params = (unsigned char *)buf_mem->data; | |
941 | buf_mem->data = NULL; | |
942 | ||
943 | if (!WPACKET_finish(&wpkt)) | |
944 | goto err; | |
945 | ||
946 | wpkt_valid = 0; | |
947 | ||
948 | if (!ossl_quic_dhs_set_transport_params(ch->dhs, ch->local_transport_params, | |
949 | buf_len)) | |
950 | goto err; | |
951 | ||
952 | ok = 1; | |
953 | err: | |
954 | if (wpkt_valid) | |
955 | WPACKET_cleanup(&wpkt); | |
956 | BUF_MEM_free(buf_mem); | |
957 | return ok; | |
958 | } | |
959 | ||
960 | /* | |
961 | * QUIC Channel: Ticker-Mutator | |
962 | * ============================ | |
963 | */ | |
964 | ||
965 | /* | |
966 | * The central ticker function called by the reactor. This does everything, or | |
967 | * at least everything network I/O related. Best effort - not allowed to fail | |
968 | * "loudly". | |
969 | */ | |
970 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg) | |
971 | { | |
972 | OSSL_TIME now, deadline; | |
973 | QUIC_CHANNEL *ch = arg; | |
974 | ||
975 | /* | |
976 | * When we tick the QUIC connection, we do everything we need to do | |
977 | * periodically. In order, we: | |
978 | * | |
979 | * - handle any incoming data from the network; | |
980 | * - handle any timer events which are due to fire (ACKM, etc.) | |
981 | * - write any data to the network due to be sent, to the extent | |
982 | * possible; | |
983 | * - determine the time at which we should next be ticked. | |
984 | */ | |
985 | ||
986 | /* If we are in the TERMINATED state, there is nothing to do. */ | |
987 | if (ossl_quic_channel_is_terminated(ch)) { | |
988 | res->want_net_read = 0; | |
989 | res->want_net_write = 0; | |
990 | res->tick_deadline = ossl_time_infinite(); | |
991 | return; | |
992 | } | |
993 | ||
994 | /* | |
995 | * If we are in the TERMINATING state, check if the terminating timer has | |
996 | * expired. | |
997 | */ | |
998 | if (ossl_quic_channel_is_terminating(ch)) { | |
999 | now = ossl_time_now(); | |
1000 | ||
1001 | if (ossl_time_compare(now, ch->terminate_deadline) >= 0) { | |
1002 | ch_on_terminating_timeout(ch); | |
1003 | res->want_net_read = 0; | |
1004 | res->want_net_write = 0; | |
1005 | res->tick_deadline = ossl_time_infinite(); | |
1006 | return; /* abort normal processing, nothing to do */ | |
1007 | } | |
1008 | } | |
1009 | ||
4e64437a HL |
1010 | do { |
1011 | /* Handle any incoming data from the network. */ | |
1012 | ch_rx(ch); | |
f538b421 | 1013 | |
4e64437a HL |
1014 | /* |
1015 | * Allow the handshake layer to check for any new incoming data and generate | |
1016 | * new outgoing data. | |
1017 | */ | |
92282a17 | 1018 | ch->have_new_rx_secret = 0; |
4e64437a HL |
1019 | ossl_quic_dhs_tick(ch->dhs); |
1020 | ||
1021 | /* | |
1022 | * If the handshake layer gave us a new secret, we need to do RX again | |
1023 | * because packets that were not previously processable and were | |
1024 | * deferred might now be processable. | |
1025 | */ | |
92282a17 | 1026 | } while (ch->have_new_rx_secret); |
f538b421 HL |
1027 | |
1028 | /* | |
1029 | * Handle any timer events which are due to fire; namely, the loss detection | |
1030 | * deadline and the idle timeout. | |
1031 | * | |
1032 | * ACKM ACK generation deadline is polled by TXP, so we don't need to handle | |
1033 | * it here. | |
1034 | */ | |
1035 | now = ossl_time_now(); | |
1036 | if (ossl_time_compare(now, ch->idle_deadline) >= 0) { | |
1037 | /* | |
1038 | * Idle timeout differs from normal protocol violation because we do not | |
1039 | * send a CONN_CLOSE frame; go straight to TERMINATED. | |
1040 | */ | |
1041 | ch_on_idle_timeout(ch); | |
1042 | res->want_net_read = 0; | |
1043 | res->want_net_write = 0; | |
1044 | res->tick_deadline = ossl_time_infinite(); | |
1045 | return; | |
1046 | } | |
1047 | ||
1048 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); | |
1049 | if (!ossl_time_is_zero(deadline) && ossl_time_compare(now, deadline) >= 0) | |
1050 | ossl_ackm_on_timeout(ch->ackm); | |
1051 | ||
1052 | /* Write any data to the network due to be sent. */ | |
1053 | ch_tx(ch); | |
1054 | ||
1055 | /* Determine the time at which we should next be ticked. */ | |
1056 | res->tick_deadline = ch_determine_next_tick_deadline(ch); | |
1057 | ||
1058 | /* Always process network input. */ | |
1059 | res->want_net_read = 1; | |
1060 | ||
1061 | /* We want to write to the network if we have any in our queue. */ | |
1062 | res->want_net_write = (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0); | |
1063 | } | |
1064 | ||
1065 | /* Process incoming packets and handle frames, if any. */ | |
1066 | static int ch_rx(QUIC_CHANNEL *ch) | |
1067 | { | |
1068 | int handled_any = 0; | |
1069 | ||
1070 | if (!ch->have_sent_any_pkt) | |
1071 | /* | |
1072 | * We have not sent anything yet, therefore there is no need to check | |
1073 | * for incoming data. TODO SERVER | |
1074 | */ | |
1075 | return 1; | |
1076 | ||
1077 | /* | |
1078 | * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams | |
1079 | * to the appropriate QRX instance. | |
1080 | */ | |
1081 | ossl_quic_demux_pump(ch->demux); /* best effort */ | |
1082 | ||
1083 | for (;;) { | |
1084 | assert(ch->qrx_pkt == NULL); | |
1085 | ||
1086 | if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt)) | |
1087 | break; | |
1088 | ||
1089 | if (!handled_any) | |
1090 | ch_update_idle(ch); | |
1091 | ||
1092 | ch_rx_handle_packet(ch); /* best effort */ | |
1093 | ||
1094 | /* | |
1095 | * Regardless of the outcome of frame handling, unref the packet. | |
1096 | * This will free the packet unless something added another | |
1097 | * reference to it during frame processing. | |
1098 | */ | |
1099 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
1100 | ch->qrx_pkt = NULL; | |
1101 | ||
1102 | handled_any = 1; | |
1103 | } | |
1104 | ||
1105 | /* | |
1106 | * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we | |
1107 | * process one or more incoming packets. | |
1108 | */ | |
1109 | if (handled_any && ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) | |
1110 | ch->conn_close_queued = 1; | |
1111 | ||
1112 | return 1; | |
1113 | } | |
1114 | ||
1115 | /* Handles the packet currently in ch->qrx_pkt->hdr. */ | |
1116 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch) | |
1117 | { | |
1118 | uint32_t enc_level; | |
1119 | ||
1120 | assert(ch->qrx_pkt != NULL); | |
1121 | ||
1122 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) { | |
1123 | if (!ch->have_received_enc_pkt) { | |
1124 | ch->init_scid = ch->qrx_pkt->hdr->src_conn_id; | |
1125 | ch->have_received_enc_pkt = 1; | |
1126 | ||
1127 | /* | |
1128 | * We change to using the SCID in the first Initial packet as the | |
1129 | * DCID. | |
1130 | */ | |
1131 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid); | |
1132 | } | |
1133 | ||
1134 | enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type); | |
1135 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
1136 | /* Do not process packets from ELs we have already discarded. */ | |
1137 | return; | |
1138 | } | |
1139 | ||
1140 | /* Handle incoming packet. */ | |
1141 | switch (ch->qrx_pkt->hdr->type) { | |
1142 | case QUIC_PKT_TYPE_RETRY: | |
1143 | if (ch->doing_retry) | |
1144 | /* It is not allowed to ask a client to do a retry more than | |
1145 | * once. */ | |
1146 | return; | |
1147 | ||
1148 | /* TODO if server */ | |
1149 | ||
1150 | if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN) | |
1151 | /* Packets with zero-length Retry Tokens are invalid. */ | |
1152 | return; | |
1153 | ||
1154 | /* | |
1155 | * TODO(QUIC): Theoretically this should probably be in the QRX. | |
1156 | * However because validation is dependent on context (namely the | |
1157 | * client's initial DCID) we can't do this cleanly. In the future we | |
1158 | * should probably add a callback to the QRX to let it call us (via | |
1159 | * the DEMUX) and ask us about the correct original DCID, rather | |
1160 | * than allow the QRX to emit a potentially malformed packet to the | |
1161 | * upper layers. However, special casing this will do for now. | |
1162 | */ | |
1163 | if (!ossl_quic_validate_retry_integrity_tag(ch->libctx, | |
1164 | ch->propq, | |
1165 | ch->qrx_pkt->hdr, | |
1166 | &ch->init_dcid)) | |
1167 | /* Malformed retry packet, ignore. */ | |
1168 | return; | |
1169 | ||
1170 | ch_retry(ch, ch->qrx_pkt->hdr->data, | |
1171 | ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN, | |
1172 | &ch->qrx_pkt->hdr->src_conn_id); | |
1173 | break; | |
1174 | ||
1175 | case QUIC_PKT_TYPE_VERSION_NEG: | |
1176 | /* TODO(QUIC): Implement version negotiation */ | |
1177 | break; | |
1178 | ||
1179 | case QUIC_PKT_TYPE_0RTT: | |
1180 | /* TODO if server */ | |
1181 | /* Clients should never receive 0-RTT packets */ | |
1182 | break; | |
1183 | ||
1184 | default: | |
1185 | if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE) | |
1186 | /* | |
1187 | * We automatically drop INITIAL EL keys when first successfully | |
1188 | * decrypting a HANDSHAKE packet, as per the RFC. | |
1189 | */ | |
1190 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); | |
1191 | ||
1192 | /* This packet contains frames, pass to the RXDP. */ | |
1193 | ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */ | |
1194 | break; | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | /* Try to generate packets and if possible, flush them to the network. */ | |
1199 | static int ch_tx(QUIC_CHANNEL *ch) | |
1200 | { | |
1201 | if (ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) { | |
1202 | /* | |
1203 | * While closing, only send CONN_CLOSE if we've received more traffic | |
1204 | * from the peer. Once we tell the TXP to generate CONN_CLOSE, all | |
1205 | * future calls to it generate CONN_CLOSE frames, so otherwise we would | |
1206 | * just constantly generate CONN_CLOSE frames. | |
1207 | */ | |
1208 | if (!ch->conn_close_queued) | |
1209 | return 0; | |
1210 | ||
1211 | ch->conn_close_queued = 0; | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Send a packet, if we need to. Best effort. The TXP consults the CC and | |
1216 | * applies any limitations imposed by it, so we don't need to do it here. | |
1217 | * | |
1218 | * Best effort. In particular if TXP fails for some reason we should still | |
1219 | * flush any queued packets which we already generated. | |
1220 | */ | |
1221 | if (ossl_quic_tx_packetiser_generate(ch->txp, | |
1222 | TX_PACKETISER_ARCHETYPE_NORMAL) | |
1223 | == TX_PACKETISER_RES_SENT_PKT) | |
1224 | ch->have_sent_any_pkt = 1; | |
1225 | ||
1226 | ossl_qtx_flush_net(ch->qtx); /* best effort */ | |
1227 | return 1; | |
1228 | } | |
1229 | ||
1230 | /* Determine next tick deadline. */ | |
1231 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch) | |
1232 | { | |
1233 | OSSL_TIME deadline; | |
1234 | uint32_t pn_space; | |
1235 | ||
1236 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); | |
1237 | if (ossl_time_is_zero(deadline)) | |
1238 | deadline = ossl_time_infinite(); | |
1239 | ||
1240 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) | |
1241 | deadline = ossl_time_min(deadline, | |
1242 | ossl_ackm_get_ack_deadline(ch->ackm, pn_space)); | |
1243 | ||
1244 | /* When will CC let us send more? */ | |
1245 | if (ossl_quic_tx_packetiser_has_pending(ch->txp, TX_PACKETISER_ARCHETYPE_NORMAL, | |
1246 | TX_PACKETISER_BYPASS_CC)) | |
1247 | deadline = ossl_time_min(deadline, | |
1248 | ch->cc_method->get_next_credit_time(ch->cc_data)); | |
1249 | ||
1250 | /* Is the terminating timer armed? */ | |
1251 | if (ossl_quic_channel_is_terminating(ch)) | |
1252 | deadline = ossl_time_min(deadline, | |
1253 | ch->terminate_deadline); | |
1254 | else if (!ossl_time_is_infinite(ch->idle_deadline)) | |
1255 | deadline = ossl_time_min(deadline, | |
1256 | ch->idle_deadline); | |
1257 | ||
1258 | return deadline; | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * QUIC Channel: Network BIO Configuration | |
1263 | * ======================================= | |
1264 | */ | |
1265 | ||
1266 | /* Determines whether we can support a given poll descriptor. */ | |
1267 | static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d) | |
1268 | { | |
1269 | if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) | |
1270 | return 0; | |
1271 | ||
1272 | return 1; | |
1273 | } | |
1274 | ||
1275 | BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch) | |
1276 | { | |
1277 | return ch->net_rbio; | |
1278 | } | |
1279 | ||
1280 | BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch) | |
1281 | { | |
1282 | return ch->net_wbio; | |
1283 | } | |
1284 | ||
d1ac77b1 HL |
1285 | /* |
1286 | * QUIC_CHANNEL does not ref any BIO it is provided with, nor is any ref | |
1287 | * transferred to it. The caller (i.e., QUIC_CONNECTION) is responsible for | |
1288 | * ensuring the BIO lasts until the channel is freed or the BIO is switched out | |
1289 | * for another BIO by a subsequent successful call to this function. | |
1290 | */ | |
1291 | int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio) | |
f538b421 HL |
1292 | { |
1293 | BIO_POLL_DESCRIPTOR d = {0}; | |
1294 | ||
1295 | if (ch->net_rbio == net_rbio) | |
1296 | return 1; | |
1297 | ||
1298 | if (net_rbio != NULL) { | |
1299 | if (!BIO_get_rpoll_descriptor(net_rbio, &d)) | |
1300 | /* Non-pollable BIO */ | |
1301 | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; | |
1302 | ||
1303 | if (!validate_poll_descriptor(&d)) | |
1304 | return 0; | |
1305 | } | |
1306 | ||
1307 | ossl_quic_reactor_set_poll_r(&ch->rtor, &d); | |
f538b421 HL |
1308 | ossl_quic_demux_set_bio(ch->demux, net_rbio); |
1309 | ch->net_rbio = net_rbio; | |
1310 | return 1; | |
1311 | } | |
1312 | ||
d1ac77b1 | 1313 | int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio) |
f538b421 HL |
1314 | { |
1315 | BIO_POLL_DESCRIPTOR d = {0}; | |
1316 | ||
1317 | if (ch->net_wbio == net_wbio) | |
1318 | return 1; | |
1319 | ||
1320 | if (net_wbio != NULL) { | |
1321 | if (!BIO_get_wpoll_descriptor(net_wbio, &d)) | |
1322 | /* Non-pollable BIO */ | |
1323 | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; | |
1324 | ||
1325 | if (!validate_poll_descriptor(&d)) | |
1326 | return 0; | |
1327 | } | |
1328 | ||
1329 | ossl_quic_reactor_set_poll_w(&ch->rtor, &d); | |
f538b421 HL |
1330 | ossl_qtx_set_bio(ch->qtx, net_wbio); |
1331 | ch->net_wbio = net_wbio; | |
1332 | return 1; | |
1333 | } | |
1334 | ||
1335 | /* | |
1336 | * QUIC Channel: Lifecycle Events | |
1337 | * ============================== | |
1338 | */ | |
1339 | ||
1340 | int ossl_quic_channel_start(QUIC_CHANNEL *ch) | |
1341 | { | |
1342 | /* TODO SERVER */ | |
1343 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) | |
1344 | /* Calls to connect are idempotent */ | |
1345 | return 1; | |
1346 | ||
1347 | /* Inform QTX of peer address. */ | |
1348 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) | |
1349 | return 0; | |
1350 | ||
1351 | /* Plug in secrets for the Initial EL. */ | |
1352 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
1353 | ch->propq, | |
1354 | &ch->init_dcid, | |
1355 | /*is_server=*/0, | |
1356 | ch->qrx, ch->qtx)) | |
1357 | return 0; | |
1358 | ||
1359 | /* Change state. */ | |
1360 | ch->state = QUIC_CHANNEL_STATE_ACTIVE; | |
1361 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ | |
1362 | ||
1363 | /* Handshake layer: start (e.g. send CH). */ | |
1364 | if (!ossl_quic_dhs_tick(ch->dhs)) | |
1365 | return 0; | |
1366 | ||
1367 | ossl_quic_reactor_tick(&ch->rtor); /* best effort */ | |
1368 | return 1; | |
1369 | } | |
1370 | ||
1371 | /* Start a locally initiated connection shutdown. */ | |
1372 | void ossl_quic_channel_local_close(QUIC_CHANNEL *ch) | |
1373 | { | |
1374 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1375 | ||
1376 | if (ossl_quic_channel_is_term_any(ch)) | |
1377 | return; | |
1378 | ||
1379 | tcause.app = 1; | |
1380 | ch_start_terminating(ch, &tcause); | |
1381 | } | |
1382 | ||
1383 | static void free_token(const unsigned char *buf, size_t buf_len, void *arg) | |
1384 | { | |
1385 | OPENSSL_free((unsigned char *)buf); | |
1386 | } | |
1387 | ||
1388 | /* Called when a server asks us to do a retry. */ | |
1389 | static int ch_retry(QUIC_CHANNEL *ch, | |
1390 | const unsigned char *retry_token, | |
1391 | size_t retry_token_len, | |
1392 | const QUIC_CONN_ID *retry_scid) | |
1393 | { | |
1394 | void *buf; | |
1395 | ||
1396 | /* We change to using the SCID in the Retry packet as the DCID. */ | |
1397 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid)) | |
1398 | return 0; | |
1399 | ||
1400 | /* | |
1401 | * Now we retry. We will release the Retry packet immediately, so copy | |
1402 | * the token. | |
1403 | */ | |
1404 | if ((buf = OPENSSL_malloc(retry_token_len)) == NULL) | |
1405 | return 0; | |
1406 | ||
1407 | memcpy(buf, retry_token, retry_token_len); | |
1408 | ||
1409 | ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, retry_token_len, | |
1410 | free_token, NULL); | |
1411 | ||
1412 | ch->retry_scid = *retry_scid; | |
1413 | ch->doing_retry = 1; | |
1414 | ||
1415 | /* | |
1416 | * We need to stimulate the Initial EL to generate the first CRYPTO frame | |
1417 | * again. We can do this most cleanly by simply forcing the ACKM to consider | |
1418 | * the first Initial packet as lost, which it effectively was as the server | |
1419 | * hasn't processed it. This also maintains the desired behaviour with e.g. | |
1420 | * PNs not resetting and so on. | |
1421 | * | |
1422 | * The PN we used initially is always zero, because QUIC does not allow | |
1423 | * repeated retries. | |
1424 | */ | |
1425 | if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL, | |
1426 | /*PN=*/0)) | |
1427 | return 0; | |
1428 | ||
1429 | /* | |
1430 | * Plug in new secrets for the Initial EL. This is the only time we change | |
1431 | * the secrets for an EL after we already provisioned it. | |
1432 | */ | |
1433 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
1434 | ch->propq, | |
1435 | &ch->retry_scid, | |
1436 | /*is_server=*/0, | |
1437 | ch->qrx, ch->qtx)) | |
1438 | return 0; | |
1439 | ||
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | /* Called when an EL is to be discarded. */ | |
1444 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
1445 | uint32_t enc_level) | |
1446 | { | |
1447 | if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT)) | |
1448 | return 0; | |
1449 | ||
1450 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
1451 | /* Already done. */ | |
1452 | return 1; | |
1453 | ||
1454 | /* Best effort for all of these. */ | |
1455 | ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level); | |
1456 | ossl_qrx_discard_enc_level(ch->qrx, enc_level); | |
1457 | ossl_qtx_discard_enc_level(ch->qtx, enc_level); | |
1458 | ||
1459 | if (enc_level != QUIC_ENC_LEVEL_0RTT) { | |
1460 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
1461 | ||
1462 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
1463 | ||
1464 | /* We should still have crypto streams at this point. */ | |
1465 | assert(ch->crypto_send[pn_space] != NULL); | |
1466 | assert(ch->crypto_recv[pn_space] != NULL); | |
1467 | ||
1468 | /* Get rid of the crypto stream state for the EL. */ | |
1469 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
1470 | ch->crypto_send[pn_space] = NULL; | |
1471 | ||
1472 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
1473 | ch->crypto_recv[pn_space] = NULL; | |
1474 | } | |
1475 | ||
1476 | ch->el_discarded |= (1U << enc_level); | |
1477 | return 1; | |
1478 | } | |
1479 | ||
1480 | /* Intended to be called by the RXDP. */ | |
1481 | int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch) | |
1482 | { | |
1483 | if (ch->handshake_confirmed) | |
1484 | return 1; | |
1485 | ||
1486 | if (!ch->handshake_complete) { | |
1487 | /* | |
1488 | * Does not make sense for handshake to be confirmed before it is | |
1489 | * completed. | |
1490 | */ | |
1491 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
1492 | OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, | |
1493 | "handshake cannot be confirmed " | |
1494 | "before it is completed"); | |
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE); | |
1499 | ch->handshake_confirmed = 1; | |
1500 | return 1; | |
1501 | } | |
1502 | ||
1503 | /* | |
1504 | * Master function used when we want to start tearing down a connection: | |
1505 | * | |
1506 | * - If the connection is still IDLE we can go straight to TERMINATED; | |
1507 | * | |
1508 | * - If we are already TERMINATED this is a no-op. | |
1509 | * | |
1510 | * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE | |
1511 | * from the peer (tcause->remote == 1), we move to TERMINATING - CLOSING. | |
1512 | * | |
1513 | * - If we are TERMINATING - DRAINING, we remain here until the terminating | |
1514 | * timer expires. | |
1515 | * | |
1516 | * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING. | |
1517 | * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note | |
1518 | * that we are considered to have caused a termination if we sent the first | |
1519 | * CONNECTION_CLOSE frame, even if it is caused by a peer protocol | |
1520 | * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to | |
1521 | * TERMINATING - DRAINING. | |
1522 | * | |
1523 | * We record the termination cause structure passed on the first call only. | |
1524 | * Any successive calls have their termination cause data discarded; | |
1525 | * once we start sending a CONNECTION_CLOSE frame, we don't change the details | |
1526 | * in it. | |
1527 | */ | |
1528 | static void ch_start_terminating(QUIC_CHANNEL *ch, | |
1529 | const QUIC_TERMINATE_CAUSE *tcause) | |
1530 | { | |
1531 | switch (ch->state) { | |
1532 | default: | |
1533 | case QUIC_CHANNEL_STATE_IDLE: | |
1534 | ch->terminate_cause = *tcause; | |
1535 | ch_on_terminating_timeout(ch); | |
1536 | break; | |
1537 | ||
1538 | case QUIC_CHANNEL_STATE_ACTIVE: | |
1539 | ch->state = tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING | |
1540 | : QUIC_CHANNEL_STATE_TERMINATING_CLOSING; | |
1541 | ch->terminate_cause = *tcause; | |
1542 | ch->terminate_deadline | |
1543 | = ossl_time_add(ossl_time_now(), | |
1544 | ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm), | |
1545 | 3)); | |
1546 | ||
1547 | if (!tcause->remote) { | |
1548 | OSSL_QUIC_FRAME_CONN_CLOSE f = {0}; | |
1549 | ||
1550 | /* best effort */ | |
1551 | f.error_code = ch->terminate_cause.error_code; | |
1552 | f.frame_type = ch->terminate_cause.frame_type; | |
1553 | f.is_app = ch->terminate_cause.app; | |
1554 | ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f); | |
1555 | ch->conn_close_queued = 1; | |
1556 | } | |
1557 | break; | |
1558 | ||
1559 | case QUIC_CHANNEL_STATE_TERMINATING_CLOSING: | |
1560 | if (tcause->remote) | |
1561 | ch->state = QUIC_CHANNEL_STATE_TERMINATING_DRAINING; | |
1562 | ||
1563 | break; | |
1564 | ||
1565 | case QUIC_CHANNEL_STATE_TERMINATING_DRAINING: | |
1566 | /* We remain here until the timout expires. */ | |
1567 | break; | |
1568 | ||
1569 | case QUIC_CHANNEL_STATE_TERMINATED: | |
1570 | /* No-op. */ | |
1571 | break; | |
1572 | } | |
1573 | } | |
1574 | ||
1575 | /* For RXDP use. */ | |
1576 | void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch, | |
1577 | OSSL_QUIC_FRAME_CONN_CLOSE *f) | |
1578 | { | |
1579 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1580 | ||
1581 | if (!ossl_quic_channel_is_active(ch)) | |
1582 | return; | |
1583 | ||
1584 | tcause.remote = 1; | |
1585 | tcause.app = f->is_app; | |
1586 | tcause.error_code = f->error_code; | |
1587 | tcause.frame_type = f->frame_type; | |
1588 | ||
1589 | ch_start_terminating(ch, &tcause); | |
1590 | } | |
1591 | ||
1592 | void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch, | |
1593 | uint64_t error_code, | |
1594 | uint64_t frame_type, | |
1595 | const char *reason) | |
1596 | { | |
1597 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1598 | ||
1599 | tcause.error_code = error_code; | |
1600 | tcause.frame_type = frame_type; | |
1601 | ||
1602 | ch_start_terminating(ch, &tcause); | |
1603 | } | |
1604 | ||
1605 | /* | |
1606 | * Called once the terminating timer expires, meaning we move from TERMINATING | |
1607 | * to TERMINATED. | |
1608 | */ | |
1609 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch) | |
1610 | { | |
1611 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * Updates our idle deadline. Called when an event happens which should bump the | |
1616 | * idle timeout. | |
1617 | */ | |
1618 | static void ch_update_idle(QUIC_CHANNEL *ch) | |
1619 | { | |
1620 | if (ch->max_idle_timeout == 0) | |
1621 | ch->idle_deadline = ossl_time_infinite(); | |
1622 | else | |
1623 | ch->idle_deadline = ossl_time_add(ossl_time_now(), | |
1624 | ossl_ms2time(ch->max_idle_timeout)); | |
1625 | } | |
1626 | ||
1627 | /* Called when the idle timeout expires. */ | |
1628 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch) | |
1629 | { | |
1630 | /* | |
1631 | * Idle timeout does not have an error code associated with it because a | |
1632 | * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach | |
1633 | * TERMINATED anyway. | |
1634 | */ | |
1635 | ch->terminate_cause.app = 0; | |
1636 | ch->terminate_cause.error_code = UINT64_MAX; | |
1637 | ch->terminate_cause.frame_type = 0; | |
1638 | ||
1639 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
1640 | } |