Coverage Report

Created: 2026-10-28 06:28

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