Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/quic/quic_rx_depack.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/packet_quic.h"
11
#include "internal/nelem.h"
12
#include "internal/quic_wire.h"
13
#include "internal/quic_record_rx.h"
14
#include "internal/quic_ackm.h"
15
#include "internal/quic_rx_depack.h"
16
#include "internal/quic_error.h"
17
#include "internal/quic_fc.h"
18
#include "internal/quic_channel.h"
19
#include "internal/sockets.h"
20
21
#include "quic_local.h"
22
#include "quic_channel_local.h"
23
#include "../ssl_local.h"
24
25
/*
26
 * Helper functions to process different frame types.
27
 *
28
 * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
29
 * pointer argument, the few that aren't ACK eliciting will not.  This makes
30
 * them a verifiable pattern against tables where this is specified.
31
 */
32
static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
33
    uint64_t stream_id,
34
    uint64_t frame_type,
35
    QUIC_STREAM **result);
36
37
static int depack_do_frame_padding(PACKET *pkt)
38
255k
{
39
    /* We ignore this frame */
40
255k
    ossl_quic_wire_decode_padding(pkt);
41
255k
    return 1;
42
255k
}
43
44
static int depack_do_frame_ping(PACKET *pkt, QUIC_CHANNEL *ch,
45
    uint32_t enc_level,
46
    OSSL_ACKM_RX_PKT *ackm_data)
47
449k
{
48
    /* We ignore this frame, apart from eliciting an ACK */
49
449k
    if (!ossl_quic_wire_decode_frame_ping(pkt)) {
50
0
        ossl_quic_channel_raise_protocol_error(ch,
51
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
52
0
            OSSL_QUIC_FRAME_TYPE_PING,
53
0
            "decode error");
54
0
        return 0;
55
0
    }
56
57
449k
    ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
58
449k
    return 1;
59
449k
}
60
61
static int depack_do_frame_ack(PACKET *pkt, QUIC_CHANNEL *ch,
62
    int packet_space, OSSL_TIME received,
63
    uint64_t frame_type,
64
    OSSL_QRX_PKT *qpacket)
65
114k
{
66
114k
    OSSL_QUIC_FRAME_ACK ack;
67
114k
    OSSL_QUIC_ACK_RANGE *p;
68
114k
    uint64_t total_ranges = 0;
69
114k
    uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
70
71
114k
    if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
72
        /* In case sizeof(uint64_t) > sizeof(size_t) */
73
114k
        || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
74
656
        goto malformed;
75
76
114k
    if (ch->num_ack_range_scratch < (size_t)total_ranges) {
77
29.5k
        if ((p = OPENSSL_realloc(ch->ack_range_scratch,
78
29.5k
                 sizeof(OSSL_QUIC_ACK_RANGE)
79
29.5k
                     * (size_t)total_ranges))
80
29.5k
            == NULL)
81
0
            goto malformed;
82
83
29.5k
        ch->ack_range_scratch = p;
84
29.5k
        ch->num_ack_range_scratch = (size_t)total_ranges;
85
29.5k
    }
86
87
114k
    ack.ack_ranges = ch->ack_range_scratch;
88
114k
    ack.num_ack_ranges = (size_t)total_ranges;
89
90
114k
    if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
91
1.07k
        goto malformed;
92
93
113k
    if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
94
19.5k
        && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
95
19.1k
            || ch->rxku_expected)
96
445
        && ack.ack_ranges[0].end >= ch->txku_pn) {
97
        /*
98
         * RFC 9001 s. 6.2: An endpoint that receives an acknowledgment that is
99
         * carried in a packet protected with old keys where any acknowledged
100
         * packet was protected with newer keys MAY treat that as a connection
101
         * error of type KEY_UPDATE_ERROR.
102
         *
103
         * Two cases to handle here:
104
         *
105
         *   - We did spontaneous TXKU, the peer has responded in kind and we
106
         *     have detected RXKU; !ch->rxku_expected, but then it sent a packet
107
         *     with old keys acknowledging a packet in the new key epoch.
108
         *
109
         *     This also covers the case where we got RXKU and triggered
110
         *     solicited TXKU, and then for some reason the peer sent an ACK of
111
         *     a PN in our new TX key epoch with old keys.
112
         *
113
         *   - We did spontaneous TXKU; ch->txku_pn is the starting PN of our
114
         *     new TX key epoch; the peer has not initiated a solicited TXKU in
115
         *     response (so we have not detected RXKU); in this case the RX key
116
         *     epoch has not incremented and ch->rxku_expected is still 1.
117
         */
118
33
        ossl_quic_channel_raise_protocol_error(ch,
119
33
            OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
120
33
            frame_type,
121
33
            "acked packet which initiated a "
122
33
            "key update without a "
123
33
            "corresponding key update");
124
33
        return 0;
125
33
    }
126
127
113k
    if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
128
113k
            packet_space, received)) {
129
        /*
130
         * The ACK manager rejects the frame if it acknowledges a packet number
131
         * we have not sent. RFC 9000 s. 13.1 recommends treating this as a
132
         * PROTOCOL_VIOLATION connection error (distinct from a frame decoding
133
         * error, which is handled at the malformed label below).
134
         */
135
1.34k
        ossl_quic_channel_raise_protocol_error(ch,
136
1.34k
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
137
1.34k
            frame_type,
138
1.34k
            "ACK for unsent packet number");
139
1.34k
        return 0;
140
1.34k
    }
141
142
111k
    ++ch->diag_num_rx_ack;
143
111k
    return 1;
144
145
1.73k
malformed:
146
1.73k
    ossl_quic_channel_raise_protocol_error(ch,
147
1.73k
        OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
148
1.73k
        frame_type,
149
1.73k
        "decode error");
150
1.73k
    return 0;
151
113k
}
152
153
static int depack_do_frame_reset_stream(PACKET *pkt,
154
    QUIC_CHANNEL *ch,
155
    OSSL_ACKM_RX_PKT *ackm_data)
156
3.76k
{
157
3.76k
    OSSL_QUIC_FRAME_RESET_STREAM frame_data;
158
3.76k
    QUIC_STREAM *stream = NULL;
159
3.76k
    uint64_t fce;
160
161
3.76k
    if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
162
54
        ossl_quic_channel_raise_protocol_error(ch,
163
54
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
164
54
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
165
54
            "decode error");
166
54
        return 0;
167
54
    }
168
169
3.70k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
170
3.70k
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
171
3.70k
            &stream))
172
124
        return 0; /* error already raised for us */
173
174
3.58k
    if (stream == NULL)
175
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
176
177
3.58k
    if (!ossl_quic_stream_has_recv(stream)) {
178
0
        ossl_quic_channel_raise_protocol_error(ch,
179
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
180
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
181
0
            "RESET_STREAM frame for "
182
0
            "TX only stream");
183
0
        return 0;
184
0
    }
185
186
    /*
187
     * The final size field of the RESET_STREAM frame must be used to determine
188
     * how much flow control credit the aborted stream was considered to have
189
     * consumed.
190
     *
191
     * We also need to ensure that if we already have a final size for the
192
     * stream, the RESET_STREAM frame's Final Size field matches this; we SHOULD
193
     * terminate the connection otherwise (RFC 9000 s. 4.5). The RXFC takes care
194
     * of this for us.
195
     */
196
3.58k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
197
3.58k
            frame_data.final_size, /*is_fin=*/1)) {
198
0
        ossl_quic_channel_raise_protocol_error(ch,
199
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
200
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
201
0
            "internal error (flow control)");
202
0
        return 0;
203
0
    }
204
205
    /* Has a flow control error occurred? */
206
3.58k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
207
3.58k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
208
120
        ossl_quic_channel_raise_protocol_error(ch,
209
120
            fce,
210
120
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
211
120
            "flow control violation");
212
120
        return 0;
213
120
    }
214
215
    /*
216
     * Depending on the receive part state this is handled either as a reset
217
     * transition or a no-op (e.g. if a reset has already been received before,
218
     * or the application already retired a FIN). Best effort - there are no
219
     * protocol error conditions we need to check for here.
220
     */
221
3.46k
    ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
222
3.46k
        frame_data.app_error_code,
223
3.46k
        frame_data.final_size);
224
225
3.46k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
226
3.46k
    return 1;
227
3.58k
}
228
229
static int depack_do_frame_stop_sending(PACKET *pkt,
230
    QUIC_CHANNEL *ch,
231
    OSSL_ACKM_RX_PKT *ackm_data)
232
37.0k
{
233
37.0k
    OSSL_QUIC_FRAME_STOP_SENDING frame_data;
234
37.0k
    QUIC_STREAM *stream = NULL;
235
236
37.0k
    if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
237
59
        ossl_quic_channel_raise_protocol_error(ch,
238
59
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
239
59
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
240
59
            "decode error");
241
59
        return 0;
242
59
    }
243
244
36.9k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
245
36.9k
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
246
36.9k
            &stream))
247
146
        return 0; /* error already raised for us */
248
249
36.8k
    if (stream == NULL)
250
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
251
252
36.8k
    if (!ossl_quic_stream_has_send(stream)) {
253
22
        ossl_quic_channel_raise_protocol_error(ch,
254
22
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
255
22
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
256
22
            "STOP_SENDING frame for "
257
22
            "RX only stream");
258
22
        return 0;
259
22
    }
260
261
36.7k
    stream->peer_stop_sending = 1;
262
36.7k
    stream->peer_stop_sending_aec = frame_data.app_error_code;
263
264
    /*
265
     * RFC 9000 s. 3.5: Receiving a STOP_SENDING frame means we must respond in
266
     * turn with a RESET_STREAM frame for the same part of the stream. The other
267
     * part is unaffected.
268
     */
269
36.7k
    ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
270
36.7k
        frame_data.app_error_code);
271
36.7k
    return 1;
272
36.8k
}
273
274
static int depack_do_frame_crypto(PACKET *pkt, QUIC_CHANNEL *ch,
275
    OSSL_QRX_PKT *parent_pkt,
276
    OSSL_ACKM_RX_PKT *ackm_data,
277
    uint64_t *datalen)
278
184k
{
279
184k
    OSSL_QUIC_FRAME_CRYPTO f;
280
184k
    QUIC_RSTREAM *rstream;
281
184k
    QUIC_RXFC *rxfc;
282
283
184k
    *datalen = 0;
284
285
184k
    if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
286
651
        ossl_quic_channel_raise_protocol_error(ch,
287
651
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
288
651
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
289
651
            "decode error");
290
651
        return 0;
291
651
    }
292
293
183k
    if (f.len == 0)
294
53.6k
        return 1; /* nothing to do */
295
296
129k
    rstream = ch->crypto_recv[ackm_data->pkt_space];
297
129k
    if (!ossl_assert(rstream != NULL))
298
        /*
299
         * This should not happen; we should only have a NULL stream here if
300
         * the EL has been discarded, and if the EL has been discarded we
301
         * shouldn't be here.
302
         */
303
0
        return 0;
304
305
129k
    rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
306
307
129k
    if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
308
129k
            /*is_fin=*/0)) {
309
0
        ossl_quic_channel_raise_protocol_error(ch,
310
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
311
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
312
0
            "internal error (crypto RXFC)");
313
0
        return 0;
314
0
    }
315
316
129k
    if (ossl_quic_rxfc_get_error(rxfc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
317
506
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
318
506
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
319
506
            "exceeded maximum crypto buffer");
320
506
        return 0;
321
506
    }
322
323
129k
    if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
324
129k
            f.offset, f.data, f.len, 0)) {
325
0
        ossl_quic_channel_raise_protocol_error(ch,
326
0
            OSSL_QUIC_ERR_INTERNAL_ERROR,
327
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
328
0
            "internal error (rstream queue)");
329
0
        return 0;
330
0
    }
331
332
129k
    ch->did_crypto_frame = 1;
333
129k
    *datalen = f.len;
334
335
129k
    return 1;
336
129k
}
337
338
static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
339
    OSSL_ACKM_RX_PKT *ackm_data)
340
1.64k
{
341
1.64k
    const uint8_t *token;
342
1.64k
    size_t token_len;
343
344
1.64k
    if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
345
53
        ossl_quic_channel_raise_protocol_error(ch,
346
53
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
347
53
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
348
53
            "decode error");
349
53
        return 0;
350
53
    }
351
352
1.59k
    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
1.57k
    if (!ossl_quic_set_peer_token(ossl_quic_port_get_channel_ctx(ch->port),
367
1.57k
            &ch->cur_peer_addr, token, token_len))
368
0
        return 0;
369
370
1.57k
    return 1;
371
1.57k
}
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
83.4k
{
383
83.4k
    QUIC_STREAM *stream;
384
83.4k
    uint64_t peer_role, stream_ordinal;
385
83.4k
    uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
386
83.4k
    QUIC_RXFC *max_streams_fc;
387
83.4k
    int is_uni, is_remote_init;
388
389
83.4k
    stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
390
83.4k
    if (stream != NULL) {
391
72.5k
        *result = stream;
392
72.5k
        return 1;
393
72.5k
    }
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
10.9k
    peer_role = ch->is_server
414
10.9k
        ? QUIC_STREAM_INITIATOR_CLIENT
415
10.9k
        : QUIC_STREAM_INITIATOR_SERVER;
416
417
10.9k
    is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
418
10.9k
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
419
420
10.9k
    stream_ordinal = stream_id >> 2;
421
422
10.9k
    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
10.3k
        p_next_ordinal_remote = is_uni
431
10.3k
            ? &ch->next_remote_stream_ordinal_uni
432
10.3k
            : &ch->next_remote_stream_ordinal_bidi;
433
434
        /* Check this isn't violating stream count flow control. */
435
10.3k
        max_streams_fc = is_uni
436
10.3k
            ? &ch->max_streams_uni_rxfc
437
10.3k
            : &ch->max_streams_bidi_rxfc;
438
439
10.3k
        if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
440
10.3k
                stream_ordinal + 1,
441
10.3k
                /*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
10.3k
        if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
450
101
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
451
101
                frame_type,
452
101
                "exceeded maximum allowed streams");
453
101
            return 0;
454
101
        }
455
456
        /*
457
         * Create the named stream and any streams coming before it yet to be
458
         * created.
459
         */
460
118k
        while (*p_next_ordinal_remote <= stream_ordinal) {
461
108k
            uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
462
463
108k
            stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
464
108k
            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
108k
            ++*p_next_ordinal_remote;
473
108k
        }
474
475
10.2k
        *result = stream;
476
10.2k
    } else {
477
        /* Locally-created stream which does not yet exist. */
478
584
        p_next_ordinal_local = is_uni
479
584
            ? &ch->next_local_stream_ordinal_uni
480
584
            : &ch->next_local_stream_ordinal_bidi;
481
482
584
        if (stream_ordinal >= *p_next_ordinal_local) {
483
            /*
484
             * We never created this stream yet, this is a protocol
485
             * violation.
486
             */
487
584
            ossl_quic_channel_raise_protocol_error(ch,
488
584
                OSSL_QUIC_ERR_STREAM_STATE_ERROR,
489
584
                frame_type,
490
584
                "STREAM frame for nonexistent "
491
584
                "stream");
492
584
            return 0;
493
584
        }
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
10.2k
    return 1;
505
10.9k
}
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
32.1k
{
513
32.1k
    OSSL_QUIC_FRAME_STREAM frame_data;
514
32.1k
    QUIC_STREAM *stream;
515
32.1k
    uint64_t fce;
516
32.1k
    size_t rs_avail;
517
32.1k
    int rs_fin = 0;
518
519
32.1k
    *datalen = 0;
520
521
32.1k
    if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
522
348
        ossl_quic_channel_raise_protocol_error(ch,
523
348
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
524
348
            frame_type,
525
348
            "decode error");
526
348
        return 0;
527
348
    }
528
529
31.8k
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
530
31.8k
            frame_type, &stream))
531
266
        return 0; /* protocol error raised by above call */
532
533
31.5k
    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
31.5k
    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
31.5k
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
551
31.5k
            frame_data.offset + frame_data.len,
552
31.5k
            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
31.5k
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
562
31.5k
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
563
240
        ossl_quic_channel_raise_protocol_error(ch,
564
240
            fce,
565
240
            frame_type,
566
240
            "flow control violation");
567
240
        return 0;
568
240
    }
569
570
31.3k
    switch (stream->recv_state) {
571
9.86k
    case QUIC_RSTREAM_STATE_RECV:
572
24.8k
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
573
        /*
574
         * It only makes sense to process incoming STREAM frames in these
575
         * states.
576
         */
577
24.8k
        break;
578
579
6.45k
    case QUIC_RSTREAM_STATE_DATA_RECVD:
580
6.45k
    case QUIC_RSTREAM_STATE_DATA_READ:
581
6.49k
    case QUIC_RSTREAM_STATE_RESET_RECVD:
582
6.49k
    case QUIC_RSTREAM_STATE_RESET_READ:
583
6.49k
    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
6.49k
        return 1;
589
31.3k
    }
590
591
    /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
592
24.8k
    if (frame_data.is_fin
593
20.4k
        && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
594
595
        /* State was already checked above, so can't fail. */
596
6.12k
        ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
597
6.12k
            frame_data.offset
598
6.12k
                + frame_data.len);
599
6.12k
    }
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
24.8k
    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
24.8k
    if ((frame_data.len > 0 || frame_data.is_fin)
620
24.5k
        && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
621
24.5k
            frame_data.offset,
622
24.5k
            frame_data.data,
623
24.5k
            frame_data.len,
624
24.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
24.8k
    if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
639
21.0k
        && !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
24.8k
    if (rs_fin)
648
2.18k
        ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
649
650
24.8k
    *datalen = frame_data.len;
651
652
24.8k
    return 1;
653
24.8k
}
654
655
static void update_streams(QUIC_STREAM *s, void *arg)
656
94.9k
{
657
94.9k
    QUIC_CHANNEL *ch = arg;
658
659
94.9k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
660
94.9k
}
661
662
static void update_streams_bidi(QUIC_STREAM *s, void *arg)
663
25.9k
{
664
25.9k
    QUIC_CHANNEL *ch = arg;
665
666
25.9k
    if (!ossl_quic_stream_is_bidi(s))
667
10.1k
        return;
668
669
15.8k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
670
15.8k
}
671
672
static void update_streams_uni(QUIC_STREAM *s, void *arg)
673
275k
{
674
275k
    QUIC_CHANNEL *ch = arg;
675
676
275k
    if (ossl_quic_stream_is_bidi(s))
677
119k
        return;
678
679
156k
    ossl_quic_stream_map_update_state(&ch->qsm, s);
680
156k
}
681
682
static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
683
    OSSL_ACKM_RX_PKT *ackm_data)
684
38.8k
{
685
38.8k
    uint64_t max_data = 0;
686
687
38.8k
    if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
688
21
        ossl_quic_channel_raise_protocol_error(ch,
689
21
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
690
21
            OSSL_QUIC_FRAME_TYPE_MAX_DATA,
691
21
            "decode error");
692
21
        return 0;
693
21
    }
694
695
38.8k
    ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
696
38.8k
    ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
697
38.8k
    return 1;
698
38.8k
}
699
700
static int depack_do_frame_max_stream_data(PACKET *pkt,
701
    QUIC_CHANNEL *ch,
702
    OSSL_ACKM_RX_PKT *ackm_data)
703
6.69k
{
704
6.69k
    uint64_t stream_id = 0;
705
6.69k
    uint64_t max_stream_data = 0;
706
6.69k
    QUIC_STREAM *stream;
707
708
6.69k
    if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
709
6.69k
            &max_stream_data)) {
710
31
        ossl_quic_channel_raise_protocol_error(ch,
711
31
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
712
31
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
713
31
            "decode error");
714
31
        return 0;
715
31
    }
716
717
6.66k
    if (!depack_do_implicit_stream_create(ch, stream_id,
718
6.66k
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
719
6.66k
            &stream))
720
80
        return 0; /* error already raised for us */
721
722
6.58k
    if (stream == NULL)
723
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
724
725
6.58k
    if (!ossl_quic_stream_has_send(stream)) {
726
24
        ossl_quic_channel_raise_protocol_error(ch,
727
24
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
728
24
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
729
24
            "MAX_STREAM_DATA for TX only "
730
24
            "stream");
731
24
        return 0;
732
24
    }
733
734
6.55k
    ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
735
6.55k
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
736
6.55k
    return 1;
737
6.58k
}
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
38.5k
{
744
38.5k
    uint64_t max_streams = 0;
745
746
38.5k
    if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
747
22
        ossl_quic_channel_raise_protocol_error(ch,
748
22
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
749
22
            frame_type,
750
22
            "decode error");
751
22
        return 0;
752
22
    }
753
754
38.5k
    if (max_streams > (((uint64_t)1) << 60)) {
755
75
        ossl_quic_channel_raise_protocol_error(ch,
756
75
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
757
75
            frame_type,
758
75
            "invalid max streams value");
759
75
        return 0;
760
75
    }
761
762
38.4k
    switch (frame_type) {
763
6.83k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
764
6.83k
        if (max_streams > ch->max_local_streams_bidi)
765
479
            ch->max_local_streams_bidi = max_streams;
766
767
        /* Some streams may now be able to send. */
768
6.83k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
769
6.83k
        break;
770
31.6k
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
771
31.6k
        if (max_streams > ch->max_local_streams_uni)
772
754
            ch->max_local_streams_uni = max_streams;
773
774
        /* Some streams may now be able to send. */
775
31.6k
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
776
31.6k
        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
38.4k
    }
784
785
38.4k
    return 1;
786
38.4k
}
787
788
static int depack_do_frame_data_blocked(PACKET *pkt,
789
    QUIC_CHANNEL *ch,
790
    OSSL_ACKM_RX_PKT *ackm_data)
791
11.0k
{
792
11.0k
    uint64_t max_data = 0;
793
794
11.0k
    if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
795
16
        ossl_quic_channel_raise_protocol_error(ch,
796
16
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
797
16
            OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
798
16
            "decode error");
799
16
        return 0;
800
16
    }
801
802
    /* No-op - informative/debugging frame. */
803
11.0k
    return 1;
804
11.0k
}
805
806
static int depack_do_frame_stream_data_blocked(PACKET *pkt,
807
    QUIC_CHANNEL *ch,
808
    OSSL_ACKM_RX_PKT *ackm_data)
809
4.37k
{
810
4.37k
    uint64_t stream_id = 0;
811
4.37k
    uint64_t max_data = 0;
812
4.37k
    QUIC_STREAM *stream;
813
814
4.37k
    if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
815
4.37k
            &max_data)) {
816
44
        ossl_quic_channel_raise_protocol_error(ch,
817
44
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
818
44
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
819
44
            "decode error");
820
44
        return 0;
821
44
    }
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
4.33k
    if (!depack_do_implicit_stream_create(ch, stream_id,
828
4.33k
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
829
4.33k
            &stream))
830
69
        return 0; /* error already raised for us */
831
832
4.26k
    if (stream == NULL)
833
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
834
835
4.26k
    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
4.26k
    return 1;
851
4.26k
}
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
72.7k
{
858
72.7k
    uint64_t max_data = 0;
859
860
72.7k
    if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
861
28
        ossl_quic_channel_raise_protocol_error(ch,
862
28
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
863
28
            frame_type,
864
28
            "decode error");
865
28
        return 0;
866
28
    }
867
868
72.7k
    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
69
        ossl_quic_channel_raise_protocol_error(ch,
876
69
            OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
877
69
            frame_type,
878
69
            "invalid stream count limit");
879
69
        return 0;
880
69
    }
881
882
    /* No-op - informative/debugging frame. */
883
72.6k
    return 1;
884
72.7k
}
885
886
static int depack_do_frame_new_conn_id(PACKET *pkt,
887
    QUIC_CHANNEL *ch,
888
    OSSL_ACKM_RX_PKT *ackm_data)
889
9.61k
{
890
9.61k
    OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
891
892
9.61k
    if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
893
129
        ossl_quic_channel_raise_protocol_error(ch,
894
129
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
895
129
            OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
896
129
            "decode error");
897
129
        return 0;
898
129
    }
899
900
9.48k
    ossl_quic_channel_on_new_conn_id(ch, &frame_data);
901
902
9.48k
    return 1;
903
9.61k
}
904
905
static int depack_do_frame_retire_conn_id(PACKET *pkt,
906
    QUIC_CHANNEL *ch,
907
    OSSL_ACKM_RX_PKT *ackm_data)
908
77
{
909
77
    uint64_t seq_num;
910
911
77
    if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
912
11
        ossl_quic_channel_raise_protocol_error(ch,
913
11
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
914
11
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
915
11
            "decode error");
916
11
        return 0;
917
11
    }
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
66
    if (!ch->is_server) {
933
66
        ossl_quic_channel_raise_protocol_error(ch,
934
66
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
935
66
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
936
66
            "conn has zero-length CID");
937
66
        return 0;
938
66
    }
939
940
0
    return 1;
941
66
}
942
943
static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
944
8.24k
{
945
8.24k
    QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg;
946
947
8.24k
    assert(ch->path_response_limit > 0);
948
949
8.24k
    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
8.24k
    ch->path_response_tx++;
960
961
8.24k
    OPENSSL_free(buf);
962
8.24k
}
963
964
static int depack_do_frame_path_challenge(PACKET *pkt,
965
    QUIC_CHANNEL *ch,
966
    OSSL_ACKM_RX_PKT *ackm_data)
967
106k
{
968
106k
    uint64_t frame_data = 0;
969
106k
    unsigned char *encoded = NULL;
970
106k
    size_t encoded_len;
971
106k
    WPACKET wpkt;
972
973
106k
    if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
974
35
        ossl_quic_channel_raise_protocol_error(ch,
975
35
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
976
35
            OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
977
35
            "decode error");
978
35
        return 0;
979
35
    }
980
981
106k
    if (ch->seen_path_challenge == 0
982
9.02k
        && 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
8.24k
        encoded_len = sizeof(uint64_t) + 1;
992
8.24k
        if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
993
0
            goto err;
994
995
8.24k
        if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
996
0
            goto err;
997
998
8.24k
        if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
999
0
            WPACKET_cleanup(&wpkt);
1000
0
            goto err;
1001
0
        }
1002
1003
8.24k
        WPACKET_finish(&wpkt);
1004
1005
8.24k
        if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
1006
8.24k
                OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1007
8.24k
                QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
1008
8.24k
                encoded, encoded_len,
1009
8.24k
                free_path_response, ch))
1010
0
            goto err;
1011
8.24k
        ch->seen_path_challenge = 1;
1012
8.24k
        ch->path_response_limit++;
1013
8.24k
    }
1014
1015
106k
    ch->path_challenge_rx++;
1016
1017
106k
    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
106k
}
1026
1027
static int depack_do_frame_path_response(PACKET *pkt,
1028
    QUIC_CHANNEL *ch,
1029
    OSSL_ACKM_RX_PKT *ackm_data)
1030
2.31k
{
1031
2.31k
    uint64_t frame_data = 0;
1032
1033
2.31k
    if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
1034
23
        ossl_quic_channel_raise_protocol_error(ch,
1035
23
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1036
23
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1037
23
            "decode error");
1038
23
        return 0;
1039
23
    }
1040
1041
    /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
1042
1043
2.28k
    return 1;
1044
2.31k
}
1045
1046
static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
1047
    uint64_t frame_type)
1048
1.95k
{
1049
1.95k
    OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
1050
1051
1.95k
    if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
1052
380
        ossl_quic_channel_raise_protocol_error(ch,
1053
380
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1054
380
            frame_type,
1055
380
            "decode error");
1056
380
        return 0;
1057
380
    }
1058
1059
1.57k
    ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
1060
1.57k
    return 1;
1061
1.95k
}
1062
1063
static int depack_do_frame_handshake_done(PACKET *pkt,
1064
    QUIC_CHANNEL *ch,
1065
    OSSL_ACKM_RX_PKT *ackm_data)
1066
165k
{
1067
165k
    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
165k
    ossl_quic_channel_on_handshake_confirmed(ch);
1077
165k
    return 1;
1078
165k
}
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
234k
{
1086
234k
    uint32_t pkt_type = parent_pkt->hdr->type;
1087
234k
    uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
1088
1089
234k
    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
1.44M
    while (PACKET_remaining(pkt) > 0) {
1103
1.21M
        int was_minimal;
1104
1.21M
        uint64_t frame_type;
1105
1.21M
        const unsigned char *sof = NULL;
1106
1.21M
        uint64_t datalen = 0;
1107
1108
1.21M
        if (ch->msg_callback != NULL)
1109
0
            sof = PACKET_data(pkt);
1110
1111
1.21M
        if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
1112
468
            ossl_quic_channel_raise_protocol_error(ch,
1113
468
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1114
468
                0,
1115
468
                "malformed frame header");
1116
468
            return 0;
1117
468
        }
1118
1119
1.21M
        if (!was_minimal) {
1120
84
            ossl_quic_channel_raise_protocol_error(ch,
1121
84
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1122
84
                frame_type,
1123
84
                "non-minimal frame type encoding");
1124
84
            return 0;
1125
84
        }
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.21M
        switch (frame_type) {
1134
204k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1135
264k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1136
305k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1137
306k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1138
306k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1139
306k
            break;
1140
910k
        default:
1141
910k
            ackm_data->is_ack_eliciting = 1;
1142
910k
            break;
1143
1.21M
        }
1144
1145
1.21M
        switch (frame_type) {
1146
309k
        case OSSL_QUIC_FRAME_TYPE_PING:
1147
            /* Allowed in all packet types */
1148
309k
            if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
1149
0
                return 0;
1150
309k
            break;
1151
309k
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1152
            /* Allowed in all packet types */
1153
204k
            if (!depack_do_frame_padding(pkt))
1154
0
                return 0;
1155
204k
            break;
1156
1157
204k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1158
100k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1159
            /* ACK frames are valid everywhere except in 0RTT packets */
1160
100k
            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
100k
            if (!depack_do_frame_ack(pkt, ch, packet_space, received,
1168
100k
                    frame_type, parent_pkt))
1169
2.44k
                return 0;
1170
97.9k
            break;
1171
1172
97.9k
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1173
            /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
1174
3.52k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1175
3.52k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1176
71
                ossl_quic_channel_raise_protocol_error(ch,
1177
71
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1178
71
                    frame_type,
1179
71
                    "RESET_STREAM not valid in "
1180
71
                    "INITIAL/HANDSHAKE");
1181
71
                return 0;
1182
71
            }
1183
3.45k
            if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
1184
226
                return 0;
1185
3.22k
            break;
1186
28.9k
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1187
            /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
1188
28.9k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1189
28.9k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1190
28
                ossl_quic_channel_raise_protocol_error(ch,
1191
28
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1192
28
                    frame_type,
1193
28
                    "STOP_SENDING not valid in "
1194
28
                    "INITIAL/HANDSHAKE");
1195
28
                return 0;
1196
28
            }
1197
28.9k
            if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
1198
183
                return 0;
1199
28.7k
            break;
1200
141k
        case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1201
            /* CRYPTO frames are valid everywhere except in 0RTT packets */
1202
141k
            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
141k
            if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
1210
888
                return 0;
1211
140k
            break;
1212
140k
        case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1213
            /* NEW_TOKEN frames are valid in 1RTT packets */
1214
1.68k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1215
39
                ossl_quic_channel_raise_protocol_error(ch,
1216
39
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1217
39
                    frame_type,
1218
39
                    "NEW_TOKEN valid only in 1-RTT");
1219
39
                return 0;
1220
39
            }
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
1.64k
            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
1.64k
            if (!depack_do_frame_new_token(pkt, ch, ackm_data))
1235
77
                return 0;
1236
1.57k
            break;
1237
1238
1.93k
        case OSSL_QUIC_FRAME_TYPE_STREAM:
1239
7.21k
        case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1240
8.25k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1241
10.3k
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1242
10.5k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1243
18.4k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1244
19.0k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1245
27.5k
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1246
            /* STREAM frames are valid in 0RTT and 1RTT packets */
1247
27.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1248
27.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1249
205
                ossl_quic_channel_raise_protocol_error(ch,
1250
205
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1251
205
                    frame_type,
1252
205
                    "STREAM valid only in 0/1-RTT");
1253
205
                return 0;
1254
205
            }
1255
27.3k
            if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
1256
27.3k
                    frame_type, &datalen))
1257
684
                return 0;
1258
26.6k
            break;
1259
1260
35.5k
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1261
            /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
1262
35.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1263
35.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1264
45
                ossl_quic_channel_raise_protocol_error(ch,
1265
45
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1266
45
                    frame_type,
1267
45
                    "MAX_DATA valid only in 0/1-RTT");
1268
45
                return 0;
1269
45
            }
1270
35.4k
            if (!depack_do_frame_max_data(pkt, ch, ackm_data))
1271
18
                return 0;
1272
35.4k
            break;
1273
35.4k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1274
            /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
1275
5.29k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1276
5.29k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1277
17
                ossl_quic_channel_raise_protocol_error(ch,
1278
17
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1279
17
                    frame_type,
1280
17
                    "MAX_STREAM_DATA valid only in 0/1-RTT");
1281
17
                return 0;
1282
17
            }
1283
5.28k
            if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
1284
108
                return 0;
1285
5.17k
            break;
1286
1287
5.70k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1288
35.3k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1289
            /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
1290
35.3k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1291
35.3k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1292
21
                ossl_quic_channel_raise_protocol_error(ch,
1293
21
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1294
21
                    frame_type,
1295
21
                    "MAX_STREAMS valid only in 0/1-RTT");
1296
21
                return 0;
1297
21
            }
1298
35.3k
            if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
1299
35.3k
                    frame_type))
1300
80
                return 0;
1301
35.2k
            break;
1302
1303
35.2k
        case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
1304
            /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1305
10.5k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1306
10.5k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1307
22
                ossl_quic_channel_raise_protocol_error(ch,
1308
22
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1309
22
                    frame_type,
1310
22
                    "DATA_BLOCKED valid only in 0/1-RTT");
1311
22
                return 0;
1312
22
            }
1313
10.4k
            if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
1314
14
                return 0;
1315
10.4k
            break;
1316
10.4k
        case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
1317
            /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1318
3.88k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1319
3.88k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1320
30
                ossl_quic_channel_raise_protocol_error(ch,
1321
30
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1322
30
                    frame_type,
1323
30
                    "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
1324
30
                return 0;
1325
30
            }
1326
3.85k
            if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
1327
94
                return 0;
1328
3.75k
            break;
1329
1330
3.75k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
1331
58.9k
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
1332
            /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
1333
58.9k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1334
58.9k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1335
37
                ossl_quic_channel_raise_protocol_error(ch,
1336
37
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1337
37
                    frame_type,
1338
37
                    "STREAMS valid only in 0/1-RTT");
1339
37
                return 0;
1340
37
            }
1341
58.9k
            if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
1342
58.9k
                    frame_type))
1343
72
                return 0;
1344
58.8k
            break;
1345
1346
58.8k
        case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1347
            /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
1348
6.73k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1349
6.73k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1350
33
                ossl_quic_channel_raise_protocol_error(ch,
1351
33
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1352
33
                    frame_type,
1353
33
                    "NEW_CONN_ID valid only in 0/1-RTT");
1354
33
                return 0;
1355
33
            }
1356
6.70k
            if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
1357
104
                return 0;
1358
6.60k
            break;
1359
6.60k
        case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1360
            /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
1361
83
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1362
83
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1363
16
                ossl_quic_channel_raise_protocol_error(ch,
1364
16
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1365
16
                    frame_type,
1366
16
                    "RETIRE_CONN_ID valid only in 0/1-RTT");
1367
16
                return 0;
1368
16
            }
1369
67
            if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
1370
67
                return 0;
1371
0
            break;
1372
79.9k
        case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
1373
            /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
1374
79.9k
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1375
79.9k
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1376
16
                ossl_quic_channel_raise_protocol_error(ch,
1377
16
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1378
16
                    frame_type,
1379
16
                    "PATH_CHALLENGE valid only in 0/1-RTT");
1380
16
                return 0;
1381
16
            }
1382
79.9k
            if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
1383
29
                return 0;
1384
1385
79.8k
            break;
1386
79.8k
        case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1387
            /* PATH_RESPONSE frames are valid in 1RTT packets */
1388
1.72k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1389
10
                ossl_quic_channel_raise_protocol_error(ch,
1390
10
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1391
10
                    frame_type,
1392
10
                    "PATH_CHALLENGE valid only in 1-RTT");
1393
10
                return 0;
1394
10
            }
1395
1.71k
            if (!depack_do_frame_path_response(pkt, ch, ackm_data))
1396
19
                return 0;
1397
1.69k
            break;
1398
1399
1.69k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1400
            /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
1401
509
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1402
509
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1403
19
                ossl_quic_channel_raise_protocol_error(ch,
1404
19
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1405
19
                    frame_type,
1406
19
                    "CONN_CLOSE (APP) valid only in 0/1-RTT");
1407
19
                return 0;
1408
19
            }
1409
            /* FALLTHRU */
1410
1.52k
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1411
            /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
1412
1.52k
            if (!depack_do_frame_conn_close(pkt, ch, frame_type))
1413
295
                return 0;
1414
1.23k
            break;
1415
1416
156k
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1417
            /* HANDSHAKE_DONE frames are valid in 1RTT packets */
1418
156k
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1419
12
                ossl_quic_channel_raise_protocol_error(ch,
1420
12
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1421
12
                    frame_type,
1422
12
                    "HANDSHAKE_DONE valid only in 1-RTT");
1423
12
                return 0;
1424
12
            }
1425
156k
            if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
1426
0
                return 0;
1427
156k
            break;
1428
1429
156k
        default:
1430
            /* Unknown frame type */
1431
3.06k
            ossl_quic_channel_raise_protocol_error(ch,
1432
3.06k
                OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1433
3.06k
                frame_type,
1434
3.06k
                "Unknown frame type received");
1435
3.06k
            return 0;
1436
1.21M
        }
1437
1438
1.20M
        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.20M
    }
1455
1456
225k
    return 1;
1457
234k
}
1458
1459
QUIC_NEEDS_LOCK
1460
int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
1461
234k
{
1462
234k
    PACKET pkt;
1463
234k
    OSSL_ACKM_RX_PKT ackm_data;
1464
234k
    uint32_t enc_level;
1465
234k
    size_t dgram_len = qpacket->datagram_len;
1466
1467
234k
    if (ch == NULL)
1468
0
        return 0;
1469
1470
234k
    ossl_ch_reset_rx_state(ch);
1471
1472
    /* Initialize |ackm_data| (and reinitialize |ok|)*/
1473
234k
    memset(&ackm_data, 0, sizeof(ackm_data));
1474
    /*
1475
     * ASSUMPTION: All packets that aren't special case have a
1476
     * packet number.
1477
     */
1478
234k
    ackm_data.pkt_num = qpacket->pn;
1479
234k
    ackm_data.time = qpacket->time;
1480
234k
    enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
1481
234k
    if (enc_level >= QUIC_ENC_LEVEL_NUM)
1482
        /*
1483
         * Retry and Version Negotiation packets should not be passed to this
1484
         * function.
1485
         */
1486
0
        return 0;
1487
1488
234k
    ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
1489
1490
    /*
1491
     * RFC 9000 s. 8.1
1492
     * We can consider the connection to be validated, if we receive a packet
1493
     * from the client protected via handshake keys, meaning that the
1494
     * amplification limit no longer applies (i.e. we can set it as validated.
1495
     * Otherwise, add the size of this packet to the unvalidated credit for
1496
     * the connection.
1497
     */
1498
234k
    if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
1499
26.2k
        ossl_quic_tx_packetiser_set_validated(ch->txp);
1500
208k
    else
1501
208k
        ossl_quic_tx_packetiser_add_unvalidated_credit(ch->txp, dgram_len);
1502
1503
    /* Now that special cases are out of the way, parse frames */
1504
234k
    if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
1505
234k
        || !depack_process_frames(ch, &pkt, qpacket,
1506
234k
            enc_level,
1507
234k
            qpacket->time,
1508
234k
            &ackm_data))
1509
9.64k
        return 0;
1510
1511
225k
    ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
1512
1513
225k
    return 1;
1514
234k
}