Coverage Report

Created: 2025-08-11 07:04

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