Coverage Report

Created: 2026-10-28 06:28

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