Coverage Report

Created: 2026-09-12 06:55

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