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