Coverage Report

Created: 2026-10-28 06:28

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