Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
1.75k
{
330
1.75k
    const uint8_t *token;
331
1.75k
    size_t token_len;
332
333
1.75k
    if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
334
43
        ossl_quic_channel_raise_protocol_error(ch,
335
43
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
336
43
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
337
43
            "decode error");
338
43
        return 0;
339
43
    }
340
341
1.70k
    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
11
        ossl_quic_channel_raise_protocol_error(ch,
348
11
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
349
11
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
350
11
            "zero-length NEW_TOKEN");
351
11
        return 0;
352
11
    }
353
354
    /* TODO(QUIC FUTURE): ADD CODE to send |token| to the session manager */
355
356
1.69k
    return 1;
357
1.70k
}
358
359
/*
360
 * Returns 1 if no protocol violation has occurred. In this case *result will be
361
 * non-NULL unless this is an old deleted stream and we should ignore the frame
362
 * causing this function to be called. Returns 0 on protocol violation.
363
 */
364
static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
365
    uint64_t stream_id,
366
    uint64_t frame_type,
367
    QUIC_STREAM **result)
368
168k
{
369
168k
    QUIC_STREAM *stream;
370
168k
    uint64_t peer_role, stream_ordinal;
371
168k
    uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
372
168k
    QUIC_RXFC *max_streams_fc;
373
168k
    int is_uni, is_remote_init;
374
375
168k
    stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
376
168k
    if (stream != NULL) {
377
153k
        *result = stream;
378
153k
        return 1;
379
153k
    }
380
381
    /*
382
     * If we do not yet have a stream with the given ID, there are three
383
     * possibilities:
384
     *
385
     *   (a) The stream ID is for a remotely-created stream and the peer
386
     *       is creating a stream.
387
     *
388
     *   (b) The stream ID is for a locally-created stream which has
389
     *       previously been deleted.
390
     *
391
     *   (c) The stream ID is for a locally-created stream which does
392
     *       not exist yet. This is a protocol violation and we must
393
     *       terminate the connection in this case.
394
     *
395
     * We distinguish between (b) and (c) using the stream ID allocator
396
     * variable. Since stream ordinals are allocated monotonically, we
397
     * simply determine if the stream ordinal is in the future.
398
     */
399
15.4k
    peer_role = ch->is_server
400
15.4k
        ? QUIC_STREAM_INITIATOR_CLIENT
401
15.4k
        : QUIC_STREAM_INITIATOR_SERVER;
402
403
15.4k
    is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
404
15.4k
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
405
406
15.4k
    stream_ordinal = stream_id >> 2;
407
408
15.4k
    if (is_remote_init) {
409
        /*
410
         * Peer-created stream which does not yet exist. Create it. QUIC stream
411
         * ordinals within a given stream type MUST be used in sequence and
412
         * receiving a STREAM frame for ordinal n must implicitly create streams
413
         * with ordinals [0, n) within that stream type even if no explicit
414
         * STREAM frames are received for those ordinals.
415
         */
416
14.6k
        p_next_ordinal_remote = is_uni
417
14.6k
            ? &ch->next_remote_stream_ordinal_uni
418
14.6k
            : &ch->next_remote_stream_ordinal_bidi;
419
420
        /* Check this isn't violating stream count flow control. */
421
14.6k
        max_streams_fc = is_uni
422
14.6k
            ? &ch->max_streams_uni_rxfc
423
14.6k
            : &ch->max_streams_bidi_rxfc;
424
425
14.6k
        if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
426
14.6k
                stream_ordinal + 1,
427
14.6k
                /*is_fin=*/0)) {
428
0
            ossl_quic_channel_raise_protocol_error(ch,
429
0
                OSSL_QUIC_ERR_INTERNAL_ERROR,
430
0
                frame_type,
431
0
                "internal error (stream count RXFC)");
432
0
            return 0;
433
0
        }
434
435
14.6k
        if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
436
102
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
437
102
                frame_type,
438
102
                "exceeded maximum allowed streams");
439
102
            return 0;
440
102
        }
441
442
        /*
443
         * Create the named stream and any streams coming before it yet to be
444
         * created.
445
         */
446
155k
        while (*p_next_ordinal_remote <= stream_ordinal) {
447
140k
            uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
448
449
140k
            stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
450
140k
            if (stream == NULL) {
451
0
                ossl_quic_channel_raise_protocol_error(ch,
452
0
                    OSSL_QUIC_ERR_INTERNAL_ERROR,
453
0
                    frame_type,
454
0
                    "internal error (stream allocation)");
455
0
                return 0;
456
0
            }
457
458
140k
            ++*p_next_ordinal_remote;
459
140k
        }
460
461
14.5k
        *result = stream;
462
14.5k
    } else {
463
        /* Locally-created stream which does not yet exist. */
464
765
        p_next_ordinal_local = is_uni
465
765
            ? &ch->next_local_stream_ordinal_uni
466
765
            : &ch->next_local_stream_ordinal_bidi;
467
468
765
        if (stream_ordinal >= *p_next_ordinal_local) {
469
            /*
470
             * We never created this stream yet, this is a protocol
471
             * violation.
472
             */
473
765
            ossl_quic_channel_raise_protocol_error(ch,
474
765
                OSSL_QUIC_ERR_STREAM_STATE_ERROR,
475
765
                frame_type,
476
765
                "STREAM frame for nonexistent "
477
765
                "stream");
478
765
            return 0;
479
765
        }
480
481
        /*
482
         * Otherwise this is for an old locally-initiated stream which we
483
         * have subsequently deleted. Ignore the data; it may simply be a
484
         * retransmission. We already take care of notifying the peer of the
485
         * termination of the stream during the stream deletion lifecycle.
486
         */
487
0
        *result = NULL;
488
0
    }
489
490
14.5k
    return 1;
491
15.4k
}
492
493
static int depack_do_frame_stream(PACKET *pkt, QUIC_CHANNEL *ch,
494
    OSSL_QRX_PKT *parent_pkt,
495
    OSSL_ACKM_RX_PKT *ackm_data,
496
    uint64_t frame_type,
497
    uint64_t *datalen)
498
63.1k
{
499
63.1k
    OSSL_QUIC_FRAME_STREAM frame_data;
500
63.1k
    QUIC_STREAM *stream;
501
63.1k
    uint64_t fce;
502
63.1k
    size_t rs_avail;
503
63.1k
    int rs_fin = 0;
504
505
63.1k
    *datalen = 0;
506
507
63.1k
    if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
508
432
        ossl_quic_channel_raise_protocol_error(ch,
509
432
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
510
432
            frame_type,
511
432
            "decode error");
512
432
        return 0;
513
432
    }
514
515
62.7k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
516
62.7k
            frame_type, &stream))
517
326
        return 0; /* protocol error raised by above call */
518
519
62.4k
    if (stream == NULL)
520
        /*
521
         * Data for old stream which is not a protocol violation but should be
522
         * ignored, so stop here.
523
         */
524
0
        return 1;
525
526
62.4k
    if (!ossl_quic_stream_has_recv(stream)) {
527
0
        ossl_quic_channel_raise_protocol_error(ch,
528
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
529
0
            frame_type,
530
0
            "STREAM frame for TX only "
531
0
            "stream");
532
0
        return 0;
533
0
    }
534
535
    /* Notify stream flow controller. */
536
62.4k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
537
62.4k
            frame_data.offset + frame_data.len,
538
62.4k
            frame_data.is_fin)) {
539
0
        ossl_quic_channel_raise_protocol_error(ch,
540
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
541
0
            frame_type,
542
0
            "internal error (flow control)");
543
0
        return 0;
544
0
    }
545
546
    /* Has a flow control error occurred? */
547
62.4k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
548
62.4k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
549
398
        ossl_quic_channel_raise_protocol_error(ch,
550
398
            fce,
551
398
            frame_type,
552
398
            "flow control violation");
553
398
        return 0;
554
398
    }
555
556
62.0k
    switch (stream->recv_state) {
557
15.0k
    case QUIC_RSTREAM_STATE_RECV:
558
52.0k
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
559
        /*
560
         * It only makes sense to process incoming STREAM frames in these
561
         * states.
562
         */
563
52.0k
        break;
564
565
9.80k
    case QUIC_RSTREAM_STATE_DATA_RECVD:
566
9.88k
    case QUIC_RSTREAM_STATE_DATA_READ:
567
9.92k
    case QUIC_RSTREAM_STATE_RESET_RECVD:
568
9.92k
    case QUIC_RSTREAM_STATE_RESET_READ:
569
9.92k
    default:
570
        /*
571
         * We have no use for STREAM frames once the receive part reaches any of
572
         * these states, so just ignore.
573
         */
574
9.92k
        return 1;
575
62.0k
    }
576
577
    /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
578
52.0k
    if (frame_data.is_fin
579
44.3k
        && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
580
581
        /* State was already checked above, so can't fail. */
582
8.01k
        ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
583
8.01k
            frame_data.offset
584
8.01k
                + frame_data.len);
585
8.01k
    }
586
587
    /*
588
     * If we requested STOP_SENDING do not bother buffering the data. Note that
589
     * this must happen after RXFC checks above as even if we sent STOP_SENDING
590
     * we must still enforce correct flow control (RFC 9000 s. 3.5).
591
     */
592
52.0k
    if (stream->stop_sending)
593
0
        return 1; /* not an error - packet reordering, etc. */
594
595
    /*
596
     * The receive stream buffer may or may not choose to consume the data
597
     * without copying by reffing the OSSL_QRX_PKT. In this case
598
     * ossl_qrx_pkt_release() will be eventually called when the data is no
599
     * longer needed.
600
     *
601
     * It is OK for the peer to send us a zero-length non-FIN STREAM frame,
602
     * which is a no-op, aside from the fact that it ensures the stream exists.
603
     * In this case we have nothing to report to the receive buffer.
604
     */
605
52.0k
    if ((frame_data.len > 0 || frame_data.is_fin)
606
51.4k
        && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
607
51.4k
            frame_data.offset,
608
51.4k
            frame_data.data,
609
51.4k
            frame_data.len,
610
51.4k
            frame_data.is_fin)) {
611
0
        ossl_quic_channel_raise_protocol_error(ch,
612
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
613
0
            frame_type,
614
0
            "internal error (rstream queue)");
615
0
        return 0;
616
0
    }
617
618
    /*
619
     * rs_fin will be 1 only if we can read all data up to and including the FIN
620
     * without any gaps before it; this implies we have received all data. Avoid
621
     * calling ossl_quic_rstream_available() where it is not necessary as it is
622
     * more expensive.
623
     */
624
52.0k
    if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
625
45.0k
        && !ossl_quic_rstream_available(stream->rstream, &rs_avail, &rs_fin)) {
626
0
        ossl_quic_channel_raise_protocol_error(ch,
627
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
628
0
            frame_type,
629
0
            "internal error (rstream available)");
630
0
        return 0;
631
0
    }
632
633
52.0k
    if (rs_fin)
634
3.16k
        ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
635
636
52.0k
    *datalen = frame_data.len;
637
638
52.0k
    return 1;
639
52.0k
}
640
641
static void update_streams(QUIC_STREAM *s, void *arg)
642
399k
{
643
399k
    QUIC_CHANNEL *ch = arg;
644
645
399k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
646
399k
}
647
648
static void update_streams_bidi(QUIC_STREAM *s, void *arg)
649
49.8k
{
650
49.8k
    QUIC_CHANNEL *ch = arg;
651
652
49.8k
    if (!ossl_quic_stream_is_bidi(s))
653
13.7k
        return;
654
655
36.0k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
656
36.0k
}
657
658
static void update_streams_uni(QUIC_STREAM *s, void *arg)
659
740k
{
660
740k
    QUIC_CHANNEL *ch = arg;
661
662
740k
    if (ossl_quic_stream_is_bidi(s))
663
103k
        return;
664
665
637k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
666
637k
}
667
668
static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
669
    OSSL_ACKM_RX_PKT *ackm_data)
670
53.5k
{
671
53.5k
    uint64_t max_data = 0;
672
673
53.5k
    if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
674
26
        ossl_quic_channel_raise_protocol_error(ch,
675
26
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
676
26
            OSSL_QUIC_FRAME_TYPE_MAX_DATA,
677
26
            "decode error");
678
26
        return 0;
679
26
    }
680
681
53.5k
    ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
682
53.5k
    ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
683
53.5k
    return 1;
684
53.5k
}
685
686
static int depack_do_frame_max_stream_data(PACKET *pkt,
687
    QUIC_CHANNEL *ch,
688
    OSSL_ACKM_RX_PKT *ackm_data)
689
9.22k
{
690
9.22k
    uint64_t stream_id = 0;
691
9.22k
    uint64_t max_stream_data = 0;
692
9.22k
    QUIC_STREAM *stream;
693
694
9.22k
    if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
695
9.22k
            &max_stream_data)) {
696
34
        ossl_quic_channel_raise_protocol_error(ch,
697
34
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
698
34
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
699
34
            "decode error");
700
34
        return 0;
701
34
    }
702
703
9.19k
    if (!depack_do_implicit_stream_create(ch, stream_id,
704
9.19k
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
705
9.19k
            &stream))
706
114
        return 0; /* error already raised for us */
707
708
9.07k
    if (stream == NULL)
709
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
710
711
9.07k
    if (!ossl_quic_stream_has_send(stream)) {
712
30
        ossl_quic_channel_raise_protocol_error(ch,
713
30
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
714
30
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
715
30
            "MAX_STREAM_DATA for TX only "
716
30
            "stream");
717
30
        return 0;
718
30
    }
719
720
9.04k
    ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
721
9.04k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
722
9.04k
    return 1;
723
9.07k
}
724
725
static int depack_do_frame_max_streams(PACKET *pkt,
726
    QUIC_CHANNEL *ch,
727
    OSSL_ACKM_RX_PKT *ackm_data,
728
    uint64_t frame_type)
729
40.8k
{
730
40.8k
    uint64_t max_streams = 0;
731
732
40.8k
    if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
733
32
        ossl_quic_channel_raise_protocol_error(ch,
734
32
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
735
32
            frame_type,
736
32
            "decode error");
737
32
        return 0;
738
32
    }
739
740
40.8k
    if (max_streams > (((uint64_t)1) << 60)) {
741
61
        ossl_quic_channel_raise_protocol_error(ch,
742
61
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
743
61
            frame_type,
744
61
            "invalid max streams value");
745
61
        return 0;
746
61
    }
747
748
40.8k
    switch (frame_type) {
749
10.5k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
750
10.5k
        if (max_streams > ch->max_local_streams_bidi)
751
660
            ch->max_local_streams_bidi = max_streams;
752
753
        /* Some streams may now be able to send. */
754
10.5k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
755
10.5k
        break;
756
30.2k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
757
30.2k
        if (max_streams > ch->max_local_streams_uni)
758
1.02k
            ch->max_local_streams_uni = max_streams;
759
760
        /* Some streams may now be able to send. */
761
30.2k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
762
30.2k
        break;
763
0
    default:
764
0
        ossl_quic_channel_raise_protocol_error(ch,
765
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
766
0
            frame_type,
767
0
            "decode error");
768
0
        return 0;
769
40.8k
    }
770
771
40.8k
    return 1;
772
40.8k
}
773
774
static int depack_do_frame_data_blocked(PACKET *pkt,
775
    QUIC_CHANNEL *ch,
776
    OSSL_ACKM_RX_PKT *ackm_data)
777
14.0k
{
778
14.0k
    uint64_t max_data = 0;
779
780
14.0k
    if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
781
19
        ossl_quic_channel_raise_protocol_error(ch,
782
19
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
783
19
            OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
784
19
            "decode error");
785
19
        return 0;
786
19
    }
787
788
    /* No-op - informative/debugging frame. */
789
14.0k
    return 1;
790
14.0k
}
791
792
static int depack_do_frame_stream_data_blocked(PACKET *pkt,
793
    QUIC_CHANNEL *ch,
794
    OSSL_ACKM_RX_PKT *ackm_data)
795
4.48k
{
796
4.48k
    uint64_t stream_id = 0;
797
4.48k
    uint64_t max_data = 0;
798
4.48k
    QUIC_STREAM *stream;
799
800
4.48k
    if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
801
4.48k
            &max_data)) {
802
45
        ossl_quic_channel_raise_protocol_error(ch,
803
45
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
804
45
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
805
45
            "decode error");
806
45
        return 0;
807
45
    }
808
809
    /*
810
     * This is an informative/debugging frame, so we don't have to do anything,
811
     * but it does trigger stream creation.
812
     */
813
4.44k
    if (!depack_do_implicit_stream_create(ch, stream_id,
814
4.44k
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
815
4.44k
            &stream))
816
78
        return 0; /* error already raised for us */
817
818
4.36k
    if (stream == NULL)
819
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
820
821
4.36k
    if (!ossl_quic_stream_has_recv(stream)) {
822
        /*
823
         * RFC 9000 s. 19.14: "An endpoint that receives a STREAM_DATA_BLOCKED
824
         * frame for a send-only stream MUST terminate the connection with error
825
         * STREAM_STATE_ERROR."
826
         */
827
0
        ossl_quic_channel_raise_protocol_error(ch,
828
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
829
0
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
830
0
            "STREAM_DATA_BLOCKED frame for "
831
0
            "TX only stream");
832
0
        return 0;
833
0
    }
834
835
    /* No-op - informative/debugging frame. */
836
4.36k
    return 1;
837
4.36k
}
838
839
static int depack_do_frame_streams_blocked(PACKET *pkt,
840
    QUIC_CHANNEL *ch,
841
    OSSL_ACKM_RX_PKT *ackm_data,
842
    uint64_t frame_type)
843
104k
{
844
104k
    uint64_t max_data = 0;
845
846
104k
    if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
847
29
        ossl_quic_channel_raise_protocol_error(ch,
848
29
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
849
29
            frame_type,
850
29
            "decode error");
851
29
        return 0;
852
29
    }
853
854
104k
    if (max_data > (((uint64_t)1) << 60)) {
855
        /*
856
         * RFC 9000 s. 19.14: "This value cannot exceed 2**60, as it is not
857
         * possible to encode stream IDs larger than 2**62 - 1. Receipt of a
858
         * frame that encodes a larger stream ID MUST be treated as a connection
859
         * error of type STREAM_LIMIT_ERROR or FRAME_ENCODING_ERROR."
860
         */
861
119
        ossl_quic_channel_raise_protocol_error(ch,
862
119
            OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
863
119
            frame_type,
864
119
            "invalid stream count limit");
865
119
        return 0;
866
119
    }
867
868
    /* No-op - informative/debugging frame. */
869
104k
    return 1;
870
104k
}
871
872
static int depack_do_frame_new_conn_id(PACKET *pkt,
873
    QUIC_CHANNEL *ch,
874
    OSSL_ACKM_RX_PKT *ackm_data)
875
16.1k
{
876
16.1k
    OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
877
878
16.1k
    if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
879
151
        ossl_quic_channel_raise_protocol_error(ch,
880
151
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
881
151
            OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
882
151
            "decode error");
883
151
        return 0;
884
151
    }
885
886
15.9k
    ossl_quic_channel_on_new_conn_id(ch, &frame_data);
887
888
15.9k
    return 1;
889
16.1k
}
890
891
static int depack_do_frame_retire_conn_id(PACKET *pkt,
892
    QUIC_CHANNEL *ch,
893
    OSSL_ACKM_RX_PKT *ackm_data)
894
82
{
895
82
    uint64_t seq_num;
896
897
82
    if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
898
10
        ossl_quic_channel_raise_protocol_error(ch,
899
10
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
900
10
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
901
10
            "decode error");
902
10
        return 0;
903
10
    }
904
905
    /*
906
     * RFC 9000 s. 19.16: "An endpoint cannot send this frame if it was provided
907
     * with a zero-length connection ID by its peer. An endpoint that provides a
908
     * zero-length connection ID MUST treat receipt of a RETIRE_CONNECTION_ID
909
     * frame as a connection error of type PROTOCOL_VIOLATION."
910
     *
911
     * Since we always use a zero-length SCID as a client, there is no case
912
     * where it is valid for a server to send this. Our server support is
913
     * currently non-conformant and for internal testing use; simply handle it
914
     * as a no-op in this case.
915
     *
916
     * TODO(QUIC SERVER): Revise and implement correctly for server support.
917
     */
918
72
    if (!ch->is_server) {
919
72
        ossl_quic_channel_raise_protocol_error(ch,
920
72
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
921
72
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
922
72
            "conn has zero-length CID");
923
72
        return 0;
924
72
    }
925
926
0
    return 1;
927
72
}
928
929
static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
930
9.76k
{
931
9.76k
    QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg;
932
933
9.76k
    assert(ch->path_response_limit > 0);
934
935
9.76k
    ch->path_response_limit--;
936
937
9.76k
    OPENSSL_free(buf);
938
9.76k
}
939
940
static int depack_do_frame_path_challenge(PACKET *pkt,
941
    QUIC_CHANNEL *ch,
942
    OSSL_ACKM_RX_PKT *ackm_data)
943
132k
{
944
132k
    uint64_t frame_data = 0;
945
132k
    unsigned char *encoded = NULL;
946
132k
    size_t encoded_len;
947
132k
    WPACKET wpkt;
948
949
132k
    if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
950
27
        ossl_quic_channel_raise_protocol_error(ch,
951
27
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
952
27
            OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
953
27
            "decode error");
954
27
        return 0;
955
27
    }
956
957
132k
    if (ch->seen_path_challenge == 0
958
11.0k
        && ch->path_response_limit < QUIC_PATH_RESPONSE_QLEN) {
959
        /*
960
         * RFC 9000 s. 8.2.2: On receiving a PATH_CHALLENGE frame, an endpoint
961
         * MUST respond by echoing the data contained in the PATH_CHALLENGE
962
         * frame in a PATH_RESPONSE frame.
963
         *
964
         * TODO(QUIC FUTURE): We should try to avoid allocation here in the
965
         * future.
966
         */
967
9.76k
        encoded_len = sizeof(uint64_t) + 1;
968
9.76k
        if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
969
0
            goto err;
970
971
9.76k
        if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
972
0
            goto err;
973
974
9.76k
        if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
975
0
            WPACKET_cleanup(&wpkt);
976
0
            goto err;
977
0
        }
978
979
9.76k
        WPACKET_finish(&wpkt);
980
981
9.76k
        if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
982
9.76k
                OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
983
9.76k
                QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
984
9.76k
                encoded, encoded_len,
985
9.76k
                free_path_response, ch))
986
0
            goto err;
987
9.76k
        ch->seen_path_challenge = 1;
988
9.76k
        ch->path_response_limit++;
989
9.76k
    }
990
991
132k
    return 1;
992
993
0
err:
994
0
    OPENSSL_free(encoded);
995
0
    ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
996
0
        OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
997
0
        "internal error");
998
0
    return 0;
999
132k
}
1000
1001
static int depack_do_frame_path_response(PACKET *pkt,
1002
    QUIC_CHANNEL *ch,
1003
    OSSL_ACKM_RX_PKT *ackm_data)
1004
20.4k
{
1005
20.4k
    uint64_t frame_data = 0;
1006
1007
20.4k
    if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
1008
23
        ossl_quic_channel_raise_protocol_error(ch,
1009
23
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1010
23
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1011
23
            "decode error");
1012
23
        return 0;
1013
23
    }
1014
1015
    /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
1016
1017
20.4k
    return 1;
1018
20.4k
}
1019
1020
static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
1021
    uint64_t frame_type)
1022
2.28k
{
1023
2.28k
    OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
1024
1025
2.28k
    if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
1026
410
        ossl_quic_channel_raise_protocol_error(ch,
1027
410
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1028
410
            frame_type,
1029
410
            "decode error");
1030
410
        return 0;
1031
410
    }
1032
1033
1.87k
    ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
1034
1.87k
    return 1;
1035
2.28k
}
1036
1037
static int depack_do_frame_handshake_done(PACKET *pkt,
1038
    QUIC_CHANNEL *ch,
1039
    OSSL_ACKM_RX_PKT *ackm_data)
1040
460k
{
1041
460k
    if (!ossl_quic_wire_decode_frame_handshake_done(pkt)) {
1042
        /* This can fail only with an internal error. */
1043
0
        ossl_quic_channel_raise_protocol_error(ch,
1044
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
1045
0
            OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
1046
0
            "internal error (decode frame handshake done)");
1047
0
        return 0;
1048
0
    }
1049
1050
460k
    ossl_quic_channel_on_handshake_confirmed(ch);
1051
460k
    return 1;
1052
460k
}
1053
1054
/* Main frame processor */
1055
1056
static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
1057
    OSSL_QRX_PKT *parent_pkt, uint32_t enc_level,
1058
    OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
1059
371k
{
1060
371k
    uint32_t pkt_type = parent_pkt->hdr->type;
1061
371k
    uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
1062
1063
371k
    if (PACKET_remaining(pkt) == 0) {
1064
        /*
1065
         * RFC 9000 s. 12.4: An endpoint MUST treat receipt of a packet
1066
         * containing no frames as a connection error of type
1067
         * PROTOCOL_VIOLATION.
1068
         */
1069
0
        ossl_quic_channel_raise_protocol_error(ch,
1070
0
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1071
0
            0,
1072
0
            "empty packet payload");
1073
0
        return 0;
1074
0
    }
1075
1076
2.17M
    while (PACKET_remaining(pkt) > 0) {
1077
1.81M
        int was_minimal;
1078
1.81M
        uint64_t frame_type;
1079
1.81M
        const unsigned char *sof = NULL;
1080
1.81M
        uint64_t datalen = 0;
1081
1082
1.81M
        if (ch->msg_callback != NULL)
1083
0
            sof = PACKET_data(pkt);
1084
1085
1.81M
        if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
1086
462
            ossl_quic_channel_raise_protocol_error(ch,
1087
462
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1088
462
                0,
1089
462
                "malformed frame header");
1090
462
            return 0;
1091
462
        }
1092
1093
1.81M
        if (!was_minimal) {
1094
99
            ossl_quic_channel_raise_protocol_error(ch,
1095
99
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1096
99
                frame_type,
1097
99
                "non-minimal frame type encoding");
1098
99
            return 0;
1099
99
        }
1100
1101
        /*
1102
         * There are only a few frame types which are not ACK-eliciting. Handle
1103
         * these centrally to make error handling cases more resilient, as we
1104
         * should tell the ACKM about an ACK-eliciting frame even if it was not
1105
         * successfully handled.
1106
         */
1107
1.81M
        switch (frame_type) {
1108
306k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1109
400k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1110
448k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1111
448k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1112
449k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1113
449k
            break;
1114
1.36M
        default:
1115
1.36M
            ackm_data->is_ack_eliciting = 1;
1116
1.36M
            break;
1117
1.81M
        }
1118
1119
1.81M
        switch (frame_type) {
1120
700k
        case OSSL_QUIC_FRAME_TYPE_PING:
1121
            /* Allowed in all packet types */
1122
700k
            if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
1123
0
                return 0;
1124
700k
            break;
1125
700k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1126
            /* Allowed in all packet types */
1127
306k
            if (!depack_do_frame_padding(pkt))
1128
0
                return 0;
1129
306k
            break;
1130
1131
306k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1132
141k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1133
            /* ACK frames are valid everywhere except in 0RTT packets */
1134
141k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1135
0
                ossl_quic_channel_raise_protocol_error(ch,
1136
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1137
0
                    frame_type,
1138
0
                    "ACK not valid in 0-RTT");
1139
0
                return 0;
1140
0
            }
1141
141k
            if (!depack_do_frame_ack(pkt, ch, packet_space, received,
1142
141k
                    frame_type, parent_pkt))
1143
1.03k
                return 0;
1144
140k
            break;
1145
1146
140k
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1147
            /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
1148
3.56k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1149
3.56k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1150
64
                ossl_quic_channel_raise_protocol_error(ch,
1151
64
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1152
64
                    frame_type,
1153
64
                    "RESET_STREAM not valid in "
1154
64
                    "INITIAL/HANDSHAKE");
1155
64
                return 0;
1156
64
            }
1157
3.49k
            if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
1158
209
                return 0;
1159
3.28k
            break;
1160
41.4k
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1161
            /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
1162
41.4k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1163
41.4k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1164
31
                ossl_quic_channel_raise_protocol_error(ch,
1165
31
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1166
31
                    frame_type,
1167
31
                    "STOP_SENDING not valid in "
1168
31
                    "INITIAL/HANDSHAKE");
1169
31
                return 0;
1170
31
            }
1171
41.3k
            if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
1172
126
                return 0;
1173
41.2k
            break;
1174
159k
        case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1175
            /* CRYPTO frames are valid everywhere except in 0RTT packets */
1176
159k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1177
0
                ossl_quic_channel_raise_protocol_error(ch,
1178
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1179
0
                    frame_type,
1180
0
                    "CRYPTO frame not valid in 0-RTT");
1181
0
                return 0;
1182
0
            }
1183
159k
            if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
1184
568
                return 0;
1185
158k
            break;
1186
158k
        case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1187
            /* NEW_TOKEN frames are valid in 1RTT packets */
1188
1.77k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1189
21
                ossl_quic_channel_raise_protocol_error(ch,
1190
21
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1191
21
                    frame_type,
1192
21
                    "NEW_TOKEN valid only in 1-RTT");
1193
21
                return 0;
1194
21
            }
1195
1.75k
            if (!depack_do_frame_new_token(pkt, ch, ackm_data))
1196
54
                return 0;
1197
1.69k
            break;
1198
1199
2.55k
        case OSSL_QUIC_FRAME_TYPE_STREAM:
1200
5.76k
        case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1201
6.75k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1202
8.87k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1203
9.16k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1204
14.5k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1205
14.8k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1206
28.9k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1207
            /* STREAM frames are valid in 0RTT and 1RTT packets */
1208
28.9k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1209
28.9k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1210
140
                ossl_quic_channel_raise_protocol_error(ch,
1211
140
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1212
140
                    frame_type,
1213
140
                    "STREAM valid only in 0/1-RTT");
1214
140
                return 0;
1215
140
            }
1216
28.7k
            if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
1217
28.7k
                    frame_type, &datalen))
1218
552
                return 0;
1219
28.2k
            break;
1220
1221
28.2k
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1222
            /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
1223
17.4k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1224
17.4k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1225
39
                ossl_quic_channel_raise_protocol_error(ch,
1226
39
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1227
39
                    frame_type,
1228
39
                    "MAX_DATA valid only in 0/1-RTT");
1229
39
                return 0;
1230
39
            }
1231
17.4k
            if (!depack_do_frame_max_data(pkt, ch, ackm_data))
1232
12
                return 0;
1233
17.4k
            break;
1234
17.4k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1235
            /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
1236
4.07k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1237
4.07k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1238
18
                ossl_quic_channel_raise_protocol_error(ch,
1239
18
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1240
18
                    frame_type,
1241
18
                    "MAX_STREAM_DATA valid only in 0/1-RTT");
1242
18
                return 0;
1243
18
            }
1244
4.05k
            if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
1245
92
                return 0;
1246
3.96k
            break;
1247
1248
4.51k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1249
20.5k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1250
            /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
1251
20.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1252
20.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1253
35
                ossl_quic_channel_raise_protocol_error(ch,
1254
35
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1255
35
                    frame_type,
1256
35
                    "MAX_STREAMS valid only in 0/1-RTT");
1257
35
                return 0;
1258
35
            }
1259
20.4k
            if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
1260
20.4k
                    frame_type))
1261
44
                return 0;
1262
20.4k
            break;
1263
1264
20.4k
        case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
1265
            /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1266
4.42k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1267
4.42k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1268
8
                ossl_quic_channel_raise_protocol_error(ch,
1269
8
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1270
8
                    frame_type,
1271
8
                    "DATA_BLOCKED valid only in 0/1-RTT");
1272
8
                return 0;
1273
8
            }
1274
4.42k
            if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
1275
8
                return 0;
1276
4.41k
            break;
1277
4.41k
        case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
1278
            /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1279
1.69k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1280
1.69k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1281
21
                ossl_quic_channel_raise_protocol_error(ch,
1282
21
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1283
21
                    frame_type,
1284
21
                    "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
1285
21
                return 0;
1286
21
            }
1287
1.67k
            if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
1288
46
                return 0;
1289
1.62k
            break;
1290
1291
1.94k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
1292
46.0k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
1293
            /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
1294
46.0k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1295
46.0k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1296
45
                ossl_quic_channel_raise_protocol_error(ch,
1297
45
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1298
45
                    frame_type,
1299
45
                    "STREAMS valid only in 0/1-RTT");
1300
45
                return 0;
1301
45
            }
1302
45.9k
            if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
1303
45.9k
                    frame_type))
1304
91
                return 0;
1305
45.8k
            break;
1306
1307
45.8k
        case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1308
            /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
1309
7.39k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1310
7.39k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1311
35
                ossl_quic_channel_raise_protocol_error(ch,
1312
35
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1313
35
                    frame_type,
1314
35
                    "NEW_CONN_ID valid only in 0/1-RTT");
1315
35
                return 0;
1316
35
            }
1317
7.35k
            if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
1318
67
                return 0;
1319
7.28k
            break;
1320
7.28k
        case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1321
            /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
1322
64
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1323
64
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1324
28
                ossl_quic_channel_raise_protocol_error(ch,
1325
28
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1326
28
                    frame_type,
1327
28
                    "RETIRE_CONN_ID valid only in 0/1-RTT");
1328
28
                return 0;
1329
28
            }
1330
36
            if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
1331
36
                return 0;
1332
0
            break;
1333
104k
        case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
1334
            /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
1335
104k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1336
104k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1337
12
                ossl_quic_channel_raise_protocol_error(ch,
1338
12
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1339
12
                    frame_type,
1340
12
                    "PATH_CHALLENGE valid only in 0/1-RTT");
1341
12
                return 0;
1342
12
            }
1343
104k
            if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
1344
20
                return 0;
1345
1346
104k
            break;
1347
104k
        case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1348
            /* PATH_RESPONSE frames are valid in 1RTT packets */
1349
13.4k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1350
10
                ossl_quic_channel_raise_protocol_error(ch,
1351
10
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1352
10
                    frame_type,
1353
10
                    "PATH_CHALLENGE valid only in 1-RTT");
1354
10
                return 0;
1355
10
            }
1356
13.4k
            if (!depack_do_frame_path_response(pkt, ch, ackm_data))
1357
8
                return 0;
1358
13.4k
            break;
1359
1360
13.4k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1361
            /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
1362
321
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1363
321
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1364
11
                ossl_quic_channel_raise_protocol_error(ch,
1365
11
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1366
11
                    frame_type,
1367
11
                    "CONN_CLOSE (APP) valid only in 0/1-RTT");
1368
11
                return 0;
1369
11
            }
1370
            /* FALLTHRU */
1371
972
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1372
            /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
1373
972
            if (!depack_do_frame_conn_close(pkt, ch, frame_type))
1374
173
                return 0;
1375
799
            break;
1376
1377
205k
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1378
            /* HANDSHAKE_DONE frames are valid in 1RTT packets */
1379
205k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1380
17
                ossl_quic_channel_raise_protocol_error(ch,
1381
17
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1382
17
                    frame_type,
1383
17
                    "HANDSHAKE_DONE valid only in 1-RTT");
1384
17
                return 0;
1385
17
            }
1386
205k
            if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
1387
0
                return 0;
1388
205k
            break;
1389
1390
205k
        default:
1391
            /* Unknown frame type */
1392
2.23k
            ossl_quic_channel_raise_protocol_error(ch,
1393
2.23k
                OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1394
2.23k
                frame_type,
1395
2.23k
                "Unknown frame type received");
1396
2.23k
            return 0;
1397
1.81M
        }
1398
1399
1.80M
        if (ch->msg_callback != NULL) {
1400
0
            int ctype = SSL3_RT_QUIC_FRAME_FULL;
1401
1402
0
            size_t framelen = PACKET_data(pkt) - sof;
1403
1404
0
            if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) {
1405
0
                ctype = SSL3_RT_QUIC_FRAME_PADDING;
1406
0
            } else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(frame_type)
1407
0
                || frame_type == OSSL_QUIC_FRAME_TYPE_CRYPTO) {
1408
0
                ctype = SSL3_RT_QUIC_FRAME_HEADER;
1409
0
                framelen -= (size_t)datalen;
1410
0
            }
1411
1412
0
            ch->msg_callback(0, OSSL_QUIC1_VERSION, ctype, sof, framelen,
1413
0
                ch->msg_callback_ssl, ch->msg_callback_arg);
1414
0
        }
1415
1.80M
    }
1416
1417
365k
    return 1;
1418
371k
}
1419
1420
QUIC_NEEDS_LOCK
1421
int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
1422
371k
{
1423
371k
    PACKET pkt;
1424
371k
    OSSL_ACKM_RX_PKT ackm_data;
1425
371k
    uint32_t enc_level;
1426
1427
371k
    if (ch == NULL)
1428
0
        return 0;
1429
1430
371k
    ossl_ch_reset_rx_state(ch);
1431
1432
    /* Initialize |ackm_data| (and reinitialize |ok|)*/
1433
371k
    memset(&ackm_data, 0, sizeof(ackm_data));
1434
    /*
1435
     * ASSUMPTION: All packets that aren't special case have a
1436
     * packet number.
1437
     */
1438
371k
    ackm_data.pkt_num = qpacket->pn;
1439
371k
    ackm_data.time = qpacket->time;
1440
371k
    enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
1441
371k
    if (enc_level >= QUIC_ENC_LEVEL_NUM)
1442
        /*
1443
         * Retry and Version Negotiation packets should not be passed to this
1444
         * function.
1445
         */
1446
0
        return 0;
1447
1448
371k
    ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
1449
1450
    /* Now that special cases are out of the way, parse frames */
1451
371k
    if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
1452
371k
        || !depack_process_frames(ch, &pkt, qpacket,
1453
371k
            enc_level,
1454
371k
            qpacket->time,
1455
371k
            &ackm_data))
1456
6.47k
        return 0;
1457
1458
365k
    ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
1459
1460
365k
    return 1;
1461
371k
}