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