Coverage Report

Created: 2026-04-09 06:50

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