Coverage Report

Created: 2026-09-12 06:55

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