Coverage Report

Created: 2025-08-28 07:07

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