Coverage Report

Created: 2026-09-12 06:55

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