]>
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 | ||
b1b06da2 HL |
17 | /* |
18 | * NOTE: While this channel implementation currently has basic server support, | |
19 | * this functionality has been implemented for internal testing purposes and is | |
20 | * not suitable for network use. In particular, it does not implement address | |
21 | * validation, anti-amplification or retry logic. | |
22 | * | |
23 | * TODO(QUIC): Implement address validation and anti-amplification | |
24 | * TODO(QUIC): Implement retry logic | |
25 | */ | |
26 | ||
f538b421 HL |
27 | #define INIT_DCID_LEN 8 |
28 | #define INIT_CRYPTO_BUF_LEN 8192 | |
29 | #define INIT_APP_BUF_LEN 8192 | |
30 | ||
3bf4dc8c | 31 | static void ch_rx_pre(QUIC_CHANNEL *ch); |
f538b421 HL |
32 | static int ch_rx(QUIC_CHANNEL *ch); |
33 | static int ch_tx(QUIC_CHANNEL *ch); | |
34 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg); | |
35 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch); | |
36 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch); | |
37 | static int ch_retry(QUIC_CHANNEL *ch, | |
38 | const unsigned char *retry_token, | |
39 | size_t retry_token_len, | |
40 | const QUIC_CONN_ID *retry_scid); | |
41 | static void ch_cleanup(QUIC_CHANNEL *ch); | |
42 | static int ch_generate_transport_params(QUIC_CHANNEL *ch); | |
43 | static int ch_on_transport_params(const unsigned char *params, | |
44 | size_t params_len, | |
45 | void *arg); | |
46 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code); | |
47 | static int ch_on_handshake_complete(void *arg); | |
48 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
49 | uint32_t suite_id, EVP_MD *md, | |
50 | const unsigned char *secret, | |
51 | size_t secret_len, | |
52 | void *arg); | |
53 | static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len, | |
54 | size_t *bytes_read, void *arg); | |
55 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream); | |
56 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
57 | size_t *consumed, void *arg); | |
58 | static OSSL_TIME get_time(void *arg); | |
59 | static uint64_t get_stream_limit(int uni, void *arg); | |
60 | static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg); | |
61 | static int ch_retry(QUIC_CHANNEL *ch, | |
62 | const unsigned char *retry_token, | |
63 | size_t retry_token_len, | |
64 | const QUIC_CONN_ID *retry_scid); | |
65 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
66 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
67 | uint32_t enc_level); | |
68 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch); | |
69 | static void ch_update_idle(QUIC_CHANNEL *ch); | |
df15e990 | 70 | static void ch_raise_net_error(QUIC_CHANNEL *ch); |
f538b421 HL |
71 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch); |
72 | static void ch_start_terminating(QUIC_CHANNEL *ch, | |
df15e990 HL |
73 | const QUIC_TERMINATE_CAUSE *tcause, |
74 | int force_immediate); | |
b1b06da2 HL |
75 | static void ch_default_packet_handler(QUIC_URXE *e, void *arg); |
76 | static int ch_server_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer, | |
77 | const QUIC_CONN_ID *peer_scid, | |
78 | const QUIC_CONN_ID *peer_dcid); | |
f538b421 HL |
79 | |
80 | static int gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len, QUIC_CONN_ID *cid) | |
81 | { | |
82 | if (len > QUIC_MAX_CONN_ID_LEN) | |
83 | return 0; | |
84 | ||
85 | cid->id_len = (unsigned char)len; | |
86 | ||
87 | if (RAND_bytes_ex(libctx, cid->id, len, len * 8) != 1) { | |
88 | cid->id_len = 0; | |
89 | return 0; | |
90 | } | |
91 | ||
92 | return 1; | |
93 | } | |
94 | ||
95 | /* | |
96 | * QUIC Channel Initialization and Teardown | |
97 | * ======================================== | |
98 | */ | |
99 | static int ch_init(QUIC_CHANNEL *ch) | |
100 | { | |
101 | OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0}; | |
102 | OSSL_QTX_ARGS qtx_args = {0}; | |
103 | OSSL_QRX_ARGS qrx_args = {0}; | |
2723d705 | 104 | QUIC_TLS_ARGS tls_args = {0}; |
f538b421 | 105 | uint32_t pn_space; |
b1b06da2 | 106 | size_t rx_short_cid_len = ch->is_server ? INIT_DCID_LEN : 0; |
f538b421 | 107 | |
b1b06da2 HL |
108 | /* For clients, generate our initial DCID. */ |
109 | if (!ch->is_server | |
110 | && !gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, &ch->init_dcid)) | |
f538b421 HL |
111 | goto err; |
112 | ||
113 | /* We plug in a network write BIO to the QTX later when we get one. */ | |
114 | qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN; | |
115 | ch->rx_max_udp_payload_size = qtx_args.mdpl; | |
116 | ||
117 | ch->qtx = ossl_qtx_new(&qtx_args); | |
118 | if (ch->qtx == NULL) | |
119 | goto err; | |
120 | ||
121 | ch->txpim = ossl_quic_txpim_new(); | |
122 | if (ch->txpim == NULL) | |
123 | goto err; | |
124 | ||
125 | ch->cfq = ossl_quic_cfq_new(); | |
126 | if (ch->cfq == NULL) | |
127 | goto err; | |
128 | ||
129 | if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL)) | |
130 | goto err; | |
131 | ||
132 | if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL, | |
133 | 2 * 1024 * 1024, | |
134 | 10 * 1024 * 1024, | |
135 | get_time, NULL)) | |
136 | goto err; | |
137 | ||
138 | if (!ossl_statm_init(&ch->statm)) | |
139 | goto err; | |
140 | ||
141 | ch->have_statm = 1; | |
142 | ch->cc_method = &ossl_cc_dummy_method; | |
143 | if ((ch->cc_data = ch->cc_method->new(NULL, NULL, NULL)) == NULL) | |
144 | goto err; | |
145 | ||
146 | if ((ch->ackm = ossl_ackm_new(get_time, NULL, &ch->statm, | |
147 | ch->cc_method, ch->cc_data)) == NULL) | |
148 | goto err; | |
149 | ||
150 | if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch)) | |
151 | goto err; | |
152 | ||
153 | ch->have_qsm = 1; | |
154 | ||
155 | /* We use a zero-length SCID. */ | |
156 | txp_args.cur_dcid = ch->init_dcid; | |
157 | txp_args.ack_delay_exponent = 3; | |
158 | txp_args.qtx = ch->qtx; | |
159 | txp_args.txpim = ch->txpim; | |
160 | txp_args.cfq = ch->cfq; | |
161 | txp_args.ackm = ch->ackm; | |
162 | txp_args.qsm = &ch->qsm; | |
163 | txp_args.conn_txfc = &ch->conn_txfc; | |
164 | txp_args.conn_rxfc = &ch->conn_rxfc; | |
165 | txp_args.cc_method = ch->cc_method; | |
166 | txp_args.cc_data = ch->cc_data; | |
167 | txp_args.now = get_time; | |
168 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
169 | ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_BUF_LEN); | |
170 | if (ch->crypto_send[pn_space] == NULL) | |
171 | goto err; | |
172 | ||
173 | txp_args.crypto[pn_space] = ch->crypto_send[pn_space]; | |
174 | } | |
175 | ||
176 | ch->txp = ossl_quic_tx_packetiser_new(&txp_args); | |
177 | if (ch->txp == NULL) | |
178 | goto err; | |
179 | ||
b1b06da2 HL |
180 | if ((ch->demux = ossl_quic_demux_new(/*BIO=*/NULL, |
181 | /*Short CID Len=*/rx_short_cid_len, | |
d7668ff2 | 182 | get_time, NULL)) == NULL) |
f538b421 HL |
183 | goto err; |
184 | ||
b1b06da2 HL |
185 | /* |
186 | * If we are a server, setup our handler for packets not corresponding to | |
187 | * any known DCID on our end. This is for handling clients establishing new | |
188 | * connections. | |
189 | */ | |
190 | if (ch->is_server) | |
191 | ossl_quic_demux_set_default_handler(ch->demux, | |
192 | ch_default_packet_handler, | |
193 | ch); | |
194 | ||
f538b421 | 195 | qrx_args.demux = ch->demux; |
b1b06da2 | 196 | qrx_args.short_conn_id_len = rx_short_cid_len; |
f538b421 HL |
197 | qrx_args.max_deferred = 32; |
198 | ||
199 | if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL) | |
200 | goto err; | |
201 | ||
202 | if (!ossl_qrx_set_early_validation_cb(ch->qrx, | |
203 | rx_early_validate, | |
204 | ch)) | |
205 | goto err; | |
206 | ||
b1b06da2 | 207 | if (!ch->is_server && !ossl_qrx_add_dst_conn_id(ch->qrx, &txp_args.cur_scid)) |
f538b421 HL |
208 | goto err; |
209 | ||
210 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
211 | ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL); | |
212 | if (ch->crypto_recv[pn_space] == NULL) | |
213 | goto err; | |
214 | } | |
215 | ||
216 | if ((ch->stream0 = ossl_quic_stream_map_alloc(&ch->qsm, 0, | |
217 | QUIC_STREAM_INITIATOR_CLIENT | |
218 | | QUIC_STREAM_DIR_BIDI)) == NULL) | |
219 | goto err; | |
220 | ||
221 | if ((ch->stream0->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL) | |
222 | goto err; | |
223 | ||
224 | if ((ch->stream0->rstream = ossl_quic_rstream_new(NULL, NULL)) == NULL) | |
225 | goto err; | |
226 | ||
227 | if (!ossl_quic_txfc_init(&ch->stream0->txfc, &ch->conn_txfc)) | |
228 | goto err; | |
229 | ||
230 | if (!ossl_quic_rxfc_init(&ch->stream0->rxfc, &ch->conn_rxfc, | |
231 | 1 * 1024 * 1024, | |
232 | 5 * 1024 * 1024, | |
233 | get_time, NULL)) | |
234 | goto err; | |
235 | ||
2723d705 MC |
236 | /* Plug in the TLS handshake layer. */ |
237 | tls_args.s = ch->tls; | |
238 | tls_args.crypto_send_cb = ch_on_crypto_send; | |
239 | tls_args.crypto_send_cb_arg = ch; | |
240 | tls_args.crypto_recv_cb = ch_on_crypto_recv; | |
241 | tls_args.crypto_recv_cb_arg = ch; | |
242 | tls_args.yield_secret_cb = ch_on_handshake_yield_secret; | |
243 | tls_args.yield_secret_cb_arg = ch; | |
244 | tls_args.got_transport_params_cb = ch_on_transport_params; | |
245 | tls_args.got_transport_params_cb_arg= ch; | |
246 | tls_args.handshake_complete_cb = ch_on_handshake_complete; | |
247 | tls_args.handshake_complete_cb_arg = ch; | |
248 | tls_args.alert_cb = ch_on_handshake_alert; | |
249 | tls_args.alert_cb_arg = ch; | |
250 | tls_args.is_server = ch->is_server; | |
251 | ||
252 | if ((ch->qtls = ossl_quic_tls_new(&tls_args)) == NULL) | |
f538b421 HL |
253 | goto err; |
254 | ||
255 | /* | |
256 | * Determine the QUIC Transport Parameters and serialize the transport | |
257 | * parameters block. (For servers, we do this later as we must defer | |
258 | * generation until we have received the client's transport parameters.) | |
259 | */ | |
260 | if (!ch->is_server && !ch_generate_transport_params(ch)) | |
261 | goto err; | |
262 | ||
263 | ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY; | |
264 | ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP; | |
265 | ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT; | |
266 | ch->max_idle_timeout = QUIC_DEFAULT_IDLE_TIMEOUT; | |
267 | ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL; | |
45ecfc9b | 268 | ch->rx_enc_level = QUIC_ENC_LEVEL_INITIAL; |
f538b421 HL |
269 | ch_update_idle(ch); |
270 | ossl_quic_reactor_init(&ch->rtor, ch_tick, ch, | |
271 | ch_determine_next_tick_deadline(ch)); | |
272 | return 1; | |
273 | ||
274 | err: | |
275 | ch_cleanup(ch); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | static void ch_cleanup(QUIC_CHANNEL *ch) | |
280 | { | |
281 | uint32_t pn_space; | |
282 | ||
283 | if (ch->ackm != NULL) | |
284 | for (pn_space = QUIC_PN_SPACE_INITIAL; | |
285 | pn_space < QUIC_PN_SPACE_NUM; | |
286 | ++pn_space) | |
287 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
288 | ||
289 | ossl_quic_tx_packetiser_free(ch->txp); | |
290 | ossl_quic_txpim_free(ch->txpim); | |
291 | ossl_quic_cfq_free(ch->cfq); | |
292 | ossl_qtx_free(ch->qtx); | |
293 | if (ch->cc_data != NULL) | |
294 | ch->cc_method->free(ch->cc_data); | |
295 | if (ch->have_statm) | |
296 | ossl_statm_destroy(&ch->statm); | |
297 | ossl_ackm_free(ch->ackm); | |
298 | ||
299 | if (ch->stream0 != NULL) { | |
300 | assert(ch->have_qsm); | |
301 | ossl_quic_stream_map_release(&ch->qsm, ch->stream0); /* frees sstream */ | |
302 | } | |
303 | ||
304 | if (ch->have_qsm) | |
305 | ossl_quic_stream_map_cleanup(&ch->qsm); | |
306 | ||
307 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { | |
308 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
309 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
310 | } | |
311 | ||
312 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
313 | ch->qrx_pkt = NULL; | |
314 | ||
2723d705 | 315 | ossl_quic_tls_free(ch->qtls); |
f538b421 HL |
316 | ossl_qrx_free(ch->qrx); |
317 | ossl_quic_demux_free(ch->demux); | |
318 | OPENSSL_free(ch->local_transport_params); | |
f538b421 HL |
319 | } |
320 | ||
321 | QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args) | |
322 | { | |
323 | QUIC_CHANNEL *ch = NULL; | |
324 | ||
325 | if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL) | |
326 | return NULL; | |
327 | ||
328 | ch->libctx = args->libctx; | |
329 | ch->propq = args->propq; | |
330 | ch->is_server = args->is_server; | |
2723d705 | 331 | ch->tls = args->tls; |
f538b421 HL |
332 | |
333 | if (!ch_init(ch)) { | |
334 | OPENSSL_free(ch); | |
335 | return NULL; | |
336 | } | |
337 | ||
338 | return ch; | |
339 | } | |
340 | ||
341 | void ossl_quic_channel_free(QUIC_CHANNEL *ch) | |
342 | { | |
343 | if (ch == NULL) | |
344 | return; | |
345 | ||
346 | ch_cleanup(ch); | |
347 | OPENSSL_free(ch); | |
348 | } | |
349 | ||
14e31409 MC |
350 | /* Set mutator callbacks for test framework support */ |
351 | int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch, | |
352 | ossl_mutate_packet_cb mutatecb, | |
353 | ossl_finish_mutate_cb finishmutatecb, | |
354 | void *mutatearg) | |
355 | { | |
356 | if (ch->qtx == NULL) | |
357 | return 0; | |
358 | ||
359 | ossl_qtx_set_mutator(ch->qtx, mutatecb, finishmutatecb, mutatearg); | |
360 | return 1; | |
361 | } | |
362 | ||
f538b421 HL |
363 | int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr) |
364 | { | |
365 | *peer_addr = ch->cur_peer_addr; | |
366 | return 1; | |
367 | } | |
368 | ||
369 | int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr) | |
370 | { | |
371 | ch->cur_peer_addr = *peer_addr; | |
372 | return 1; | |
373 | } | |
374 | ||
375 | QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch) | |
376 | { | |
377 | return &ch->rtor; | |
378 | } | |
379 | ||
380 | QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch) | |
381 | { | |
382 | return &ch->qsm; | |
383 | } | |
384 | ||
385 | OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch) | |
386 | { | |
387 | return &ch->statm; | |
388 | } | |
389 | ||
390 | QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch, | |
391 | uint64_t stream_id) | |
392 | { | |
393 | return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id); | |
394 | } | |
395 | ||
396 | int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch) | |
397 | { | |
398 | return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE; | |
399 | } | |
400 | ||
149a8e6c MC |
401 | int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch, |
402 | QUIC_TERMINATE_CAUSE *cause) | |
f538b421 | 403 | { |
149a8e6c MC |
404 | if (ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING |
405 | || ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING) { | |
406 | if (cause != NULL) | |
407 | *cause = ch->terminate_cause; | |
408 | return 1; | |
409 | } | |
410 | return 0; | |
f538b421 HL |
411 | } |
412 | ||
149a8e6c MC |
413 | int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch, |
414 | QUIC_TERMINATE_CAUSE *cause) | |
f538b421 | 415 | { |
149a8e6c MC |
416 | if (ch->state == QUIC_CHANNEL_STATE_TERMINATED) { |
417 | if (cause != NULL) | |
418 | *cause = ch->terminate_cause; | |
419 | return 1; | |
420 | } | |
421 | return 0; | |
f538b421 HL |
422 | } |
423 | ||
149a8e6c MC |
424 | int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch, |
425 | QUIC_TERMINATE_CAUSE *cause) | |
f538b421 | 426 | { |
149a8e6c MC |
427 | return ossl_quic_channel_is_terminating(ch, cause) |
428 | || ossl_quic_channel_is_terminated(ch, cause); | |
f538b421 HL |
429 | } |
430 | ||
431 | int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch) | |
432 | { | |
433 | return ch->handshake_complete; | |
434 | } | |
435 | ||
ce8f20b6 MC |
436 | int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch) |
437 | { | |
438 | return ch->handshake_confirmed; | |
439 | } | |
440 | ||
f538b421 HL |
441 | /* |
442 | * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components | |
443 | * ================================================================ | |
444 | */ | |
445 | ||
446 | /* Used by various components. */ | |
447 | static OSSL_TIME get_time(void *arg) | |
448 | { | |
449 | return ossl_time_now(); | |
450 | } | |
451 | ||
452 | /* Used by QSM. */ | |
453 | static uint64_t get_stream_limit(int uni, void *arg) | |
454 | { | |
455 | QUIC_CHANNEL *ch = arg; | |
456 | ||
457 | return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi; | |
458 | } | |
459 | ||
460 | /* | |
461 | * Called by QRX to determine if a packet is potentially invalid before trying | |
462 | * to decrypt it. | |
463 | */ | |
464 | static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg) | |
465 | { | |
466 | QUIC_CHANNEL *ch = arg; | |
467 | ||
468 | /* Potential duplicates should not be processed. */ | |
469 | if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space)) | |
470 | return 0; | |
471 | ||
472 | return 1; | |
473 | } | |
474 | ||
475 | /* | |
476 | * QUIC Channel: Handshake Layer Event Handling | |
477 | * ============================================ | |
478 | */ | |
479 | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, | |
480 | size_t *consumed, void *arg) | |
481 | { | |
482 | int ret; | |
483 | QUIC_CHANNEL *ch = arg; | |
484 | uint32_t enc_level = ch->tx_enc_level; | |
485 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
486 | QUIC_SSTREAM *sstream = ch->crypto_send[pn_space]; | |
487 | ||
488 | if (!ossl_assert(sstream != NULL)) | |
489 | return 0; | |
490 | ||
491 | ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed); | |
492 | return ret; | |
493 | } | |
494 | ||
495 | static int crypto_ensure_empty(QUIC_RSTREAM *rstream) | |
496 | { | |
497 | size_t avail = 0; | |
498 | int is_fin = 0; | |
499 | ||
500 | if (rstream == NULL) | |
501 | return 1; | |
502 | ||
503 | if (!ossl_quic_rstream_available(rstream, &avail, &is_fin)) | |
504 | return 0; | |
505 | ||
506 | return avail == 0; | |
507 | } | |
508 | ||
509 | static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len, | |
510 | size_t *bytes_read, void *arg) | |
511 | { | |
512 | QUIC_CHANNEL *ch = arg; | |
513 | QUIC_RSTREAM *rstream; | |
514 | int is_fin = 0; /* crypto stream is never finished, so we don't use this */ | |
515 | uint32_t i; | |
516 | ||
517 | /* | |
518 | * After we move to a later EL we must not allow our peer to send any new | |
519 | * bytes in the crypto stream on a previous EL. Retransmissions of old bytes | |
520 | * are allowed. | |
521 | * | |
522 | * In practice we will only move to a new EL when we have consumed all bytes | |
523 | * which should be sent on the crypto stream at a previous EL. For example, | |
524 | * the Handshake EL should not be provisioned until we have completely | |
525 | * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output | |
526 | * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a | |
527 | * given EL is available we simply ensure we have not received any further | |
528 | * bytes at a lower EL. | |
529 | */ | |
45ecfc9b | 530 | for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->rx_enc_level; ++i) |
f538b421 HL |
531 | if (i != QUIC_ENC_LEVEL_0RTT && |
532 | !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { | |
533 | /* Protocol violation (RFC 9001 s. 4.1.3) */ | |
534 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
535 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
536 | "crypto stream data in wrong EL"); | |
537 | return 0; | |
538 | } | |
539 | ||
45ecfc9b | 540 | rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->rx_enc_level)]; |
f538b421 HL |
541 | if (rstream == NULL) |
542 | return 0; | |
543 | ||
544 | return ossl_quic_rstream_read(rstream, buf, buf_len, bytes_read, | |
545 | &is_fin); | |
546 | } | |
547 | ||
548 | static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction, | |
549 | uint32_t suite_id, EVP_MD *md, | |
550 | const unsigned char *secret, | |
551 | size_t secret_len, | |
552 | void *arg) | |
553 | { | |
554 | QUIC_CHANNEL *ch = arg; | |
555 | uint32_t i; | |
556 | ||
557 | if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM) | |
558 | /* Invalid EL. */ | |
559 | return 0; | |
560 | ||
f538b421 HL |
561 | |
562 | if (direction) { | |
563 | /* TX */ | |
45ecfc9b MC |
564 | if (enc_level <= ch->tx_enc_level) |
565 | /* | |
9f0ade7c HL |
566 | * Does not make sense for us to try and provision an EL we have already |
567 | * attained. | |
568 | */ | |
45ecfc9b MC |
569 | return 0; |
570 | ||
f538b421 HL |
571 | if (!ossl_qtx_provide_secret(ch->qtx, enc_level, |
572 | suite_id, md, | |
573 | secret, secret_len)) | |
574 | return 0; | |
575 | ||
576 | ch->tx_enc_level = enc_level; | |
577 | } else { | |
578 | /* RX */ | |
45ecfc9b MC |
579 | if (enc_level <= ch->rx_enc_level) |
580 | /* | |
9f0ade7c HL |
581 | * Does not make sense for us to try and provision an EL we have already |
582 | * attained. | |
583 | */ | |
45ecfc9b MC |
584 | return 0; |
585 | ||
586 | /* | |
9f0ade7c HL |
587 | * Ensure all crypto streams for previous ELs are now empty of available |
588 | * data. | |
589 | */ | |
45ecfc9b | 590 | for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i) |
e28f512f | 591 | if (!crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { |
45ecfc9b MC |
592 | /* Protocol violation (RFC 9001 s. 4.1.3) */ |
593 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
594 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
595 | "crypto stream data in wrong EL"); | |
596 | return 0; | |
597 | } | |
598 | ||
f538b421 HL |
599 | if (!ossl_qrx_provide_secret(ch->qrx, enc_level, |
600 | suite_id, md, | |
601 | secret, secret_len)) | |
602 | return 0; | |
92282a17 HL |
603 | |
604 | ch->have_new_rx_secret = 1; | |
45ecfc9b | 605 | ch->rx_enc_level = enc_level; |
f538b421 HL |
606 | } |
607 | ||
608 | return 1; | |
609 | } | |
610 | ||
611 | static int ch_on_handshake_complete(void *arg) | |
612 | { | |
613 | QUIC_CHANNEL *ch = arg; | |
614 | ||
e28f512f | 615 | if (!ossl_assert(!ch->handshake_complete)) |
f538b421 HL |
616 | return 0; /* this should not happen twice */ |
617 | ||
618 | if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT)) | |
619 | return 0; | |
620 | ||
62d0da12 | 621 | if (!ch->got_remote_transport_params) { |
f538b421 HL |
622 | /* |
623 | * Was not a valid QUIC handshake if we did not get valid transport | |
624 | * params. | |
625 | */ | |
62d0da12 MC |
626 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, |
627 | OSSL_QUIC_FRAME_TYPE_CRYPTO, | |
628 | "no transport parameters received"); | |
f538b421 | 629 | return 0; |
62d0da12 | 630 | } |
f538b421 HL |
631 | |
632 | /* Don't need transport parameters anymore. */ | |
633 | OPENSSL_free(ch->local_transport_params); | |
634 | ch->local_transport_params = NULL; | |
635 | ||
636 | /* Tell TXP the handshake is complete. */ | |
637 | ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp); | |
638 | ||
639 | ch->handshake_complete = 1; | |
b1b06da2 HL |
640 | |
641 | if (ch->is_server) { | |
642 | /* | |
643 | * On the server, the handshake is confirmed as soon as it is complete. | |
644 | */ | |
645 | ossl_quic_channel_on_handshake_confirmed(ch); | |
646 | ||
647 | ossl_quic_tx_packetiser_schedule_handshake_done(ch->txp); | |
648 | } | |
649 | ||
f538b421 HL |
650 | return 1; |
651 | } | |
652 | ||
653 | static int ch_on_handshake_alert(void *arg, unsigned char alert_code) | |
654 | { | |
655 | QUIC_CHANNEL *ch = arg; | |
656 | ||
657 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_ERR_BEGIN + alert_code, | |
658 | 0, "handshake alert"); | |
659 | return 1; | |
660 | } | |
661 | ||
662 | /* | |
663 | * QUIC Channel: Transport Parameter Handling | |
664 | * ========================================== | |
665 | */ | |
666 | ||
667 | /* | |
668 | * Called by handshake layer when we receive QUIC Transport Parameters from the | |
669 | * peer. Note that these are not authenticated until the handshake is marked | |
670 | * as complete. | |
671 | */ | |
3c567a52 HL |
672 | #define TP_REASON_SERVER_ONLY(x) \ |
673 | x " may not be sent by a client" | |
674 | #define TP_REASON_DUP(x) \ | |
675 | x " appears multiple times" | |
676 | #define TP_REASON_MALFORMED(x) \ | |
677 | x " is malformed" | |
678 | #define TP_REASON_EXPECTED_VALUE(x) \ | |
679 | x " does not match expected value" | |
680 | #define TP_REASON_NOT_RETRY(x) \ | |
681 | x " sent when not performing a retry" | |
682 | #define TP_REASON_REQUIRED(x) \ | |
683 | x " was not sent but is required" | |
684 | ||
f538b421 HL |
685 | static int ch_on_transport_params(const unsigned char *params, |
686 | size_t params_len, | |
687 | void *arg) | |
688 | { | |
689 | QUIC_CHANNEL *ch = arg; | |
690 | PACKET pkt; | |
691 | uint64_t id, v; | |
692 | size_t len; | |
693 | const unsigned char *body; | |
694 | int got_orig_dcid = 0; | |
695 | int got_initial_scid = 0; | |
696 | int got_retry_scid = 0; | |
697 | int got_initial_max_data = 0; | |
698 | int got_initial_max_stream_data_bidi_local = 0; | |
699 | int got_initial_max_stream_data_bidi_remote = 0; | |
700 | int got_initial_max_stream_data_uni = 0; | |
701 | int got_initial_max_streams_bidi = 0; | |
702 | int got_initial_max_streams_uni = 0; | |
703 | int got_ack_delay_exp = 0; | |
704 | int got_max_ack_delay = 0; | |
705 | int got_max_udp_payload_size = 0; | |
706 | int got_max_idle_timeout = 0; | |
707 | int got_active_conn_id_limit = 0; | |
708 | QUIC_CONN_ID cid; | |
3c567a52 | 709 | const char *reason = "bad transport parameter"; |
f538b421 HL |
710 | |
711 | if (ch->got_remote_transport_params) | |
712 | goto malformed; | |
713 | ||
714 | if (!PACKET_buf_init(&pkt, params, params_len)) | |
715 | return 0; | |
716 | ||
717 | while (PACKET_remaining(&pkt) > 0) { | |
718 | if (!ossl_quic_wire_peek_transport_param(&pkt, &id)) | |
719 | goto malformed; | |
720 | ||
721 | switch (id) { | |
75b2920a | 722 | case QUIC_TPARAM_ORIG_DCID: |
3c567a52 HL |
723 | if (got_orig_dcid) { |
724 | reason = TP_REASON_DUP("ORIG_DCID"); | |
725 | goto malformed; | |
726 | } | |
727 | ||
728 | if (ch->is_server) { | |
729 | reason = TP_REASON_SERVER_ONLY("ORIG_DCID"); | |
75b2920a | 730 | goto malformed; |
3c567a52 | 731 | } |
75b2920a | 732 | |
3c567a52 HL |
733 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
734 | reason = TP_REASON_MALFORMED("ORIG_DCID"); | |
75b2920a | 735 | goto malformed; |
3c567a52 | 736 | } |
75b2920a HL |
737 | |
738 | /* Must match our initial DCID. */ | |
3c567a52 HL |
739 | if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid)) { |
740 | reason = TP_REASON_EXPECTED_VALUE("ORIG_DCID"); | |
75b2920a | 741 | goto malformed; |
3c567a52 | 742 | } |
75b2920a HL |
743 | |
744 | got_orig_dcid = 1; | |
745 | break; | |
746 | ||
747 | case QUIC_TPARAM_RETRY_SCID: | |
3c567a52 HL |
748 | if (ch->is_server) { |
749 | reason = TP_REASON_SERVER_ONLY("RETRY_SCID"); | |
750 | goto malformed; | |
751 | } | |
752 | ||
753 | if (got_retry_scid) { | |
754 | reason = TP_REASON_DUP("RETRY_SCID"); | |
75b2920a | 755 | goto malformed; |
3c567a52 HL |
756 | } |
757 | ||
758 | if (!ch->doing_retry) { | |
759 | reason = TP_REASON_NOT_RETRY("RETRY_SCID"); | |
760 | goto malformed; | |
761 | } | |
75b2920a | 762 | |
3c567a52 HL |
763 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
764 | reason = TP_REASON_MALFORMED("RETRY_SCID"); | |
75b2920a | 765 | goto malformed; |
3c567a52 | 766 | } |
75b2920a HL |
767 | |
768 | /* Must match Retry packet SCID. */ | |
3c567a52 HL |
769 | if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid)) { |
770 | reason = TP_REASON_EXPECTED_VALUE("RETRY_SCID"); | |
75b2920a | 771 | goto malformed; |
3c567a52 | 772 | } |
75b2920a HL |
773 | |
774 | got_retry_scid = 1; | |
775 | break; | |
776 | ||
777 | case QUIC_TPARAM_INITIAL_SCID: | |
3c567a52 | 778 | if (got_initial_scid) { |
75b2920a | 779 | /* must not appear more than once */ |
3c567a52 | 780 | reason = TP_REASON_DUP("INITIAL_SCID"); |
75b2920a | 781 | goto malformed; |
3c567a52 | 782 | } |
75b2920a | 783 | |
3c567a52 HL |
784 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
785 | reason = TP_REASON_MALFORMED("INITIAL_SCID"); | |
75b2920a | 786 | goto malformed; |
3c567a52 | 787 | } |
75b2920a HL |
788 | |
789 | /* Must match SCID of first Initial packet from server. */ | |
3c567a52 HL |
790 | if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) { |
791 | reason = TP_REASON_EXPECTED_VALUE("INITIAL_SCID"); | |
75b2920a | 792 | goto malformed; |
3c567a52 | 793 | } |
75b2920a HL |
794 | |
795 | got_initial_scid = 1; | |
796 | break; | |
797 | ||
798 | case QUIC_TPARAM_INITIAL_MAX_DATA: | |
3c567a52 | 799 | if (got_initial_max_data) { |
75b2920a | 800 | /* must not appear more than once */ |
3c567a52 | 801 | reason = TP_REASON_DUP("INITIAL_MAX_DATA"); |
75b2920a | 802 | goto malformed; |
3c567a52 | 803 | } |
75b2920a | 804 | |
3c567a52 HL |
805 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
806 | reason = TP_REASON_MALFORMED("INITIAL_MAX_DATA"); | |
75b2920a | 807 | goto malformed; |
3c567a52 | 808 | } |
75b2920a HL |
809 | |
810 | ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v); | |
811 | got_initial_max_data = 1; | |
812 | break; | |
813 | ||
814 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: | |
3c567a52 | 815 | if (got_initial_max_stream_data_bidi_local) { |
75b2920a | 816 | /* must not appear more than once */ |
3c567a52 | 817 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); |
75b2920a | 818 | goto malformed; |
3c567a52 | 819 | } |
75b2920a | 820 | |
3c567a52 HL |
821 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
822 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); | |
75b2920a | 823 | goto malformed; |
3c567a52 | 824 | } |
f538b421 HL |
825 | |
826 | /* | |
75b2920a HL |
827 | * This is correct; the BIDI_LOCAL TP governs streams created by |
828 | * the endpoint which sends the TP, i.e., our peer. | |
f538b421 | 829 | */ |
75b2920a HL |
830 | ch->init_max_stream_data_bidi_remote = v; |
831 | got_initial_max_stream_data_bidi_local = 1; | |
832 | break; | |
833 | ||
834 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: | |
3c567a52 | 835 | if (got_initial_max_stream_data_bidi_remote) { |
75b2920a | 836 | /* must not appear more than once */ |
3c567a52 | 837 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); |
75b2920a | 838 | goto malformed; |
3c567a52 | 839 | } |
75b2920a | 840 | |
3c567a52 HL |
841 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
842 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); | |
75b2920a | 843 | goto malformed; |
3c567a52 | 844 | } |
75b2920a HL |
845 | |
846 | /* | |
847 | * This is correct; the BIDI_REMOTE TP governs streams created | |
848 | * by the endpoint which receives the TP, i.e., us. | |
849 | */ | |
850 | ch->init_max_stream_data_bidi_local = v; | |
851 | ||
852 | /* Apply to stream 0. */ | |
853 | ossl_quic_txfc_bump_cwm(&ch->stream0->txfc, v); | |
854 | got_initial_max_stream_data_bidi_remote = 1; | |
855 | break; | |
856 | ||
857 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI: | |
3c567a52 | 858 | if (got_initial_max_stream_data_uni) { |
75b2920a | 859 | /* must not appear more than once */ |
3c567a52 | 860 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_UNI"); |
75b2920a | 861 | goto malformed; |
3c567a52 | 862 | } |
75b2920a | 863 | |
3c567a52 HL |
864 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
865 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_UNI"); | |
75b2920a | 866 | goto malformed; |
3c567a52 | 867 | } |
75b2920a HL |
868 | |
869 | ch->init_max_stream_data_uni_remote = v; | |
870 | got_initial_max_stream_data_uni = 1; | |
871 | break; | |
872 | ||
873 | case QUIC_TPARAM_ACK_DELAY_EXP: | |
3c567a52 | 874 | if (got_ack_delay_exp) { |
75b2920a | 875 | /* must not appear more than once */ |
3c567a52 | 876 | reason = TP_REASON_DUP("ACK_DELAY_EXP"); |
75b2920a | 877 | goto malformed; |
3c567a52 | 878 | } |
75b2920a HL |
879 | |
880 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
881 | || v > QUIC_MAX_ACK_DELAY_EXP) { |
882 | reason = TP_REASON_MALFORMED("ACK_DELAY_EXP"); | |
75b2920a | 883 | goto malformed; |
3c567a52 | 884 | } |
75b2920a HL |
885 | |
886 | ch->rx_ack_delay_exp = (unsigned char)v; | |
887 | got_ack_delay_exp = 1; | |
888 | break; | |
889 | ||
890 | case QUIC_TPARAM_MAX_ACK_DELAY: | |
3c567a52 | 891 | if (got_max_ack_delay) { |
75b2920a | 892 | /* must not appear more than once */ |
3c567a52 | 893 | reason = TP_REASON_DUP("MAX_ACK_DELAY"); |
75b2920a | 894 | return 0; |
3c567a52 | 895 | } |
75b2920a HL |
896 | |
897 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
898 | || v >= (((uint64_t)1) << 14)) { |
899 | reason = TP_REASON_MALFORMED("MAX_ACK_DELAY"); | |
75b2920a | 900 | goto malformed; |
3c567a52 | 901 | } |
75b2920a HL |
902 | |
903 | ch->rx_max_ack_delay = v; | |
904 | got_max_ack_delay = 1; | |
905 | break; | |
906 | ||
907 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI: | |
3c567a52 | 908 | if (got_initial_max_streams_bidi) { |
75b2920a | 909 | /* must not appear more than once */ |
3c567a52 | 910 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_BIDI"); |
75b2920a | 911 | return 0; |
3c567a52 | 912 | } |
75b2920a HL |
913 | |
914 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
915 | || v > (((uint64_t)1) << 60)) { |
916 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_BIDI"); | |
75b2920a | 917 | goto malformed; |
3c567a52 | 918 | } |
75b2920a HL |
919 | |
920 | assert(ch->max_local_streams_bidi == 0); | |
921 | ch->max_local_streams_bidi = v; | |
922 | got_initial_max_streams_bidi = 1; | |
923 | break; | |
924 | ||
925 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI: | |
3c567a52 | 926 | if (got_initial_max_streams_uni) { |
75b2920a | 927 | /* must not appear more than once */ |
3c567a52 | 928 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_UNI"); |
75b2920a | 929 | goto malformed; |
3c567a52 | 930 | } |
75b2920a HL |
931 | |
932 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
933 | || v > (((uint64_t)1) << 60)) { |
934 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_UNI"); | |
75b2920a | 935 | goto malformed; |
3c567a52 | 936 | } |
75b2920a HL |
937 | |
938 | assert(ch->max_local_streams_uni == 0); | |
939 | ch->max_local_streams_uni = v; | |
940 | got_initial_max_streams_uni = 1; | |
941 | break; | |
942 | ||
943 | case QUIC_TPARAM_MAX_IDLE_TIMEOUT: | |
3c567a52 | 944 | if (got_max_idle_timeout) { |
75b2920a | 945 | /* must not appear more than once */ |
3c567a52 | 946 | reason = TP_REASON_DUP("MAX_IDLE_TIMEOUT"); |
75b2920a | 947 | goto malformed; |
3c567a52 | 948 | } |
75b2920a | 949 | |
3c567a52 HL |
950 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
951 | reason = TP_REASON_MALFORMED("MAX_IDLE_TIMEOUT"); | |
75b2920a | 952 | goto malformed; |
3c567a52 | 953 | } |
75b2920a HL |
954 | |
955 | if (v < ch->max_idle_timeout) | |
956 | ch->max_idle_timeout = v; | |
957 | ||
958 | ch_update_idle(ch); | |
959 | got_max_idle_timeout = 1; | |
960 | break; | |
f538b421 | 961 | |
75b2920a | 962 | case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE: |
3c567a52 | 963 | if (got_max_udp_payload_size) { |
75b2920a | 964 | /* must not appear more than once */ |
3c567a52 | 965 | reason = TP_REASON_DUP("MAX_UDP_PAYLOAD_SIZE"); |
75b2920a | 966 | goto malformed; |
3c567a52 | 967 | } |
f538b421 | 968 | |
75b2920a | 969 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
3c567a52 HL |
970 | || v < QUIC_MIN_INITIAL_DGRAM_LEN) { |
971 | reason = TP_REASON_MALFORMED("MAX_UDP_PAYLOAD_SIZE"); | |
75b2920a | 972 | goto malformed; |
3c567a52 | 973 | } |
75b2920a HL |
974 | |
975 | ch->rx_max_udp_payload_size = v; | |
976 | got_max_udp_payload_size = 1; | |
977 | break; | |
978 | ||
979 | case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT: | |
3c567a52 | 980 | if (got_active_conn_id_limit) { |
75b2920a | 981 | /* must not appear more than once */ |
3c567a52 | 982 | reason = TP_REASON_DUP("ACTIVE_CONN_ID_LIMIT"); |
75b2920a | 983 | goto malformed; |
3c567a52 | 984 | } |
75b2920a HL |
985 | |
986 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) | |
3c567a52 HL |
987 | || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT) { |
988 | reason = TP_REASON_MALFORMED("ACTIVE_CONN_ID_LIMIT"); | |
75b2920a | 989 | goto malformed; |
3c567a52 | 990 | } |
75b2920a HL |
991 | |
992 | ch->rx_active_conn_id_limit = v; | |
993 | got_active_conn_id_limit = 1; | |
994 | break; | |
995 | ||
3c567a52 HL |
996 | case QUIC_TPARAM_STATELESS_RESET_TOKEN: |
997 | /* TODO(QUIC): Handle stateless reset tokens. */ | |
998 | /* | |
999 | * We ignore these for now, but we must ensure a client doesn't | |
1000 | * send them. | |
1001 | */ | |
1002 | if (ch->is_server) { | |
1003 | reason = TP_REASON_SERVER_ONLY("STATELESS_RESET_TOKEN"); | |
1004 | goto malformed; | |
1005 | } | |
1006 | ||
1007 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); | |
1008 | if (body == NULL || len != QUIC_STATELESS_RESET_TOKEN_LEN) { | |
1009 | reason = TP_REASON_MALFORMED("STATELESS_RESET_TOKEN"); | |
1010 | goto malformed; | |
1011 | } | |
1012 | ||
1013 | break; | |
1014 | ||
1015 | case QUIC_TPARAM_PREFERRED_ADDR: | |
1016 | /* TODO(QUIC): Handle preferred address. */ | |
1017 | if (ch->is_server) { | |
1018 | reason = TP_REASON_SERVER_ONLY("PREFERRED_ADDR"); | |
1019 | goto malformed; | |
1020 | } | |
1021 | ||
1022 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); | |
1023 | if (body == NULL) { | |
1024 | reason = TP_REASON_MALFORMED("PREFERRED_ADDR"); | |
1025 | goto malformed; | |
1026 | } | |
1027 | ||
1028 | break; | |
75b2920a HL |
1029 | |
1030 | case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION: | |
1031 | /* We do not currently handle migration, so nothing to do. */ | |
1032 | default: | |
1033 | /* Skip over and ignore. */ | |
1034 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, | |
1035 | &len); | |
1036 | if (body == NULL) | |
1037 | goto malformed; | |
1038 | ||
1039 | break; | |
f538b421 HL |
1040 | } |
1041 | } | |
1042 | ||
3c567a52 HL |
1043 | if (!got_initial_scid) { |
1044 | reason = TP_REASON_REQUIRED("INITIAL_SCID"); | |
f538b421 | 1045 | goto malformed; |
3c567a52 HL |
1046 | } |
1047 | ||
1048 | if (!ch->is_server) { | |
1049 | if (!got_orig_dcid) { | |
1050 | reason = TP_REASON_REQUIRED("ORIG_DCID"); | |
1051 | goto malformed; | |
1052 | } | |
1053 | ||
1054 | if (ch->doing_retry && !got_retry_scid) { | |
1055 | reason = TP_REASON_REQUIRED("RETRY_SCID"); | |
1056 | goto malformed; | |
1057 | } | |
1058 | } | |
f538b421 HL |
1059 | |
1060 | ch->got_remote_transport_params = 1; | |
1061 | ||
1062 | if (got_initial_max_data || got_initial_max_stream_data_bidi_remote | |
1063 | || got_initial_max_streams_bidi || got_initial_max_streams_uni) | |
1064 | /* If FC credit was bumped, we may now be able to send. */ | |
1065 | ossl_quic_stream_map_update_state(&ch->qsm, ch->stream0); | |
1066 | ||
1067 | /* If we are a server, we now generate our own transport parameters. */ | |
1068 | if (ch->is_server && !ch_generate_transport_params(ch)) { | |
1069 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
1070 | "internal error"); | |
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | return 1; | |
1075 | ||
1076 | malformed: | |
1077 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_TRANSPORT_PARAMETER_ERROR, | |
3c567a52 | 1078 | 0, reason); |
f538b421 HL |
1079 | return 0; |
1080 | } | |
1081 | ||
1082 | /* | |
1083 | * Called when we want to generate transport parameters. This is called | |
1084 | * immediately at instantiation time for a client and after we receive the | |
1085 | * client's transport parameters for a server. | |
1086 | */ | |
1087 | static int ch_generate_transport_params(QUIC_CHANNEL *ch) | |
1088 | { | |
1089 | int ok = 0; | |
1090 | BUF_MEM *buf_mem = NULL; | |
1091 | WPACKET wpkt; | |
1092 | int wpkt_valid = 0; | |
1093 | size_t buf_len = 0; | |
1094 | ||
1095 | if (ch->local_transport_params != NULL) | |
1096 | goto err; | |
1097 | ||
1098 | if ((buf_mem = BUF_MEM_new()) == NULL) | |
1099 | goto err; | |
1100 | ||
1101 | if (!WPACKET_init(&wpkt, buf_mem)) | |
1102 | goto err; | |
1103 | ||
1104 | wpkt_valid = 1; | |
1105 | ||
1106 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION, | |
1107 | NULL, 0) == NULL) | |
1108 | goto err; | |
1109 | ||
3c567a52 HL |
1110 | if (ch->is_server) { |
1111 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_ORIG_DCID, | |
1112 | &ch->init_dcid)) | |
1113 | goto err; | |
1114 | ||
1115 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_INITIAL_SCID, | |
1116 | &ch->cur_local_dcid)) | |
1117 | goto err; | |
1118 | } else { | |
1119 | /* Client always uses an empty SCID. */ | |
1120 | if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_INITIAL_SCID, | |
1121 | NULL, 0) == NULL) | |
1122 | goto err; | |
1123 | } | |
f538b421 HL |
1124 | |
1125 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT, | |
1126 | ch->max_idle_timeout)) | |
1127 | goto err; | |
1128 | ||
1129 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE, | |
1130 | QUIC_MIN_INITIAL_DGRAM_LEN)) | |
1131 | goto err; | |
1132 | ||
1133 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT, | |
1134 | 4)) | |
1135 | goto err; | |
1136 | ||
1137 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA, | |
1138 | ossl_quic_rxfc_get_cwm(&ch->conn_rxfc))) | |
1139 | goto err; | |
1140 | ||
1141 | /* | |
1142 | * We actually want the default CWM for a new RXFC, but here we just use | |
1143 | * stream0 as a representative specimen. TODO(QUIC): revisit this when we | |
1144 | * support multiple streams. | |
1145 | */ | |
1146 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, | |
1147 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
1148 | goto err; | |
1149 | ||
1150 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, | |
1151 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
1152 | goto err; | |
1153 | ||
1154 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI, | |
1155 | ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc))) | |
1156 | goto err; | |
1157 | ||
1158 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI, | |
3c567a52 | 1159 | ch->is_server ? 1 : 0)) |
f538b421 HL |
1160 | goto err; |
1161 | ||
1162 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI, | |
1163 | 0)) | |
1164 | goto err; | |
1165 | ||
1166 | if (!WPACKET_get_total_written(&wpkt, &buf_len)) | |
1167 | goto err; | |
1168 | ||
1169 | ch->local_transport_params = (unsigned char *)buf_mem->data; | |
1170 | buf_mem->data = NULL; | |
1171 | ||
1172 | if (!WPACKET_finish(&wpkt)) | |
1173 | goto err; | |
1174 | ||
1175 | wpkt_valid = 0; | |
1176 | ||
2723d705 | 1177 | if (!ossl_quic_tls_set_transport_params(ch->qtls, ch->local_transport_params, |
f538b421 HL |
1178 | buf_len)) |
1179 | goto err; | |
1180 | ||
1181 | ok = 1; | |
1182 | err: | |
1183 | if (wpkt_valid) | |
1184 | WPACKET_cleanup(&wpkt); | |
1185 | BUF_MEM_free(buf_mem); | |
1186 | return ok; | |
1187 | } | |
1188 | ||
1189 | /* | |
1190 | * QUIC Channel: Ticker-Mutator | |
1191 | * ============================ | |
1192 | */ | |
1193 | ||
1194 | /* | |
1195 | * The central ticker function called by the reactor. This does everything, or | |
1196 | * at least everything network I/O related. Best effort - not allowed to fail | |
1197 | * "loudly". | |
1198 | */ | |
1199 | static void ch_tick(QUIC_TICK_RESULT *res, void *arg) | |
1200 | { | |
1201 | OSSL_TIME now, deadline; | |
1202 | QUIC_CHANNEL *ch = arg; | |
1203 | ||
1204 | /* | |
1205 | * When we tick the QUIC connection, we do everything we need to do | |
1206 | * periodically. In order, we: | |
1207 | * | |
1208 | * - handle any incoming data from the network; | |
1209 | * - handle any timer events which are due to fire (ACKM, etc.) | |
1210 | * - write any data to the network due to be sent, to the extent | |
1211 | * possible; | |
1212 | * - determine the time at which we should next be ticked. | |
1213 | */ | |
1214 | ||
1215 | /* If we are in the TERMINATED state, there is nothing to do. */ | |
149a8e6c | 1216 | if (ossl_quic_channel_is_terminated(ch, NULL)) { |
b639475a HL |
1217 | res->net_read_desired = 0; |
1218 | res->net_write_desired = 0; | |
1219 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1220 | return; |
1221 | } | |
1222 | ||
1223 | /* | |
1224 | * If we are in the TERMINATING state, check if the terminating timer has | |
1225 | * expired. | |
1226 | */ | |
149a8e6c | 1227 | if (ossl_quic_channel_is_terminating(ch, NULL)) { |
f538b421 HL |
1228 | now = ossl_time_now(); |
1229 | ||
1230 | if (ossl_time_compare(now, ch->terminate_deadline) >= 0) { | |
1231 | ch_on_terminating_timeout(ch); | |
b639475a HL |
1232 | res->net_read_desired = 0; |
1233 | res->net_write_desired = 0; | |
1234 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1235 | return; /* abort normal processing, nothing to do */ |
1236 | } | |
1237 | } | |
1238 | ||
3bf4dc8c HL |
1239 | /* Handle any incoming data from network. */ |
1240 | ch_rx_pre(ch); | |
1241 | ||
4e64437a | 1242 | do { |
3bf4dc8c | 1243 | /* Process queued incoming packets. */ |
4e64437a | 1244 | ch_rx(ch); |
f538b421 | 1245 | |
4e64437a HL |
1246 | /* |
1247 | * Allow the handshake layer to check for any new incoming data and generate | |
1248 | * new outgoing data. | |
1249 | */ | |
92282a17 | 1250 | ch->have_new_rx_secret = 0; |
2723d705 | 1251 | ossl_quic_tls_tick(ch->qtls); |
4e64437a HL |
1252 | |
1253 | /* | |
1254 | * If the handshake layer gave us a new secret, we need to do RX again | |
1255 | * because packets that were not previously processable and were | |
1256 | * deferred might now be processable. | |
9f0ade7c HL |
1257 | * |
1258 | * TODO(QUIC): Consider handling this in the yield_secret callback. | |
4e64437a | 1259 | */ |
92282a17 | 1260 | } while (ch->have_new_rx_secret); |
f538b421 HL |
1261 | |
1262 | /* | |
1263 | * Handle any timer events which are due to fire; namely, the loss detection | |
1264 | * deadline and the idle timeout. | |
1265 | * | |
1266 | * ACKM ACK generation deadline is polled by TXP, so we don't need to handle | |
1267 | * it here. | |
1268 | */ | |
1269 | now = ossl_time_now(); | |
1270 | if (ossl_time_compare(now, ch->idle_deadline) >= 0) { | |
1271 | /* | |
1272 | * Idle timeout differs from normal protocol violation because we do not | |
1273 | * send a CONN_CLOSE frame; go straight to TERMINATED. | |
1274 | */ | |
1275 | ch_on_idle_timeout(ch); | |
b639475a HL |
1276 | res->net_read_desired = 0; |
1277 | res->net_write_desired = 0; | |
1278 | res->tick_deadline = ossl_time_infinite(); | |
f538b421 HL |
1279 | return; |
1280 | } | |
1281 | ||
1282 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); | |
1283 | if (!ossl_time_is_zero(deadline) && ossl_time_compare(now, deadline) >= 0) | |
1284 | ossl_ackm_on_timeout(ch->ackm); | |
1285 | ||
1286 | /* Write any data to the network due to be sent. */ | |
1287 | ch_tx(ch); | |
1288 | ||
1289 | /* Determine the time at which we should next be ticked. */ | |
1290 | res->tick_deadline = ch_determine_next_tick_deadline(ch); | |
1291 | ||
df15e990 HL |
1292 | /* |
1293 | * Always process network input unless we are now terminated. | |
1294 | * Although we had not terminated at the beginning of this tick, network | |
1295 | * errors in ch_rx_pre() or ch_tx() may have caused us to transition to the | |
1296 | * Terminated state. | |
1297 | */ | |
149a8e6c | 1298 | res->net_read_desired = !ossl_quic_channel_is_terminated(ch, NULL); |
f538b421 HL |
1299 | |
1300 | /* We want to write to the network if we have any in our queue. */ | |
b639475a | 1301 | res->net_write_desired |
149a8e6c | 1302 | = (!ossl_quic_channel_is_terminated(ch, NULL) |
df15e990 | 1303 | && ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0); |
f538b421 HL |
1304 | } |
1305 | ||
3bf4dc8c HL |
1306 | /* Process incoming datagrams, if any. */ |
1307 | static void ch_rx_pre(QUIC_CHANNEL *ch) | |
1308 | { | |
df15e990 HL |
1309 | int ret; |
1310 | ||
b1b06da2 | 1311 | if (!ch->is_server && !ch->have_sent_any_pkt) |
3bf4dc8c HL |
1312 | return; |
1313 | ||
1314 | /* | |
1315 | * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams | |
1316 | * to the appropriate QRX instance. | |
1317 | */ | |
df15e990 HL |
1318 | ret = ossl_quic_demux_pump(ch->demux); |
1319 | if (ret == QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL) | |
1320 | /* | |
1321 | * We don't care about transient failure, but permanent failure means we | |
1322 | * should tear down the connection as though a protocol violation | |
1323 | * occurred. Skip straight to the Terminating state as there is no point | |
1324 | * trying to send CONNECTION_CLOSE frames if the network BIO is not | |
1325 | * operating correctly. | |
1326 | */ | |
1327 | ch_raise_net_error(ch); | |
3bf4dc8c HL |
1328 | } |
1329 | ||
1330 | /* Process queued incoming packets and handle frames, if any. */ | |
f538b421 HL |
1331 | static int ch_rx(QUIC_CHANNEL *ch) |
1332 | { | |
1333 | int handled_any = 0; | |
1334 | ||
b1b06da2 | 1335 | if (!ch->is_server && !ch->have_sent_any_pkt) |
f538b421 HL |
1336 | /* |
1337 | * We have not sent anything yet, therefore there is no need to check | |
75b2920a | 1338 | * for incoming data. |
f538b421 HL |
1339 | */ |
1340 | return 1; | |
1341 | ||
f538b421 HL |
1342 | for (;;) { |
1343 | assert(ch->qrx_pkt == NULL); | |
1344 | ||
1345 | if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt)) | |
1346 | break; | |
1347 | ||
1348 | if (!handled_any) | |
1349 | ch_update_idle(ch); | |
1350 | ||
1351 | ch_rx_handle_packet(ch); /* best effort */ | |
1352 | ||
1353 | /* | |
1354 | * Regardless of the outcome of frame handling, unref the packet. | |
1355 | * This will free the packet unless something added another | |
1356 | * reference to it during frame processing. | |
1357 | */ | |
1358 | ossl_qrx_pkt_release(ch->qrx_pkt); | |
1359 | ch->qrx_pkt = NULL; | |
1360 | ||
1361 | handled_any = 1; | |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we | |
1366 | * process one or more incoming packets. | |
1367 | */ | |
1368 | if (handled_any && ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) | |
1369 | ch->conn_close_queued = 1; | |
1370 | ||
1371 | return 1; | |
1372 | } | |
1373 | ||
1374 | /* Handles the packet currently in ch->qrx_pkt->hdr. */ | |
1375 | static void ch_rx_handle_packet(QUIC_CHANNEL *ch) | |
1376 | { | |
1377 | uint32_t enc_level; | |
1378 | ||
1379 | assert(ch->qrx_pkt != NULL); | |
1380 | ||
1381 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) { | |
1382 | if (!ch->have_received_enc_pkt) { | |
1383 | ch->init_scid = ch->qrx_pkt->hdr->src_conn_id; | |
1384 | ch->have_received_enc_pkt = 1; | |
1385 | ||
1386 | /* | |
1387 | * We change to using the SCID in the first Initial packet as the | |
1388 | * DCID. | |
1389 | */ | |
1390 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid); | |
1391 | } | |
1392 | ||
1393 | enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type); | |
1394 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
1395 | /* Do not process packets from ELs we have already discarded. */ | |
1396 | return; | |
1397 | } | |
1398 | ||
1399 | /* Handle incoming packet. */ | |
1400 | switch (ch->qrx_pkt->hdr->type) { | |
75b2920a | 1401 | case QUIC_PKT_TYPE_RETRY: |
b1b06da2 | 1402 | if (ch->doing_retry || ch->is_server) |
75b2920a HL |
1403 | /* |
1404 | * It is not allowed to ask a client to do a retry more than | |
b1b06da2 | 1405 | * once. Clients may not send retries. |
75b2920a HL |
1406 | */ |
1407 | return; | |
f538b421 | 1408 | |
75b2920a HL |
1409 | if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN) |
1410 | /* Packets with zero-length Retry Tokens are invalid. */ | |
1411 | return; | |
f538b421 | 1412 | |
75b2920a HL |
1413 | /* |
1414 | * TODO(QUIC): Theoretically this should probably be in the QRX. | |
1415 | * However because validation is dependent on context (namely the | |
1416 | * client's initial DCID) we can't do this cleanly. In the future we | |
1417 | * should probably add a callback to the QRX to let it call us (via | |
1418 | * the DEMUX) and ask us about the correct original DCID, rather | |
1419 | * than allow the QRX to emit a potentially malformed packet to the | |
1420 | * upper layers. However, special casing this will do for now. | |
1421 | */ | |
1422 | if (!ossl_quic_validate_retry_integrity_tag(ch->libctx, | |
1423 | ch->propq, | |
1424 | ch->qrx_pkt->hdr, | |
1425 | &ch->init_dcid)) | |
1426 | /* Malformed retry packet, ignore. */ | |
1427 | return; | |
f538b421 | 1428 | |
75b2920a HL |
1429 | ch_retry(ch, ch->qrx_pkt->hdr->data, |
1430 | ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN, | |
1431 | &ch->qrx_pkt->hdr->src_conn_id); | |
1432 | break; | |
f538b421 | 1433 | |
75b2920a | 1434 | case QUIC_PKT_TYPE_0RTT: |
b1b06da2 HL |
1435 | if (!ch->is_server) |
1436 | /* Clients should never receive 0-RTT packets. */ | |
1437 | return; | |
1438 | ||
1439 | /* | |
1440 | * TODO(QUIC): Implement 0-RTT on the server side. We currently do | |
1441 | * not need to implement this as a client can only do 0-RTT if we | |
1442 | * have given it permission to in a previous session. | |
1443 | */ | |
75b2920a HL |
1444 | break; |
1445 | ||
b1b06da2 HL |
1446 | case QUIC_PKT_TYPE_INITIAL: |
1447 | case QUIC_PKT_TYPE_HANDSHAKE: | |
1448 | case QUIC_PKT_TYPE_1RTT: | |
75b2920a HL |
1449 | if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE) |
1450 | /* | |
1451 | * We automatically drop INITIAL EL keys when first successfully | |
1452 | * decrypting a HANDSHAKE packet, as per the RFC. | |
1453 | */ | |
1454 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); | |
1455 | ||
1456 | /* This packet contains frames, pass to the RXDP. */ | |
1457 | ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */ | |
1458 | break; | |
b1b06da2 HL |
1459 | |
1460 | default: | |
1461 | assert(0); | |
1462 | break; | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | /* | |
1467 | * This is called by the demux when we get a packet not destined for any known | |
1468 | * DCID. | |
1469 | */ | |
1470 | static void ch_default_packet_handler(QUIC_URXE *e, void *arg) | |
1471 | { | |
1472 | QUIC_CHANNEL *ch = arg; | |
1473 | PACKET pkt; | |
1474 | QUIC_PKT_HDR hdr; | |
1475 | ||
1476 | if (!ossl_assert(ch->is_server)) | |
1477 | goto undesirable; | |
1478 | ||
1479 | /* | |
1480 | * We only support one connection to our server currently, so if we already | |
1481 | * started one, ignore any new connection attempts. | |
1482 | */ | |
1483 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) | |
1484 | goto undesirable; | |
1485 | ||
1486 | /* | |
1487 | * We have got a packet for an unknown DCID. This might be an attempt to | |
1488 | * open a new connection. | |
1489 | */ | |
1490 | if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN) | |
1491 | goto undesirable; | |
1492 | ||
1493 | if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len)) | |
091f532e | 1494 | goto err; |
b1b06da2 HL |
1495 | |
1496 | /* | |
1497 | * We set short_conn_id_len to SIZE_MAX here which will cause the decode | |
1498 | * operation to fail if we get a 1-RTT packet. This is fine since we only | |
1499 | * care about Initial packets. | |
1500 | */ | |
1501 | if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, &hdr, NULL)) | |
1502 | goto undesirable; | |
1503 | ||
1504 | switch (hdr.version) { | |
1505 | case QUIC_VERSION_1: | |
1506 | break; | |
1507 | ||
1508 | case QUIC_VERSION_NONE: | |
1509 | default: | |
1510 | /* Unknown version or proactive version negotiation request, bail. */ | |
1511 | /* TODO(QUIC): Handle version negotiation on server side */ | |
1512 | goto undesirable; | |
f538b421 | 1513 | } |
b1b06da2 HL |
1514 | |
1515 | /* | |
1516 | * We only care about Initial packets which might be trying to establish a | |
1517 | * connection. | |
1518 | */ | |
1519 | if (hdr.type != QUIC_PKT_TYPE_INITIAL) | |
1520 | goto undesirable; | |
1521 | ||
1522 | /* | |
1523 | * Assume this is a valid attempt to initiate a connection. | |
1524 | * | |
1525 | * We do not register the DCID in the initial packet we received and that | |
1526 | * DCID is not actually used again, thus after provisioning the correct | |
1527 | * Initial keys derived from it (which is done in the call below) we pass | |
1528 | * the received packet directly to the QRX so that it can process it as a | |
1529 | * one-time thing, instead of going through the usual DEMUX DCID-based | |
1530 | * routing. | |
1531 | */ | |
1532 | if (!ch_server_on_new_conn(ch, &e->peer, | |
1533 | &hdr.src_conn_id, | |
1534 | &hdr.dst_conn_id)) | |
091f532e | 1535 | goto err; |
b1b06da2 HL |
1536 | |
1537 | ossl_qrx_inject_urxe(ch->qrx, e); | |
1538 | return; | |
1539 | ||
091f532e HL |
1540 | err: |
1541 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
1542 | "internal error"); | |
b1b06da2 HL |
1543 | undesirable: |
1544 | ossl_quic_demux_release_urxe(ch->demux, e); | |
f538b421 HL |
1545 | } |
1546 | ||
1547 | /* Try to generate packets and if possible, flush them to the network. */ | |
1548 | static int ch_tx(QUIC_CHANNEL *ch) | |
1549 | { | |
1550 | if (ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) { | |
1551 | /* | |
1552 | * While closing, only send CONN_CLOSE if we've received more traffic | |
1553 | * from the peer. Once we tell the TXP to generate CONN_CLOSE, all | |
1554 | * future calls to it generate CONN_CLOSE frames, so otherwise we would | |
1555 | * just constantly generate CONN_CLOSE frames. | |
1556 | */ | |
1557 | if (!ch->conn_close_queued) | |
1558 | return 0; | |
1559 | ||
1560 | ch->conn_close_queued = 0; | |
1561 | } | |
1562 | ||
1563 | /* | |
1564 | * Send a packet, if we need to. Best effort. The TXP consults the CC and | |
1565 | * applies any limitations imposed by it, so we don't need to do it here. | |
1566 | * | |
1567 | * Best effort. In particular if TXP fails for some reason we should still | |
1568 | * flush any queued packets which we already generated. | |
1569 | */ | |
df15e990 HL |
1570 | switch (ossl_quic_tx_packetiser_generate(ch->txp, |
1571 | TX_PACKETISER_ARCHETYPE_NORMAL)) { | |
1572 | case TX_PACKETISER_RES_SENT_PKT: | |
1573 | ch->have_sent_any_pkt = 1; /* Packet was sent */ | |
1574 | break; | |
1575 | case TX_PACKETISER_RES_NO_PKT: | |
1576 | break; /* No packet was sent */ | |
1577 | default: | |
1578 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0, | |
1579 | "internal error"); | |
1580 | break; /* Internal failure (e.g. allocation, assertion) */ | |
1581 | } | |
1582 | ||
1583 | /* Flush packets to network. */ | |
1584 | switch (ossl_qtx_flush_net(ch->qtx)) { | |
1585 | case QTX_FLUSH_NET_RES_OK: | |
1586 | case QTX_FLUSH_NET_RES_TRANSIENT_FAIL: | |
1587 | /* Best effort, done for now. */ | |
1588 | break; | |
1589 | ||
1590 | case QTX_FLUSH_NET_RES_PERMANENT_FAIL: | |
1591 | default: | |
1592 | /* Permanent underlying network BIO, start terminating. */ | |
1593 | ch_raise_net_error(ch); | |
1594 | break; | |
1595 | } | |
f538b421 | 1596 | |
f538b421 HL |
1597 | return 1; |
1598 | } | |
1599 | ||
1600 | /* Determine next tick deadline. */ | |
1601 | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch) | |
1602 | { | |
1603 | OSSL_TIME deadline; | |
1604 | uint32_t pn_space; | |
1605 | ||
149a8e6c | 1606 | if (ossl_quic_channel_is_terminated(ch, NULL)) |
df15e990 HL |
1607 | return ossl_time_infinite(); |
1608 | ||
f538b421 HL |
1609 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); |
1610 | if (ossl_time_is_zero(deadline)) | |
1611 | deadline = ossl_time_infinite(); | |
1612 | ||
1613 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) | |
1614 | deadline = ossl_time_min(deadline, | |
1615 | ossl_ackm_get_ack_deadline(ch->ackm, pn_space)); | |
1616 | ||
1617 | /* When will CC let us send more? */ | |
1618 | if (ossl_quic_tx_packetiser_has_pending(ch->txp, TX_PACKETISER_ARCHETYPE_NORMAL, | |
1619 | TX_PACKETISER_BYPASS_CC)) | |
1620 | deadline = ossl_time_min(deadline, | |
1621 | ch->cc_method->get_next_credit_time(ch->cc_data)); | |
1622 | ||
1623 | /* Is the terminating timer armed? */ | |
149a8e6c | 1624 | if (ossl_quic_channel_is_terminating(ch, NULL)) |
f538b421 HL |
1625 | deadline = ossl_time_min(deadline, |
1626 | ch->terminate_deadline); | |
1627 | else if (!ossl_time_is_infinite(ch->idle_deadline)) | |
1628 | deadline = ossl_time_min(deadline, | |
1629 | ch->idle_deadline); | |
1630 | ||
1631 | return deadline; | |
1632 | } | |
1633 | ||
1634 | /* | |
1635 | * QUIC Channel: Network BIO Configuration | |
1636 | * ======================================= | |
1637 | */ | |
1638 | ||
1639 | /* Determines whether we can support a given poll descriptor. */ | |
1640 | static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d) | |
1641 | { | |
1642 | if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) | |
1643 | return 0; | |
1644 | ||
1645 | return 1; | |
1646 | } | |
1647 | ||
1648 | BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch) | |
1649 | { | |
1650 | return ch->net_rbio; | |
1651 | } | |
1652 | ||
1653 | BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch) | |
1654 | { | |
1655 | return ch->net_wbio; | |
1656 | } | |
1657 | ||
d1ac77b1 HL |
1658 | /* |
1659 | * QUIC_CHANNEL does not ref any BIO it is provided with, nor is any ref | |
1660 | * transferred to it. The caller (i.e., QUIC_CONNECTION) is responsible for | |
1661 | * ensuring the BIO lasts until the channel is freed or the BIO is switched out | |
1662 | * for another BIO by a subsequent successful call to this function. | |
1663 | */ | |
1664 | int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio) | |
f538b421 HL |
1665 | { |
1666 | BIO_POLL_DESCRIPTOR d = {0}; | |
1667 | ||
1668 | if (ch->net_rbio == net_rbio) | |
1669 | return 1; | |
1670 | ||
1671 | if (net_rbio != NULL) { | |
1672 | if (!BIO_get_rpoll_descriptor(net_rbio, &d)) | |
1673 | /* Non-pollable BIO */ | |
1674 | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; | |
1675 | ||
1676 | if (!validate_poll_descriptor(&d)) | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | ossl_quic_reactor_set_poll_r(&ch->rtor, &d); | |
f538b421 HL |
1681 | ossl_quic_demux_set_bio(ch->demux, net_rbio); |
1682 | ch->net_rbio = net_rbio; | |
1683 | return 1; | |
1684 | } | |
1685 | ||
d1ac77b1 | 1686 | int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio) |
f538b421 HL |
1687 | { |
1688 | BIO_POLL_DESCRIPTOR d = {0}; | |
1689 | ||
1690 | if (ch->net_wbio == net_wbio) | |
1691 | return 1; | |
1692 | ||
1693 | if (net_wbio != NULL) { | |
1694 | if (!BIO_get_wpoll_descriptor(net_wbio, &d)) | |
1695 | /* Non-pollable BIO */ | |
1696 | d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE; | |
1697 | ||
1698 | if (!validate_poll_descriptor(&d)) | |
1699 | return 0; | |
1700 | } | |
1701 | ||
1702 | ossl_quic_reactor_set_poll_w(&ch->rtor, &d); | |
f538b421 HL |
1703 | ossl_qtx_set_bio(ch->qtx, net_wbio); |
1704 | ch->net_wbio = net_wbio; | |
1705 | return 1; | |
1706 | } | |
1707 | ||
1708 | /* | |
1709 | * QUIC Channel: Lifecycle Events | |
1710 | * ============================== | |
1711 | */ | |
f538b421 HL |
1712 | int ossl_quic_channel_start(QUIC_CHANNEL *ch) |
1713 | { | |
b1b06da2 HL |
1714 | if (ch->is_server) |
1715 | /* | |
1716 | * This is not used by the server. The server moves to active | |
1717 | * automatically on receiving an incoming connection. | |
1718 | */ | |
1719 | return 0; | |
1720 | ||
f538b421 HL |
1721 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) |
1722 | /* Calls to connect are idempotent */ | |
1723 | return 1; | |
1724 | ||
1725 | /* Inform QTX of peer address. */ | |
1726 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) | |
1727 | return 0; | |
1728 | ||
1729 | /* Plug in secrets for the Initial EL. */ | |
1730 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
1731 | ch->propq, | |
1732 | &ch->init_dcid, | |
091f532e | 1733 | ch->is_server, |
f538b421 HL |
1734 | ch->qrx, ch->qtx)) |
1735 | return 0; | |
1736 | ||
1737 | /* Change state. */ | |
1738 | ch->state = QUIC_CHANNEL_STATE_ACTIVE; | |
1739 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ | |
1740 | ||
1741 | /* Handshake layer: start (e.g. send CH). */ | |
2723d705 | 1742 | if (!ossl_quic_tls_tick(ch->qtls)) |
f538b421 HL |
1743 | return 0; |
1744 | ||
1745 | ossl_quic_reactor_tick(&ch->rtor); /* best effort */ | |
1746 | return 1; | |
1747 | } | |
1748 | ||
1749 | /* Start a locally initiated connection shutdown. */ | |
e8043229 | 1750 | void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code) |
f538b421 HL |
1751 | { |
1752 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1753 | ||
149a8e6c | 1754 | if (ossl_quic_channel_is_term_any(ch, NULL)) |
f538b421 HL |
1755 | return; |
1756 | ||
e8043229 HL |
1757 | tcause.app = 1; |
1758 | tcause.error_code = app_error_code; | |
df15e990 | 1759 | ch_start_terminating(ch, &tcause, 0); |
f538b421 HL |
1760 | } |
1761 | ||
1762 | static void free_token(const unsigned char *buf, size_t buf_len, void *arg) | |
1763 | { | |
1764 | OPENSSL_free((unsigned char *)buf); | |
1765 | } | |
1766 | ||
1767 | /* Called when a server asks us to do a retry. */ | |
1768 | static int ch_retry(QUIC_CHANNEL *ch, | |
1769 | const unsigned char *retry_token, | |
1770 | size_t retry_token_len, | |
1771 | const QUIC_CONN_ID *retry_scid) | |
1772 | { | |
1773 | void *buf; | |
1774 | ||
1775 | /* We change to using the SCID in the Retry packet as the DCID. */ | |
1776 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid)) | |
1777 | return 0; | |
1778 | ||
1779 | /* | |
1780 | * Now we retry. We will release the Retry packet immediately, so copy | |
1781 | * the token. | |
1782 | */ | |
e28f512f | 1783 | if ((buf = OPENSSL_memdup(retry_token, retry_token_len)) == NULL) |
f538b421 HL |
1784 | return 0; |
1785 | ||
f538b421 HL |
1786 | ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, retry_token_len, |
1787 | free_token, NULL); | |
1788 | ||
1789 | ch->retry_scid = *retry_scid; | |
1790 | ch->doing_retry = 1; | |
1791 | ||
1792 | /* | |
1793 | * We need to stimulate the Initial EL to generate the first CRYPTO frame | |
1794 | * again. We can do this most cleanly by simply forcing the ACKM to consider | |
1795 | * the first Initial packet as lost, which it effectively was as the server | |
1796 | * hasn't processed it. This also maintains the desired behaviour with e.g. | |
1797 | * PNs not resetting and so on. | |
1798 | * | |
1799 | * The PN we used initially is always zero, because QUIC does not allow | |
1800 | * repeated retries. | |
1801 | */ | |
1802 | if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL, | |
1803 | /*PN=*/0)) | |
1804 | return 0; | |
1805 | ||
1806 | /* | |
1807 | * Plug in new secrets for the Initial EL. This is the only time we change | |
1808 | * the secrets for an EL after we already provisioned it. | |
1809 | */ | |
1810 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
1811 | ch->propq, | |
1812 | &ch->retry_scid, | |
1813 | /*is_server=*/0, | |
1814 | ch->qrx, ch->qtx)) | |
1815 | return 0; | |
1816 | ||
1817 | return 1; | |
1818 | } | |
1819 | ||
1820 | /* Called when an EL is to be discarded. */ | |
1821 | static int ch_discard_el(QUIC_CHANNEL *ch, | |
1822 | uint32_t enc_level) | |
1823 | { | |
1824 | if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT)) | |
1825 | return 0; | |
1826 | ||
1827 | if ((ch->el_discarded & (1U << enc_level)) != 0) | |
1828 | /* Already done. */ | |
1829 | return 1; | |
1830 | ||
1831 | /* Best effort for all of these. */ | |
1832 | ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level); | |
1833 | ossl_qrx_discard_enc_level(ch->qrx, enc_level); | |
1834 | ossl_qtx_discard_enc_level(ch->qtx, enc_level); | |
1835 | ||
1836 | if (enc_level != QUIC_ENC_LEVEL_0RTT) { | |
1837 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); | |
1838 | ||
1839 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); | |
1840 | ||
1841 | /* We should still have crypto streams at this point. */ | |
79534440 HL |
1842 | if (!ossl_assert(ch->crypto_send[pn_space] != NULL) |
1843 | || !ossl_assert(ch->crypto_recv[pn_space] != NULL)) | |
1844 | return 0; | |
f538b421 HL |
1845 | |
1846 | /* Get rid of the crypto stream state for the EL. */ | |
1847 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); | |
1848 | ch->crypto_send[pn_space] = NULL; | |
1849 | ||
1850 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); | |
1851 | ch->crypto_recv[pn_space] = NULL; | |
1852 | } | |
1853 | ||
1854 | ch->el_discarded |= (1U << enc_level); | |
1855 | return 1; | |
1856 | } | |
1857 | ||
1858 | /* Intended to be called by the RXDP. */ | |
1859 | int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch) | |
1860 | { | |
1861 | if (ch->handshake_confirmed) | |
1862 | return 1; | |
1863 | ||
1864 | if (!ch->handshake_complete) { | |
1865 | /* | |
1866 | * Does not make sense for handshake to be confirmed before it is | |
1867 | * completed. | |
1868 | */ | |
1869 | ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION, | |
1870 | OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, | |
1871 | "handshake cannot be confirmed " | |
1872 | "before it is completed"); | |
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE); | |
1877 | ch->handshake_confirmed = 1; | |
1878 | return 1; | |
1879 | } | |
1880 | ||
1881 | /* | |
1882 | * Master function used when we want to start tearing down a connection: | |
1883 | * | |
1884 | * - If the connection is still IDLE we can go straight to TERMINATED; | |
1885 | * | |
1886 | * - If we are already TERMINATED this is a no-op. | |
1887 | * | |
1888 | * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE | |
9bbc5b54 | 1889 | * from the peer (tcause->remote == 1), we move to TERMINATING - DRAINING. |
f538b421 HL |
1890 | * |
1891 | * - If we are TERMINATING - DRAINING, we remain here until the terminating | |
1892 | * timer expires. | |
1893 | * | |
1894 | * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING. | |
1895 | * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note | |
1896 | * that we are considered to have caused a termination if we sent the first | |
1897 | * CONNECTION_CLOSE frame, even if it is caused by a peer protocol | |
1898 | * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to | |
1899 | * TERMINATING - DRAINING. | |
1900 | * | |
1901 | * We record the termination cause structure passed on the first call only. | |
1902 | * Any successive calls have their termination cause data discarded; | |
1903 | * once we start sending a CONNECTION_CLOSE frame, we don't change the details | |
1904 | * in it. | |
1905 | */ | |
1906 | static void ch_start_terminating(QUIC_CHANNEL *ch, | |
df15e990 HL |
1907 | const QUIC_TERMINATE_CAUSE *tcause, |
1908 | int force_immediate) | |
f538b421 HL |
1909 | { |
1910 | switch (ch->state) { | |
75b2920a HL |
1911 | default: |
1912 | case QUIC_CHANNEL_STATE_IDLE: | |
1913 | ch->terminate_cause = *tcause; | |
1914 | ch_on_terminating_timeout(ch); | |
1915 | break; | |
1916 | ||
1917 | case QUIC_CHANNEL_STATE_ACTIVE: | |
75b2920a | 1918 | ch->terminate_cause = *tcause; |
df15e990 HL |
1919 | |
1920 | if (!force_immediate) { | |
1921 | ch->state = tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING | |
1922 | : QUIC_CHANNEL_STATE_TERMINATING_CLOSING; | |
1923 | ch->terminate_deadline | |
1924 | = ossl_time_add(ossl_time_now(), | |
1925 | ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm), | |
1926 | 3)); | |
1927 | ||
1928 | if (!tcause->remote) { | |
1929 | OSSL_QUIC_FRAME_CONN_CLOSE f = {0}; | |
1930 | ||
1931 | /* best effort */ | |
1932 | f.error_code = ch->terminate_cause.error_code; | |
1933 | f.frame_type = ch->terminate_cause.frame_type; | |
1934 | f.is_app = ch->terminate_cause.app; | |
1935 | ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f); | |
1936 | ch->conn_close_queued = 1; | |
1937 | } | |
1938 | } else { | |
1939 | ch_on_terminating_timeout(ch); | |
75b2920a HL |
1940 | } |
1941 | break; | |
f538b421 | 1942 | |
75b2920a | 1943 | case QUIC_CHANNEL_STATE_TERMINATING_CLOSING: |
df15e990 HL |
1944 | if (force_immediate) |
1945 | ch_on_terminating_timeout(ch); | |
1946 | else if (tcause->remote) | |
75b2920a | 1947 | ch->state = QUIC_CHANNEL_STATE_TERMINATING_DRAINING; |
f538b421 | 1948 | |
75b2920a | 1949 | break; |
f538b421 | 1950 | |
75b2920a | 1951 | case QUIC_CHANNEL_STATE_TERMINATING_DRAINING: |
df15e990 HL |
1952 | /* |
1953 | * Other than in the force-immediate case, we remain here until the | |
1954 | * timout expires. | |
1955 | */ | |
1956 | if (force_immediate) | |
1957 | ch_on_terminating_timeout(ch); | |
1958 | ||
75b2920a | 1959 | break; |
f538b421 | 1960 | |
75b2920a HL |
1961 | case QUIC_CHANNEL_STATE_TERMINATED: |
1962 | /* No-op. */ | |
1963 | break; | |
f538b421 HL |
1964 | } |
1965 | } | |
1966 | ||
1967 | /* For RXDP use. */ | |
1968 | void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch, | |
1969 | OSSL_QUIC_FRAME_CONN_CLOSE *f) | |
1970 | { | |
1971 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1972 | ||
1973 | if (!ossl_quic_channel_is_active(ch)) | |
1974 | return; | |
1975 | ||
1976 | tcause.remote = 1; | |
1977 | tcause.app = f->is_app; | |
1978 | tcause.error_code = f->error_code; | |
1979 | tcause.frame_type = f->frame_type; | |
1980 | ||
df15e990 HL |
1981 | ch_start_terminating(ch, &tcause, 0); |
1982 | } | |
1983 | ||
1984 | static void ch_raise_net_error(QUIC_CHANNEL *ch) | |
1985 | { | |
1986 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
1987 | ||
1988 | tcause.error_code = QUIC_ERR_INTERNAL_ERROR; | |
1989 | ||
1990 | /* | |
1991 | * Skip Terminating state and go directly to Terminated, no point trying to | |
1992 | * send CONNECTION_CLOSE if we cannot communicate. | |
1993 | */ | |
1994 | ch_start_terminating(ch, &tcause, 1); | |
f538b421 HL |
1995 | } |
1996 | ||
1997 | void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch, | |
1998 | uint64_t error_code, | |
1999 | uint64_t frame_type, | |
2000 | const char *reason) | |
2001 | { | |
2002 | QUIC_TERMINATE_CAUSE tcause = {0}; | |
2003 | ||
2004 | tcause.error_code = error_code; | |
2005 | tcause.frame_type = frame_type; | |
2006 | ||
df15e990 | 2007 | ch_start_terminating(ch, &tcause, 0); |
f538b421 HL |
2008 | } |
2009 | ||
2010 | /* | |
2011 | * Called once the terminating timer expires, meaning we move from TERMINATING | |
2012 | * to TERMINATED. | |
2013 | */ | |
2014 | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch) | |
2015 | { | |
2016 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
2017 | } | |
2018 | ||
2019 | /* | |
2020 | * Updates our idle deadline. Called when an event happens which should bump the | |
2021 | * idle timeout. | |
2022 | */ | |
2023 | static void ch_update_idle(QUIC_CHANNEL *ch) | |
2024 | { | |
2025 | if (ch->max_idle_timeout == 0) | |
2026 | ch->idle_deadline = ossl_time_infinite(); | |
2027 | else | |
2028 | ch->idle_deadline = ossl_time_add(ossl_time_now(), | |
2029 | ossl_ms2time(ch->max_idle_timeout)); | |
2030 | } | |
2031 | ||
2032 | /* Called when the idle timeout expires. */ | |
2033 | static void ch_on_idle_timeout(QUIC_CHANNEL *ch) | |
2034 | { | |
2035 | /* | |
2036 | * Idle timeout does not have an error code associated with it because a | |
2037 | * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach | |
2038 | * TERMINATED anyway. | |
2039 | */ | |
2040 | ch->terminate_cause.app = 0; | |
2041 | ch->terminate_cause.error_code = UINT64_MAX; | |
2042 | ch->terminate_cause.frame_type = 0; | |
2043 | ||
2044 | ch->state = QUIC_CHANNEL_STATE_TERMINATED; | |
2045 | } | |
b1b06da2 HL |
2046 | |
2047 | /* Called when we, as a server, get a new incoming connection. */ | |
2048 | static int ch_server_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer, | |
2049 | const QUIC_CONN_ID *peer_scid, | |
2050 | const QUIC_CONN_ID *peer_dcid) | |
2051 | { | |
2052 | if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server)) | |
2053 | return 0; | |
2054 | ||
2055 | /* Generate a SCID we will use for the connection. */ | |
2056 | if (!gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, | |
2057 | &ch->cur_local_dcid)) | |
2058 | return 0; | |
2059 | ||
2060 | /* Note our newly learnt peer address and CIDs. */ | |
2061 | ch->cur_peer_addr = *peer; | |
2062 | ch->init_dcid = *peer_dcid; | |
2063 | ch->cur_remote_dcid = *peer_scid; | |
2064 | ||
2065 | /* Inform QTX of peer address. */ | |
2066 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) | |
2067 | return 0; | |
2068 | ||
2069 | /* Inform TXP of desired CIDs. */ | |
2070 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid)) | |
2071 | return 0; | |
2072 | ||
2073 | if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_dcid)) | |
2074 | return 0; | |
2075 | ||
2076 | /* Plug in secrets for the Initial EL. */ | |
2077 | if (!ossl_quic_provide_initial_secret(ch->libctx, | |
2078 | ch->propq, | |
2079 | &ch->init_dcid, | |
2080 | /*is_server=*/1, | |
2081 | ch->qrx, ch->qtx)) | |
2082 | return 0; | |
2083 | ||
2084 | /* Register our local DCID in the DEMUX. */ | |
2085 | if (!ossl_qrx_add_dst_conn_id(ch->qrx, &ch->cur_local_dcid)) | |
2086 | return 0; | |
2087 | ||
2088 | /* Change state. */ | |
2089 | ch->state = QUIC_CHANNEL_STATE_ACTIVE; | |
2090 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ | |
2091 | return 1; | |
2092 | } | |
d03fe5de MC |
2093 | |
2094 | SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch) | |
2095 | { | |
2096 | return ch->tls; | |
2097 | } |