Coverage Report

Created: 2025-12-31 06:58

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