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