/src/openssl/ssl/quic/quic_channel.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022-2026 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 <openssl/rand.h> |
11 | | #include <openssl/err.h> |
12 | | #include "internal/ssl_unwrap.h" |
13 | | #include "internal/quic_channel.h" |
14 | | #include "internal/quic_error.h" |
15 | | #include "internal/quic_rx_depack.h" |
16 | | #include "internal/quic_lcidm.h" |
17 | | #include "internal/quic_srtm.h" |
18 | | #include "internal/qlog_event_helpers.h" |
19 | | #include "internal/quic_txp.h" |
20 | | #include "internal/quic_tls.h" |
21 | | #include "internal/quic_ssl.h" |
22 | | #include "../ssl_local.h" |
23 | | #include "quic_channel_local.h" |
24 | | #include "quic_port_local.h" |
25 | | #include "quic_engine_local.h" |
26 | | |
27 | 0 | #define INIT_CRYPTO_RECV_BUF_LEN 16384 |
28 | 0 | #define INIT_CRYPTO_SEND_BUF_LEN 16384 |
29 | 0 | #define INIT_APP_BUF_LEN 8192 |
30 | | |
31 | | /* |
32 | | * Interval before we force a PING to ensure NATs don't timeout. This is based |
33 | | * on the lowest commonly seen value of 30 seconds as cited in RFC 9000 s. |
34 | | * 10.1.2. |
35 | | */ |
36 | 0 | #define MAX_NAT_INTERVAL (ossl_ms2time(25000)) |
37 | | |
38 | 0 | DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL); Unexecuted instantiation: quic_channel.c:ossl_list_ch_insert_tail Unexecuted instantiation: quic_channel.c:ossl_list_ch_remove |
39 | | |
40 | | static void ch_save_err_state(QUIC_CHANNEL *ch); |
41 | | static int ch_rx(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads); |
42 | | static int ch_tx(QUIC_CHANNEL *ch, int *notify_other_threads); |
43 | | static int ch_tick_tls(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads); |
44 | | static void ch_rx_handle_packet(QUIC_CHANNEL *ch, int channel_only); |
45 | | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch); |
46 | | static int ch_retry(QUIC_CHANNEL *ch, |
47 | | const unsigned char *retry_token, |
48 | | size_t retry_token_len, |
49 | | const QUIC_CONN_ID *retry_scid); |
50 | | static int ch_restart(QUIC_CHANNEL *ch); |
51 | | |
52 | | static void ch_cleanup(QUIC_CHANNEL *ch); |
53 | | static int ch_generate_transport_params(QUIC_CHANNEL *ch); |
54 | | static int ch_on_transport_params(const unsigned char *params, |
55 | | size_t params_len, |
56 | | void *arg); |
57 | | static int ch_on_handshake_alert(void *arg, unsigned char alert_code); |
58 | | static int ch_on_handshake_complete(void *arg); |
59 | | static int ch_on_handshake_yield_secret(uint32_t prot_level, int direction, |
60 | | uint32_t suite_id, EVP_MD *md, |
61 | | const unsigned char *secret, |
62 | | size_t secret_len, |
63 | | void *arg); |
64 | | static int ch_on_crypto_recv_record(const unsigned char **buf, |
65 | | size_t *bytes_read, void *arg); |
66 | | static int ch_on_crypto_release_record(size_t bytes_read, void *arg); |
67 | | static int crypto_ensure_empty(QUIC_RSTREAM *rstream); |
68 | | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, |
69 | | size_t *consumed, void *arg); |
70 | | static OSSL_TIME get_time(void *arg); |
71 | | static uint64_t get_stream_limit(int uni, void *arg); |
72 | | static int rx_late_validate(QUIC_PN pn, int pn_space, void *arg); |
73 | | static void rxku_detected(QUIC_PN pn, void *arg); |
74 | | static int ch_retry(QUIC_CHANNEL *ch, |
75 | | const unsigned char *retry_token, |
76 | | size_t retry_token_len, |
77 | | const QUIC_CONN_ID *retry_scid); |
78 | | static void ch_update_idle(QUIC_CHANNEL *ch); |
79 | | static int ch_discard_el(QUIC_CHANNEL *ch, |
80 | | uint32_t enc_level); |
81 | | static void ch_on_idle_timeout(QUIC_CHANNEL *ch); |
82 | | static void ch_update_idle(QUIC_CHANNEL *ch); |
83 | | static void ch_update_ping_deadline(QUIC_CHANNEL *ch); |
84 | | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch); |
85 | | static void ch_start_terminating(QUIC_CHANNEL *ch, |
86 | | const QUIC_TERMINATE_CAUSE *tcause, |
87 | | int force_immediate); |
88 | | static void ch_on_txp_ack_tx(const OSSL_QUIC_FRAME_ACK *ack, uint32_t pn_space, |
89 | | void *arg); |
90 | | static void ch_record_state_transition(QUIC_CHANNEL *ch, uint32_t new_state); |
91 | | |
92 | | typedef struct cfq_data_retire_cid { |
93 | | uint64_t rtcid_seq; |
94 | | QUIC_CHANNEL *rtcid_ch; |
95 | | } CFQ_DATA_RETIRE_CID_T; |
96 | | |
97 | | QUIC_NEEDS_LOCK |
98 | | static QLOG *ch_get_qlog(QUIC_CHANNEL *ch) |
99 | 0 | { |
100 | 0 | #ifndef OPENSSL_NO_QLOG |
101 | 0 | QLOG_TRACE_INFO qti = { 0 }; |
102 | |
|
103 | 0 | if (ch->qlog != NULL) |
104 | 0 | return ch->qlog; |
105 | | |
106 | 0 | if (!ch->use_qlog) |
107 | 0 | return NULL; |
108 | | |
109 | 0 | if (ch->is_server && ch->init_dcid.id_len == 0) |
110 | 0 | return NULL; |
111 | | |
112 | 0 | qti.odcid = ch->init_dcid; |
113 | 0 | qti.title = ch->qlog_title; |
114 | 0 | qti.description = NULL; |
115 | 0 | qti.group_id = NULL; |
116 | 0 | qti.is_server = ch->is_server; |
117 | 0 | qti.now_cb = get_time; |
118 | 0 | qti.now_cb_arg = ch; |
119 | 0 | if ((ch->qlog = ossl_qlog_new_from_env(&qti)) == NULL) { |
120 | 0 | ch->use_qlog = 0; /* don't try again */ |
121 | 0 | return NULL; |
122 | 0 | } |
123 | | |
124 | 0 | return ch->qlog; |
125 | | #else |
126 | | return NULL; |
127 | | #endif |
128 | 0 | } |
129 | | |
130 | | QUIC_NEEDS_LOCK |
131 | | static QLOG *ch_get_qlog_cb(void *arg) |
132 | 0 | { |
133 | 0 | QUIC_CHANNEL *ch = arg; |
134 | |
|
135 | 0 | return ch_get_qlog(ch); |
136 | 0 | } |
137 | | |
138 | | /* |
139 | | * QUIC Channel Initialization and Teardown |
140 | | * ======================================== |
141 | | */ |
142 | 0 | #define DEFAULT_CONN_RXFC_MAX_WND_MUL 20 |
143 | | |
144 | 0 | #define DEFAULT_STREAM_RXFC_MAX_WND_MUL 12 |
145 | | |
146 | | static int ch_init(QUIC_CHANNEL *ch) |
147 | 0 | { |
148 | 0 | OSSL_QUIC_TX_PACKETISER_ARGS txp_args = { 0 }; |
149 | 0 | OSSL_QTX_ARGS qtx_args = { 0 }; |
150 | 0 | OSSL_QRX_ARGS qrx_args = { 0 }; |
151 | 0 | QUIC_TLS_ARGS tls_args = { 0 }; |
152 | 0 | uint32_t pn_space; |
153 | 0 | size_t rx_short_dcid_len; |
154 | 0 | size_t tx_init_dcid_len; |
155 | |
|
156 | 0 | if (ch->port == NULL || ch->lcidm == NULL || ch->srtm == NULL) |
157 | 0 | goto err; |
158 | | |
159 | 0 | rx_short_dcid_len = ossl_quic_port_get_rx_short_dcid_len(ch->port); |
160 | 0 | tx_init_dcid_len = ossl_quic_port_get_tx_init_dcid_len(ch->port); |
161 | | |
162 | | /* For clients, generate our initial DCID. */ |
163 | 0 | if (!ch->is_server |
164 | 0 | && !ossl_quic_gen_rand_conn_id(ch->port->engine->libctx, tx_init_dcid_len, |
165 | 0 | &ch->init_dcid)) |
166 | 0 | goto err; |
167 | | |
168 | | /* We plug in a network write BIO to the QTX later when we get one. */ |
169 | 0 | qtx_args.libctx = ch->port->engine->libctx; |
170 | 0 | qtx_args.get_qlog_cb = ch_get_qlog_cb; |
171 | 0 | qtx_args.get_qlog_cb_arg = ch; |
172 | 0 | qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN; |
173 | 0 | ch->rx_max_udp_payload_size = QUIC_DEFAULT_MAX_UDP_PAYLOAD_SIZE; |
174 | |
|
175 | 0 | ch->ping_deadline = ossl_time_infinite(); |
176 | |
|
177 | 0 | ch->qtx = ossl_qtx_new(&qtx_args); |
178 | 0 | if (ch->qtx == NULL) |
179 | 0 | goto err; |
180 | | |
181 | 0 | ch->txpim = ossl_quic_txpim_new(); |
182 | 0 | if (ch->txpim == NULL) |
183 | 0 | goto err; |
184 | | |
185 | 0 | ch->cfq = ossl_quic_cfq_new(); |
186 | 0 | if (ch->cfq == NULL) |
187 | 0 | goto err; |
188 | | |
189 | 0 | if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL)) |
190 | 0 | goto err; |
191 | | |
192 | 0 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) |
193 | 0 | if (!ossl_quic_rxfc_init_standalone(&ch->crypto_rxfc[pn_space], |
194 | 0 | INIT_CRYPTO_RECV_BUF_LEN, |
195 | 0 | get_time, ch)) |
196 | 0 | goto err; |
197 | | |
198 | 0 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_bidi_rxfc, |
199 | 0 | ch->tx_init_max_streams_bidi, |
200 | 0 | get_time, ch)) |
201 | 0 | goto err; |
202 | | |
203 | 0 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_uni_rxfc, |
204 | 0 | ch->tx_init_max_streams_uni, |
205 | 0 | get_time, ch)) |
206 | 0 | goto err; |
207 | | |
208 | 0 | if (!ossl_statm_init(&ch->statm)) |
209 | 0 | goto err; |
210 | | |
211 | 0 | ch->have_statm = 1; |
212 | 0 | ch->cc_method = &ossl_cc_newreno_method; |
213 | 0 | if ((ch->cc_data = ch->cc_method->new(get_time, ch)) == NULL) |
214 | 0 | goto err; |
215 | | |
216 | 0 | if ((ch->ackm = ossl_ackm_new(get_time, ch, &ch->statm, |
217 | 0 | ch->cc_method, ch->cc_data, |
218 | 0 | ch->is_server)) |
219 | 0 | == NULL) |
220 | 0 | goto err; |
221 | | |
222 | 0 | if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch, |
223 | 0 | &ch->max_streams_bidi_rxfc, |
224 | 0 | &ch->max_streams_uni_rxfc, |
225 | 0 | ch)) |
226 | 0 | goto err; |
227 | | |
228 | 0 | ch->have_qsm = 1; |
229 | |
|
230 | 0 | if (!ch->is_server |
231 | 0 | && !ossl_quic_lcidm_generate_initial(ch->lcidm, ch, &ch->init_scid)) |
232 | 0 | goto err; |
233 | | |
234 | 0 | ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP; |
235 | |
|
236 | 0 | txp_args.cur_scid = ch->init_scid; |
237 | 0 | txp_args.cur_dcid = ch->init_dcid; |
238 | 0 | txp_args.ack_delay_exponent = ch->tx_ack_delay_exp; |
239 | 0 | txp_args.qtx = ch->qtx; |
240 | 0 | txp_args.txpim = ch->txpim; |
241 | 0 | txp_args.cfq = ch->cfq; |
242 | 0 | txp_args.ackm = ch->ackm; |
243 | 0 | txp_args.qsm = &ch->qsm; |
244 | 0 | txp_args.conn_txfc = &ch->conn_txfc; |
245 | 0 | txp_args.conn_rxfc = &ch->conn_rxfc; |
246 | 0 | txp_args.max_streams_bidi_rxfc = &ch->max_streams_bidi_rxfc; |
247 | 0 | txp_args.max_streams_uni_rxfc = &ch->max_streams_uni_rxfc; |
248 | 0 | txp_args.cc_method = ch->cc_method; |
249 | 0 | txp_args.cc_data = ch->cc_data; |
250 | 0 | txp_args.now = get_time; |
251 | 0 | txp_args.now_arg = ch; |
252 | 0 | txp_args.get_qlog_cb = ch_get_qlog_cb; |
253 | 0 | txp_args.get_qlog_cb_arg = ch; |
254 | 0 | txp_args.protocol_version = QUIC_VERSION_1; |
255 | |
|
256 | 0 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { |
257 | 0 | ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_SEND_BUF_LEN); |
258 | 0 | if (ch->crypto_send[pn_space] == NULL) |
259 | 0 | goto err; |
260 | | |
261 | 0 | txp_args.crypto[pn_space] = ch->crypto_send[pn_space]; |
262 | 0 | } |
263 | | |
264 | 0 | ch->txp = ossl_quic_tx_packetiser_new(&txp_args); |
265 | 0 | if (ch->txp == NULL) |
266 | 0 | goto err; |
267 | | |
268 | | /* clients have no amplification limit, so are considered always valid */ |
269 | 0 | if (!ch->is_server) |
270 | 0 | ossl_quic_tx_packetiser_set_validated(ch->txp); |
271 | |
|
272 | 0 | ossl_quic_tx_packetiser_set_ack_tx_cb(ch->txp, ch_on_txp_ack_tx, ch); |
273 | | |
274 | | /* |
275 | | * qrx does not exist yet, then we must be dealing with client channel |
276 | | * (QUIC connection initiator). |
277 | | * If qrx exists already, then we are dealing with server channel which |
278 | | * qrx gets created by port_default_packet_handler() before |
279 | | * port_default_packet_handler() accepts connection and creates channel |
280 | | * for it. |
281 | | * The exception here is tserver which always creates channel, |
282 | | * before the first packet is ever seen. |
283 | | */ |
284 | 0 | if (ch->qrx == NULL && ch->is_tserver_ch == 0) { |
285 | | /* we are regular client, create channel */ |
286 | 0 | qrx_args.libctx = ch->port->engine->libctx; |
287 | 0 | qrx_args.demux = ch->port->demux; |
288 | 0 | qrx_args.short_conn_id_len = rx_short_dcid_len; |
289 | 0 | qrx_args.max_deferred = 32; |
290 | |
|
291 | 0 | if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL) |
292 | 0 | goto err; |
293 | 0 | } |
294 | | |
295 | 0 | if (ch->qrx != NULL) { |
296 | | /* |
297 | | * callbacks for channels associated with tserver's port |
298 | | * are set up later when we call ossl_quic_channel_bind_qrx() |
299 | | * in port_default_packet_handler() |
300 | | */ |
301 | 0 | if (!ossl_qrx_set_late_validation_cb(ch->qrx, |
302 | 0 | rx_late_validate, |
303 | 0 | ch)) |
304 | 0 | goto err; |
305 | | |
306 | 0 | if (!ossl_qrx_set_key_update_cb(ch->qrx, |
307 | 0 | rxku_detected, |
308 | 0 | ch)) |
309 | 0 | goto err; |
310 | 0 | } |
311 | | |
312 | 0 | ch->rsqp = ossl_quic_rstream_qparm_new(ch); |
313 | 0 | if (ch->rsqp == NULL) |
314 | 0 | goto err; |
315 | | |
316 | 0 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { |
317 | 0 | ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL, ch->rsqp); |
318 | 0 | if (ch->crypto_recv[pn_space] == NULL) |
319 | 0 | goto err; |
320 | 0 | } |
321 | | |
322 | | /* Plug in the TLS handshake layer. */ |
323 | 0 | tls_args.s = ch->tls; |
324 | 0 | tls_args.crypto_send_cb = ch_on_crypto_send; |
325 | 0 | tls_args.crypto_send_cb_arg = ch; |
326 | 0 | tls_args.crypto_recv_rcd_cb = ch_on_crypto_recv_record; |
327 | 0 | tls_args.crypto_recv_rcd_cb_arg = ch; |
328 | 0 | tls_args.crypto_release_rcd_cb = ch_on_crypto_release_record; |
329 | 0 | tls_args.crypto_release_rcd_cb_arg = ch; |
330 | 0 | tls_args.yield_secret_cb = ch_on_handshake_yield_secret; |
331 | 0 | tls_args.yield_secret_cb_arg = ch; |
332 | 0 | tls_args.got_transport_params_cb = ch_on_transport_params; |
333 | 0 | tls_args.got_transport_params_cb_arg = ch; |
334 | 0 | tls_args.handshake_complete_cb = ch_on_handshake_complete; |
335 | 0 | tls_args.handshake_complete_cb_arg = ch; |
336 | 0 | tls_args.alert_cb = ch_on_handshake_alert; |
337 | 0 | tls_args.alert_cb_arg = ch; |
338 | 0 | tls_args.is_server = ch->is_server; |
339 | 0 | tls_args.ossl_quic = 1; |
340 | |
|
341 | 0 | if ((ch->qtls = ossl_quic_tls_new(&tls_args)) == NULL) |
342 | 0 | goto err; |
343 | | |
344 | 0 | ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY; |
345 | 0 | ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT; |
346 | 0 | ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL; |
347 | 0 | ch->rx_enc_level = QUIC_ENC_LEVEL_INITIAL; |
348 | 0 | ch->txku_threshold_override = UINT64_MAX; |
349 | |
|
350 | 0 | ch->max_idle_timeout = ch->max_idle_timeout_local_req; |
351 | |
|
352 | 0 | ossl_ackm_set_tx_max_ack_delay(ch->ackm, ossl_ms2time(ch->tx_max_ack_delay)); |
353 | 0 | ossl_ackm_set_rx_max_ack_delay(ch->ackm, ossl_ms2time(ch->rx_max_ack_delay)); |
354 | |
|
355 | 0 | ch_update_idle(ch); |
356 | 0 | ossl_list_ch_insert_tail(&ch->port->channel_list, ch); |
357 | 0 | ch->on_port_list = 1; |
358 | 0 | return 1; |
359 | | |
360 | 0 | err: |
361 | 0 | ch_cleanup(ch); |
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | | /* |
366 | | * ch_cleanup() is idempotent: every owned pointer is NULL'd after its free, |
367 | | * and every "have_*" flag is reset after its destructor runs. Calling this |
368 | | * twice on the same channel is safe. |
369 | | */ |
370 | | static void ch_cleanup(QUIC_CHANNEL *ch) |
371 | 0 | { |
372 | 0 | uint32_t pn_space; |
373 | |
|
374 | 0 | if (ch->ackm != NULL) |
375 | 0 | for (pn_space = QUIC_PN_SPACE_INITIAL; |
376 | 0 | pn_space < QUIC_PN_SPACE_NUM; |
377 | 0 | ++pn_space) |
378 | 0 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); |
379 | |
|
380 | 0 | if (ch->lcidm != NULL) |
381 | 0 | ossl_quic_lcidm_cull(ch->lcidm, ch); |
382 | |
|
383 | 0 | if (ch->srtm != NULL) |
384 | 0 | ossl_quic_srtm_cull(ch->srtm, ch); |
385 | |
|
386 | 0 | ossl_quic_tx_packetiser_free(ch->txp); |
387 | 0 | ch->txp = NULL; |
388 | 0 | ossl_quic_txpim_free(ch->txpim); |
389 | 0 | ch->txpim = NULL; |
390 | 0 | ossl_quic_cfq_free(ch->cfq); |
391 | 0 | ch->cfq = NULL; |
392 | 0 | ossl_qtx_free(ch->qtx); |
393 | 0 | ch->qtx = NULL; |
394 | 0 | if (ch->cc_data != NULL) { |
395 | 0 | ch->cc_method->free(ch->cc_data); |
396 | 0 | ch->cc_data = NULL; |
397 | 0 | } |
398 | 0 | if (ch->have_statm) { |
399 | 0 | ossl_statm_destroy(&ch->statm); |
400 | 0 | ch->have_statm = 0; |
401 | 0 | } |
402 | 0 | ossl_ackm_free(ch->ackm); |
403 | 0 | ch->ackm = NULL; |
404 | |
|
405 | 0 | if (ch->have_qsm) { |
406 | 0 | ossl_quic_stream_map_cleanup(&ch->qsm); |
407 | 0 | ch->have_qsm = 0; |
408 | 0 | } |
409 | |
|
410 | 0 | for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) { |
411 | 0 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); |
412 | 0 | ch->crypto_send[pn_space] = NULL; |
413 | 0 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); |
414 | 0 | ch->crypto_recv[pn_space] = NULL; |
415 | 0 | } |
416 | |
|
417 | 0 | ossl_quic_rstream_qparm_destroy(ch->rsqp); |
418 | 0 | ch->rsqp = NULL; |
419 | |
|
420 | 0 | ossl_qrx_pkt_release(ch->qrx_pkt); |
421 | 0 | ch->qrx_pkt = NULL; |
422 | |
|
423 | 0 | ossl_quic_tls_free(ch->qtls); |
424 | 0 | ch->qtls = NULL; |
425 | 0 | ossl_qrx_free(ch->qrx); |
426 | 0 | ch->qrx = NULL; |
427 | 0 | OPENSSL_free(ch->local_transport_params); |
428 | 0 | ch->local_transport_params = NULL; |
429 | 0 | OPENSSL_free((char *)ch->terminate_cause.reason); |
430 | 0 | ch->terminate_cause.reason = NULL; |
431 | 0 | OSSL_ERR_STATE_free(ch->err_state); |
432 | 0 | ch->err_state = NULL; |
433 | 0 | OPENSSL_free(ch->ack_range_scratch); |
434 | 0 | ch->ack_range_scratch = NULL; |
435 | 0 | OPENSSL_free(ch->pending_new_token); |
436 | 0 | ch->pending_new_token = NULL; |
437 | |
|
438 | 0 | if (ch->on_port_list) { |
439 | 0 | ossl_list_ch_remove(&ch->port->channel_list, ch); |
440 | 0 | ch->on_port_list = 0; |
441 | 0 | } |
442 | |
|
443 | 0 | #ifndef OPENSSL_NO_QLOG |
444 | 0 | if (ch->qlog != NULL) |
445 | 0 | ossl_qlog_flush(ch->qlog); /* best effort */ |
446 | |
|
447 | 0 | OPENSSL_free(ch->qlog_title); |
448 | 0 | ch->qlog_title = NULL; |
449 | 0 | ossl_qlog_free(ch->qlog); |
450 | 0 | ch->qlog = NULL; |
451 | 0 | #endif |
452 | 0 | } |
453 | | |
454 | | int ossl_quic_channel_init(QUIC_CHANNEL *ch) |
455 | 0 | { |
456 | 0 | return ch_init(ch); |
457 | 0 | } |
458 | | |
459 | | void ossl_quic_channel_bind_qrx(QUIC_CHANNEL *tserver_ch, OSSL_QRX *qrx) |
460 | 0 | { |
461 | 0 | if (tserver_ch->qrx == NULL && tserver_ch->is_tserver_ch == 1) { |
462 | 0 | tserver_ch->qrx = qrx; |
463 | 0 | ossl_qrx_set_late_validation_cb(tserver_ch->qrx, rx_late_validate, |
464 | 0 | tserver_ch); |
465 | 0 | ossl_qrx_set_key_update_cb(tserver_ch->qrx, rxku_detected, |
466 | 0 | tserver_ch); |
467 | 0 | } |
468 | 0 | } |
469 | | |
470 | | QUIC_CHANNEL *ossl_quic_channel_alloc(const QUIC_CHANNEL_ARGS *args) |
471 | 0 | { |
472 | 0 | QUIC_CHANNEL *ch; |
473 | |
|
474 | 0 | if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL) |
475 | 0 | return NULL; |
476 | | |
477 | 0 | ch->port = args->port; |
478 | 0 | ch->is_server = args->is_server; |
479 | 0 | ch->tls = args->tls; |
480 | 0 | ch->lcidm = args->lcidm; |
481 | 0 | ch->srtm = args->srtm; |
482 | 0 | ch->qrx = args->qrx; |
483 | 0 | ch->is_tserver_ch = args->is_tserver_ch; |
484 | 0 | #ifndef OPENSSL_NO_QLOG |
485 | 0 | ch->use_qlog = args->use_qlog; |
486 | |
|
487 | 0 | if (ch->use_qlog && args->qlog_title != NULL) { |
488 | 0 | if ((ch->qlog_title = OPENSSL_strdup(args->qlog_title)) == NULL) |
489 | 0 | goto err; |
490 | 0 | } |
491 | 0 | #endif |
492 | | |
493 | 0 | ch->max_idle_timeout_local_req = args->max_idle_timeout; |
494 | 0 | ch->tx_max_udp_payload_size = args->max_udp_payload_size; |
495 | 0 | ch->tx_init_max_data = args->init_max_data; |
496 | 0 | ch->tx_init_max_stream_data_bidi_local = args->init_max_stream_data_bidi_local; |
497 | 0 | ch->tx_init_max_stream_data_bidi_remote = args->init_max_stream_data_bidi_remote; |
498 | 0 | ch->tx_init_max_stream_data_uni = args->init_max_stream_data_uni; |
499 | 0 | ch->tx_init_max_streams_bidi = args->init_max_streams_bidi; |
500 | 0 | ch->tx_init_max_streams_uni = args->init_max_streams_uni; |
501 | 0 | ch->tx_ack_delay_exp = args->ack_delay_exponent; |
502 | 0 | ch->tx_max_ack_delay = args->max_ack_delay; |
503 | 0 | ch->tx_disable_active_migration = args->disable_active_migration; |
504 | 0 | ch->tx_active_conn_id_limit = args->active_conn_id_limit; |
505 | |
|
506 | 0 | if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL, |
507 | 0 | ch->tx_init_max_data, |
508 | 0 | DEFAULT_CONN_RXFC_MAX_WND_MUL * ch->tx_init_max_data, |
509 | 0 | get_time, ch)) { |
510 | 0 | goto err; |
511 | 0 | } |
512 | | |
513 | 0 | return ch; |
514 | | |
515 | 0 | err: |
516 | 0 | #ifndef OPENSSL_NO_QLOG |
517 | 0 | OPENSSL_free(ch->qlog_title); |
518 | 0 | #endif |
519 | 0 | OPENSSL_free(ch); |
520 | 0 | return NULL; |
521 | 0 | } |
522 | | |
523 | | void ossl_quic_channel_free(QUIC_CHANNEL *ch) |
524 | 0 | { |
525 | 0 | if (ch == NULL) |
526 | 0 | return; |
527 | | |
528 | 0 | ch_cleanup(ch); |
529 | 0 | OPENSSL_free(ch); |
530 | 0 | } |
531 | | |
532 | | /* Set mutator callbacks for test framework support */ |
533 | | int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch, |
534 | | ossl_mutate_packet_cb mutatecb, |
535 | | ossl_finish_mutate_cb finishmutatecb, |
536 | | void *mutatearg) |
537 | 0 | { |
538 | 0 | if (ch->qtx == NULL) |
539 | 0 | return 0; |
540 | | |
541 | 0 | ossl_qtx_set_mutator(ch->qtx, mutatecb, finishmutatecb, mutatearg); |
542 | 0 | return 1; |
543 | 0 | } |
544 | | |
545 | | int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr) |
546 | 0 | { |
547 | 0 | if (!ch->addressed_mode) |
548 | 0 | return 0; |
549 | | |
550 | 0 | return BIO_ADDR_copy(peer_addr, &ch->cur_peer_addr); |
551 | 0 | } |
552 | | |
553 | | int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr) |
554 | 0 | { |
555 | 0 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) |
556 | 0 | return 0; |
557 | | |
558 | 0 | if (peer_addr == NULL || BIO_ADDR_family(peer_addr) == AF_UNSPEC) { |
559 | 0 | BIO_ADDR_clear(&ch->cur_peer_addr); |
560 | 0 | ch->addressed_mode = 0; |
561 | 0 | return 1; |
562 | 0 | } |
563 | | |
564 | 0 | if (!BIO_ADDR_copy(&ch->cur_peer_addr, peer_addr)) { |
565 | 0 | ch->addressed_mode = 0; |
566 | 0 | return 0; |
567 | 0 | } |
568 | 0 | ch->addressed_mode = 1; |
569 | |
|
570 | 0 | return 1; |
571 | 0 | } |
572 | | |
573 | | QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch) |
574 | 0 | { |
575 | 0 | return ossl_quic_port_get0_reactor(ch->port); |
576 | 0 | } |
577 | | |
578 | | QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch) |
579 | 0 | { |
580 | 0 | return &ch->qsm; |
581 | 0 | } |
582 | | |
583 | | OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch) |
584 | 0 | { |
585 | 0 | return &ch->statm; |
586 | 0 | } |
587 | | |
588 | | SSL *ossl_quic_channel_get0_tls(QUIC_CHANNEL *ch) |
589 | 0 | { |
590 | 0 | return ch->tls; |
591 | 0 | } |
592 | | |
593 | | int ossl_quic_channel_set0_tls(QUIC_CHANNEL *ch, SSL *ssl) |
594 | 0 | { |
595 | | /* |
596 | | * Rebind the handshake layer first, so that a failure leaves the channel |
597 | | * unmodified. |
598 | | */ |
599 | 0 | if (!ossl_assert(ch != NULL && ssl != NULL && ch->tls == NULL) |
600 | 0 | || !ossl_quic_tls_set0_ssl(ch->qtls, ssl)) |
601 | 0 | return 0; |
602 | | |
603 | 0 | ch->tls = ssl; |
604 | 0 | return 1; |
605 | 0 | } |
606 | | |
607 | | static void free_buf_mem(unsigned char *buf, size_t buf_len, void *arg) |
608 | 0 | { |
609 | 0 | BUF_MEM_free((BUF_MEM *)arg); |
610 | 0 | } |
611 | | |
612 | | int ossl_quic_channel_schedule_new_token(QUIC_CHANNEL *ch, |
613 | | const unsigned char *token, |
614 | | size_t token_len) |
615 | 0 | { |
616 | 0 | int rc = 0; |
617 | 0 | QUIC_CFQ_ITEM *cfq_item; |
618 | 0 | WPACKET wpkt; |
619 | 0 | BUF_MEM *buf_mem = NULL; |
620 | 0 | size_t l = 0; |
621 | |
|
622 | 0 | buf_mem = BUF_MEM_new(); |
623 | 0 | if (buf_mem == NULL) |
624 | 0 | goto err; |
625 | | |
626 | 0 | if (!WPACKET_init(&wpkt, buf_mem)) |
627 | 0 | goto err; |
628 | | |
629 | 0 | if (!ossl_quic_wire_encode_frame_new_token(&wpkt, token, |
630 | 0 | token_len)) { |
631 | 0 | WPACKET_cleanup(&wpkt); |
632 | 0 | goto err; |
633 | 0 | } |
634 | | |
635 | 0 | WPACKET_finish(&wpkt); |
636 | |
|
637 | 0 | if (!WPACKET_get_total_written(&wpkt, &l)) |
638 | 0 | goto err; |
639 | | |
640 | 0 | cfq_item = ossl_quic_cfq_add_frame(ch->cfq, 1, |
641 | 0 | QUIC_PN_SPACE_APP, |
642 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, 0, |
643 | 0 | (unsigned char *)buf_mem->data, l, |
644 | 0 | free_buf_mem, |
645 | 0 | buf_mem); |
646 | 0 | if (cfq_item == NULL) |
647 | 0 | goto err; |
648 | | |
649 | 0 | rc = 1; |
650 | 0 | err: |
651 | 0 | if (!rc) |
652 | 0 | BUF_MEM_free(buf_mem); |
653 | 0 | return rc; |
654 | 0 | } |
655 | | |
656 | | size_t ossl_quic_channel_get_short_header_conn_id_len(QUIC_CHANNEL *ch) |
657 | 0 | { |
658 | 0 | return ossl_quic_port_get_rx_short_dcid_len(ch->port); |
659 | 0 | } |
660 | | |
661 | | QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch, |
662 | | uint64_t stream_id) |
663 | 0 | { |
664 | 0 | return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id); |
665 | 0 | } |
666 | | |
667 | | int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch) |
668 | 0 | { |
669 | 0 | return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE; |
670 | 0 | } |
671 | | |
672 | | int ossl_quic_channel_is_closing(const QUIC_CHANNEL *ch) |
673 | 0 | { |
674 | 0 | return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING; |
675 | 0 | } |
676 | | |
677 | | static int ossl_quic_channel_is_draining(const QUIC_CHANNEL *ch) |
678 | 0 | { |
679 | 0 | return ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING; |
680 | 0 | } |
681 | | |
682 | | static int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch) |
683 | 0 | { |
684 | 0 | return ossl_quic_channel_is_closing(ch) |
685 | 0 | || ossl_quic_channel_is_draining(ch); |
686 | 0 | } |
687 | | |
688 | | int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch) |
689 | 0 | { |
690 | 0 | return ch->state == QUIC_CHANNEL_STATE_TERMINATED; |
691 | 0 | } |
692 | | |
693 | | int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch) |
694 | 0 | { |
695 | 0 | return ossl_quic_channel_is_terminating(ch) |
696 | 0 | || ossl_quic_channel_is_terminated(ch); |
697 | 0 | } |
698 | | |
699 | | int ossl_quic_channel_is_server(const QUIC_CHANNEL *ch) |
700 | 0 | { |
701 | 0 | return ch->is_server; |
702 | 0 | } |
703 | | |
704 | | void ossl_quic_channel_notify_flush_done(QUIC_CHANNEL *ch) |
705 | 0 | { |
706 | 0 | ch_record_state_transition(ch, ch->terminate_cause.remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING : QUIC_CHANNEL_STATE_TERMINATING_CLOSING); |
707 | | /* |
708 | | * RFC 9000 s. 10.2 Immediate Close |
709 | | * These states SHOULD persist for at least three times |
710 | | * the current PTO interval as defined in [QUIC-RECOVERY]. |
711 | | */ |
712 | 0 | ch->terminate_deadline |
713 | 0 | = ossl_time_add(get_time(ch), |
714 | 0 | ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm), 3)); |
715 | 0 | if (!ch->terminate_cause.remote) { |
716 | 0 | OSSL_QUIC_FRAME_CONN_CLOSE f = { 0 }; |
717 | | |
718 | | /* best effort */ |
719 | 0 | f.error_code = ch->terminate_cause.error_code; |
720 | 0 | f.frame_type = ch->terminate_cause.frame_type; |
721 | 0 | f.is_app = ch->terminate_cause.app; |
722 | 0 | f.reason = (char *)ch->terminate_cause.reason; |
723 | 0 | f.reason_len = ch->terminate_cause.reason_len; |
724 | 0 | ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f); |
725 | | /* |
726 | | * RFC 9000 s. 10.2.2 Draining Connection State: |
727 | | * An endpoint that receives a CONNECTION_CLOSE frame MAY |
728 | | * send a single packet containing a CONNECTION_CLOSE |
729 | | * frame before entering the draining state, using a |
730 | | * NO_ERROR code if appropriate |
731 | | */ |
732 | 0 | ch->conn_close_queued = 1; |
733 | 0 | } |
734 | 0 | } |
735 | | |
736 | | const QUIC_TERMINATE_CAUSE * |
737 | | ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch) |
738 | 0 | { |
739 | 0 | return ossl_quic_channel_is_term_any(ch) ? &ch->terminate_cause : NULL; |
740 | 0 | } |
741 | | |
742 | | int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch) |
743 | 0 | { |
744 | 0 | return ch->handshake_complete; |
745 | 0 | } |
746 | | |
747 | | int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch) |
748 | 0 | { |
749 | 0 | return ch->handshake_confirmed; |
750 | 0 | } |
751 | | |
752 | | QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch) |
753 | 0 | { |
754 | 0 | return ch->port->demux; |
755 | 0 | } |
756 | | |
757 | | QUIC_PORT *ossl_quic_channel_get0_port(QUIC_CHANNEL *ch) |
758 | 0 | { |
759 | 0 | return ch->port; |
760 | 0 | } |
761 | | |
762 | | QUIC_ENGINE *ossl_quic_channel_get0_engine(QUIC_CHANNEL *ch) |
763 | 0 | { |
764 | 0 | return ossl_quic_port_get0_engine(ch->port); |
765 | 0 | } |
766 | | |
767 | | CRYPTO_MUTEX *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch) |
768 | 0 | { |
769 | 0 | return ossl_quic_port_get0_mutex(ch->port); |
770 | 0 | } |
771 | | |
772 | | int ossl_quic_channel_has_pending(const QUIC_CHANNEL *ch) |
773 | 0 | { |
774 | 0 | return ossl_quic_demux_has_pending(ch->port->demux) |
775 | 0 | || ossl_qrx_processed_read_pending(ch->qrx); |
776 | 0 | } |
777 | | |
778 | | /* |
779 | | * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components |
780 | | * ================================================================ |
781 | | */ |
782 | | |
783 | | /* Used by various components. */ |
784 | | static OSSL_TIME get_time(void *arg) |
785 | 0 | { |
786 | 0 | QUIC_CHANNEL *ch = arg; |
787 | |
|
788 | 0 | return ossl_quic_port_get_time(ch->port); |
789 | 0 | } |
790 | | |
791 | | /* Used by QSM. */ |
792 | | static uint64_t get_stream_limit(int uni, void *arg) |
793 | 0 | { |
794 | 0 | QUIC_CHANNEL *ch = arg; |
795 | |
|
796 | 0 | return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi; |
797 | 0 | } |
798 | | |
799 | | /* |
800 | | * Called by QRX to determine if a packet is potentially invalid before trying |
801 | | * to decrypt it. |
802 | | */ |
803 | | static int rx_late_validate(QUIC_PN pn, int pn_space, void *arg) |
804 | 0 | { |
805 | 0 | QUIC_CHANNEL *ch = arg; |
806 | | |
807 | | /* Potential duplicates should not be processed. */ |
808 | 0 | if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space)) |
809 | 0 | return 0; |
810 | | |
811 | 0 | return 1; |
812 | 0 | } |
813 | | |
814 | | /* |
815 | | * Triggers a TXKU (whether spontaneous or solicited). Does not check whether |
816 | | * spontaneous TXKU is currently allowed. |
817 | | */ |
818 | | QUIC_NEEDS_LOCK |
819 | | static void ch_trigger_txku(QUIC_CHANNEL *ch) |
820 | 0 | { |
821 | 0 | uint64_t next_pn |
822 | 0 | = ossl_quic_tx_packetiser_get_next_pn(ch->txp, QUIC_PN_SPACE_APP); |
823 | |
|
824 | 0 | if (!ossl_quic_pn_valid(next_pn) |
825 | 0 | || !ossl_qtx_trigger_key_update(ch->qtx)) { |
826 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0, |
827 | 0 | "key update"); |
828 | 0 | return; |
829 | 0 | } |
830 | | |
831 | 0 | ch->txku_in_progress = 1; |
832 | 0 | ch->txku_pn = next_pn; |
833 | 0 | ch->rxku_expected = ch->ku_locally_initiated; |
834 | 0 | } |
835 | | |
836 | | QUIC_NEEDS_LOCK |
837 | | static int txku_in_progress(QUIC_CHANNEL *ch) |
838 | 0 | { |
839 | 0 | if (ch->txku_in_progress |
840 | 0 | && ossl_ackm_get_largest_acked(ch->ackm, QUIC_PN_SPACE_APP) >= ch->txku_pn) { |
841 | 0 | OSSL_TIME pto = ossl_ackm_get_pto_duration(ch->ackm); |
842 | | |
843 | | /* |
844 | | * RFC 9001 s. 6.5: Endpoints SHOULD wait three times the PTO before |
845 | | * initiating a key update after receiving an acknowledgment that |
846 | | * confirms that the previous key update was received. |
847 | | * |
848 | | * Note that by the above wording, this period starts from when we get |
849 | | * the ack for a TXKU-triggering packet, not when the TXKU is initiated. |
850 | | * So we defer TXKU cooldown deadline calculation to this point. |
851 | | */ |
852 | 0 | ch->txku_in_progress = 0; |
853 | 0 | ch->txku_cooldown_deadline = ossl_time_add(get_time(ch), |
854 | 0 | ossl_time_multiply(pto, 3)); |
855 | 0 | } |
856 | |
|
857 | 0 | return ch->txku_in_progress; |
858 | 0 | } |
859 | | |
860 | | QUIC_NEEDS_LOCK |
861 | | static int txku_allowed(QUIC_CHANNEL *ch) |
862 | 0 | { |
863 | 0 | return ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT /* Sanity check. */ |
864 | | /* Strict RFC 9001 criterion for TXKU. */ |
865 | 0 | && ch->handshake_confirmed |
866 | 0 | && !txku_in_progress(ch); |
867 | 0 | } |
868 | | |
869 | | QUIC_NEEDS_LOCK |
870 | | static int txku_recommendable(QUIC_CHANNEL *ch) |
871 | 0 | { |
872 | 0 | if (!txku_allowed(ch)) |
873 | 0 | return 0; |
874 | | |
875 | 0 | return |
876 | | /* Recommended RFC 9001 criterion for TXKU. */ |
877 | 0 | ossl_time_compare(get_time(ch), ch->txku_cooldown_deadline) >= 0 |
878 | | /* Some additional sensible criteria. */ |
879 | 0 | && !ch->rxku_in_progress |
880 | 0 | && !ch->rxku_pending_confirm; |
881 | 0 | } |
882 | | |
883 | | QUIC_NEEDS_LOCK |
884 | | static int txku_desirable(QUIC_CHANNEL *ch) |
885 | 0 | { |
886 | 0 | uint64_t cur_pkt_count, max_pkt_count, thresh_pkt_count; |
887 | 0 | const uint32_t enc_level = QUIC_ENC_LEVEL_1RTT; |
888 | | |
889 | | /* Check AEAD limit to determine if we should perform a spontaneous TXKU. */ |
890 | 0 | cur_pkt_count = ossl_qtx_get_cur_epoch_pkt_count(ch->qtx, enc_level); |
891 | 0 | max_pkt_count = ossl_qtx_get_max_epoch_pkt_count(ch->qtx, enc_level); |
892 | |
|
893 | 0 | thresh_pkt_count = max_pkt_count / 2; |
894 | 0 | if (ch->txku_threshold_override != UINT64_MAX) |
895 | 0 | thresh_pkt_count = ch->txku_threshold_override; |
896 | |
|
897 | 0 | return cur_pkt_count >= thresh_pkt_count; |
898 | 0 | } |
899 | | |
900 | | QUIC_NEEDS_LOCK |
901 | | static void ch_maybe_trigger_spontaneous_txku(QUIC_CHANNEL *ch) |
902 | 0 | { |
903 | 0 | if (!txku_recommendable(ch) || !txku_desirable(ch)) |
904 | 0 | return; |
905 | | |
906 | 0 | ch->ku_locally_initiated = 1; |
907 | 0 | ch_trigger_txku(ch); |
908 | 0 | } |
909 | | |
910 | | QUIC_NEEDS_LOCK |
911 | | static int rxku_allowed(QUIC_CHANNEL *ch) |
912 | 0 | { |
913 | | /* |
914 | | * RFC 9001 s. 6.1: An endpoint MUST NOT initiate a key update prior to |
915 | | * having confirmed the handshake (Section 4.1.2). |
916 | | * |
917 | | * RFC 9001 s. 6.1: An endpoint MUST NOT initiate a subsequent key update |
918 | | * unless it has received an acknowledgment for a packet that was sent |
919 | | * protected with keys from the current key phase. |
920 | | * |
921 | | * RFC 9001 s. 6.2: If an endpoint detects a second update before it has |
922 | | * sent any packets with updated keys containing an acknowledgment for the |
923 | | * packet that initiated the key update, it indicates that its peer has |
924 | | * updated keys twice without awaiting confirmation. An endpoint MAY treat |
925 | | * such consecutive key updates as a connection error of type |
926 | | * KEY_UPDATE_ERROR. |
927 | | */ |
928 | 0 | return ch->handshake_confirmed && !ch->rxku_pending_confirm; |
929 | 0 | } |
930 | | |
931 | | /* |
932 | | * Called when the QRX detects a new RX key update event. |
933 | | */ |
934 | | enum rxku_decision { |
935 | | DECISION_RXKU_ONLY, |
936 | | DECISION_PROTOCOL_VIOLATION, |
937 | | DECISION_SOLICITED_TXKU |
938 | | }; |
939 | | |
940 | | /* Called when the QRX detects a key update has occurred. */ |
941 | | QUIC_NEEDS_LOCK |
942 | | static void rxku_detected(QUIC_PN pn, void *arg) |
943 | 0 | { |
944 | 0 | QUIC_CHANNEL *ch = arg; |
945 | 0 | enum rxku_decision decision; |
946 | 0 | OSSL_TIME pto; |
947 | | |
948 | | /* |
949 | | * Note: rxku_in_progress is always 0 here as an RXKU cannot be detected |
950 | | * when we are still in UPDATING or COOLDOWN (see quic_record_rx.h). |
951 | | */ |
952 | 0 | assert(!ch->rxku_in_progress); |
953 | |
|
954 | 0 | if (!rxku_allowed(ch)) |
955 | | /* Is RXKU even allowed at this time? */ |
956 | 0 | decision = DECISION_PROTOCOL_VIOLATION; |
957 | | |
958 | 0 | else if (ch->ku_locally_initiated) |
959 | | /* |
960 | | * If this key update was locally initiated (meaning that this detected |
961 | | * RXKU event is a result of our own spontaneous TXKU), we do not |
962 | | * trigger another TXKU; after all, to do so would result in an infinite |
963 | | * ping-pong of key updates. We still process it as an RXKU. |
964 | | */ |
965 | 0 | decision = DECISION_RXKU_ONLY; |
966 | | |
967 | 0 | else |
968 | | /* |
969 | | * Otherwise, a peer triggering a KU means we have to trigger a KU also. |
970 | | */ |
971 | 0 | decision = DECISION_SOLICITED_TXKU; |
972 | |
|
973 | 0 | if (decision == DECISION_PROTOCOL_VIOLATION) { |
974 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR, |
975 | 0 | 0, "RX key update again too soon"); |
976 | 0 | return; |
977 | 0 | } |
978 | | |
979 | 0 | pto = ossl_ackm_get_pto_duration(ch->ackm); |
980 | |
|
981 | 0 | ch->ku_locally_initiated = 0; |
982 | 0 | ch->rxku_in_progress = 1; |
983 | 0 | ch->rxku_pending_confirm = 1; |
984 | 0 | ch->rxku_trigger_pn = pn; |
985 | 0 | ch->rxku_update_end_deadline = ossl_time_add(get_time(ch), pto); |
986 | 0 | ch->rxku_expected = 0; |
987 | |
|
988 | 0 | if (decision == DECISION_SOLICITED_TXKU) |
989 | | /* NOT gated by usual txku_allowed() */ |
990 | 0 | ch_trigger_txku(ch); |
991 | | |
992 | | /* |
993 | | * Ordinarily, we only generate ACK when some ACK-eliciting frame has been |
994 | | * received. In some cases, this may not occur for a long time, for example |
995 | | * if transmission of application data is going in only one direction and |
996 | | * nothing else is happening with the connection. However, since the peer |
997 | | * cannot initiate a subsequent (spontaneous) TXKU until its prior |
998 | | * (spontaneous or solicited) TXKU has completed - meaning that prior |
999 | | * TXKU's trigger packet (or subsequent packet) has been acknowledged, this |
1000 | | * can lead to very long times before a TXKU is considered 'completed'. |
1001 | | * Optimise this by forcing ACK generation after triggering TXKU. |
1002 | | * (Basically, we consider a RXKU event something that is 'ACK-eliciting', |
1003 | | * which it more or less should be; it is necessarily separate from ordinary |
1004 | | * processing of ACK-eliciting frames as key update is not indicated via a |
1005 | | * frame.) |
1006 | | */ |
1007 | 0 | ossl_quic_tx_packetiser_schedule_ack(ch->txp, QUIC_PN_SPACE_APP); |
1008 | 0 | } |
1009 | | |
1010 | | /* Called per tick to handle RXKU timer events. */ |
1011 | | QUIC_NEEDS_LOCK |
1012 | | static void ch_rxku_tick(QUIC_CHANNEL *ch) |
1013 | 0 | { |
1014 | 0 | if (!ch->rxku_in_progress |
1015 | 0 | || ossl_time_compare(get_time(ch), ch->rxku_update_end_deadline) < 0) |
1016 | 0 | return; |
1017 | | |
1018 | 0 | ch->rxku_update_end_deadline = ossl_time_infinite(); |
1019 | 0 | ch->rxku_in_progress = 0; |
1020 | |
|
1021 | 0 | if (!ossl_qrx_key_update_timeout(ch->qrx, /*normal=*/1)) |
1022 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0, |
1023 | 0 | "RXKU cooldown internal error"); |
1024 | 0 | } |
1025 | | |
1026 | | QUIC_NEEDS_LOCK |
1027 | | static void ch_on_txp_ack_tx(const OSSL_QUIC_FRAME_ACK *ack, uint32_t pn_space, |
1028 | | void *arg) |
1029 | 0 | { |
1030 | 0 | QUIC_CHANNEL *ch = arg; |
1031 | |
|
1032 | 0 | if (pn_space != QUIC_PN_SPACE_APP || !ch->rxku_pending_confirm |
1033 | 0 | || !ossl_quic_frame_ack_contains_pn(ack, ch->rxku_trigger_pn)) |
1034 | 0 | return; |
1035 | | |
1036 | | /* |
1037 | | * Defer clearing rxku_pending_confirm until TXP generate call returns |
1038 | | * successfully. |
1039 | | */ |
1040 | 0 | ch->rxku_pending_confirm_done = 1; |
1041 | 0 | } |
1042 | | |
1043 | | /* |
1044 | | * QUIC Channel: Handshake Layer Event Handling |
1045 | | * ============================================ |
1046 | | */ |
1047 | | static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len, |
1048 | | size_t *consumed, void *arg) |
1049 | 0 | { |
1050 | 0 | int ret; |
1051 | 0 | QUIC_CHANNEL *ch = arg; |
1052 | 0 | uint32_t enc_level = ch->tx_enc_level; |
1053 | 0 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); |
1054 | 0 | QUIC_SSTREAM *sstream = ch->crypto_send[pn_space]; |
1055 | |
|
1056 | 0 | if (!ossl_assert(sstream != NULL)) |
1057 | 0 | return 0; |
1058 | | |
1059 | 0 | ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed); |
1060 | 0 | return ret; |
1061 | 0 | } |
1062 | | |
1063 | | static int crypto_ensure_empty(QUIC_RSTREAM *rstream) |
1064 | 0 | { |
1065 | 0 | size_t avail = 0; |
1066 | 0 | int is_fin = 0; |
1067 | |
|
1068 | 0 | if (rstream == NULL) |
1069 | 0 | return 1; |
1070 | | |
1071 | 0 | if (!ossl_quic_rstream_available(rstream, &avail, &is_fin)) |
1072 | 0 | return 0; |
1073 | | |
1074 | 0 | return avail == 0; |
1075 | 0 | } |
1076 | | |
1077 | | static int ch_on_crypto_recv_record(const unsigned char **buf, |
1078 | | size_t *bytes_read, void *arg) |
1079 | 0 | { |
1080 | 0 | QUIC_CHANNEL *ch = arg; |
1081 | 0 | QUIC_RSTREAM *rstream; |
1082 | 0 | int is_fin = 0; /* crypto stream is never finished, so we don't use this */ |
1083 | 0 | uint32_t i; |
1084 | | |
1085 | | /* |
1086 | | * After we move to a later EL we must not allow our peer to send any new |
1087 | | * bytes in the crypto stream on a previous EL. Retransmissions of old bytes |
1088 | | * are allowed. |
1089 | | * |
1090 | | * In practice we will only move to a new EL when we have consumed all bytes |
1091 | | * which should be sent on the crypto stream at a previous EL. For example, |
1092 | | * the Handshake EL should not be provisioned until we have completely |
1093 | | * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output |
1094 | | * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a |
1095 | | * given EL is available we simply ensure we have not received any further |
1096 | | * bytes at a lower EL. |
1097 | | */ |
1098 | 0 | for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->rx_enc_level; ++i) |
1099 | 0 | if (i != QUIC_ENC_LEVEL_0RTT && !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { |
1100 | | /* Protocol violation (RFC 9001 s. 4.1.3) */ |
1101 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
1102 | 0 | OSSL_QUIC_FRAME_TYPE_CRYPTO, |
1103 | 0 | "crypto stream data in wrong EL"); |
1104 | 0 | return 0; |
1105 | 0 | } |
1106 | | |
1107 | 0 | rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->rx_enc_level)]; |
1108 | 0 | if (rstream == NULL) |
1109 | 0 | return 0; |
1110 | | |
1111 | 0 | return ossl_quic_rstream_get_record(rstream, buf, bytes_read, |
1112 | 0 | &is_fin); |
1113 | 0 | } |
1114 | | |
1115 | | static int ch_on_crypto_release_record(size_t bytes_read, void *arg) |
1116 | 0 | { |
1117 | 0 | QUIC_CHANNEL *ch = arg; |
1118 | 0 | QUIC_RSTREAM *rstream; |
1119 | 0 | OSSL_RTT_INFO rtt_info; |
1120 | 0 | uint32_t rx_pn_space = ossl_quic_enc_level_to_pn_space(ch->rx_enc_level); |
1121 | |
|
1122 | 0 | rstream = ch->crypto_recv[rx_pn_space]; |
1123 | 0 | if (rstream == NULL) |
1124 | 0 | return 0; |
1125 | | |
1126 | 0 | ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ch), &rtt_info); |
1127 | 0 | if (!ossl_quic_rxfc_on_retire(&ch->crypto_rxfc[rx_pn_space], bytes_read, |
1128 | 0 | rtt_info.smoothed_rtt)) |
1129 | 0 | return 0; |
1130 | | |
1131 | 0 | return ossl_quic_rstream_release_record(rstream, bytes_read); |
1132 | 0 | } |
1133 | | |
1134 | | static int ch_on_handshake_yield_secret(uint32_t prot_level, int direction, |
1135 | | uint32_t suite_id, EVP_MD *md, |
1136 | | const unsigned char *secret, |
1137 | | size_t secret_len, |
1138 | | void *arg) |
1139 | 0 | { |
1140 | 0 | QUIC_CHANNEL *ch = arg; |
1141 | 0 | uint32_t i; |
1142 | 0 | uint32_t enc_level; |
1143 | | |
1144 | | /* Convert TLS protection level to QUIC encryption level */ |
1145 | 0 | switch (prot_level) { |
1146 | 0 | case OSSL_RECORD_PROTECTION_LEVEL_EARLY: |
1147 | 0 | enc_level = QUIC_ENC_LEVEL_0RTT; |
1148 | 0 | break; |
1149 | | |
1150 | 0 | case OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE: |
1151 | 0 | enc_level = QUIC_ENC_LEVEL_HANDSHAKE; |
1152 | 0 | break; |
1153 | | |
1154 | 0 | case OSSL_RECORD_PROTECTION_LEVEL_APPLICATION: |
1155 | 0 | enc_level = QUIC_ENC_LEVEL_1RTT; |
1156 | 0 | break; |
1157 | | |
1158 | 0 | default: |
1159 | 0 | return 0; |
1160 | 0 | } |
1161 | | |
1162 | 0 | if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM) |
1163 | | /* Invalid EL. */ |
1164 | 0 | return 0; |
1165 | | |
1166 | 0 | if (direction) { |
1167 | | /* TX */ |
1168 | 0 | if (enc_level <= ch->tx_enc_level) |
1169 | | /* |
1170 | | * Does not make sense for us to try and provision an EL we have already |
1171 | | * attained. |
1172 | | */ |
1173 | 0 | return 0; |
1174 | | |
1175 | 0 | if (!ossl_qtx_provide_secret(ch->qtx, enc_level, |
1176 | 0 | suite_id, md, |
1177 | 0 | secret, secret_len)) |
1178 | 0 | return 0; |
1179 | | |
1180 | 0 | ch->tx_enc_level = enc_level; |
1181 | 0 | } else { |
1182 | | /* RX */ |
1183 | 0 | if (enc_level <= ch->rx_enc_level) |
1184 | | /* |
1185 | | * Does not make sense for us to try and provision an EL we have already |
1186 | | * attained. |
1187 | | */ |
1188 | 0 | return 0; |
1189 | | |
1190 | | /* |
1191 | | * Ensure all crypto streams for previous ELs are now empty of available |
1192 | | * data. |
1193 | | */ |
1194 | 0 | for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i) |
1195 | 0 | if (!crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) { |
1196 | | /* Protocol violation (RFC 9001 s. 4.1.3) */ |
1197 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
1198 | 0 | OSSL_QUIC_FRAME_TYPE_CRYPTO, |
1199 | 0 | "crypto stream data in wrong EL"); |
1200 | 0 | return 0; |
1201 | 0 | } |
1202 | | |
1203 | 0 | if (!ossl_qrx_provide_secret(ch->qrx, enc_level, |
1204 | 0 | suite_id, md, |
1205 | 0 | secret, secret_len)) |
1206 | 0 | return 0; |
1207 | | |
1208 | 0 | ch->have_new_rx_secret = 1; |
1209 | 0 | ch->rx_enc_level = enc_level; |
1210 | 0 | } |
1211 | | |
1212 | 0 | return 1; |
1213 | 0 | } |
1214 | | |
1215 | | static int ch_on_handshake_complete(void *arg) |
1216 | 0 | { |
1217 | 0 | QUIC_CHANNEL *ch = arg; |
1218 | |
|
1219 | 0 | if (!ossl_assert(!ch->handshake_complete)) |
1220 | 0 | return 0; /* this should not happen twice */ |
1221 | | |
1222 | 0 | if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT)) |
1223 | 0 | return 0; |
1224 | | |
1225 | | /* |
1226 | | * When handshake is complete, we no longer need to abide by the |
1227 | | * 3x amplification limit, though we should be validated as soon |
1228 | | * as we see a handshake key encrypted packet (see ossl_quic_handle_packet) |
1229 | | */ |
1230 | 0 | ossl_quic_tx_packetiser_set_validated(ch->txp); |
1231 | |
|
1232 | 0 | if (!ch->got_remote_transport_params) { |
1233 | | /* |
1234 | | * Was not a valid QUIC handshake if we did not get valid transport |
1235 | | * params. |
1236 | | */ |
1237 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_MISSING_EXT, |
1238 | 0 | OSSL_QUIC_FRAME_TYPE_CRYPTO, |
1239 | 0 | "no transport parameters received"); |
1240 | 0 | return 0; |
1241 | 0 | } |
1242 | | |
1243 | | /* Don't need transport parameters anymore. */ |
1244 | 0 | OPENSSL_free(ch->local_transport_params); |
1245 | 0 | ch->local_transport_params = NULL; |
1246 | | |
1247 | | /* Tell the QRX it can now process 1-RTT packets. */ |
1248 | 0 | ossl_qrx_allow_1rtt_processing(ch->qrx); |
1249 | | |
1250 | | /* Tell TXP the handshake is complete. */ |
1251 | 0 | ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp); |
1252 | |
|
1253 | 0 | ch->handshake_complete = 1; |
1254 | |
|
1255 | 0 | if (ch->pending_new_token != NULL) { |
1256 | | /* |
1257 | | * Note this is a best effort operation here |
1258 | | * If scheduling a new token fails, the worst outcome is that |
1259 | | * a client, not having received it, will just have to go through |
1260 | | * an extra roundtrip on a subsequent connection via the retry frame |
1261 | | * path, at which point we get another opportunity to schedule another |
1262 | | * new token. As a result, we don't need to handle any errors here |
1263 | | */ |
1264 | 0 | ossl_quic_channel_schedule_new_token(ch, |
1265 | 0 | ch->pending_new_token, |
1266 | 0 | ch->pending_new_token_len); |
1267 | 0 | OPENSSL_free(ch->pending_new_token); |
1268 | 0 | ch->pending_new_token = NULL; |
1269 | 0 | ch->pending_new_token_len = 0; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | if (ch->is_server) { |
1273 | | /* |
1274 | | * On the server, the handshake is confirmed as soon as it is complete. |
1275 | | */ |
1276 | 0 | ossl_quic_channel_on_handshake_confirmed(ch); |
1277 | |
|
1278 | 0 | ossl_quic_tx_packetiser_schedule_handshake_done(ch->txp); |
1279 | 0 | } |
1280 | |
|
1281 | 0 | ch_record_state_transition(ch, ch->state); |
1282 | 0 | return 1; |
1283 | 0 | } |
1284 | | |
1285 | | static int ch_on_handshake_alert(void *arg, unsigned char alert_code) |
1286 | 0 | { |
1287 | 0 | QUIC_CHANNEL *ch = arg; |
1288 | | |
1289 | | /* |
1290 | | * RFC 9001 s. 4.4: More specifically, servers MUST NOT send post-handshake |
1291 | | * TLS CertificateRequest messages, and clients MUST treat receipt of such |
1292 | | * messages as a connection error of type PROTOCOL_VIOLATION. |
1293 | | */ |
1294 | 0 | if (alert_code == SSL_AD_UNEXPECTED_MESSAGE |
1295 | 0 | && ch->handshake_complete |
1296 | 0 | && ossl_quic_tls_is_cert_request(ch->qtls)) |
1297 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
1298 | 0 | OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
1299 | 0 | 0, |
1300 | 0 | "Post-handshake TLS " |
1301 | 0 | "CertificateRequest received"); |
1302 | | /* |
1303 | | * RFC 9001 s. 4.6.1: Servers MUST NOT send the early_data extension with a |
1304 | | * max_early_data_size field set to any value other than 0xffffffff. A |
1305 | | * client MUST treat receipt of a NewSessionTicket that contains an |
1306 | | * early_data extension with any other value as a connection error of type |
1307 | | * PROTOCOL_VIOLATION. |
1308 | | */ |
1309 | 0 | else if (alert_code == SSL_AD_ILLEGAL_PARAMETER |
1310 | 0 | && ch->handshake_complete |
1311 | 0 | && ossl_quic_tls_has_bad_max_early_data(ch->qtls)) |
1312 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
1313 | 0 | OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
1314 | 0 | 0, |
1315 | 0 | "Bad max_early_data received"); |
1316 | 0 | else |
1317 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
1318 | 0 | OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN |
1319 | 0 | + alert_code, |
1320 | 0 | 0, "handshake alert"); |
1321 | |
|
1322 | 0 | return 1; |
1323 | 0 | } |
1324 | | |
1325 | | /* |
1326 | | * QUIC Channel: Transport Parameter Handling |
1327 | | * ========================================== |
1328 | | */ |
1329 | | |
1330 | | /* |
1331 | | * Called by handshake layer when we receive QUIC Transport Parameters from the |
1332 | | * peer. Note that these are not authenticated until the handshake is marked |
1333 | | * as complete. |
1334 | | */ |
1335 | | #define TP_REASON_SERVER_ONLY(x) \ |
1336 | 0 | x " may not be sent by a client" |
1337 | | #define TP_REASON_DUP(x) \ |
1338 | 0 | x " appears multiple times" |
1339 | | #define TP_REASON_MALFORMED(x) \ |
1340 | 0 | x " is malformed" |
1341 | | #define TP_REASON_EXPECTED_VALUE(x) \ |
1342 | 0 | x " does not match expected value" |
1343 | | #define TP_REASON_NOT_RETRY(x) \ |
1344 | 0 | x " sent when not performing a retry" |
1345 | | #define TP_REASON_REQUIRED(x) \ |
1346 | 0 | x " was not sent but is required" |
1347 | | #define TP_REASON_INTERNAL_ERROR(x) \ |
1348 | 0 | x " encountered internal error" |
1349 | | |
1350 | | static void txfc_bump_cwm_bidi(QUIC_STREAM *s, void *arg) |
1351 | 0 | { |
1352 | 0 | if (!ossl_quic_stream_is_bidi(s) |
1353 | 0 | || ossl_quic_stream_is_server_init(s)) |
1354 | 0 | return; |
1355 | | |
1356 | 0 | ossl_quic_txfc_bump_cwm(&s->txfc, *(uint64_t *)arg); |
1357 | 0 | } |
1358 | | |
1359 | | static void txfc_bump_cwm_uni(QUIC_STREAM *s, void *arg) |
1360 | 0 | { |
1361 | 0 | if (ossl_quic_stream_is_bidi(s) |
1362 | 0 | || ossl_quic_stream_is_server_init(s)) |
1363 | 0 | return; |
1364 | | |
1365 | 0 | ossl_quic_txfc_bump_cwm(&s->txfc, *(uint64_t *)arg); |
1366 | 0 | } |
1367 | | |
1368 | | static void do_update(QUIC_STREAM *s, void *arg) |
1369 | 0 | { |
1370 | 0 | QUIC_CHANNEL *ch = arg; |
1371 | |
|
1372 | 0 | ossl_quic_stream_map_update_state(&ch->qsm, s); |
1373 | 0 | } |
1374 | | |
1375 | | static uint64_t min_u64_ignore_0(uint64_t a, uint64_t b) |
1376 | 0 | { |
1377 | 0 | if (a == 0) |
1378 | 0 | return b; |
1379 | 0 | if (b == 0) |
1380 | 0 | return a; |
1381 | | |
1382 | 0 | return a < b ? a : b; |
1383 | 0 | } |
1384 | | |
1385 | | static int ch_on_transport_params(const unsigned char *params, |
1386 | | size_t params_len, |
1387 | | void *arg) |
1388 | 0 | { |
1389 | 0 | QUIC_CHANNEL *ch = arg; |
1390 | 0 | PACKET pkt; |
1391 | 0 | uint64_t id, v; |
1392 | 0 | size_t len; |
1393 | 0 | const unsigned char *body; |
1394 | 0 | int got_orig_dcid = 0; |
1395 | 0 | int got_initial_scid = 0; |
1396 | 0 | int got_retry_scid = 0; |
1397 | 0 | int got_initial_max_data = 0; |
1398 | 0 | int got_initial_max_stream_data_bidi_local = 0; |
1399 | 0 | int got_initial_max_stream_data_bidi_remote = 0; |
1400 | 0 | int got_initial_max_stream_data_uni = 0; |
1401 | 0 | int got_initial_max_streams_bidi = 0; |
1402 | 0 | int got_initial_max_streams_uni = 0; |
1403 | 0 | int got_stateless_reset_token = 0; |
1404 | 0 | int got_preferred_addr = 0; |
1405 | 0 | int got_ack_delay_exp = 0; |
1406 | 0 | int got_max_ack_delay = 0; |
1407 | 0 | int got_max_udp_payload_size = 0; |
1408 | 0 | int got_max_idle_timeout = 0; |
1409 | 0 | int got_active_conn_id_limit = 0; |
1410 | 0 | int got_disable_active_migration = 0; |
1411 | 0 | QUIC_CONN_ID cid; |
1412 | 0 | const char *reason = "bad transport parameter"; |
1413 | 0 | ossl_unused const void *stateless_reset_token_p = NULL; |
1414 | 0 | QUIC_PREFERRED_ADDR pfa; |
1415 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ch->tls); |
1416 | |
|
1417 | 0 | if (sc == NULL) { |
1418 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0, |
1419 | 0 | "could not get ssl connection"); |
1420 | 0 | return 0; |
1421 | 0 | } |
1422 | | /* |
1423 | | * When HRR happens the client sends the transport params in the new client |
1424 | | * hello again. Reset the transport params here and load them again. |
1425 | | */ |
1426 | 0 | if (ch->is_server && sc->hello_retry_request != SSL_HRR_NONE |
1427 | 0 | && ch->got_remote_transport_params) { |
1428 | 0 | ch->max_local_streams_bidi = 0; |
1429 | 0 | ch->max_local_streams_uni = 0; |
1430 | 0 | ch->got_local_transport_params = 0; |
1431 | 0 | OPENSSL_free(ch->local_transport_params); |
1432 | 0 | ch->local_transport_params = NULL; |
1433 | 0 | } else if (ch->got_remote_transport_params) { |
1434 | 0 | reason = "multiple transport parameter extensions"; |
1435 | 0 | goto malformed; |
1436 | 0 | } |
1437 | | |
1438 | 0 | if (!PACKET_buf_init(&pkt, params, params_len)) { |
1439 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0, |
1440 | 0 | "internal error (packet buf init)"); |
1441 | 0 | return 0; |
1442 | 0 | } |
1443 | | |
1444 | 0 | while (PACKET_remaining(&pkt) > 0) { |
1445 | 0 | if (!ossl_quic_wire_peek_transport_param(&pkt, &id)) |
1446 | 0 | goto malformed; |
1447 | | |
1448 | 0 | switch (id) { |
1449 | 0 | case QUIC_TPARAM_ORIG_DCID: |
1450 | 0 | if (got_orig_dcid) { |
1451 | 0 | reason = TP_REASON_DUP("ORIG_DCID"); |
1452 | 0 | goto malformed; |
1453 | 0 | } |
1454 | | |
1455 | 0 | if (ch->is_server) { |
1456 | 0 | reason = TP_REASON_SERVER_ONLY("ORIG_DCID"); |
1457 | 0 | goto malformed; |
1458 | 0 | } |
1459 | | |
1460 | 0 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1461 | 0 | reason = TP_REASON_MALFORMED("ORIG_DCID"); |
1462 | 0 | goto malformed; |
1463 | 0 | } |
1464 | | |
1465 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1466 | | /* Must match our initial DCID. */ |
1467 | | if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid)) { |
1468 | | reason = TP_REASON_EXPECTED_VALUE("ORIG_DCID"); |
1469 | | goto malformed; |
1470 | | } |
1471 | | #endif |
1472 | | |
1473 | 0 | got_orig_dcid = 1; |
1474 | 0 | break; |
1475 | | |
1476 | 0 | case QUIC_TPARAM_RETRY_SCID: |
1477 | 0 | if (ch->is_server) { |
1478 | 0 | reason = TP_REASON_SERVER_ONLY("RETRY_SCID"); |
1479 | 0 | goto malformed; |
1480 | 0 | } |
1481 | | |
1482 | 0 | if (got_retry_scid) { |
1483 | 0 | reason = TP_REASON_DUP("RETRY_SCID"); |
1484 | 0 | goto malformed; |
1485 | 0 | } |
1486 | | |
1487 | 0 | if (!ch->doing_retry) { |
1488 | 0 | reason = TP_REASON_NOT_RETRY("RETRY_SCID"); |
1489 | 0 | goto malformed; |
1490 | 0 | } |
1491 | | |
1492 | 0 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1493 | 0 | reason = TP_REASON_MALFORMED("RETRY_SCID"); |
1494 | 0 | goto malformed; |
1495 | 0 | } |
1496 | | |
1497 | | /* Must match Retry packet SCID. */ |
1498 | 0 | if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid)) { |
1499 | 0 | reason = TP_REASON_EXPECTED_VALUE("RETRY_SCID"); |
1500 | 0 | goto malformed; |
1501 | 0 | } |
1502 | | |
1503 | 0 | got_retry_scid = 1; |
1504 | 0 | break; |
1505 | | |
1506 | 0 | case QUIC_TPARAM_INITIAL_SCID: |
1507 | 0 | if (got_initial_scid) { |
1508 | | /* must not appear more than once */ |
1509 | 0 | reason = TP_REASON_DUP("INITIAL_SCID"); |
1510 | 0 | goto malformed; |
1511 | 0 | } |
1512 | | |
1513 | 0 | if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) { |
1514 | 0 | reason = TP_REASON_MALFORMED("INITIAL_SCID"); |
1515 | 0 | goto malformed; |
1516 | 0 | } |
1517 | | |
1518 | 0 | if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) { |
1519 | 0 | reason = TP_REASON_EXPECTED_VALUE("INITIAL_SCID"); |
1520 | 0 | goto malformed; |
1521 | 0 | } |
1522 | | |
1523 | 0 | got_initial_scid = 1; |
1524 | 0 | break; |
1525 | | |
1526 | 0 | case QUIC_TPARAM_INITIAL_MAX_DATA: |
1527 | 0 | if (got_initial_max_data) { |
1528 | | /* must not appear more than once */ |
1529 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_DATA"); |
1530 | 0 | goto malformed; |
1531 | 0 | } |
1532 | | |
1533 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1534 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_DATA"); |
1535 | 0 | goto malformed; |
1536 | 0 | } |
1537 | | |
1538 | 0 | ch->rx_init_max_data = v; |
1539 | |
|
1540 | 0 | ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v); |
1541 | 0 | got_initial_max_data = 1; |
1542 | 0 | break; |
1543 | | |
1544 | 0 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: |
1545 | 0 | if (got_initial_max_stream_data_bidi_local) { |
1546 | | /* must not appear more than once */ |
1547 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); |
1548 | 0 | goto malformed; |
1549 | 0 | } |
1550 | | |
1551 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1552 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL"); |
1553 | 0 | goto malformed; |
1554 | 0 | } |
1555 | | |
1556 | | /* |
1557 | | * This is correct; the BIDI_LOCAL TP governs streams created by |
1558 | | * the endpoint which sends the TP, i.e., our peer. |
1559 | | */ |
1560 | 0 | ch->rx_init_max_stream_data_bidi_remote = v; |
1561 | 0 | got_initial_max_stream_data_bidi_local = 1; |
1562 | 0 | break; |
1563 | | |
1564 | 0 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: |
1565 | 0 | if (got_initial_max_stream_data_bidi_remote) { |
1566 | | /* must not appear more than once */ |
1567 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); |
1568 | 0 | goto malformed; |
1569 | 0 | } |
1570 | | |
1571 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1572 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE"); |
1573 | 0 | goto malformed; |
1574 | 0 | } |
1575 | | |
1576 | | /* |
1577 | | * This is correct; the BIDI_REMOTE TP governs streams created |
1578 | | * by the endpoint which receives the TP, i.e., us. |
1579 | | */ |
1580 | 0 | ch->rx_init_max_stream_data_bidi_local = v; |
1581 | | |
1582 | | /* Apply to all existing streams. */ |
1583 | 0 | ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_bidi, &v); |
1584 | 0 | got_initial_max_stream_data_bidi_remote = 1; |
1585 | 0 | break; |
1586 | | |
1587 | 0 | case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI: |
1588 | 0 | if (got_initial_max_stream_data_uni) { |
1589 | | /* must not appear more than once */ |
1590 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_UNI"); |
1591 | 0 | goto malformed; |
1592 | 0 | } |
1593 | | |
1594 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1595 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_UNI"); |
1596 | 0 | goto malformed; |
1597 | 0 | } |
1598 | | |
1599 | 0 | ch->rx_init_max_stream_data_uni = v; |
1600 | | |
1601 | | /* Apply to all existing streams. */ |
1602 | 0 | ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_uni, &v); |
1603 | 0 | got_initial_max_stream_data_uni = 1; |
1604 | 0 | break; |
1605 | | |
1606 | 0 | case QUIC_TPARAM_ACK_DELAY_EXP: |
1607 | 0 | if (got_ack_delay_exp) { |
1608 | | /* must not appear more than once */ |
1609 | 0 | reason = TP_REASON_DUP("ACK_DELAY_EXP"); |
1610 | 0 | goto malformed; |
1611 | 0 | } |
1612 | | |
1613 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1614 | 0 | || v > QUIC_MAX_ACK_DELAY_EXP) { |
1615 | 0 | reason = TP_REASON_MALFORMED("ACK_DELAY_EXP"); |
1616 | 0 | goto malformed; |
1617 | 0 | } |
1618 | | |
1619 | 0 | ch->rx_ack_delay_exp = (unsigned char)v; |
1620 | 0 | got_ack_delay_exp = 1; |
1621 | 0 | break; |
1622 | | |
1623 | 0 | case QUIC_TPARAM_MAX_ACK_DELAY: |
1624 | 0 | if (got_max_ack_delay) { |
1625 | | /* must not appear more than once */ |
1626 | 0 | reason = TP_REASON_DUP("MAX_ACK_DELAY"); |
1627 | 0 | goto malformed; |
1628 | 0 | } |
1629 | | |
1630 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1631 | 0 | || v >= (((uint64_t)1) << 14)) { |
1632 | 0 | reason = TP_REASON_MALFORMED("MAX_ACK_DELAY"); |
1633 | 0 | goto malformed; |
1634 | 0 | } |
1635 | | |
1636 | 0 | ch->rx_max_ack_delay = v; |
1637 | 0 | ossl_ackm_set_rx_max_ack_delay(ch->ackm, |
1638 | 0 | ossl_ms2time(ch->rx_max_ack_delay)); |
1639 | |
|
1640 | 0 | got_max_ack_delay = 1; |
1641 | 0 | break; |
1642 | | |
1643 | 0 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI: |
1644 | 0 | if (got_initial_max_streams_bidi) { |
1645 | | /* must not appear more than once */ |
1646 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_BIDI"); |
1647 | 0 | goto malformed; |
1648 | 0 | } |
1649 | | |
1650 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1651 | 0 | || v > (((uint64_t)1) << 60)) { |
1652 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_BIDI"); |
1653 | 0 | goto malformed; |
1654 | 0 | } |
1655 | | |
1656 | 0 | assert(ch->max_local_streams_bidi == 0); |
1657 | 0 | ch->max_local_streams_bidi = v; |
1658 | 0 | ch->rx_init_max_streams_bidi = v; |
1659 | 0 | got_initial_max_streams_bidi = 1; |
1660 | 0 | break; |
1661 | | |
1662 | 0 | case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI: |
1663 | 0 | if (got_initial_max_streams_uni) { |
1664 | | /* must not appear more than once */ |
1665 | 0 | reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_UNI"); |
1666 | 0 | goto malformed; |
1667 | 0 | } |
1668 | | |
1669 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1670 | 0 | || v > (((uint64_t)1) << 60)) { |
1671 | 0 | reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_UNI"); |
1672 | 0 | goto malformed; |
1673 | 0 | } |
1674 | | |
1675 | 0 | assert(ch->max_local_streams_uni == 0); |
1676 | 0 | ch->max_local_streams_uni = v; |
1677 | 0 | ch->rx_init_max_streams_uni = v; |
1678 | 0 | got_initial_max_streams_uni = 1; |
1679 | 0 | break; |
1680 | | |
1681 | 0 | case QUIC_TPARAM_MAX_IDLE_TIMEOUT: |
1682 | 0 | if (got_max_idle_timeout) { |
1683 | | /* must not appear more than once */ |
1684 | 0 | reason = TP_REASON_DUP("MAX_IDLE_TIMEOUT"); |
1685 | 0 | goto malformed; |
1686 | 0 | } |
1687 | | |
1688 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) { |
1689 | 0 | reason = TP_REASON_MALFORMED("MAX_IDLE_TIMEOUT"); |
1690 | 0 | goto malformed; |
1691 | 0 | } |
1692 | | |
1693 | 0 | ch->max_idle_timeout_remote_req = v; |
1694 | |
|
1695 | 0 | ch->max_idle_timeout = min_u64_ignore_0(ch->max_idle_timeout_local_req, |
1696 | 0 | ch->max_idle_timeout_remote_req); |
1697 | |
|
1698 | 0 | ch_update_idle(ch); |
1699 | 0 | got_max_idle_timeout = 1; |
1700 | 0 | break; |
1701 | | |
1702 | 0 | case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE: |
1703 | 0 | if (got_max_udp_payload_size) { |
1704 | | /* must not appear more than once */ |
1705 | 0 | reason = TP_REASON_DUP("MAX_UDP_PAYLOAD_SIZE"); |
1706 | 0 | goto malformed; |
1707 | 0 | } |
1708 | | |
1709 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1710 | 0 | || v < QUIC_MIN_INITIAL_DGRAM_LEN |
1711 | 0 | || v > QUIC_MAX_MAX_UDP_PAYLOAD_SIZE) { |
1712 | 0 | reason = TP_REASON_MALFORMED("MAX_UDP_PAYLOAD_SIZE"); |
1713 | 0 | goto malformed; |
1714 | 0 | } |
1715 | | |
1716 | 0 | ch->rx_max_udp_payload_size = v; |
1717 | 0 | got_max_udp_payload_size = 1; |
1718 | 0 | break; |
1719 | | |
1720 | 0 | case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT: |
1721 | 0 | if (got_active_conn_id_limit) { |
1722 | | /* must not appear more than once */ |
1723 | 0 | reason = TP_REASON_DUP("ACTIVE_CONN_ID_LIMIT"); |
1724 | 0 | goto malformed; |
1725 | 0 | } |
1726 | | |
1727 | 0 | if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v) |
1728 | 0 | || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT) { |
1729 | 0 | reason = TP_REASON_MALFORMED("ACTIVE_CONN_ID_LIMIT"); |
1730 | 0 | goto malformed; |
1731 | 0 | } |
1732 | | |
1733 | 0 | ch->rx_active_conn_id_limit = v; |
1734 | 0 | got_active_conn_id_limit = 1; |
1735 | 0 | break; |
1736 | | |
1737 | 0 | case QUIC_TPARAM_STATELESS_RESET_TOKEN: |
1738 | 0 | if (got_stateless_reset_token) { |
1739 | 0 | reason = TP_REASON_DUP("STATELESS_RESET_TOKEN"); |
1740 | 0 | goto malformed; |
1741 | 0 | } |
1742 | | |
1743 | | /* |
1744 | | * RFC 9000 s. 18.2: This transport parameter MUST NOT be sent |
1745 | | * by a client but MAY be sent by a server. |
1746 | | */ |
1747 | 0 | if (ch->is_server) { |
1748 | 0 | reason = TP_REASON_SERVER_ONLY("STATELESS_RESET_TOKEN"); |
1749 | 0 | goto malformed; |
1750 | 0 | } |
1751 | | |
1752 | 0 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); |
1753 | 0 | if (body == NULL || len != QUIC_STATELESS_RESET_TOKEN_LEN) { |
1754 | 0 | reason = TP_REASON_MALFORMED("STATELESS_RESET_TOKEN"); |
1755 | 0 | goto malformed; |
1756 | 0 | } |
1757 | 0 | if (!ossl_quic_srtm_add(ch->srtm, ch, ch->cur_remote_seq_num, |
1758 | 0 | (const QUIC_STATELESS_RESET_TOKEN *)body)) { |
1759 | 0 | reason = TP_REASON_INTERNAL_ERROR("STATELESS_RESET_TOKEN"); |
1760 | 0 | goto malformed; |
1761 | 0 | } |
1762 | | |
1763 | 0 | stateless_reset_token_p = body; |
1764 | 0 | got_stateless_reset_token = 1; |
1765 | 0 | break; |
1766 | | |
1767 | 0 | case QUIC_TPARAM_PREFERRED_ADDR: |
1768 | | /* TODO(QUIC FUTURE): Handle preferred address. */ |
1769 | 0 | if (got_preferred_addr) { |
1770 | 0 | reason = TP_REASON_DUP("PREFERRED_ADDR"); |
1771 | 0 | goto malformed; |
1772 | 0 | } |
1773 | | |
1774 | | /* |
1775 | | * RFC 9000 s. 18.2: "A server that chooses a zero-length |
1776 | | * connection ID MUST NOT provide a preferred address. |
1777 | | * Similarly, a server MUST NOT include a zero-length connection |
1778 | | * ID in this transport parameter. A client MUST treat a |
1779 | | * violation of these requirements as a connection error of type |
1780 | | * TRANSPORT_PARAMETER_ERROR." |
1781 | | */ |
1782 | 0 | if (ch->is_server) { |
1783 | 0 | reason = TP_REASON_SERVER_ONLY("PREFERRED_ADDR"); |
1784 | 0 | goto malformed; |
1785 | 0 | } |
1786 | | |
1787 | 0 | if (ch->cur_remote_dcid.id_len == 0) { |
1788 | 0 | reason = "PREFERRED_ADDR provided for zero-length CID"; |
1789 | 0 | goto malformed; |
1790 | 0 | } |
1791 | | |
1792 | 0 | if (!ossl_quic_wire_decode_transport_param_preferred_addr(&pkt, &pfa)) { |
1793 | 0 | reason = TP_REASON_MALFORMED("PREFERRED_ADDR"); |
1794 | 0 | goto malformed; |
1795 | 0 | } |
1796 | | |
1797 | 0 | if (pfa.cid.id_len == 0) { |
1798 | 0 | reason = "zero-length CID in PREFERRED_ADDR"; |
1799 | 0 | goto malformed; |
1800 | 0 | } |
1801 | | |
1802 | 0 | got_preferred_addr = 1; |
1803 | 0 | break; |
1804 | | |
1805 | 0 | case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION: |
1806 | | /* We do not currently handle migration, so nothing to do. */ |
1807 | 0 | if (got_disable_active_migration) { |
1808 | | /* must not appear more than once */ |
1809 | 0 | reason = TP_REASON_DUP("DISABLE_ACTIVE_MIGRATION"); |
1810 | 0 | goto malformed; |
1811 | 0 | } |
1812 | | |
1813 | 0 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len); |
1814 | 0 | if (body == NULL || len > 0) { |
1815 | 0 | reason = TP_REASON_MALFORMED("DISABLE_ACTIVE_MIGRATION"); |
1816 | 0 | goto malformed; |
1817 | 0 | } |
1818 | | |
1819 | 0 | ch->rx_disable_active_migration = 1; |
1820 | 0 | got_disable_active_migration = 1; |
1821 | 0 | break; |
1822 | | |
1823 | 0 | default: |
1824 | | /* |
1825 | | * Skip over and ignore. |
1826 | | * |
1827 | | * RFC 9000 s. 7.4: We SHOULD treat duplicated transport parameters |
1828 | | * as a connection error, but we are not required to. Currently, |
1829 | | * handle this programmatically by checking for duplicates in the |
1830 | | * parameters that we recognise, as above, but don't bother |
1831 | | * maintaining a list of duplicates for anything we don't recognise. |
1832 | | */ |
1833 | 0 | body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, |
1834 | 0 | &len); |
1835 | 0 | if (body == NULL) |
1836 | 0 | goto malformed; |
1837 | | |
1838 | 0 | break; |
1839 | 0 | } |
1840 | 0 | } |
1841 | | |
1842 | 0 | if (!got_initial_scid) { |
1843 | 0 | reason = TP_REASON_REQUIRED("INITIAL_SCID"); |
1844 | 0 | goto malformed; |
1845 | 0 | } |
1846 | | |
1847 | 0 | if (!ch->is_server) { |
1848 | 0 | if (!got_orig_dcid) { |
1849 | 0 | reason = TP_REASON_REQUIRED("ORIG_DCID"); |
1850 | 0 | goto malformed; |
1851 | 0 | } |
1852 | | |
1853 | 0 | if (ch->doing_retry && !got_retry_scid) { |
1854 | 0 | reason = TP_REASON_REQUIRED("RETRY_SCID"); |
1855 | 0 | goto malformed; |
1856 | 0 | } |
1857 | 0 | } |
1858 | | |
1859 | 0 | ch->got_remote_transport_params = 1; |
1860 | |
|
1861 | 0 | #ifndef OPENSSL_NO_QLOG |
1862 | 0 | QLOG_EVENT_BEGIN(ch_get_qlog(ch), transport, parameters_set) |
1863 | 0 | QLOG_STR("owner", "remote"); |
1864 | |
|
1865 | 0 | if (got_orig_dcid) |
1866 | 0 | QLOG_CID("original_destination_connection_id", |
1867 | 0 | &ch->init_dcid); |
1868 | 0 | if (got_initial_scid) |
1869 | 0 | QLOG_CID("original_source_connection_id", |
1870 | 0 | &ch->init_dcid); |
1871 | 0 | if (got_retry_scid) |
1872 | 0 | QLOG_CID("retry_source_connection_id", |
1873 | 0 | &ch->retry_scid); |
1874 | 0 | if (got_initial_max_data) |
1875 | 0 | QLOG_U64("initial_max_data", |
1876 | 0 | ossl_quic_txfc_get_cwm(&ch->conn_txfc)); |
1877 | 0 | if (got_initial_max_stream_data_bidi_local) |
1878 | 0 | QLOG_U64("initial_max_stream_data_bidi_local", |
1879 | 0 | ch->rx_init_max_stream_data_bidi_local); |
1880 | 0 | if (got_initial_max_stream_data_bidi_remote) |
1881 | 0 | QLOG_U64("initial_max_stream_data_bidi_remote", |
1882 | 0 | ch->rx_init_max_stream_data_bidi_remote); |
1883 | 0 | if (got_initial_max_stream_data_uni) |
1884 | 0 | QLOG_U64("initial_max_stream_data_uni", |
1885 | 0 | ch->rx_init_max_stream_data_uni); |
1886 | 0 | if (got_initial_max_streams_bidi) |
1887 | 0 | QLOG_U64("initial_max_streams_bidi", |
1888 | 0 | ch->rx_init_max_streams_bidi); |
1889 | 0 | if (got_initial_max_streams_uni) |
1890 | 0 | QLOG_U64("initial_max_streams_uni", |
1891 | 0 | ch->rx_init_max_streams_uni); |
1892 | 0 | if (got_ack_delay_exp) |
1893 | 0 | QLOG_U64("ack_delay_exponent", ch->rx_ack_delay_exp); |
1894 | 0 | if (got_max_ack_delay) |
1895 | 0 | QLOG_U64("max_ack_delay", ch->rx_max_ack_delay); |
1896 | 0 | if (got_max_udp_payload_size) |
1897 | 0 | QLOG_U64("max_udp_payload_size", ch->rx_max_udp_payload_size); |
1898 | 0 | if (got_max_idle_timeout) |
1899 | 0 | QLOG_U64("max_idle_timeout", ch->max_idle_timeout_remote_req); |
1900 | 0 | if (got_active_conn_id_limit) |
1901 | 0 | QLOG_U64("active_connection_id_limit", ch->rx_active_conn_id_limit); |
1902 | 0 | if (got_stateless_reset_token) |
1903 | 0 | QLOG_BIN("stateless_reset_token", stateless_reset_token_p, |
1904 | 0 | QUIC_STATELESS_RESET_TOKEN_LEN); |
1905 | 0 | if (got_preferred_addr) { |
1906 | 0 | QLOG_BEGIN("preferred_addr") |
1907 | 0 | QLOG_U64("port_v4", pfa.ipv4_port); |
1908 | 0 | QLOG_U64("port_v6", pfa.ipv6_port); |
1909 | 0 | QLOG_BIN("ip_v4", pfa.ipv4, sizeof(pfa.ipv4)); |
1910 | 0 | QLOG_BIN("ip_v6", pfa.ipv6, sizeof(pfa.ipv6)); |
1911 | 0 | QLOG_BIN("stateless_reset_token", pfa.stateless_reset.token, |
1912 | 0 | sizeof(pfa.stateless_reset.token)); |
1913 | 0 | QLOG_CID("connection_id", &pfa.cid); |
1914 | 0 | QLOG_END() |
1915 | 0 | } |
1916 | 0 | QLOG_BOOL("disable_active_migration", ch->rx_disable_active_migration); |
1917 | 0 | QLOG_EVENT_END() |
1918 | 0 | #endif |
1919 | |
|
1920 | 0 | if (got_initial_max_data |
1921 | 0 | || got_initial_max_stream_data_bidi_remote || got_initial_max_stream_data_uni |
1922 | 0 | || got_initial_max_streams_bidi || got_initial_max_streams_uni) |
1923 | | /* |
1924 | | * If FC credit was bumped, we may now be able to send. Update all |
1925 | | * streams. |
1926 | | */ |
1927 | 0 | ossl_quic_stream_map_visit(&ch->qsm, do_update, ch); |
1928 | | |
1929 | | /* If we are a server, we now generate our own transport parameters. */ |
1930 | 0 | if (ch->is_server && !ch_generate_transport_params(ch)) { |
1931 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0, |
1932 | 0 | "internal error"); |
1933 | 0 | return 0; |
1934 | 0 | } |
1935 | | |
1936 | 0 | return 1; |
1937 | | |
1938 | 0 | malformed: |
1939 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR, |
1940 | 0 | 0, reason); |
1941 | 0 | return 0; |
1942 | 0 | } |
1943 | | |
1944 | | /* |
1945 | | * Called when we want to generate transport parameters. This is called |
1946 | | * immediately at instantiation time for a client and after we receive the |
1947 | | * client's transport parameters for a server. |
1948 | | */ |
1949 | | static int ch_generate_transport_params(QUIC_CHANNEL *ch) |
1950 | 0 | { |
1951 | 0 | int ok = 0; |
1952 | 0 | BUF_MEM *buf_mem = NULL; |
1953 | 0 | WPACKET wpkt; |
1954 | 0 | int wpkt_valid = 0; |
1955 | 0 | size_t buf_len = 0; |
1956 | 0 | QUIC_CONN_ID *id_to_use = NULL; |
1957 | | |
1958 | | /* |
1959 | | * We need to select which connection id to encode in the |
1960 | | * QUIC_TPARAM_ORIG_DCID transport parameter |
1961 | | * If we have an odcid, then this connection was established |
1962 | | * in response to a retry request, and we need to use the connection |
1963 | | * id sent in the first initial packet. |
1964 | | * If we don't have an odcid, then this connection was established |
1965 | | * without a retry and the init_dcid is the connection we should use |
1966 | | */ |
1967 | 0 | if (ch->odcid.id_len == 0) |
1968 | 0 | id_to_use = &ch->init_dcid; |
1969 | 0 | else |
1970 | 0 | id_to_use = &ch->odcid; |
1971 | |
|
1972 | 0 | if (ch->local_transport_params != NULL || ch->got_local_transport_params) |
1973 | 0 | goto err; |
1974 | | |
1975 | 0 | if ((buf_mem = BUF_MEM_new()) == NULL) |
1976 | 0 | goto err; |
1977 | | |
1978 | 0 | if (!WPACKET_init(&wpkt, buf_mem)) |
1979 | 0 | goto err; |
1980 | | |
1981 | 0 | wpkt_valid = 1; |
1982 | |
|
1983 | 0 | if (ch->tx_disable_active_migration != 0 && ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION, NULL, 0) == NULL) |
1984 | 0 | goto err; |
1985 | | |
1986 | 0 | if (ch->is_server) { |
1987 | 0 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_ORIG_DCID, |
1988 | 0 | id_to_use)) |
1989 | 0 | goto err; |
1990 | | |
1991 | 0 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_INITIAL_SCID, |
1992 | 0 | &ch->cur_local_cid)) |
1993 | 0 | goto err; |
1994 | 0 | if (ch->odcid.id_len != 0) |
1995 | 0 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, |
1996 | 0 | QUIC_TPARAM_RETRY_SCID, |
1997 | 0 | &ch->init_dcid)) |
1998 | 0 | goto err; |
1999 | 0 | } else { |
2000 | 0 | if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_INITIAL_SCID, |
2001 | 0 | &ch->init_scid)) |
2002 | 0 | goto err; |
2003 | 0 | } |
2004 | | |
2005 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT, |
2006 | 0 | ch->max_idle_timeout_local_req)) |
2007 | 0 | goto err; |
2008 | | |
2009 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE, |
2010 | 0 | ch->tx_max_udp_payload_size)) |
2011 | 0 | goto err; |
2012 | | |
2013 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT, |
2014 | 0 | ch->tx_active_conn_id_limit)) |
2015 | 0 | goto err; |
2016 | | |
2017 | 0 | if (ch->tx_ack_delay_exp != QUIC_DEFAULT_ACK_DELAY_EXP |
2018 | 0 | && !ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACK_DELAY_EXP, |
2019 | 0 | ch->tx_ack_delay_exp)) |
2020 | 0 | goto err; |
2021 | | |
2022 | 0 | if (ch->tx_max_ack_delay != QUIC_DEFAULT_MAX_ACK_DELAY |
2023 | 0 | && !ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_ACK_DELAY, |
2024 | 0 | ch->tx_max_ack_delay)) |
2025 | 0 | goto err; |
2026 | | |
2027 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA, |
2028 | 0 | ossl_quic_rxfc_get_cwm(&ch->conn_rxfc))) |
2029 | 0 | goto err; |
2030 | | |
2031 | | /* Send the default CWM for a new RXFC. */ |
2032 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, |
2033 | 0 | ch->tx_init_max_stream_data_bidi_local)) |
2034 | 0 | goto err; |
2035 | | |
2036 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, |
2037 | 0 | ch->tx_init_max_stream_data_bidi_remote)) |
2038 | 0 | goto err; |
2039 | | |
2040 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI, |
2041 | 0 | ch->tx_init_max_stream_data_uni)) |
2042 | 0 | goto err; |
2043 | | |
2044 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI, |
2045 | 0 | ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc))) |
2046 | 0 | goto err; |
2047 | | |
2048 | 0 | if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI, |
2049 | 0 | ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc))) |
2050 | 0 | goto err; |
2051 | | |
2052 | 0 | if (!WPACKET_finish(&wpkt)) |
2053 | 0 | goto err; |
2054 | | |
2055 | 0 | wpkt_valid = 0; |
2056 | |
|
2057 | 0 | if (!WPACKET_get_total_written(&wpkt, &buf_len)) |
2058 | 0 | goto err; |
2059 | | |
2060 | 0 | ch->local_transport_params = (unsigned char *)buf_mem->data; |
2061 | 0 | buf_mem->data = NULL; |
2062 | |
|
2063 | 0 | if (!ossl_quic_tls_set_transport_params(ch->qtls, ch->local_transport_params, |
2064 | 0 | buf_len)) |
2065 | 0 | goto err; |
2066 | | |
2067 | 0 | #ifndef OPENSSL_NO_QLOG |
2068 | 0 | QLOG_EVENT_BEGIN(ch_get_qlog(ch), transport, parameters_set) |
2069 | 0 | QLOG_STR("owner", "local"); |
2070 | 0 | QLOG_BOOL("disable_active_migration", ch->tx_disable_active_migration); |
2071 | 0 | if (ch->is_server) { |
2072 | 0 | QLOG_CID("original_destination_connection_id", &ch->init_dcid); |
2073 | 0 | QLOG_CID("initial_source_connection_id", &ch->cur_local_cid); |
2074 | 0 | } else { |
2075 | 0 | QLOG_STR("initial_source_connection_id", ""); |
2076 | 0 | } |
2077 | 0 | QLOG_U64("max_idle_timeout", ch->max_idle_timeout_local_req); |
2078 | 0 | QLOG_U64("max_udp_payload_size", ch->tx_max_udp_payload_size); |
2079 | 0 | QLOG_U64("active_connection_id_limit", ch->tx_active_conn_id_limit); |
2080 | 0 | if (ch->tx_ack_delay_exp != QUIC_DEFAULT_ACK_DELAY_EXP) |
2081 | 0 | QLOG_U64("ack_delay_exponent", ch->tx_ack_delay_exp); |
2082 | 0 | if (ch->tx_max_ack_delay != QUIC_DEFAULT_MAX_ACK_DELAY) |
2083 | 0 | QLOG_U64("max_ack_delay", ch->tx_max_ack_delay); |
2084 | 0 | QLOG_U64("initial_max_data", ossl_quic_rxfc_get_cwm(&ch->conn_rxfc)); |
2085 | 0 | QLOG_U64("initial_max_stream_data_bidi_local", |
2086 | 0 | ch->tx_init_max_stream_data_bidi_local); |
2087 | 0 | QLOG_U64("initial_max_stream_data_bidi_remote", |
2088 | 0 | ch->tx_init_max_stream_data_bidi_remote); |
2089 | 0 | QLOG_U64("initial_max_stream_data_uni", |
2090 | 0 | ch->tx_init_max_stream_data_uni); |
2091 | 0 | QLOG_U64("initial_max_streams_bidi", |
2092 | 0 | ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc)); |
2093 | 0 | QLOG_U64("initial_max_streams_uni", |
2094 | 0 | ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc)); |
2095 | 0 | QLOG_EVENT_END() |
2096 | 0 | #endif |
2097 | |
|
2098 | 0 | ch->got_local_transport_params = 1; |
2099 | |
|
2100 | 0 | ok = 1; |
2101 | 0 | err: |
2102 | 0 | if (wpkt_valid) |
2103 | 0 | WPACKET_cleanup(&wpkt); |
2104 | 0 | BUF_MEM_free(buf_mem); |
2105 | 0 | return ok; |
2106 | 0 | } |
2107 | | |
2108 | | /* |
2109 | | * QUIC Channel: Ticker-Mutator |
2110 | | * ============================ |
2111 | | */ |
2112 | | |
2113 | | /* |
2114 | | * The central ticker function called by the reactor. This does everything, or |
2115 | | * at least everything network I/O related. Best effort - not allowed to fail |
2116 | | * "loudly". |
2117 | | */ |
2118 | | void ossl_quic_channel_subtick(QUIC_CHANNEL *ch, QUIC_TICK_RESULT *res, |
2119 | | uint32_t flags) |
2120 | 0 | { |
2121 | 0 | OSSL_TIME now, deadline; |
2122 | 0 | int channel_only = (flags & QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY) != 0; |
2123 | 0 | int notify_other_threads = 0; |
2124 | | |
2125 | | /* |
2126 | | * When we tick the QUIC connection, we do everything we need to do |
2127 | | * periodically. Network I/O handling will already have been performed |
2128 | | * as necessary by the QUIC port. Thus, in order, we: |
2129 | | * |
2130 | | * - handle any packets the DEMUX has queued up for us; |
2131 | | * - handle any timer events which are due to fire (ACKM, etc.); |
2132 | | * - generate any packets which need to be sent; |
2133 | | * - determine the time at which we should next be ticked. |
2134 | | */ |
2135 | | |
2136 | | /* |
2137 | | * If the connection has not yet started, or we are in the TERMINATED state, |
2138 | | * there is nothing to do. |
2139 | | */ |
2140 | 0 | if (ch->state == QUIC_CHANNEL_STATE_IDLE |
2141 | 0 | || ossl_quic_channel_is_terminated(ch)) { |
2142 | 0 | res->net_read_desired = 0; |
2143 | 0 | res->net_write_desired = 0; |
2144 | 0 | res->notify_other_threads = 0; |
2145 | 0 | res->tick_deadline = ossl_time_infinite(); |
2146 | 0 | return; |
2147 | 0 | } |
2148 | | |
2149 | | /* |
2150 | | * If we are in the TERMINATING state, check if the terminating timer has |
2151 | | * expired. |
2152 | | */ |
2153 | 0 | if (ossl_quic_channel_is_terminating(ch)) { |
2154 | 0 | now = get_time(ch); |
2155 | |
|
2156 | 0 | if (ossl_time_compare(now, ch->terminate_deadline) >= 0) { |
2157 | 0 | ch_on_terminating_timeout(ch); |
2158 | 0 | res->net_read_desired = 0; |
2159 | 0 | res->net_write_desired = 0; |
2160 | 0 | res->notify_other_threads = 1; |
2161 | 0 | res->tick_deadline = ossl_time_infinite(); |
2162 | 0 | return; /* abort normal processing, nothing to do */ |
2163 | 0 | } |
2164 | 0 | } |
2165 | | |
2166 | 0 | if (!ch->port->engine->inhibit_tick) { |
2167 | | /* Handle RXKU timeouts. */ |
2168 | 0 | ch_rxku_tick(ch); |
2169 | |
|
2170 | 0 | do { |
2171 | | /* Process queued incoming packets. */ |
2172 | 0 | ch->did_tls_tick = 0; |
2173 | 0 | ch->have_new_rx_secret = 0; |
2174 | 0 | ch_rx(ch, channel_only, ¬ify_other_threads); |
2175 | | |
2176 | | /* |
2177 | | * Allow the handshake layer to check for any new incoming data and |
2178 | | * generate new outgoing data. |
2179 | | */ |
2180 | 0 | if (!ch->did_tls_tick) |
2181 | 0 | ch_tick_tls(ch, channel_only, ¬ify_other_threads); |
2182 | | |
2183 | | /* |
2184 | | * If the handshake layer gave us a new secret, we need to do RX |
2185 | | * again because packets that were not previously processable and |
2186 | | * were deferred might now be processable. |
2187 | | * |
2188 | | * TODO(QUIC FUTURE): Consider handling this in the yield_secret callback. |
2189 | | */ |
2190 | 0 | } while (ch->have_new_rx_secret); |
2191 | 0 | } |
2192 | | |
2193 | | /* |
2194 | | * Handle any timer events which are due to fire; namely, the loss |
2195 | | * detection deadline and the idle timeout. |
2196 | | * |
2197 | | * ACKM ACK generation deadline is polled by TXP, so we don't need to |
2198 | | * handle it here. |
2199 | | */ |
2200 | 0 | now = get_time(ch); |
2201 | 0 | if (ossl_time_compare(now, ch->idle_deadline) >= 0) { |
2202 | | /* |
2203 | | * Idle timeout differs from normal protocol violation because we do |
2204 | | * not send a CONN_CLOSE frame; go straight to TERMINATED. |
2205 | | */ |
2206 | 0 | if (!ch->port->engine->inhibit_tick) |
2207 | 0 | ch_on_idle_timeout(ch); |
2208 | |
|
2209 | 0 | res->net_read_desired = 0; |
2210 | 0 | res->net_write_desired = 0; |
2211 | 0 | res->notify_other_threads = 1; |
2212 | 0 | res->tick_deadline = ossl_time_infinite(); |
2213 | 0 | return; |
2214 | 0 | } |
2215 | | |
2216 | 0 | if (!ch->port->engine->inhibit_tick) { |
2217 | 0 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); |
2218 | 0 | if (!ossl_time_is_zero(deadline) |
2219 | 0 | && ossl_time_compare(now, deadline) >= 0) |
2220 | 0 | ossl_ackm_on_timeout(ch->ackm); |
2221 | | |
2222 | | /* If a ping is due, inform TXP. */ |
2223 | 0 | if (ossl_time_compare(now, ch->ping_deadline) >= 0) { |
2224 | 0 | int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level); |
2225 | |
|
2226 | 0 | ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space); |
2227 | | |
2228 | | /* |
2229 | | * If we have no CC budget at this time we cannot process the above |
2230 | | * PING request immediately. In any case we have scheduled the |
2231 | | * request so bump the ping deadline. If we don't do this we will |
2232 | | * busy-loop endlessly as the above deadline comparison condition |
2233 | | * will still be met. |
2234 | | */ |
2235 | 0 | ch_update_ping_deadline(ch); |
2236 | 0 | } |
2237 | | |
2238 | | /* Queue any data to be sent for transmission. */ |
2239 | 0 | ch_tx(ch, ¬ify_other_threads); |
2240 | | |
2241 | | /* Do stream GC. */ |
2242 | 0 | ossl_quic_stream_map_gc(&ch->qsm); |
2243 | 0 | } |
2244 | | |
2245 | | /* Determine the time at which we should next be ticked. */ |
2246 | 0 | res->tick_deadline = ch_determine_next_tick_deadline(ch); |
2247 | | |
2248 | | /* |
2249 | | * Always process network input unless we are now terminated. Although we |
2250 | | * had not terminated at the beginning of this tick, network errors in |
2251 | | * ch_tx() may have caused us to transition to the Terminated state. |
2252 | | */ |
2253 | 0 | res->net_read_desired = !ossl_quic_channel_is_terminated(ch); |
2254 | | |
2255 | | /* We want to write to the network if we have any data in our TX queue. */ |
2256 | 0 | res->net_write_desired |
2257 | 0 | = (!ossl_quic_channel_is_terminated(ch) |
2258 | 0 | && ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0); |
2259 | |
|
2260 | 0 | res->notify_other_threads = notify_other_threads; |
2261 | 0 | } |
2262 | | |
2263 | | static int ch_tick_tls(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads) |
2264 | 0 | { |
2265 | 0 | uint64_t error_code; |
2266 | 0 | const char *error_msg; |
2267 | 0 | ERR_STATE *error_state = NULL; |
2268 | |
|
2269 | 0 | if (channel_only) |
2270 | 0 | return 1; |
2271 | | |
2272 | 0 | ch->did_tls_tick = 1; |
2273 | 0 | ossl_quic_tls_tick(ch->qtls); |
2274 | |
|
2275 | 0 | if (ossl_quic_tls_get_error(ch->qtls, &error_code, &error_msg, |
2276 | 0 | &error_state)) { |
2277 | 0 | ossl_quic_channel_raise_protocol_error_state(ch, error_code, 0, |
2278 | 0 | error_msg, error_state); |
2279 | 0 | if (notify_other_threads != NULL) |
2280 | 0 | *notify_other_threads = 1; |
2281 | |
|
2282 | 0 | return 0; |
2283 | 0 | } |
2284 | | |
2285 | 0 | return 1; |
2286 | 0 | } |
2287 | | |
2288 | | /* Check incoming forged packet limit and terminate connection if needed. */ |
2289 | | static void ch_rx_check_forged_pkt_limit(QUIC_CHANNEL *ch) |
2290 | 0 | { |
2291 | 0 | uint32_t enc_level; |
2292 | 0 | uint64_t limit = UINT64_MAX, l; |
2293 | |
|
2294 | 0 | for (enc_level = QUIC_ENC_LEVEL_INITIAL; |
2295 | 0 | enc_level < QUIC_ENC_LEVEL_NUM; |
2296 | 0 | ++enc_level) { |
2297 | | /* |
2298 | | * Different ELs can have different AEADs which can in turn impose |
2299 | | * different limits, so use the lowest value of any currently valid EL. |
2300 | | */ |
2301 | 0 | if ((ch->el_discarded & (1U << enc_level)) != 0) |
2302 | 0 | continue; |
2303 | | |
2304 | 0 | if (enc_level > ch->rx_enc_level) |
2305 | 0 | break; |
2306 | | |
2307 | 0 | l = ossl_qrx_get_max_forged_pkt_count(ch->qrx, enc_level); |
2308 | 0 | if (l < limit) |
2309 | 0 | limit = l; |
2310 | 0 | } |
2311 | |
|
2312 | 0 | if (ossl_qrx_get_cur_forged_pkt_count(ch->qrx) < limit) |
2313 | 0 | return; |
2314 | | |
2315 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_AEAD_LIMIT_REACHED, 0, |
2316 | 0 | "forgery limit"); |
2317 | 0 | } |
2318 | | |
2319 | | void ossl_ch_reset_rx_state(QUIC_CHANNEL *ch) |
2320 | 0 | { |
2321 | 0 | ch->did_crypto_frame = 0; |
2322 | 0 | ch->seen_path_challenge = 0; |
2323 | 0 | } |
2324 | | |
2325 | | /* Process queued incoming packets and handle frames, if any. */ |
2326 | | static int ch_rx(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads) |
2327 | 0 | { |
2328 | 0 | int handled_any = 0; |
2329 | 0 | const int closing = ossl_quic_channel_is_closing(ch); |
2330 | |
|
2331 | 0 | if (!ch->is_server && !ch->have_sent_any_pkt) |
2332 | | /* |
2333 | | * We have not sent anything yet, therefore there is no need to check |
2334 | | * for incoming data. |
2335 | | */ |
2336 | 0 | return 1; |
2337 | | |
2338 | 0 | for (;;) { |
2339 | 0 | assert(ch->qrx_pkt == NULL); |
2340 | |
|
2341 | 0 | if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt)) |
2342 | 0 | break; |
2343 | | |
2344 | | /* Track the amount of data received while in the closing state */ |
2345 | 0 | if (closing) |
2346 | 0 | ossl_quic_tx_packetiser_record_received_closing_bytes( |
2347 | 0 | ch->txp, ch->qrx_pkt->hdr->len); |
2348 | |
|
2349 | 0 | if (!handled_any) { |
2350 | 0 | ch_update_idle(ch); |
2351 | 0 | ch_update_ping_deadline(ch); |
2352 | 0 | } |
2353 | |
|
2354 | 0 | ch_rx_handle_packet(ch, channel_only); /* best effort */ |
2355 | | |
2356 | | /* |
2357 | | * Regardless of the outcome of frame handling, unref the packet. |
2358 | | * This will free the packet unless something added another |
2359 | | * reference to it during frame processing. |
2360 | | */ |
2361 | 0 | ossl_qrx_pkt_release(ch->qrx_pkt); |
2362 | 0 | ch->qrx_pkt = NULL; |
2363 | |
|
2364 | 0 | ch->have_sent_ack_eliciting_since_rx = 0; |
2365 | 0 | handled_any = 1; |
2366 | 0 | } |
2367 | |
|
2368 | 0 | ch_rx_check_forged_pkt_limit(ch); |
2369 | |
|
2370 | 0 | if (handled_any && notify_other_threads != NULL) |
2371 | 0 | *notify_other_threads = 1; |
2372 | | |
2373 | | /* |
2374 | | * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we |
2375 | | * process one or more incoming packets. |
2376 | | */ |
2377 | 0 | if (handled_any && closing) |
2378 | 0 | ch->conn_close_queued = 1; |
2379 | |
|
2380 | 0 | return 1; |
2381 | 0 | } |
2382 | | |
2383 | | static int bio_addr_eq(const BIO_ADDR *a, const BIO_ADDR *b) |
2384 | 0 | { |
2385 | 0 | if (BIO_ADDR_family(a) != BIO_ADDR_family(b)) |
2386 | 0 | return 0; |
2387 | | |
2388 | 0 | switch (BIO_ADDR_family(a)) { |
2389 | 0 | case AF_INET: |
2390 | 0 | return !memcmp(&a->s_in.sin_addr, |
2391 | 0 | &b->s_in.sin_addr, |
2392 | 0 | sizeof(a->s_in.sin_addr)) |
2393 | 0 | && a->s_in.sin_port == b->s_in.sin_port; |
2394 | 0 | #if OPENSSL_USE_IPV6 |
2395 | 0 | case AF_INET6: |
2396 | 0 | return !memcmp(&a->s_in6.sin6_addr, |
2397 | 0 | &b->s_in6.sin6_addr, |
2398 | 0 | sizeof(a->s_in6.sin6_addr)) |
2399 | 0 | && a->s_in6.sin6_port == b->s_in6.sin6_port; |
2400 | 0 | #endif |
2401 | 0 | default: |
2402 | 0 | return 0; /* not supported */ |
2403 | 0 | } |
2404 | | |
2405 | 0 | return 1; |
2406 | 0 | } |
2407 | | |
2408 | | /* Handles the packet currently in ch->qrx_pkt->hdr. */ |
2409 | | static void ch_rx_handle_packet(QUIC_CHANNEL *ch, int channel_only) |
2410 | 0 | { |
2411 | 0 | uint32_t enc_level; |
2412 | 0 | int old_have_processed_any_pkt = ch->have_processed_any_pkt; |
2413 | 0 | OSSL_QTX_IOVEC iovec; |
2414 | 0 | PACKET vpkt; |
2415 | 0 | unsigned long supported_ver; |
2416 | |
|
2417 | 0 | assert(ch->qrx_pkt != NULL); |
2418 | | |
2419 | | /* |
2420 | | * RFC 9000 s. 10.2.1 Closing Connection State: |
2421 | | * An endpoint that is closing is not required to process any |
2422 | | * received frame. |
2423 | | */ |
2424 | 0 | if (!ossl_quic_channel_is_active(ch)) |
2425 | 0 | return; |
2426 | | |
2427 | 0 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) { |
2428 | 0 | if (!ch->have_received_enc_pkt) { |
2429 | 0 | ch->cur_remote_dcid = ch->init_scid = ch->qrx_pkt->hdr->src_conn_id; |
2430 | 0 | ch->have_received_enc_pkt = 1; |
2431 | | |
2432 | | /* |
2433 | | * We change to using the SCID in the first Initial packet as the |
2434 | | * DCID. |
2435 | | */ |
2436 | 0 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid); |
2437 | 0 | } |
2438 | |
|
2439 | 0 | enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type); |
2440 | 0 | if ((ch->el_discarded & (1U << enc_level)) != 0) |
2441 | | /* Do not process packets from ELs we have already discarded. */ |
2442 | 0 | return; |
2443 | 0 | } |
2444 | | |
2445 | | /* |
2446 | | * RFC 9000 s. 9.6: "If a client receives packets from a new server address |
2447 | | * when the client has not initiated a migration to that address, the client |
2448 | | * SHOULD discard these packets." |
2449 | | * |
2450 | | * We need to be a bit careful here as due to the BIO abstraction layer an |
2451 | | * application is liable to be weird and lie to us about peer addresses. |
2452 | | * Only apply this check if we actually are using a real AF_INET or AF_INET6 |
2453 | | * address. |
2454 | | */ |
2455 | 0 | if (!ch->is_server |
2456 | 0 | && ch->qrx_pkt->peer != NULL |
2457 | 0 | && (BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET |
2458 | 0 | #if OPENSSL_USE_IPV6 |
2459 | 0 | || BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET6 |
2460 | 0 | #endif |
2461 | 0 | ) |
2462 | 0 | && !bio_addr_eq(ch->qrx_pkt->peer, &ch->cur_peer_addr)) |
2463 | 0 | return; |
2464 | | |
2465 | 0 | if (!ch->is_server |
2466 | 0 | && ch->have_received_enc_pkt |
2467 | 0 | && ossl_quic_pkt_type_has_scid(ch->qrx_pkt->hdr->type)) { |
2468 | | /* |
2469 | | * RFC 9000 s. 7.2: "Once a client has received a valid Initial packet |
2470 | | * from the server, it MUST discard any subsequent packet it receives on |
2471 | | * that connection with a different SCID." |
2472 | | */ |
2473 | 0 | if (!ossl_quic_conn_id_eq(&ch->qrx_pkt->hdr->src_conn_id, |
2474 | 0 | &ch->init_scid)) |
2475 | 0 | return; |
2476 | 0 | } |
2477 | | |
2478 | 0 | if (ossl_quic_pkt_type_has_version(ch->qrx_pkt->hdr->type) |
2479 | 0 | && ch->qrx_pkt->hdr->version != QUIC_VERSION_1) |
2480 | | /* |
2481 | | * RFC 9000 s. 5.2.1: If a client receives a packet that uses a |
2482 | | * different version than it initially selected, it MUST discard the |
2483 | | * packet. We only ever use v1, so require it. |
2484 | | */ |
2485 | 0 | return; |
2486 | | |
2487 | 0 | if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_VERSION_NEG) { |
2488 | | |
2489 | | /* |
2490 | | * Sanity check. Version negotiation packet MUST have a version |
2491 | | * value of 0 according to the RFC. We must discard such packets |
2492 | | */ |
2493 | 0 | if (ch->qrx_pkt->hdr->version != 0) |
2494 | 0 | return; |
2495 | | |
2496 | | /* |
2497 | | * RFC 9000 s. 6.2: If a client receives a version negotiation |
2498 | | * packet, we need to do the following: |
2499 | | * a) If the negotiation packet lists the version we initially sent |
2500 | | * then we must abandon this connection attempt |
2501 | | * b) We have to select a version from the list provided in the |
2502 | | * version negotiation packet, and retry the connection attempt |
2503 | | * in much the same way that ch_retry does, but we can reuse the |
2504 | | * connection id values |
2505 | | */ |
2506 | | |
2507 | 0 | if (old_have_processed_any_pkt == 1) { |
2508 | | /* |
2509 | | * We've gotten previous packets, need to discard this. |
2510 | | */ |
2511 | 0 | return; |
2512 | 0 | } |
2513 | | |
2514 | | /* |
2515 | | * Indicate that we have processed a packet, as any subsequently |
2516 | | * received version negotiation packet must be discarded above |
2517 | | */ |
2518 | 0 | ch->have_processed_any_pkt = 1; |
2519 | | |
2520 | | /* |
2521 | | * Following the header, version negotiation packets |
2522 | | * contain an array of 32 bit integers representing |
2523 | | * the supported versions that the server honors |
2524 | | * this array, bounded by the hdr->len field |
2525 | | * needs to be traversed so that we can find a matching |
2526 | | * version |
2527 | | */ |
2528 | 0 | if (!PACKET_buf_init(&vpkt, ch->qrx_pkt->hdr->data, |
2529 | 0 | ch->qrx_pkt->hdr->len)) |
2530 | 0 | return; |
2531 | | |
2532 | 0 | while (PACKET_remaining(&vpkt) > 0) { |
2533 | | /* |
2534 | | * We only support quic version 1 at the moment, so |
2535 | | * look to see if that's offered |
2536 | | */ |
2537 | 0 | if (!PACKET_get_net_4(&vpkt, &supported_ver)) |
2538 | 0 | return; |
2539 | | |
2540 | 0 | if (supported_ver == QUIC_VERSION_1) { |
2541 | | /* |
2542 | | * If the server supports version 1, set it as |
2543 | | * the packetisers version |
2544 | | */ |
2545 | 0 | ossl_quic_tx_packetiser_set_protocol_version(ch->txp, QUIC_VERSION_1); |
2546 | | |
2547 | | /* |
2548 | | * And then request a restart of the QUIC connection |
2549 | | */ |
2550 | 0 | if (!ch_restart(ch)) |
2551 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
2552 | 0 | OSSL_QUIC_ERR_INTERNAL_ERROR, |
2553 | 0 | 0, "handling ver negotiation packet"); |
2554 | 0 | return; |
2555 | 0 | } |
2556 | 0 | } |
2557 | | |
2558 | | /* |
2559 | | * If we get here, then the server doesn't support a version of the |
2560 | | * protocol that we can handle, abandon the connection |
2561 | | */ |
2562 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CONNECTION_REFUSED, |
2563 | 0 | 0, "unsupported protocol version"); |
2564 | 0 | return; |
2565 | 0 | } |
2566 | | |
2567 | 0 | ch->have_processed_any_pkt = 1; |
2568 | | |
2569 | | /* |
2570 | | * RFC 9000 s. 17.2: "An endpoint MUST treat receipt of a packet that has a |
2571 | | * non-zero value for [the reserved bits] after removing both packet and |
2572 | | * header protection as a connection error of type PROTOCOL_VIOLATION." |
2573 | | */ |
2574 | 0 | if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type) |
2575 | 0 | && ch->qrx_pkt->hdr->reserved != 0) { |
2576 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
2577 | 0 | 0, "packet header reserved bits"); |
2578 | 0 | return; |
2579 | 0 | } |
2580 | | |
2581 | 0 | iovec.buf = ch->qrx_pkt->hdr->data; |
2582 | 0 | iovec.buf_len = ch->qrx_pkt->hdr->len; |
2583 | 0 | ossl_qlog_event_transport_packet_received(ch_get_qlog(ch), ch->qrx_pkt->hdr, |
2584 | 0 | ch->qrx_pkt->pn, &iovec, 1, |
2585 | 0 | ch->qrx_pkt->datagram_id); |
2586 | | |
2587 | | /* Handle incoming packet. */ |
2588 | 0 | switch (ch->qrx_pkt->hdr->type) { |
2589 | 0 | case QUIC_PKT_TYPE_RETRY: |
2590 | 0 | if (ch->doing_retry || ch->is_server) |
2591 | | /* |
2592 | | * It is not allowed to ask a client to do a retry more than |
2593 | | * once. Clients may not send retries. |
2594 | | */ |
2595 | 0 | return; |
2596 | | |
2597 | | /* |
2598 | | * RFC 9000 s 17.2.5.2: After the client has received and processed an |
2599 | | * Initial or Retry packet from the server, it MUST discard any |
2600 | | * subsequent Retry packets that it receives. |
2601 | | */ |
2602 | 0 | if (ch->have_received_enc_pkt) |
2603 | 0 | return; |
2604 | | |
2605 | 0 | if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN) |
2606 | | /* Packets with zero-length Retry Tokens are invalid. */ |
2607 | 0 | return; |
2608 | | |
2609 | | /* |
2610 | | * TODO(QUIC FUTURE): Theoretically this should probably be in the QRX. |
2611 | | * However because validation is dependent on context (namely the |
2612 | | * client's initial DCID) we can't do this cleanly. In the future we |
2613 | | * should probably add a callback to the QRX to let it call us (via |
2614 | | * the DEMUX) and ask us about the correct original DCID, rather |
2615 | | * than allow the QRX to emit a potentially malformed packet to the |
2616 | | * upper layers. However, special casing this will do for now. |
2617 | | */ |
2618 | 0 | if (!ossl_quic_validate_retry_integrity_tag(ch->port->engine->libctx, |
2619 | 0 | ch->port->engine->propq, |
2620 | 0 | ch->qrx_pkt->hdr, |
2621 | 0 | &ch->init_dcid)) |
2622 | | /* Malformed retry packet, ignore. */ |
2623 | 0 | return; |
2624 | | |
2625 | 0 | if (!ch_retry(ch, ch->qrx_pkt->hdr->data, |
2626 | 0 | ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN, |
2627 | 0 | &ch->qrx_pkt->hdr->src_conn_id)) |
2628 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, |
2629 | 0 | 0, "handling retry packet"); |
2630 | 0 | break; |
2631 | | |
2632 | 0 | case QUIC_PKT_TYPE_0RTT: |
2633 | 0 | if (!ch->is_server) |
2634 | | /* Clients should never receive 0-RTT packets. */ |
2635 | 0 | return; |
2636 | | |
2637 | | /* |
2638 | | * TODO(QUIC 0RTT): Implement 0-RTT on the server side. We currently |
2639 | | * do not need to implement this as a client can only do 0-RTT if we |
2640 | | * have given it permission to in a previous session. |
2641 | | */ |
2642 | 0 | break; |
2643 | | |
2644 | 0 | case QUIC_PKT_TYPE_INITIAL: |
2645 | 0 | case QUIC_PKT_TYPE_HANDSHAKE: |
2646 | 0 | case QUIC_PKT_TYPE_1RTT: |
2647 | 0 | if (ch->is_server && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE) |
2648 | | /* |
2649 | | * We automatically drop INITIAL EL keys when first successfully |
2650 | | * decrypting a HANDSHAKE packet, as per the RFC. |
2651 | | */ |
2652 | 0 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); |
2653 | |
|
2654 | 0 | if (ch->rxku_in_progress |
2655 | 0 | && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_1RTT |
2656 | 0 | && ch->qrx_pkt->pn >= ch->rxku_trigger_pn |
2657 | 0 | && ch->qrx_pkt->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)) { |
2658 | | /* |
2659 | | * RFC 9001 s. 6.4: Packets with higher packet numbers MUST be |
2660 | | * protected with either the same or newer packet protection keys |
2661 | | * than packets with lower packet numbers. An endpoint that |
2662 | | * successfully removes protection with old keys when newer keys |
2663 | | * were used for packets with lower packet numbers MUST treat this |
2664 | | * as a connection error of type KEY_UPDATE_ERROR. |
2665 | | */ |
2666 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR, |
2667 | 0 | 0, "new packet with old keys"); |
2668 | 0 | break; |
2669 | 0 | } |
2670 | | |
2671 | 0 | if (!ch->is_server |
2672 | 0 | && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL |
2673 | 0 | && ch->qrx_pkt->hdr->token_len > 0) { |
2674 | | /* |
2675 | | * RFC 9000 s. 17.2.2: Clients that receive an Initial packet with a |
2676 | | * non-zero Token Length field MUST either discard the packet or |
2677 | | * generate a connection error of type PROTOCOL_VIOLATION. |
2678 | | * |
2679 | | * TODO(QUIC FUTURE): consider the implications of RFC 9000 s. 10.2.3 |
2680 | | * Immediate Close during the Handshake: |
2681 | | * However, at the cost of reducing feedback about |
2682 | | * errors for legitimate peers, some forms of denial of |
2683 | | * service can be made more difficult for an attacker |
2684 | | * if endpoints discard illegal packets rather than |
2685 | | * terminating a connection with CONNECTION_CLOSE. For |
2686 | | * this reason, endpoints MAY discard packets rather |
2687 | | * than immediately close if errors are detected in |
2688 | | * packets that lack authentication. |
2689 | | * I.e. should we drop this packet instead of closing the connection? |
2690 | | */ |
2691 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
2692 | 0 | 0, "client received initial token"); |
2693 | 0 | break; |
2694 | 0 | } |
2695 | | |
2696 | | /* This packet contains frames, pass to the RXDP. */ |
2697 | 0 | ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */ |
2698 | |
|
2699 | 0 | if (ch->did_crypto_frame) |
2700 | 0 | ch_tick_tls(ch, channel_only, NULL); |
2701 | |
|
2702 | 0 | break; |
2703 | | |
2704 | 0 | default: |
2705 | 0 | assert(0); |
2706 | 0 | break; |
2707 | 0 | } |
2708 | 0 | } |
2709 | | |
2710 | | /* Try to generate packets and if possible, flush them to the network. */ |
2711 | | static int ch_tx(QUIC_CHANNEL *ch, int *notify_other_threads) |
2712 | 0 | { |
2713 | 0 | QUIC_TXP_STATUS status; |
2714 | 0 | int res; |
2715 | | |
2716 | | /* |
2717 | | * RFC 9000 s. 10.2.2: Draining Connection State: |
2718 | | * While otherwise identical to the closing state, an endpoint |
2719 | | * in the draining state MUST NOT send any packets. |
2720 | | * and: |
2721 | | * An endpoint MUST NOT send further packets. |
2722 | | */ |
2723 | 0 | if (ossl_quic_channel_is_draining(ch)) |
2724 | 0 | return 0; |
2725 | | |
2726 | 0 | if (ossl_quic_channel_is_closing(ch)) { |
2727 | | /* |
2728 | | * While closing, only send CONN_CLOSE if we've received more traffic |
2729 | | * from the peer. Once we tell the TXP to generate CONN_CLOSE, all |
2730 | | * future calls to it generate CONN_CLOSE frames, so otherwise we would |
2731 | | * just constantly generate CONN_CLOSE frames. |
2732 | | * |
2733 | | * Confirming to RFC 9000 s. 10.2.1 Closing Connection State: |
2734 | | * An endpoint SHOULD limit the rate at which it generates |
2735 | | * packets in the closing state. |
2736 | | */ |
2737 | 0 | if (!ch->conn_close_queued) |
2738 | 0 | return 0; |
2739 | | |
2740 | 0 | ch->conn_close_queued = 0; |
2741 | 0 | } |
2742 | | |
2743 | | /* Do TXKU if we need to. */ |
2744 | 0 | ch_maybe_trigger_spontaneous_txku(ch); |
2745 | |
|
2746 | 0 | ch->rxku_pending_confirm_done = 0; |
2747 | | |
2748 | | /* Loop until we stop generating packets to send */ |
2749 | 0 | do { |
2750 | | /* |
2751 | | * Send packet, if we need to. Best effort. The TXP consults the CC and |
2752 | | * applies any limitations imposed by it, so we don't need to do it here. |
2753 | | * |
2754 | | * Best effort. In particular if TXP fails for some reason we should |
2755 | | * still flush any queued packets which we already generated. |
2756 | | */ |
2757 | 0 | res = ossl_quic_tx_packetiser_generate(ch->txp, &status); |
2758 | 0 | if (status.sent_pkt > 0) { |
2759 | 0 | ch->have_sent_any_pkt = 1; /* Packet(s) were sent */ |
2760 | 0 | ch->port->have_sent_any_pkt = 1; |
2761 | | |
2762 | | /* |
2763 | | * RFC 9000 s. 10.1. 'An endpoint also restarts its idle timer when |
2764 | | * sending an ack-eliciting packet if no other ack-eliciting packets |
2765 | | * have been sent since last receiving and processing a packet.' |
2766 | | */ |
2767 | 0 | if (status.sent_ack_eliciting |
2768 | 0 | && !ch->have_sent_ack_eliciting_since_rx) { |
2769 | 0 | ch_update_idle(ch); |
2770 | 0 | ch->have_sent_ack_eliciting_since_rx = 1; |
2771 | 0 | } |
2772 | |
|
2773 | 0 | if (!ch->is_server && status.sent_handshake) |
2774 | | /* |
2775 | | * RFC 9001 s. 4.9.1: A client MUST discard Initial keys when it |
2776 | | * first sends a Handshake packet. |
2777 | | */ |
2778 | 0 | ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL); |
2779 | |
|
2780 | 0 | if (ch->rxku_pending_confirm_done) |
2781 | 0 | ch->rxku_pending_confirm = 0; |
2782 | |
|
2783 | 0 | ch_update_ping_deadline(ch); |
2784 | 0 | } |
2785 | |
|
2786 | 0 | if (!res) { |
2787 | | /* |
2788 | | * One case where TXP can fail is if we reach a TX PN of 2**62 - 1. |
2789 | | * As per RFC 9000 s. 12.3, if this happens we MUST close the |
2790 | | * connection without sending a CONNECTION_CLOSE frame. This is |
2791 | | * actually handled as an emergent consequence of our design, as the |
2792 | | * TX packetiser will never transmit another packet when the TX PN |
2793 | | * reaches the limit. |
2794 | | * |
2795 | | * Calling the below function terminates the connection; its attempt |
2796 | | * to schedule a CONNECTION_CLOSE frame will not actually cause a |
2797 | | * packet to be transmitted for this reason. |
2798 | | */ |
2799 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, |
2800 | 0 | 0, |
2801 | 0 | "internal error (txp generate)"); |
2802 | 0 | break; |
2803 | 0 | } |
2804 | 0 | } while (status.sent_pkt > 0); |
2805 | | |
2806 | | /* Flush packets to network. */ |
2807 | 0 | switch (ossl_qtx_flush_net(ch->qtx)) { |
2808 | 0 | case QTX_FLUSH_NET_RES_OK: |
2809 | 0 | case QTX_FLUSH_NET_RES_TRANSIENT_FAIL: |
2810 | | /* Best effort, done for now. */ |
2811 | 0 | break; |
2812 | | |
2813 | 0 | case QTX_FLUSH_NET_RES_PERMANENT_FAIL: |
2814 | 0 | default: |
2815 | | /* Permanent underlying network BIO, start terminating. */ |
2816 | 0 | ossl_quic_port_raise_net_error(ch->port, ch); |
2817 | 0 | break; |
2818 | 0 | } |
2819 | | |
2820 | | /* |
2821 | | * If we have datagrams we have yet to successfully transmit, we need to |
2822 | | * notify other threads so that they can switch to polling on POLLOUT as |
2823 | | * well as POLLIN. |
2824 | | */ |
2825 | 0 | if (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0) |
2826 | 0 | *notify_other_threads = 1; |
2827 | |
|
2828 | 0 | return 1; |
2829 | 0 | } |
2830 | | |
2831 | | /* Determine next tick deadline. */ |
2832 | | static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch) |
2833 | 0 | { |
2834 | 0 | OSSL_TIME deadline; |
2835 | 0 | int i; |
2836 | |
|
2837 | 0 | if (ossl_quic_channel_is_terminated(ch)) |
2838 | 0 | return ossl_time_infinite(); |
2839 | | |
2840 | 0 | deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm); |
2841 | 0 | if (ossl_time_is_zero(deadline)) |
2842 | 0 | deadline = ossl_time_infinite(); |
2843 | | |
2844 | | /* |
2845 | | * Check the ack deadline for all enc_levels that are actually provisioned. |
2846 | | * ACKs aren't restricted by CC. |
2847 | | */ |
2848 | 0 | for (i = 0; i < QUIC_ENC_LEVEL_NUM; i++) { |
2849 | 0 | if (ossl_qtx_is_enc_level_provisioned(ch->qtx, i)) { |
2850 | 0 | deadline = ossl_time_min(deadline, |
2851 | 0 | ossl_ackm_get_ack_deadline(ch->ackm, |
2852 | 0 | ossl_quic_enc_level_to_pn_space(i))); |
2853 | 0 | } |
2854 | 0 | } |
2855 | | |
2856 | | /* |
2857 | | * When do we need to send an ACK-eliciting packet to reset the idle |
2858 | | * deadline timer for the peer? |
2859 | | */ |
2860 | 0 | if (!ossl_time_is_infinite(ch->ping_deadline)) |
2861 | 0 | deadline = ossl_time_min(deadline, ch->ping_deadline); |
2862 | | |
2863 | | /* Apply TXP wakeup deadline. */ |
2864 | 0 | deadline = ossl_time_min(deadline, |
2865 | 0 | ossl_quic_tx_packetiser_get_deadline(ch->txp)); |
2866 | | |
2867 | | /* Is the terminating timer armed? */ |
2868 | 0 | if (ossl_quic_channel_is_terminating(ch)) |
2869 | 0 | deadline = ossl_time_min(deadline, |
2870 | 0 | ch->terminate_deadline); |
2871 | 0 | else if (!ossl_time_is_infinite(ch->idle_deadline)) |
2872 | 0 | deadline = ossl_time_min(deadline, |
2873 | 0 | ch->idle_deadline); |
2874 | | |
2875 | | /* When does the RXKU process complete? */ |
2876 | 0 | if (ch->rxku_in_progress) |
2877 | 0 | deadline = ossl_time_min(deadline, ch->rxku_update_end_deadline); |
2878 | |
|
2879 | 0 | return deadline; |
2880 | 0 | } |
2881 | | |
2882 | | /* |
2883 | | * QUIC Channel: Lifecycle Events |
2884 | | * ============================== |
2885 | | */ |
2886 | | |
2887 | | /* |
2888 | | * Record a state transition. This is not necessarily a change to ch->state but |
2889 | | * also includes the handshake becoming complete or confirmed, etc. |
2890 | | */ |
2891 | | static void ch_record_state_transition(QUIC_CHANNEL *ch, uint32_t new_state) |
2892 | 0 | { |
2893 | 0 | uint32_t old_state = ch->state; |
2894 | |
|
2895 | 0 | ch->state = new_state; |
2896 | |
|
2897 | 0 | ossl_qlog_event_connectivity_connection_state_updated(ch_get_qlog(ch), |
2898 | 0 | old_state, |
2899 | 0 | new_state, |
2900 | 0 | ch->handshake_complete, |
2901 | 0 | ch->handshake_confirmed); |
2902 | 0 | } |
2903 | | |
2904 | | static void free_peer_token(const unsigned char *token, |
2905 | | size_t token_len, void *arg) |
2906 | 0 | { |
2907 | 0 | ossl_quic_free_peer_token((QUIC_TOKEN *)arg); |
2908 | 0 | } |
2909 | | |
2910 | | int ossl_quic_channel_start(QUIC_CHANNEL *ch) |
2911 | 0 | { |
2912 | 0 | QUIC_TOKEN *token; |
2913 | |
|
2914 | 0 | if (ch->is_server) |
2915 | | /* |
2916 | | * This is not used by the server. The server moves to active |
2917 | | * automatically on receiving an incoming connection. |
2918 | | */ |
2919 | 0 | return 0; |
2920 | | |
2921 | 0 | if (ch->state != QUIC_CHANNEL_STATE_IDLE) |
2922 | | /* Calls to connect are idempotent */ |
2923 | 0 | return 1; |
2924 | | |
2925 | | /* Inform QTX of peer address. */ |
2926 | 0 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) |
2927 | 0 | return 0; |
2928 | | |
2929 | | /* |
2930 | | * Look to see if we have a token, and if so, set it on the packetiser |
2931 | | */ |
2932 | 0 | if (!ch->is_server |
2933 | 0 | && ossl_quic_get_peer_token(ch->port->channel_ctx, |
2934 | 0 | &ch->cur_peer_addr, |
2935 | 0 | &token) |
2936 | 0 | && !ossl_quic_tx_packetiser_set_initial_token(ch->txp, token->token, |
2937 | 0 | token->token_len, |
2938 | 0 | free_peer_token, |
2939 | 0 | token)) |
2940 | 0 | free_peer_token(NULL, 0, token); |
2941 | | |
2942 | | /* Plug in secrets for the Initial EL. */ |
2943 | 0 | if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx, |
2944 | 0 | ch->port->engine->propq, |
2945 | 0 | &ch->init_dcid, |
2946 | 0 | ch->is_server, |
2947 | 0 | ch->qrx, ch->qtx)) |
2948 | 0 | return 0; |
2949 | | |
2950 | | /* |
2951 | | * Determine the QUIC Transport Parameters and serialize the transport |
2952 | | * parameters block. (For servers, we do this later as we must defer |
2953 | | * generation until we have received the client's transport parameters.) |
2954 | | */ |
2955 | 0 | if (!ch->is_server && !ch->got_local_transport_params |
2956 | 0 | && !ch_generate_transport_params(ch)) |
2957 | 0 | return 0; |
2958 | | |
2959 | | /* Change state. */ |
2960 | 0 | ch_record_state_transition(ch, QUIC_CHANNEL_STATE_ACTIVE); |
2961 | 0 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ |
2962 | |
|
2963 | 0 | ossl_qlog_event_connectivity_connection_started(ch_get_qlog(ch), |
2964 | 0 | &ch->init_dcid); |
2965 | | |
2966 | | /* Handshake layer: start (e.g. send CH). */ |
2967 | 0 | if (!ch_tick_tls(ch, /*channel_only=*/0, NULL)) |
2968 | 0 | return 0; |
2969 | | |
2970 | 0 | ossl_quic_reactor_tick(ossl_quic_port_get0_reactor(ch->port), 0); /* best effort */ |
2971 | 0 | return 1; |
2972 | 0 | } |
2973 | | |
2974 | | static void free_token(const unsigned char *token, size_t token_len, void *arg) |
2975 | 0 | { |
2976 | 0 | OPENSSL_free((char *)token); |
2977 | 0 | } |
2978 | | |
2979 | | /* Start a locally initiated connection shutdown. */ |
2980 | | void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code, |
2981 | | const char *app_reason) |
2982 | 0 | { |
2983 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
2984 | |
|
2985 | 0 | if (ossl_quic_channel_is_term_any(ch)) |
2986 | 0 | return; |
2987 | | |
2988 | 0 | tcause.app = 1; |
2989 | 0 | tcause.error_code = app_error_code; |
2990 | 0 | tcause.reason = app_reason; |
2991 | 0 | tcause.reason_len = app_reason != NULL ? strlen(app_reason) : 0; |
2992 | 0 | ch_start_terminating(ch, &tcause, 0); |
2993 | 0 | } |
2994 | | |
2995 | | /* |
2996 | | * Force the ACKM to consider every Initial packet still in flight as lost, so |
2997 | | * that the CRYPTO data they carried is sent again. None may be in flight, e.g. |
2998 | | * when a Version Negotiation packet has just had us mark them all and a Retry |
2999 | | * follows before we send again; their data is then already queued. |
3000 | | * |
3001 | | * Returns 1 on success, 0 on failure. |
3002 | | */ |
3003 | | static int ch_mark_initial_pkts_pseudo_lost(QUIC_CHANNEL *ch) |
3004 | 0 | { |
3005 | 0 | OSSL_ACKM *ackm = ch->ackm; |
3006 | 0 | QUIC_PN pn; |
3007 | |
|
3008 | 0 | while (ossl_ackm_get_largest_unacked(ackm, QUIC_PN_SPACE_INITIAL, &pn)) |
3009 | 0 | if (!ossl_ackm_mark_packet_pseudo_lost(ackm, QUIC_PN_SPACE_INITIAL, pn)) |
3010 | 0 | return 0; |
3011 | | |
3012 | 0 | return 1; |
3013 | 0 | } |
3014 | | |
3015 | | /** |
3016 | | * ch_restart - Restarts the QUIC channel by simulating loss of the initial |
3017 | | * packets. This forces them to be regenerated with the updated protocol |
3018 | | * version number. |
3019 | | * |
3020 | | * @ch: Pointer to the QUIC_CHANNEL structure. |
3021 | | * |
3022 | | * Returns 1 on success, 0 on failure. |
3023 | | */ |
3024 | | static int ch_restart(QUIC_CHANNEL *ch) |
3025 | 0 | { |
3026 | | /* |
3027 | | * Just pretend we lost our initial packets, so they get |
3028 | | * regenerated, with our updated protocol version number |
3029 | | */ |
3030 | 0 | return ch_mark_initial_pkts_pseudo_lost(ch); |
3031 | 0 | } |
3032 | | |
3033 | | /* Called when a server asks us to do a retry. */ |
3034 | | static int ch_retry(QUIC_CHANNEL *ch, |
3035 | | const unsigned char *retry_token, |
3036 | | size_t retry_token_len, |
3037 | | const QUIC_CONN_ID *retry_scid) |
3038 | 0 | { |
3039 | 0 | void *buf; |
3040 | | |
3041 | | /* |
3042 | | * RFC 9000 s. 17.2.5.1: "A client MUST discard a Retry packet that contains |
3043 | | * a SCID field that is identical to the DCID field of its initial packet." |
3044 | | */ |
3045 | 0 | if (ossl_quic_conn_id_eq(&ch->init_dcid, retry_scid)) |
3046 | 0 | return 1; |
3047 | | |
3048 | | /* We change to using the SCID in the Retry packet as the DCID. */ |
3049 | 0 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid)) |
3050 | 0 | return 0; |
3051 | | |
3052 | | /* |
3053 | | * Now we retry. We will release the Retry packet immediately, so copy |
3054 | | * the token. |
3055 | | */ |
3056 | 0 | if ((buf = OPENSSL_memdup(retry_token, retry_token_len)) == NULL) |
3057 | 0 | return 0; |
3058 | | |
3059 | 0 | if (!ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, |
3060 | 0 | retry_token_len, |
3061 | 0 | free_token, NULL)) { |
3062 | | /* |
3063 | | * This may fail if the token we receive is too big for us to ever be |
3064 | | * able to transmit in an outgoing Initial packet. |
3065 | | */ |
3066 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INVALID_TOKEN, 0, |
3067 | 0 | "received oversize token"); |
3068 | 0 | OPENSSL_free(buf); |
3069 | 0 | return 0; |
3070 | 0 | } |
3071 | | |
3072 | 0 | ch->retry_scid = *retry_scid; |
3073 | 0 | ch->doing_retry = 1; |
3074 | | |
3075 | | /* |
3076 | | * We need to stimulate the Initial EL to generate the CRYPTO frames |
3077 | | * again. We can do this most cleanly by simply forcing the ACKM to consider |
3078 | | * every Initial packet we have sent as lost, which they effectively were as |
3079 | | * the server hasn't processed them (RFC 9002 s. 6.3). This also maintains |
3080 | | * the desired behaviour with e.g. PNs not resetting and so on. |
3081 | | * |
3082 | | * The ClientHello may span more than one Initial packet, so all of them |
3083 | | * must be marked, not only the first. |
3084 | | */ |
3085 | 0 | if (!ch_mark_initial_pkts_pseudo_lost(ch)) |
3086 | 0 | return 0; |
3087 | | |
3088 | | /* |
3089 | | * Plug in new secrets for the Initial EL. This is the only time we change |
3090 | | * the secrets for an EL after we already provisioned it. |
3091 | | */ |
3092 | 0 | if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx, |
3093 | 0 | ch->port->engine->propq, |
3094 | 0 | &ch->retry_scid, |
3095 | 0 | /*is_server=*/0, |
3096 | 0 | ch->qrx, ch->qtx)) |
3097 | 0 | return 0; |
3098 | | |
3099 | 0 | return 1; |
3100 | 0 | } |
3101 | | |
3102 | | /* Called when an EL is to be discarded. */ |
3103 | | static int ch_discard_el(QUIC_CHANNEL *ch, |
3104 | | uint32_t enc_level) |
3105 | 0 | { |
3106 | 0 | if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT)) |
3107 | 0 | return 0; |
3108 | | |
3109 | 0 | if ((ch->el_discarded & (1U << enc_level)) != 0) |
3110 | | /* Already done. */ |
3111 | 0 | return 1; |
3112 | | |
3113 | | /* Best effort for all of these. */ |
3114 | 0 | ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level); |
3115 | 0 | ossl_qrx_discard_enc_level(ch->qrx, enc_level); |
3116 | 0 | ossl_qtx_discard_enc_level(ch->qtx, enc_level); |
3117 | |
|
3118 | 0 | if (enc_level != QUIC_ENC_LEVEL_0RTT) { |
3119 | 0 | uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level); |
3120 | |
|
3121 | 0 | ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space); |
3122 | | |
3123 | | /* We should still have crypto streams at this point. */ |
3124 | 0 | if (!ossl_assert(ch->crypto_send[pn_space] != NULL) |
3125 | 0 | || !ossl_assert(ch->crypto_recv[pn_space] != NULL)) |
3126 | 0 | return 0; |
3127 | | |
3128 | | /* Get rid of the crypto stream state for the EL. */ |
3129 | 0 | ossl_quic_sstream_free(ch->crypto_send[pn_space]); |
3130 | 0 | ch->crypto_send[pn_space] = NULL; |
3131 | |
|
3132 | 0 | ossl_quic_rstream_free(ch->crypto_recv[pn_space]); |
3133 | 0 | ch->crypto_recv[pn_space] = NULL; |
3134 | 0 | } |
3135 | | |
3136 | 0 | ch->el_discarded |= (1U << enc_level); |
3137 | 0 | return 1; |
3138 | 0 | } |
3139 | | |
3140 | | /* Intended to be called by the RXDP. */ |
3141 | | int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch) |
3142 | 0 | { |
3143 | 0 | if (ch->handshake_confirmed) |
3144 | 0 | return 1; |
3145 | | |
3146 | 0 | if (!ch->handshake_complete) { |
3147 | | /* |
3148 | | * Does not make sense for handshake to be confirmed before it is |
3149 | | * completed. |
3150 | | */ |
3151 | 0 | ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
3152 | 0 | OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, |
3153 | 0 | "handshake cannot be confirmed " |
3154 | 0 | "before it is completed"); |
3155 | 0 | return 0; |
3156 | 0 | } |
3157 | | |
3158 | 0 | ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE); |
3159 | 0 | ch->handshake_confirmed = 1; |
3160 | 0 | ch_record_state_transition(ch, ch->state); |
3161 | 0 | ossl_ackm_on_handshake_confirmed(ch->ackm); |
3162 | 0 | return 1; |
3163 | 0 | } |
3164 | | |
3165 | | /* |
3166 | | * Master function used when we want to start tearing down a connection: |
3167 | | * |
3168 | | * - If the connection is still IDLE we can go straight to TERMINATED; |
3169 | | * |
3170 | | * - If we are already TERMINATED this is a no-op. |
3171 | | * |
3172 | | * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE |
3173 | | * from the peer (tcause->remote == 1), we move to TERMINATING - DRAINING. |
3174 | | * |
3175 | | * - If we are TERMINATING - DRAINING, we remain here until the terminating |
3176 | | * timer expires. |
3177 | | * |
3178 | | * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING. |
3179 | | * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note |
3180 | | * that we are considered to have caused a termination if we sent the first |
3181 | | * CONNECTION_CLOSE frame, even if it is caused by a peer protocol |
3182 | | * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to |
3183 | | * TERMINATING - DRAINING. |
3184 | | * |
3185 | | * We record the termination cause structure passed on the first call only. |
3186 | | * Any successive calls have their termination cause data discarded; |
3187 | | * once we start sending a CONNECTION_CLOSE frame, we don't change the details |
3188 | | * in it. |
3189 | | * |
3190 | | * This conforms to RFC 9000 s. 10.2.1: Closing Connection State: |
3191 | | * To minimize the state that an endpoint maintains for a closing |
3192 | | * connection, endpoints MAY send the exact same packet in response |
3193 | | * to any received packet. |
3194 | | * |
3195 | | * We don't drop any connection state (specifically packet protection keys) |
3196 | | * even though we are permitted to. This conforms to RFC 9000 s. 10.2.1: |
3197 | | * Closing Connection State: |
3198 | | * An endpoint MAY retain packet protection keys for incoming |
3199 | | * packets to allow it to read and process a CONNECTION_CLOSE frame. |
3200 | | * |
3201 | | * Note that we do not conform to these two from the same section: |
3202 | | * An endpoint's selected connection ID and the QUIC version |
3203 | | * are sufficient information to identify packets for a closing |
3204 | | * connection; the endpoint MAY discard all other connection state. |
3205 | | * and: |
3206 | | * An endpoint MAY drop packet protection keys when entering the |
3207 | | * closing state and send a packet containing a CONNECTION_CLOSE |
3208 | | * frame in response to any UDP datagram that is received. |
3209 | | */ |
3210 | | static void copy_tcause(QUIC_TERMINATE_CAUSE *dst, |
3211 | | const QUIC_TERMINATE_CAUSE *src) |
3212 | 0 | { |
3213 | | /* |
3214 | | * do not override reason once it got set. |
3215 | | */ |
3216 | 0 | if (dst->reason != NULL) |
3217 | 0 | return; |
3218 | | |
3219 | 0 | dst->error_code = src->error_code; |
3220 | 0 | dst->frame_type = src->frame_type; |
3221 | 0 | dst->app = src->app; |
3222 | 0 | dst->remote = src->remote; |
3223 | |
|
3224 | 0 | if (src->reason != NULL && src->reason_len > 0) { |
3225 | 0 | size_t l = src->reason_len; |
3226 | 0 | char *r; |
3227 | |
|
3228 | 0 | if (l >= SIZE_MAX) |
3229 | 0 | --l; |
3230 | | |
3231 | | /* |
3232 | | * If this fails, dst->reason becomes NULL and we simply do not use a |
3233 | | * reason. This ensures termination is infallible. |
3234 | | */ |
3235 | 0 | dst->reason = r = OPENSSL_malloc(l + 1); |
3236 | 0 | if (r == NULL) |
3237 | 0 | return; |
3238 | | |
3239 | 0 | memcpy(r, src->reason, l); |
3240 | 0 | r[l] = '\0'; |
3241 | 0 | dst->reason_len = l; |
3242 | 0 | } |
3243 | 0 | } |
3244 | | |
3245 | | void ossl_quic_channel_set_tcause(QUIC_CHANNEL *ch, uint64_t app_error_code, |
3246 | | const char *app_reason) |
3247 | 0 | { |
3248 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
3249 | |
|
3250 | 0 | tcause.app = 1; |
3251 | 0 | tcause.error_code = app_error_code; |
3252 | 0 | tcause.reason = app_reason; |
3253 | 0 | tcause.reason_len = app_reason != NULL ? strlen(app_reason) : 0; |
3254 | 0 | copy_tcause(&ch->terminate_cause, &tcause); |
3255 | 0 | } |
3256 | | |
3257 | | static void ch_start_terminating(QUIC_CHANNEL *ch, |
3258 | | const QUIC_TERMINATE_CAUSE *tcause, |
3259 | | int force_immediate) |
3260 | 0 | { |
3261 | | /* No point sending anything if we haven't sent anything yet. */ |
3262 | 0 | if (!ch->have_sent_any_pkt) |
3263 | 0 | force_immediate = 1; |
3264 | |
|
3265 | 0 | switch (ch->state) { |
3266 | 0 | default: |
3267 | 0 | case QUIC_CHANNEL_STATE_IDLE: |
3268 | 0 | copy_tcause(&ch->terminate_cause, tcause); |
3269 | 0 | ch_on_terminating_timeout(ch); |
3270 | 0 | break; |
3271 | | |
3272 | 0 | case QUIC_CHANNEL_STATE_ACTIVE: |
3273 | 0 | copy_tcause(&ch->terminate_cause, tcause); |
3274 | |
|
3275 | 0 | ossl_qlog_event_connectivity_connection_closed(ch_get_qlog(ch), tcause); |
3276 | |
|
3277 | 0 | if (!force_immediate) { |
3278 | 0 | ossl_quic_channel_notify_flush_done(ch); |
3279 | 0 | } else { |
3280 | 0 | ch_on_terminating_timeout(ch); |
3281 | 0 | } |
3282 | 0 | break; |
3283 | | |
3284 | 0 | case QUIC_CHANNEL_STATE_TERMINATING_CLOSING: |
3285 | 0 | if (force_immediate) |
3286 | 0 | ch_on_terminating_timeout(ch); |
3287 | 0 | else if (tcause->remote) |
3288 | | /* |
3289 | | * RFC 9000 s. 10.2.2 Draining Connection State: |
3290 | | * An endpoint MAY enter the draining state from the |
3291 | | * closing state if it receives a CONNECTION_CLOSE frame, |
3292 | | * which indicates that the peer is also closing or draining. |
3293 | | */ |
3294 | 0 | ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATING_DRAINING); |
3295 | |
|
3296 | 0 | break; |
3297 | | |
3298 | 0 | case QUIC_CHANNEL_STATE_TERMINATING_DRAINING: |
3299 | | /* |
3300 | | * Other than in the force-immediate case, we remain here until the |
3301 | | * timeout expires. |
3302 | | */ |
3303 | 0 | if (force_immediate) |
3304 | 0 | ch_on_terminating_timeout(ch); |
3305 | |
|
3306 | 0 | break; |
3307 | | |
3308 | 0 | case QUIC_CHANNEL_STATE_TERMINATED: |
3309 | | /* No-op. */ |
3310 | 0 | break; |
3311 | 0 | } |
3312 | 0 | } |
3313 | | |
3314 | | /* For RXDP use. */ |
3315 | | void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch, |
3316 | | OSSL_QUIC_FRAME_CONN_CLOSE *f) |
3317 | 0 | { |
3318 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
3319 | |
|
3320 | 0 | if (!ossl_quic_channel_is_active(ch)) |
3321 | 0 | return; |
3322 | | |
3323 | 0 | tcause.remote = 1; |
3324 | 0 | tcause.app = f->is_app; |
3325 | 0 | tcause.error_code = f->error_code; |
3326 | 0 | tcause.frame_type = f->frame_type; |
3327 | 0 | tcause.reason = f->reason; |
3328 | 0 | tcause.reason_len = f->reason_len; |
3329 | 0 | ch_start_terminating(ch, &tcause, 0); |
3330 | 0 | } |
3331 | | |
3332 | | static void free_frame_rtcid(unsigned char *buf, size_t buf_len, void *arg) |
3333 | 0 | { |
3334 | 0 | CFQ_DATA_RETIRE_CID_T *cfq_data_rtcid = (CFQ_DATA_RETIRE_CID_T *)arg; |
3335 | 0 | QUIC_CHANNEL *ch = cfq_data_rtcid->rtcid_ch; |
3336 | |
|
3337 | 0 | if (ch->cur_retire_prior_to < cfq_data_rtcid->rtcid_seq) |
3338 | 0 | ch->cur_retire_prior_to = cfq_data_rtcid->rtcid_seq; |
3339 | |
|
3340 | 0 | OPENSSL_free(buf); |
3341 | 0 | OPENSSL_free(cfq_data_rtcid); |
3342 | 0 | } |
3343 | | |
3344 | | static int ch_enqueue_retire_conn_id(QUIC_CHANNEL *ch, uint64_t seq_num) |
3345 | 0 | { |
3346 | 0 | CFQ_DATA_RETIRE_CID_T *cfq_data_rtcid = NULL; |
3347 | 0 | BUF_MEM *buf_mem = NULL; |
3348 | 0 | WPACKET wpkt; |
3349 | 0 | size_t l; |
3350 | |
|
3351 | 0 | if (!ossl_quic_srtm_remove(ch->srtm, ch, seq_num, NULL)) |
3352 | 0 | goto err; |
3353 | | |
3354 | 0 | cfq_data_rtcid = OPENSSL_malloc(sizeof(CFQ_DATA_RETIRE_CID_T)); |
3355 | 0 | if (cfq_data_rtcid == NULL) |
3356 | 0 | goto err; |
3357 | 0 | cfq_data_rtcid->rtcid_seq = seq_num; |
3358 | 0 | cfq_data_rtcid->rtcid_ch = ch; |
3359 | |
|
3360 | 0 | if ((buf_mem = BUF_MEM_new()) == NULL) |
3361 | 0 | goto err; |
3362 | | |
3363 | 0 | if (!WPACKET_init(&wpkt, buf_mem)) |
3364 | 0 | goto err; |
3365 | | |
3366 | 0 | if (!ossl_quic_wire_encode_frame_retire_conn_id(&wpkt, seq_num)) { |
3367 | 0 | WPACKET_cleanup(&wpkt); |
3368 | 0 | goto err; |
3369 | 0 | } |
3370 | | |
3371 | 0 | WPACKET_finish(&wpkt); |
3372 | 0 | if (!WPACKET_get_total_written(&wpkt, &l)) |
3373 | 0 | goto err; |
3374 | | |
3375 | 0 | if (ossl_quic_cfq_add_frame(ch->cfq, 1, QUIC_PN_SPACE_APP, |
3376 | 0 | OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, 0, |
3377 | 0 | (unsigned char *)buf_mem->data, l, |
3378 | 0 | free_frame_rtcid, cfq_data_rtcid) |
3379 | 0 | == NULL) |
3380 | 0 | goto err; |
3381 | | |
3382 | 0 | buf_mem->data = NULL; |
3383 | 0 | BUF_MEM_free(buf_mem); |
3384 | 0 | return 1; |
3385 | | |
3386 | 0 | err: |
3387 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
3388 | 0 | OSSL_QUIC_ERR_INTERNAL_ERROR, |
3389 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, |
3390 | 0 | "internal error enqueueing retire conn id"); |
3391 | 0 | BUF_MEM_free(buf_mem); |
3392 | 0 | OPENSSL_free(cfq_data_rtcid); |
3393 | 0 | return 0; |
3394 | 0 | } |
3395 | | |
3396 | | void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL *ch, |
3397 | | OSSL_QUIC_FRAME_NEW_CONN_ID *f) |
3398 | 0 | { |
3399 | 0 | uint64_t new_remote_seq_num = ch->cur_remote_seq_num; |
3400 | 0 | uint64_t new_retire_prior_to = ch->cur_retire_prior_to; |
3401 | 0 | uint64_t retire_prior_to; |
3402 | |
|
3403 | 0 | if (!ossl_quic_channel_is_active(ch)) |
3404 | 0 | return; |
3405 | | |
3406 | | /* We allow only two active connection ids; first check some constraints */ |
3407 | 0 | if (ch->cur_remote_dcid.id_len == 0) { |
3408 | | /* Changing from 0 length connection id is disallowed */ |
3409 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
3410 | 0 | OSSL_QUIC_ERR_PROTOCOL_VIOLATION, |
3411 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, |
3412 | 0 | "zero length connection id in use"); |
3413 | |
|
3414 | 0 | return; |
3415 | 0 | } |
3416 | | |
3417 | 0 | if (f->seq_num > new_remote_seq_num) |
3418 | 0 | new_remote_seq_num = f->seq_num; |
3419 | 0 | if (f->retire_prior_to > new_retire_prior_to) |
3420 | 0 | new_retire_prior_to = f->retire_prior_to; |
3421 | | |
3422 | | /* |
3423 | | * RFC 9000-5.1.1: An endpoint MUST NOT provide more connection IDs |
3424 | | * than the peer's limit. |
3425 | | * |
3426 | | * After processing a NEW_CONNECTION_ID frame and adding and retiring |
3427 | | * active connection IDs, if the number of active connection IDs exceeds |
3428 | | * the value advertised in its active_connection_id_limit transport |
3429 | | * parameter, an endpoint MUST close the connection with an error of |
3430 | | * type CONNECTION_ID_LIMIT_ERROR. |
3431 | | */ |
3432 | 0 | if (new_remote_seq_num - new_retire_prior_to > 1) { |
3433 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
3434 | 0 | OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, |
3435 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, |
3436 | 0 | "active_connection_id limit violated"); |
3437 | 0 | return; |
3438 | 0 | } |
3439 | | |
3440 | | /* |
3441 | | * RFC 9000-5.1.1: An endpoint MAY send connection IDs that temporarily |
3442 | | * exceed a peer's limit if the NEW_CONNECTION_ID frame also requires |
3443 | | * the retirement of any excess, by including a sufficiently large |
3444 | | * value in the Retire Prior To field. |
3445 | | * |
3446 | | * RFC 9000-5.1.2: An endpoint SHOULD allow for sending and tracking |
3447 | | * a number of RETIRE_CONNECTION_ID frames of at least twice the value |
3448 | | * of the active_connection_id_limit transport parameter. An endpoint |
3449 | | * MUST NOT forget a connection ID without retiring it, though it MAY |
3450 | | * choose to treat having connection IDs in need of retirement that |
3451 | | * exceed this limit as a connection error of type CONNECTION_ID_LIMIT_ERROR. |
3452 | | * |
3453 | | * We are a little bit more liberal than the minimum mandated. |
3454 | | */ |
3455 | 0 | if (new_retire_prior_to - ch->cur_retire_prior_to > 10) { |
3456 | 0 | ossl_quic_channel_raise_protocol_error(ch, |
3457 | 0 | OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, |
3458 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, |
3459 | 0 | "retiring connection id limit violated"); |
3460 | |
|
3461 | 0 | return; |
3462 | 0 | } |
3463 | | |
3464 | 0 | if (new_remote_seq_num > ch->cur_remote_seq_num) { |
3465 | | /* Add new stateless reset token */ |
3466 | 0 | if (!ossl_quic_srtm_add(ch->srtm, ch, new_remote_seq_num, |
3467 | 0 | &f->stateless_reset)) { |
3468 | 0 | ossl_quic_channel_raise_protocol_error( |
3469 | 0 | ch, OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR, |
3470 | 0 | OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, |
3471 | 0 | "unable to store stateless reset token"); |
3472 | |
|
3473 | 0 | return; |
3474 | 0 | } |
3475 | 0 | ch->cur_remote_seq_num = new_remote_seq_num; |
3476 | 0 | ch->cur_remote_dcid = f->conn_id; |
3477 | 0 | ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid); |
3478 | 0 | } |
3479 | | |
3480 | | /* |
3481 | | * RFC 9000-5.1.2: Upon receipt of an increased Retire Prior To |
3482 | | * field, the peer MUST stop using the corresponding connection IDs |
3483 | | * and retire them with RETIRE_CONNECTION_ID frames before adding the |
3484 | | * newly provided connection ID to the set of active connection IDs. |
3485 | | */ |
3486 | | |
3487 | | /* |
3488 | | * Note: RFC 9000 s. 19.15 says: |
3489 | | * "An endpoint that receives a NEW_CONNECTION_ID frame with a sequence |
3490 | | * number smaller than the Retire Prior To field of a previously received |
3491 | | * NEW_CONNECTION_ID frame MUST send a corresponding |
3492 | | * RETIRE_CONNECTION_ID frame that retires the newly received connection |
3493 | | * ID, unless it has already done so for that sequence number." |
3494 | | * |
3495 | | * Since we currently always queue RETIRE_CONN_ID frames based on the Retire |
3496 | | * Prior To field of a NEW_CONNECTION_ID frame immediately upon receiving |
3497 | | * that NEW_CONNECTION_ID frame, by definition this will always be met. |
3498 | | * This may change in future when we change our CID handling. |
3499 | | */ |
3500 | 0 | retire_prior_to = ch->cur_retire_prior_to; |
3501 | 0 | while (new_retire_prior_to > retire_prior_to) { |
3502 | 0 | ch_enqueue_retire_conn_id(ch, retire_prior_to); |
3503 | 0 | retire_prior_to++; |
3504 | 0 | } |
3505 | 0 | } |
3506 | | |
3507 | | static void ch_save_err_state(QUIC_CHANNEL *ch) |
3508 | 0 | { |
3509 | 0 | if (ch->err_state == NULL) |
3510 | 0 | ch->err_state = OSSL_ERR_STATE_new(); |
3511 | |
|
3512 | 0 | if (ch->err_state == NULL) |
3513 | 0 | return; |
3514 | | |
3515 | 0 | OSSL_ERR_STATE_save(ch->err_state); |
3516 | 0 | } |
3517 | | |
3518 | | void ossl_quic_channel_inject(QUIC_CHANNEL *ch, QUIC_URXE *e) |
3519 | 0 | { |
3520 | 0 | ossl_qrx_inject_urxe(ch->qrx, e); |
3521 | 0 | } |
3522 | | |
3523 | | void ossl_quic_channel_inject_pkt(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpkt) |
3524 | 0 | { |
3525 | 0 | ossl_qrx_inject_pkt(ch->qrx, qpkt); |
3526 | 0 | } |
3527 | | |
3528 | | void ossl_quic_channel_on_stateless_reset(QUIC_CHANNEL *ch) |
3529 | 0 | { |
3530 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
3531 | |
|
3532 | 0 | tcause.error_code = OSSL_QUIC_ERR_NO_ERROR; |
3533 | 0 | tcause.remote = 1; |
3534 | 0 | ch_start_terminating(ch, &tcause, 0); |
3535 | 0 | } |
3536 | | |
3537 | | void ossl_quic_channel_raise_net_error(QUIC_CHANNEL *ch) |
3538 | 0 | { |
3539 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
3540 | |
|
3541 | 0 | if (ch->net_error) |
3542 | 0 | return; |
3543 | | |
3544 | 0 | ch->net_error = 1; |
3545 | |
|
3546 | 0 | tcause.error_code = OSSL_QUIC_ERR_INTERNAL_ERROR; |
3547 | 0 | tcause.reason = "network BIO I/O error"; |
3548 | 0 | tcause.reason_len = strlen(tcause.reason); |
3549 | | |
3550 | | /* |
3551 | | * Skip Terminating state and go directly to Terminated, no point trying to |
3552 | | * send CONNECTION_CLOSE if we cannot communicate. |
3553 | | */ |
3554 | 0 | ch_start_terminating(ch, &tcause, 1); |
3555 | 0 | } |
3556 | | |
3557 | | int ossl_quic_channel_net_error(QUIC_CHANNEL *ch) |
3558 | 0 | { |
3559 | 0 | return ch->net_error; |
3560 | 0 | } |
3561 | | |
3562 | | void ossl_quic_channel_restore_err_state(QUIC_CHANNEL *ch) |
3563 | 0 | { |
3564 | 0 | if (ch == NULL) |
3565 | 0 | return; |
3566 | | |
3567 | 0 | if (!ossl_quic_port_is_running(ch->port)) |
3568 | 0 | ossl_quic_port_restore_err_state(ch->port); |
3569 | 0 | else |
3570 | 0 | OSSL_ERR_STATE_restore(ch->err_state); |
3571 | 0 | } |
3572 | | |
3573 | | void ossl_quic_channel_raise_protocol_error_loc(QUIC_CHANNEL *ch, |
3574 | | uint64_t error_code, |
3575 | | uint64_t frame_type, |
3576 | | const char *reason, |
3577 | | ERR_STATE *err_state, |
3578 | | const char *src_file, |
3579 | | int src_line, |
3580 | | const char *src_func) |
3581 | 0 | { |
3582 | 0 | QUIC_TERMINATE_CAUSE tcause = { 0 }; |
3583 | 0 | int err_reason = error_code == OSSL_QUIC_ERR_INTERNAL_ERROR |
3584 | 0 | ? ERR_R_INTERNAL_ERROR |
3585 | 0 | : SSL_R_QUIC_PROTOCOL_ERROR; |
3586 | 0 | const char *err_str = ossl_quic_err_to_string(error_code); |
3587 | 0 | const char *err_str_pfx = " (", *err_str_sfx = ")"; |
3588 | 0 | const char *ft_str = NULL; |
3589 | 0 | const char *ft_str_pfx = " (", *ft_str_sfx = ")"; |
3590 | |
|
3591 | 0 | if (ch->protocol_error) |
3592 | | /* Only the first call to this function matters. */ |
3593 | 0 | return; |
3594 | | |
3595 | 0 | if (err_str == NULL) { |
3596 | 0 | err_str = ""; |
3597 | 0 | err_str_pfx = ""; |
3598 | 0 | err_str_sfx = ""; |
3599 | 0 | } |
3600 | | |
3601 | | /* |
3602 | | * If we were provided an underlying error state, restore it and then append |
3603 | | * our ERR on top as a "cover letter" error. |
3604 | | */ |
3605 | 0 | if (err_state != NULL) |
3606 | 0 | OSSL_ERR_STATE_restore(err_state); |
3607 | |
|
3608 | 0 | if (frame_type != 0) { |
3609 | 0 | ft_str = ossl_quic_frame_type_to_string(frame_type); |
3610 | 0 | if (ft_str == NULL) { |
3611 | 0 | ft_str = ""; |
3612 | 0 | ft_str_pfx = ""; |
3613 | 0 | ft_str_sfx = ""; |
3614 | 0 | } |
3615 | |
|
3616 | 0 | ERR_raise_data(ERR_LIB_SSL, err_reason, |
3617 | 0 | "QUIC error code: 0x%llx%s%s%s " |
3618 | 0 | "(triggered by frame type: 0x%llx%s%s%s), reason: \"%s\"", |
3619 | 0 | (unsigned long long)error_code, |
3620 | 0 | err_str_pfx, err_str, err_str_sfx, |
3621 | 0 | (unsigned long long)frame_type, |
3622 | 0 | ft_str_pfx, ft_str, ft_str_sfx, |
3623 | 0 | reason); |
3624 | 0 | } else { |
3625 | 0 | ERR_raise_data(ERR_LIB_SSL, err_reason, |
3626 | 0 | "QUIC error code: 0x%llx%s%s%s, reason: \"%s\"", |
3627 | 0 | (unsigned long long)error_code, |
3628 | 0 | err_str_pfx, err_str, err_str_sfx, |
3629 | 0 | reason); |
3630 | 0 | } |
3631 | |
|
3632 | 0 | if (src_file != NULL) |
3633 | 0 | ERR_set_debug(src_file, src_line, src_func); |
3634 | |
|
3635 | 0 | ch_save_err_state(ch); |
3636 | |
|
3637 | 0 | tcause.error_code = error_code; |
3638 | 0 | tcause.frame_type = frame_type; |
3639 | 0 | tcause.reason = reason; |
3640 | 0 | tcause.reason_len = strlen(reason); |
3641 | |
|
3642 | 0 | ch->protocol_error = 1; |
3643 | 0 | ch_start_terminating(ch, &tcause, 0); |
3644 | 0 | } |
3645 | | |
3646 | | /* |
3647 | | * Called once the terminating timer expires, meaning we move from TERMINATING |
3648 | | * to TERMINATED. |
3649 | | */ |
3650 | | static void ch_on_terminating_timeout(QUIC_CHANNEL *ch) |
3651 | 0 | { |
3652 | 0 | ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED); |
3653 | 0 | } |
3654 | | |
3655 | | /* |
3656 | | * Determines the effective idle timeout duration. This is based on the idle |
3657 | | * timeout values that we and our peer signalled in transport parameters |
3658 | | * but have some limits applied. |
3659 | | */ |
3660 | | static OSSL_TIME ch_get_effective_idle_timeout_duration(QUIC_CHANNEL *ch) |
3661 | 0 | { |
3662 | 0 | OSSL_TIME pto; |
3663 | |
|
3664 | 0 | if (ch->max_idle_timeout == 0) |
3665 | 0 | return ossl_time_infinite(); |
3666 | | |
3667 | | /* |
3668 | | * RFC 9000 s. 10.1: Idle Timeout |
3669 | | * To avoid excessively small idle timeout periods, endpoints |
3670 | | * MUST increase the idle timeout period to be at least three |
3671 | | * times the current Probe Timeout (PTO). This allows for |
3672 | | * multiple PTOs to expire, and therefore multiple probes to |
3673 | | * be sent and lost, prior to idle timeout. |
3674 | | */ |
3675 | 0 | pto = ossl_ackm_get_pto_duration(ch->ackm); |
3676 | 0 | return ossl_time_max(ossl_ms2time(ch->max_idle_timeout), |
3677 | 0 | ossl_time_multiply(pto, 3)); |
3678 | 0 | } |
3679 | | |
3680 | | /* |
3681 | | * Updates our idle deadline. Called when an event happens which should bump the |
3682 | | * idle timeout. |
3683 | | */ |
3684 | | static void ch_update_idle(QUIC_CHANNEL *ch) |
3685 | 0 | { |
3686 | 0 | ch->idle_deadline = ossl_time_add(get_time(ch), |
3687 | 0 | ch_get_effective_idle_timeout_duration(ch)); |
3688 | 0 | } |
3689 | | |
3690 | | /* |
3691 | | * Updates our ping deadline, which determines when we next generate a ping if |
3692 | | * we don't have any other ACK-eliciting frames to send. |
3693 | | */ |
3694 | | static void ch_update_ping_deadline(QUIC_CHANNEL *ch) |
3695 | 0 | { |
3696 | 0 | OSSL_TIME max_span, idle_duration; |
3697 | |
|
3698 | 0 | idle_duration = ch_get_effective_idle_timeout_duration(ch); |
3699 | 0 | if (ossl_time_is_infinite(idle_duration)) { |
3700 | 0 | ch->ping_deadline = ossl_time_infinite(); |
3701 | 0 | return; |
3702 | 0 | } |
3703 | | |
3704 | | /* |
3705 | | * Maximum amount of time without traffic before we send a PING to keep |
3706 | | * the connection open. Usually we use max_idle_timeout/2, but ensure |
3707 | | * the period never exceeds the assumed NAT interval to ensure NAT |
3708 | | * devices don't have their state time out (RFC 9000 s. 10.1.2). |
3709 | | */ |
3710 | 0 | max_span = ossl_time_divide(idle_duration, 2); |
3711 | 0 | max_span = ossl_time_min(max_span, MAX_NAT_INTERVAL); |
3712 | 0 | ch->ping_deadline = ossl_time_add(get_time(ch), max_span); |
3713 | 0 | } |
3714 | | |
3715 | | /* Called when the idle timeout expires. */ |
3716 | | static void ch_on_idle_timeout(QUIC_CHANNEL *ch) |
3717 | 0 | { |
3718 | | /* |
3719 | | * Idle timeout does not have an error code associated with it because a |
3720 | | * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach |
3721 | | * TERMINATED anyway. |
3722 | | */ |
3723 | 0 | ch->terminate_cause.app = 0; |
3724 | 0 | ch->terminate_cause.error_code = OSSL_QUIC_LOCAL_ERR_IDLE_TIMEOUT; |
3725 | 0 | ch->terminate_cause.frame_type = 0; |
3726 | |
|
3727 | 0 | ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED); |
3728 | 0 | } |
3729 | | |
3730 | | /** |
3731 | | * @brief Common handler for initializing a new QUIC connection. |
3732 | | * |
3733 | | * This function configures a QUIC channel (`QUIC_CHANNEL *ch`) for a new |
3734 | | * connection by setting the peer address, connection IDs, and necessary |
3735 | | * callbacks. It establishes initial secrets, sets up logging, and performs |
3736 | | * required transitions for the channel state. |
3737 | | * |
3738 | | * @param ch Pointer to the QUIC channel being initialized. |
3739 | | * @param peer Address of the peer to which the channel connects. |
3740 | | * @param peer_scid Peer-specified source connection ID. |
3741 | | * @param peer_dcid Peer-specified destination connection ID. |
3742 | | * @param peer_odcid Peer-specified original destination connection ID |
3743 | | * may be NULL if retry frame not sent to client |
3744 | | * @return 1 on success, 0 on failure to set required elements. |
3745 | | */ |
3746 | | static int ch_on_new_conn_common(QUIC_CHANNEL *ch, const BIO_ADDR *peer, |
3747 | | const QUIC_CONN_ID *peer_dcid, |
3748 | | const QUIC_CONN_ID *peer_odcid) |
3749 | 0 | { |
3750 | | /* Note our newly learnt peer address and CIDs. */ |
3751 | 0 | if (!ossl_quic_channel_set_peer_addr(ch, peer)) |
3752 | 0 | return 0; |
3753 | | |
3754 | 0 | ch->init_dcid = *peer_dcid; |
3755 | 0 | ch->odcid.id_len = 0; |
3756 | |
|
3757 | 0 | if (peer_odcid != NULL) |
3758 | 0 | ch->odcid = *peer_odcid; |
3759 | | |
3760 | | /* Inform QTX of peer address. */ |
3761 | 0 | if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr)) |
3762 | 0 | return 0; |
3763 | | |
3764 | | /* Inform TXP of desired CIDs. */ |
3765 | 0 | if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid)) |
3766 | 0 | return 0; |
3767 | | |
3768 | 0 | if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid)) |
3769 | 0 | return 0; |
3770 | | |
3771 | | /* Setup QLOG, which did not happen earlier due to lacking an Initial ODCID. */ |
3772 | 0 | ossl_qtx_set_qlog_cb(ch->qtx, ch_get_qlog_cb, ch); |
3773 | 0 | ossl_quic_tx_packetiser_set_qlog_cb(ch->txp, ch_get_qlog_cb, ch); |
3774 | | |
3775 | | /* |
3776 | | * Plug in secrets for the Initial EL. secrets for QRX were created in |
3777 | | * port_default_packet_handler() already. |
3778 | | */ |
3779 | 0 | if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx, |
3780 | 0 | ch->port->engine->propq, |
3781 | 0 | &ch->init_dcid, |
3782 | 0 | /*is_server=*/1, |
3783 | 0 | NULL, ch->qtx)) |
3784 | 0 | return 0; |
3785 | | |
3786 | | /* Register the peer ODCID in the LCIDM. */ |
3787 | 0 | if (!ossl_quic_lcidm_enrol_odcid(ch->lcidm, ch, peer_odcid == NULL ? &ch->init_dcid : peer_odcid)) |
3788 | 0 | return 0; |
3789 | | |
3790 | | /* Change state. */ |
3791 | 0 | ch_record_state_transition(ch, QUIC_CHANNEL_STATE_ACTIVE); |
3792 | 0 | ch->doing_proactive_ver_neg = 0; /* not currently supported */ |
3793 | 0 | return 1; |
3794 | 0 | } |
3795 | | |
3796 | | /* Called when we, as a server, get a new incoming connection. */ |
3797 | | int ossl_quic_channel_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer, |
3798 | | const QUIC_CONN_ID *peer_dcid) |
3799 | 0 | { |
3800 | 0 | if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server)) |
3801 | 0 | return 0; |
3802 | | |
3803 | | /* Generate an Initial LCID we will use for the connection. */ |
3804 | 0 | if (!ossl_quic_lcidm_generate_initial(ch->lcidm, ch, &ch->cur_local_cid)) |
3805 | 0 | return 0; |
3806 | | |
3807 | 0 | return ch_on_new_conn_common(ch, peer, peer_dcid, NULL); |
3808 | 0 | } |
3809 | | |
3810 | | /** |
3811 | | * Binds a QUIC channel to a specific peer's address and connection IDs. |
3812 | | * |
3813 | | * This function is used to establish a binding between a QUIC channel and a |
3814 | | * peer's address and connection IDs. The binding is performed only if the |
3815 | | * channel is idle and is on the server side. The peer's destination connection |
3816 | | * ID (`peer_dcid`) is mandatory, and the channel's current local connection ID |
3817 | | * is set to this value. |
3818 | | * |
3819 | | * @param ch Pointer to the QUIC_CHANNEL structure representing the |
3820 | | * channel to be bound. |
3821 | | * @param peer Pointer to a BIO_ADDR structure representing the peer's |
3822 | | * address. |
3823 | | * @param peer_scid Pointer to the peer's source connection ID (QUIC_CONN_ID). |
3824 | | * @param peer_dcid Pointer to the peer's destination connection ID |
3825 | | * (QUIC_CONN_ID). This must not be NULL. |
3826 | | * @param peer_odcid Pointer to the original destination connection ID |
3827 | | * (QUIC_CONN_ID) chosen by the peer in its first initial |
3828 | | * packet received without a token. |
3829 | | * |
3830 | | * @return 1 on success, or 0 on failure if the conditions for binding are not |
3831 | | * met (e.g., channel is not idle or not a server, or binding fails). |
3832 | | */ |
3833 | | int ossl_quic_bind_channel(QUIC_CHANNEL *ch, const BIO_ADDR *peer, |
3834 | | const QUIC_CONN_ID *peer_dcid, |
3835 | | const QUIC_CONN_ID *peer_odcid) |
3836 | 0 | { |
3837 | 0 | if (peer_dcid == NULL) |
3838 | 0 | return 0; |
3839 | | |
3840 | 0 | if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server)) |
3841 | 0 | return 0; |
3842 | | |
3843 | 0 | if (!ossl_quic_lcidm_bind_channel(ch->lcidm, ch, peer_dcid)) |
3844 | 0 | return 0; |
3845 | | |
3846 | 0 | ch->cur_local_cid = *peer_dcid; |
3847 | | |
3848 | | /* |
3849 | | * peer_odcid <=> is initial dst conn id chosen by peer in its |
3850 | | * first initial packet we received without token. |
3851 | | */ |
3852 | 0 | return ch_on_new_conn_common(ch, peer, peer_dcid, peer_odcid); |
3853 | 0 | } |
3854 | | |
3855 | | SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch) |
3856 | 0 | { |
3857 | 0 | return ch->tls; |
3858 | 0 | } |
3859 | | |
3860 | | static int ch_init_new_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs, |
3861 | | int can_send, int can_recv) |
3862 | 0 | { |
3863 | 0 | uint64_t rxfc_wnd; |
3864 | 0 | int server_init = ossl_quic_stream_is_server_init(qs); |
3865 | 0 | int local_init = (ch->is_server == server_init); |
3866 | 0 | int is_uni = !ossl_quic_stream_is_bidi(qs); |
3867 | |
|
3868 | 0 | if (can_send) |
3869 | 0 | if ((qs->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL) |
3870 | 0 | goto err; |
3871 | | |
3872 | 0 | if (can_recv) |
3873 | 0 | if ((qs->rstream = ossl_quic_rstream_new(NULL, NULL, ch->rsqp)) == NULL) |
3874 | 0 | goto err; |
3875 | | |
3876 | | /* TXFC */ |
3877 | 0 | if (!ossl_quic_txfc_init(&qs->txfc, &ch->conn_txfc)) |
3878 | 0 | goto err; |
3879 | | |
3880 | 0 | if (ch->got_remote_transport_params) { |
3881 | | /* |
3882 | | * If we already got peer TPs we need to apply the initial CWM credit |
3883 | | * now. If we didn't already get peer TPs this will be done |
3884 | | * automatically for all extant streams when we do. |
3885 | | */ |
3886 | 0 | if (can_send) { |
3887 | 0 | uint64_t cwm; |
3888 | |
|
3889 | 0 | if (is_uni) |
3890 | 0 | cwm = ch->rx_init_max_stream_data_uni; |
3891 | 0 | else if (local_init) |
3892 | 0 | cwm = ch->rx_init_max_stream_data_bidi_local; |
3893 | 0 | else |
3894 | 0 | cwm = ch->rx_init_max_stream_data_bidi_remote; |
3895 | |
|
3896 | 0 | ossl_quic_txfc_bump_cwm(&qs->txfc, cwm); |
3897 | 0 | } |
3898 | 0 | } |
3899 | | |
3900 | | /* RXFC */ |
3901 | 0 | if (!can_recv) |
3902 | 0 | rxfc_wnd = 0; |
3903 | 0 | else if (is_uni) |
3904 | 0 | rxfc_wnd = ch->tx_init_max_stream_data_uni; |
3905 | 0 | else if (local_init) |
3906 | 0 | rxfc_wnd = ch->tx_init_max_stream_data_bidi_local; |
3907 | 0 | else |
3908 | 0 | rxfc_wnd = ch->tx_init_max_stream_data_bidi_remote; |
3909 | |
|
3910 | 0 | if (!ossl_quic_rxfc_init(&qs->rxfc, &ch->conn_rxfc, |
3911 | 0 | rxfc_wnd, |
3912 | 0 | DEFAULT_STREAM_RXFC_MAX_WND_MUL * rxfc_wnd, |
3913 | 0 | get_time, ch)) |
3914 | 0 | goto err; |
3915 | | |
3916 | 0 | return 1; |
3917 | | |
3918 | 0 | err: |
3919 | 0 | ossl_quic_sstream_free(qs->sstream); |
3920 | 0 | qs->sstream = NULL; |
3921 | 0 | ossl_quic_rstream_free(qs->rstream); |
3922 | 0 | qs->rstream = NULL; |
3923 | 0 | return 0; |
3924 | 0 | } |
3925 | | |
3926 | | static uint64_t *ch_get_local_stream_next_ordinal_ptr(QUIC_CHANNEL *ch, |
3927 | | int is_uni) |
3928 | 0 | { |
3929 | 0 | return is_uni ? &ch->next_local_stream_ordinal_uni |
3930 | 0 | : &ch->next_local_stream_ordinal_bidi; |
3931 | 0 | } |
3932 | | |
3933 | | static const uint64_t *ch_get_local_stream_max_ptr(const QUIC_CHANNEL *ch, |
3934 | | int is_uni) |
3935 | 0 | { |
3936 | 0 | return is_uni ? &ch->max_local_streams_uni |
3937 | 0 | : &ch->max_local_streams_bidi; |
3938 | 0 | } |
3939 | | |
3940 | | static const QUIC_RXFC *ch_get_remote_stream_count_rxfc(const QUIC_CHANNEL *ch, |
3941 | | int is_uni) |
3942 | 0 | { |
3943 | 0 | return is_uni ? &ch->max_streams_uni_rxfc |
3944 | 0 | : &ch->max_streams_bidi_rxfc; |
3945 | 0 | } |
3946 | | |
3947 | | int ossl_quic_channel_is_new_local_stream_admissible(QUIC_CHANNEL *ch, |
3948 | | int is_uni) |
3949 | 0 | { |
3950 | 0 | const uint64_t *p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni); |
3951 | |
|
3952 | 0 | return ossl_quic_stream_map_is_local_allowed_by_stream_limit(&ch->qsm, |
3953 | 0 | *p_next_ordinal, |
3954 | 0 | is_uni); |
3955 | 0 | } |
3956 | | |
3957 | | uint64_t ossl_quic_channel_get_local_stream_count_avail(const QUIC_CHANNEL *ch, |
3958 | | int is_uni) |
3959 | 0 | { |
3960 | 0 | const uint64_t *p_next_ordinal, *p_max; |
3961 | |
|
3962 | 0 | p_next_ordinal = ch_get_local_stream_next_ordinal_ptr((QUIC_CHANNEL *)ch, |
3963 | 0 | is_uni); |
3964 | 0 | p_max = ch_get_local_stream_max_ptr(ch, is_uni); |
3965 | |
|
3966 | 0 | return *p_max - *p_next_ordinal; |
3967 | 0 | } |
3968 | | |
3969 | | uint64_t ossl_quic_channel_get_remote_stream_count_avail(const QUIC_CHANNEL *ch, |
3970 | | int is_uni) |
3971 | 0 | { |
3972 | 0 | return ossl_quic_rxfc_get_credit(ch_get_remote_stream_count_rxfc(ch, is_uni)); |
3973 | 0 | } |
3974 | | |
3975 | | QUIC_STREAM *ossl_quic_channel_new_stream_local(QUIC_CHANNEL *ch, int is_uni) |
3976 | 0 | { |
3977 | 0 | QUIC_STREAM *qs; |
3978 | 0 | int type; |
3979 | 0 | uint64_t stream_id; |
3980 | 0 | uint64_t *p_next_ordinal; |
3981 | |
|
3982 | 0 | type = ch->is_server ? QUIC_STREAM_INITIATOR_SERVER |
3983 | 0 | : QUIC_STREAM_INITIATOR_CLIENT; |
3984 | |
|
3985 | 0 | p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni); |
3986 | |
|
3987 | 0 | if (is_uni) |
3988 | 0 | type |= QUIC_STREAM_DIR_UNI; |
3989 | 0 | else |
3990 | 0 | type |= QUIC_STREAM_DIR_BIDI; |
3991 | |
|
3992 | 0 | if (*p_next_ordinal >= ((uint64_t)1) << 62) |
3993 | 0 | return NULL; |
3994 | | |
3995 | 0 | stream_id = ((*p_next_ordinal) << 2) | type; |
3996 | |
|
3997 | 0 | if ((qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id, type)) == NULL) |
3998 | 0 | return NULL; |
3999 | | |
4000 | | /* Locally-initiated stream, so we always want a send buffer. */ |
4001 | 0 | if (!ch_init_new_stream(ch, qs, /*can_send=*/1, /*can_recv=*/!is_uni)) |
4002 | 0 | goto err; |
4003 | | |
4004 | 0 | ++*p_next_ordinal; |
4005 | 0 | return qs; |
4006 | | |
4007 | 0 | err: |
4008 | 0 | ossl_quic_stream_map_release(&ch->qsm, qs); |
4009 | 0 | return NULL; |
4010 | 0 | } |
4011 | | |
4012 | | QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch, |
4013 | | uint64_t stream_id) |
4014 | 0 | { |
4015 | 0 | uint64_t peer_role; |
4016 | 0 | int is_uni; |
4017 | 0 | QUIC_STREAM *qs; |
4018 | |
|
4019 | 0 | peer_role = ch->is_server |
4020 | 0 | ? QUIC_STREAM_INITIATOR_CLIENT |
4021 | 0 | : QUIC_STREAM_INITIATOR_SERVER; |
4022 | |
|
4023 | 0 | if ((stream_id & QUIC_STREAM_INITIATOR_MASK) != peer_role) |
4024 | 0 | return NULL; |
4025 | | |
4026 | 0 | is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI); |
4027 | |
|
4028 | 0 | qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id, |
4029 | 0 | stream_id & (QUIC_STREAM_INITIATOR_MASK | QUIC_STREAM_DIR_MASK)); |
4030 | 0 | if (qs == NULL) |
4031 | 0 | return NULL; |
4032 | | |
4033 | 0 | if (!ch_init_new_stream(ch, qs, /*can_send=*/!is_uni, /*can_recv=*/1)) |
4034 | 0 | goto err; |
4035 | | |
4036 | 0 | if (ch->incoming_stream_auto_reject) |
4037 | 0 | ossl_quic_channel_reject_stream(ch, qs); |
4038 | 0 | else |
4039 | 0 | ossl_quic_stream_map_push_accept_queue(&ch->qsm, qs); |
4040 | |
|
4041 | 0 | return qs; |
4042 | | |
4043 | 0 | err: |
4044 | 0 | ossl_quic_stream_map_release(&ch->qsm, qs); |
4045 | 0 | return NULL; |
4046 | 0 | } |
4047 | | |
4048 | | void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL *ch, |
4049 | | int enable, |
4050 | | uint64_t aec) |
4051 | 0 | { |
4052 | 0 | ch->incoming_stream_auto_reject = (enable != 0); |
4053 | 0 | ch->incoming_stream_auto_reject_aec = aec; |
4054 | 0 | } |
4055 | | |
4056 | | void ossl_quic_channel_reject_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs) |
4057 | 0 | { |
4058 | 0 | OSSL_RTT_INFO rtt_info; |
4059 | |
|
4060 | 0 | ossl_quic_stream_map_stop_sending_recv_part(&ch->qsm, qs, |
4061 | 0 | ch->incoming_stream_auto_reject_aec); |
4062 | |
|
4063 | 0 | ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, qs, |
4064 | 0 | ch->incoming_stream_auto_reject_aec); |
4065 | 0 | qs->deleted = 1; |
4066 | | |
4067 | | /* |
4068 | | * A rejected stream is never placed on the accept queue, so it would |
4069 | | * otherwise never be retired and would consume the peer's stream credit |
4070 | | * for the lifetime of the connection. |
4071 | | */ |
4072 | 0 | ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ch), &rtt_info); |
4073 | 0 | ossl_quic_stream_map_retire_stream_credit(&ch->qsm, qs, |
4074 | 0 | rtt_info.smoothed_rtt); |
4075 | |
|
4076 | 0 | ossl_quic_stream_map_update_state(&ch->qsm, qs); |
4077 | 0 | } |
4078 | | |
4079 | | /* Replace local connection ID in TXP and DEMUX for testing purposes. */ |
4080 | | int ossl_quic_channel_replace_local_cid(QUIC_CHANNEL *ch, |
4081 | | const QUIC_CONN_ID *conn_id) |
4082 | 0 | { |
4083 | | /* Remove the current LCID from the LCIDM. */ |
4084 | 0 | if (!ossl_quic_lcidm_debug_remove(ch->lcidm, &ch->cur_local_cid)) |
4085 | 0 | return 0; |
4086 | 0 | ch->cur_local_cid = *conn_id; |
4087 | | /* Set in the TXP, used only for long header packets. */ |
4088 | 0 | if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid)) |
4089 | 0 | return 0; |
4090 | | /* Add the new LCID to the LCIDM. */ |
4091 | 0 | if (!ossl_quic_lcidm_debug_add(ch->lcidm, ch, &ch->cur_local_cid, |
4092 | 0 | 100)) |
4093 | 0 | return 0; |
4094 | 0 | return 1; |
4095 | 0 | } |
4096 | | |
4097 | | void ossl_quic_channel_set_msg_callback(QUIC_CHANNEL *ch, |
4098 | | ossl_msg_cb msg_callback, |
4099 | | SSL *msg_callback_ssl) |
4100 | 0 | { |
4101 | 0 | ch->msg_callback = msg_callback; |
4102 | 0 | ch->msg_callback_ssl = msg_callback_ssl; |
4103 | 0 | ossl_qtx_set_msg_callback(ch->qtx, msg_callback, msg_callback_ssl); |
4104 | 0 | ossl_quic_tx_packetiser_set_msg_callback(ch->txp, msg_callback, |
4105 | 0 | msg_callback_ssl); |
4106 | | /* |
4107 | | * postpone msg callback setting for tserver until port calls |
4108 | | * port_bind_channel(). |
4109 | | */ |
4110 | 0 | if (ch->is_tserver_ch == 0) |
4111 | 0 | ossl_qrx_set_msg_callback(ch->qrx, msg_callback, msg_callback_ssl); |
4112 | 0 | } |
4113 | | |
4114 | | void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL *ch, |
4115 | | void *msg_callback_arg) |
4116 | 0 | { |
4117 | 0 | ch->msg_callback_arg = msg_callback_arg; |
4118 | 0 | ossl_qtx_set_msg_callback_arg(ch->qtx, msg_callback_arg); |
4119 | 0 | ossl_quic_tx_packetiser_set_msg_callback_arg(ch->txp, msg_callback_arg); |
4120 | | |
4121 | | /* |
4122 | | * postpone msg callback setting for tserver until port calls |
4123 | | * port_bind_channel(). |
4124 | | */ |
4125 | 0 | if (ch->is_tserver_ch == 0) |
4126 | 0 | ossl_qrx_set_msg_callback_arg(ch->qrx, msg_callback_arg); |
4127 | 0 | } |
4128 | | |
4129 | | void ossl_quic_channel_set_txku_threshold_override(QUIC_CHANNEL *ch, |
4130 | | uint64_t tx_pkt_threshold) |
4131 | 0 | { |
4132 | 0 | ch->txku_threshold_override = tx_pkt_threshold; |
4133 | 0 | } |
4134 | | |
4135 | | uint64_t ossl_quic_channel_get_tx_key_epoch(QUIC_CHANNEL *ch) |
4136 | 0 | { |
4137 | 0 | return ossl_qtx_get_key_epoch(ch->qtx); |
4138 | 0 | } |
4139 | | |
4140 | | uint64_t ossl_quic_channel_get_rx_key_epoch(QUIC_CHANNEL *ch) |
4141 | 0 | { |
4142 | 0 | return ossl_qrx_get_key_epoch(ch->qrx); |
4143 | 0 | } |
4144 | | |
4145 | | int ossl_quic_channel_trigger_txku(QUIC_CHANNEL *ch) |
4146 | 0 | { |
4147 | 0 | if (!txku_allowed(ch)) |
4148 | 0 | return 0; |
4149 | | |
4150 | 0 | ch->ku_locally_initiated = 1; |
4151 | 0 | ch_trigger_txku(ch); |
4152 | 0 | return 1; |
4153 | 0 | } |
4154 | | |
4155 | | int ossl_quic_channel_ping(QUIC_CHANNEL *ch) |
4156 | 0 | { |
4157 | 0 | int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level); |
4158 | |
|
4159 | 0 | ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space); |
4160 | |
|
4161 | 0 | return 1; |
4162 | 0 | } |
4163 | | |
4164 | | uint16_t ossl_quic_channel_get_diag_num_rx_ack(QUIC_CHANNEL *ch) |
4165 | 0 | { |
4166 | 0 | return ch->diag_num_rx_ack; |
4167 | 0 | } |
4168 | | |
4169 | | void ossl_quic_channel_get_diag_local_cid(QUIC_CHANNEL *ch, QUIC_CONN_ID *cid) |
4170 | 0 | { |
4171 | 0 | *cid = ch->cur_local_cid; |
4172 | 0 | } |
4173 | | |
4174 | | int ossl_quic_channel_have_generated_transport_params(const QUIC_CHANNEL *ch) |
4175 | 0 | { |
4176 | 0 | return ch->got_local_transport_params; |
4177 | 0 | } |
4178 | | |
4179 | | int ossl_quic_channel_set_max_idle_timeout_request(QUIC_CHANNEL *ch, uint64_t ms) |
4180 | 0 | { |
4181 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4182 | 0 | return 0; |
4183 | | |
4184 | 0 | ch->max_idle_timeout_local_req = ms; |
4185 | 0 | return 1; |
4186 | 0 | } |
4187 | | |
4188 | | uint64_t ossl_quic_channel_get_max_idle_timeout_request(const QUIC_CHANNEL *ch) |
4189 | 0 | { |
4190 | 0 | return ch->max_idle_timeout_local_req; |
4191 | 0 | } |
4192 | | |
4193 | | uint64_t ossl_quic_channel_get_max_idle_timeout_peer_request(const QUIC_CHANNEL *ch) |
4194 | 0 | { |
4195 | 0 | return ch->max_idle_timeout_remote_req; |
4196 | 0 | } |
4197 | | |
4198 | | uint64_t ossl_quic_channel_get_max_idle_timeout_actual(const QUIC_CHANNEL *ch) |
4199 | 0 | { |
4200 | 0 | return ch->max_idle_timeout; |
4201 | 0 | } |
4202 | | |
4203 | | int ossl_quic_channel_set_max_udp_payload_size_request(QUIC_CHANNEL *ch, uint64_t size) |
4204 | 0 | { |
4205 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4206 | 0 | return 0; |
4207 | | |
4208 | 0 | ch->tx_max_udp_payload_size = size; |
4209 | 0 | return 1; |
4210 | 0 | } |
4211 | | |
4212 | | uint64_t ossl_quic_channel_get_max_udp_payload_size_request(const QUIC_CHANNEL *ch) |
4213 | 0 | { |
4214 | 0 | return ch->tx_max_udp_payload_size; |
4215 | 0 | } |
4216 | | |
4217 | | uint64_t ossl_quic_channel_get_max_udp_payload_size_peer_request(const QUIC_CHANNEL *ch) |
4218 | 0 | { |
4219 | 0 | return ch->rx_max_udp_payload_size; |
4220 | 0 | } |
4221 | | |
4222 | | int ossl_quic_channel_set_max_data_request(QUIC_CHANNEL *ch, uint64_t max_data) |
4223 | 0 | { |
4224 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4225 | 0 | return 0; |
4226 | | |
4227 | 0 | if (max_data > UINT64_MAX / DEFAULT_CONN_RXFC_MAX_WND_MUL) |
4228 | 0 | return 0; |
4229 | | |
4230 | 0 | if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL, |
4231 | 0 | max_data, DEFAULT_CONN_RXFC_MAX_WND_MUL * max_data, |
4232 | 0 | get_time, ch)) |
4233 | 0 | return 0; |
4234 | | |
4235 | 0 | ch->tx_init_max_data = max_data; |
4236 | |
|
4237 | 0 | return 1; |
4238 | 0 | } |
4239 | | |
4240 | | uint64_t ossl_quic_channel_get_max_data_request(const QUIC_CHANNEL *ch) |
4241 | 0 | { |
4242 | 0 | return ch->tx_init_max_data; |
4243 | 0 | } |
4244 | | |
4245 | | uint64_t ossl_quic_channel_get_max_data_peer_request(const QUIC_CHANNEL *ch) |
4246 | 0 | { |
4247 | 0 | return ch->rx_init_max_data; |
4248 | 0 | } |
4249 | | |
4250 | | int ossl_quic_channel_set_max_stream_data_request(QUIC_CHANNEL *ch, uint64_t max_data, int is_uni, int is_remote) |
4251 | 0 | { |
4252 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4253 | 0 | return 0; |
4254 | | |
4255 | | /* no need to update fc here since no stream is created yet */ |
4256 | 0 | if (is_uni) { |
4257 | 0 | ch->tx_init_max_stream_data_uni = max_data; |
4258 | 0 | } else { |
4259 | 0 | if (is_remote) |
4260 | 0 | ch->tx_init_max_stream_data_bidi_remote = max_data; |
4261 | 0 | else |
4262 | 0 | ch->tx_init_max_stream_data_bidi_local = max_data; |
4263 | 0 | } |
4264 | |
|
4265 | 0 | return 1; |
4266 | 0 | } |
4267 | | |
4268 | | uint64_t ossl_quic_channel_get_max_stream_data_request(const QUIC_CHANNEL *ch, int is_uni, int is_remote) |
4269 | 0 | { |
4270 | 0 | if (is_uni) |
4271 | 0 | return ch->tx_init_max_stream_data_uni; |
4272 | 0 | else |
4273 | 0 | return is_remote ? ch->tx_init_max_stream_data_bidi_remote : ch->tx_init_max_stream_data_bidi_local; |
4274 | 0 | } |
4275 | | |
4276 | | uint64_t ossl_quic_channel_get_max_stream_data_peer_request(const QUIC_CHANNEL *ch, int is_uni, int is_remote) |
4277 | 0 | { |
4278 | 0 | if (is_uni) |
4279 | 0 | return ch->rx_init_max_stream_data_uni; |
4280 | 0 | else |
4281 | 0 | return is_remote ? ch->rx_init_max_stream_data_bidi_remote : ch->rx_init_max_stream_data_bidi_local; |
4282 | 0 | } |
4283 | | |
4284 | | int ossl_quic_channel_set_max_streams_request(QUIC_CHANNEL *ch, uint64_t max_streams, int is_uni) |
4285 | 0 | { |
4286 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4287 | 0 | return 0; |
4288 | | |
4289 | 0 | if (is_uni) { |
4290 | 0 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_uni_rxfc, |
4291 | 0 | max_streams, get_time, ch)) |
4292 | 0 | return 0; |
4293 | | |
4294 | 0 | ch->tx_init_max_streams_uni = max_streams; |
4295 | 0 | } else { |
4296 | 0 | if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_bidi_rxfc, |
4297 | 0 | max_streams, get_time, ch)) |
4298 | 0 | return 0; |
4299 | | |
4300 | 0 | ch->tx_init_max_streams_bidi = max_streams; |
4301 | 0 | } |
4302 | | |
4303 | 0 | return 1; |
4304 | 0 | } |
4305 | | |
4306 | | uint64_t ossl_quic_channel_get_max_streams_request(const QUIC_CHANNEL *ch, int is_uni) |
4307 | 0 | { |
4308 | 0 | return is_uni ? ch->tx_init_max_streams_uni : ch->tx_init_max_streams_bidi; |
4309 | 0 | } |
4310 | | |
4311 | | uint64_t ossl_quic_channel_get_max_streams_peer_request(const QUIC_CHANNEL *ch, int is_uni) |
4312 | 0 | { |
4313 | 0 | return is_uni ? ch->rx_init_max_streams_uni : ch->rx_init_max_streams_bidi; |
4314 | 0 | } |
4315 | | |
4316 | | int ossl_quic_channel_set_ack_delay_exponent_request(QUIC_CHANNEL *ch, uint64_t exp) |
4317 | 0 | { |
4318 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4319 | 0 | return 0; |
4320 | | |
4321 | | /* |
4322 | | * ossl_quic_tx_packetiser_args_st::ack_delay_exponent is uint32_t, |
4323 | | * but quic_channel_st::tx_ack_delay_exp is unsigned char, checking |
4324 | | * against the smaller type. |
4325 | | */ |
4326 | 0 | if (exp > UCHAR_MAX) |
4327 | 0 | return 0; |
4328 | | |
4329 | 0 | if (!ossl_quic_tx_packetiser_set_ack_delay_exponent(ch->txp, (uint32_t)exp)) |
4330 | 0 | return 0; |
4331 | | |
4332 | 0 | ch->tx_ack_delay_exp = (unsigned char)exp; |
4333 | |
|
4334 | 0 | return 1; |
4335 | 0 | } |
4336 | | |
4337 | | uint64_t ossl_quic_channel_get_ack_delay_exponent_request(const QUIC_CHANNEL *ch) |
4338 | 0 | { |
4339 | 0 | return ch->tx_ack_delay_exp; |
4340 | 0 | } |
4341 | | |
4342 | | uint64_t ossl_quic_channel_get_ack_delay_exponent_peer_request(const QUIC_CHANNEL *ch) |
4343 | 0 | { |
4344 | 0 | return ch->rx_ack_delay_exp; |
4345 | 0 | } |
4346 | | |
4347 | | int ossl_quic_channel_set_max_ack_delay_request(QUIC_CHANNEL *ch, uint64_t ms) |
4348 | 0 | { |
4349 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4350 | 0 | return 0; |
4351 | | |
4352 | 0 | ch->tx_max_ack_delay = ms; |
4353 | 0 | ossl_ackm_set_tx_max_ack_delay(ch->ackm, ossl_ms2time(ch->tx_max_ack_delay)); |
4354 | 0 | return 1; |
4355 | 0 | } |
4356 | | |
4357 | | uint64_t ossl_quic_channel_get_max_ack_delay_request(const QUIC_CHANNEL *ch) |
4358 | 0 | { |
4359 | 0 | return ch->tx_max_ack_delay; |
4360 | 0 | } |
4361 | | |
4362 | | uint64_t ossl_quic_channel_get_max_ack_delay_peer_request(const QUIC_CHANNEL *ch) |
4363 | 0 | { |
4364 | 0 | return ch->rx_max_ack_delay; |
4365 | 0 | } |
4366 | | |
4367 | | int ossl_quic_channel_set_disable_active_migration_request(QUIC_CHANNEL *ch, uint64_t disable) |
4368 | 0 | { |
4369 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4370 | 0 | return 0; |
4371 | | |
4372 | 0 | if (disable > UCHAR_MAX) |
4373 | 0 | return 0; |
4374 | | |
4375 | 0 | ch->tx_disable_active_migration = (unsigned char)disable; |
4376 | 0 | return 1; |
4377 | 0 | } |
4378 | | |
4379 | | uint64_t ossl_quic_channel_get_disable_active_migration_request(const QUIC_CHANNEL *ch) |
4380 | 0 | { |
4381 | 0 | return ch->tx_disable_active_migration; |
4382 | 0 | } |
4383 | | |
4384 | | uint64_t ossl_quic_channel_get_disable_active_migration_peer_request(const QUIC_CHANNEL *ch) |
4385 | 0 | { |
4386 | 0 | return ch->rx_disable_active_migration; |
4387 | 0 | } |
4388 | | |
4389 | | int ossl_quic_channel_set_active_conn_id_limit_request(QUIC_CHANNEL *ch, uint64_t limit) |
4390 | 0 | { |
4391 | 0 | if (ossl_quic_channel_have_generated_transport_params(ch)) |
4392 | 0 | return 0; |
4393 | | |
4394 | 0 | ch->tx_active_conn_id_limit = limit; |
4395 | 0 | return 1; |
4396 | 0 | } |
4397 | | |
4398 | | uint64_t ossl_quic_channel_get_active_conn_id_limit_request(const QUIC_CHANNEL *ch) |
4399 | 0 | { |
4400 | 0 | return ch->tx_active_conn_id_limit; |
4401 | 0 | } |
4402 | | |
4403 | | uint64_t ossl_quic_channel_get_active_conn_id_limit_peer_request(const QUIC_CHANNEL *ch) |
4404 | 0 | { |
4405 | 0 | return ch->rx_active_conn_id_limit; |
4406 | 0 | } |
4407 | | |
4408 | | uint64_t ossl_quic_channel_get_path_challenge_count(const QUIC_CHANNEL *ch) |
4409 | 0 | { |
4410 | 0 | return ch->path_challenge_rx; |
4411 | 0 | } |
4412 | | |
4413 | | uint64_t ossl_quic_channel_get_path_response_count(const QUIC_CHANNEL *ch) |
4414 | 0 | { |
4415 | 0 | return ch->path_response_tx; |
4416 | 0 | } |