Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/ssl/quic/quic_wire_pkt.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2023 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/err.h>
11
#include "internal/common.h"
12
#include "internal/quic_wire_pkt.h"
13
14
int ossl_quic_hdr_protector_init(QUIC_HDR_PROTECTOR *hpr,
15
    OSSL_LIB_CTX *libctx,
16
    const char *propq,
17
    uint32_t cipher_id,
18
    const unsigned char *quic_hp_key,
19
    size_t quic_hp_key_len)
20
146k
{
21
146k
    const char *cipher_name = NULL;
22
23
146k
    switch (cipher_id) {
24
86.8k
    case QUIC_HDR_PROT_CIPHER_AES_128:
25
86.8k
        cipher_name = "AES-128-ECB";
26
86.8k
        break;
27
55.6k
    case QUIC_HDR_PROT_CIPHER_AES_256:
28
55.6k
        cipher_name = "AES-256-ECB";
29
55.6k
        break;
30
3.82k
    case QUIC_HDR_PROT_CIPHER_CHACHA:
31
3.82k
        cipher_name = "ChaCha20";
32
3.82k
        break;
33
0
    default:
34
0
        ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
35
0
        return 0;
36
146k
    }
37
38
146k
    hpr->cipher_ctx = EVP_CIPHER_CTX_new();
39
146k
    if (hpr->cipher_ctx == NULL) {
40
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
41
0
        return 0;
42
0
    }
43
44
146k
    hpr->cipher = EVP_CIPHER_fetch(libctx, cipher_name, propq);
45
146k
    if (hpr->cipher == NULL
46
146k
        || quic_hp_key_len != (size_t)EVP_CIPHER_get_key_length(hpr->cipher)) {
47
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
48
0
        goto err;
49
0
    }
50
51
146k
    if (!EVP_CipherInit_ex(hpr->cipher_ctx, hpr->cipher, NULL,
52
146k
            quic_hp_key, NULL, 1)) {
53
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
54
0
        goto err;
55
0
    }
56
57
146k
    hpr->libctx = libctx;
58
146k
    hpr->propq = propq;
59
146k
    hpr->cipher_id = cipher_id;
60
146k
    return 1;
61
62
0
err:
63
0
    ossl_quic_hdr_protector_cleanup(hpr);
64
0
    return 0;
65
146k
}
66
67
void ossl_quic_hdr_protector_cleanup(QUIC_HDR_PROTECTOR *hpr)
68
146k
{
69
146k
    EVP_CIPHER_CTX_free(hpr->cipher_ctx);
70
146k
    hpr->cipher_ctx = NULL;
71
72
146k
    EVP_CIPHER_free(hpr->cipher);
73
146k
    hpr->cipher = NULL;
74
146k
}
75
76
static int hdr_generate_mask(QUIC_HDR_PROTECTOR *hpr,
77
    const unsigned char *sample, size_t sample_len,
78
    unsigned char *mask)
79
1.59M
{
80
1.59M
    int l = 0;
81
1.59M
    unsigned char dst[16];
82
1.59M
    static const unsigned char zeroes[5] = { 0 };
83
1.59M
    size_t i;
84
85
1.59M
    if (hpr->cipher_id == QUIC_HDR_PROT_CIPHER_AES_128
86
1.49M
        || hpr->cipher_id == QUIC_HDR_PROT_CIPHER_AES_256) {
87
1.49M
        if (sample_len < 16) {
88
3.32k
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
89
3.32k
            return 0;
90
3.32k
        }
91
92
1.48M
        if (!EVP_CipherInit_ex(hpr->cipher_ctx, NULL, NULL, NULL, NULL, 1)
93
1.48M
            || !EVP_CipherUpdate(hpr->cipher_ctx, dst, &l, sample, 16)) {
94
0
            ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
95
0
            return 0;
96
0
        }
97
98
8.93M
        for (i = 0; i < 5; ++i)
99
7.44M
            mask[i] = dst[i];
100
1.48M
    } else if (hpr->cipher_id == QUIC_HDR_PROT_CIPHER_CHACHA) {
101
104k
        if (sample_len < 16) {
102
2.27k
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
103
2.27k
            return 0;
104
2.27k
        }
105
106
102k
        if (!EVP_CipherInit_ex(hpr->cipher_ctx, NULL, NULL, NULL, sample, 1)
107
102k
            || !EVP_CipherUpdate(hpr->cipher_ctx, mask, &l,
108
102k
                zeroes, sizeof(zeroes))) {
109
0
            ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
110
0
            return 0;
111
0
        }
112
102k
    } else {
113
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
114
0
        assert(0);
115
0
        return 0;
116
0
    }
117
118
1.59M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
119
    /* No matter what we did above we use the same mask in fuzzing mode */
120
1.59M
    memset(mask, 0, 5);
121
1.59M
#endif
122
123
1.59M
    return 1;
124
1.59M
}
125
126
int ossl_quic_hdr_protector_decrypt(QUIC_HDR_PROTECTOR *hpr,
127
    QUIC_PKT_HDR_PTRS *ptrs)
128
735k
{
129
735k
    return ossl_quic_hdr_protector_decrypt_fields(hpr,
130
735k
        ptrs->raw_sample,
131
735k
        ptrs->raw_sample_len,
132
735k
        ptrs->raw_start,
133
735k
        ptrs->raw_pn);
134
735k
}
135
136
int ossl_quic_hdr_protector_decrypt_fields(QUIC_HDR_PROTECTOR *hpr,
137
    const unsigned char *sample,
138
    size_t sample_len,
139
    unsigned char *first_byte,
140
    unsigned char *pn_bytes)
141
735k
{
142
735k
    unsigned char mask[5], pn_len, i;
143
144
735k
    if (!hdr_generate_mask(hpr, sample, sample_len, mask))
145
5.59k
        return 0;
146
147
730k
    *first_byte ^= mask[0] & ((*first_byte & 0x80) != 0 ? 0xf : 0x1f);
148
730k
    pn_len = (*first_byte & 0x3) + 1;
149
150
3.38M
    for (i = 0; i < pn_len; ++i)
151
2.65M
        pn_bytes[i] ^= mask[i + 1];
152
153
730k
    return 1;
154
735k
}
155
156
int ossl_quic_hdr_protector_encrypt(QUIC_HDR_PROTECTOR *hpr,
157
    QUIC_PKT_HDR_PTRS *ptrs)
158
860k
{
159
860k
    return ossl_quic_hdr_protector_encrypt_fields(hpr,
160
860k
        ptrs->raw_sample,
161
860k
        ptrs->raw_sample_len,
162
860k
        ptrs->raw_start,
163
860k
        ptrs->raw_pn);
164
860k
}
165
166
int ossl_quic_hdr_protector_encrypt_fields(QUIC_HDR_PROTECTOR *hpr,
167
    const unsigned char *sample,
168
    size_t sample_len,
169
    unsigned char *first_byte,
170
    unsigned char *pn_bytes)
171
860k
{
172
860k
    unsigned char mask[5], pn_len, i;
173
174
860k
    if (!hdr_generate_mask(hpr, sample, sample_len, mask))
175
0
        return 0;
176
177
860k
    pn_len = (*first_byte & 0x3) + 1;
178
4.30M
    for (i = 0; i < pn_len; ++i)
179
3.44M
        pn_bytes[i] ^= mask[i + 1];
180
181
860k
    *first_byte ^= mask[0] & ((*first_byte & 0x80) != 0 ? 0xf : 0x1f);
182
860k
    return 1;
183
860k
}
184
185
int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
186
    size_t short_conn_id_len,
187
    int partial,
188
    int nodata,
189
    QUIC_PKT_HDR *hdr,
190
    QUIC_PKT_HDR_PTRS *ptrs)
191
818k
{
192
818k
    unsigned int b0;
193
818k
    unsigned char *pn = NULL;
194
818k
    size_t l = PACKET_remaining(pkt);
195
196
818k
    if (ptrs != NULL) {
197
609k
        ptrs->raw_start = (unsigned char *)PACKET_data(pkt);
198
609k
        ptrs->raw_sample = NULL;
199
609k
        ptrs->raw_sample_len = 0;
200
609k
        ptrs->raw_pn = NULL;
201
609k
    }
202
203
818k
    if (l < QUIC_MIN_VALID_PKT_LEN
204
818k
        || !PACKET_get_1(pkt, &b0))
205
0
        return 0;
206
207
818k
    hdr->partial = partial;
208
818k
    hdr->unused = 0;
209
818k
    hdr->reserved = 0;
210
211
818k
    if ((b0 & 0x80) == 0) {
212
        /* Short header. */
213
39.0k
        if (short_conn_id_len > QUIC_MAX_CONN_ID_LEN)
214
0
            return 0;
215
216
39.0k
        if ((b0 & 0x40) == 0 /* fixed bit not set? */
217
31.0k
            || l < QUIC_MIN_VALID_PKT_LEN_CRYPTO)
218
12.3k
            return 0;
219
220
26.7k
        hdr->type = QUIC_PKT_TYPE_1RTT;
221
26.7k
        hdr->fixed = 1;
222
26.7k
        hdr->spin_bit = (b0 & 0x20) != 0;
223
26.7k
        if (partial) {
224
14.8k
            hdr->key_phase = 0; /* protected, zero for now */
225
14.8k
            hdr->pn_len = 0; /* protected, zero for now */
226
14.8k
            hdr->reserved = 0; /* protected, zero for now */
227
14.8k
        } else {
228
11.8k
            hdr->key_phase = (b0 & 0x04) != 0;
229
11.8k
            hdr->pn_len = (b0 & 0x03) + 1;
230
11.8k
            hdr->reserved = (b0 & 0x18) >> 3;
231
11.8k
        }
232
233
        /* Copy destination connection ID field to header structure. */
234
26.7k
        if (!PACKET_copy_bytes(pkt, hdr->dst_conn_id.id, short_conn_id_len))
235
0
            return 0;
236
237
26.7k
        hdr->dst_conn_id.id_len = (unsigned char)short_conn_id_len;
238
239
        /*
240
         * Skip over the PN. If this is a partial decode, the PN length field
241
         * currently has header protection applied. Thus we do not know the
242
         * length of the PN but we are allowed to assume it is 4 bytes long at
243
         * this stage.
244
         */
245
26.7k
        memset(hdr->pn, 0, sizeof(hdr->pn));
246
26.7k
        pn = (unsigned char *)PACKET_data(pkt);
247
26.7k
        if (partial) {
248
14.8k
            if (!PACKET_forward(pkt, sizeof(hdr->pn)))
249
0
                return 0;
250
14.8k
        } else {
251
11.8k
            if (!PACKET_copy_bytes(pkt, hdr->pn, hdr->pn_len))
252
0
                return 0;
253
11.8k
        }
254
255
        /* Fields not used in short-header packets. */
256
26.7k
        hdr->version = 0;
257
26.7k
        hdr->src_conn_id.id_len = 0;
258
26.7k
        hdr->token = NULL;
259
26.7k
        hdr->token_len = 0;
260
261
        /*
262
         * Short-header packets always come last in a datagram, the length
263
         * is the remainder of the buffer.
264
         */
265
26.7k
        hdr->len = PACKET_remaining(pkt);
266
26.7k
        hdr->data = PACKET_data(pkt);
267
268
        /*
269
         * Skip over payload. Since this is a short header packet, which cannot
270
         * be followed by any other kind of packet, this advances us to the end
271
         * of the datagram.
272
         */
273
26.7k
        if (!PACKET_forward(pkt, hdr->len))
274
0
            return 0;
275
779k
    } else {
276
        /* Long header. */
277
779k
        unsigned long version;
278
779k
        unsigned int dst_conn_id_len, src_conn_id_len, raw_type;
279
280
779k
        if (!PACKET_get_net_4(pkt, &version))
281
0
            return 0;
282
283
        /*
284
         * All QUIC packets must have the fixed bit set, except exceptionally
285
         * for Version Negotiation packets.
286
         */
287
779k
        if (version != 0 && (b0 & 0x40) == 0)
288
564
            return 0;
289
290
778k
        if (!PACKET_get_1(pkt, &dst_conn_id_len)
291
778k
            || dst_conn_id_len > QUIC_MAX_CONN_ID_LEN
292
776k
            || !PACKET_copy_bytes(pkt, hdr->dst_conn_id.id, dst_conn_id_len)
293
772k
            || !PACKET_get_1(pkt, &src_conn_id_len)
294
771k
            || src_conn_id_len > QUIC_MAX_CONN_ID_LEN
295
767k
            || !PACKET_copy_bytes(pkt, hdr->src_conn_id.id, src_conn_id_len))
296
12.1k
            return 0;
297
298
766k
        hdr->version = (uint32_t)version;
299
766k
        hdr->dst_conn_id.id_len = (unsigned char)dst_conn_id_len;
300
766k
        hdr->src_conn_id.id_len = (unsigned char)src_conn_id_len;
301
302
766k
        if (version == 0) {
303
            /*
304
             * Version negotiation packet. Version negotiation packets are
305
             * identified by a version field of 0 and the type bits in the first
306
             * byte are ignored (they may take any value, and we ignore them).
307
             */
308
159k
            hdr->type = QUIC_PKT_TYPE_VERSION_NEG;
309
159k
            hdr->fixed = (b0 & 0x40) != 0;
310
311
159k
            hdr->data = PACKET_data(pkt);
312
159k
            hdr->len = PACKET_remaining(pkt);
313
314
            /*
315
             * Version negotiation packets must contain an array of u32s, so it
316
             * is invalid for their payload length to not be divisible by 4.
317
             */
318
159k
            if ((hdr->len % 4) != 0)
319
1.05k
                return 0;
320
321
            /* Version negotiation packets are always fully decoded. */
322
158k
            hdr->partial = 0;
323
324
            /* Fields not used in version negotiation packets. */
325
158k
            hdr->pn_len = 0;
326
158k
            hdr->spin_bit = 0;
327
158k
            hdr->key_phase = 0;
328
158k
            hdr->token = NULL;
329
158k
            hdr->token_len = 0;
330
158k
            memset(hdr->pn, 0, sizeof(hdr->pn));
331
332
158k
            if (!PACKET_forward(pkt, hdr->len))
333
0
                return 0;
334
606k
        } else if (version != QUIC_VERSION_1) {
335
            /* Unknown version, do not decode. */
336
12.2k
            return 0;
337
594k
        } else {
338
594k
            if (l < QUIC_MIN_VALID_PKT_LEN_CRYPTO)
339
242
                return 0;
340
341
            /* Get long packet type and decode to QUIC_PKT_TYPE_*. */
342
594k
            raw_type = ((b0 >> 4) & 0x3);
343
344
594k
            switch (raw_type) {
345
386k
            case 0:
346
386k
                hdr->type = QUIC_PKT_TYPE_INITIAL;
347
386k
                break;
348
1.07k
            case 1:
349
1.07k
                hdr->type = QUIC_PKT_TYPE_0RTT;
350
1.07k
                break;
351
38.7k
            case 2:
352
38.7k
                hdr->type = QUIC_PKT_TYPE_HANDSHAKE;
353
38.7k
                break;
354
168k
            case 3:
355
168k
                hdr->type = QUIC_PKT_TYPE_RETRY;
356
168k
                break;
357
594k
            }
358
359
594k
            hdr->pn_len = 0;
360
594k
            hdr->fixed = 1;
361
362
            /* Fields not used in long-header packets. */
363
594k
            hdr->spin_bit = 0;
364
594k
            hdr->key_phase = 0;
365
366
594k
            if (hdr->type == QUIC_PKT_TYPE_INITIAL) {
367
                /* Initial packet. */
368
386k
                uint64_t token_len;
369
370
386k
                if (!PACKET_get_quic_vlint(pkt, &token_len)
371
385k
                    || token_len > SIZE_MAX
372
385k
                    || !PACKET_get_bytes(pkt, &hdr->token, (size_t)token_len))
373
900
                    return 0;
374
375
385k
                hdr->token_len = (size_t)token_len;
376
385k
                if (token_len == 0)
377
383k
                    hdr->token = NULL;
378
385k
            } else {
379
208k
                hdr->token = NULL;
380
208k
                hdr->token_len = 0;
381
208k
            }
382
383
593k
            if (hdr->type == QUIC_PKT_TYPE_RETRY) {
384
                /* Retry packet. */
385
168k
                hdr->data = PACKET_data(pkt);
386
168k
                hdr->len = PACKET_remaining(pkt);
387
388
                /* Retry packets are always fully decoded. */
389
168k
                hdr->partial = 0;
390
391
                /* Unused bits in Retry header. */
392
168k
                hdr->unused = b0 & 0x0f;
393
394
                /* Fields not used in Retry packets. */
395
168k
                memset(hdr->pn, 0, sizeof(hdr->pn));
396
397
168k
                if (!PACKET_forward(pkt, hdr->len))
398
0
                    return 0;
399
425k
            } else {
400
                /* Initial, 0-RTT or Handshake packet. */
401
425k
                uint64_t len;
402
403
425k
                hdr->pn_len = partial ? 0 : ((b0 & 0x03) + 1);
404
425k
                hdr->reserved = partial ? 0 : ((b0 & 0x0C) >> 2);
405
406
425k
                if (!PACKET_get_quic_vlint(pkt, &len)
407
425k
                    || len < sizeof(hdr->pn))
408
1.09k
                    return 0;
409
410
424k
                if (!nodata && len > PACKET_remaining(pkt))
411
871
                    return 0;
412
413
                /*
414
                 * Skip over the PN. If this is a partial decode, the PN length
415
                 * field currently has header protection applied. Thus we do not
416
                 * know the length of the PN but we are allowed to assume it is
417
                 * 4 bytes long at this stage.
418
                 */
419
423k
                pn = (unsigned char *)PACKET_data(pkt);
420
423k
                memset(hdr->pn, 0, sizeof(hdr->pn));
421
423k
                if (partial) {
422
223k
                    if (!PACKET_forward(pkt, sizeof(hdr->pn)))
423
0
                        return 0;
424
425
223k
                    hdr->len = (size_t)(len - sizeof(hdr->pn));
426
223k
                } else {
427
200k
                    if (!PACKET_copy_bytes(pkt, hdr->pn, hdr->pn_len))
428
0
                        return 0;
429
430
200k
                    hdr->len = (size_t)(len - hdr->pn_len);
431
200k
                }
432
433
423k
                if (nodata) {
434
0
                    hdr->data = NULL;
435
423k
                } else {
436
423k
                    hdr->data = PACKET_data(pkt);
437
438
                    /* Skip over packet body. */
439
423k
                    if (!PACKET_forward(pkt, hdr->len))
440
0
                        return 0;
441
423k
                }
442
423k
            }
443
593k
        }
444
766k
    }
445
446
776k
    if (ptrs != NULL) {
447
568k
        ptrs->raw_pn = pn;
448
568k
        if (pn != NULL) {
449
241k
            ptrs->raw_sample = pn + 4;
450
241k
            ptrs->raw_sample_len = PACKET_end(pkt) - ptrs->raw_sample;
451
241k
        }
452
568k
    }
453
454
776k
    return 1;
455
818k
}
456
457
int ossl_quic_wire_encode_pkt_hdr(WPACKET *pkt,
458
    size_t short_conn_id_len,
459
    const QUIC_PKT_HDR *hdr,
460
    QUIC_PKT_HDR_PTRS *ptrs)
461
264k
{
462
264k
    unsigned char b0;
463
264k
    size_t off_start, off_sample, off_pn;
464
264k
    unsigned char *start = WPACKET_get_curr(pkt);
465
466
264k
    if (!WPACKET_get_total_written(pkt, &off_start))
467
0
        return 0;
468
469
264k
    if (ptrs != NULL) {
470
        /* ptrs would not be stable on non-static WPACKET */
471
174k
        if (!ossl_assert(pkt->staticbuf != NULL))
472
0
            return 0;
473
174k
        ptrs->raw_start = NULL;
474
174k
        ptrs->raw_sample = NULL;
475
174k
        ptrs->raw_sample_len = 0;
476
174k
        ptrs->raw_pn = 0;
477
174k
    }
478
479
    /* Cannot serialize a partial header, or one whose DCID length is wrong. */
480
264k
    if (hdr->partial
481
264k
        || (hdr->type == QUIC_PKT_TYPE_1RTT
482
55.5k
            && hdr->dst_conn_id.id_len != short_conn_id_len))
483
0
        return 0;
484
485
264k
    if (hdr->type == QUIC_PKT_TYPE_1RTT) {
486
        /* Short header. */
487
488
        /*
489
         * Cannot serialize a header whose DCID length is wrong, or with an
490
         * invalid PN length.
491
         */
492
55.5k
        if (hdr->dst_conn_id.id_len != short_conn_id_len
493
55.5k
            || short_conn_id_len > QUIC_MAX_CONN_ID_LEN
494
55.5k
            || hdr->pn_len < 1 || hdr->pn_len > 4)
495
0
            return 0;
496
497
55.5k
        b0 = (hdr->spin_bit << 5)
498
55.5k
            | (hdr->key_phase << 2)
499
55.5k
            | (hdr->pn_len - 1)
500
55.5k
            | (hdr->reserved << 3)
501
55.5k
            | 0x40; /* fixed bit */
502
503
55.5k
        if (!WPACKET_put_bytes_u8(pkt, b0)
504
55.5k
            || !WPACKET_memcpy(pkt, hdr->dst_conn_id.id, short_conn_id_len)
505
55.5k
            || !WPACKET_get_total_written(pkt, &off_pn)
506
55.5k
            || !WPACKET_memcpy(pkt, hdr->pn, hdr->pn_len))
507
0
            return 0;
508
208k
    } else {
509
        /* Long header. */
510
208k
        unsigned int raw_type;
511
512
208k
        if (hdr->dst_conn_id.id_len > QUIC_MAX_CONN_ID_LEN
513
208k
            || hdr->src_conn_id.id_len > QUIC_MAX_CONN_ID_LEN)
514
0
            return 0;
515
516
208k
        if (ossl_quic_pkt_type_has_pn(hdr->type)
517
119k
            && (hdr->pn_len < 1 || hdr->pn_len > 4))
518
0
            return 0;
519
520
208k
        switch (hdr->type) {
521
0
        case QUIC_PKT_TYPE_VERSION_NEG:
522
0
            if (hdr->version != 0)
523
0
                return 0;
524
525
            /* Version negotiation packets use zero for the type bits */
526
0
            raw_type = 0;
527
0
            break;
528
529
58.3k
        case QUIC_PKT_TYPE_INITIAL:
530
58.3k
            raw_type = 0;
531
58.3k
            break;
532
0
        case QUIC_PKT_TYPE_0RTT:
533
0
            raw_type = 1;
534
0
            break;
535
61.0k
        case QUIC_PKT_TYPE_HANDSHAKE:
536
61.0k
            raw_type = 2;
537
61.0k
            break;
538
89.5k
        case QUIC_PKT_TYPE_RETRY:
539
89.5k
            raw_type = 3;
540
89.5k
            break;
541
0
        default:
542
0
            return 0;
543
208k
        }
544
545
208k
        b0 = (raw_type << 4) | 0x80; /* long */
546
208k
        if (hdr->type != QUIC_PKT_TYPE_VERSION_NEG || hdr->fixed)
547
208k
            b0 |= 0x40; /* fixed */
548
208k
        if (ossl_quic_pkt_type_has_pn(hdr->type)) {
549
119k
            b0 |= hdr->pn_len - 1;
550
119k
            b0 |= (hdr->reserved << 2);
551
119k
        }
552
208k
        if (hdr->type == QUIC_PKT_TYPE_RETRY)
553
89.5k
            b0 |= hdr->unused;
554
555
208k
        if (!WPACKET_put_bytes_u8(pkt, b0)
556
208k
            || !WPACKET_put_bytes_u32(pkt, hdr->version)
557
208k
            || !WPACKET_put_bytes_u8(pkt, hdr->dst_conn_id.id_len)
558
208k
            || !WPACKET_memcpy(pkt, hdr->dst_conn_id.id,
559
208k
                hdr->dst_conn_id.id_len)
560
208k
            || !WPACKET_put_bytes_u8(pkt, hdr->src_conn_id.id_len)
561
208k
            || !WPACKET_memcpy(pkt, hdr->src_conn_id.id,
562
208k
                hdr->src_conn_id.id_len))
563
0
            return 0;
564
565
208k
        if (hdr->type == QUIC_PKT_TYPE_VERSION_NEG
566
208k
            || hdr->type == QUIC_PKT_TYPE_RETRY) {
567
89.5k
            if (hdr->len > 0 && !WPACKET_reserve_bytes(pkt, hdr->len, NULL))
568
0
                return 0;
569
570
89.5k
            return 1;
571
89.5k
        }
572
573
119k
        if (hdr->type == QUIC_PKT_TYPE_INITIAL) {
574
58.3k
            if (!WPACKET_quic_write_vlint(pkt, hdr->token_len)
575
58.3k
                || !WPACKET_memcpy(pkt, hdr->token, hdr->token_len))
576
0
                return 0;
577
58.3k
        }
578
579
119k
        if (!WPACKET_quic_write_vlint(pkt, hdr->len + hdr->pn_len)
580
119k
            || !WPACKET_get_total_written(pkt, &off_pn)
581
119k
            || !WPACKET_memcpy(pkt, hdr->pn, hdr->pn_len))
582
0
            return 0;
583
119k
    }
584
585
174k
    if (hdr->len > 0 && !WPACKET_reserve_bytes(pkt, hdr->len, NULL))
586
0
        return 0;
587
588
174k
    off_sample = off_pn + 4;
589
590
174k
    if (ptrs != NULL) {
591
174k
        ptrs->raw_start = start;
592
174k
        ptrs->raw_sample = start + (off_sample - off_start);
593
174k
        ptrs->raw_sample_len
594
174k
            = WPACKET_get_curr(pkt) + hdr->len - ptrs->raw_sample;
595
174k
        ptrs->raw_pn = start + (off_pn - off_start);
596
174k
    }
597
598
174k
    return 1;
599
174k
}
600
601
int ossl_quic_wire_get_encoded_pkt_hdr_len(size_t short_conn_id_len,
602
    const QUIC_PKT_HDR *hdr)
603
37.5M
{
604
37.5M
    size_t len = 0, enclen;
605
606
    /* Cannot serialize a partial header, or one whose DCID length is wrong. */
607
37.5M
    if (hdr->partial
608
37.5M
        || (hdr->type == QUIC_PKT_TYPE_1RTT
609
10.0M
            && hdr->dst_conn_id.id_len != short_conn_id_len))
610
0
        return 0;
611
612
37.5M
    if (hdr->type == QUIC_PKT_TYPE_1RTT) {
613
        /* Short header. */
614
615
        /*
616
         * Cannot serialize a header whose DCID length is wrong, or with an
617
         * invalid PN length.
618
         */
619
10.0M
        if (hdr->dst_conn_id.id_len != short_conn_id_len
620
10.0M
            || short_conn_id_len > QUIC_MAX_CONN_ID_LEN
621
10.0M
            || hdr->pn_len < 1 || hdr->pn_len > 4)
622
0
            return 0;
623
624
10.0M
        return 1 + short_conn_id_len + hdr->pn_len;
625
27.5M
    } else {
626
        /* Long header. */
627
27.5M
        if (hdr->dst_conn_id.id_len > QUIC_MAX_CONN_ID_LEN
628
27.5M
            || hdr->src_conn_id.id_len > QUIC_MAX_CONN_ID_LEN)
629
0
            return 0;
630
631
27.5M
        len += 1 /* Initial byte */ + 4 /* Version */
632
27.5M
            + 1 + hdr->dst_conn_id.id_len /* DCID Len, DCID */
633
27.5M
            + 1 + hdr->src_conn_id.id_len /* SCID Len, SCID */
634
27.5M
            ;
635
636
27.5M
        if (ossl_quic_pkt_type_has_pn(hdr->type)) {
637
27.5M
            if (hdr->pn_len < 1 || hdr->pn_len > 4)
638
0
                return 0;
639
640
27.5M
            len += hdr->pn_len;
641
27.5M
        }
642
643
27.5M
        if (hdr->type == QUIC_PKT_TYPE_INITIAL) {
644
24.1M
            enclen = ossl_quic_vlint_encode_len(hdr->token_len);
645
24.1M
            if (!enclen)
646
0
                return 0;
647
648
24.1M
            len += enclen + hdr->token_len;
649
24.1M
        }
650
651
27.5M
        if (!ossl_quic_pkt_type_must_be_last(hdr->type)) {
652
27.5M
            enclen = ossl_quic_vlint_encode_len(hdr->len + hdr->pn_len);
653
27.5M
            if (!enclen)
654
0
                return 0;
655
656
27.5M
            len += enclen;
657
27.5M
        }
658
659
27.5M
        return len;
660
27.5M
    }
661
37.5M
}
662
663
int ossl_quic_wire_get_pkt_hdr_dst_conn_id(const unsigned char *buf,
664
    size_t buf_len,
665
    size_t short_conn_id_len,
666
    QUIC_CONN_ID *dst_conn_id)
667
7.43M
{
668
7.43M
    unsigned char b0;
669
7.43M
    size_t blen;
670
671
7.43M
    if (buf_len < QUIC_MIN_VALID_PKT_LEN
672
3.08M
        || short_conn_id_len > QUIC_MAX_CONN_ID_LEN)
673
4.34M
        return 0;
674
675
3.08M
    b0 = buf[0];
676
3.08M
    if ((b0 & 0x80) != 0) {
677
        /*
678
         * Long header. We need 6 bytes (initial byte, 4 version bytes, DCID
679
         * length byte to begin with). This is covered by the buf_len test
680
         * above.
681
         */
682
683
        /*
684
         * If the version field is non-zero (meaning that this is not a Version
685
         * Negotiation packet), the fixed bit must be set.
686
         */
687
2.77M
        if ((buf[1] || buf[2] || buf[3] || buf[4]) && (b0 & 0x40) == 0)
688
7.30k
            return 0;
689
690
2.76M
        blen = (size_t)buf[5]; /* DCID Length */
691
2.76M
        if (blen > QUIC_MAX_CONN_ID_LEN
692
2.44M
            || buf_len < QUIC_MIN_VALID_PKT_LEN + blen)
693
318k
            return 0;
694
695
2.44M
        dst_conn_id->id_len = (unsigned char)blen;
696
2.44M
        memcpy(dst_conn_id->id, buf + 6, blen);
697
2.44M
        return 1;
698
2.76M
    } else {
699
        /* Short header. */
700
313k
        if ((b0 & 0x40) == 0)
701
            /* Fixed bit not set, not a valid QUIC packet header. */
702
244k
            return 0;
703
704
68.6k
        if (buf_len < QUIC_MIN_VALID_PKT_LEN_CRYPTO + short_conn_id_len)
705
788
            return 0;
706
707
67.8k
        dst_conn_id->id_len = (unsigned char)short_conn_id_len;
708
67.8k
        memcpy(dst_conn_id->id, buf + 1, short_conn_id_len);
709
67.8k
        return 1;
710
68.6k
    }
711
3.08M
}
712
713
int ossl_quic_wire_decode_pkt_hdr_pn(const unsigned char *enc_pn,
714
    size_t enc_pn_len,
715
    QUIC_PN largest_pn,
716
    QUIC_PN *res_pn)
717
730k
{
718
730k
    int64_t expected_pn, truncated_pn, candidate_pn, pn_win, pn_hwin, pn_mask;
719
720
730k
    switch (enc_pn_len) {
721
28.1k
    case 1:
722
28.1k
        truncated_pn = enc_pn[0];
723
28.1k
        break;
724
2.96k
    case 2:
725
2.96k
        truncated_pn = ((QUIC_PN)enc_pn[0] << 8)
726
2.96k
            | (QUIC_PN)enc_pn[1];
727
2.96k
        break;
728
174k
    case 3:
729
174k
        truncated_pn = ((QUIC_PN)enc_pn[0] << 16)
730
174k
            | ((QUIC_PN)enc_pn[1] << 8)
731
174k
            | (QUIC_PN)enc_pn[2];
732
174k
        break;
733
524k
    case 4:
734
524k
        truncated_pn = ((QUIC_PN)enc_pn[0] << 24)
735
524k
            | ((QUIC_PN)enc_pn[1] << 16)
736
524k
            | ((QUIC_PN)enc_pn[2] << 8)
737
524k
            | (QUIC_PN)enc_pn[3];
738
524k
        break;
739
0
    default:
740
0
        return 0;
741
730k
    }
742
743
    /* Implemented as per RFC 9000 Section A.3. */
744
730k
    expected_pn = largest_pn + 1;
745
730k
    pn_win = ((int64_t)1) << (enc_pn_len * 8);
746
730k
    pn_hwin = pn_win / 2;
747
730k
    pn_mask = pn_win - 1;
748
730k
    candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
749
730k
    if (candidate_pn <= expected_pn - pn_hwin
750
98.1k
        && candidate_pn < (((int64_t)1) << 62) - pn_win)
751
98.1k
        *res_pn = candidate_pn + pn_win;
752
631k
    else if (candidate_pn > expected_pn + pn_hwin
753
38.1k
        && candidate_pn >= pn_win)
754
30.2k
        *res_pn = candidate_pn - pn_win;
755
601k
    else
756
601k
        *res_pn = candidate_pn;
757
730k
    return 1;
758
730k
}
759
760
/* From RFC 9000 Section A.2. Simplified implementation. */
761
int ossl_quic_wire_determine_pn_len(QUIC_PN pn,
762
    QUIC_PN largest_acked)
763
0
{
764
0
    uint64_t num_unacked
765
0
        = (largest_acked == QUIC_PN_INVALID) ? pn + 1 : pn - largest_acked;
766
767
    /*
768
     * num_unacked \in [    0, 2** 7] -> 1 byte
769
     * num_unacked \in (2** 7, 2**15] -> 2 bytes
770
     * num_unacked \in (2**15, 2**23] -> 3 bytes
771
     * num_unacked \in (2**23,      ] -> 4 bytes
772
     */
773
774
0
    if (num_unacked <= (1U << 7))
775
0
        return 1;
776
0
    if (num_unacked <= (1U << 15))
777
0
        return 2;
778
0
    if (num_unacked <= (1U << 23))
779
0
        return 3;
780
0
    return 4;
781
0
}
782
783
int ossl_quic_wire_encode_pkt_hdr_pn(QUIC_PN pn,
784
    unsigned char *enc_pn,
785
    size_t enc_pn_len)
786
860k
{
787
860k
    switch (enc_pn_len) {
788
0
    case 1:
789
0
        enc_pn[0] = (unsigned char)pn;
790
0
        break;
791
0
    case 2:
792
0
        enc_pn[1] = (unsigned char)pn;
793
0
        enc_pn[0] = (unsigned char)(pn >> 8);
794
0
        break;
795
0
    case 3:
796
0
        enc_pn[2] = (unsigned char)pn;
797
0
        enc_pn[1] = (unsigned char)(pn >> 8);
798
0
        enc_pn[0] = (unsigned char)(pn >> 16);
799
0
        break;
800
860k
    case 4:
801
860k
        enc_pn[3] = (unsigned char)pn;
802
860k
        enc_pn[2] = (unsigned char)(pn >> 8);
803
860k
        enc_pn[1] = (unsigned char)(pn >> 16);
804
860k
        enc_pn[0] = (unsigned char)(pn >> 24);
805
860k
        break;
806
0
    default:
807
0
        return 0;
808
860k
    }
809
810
860k
    return 1;
811
860k
}
812
813
int ossl_quic_validate_retry_integrity_tag(OSSL_LIB_CTX *libctx,
814
    const char *propq,
815
    const QUIC_PKT_HDR *hdr,
816
    const QUIC_CONN_ID *client_initial_dcid)
817
502k
{
818
502k
    unsigned char expected_tag[QUIC_RETRY_INTEGRITY_TAG_LEN];
819
502k
    const unsigned char *actual_tag;
820
821
502k
    if (hdr == NULL || hdr->len < QUIC_RETRY_INTEGRITY_TAG_LEN)
822
0
        return 0;
823
824
502k
    if (!ossl_quic_calculate_retry_integrity_tag(libctx, propq,
825
502k
            hdr, client_initial_dcid,
826
502k
            expected_tag))
827
0
        return 0;
828
829
502k
    actual_tag = hdr->data + hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN;
830
831
502k
    return !CRYPTO_memcmp(expected_tag, actual_tag,
832
502k
        QUIC_RETRY_INTEGRITY_TAG_LEN);
833
502k
}
834
835
/* RFC 9001 s. 5.8 */
836
static const unsigned char retry_integrity_key[] = {
837
    0xbe, 0x0c, 0x69, 0x0b, 0x9f, 0x66, 0x57, 0x5a,
838
    0x1d, 0x76, 0x6b, 0x54, 0xe3, 0x68, 0xc8, 0x4e
839
};
840
841
static const unsigned char retry_integrity_nonce[] = {
842
    0x46, 0x15, 0x99, 0xd3, 0x5d, 0x63, 0x2b, 0xf2,
843
    0x23, 0x98, 0x25, 0xbb
844
};
845
846
int ossl_quic_calculate_retry_integrity_tag(OSSL_LIB_CTX *libctx,
847
    const char *propq,
848
    const QUIC_PKT_HDR *hdr,
849
    const QUIC_CONN_ID *client_initial_dcid,
850
    unsigned char *tag)
851
502k
{
852
502k
    EVP_CIPHER *cipher = NULL;
853
502k
    EVP_CIPHER_CTX *cctx = NULL;
854
502k
    int ok = 0, l = 0, l2 = 0, wpkt_valid = 0;
855
502k
    WPACKET wpkt;
856
    /* Worst case length of the Retry Psuedo-Packet header is 68 bytes. */
857
502k
    unsigned char buf[128];
858
502k
    QUIC_PKT_HDR hdr2;
859
502k
    size_t hdr_enc_len = 0;
860
861
502k
    if (hdr->type != QUIC_PKT_TYPE_RETRY || hdr->version == 0
862
502k
        || hdr->len < QUIC_RETRY_INTEGRITY_TAG_LEN
863
502k
        || hdr->data == NULL
864
502k
        || client_initial_dcid == NULL || tag == NULL
865
502k
        || client_initial_dcid->id_len > QUIC_MAX_CONN_ID_LEN) {
866
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
867
0
        goto err;
868
0
    }
869
870
    /*
871
     * Do not reserve packet body in WPACKET. Retry packet header
872
     * does not contain a Length field so this does not affect
873
     * the serialized packet header.
874
     */
875
502k
    hdr2 = *hdr;
876
502k
    hdr2.len = 0;
877
878
    /* Assemble retry psuedo-packet. */
879
502k
    if (!WPACKET_init_static_len(&wpkt, buf, sizeof(buf), 0)) {
880
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
881
0
        goto err;
882
0
    }
883
884
502k
    wpkt_valid = 1;
885
886
    /* Prepend original DCID to the packet. */
887
502k
    if (!WPACKET_put_bytes_u8(&wpkt, client_initial_dcid->id_len)
888
502k
        || !WPACKET_memcpy(&wpkt, client_initial_dcid->id,
889
502k
            client_initial_dcid->id_len)) {
890
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
891
0
        goto err;
892
0
    }
893
894
    /* Encode main retry header. */
895
502k
    if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, hdr2.dst_conn_id.id_len,
896
502k
            &hdr2, NULL))
897
0
        goto err;
898
899
502k
    if (!WPACKET_get_total_written(&wpkt, &hdr_enc_len)) {
900
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
901
0
        goto err;
902
0
    }
903
904
    /* Create and initialise cipher context. */
905
    /* TODO(QUIC FUTURE): Cipher fetch caching. */
906
502k
    if ((cipher = EVP_CIPHER_fetch(libctx, "AES-128-GCM", propq)) == NULL) {
907
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
908
0
        goto err;
909
0
    }
910
911
502k
    if ((cctx = EVP_CIPHER_CTX_new()) == NULL) {
912
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
913
0
        goto err;
914
0
    }
915
916
502k
    if (!EVP_CipherInit_ex(cctx, cipher, NULL,
917
502k
            retry_integrity_key, retry_integrity_nonce, /*enc=*/1)) {
918
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
919
0
        goto err;
920
0
    }
921
922
    /* Feed packet header as AAD data. */
923
502k
    if (EVP_CipherUpdate(cctx, NULL, &l, buf, hdr_enc_len) != 1) {
924
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
925
0
        goto err;
926
0
    }
927
928
    /* Feed packet body as AAD data. */
929
502k
    if (EVP_CipherUpdate(cctx, NULL, &l, hdr->data,
930
502k
            hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN)
931
502k
        != 1) {
932
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
933
0
        goto err;
934
0
    }
935
936
    /* Finalise and get tag. */
937
502k
    if (EVP_CipherFinal_ex(cctx, NULL, &l2) != 1) {
938
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
939
0
        goto err;
940
0
    }
941
942
502k
    if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_GET_TAG,
943
502k
            QUIC_RETRY_INTEGRITY_TAG_LEN,
944
502k
            tag)
945
502k
        != 1) {
946
0
        ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
947
0
        goto err;
948
0
    }
949
950
502k
    ok = 1;
951
502k
err:
952
502k
    EVP_CIPHER_free(cipher);
953
502k
    EVP_CIPHER_CTX_free(cctx);
954
502k
    if (wpkt_valid)
955
502k
        WPACKET_finish(&wpkt);
956
957
502k
    return ok;
958
502k
}