Coverage Report

Created: 2025-08-11 07:04

/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
52.6k
#define INIT_CRYPTO_RECV_BUF_LEN    16384
28
52.6k
#define INIT_CRYPTO_SEND_BUF_LEN    16384
29
36.5k
#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
20.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
17.5k
#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
1.84M
{
109
1.84M
#ifndef OPENSSL_NO_QLOG
110
1.84M
    QLOG_TRACE_INFO qti = {0};
111
112
1.84M
    if (ch->qlog != NULL)
113
0
        return ch->qlog;
114
115
1.84M
    if (!ch->use_qlog)
116
1.80M
        return NULL;
117
118
35.5k
    if (ch->is_server && ch->init_dcid.id_len == 0)
119
0
        return NULL;
120
121
35.5k
    qti.odcid       = ch->init_dcid;
122
35.5k
    qti.title       = ch->qlog_title;
123
35.5k
    qti.description = NULL;
124
35.5k
    qti.group_id    = NULL;
125
35.5k
    qti.is_server   = ch->is_server;
126
35.5k
    qti.now_cb      = get_time;
127
35.5k
    qti.now_cb_arg  = ch;
128
35.5k
    if ((ch->qlog = ossl_qlog_new_from_env(&qti)) == NULL) {
129
35.5k
        ch->use_qlog = 0; /* don't try again */
130
35.5k
        return NULL;
131
35.5k
    }
132
133
0
    return ch->qlog;
134
#else
135
    return NULL;
136
#endif
137
35.5k
}
138
139
QUIC_NEEDS_LOCK
140
static QLOG *ch_get_qlog_cb(void *arg)
141
1.02M
{
142
1.02M
    QUIC_CHANNEL *ch = arg;
143
144
1.02M
    return ch_get_qlog(ch);
145
1.02M
}
146
147
/*
148
 * QUIC Channel Initialization and Teardown
149
 * ========================================
150
 */
151
35.0k
#define DEFAULT_INIT_CONN_RXFC_WND      (768 * 1024)
152
17.5k
#define DEFAULT_CONN_RXFC_MAX_WND_MUL   20
153
154
52.6k
#define DEFAULT_INIT_STREAM_RXFC_WND    (512 * 1024)
155
95.9k
#define DEFAULT_STREAM_RXFC_MAX_WND_MUL 12
156
157
35.0k
#define DEFAULT_INIT_CONN_MAX_STREAMS           100
158
159
static int ch_init(QUIC_CHANNEL *ch)
160
17.5k
{
161
17.5k
    OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0};
162
17.5k
    OSSL_QTX_ARGS qtx_args = {0};
163
17.5k
    OSSL_QRX_ARGS qrx_args = {0};
164
17.5k
    QUIC_TLS_ARGS tls_args = {0};
165
17.5k
    uint32_t pn_space;
166
17.5k
    size_t rx_short_dcid_len;
167
17.5k
    size_t tx_init_dcid_len;
168
169
17.5k
    if (ch->port == NULL || ch->lcidm == NULL || ch->srtm == NULL)
170
0
        goto err;
171
172
17.5k
    rx_short_dcid_len = ossl_quic_port_get_rx_short_dcid_len(ch->port);
173
17.5k
    tx_init_dcid_len = ossl_quic_port_get_tx_init_dcid_len(ch->port);
174
175
    /* For clients, generate our initial DCID. */
176
17.5k
    if (!ch->is_server
177
17.5k
        && !ossl_quic_gen_rand_conn_id(ch->port->engine->libctx, tx_init_dcid_len,
178
17.5k
                                       &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
17.5k
    qtx_args.libctx             = ch->port->engine->libctx;
183
17.5k
    qtx_args.get_qlog_cb        = ch_get_qlog_cb;
184
17.5k
    qtx_args.get_qlog_cb_arg    = ch;
185
17.5k
    qtx_args.mdpl               = QUIC_MIN_INITIAL_DGRAM_LEN;
186
17.5k
    ch->rx_max_udp_payload_size = qtx_args.mdpl;
187
188
17.5k
    ch->ping_deadline = ossl_time_infinite();
189
190
17.5k
    ch->qtx = ossl_qtx_new(&qtx_args);
191
17.5k
    if (ch->qtx == NULL)
192
0
        goto err;
193
194
17.5k
    ch->txpim = ossl_quic_txpim_new();
195
17.5k
    if (ch->txpim == NULL)
196
0
        goto err;
197
198
17.5k
    ch->cfq = ossl_quic_cfq_new();
199
17.5k
    if (ch->cfq == NULL)
200
0
        goto err;
201
202
17.5k
    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
17.5k
    ch->tx_init_max_stream_data_bidi_local  = DEFAULT_INIT_STREAM_RXFC_WND;
210
17.5k
    ch->tx_init_max_stream_data_bidi_remote = DEFAULT_INIT_STREAM_RXFC_WND;
211
17.5k
    ch->tx_init_max_stream_data_uni         = DEFAULT_INIT_STREAM_RXFC_WND;
212
213
17.5k
    if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL,
214
17.5k
                             DEFAULT_INIT_CONN_RXFC_WND,
215
17.5k
                             DEFAULT_CONN_RXFC_MAX_WND_MUL *
216
17.5k
                             DEFAULT_INIT_CONN_RXFC_WND,
217
17.5k
                             get_time, ch))
218
0
        goto err;
219
220
70.1k
    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
221
52.6k
        if (!ossl_quic_rxfc_init_standalone(&ch->crypto_rxfc[pn_space],
222
52.6k
                                            INIT_CRYPTO_RECV_BUF_LEN,
223
52.6k
                                            get_time, ch))
224
0
            goto err;
225
226
17.5k
    if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_bidi_rxfc,
227
17.5k
                                        DEFAULT_INIT_CONN_MAX_STREAMS,
228
17.5k
                                        get_time, ch))
229
0
        goto err;
230
231
17.5k
    if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_uni_rxfc,
232
17.5k
                                        DEFAULT_INIT_CONN_MAX_STREAMS,
233
17.5k
                                        get_time, ch))
234
0
        goto err;
235
236
17.5k
    if (!ossl_statm_init(&ch->statm))
237
0
        goto err;
238
239
17.5k
    ch->have_statm = 1;
240
17.5k
    ch->cc_method = &ossl_cc_newreno_method;
241
17.5k
    if ((ch->cc_data = ch->cc_method->new(get_time, ch)) == NULL)
242
0
        goto err;
243
244
17.5k
    if ((ch->ackm = ossl_ackm_new(get_time, ch, &ch->statm,
245
17.5k
                                  ch->cc_method, ch->cc_data,
246
17.5k
                                  ch->is_server)) == NULL)
247
0
        goto err;
248
249
17.5k
    if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch,
250
17.5k
                                   &ch->max_streams_bidi_rxfc,
251
17.5k
                                   &ch->max_streams_uni_rxfc,
252
17.5k
                                   ch->is_server))
253
0
        goto err;
254
255
17.5k
    ch->have_qsm = 1;
256
257
17.5k
    if (!ch->is_server
258
17.5k
        && !ossl_quic_lcidm_generate_initial(ch->lcidm, ch, &ch->init_scid))
259
0
        goto err;
260
261
17.5k
    txp_args.cur_scid               = ch->init_scid;
262
17.5k
    txp_args.cur_dcid               = ch->init_dcid;
263
17.5k
    txp_args.ack_delay_exponent     = 3;
264
17.5k
    txp_args.qtx                    = ch->qtx;
265
17.5k
    txp_args.txpim                  = ch->txpim;
266
17.5k
    txp_args.cfq                    = ch->cfq;
267
17.5k
    txp_args.ackm                   = ch->ackm;
268
17.5k
    txp_args.qsm                    = &ch->qsm;
269
17.5k
    txp_args.conn_txfc              = &ch->conn_txfc;
270
17.5k
    txp_args.conn_rxfc              = &ch->conn_rxfc;
271
17.5k
    txp_args.max_streams_bidi_rxfc  = &ch->max_streams_bidi_rxfc;
272
17.5k
    txp_args.max_streams_uni_rxfc   = &ch->max_streams_uni_rxfc;
273
17.5k
    txp_args.cc_method              = ch->cc_method;
274
17.5k
    txp_args.cc_data                = ch->cc_data;
275
17.5k
    txp_args.now                    = get_time;
276
17.5k
    txp_args.now_arg                = ch;
277
17.5k
    txp_args.get_qlog_cb            = ch_get_qlog_cb;
278
17.5k
    txp_args.get_qlog_cb_arg        = ch;
279
17.5k
    txp_args.protocol_version       = QUIC_VERSION_1;
280
281
70.1k
    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
282
52.6k
        ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_SEND_BUF_LEN);
283
52.6k
        if (ch->crypto_send[pn_space] == NULL)
284
0
            goto err;
285
286
52.6k
        txp_args.crypto[pn_space] = ch->crypto_send[pn_space];
287
52.6k
    }
288
289
17.5k
    ch->txp = ossl_quic_tx_packetiser_new(&txp_args);
290
17.5k
    if (ch->txp == NULL)
291
0
        goto err;
292
293
    /* clients have no amplification limit, so are considered always valid */
294
17.5k
    if (!ch->is_server)
295
17.5k
        ossl_quic_tx_packetiser_set_validated(ch->txp);
296
297
17.5k
    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
17.5k
    if (ch->qrx == NULL && ch->is_tserver_ch == 0) {
310
        /* we are regular client, create channel */
311
17.5k
        qrx_args.libctx             = ch->port->engine->libctx;
312
17.5k
        qrx_args.demux              = ch->port->demux;
313
17.5k
        qrx_args.short_conn_id_len  = rx_short_dcid_len;
314
17.5k
        qrx_args.max_deferred       = 32;
315
316
17.5k
        if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL)
317
0
            goto err;
318
17.5k
    }
319
320
17.5k
    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
17.5k
        if (!ossl_qrx_set_late_validation_cb(ch->qrx,
327
17.5k
                                             rx_late_validate,
328
17.5k
                                             ch))
329
0
            goto err;
330
331
17.5k
        if (!ossl_qrx_set_key_update_cb(ch->qrx,
332
17.5k
                                        rxku_detected,
333
17.5k
                                        ch))
334
0
            goto err;
335
17.5k
    }
336
337
338
70.1k
    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
339
52.6k
        ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL, 0);
340
52.6k
        if (ch->crypto_recv[pn_space] == NULL)
341
0
            goto err;
342
52.6k
    }
343
344
    /* Plug in the TLS handshake layer. */
345
17.5k
    tls_args.s                          = ch->tls;
346
17.5k
    tls_args.crypto_send_cb             = ch_on_crypto_send;
347
17.5k
    tls_args.crypto_send_cb_arg         = ch;
348
17.5k
    tls_args.crypto_recv_rcd_cb         = ch_on_crypto_recv_record;
349
17.5k
    tls_args.crypto_recv_rcd_cb_arg     = ch;
350
17.5k
    tls_args.crypto_release_rcd_cb      = ch_on_crypto_release_record;
351
17.5k
    tls_args.crypto_release_rcd_cb_arg  = ch;
352
17.5k
    tls_args.yield_secret_cb            = ch_on_handshake_yield_secret;
353
17.5k
    tls_args.yield_secret_cb_arg        = ch;
354
17.5k
    tls_args.got_transport_params_cb    = ch_on_transport_params;
355
17.5k
    tls_args.got_transport_params_cb_arg= ch;
356
17.5k
    tls_args.handshake_complete_cb      = ch_on_handshake_complete;
357
17.5k
    tls_args.handshake_complete_cb_arg  = ch;
358
17.5k
    tls_args.alert_cb                   = ch_on_handshake_alert;
359
17.5k
    tls_args.alert_cb_arg               = ch;
360
17.5k
    tls_args.is_server                  = ch->is_server;
361
17.5k
    tls_args.ossl_quic                  = 1;
362
363
17.5k
    if ((ch->qtls = ossl_quic_tls_new(&tls_args)) == NULL)
364
0
        goto err;
365
366
17.5k
    ch->tx_max_ack_delay        = DEFAULT_MAX_ACK_DELAY;
367
17.5k
    ch->rx_max_ack_delay        = QUIC_DEFAULT_MAX_ACK_DELAY;
368
17.5k
    ch->rx_ack_delay_exp        = QUIC_DEFAULT_ACK_DELAY_EXP;
369
17.5k
    ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT;
370
17.5k
    ch->tx_enc_level            = QUIC_ENC_LEVEL_INITIAL;
371
17.5k
    ch->rx_enc_level            = QUIC_ENC_LEVEL_INITIAL;
372
17.5k
    ch->txku_threshold_override = UINT64_MAX;
373
374
17.5k
    ch->max_idle_timeout_local_req  = QUIC_DEFAULT_IDLE_TIMEOUT;
375
17.5k
    ch->max_idle_timeout_remote_req = 0;
376
17.5k
    ch->max_idle_timeout            = ch->max_idle_timeout_local_req;
377
378
17.5k
    ossl_ackm_set_tx_max_ack_delay(ch->ackm, ossl_ms2time(ch->tx_max_ack_delay));
379
17.5k
    ossl_ackm_set_rx_max_ack_delay(ch->ackm, ossl_ms2time(ch->rx_max_ack_delay));
380
381
17.5k
    ch_update_idle(ch);
382
17.5k
    ossl_list_ch_insert_tail(&ch->port->channel_list, ch);
383
17.5k
    ch->on_port_list = 1;
384
17.5k
    return 1;
385
386
0
err:
387
0
    ch_cleanup(ch);
388
0
    return 0;
389
17.5k
}
390
391
static void ch_cleanup(QUIC_CHANNEL *ch)
392
35.5k
{
393
35.5k
    uint32_t pn_space;
394
395
35.5k
    if (ch->ackm != NULL)
396
35.5k
        for (pn_space = QUIC_PN_SPACE_INITIAL;
397
142k
             pn_space < QUIC_PN_SPACE_NUM;
398
106k
             ++pn_space)
399
106k
            ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
400
401
35.5k
    ossl_quic_lcidm_cull(ch->lcidm, ch);
402
35.5k
    ossl_quic_srtm_cull(ch->srtm, ch);
403
35.5k
    ossl_quic_tx_packetiser_free(ch->txp);
404
35.5k
    ossl_quic_txpim_free(ch->txpim);
405
35.5k
    ossl_quic_cfq_free(ch->cfq);
406
35.5k
    ossl_qtx_free(ch->qtx);
407
35.5k
    if (ch->cc_data != NULL)
408
35.5k
        ch->cc_method->free(ch->cc_data);
409
35.5k
    if (ch->have_statm)
410
35.5k
        ossl_statm_destroy(&ch->statm);
411
35.5k
    ossl_ackm_free(ch->ackm);
412
413
35.5k
    if (ch->have_qsm)
414
35.5k
        ossl_quic_stream_map_cleanup(&ch->qsm);
415
416
142k
    for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
417
106k
        ossl_quic_sstream_free(ch->crypto_send[pn_space]);
418
106k
        ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
419
106k
    }
420
421
35.5k
    ossl_qrx_pkt_release(ch->qrx_pkt);
422
35.5k
    ch->qrx_pkt = NULL;
423
424
35.5k
    ossl_quic_tls_free(ch->qtls);
425
35.5k
    ossl_qrx_free(ch->qrx);
426
35.5k
    OPENSSL_free(ch->local_transport_params);
427
35.5k
    OPENSSL_free((char *)ch->terminate_cause.reason);
428
35.5k
    OSSL_ERR_STATE_free(ch->err_state);
429
35.5k
    OPENSSL_free(ch->ack_range_scratch);
430
35.5k
    OPENSSL_free(ch->pending_new_token);
431
432
35.5k
    if (ch->on_port_list) {
433
35.5k
        ossl_list_ch_remove(&ch->port->channel_list, ch);
434
35.5k
        ch->on_port_list = 0;
435
35.5k
    }
436
437
35.5k
#ifndef OPENSSL_NO_QLOG
438
35.5k
    if (ch->qlog != NULL)
439
0
        ossl_qlog_flush(ch->qlog); /* best effort */
440
441
35.5k
    OPENSSL_free(ch->qlog_title);
442
35.5k
    ossl_qlog_free(ch->qlog);
443
35.5k
#endif
444
35.5k
}
445
446
int ossl_quic_channel_init(QUIC_CHANNEL *ch)
447
17.5k
{
448
17.5k
    return ch_init(ch);
449
17.5k
}
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
17.5k
{
464
17.5k
    QUIC_CHANNEL *ch = NULL;
465
466
17.5k
    if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL)
467
0
        return NULL;
468
469
17.5k
    ch->port           = args->port;
470
17.5k
    ch->is_server      = args->is_server;
471
17.5k
    ch->tls            = args->tls;
472
17.5k
    ch->lcidm          = args->lcidm;
473
17.5k
    ch->srtm           = args->srtm;
474
17.5k
    ch->qrx            = args->qrx;
475
17.5k
    ch->is_tserver_ch  = args->is_tserver_ch;
476
17.5k
#ifndef OPENSSL_NO_QLOG
477
17.5k
    ch->use_qlog    = args->use_qlog;
478
479
17.5k
    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
17.5k
#endif
486
487
17.5k
    return ch;
488
17.5k
}
489
490
void ossl_quic_channel_free(QUIC_CHANNEL *ch)
491
46.3k
{
492
46.3k
    if (ch == NULL)
493
0
        return;
494
495
46.3k
    ch_cleanup(ch);
496
46.3k
    OPENSSL_free(ch);
497
46.3k
}
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
46.3k
{
522
46.3k
    if (ch->state != QUIC_CHANNEL_STATE_IDLE)
523
0
        return 0;
524
525
46.3k
    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
46.3k
    if (!BIO_ADDR_copy(&ch->cur_peer_addr, peer_addr)) {
532
0
        ch->addressed_mode = 0;
533
0
        return 0;
534
0
    }
535
46.3k
    ch->addressed_mode = 1;
536
537
46.3k
    return 1;
538
46.3k
}
539
540
QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch)
541
98.7M
{
542
98.7M
    return ossl_quic_port_get0_reactor(ch->port);
543
98.7M
}
544
545
QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch)
546
24.3M
{
547
24.3M
    return &ch->qsm;
548
24.3M
}
549
550
OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch)
551
79.5k
{
552
79.5k
    return &ch->statm;
553
79.5k
}
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.52M
{
622
2.52M
    return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE;
623
2.52M
}
624
625
int ossl_quic_channel_is_closing(const QUIC_CHANNEL *ch)
626
332M
{
627
332M
    return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING;
628
332M
}
629
630
static int ossl_quic_channel_is_draining(const QUIC_CHANNEL *ch)
631
272M
{
632
272M
    return ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
633
272M
}
634
635
static int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch)
636
213M
{
637
213M
    return ossl_quic_channel_is_closing(ch)
638
213M
        || ossl_quic_channel_is_draining(ch);
639
213M
}
640
641
int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch)
642
332M
{
643
332M
    return ch->state == QUIC_CHANNEL_STATE_TERMINATED;
644
332M
}
645
646
int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch)
647
95.5M
{
648
95.5M
    return ossl_quic_channel_is_terminating(ch)
649
95.5M
        || ossl_quic_channel_is_terminated(ch);
650
95.5M
}
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
132M
{
660
132M
    return ch->handshake_complete;
661
132M
}
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
88.3M
{
702
88.3M
    QUIC_CHANNEL *ch = arg;
703
704
88.3M
    return ossl_quic_port_get_time(ch->port);
705
88.3M
}
706
707
/* Used by QSM. */
708
static uint64_t get_stream_limit(int uni, void *arg)
709
65.6k
{
710
65.6k
    QUIC_CHANNEL *ch = arg;
711
712
65.6k
    return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi;
713
65.6k
}
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
731k
{
721
731k
    QUIC_CHANNEL *ch = arg;
722
723
    /* Potential duplicates should not be processed. */
724
731k
    if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space))
725
146k
        return 0;
726
727
585k
    return 1;
728
731k
}
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
15.9k
{
737
15.9k
    uint64_t next_pn
738
15.9k
        = ossl_quic_tx_packetiser_get_next_pn(ch->txp, QUIC_PN_SPACE_APP);
739
740
15.9k
    if (!ossl_quic_pn_valid(next_pn)
741
15.9k
        || !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
15.9k
    ch->txku_in_progress    = 1;
748
15.9k
    ch->txku_pn             = next_pn;
749
15.9k
    ch->rxku_expected       = ch->ku_locally_initiated;
750
15.9k
}
751
752
QUIC_NEEDS_LOCK
753
static int txku_in_progress(QUIC_CHANNEL *ch)
754
15.9M
{
755
15.9M
    if (ch->txku_in_progress
756
15.9M
        && ossl_ackm_get_largest_acked(ch->ackm, QUIC_PN_SPACE_APP) >= ch->txku_pn) {
757
13.4k
        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.4k
        ch->txku_in_progress        = 0;
769
13.4k
        ch->txku_cooldown_deadline  = ossl_time_add(get_time(ch),
770
13.4k
                                                    ossl_time_multiply(pto, 3));
771
13.4k
    }
772
773
15.9M
    return ch->txku_in_progress;
774
15.9M
}
775
776
QUIC_NEEDS_LOCK
777
static int txku_allowed(QUIC_CHANNEL *ch)
778
59.1M
{
779
59.1M
    return ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT /* Sanity check. */
780
        /* Strict RFC 9001 criterion for TXKU. */
781
59.1M
        && ch->handshake_confirmed
782
59.1M
        && !txku_in_progress(ch);
783
59.1M
}
784
785
QUIC_NEEDS_LOCK
786
static int txku_recommendable(QUIC_CHANNEL *ch)
787
59.1M
{
788
59.1M
    if (!txku_allowed(ch))
789
44.7M
        return 0;
790
791
14.3M
    return
792
        /* Recommended RFC 9001 criterion for TXKU. */
793
14.3M
        ossl_time_compare(get_time(ch), ch->txku_cooldown_deadline) >= 0
794
        /* Some additional sensible criteria. */
795
14.3M
        && !ch->rxku_in_progress
796
14.3M
        && !ch->rxku_pending_confirm;
797
59.1M
}
798
799
QUIC_NEEDS_LOCK
800
static int txku_desirable(QUIC_CHANNEL *ch)
801
10.6M
{
802
10.6M
    uint64_t cur_pkt_count, max_pkt_count, thresh_pkt_count;
803
10.6M
    const uint32_t enc_level = QUIC_ENC_LEVEL_1RTT;
804
805
    /* Check AEAD limit to determine if we should perform a spontaneous TXKU. */
806
10.6M
    cur_pkt_count = ossl_qtx_get_cur_epoch_pkt_count(ch->qtx, enc_level);
807
10.6M
    max_pkt_count = ossl_qtx_get_max_epoch_pkt_count(ch->qtx, enc_level);
808
809
10.6M
    thresh_pkt_count = max_pkt_count / 2;
810
10.6M
    if (ch->txku_threshold_override != UINT64_MAX)
811
0
        thresh_pkt_count = ch->txku_threshold_override;
812
813
10.6M
    return cur_pkt_count >= thresh_pkt_count;
814
10.6M
}
815
816
QUIC_NEEDS_LOCK
817
static void ch_maybe_trigger_spontaneous_txku(QUIC_CHANNEL *ch)
818
59.1M
{
819
59.1M
    if (!txku_recommendable(ch) || !txku_desirable(ch))
820
59.1M
        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.1k
{
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.1k
    return ch->handshake_confirmed && !ch->rxku_pending_confirm;
845
16.1k
}
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.1k
{
860
16.1k
    QUIC_CHANNEL *ch = arg;
861
16.1k
    enum rxku_decision decision;
862
16.1k
    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.1k
    assert(!ch->rxku_in_progress);
869
870
16.1k
    if (!rxku_allowed(ch))
871
        /* Is RXKU even allowed at this time? */
872
165
        decision = DECISION_PROTOCOL_VIOLATION;
873
874
15.9k
    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
15.9k
    else
884
        /*
885
         * Otherwise, a peer triggering a KU means we have to trigger a KU also.
886
         */
887
15.9k
        decision = DECISION_SOLICITED_TXKU;
888
889
16.1k
    if (decision == DECISION_PROTOCOL_VIOLATION) {
890
165
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
891
165
                                               0, "RX key update again too soon");
892
165
        return;
893
165
    }
894
895
15.9k
    pto = ossl_ackm_get_pto_duration(ch->ackm);
896
897
15.9k
    ch->ku_locally_initiated        = 0;
898
15.9k
    ch->rxku_in_progress            = 1;
899
15.9k
    ch->rxku_pending_confirm        = 1;
900
15.9k
    ch->rxku_trigger_pn             = pn;
901
15.9k
    ch->rxku_update_end_deadline    = ossl_time_add(get_time(ch), pto);
902
15.9k
    ch->rxku_expected               = 0;
903
904
15.9k
    if (decision == DECISION_SOLICITED_TXKU)
905
        /* NOT gated by usual txku_allowed() */
906
15.9k
        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
15.9k
    ossl_quic_tx_packetiser_schedule_ack(ch->txp, QUIC_PN_SPACE_APP);
924
15.9k
}
925
926
/* Called per tick to handle RXKU timer events. */
927
QUIC_NEEDS_LOCK
928
static void ch_rxku_tick(QUIC_CHANNEL *ch)
929
59.1M
{
930
59.1M
    if (!ch->rxku_in_progress
931
59.1M
        || ossl_time_compare(get_time(ch), ch->rxku_update_end_deadline) < 0)
932
59.1M
        return;
933
934
13.4k
    ch->rxku_update_end_deadline    = ossl_time_infinite();
935
13.4k
    ch->rxku_in_progress            = 0;
936
937
13.4k
    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.4k
                                               "RXKU cooldown internal error");
940
13.4k
}
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
4.43M
{
946
4.43M
    QUIC_CHANNEL *ch = arg;
947
948
4.43M
    if (pn_space != QUIC_PN_SPACE_APP || !ch->rxku_pending_confirm
949
4.43M
        || !ossl_quic_frame_ack_contains_pn(ack, ch->rxku_trigger_pn))
950
4.38M
        return;
951
952
    /*
953
     * Defer clearing rxku_pending_confirm until TXP generate call returns
954
     * successfully.
955
     */
956
49.7k
    ch->rxku_pending_confirm_done = 1;
957
49.7k
}
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
57.9k
{
966
57.9k
    int ret;
967
57.9k
    QUIC_CHANNEL *ch = arg;
968
57.9k
    uint32_t enc_level = ch->tx_enc_level;
969
57.9k
    uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
970
57.9k
    QUIC_SSTREAM *sstream = ch->crypto_send[pn_space];
971
972
57.9k
    if (!ossl_assert(sstream != NULL))
973
0
        return 0;
974
975
57.9k
    ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed);
976
57.9k
    return ret;
977
57.9k
}
978
979
static int crypto_ensure_empty(QUIC_RSTREAM *rstream)
980
49.6M
{
981
49.6M
    size_t avail = 0;
982
49.6M
    int is_fin = 0;
983
984
49.6M
    if (rstream == NULL)
985
36.5M
        return 1;
986
987
13.0M
    if (!ossl_quic_rstream_available(rstream, &avail, &is_fin))
988
0
        return 0;
989
990
13.0M
    return avail == 0;
991
13.0M
}
992
993
static int ch_on_crypto_recv_record(const unsigned char **buf,
994
                                    size_t *bytes_read, void *arg)
995
59.2M
{
996
59.2M
    QUIC_CHANNEL *ch = arg;
997
59.2M
    QUIC_RSTREAM *rstream;
998
59.2M
    int is_fin = 0; /* crypto stream is never finished, so we don't use this */
999
59.2M
    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
133M
    for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->rx_enc_level; ++i)
1015
73.7M
        if (i != QUIC_ENC_LEVEL_0RTT &&
1016
73.7M
            !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) {
1017
            /* Protocol violation (RFC 9001 s. 4.1.3) */
1018
16
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1019
16
                                                   OSSL_QUIC_FRAME_TYPE_CRYPTO,
1020
16
                                                   "crypto stream data in wrong EL");
1021
16
            return 0;
1022
16
        }
1023
1024
59.2M
    rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->rx_enc_level)];
1025
59.2M
    if (rstream == NULL)
1026
0
        return 0;
1027
1028
59.2M
    return ossl_quic_rstream_get_record(rstream, buf, bytes_read,
1029
59.2M
                                        &is_fin);
1030
59.2M
}
1031
1032
static int ch_on_crypto_release_record(size_t bytes_read, void *arg)
1033
67.5k
{
1034
67.5k
    QUIC_CHANNEL *ch = arg;
1035
67.5k
    QUIC_RSTREAM *rstream;
1036
67.5k
    OSSL_RTT_INFO rtt_info;
1037
67.5k
    uint32_t rx_pn_space = ossl_quic_enc_level_to_pn_space(ch->rx_enc_level);
1038
1039
67.5k
    rstream = ch->crypto_recv[rx_pn_space];
1040
67.5k
    if (rstream == NULL)
1041
0
        return 0;
1042
1043
67.5k
    ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ch), &rtt_info);
1044
67.5k
    if (!ossl_quic_rxfc_on_retire(&ch->crypto_rxfc[rx_pn_space], bytes_read,
1045
67.5k
                                  rtt_info.smoothed_rtt))
1046
0
        return 0;
1047
1048
67.5k
    return ossl_quic_rstream_release_record(rstream, bytes_read);
1049
67.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
25.5k
{
1057
25.5k
    QUIC_CHANNEL *ch = arg;
1058
25.5k
    uint32_t i;
1059
25.5k
    uint32_t enc_level;
1060
1061
    /* Convert TLS protection level to QUIC encryption level */
1062
25.5k
    switch (prot_level) {
1063
0
    case OSSL_RECORD_PROTECTION_LEVEL_EARLY:
1064
0
        enc_level = QUIC_ENC_LEVEL_0RTT;
1065
0
        break;
1066
1067
16.9k
    case OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE:
1068
16.9k
        enc_level = QUIC_ENC_LEVEL_HANDSHAKE;
1069
16.9k
        break;
1070
1071
8.57k
    case OSSL_RECORD_PROTECTION_LEVEL_APPLICATION:
1072
8.57k
        enc_level = QUIC_ENC_LEVEL_1RTT;
1073
8.57k
        break;
1074
1075
0
    default:
1076
0
        return 0;
1077
25.5k
    }
1078
1079
25.5k
    if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM)
1080
        /* Invalid EL. */
1081
0
        return 0;
1082
1083
1084
25.5k
    if (direction) {
1085
        /* TX */
1086
12.7k
        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
12.7k
        if (!ossl_qtx_provide_secret(ch->qtx, enc_level,
1094
12.7k
                                     suite_id, md,
1095
12.7k
                                     secret, secret_len))
1096
0
            return 0;
1097
1098
12.7k
        ch->tx_enc_level = enc_level;
1099
12.7k
    } else {
1100
        /* RX */
1101
12.7k
        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
42.6k
        for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i)
1113
29.8k
            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
15
                ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1116
15
                                                    OSSL_QUIC_FRAME_TYPE_CRYPTO,
1117
15
                                                    "crypto stream data in wrong EL");
1118
15
                return 0;
1119
15
            }
1120
1121
12.7k
        if (!ossl_qrx_provide_secret(ch->qrx, enc_level,
1122
12.7k
                                     suite_id, md,
1123
12.7k
                                     secret, secret_len))
1124
0
            return 0;
1125
1126
12.7k
        ch->have_new_rx_secret = 1;
1127
12.7k
        ch->rx_enc_level = enc_level;
1128
12.7k
    }
1129
1130
25.5k
    return 1;
1131
25.5k
}
1132
1133
static int ch_on_handshake_complete(void *arg)
1134
4.26k
{
1135
4.26k
    QUIC_CHANNEL *ch = arg;
1136
1137
4.26k
    if (!ossl_assert(!ch->handshake_complete))
1138
0
        return 0; /* this should not happen twice */
1139
1140
4.26k
    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.26k
    ossl_quic_tx_packetiser_set_validated(ch->txp);
1149
1150
4.26k
    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
30
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_MISSING_EXT,
1156
30
                                               OSSL_QUIC_FRAME_TYPE_CRYPTO,
1157
30
                                               "no transport parameters received");
1158
30
        return 0;
1159
30
    }
1160
1161
    /* Don't need transport parameters anymore. */
1162
4.23k
    OPENSSL_free(ch->local_transport_params);
1163
4.23k
    ch->local_transport_params = NULL;
1164
1165
    /* Tell the QRX it can now process 1-RTT packets. */
1166
4.23k
    ossl_qrx_allow_1rtt_processing(ch->qrx);
1167
1168
    /* Tell TXP the handshake is complete. */
1169
4.23k
    ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp);
1170
1171
4.23k
    ch->handshake_complete = 1;
1172
1173
4.23k
    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.23k
    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.23k
    ch_record_state_transition(ch, ch->state);
1200
4.23k
    return 1;
1201
4.26k
}
1202
1203
static int ch_on_handshake_alert(void *arg, unsigned char alert_code)
1204
10.3k
{
1205
10.3k
    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.3k
    if (alert_code == SSL_AD_UNEXPECTED_MESSAGE
1213
10.3k
            && ch->handshake_complete
1214
10.3k
            && ossl_quic_tls_is_cert_request(ch->qtls))
1215
5
        ossl_quic_channel_raise_protocol_error(ch,
1216
10.3k
                                               OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1217
10.3k
                                               0,
1218
10.3k
                                               "Post-handshake TLS "
1219
10.3k
                                               "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.3k
    else if (alert_code == SSL_AD_ILLEGAL_PARAMETER
1228
10.3k
             && ch->handshake_complete
1229
10.3k
             && ossl_quic_tls_has_bad_max_early_data(ch->qtls))
1230
0
        ossl_quic_channel_raise_protocol_error(ch,
1231
10.3k
                                               OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1232
10.3k
                                               0,
1233
10.3k
                                               "Bad max_early_data received");
1234
10.3k
    else
1235
10.3k
        ossl_quic_channel_raise_protocol_error(ch,
1236
10.3k
                                               OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN
1237
10.3k
                                               + alert_code,
1238
10.3k
                                               0, "handshake alert");
1239
1240
10.3k
    return 1;
1241
10.3k
}
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
28
    x " appears multiple times"
1257
#define TP_REASON_MALFORMED(x) \
1258
58
    x " is malformed"
1259
#define TP_REASON_EXPECTED_VALUE(x) \
1260
9
    x " does not match expected value"
1261
#define TP_REASON_NOT_RETRY(x) \
1262
2
    x " sent when not performing a retry"
1263
#define TP_REASON_REQUIRED(x) \
1264
9
    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.1k
{
1295
14.1k
    if (a == 0)
1296
0
        return b;
1297
14.1k
    if (b == 0)
1298
15
        return a;
1299
1300
14.1k
    return a < b ? a : b;
1301
14.1k
}
1302
1303
static int ch_on_transport_params(const unsigned char *params,
1304
                                  size_t params_len,
1305
                                  void *arg)
1306
3.43k
{
1307
3.43k
    QUIC_CHANNEL *ch = arg;
1308
3.43k
    PACKET pkt;
1309
3.43k
    uint64_t id, v;
1310
3.43k
    size_t len;
1311
3.43k
    const unsigned char *body;
1312
3.43k
    int got_orig_dcid = 0;
1313
3.43k
    int got_initial_scid = 0;
1314
3.43k
    int got_retry_scid = 0;
1315
3.43k
    int got_initial_max_data = 0;
1316
3.43k
    int got_initial_max_stream_data_bidi_local = 0;
1317
3.43k
    int got_initial_max_stream_data_bidi_remote = 0;
1318
3.43k
    int got_initial_max_stream_data_uni = 0;
1319
3.43k
    int got_initial_max_streams_bidi = 0;
1320
3.43k
    int got_initial_max_streams_uni = 0;
1321
3.43k
    int got_stateless_reset_token = 0;
1322
3.43k
    int got_preferred_addr = 0;
1323
3.43k
    int got_ack_delay_exp = 0;
1324
3.43k
    int got_max_ack_delay = 0;
1325
3.43k
    int got_max_udp_payload_size = 0;
1326
3.43k
    int got_max_idle_timeout = 0;
1327
3.43k
    int got_active_conn_id_limit = 0;
1328
3.43k
    int got_disable_active_migration = 0;
1329
3.43k
    QUIC_CONN_ID cid;
1330
3.43k
    const char *reason = "bad transport parameter";
1331
3.43k
    ossl_unused uint64_t rx_max_idle_timeout = 0;
1332
3.43k
    ossl_unused const void *stateless_reset_token_p = NULL;
1333
3.43k
    QUIC_PREFERRED_ADDR pfa;
1334
3.43k
    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.43k
    if (ch->is_server && sc->hello_retry_request != SSL_HRR_NONE
1341
3.43k
        && 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.43k
    } else if (ch->got_remote_transport_params) {
1348
0
        reason = "multiple transport parameter extensions";
1349
0
        goto malformed;
1350
0
    }
1351
1352
3.43k
    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
43.6k
    while (PACKET_remaining(&pkt) > 0) {
1359
40.3k
        if (!ossl_quic_wire_peek_transport_param(&pkt, &id))
1360
1
            goto malformed;
1361
1362
40.3k
        switch (id) {
1363
3.43k
        case QUIC_TPARAM_ORIG_DCID:
1364
3.43k
            if (got_orig_dcid) {
1365
7
                reason = TP_REASON_DUP("ORIG_DCID");
1366
7
                goto malformed;
1367
7
            }
1368
1369
3.42k
            if (ch->is_server) {
1370
0
                reason = TP_REASON_SERVER_ONLY("ORIG_DCID");
1371
0
                goto malformed;
1372
0
            }
1373
1374
3.42k
            if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) {
1375
5
                reason = TP_REASON_MALFORMED("ORIG_DCID");
1376
5
                goto malformed;
1377
5
            }
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.42k
            got_orig_dcid = 1;
1388
3.42k
            break;
1389
1390
7
        case QUIC_TPARAM_RETRY_SCID:
1391
7
            if (ch->is_server) {
1392
0
                reason = TP_REASON_SERVER_ONLY("RETRY_SCID");
1393
0
                goto malformed;
1394
0
            }
1395
1396
7
            if (got_retry_scid) {
1397
1
                reason = TP_REASON_DUP("RETRY_SCID");
1398
1
                goto malformed;
1399
1
            }
1400
1401
6
            if (!ch->doing_retry) {
1402
2
                reason = TP_REASON_NOT_RETRY("RETRY_SCID");
1403
2
                goto malformed;
1404
2
            }
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.40k
        case QUIC_TPARAM_INITIAL_SCID:
1421
3.40k
            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.40k
            if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid)) {
1428
1
                reason = TP_REASON_MALFORMED("INITIAL_SCID");
1429
1
                goto malformed;
1430
1
            }
1431
1432
3.40k
            if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) {
1433
7
                reason = TP_REASON_EXPECTED_VALUE("INITIAL_SCID");
1434
7
                goto malformed;
1435
7
            }
1436
1437
3.39k
            got_initial_scid = 1;
1438
3.39k
            break;
1439
1440
3.23k
        case QUIC_TPARAM_INITIAL_MAX_DATA:
1441
3.23k
            if (got_initial_max_data) {
1442
                /* must not appear more than once */
1443
2
                reason = TP_REASON_DUP("INITIAL_MAX_DATA");
1444
2
                goto malformed;
1445
2
            }
1446
1447
3.23k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) {
1448
8
                reason = TP_REASON_MALFORMED("INITIAL_MAX_DATA");
1449
8
                goto malformed;
1450
8
            }
1451
1452
3.22k
            ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v);
1453
3.22k
            got_initial_max_data = 1;
1454
3.22k
            break;
1455
1456
3.27k
        case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL:
1457
3.27k
            if (got_initial_max_stream_data_bidi_local) {
1458
                /* must not appear more than once */
1459
2
                reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL");
1460
2
                goto malformed;
1461
2
            }
1462
1463
3.27k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) {
1464
2
                reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_LOCAL");
1465
2
                goto malformed;
1466
2
            }
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.27k
            ch->rx_init_max_stream_data_bidi_remote = v;
1473
3.27k
            got_initial_max_stream_data_bidi_local = 1;
1474
3.27k
            break;
1475
1476
3.32k
        case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE:
1477
3.32k
            if (got_initial_max_stream_data_bidi_remote) {
1478
                /* must not appear more than once */
1479
4
                reason = TP_REASON_DUP("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE");
1480
4
                goto malformed;
1481
4
            }
1482
1483
3.32k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) {
1484
2
                reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_BIDI_REMOTE");
1485
2
                goto malformed;
1486
2
            }
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.32k
            ch->rx_init_max_stream_data_bidi_local = v;
1493
1494
            /* Apply to all existing streams. */
1495
3.32k
            ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_bidi, &v);
1496
3.32k
            got_initial_max_stream_data_bidi_remote = 1;
1497
3.32k
            break;
1498
1499
3.33k
        case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI:
1500
3.33k
            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.33k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) {
1507
2
                reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAM_DATA_UNI");
1508
2
                goto malformed;
1509
2
            }
1510
1511
3.32k
            ch->rx_init_max_stream_data_uni = v;
1512
1513
            /* Apply to all existing streams. */
1514
3.32k
            ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_uni, &v);
1515
3.32k
            got_initial_max_stream_data_uni = 1;
1516
3.32k
            break;
1517
1518
5
        case QUIC_TPARAM_ACK_DELAY_EXP:
1519
5
            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
4
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1526
4
                || v > QUIC_MAX_ACK_DELAY_EXP) {
1527
2
                reason = TP_REASON_MALFORMED("ACK_DELAY_EXP");
1528
2
                goto malformed;
1529
2
            }
1530
1531
2
            ch->rx_ack_delay_exp = (unsigned char)v;
1532
2
            got_ack_delay_exp = 1;
1533
2
            break;
1534
1535
31
        case QUIC_TPARAM_MAX_ACK_DELAY:
1536
31
            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
30
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1543
30
                || v >= (((uint64_t)1) << 14)) {
1544
8
                reason = TP_REASON_MALFORMED("MAX_ACK_DELAY");
1545
8
                goto malformed;
1546
8
            }
1547
1548
22
            ch->rx_max_ack_delay = v;
1549
22
            ossl_ackm_set_rx_max_ack_delay(ch->ackm,
1550
22
                                           ossl_ms2time(ch->rx_max_ack_delay));
1551
1552
22
            got_max_ack_delay = 1;
1553
22
            break;
1554
1555
3.23k
        case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI:
1556
3.23k
            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.23k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1563
3.23k
                || v > (((uint64_t)1) << 60)) {
1564
3
                reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_BIDI");
1565
3
                goto malformed;
1566
3
            }
1567
1568
3.23k
            assert(ch->max_local_streams_bidi == 0);
1569
3.23k
            ch->max_local_streams_bidi = v;
1570
3.23k
            got_initial_max_streams_bidi = 1;
1571
3.23k
            break;
1572
1573
3.30k
        case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI:
1574
3.30k
            if (got_initial_max_streams_uni) {
1575
                /* must not appear more than once */
1576
1
                reason = TP_REASON_DUP("INITIAL_MAX_STREAMS_UNI");
1577
1
                goto malformed;
1578
1
            }
1579
1580
3.30k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1581
3.30k
                || v > (((uint64_t)1) << 60)) {
1582
4
                reason = TP_REASON_MALFORMED("INITIAL_MAX_STREAMS_UNI");
1583
4
                goto malformed;
1584
4
            }
1585
1586
3.29k
            assert(ch->max_local_streams_uni == 0);
1587
3.29k
            ch->max_local_streams_uni = v;
1588
3.29k
            got_initial_max_streams_uni = 1;
1589
3.29k
            break;
1590
1591
3.39k
        case QUIC_TPARAM_MAX_IDLE_TIMEOUT:
1592
3.39k
            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.39k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)) {
1599
2
                reason = TP_REASON_MALFORMED("MAX_IDLE_TIMEOUT");
1600
2
                goto malformed;
1601
2
            }
1602
1603
3.39k
            ch->max_idle_timeout_remote_req = v;
1604
1605
3.39k
            ch->max_idle_timeout = min_u64_ignore_0(ch->max_idle_timeout_local_req,
1606
3.39k
                                                    ch->max_idle_timeout_remote_req);
1607
1608
1609
3.39k
            ch_update_idle(ch);
1610
3.39k
            got_max_idle_timeout = 1;
1611
3.39k
            rx_max_idle_timeout = v;
1612
3.39k
            break;
1613
1614
3.26k
        case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE:
1615
3.26k
            if (got_max_udp_payload_size) {
1616
                /* must not appear more than once */
1617
1
                reason = TP_REASON_DUP("MAX_UDP_PAYLOAD_SIZE");
1618
1
                goto malformed;
1619
1
            }
1620
1621
3.26k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1622
3.26k
                || v < QUIC_MIN_INITIAL_DGRAM_LEN) {
1623
4
                reason = TP_REASON_MALFORMED("MAX_UDP_PAYLOAD_SIZE");
1624
4
                goto malformed;
1625
4
            }
1626
1627
3.26k
            ch->rx_max_udp_payload_size = v;
1628
3.26k
            got_max_udp_payload_size    = 1;
1629
3.26k
            break;
1630
1631
3.24k
        case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT:
1632
3.24k
            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.24k
            if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
1639
3.24k
                || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT) {
1640
4
                reason = TP_REASON_MALFORMED("ACTIVE_CONN_ID_LIMIT");
1641
4
                goto malformed;
1642
4
            }
1643
1644
3.23k
            ch->rx_active_conn_id_limit = v;
1645
3.23k
            got_active_conn_id_limit = 1;
1646
3.23k
            break;
1647
1648
4
        case QUIC_TPARAM_STATELESS_RESET_TOKEN:
1649
4
            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
3
            if (ch->is_server) {
1659
0
                reason = TP_REASON_SERVER_ONLY("STATELESS_RESET_TOKEN");
1660
0
                goto malformed;
1661
0
            }
1662
1663
3
            body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len);
1664
3
            if (body == NULL || len != QUIC_STATELESS_RESET_TOKEN_LEN) {
1665
2
                reason = TP_REASON_MALFORMED("STATELESS_RESET_TOKEN");
1666
2
                goto malformed;
1667
2
            }
1668
1
            if (!ossl_quic_srtm_add(ch->srtm, ch, ch->cur_remote_seq_num,
1669
1
                                    (const QUIC_STATELESS_RESET_TOKEN *)body)) {
1670
0
                reason = TP_REASON_INTERNAL_ERROR("STATELESS_RESET_TOKEN");
1671
0
                goto malformed;
1672
0
            }
1673
1674
1
            stateless_reset_token_p     = body;
1675
1
            got_stateless_reset_token   = 1;
1676
1
            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.29k
        case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION:
1717
            /* We do not currently handle migration, so nothing to do. */
1718
3.29k
            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.29k
            body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &len);
1725
3.29k
            if (body == NULL || len > 0) {
1726
2
                reason = TP_REASON_MALFORMED("DISABLE_ACTIVE_MIGRATION");
1727
2
                goto malformed;
1728
2
            }
1729
1730
3.29k
            got_disable_active_migration = 1;
1731
3.29k
            break;
1732
1733
509
        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
509
            body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id,
1744
509
                                                               &len);
1745
509
            if (body == NULL)
1746
14
                goto malformed;
1747
1748
495
            break;
1749
40.3k
        }
1750
40.3k
    }
1751
1752
3.32k
    if (!got_initial_scid) {
1753
4
        reason = TP_REASON_REQUIRED("INITIAL_SCID");
1754
4
        goto malformed;
1755
4
    }
1756
1757
3.31k
    if (!ch->is_server) {
1758
3.31k
        if (!got_orig_dcid) {
1759
1
            reason = TP_REASON_REQUIRED("ORIG_DCID");
1760
1
            goto malformed;
1761
1
        }
1762
1763
3.31k
        if (ch->doing_retry && !got_retry_scid) {
1764
4
            reason = TP_REASON_REQUIRED("RETRY_SCID");
1765
4
            goto malformed;
1766
4
        }
1767
3.31k
    }
1768
1769
3.31k
    ch->got_remote_transport_params = 1;
1770
1771
3.31k
#ifndef OPENSSL_NO_QLOG
1772
3.31k
    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.31k
#endif
1829
1830
3.31k
    if (got_initial_max_data || got_initial_max_stream_data_bidi_remote
1831
3.31k
        || 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.31k
        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.31k
    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.31k
    return 1;
1846
1847
122
malformed:
1848
122
    ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR,
1849
122
                                           0, reason);
1850
122
    return 0;
1851
3.31k
}
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
17.5k
{
1860
17.5k
    int ok = 0;
1861
17.5k
    BUF_MEM *buf_mem = NULL;
1862
17.5k
    WPACKET wpkt;
1863
17.5k
    int wpkt_valid = 0;
1864
17.5k
    size_t buf_len = 0;
1865
17.5k
    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
17.5k
    if (ch->odcid.id_len == 0)
1877
17.5k
        id_to_use = &ch->init_dcid;
1878
0
    else
1879
0
        id_to_use = &ch->odcid;
1880
1881
17.5k
    if (ch->local_transport_params != NULL || ch->got_local_transport_params)
1882
0
        goto err;
1883
1884
17.5k
    if ((buf_mem = BUF_MEM_new()) == NULL)
1885
0
        goto err;
1886
1887
17.5k
    if (!WPACKET_init(&wpkt, buf_mem))
1888
0
        goto err;
1889
1890
17.5k
    wpkt_valid = 1;
1891
1892
17.5k
    if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION,
1893
17.5k
                                                    NULL, 0) == NULL)
1894
0
        goto err;
1895
1896
17.5k
    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
17.5k
    } else {
1910
17.5k
        if (!ossl_quic_wire_encode_transport_param_cid(&wpkt, QUIC_TPARAM_INITIAL_SCID,
1911
17.5k
                                                       &ch->init_scid))
1912
0
            goto err;
1913
17.5k
    }
1914
1915
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT,
1916
17.5k
                                                   ch->max_idle_timeout_local_req))
1917
0
        goto err;
1918
1919
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE,
1920
17.5k
                                                   QUIC_MIN_INITIAL_DGRAM_LEN))
1921
0
        goto err;
1922
1923
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT,
1924
17.5k
                                                   QUIC_MIN_ACTIVE_CONN_ID_LIMIT))
1925
0
        goto err;
1926
1927
17.5k
    if (ch->tx_max_ack_delay != QUIC_DEFAULT_MAX_ACK_DELAY
1928
17.5k
        && !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
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA,
1933
17.5k
                                                   ossl_quic_rxfc_get_cwm(&ch->conn_rxfc)))
1934
0
        goto err;
1935
1936
    /* Send the default CWM for a new RXFC. */
1937
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
1938
17.5k
                                                   ch->tx_init_max_stream_data_bidi_local))
1939
0
        goto err;
1940
1941
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
1942
17.5k
                                                   ch->tx_init_max_stream_data_bidi_remote))
1943
0
        goto err;
1944
1945
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI,
1946
17.5k
                                                   ch->tx_init_max_stream_data_uni))
1947
0
        goto err;
1948
1949
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI,
1950
17.5k
                                                   ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc)))
1951
0
        goto err;
1952
1953
17.5k
    if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI,
1954
17.5k
                                                   ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc)))
1955
0
        goto err;
1956
1957
17.5k
    if (!WPACKET_finish(&wpkt))
1958
0
        goto err;
1959
1960
17.5k
    wpkt_valid = 0;
1961
1962
17.5k
    if (!WPACKET_get_total_written(&wpkt, &buf_len))
1963
0
        goto err;
1964
1965
17.5k
    ch->local_transport_params = (unsigned char *)buf_mem->data;
1966
17.5k
    buf_mem->data = NULL;
1967
1968
17.5k
    if (!ossl_quic_tls_set_transport_params(ch->qtls, ch->local_transport_params,
1969
17.5k
                                            buf_len))
1970
0
        goto err;
1971
1972
17.5k
#ifndef OPENSSL_NO_QLOG
1973
17.5k
    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
17.5k
#endif
1999
2000
17.5k
    ch->got_local_transport_params = 1;
2001
2002
17.5k
    ok = 1;
2003
17.5k
err:
2004
17.5k
    if (wpkt_valid)
2005
0
        WPACKET_cleanup(&wpkt);
2006
17.5k
    BUF_MEM_free(buf_mem);
2007
17.5k
    return ok;
2008
17.5k
}
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
22.1M
{
2023
22.1M
    OSSL_TIME now, deadline;
2024
22.1M
    int channel_only = (flags & QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY) != 0;
2025
22.1M
    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
22.1M
    if (ch->state == QUIC_CHANNEL_STATE_IDLE
2043
22.1M
            || 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
22.1M
    if (ossl_quic_channel_is_terminating(ch)) {
2056
169
        now = get_time(ch);
2057
2058
169
        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
169
    }
2067
2068
22.1M
    if (!ch->port->engine->inhibit_tick) {
2069
        /* Handle RXKU timeouts. */
2070
22.1M
        ch_rxku_tick(ch);
2071
2072
22.1M
        do {
2073
            /* Process queued incoming packets. */
2074
22.1M
            ch->did_tls_tick        = 0;
2075
22.1M
            ch->have_new_rx_secret  = 0;
2076
22.1M
            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
22.1M
            if (!ch->did_tls_tick)
2083
22.1M
                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
22.1M
        } while (ch->have_new_rx_secret);
2093
22.1M
    }
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
22.1M
    now = get_time(ch);
2103
22.1M
    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
5.83k
        if (!ch->port->engine->inhibit_tick)
2109
5.83k
            ch_on_idle_timeout(ch);
2110
2111
5.83k
        res->net_read_desired       = 0;
2112
5.83k
        res->net_write_desired      = 0;
2113
5.83k
        res->notify_other_threads   = 1;
2114
5.83k
        res->tick_deadline          = ossl_time_infinite();
2115
5.83k
        return;
2116
5.83k
    }
2117
2118
22.1M
    if (!ch->port->engine->inhibit_tick) {
2119
22.1M
        deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
2120
22.1M
        if (!ossl_time_is_zero(deadline)
2121
22.1M
            && ossl_time_compare(now, deadline) >= 0)
2122
113k
            ossl_ackm_on_timeout(ch->ackm);
2123
2124
        /* If a ping is due, inform TXP. */
2125
22.1M
        if (ossl_time_compare(now, ch->ping_deadline) >= 0) {
2126
6.78M
            int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level);
2127
2128
6.78M
            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.78M
            ch_update_ping_deadline(ch);
2138
6.78M
        }
2139
2140
        /* Queue any data to be sent for transmission. */
2141
22.1M
        ch_tx(ch, &notify_other_threads);
2142
2143
        /* Do stream GC. */
2144
22.1M
        ossl_quic_stream_map_gc(&ch->qsm);
2145
22.1M
    }
2146
2147
    /* Determine the time at which we should next be ticked. */
2148
22.1M
    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
22.1M
    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
22.1M
    res->net_write_desired
2159
22.1M
        = (!ossl_quic_channel_is_terminated(ch)
2160
22.1M
           && ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0);
2161
2162
22.1M
    res->notify_other_threads = notify_other_threads;
2163
22.1M
}
2164
2165
static int ch_tick_tls(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads)
2166
22.2M
{
2167
22.2M
    uint64_t error_code;
2168
22.2M
    const char *error_msg;
2169
22.2M
    ERR_STATE *error_state = NULL;
2170
2171
22.2M
    if (channel_only)
2172
0
        return 1;
2173
2174
22.2M
    ch->did_tls_tick = 1;
2175
22.2M
    ossl_quic_tls_tick(ch->qtls);
2176
2177
22.2M
    if (ossl_quic_tls_get_error(ch->qtls, &error_code, &error_msg,
2178
22.2M
                                &error_state)) {
2179
6.03k
        ossl_quic_channel_raise_protocol_error_state(ch, error_code, 0,
2180
6.03k
                                                     error_msg, error_state);
2181
6.03k
        if (notify_other_threads != NULL)
2182
2.22k
            *notify_other_threads = 1;
2183
2184
6.03k
        return 0;
2185
6.03k
    }
2186
2187
22.2M
    return 1;
2188
22.2M
}
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
59.1M
{
2193
59.1M
    uint32_t enc_level;
2194
59.1M
    uint64_t limit = UINT64_MAX, l;
2195
2196
59.1M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
2197
191M
         enc_level < QUIC_ENC_LEVEL_NUM;
2198
132M
         ++enc_level)
2199
169M
    {
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
169M
        if ((ch->el_discarded & (1U << enc_level)) != 0)
2205
36.5M
            continue;
2206
2207
132M
        if (enc_level > ch->rx_enc_level)
2208
36.5M
            break;
2209
2210
96.2M
        l = ossl_qrx_get_max_forged_pkt_count(ch->qrx, enc_level);
2211
96.2M
        if (l < limit)
2212
59.2M
            limit = l;
2213
96.2M
    }
2214
2215
59.1M
    if (ossl_qrx_get_cur_forged_pkt_count(ch->qrx) < limit)
2216
59.1M
        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
22.1M
{
2225
22.1M
    int handled_any = 0;
2226
22.1M
    const int closing = ossl_quic_channel_is_closing(ch);
2227
2228
22.1M
    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
17.5k
        return 1;
2234
2235
23.4M
    for (;;) {
2236
23.4M
        assert(ch->qrx_pkt == NULL);
2237
2238
23.4M
        if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt))
2239
22.1M
            break;
2240
2241
        /* Track the amount of data received while in the closing state */
2242
1.29M
        if (closing)
2243
30
            ossl_quic_tx_packetiser_record_received_closing_bytes(
2244
30
                    ch->txp, ch->qrx_pkt->hdr->len);
2245
2246
1.29M
        if (!handled_any) {
2247
1.28M
            ch_update_idle(ch);
2248
1.28M
            ch_update_ping_deadline(ch);
2249
1.28M
        }
2250
2251
1.29M
        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.29M
        ossl_qrx_pkt_release(ch->qrx_pkt);
2259
1.29M
        ch->qrx_pkt = NULL;
2260
2261
1.29M
        ch->have_sent_ack_eliciting_since_rx = 0;
2262
1.29M
        handled_any = 1;
2263
1.29M
    }
2264
2265
22.1M
    ch_rx_check_forged_pkt_limit(ch);
2266
2267
22.1M
    if (handled_any && notify_other_threads != NULL)
2268
1.28M
        *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
22.1M
    if (handled_any && closing)
2275
27
        ch->conn_close_queued = 1;
2276
2277
22.1M
    return 1;
2278
22.1M
}
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.29M
{
2308
1.29M
    uint32_t enc_level;
2309
1.29M
    int old_have_processed_any_pkt = ch->have_processed_any_pkt;
2310
1.29M
    OSSL_QTX_IOVEC iovec;
2311
1.29M
    PACKET vpkt;
2312
1.29M
    unsigned long supported_ver;
2313
2314
1.29M
    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.29M
    if (!ossl_quic_channel_is_active(ch))
2322
767
        return;
2323
2324
1.29M
    if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) {
2325
263k
        if (!ch->have_received_enc_pkt) {
2326
15.3k
            ch->cur_remote_dcid = ch->init_scid = ch->qrx_pkt->hdr->src_conn_id;
2327
15.3k
            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
15.3k
            ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid);
2334
15.3k
        }
2335
2336
263k
        enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type);
2337
263k
        if ((ch->el_discarded & (1U << enc_level)) != 0)
2338
            /* Do not process packets from ELs we have already discarded. */
2339
11
            return;
2340
263k
    }
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.29M
    if (!ch->is_server
2353
1.29M
        && ch->qrx_pkt->peer != NULL
2354
1.29M
        && (
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.29M
        && !bio_addr_eq(ch->qrx_pkt->peer, &ch->cur_peer_addr))
2361
0
        return;
2362
2363
1.29M
    if (!ch->is_server
2364
1.29M
        && ch->have_received_enc_pkt
2365
1.29M
        && 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.05M
        if (!ossl_quic_conn_id_eq(&ch->qrx_pkt->hdr->src_conn_id,
2372
1.05M
                                  &ch->init_scid))
2373
610k
            return;
2374
1.05M
    }
2375
2376
685k
    if (ossl_quic_pkt_type_has_version(ch->qrx_pkt->hdr->type)
2377
685k
        && 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
685k
    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
226k
        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
226k
        if (old_have_processed_any_pkt == 1) {
2406
            /*
2407
             * We've gotten previous packets, need to discard this.
2408
             */
2409
226k
            return;
2410
226k
        }
2411
2412
        /*
2413
         * Indicate that we have processed a packet, as any subsequently
2414
         * received version negotiation packet must be discarded above
2415
         */
2416
57
        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
57
        if (!PACKET_buf_init(&vpkt, ch->qrx_pkt->hdr->data,
2427
57
                             ch->qrx_pkt->hdr->len))
2428
0
            return;
2429
2430
675
        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
635
            if (!PACKET_get_net_4(&vpkt, &supported_ver))
2436
0
                return;
2437
2438
635
            if (supported_ver == QUIC_VERSION_1) {
2439
                /*
2440
                 * If the server supports version 1, set it as
2441
                 * the packetisers version
2442
                 */
2443
17
                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
17
                if (!ch_restart(ch))
2449
0
                    ossl_quic_channel_raise_protocol_error(ch,
2450
17
                                                           OSSL_QUIC_ERR_INTERNAL_ERROR,
2451
17
                                                           0, "handling ver negotiation packet");
2452
17
                return;
2453
17
            }
2454
635
        }
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
40
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CONNECTION_REFUSED,
2461
40
                                               0, "unsupported protocol version");
2462
40
        return;
2463
57
    }
2464
2465
458k
    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
458k
    if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)
2473
458k
        && ch->qrx_pkt->hdr->reserved != 0) {
2474
124
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
2475
124
                                               0, "packet header reserved bits");
2476
124
        return;
2477
124
    }
2478
2479
458k
    iovec.buf       = ch->qrx_pkt->hdr->data;
2480
458k
    iovec.buf_len   = ch->qrx_pkt->hdr->len;
2481
458k
    ossl_qlog_event_transport_packet_received(ch_get_qlog(ch), ch->qrx_pkt->hdr,
2482
458k
                                              ch->qrx_pkt->pn, &iovec, 1,
2483
458k
                                              ch->qrx_pkt->datagram_id);
2484
2485
    /* Handle incoming packet. */
2486
458k
    switch (ch->qrx_pkt->hdr->type) {
2487
251k
    case QUIC_PKT_TYPE_RETRY:
2488
251k
        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
115k
            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
136k
        if (ch->have_received_enc_pkt)
2501
28.3k
            return;
2502
2503
108k
        if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN)
2504
            /* Packets with zero-length Retry Tokens are invalid. */
2505
473
            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
107k
        if (!ossl_quic_validate_retry_integrity_tag(ch->port->engine->libctx,
2517
107k
                                                    ch->port->engine->propq,
2518
107k
                                                    ch->qrx_pkt->hdr,
2519
107k
                                                    &ch->init_dcid))
2520
            /* Malformed retry packet, ignore. */
2521
107k
            return;
2522
2523
398
        if (!ch_retry(ch, ch->qrx_pkt->hdr->data,
2524
398
                      ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN,
2525
398
                      &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
398
                                                   0, "handling retry packet");
2528
398
        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
173k
    case QUIC_PKT_TYPE_INITIAL:
2543
187k
    case QUIC_PKT_TYPE_HANDSHAKE:
2544
206k
    case QUIC_PKT_TYPE_1RTT:
2545
206k
        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
206k
        if (ch->rxku_in_progress
2553
206k
            && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_1RTT
2554
206k
            && ch->qrx_pkt->pn >= ch->rxku_trigger_pn
2555
206k
            && 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
2
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
2565
2
                                                   0, "new packet with old keys");
2566
2
            break;
2567
2
        }
2568
2569
206k
        if (!ch->is_server
2570
206k
            && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL
2571
206k
            && 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
30
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
2590
30
                                                   0, "client received initial token");
2591
30
            break;
2592
30
        }
2593
2594
        /* This packet contains frames, pass to the RXDP. */
2595
206k
        ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */
2596
2597
206k
        if (ch->did_crypto_frame)
2598
75.1k
            ch_tick_tls(ch, channel_only, NULL);
2599
2600
206k
        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
458k
    }
2616
2617
458k
}
2618
2619
static void ch_rx_handle_version_neg(QUIC_CHANNEL *ch, OSSL_QRX_PKT *pkt)
2620
122
{
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
122
    PACKET vpkt;
2628
122
    unsigned long v;
2629
2630
122
    if (!PACKET_buf_init(&vpkt, pkt->hdr->data, pkt->hdr->len))
2631
0
        return;
2632
2633
1.67k
    while (PACKET_remaining(&vpkt) > 0) {
2634
1.63k
        if (!PACKET_get_net_4(&vpkt, &v))
2635
0
            break;
2636
2637
1.63k
        if ((uint32_t)v == QUIC_VERSION_1)
2638
79
            return;
2639
1.63k
    }
2640
2641
    /* No match, this is a failure case. */
2642
43
    ch_raise_version_neg_failure(ch);
2643
43
}
2644
2645
static void ch_raise_version_neg_failure(QUIC_CHANNEL *ch)
2646
43
{
2647
43
    QUIC_TERMINATE_CAUSE tcause = {0};
2648
2649
43
    tcause.error_code = OSSL_QUIC_ERR_CONNECTION_REFUSED;
2650
43
    tcause.reason     = "version negotiation failure";
2651
43
    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
43
    ch_start_terminating(ch, &tcause, 1);
2658
43
}
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
22.1M
{
2663
22.1M
    QUIC_TXP_STATUS status;
2664
22.1M
    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
22.1M
    if (ossl_quic_channel_is_draining(ch))
2674
342
        return 0;
2675
2676
22.1M
    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
8.64k
        if (!ch->conn_close_queued)
2688
76
            return 0;
2689
2690
8.56k
        ch->conn_close_queued = 0;
2691
8.56k
    }
2692
2693
    /* Do TXKU if we need to. */
2694
22.1M
    ch_maybe_trigger_spontaneous_txku(ch);
2695
2696
22.1M
    ch->rxku_pending_confirm_done = 0;
2697
2698
    /* Loop until we stop generating packets to send */
2699
22.7M
    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.7M
        res = ossl_quic_tx_packetiser_generate(ch->txp, &status);
2708
22.7M
        if (status.sent_pkt > 0) {
2709
564k
            ch->have_sent_any_pkt = 1; /* Packet(s) were sent */
2710
564k
            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
564k
            if (status.sent_ack_eliciting
2718
564k
                    && !ch->have_sent_ack_eliciting_since_rx) {
2719
313k
                ch_update_idle(ch);
2720
313k
                ch->have_sent_ack_eliciting_since_rx = 1;
2721
313k
            }
2722
2723
564k
            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
87.7k
                ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL);
2729
2730
564k
            if (ch->rxku_pending_confirm_done)
2731
7.62k
                ch->rxku_pending_confirm = 0;
2732
2733
564k
            ch_update_ping_deadline(ch);
2734
564k
        }
2735
2736
22.7M
        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.7M
    } while (status.sent_pkt > 0);
2755
2756
    /* Flush packets to network. */
2757
0
    switch (ossl_qtx_flush_net(ch->qtx)) {
2758
22.1M
    case QTX_FLUSH_NET_RES_OK:
2759
22.1M
    case QTX_FLUSH_NET_RES_TRANSIENT_FAIL:
2760
        /* Best effort, done for now. */
2761
22.1M
        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
22.1M
    }
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
22.1M
    if (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0)
2776
0
        *notify_other_threads = 1;
2777
2778
22.1M
    return 1;
2779
22.1M
}
2780
2781
/* Determine next tick deadline. */
2782
static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch)
2783
59.1M
{
2784
59.1M
    OSSL_TIME deadline;
2785
59.1M
    int i;
2786
2787
59.1M
    if (ossl_quic_channel_is_terminated(ch))
2788
44
        return ossl_time_infinite();
2789
2790
59.1M
    deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
2791
59.1M
    if (ossl_time_is_zero(deadline))
2792
97.6k
        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
295M
    for (i = 0; i < QUIC_ENC_LEVEL_NUM; i++) {
2799
236M
        if (ossl_qtx_is_enc_level_provisioned(ch->qtx, i)) {
2800
72.0M
            deadline = ossl_time_min(deadline,
2801
72.0M
                                     ossl_ackm_get_ack_deadline(ch->ackm,
2802
72.0M
                                                                ossl_quic_enc_level_to_pn_space(i)));
2803
72.0M
        }
2804
236M
    }
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
59.1M
    if (!ossl_time_is_infinite(ch->ping_deadline))
2811
59.1M
        deadline = ossl_time_min(deadline, ch->ping_deadline);
2812
2813
    /* Apply TXP wakeup deadline. */
2814
59.1M
    deadline = ossl_time_min(deadline,
2815
59.1M
                             ossl_quic_tx_packetiser_get_deadline(ch->txp));
2816
2817
    /* Is the terminating timer armed? */
2818
59.1M
    if (ossl_quic_channel_is_terminating(ch))
2819
24.9k
        deadline = ossl_time_min(deadline,
2820
24.9k
                                 ch->terminate_deadline);
2821
59.0M
    else if (!ossl_time_is_infinite(ch->idle_deadline))
2822
59.0M
        deadline = ossl_time_min(deadline,
2823
59.0M
                                 ch->idle_deadline);
2824
2825
    /* When does the RXKU process complete? */
2826
59.1M
    if (ch->rxku_in_progress)
2827
2.40M
        deadline = ossl_time_min(deadline, ch->rxku_update_end_deadline);
2828
2829
59.1M
    return deadline;
2830
59.1M
}
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
78.2k
{
2843
78.2k
    uint32_t old_state = ch->state;
2844
2845
78.2k
    ch->state = new_state;
2846
2847
78.2k
    ossl_qlog_event_connectivity_connection_state_updated(ch_get_qlog(ch),
2848
78.2k
                                                          old_state,
2849
78.2k
                                                          new_state,
2850
78.2k
                                                          ch->handshake_complete,
2851
78.2k
                                                          ch->handshake_confirmed);
2852
78.2k
}
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
17.5k
{
2862
17.5k
    QUIC_TOKEN *token;
2863
2864
17.5k
    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
17.5k
    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
17.5k
    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
17.5k
    if (!ch->is_server
2883
17.5k
        && ossl_quic_get_peer_token(ch->port->channel_ctx,
2884
17.5k
                                    &ch->cur_peer_addr,
2885
17.5k
                                    &token)
2886
17.5k
        && !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
17.5k
    if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,
2894
17.5k
                                          ch->port->engine->propq,
2895
17.5k
                                          &ch->init_dcid,
2896
17.5k
                                          ch->is_server,
2897
17.5k
                                          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
17.5k
    if (!ch->is_server && !ch->got_local_transport_params
2906
17.5k
        && !ch_generate_transport_params(ch))
2907
0
        return 0;
2908
2909
    /* Change state. */
2910
17.5k
    ch_record_state_transition(ch, QUIC_CHANNEL_STATE_ACTIVE);
2911
17.5k
    ch->doing_proactive_ver_neg = 0; /* not currently supported */
2912
2913
17.5k
    ossl_qlog_event_connectivity_connection_started(ch_get_qlog(ch),
2914
17.5k
                                                    &ch->init_dcid);
2915
2916
    /* Handshake layer: start (e.g. send CH). */
2917
17.5k
    if (!ch_tick_tls(ch, /*channel_only=*/0, NULL))
2918
0
        return 0;
2919
2920
17.5k
    ossl_quic_reactor_tick(ossl_quic_port_get0_reactor(ch->port), 0); /* best effort */
2921
17.5k
    return 1;
2922
17.5k
}
2923
2924
static void free_token(const unsigned char *token, size_t token_len, void *arg)
2925
815
{
2926
815
    OPENSSL_free((char *)token);
2927
815
}
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
17
{
2956
    /*
2957
     * Just pretend we lost our initial packet, so it gets
2958
     * regenerated, with our updated protocol version number
2959
     */
2960
17
   return ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
2961
17
                                            /* PN= */ 0);
2962
17
}
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
398
{
2971
398
    void *buf;
2972
398
    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
398
    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
398
    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
398
    if ((buf = OPENSSL_memdup(retry_token, retry_token_len)) == NULL)
2990
0
        return 0;
2991
2992
398
    if (!ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf,
2993
398
                                                   retry_token_len,
2994
398
                                                   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
393
    ch->retry_scid  = *retry_scid;
3006
393
    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
393
    if (drop_later_pn == 1)
3013
228
        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
393
    if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
3026
393
                                           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
393
    if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,
3034
393
                                          ch->port->engine->propq,
3035
393
                                          &ch->retry_scid,
3036
393
                                          /*is_server=*/0,
3037
393
                                          ch->qrx, ch->qtx))
3038
0
        return 0;
3039
3040
393
    return 1;
3041
393
}
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
233k
{
3047
233k
    if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT))
3048
0
        return 0;
3049
3050
233k
    if ((ch->el_discarded & (1U << enc_level)) != 0)
3051
        /* Already done. */
3052
207k
        return 1;
3053
3054
    /* Best effort for all of these. */
3055
26.5k
    ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level);
3056
26.5k
    ossl_qrx_discard_enc_level(ch->qrx, enc_level);
3057
26.5k
    ossl_qtx_discard_enc_level(ch->qtx, enc_level);
3058
3059
26.5k
    if (enc_level != QUIC_ENC_LEVEL_0RTT) {
3060
26.5k
        uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
3061
3062
26.5k
        ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
3063
3064
        /* We should still have crypto streams at this point. */
3065
26.5k
        if (!ossl_assert(ch->crypto_send[pn_space] != NULL)
3066
26.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
26.5k
        ossl_quic_sstream_free(ch->crypto_send[pn_space]);
3071
26.5k
        ch->crypto_send[pn_space] = NULL;
3072
3073
26.5k
        ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
3074
26.5k
        ch->crypto_recv[pn_space] = NULL;
3075
26.5k
    }
3076
3077
26.5k
    ch->el_discarded |= (1U << enc_level);
3078
26.5k
    return 1;
3079
26.5k
}
3080
3081
/* Intended to be called by the RXDP. */
3082
int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch)
3083
219k
{
3084
219k
    if (ch->handshake_confirmed)
3085
213k
        return 1;
3086
3087
5.97k
    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
5.97k
    ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE);
3100
5.97k
    ch->handshake_confirmed = 1;
3101
5.97k
    ch_record_state_transition(ch, ch->state);
3102
5.97k
    ossl_ackm_on_handshake_confirmed(ch->ackm);
3103
5.97k
    return 1;
3104
5.97k
}
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
23.9k
{
3154
23.9k
    dst->error_code = src->error_code;
3155
23.9k
    dst->frame_type = src->frame_type;
3156
23.9k
    dst->app        = src->app;
3157
23.9k
    dst->remote     = src->remote;
3158
3159
23.9k
    dst->reason     = NULL;
3160
23.9k
    dst->reason_len = 0;
3161
3162
23.9k
    if (src->reason != NULL && src->reason_len > 0) {
3163
23.5k
        size_t l = src->reason_len;
3164
23.5k
        char *r;
3165
3166
23.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
23.5k
        dst->reason = r = OPENSSL_memdup(src->reason, l + 1);
3174
23.5k
        if (r == NULL)
3175
0
            return;
3176
3177
23.5k
        r[l]  = '\0';
3178
23.5k
        dst->reason_len = l;
3179
23.5k
    }
3180
23.9k
}
3181
3182
static void ch_start_terminating(QUIC_CHANNEL *ch,
3183
                                 const QUIC_TERMINATE_CAUSE *tcause,
3184
                                 int force_immediate)
3185
24.4k
{
3186
    /* No point sending anything if we haven't sent anything yet. */
3187
24.4k
    if (!ch->have_sent_any_pkt)
3188
0
        force_immediate = 1;
3189
3190
24.4k
    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
23.9k
    case QUIC_CHANNEL_STATE_ACTIVE:
3198
23.9k
        copy_tcause(&ch->terminate_cause, tcause);
3199
3200
23.9k
        ossl_qlog_event_connectivity_connection_closed(ch_get_qlog(ch), tcause);
3201
3202
23.9k
        if (!force_immediate) {
3203
23.8k
            ch_record_state_transition(ch, tcause->remote
3204
23.8k
                                           ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING
3205
23.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
23.8k
            ch->terminate_deadline
3212
23.8k
                = ossl_time_add(get_time(ch),
3213
23.8k
                                ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm),
3214
23.8k
                                                   3));
3215
3216
23.8k
            if (!tcause->remote) {
3217
23.0k
                OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
3218
3219
                /* best effort */
3220
23.0k
                f.error_code = ch->terminate_cause.error_code;
3221
23.0k
                f.frame_type = ch->terminate_cause.frame_type;
3222
23.0k
                f.is_app     = ch->terminate_cause.app;
3223
23.0k
                f.reason     = (char *)ch->terminate_cause.reason;
3224
23.0k
                f.reason_len = ch->terminate_cause.reason_len;
3225
23.0k
                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
23.0k
                ch->conn_close_queued = 1;
3234
23.0k
            }
3235
23.8k
        } else {
3236
44
            ch_on_terminating_timeout(ch);
3237
44
        }
3238
23.9k
        break;
3239
3240
1
    case QUIC_CHANNEL_STATE_TERMINATING_CLOSING:
3241
1
        if (force_immediate)
3242
0
            ch_on_terminating_timeout(ch);
3243
1
        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
1
            ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATING_DRAINING);
3251
3252
1
        break;
3253
3254
506
    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
506
        if (force_immediate)
3260
0
            ch_on_terminating_timeout(ch);
3261
3262
506
        break;
3263
3264
0
    case QUIC_CHANNEL_STATE_TERMINATED:
3265
        /* No-op. */
3266
0
        break;
3267
24.4k
    }
3268
24.4k
}
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.79k
{
3274
1.79k
    QUIC_TERMINATE_CAUSE tcause = {0};
3275
3276
1.79k
    if (!ossl_quic_channel_is_active(ch))
3277
1.03k
        return;
3278
3279
758
    tcause.remote     = 1;
3280
758
    tcause.app        = f->is_app;
3281
758
    tcause.error_code = f->error_code;
3282
758
    tcause.frame_type = f->frame_type;
3283
758
    tcause.reason     = f->reason;
3284
758
    tcause.reason_len = f->reason_len;
3285
758
    ch_start_terminating(ch, &tcause, 0);
3286
758
}
3287
3288
static void free_frame_data(unsigned char *buf, size_t buf_len, void *arg)
3289
10.8k
{
3290
10.8k
    OPENSSL_free(buf);
3291
10.8k
}
3292
3293
static int ch_enqueue_retire_conn_id(QUIC_CHANNEL *ch, uint64_t seq_num)
3294
10.8k
{
3295
10.8k
    BUF_MEM *buf_mem = NULL;
3296
10.8k
    WPACKET wpkt;
3297
10.8k
    size_t l;
3298
3299
10.8k
    ossl_quic_srtm_remove(ch->srtm, ch, seq_num);
3300
3301
10.8k
    if ((buf_mem = BUF_MEM_new()) == NULL)
3302
0
        goto err;
3303
3304
10.8k
    if (!WPACKET_init(&wpkt, buf_mem))
3305
0
        goto err;
3306
3307
10.8k
    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
10.8k
    WPACKET_finish(&wpkt);
3313
10.8k
    if (!WPACKET_get_total_written(&wpkt, &l))
3314
0
        goto err;
3315
3316
10.8k
    if (ossl_quic_cfq_add_frame(ch->cfq, 1, QUIC_PN_SPACE_APP,
3317
10.8k
                                OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, 0,
3318
10.8k
                                (unsigned char *)buf_mem->data, l,
3319
10.8k
                                free_frame_data, NULL) == NULL)
3320
0
        goto err;
3321
3322
10.8k
    buf_mem->data = NULL;
3323
10.8k
    BUF_MEM_free(buf_mem);
3324
10.8k
    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
10.8k
}
3334
3335
void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL *ch,
3336
                                      OSSL_QUIC_FRAME_NEW_CONN_ID *f)
3337
12.8k
{
3338
12.8k
    uint64_t new_remote_seq_num = ch->cur_remote_seq_num;
3339
12.8k
    uint64_t new_retire_prior_to = ch->cur_retire_prior_to;
3340
3341
12.8k
    if (!ossl_quic_channel_is_active(ch))
3342
365
        return;
3343
3344
    /* We allow only two active connection ids; first check some constraints */
3345
12.4k
    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.4k
    if (f->seq_num > new_remote_seq_num)
3356
6.24k
        new_remote_seq_num = f->seq_num;
3357
12.4k
    if (f->retire_prior_to > new_retire_prior_to)
3358
5.80k
        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.4k
    if (new_remote_seq_num - new_retire_prior_to > 1) {
3371
303
        ossl_quic_channel_raise_protocol_error(ch,
3372
303
                                               OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR,
3373
303
                                               OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
3374
303
                                               "active_connection_id limit violated");
3375
303
        return;
3376
303
    }
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.1k
    if (new_retire_prior_to - ch->cur_retire_prior_to > 10) {
3394
43
        ossl_quic_channel_raise_protocol_error(ch,
3395
43
                                               OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR,
3396
43
                                               OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
3397
43
                                               "retiring connection id limit violated");
3398
3399
43
        return;
3400
43
    }
3401
3402
12.1k
    if (new_remote_seq_num > ch->cur_remote_seq_num) {
3403
        /* Add new stateless reset token */
3404
5.89k
        if (!ossl_quic_srtm_add(ch->srtm, ch, new_remote_seq_num,
3405
5.89k
                                &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.89k
        ch->cur_remote_seq_num = new_remote_seq_num;
3414
5.89k
        ch->cur_remote_dcid = f->conn_id;
3415
5.89k
        ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid);
3416
5.89k
    }
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.0k
    while (new_retire_prior_to > ch->cur_retire_prior_to) {
3439
10.8k
        if (!ch_enqueue_retire_conn_id(ch, ch->cur_retire_prior_to))
3440
0
            break;
3441
10.8k
        ++ch->cur_retire_prior_to;
3442
10.8k
    }
3443
12.1k
}
3444
3445
static void ch_save_err_state(QUIC_CHANNEL *ch)
3446
23.5k
{
3447
23.5k
    if (ch->err_state == NULL)
3448
23.5k
        ch->err_state = OSSL_ERR_STATE_new();
3449
3450
23.5k
    if (ch->err_state == NULL)
3451
0
        return;
3452
3453
23.5k
    OSSL_ERR_STATE_save(ch->err_state);
3454
23.5k
}
3455
3456
void ossl_quic_channel_inject(QUIC_CHANNEL *ch, QUIC_URXE *e)
3457
1.98M
{
3458
1.98M
    ossl_qrx_inject_urxe(ch->qrx, e);
3459
1.98M
}
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
16
{
3468
16
    QUIC_TERMINATE_CAUSE tcause = {0};
3469
3470
16
    tcause.error_code   = OSSL_QUIC_ERR_NO_ERROR;
3471
16
    tcause.remote       = 1;
3472
16
    ch_start_terminating(ch, &tcause, 0);
3473
16
}
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
59.0M
{
3497
59.0M
    return ch->net_error;
3498
59.0M
}
3499
3500
void ossl_quic_channel_restore_err_state(QUIC_CHANNEL *ch)
3501
28.0k
{
3502
28.0k
    if (ch == NULL)
3503
0
        return;
3504
3505
28.0k
    if (!ossl_quic_port_is_running(ch->port))
3506
0
        ossl_quic_port_restore_err_state(ch->port);
3507
28.0k
    else
3508
28.0k
        OSSL_ERR_STATE_restore(ch->err_state);
3509
28.0k
}
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
43.7k
{
3520
43.7k
    QUIC_TERMINATE_CAUSE tcause = {0};
3521
43.7k
    int err_reason = error_code == OSSL_QUIC_ERR_INTERNAL_ERROR
3522
43.7k
                     ? ERR_R_INTERNAL_ERROR : SSL_R_QUIC_PROTOCOL_ERROR;
3523
43.7k
    const char *err_str = ossl_quic_err_to_string(error_code);
3524
43.7k
    const char *err_str_pfx = " (", *err_str_sfx = ")";
3525
43.7k
    const char *ft_str = NULL;
3526
43.7k
    const char *ft_str_pfx = " (", *ft_str_sfx = ")";
3527
3528
43.7k
    if (ch->protocol_error)
3529
        /* Only the first call to this function matters. */
3530
20.1k
        return;
3531
3532
23.5k
    if (err_str == NULL) {
3533
8.23k
        err_str     = "";
3534
8.23k
        err_str_pfx = "";
3535
8.23k
        err_str_sfx = "";
3536
8.23k
    }
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
23.5k
    if (err_state != NULL)
3543
62
        OSSL_ERR_STATE_restore(err_state);
3544
3545
23.5k
    if (frame_type != 0) {
3546
13.1k
        ft_str = ossl_quic_frame_type_to_string(frame_type);
3547
13.1k
        if (ft_str == NULL) {
3548
5.04k
            ft_str      = "";
3549
5.04k
            ft_str_pfx  = "";
3550
5.04k
            ft_str_sfx  = "";
3551
5.04k
        }
3552
3553
13.1k
        ERR_raise_data(ERR_LIB_SSL, err_reason,
3554
13.1k
                       "QUIC error code: 0x%llx%s%s%s "
3555
13.1k
                       "(triggered by frame type: 0x%llx%s%s%s), reason: \"%s\"",
3556
13.1k
                       (unsigned long long) error_code,
3557
13.1k
                       err_str_pfx, err_str, err_str_sfx,
3558
13.1k
                       (unsigned long long) frame_type,
3559
13.1k
                       ft_str_pfx, ft_str, ft_str_sfx,
3560
13.1k
                       reason);
3561
13.1k
    } else {
3562
10.4k
        ERR_raise_data(ERR_LIB_SSL, err_reason,
3563
10.4k
                       "QUIC error code: 0x%llx%s%s%s, reason: \"%s\"",
3564
10.4k
                       (unsigned long long) error_code,
3565
10.4k
                       err_str_pfx, err_str, err_str_sfx,
3566
10.4k
                       reason);
3567
10.4k
    }
3568
3569
23.5k
    if (src_file != NULL)
3570
23.5k
        ERR_set_debug(src_file, src_line, src_func);
3571
3572
23.5k
    ch_save_err_state(ch);
3573
3574
23.5k
    tcause.error_code = error_code;
3575
23.5k
    tcause.frame_type = frame_type;
3576
23.5k
    tcause.reason     = reason;
3577
23.5k
    tcause.reason_len = strlen(reason);
3578
3579
23.5k
    ch->protocol_error = 1;
3580
23.5k
    ch_start_terminating(ch, &tcause, 0);
3581
23.5k
}
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
46
{
3589
46
    ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED);
3590
46
}
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
23.4M
{
3599
23.4M
    OSSL_TIME pto;
3600
3601
23.4M
    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
23.4M
    pto = ossl_ackm_get_pto_duration(ch->ackm);
3613
23.4M
    return ossl_time_max(ossl_ms2time(ch->max_idle_timeout),
3614
23.4M
                                      ossl_time_multiply(pto, 3));
3615
23.4M
}
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.22M
{
3623
3.22M
    ch->idle_deadline = ossl_time_add(get_time(ch),
3624
3.22M
                                      ch_get_effective_idle_timeout_duration(ch));
3625
3.22M
}
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
20.2M
{
3633
20.2M
    OSSL_TIME max_span, idle_duration;
3634
3635
20.2M
    idle_duration = ch_get_effective_idle_timeout_duration(ch);
3636
20.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
20.2M
    max_span = ossl_time_divide(idle_duration, 2);
3648
20.2M
    max_span = ossl_time_min(max_span, MAX_NAT_INTERVAL);
3649
20.2M
    ch->ping_deadline = ossl_time_add(get_time(ch), max_span);
3650
20.2M
}
3651
3652
/* Called when the idle timeout expires. */
3653
static void ch_on_idle_timeout(QUIC_CHANNEL *ch)
3654
14.4k
{
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
14.4k
    ch->terminate_cause.app         = 0;
3661
14.4k
    ch->terminate_cause.error_code  = OSSL_QUIC_LOCAL_ERR_IDLE_TIMEOUT;
3662
14.4k
    ch->terminate_cause.frame_type  = 0;
3663
3664
14.4k
    ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED);
3665
14.4k
}
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
95.9k
{
3805
95.9k
    uint64_t rxfc_wnd;
3806
95.9k
    int server_init = ossl_quic_stream_is_server_init(qs);
3807
95.9k
    int local_init = (ch->is_server == server_init);
3808
95.9k
    int is_uni = !ossl_quic_stream_is_bidi(qs);
3809
3810
95.9k
    if (can_send)
3811
36.5k
        if ((qs->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL)
3812
0
            goto err;
3813
3814
95.9k
    if (can_recv)
3815
95.9k
        if ((qs->rstream = ossl_quic_rstream_new(NULL, NULL, 0)) == NULL)
3816
0
            goto err;
3817
3818
    /* TXFC */
3819
95.9k
    if (!ossl_quic_txfc_init(&qs->txfc, &ch->conn_txfc))
3820
0
        goto err;
3821
3822
95.9k
    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
95.9k
        if (can_send) {
3829
36.5k
            uint64_t cwm;
3830
3831
36.5k
            if (is_uni)
3832
0
                cwm = ch->rx_init_max_stream_data_uni;
3833
36.5k
            else if (local_init)
3834
4.94k
                cwm = ch->rx_init_max_stream_data_bidi_local;
3835
31.5k
            else
3836
31.5k
                cwm = ch->rx_init_max_stream_data_bidi_remote;
3837
3838
36.5k
            ossl_quic_txfc_bump_cwm(&qs->txfc, cwm);
3839
36.5k
        }
3840
95.9k
    }
3841
3842
    /* RXFC */
3843
95.9k
    if (!can_recv)
3844
0
        rxfc_wnd = 0;
3845
95.9k
    else if (is_uni)
3846
59.4k
        rxfc_wnd = ch->tx_init_max_stream_data_uni;
3847
36.5k
    else if (local_init)
3848
4.94k
        rxfc_wnd = ch->tx_init_max_stream_data_bidi_local;
3849
31.5k
    else
3850
31.5k
        rxfc_wnd = ch->tx_init_max_stream_data_bidi_remote;
3851
3852
95.9k
    if (!ossl_quic_rxfc_init(&qs->rxfc, &ch->conn_rxfc,
3853
95.9k
                             rxfc_wnd,
3854
95.9k
                             DEFAULT_STREAM_RXFC_MAX_WND_MUL * rxfc_wnd,
3855
95.9k
                             get_time, ch))
3856
0
        goto err;
3857
3858
95.9k
    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
95.9k
}
3867
3868
static uint64_t *ch_get_local_stream_next_ordinal_ptr(QUIC_CHANNEL *ch,
3869
                                                      int is_uni)
3870
11.6k
{
3871
11.6k
    return is_uni ? &ch->next_local_stream_ordinal_uni
3872
11.6k
                  : &ch->next_local_stream_ordinal_bidi;
3873
11.6k
}
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
6.73k
{
3892
6.73k
    const uint64_t *p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni);
3893
3894
6.73k
    return ossl_quic_stream_map_is_local_allowed_by_stream_limit(&ch->qsm,
3895
6.73k
                                                                 *p_next_ordinal,
3896
6.73k
                                                                 is_uni);
3897
6.73k
}
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
4.94k
{
3919
4.94k
    QUIC_STREAM *qs;
3920
4.94k
    int type;
3921
4.94k
    uint64_t stream_id;
3922
4.94k
    uint64_t *p_next_ordinal;
3923
3924
4.94k
    type = ch->is_server ? QUIC_STREAM_INITIATOR_SERVER
3925
4.94k
                         : QUIC_STREAM_INITIATOR_CLIENT;
3926
3927
4.94k
    p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni);
3928
3929
4.94k
    if (is_uni)
3930
0
        type |= QUIC_STREAM_DIR_UNI;
3931
4.94k
    else
3932
4.94k
        type |= QUIC_STREAM_DIR_BIDI;
3933
3934
4.94k
    if (*p_next_ordinal >= ((uint64_t)1) << 62)
3935
0
        return NULL;
3936
3937
4.94k
    stream_id = ((*p_next_ordinal) << 2) | type;
3938
3939
4.94k
    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
4.94k
    if (!ch_init_new_stream(ch, qs, /*can_send=*/1, /*can_recv=*/!is_uni))
3944
0
        goto err;
3945
3946
4.94k
    ++*p_next_ordinal;
3947
4.94k
    return qs;
3948
3949
0
err:
3950
0
    ossl_quic_stream_map_release(&ch->qsm, qs);
3951
0
    return NULL;
3952
4.94k
}
3953
3954
QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch,
3955
                                                 uint64_t stream_id)
3956
91.0k
{
3957
91.0k
    uint64_t peer_role;
3958
91.0k
    int is_uni;
3959
91.0k
    QUIC_STREAM *qs;
3960
3961
91.0k
    peer_role = ch->is_server
3962
91.0k
        ? QUIC_STREAM_INITIATOR_CLIENT
3963
91.0k
        : QUIC_STREAM_INITIATOR_SERVER;
3964
3965
91.0k
    if ((stream_id & QUIC_STREAM_INITIATOR_MASK) != peer_role)
3966
0
        return NULL;
3967
3968
91.0k
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
3969
3970
91.0k
    qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id,
3971
91.0k
                                    stream_id & (QUIC_STREAM_INITIATOR_MASK
3972
91.0k
                                                 | QUIC_STREAM_DIR_MASK));
3973
91.0k
    if (qs == NULL)
3974
0
        return NULL;
3975
3976
91.0k
    if (!ch_init_new_stream(ch, qs, /*can_send=*/!is_uni, /*can_recv=*/1))
3977
0
        goto err;
3978
3979
91.0k
    if (ch->incoming_stream_auto_reject)
3980
0
        ossl_quic_channel_reject_stream(ch, qs);
3981
91.0k
    else
3982
91.0k
        ossl_quic_stream_map_push_accept_queue(&ch->qsm, qs);
3983
3984
91.0k
    return qs;
3985
3986
0
err:
3987
0
    ossl_quic_stream_map_release(&ch->qsm, qs);
3988
0
    return NULL;
3989
91.0k
}
3990
3991
void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL *ch,
3992
                                                       int enable,
3993
                                                       uint64_t aec)
3994
103k
{
3995
103k
    ch->incoming_stream_auto_reject     = (enable != 0);
3996
103k
    ch->incoming_stream_auto_reject_aec = aec;
3997
103k
}
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
17.5k
{
4033
17.5k
    ch->msg_callback = msg_callback;
4034
17.5k
    ch->msg_callback_ssl = msg_callback_ssl;
4035
17.5k
    ossl_qtx_set_msg_callback(ch->qtx, msg_callback, msg_callback_ssl);
4036
17.5k
    ossl_quic_tx_packetiser_set_msg_callback(ch->txp, msg_callback,
4037
17.5k
                                             msg_callback_ssl);
4038
    /*
4039
     * postpone msg callback setting for tserver until port calls
4040
     * port_bind_channel().
4041
     */
4042
17.5k
    if (ch->is_tserver_ch == 0)
4043
17.5k
        ossl_qrx_set_msg_callback(ch->qrx, msg_callback, msg_callback_ssl);
4044
17.5k
}
4045
4046
void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL *ch,
4047
                                            void *msg_callback_arg)
4048
17.5k
{
4049
17.5k
    ch->msg_callback_arg = msg_callback_arg;
4050
17.5k
    ossl_qtx_set_msg_callback_arg(ch->qtx, msg_callback_arg);
4051
17.5k
    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
17.5k
    if (ch->is_tserver_ch == 0)
4058
17.5k
        ossl_qrx_set_msg_callback_arg(ch->qrx, msg_callback_arg);
4059
17.5k
}
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
}