Coverage Report

Created: 2026-10-28 06:28

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