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