Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/quic/quic_rx_depack.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/packet_quic.h"
11
#include "internal/nelem.h"
12
#include "internal/quic_wire.h"
13
#include "internal/quic_record_rx.h"
14
#include "internal/quic_ackm.h"
15
#include "internal/quic_rx_depack.h"
16
#include "internal/quic_error.h"
17
#include "internal/quic_fc.h"
18
#include "internal/quic_channel.h"
19
#include "internal/sockets.h"
20
21
#include "quic_local.h"
22
#include "quic_channel_local.h"
23
#include "../ssl_local.h"
24
25
/*
26
 * Helper functions to process different frame types.
27
 *
28
 * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
29
 * pointer argument, the few that aren't ACK eliciting will not.  This makes
30
 * them a verifiable pattern against tables where this is specified.
31
 */
32
static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
33
    uint64_t stream_id,
34
    uint64_t frame_type,
35
    QUIC_STREAM **result);
36
37
static int depack_do_frame_padding(PACKET *pkt)
38
584k
{
39
    /* We ignore this frame */
40
584k
    ossl_quic_wire_decode_padding(pkt);
41
584k
    return 1;
42
584k
}
43
44
static int depack_do_frame_ping(PACKET *pkt, QUIC_CHANNEL *ch,
45
    uint32_t enc_level,
46
    OSSL_ACKM_RX_PKT *ackm_data)
47
2.20M
{
48
    /* We ignore this frame, apart from eliciting an ACK */
49
2.20M
    if (!ossl_quic_wire_decode_frame_ping(pkt)) {
50
0
        ossl_quic_channel_raise_protocol_error(ch,
51
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
52
0
            OSSL_QUIC_FRAME_TYPE_PING,
53
0
            "decode error");
54
0
        return 0;
55
0
    }
56
57
2.20M
    ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
58
2.20M
    return 1;
59
2.20M
}
60
61
static int depack_do_frame_ack(PACKET *pkt, QUIC_CHANNEL *ch,
62
    int packet_space, OSSL_TIME received,
63
    uint64_t frame_type,
64
    OSSL_QRX_PKT *qpacket)
65
291k
{
66
291k
    OSSL_QUIC_FRAME_ACK ack;
67
291k
    OSSL_QUIC_ACK_RANGE *p;
68
291k
    uint64_t total_ranges = 0;
69
291k
    uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
70
71
291k
    if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
72
        /* In case sizeof(uint64_t) > sizeof(size_t) */
73
290k
        || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
74
778
        goto malformed;
75
76
290k
    if (ch->num_ack_range_scratch < (size_t)total_ranges) {
77
32.1k
        if ((p = OPENSSL_realloc(ch->ack_range_scratch,
78
32.1k
                 sizeof(OSSL_QUIC_ACK_RANGE)
79
32.1k
                     * (size_t)total_ranges))
80
32.1k
            == NULL)
81
0
            goto malformed;
82
83
32.1k
        ch->ack_range_scratch = p;
84
32.1k
        ch->num_ack_range_scratch = (size_t)total_ranges;
85
32.1k
    }
86
87
290k
    ack.ack_ranges = ch->ack_range_scratch;
88
290k
    ack.num_ack_ranges = (size_t)total_ranges;
89
90
290k
    if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
91
1.03k
        goto malformed;
92
93
289k
    if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
94
50.1k
        && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
95
49.3k
            || ch->rxku_expected)
96
767
        && ack.ack_ranges[0].end >= ch->txku_pn) {
97
        /*
98
         * RFC 9001 s. 6.2: An endpoint that receives an acknowledgment that is
99
         * carried in a packet protected with old keys where any acknowledged
100
         * packet was protected with newer keys MAY treat that as a connection
101
         * error of type KEY_UPDATE_ERROR.
102
         *
103
         * Two cases to handle here:
104
         *
105
         *   - We did spontaneous TXKU, the peer has responded in kind and we
106
         *     have detected RXKU; !ch->rxku_expected, but then it sent a packet
107
         *     with old keys acknowledging a packet in the new key epoch.
108
         *
109
         *     This also covers the case where we got RXKU and triggered
110
         *     solicited TXKU, and then for some reason the peer sent an ACK of
111
         *     a PN in our new TX key epoch with old keys.
112
         *
113
         *   - We did spontaneous TXKU; ch->txku_pn is the starting PN of our
114
         *     new TX key epoch; the peer has not initiated a solicited TXKU in
115
         *     response (so we have not detected RXKU); in this case the RX key
116
         *     epoch has not incremented and ch->rxku_expected is still 1.
117
         */
118
100
        ossl_quic_channel_raise_protocol_error(ch,
119
100
            OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
120
100
            frame_type,
121
100
            "acked packet which initiated a "
122
100
            "key update without a "
123
100
            "corresponding key update");
124
100
        return 0;
125
100
    }
126
127
289k
    if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
128
289k
            packet_space, received))
129
0
        goto malformed;
130
131
289k
    ++ch->diag_num_rx_ack;
132
289k
    return 1;
133
134
1.81k
malformed:
135
1.81k
    ossl_quic_channel_raise_protocol_error(ch,
136
1.81k
        OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
137
1.81k
        frame_type,
138
1.81k
        "decode error");
139
1.81k
    return 0;
140
289k
}
141
142
static int depack_do_frame_reset_stream(PACKET *pkt,
143
    QUIC_CHANNEL *ch,
144
    OSSL_ACKM_RX_PKT *ackm_data)
145
7.45k
{
146
7.45k
    OSSL_QUIC_FRAME_RESET_STREAM frame_data;
147
7.45k
    QUIC_STREAM *stream = NULL;
148
7.45k
    uint64_t fce;
149
150
7.45k
    if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
151
56
        ossl_quic_channel_raise_protocol_error(ch,
152
56
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
153
56
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
154
56
            "decode error");
155
56
        return 0;
156
56
    }
157
158
7.39k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
159
7.39k
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
160
7.39k
            &stream))
161
171
        return 0; /* error already raised for us */
162
163
7.22k
    if (stream == NULL)
164
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
165
166
7.22k
    if (!ossl_quic_stream_has_recv(stream)) {
167
0
        ossl_quic_channel_raise_protocol_error(ch,
168
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
169
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
170
0
            "RESET_STREAM frame for "
171
0
            "TX only stream");
172
0
        return 0;
173
0
    }
174
175
    /*
176
     * The final size field of the RESET_STREAM frame must be used to determine
177
     * how much flow control credit the aborted stream was considered to have
178
     * consumed.
179
     *
180
     * We also need to ensure that if we already have a final size for the
181
     * stream, the RESET_STREAM frame's Final Size field matches this; we SHOULD
182
     * terminate the connection otherwise (RFC 9000 s. 4.5). The RXFC takes care
183
     * of this for us.
184
     */
185
7.22k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
186
7.22k
            frame_data.final_size, /*is_fin=*/1)) {
187
0
        ossl_quic_channel_raise_protocol_error(ch,
188
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
189
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
190
0
            "internal error (flow control)");
191
0
        return 0;
192
0
    }
193
194
    /* Has a flow control error occurred? */
195
7.22k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
196
7.22k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
197
185
        ossl_quic_channel_raise_protocol_error(ch,
198
185
            fce,
199
185
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
200
185
            "flow control violation");
201
185
        return 0;
202
185
    }
203
204
    /*
205
     * Depending on the receive part state this is handled either as a reset
206
     * transition or a no-op (e.g. if a reset has already been received before,
207
     * or the application already retired a FIN). Best effort - there are no
208
     * protocol error conditions we need to check for here.
209
     */
210
7.04k
    ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
211
7.04k
        frame_data.app_error_code,
212
7.04k
        frame_data.final_size);
213
214
7.04k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
215
7.04k
    return 1;
216
7.22k
}
217
218
static int depack_do_frame_stop_sending(PACKET *pkt,
219
    QUIC_CHANNEL *ch,
220
    OSSL_ACKM_RX_PKT *ackm_data)
221
85.2k
{
222
85.2k
    OSSL_QUIC_FRAME_STOP_SENDING frame_data;
223
85.2k
    QUIC_STREAM *stream = NULL;
224
225
85.2k
    if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
226
67
        ossl_quic_channel_raise_protocol_error(ch,
227
67
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
228
67
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
229
67
            "decode error");
230
67
        return 0;
231
67
    }
232
233
85.1k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
234
85.1k
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
235
85.1k
            &stream))
236
178
        return 0; /* error already raised for us */
237
238
85.0k
    if (stream == NULL)
239
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
240
241
85.0k
    if (!ossl_quic_stream_has_send(stream)) {
242
27
        ossl_quic_channel_raise_protocol_error(ch,
243
27
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
244
27
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
245
27
            "STOP_SENDING frame for "
246
27
            "RX only stream");
247
27
        return 0;
248
27
    }
249
250
84.9k
    stream->peer_stop_sending = 1;
251
84.9k
    stream->peer_stop_sending_aec = frame_data.app_error_code;
252
253
    /*
254
     * RFC 9000 s. 3.5: Receiving a STOP_SENDING frame means we must respond in
255
     * turn with a RESET_STREAM frame for the same part of the stream. The other
256
     * part is unaffected.
257
     */
258
84.9k
    ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
259
84.9k
        frame_data.app_error_code);
260
84.9k
    return 1;
261
85.0k
}
262
263
static int depack_do_frame_crypto(PACKET *pkt, QUIC_CHANNEL *ch,
264
    OSSL_QRX_PKT *parent_pkt,
265
    OSSL_ACKM_RX_PKT *ackm_data,
266
    uint64_t *datalen)
267
343k
{
268
343k
    OSSL_QUIC_FRAME_CRYPTO f;
269
343k
    QUIC_RSTREAM *rstream;
270
343k
    QUIC_RXFC *rxfc;
271
272
343k
    *datalen = 0;
273
274
343k
    if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
275
761
        ossl_quic_channel_raise_protocol_error(ch,
276
761
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
277
761
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
278
761
            "decode error");
279
761
        return 0;
280
761
    }
281
282
342k
    if (f.len == 0)
283
107k
        return 1; /* nothing to do */
284
285
234k
    rstream = ch->crypto_recv[ackm_data->pkt_space];
286
234k
    if (!ossl_assert(rstream != NULL))
287
        /*
288
         * This should not happen; we should only have a NULL stream here if
289
         * the EL has been discarded, and if the EL has been discarded we
290
         * shouldn't be here.
291
         */
292
0
        return 0;
293
294
234k
    rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
295
296
234k
    if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
297
234k
            /*is_fin=*/0)) {
298
0
        ossl_quic_channel_raise_protocol_error(ch,
299
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
300
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
301
0
            "internal error (crypto RXFC)");
302
0
        return 0;
303
0
    }
304
305
234k
    if (ossl_quic_rxfc_get_error(rxfc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
306
531
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
307
531
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
308
531
            "exceeded maximum crypto buffer");
309
531
        return 0;
310
531
    }
311
312
233k
    if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
313
233k
            f.offset, f.data, f.len, 0)) {
314
0
        ossl_quic_channel_raise_protocol_error(ch,
315
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
316
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
317
0
            "internal error (rstream queue)");
318
0
        return 0;
319
0
    }
320
321
233k
    ch->did_crypto_frame = 1;
322
233k
    *datalen = f.len;
323
324
233k
    return 1;
325
233k
}
326
327
static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
328
    OSSL_ACKM_RX_PKT *ackm_data)
329
2.13k
{
330
2.13k
    const uint8_t *token;
331
2.13k
    size_t token_len;
332
333
2.13k
    if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
334
42
        ossl_quic_channel_raise_protocol_error(ch,
335
42
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
336
42
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
337
42
            "decode error");
338
42
        return 0;
339
42
    }
340
341
2.09k
    if (token_len == 0) {
342
        /*
343
         * RFC 9000 s. 19.7: "A client MUST treat receipt of a NEW_TOKEN frame
344
         * with an empty Token field as a connection error of type
345
         * FRAME_ENCODING_ERROR."
346
         */
347
16
        ossl_quic_channel_raise_protocol_error(ch,
348
16
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
349
16
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
350
16
            "zero-length NEW_TOKEN");
351
16
        return 0;
352
16
    }
353
354
    /* store the new token in our token cache */
355
2.07k
    if (!ossl_quic_set_peer_token(ossl_quic_port_get_channel_ctx(ch->port),
356
2.07k
            &ch->cur_peer_addr, token, token_len))
357
0
        return 0;
358
359
2.07k
    return 1;
360
2.07k
}
361
362
/*
363
 * Returns 1 if no protocol violation has occurred. In this case *result will be
364
 * non-NULL unless this is an old deleted stream and we should ignore the frame
365
 * causing this function to be called. Returns 0 on protocol violation.
366
 */
367
static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
368
    uint64_t stream_id,
369
    uint64_t frame_type,
370
    QUIC_STREAM **result)
371
168k
{
372
168k
    QUIC_STREAM *stream;
373
168k
    uint64_t peer_role, stream_ordinal;
374
168k
    uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
375
168k
    QUIC_RXFC *max_streams_fc;
376
168k
    int is_uni, is_remote_init;
377
378
168k
    stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
379
168k
    if (stream != NULL) {
380
153k
        *result = stream;
381
153k
        return 1;
382
153k
    }
383
384
    /*
385
     * If we do not yet have a stream with the given ID, there are three
386
     * possibilities:
387
     *
388
     *   (a) The stream ID is for a remotely-created stream and the peer
389
     *       is creating a stream.
390
     *
391
     *   (b) The stream ID is for a locally-created stream which has
392
     *       previously been deleted.
393
     *
394
     *   (c) The stream ID is for a locally-created stream which does
395
     *       not exist yet. This is a protocol violation and we must
396
     *       terminate the connection in this case.
397
     *
398
     * We distinguish between (b) and (c) using the stream ID allocator
399
     * variable. Since stream ordinals are allocated monotonically, we
400
     * simply determine if the stream ordinal is in the future.
401
     */
402
15.4k
    peer_role = ch->is_server
403
15.4k
        ? QUIC_STREAM_INITIATOR_CLIENT
404
15.4k
        : QUIC_STREAM_INITIATOR_SERVER;
405
406
15.4k
    is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
407
15.4k
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
408
409
15.4k
    stream_ordinal = stream_id >> 2;
410
411
15.4k
    if (is_remote_init) {
412
        /*
413
         * Peer-created stream which does not yet exist. Create it. QUIC stream
414
         * ordinals within a given stream type MUST be used in sequence and
415
         * receiving a STREAM frame for ordinal n must implicitly create streams
416
         * with ordinals [0, n) within that stream type even if no explicit
417
         * STREAM frames are received for those ordinals.
418
         */
419
14.6k
        p_next_ordinal_remote = is_uni
420
14.6k
            ? &ch->next_remote_stream_ordinal_uni
421
14.6k
            : &ch->next_remote_stream_ordinal_bidi;
422
423
        /* Check this isn't violating stream count flow control. */
424
14.6k
        max_streams_fc = is_uni
425
14.6k
            ? &ch->max_streams_uni_rxfc
426
14.6k
            : &ch->max_streams_bidi_rxfc;
427
428
14.6k
        if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
429
14.6k
                stream_ordinal + 1,
430
14.6k
                /*is_fin=*/0)) {
431
0
            ossl_quic_channel_raise_protocol_error(ch,
432
0
                OSSL_QUIC_ERR_INTERNAL_ERROR,
433
0
                frame_type,
434
0
                "internal error (stream count RXFC)");
435
0
            return 0;
436
0
        }
437
438
14.6k
        if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
439
102
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
440
102
                frame_type,
441
102
                "exceeded maximum allowed streams");
442
102
            return 0;
443
102
        }
444
445
        /*
446
         * Create the named stream and any streams coming before it yet to be
447
         * created.
448
         */
449
155k
        while (*p_next_ordinal_remote <= stream_ordinal) {
450
140k
            uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
451
452
140k
            stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
453
140k
            if (stream == NULL) {
454
0
                ossl_quic_channel_raise_protocol_error(ch,
455
0
                    OSSL_QUIC_ERR_INTERNAL_ERROR,
456
0
                    frame_type,
457
0
                    "internal error (stream allocation)");
458
0
                return 0;
459
0
            }
460
461
140k
            ++*p_next_ordinal_remote;
462
140k
        }
463
464
14.5k
        *result = stream;
465
14.5k
    } else {
466
        /* Locally-created stream which does not yet exist. */
467
765
        p_next_ordinal_local = is_uni
468
765
            ? &ch->next_local_stream_ordinal_uni
469
765
            : &ch->next_local_stream_ordinal_bidi;
470
471
765
        if (stream_ordinal >= *p_next_ordinal_local) {
472
            /*
473
             * We never created this stream yet, this is a protocol
474
             * violation.
475
             */
476
765
            ossl_quic_channel_raise_protocol_error(ch,
477
765
                OSSL_QUIC_ERR_STREAM_STATE_ERROR,
478
765
                frame_type,
479
765
                "STREAM frame for nonexistent "
480
765
                "stream");
481
765
            return 0;
482
765
        }
483
484
        /*
485
         * Otherwise this is for an old locally-initiated stream which we
486
         * have subsequently deleted. Ignore the data; it may simply be a
487
         * retransmission. We already take care of notifying the peer of the
488
         * termination of the stream during the stream deletion lifecycle.
489
         */
490
0
        *result = NULL;
491
0
    }
492
493
14.5k
    return 1;
494
15.4k
}
495
496
static int depack_do_frame_stream(PACKET *pkt, QUIC_CHANNEL *ch,
497
    OSSL_QRX_PKT *parent_pkt,
498
    OSSL_ACKM_RX_PKT *ackm_data,
499
    uint64_t frame_type,
500
    uint64_t *datalen)
501
63.1k
{
502
63.1k
    OSSL_QUIC_FRAME_STREAM frame_data;
503
63.1k
    QUIC_STREAM *stream;
504
63.1k
    uint64_t fce;
505
63.1k
    size_t rs_avail;
506
63.1k
    int rs_fin = 0;
507
508
63.1k
    *datalen = 0;
509
510
63.1k
    if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
511
432
        ossl_quic_channel_raise_protocol_error(ch,
512
432
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
513
432
            frame_type,
514
432
            "decode error");
515
432
        return 0;
516
432
    }
517
518
62.7k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
519
62.7k
            frame_type, &stream))
520
326
        return 0; /* protocol error raised by above call */
521
522
62.4k
    if (stream == NULL)
523
        /*
524
         * Data for old stream which is not a protocol violation but should be
525
         * ignored, so stop here.
526
         */
527
0
        return 1;
528
529
62.4k
    if (!ossl_quic_stream_has_recv(stream)) {
530
0
        ossl_quic_channel_raise_protocol_error(ch,
531
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
532
0
            frame_type,
533
0
            "STREAM frame for TX only "
534
0
            "stream");
535
0
        return 0;
536
0
    }
537
538
    /* Notify stream flow controller. */
539
62.4k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
540
62.4k
            frame_data.offset + frame_data.len,
541
62.4k
            frame_data.is_fin)) {
542
0
        ossl_quic_channel_raise_protocol_error(ch,
543
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
544
0
            frame_type,
545
0
            "internal error (flow control)");
546
0
        return 0;
547
0
    }
548
549
    /* Has a flow control error occurred? */
550
62.4k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
551
62.4k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
552
398
        ossl_quic_channel_raise_protocol_error(ch,
553
398
            fce,
554
398
            frame_type,
555
398
            "flow control violation");
556
398
        return 0;
557
398
    }
558
559
62.0k
    switch (stream->recv_state) {
560
15.0k
    case QUIC_RSTREAM_STATE_RECV:
561
52.0k
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
562
        /*
563
         * It only makes sense to process incoming STREAM frames in these
564
         * states.
565
         */
566
52.0k
        break;
567
568
9.80k
    case QUIC_RSTREAM_STATE_DATA_RECVD:
569
9.88k
    case QUIC_RSTREAM_STATE_DATA_READ:
570
9.92k
    case QUIC_RSTREAM_STATE_RESET_RECVD:
571
9.92k
    case QUIC_RSTREAM_STATE_RESET_READ:
572
9.92k
    default:
573
        /*
574
         * We have no use for STREAM frames once the receive part reaches any of
575
         * these states, so just ignore.
576
         */
577
9.92k
        return 1;
578
62.0k
    }
579
580
    /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
581
52.0k
    if (frame_data.is_fin
582
44.3k
        && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
583
584
        /* State was already checked above, so can't fail. */
585
8.01k
        ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
586
8.01k
            frame_data.offset
587
8.01k
                + frame_data.len);
588
8.01k
    }
589
590
    /*
591
     * If we requested STOP_SENDING do not bother buffering the data. Note that
592
     * this must happen after RXFC checks above as even if we sent STOP_SENDING
593
     * we must still enforce correct flow control (RFC 9000 s. 3.5).
594
     */
595
52.0k
    if (stream->stop_sending)
596
0
        return 1; /* not an error - packet reordering, etc. */
597
598
    /*
599
     * The receive stream buffer may or may not choose to consume the data
600
     * without copying by reffing the OSSL_QRX_PKT. In this case
601
     * ossl_qrx_pkt_release() will be eventually called when the data is no
602
     * longer needed.
603
     *
604
     * It is OK for the peer to send us a zero-length non-FIN STREAM frame,
605
     * which is a no-op, aside from the fact that it ensures the stream exists.
606
     * In this case we have nothing to report to the receive buffer.
607
     */
608
52.0k
    if ((frame_data.len > 0 || frame_data.is_fin)
609
51.4k
        && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
610
51.4k
            frame_data.offset,
611
51.4k
            frame_data.data,
612
51.4k
            frame_data.len,
613
51.4k
            frame_data.is_fin)) {
614
0
        ossl_quic_channel_raise_protocol_error(ch,
615
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
616
0
            frame_type,
617
0
            "internal error (rstream queue)");
618
0
        return 0;
619
0
    }
620
621
    /*
622
     * rs_fin will be 1 only if we can read all data up to and including the FIN
623
     * without any gaps before it; this implies we have received all data. Avoid
624
     * calling ossl_quic_rstream_available() where it is not necessary as it is
625
     * more expensive.
626
     */
627
52.0k
    if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
628
45.0k
        && !ossl_quic_rstream_available(stream->rstream, &rs_avail, &rs_fin)) {
629
0
        ossl_quic_channel_raise_protocol_error(ch,
630
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
631
0
            frame_type,
632
0
            "internal error (rstream available)");
633
0
        return 0;
634
0
    }
635
636
52.0k
    if (rs_fin)
637
3.16k
        ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
638
639
52.0k
    *datalen = frame_data.len;
640
641
52.0k
    return 1;
642
52.0k
}
643
644
static void update_streams(QUIC_STREAM *s, void *arg)
645
399k
{
646
399k
    QUIC_CHANNEL *ch = arg;
647
648
399k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
649
399k
}
650
651
static void update_streams_bidi(QUIC_STREAM *s, void *arg)
652
49.8k
{
653
49.8k
    QUIC_CHANNEL *ch = arg;
654
655
49.8k
    if (!ossl_quic_stream_is_bidi(s))
656
13.7k
        return;
657
658
36.0k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
659
36.0k
}
660
661
static void update_streams_uni(QUIC_STREAM *s, void *arg)
662
740k
{
663
740k
    QUIC_CHANNEL *ch = arg;
664
665
740k
    if (ossl_quic_stream_is_bidi(s))
666
103k
        return;
667
668
637k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
669
637k
}
670
671
static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
672
    OSSL_ACKM_RX_PKT *ackm_data)
673
53.5k
{
674
53.5k
    uint64_t max_data = 0;
675
676
53.5k
    if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
677
26
        ossl_quic_channel_raise_protocol_error(ch,
678
26
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
679
26
            OSSL_QUIC_FRAME_TYPE_MAX_DATA,
680
26
            "decode error");
681
26
        return 0;
682
26
    }
683
684
53.5k
    ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
685
53.5k
    ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
686
53.5k
    return 1;
687
53.5k
}
688
689
static int depack_do_frame_max_stream_data(PACKET *pkt,
690
    QUIC_CHANNEL *ch,
691
    OSSL_ACKM_RX_PKT *ackm_data)
692
9.22k
{
693
9.22k
    uint64_t stream_id = 0;
694
9.22k
    uint64_t max_stream_data = 0;
695
9.22k
    QUIC_STREAM *stream;
696
697
9.22k
    if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
698
9.22k
            &max_stream_data)) {
699
34
        ossl_quic_channel_raise_protocol_error(ch,
700
34
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
701
34
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
702
34
            "decode error");
703
34
        return 0;
704
34
    }
705
706
9.19k
    if (!depack_do_implicit_stream_create(ch, stream_id,
707
9.19k
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
708
9.19k
            &stream))
709
114
        return 0; /* error already raised for us */
710
711
9.07k
    if (stream == NULL)
712
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
713
714
9.07k
    if (!ossl_quic_stream_has_send(stream)) {
715
30
        ossl_quic_channel_raise_protocol_error(ch,
716
30
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
717
30
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
718
30
            "MAX_STREAM_DATA for TX only "
719
30
            "stream");
720
30
        return 0;
721
30
    }
722
723
9.04k
    ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
724
9.04k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
725
9.04k
    return 1;
726
9.07k
}
727
728
static int depack_do_frame_max_streams(PACKET *pkt,
729
    QUIC_CHANNEL *ch,
730
    OSSL_ACKM_RX_PKT *ackm_data,
731
    uint64_t frame_type)
732
40.8k
{
733
40.8k
    uint64_t max_streams = 0;
734
735
40.8k
    if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
736
32
        ossl_quic_channel_raise_protocol_error(ch,
737
32
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
738
32
            frame_type,
739
32
            "decode error");
740
32
        return 0;
741
32
    }
742
743
40.8k
    if (max_streams > (((uint64_t)1) << 60)) {
744
61
        ossl_quic_channel_raise_protocol_error(ch,
745
61
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
746
61
            frame_type,
747
61
            "invalid max streams value");
748
61
        return 0;
749
61
    }
750
751
40.8k
    switch (frame_type) {
752
10.5k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
753
10.5k
        if (max_streams > ch->max_local_streams_bidi)
754
660
            ch->max_local_streams_bidi = max_streams;
755
756
        /* Some streams may now be able to send. */
757
10.5k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
758
10.5k
        break;
759
30.2k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
760
30.2k
        if (max_streams > ch->max_local_streams_uni)
761
1.02k
            ch->max_local_streams_uni = max_streams;
762
763
        /* Some streams may now be able to send. */
764
30.2k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
765
30.2k
        break;
766
0
    default:
767
0
        ossl_quic_channel_raise_protocol_error(ch,
768
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
769
0
            frame_type,
770
0
            "decode error");
771
0
        return 0;
772
40.8k
    }
773
774
40.8k
    return 1;
775
40.8k
}
776
777
static int depack_do_frame_data_blocked(PACKET *pkt,
778
    QUIC_CHANNEL *ch,
779
    OSSL_ACKM_RX_PKT *ackm_data)
780
14.0k
{
781
14.0k
    uint64_t max_data = 0;
782
783
14.0k
    if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
784
19
        ossl_quic_channel_raise_protocol_error(ch,
785
19
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
786
19
            OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
787
19
            "decode error");
788
19
        return 0;
789
19
    }
790
791
    /* No-op - informative/debugging frame. */
792
14.0k
    return 1;
793
14.0k
}
794
795
static int depack_do_frame_stream_data_blocked(PACKET *pkt,
796
    QUIC_CHANNEL *ch,
797
    OSSL_ACKM_RX_PKT *ackm_data)
798
4.48k
{
799
4.48k
    uint64_t stream_id = 0;
800
4.48k
    uint64_t max_data = 0;
801
4.48k
    QUIC_STREAM *stream;
802
803
4.48k
    if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
804
4.48k
            &max_data)) {
805
45
        ossl_quic_channel_raise_protocol_error(ch,
806
45
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
807
45
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
808
45
            "decode error");
809
45
        return 0;
810
45
    }
811
812
    /*
813
     * This is an informative/debugging frame, so we don't have to do anything,
814
     * but it does trigger stream creation.
815
     */
816
4.44k
    if (!depack_do_implicit_stream_create(ch, stream_id,
817
4.44k
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
818
4.44k
            &stream))
819
78
        return 0; /* error already raised for us */
820
821
4.36k
    if (stream == NULL)
822
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
823
824
4.36k
    if (!ossl_quic_stream_has_recv(stream)) {
825
        /*
826
         * RFC 9000 s. 19.14: "An endpoint that receives a STREAM_DATA_BLOCKED
827
         * frame for a send-only stream MUST terminate the connection with error
828
         * STREAM_STATE_ERROR."
829
         */
830
0
        ossl_quic_channel_raise_protocol_error(ch,
831
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
832
0
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
833
0
            "STREAM_DATA_BLOCKED frame for "
834
0
            "TX only stream");
835
0
        return 0;
836
0
    }
837
838
    /* No-op - informative/debugging frame. */
839
4.36k
    return 1;
840
4.36k
}
841
842
static int depack_do_frame_streams_blocked(PACKET *pkt,
843
    QUIC_CHANNEL *ch,
844
    OSSL_ACKM_RX_PKT *ackm_data,
845
    uint64_t frame_type)
846
104k
{
847
104k
    uint64_t max_data = 0;
848
849
104k
    if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
850
29
        ossl_quic_channel_raise_protocol_error(ch,
851
29
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
852
29
            frame_type,
853
29
            "decode error");
854
29
        return 0;
855
29
    }
856
857
104k
    if (max_data > (((uint64_t)1) << 60)) {
858
        /*
859
         * RFC 9000 s. 19.14: "This value cannot exceed 2**60, as it is not
860
         * possible to encode stream IDs larger than 2**62 - 1. Receipt of a
861
         * frame that encodes a larger stream ID MUST be treated as a connection
862
         * error of type STREAM_LIMIT_ERROR or FRAME_ENCODING_ERROR."
863
         */
864
119
        ossl_quic_channel_raise_protocol_error(ch,
865
119
            OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
866
119
            frame_type,
867
119
            "invalid stream count limit");
868
119
        return 0;
869
119
    }
870
871
    /* No-op - informative/debugging frame. */
872
104k
    return 1;
873
104k
}
874
875
static int depack_do_frame_new_conn_id(PACKET *pkt,
876
    QUIC_CHANNEL *ch,
877
    OSSL_ACKM_RX_PKT *ackm_data)
878
16.1k
{
879
16.1k
    OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
880
881
16.1k
    if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
882
151
        ossl_quic_channel_raise_protocol_error(ch,
883
151
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
884
151
            OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
885
151
            "decode error");
886
151
        return 0;
887
151
    }
888
889
15.9k
    ossl_quic_channel_on_new_conn_id(ch, &frame_data);
890
891
15.9k
    return 1;
892
16.1k
}
893
894
static int depack_do_frame_retire_conn_id(PACKET *pkt,
895
    QUIC_CHANNEL *ch,
896
    OSSL_ACKM_RX_PKT *ackm_data)
897
82
{
898
82
    uint64_t seq_num;
899
900
82
    if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
901
10
        ossl_quic_channel_raise_protocol_error(ch,
902
10
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
903
10
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
904
10
            "decode error");
905
10
        return 0;
906
10
    }
907
908
    /*
909
     * RFC 9000 s. 19.16: "An endpoint cannot send this frame if it was provided
910
     * with a zero-length connection ID by its peer. An endpoint that provides a
911
     * zero-length connection ID MUST treat receipt of a RETIRE_CONNECTION_ID
912
     * frame as a connection error of type PROTOCOL_VIOLATION."
913
     *
914
     * Since we always use a zero-length SCID as a client, there is no case
915
     * where it is valid for a server to send this. Our server support is
916
     * currently non-conformant and for internal testing use; simply handle it
917
     * as a no-op in this case.
918
     *
919
     * TODO(QUIC FUTURE): Revise and implement correctly for server support.
920
     */
921
72
    if (!ch->is_server) {
922
72
        ossl_quic_channel_raise_protocol_error(ch,
923
72
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
924
72
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
925
72
            "conn has zero-length CID");
926
72
        return 0;
927
72
    }
928
929
0
    return 1;
930
72
}
931
932
static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
933
9.76k
{
934
9.76k
    QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg;
935
936
9.76k
    assert(ch->path_response_limit > 0);
937
938
9.76k
    ch->path_response_limit--;
939
940
    /*
941
     * Assume path response frame is being freed on behalf of
942
     * finished TX operation. This is for unit testing purposes
943
     * only. The counter is also bumped when channel is being
944
     * destroyed and CFQ (control frame queue) is freed.
945
     * This currently does not matter for check_pc_flood
946
     * in test/radix/quic_tests.c.
947
     */
948
9.76k
    ch->path_response_tx++;
949
950
9.76k
    OPENSSL_free(buf);
951
9.76k
}
952
953
static int depack_do_frame_path_challenge(PACKET *pkt,
954
    QUIC_CHANNEL *ch,
955
    OSSL_ACKM_RX_PKT *ackm_data)
956
132k
{
957
132k
    uint64_t frame_data = 0;
958
132k
    unsigned char *encoded = NULL;
959
132k
    size_t encoded_len;
960
132k
    WPACKET wpkt;
961
962
132k
    if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
963
27
        ossl_quic_channel_raise_protocol_error(ch,
964
27
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
965
27
            OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
966
27
            "decode error");
967
27
        return 0;
968
27
    }
969
970
132k
    if (ch->seen_path_challenge == 0
971
11.0k
        && ch->path_response_limit < QUIC_PATH_RESPONSE_QLEN) {
972
        /*
973
         * RFC 9000 s. 8.2.2: On receiving a PATH_CHALLENGE frame, an endpoint
974
         * MUST respond by echoing the data contained in the PATH_CHALLENGE
975
         * frame in a PATH_RESPONSE frame.
976
         *
977
         * TODO(QUIC FUTURE): We should try to avoid allocation here in the
978
         * future.
979
         */
980
9.76k
        encoded_len = sizeof(uint64_t) + 1;
981
9.76k
        if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
982
0
            goto err;
983
984
9.76k
        if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
985
0
            goto err;
986
987
9.76k
        if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
988
0
            WPACKET_cleanup(&wpkt);
989
0
            goto err;
990
0
        }
991
992
9.76k
        WPACKET_finish(&wpkt);
993
994
9.76k
        if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
995
9.76k
                OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
996
9.76k
                QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
997
9.76k
                encoded, encoded_len,
998
9.76k
                free_path_response, ch))
999
0
            goto err;
1000
9.76k
        ch->seen_path_challenge = 1;
1001
9.76k
        ch->path_response_limit++;
1002
9.76k
    }
1003
1004
132k
    ch->path_challenge_rx++;
1005
1006
132k
    return 1;
1007
1008
0
err:
1009
0
    OPENSSL_free(encoded);
1010
0
    ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
1011
0
        OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
1012
0
        "internal error");
1013
0
    return 0;
1014
132k
}
1015
1016
static int depack_do_frame_path_response(PACKET *pkt,
1017
    QUIC_CHANNEL *ch,
1018
    OSSL_ACKM_RX_PKT *ackm_data)
1019
20.4k
{
1020
20.4k
    uint64_t frame_data = 0;
1021
1022
20.4k
    if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
1023
23
        ossl_quic_channel_raise_protocol_error(ch,
1024
23
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1025
23
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1026
23
            "decode error");
1027
23
        return 0;
1028
23
    }
1029
1030
    /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
1031
1032
20.4k
    return 1;
1033
20.4k
}
1034
1035
static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
1036
    uint64_t frame_type)
1037
2.28k
{
1038
2.28k
    OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
1039
1040
2.28k
    if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
1041
410
        ossl_quic_channel_raise_protocol_error(ch,
1042
410
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1043
410
            frame_type,
1044
410
            "decode error");
1045
410
        return 0;
1046
410
    }
1047
1048
1.87k
    ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
1049
1.87k
    return 1;
1050
2.28k
}
1051
1052
static int depack_do_frame_handshake_done(PACKET *pkt,
1053
    QUIC_CHANNEL *ch,
1054
    OSSL_ACKM_RX_PKT *ackm_data)
1055
460k
{
1056
460k
    if (!ossl_quic_wire_decode_frame_handshake_done(pkt)) {
1057
        /* This can fail only with an internal error. */
1058
0
        ossl_quic_channel_raise_protocol_error(ch,
1059
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
1060
0
            OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
1061
0
            "internal error (decode frame handshake done)");
1062
0
        return 0;
1063
0
    }
1064
1065
460k
    ossl_quic_channel_on_handshake_confirmed(ch);
1066
460k
    return 1;
1067
460k
}
1068
1069
/* Main frame processor */
1070
1071
static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
1072
    OSSL_QRX_PKT *parent_pkt, uint32_t enc_level,
1073
    OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
1074
361k
{
1075
361k
    uint32_t pkt_type = parent_pkt->hdr->type;
1076
361k
    uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
1077
1078
361k
    if (PACKET_remaining(pkt) == 0) {
1079
        /*
1080
         * RFC 9000 s. 12.4: An endpoint MUST treat receipt of a packet
1081
         * containing no frames as a connection error of type
1082
         * PROTOCOL_VIOLATION.
1083
         */
1084
0
        ossl_quic_channel_raise_protocol_error(ch,
1085
0
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1086
0
            0,
1087
0
            "empty packet payload");
1088
0
        return 0;
1089
0
    }
1090
1091
3.07M
    while (PACKET_remaining(pkt) > 0) {
1092
2.72M
        int was_minimal;
1093
2.72M
        uint64_t frame_type;
1094
2.72M
        const unsigned char *sof = NULL;
1095
2.72M
        uint64_t datalen = 0;
1096
1097
2.72M
        if (ch->msg_callback != NULL)
1098
0
            sof = PACKET_data(pkt);
1099
1100
2.72M
        if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
1101
561
            ossl_quic_channel_raise_protocol_error(ch,
1102
561
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1103
561
                0,
1104
561
                "malformed frame header");
1105
561
            return 0;
1106
561
        }
1107
1108
2.72M
        if (!was_minimal) {
1109
83
            ossl_quic_channel_raise_protocol_error(ch,
1110
83
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1111
83
                frame_type,
1112
83
                "non-minimal frame type encoding");
1113
83
            return 0;
1114
83
        }
1115
1116
        /*
1117
         * There are only a few frame types which are not ACK-eliciting. Handle
1118
         * these centrally to make error handling cases more resilient, as we
1119
         * should tell the ACKM about an ACK-eliciting frame even if it was not
1120
         * successfully handled.
1121
         */
1122
2.72M
        switch (frame_type) {
1123
277k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1124
402k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1125
445k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1126
446k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1127
447k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1128
447k
            break;
1129
2.27M
        default:
1130
2.27M
            ackm_data->is_ack_eliciting = 1;
1131
2.27M
            break;
1132
2.72M
        }
1133
1134
2.72M
        switch (frame_type) {
1135
1.50M
        case OSSL_QUIC_FRAME_TYPE_PING:
1136
            /* Allowed in all packet types */
1137
1.50M
            if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
1138
0
                return 0;
1139
1.50M
            break;
1140
1.50M
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1141
            /* Allowed in all packet types */
1142
277k
            if (!depack_do_frame_padding(pkt))
1143
0
                return 0;
1144
277k
            break;
1145
1146
277k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1147
167k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1148
            /* ACK frames are valid everywhere except in 0RTT packets */
1149
167k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1150
0
                ossl_quic_channel_raise_protocol_error(ch,
1151
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1152
0
                    frame_type,
1153
0
                    "ACK not valid in 0-RTT");
1154
0
                return 0;
1155
0
            }
1156
167k
            if (!depack_do_frame_ack(pkt, ch, packet_space, received,
1157
167k
                    frame_type, parent_pkt))
1158
1.53k
                return 0;
1159
166k
            break;
1160
1161
166k
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1162
            /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
1163
4.03k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1164
4.03k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1165
72
                ossl_quic_channel_raise_protocol_error(ch,
1166
72
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1167
72
                    frame_type,
1168
72
                    "RESET_STREAM not valid in "
1169
72
                    "INITIAL/HANDSHAKE");
1170
72
                return 0;
1171
72
            }
1172
3.95k
            if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
1173
203
                return 0;
1174
3.75k
            break;
1175
43.8k
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1176
            /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
1177
43.8k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1178
43.8k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1179
32
                ossl_quic_channel_raise_protocol_error(ch,
1180
32
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1181
32
                    frame_type,
1182
32
                    "STOP_SENDING not valid in "
1183
32
                    "INITIAL/HANDSHAKE");
1184
32
                return 0;
1185
32
            }
1186
43.8k
            if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
1187
146
                return 0;
1188
43.7k
            break;
1189
183k
        case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1190
            /* CRYPTO frames are valid everywhere except in 0RTT packets */
1191
183k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1192
0
                ossl_quic_channel_raise_protocol_error(ch,
1193
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1194
0
                    frame_type,
1195
0
                    "CRYPTO frame not valid in 0-RTT");
1196
0
                return 0;
1197
0
            }
1198
183k
            if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
1199
724
                return 0;
1200
182k
            break;
1201
182k
        case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1202
            /* NEW_TOKEN frames are valid in 1RTT packets */
1203
2.16k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1204
27
                ossl_quic_channel_raise_protocol_error(ch,
1205
27
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1206
27
                    frame_type,
1207
27
                    "NEW_TOKEN valid only in 1-RTT");
1208
27
                return 0;
1209
27
            }
1210
1211
            /*
1212
             * RFC 9000 s. 19.7: "A server MUST treat receipt of a NEW_TOKEN
1213
             * frame as a connection error of type PROTOCOL_VIOLATION."
1214
             */
1215
2.13k
            if (ch->is_server) {
1216
0
                ossl_quic_channel_raise_protocol_error(ch,
1217
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1218
0
                    frame_type,
1219
0
                    "NEW_TOKEN can only be sent by a server");
1220
0
                return 0;
1221
0
            }
1222
1223
2.13k
            if (!depack_do_frame_new_token(pkt, ch, ackm_data))
1224
58
                return 0;
1225
2.07k
            break;
1226
1227
2.68k
        case OSSL_QUIC_FRAME_TYPE_STREAM:
1228
6.96k
        case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1229
8.08k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1230
10.6k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1231
10.9k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1232
18.2k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1233
18.6k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1234
34.6k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1235
            /* STREAM frames are valid in 0RTT and 1RTT packets */
1236
34.6k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1237
34.6k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1238
199
                ossl_quic_channel_raise_protocol_error(ch,
1239
199
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1240
199
                    frame_type,
1241
199
                    "STREAM valid only in 0/1-RTT");
1242
199
                return 0;
1243
199
            }
1244
34.4k
            if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
1245
34.4k
                    frame_type, &datalen))
1246
604
                return 0;
1247
33.8k
            break;
1248
1249
36.1k
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1250
            /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
1251
36.1k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1252
36.1k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1253
49
                ossl_quic_channel_raise_protocol_error(ch,
1254
49
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1255
49
                    frame_type,
1256
49
                    "MAX_DATA valid only in 0/1-RTT");
1257
49
                return 0;
1258
49
            }
1259
36.0k
            if (!depack_do_frame_max_data(pkt, ch, ackm_data))
1260
14
                return 0;
1261
36.0k
            break;
1262
36.0k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1263
            /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
1264
5.18k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1265
5.18k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1266
15
                ossl_quic_channel_raise_protocol_error(ch,
1267
15
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1268
15
                    frame_type,
1269
15
                    "MAX_STREAM_DATA valid only in 0/1-RTT");
1270
15
                return 0;
1271
15
            }
1272
5.17k
            if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
1273
86
                return 0;
1274
5.08k
            break;
1275
1276
6.08k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1277
20.4k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1278
            /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
1279
20.4k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1280
20.4k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1281
22
                ossl_quic_channel_raise_protocol_error(ch,
1282
22
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1283
22
                    frame_type,
1284
22
                    "MAX_STREAMS valid only in 0/1-RTT");
1285
22
                return 0;
1286
22
            }
1287
20.4k
            if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
1288
20.4k
                    frame_type))
1289
49
                return 0;
1290
20.3k
            break;
1291
1292
20.3k
        case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
1293
            /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1294
9.65k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1295
9.65k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1296
20
                ossl_quic_channel_raise_protocol_error(ch,
1297
20
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1298
20
                    frame_type,
1299
20
                    "DATA_BLOCKED valid only in 0/1-RTT");
1300
20
                return 0;
1301
20
            }
1302
9.63k
            if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
1303
11
                return 0;
1304
9.62k
            break;
1305
9.62k
        case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
1306
            /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1307
2.84k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1308
2.84k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1309
26
                ossl_quic_channel_raise_protocol_error(ch,
1310
26
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1311
26
                    frame_type,
1312
26
                    "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
1313
26
                return 0;
1314
26
            }
1315
2.81k
            if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
1316
77
                return 0;
1317
2.73k
            break;
1318
1319
2.73k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
1320
58.5k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
1321
            /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
1322
58.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1323
58.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1324
42
                ossl_quic_channel_raise_protocol_error(ch,
1325
42
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1326
42
                    frame_type,
1327
42
                    "STREAMS valid only in 0/1-RTT");
1328
42
                return 0;
1329
42
            }
1330
58.4k
            if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
1331
58.4k
                    frame_type))
1332
57
                return 0;
1333
58.4k
            break;
1334
1335
58.4k
        case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1336
            /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
1337
8.79k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1338
8.79k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1339
29
                ossl_quic_channel_raise_protocol_error(ch,
1340
29
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1341
29
                    frame_type,
1342
29
                    "NEW_CONN_ID valid only in 0/1-RTT");
1343
29
                return 0;
1344
29
            }
1345
8.76k
            if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
1346
84
                return 0;
1347
8.68k
            break;
1348
8.68k
        case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1349
            /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
1350
62
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1351
62
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1352
16
                ossl_quic_channel_raise_protocol_error(ch,
1353
16
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1354
16
                    frame_type,
1355
16
                    "RETIRE_CONN_ID valid only in 0/1-RTT");
1356
16
                return 0;
1357
16
            }
1358
46
            if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
1359
46
                return 0;
1360
0
            break;
1361
98.7k
        case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
1362
            /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
1363
98.7k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1364
98.7k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1365
12
                ossl_quic_channel_raise_protocol_error(ch,
1366
12
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1367
12
                    frame_type,
1368
12
                    "PATH_CHALLENGE valid only in 0/1-RTT");
1369
12
                return 0;
1370
12
            }
1371
98.7k
            if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
1372
19
                return 0;
1373
1374
98.7k
            break;
1375
98.7k
        case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1376
            /* PATH_RESPONSE frames are valid in 1RTT packets */
1377
7.02k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1378
8
                ossl_quic_channel_raise_protocol_error(ch,
1379
8
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1380
8
                    frame_type,
1381
8
                    "PATH_CHALLENGE valid only in 1-RTT");
1382
8
                return 0;
1383
8
            }
1384
7.01k
            if (!depack_do_frame_path_response(pkt, ch, ackm_data))
1385
15
                return 0;
1386
6.99k
            break;
1387
1388
6.99k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1389
            /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
1390
440
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1391
440
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1392
19
                ossl_quic_channel_raise_protocol_error(ch,
1393
19
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1394
19
                    frame_type,
1395
19
                    "CONN_CLOSE (APP) valid only in 0/1-RTT");
1396
19
                return 0;
1397
19
            }
1398
            /* FALLTHRU */
1399
1.30k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1400
            /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
1401
1.30k
            if (!depack_do_frame_conn_close(pkt, ch, frame_type))
1402
237
                return 0;
1403
1.07k
            break;
1404
1405
255k
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1406
            /* HANDSHAKE_DONE frames are valid in 1RTT packets */
1407
255k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1408
14
                ossl_quic_channel_raise_protocol_error(ch,
1409
14
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1410
14
                    frame_type,
1411
14
                    "HANDSHAKE_DONE valid only in 1-RTT");
1412
14
                return 0;
1413
14
            }
1414
255k
            if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
1415
0
                return 0;
1416
255k
            break;
1417
1418
255k
        default:
1419
            /* Unknown frame type */
1420
2.74k
            ossl_quic_channel_raise_protocol_error(ch,
1421
2.74k
                OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1422
2.74k
                frame_type,
1423
2.74k
                "Unknown frame type received");
1424
2.74k
            return 0;
1425
2.72M
        }
1426
1427
2.71M
        if (ch->msg_callback != NULL) {
1428
0
            int ctype = SSL3_RT_QUIC_FRAME_FULL;
1429
1430
0
            size_t framelen = PACKET_data(pkt) - sof;
1431
1432
0
            if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) {
1433
0
                ctype = SSL3_RT_QUIC_FRAME_PADDING;
1434
0
            } else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(frame_type)
1435
0
                || frame_type == OSSL_QUIC_FRAME_TYPE_CRYPTO) {
1436
0
                ctype = SSL3_RT_QUIC_FRAME_HEADER;
1437
0
                framelen -= (size_t)datalen;
1438
0
            }
1439
1440
0
            ch->msg_callback(0, OSSL_QUIC1_VERSION, ctype, sof, framelen,
1441
0
                ch->msg_callback_ssl, ch->msg_callback_arg);
1442
0
        }
1443
2.71M
    }
1444
1445
353k
    return 1;
1446
361k
}
1447
1448
QUIC_NEEDS_LOCK
1449
int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
1450
361k
{
1451
361k
    PACKET pkt;
1452
361k
    OSSL_ACKM_RX_PKT ackm_data;
1453
361k
    uint32_t enc_level;
1454
361k
    size_t dgram_len = qpacket->datagram_len;
1455
1456
361k
    if (ch == NULL)
1457
0
        return 0;
1458
1459
361k
    ossl_ch_reset_rx_state(ch);
1460
1461
    /* Initialize |ackm_data| (and reinitialize |ok|)*/
1462
361k
    memset(&ackm_data, 0, sizeof(ackm_data));
1463
    /*
1464
     * ASSUMPTION: All packets that aren't special case have a
1465
     * packet number.
1466
     */
1467
361k
    ackm_data.pkt_num = qpacket->pn;
1468
361k
    ackm_data.time = qpacket->time;
1469
361k
    enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
1470
361k
    if (enc_level >= QUIC_ENC_LEVEL_NUM)
1471
        /*
1472
         * Retry and Version Negotiation packets should not be passed to this
1473
         * function.
1474
         */
1475
0
        return 0;
1476
1477
361k
    ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
1478
1479
    /*
1480
     * RFC 9000 s. 8.1
1481
     * We can consider the connection to be validated, if we receive a packet
1482
     * from the client protected via handshake keys, meaning that the
1483
     * amplification limit no longer applies (i.e. we can set it as validated.
1484
     * Otherwise, add the size of this packet to the unvalidated credit for
1485
     * the connection.
1486
     */
1487
361k
    if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
1488
20.9k
        ossl_quic_tx_packetiser_set_validated(ch->txp);
1489
340k
    else
1490
340k
        ossl_quic_tx_packetiser_add_unvalidated_credit(ch->txp, dgram_len);
1491
1492
    /* Now that special cases are out of the way, parse frames */
1493
361k
    if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
1494
361k
        || !depack_process_frames(ch, &pkt, qpacket,
1495
361k
            enc_level,
1496
361k
            qpacket->time,
1497
361k
            &ackm_data))
1498
7.94k
        return 0;
1499
1500
353k
    ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
1501
1502
353k
    return 1;
1503
361k
}