Coverage Report

Created: 2026-07-16 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/quic/quic_rx_depack.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2025 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
0
{
39
    /* We ignore this frame */
40
0
    ossl_quic_wire_decode_padding(pkt);
41
0
    return 1;
42
0
}
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
0
{
48
    /* We ignore this frame, apart from eliciting an ACK */
49
0
    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
0
    ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
58
0
    return 1;
59
0
}
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
0
{
66
0
    OSSL_QUIC_FRAME_ACK ack;
67
0
    OSSL_QUIC_ACK_RANGE *p;
68
0
    uint64_t total_ranges = 0;
69
0
    uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
70
71
0
    if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
72
        /* In case sizeof(uint64_t) > sizeof(size_t) */
73
0
        || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
74
0
        goto malformed;
75
76
0
    if (ch->num_ack_range_scratch < (size_t)total_ranges) {
77
0
        if ((p = OPENSSL_realloc(ch->ack_range_scratch,
78
0
                 sizeof(OSSL_QUIC_ACK_RANGE)
79
0
                     * (size_t)total_ranges))
80
0
            == NULL)
81
0
            goto malformed;
82
83
0
        ch->ack_range_scratch = p;
84
0
        ch->num_ack_range_scratch = (size_t)total_ranges;
85
0
    }
86
87
0
    ack.ack_ranges = ch->ack_range_scratch;
88
0
    ack.num_ack_ranges = (size_t)total_ranges;
89
90
0
    if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
91
0
        goto malformed;
92
93
0
    if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
94
0
        && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
95
0
            || ch->rxku_expected)
96
0
        && 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
0
        ossl_quic_channel_raise_protocol_error(ch,
119
0
            OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
120
0
            frame_type,
121
0
            "acked packet which initiated a "
122
0
            "key update without a "
123
0
            "corresponding key update");
124
0
        return 0;
125
0
    }
126
127
0
    if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
128
0
            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
0
        ossl_quic_channel_raise_protocol_error(ch,
136
0
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
137
0
            frame_type,
138
0
            "ACK for unsent packet number");
139
0
        return 0;
140
0
    }
141
142
0
    ++ch->diag_num_rx_ack;
143
0
    return 1;
144
145
0
malformed:
146
0
    ossl_quic_channel_raise_protocol_error(ch,
147
0
        OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
148
0
        frame_type,
149
0
        "decode error");
150
0
    return 0;
151
0
}
152
153
static int depack_do_frame_reset_stream(PACKET *pkt,
154
    QUIC_CHANNEL *ch,
155
    OSSL_ACKM_RX_PKT *ackm_data)
156
0
{
157
0
    OSSL_QUIC_FRAME_RESET_STREAM frame_data;
158
0
    QUIC_STREAM *stream = NULL;
159
0
    uint64_t fce;
160
161
0
    if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
162
0
        ossl_quic_channel_raise_protocol_error(ch,
163
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
164
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
165
0
            "decode error");
166
0
        return 0;
167
0
    }
168
169
0
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
170
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
171
0
            &stream))
172
0
        return 0; /* error already raised for us */
173
174
0
    if (stream == NULL)
175
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
176
177
0
    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
0
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
197
0
            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
0
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
207
0
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
208
0
        ossl_quic_channel_raise_protocol_error(ch,
209
0
            fce,
210
0
            OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
211
0
            "flow control violation");
212
0
        return 0;
213
0
    }
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
0
    ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
222
0
        frame_data.app_error_code,
223
0
        frame_data.final_size);
224
225
0
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
226
0
    return 1;
227
0
}
228
229
static int depack_do_frame_stop_sending(PACKET *pkt,
230
    QUIC_CHANNEL *ch,
231
    OSSL_ACKM_RX_PKT *ackm_data)
232
0
{
233
0
    OSSL_QUIC_FRAME_STOP_SENDING frame_data;
234
0
    QUIC_STREAM *stream = NULL;
235
236
0
    if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
237
0
        ossl_quic_channel_raise_protocol_error(ch,
238
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
239
0
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
240
0
            "decode error");
241
0
        return 0;
242
0
    }
243
244
0
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
245
0
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
246
0
            &stream))
247
0
        return 0; /* error already raised for us */
248
249
0
    if (stream == NULL)
250
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
251
252
0
    if (!ossl_quic_stream_has_send(stream)) {
253
0
        ossl_quic_channel_raise_protocol_error(ch,
254
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
255
0
            OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
256
0
            "STOP_SENDING frame for "
257
0
            "RX only stream");
258
0
        return 0;
259
0
    }
260
261
0
    stream->peer_stop_sending = 1;
262
0
    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
0
    ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
270
0
        frame_data.app_error_code);
271
0
    return 1;
272
0
}
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
0
{
279
0
    OSSL_QUIC_FRAME_CRYPTO f;
280
0
    QUIC_RSTREAM *rstream;
281
0
    QUIC_RXFC *rxfc;
282
283
0
    *datalen = 0;
284
285
0
    if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
286
0
        ossl_quic_channel_raise_protocol_error(ch,
287
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
288
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
289
0
            "decode error");
290
0
        return 0;
291
0
    }
292
293
0
    if (f.len == 0)
294
0
        return 1; /* nothing to do */
295
296
0
    rstream = ch->crypto_recv[ackm_data->pkt_space];
297
0
    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
0
    rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
306
307
0
    if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
308
0
            /*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
0
    if (ossl_quic_rxfc_get_error(rxfc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
317
0
        ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
318
0
            OSSL_QUIC_FRAME_TYPE_CRYPTO,
319
0
            "exceeded maximum crypto buffer");
320
0
        return 0;
321
0
    }
322
323
0
    if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
324
0
            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
0
    ch->did_crypto_frame = 1;
333
0
    *datalen = f.len;
334
335
0
    return 1;
336
0
}
337
338
static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
339
    OSSL_ACKM_RX_PKT *ackm_data)
340
0
{
341
0
    const uint8_t *token;
342
0
    size_t token_len;
343
344
0
    if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
345
0
        ossl_quic_channel_raise_protocol_error(ch,
346
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
347
0
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
348
0
            "decode error");
349
0
        return 0;
350
0
    }
351
352
0
    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
0
        ossl_quic_channel_raise_protocol_error(ch,
359
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
360
0
            OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
361
0
            "zero-length NEW_TOKEN");
362
0
        return 0;
363
0
    }
364
365
    /* store the new token in our token cache */
366
0
    if (!ossl_quic_set_peer_token(ossl_quic_port_get_channel_ctx(ch->port),
367
0
            &ch->cur_peer_addr, token, token_len))
368
0
        return 0;
369
370
0
    return 1;
371
0
}
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
0
{
383
0
    QUIC_STREAM *stream;
384
0
    uint64_t peer_role, stream_ordinal;
385
0
    uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
386
0
    QUIC_RXFC *max_streams_fc;
387
0
    int is_uni, is_remote_init;
388
389
0
    stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
390
0
    if (stream != NULL) {
391
0
        *result = stream;
392
0
        return 1;
393
0
    }
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
0
    peer_role = ch->is_server
414
0
        ? QUIC_STREAM_INITIATOR_CLIENT
415
0
        : QUIC_STREAM_INITIATOR_SERVER;
416
417
0
    is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
418
0
    is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
419
420
0
    stream_ordinal = stream_id >> 2;
421
422
0
    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
0
        p_next_ordinal_remote = is_uni
431
0
            ? &ch->next_remote_stream_ordinal_uni
432
0
            : &ch->next_remote_stream_ordinal_bidi;
433
434
        /* Check this isn't violating stream count flow control. */
435
0
        max_streams_fc = is_uni
436
0
            ? &ch->max_streams_uni_rxfc
437
0
            : &ch->max_streams_bidi_rxfc;
438
439
0
        if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
440
0
                stream_ordinal + 1,
441
0
                /*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
0
        if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != OSSL_QUIC_ERR_NO_ERROR) {
450
0
            ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
451
0
                frame_type,
452
0
                "exceeded maximum allowed streams");
453
0
            return 0;
454
0
        }
455
456
        /*
457
         * Create the named stream and any streams coming before it yet to be
458
         * created.
459
         */
460
0
        while (*p_next_ordinal_remote <= stream_ordinal) {
461
0
            uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) | (stream_id & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
462
463
0
            stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
464
0
            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
0
            ++*p_next_ordinal_remote;
473
0
        }
474
475
0
        *result = stream;
476
0
    } else {
477
        /* Locally-created stream which does not yet exist. */
478
0
        p_next_ordinal_local = is_uni
479
0
            ? &ch->next_local_stream_ordinal_uni
480
0
            : &ch->next_local_stream_ordinal_bidi;
481
482
0
        if (stream_ordinal >= *p_next_ordinal_local) {
483
            /*
484
             * We never created this stream yet, this is a protocol
485
             * violation.
486
             */
487
0
            ossl_quic_channel_raise_protocol_error(ch,
488
0
                OSSL_QUIC_ERR_STREAM_STATE_ERROR,
489
0
                frame_type,
490
0
                "STREAM frame for nonexistent "
491
0
                "stream");
492
0
            return 0;
493
0
        }
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
0
    return 1;
505
0
}
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
0
{
513
0
    OSSL_QUIC_FRAME_STREAM frame_data;
514
0
    QUIC_STREAM *stream;
515
0
    uint64_t fce;
516
0
    size_t rs_avail;
517
0
    int rs_fin = 0;
518
519
0
    *datalen = 0;
520
521
0
    if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
522
0
        ossl_quic_channel_raise_protocol_error(ch,
523
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
524
0
            frame_type,
525
0
            "decode error");
526
0
        return 0;
527
0
    }
528
529
0
    if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
530
0
            frame_type, &stream))
531
0
        return 0; /* protocol error raised by above call */
532
533
0
    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
0
    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
0
    if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
551
0
            frame_data.offset + frame_data.len,
552
0
            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
0
    fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
562
0
    if (fce != OSSL_QUIC_ERR_NO_ERROR) {
563
0
        ossl_quic_channel_raise_protocol_error(ch,
564
0
            fce,
565
0
            frame_type,
566
0
            "flow control violation");
567
0
        return 0;
568
0
    }
569
570
0
    switch (stream->recv_state) {
571
0
    case QUIC_RSTREAM_STATE_RECV:
572
0
    case QUIC_RSTREAM_STATE_SIZE_KNOWN:
573
        /*
574
         * It only makes sense to process incoming STREAM frames in these
575
         * states.
576
         */
577
0
        break;
578
579
0
    case QUIC_RSTREAM_STATE_DATA_RECVD:
580
0
    case QUIC_RSTREAM_STATE_DATA_READ:
581
0
    case QUIC_RSTREAM_STATE_RESET_RECVD:
582
0
    case QUIC_RSTREAM_STATE_RESET_READ:
583
0
    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
0
        return 1;
589
0
    }
590
591
    /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
592
0
    if (frame_data.is_fin
593
0
        && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
594
595
        /* State was already checked above, so can't fail. */
596
0
        ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
597
0
            frame_data.offset
598
0
                + frame_data.len);
599
0
    }
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
0
    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
0
    if ((frame_data.len > 0 || frame_data.is_fin)
620
0
        && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
621
0
            frame_data.offset,
622
0
            frame_data.data,
623
0
            frame_data.len,
624
0
            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
0
    if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
639
0
        && !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
0
    if (rs_fin)
648
0
        ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
649
650
0
    *datalen = frame_data.len;
651
652
0
    return 1;
653
0
}
654
655
static void update_streams(QUIC_STREAM *s, void *arg)
656
0
{
657
0
    QUIC_CHANNEL *ch = arg;
658
659
0
    ossl_quic_stream_map_update_state(&ch->qsm, s);
660
0
}
661
662
static void update_streams_bidi(QUIC_STREAM *s, void *arg)
663
0
{
664
0
    QUIC_CHANNEL *ch = arg;
665
666
0
    if (!ossl_quic_stream_is_bidi(s))
667
0
        return;
668
669
0
    ossl_quic_stream_map_update_state(&ch->qsm, s);
670
0
}
671
672
static void update_streams_uni(QUIC_STREAM *s, void *arg)
673
0
{
674
0
    QUIC_CHANNEL *ch = arg;
675
676
0
    if (ossl_quic_stream_is_bidi(s))
677
0
        return;
678
679
0
    ossl_quic_stream_map_update_state(&ch->qsm, s);
680
0
}
681
682
static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
683
    OSSL_ACKM_RX_PKT *ackm_data)
684
0
{
685
0
    uint64_t max_data = 0;
686
687
0
    if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
688
0
        ossl_quic_channel_raise_protocol_error(ch,
689
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
690
0
            OSSL_QUIC_FRAME_TYPE_MAX_DATA,
691
0
            "decode error");
692
0
        return 0;
693
0
    }
694
695
0
    ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
696
0
    ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
697
0
    return 1;
698
0
}
699
700
static int depack_do_frame_max_stream_data(PACKET *pkt,
701
    QUIC_CHANNEL *ch,
702
    OSSL_ACKM_RX_PKT *ackm_data)
703
0
{
704
0
    uint64_t stream_id = 0;
705
0
    uint64_t max_stream_data = 0;
706
0
    QUIC_STREAM *stream;
707
708
0
    if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
709
0
            &max_stream_data)) {
710
0
        ossl_quic_channel_raise_protocol_error(ch,
711
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
712
0
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
713
0
            "decode error");
714
0
        return 0;
715
0
    }
716
717
0
    if (!depack_do_implicit_stream_create(ch, stream_id,
718
0
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
719
0
            &stream))
720
0
        return 0; /* error already raised for us */
721
722
0
    if (stream == NULL)
723
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
724
725
0
    if (!ossl_quic_stream_has_send(stream)) {
726
0
        ossl_quic_channel_raise_protocol_error(ch,
727
0
            OSSL_QUIC_ERR_STREAM_STATE_ERROR,
728
0
            OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
729
0
            "MAX_STREAM_DATA for TX only "
730
0
            "stream");
731
0
        return 0;
732
0
    }
733
734
0
    ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
735
0
    ossl_quic_stream_map_update_state(&ch->qsm, stream);
736
0
    return 1;
737
0
}
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
0
{
744
0
    uint64_t max_streams = 0;
745
746
0
    if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
747
0
        ossl_quic_channel_raise_protocol_error(ch,
748
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
749
0
            frame_type,
750
0
            "decode error");
751
0
        return 0;
752
0
    }
753
754
0
    if (max_streams > (((uint64_t)1) << 60)) {
755
0
        ossl_quic_channel_raise_protocol_error(ch,
756
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
757
0
            frame_type,
758
0
            "invalid max streams value");
759
0
        return 0;
760
0
    }
761
762
0
    switch (frame_type) {
763
0
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
764
0
        if (max_streams > ch->max_local_streams_bidi)
765
0
            ch->max_local_streams_bidi = max_streams;
766
767
        /* Some streams may now be able to send. */
768
0
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
769
0
        break;
770
0
    case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
771
0
        if (max_streams > ch->max_local_streams_uni)
772
0
            ch->max_local_streams_uni = max_streams;
773
774
        /* Some streams may now be able to send. */
775
0
        ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
776
0
        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
0
    }
784
785
0
    return 1;
786
0
}
787
788
static int depack_do_frame_data_blocked(PACKET *pkt,
789
    QUIC_CHANNEL *ch,
790
    OSSL_ACKM_RX_PKT *ackm_data)
791
0
{
792
0
    uint64_t max_data = 0;
793
794
0
    if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
795
0
        ossl_quic_channel_raise_protocol_error(ch,
796
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
797
0
            OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
798
0
            "decode error");
799
0
        return 0;
800
0
    }
801
802
    /* No-op - informative/debugging frame. */
803
0
    return 1;
804
0
}
805
806
static int depack_do_frame_stream_data_blocked(PACKET *pkt,
807
    QUIC_CHANNEL *ch,
808
    OSSL_ACKM_RX_PKT *ackm_data)
809
0
{
810
0
    uint64_t stream_id = 0;
811
0
    uint64_t max_data = 0;
812
0
    QUIC_STREAM *stream;
813
814
0
    if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
815
0
            &max_data)) {
816
0
        ossl_quic_channel_raise_protocol_error(ch,
817
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
818
0
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
819
0
            "decode error");
820
0
        return 0;
821
0
    }
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
0
    if (!depack_do_implicit_stream_create(ch, stream_id,
828
0
            OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
829
0
            &stream))
830
0
        return 0; /* error already raised for us */
831
832
0
    if (stream == NULL)
833
0
        return 1; /* old deleted stream, not a protocol violation, ignore */
834
835
0
    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
0
    return 1;
851
0
}
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
0
{
858
0
    uint64_t max_data = 0;
859
860
0
    if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
861
0
        ossl_quic_channel_raise_protocol_error(ch,
862
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
863
0
            frame_type,
864
0
            "decode error");
865
0
        return 0;
866
0
    }
867
868
0
    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
0
        ossl_quic_channel_raise_protocol_error(ch,
876
0
            OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,
877
0
            frame_type,
878
0
            "invalid stream count limit");
879
0
        return 0;
880
0
    }
881
882
    /* No-op - informative/debugging frame. */
883
0
    return 1;
884
0
}
885
886
static int depack_do_frame_new_conn_id(PACKET *pkt,
887
    QUIC_CHANNEL *ch,
888
    OSSL_ACKM_RX_PKT *ackm_data)
889
0
{
890
0
    OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
891
892
0
    if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
893
0
        ossl_quic_channel_raise_protocol_error(ch,
894
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
895
0
            OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
896
0
            "decode error");
897
0
        return 0;
898
0
    }
899
900
0
    ossl_quic_channel_on_new_conn_id(ch, &frame_data);
901
902
0
    return 1;
903
0
}
904
905
static int depack_do_frame_retire_conn_id(PACKET *pkt,
906
    QUIC_CHANNEL *ch,
907
    OSSL_ACKM_RX_PKT *ackm_data)
908
0
{
909
0
    uint64_t seq_num;
910
911
0
    if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
912
0
        ossl_quic_channel_raise_protocol_error(ch,
913
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
914
0
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
915
0
            "decode error");
916
0
        return 0;
917
0
    }
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
0
    if (!ch->is_server) {
933
0
        ossl_quic_channel_raise_protocol_error(ch,
934
0
            OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
935
0
            OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
936
0
            "conn has zero-length CID");
937
0
        return 0;
938
0
    }
939
940
0
    return 1;
941
0
}
942
943
static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
944
0
{
945
0
    QUIC_CHANNEL *ch = (QUIC_CHANNEL *)arg;
946
947
0
    assert(ch->path_response_limit > 0);
948
949
0
    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
0
    ch->path_response_tx++;
960
961
0
    OPENSSL_free(buf);
962
0
}
963
964
static int depack_do_frame_path_challenge(PACKET *pkt,
965
    QUIC_CHANNEL *ch,
966
    OSSL_ACKM_RX_PKT *ackm_data)
967
0
{
968
0
    uint64_t frame_data = 0;
969
0
    unsigned char *encoded = NULL;
970
0
    size_t encoded_len;
971
0
    WPACKET wpkt;
972
973
0
    if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
974
0
        ossl_quic_channel_raise_protocol_error(ch,
975
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
976
0
            OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
977
0
            "decode error");
978
0
        return 0;
979
0
    }
980
981
0
    if (ch->seen_path_challenge == 0
982
0
        && 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
0
        encoded_len = sizeof(uint64_t) + 1;
992
0
        if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
993
0
            goto err;
994
995
0
        if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
996
0
            goto err;
997
998
0
        if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
999
0
            WPACKET_cleanup(&wpkt);
1000
0
            goto err;
1001
0
        }
1002
1003
0
        WPACKET_finish(&wpkt);
1004
1005
0
        if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
1006
0
                OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1007
0
                QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
1008
0
                encoded, encoded_len,
1009
0
                free_path_response, ch))
1010
0
            goto err;
1011
0
        ch->seen_path_challenge = 1;
1012
0
        ch->path_response_limit++;
1013
0
    }
1014
1015
0
    ch->path_challenge_rx++;
1016
1017
0
    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
0
}
1026
1027
static int depack_do_frame_path_response(PACKET *pkt,
1028
    QUIC_CHANNEL *ch,
1029
    OSSL_ACKM_RX_PKT *ackm_data)
1030
0
{
1031
0
    uint64_t frame_data = 0;
1032
1033
0
    if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
1034
0
        ossl_quic_channel_raise_protocol_error(ch,
1035
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1036
0
            OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
1037
0
            "decode error");
1038
0
        return 0;
1039
0
    }
1040
1041
    /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
1042
1043
0
    return 1;
1044
0
}
1045
1046
static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
1047
    uint64_t frame_type)
1048
0
{
1049
0
    OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
1050
1051
0
    if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
1052
0
        ossl_quic_channel_raise_protocol_error(ch,
1053
0
            OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1054
0
            frame_type,
1055
0
            "decode error");
1056
0
        return 0;
1057
0
    }
1058
1059
0
    ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
1060
0
    return 1;
1061
0
}
1062
1063
static int depack_do_frame_handshake_done(PACKET *pkt,
1064
    QUIC_CHANNEL *ch,
1065
    OSSL_ACKM_RX_PKT *ackm_data)
1066
0
{
1067
0
    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
0
    ossl_quic_channel_on_handshake_confirmed(ch);
1077
0
    return 1;
1078
0
}
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
0
{
1086
0
    uint32_t pkt_type = parent_pkt->hdr->type;
1087
0
    uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
1088
1089
0
    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
0
    while (PACKET_remaining(pkt) > 0) {
1103
0
        int was_minimal;
1104
0
        uint64_t frame_type;
1105
0
        const unsigned char *sof = NULL;
1106
0
        uint64_t datalen = 0;
1107
1108
0
        if (ch->msg_callback != NULL)
1109
0
            sof = PACKET_data(pkt);
1110
1111
0
        if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
1112
0
            ossl_quic_channel_raise_protocol_error(ch,
1113
0
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1114
0
                0,
1115
0
                "malformed frame header");
1116
0
            return 0;
1117
0
        }
1118
1119
0
        if (!was_minimal) {
1120
0
            ossl_quic_channel_raise_protocol_error(ch,
1121
0
                OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1122
0
                frame_type,
1123
0
                "non-minimal frame type encoding");
1124
0
            return 0;
1125
0
        }
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
0
        switch (frame_type) {
1134
0
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1135
0
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1136
0
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1137
0
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1138
0
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1139
0
            break;
1140
0
        default:
1141
0
            ackm_data->is_ack_eliciting = 1;
1142
0
            break;
1143
0
        }
1144
1145
0
        switch (frame_type) {
1146
0
        case OSSL_QUIC_FRAME_TYPE_PING:
1147
            /* Allowed in all packet types */
1148
0
            if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
1149
0
                return 0;
1150
0
            break;
1151
0
        case OSSL_QUIC_FRAME_TYPE_PADDING:
1152
            /* Allowed in all packet types */
1153
0
            if (!depack_do_frame_padding(pkt))
1154
0
                return 0;
1155
0
            break;
1156
1157
0
        case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
1158
0
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1159
            /* ACK frames are valid everywhere except in 0RTT packets */
1160
0
            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
0
            if (!depack_do_frame_ack(pkt, ch, packet_space, received,
1168
0
                    frame_type, parent_pkt))
1169
0
                return 0;
1170
0
            break;
1171
1172
0
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1173
            /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
1174
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1175
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1176
0
                ossl_quic_channel_raise_protocol_error(ch,
1177
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1178
0
                    frame_type,
1179
0
                    "RESET_STREAM not valid in "
1180
0
                    "INITIAL/HANDSHAKE");
1181
0
                return 0;
1182
0
            }
1183
0
            if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
1184
0
                return 0;
1185
0
            break;
1186
0
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1187
            /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
1188
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1189
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1190
0
                ossl_quic_channel_raise_protocol_error(ch,
1191
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1192
0
                    frame_type,
1193
0
                    "STOP_SENDING not valid in "
1194
0
                    "INITIAL/HANDSHAKE");
1195
0
                return 0;
1196
0
            }
1197
0
            if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
1198
0
                return 0;
1199
0
            break;
1200
0
        case OSSL_QUIC_FRAME_TYPE_CRYPTO:
1201
            /* CRYPTO frames are valid everywhere except in 0RTT packets */
1202
0
            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
0
            if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
1210
0
                return 0;
1211
0
            break;
1212
0
        case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1213
            /* NEW_TOKEN frames are valid in 1RTT packets */
1214
0
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1215
0
                ossl_quic_channel_raise_protocol_error(ch,
1216
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1217
0
                    frame_type,
1218
0
                    "NEW_TOKEN valid only in 1-RTT");
1219
0
                return 0;
1220
0
            }
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
0
            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
0
            if (!depack_do_frame_new_token(pkt, ch, ackm_data))
1235
0
                return 0;
1236
0
            break;
1237
1238
0
        case OSSL_QUIC_FRAME_TYPE_STREAM:
1239
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
1240
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
1241
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
1242
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
1243
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
1244
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
1245
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
1246
            /* STREAM frames are valid in 0RTT and 1RTT packets */
1247
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1248
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1249
0
                ossl_quic_channel_raise_protocol_error(ch,
1250
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1251
0
                    frame_type,
1252
0
                    "STREAM valid only in 0/1-RTT");
1253
0
                return 0;
1254
0
            }
1255
0
            if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
1256
0
                    frame_type, &datalen))
1257
0
                return 0;
1258
0
            break;
1259
1260
0
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1261
            /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
1262
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1263
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1264
0
                ossl_quic_channel_raise_protocol_error(ch,
1265
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1266
0
                    frame_type,
1267
0
                    "MAX_DATA valid only in 0/1-RTT");
1268
0
                return 0;
1269
0
            }
1270
0
            if (!depack_do_frame_max_data(pkt, ch, ackm_data))
1271
0
                return 0;
1272
0
            break;
1273
0
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1274
            /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
1275
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1276
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1277
0
                ossl_quic_channel_raise_protocol_error(ch,
1278
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1279
0
                    frame_type,
1280
0
                    "MAX_STREAM_DATA valid only in 0/1-RTT");
1281
0
                return 0;
1282
0
            }
1283
0
            if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
1284
0
                return 0;
1285
0
            break;
1286
1287
0
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1288
0
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1289
            /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
1290
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1291
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1292
0
                ossl_quic_channel_raise_protocol_error(ch,
1293
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1294
0
                    frame_type,
1295
0
                    "MAX_STREAMS valid only in 0/1-RTT");
1296
0
                return 0;
1297
0
            }
1298
0
            if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
1299
0
                    frame_type))
1300
0
                return 0;
1301
0
            break;
1302
1303
0
        case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
1304
            /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1305
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1306
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1307
0
                ossl_quic_channel_raise_protocol_error(ch,
1308
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1309
0
                    frame_type,
1310
0
                    "DATA_BLOCKED valid only in 0/1-RTT");
1311
0
                return 0;
1312
0
            }
1313
0
            if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
1314
0
                return 0;
1315
0
            break;
1316
0
        case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
1317
            /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
1318
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1319
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1320
0
                ossl_quic_channel_raise_protocol_error(ch,
1321
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1322
0
                    frame_type,
1323
0
                    "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
1324
0
                return 0;
1325
0
            }
1326
0
            if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
1327
0
                return 0;
1328
0
            break;
1329
1330
0
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
1331
0
        case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
1332
            /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
1333
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1334
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1335
0
                ossl_quic_channel_raise_protocol_error(ch,
1336
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1337
0
                    frame_type,
1338
0
                    "STREAMS valid only in 0/1-RTT");
1339
0
                return 0;
1340
0
            }
1341
0
            if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
1342
0
                    frame_type))
1343
0
                return 0;
1344
0
            break;
1345
1346
0
        case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1347
            /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
1348
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1349
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1350
0
                ossl_quic_channel_raise_protocol_error(ch,
1351
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1352
0
                    frame_type,
1353
0
                    "NEW_CONN_ID valid only in 0/1-RTT");
1354
0
                return 0;
1355
0
            }
1356
0
            if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
1357
0
                return 0;
1358
0
            break;
1359
0
        case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1360
            /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
1361
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1362
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1363
0
                ossl_quic_channel_raise_protocol_error(ch,
1364
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1365
0
                    frame_type,
1366
0
                    "RETIRE_CONN_ID valid only in 0/1-RTT");
1367
0
                return 0;
1368
0
            }
1369
0
            if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
1370
0
                return 0;
1371
0
            break;
1372
0
        case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
1373
            /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
1374
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1375
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1376
0
                ossl_quic_channel_raise_protocol_error(ch,
1377
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1378
0
                    frame_type,
1379
0
                    "PATH_CHALLENGE valid only in 0/1-RTT");
1380
0
                return 0;
1381
0
            }
1382
0
            if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
1383
0
                return 0;
1384
1385
0
            break;
1386
0
        case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1387
            /* PATH_RESPONSE frames are valid in 1RTT packets */
1388
0
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1389
0
                ossl_quic_channel_raise_protocol_error(ch,
1390
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1391
0
                    frame_type,
1392
0
                    "PATH_CHALLENGE valid only in 1-RTT");
1393
0
                return 0;
1394
0
            }
1395
0
            if (!depack_do_frame_path_response(pkt, ch, ackm_data))
1396
0
                return 0;
1397
0
            break;
1398
1399
0
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
1400
            /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
1401
0
            if (pkt_type != QUIC_PKT_TYPE_0RTT
1402
0
                && pkt_type != QUIC_PKT_TYPE_1RTT) {
1403
0
                ossl_quic_channel_raise_protocol_error(ch,
1404
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1405
0
                    frame_type,
1406
0
                    "CONN_CLOSE (APP) valid only in 0/1-RTT");
1407
0
                return 0;
1408
0
            }
1409
            /* FALLTHRU */
1410
0
        case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
1411
            /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
1412
0
            if (!depack_do_frame_conn_close(pkt, ch, frame_type))
1413
0
                return 0;
1414
0
            break;
1415
1416
0
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1417
            /* HANDSHAKE_DONE frames are valid in 1RTT packets */
1418
0
            if (pkt_type != QUIC_PKT_TYPE_1RTT) {
1419
0
                ossl_quic_channel_raise_protocol_error(ch,
1420
0
                    OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1421
0
                    frame_type,
1422
0
                    "HANDSHAKE_DONE valid only in 1-RTT");
1423
0
                return 0;
1424
0
            }
1425
0
            if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
1426
0
                return 0;
1427
0
            break;
1428
1429
0
        default:
1430
            /* Unknown frame type */
1431
0
            ossl_quic_channel_raise_protocol_error(ch,
1432
0
                OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,
1433
0
                frame_type,
1434
0
                "Unknown frame type received");
1435
0
            return 0;
1436
0
        }
1437
1438
0
        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
0
    }
1455
1456
0
    return 1;
1457
0
}
1458
1459
QUIC_NEEDS_LOCK
1460
int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
1461
0
{
1462
0
    PACKET pkt;
1463
0
    OSSL_ACKM_RX_PKT ackm_data;
1464
0
    uint32_t enc_level;
1465
0
    size_t dgram_len = qpacket->datagram_len;
1466
1467
0
    if (ch == NULL)
1468
0
        return 0;
1469
1470
0
    ossl_ch_reset_rx_state(ch);
1471
1472
    /* Initialize |ackm_data| (and reinitialize |ok|)*/
1473
0
    memset(&ackm_data, 0, sizeof(ackm_data));
1474
    /*
1475
     * ASSUMPTION: All packets that aren't special case have a
1476
     * packet number.
1477
     */
1478
0
    ackm_data.pkt_num = qpacket->pn;
1479
0
    ackm_data.time = qpacket->time;
1480
0
    enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
1481
0
    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
0
    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
0
    if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
1499
0
        ossl_quic_tx_packetiser_set_validated(ch->txp);
1500
0
    else
1501
0
        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
0
    if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
1505
0
        || !depack_process_frames(ch, &pkt, qpacket,
1506
0
            enc_level,
1507
0
            qpacket->time,
1508
0
            &ackm_data))
1509
0
        return 0;
1510
1511
0
    ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
1512
1513
0
    return 1;
1514
0
}