Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/packet.c
Line
Count
Source
1
/*
2
 * Copyright 2015-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/cryptlib.h"
11
#include "internal/packet.h"
12
#if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
13
#include "internal/packet_quic.h"
14
#endif
15
#include <openssl/err.h>
16
17
0
#define DEFAULT_BUF_SIZE 256
18
19
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
20
0
{
21
0
    if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
22
0
        return 0;
23
24
0
    pkt->written += len;
25
0
    pkt->curr += len;
26
0
    return 1;
27
0
}
28
29
int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
30
    unsigned char **allocbytes, size_t lenbytes)
31
0
{
32
0
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
33
0
        || !WPACKET_allocate_bytes(pkt, len, allocbytes)
34
0
        || !WPACKET_close(pkt))
35
0
        return 0;
36
37
0
    return 1;
38
0
}
39
40
0
#define GETBUF(p) (((p)->staticbuf != NULL)         \
41
0
        ? (p)->staticbuf                            \
42
0
        : ((p)->buf != NULL                         \
43
0
                  ? (unsigned char *)(p)->buf->data \
44
0
                  : NULL))
45
46
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
47
0
{
48
    /* Internal API, so should not fail */
49
0
    if (!ossl_assert(pkt->subs != NULL && len != 0))
50
0
        return 0;
51
52
0
    if (pkt->maxsize - pkt->written < len)
53
0
        return 0;
54
55
0
    if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
56
0
        size_t newlen;
57
0
        size_t reflen;
58
59
0
        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
60
61
0
        if (reflen > SIZE_MAX / 2) {
62
0
            newlen = SIZE_MAX;
63
0
        } else {
64
0
            newlen = reflen * 2;
65
0
            if (newlen < DEFAULT_BUF_SIZE)
66
0
                newlen = DEFAULT_BUF_SIZE;
67
0
        }
68
0
        if (BUF_MEM_grow(pkt->buf, newlen) == 0)
69
0
            return 0;
70
0
    }
71
0
    if (allocbytes != NULL) {
72
0
        *allocbytes = WPACKET_get_curr(pkt);
73
0
        if (pkt->endfirst && *allocbytes != NULL)
74
0
            *allocbytes -= len;
75
0
    }
76
77
0
    return 1;
78
0
}
79
80
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
81
    unsigned char **allocbytes, size_t lenbytes)
82
0
{
83
0
    if (pkt->endfirst && lenbytes > 0)
84
0
        return 0;
85
86
0
    if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
87
0
        return 0;
88
89
0
    if (*allocbytes != NULL)
90
0
        *allocbytes += lenbytes;
91
92
0
    return 1;
93
0
}
94
95
static size_t maxmaxsize(size_t lenbytes)
96
0
{
97
0
    if (lenbytes >= sizeof(size_t) || lenbytes == 0)
98
0
        return SIZE_MAX;
99
100
0
    return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
101
0
}
102
103
static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
104
0
{
105
0
    unsigned char *lenchars;
106
107
0
    pkt->curr = 0;
108
0
    pkt->written = 0;
109
110
0
    if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL)
111
0
        return 0;
112
113
0
    if (lenbytes == 0)
114
0
        return 1;
115
116
0
    pkt->subs->pwritten = lenbytes;
117
0
    pkt->subs->lenbytes = lenbytes;
118
119
0
    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
120
0
        OPENSSL_free(pkt->subs);
121
0
        pkt->subs = NULL;
122
0
        return 0;
123
0
    }
124
0
    pkt->subs->packet_len = 0;
125
126
0
    return 1;
127
0
}
128
129
int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
130
    size_t lenbytes)
131
0
{
132
0
    size_t max = maxmaxsize(lenbytes);
133
134
    /* Internal API, so should not fail */
135
0
    if (!ossl_assert(buf != NULL && len > 0))
136
0
        return 0;
137
138
0
    pkt->staticbuf = buf;
139
0
    pkt->buf = NULL;
140
0
    pkt->maxsize = (max < len) ? max : len;
141
0
    pkt->endfirst = 0;
142
143
0
    return wpacket_intern_init_len(pkt, lenbytes);
144
0
}
145
146
int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
147
0
{
148
    /* Internal API, so should not fail */
149
0
    if (!ossl_assert(buf != NULL && len > 0))
150
0
        return 0;
151
152
0
    pkt->staticbuf = buf;
153
0
    pkt->buf = NULL;
154
0
    pkt->maxsize = len;
155
0
    pkt->endfirst = 1;
156
157
0
    return wpacket_intern_init_len(pkt, 0);
158
0
}
159
160
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
161
0
{
162
    /* Internal API, so should not fail */
163
0
    if (!ossl_assert(buf != NULL))
164
0
        return 0;
165
166
0
    pkt->staticbuf = NULL;
167
0
    pkt->buf = buf;
168
0
    pkt->maxsize = maxmaxsize(lenbytes);
169
0
    pkt->endfirst = 0;
170
171
0
    return wpacket_intern_init_len(pkt, lenbytes);
172
0
}
173
174
int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
175
0
{
176
0
    return WPACKET_init_len(pkt, buf, 0);
177
0
}
178
179
int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
180
0
{
181
0
    pkt->staticbuf = NULL;
182
0
    pkt->buf = NULL;
183
0
    pkt->maxsize = maxmaxsize(lenbytes);
184
0
    pkt->endfirst = 0;
185
186
0
    return wpacket_intern_init_len(pkt, 0);
187
0
}
188
189
int WPACKET_init_null_der(WPACKET *pkt)
190
0
{
191
0
    pkt->staticbuf = NULL;
192
0
    pkt->buf = NULL;
193
0
    pkt->maxsize = SIZE_MAX;
194
0
    pkt->endfirst = 1;
195
196
0
    return wpacket_intern_init_len(pkt, 0);
197
0
}
198
199
int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
200
0
{
201
    /* Internal API, so should not fail */
202
0
    if (!ossl_assert(pkt->subs != NULL))
203
0
        return 0;
204
205
0
    pkt->subs->flags = flags;
206
207
0
    return 1;
208
0
}
209
210
/* Store the |value| of length |len| at location |data| */
211
static int put_value(unsigned char *data, uint64_t value, size_t len)
212
0
{
213
0
    if (data == NULL)
214
0
        return 1;
215
216
0
    for (data += len - 1; len > 0; len--) {
217
0
        *data = (unsigned char)(value & 0xff);
218
0
        data--;
219
0
        value >>= 8;
220
0
    }
221
222
    /* Check whether we could fit the value in the assigned number of bytes */
223
0
    if (value > 0)
224
0
        return 0;
225
226
0
    return 1;
227
0
}
228
229
#if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
230
static int put_quic_value(unsigned char *data, size_t value, size_t len)
231
0
{
232
0
    if (data == NULL)
233
0
        return 1;
234
235
    /* Value too large for field. */
236
0
    if (ossl_quic_vlint_encode_len(value) > len)
237
0
        return 0;
238
239
0
    ossl_quic_vlint_encode_n(data, value, (int)len);
240
0
    return 1;
241
0
}
242
#endif
243
244
/*
245
 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
246
 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
247
 * necessary. If |doclose| is 0 then it goes through the motions of closing
248
 * (i.e. it fills in all the lengths), but doesn't actually close anything.
249
 */
250
static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
251
0
{
252
0
    size_t packlen = pkt->written - sub->pwritten;
253
254
0
    if (packlen == 0
255
0
        && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
256
0
        return 0;
257
258
0
    if (packlen == 0
259
0
        && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
260
        /* We can't handle this case. Return an error */
261
0
        if (!doclose)
262
0
            return 0;
263
264
        /* Deallocate any bytes allocated for the length of the WPACKET */
265
0
        if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
266
0
            pkt->written -= sub->lenbytes;
267
0
            pkt->curr -= sub->lenbytes;
268
0
        }
269
270
        /* Don't write out the packet length */
271
0
        sub->packet_len = 0;
272
0
        sub->lenbytes = 0;
273
0
    }
274
275
    /* Write out the WPACKET length if needed */
276
0
    if (sub->lenbytes > 0) {
277
0
        unsigned char *buf = GETBUF(pkt);
278
279
0
        if (buf != NULL) {
280
0
#if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
281
0
            if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
282
0
                if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
283
0
                    return 0;
284
0
            } else {
285
0
                if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
286
0
                    return 0;
287
0
            }
288
#else
289
            if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
290
                return 0;
291
#endif
292
0
        }
293
0
    } else if (pkt->endfirst && sub->parent != NULL
294
0
        && (packlen != 0
295
0
            || (sub->flags
296
0
                   & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
297
0
                == 0)) {
298
0
        size_t tmplen = packlen;
299
0
        size_t numlenbytes = 1;
300
301
0
        while ((tmplen = tmplen >> 8) > 0)
302
0
            numlenbytes++;
303
0
        if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
304
0
            return 0;
305
0
        if (packlen > 0x7f) {
306
0
            numlenbytes |= 0x80;
307
0
            if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
308
0
                return 0;
309
0
        }
310
0
    }
311
312
0
    if (doclose) {
313
0
        pkt->subs = sub->parent;
314
0
        OPENSSL_free(sub);
315
0
    }
316
317
0
    return 1;
318
0
}
319
320
int WPACKET_fill_lengths(WPACKET *pkt)
321
0
{
322
0
    WPACKET_SUB *sub;
323
324
0
    if (!ossl_assert(pkt->subs != NULL))
325
0
        return 0;
326
327
0
    for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
328
0
        if (!wpacket_intern_close(pkt, sub, 0))
329
0
            return 0;
330
0
    }
331
332
0
    return 1;
333
0
}
334
335
int WPACKET_close(WPACKET *pkt)
336
0
{
337
    /*
338
     * Internal API, so should not fail - but we do negative testing of this
339
     * so no assert (otherwise the tests fail)
340
     */
341
0
    if (pkt->subs == NULL || pkt->subs->parent == NULL)
342
0
        return 0;
343
344
0
    return wpacket_intern_close(pkt, pkt->subs, 1);
345
0
}
346
347
int WPACKET_finish(WPACKET *pkt)
348
0
{
349
0
    int ret;
350
351
    /*
352
     * Internal API, so should not fail - but we do negative testing of this
353
     * so no assert (otherwise the tests fail)
354
     */
355
0
    if (pkt->subs == NULL || pkt->subs->parent != NULL)
356
0
        return 0;
357
358
0
    ret = wpacket_intern_close(pkt, pkt->subs, 1);
359
0
    if (ret) {
360
0
        OPENSSL_free(pkt->subs);
361
0
        pkt->subs = NULL;
362
0
    }
363
364
0
    return ret;
365
0
}
366
367
int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
368
0
{
369
0
    WPACKET_SUB *sub;
370
0
    unsigned char *lenchars;
371
372
    /* Internal API, so should not fail */
373
0
    if (!ossl_assert(pkt->subs != NULL))
374
0
        return 0;
375
376
    /* We don't support lenbytes greater than 0 when doing endfirst writing */
377
0
    if (lenbytes > 0 && pkt->endfirst)
378
0
        return 0;
379
380
0
    if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL)
381
0
        return 0;
382
383
0
    sub->parent = pkt->subs;
384
0
    pkt->subs = sub;
385
0
    sub->pwritten = pkt->written + lenbytes;
386
0
    sub->lenbytes = lenbytes;
387
388
0
    if (lenbytes == 0) {
389
0
        sub->packet_len = 0;
390
0
        return 1;
391
0
    }
392
393
0
    sub->packet_len = pkt->written;
394
395
0
    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
396
0
        return 0;
397
398
0
    return 1;
399
0
}
400
401
int WPACKET_start_sub_packet(WPACKET *pkt)
402
0
{
403
0
    return WPACKET_start_sub_packet_len__(pkt, 0);
404
0
}
405
406
int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
407
0
{
408
0
    unsigned char *data;
409
410
    /* Internal API, so should not fail */
411
0
    if (!ossl_assert(size <= sizeof(uint64_t))
412
0
        || !WPACKET_allocate_bytes(pkt, size, &data)
413
0
        || !put_value(data, val, size))
414
0
        return 0;
415
416
0
    return 1;
417
0
}
418
419
int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
420
0
{
421
0
    WPACKET_SUB *sub;
422
0
    size_t lenbytes;
423
424
    /* Internal API, so should not fail */
425
0
    if (!ossl_assert(pkt->subs != NULL))
426
0
        return 0;
427
428
    /* Find the WPACKET_SUB for the top level */
429
0
    for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
430
0
        continue;
431
432
0
    lenbytes = sub->lenbytes;
433
0
    if (lenbytes == 0)
434
0
        lenbytes = sizeof(pkt->maxsize);
435
436
0
    if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
437
0
        return 0;
438
439
0
    pkt->maxsize = maxsize;
440
441
0
    return 1;
442
0
}
443
444
int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
445
0
{
446
0
    unsigned char *dest;
447
448
0
    if (len == 0)
449
0
        return 1;
450
451
0
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
452
0
        return 0;
453
454
0
    if (dest != NULL)
455
0
        memset(dest, ch, len);
456
457
0
    return 1;
458
0
}
459
460
int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
461
0
{
462
0
    unsigned char *dest;
463
464
0
    if (len == 0)
465
0
        return 1;
466
467
0
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
468
0
        return 0;
469
470
0
    if (dest != NULL)
471
0
        memcpy(dest, src, len);
472
473
0
    return 1;
474
0
}
475
476
int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
477
    size_t lenbytes)
478
0
{
479
0
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
480
0
        || !WPACKET_memcpy(pkt, src, len)
481
0
        || !WPACKET_close(pkt))
482
0
        return 0;
483
484
0
    return 1;
485
0
}
486
487
int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
488
0
{
489
    /* Internal API, so should not fail */
490
0
    if (!ossl_assert(written != NULL))
491
0
        return 0;
492
493
0
    *written = pkt->written;
494
495
0
    return 1;
496
0
}
497
498
int WPACKET_get_length(WPACKET *pkt, size_t *len)
499
0
{
500
    /* Internal API, so should not fail */
501
0
    if (!ossl_assert(pkt->subs != NULL && len != NULL))
502
0
        return 0;
503
504
0
    *len = pkt->written - pkt->subs->pwritten;
505
506
0
    return 1;
507
0
}
508
509
unsigned char *WPACKET_get_curr(WPACKET *pkt)
510
0
{
511
0
    unsigned char *buf = GETBUF(pkt);
512
513
0
    if (buf == NULL)
514
0
        return NULL;
515
516
0
    if (pkt->endfirst)
517
0
        return buf + pkt->maxsize - pkt->curr;
518
519
0
    return buf + pkt->curr;
520
0
}
521
522
int WPACKET_is_null_buf(WPACKET *pkt)
523
0
{
524
0
    return pkt->buf == NULL && pkt->staticbuf == NULL;
525
0
}
526
527
void WPACKET_cleanup(WPACKET *pkt)
528
0
{
529
0
    WPACKET_SUB *sub, *parent;
530
531
0
    for (sub = pkt->subs; sub != NULL; sub = parent) {
532
0
        parent = sub->parent;
533
0
        OPENSSL_free(sub);
534
0
    }
535
0
    pkt->subs = NULL;
536
0
}
537
538
#if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
539
540
int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
541
0
{
542
0
    size_t enclen = ossl_quic_vlint_encode_len(max_len);
543
544
0
    if (enclen == 0)
545
0
        return 0;
546
547
0
    if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
548
0
        return 0;
549
550
0
    pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
551
0
    return 1;
552
0
}
553
554
int WPACKET_start_quic_sub_packet(WPACKET *pkt)
555
0
{
556
    /*
557
     * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
558
     * be used.
559
     */
560
0
    return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
561
0
}
562
563
int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
564
0
{
565
0
    if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
566
0
        || !WPACKET_allocate_bytes(pkt, len, allocbytes)
567
0
        || !WPACKET_close(pkt))
568
0
        return 0;
569
570
0
    return 1;
571
0
}
572
573
/*
574
 * Write a QUIC variable-length integer to the packet.
575
 */
576
int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
577
0
{
578
0
    unsigned char *b = NULL;
579
0
    size_t enclen = ossl_quic_vlint_encode_len(v);
580
581
0
    if (enclen == 0)
582
0
        return 0;
583
584
0
    if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
585
0
        return 0;
586
587
0
    ossl_quic_vlint_encode(b, v);
588
0
    return 1;
589
0
}
590
591
#endif