Coverage Report

Created: 2026-10-16 07:31

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