Coverage Report

Created: 2024-07-27 06:39

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