Coverage Report

Created: 2025-12-14 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/quic/quic_wire.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/macros.h>
11
#include <openssl/objects.h>
12
#include "internal/quic_ssl.h"
13
#include "internal/quic_vlint.h"
14
#include "internal/quic_wire.h"
15
#include "internal/quic_error.h"
16
17
OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)
18
19
int ossl_quic_frame_ack_contains_pn(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pn)
20
0
{
21
0
    size_t i;
22
23
0
    for (i = 0; i < ack->num_ack_ranges; ++i)
24
0
        if (pn >= ack->ack_ranges[i].start
25
0
            && pn <= ack->ack_ranges[i].end)
26
0
            return 1;
27
28
0
    return 0;
29
0
}
30
31
/*
32
 * QUIC Wire Format Encoding
33
 * =========================
34
 */
35
36
int ossl_quic_wire_encode_padding(WPACKET *pkt, size_t num_bytes)
37
0
{
38
    /*
39
     * PADDING is frame type zero, which as a variable-length integer is
40
     * represented as a single zero byte. As an optimisation, just use memset.
41
     */
42
0
    return WPACKET_memset(pkt, 0, num_bytes);
43
0
}
44
45
static int encode_frame_hdr(WPACKET *pkt, uint64_t frame_type)
46
0
{
47
0
    return WPACKET_quic_write_vlint(pkt, frame_type);
48
0
}
49
50
int ossl_quic_wire_encode_frame_ping(WPACKET *pkt)
51
0
{
52
0
    return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PING);
53
0
}
54
55
int ossl_quic_wire_encode_frame_ack(WPACKET *pkt,
56
    uint32_t ack_delay_exponent,
57
    const OSSL_QUIC_FRAME_ACK *ack)
58
0
{
59
0
    uint64_t frame_type = ack->ecn_present ? OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN
60
0
                                           : OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
61
62
0
    uint64_t largest_ackd, first_ack_range, ack_delay_enc;
63
0
    uint64_t i, num_ack_ranges = ack->num_ack_ranges;
64
0
    OSSL_TIME delay;
65
66
0
    if (num_ack_ranges == 0)
67
0
        return 0;
68
69
0
    delay = ossl_time_divide(ossl_time_divide(ack->delay_time, OSSL_TIME_US),
70
0
        (uint64_t)1 << ack_delay_exponent);
71
0
    ack_delay_enc = ossl_time2ticks(delay);
72
73
0
    largest_ackd = ack->ack_ranges[0].end;
74
0
    first_ack_range = ack->ack_ranges[0].end - ack->ack_ranges[0].start;
75
76
0
    if (!encode_frame_hdr(pkt, frame_type)
77
0
        || !WPACKET_quic_write_vlint(pkt, largest_ackd)
78
0
        || !WPACKET_quic_write_vlint(pkt, ack_delay_enc)
79
0
        || !WPACKET_quic_write_vlint(pkt, num_ack_ranges - 1)
80
0
        || !WPACKET_quic_write_vlint(pkt, first_ack_range))
81
0
        return 0;
82
83
0
    for (i = 1; i < num_ack_ranges; ++i) {
84
0
        uint64_t gap, range_len;
85
86
0
        gap = ack->ack_ranges[i - 1].start - ack->ack_ranges[i].end - 2;
87
0
        range_len = ack->ack_ranges[i].end - ack->ack_ranges[i].start;
88
89
0
        if (!WPACKET_quic_write_vlint(pkt, gap)
90
0
            || !WPACKET_quic_write_vlint(pkt, range_len))
91
0
            return 0;
92
0
    }
93
94
0
    if (ack->ecn_present)
95
0
        if (!WPACKET_quic_write_vlint(pkt, ack->ect0)
96
0
            || !WPACKET_quic_write_vlint(pkt, ack->ect1)
97
0
            || !WPACKET_quic_write_vlint(pkt, ack->ecnce))
98
0
            return 0;
99
100
0
    return 1;
101
0
}
102
103
int ossl_quic_wire_encode_frame_reset_stream(WPACKET *pkt,
104
    const OSSL_QUIC_FRAME_RESET_STREAM *f)
105
0
{
106
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
107
0
        || !WPACKET_quic_write_vlint(pkt, f->stream_id)
108
0
        || !WPACKET_quic_write_vlint(pkt, f->app_error_code)
109
0
        || !WPACKET_quic_write_vlint(pkt, f->final_size))
110
0
        return 0;
111
112
0
    return 1;
113
0
}
114
115
int ossl_quic_wire_encode_frame_stop_sending(WPACKET *pkt,
116
    const OSSL_QUIC_FRAME_STOP_SENDING *f)
117
0
{
118
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
119
0
        || !WPACKET_quic_write_vlint(pkt, f->stream_id)
120
0
        || !WPACKET_quic_write_vlint(pkt, f->app_error_code))
121
0
        return 0;
122
123
0
    return 1;
124
0
}
125
126
int ossl_quic_wire_encode_frame_crypto_hdr(WPACKET *pkt,
127
    const OSSL_QUIC_FRAME_CRYPTO *f)
128
0
{
129
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
130
0
        || !WPACKET_quic_write_vlint(pkt, f->offset)
131
0
        || !WPACKET_quic_write_vlint(pkt, f->len))
132
0
        return 0;
133
134
0
    return 1;
135
0
}
136
137
size_t ossl_quic_wire_get_encoded_frame_len_crypto_hdr(const OSSL_QUIC_FRAME_CRYPTO *f)
138
0
{
139
0
    size_t a, b, c;
140
141
0
    a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_CRYPTO);
142
0
    b = ossl_quic_vlint_encode_len(f->offset);
143
0
    c = ossl_quic_vlint_encode_len(f->len);
144
0
    if (a == 0 || b == 0 || c == 0)
145
0
        return 0;
146
147
0
    return a + b + c;
148
0
}
149
150
void *ossl_quic_wire_encode_frame_crypto(WPACKET *pkt,
151
    const OSSL_QUIC_FRAME_CRYPTO *f)
152
0
{
153
0
    unsigned char *p = NULL;
154
155
0
    if (!ossl_quic_wire_encode_frame_crypto_hdr(pkt, f)
156
0
        || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */
157
0
        || !WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))
158
0
        return NULL;
159
160
0
    if (f->data != NULL)
161
0
        memcpy(p, f->data, (size_t)f->len);
162
163
0
    return p;
164
0
}
165
166
int ossl_quic_wire_encode_frame_new_token(WPACKET *pkt,
167
    const unsigned char *token,
168
    size_t token_len)
169
0
{
170
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
171
0
        || !WPACKET_quic_write_vlint(pkt, token_len)
172
0
        || !WPACKET_memcpy(pkt, token, token_len))
173
0
        return 0;
174
175
0
    return 1;
176
0
}
177
178
int ossl_quic_wire_encode_frame_stream_hdr(WPACKET *pkt,
179
    const OSSL_QUIC_FRAME_STREAM *f)
180
0
{
181
0
    uint64_t frame_type = OSSL_QUIC_FRAME_TYPE_STREAM;
182
183
0
    if (f->offset != 0)
184
0
        frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_OFF;
185
0
    if (f->has_explicit_len)
186
0
        frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_LEN;
187
0
    if (f->is_fin)
188
0
        frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_FIN;
189
190
0
    if (!encode_frame_hdr(pkt, frame_type)
191
0
        || !WPACKET_quic_write_vlint(pkt, f->stream_id))
192
0
        return 0;
193
194
0
    if (f->offset != 0 && !WPACKET_quic_write_vlint(pkt, f->offset))
195
0
        return 0;
196
197
0
    if (f->has_explicit_len && !WPACKET_quic_write_vlint(pkt, f->len))
198
0
        return 0;
199
200
0
    return 1;
201
0
}
202
203
size_t ossl_quic_wire_get_encoded_frame_len_stream_hdr(const OSSL_QUIC_FRAME_STREAM *f)
204
0
{
205
0
    size_t a, b, c, d;
206
207
0
    a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_STREAM);
208
0
    b = ossl_quic_vlint_encode_len(f->stream_id);
209
0
    if (a == 0 || b == 0)
210
0
        return 0;
211
212
0
    if (f->offset > 0) {
213
0
        c = ossl_quic_vlint_encode_len(f->offset);
214
0
        if (c == 0)
215
0
            return 0;
216
0
    } else {
217
0
        c = 0;
218
0
    }
219
220
0
    if (f->has_explicit_len) {
221
0
        d = ossl_quic_vlint_encode_len(f->len);
222
0
        if (d == 0)
223
0
            return 0;
224
0
    } else {
225
0
        d = 0;
226
0
    }
227
228
0
    return a + b + c + d;
229
0
}
230
231
void *ossl_quic_wire_encode_frame_stream(WPACKET *pkt,
232
    const OSSL_QUIC_FRAME_STREAM *f)
233
0
{
234
235
0
    unsigned char *p = NULL;
236
237
0
    if (!ossl_quic_wire_encode_frame_stream_hdr(pkt, f)
238
0
        || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
239
0
        return NULL;
240
241
0
    if (!WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))
242
0
        return NULL;
243
244
0
    if (f->data != NULL)
245
0
        memcpy(p, f->data, (size_t)f->len);
246
247
0
    return p;
248
0
}
249
250
int ossl_quic_wire_encode_frame_max_data(WPACKET *pkt,
251
    uint64_t max_data)
252
0
{
253
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
254
0
        || !WPACKET_quic_write_vlint(pkt, max_data))
255
0
        return 0;
256
257
0
    return 1;
258
0
}
259
260
int ossl_quic_wire_encode_frame_max_stream_data(WPACKET *pkt,
261
    uint64_t stream_id,
262
    uint64_t max_data)
263
0
{
264
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
265
0
        || !WPACKET_quic_write_vlint(pkt, stream_id)
266
0
        || !WPACKET_quic_write_vlint(pkt, max_data))
267
0
        return 0;
268
269
0
    return 1;
270
0
}
271
272
int ossl_quic_wire_encode_frame_max_streams(WPACKET *pkt,
273
    char is_uni,
274
    uint64_t max_streams)
275
0
{
276
0
    if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI : OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
277
0
        || !WPACKET_quic_write_vlint(pkt, max_streams))
278
0
        return 0;
279
280
0
    return 1;
281
0
}
282
283
int ossl_quic_wire_encode_frame_data_blocked(WPACKET *pkt,
284
    uint64_t max_data)
285
0
{
286
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
287
0
        || !WPACKET_quic_write_vlint(pkt, max_data))
288
0
        return 0;
289
290
0
    return 1;
291
0
}
292
293
int ossl_quic_wire_encode_frame_stream_data_blocked(WPACKET *pkt,
294
    uint64_t stream_id,
295
    uint64_t max_stream_data)
296
0
{
297
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
298
0
        || !WPACKET_quic_write_vlint(pkt, stream_id)
299
0
        || !WPACKET_quic_write_vlint(pkt, max_stream_data))
300
0
        return 0;
301
302
0
    return 1;
303
0
}
304
305
int ossl_quic_wire_encode_frame_streams_blocked(WPACKET *pkt,
306
    char is_uni,
307
    uint64_t max_streams)
308
0
{
309
0
    if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI : OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
310
0
        || !WPACKET_quic_write_vlint(pkt, max_streams))
311
0
        return 0;
312
313
0
    return 1;
314
0
}
315
316
int ossl_quic_wire_encode_frame_new_conn_id(WPACKET *pkt,
317
    const OSSL_QUIC_FRAME_NEW_CONN_ID *f)
318
0
{
319
0
    if (f->conn_id.id_len < 1
320
0
        || f->conn_id.id_len > QUIC_MAX_CONN_ID_LEN)
321
0
        return 0;
322
323
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
324
0
        || !WPACKET_quic_write_vlint(pkt, f->seq_num)
325
0
        || !WPACKET_quic_write_vlint(pkt, f->retire_prior_to)
326
0
        || !WPACKET_put_bytes_u8(pkt, f->conn_id.id_len)
327
0
        || !WPACKET_memcpy(pkt, f->conn_id.id, f->conn_id.id_len)
328
0
        || !WPACKET_memcpy(pkt, f->stateless_reset.token,
329
0
            sizeof(f->stateless_reset.token)))
330
0
        return 0;
331
332
0
    return 1;
333
0
}
334
335
int ossl_quic_wire_encode_frame_retire_conn_id(WPACKET *pkt,
336
    uint64_t seq_num)
337
0
{
338
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
339
0
        || !WPACKET_quic_write_vlint(pkt, seq_num))
340
0
        return 0;
341
342
0
    return 1;
343
0
}
344
345
int ossl_quic_wire_encode_frame_path_challenge(WPACKET *pkt,
346
    uint64_t data)
347
0
{
348
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
349
0
        || !WPACKET_put_bytes_u64(pkt, data))
350
0
        return 0;
351
352
0
    return 1;
353
0
}
354
355
int ossl_quic_wire_encode_frame_path_response(WPACKET *pkt,
356
    uint64_t data)
357
0
{
358
0
    if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
359
0
        || !WPACKET_put_bytes_u64(pkt, data))
360
0
        return 0;
361
362
0
    return 1;
363
0
}
364
365
int ossl_quic_wire_encode_frame_conn_close(WPACKET *pkt,
366
    const OSSL_QUIC_FRAME_CONN_CLOSE *f)
367
0
{
368
0
    if (!encode_frame_hdr(pkt, f->is_app ? OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP : OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)
369
0
        || !WPACKET_quic_write_vlint(pkt, f->error_code))
370
0
        return 0;
371
372
    /*
373
     * RFC 9000 s. 19.19: The application-specific variant of CONNECTION_CLOSE
374
     * (type 0x1d) does not include this field.
375
     */
376
0
    if (!f->is_app && !WPACKET_quic_write_vlint(pkt, f->frame_type))
377
0
        return 0;
378
379
0
    if (!WPACKET_quic_write_vlint(pkt, f->reason_len)
380
0
        || !WPACKET_memcpy(pkt, f->reason, f->reason_len))
381
0
        return 0;
382
383
0
    return 1;
384
0
}
385
386
int ossl_quic_wire_encode_frame_handshake_done(WPACKET *pkt)
387
0
{
388
0
    return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
389
0
}
390
391
unsigned char *ossl_quic_wire_encode_transport_param_bytes(WPACKET *pkt,
392
    uint64_t id,
393
    const unsigned char *value,
394
    size_t value_len)
395
0
{
396
0
    unsigned char *b = NULL;
397
398
0
    if (!WPACKET_quic_write_vlint(pkt, id)
399
0
        || !WPACKET_quic_write_vlint(pkt, value_len))
400
0
        return NULL;
401
402
0
    if (value_len == 0)
403
0
        b = WPACKET_get_curr(pkt);
404
0
    else if (!WPACKET_allocate_bytes(pkt, value_len, (unsigned char **)&b))
405
0
        return NULL;
406
407
0
    if (value != NULL)
408
0
        memcpy(b, value, value_len);
409
410
0
    return b;
411
0
}
412
413
int ossl_quic_wire_encode_transport_param_int(WPACKET *pkt,
414
    uint64_t id,
415
    uint64_t value)
416
0
{
417
0
    if (!WPACKET_quic_write_vlint(pkt, id)
418
0
        || !WPACKET_quic_write_vlint(pkt, ossl_quic_vlint_encode_len(value))
419
0
        || !WPACKET_quic_write_vlint(pkt, value))
420
0
        return 0;
421
422
0
    return 1;
423
0
}
424
425
int ossl_quic_wire_encode_transport_param_cid(WPACKET *wpkt,
426
    uint64_t id,
427
    const QUIC_CONN_ID *cid)
428
0
{
429
0
    if (cid->id_len > QUIC_MAX_CONN_ID_LEN)
430
0
        return 0;
431
432
0
    if (ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
433
0
            cid->id,
434
0
            cid->id_len)
435
0
        == NULL)
436
0
        return 0;
437
438
0
    return 1;
439
0
}
440
441
/*
442
 * QUIC Wire Format Decoding
443
 * =========================
444
 */
445
int ossl_quic_wire_peek_frame_header(PACKET *pkt, uint64_t *type,
446
    int *was_minimal)
447
0
{
448
0
    return PACKET_peek_quic_vlint_ex(pkt, type, was_minimal);
449
0
}
450
451
int ossl_quic_wire_skip_frame_header(PACKET *pkt, uint64_t *type)
452
0
{
453
0
    return PACKET_get_quic_vlint(pkt, type);
454
0
}
455
456
static int expect_frame_header_mask(PACKET *pkt,
457
    uint64_t expected_frame_type,
458
    uint64_t mask_bits,
459
    uint64_t *actual_frame_type)
460
0
{
461
0
    uint64_t actual_frame_type_;
462
463
0
    if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type_)
464
0
        || (actual_frame_type_ & ~mask_bits) != expected_frame_type)
465
0
        return 0;
466
467
0
    if (actual_frame_type != NULL)
468
0
        *actual_frame_type = actual_frame_type_;
469
470
0
    return 1;
471
0
}
472
473
static int expect_frame_header(PACKET *pkt, uint64_t expected_frame_type)
474
0
{
475
0
    uint64_t actual_frame_type;
476
477
0
    if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type)
478
0
        || actual_frame_type != expected_frame_type)
479
0
        return 0;
480
481
0
    return 1;
482
0
}
483
484
int ossl_quic_wire_peek_frame_ack_num_ranges(const PACKET *orig_pkt,
485
    uint64_t *total_ranges)
486
0
{
487
0
    PACKET pkt = *orig_pkt;
488
0
    uint64_t ack_range_count, i;
489
490
0
    if (!expect_frame_header_mask(&pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
491
0
            1, NULL)
492
0
        || !PACKET_skip_quic_vlint(&pkt)
493
0
        || !PACKET_skip_quic_vlint(&pkt)
494
0
        || !PACKET_get_quic_vlint(&pkt, &ack_range_count))
495
0
        return 0;
496
497
    /*
498
     * Ensure the specified number of ack ranges listed in the ACK frame header
499
     * actually are available in the frame data. This naturally bounds the
500
     * number of ACK ranges which can be requested by the MDPL, and therefore by
501
     * the MTU. This ensures we do not allocate memory for an excessive number
502
     * of ACK ranges.
503
     */
504
0
    for (i = 0; i < ack_range_count; ++i)
505
0
        if (!PACKET_skip_quic_vlint(&pkt)
506
0
            || !PACKET_skip_quic_vlint(&pkt))
507
0
            return 0;
508
509
    /* (cannot overflow because QUIC vlints can only encode up to 2**62-1) */
510
0
    *total_ranges = ack_range_count + 1;
511
0
    return 1;
512
0
}
513
514
int ossl_quic_wire_decode_frame_ack(PACKET *pkt,
515
    uint32_t ack_delay_exponent,
516
    OSSL_QUIC_FRAME_ACK *ack,
517
    uint64_t *total_ranges)
518
0
{
519
0
    uint64_t frame_type, largest_ackd, ack_delay_raw;
520
0
    uint64_t ack_range_count, first_ack_range, start, end, i;
521
522
    /* This call matches both ACK_WITHOUT_ECN and ACK_WITH_ECN. */
523
0
    if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
524
0
            1, &frame_type)
525
0
        || !PACKET_get_quic_vlint(pkt, &largest_ackd)
526
0
        || !PACKET_get_quic_vlint(pkt, &ack_delay_raw)
527
0
        || !PACKET_get_quic_vlint(pkt, &ack_range_count)
528
0
        || !PACKET_get_quic_vlint(pkt, &first_ack_range))
529
0
        return 0;
530
531
0
    if (first_ack_range > largest_ackd)
532
0
        return 0;
533
534
0
    if (ack_range_count > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
535
0
        return 0;
536
537
0
    start = largest_ackd - first_ack_range;
538
539
0
    if (ack != NULL) {
540
0
        int err = 0;
541
0
        ack->delay_time
542
0
            = ossl_time_multiply(ossl_ticks2time(OSSL_TIME_US),
543
0
                safe_mul_uint64_t(ack_delay_raw,
544
0
                    (uint64_t)1 << ack_delay_exponent,
545
0
                    &err));
546
0
        if (err)
547
0
            ack->delay_time = ossl_time_infinite();
548
549
0
        if (ack->num_ack_ranges > 0) {
550
0
            ack->ack_ranges[0].end = largest_ackd;
551
0
            ack->ack_ranges[0].start = start;
552
0
        }
553
0
    }
554
555
0
    for (i = 0; i < ack_range_count; ++i) {
556
0
        uint64_t gap, len;
557
558
0
        if (!PACKET_get_quic_vlint(pkt, &gap)
559
0
            || !PACKET_get_quic_vlint(pkt, &len))
560
0
            return 0;
561
562
0
        end = start - gap - 2;
563
0
        if (start < gap + 2 || len > end)
564
0
            return 0;
565
566
0
        if (ack != NULL && i + 1 < ack->num_ack_ranges) {
567
0
            ack->ack_ranges[i + 1].start = start = end - len;
568
0
            ack->ack_ranges[i + 1].end = end;
569
0
        }
570
0
    }
571
572
0
    if (ack != NULL && ack_range_count + 1 < ack->num_ack_ranges)
573
0
        ack->num_ack_ranges = (size_t)ack_range_count + 1;
574
575
0
    if (total_ranges != NULL)
576
0
        *total_ranges = ack_range_count + 1;
577
578
0
    if (frame_type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) {
579
0
        uint64_t ect0, ect1, ecnce;
580
581
0
        if (!PACKET_get_quic_vlint(pkt, &ect0)
582
0
            || !PACKET_get_quic_vlint(pkt, &ect1)
583
0
            || !PACKET_get_quic_vlint(pkt, &ecnce))
584
0
            return 0;
585
586
0
        if (ack != NULL) {
587
0
            ack->ect0 = ect0;
588
0
            ack->ect1 = ect1;
589
0
            ack->ecnce = ecnce;
590
0
            ack->ecn_present = 1;
591
0
        }
592
0
    } else if (ack != NULL) {
593
0
        ack->ecn_present = 0;
594
0
    }
595
596
0
    return 1;
597
0
}
598
599
int ossl_quic_wire_decode_frame_reset_stream(PACKET *pkt,
600
    OSSL_QUIC_FRAME_RESET_STREAM *f)
601
0
{
602
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
603
0
        || !PACKET_get_quic_vlint(pkt, &f->stream_id)
604
0
        || !PACKET_get_quic_vlint(pkt, &f->app_error_code)
605
0
        || !PACKET_get_quic_vlint(pkt, &f->final_size))
606
0
        return 0;
607
608
0
    return 1;
609
0
}
610
611
int ossl_quic_wire_decode_frame_stop_sending(PACKET *pkt,
612
    OSSL_QUIC_FRAME_STOP_SENDING *f)
613
0
{
614
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
615
0
        || !PACKET_get_quic_vlint(pkt, &f->stream_id)
616
0
        || !PACKET_get_quic_vlint(pkt, &f->app_error_code))
617
0
        return 0;
618
619
0
    return 1;
620
0
}
621
622
int ossl_quic_wire_decode_frame_crypto(PACKET *pkt,
623
    int nodata,
624
    OSSL_QUIC_FRAME_CRYPTO *f)
625
0
{
626
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
627
0
        || !PACKET_get_quic_vlint(pkt, &f->offset)
628
0
        || !PACKET_get_quic_vlint(pkt, &f->len)
629
0
        || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
630
0
        return 0;
631
632
0
    if (f->offset + f->len > (((uint64_t)1) << 62) - 1)
633
        /* RFC 9000 s. 19.6 */
634
0
        return 0;
635
636
0
    if (nodata) {
637
0
        f->data = NULL;
638
0
    } else {
639
0
        if (PACKET_remaining(pkt) < f->len)
640
0
            return 0;
641
642
0
        f->data = PACKET_data(pkt);
643
644
0
        if (!PACKET_forward(pkt, (size_t)f->len))
645
0
            return 0;
646
0
    }
647
648
0
    return 1;
649
0
}
650
651
int ossl_quic_wire_decode_frame_new_token(PACKET *pkt,
652
    const unsigned char **token,
653
    size_t *token_len)
654
0
{
655
0
    uint64_t token_len_;
656
657
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
658
0
        || !PACKET_get_quic_vlint(pkt, &token_len_))
659
0
        return 0;
660
661
0
    if (token_len_ > SIZE_MAX)
662
0
        return 0;
663
664
0
    *token = PACKET_data(pkt);
665
0
    *token_len = (size_t)token_len_;
666
667
0
    if (!PACKET_forward(pkt, (size_t)token_len_))
668
0
        return 0;
669
670
0
    return 1;
671
0
}
672
673
int ossl_quic_wire_decode_frame_stream(PACKET *pkt,
674
    int nodata,
675
    OSSL_QUIC_FRAME_STREAM *f)
676
0
{
677
0
    uint64_t frame_type;
678
679
    /* This call matches all STREAM values (low 3 bits are masked). */
680
0
    if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAM,
681
0
            OSSL_QUIC_FRAME_FLAG_STREAM_MASK,
682
0
            &frame_type)
683
0
        || !PACKET_get_quic_vlint(pkt, &f->stream_id))
684
0
        return 0;
685
686
0
    if ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_OFF) != 0) {
687
0
        if (!PACKET_get_quic_vlint(pkt, &f->offset))
688
0
            return 0;
689
0
    } else {
690
0
        f->offset = 0;
691
0
    }
692
693
0
    f->has_explicit_len = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_LEN) != 0);
694
0
    f->is_fin = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_FIN) != 0);
695
696
0
    if (f->has_explicit_len) {
697
0
        if (!PACKET_get_quic_vlint(pkt, &f->len))
698
0
            return 0;
699
0
    } else {
700
0
        if (nodata)
701
0
            f->len = 0;
702
0
        else
703
0
            f->len = PACKET_remaining(pkt);
704
0
    }
705
706
    /*
707
     * RFC 9000 s. 19.8: "The largest offset delivered on a stream -- the sum of
708
     * the offset and data length -- cannot exceed 2**62 - 1, as it is not
709
     * possible to provide flow control credit for that data."
710
     */
711
0
    if (f->offset + f->len > (((uint64_t)1) << 62) - 1)
712
0
        return 0;
713
714
0
    if (nodata) {
715
0
        f->data = NULL;
716
0
    } else {
717
0
        f->data = PACKET_data(pkt);
718
719
0
        if (f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */
720
0
            || !PACKET_forward(pkt, (size_t)f->len))
721
0
            return 0;
722
0
    }
723
724
0
    return 1;
725
0
}
726
727
int ossl_quic_wire_decode_frame_max_data(PACKET *pkt,
728
    uint64_t *max_data)
729
0
{
730
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
731
0
        || !PACKET_get_quic_vlint(pkt, max_data))
732
0
        return 0;
733
734
0
    return 1;
735
0
}
736
737
int ossl_quic_wire_decode_frame_max_stream_data(PACKET *pkt,
738
    uint64_t *stream_id,
739
    uint64_t *max_stream_data)
740
0
{
741
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
742
0
        || !PACKET_get_quic_vlint(pkt, stream_id)
743
0
        || !PACKET_get_quic_vlint(pkt, max_stream_data))
744
0
        return 0;
745
746
0
    return 1;
747
0
}
748
749
int ossl_quic_wire_decode_frame_max_streams(PACKET *pkt,
750
    uint64_t *max_streams)
751
0
{
752
    /* This call matches both MAX_STREAMS_BIDI and MAX_STREAMS_UNI. */
753
0
    if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI,
754
0
            1, NULL)
755
0
        || !PACKET_get_quic_vlint(pkt, max_streams))
756
0
        return 0;
757
758
0
    return 1;
759
0
}
760
761
int ossl_quic_wire_decode_frame_data_blocked(PACKET *pkt,
762
    uint64_t *max_data)
763
0
{
764
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
765
0
        || !PACKET_get_quic_vlint(pkt, max_data))
766
0
        return 0;
767
768
0
    return 1;
769
0
}
770
771
int ossl_quic_wire_decode_frame_stream_data_blocked(PACKET *pkt,
772
    uint64_t *stream_id,
773
    uint64_t *max_stream_data)
774
0
{
775
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
776
0
        || !PACKET_get_quic_vlint(pkt, stream_id)
777
0
        || !PACKET_get_quic_vlint(pkt, max_stream_data))
778
0
        return 0;
779
780
0
    return 1;
781
0
}
782
783
int ossl_quic_wire_decode_frame_streams_blocked(PACKET *pkt,
784
    uint64_t *max_streams)
785
0
{
786
    /* This call matches both STREAMS_BLOCKED_BIDI and STREAMS_BLOCKED_UNI. */
787
0
    if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI,
788
0
            1, NULL)
789
0
        || !PACKET_get_quic_vlint(pkt, max_streams))
790
0
        return 0;
791
792
0
    return 1;
793
0
}
794
795
int ossl_quic_wire_decode_frame_new_conn_id(PACKET *pkt,
796
    OSSL_QUIC_FRAME_NEW_CONN_ID *f)
797
0
{
798
0
    unsigned int len;
799
800
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
801
0
        || !PACKET_get_quic_vlint(pkt, &f->seq_num)
802
0
        || !PACKET_get_quic_vlint(pkt, &f->retire_prior_to)
803
0
        || f->seq_num < f->retire_prior_to
804
0
        || !PACKET_get_1(pkt, &len)
805
0
        || len < 1
806
0
        || len > QUIC_MAX_CONN_ID_LEN)
807
0
        return 0;
808
809
0
    f->conn_id.id_len = (unsigned char)len;
810
0
    if (!PACKET_copy_bytes(pkt, f->conn_id.id, len))
811
0
        return 0;
812
813
    /* Clear unused bytes to allow consistent memcmp. */
814
0
    if (len < QUIC_MAX_CONN_ID_LEN)
815
0
        memset(f->conn_id.id + len, 0, QUIC_MAX_CONN_ID_LEN - len);
816
817
0
    if (!PACKET_copy_bytes(pkt, f->stateless_reset.token,
818
0
            sizeof(f->stateless_reset.token)))
819
0
        return 0;
820
821
0
    return 1;
822
0
}
823
824
int ossl_quic_wire_decode_frame_retire_conn_id(PACKET *pkt,
825
    uint64_t *seq_num)
826
0
{
827
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
828
0
        || !PACKET_get_quic_vlint(pkt, seq_num))
829
0
        return 0;
830
831
0
    return 1;
832
0
}
833
834
int ossl_quic_wire_decode_frame_path_challenge(PACKET *pkt,
835
    uint64_t *data)
836
0
{
837
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
838
0
        || !PACKET_get_net_8(pkt, data))
839
0
        return 0;
840
841
0
    return 1;
842
0
}
843
844
int ossl_quic_wire_decode_frame_path_response(PACKET *pkt,
845
    uint64_t *data)
846
0
{
847
0
    if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
848
0
        || !PACKET_get_net_8(pkt, data))
849
0
        return 0;
850
851
0
    return 1;
852
0
}
853
854
int ossl_quic_wire_decode_frame_conn_close(PACKET *pkt,
855
    OSSL_QUIC_FRAME_CONN_CLOSE *f)
856
0
{
857
0
    uint64_t frame_type, reason_len;
858
859
    /* This call matches both CONN_CLOSE_TRANSPORT and CONN_CLOSE_APP. */
860
0
    if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT,
861
0
            1, &frame_type)
862
0
        || !PACKET_get_quic_vlint(pkt, &f->error_code))
863
0
        return 0;
864
865
0
    f->is_app = ((frame_type & 1) != 0);
866
867
0
    if (!f->is_app) {
868
0
        if (!PACKET_get_quic_vlint(pkt, &f->frame_type))
869
0
            return 0;
870
0
    } else {
871
0
        f->frame_type = 0;
872
0
    }
873
874
0
    if (!PACKET_get_quic_vlint(pkt, &reason_len)
875
0
        || reason_len > SIZE_MAX)
876
0
        return 0;
877
878
0
    if (!PACKET_get_bytes(pkt, (const unsigned char **)&f->reason,
879
0
            (size_t)reason_len))
880
0
        return 0;
881
882
0
    f->reason_len = (size_t)reason_len;
883
0
    return 1;
884
0
}
885
886
size_t ossl_quic_wire_decode_padding(PACKET *pkt)
887
0
{
888
0
    const unsigned char *start = PACKET_data(pkt), *end = PACKET_end(pkt),
889
0
                        *p = start;
890
891
0
    while (p < end && *p == 0)
892
0
        ++p;
893
894
0
    if (!PACKET_forward(pkt, p - start))
895
0
        return 0;
896
897
0
    return p - start;
898
0
}
899
900
int ossl_quic_wire_decode_frame_ping(PACKET *pkt)
901
0
{
902
0
    return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PING);
903
0
}
904
905
int ossl_quic_wire_decode_frame_handshake_done(PACKET *pkt)
906
0
{
907
0
    return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
908
0
}
909
910
int ossl_quic_wire_peek_transport_param(PACKET *pkt, uint64_t *id)
911
0
{
912
0
    return PACKET_peek_quic_vlint(pkt, id);
913
0
}
914
915
const unsigned char *ossl_quic_wire_decode_transport_param_bytes(PACKET *pkt,
916
    uint64_t *id,
917
    size_t *len)
918
0
{
919
0
    uint64_t len_;
920
0
    const unsigned char *b = NULL;
921
0
    uint64_t id_;
922
923
0
    if (!PACKET_get_quic_vlint(pkt, &id_)
924
0
        || !PACKET_get_quic_vlint(pkt, &len_))
925
0
        return NULL;
926
927
0
    if (len_ > SIZE_MAX
928
0
        || !PACKET_get_bytes(pkt, (const unsigned char **)&b, (size_t)len_))
929
0
        return NULL;
930
931
0
    *len = (size_t)len_;
932
0
    if (id != NULL)
933
0
        *id = id_;
934
0
    return b;
935
0
}
936
937
int ossl_quic_wire_decode_transport_param_int(PACKET *pkt,
938
    uint64_t *id,
939
    uint64_t *value)
940
0
{
941
0
    PACKET sub;
942
0
    const unsigned char *body;
943
0
    size_t len = 0;
944
945
0
    body = ossl_quic_wire_decode_transport_param_bytes(pkt, id, &len);
946
0
    if (body == NULL)
947
0
        return 0;
948
0
    if (!PACKET_buf_init(&sub, body, len))
949
0
        return 0;
950
0
    if (!PACKET_get_quic_vlint(&sub, value))
951
0
        return 0;
952
953
0
    if (PACKET_remaining(&sub) > 0)
954
0
        return 0;
955
956
0
    return 1;
957
0
}
958
959
int ossl_quic_wire_decode_transport_param_cid(PACKET *pkt,
960
    uint64_t *id,
961
    QUIC_CONN_ID *cid)
962
0
{
963
0
    const unsigned char *body;
964
0
    size_t len = 0;
965
966
0
    body = ossl_quic_wire_decode_transport_param_bytes(pkt, id, &len);
967
0
    if (body == NULL || len > QUIC_MAX_CONN_ID_LEN)
968
0
        return 0;
969
970
0
    cid->id_len = (unsigned char)len;
971
0
    memcpy(cid->id, body, cid->id_len);
972
0
    return 1;
973
0
}
974
975
int ossl_quic_wire_decode_transport_param_preferred_addr(PACKET *pkt,
976
    QUIC_PREFERRED_ADDR *p)
977
0
{
978
0
    const unsigned char *body;
979
0
    uint64_t id;
980
0
    size_t len = 0;
981
0
    PACKET pkt2;
982
0
    unsigned int ipv4_port, ipv6_port, cidl;
983
984
0
    body = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
985
0
    if (body == NULL
986
0
        || len < QUIC_MIN_ENCODED_PREFERRED_ADDR_LEN
987
0
        || len > QUIC_MAX_ENCODED_PREFERRED_ADDR_LEN
988
0
        || id != QUIC_TPARAM_PREFERRED_ADDR)
989
0
        return 0;
990
991
0
    if (!PACKET_buf_init(&pkt2, body, len))
992
0
        return 0;
993
994
0
    if (!PACKET_copy_bytes(&pkt2, p->ipv4, sizeof(p->ipv4))
995
0
        || !PACKET_get_net_2(&pkt2, &ipv4_port)
996
0
        || !PACKET_copy_bytes(&pkt2, p->ipv6, sizeof(p->ipv6))
997
0
        || !PACKET_get_net_2(&pkt2, &ipv6_port)
998
0
        || !PACKET_get_1(&pkt2, &cidl)
999
0
        || cidl > QUIC_MAX_CONN_ID_LEN
1000
0
        || !PACKET_copy_bytes(&pkt2, p->cid.id, cidl)
1001
0
        || !PACKET_copy_bytes(&pkt2, p->stateless_reset.token,
1002
0
            sizeof(p->stateless_reset.token)))
1003
0
        return 0;
1004
1005
0
    p->ipv4_port = (uint16_t)ipv4_port;
1006
0
    p->ipv6_port = (uint16_t)ipv6_port;
1007
0
    p->cid.id_len = (unsigned char)cidl;
1008
0
    return 1;
1009
0
}
1010
1011
const char *
1012
ossl_quic_frame_type_to_string(uint64_t frame_type)
1013
0
{
1014
0
    switch (frame_type) {
1015
0
#define X(name)                       \
1016
0
    case OSSL_QUIC_FRAME_TYPE_##name: \
1017
0
        return #name;
1018
0
        X(PADDING)
1019
0
        X(PING)
1020
0
        X(ACK_WITHOUT_ECN)
1021
0
        X(ACK_WITH_ECN)
1022
0
        X(RESET_STREAM)
1023
0
        X(STOP_SENDING)
1024
0
        X(CRYPTO)
1025
0
        X(NEW_TOKEN)
1026
0
        X(MAX_DATA)
1027
0
        X(MAX_STREAM_DATA)
1028
0
        X(MAX_STREAMS_BIDI)
1029
0
        X(MAX_STREAMS_UNI)
1030
0
        X(DATA_BLOCKED)
1031
0
        X(STREAM_DATA_BLOCKED)
1032
0
        X(STREAMS_BLOCKED_BIDI)
1033
0
        X(STREAMS_BLOCKED_UNI)
1034
0
        X(NEW_CONN_ID)
1035
0
        X(RETIRE_CONN_ID)
1036
0
        X(PATH_CHALLENGE)
1037
0
        X(PATH_RESPONSE)
1038
0
        X(CONN_CLOSE_TRANSPORT)
1039
0
        X(CONN_CLOSE_APP)
1040
0
        X(HANDSHAKE_DONE)
1041
0
        X(STREAM)
1042
0
        X(STREAM_FIN)
1043
0
        X(STREAM_LEN)
1044
0
        X(STREAM_LEN_FIN)
1045
0
        X(STREAM_OFF)
1046
0
        X(STREAM_OFF_FIN)
1047
0
        X(STREAM_OFF_LEN)
1048
0
        X(STREAM_OFF_LEN_FIN)
1049
0
#undef X
1050
0
    default:
1051
0
        return NULL;
1052
0
    }
1053
0
}
1054
1055
const char *ossl_quic_err_to_string(uint64_t error_code)
1056
0
{
1057
0
    switch (error_code) {
1058
0
#define X(name)                \
1059
0
    case OSSL_QUIC_ERR_##name: \
1060
0
        return #name;
1061
0
        X(NO_ERROR)
1062
0
        X(INTERNAL_ERROR)
1063
0
        X(CONNECTION_REFUSED)
1064
0
        X(FLOW_CONTROL_ERROR)
1065
0
        X(STREAM_LIMIT_ERROR)
1066
0
        X(STREAM_STATE_ERROR)
1067
0
        X(FINAL_SIZE_ERROR)
1068
0
        X(FRAME_ENCODING_ERROR)
1069
0
        X(TRANSPORT_PARAMETER_ERROR)
1070
0
        X(CONNECTION_ID_LIMIT_ERROR)
1071
0
        X(PROTOCOL_VIOLATION)
1072
0
        X(INVALID_TOKEN)
1073
0
        X(APPLICATION_ERROR)
1074
0
        X(CRYPTO_BUFFER_EXCEEDED)
1075
0
        X(KEY_UPDATE_ERROR)
1076
0
        X(AEAD_LIMIT_REACHED)
1077
0
        X(NO_VIABLE_PATH)
1078
0
#undef X
1079
0
    default:
1080
        return NULL;
1081
0
    }
1082
0
}