Coverage Report

Created: 2026-04-01 06:39

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-2025 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
597k
{
39
    /* We ignore this frame */
40
597k
    ossl_quic_wire_decode_padding(pkt);
41
597k
    return 1;
42
597k
}
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
1.89M
{
48
    /* We ignore this frame, apart from eliciting an ACK */
49
1.89M
    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
1.89M
    ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
58
1.89M
    return 1;
59
1.89M
}
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
288k
{
66
288k
    OSSL_QUIC_FRAME_ACK ack;
67
288k
    OSSL_QUIC_ACK_RANGE *p;
68
288k
    uint64_t total_ranges = 0;
69
288k
    uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
70
71
288k
    if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
72
        /* In case sizeof(uint64_t) > sizeof(size_t) */
73
287k
        || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
74
987
        goto malformed;
75
76
287k
    if (ch->num_ack_range_scratch < (size_t)total_ranges) {
77
39.3k
        if ((p = OPENSSL_realloc(ch->ack_range_scratch,
78
39.3k
                 sizeof(OSSL_QUIC_ACK_RANGE)
79
39.3k
                     * (size_t)total_ranges))
80
39.3k
            == NULL)
81
0
            goto malformed;
82
83
39.3k
        ch->ack_range_scratch = p;
84
39.3k
        ch->num_ack_range_scratch = (size_t)total_ranges;
85
39.3k
    }
86
87
287k
    ack.ack_ranges = ch->ack_range_scratch;
88
287k
    ack.num_ack_ranges = (size_t)total_ranges;
89
90
287k
    if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
91
1.30k
        goto malformed;
92
93
286k
    if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
94
67.1k
        && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
95
66.3k
            || ch->rxku_expected)
96
833
        && 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
112
        ossl_quic_channel_raise_protocol_error(ch,
119
112
            OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
120
112
            frame_type,
121
112
            "acked packet which initiated a "
122
112
            "key update without a "
123
112
            "corresponding key update");
124
112
        return 0;
125
112
    }
126
127
286k
    if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
128
286k
            packet_space, received))
129
0
        goto malformed;
130
131
286k
    ++ch->diag_num_rx_ack;
132
286k
    return 1;
133
134
2.29k
malformed:
135
2.29k
    ossl_quic_channel_raise_protocol_error(ch,
136
2.29k
        OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
137
2.29k
        frame_type,
138
2.29k
        "decode error");
139
2.29k
    return 0;
140
286k
}
141
142
static int depack_do_frame_reset_stream(PACKET *pkt,
143
    QUIC_CHANNEL *ch,
144
    OSSL_ACKM_RX_PKT *ackm_data)
145
8.45k
{
146
8.45k
    OSSL_QUIC_FRAME_RESET_STREAM frame_data;
147
8.45k
    QUIC_STREAM *stream = NULL;
148
8.45k
    uint64_t fce;
149
150
8.45k
    if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
151
53
        ossl_quic_channel_raise_protocol_error(ch,
152
53
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
153
53
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
154
53
            "decode error");
155
53
        return 0;
156
53
    }
157
158
8.39k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
159
8.39k
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
160
8.39k
            &stream))
161
159
        return 0; /* error already raised for us */
162
163
8.23k
    if (stream == NULL)
164
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
165
166
8.23k
    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
8.23k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
186
8.23k
            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
8.23k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
196
8.23k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
197
170
        ossl_quic_channel_raise_protocol_error(ch,
198
170
            fce,
199
170
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
200
170
            "flow control violation");
201
170
        return 0;
202
170
    }
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
8.06k
    ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
211
8.06k
        frame_data.app_error_code,
212
8.06k
        frame_data.final_size);
213
214
8.06k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
215
8.06k
    return 1;
216
8.23k
}
217
218
static int depack_do_frame_stop_sending(PACKET *pkt,
219
    QUIC_CHANNEL *ch,
220
    OSSL_ACKM_RX_PKT *ackm_data)
221
92.2k
{
222
92.2k
    OSSL_QUIC_FRAME_STOP_SENDING frame_data;
223
92.2k
    QUIC_STREAM *stream = NULL;
224
225
92.2k
    if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
226
53
        ossl_quic_channel_raise_protocol_error(ch,
227
53
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
228
53
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
229
53
            "decode error");
230
53
        return 0;
231
53
    }
232
233
92.1k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
234
92.1k
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
235
92.1k
            &stream))
236
189
        return 0; /* error already raised for us */
237
238
91.9k
    if (stream == NULL)
239
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
240
241
91.9k
    if (!ossl_quic_stream_has_send(stream)) {
242
21
        ossl_quic_channel_raise_protocol_error(ch,
243
21
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
244
21
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
245
21
            "STOP_SENDING frame for "
246
21
            "RX only stream");
247
21
        return 0;
248
21
    }
249
250
91.9k
    stream->peer_stop_sending = 1;
251
91.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
91.9k
    ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
259
91.9k
        frame_data.app_error_code);
260
91.9k
    return 1;
261
91.9k
}
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
336k
{
268
336k
    OSSL_QUIC_FRAME_CRYPTO f;
269
336k
    QUIC_RSTREAM *rstream;
270
336k
    QUIC_RXFC *rxfc;
271
272
336k
    *datalen = 0;
273
274
336k
    if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
275
813
        ossl_quic_channel_raise_protocol_error(ch,
276
813
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
277
813
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
278
813
            "decode error");
279
813
        return 0;
280
813
    }
281
282
335k
    if (f.len == 0)
283
112k
        return 1; /* nothing to do */
284
285
223k
    rstream = ch->crypto_recv[ackm_data->pkt_space];
286
223k
    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
223k
    rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
295
296
223k
    if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
297
223k
            /*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
223k
    if (ossl_quic_rxfc_get_error(rxfc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
306
507
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
307
507
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
308
507
            "exceeded maximum crypto buffer");
309
507
        return 0;
310
507
    }
311
312
223k
    if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
313
223k
            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
223k
    ch->did_crypto_frame = 1;
322
223k
    *datalen = f.len;
323
324
223k
    return 1;
325
223k
}
326
327
static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
328
    OSSL_ACKM_RX_PKT *ackm_data)
329
1.39k
{
330
1.39k
    const uint8_t *token;
331
1.39k
    size_t token_len;
332
333
1.39k
    if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
334
32
        ossl_quic_channel_raise_protocol_error(ch,
335
32
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
336
32
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
337
32
            "decode error");
338
32
        return 0;
339
32
    }
340
341
1.36k
    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
10
        ossl_quic_channel_raise_protocol_error(ch,
348
10
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
349
10
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
350
10
            "zero-length NEW_TOKEN");
351
10
        return 0;
352
10
    }
353
354
    /* TODO(QUIC FUTURE): ADD CODE to send |token| to the session manager */
355
356
1.35k
    return 1;
357
1.36k
}
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
184k
{
369
184k
    QUIC_STREAM *stream;
370
184k
    uint64_t peer_role, stream_ordinal;
371
184k
    uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
372
184k
    QUIC_RXFC *max_streams_fc;
373
184k
    int is_uni, is_remote_init;
374
375
184k
    stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
376
184k
    if (stream != NULL) {
377
168k
        *result = stream;
378
168k
        return 1;
379
168k
    }
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
16.3k
    peer_role = ch->is_server
400
16.3k
        ? QUIC_STREAM_INITIATOR_CLIENT
401
16.3k
        : QUIC_STREAM_INITIATOR_SERVER;
402
403
16.3k
    is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
404
16.3k
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
405
406
16.3k
    stream_ordinal = stream_id >> 2;
407
408
16.3k
    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
15.6k
        p_next_ordinal_remote = is_uni
417
15.6k
            ? &ch->next_remote_stream_ordinal_uni
418
15.6k
            : &ch->next_remote_stream_ordinal_bidi;
419
420
        /* Check this isn't violating stream count flow control. */
421
15.6k
        max_streams_fc = is_uni
422
15.6k
            ? &ch->max_streams_uni_rxfc
423
15.6k
            : &ch->max_streams_bidi_rxfc;
424
425
15.6k
        if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
426
15.6k
                stream_ordinal + 1,
427
15.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
15.6k
        if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
436
119
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
437
119
                frame_type,
438
119
                "exceeded maximum allowed streams");
439
119
            return 0;
440
119
        }
441
442
        /*
443
         * Create the named stream and any streams coming before it yet to be
444
         * created.
445
         */
446
162k
        while (*p_next_ordinal_remote <= stream_ordinal) {
447
146k
            uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
448
449
146k
            stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
450
146k
            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
146k
            ++*p_next_ordinal_remote;
459
146k
        }
460
461
15.5k
        *result = stream;
462
15.5k
    } else {
463
        /* Locally-created stream which does not yet exist. */
464
749
        p_next_ordinal_local = is_uni
465
749
            ? &ch->next_local_stream_ordinal_uni
466
749
            : &ch->next_local_stream_ordinal_bidi;
467
468
749
        if (stream_ordinal >= *p_next_ordinal_local) {
469
            /*
470
             * We never created this stream yet, this is a protocol
471
             * violation.
472
             */
473
749
            ossl_quic_channel_raise_protocol_error(ch,
474
749
                OSSL_QUIC_ERR_STREAM_STATE_ERROR,
475
749
                frame_type,
476
749
                "STREAM frame for nonexistent "
477
749
                "stream");
478
749
            return 0;
479
749
        }
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
15.5k
    return 1;
491
16.3k
}
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
69.5k
{
499
69.5k
    OSSL_QUIC_FRAME_STREAM frame_data;
500
69.5k
    QUIC_STREAM *stream;
501
69.5k
    uint64_t fce;
502
69.5k
    size_t rs_avail;
503
69.5k
    int rs_fin = 0;
504
505
69.5k
    *datalen = 0;
506
507
69.5k
    if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
508
407
        ossl_quic_channel_raise_protocol_error(ch,
509
407
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
510
407
            frame_type,
511
407
            "decode error");
512
407
        return 0;
513
407
    }
514
515
69.1k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
516
69.1k
            frame_type, &stream))
517
358
        return 0; /* protocol error raised by above call */
518
519
68.8k
    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
68.8k
    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
68.8k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
537
68.8k
            frame_data.offset + frame_data.len,
538
68.8k
            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
68.8k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
548
68.8k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
549
415
        ossl_quic_channel_raise_protocol_error(ch,
550
415
            fce,
551
415
            frame_type,
552
415
            "flow control violation");
553
415
        return 0;
554
415
    }
555
556
68.4k
    switch (stream->recv_state) {
557
15.6k
    case QUIC_RSTREAM_STATE_RECV:
558
57.9k
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
559
        /*
560
         * It only makes sense to process incoming STREAM frames in these
561
         * states.
562
         */
563
57.9k
        break;
564
565
10.2k
    case QUIC_RSTREAM_STATE_DATA_RECVD:
566
10.3k
    case QUIC_RSTREAM_STATE_DATA_READ:
567
10.4k
    case QUIC_RSTREAM_STATE_RESET_RECVD:
568
10.4k
    case QUIC_RSTREAM_STATE_RESET_READ:
569
10.4k
    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
10.4k
        return 1;
575
68.4k
    }
576
577
    /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
578
57.9k
    if (frame_data.is_fin
579
49.8k
        && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
580
581
        /* State was already checked above, so can't fail. */
582
8.30k
        ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
583
8.30k
            frame_data.offset
584
8.30k
                + frame_data.len);
585
8.30k
    }
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
57.9k
    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
57.9k
    if ((frame_data.len > 0 || frame_data.is_fin)
606
57.2k
        && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
607
57.2k
            frame_data.offset,
608
57.2k
            frame_data.data,
609
57.2k
            frame_data.len,
610
57.2k
            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
57.9k
    if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
625
50.5k
        && !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
57.9k
    if (rs_fin)
634
3.20k
        ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
635
636
57.9k
    *datalen = frame_data.len;
637
638
57.9k
    return 1;
639
57.9k
}
640
641
static void update_streams(QUIC_STREAM *s, void *arg)
642
456k
{
643
456k
    QUIC_CHANNEL *ch = arg;
644
645
456k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
646
456k
}
647
648
static void update_streams_bidi(QUIC_STREAM *s, void *arg)
649
64.7k
{
650
64.7k
    QUIC_CHANNEL *ch = arg;
651
652
64.7k
    if (!ossl_quic_stream_is_bidi(s))
653
26.7k
        return;
654
655
37.9k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
656
37.9k
}
657
658
static void update_streams_uni(QUIC_STREAM *s, void *arg)
659
717k
{
660
717k
    QUIC_CHANNEL *ch = arg;
661
662
717k
    if (ossl_quic_stream_is_bidi(s))
663
119k
        return;
664
665
597k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
666
597k
}
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
22
        ossl_quic_channel_raise_protocol_error(ch,
675
22
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
676
22
            OSSL_QUIC_FRAME_TYPE_MAX_DATA,
677
22
            "decode error");
678
22
        return 0;
679
22
    }
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
10.3k
{
690
10.3k
    uint64_t stream_id = 0;
691
10.3k
    uint64_t max_stream_data = 0;
692
10.3k
    QUIC_STREAM *stream;
693
694
10.3k
    if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
695
10.3k
            &max_stream_data)) {
696
35
        ossl_quic_channel_raise_protocol_error(ch,
697
35
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
698
35
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
699
35
            "decode error");
700
35
        return 0;
701
35
    }
702
703
10.3k
    if (!depack_do_implicit_stream_create(ch, stream_id,
704
10.3k
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
705
10.3k
            &stream))
706
92
        return 0; /* error already raised for us */
707
708
10.2k
    if (stream == NULL)
709
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
710
711
10.2k
    if (!ossl_quic_stream_has_send(stream)) {
712
25
        ossl_quic_channel_raise_protocol_error(ch,
713
25
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
714
25
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
715
25
            "MAX_STREAM_DATA for TX only "
716
25
            "stream");
717
25
        return 0;
718
25
    }
719
720
10.2k
    ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
721
10.2k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
722
10.2k
    return 1;
723
10.2k
}
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
46.2k
{
730
46.2k
    uint64_t max_streams = 0;
731
732
46.2k
    if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
733
24
        ossl_quic_channel_raise_protocol_error(ch,
734
24
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
735
24
            frame_type,
736
24
            "decode error");
737
24
        return 0;
738
24
    }
739
740
46.2k
    if (max_streams > (((uint64_t)1) << 60)) {
741
53
        ossl_quic_channel_raise_protocol_error(ch,
742
53
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
743
53
            frame_type,
744
53
            "invalid max streams value");
745
53
        return 0;
746
53
    }
747
748
46.1k
    switch (frame_type) {
749
13.1k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
750
13.1k
        if (max_streams > ch->max_local_streams_bidi)
751
662
            ch->max_local_streams_bidi = max_streams;
752
753
        /* Some streams may now be able to send. */
754
13.1k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
755
13.1k
        break;
756
33.0k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
757
33.0k
        if (max_streams > ch->max_local_streams_uni)
758
1.10k
            ch->max_local_streams_uni = max_streams;
759
760
        /* Some streams may now be able to send. */
761
33.0k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
762
33.0k
        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
46.1k
    }
770
771
46.1k
    return 1;
772
46.1k
}
773
774
static int depack_do_frame_data_blocked(PACKET *pkt,
775
    QUIC_CHANNEL *ch,
776
    OSSL_ACKM_RX_PKT *ackm_data)
777
11.8k
{
778
11.8k
    uint64_t max_data = 0;
779
780
11.8k
    if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
781
14
        ossl_quic_channel_raise_protocol_error(ch,
782
14
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
783
14
            OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
784
14
            "decode error");
785
14
        return 0;
786
14
    }
787
788
    /* No-op - informative/debugging frame. */
789
11.8k
    return 1;
790
11.8k
}
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.59k
{
796
4.59k
    uint64_t stream_id = 0;
797
4.59k
    uint64_t max_data = 0;
798
4.59k
    QUIC_STREAM *stream;
799
800
4.59k
    if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
801
4.59k
            &max_data)) {
802
41
        ossl_quic_channel_raise_protocol_error(ch,
803
41
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
804
41
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
805
41
            "decode error");
806
41
        return 0;
807
41
    }
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.55k
    if (!depack_do_implicit_stream_create(ch, stream_id,
814
4.55k
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
815
4.55k
            &stream))
816
70
        return 0; /* error already raised for us */
817
818
4.48k
    if (stream == NULL)
819
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
820
821
4.48k
    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.48k
    return 1;
837
4.48k
}
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
120k
{
844
120k
    uint64_t max_data = 0;
845
846
120k
    if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
847
24
        ossl_quic_channel_raise_protocol_error(ch,
848
24
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
849
24
            frame_type,
850
24
            "decode error");
851
24
        return 0;
852
24
    }
853
854
120k
    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
131
        ossl_quic_channel_raise_protocol_error(ch,
862
131
            OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
863
131
            frame_type,
864
131
            "invalid stream count limit");
865
131
        return 0;
866
131
    }
867
868
    /* No-op - informative/debugging frame. */
869
120k
    return 1;
870
120k
}
871
872
static int depack_do_frame_new_conn_id(PACKET *pkt,
873
    QUIC_CHANNEL *ch,
874
    OSSL_ACKM_RX_PKT *ackm_data)
875
17.7k
{
876
17.7k
    OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
877
878
17.7k
    if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
879
86
        ossl_quic_channel_raise_protocol_error(ch,
880
86
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
881
86
            OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
882
86
            "decode error");
883
86
        return 0;
884
86
    }
885
886
17.6k
    ossl_quic_channel_on_new_conn_id(ch, &frame_data);
887
888
17.6k
    return 1;
889
17.7k
}
890
891
static int depack_do_frame_retire_conn_id(PACKET *pkt,
892
    QUIC_CHANNEL *ch,
893
    OSSL_ACKM_RX_PKT *ackm_data)
894
75
{
895
75
    uint64_t seq_num;
896
897
75
    if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
898
11
        ossl_quic_channel_raise_protocol_error(ch,
899
11
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
900
11
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
901
11
            "decode error");
902
11
        return 0;
903
11
    }
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
64
    if (!ch->is_server) {
919
64
        ossl_quic_channel_raise_protocol_error(ch,
920
64
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
921
64
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
922
64
            "conn has zero-length CID");
923
64
        return 0;
924
64
    }
925
926
0
    return 1;
927
64
}
928
929
static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
930
265k
{
931
265k
    OPENSSL_free(buf);
932
265k
}
933
934
static int depack_do_frame_path_challenge(PACKET *pkt,
935
    QUIC_CHANNEL *ch,
936
    OSSL_ACKM_RX_PKT *ackm_data)
937
265k
{
938
265k
    uint64_t frame_data = 0;
939
265k
    unsigned char *encoded = NULL;
940
265k
    size_t encoded_len;
941
265k
    WPACKET wpkt;
942
943
265k
    if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
944
72
        ossl_quic_channel_raise_protocol_error(ch,
945
72
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
946
72
            OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
947
72
            "decode error");
948
72
        return 0;
949
72
    }
950
951
    /*
952
     * RFC 9000 s. 8.2.2: On receiving a PATH_CHALLENGE frame, an endpoint MUST
953
     * respond by echoing the data contained in the PATH_CHALLENGE frame in a
954
     * PATH_RESPONSE frame.
955
     *
956
     * TODO(QUIC FUTURE): We should try to avoid allocation here in the future.
957
     */
958
265k
    encoded_len = sizeof(uint64_t) + 1;
959
265k
    if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
960
0
        goto err;
961
962
265k
    if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
963
0
        goto err;
964
965
265k
    if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
966
0
        WPACKET_cleanup(&wpkt);
967
0
        goto err;
968
0
    }
969
970
265k
    WPACKET_finish(&wpkt);
971
972
265k
    if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
973
265k
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
974
265k
            QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
975
265k
            encoded, encoded_len,
976
265k
            free_path_response, NULL))
977
0
        goto err;
978
979
265k
    return 1;
980
981
0
err:
982
0
    OPENSSL_free(encoded);
983
0
    ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
984
0
        OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
985
0
        "internal error");
986
0
    return 0;
987
265k
}
988
989
static int depack_do_frame_path_response(PACKET *pkt,
990
    QUIC_CHANNEL *ch,
991
    OSSL_ACKM_RX_PKT *ackm_data)
992
30.8k
{
993
30.8k
    uint64_t frame_data = 0;
994
995
30.8k
    if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
996
18
        ossl_quic_channel_raise_protocol_error(ch,
997
18
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
998
18
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
999
18
            "decode error");
1000
18
        return 0;
1001
18
    }
1002
1003
    /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
1004
1005
30.8k
    return 1;
1006
30.8k
}
1007
1008
static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
1009
    uint64_t frame_type)
1010
2.51k
{
1011
2.51k
    OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
1012
1013
2.51k
    if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
1014
417
        ossl_quic_channel_raise_protocol_error(ch,
1015
417
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1016
417
            frame_type,
1017
417
            "decode error");
1018
417
        return 0;
1019
417
    }
1020
1021
2.09k
    ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
1022
2.09k
    return 1;
1023
2.51k
}
1024
1025
static int depack_do_frame_handshake_done(PACKET *pkt,
1026
    QUIC_CHANNEL *ch,
1027
    OSSL_ACKM_RX_PKT *ackm_data)
1028
558k
{
1029
558k
    if (!ossl_quic_wire_decode_frame_handshake_done(pkt)) {
1030
        /* This can fail only with an internal error. */
1031
0
        ossl_quic_channel_raise_protocol_error(ch,
1032
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
1033
0
            OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
1034
0
            "internal error (decode frame handshake done)");
1035
0
        return 0;
1036
0
    }
1037
1038
558k
    ossl_quic_channel_on_handshake_confirmed(ch);
1039
558k
    return 1;
1040
558k
}
1041
1042
/* Main frame processor */
1043
1044
static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
1045
    OSSL_QRX_PKT *parent_pkt, uint32_t enc_level,
1046
    OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
1047
319k
{
1048
319k
    uint32_t pkt_type = parent_pkt->hdr->type;
1049
319k
    uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
1050
1051
319k
    if (PACKET_remaining(pkt) == 0) {
1052
        /*
1053
         * RFC 9000 s. 12.4: An endpoint MUST treat receipt of a packet
1054
         * containing no frames as a connection error of type
1055
         * PROTOCOL_VIOLATION.
1056
         */
1057
0
        ossl_quic_channel_raise_protocol_error(ch,
1058
0
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1059
0
            0,
1060
0
            "empty packet payload");
1061
0
        return 0;
1062
0
    }
1063
1064
2.11M
    while (PACKET_remaining(pkt) > 0) {
1065
1.80M
        int was_minimal;
1066
1.80M
        uint64_t frame_type;
1067
1.80M
        const unsigned char *sof = NULL;
1068
1.80M
        uint64_t datalen = 0;
1069
1070
1.80M
        if (ch->msg_callback != NULL)
1071
0
            sof = PACKET_data(pkt);
1072
1073
1.80M
        if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
1074
446
            ossl_quic_channel_raise_protocol_error(ch,
1075
446
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1076
446
                0,
1077
446
                "malformed frame header");
1078
446
            return 0;
1079
446
        }
1080
1081
1.80M
        if (!was_minimal) {
1082
86
            ossl_quic_channel_raise_protocol_error(ch,
1083
86
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1084
86
                frame_type,
1085
86
                "non-minimal frame type encoding");
1086
86
            return 0;
1087
86
        }
1088
1089
        /*
1090
         * There are only a few frame types which are not ACK-eliciting. Handle
1091
         * these centrally to make error handling cases more resilient, as we
1092
         * should tell the ACKM about an ACK-eliciting frame even if it was not
1093
         * successfully handled.
1094
         */
1095
1.80M
        switch (frame_type) {
1096
267k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1097
353k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1098
396k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1099
397k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1100
397k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1101
397k
            break;
1102
1.40M
        default:
1103
1.40M
            ackm_data->is_ack_eliciting = 1;
1104
1.40M
            break;
1105
1.80M
        }
1106
1107
1.80M
        switch (frame_type) {
1108
750k
        case OSSL_QUIC_FRAME_TYPE_PING:
1109
            /* Allowed in all packet types */
1110
750k
            if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
1111
0
                return 0;
1112
750k
            break;
1113
750k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1114
            /* Allowed in all packet types */
1115
267k
            if (!depack_do_frame_padding(pkt))
1116
0
                return 0;
1117
267k
            break;
1118
1119
267k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1120
128k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1121
            /* ACK frames are valid everywhere except in 0RTT packets */
1122
128k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1123
0
                ossl_quic_channel_raise_protocol_error(ch,
1124
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1125
0
                    frame_type,
1126
0
                    "ACK not valid in 0-RTT");
1127
0
                return 0;
1128
0
            }
1129
128k
            if (!depack_do_frame_ack(pkt, ch, packet_space, received,
1130
128k
                    frame_type, parent_pkt))
1131
1.05k
                return 0;
1132
127k
            break;
1133
1134
127k
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1135
            /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
1136
3.33k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1137
3.33k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1138
69
                ossl_quic_channel_raise_protocol_error(ch,
1139
69
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1140
69
                    frame_type,
1141
69
                    "RESET_STREAM not valid in "
1142
69
                    "INITIAL/HANDSHAKE");
1143
69
                return 0;
1144
69
            }
1145
3.26k
            if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
1146
142
                return 0;
1147
3.12k
            break;
1148
39.0k
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1149
            /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
1150
39.0k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1151
39.0k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1152
27
                ossl_quic_channel_raise_protocol_error(ch,
1153
27
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1154
27
                    frame_type,
1155
27
                    "STOP_SENDING not valid in "
1156
27
                    "INITIAL/HANDSHAKE");
1157
27
                return 0;
1158
27
            }
1159
39.0k
            if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
1160
114
                return 0;
1161
38.9k
            break;
1162
146k
        case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1163
            /* CRYPTO frames are valid everywhere except in 0RTT packets */
1164
146k
            if (pkt_type == QUIC_PKT_TYPE_0RTT) {
1165
0
                ossl_quic_channel_raise_protocol_error(ch,
1166
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1167
0
                    frame_type,
1168
0
                    "CRYPTO frame not valid in 0-RTT");
1169
0
                return 0;
1170
0
            }
1171
146k
            if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
1172
575
                return 0;
1173
146k
            break;
1174
146k
        case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1175
            /* NEW_TOKEN frames are valid in 1RTT packets */
1176
1.41k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1177
21
                ossl_quic_channel_raise_protocol_error(ch,
1178
21
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1179
21
                    frame_type,
1180
21
                    "NEW_TOKEN valid only in 1-RTT");
1181
21
                return 0;
1182
21
            }
1183
1.39k
            if (!depack_do_frame_new_token(pkt, ch, ackm_data))
1184
42
                return 0;
1185
1.35k
            break;
1186
1187
2.45k
        case OSSL_QUIC_FRAME_TYPE_STREAM:
1188
5.48k
        case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1189
6.39k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1190
8.55k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1191
8.81k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1192
14.0k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1193
14.3k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1194
27.5k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1195
            /* STREAM frames are valid in 0RTT and 1RTT packets */
1196
27.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1197
27.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1198
112
                ossl_quic_channel_raise_protocol_error(ch,
1199
112
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1200
112
                    frame_type,
1201
112
                    "STREAM valid only in 0/1-RTT");
1202
112
                return 0;
1203
112
            }
1204
27.4k
            if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
1205
27.4k
                    frame_type, &datalen))
1206
472
                return 0;
1207
26.9k
            break;
1208
1209
26.9k
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1210
            /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
1211
16.1k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1212
16.1k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1213
24
                ossl_quic_channel_raise_protocol_error(ch,
1214
24
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1215
24
                    frame_type,
1216
24
                    "MAX_DATA valid only in 0/1-RTT");
1217
24
                return 0;
1218
24
            }
1219
16.1k
            if (!depack_do_frame_max_data(pkt, ch, ackm_data))
1220
5
                return 0;
1221
16.1k
            break;
1222
16.1k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1223
            /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
1224
4.13k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1225
4.13k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1226
14
                ossl_quic_channel_raise_protocol_error(ch,
1227
14
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1228
14
                    frame_type,
1229
14
                    "MAX_STREAM_DATA valid only in 0/1-RTT");
1230
14
                return 0;
1231
14
            }
1232
4.11k
            if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
1233
56
                return 0;
1234
4.06k
            break;
1235
1236
5.85k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1237
21.0k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1238
            /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
1239
21.0k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1240
21.0k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1241
27
                ossl_quic_channel_raise_protocol_error(ch,
1242
27
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1243
27
                    frame_type,
1244
27
                    "MAX_STREAMS valid only in 0/1-RTT");
1245
27
                return 0;
1246
27
            }
1247
21.0k
            if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
1248
21.0k
                    frame_type))
1249
31
                return 0;
1250
20.9k
            break;
1251
1252
20.9k
        case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
1253
            /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1254
4.26k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1255
4.26k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1256
7
                ossl_quic_channel_raise_protocol_error(ch,
1257
7
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1258
7
                    frame_type,
1259
7
                    "DATA_BLOCKED valid only in 0/1-RTT");
1260
7
                return 0;
1261
7
            }
1262
4.25k
            if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
1263
5
                return 0;
1264
4.24k
            break;
1265
4.24k
        case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
1266
            /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1267
1.56k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1268
1.56k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1269
24
                ossl_quic_channel_raise_protocol_error(ch,
1270
24
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1271
24
                    frame_type,
1272
24
                    "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
1273
24
                return 0;
1274
24
            }
1275
1.54k
            if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
1276
41
                return 0;
1277
1.50k
            break;
1278
1279
1.96k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
1280
45.1k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
1281
            /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
1282
45.1k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1283
45.1k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1284
38
                ossl_quic_channel_raise_protocol_error(ch,
1285
38
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1286
38
                    frame_type,
1287
38
                    "STREAMS valid only in 0/1-RTT");
1288
38
                return 0;
1289
38
            }
1290
45.1k
            if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
1291
45.1k
                    frame_type))
1292
69
                return 0;
1293
45.0k
            break;
1294
1295
45.0k
        case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1296
            /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
1297
7.27k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1298
7.27k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1299
59
                ossl_quic_channel_raise_protocol_error(ch,
1300
59
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1301
59
                    frame_type,
1302
59
                    "NEW_CONN_ID valid only in 0/1-RTT");
1303
59
                return 0;
1304
59
            }
1305
7.21k
            if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
1306
35
                return 0;
1307
7.17k
            break;
1308
7.17k
        case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1309
            /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
1310
38
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1311
38
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1312
10
                ossl_quic_channel_raise_protocol_error(ch,
1313
10
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1314
10
                    frame_type,
1315
10
                    "RETIRE_CONN_ID valid only in 0/1-RTT");
1316
10
                return 0;
1317
10
            }
1318
28
            if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
1319
28
                return 0;
1320
0
            break;
1321
118k
        case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
1322
            /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
1323
118k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1324
118k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1325
8
                ossl_quic_channel_raise_protocol_error(ch,
1326
8
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1327
8
                    frame_type,
1328
8
                    "PATH_CHALLENGE valid only in 0/1-RTT");
1329
8
                return 0;
1330
8
            }
1331
118k
            if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
1332
24
                return 0;
1333
1334
118k
            break;
1335
118k
        case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1336
            /* PATH_RESPONSE frames are valid in 1RTT packets */
1337
13.4k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1338
11
                ossl_quic_channel_raise_protocol_error(ch,
1339
11
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1340
11
                    frame_type,
1341
11
                    "PATH_CHALLENGE valid only in 1-RTT");
1342
11
                return 0;
1343
11
            }
1344
13.4k
            if (!depack_do_frame_path_response(pkt, ch, ackm_data))
1345
6
                return 0;
1346
13.4k
            break;
1347
1348
13.4k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1349
            /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
1350
344
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1351
344
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1352
9
                ossl_quic_channel_raise_protocol_error(ch,
1353
9
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1354
9
                    frame_type,
1355
9
                    "CONN_CLOSE (APP) valid only in 0/1-RTT");
1356
9
                return 0;
1357
9
            }
1358
            /* FALLTHRU */
1359
1.08k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1360
            /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
1361
1.08k
            if (!depack_do_frame_conn_close(pkt, ch, frame_type))
1362
181
                return 0;
1363
901
            break;
1364
1365
201k
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1366
            /* HANDSHAKE_DONE frames are valid in 1RTT packets */
1367
201k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1368
15
                ossl_quic_channel_raise_protocol_error(ch,
1369
15
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1370
15
                    frame_type,
1371
15
                    "HANDSHAKE_DONE valid only in 1-RTT");
1372
15
                return 0;
1373
15
            }
1374
201k
            if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
1375
0
                return 0;
1376
201k
            break;
1377
1378
201k
        default:
1379
            /* Unknown frame type */
1380
2.16k
            ossl_quic_channel_raise_protocol_error(ch,
1381
2.16k
                OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1382
2.16k
                frame_type,
1383
2.16k
                "Unknown frame type received");
1384
2.16k
            return 0;
1385
1.80M
        }
1386
1387
1.79M
        if (ch->msg_callback != NULL) {
1388
0
            int ctype = SSL3_RT_QUIC_FRAME_FULL;
1389
1390
0
            size_t framelen = PACKET_data(pkt) - sof;
1391
1392
0
            if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) {
1393
0
                ctype = SSL3_RT_QUIC_FRAME_PADDING;
1394
0
            } else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(frame_type)
1395
0
                || frame_type == OSSL_QUIC_FRAME_TYPE_CRYPTO) {
1396
0
                ctype = SSL3_RT_QUIC_FRAME_HEADER;
1397
0
                framelen -= (size_t)datalen;
1398
0
            }
1399
1400
0
            ch->msg_callback(0, OSSL_QUIC1_VERSION, ctype, sof, framelen,
1401
0
                ch->msg_callback_ssl, ch->msg_callback_arg);
1402
0
        }
1403
1.79M
    }
1404
1405
313k
    return 1;
1406
319k
}
1407
1408
QUIC_NEEDS_LOCK
1409
int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
1410
319k
{
1411
319k
    PACKET pkt;
1412
319k
    OSSL_ACKM_RX_PKT ackm_data;
1413
319k
    uint32_t enc_level;
1414
1415
319k
    if (ch == NULL)
1416
0
        return 0;
1417
1418
319k
    ch->did_crypto_frame = 0;
1419
1420
    /* Initialize |ackm_data| (and reinitialize |ok|)*/
1421
319k
    memset(&ackm_data, 0, sizeof(ackm_data));
1422
    /*
1423
     * ASSUMPTION: All packets that aren't special case have a
1424
     * packet number.
1425
     */
1426
319k
    ackm_data.pkt_num = qpacket->pn;
1427
319k
    ackm_data.time = qpacket->time;
1428
319k
    enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
1429
319k
    if (enc_level >= QUIC_ENC_LEVEL_NUM)
1430
        /*
1431
         * Retry and Version Negotiation packets should not be passed to this
1432
         * function.
1433
         */
1434
0
        return 0;
1435
1436
319k
    ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
1437
1438
    /* Now that special cases are out of the way, parse frames */
1439
319k
    if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
1440
319k
        || !depack_process_frames(ch, &pkt, qpacket,
1441
319k
            enc_level,
1442
319k
            qpacket->time,
1443
319k
            &ackm_data))
1444
6.05k
        return 0;
1445
1446
313k
    ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
1447
1448
313k
    return 1;
1449
319k
}