Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/packet.c
Line
Count
Source
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
207k
#define DEFAULT_BUF_SIZE 256
15
16
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
17
170M
{
18
170M
    if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
19
94.5k
        return 0;
20
21
169M
    pkt->written += len;
22
169M
    pkt->curr += len;
23
169M
    return 1;
24
170M
}
25
26
int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
27
    unsigned char **allocbytes, size_t lenbytes)
28
36.5k
{
29
36.5k
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
30
36.5k
        || !WPACKET_allocate_bytes(pkt, len, allocbytes)
31
36.5k
        || !WPACKET_close(pkt))
32
0
        return 0;
33
34
36.5k
    return 1;
35
36.5k
}
36
37
175M
#define GETBUF(p) (((p)->staticbuf != NULL)         \
38
175M
        ? (p)->staticbuf                            \
39
175M
        : ((p)->buf != NULL                         \
40
23.0M
                  ? (unsigned char *)(p)->buf->data \
41
23.0M
                  : NULL))
42
43
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
44
172M
{
45
    /* Internal API, so should not fail */
46
172M
    if (!ossl_assert(pkt->subs != NULL && len != 0))
47
0
        return 0;
48
49
172M
    if (pkt->maxsize - pkt->written < len)
50
94.5k
        return 0;
51
52
172M
    if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
53
119k
        size_t newlen;
54
119k
        size_t reflen;
55
56
119k
        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
57
58
119k
        if (reflen > SIZE_MAX / 2) {
59
0
            newlen = SIZE_MAX;
60
119k
        } else {
61
119k
            newlen = reflen * 2;
62
119k
            if (newlen < DEFAULT_BUF_SIZE)
63
87.8k
                newlen = DEFAULT_BUF_SIZE;
64
119k
        }
65
119k
        if (BUF_MEM_grow(pkt->buf, newlen) == 0)
66
0
            return 0;
67
119k
    }
68
172M
    if (allocbytes != NULL) {
69
169M
        *allocbytes = WPACKET_get_curr(pkt);
70
169M
        if (pkt->endfirst && *allocbytes != NULL)
71
80.2k
            *allocbytes -= len;
72
169M
    }
73
74
172M
    return 1;
75
172M
}
76
77
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
78
    unsigned char **allocbytes, size_t lenbytes)
79
9.73k
{
80
9.73k
    if (pkt->endfirst && lenbytes > 0)
81
0
        return 0;
82
83
9.73k
    if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
84
0
        return 0;
85
86
9.73k
    if (*allocbytes != NULL)
87
9.73k
        *allocbytes += lenbytes;
88
89
9.73k
    return 1;
90
9.73k
}
91
92
static size_t maxmaxsize(size_t lenbytes)
93
26.6M
{
94
26.6M
    if (lenbytes >= sizeof(size_t) || lenbytes == 0)
95
26.6M
        return SIZE_MAX;
96
97
0
    return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
98
26.6M
}
99
100
static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
101
16.7M
{
102
16.7M
    unsigned char *lenchars;
103
104
16.7M
    pkt->curr = 0;
105
16.7M
    pkt->written = 0;
106
107
16.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
16.7M
    if (lenbytes == 0)
113
16.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
15.8M
{
131
15.8M
    size_t max = maxmaxsize(lenbytes);
132
133
    /* Internal API, so should not fail */
134
15.8M
    if (!ossl_assert(buf != NULL && len > 0))
135
0
        return 0;
136
137
15.8M
    pkt->staticbuf = buf;
138
15.8M
    pkt->buf = NULL;
139
15.8M
    pkt->maxsize = (max < len) ? max : len;
140
15.8M
    pkt->endfirst = 0;
141
142
15.8M
    return wpacket_intern_init_len(pkt, lenbytes);
143
15.8M
}
144
145
int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
146
26.6k
{
147
    /* Internal API, so should not fail */
148
26.6k
    if (!ossl_assert(buf != NULL && len > 0))
149
0
        return 0;
150
151
26.6k
    pkt->staticbuf = buf;
152
26.6k
    pkt->buf = NULL;
153
26.6k
    pkt->maxsize = len;
154
26.6k
    pkt->endfirst = 1;
155
156
26.6k
    return wpacket_intern_init_len(pkt, 0);
157
26.6k
}
158
159
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
160
345k
{
161
    /* Internal API, so should not fail */
162
345k
    if (!ossl_assert(buf != NULL))
163
0
        return 0;
164
165
345k
    pkt->staticbuf = NULL;
166
345k
    pkt->buf = buf;
167
345k
    pkt->maxsize = maxmaxsize(lenbytes);
168
345k
    pkt->endfirst = 0;
169
170
345k
    return wpacket_intern_init_len(pkt, lenbytes);
171
345k
}
172
173
int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
174
340k
{
175
340k
    return WPACKET_init_len(pkt, buf, 0);
176
340k
}
177
178
int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
179
543k
{
180
543k
    pkt->staticbuf = NULL;
181
543k
    pkt->buf = NULL;
182
543k
    pkt->maxsize = maxmaxsize(lenbytes);
183
543k
    pkt->endfirst = 0;
184
185
543k
    return wpacket_intern_init_len(pkt, 0);
186
543k
}
187
188
int WPACKET_init_null_der(WPACKET *pkt)
189
32
{
190
32
    pkt->staticbuf = NULL;
191
32
    pkt->buf = NULL;
192
32
    pkt->maxsize = SIZE_MAX;
193
32
    pkt->endfirst = 1;
194
195
32
    return wpacket_intern_init_len(pkt, 0);
196
32
}
197
198
int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
199
261k
{
200
    /* Internal API, so should not fail */
201
261k
    if (!ossl_assert(pkt->subs != NULL))
202
0
        return 0;
203
204
261k
    pkt->subs->flags = flags;
205
206
261k
    return 1;
207
261k
}
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
33.9M
{
212
33.9M
    if (data == NULL)
213
3.25M
        return 1;
214
215
86.7M
    for (data += len - 1; len > 0; len--) {
216
56.0M
        *data = (unsigned char)(value & 0xff);
217
56.0M
        data--;
218
56.0M
        value >>= 8;
219
56.0M
    }
220
221
    /* Check whether we could fit the value in the assigned number of bytes */
222
30.6M
    if (value > 0)
223
548
        return 0;
224
225
30.6M
    return 1;
226
30.6M
}
227
228
/*
229
 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
230
 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
231
 * necessary. If |doclose| is 0 then it goes through the motions of closing
232
 * (i.e. it fills in all the lengths), but doesn't actually close anything.
233
 */
234
static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
235
630k
{
236
630k
    size_t packlen = pkt->written - sub->pwritten;
237
238
630k
    if (packlen == 0
239
16.3k
        && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
240
0
        return 0;
241
242
630k
    if (packlen == 0
243
16.3k
        && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
244
        /* We can't handle this case. Return an error */
245
1.57k
        if (!doclose)
246
0
            return 0;
247
248
        /* Deallocate any bytes allocated for the length of the WPACKET */
249
1.57k
        if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
250
1.57k
            pkt->written -= sub->lenbytes;
251
1.57k
            pkt->curr -= sub->lenbytes;
252
1.57k
        }
253
254
        /* Don't write out the packet length */
255
1.57k
        sub->packet_len = 0;
256
1.57k
        sub->lenbytes = 0;
257
1.57k
    }
258
259
    /* Write out the WPACKET length if needed */
260
630k
    if (sub->lenbytes > 0) {
261
220k
        unsigned char *buf = GETBUF(pkt);
262
263
220k
        if (buf != NULL
264
220k
            && !put_value(&buf[sub->packet_len], packlen,
265
220k
                sub->lenbytes))
266
120
            return 0;
267
410k
    } else if (pkt->endfirst && sub->parent != NULL
268
3.72k
        && (packlen != 0
269
2
            || (sub->flags
270
2
                   & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
271
3.72k
                == 0)) {
272
3.72k
        size_t tmplen = packlen;
273
3.72k
        size_t numlenbytes = 1;
274
275
3.72k
        while ((tmplen = tmplen >> 8) > 0)
276
0
            numlenbytes++;
277
3.72k
        if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
278
0
            return 0;
279
3.72k
        if (packlen > 0x7f) {
280
0
            numlenbytes |= 0x80;
281
0
            if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
282
0
                return 0;
283
0
        }
284
3.72k
    }
285
286
630k
    if (doclose) {
287
630k
        pkt->subs = sub->parent;
288
630k
        OPENSSL_free(sub);
289
630k
    }
290
291
630k
    return 1;
292
630k
}
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
7.56M
{
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
7.56M
    if (pkt->subs == NULL || pkt->subs->parent == NULL)
316
0
        return 0;
317
318
7.56M
    return wpacket_intern_close(pkt, pkt->subs, 1);
319
7.56M
}
320
321
int WPACKET_finish(WPACKET *pkt)
322
16.2M
{
323
16.2M
    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
16.2M
    if (pkt->subs == NULL || pkt->subs->parent != NULL)
330
0
        return 0;
331
332
16.2M
    ret = wpacket_intern_close(pkt, pkt->subs, 1);
333
16.2M
    if (ret) {
334
16.2M
        OPENSSL_free(pkt->subs);
335
16.2M
        pkt->subs = NULL;
336
16.2M
    }
337
338
16.2M
    return ret;
339
16.2M
}
340
341
int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
342
7.56M
{
343
7.56M
    WPACKET_SUB *sub;
344
7.56M
    unsigned char *lenchars;
345
346
    /* Internal API, so should not fail */
347
7.56M
    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
7.56M
    if (lenbytes > 0 && pkt->endfirst)
352
0
        return 0;
353
354
7.56M
    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
7.56M
    sub->parent = pkt->subs;
360
7.56M
    pkt->subs = sub;
361
7.56M
    sub->pwritten = pkt->written + lenbytes;
362
7.56M
    sub->lenbytes = lenbytes;
363
364
7.56M
    if (lenbytes == 0) {
365
1.75M
        sub->packet_len = 0;
366
1.75M
        return 1;
367
1.75M
    }
368
369
5.81M
    sub->packet_len = pkt->written;
370
371
5.81M
    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
372
0
        return 0;
373
374
5.81M
    return 1;
375
5.81M
}
376
377
int WPACKET_start_sub_packet(WPACKET *pkt)
378
1.75M
{
379
1.75M
    return WPACKET_start_sub_packet_len__(pkt, 0);
380
1.75M
}
381
382
int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
383
28.1M
{
384
28.1M
    unsigned char *data;
385
386
    /* Internal API, so should not fail */
387
28.1M
    if (!ossl_assert(size <= sizeof(uint64_t))
388
28.1M
        || !WPACKET_allocate_bytes(pkt, size, &data)
389
28.1M
        || !put_value(data, val, size))
390
33.7k
        return 0;
391
392
28.1M
    return 1;
393
28.1M
}
394
395
int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
396
9.99M
{
397
9.99M
    WPACKET_SUB *sub;
398
9.99M
    size_t lenbytes;
399
400
    /* Internal API, so should not fail */
401
9.99M
    if (!ossl_assert(pkt->subs != NULL))
402
0
        return 0;
403
404
    /* Find the WPACKET_SUB for the top level */
405
9.99M
    for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
406
0
        continue;
407
408
9.99M
    lenbytes = sub->lenbytes;
409
9.99M
    if (lenbytes == 0)
410
9.99M
        lenbytes = sizeof(pkt->maxsize);
411
412
9.99M
    if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
413
0
        return 0;
414
415
9.99M
    pkt->maxsize = maxsize;
416
417
9.99M
    return 1;
418
9.99M
}
419
420
int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
421
1.43M
{
422
1.43M
    unsigned char *dest;
423
424
1.43M
    if (len == 0)
425
0
        return 1;
426
427
1.43M
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
428
4
        return 0;
429
430
1.43M
    if (dest != NULL)
431
1.43M
        memset(dest, ch, len);
432
433
1.43M
    return 1;
434
1.43M
}
435
436
int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
437
14.3M
{
438
14.3M
    unsigned char *dest;
439
440
14.3M
    if (len == 0)
441
6.25M
        return 1;
442
443
8.10M
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
444
59.7k
        return 0;
445
446
8.04M
    if (dest != NULL)
447
8.04M
        memcpy(dest, src, len);
448
449
8.04M
    return 1;
450
8.10M
}
451
452
int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
453
    size_t lenbytes)
454
1.88M
{
455
1.88M
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
456
1.88M
        || !WPACKET_memcpy(pkt, src, len)
457
1.88M
        || !WPACKET_close(pkt))
458
548
        return 0;
459
460
1.88M
    return 1;
461
1.88M
}
462
463
int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
464
19.8M
{
465
    /* Internal API, so should not fail */
466
19.8M
    if (!ossl_assert(written != NULL))
467
0
        return 0;
468
469
19.8M
    *written = pkt->written;
470
471
19.8M
    return 1;
472
19.8M
}
473
474
int WPACKET_get_length(WPACKET *pkt, size_t *len)
475
1.86M
{
476
    /* Internal API, so should not fail */
477
1.86M
    if (!ossl_assert(pkt->subs != NULL && len != NULL))
478
0
        return 0;
479
480
1.86M
    *len = pkt->written - pkt->subs->pwritten;
481
482
1.86M
    return 1;
483
1.86M
}
484
485
unsigned char *WPACKET_get_curr(WPACKET *pkt)
486
174M
{
487
174M
    unsigned char *buf = GETBUF(pkt);
488
489
174M
    if (buf == NULL)
490
4.33M
        return NULL;
491
492
170M
    if (pkt->endfirst)
493
106k
        return buf + pkt->maxsize - pkt->curr;
494
495
170M
    return buf + pkt->curr;
496
170M
}
497
498
int WPACKET_is_null_buf(WPACKET *pkt)
499
542k
{
500
542k
    return pkt->buf == NULL && pkt->staticbuf == NULL;
501
542k
}
502
503
void WPACKET_cleanup(WPACKET *pkt)
504
856k
{
505
856k
    WPACKET_SUB *sub, *parent;
506
507
1.32M
    for (sub = pkt->subs; sub != NULL; sub = parent) {
508
470k
        parent = sub->parent;
509
470k
        OPENSSL_free(sub);
510
470k
    }
511
    pkt->subs = NULL;
512
856k
}