Coverage Report

Created: 2025-08-28 07:07

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