Coverage Report

Created: 2025-08-11 07:04

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