Coverage Report

Created: 2026-10-28 06:28

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