Coverage Report

Created: 2026-06-18 06:34

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