Coverage Report

Created: 2025-12-31 06:58

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