Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/asn1_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2021 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 <stdio.h>
11
#include <limits.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/asn1.h>
14
#include "asn1_local.h"
15
16
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
17
    long max);
18
static void asn1_put_length(unsigned char **pp, int length);
19
20
static int _asn1_check_infinite_end(const unsigned char **p, long len)
21
0
{
22
    /*
23
     * If there is 0 or 1 byte left, the length check should pick things up
24
     */
25
0
    if (len <= 0) {
26
0
        return 1;
27
0
    } else {
28
0
        if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
29
0
            (*p) += 2;
30
0
            return 1;
31
0
        }
32
0
    }
33
0
    return 0;
34
0
}
35
36
int ASN1_check_infinite_end(unsigned char **p, long len)
37
0
{
38
0
    return _asn1_check_infinite_end((const unsigned char **)p, len);
39
0
}
40
41
int ASN1_const_check_infinite_end(const unsigned char **p, long len)
42
0
{
43
0
    return _asn1_check_infinite_end(p, len);
44
0
}
45
46
int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
47
    int *pclass, long omax)
48
2.84G
{
49
2.84G
    int i, ret;
50
2.84G
    long len;
51
2.84G
    const unsigned char *p = *pp;
52
2.84G
    int tag, xclass, inf;
53
2.84G
    long max = omax;
54
55
2.84G
    if (omax <= 0) {
56
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57
0
        return 0x80;
58
0
    }
59
2.84G
    ret = (*p & V_ASN1_CONSTRUCTED);
60
2.84G
    xclass = (*p & V_ASN1_PRIVATE);
61
2.84G
    i = *p & V_ASN1_PRIMITIVE_TAG;
62
2.84G
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63
16.0M
        p++;
64
16.0M
        if (--max == 0)
65
20.7k
            goto err;
66
16.0M
        len = 0;
67
82.5M
        while (*p & 0x80) {
68
66.5M
            len <<= 7L;
69
66.5M
            len |= *(p++) & 0x7f;
70
66.5M
            if (--max == 0)
71
22.2k
                goto err;
72
66.5M
            if (len > (INT_MAX >> 7L))
73
18.3k
                goto err;
74
66.5M
        }
75
16.0M
        len <<= 7L;
76
16.0M
        len |= *(p++) & 0x7f;
77
16.0M
        tag = (int)len;
78
16.0M
        if (--max == 0)
79
17.9k
            goto err;
80
2.83G
    } else {
81
2.83G
        tag = i;
82
2.83G
        p++;
83
2.83G
        if (--max == 0)
84
233k
            goto err;
85
2.83G
    }
86
2.84G
    *ptag = tag;
87
2.84G
    *pclass = xclass;
88
2.84G
    if (!asn1_get_length(&p, &inf, plength, max))
89
611k
        goto err;
90
91
2.84G
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
92
52.0k
        goto err;
93
94
2.84G
    if (*plength > (omax - (p - *pp))) {
95
165M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
96
        /*
97
         * Set this so that even if things are not long enough the values are
98
         * set correctly
99
         */
100
165M
        ret |= 0x80;
101
165M
    }
102
2.84G
    *pp = p;
103
2.84G
    return ret | inf;
104
976k
err:
105
976k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106
976k
    return 0x80;
107
2.84G
}
108
109
/*
110
 * Decode a length field.
111
 * The short form is a single byte defining a length 0 - 127.
112
 * The long form is a byte 0 - 127 with the top bit set and this indicates
113
 * the number of following octets that contain the length.  These octets
114
 * are stored most significant digit first.
115
 */
116
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
117
    long max)
118
2.84G
{
119
2.84G
    const unsigned char *p = *pp;
120
2.84G
    unsigned long ret = 0;
121
2.84G
    int i;
122
123
2.84G
    if (max-- < 1)
124
0
        return 0;
125
2.84G
    if (*p == 0x80) {
126
733M
        *inf = 1;
127
733M
        p++;
128
2.11G
    } else {
129
2.11G
        *inf = 0;
130
2.11G
        i = *p & 0x7f;
131
2.11G
        if (*p++ & 0x80) {
132
35.8M
            if (max < i + 1)
133
476k
                return 0;
134
            /* Skip leading zeroes */
135
37.6M
            while (i > 0 && *p == 0) {
136
2.22M
                p++;
137
2.22M
                i--;
138
2.22M
            }
139
35.3M
            if (i > (int)sizeof(long))
140
51.6k
                return 0;
141
76.1M
            while (i > 0) {
142
40.8M
                ret <<= 8;
143
40.8M
                ret |= *p++;
144
40.8M
                i--;
145
40.8M
            }
146
35.3M
            if (ret > LONG_MAX)
147
83.1k
                return 0;
148
2.07G
        } else {
149
2.07G
            ret = i;
150
2.07G
        }
151
2.11G
    }
152
2.84G
    *pp = p;
153
2.84G
    *rl = (long)ret;
154
2.84G
    return 1;
155
2.84G
}
156
157
/*
158
 * constructed == 2 for indefinite length constructed
159
 */
160
void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
161
    int xclass)
162
102M
{
163
102M
    unsigned char *p = *pp;
164
102M
    int i, ttag;
165
166
102M
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167
102M
    i |= (xclass & V_ASN1_PRIVATE);
168
102M
    if (tag < 31) {
169
101M
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170
101M
    } else {
171
234k
        *(p++) = i | V_ASN1_PRIMITIVE_TAG;
172
548k
        for (i = 0, ttag = tag; ttag > 0; i++)
173
313k
            ttag >>= 7;
174
234k
        ttag = i;
175
548k
        while (i-- > 0) {
176
313k
            p[i] = tag & 0x7f;
177
313k
            if (i != (ttag - 1))
178
78.8k
                p[i] |= 0x80;
179
313k
            tag >>= 7;
180
313k
        }
181
234k
        p += ttag;
182
234k
    }
183
102M
    if (constructed == 2)
184
0
        *(p++) = 0x80;
185
102M
    else
186
102M
        asn1_put_length(&p, length);
187
102M
    *pp = p;
188
102M
}
189
190
int ASN1_put_eoc(unsigned char **pp)
191
0
{
192
0
    unsigned char *p = *pp;
193
194
0
    *p++ = 0;
195
0
    *p++ = 0;
196
0
    *pp = p;
197
0
    return 2;
198
0
}
199
200
static void asn1_put_length(unsigned char **pp, int length)
201
102M
{
202
102M
    unsigned char *p = *pp;
203
102M
    int i, len;
204
205
102M
    if (length <= 127) {
206
100M
        *(p++) = (unsigned char)length;
207
100M
    } else {
208
1.36M
        len = length;
209
3.71M
        for (i = 0; len > 0; i++)
210
2.35M
            len >>= 8;
211
1.36M
        *(p++) = i | 0x80;
212
1.36M
        len = i;
213
3.71M
        while (i-- > 0) {
214
2.35M
            p[i] = length & 0xff;
215
2.35M
            length >>= 8;
216
2.35M
        }
217
1.36M
        p += len;
218
1.36M
    }
219
102M
    *pp = p;
220
102M
}
221
222
int ASN1_object_size(int constructed, int length, int tag)
223
399M
{
224
399M
    int ret = 1;
225
226
399M
    if (length < 0)
227
0
        return -1;
228
399M
    if (tag >= 31) {
229
4.72M
        while (tag > 0) {
230
2.60M
            tag >>= 7;
231
2.60M
            ret++;
232
2.60M
        }
233
2.11M
    }
234
399M
    if (constructed == 2) {
235
0
        ret += 3;
236
399M
    } else {
237
399M
        ret++;
238
399M
        if (length > 127) {
239
4.52M
            int tmplen = length;
240
12.1M
            while (tmplen > 0) {
241
7.64M
                tmplen >>= 8;
242
7.64M
                ret++;
243
7.64M
            }
244
4.52M
        }
245
399M
    }
246
399M
    if (ret >= INT_MAX - length)
247
0
        return -1;
248
399M
    return ret + length;
249
399M
}
250
251
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
252
12.1M
{
253
12.1M
    if (str == NULL)
254
0
        return 0;
255
12.1M
    dst->type = str->type;
256
12.1M
    if (!ASN1_STRING_set(dst, str->data, str->length))
257
0
        return 0;
258
    /* Copy flags but preserve embed value */
259
12.1M
    dst->flags &= ASN1_STRING_FLAG_EMBED;
260
12.1M
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
261
12.1M
    return 1;
262
12.1M
}
263
264
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
265
130k
{
266
130k
    ASN1_STRING *ret;
267
268
130k
    if (!str)
269
0
        return NULL;
270
130k
    ret = ASN1_STRING_new();
271
130k
    if (ret == NULL)
272
0
        return NULL;
273
130k
    if (!ASN1_STRING_copy(ret, str)) {
274
0
        ASN1_STRING_free(ret);
275
0
        return NULL;
276
0
    }
277
130k
    return ret;
278
130k
}
279
280
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
281
95.4M
{
282
95.4M
    unsigned char *c;
283
95.4M
    const char *data = _data;
284
95.4M
    size_t len;
285
286
95.4M
    if (len_in < 0) {
287
121k
        if (data == NULL)
288
0
            return 0;
289
121k
        len = strlen(data);
290
95.2M
    } else {
291
95.2M
        len = (size_t)len_in;
292
95.2M
    }
293
    /*
294
     * Verify that the length fits within an integer for assignment to
295
     * str->length below.  The additional 1 is subtracted to allow for the
296
     * '\0' terminator even though this isn't strictly necessary.
297
     */
298
95.4M
    if (len > INT_MAX - 1) {
299
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
300
0
        return 0;
301
0
    }
302
95.4M
    if ((size_t)str->length <= len || str->data == NULL) {
303
95.4M
        c = str->data;
304
95.4M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
305
        /* No NUL terminator in fuzzing builds */
306
95.4M
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
307
#else
308
        str->data = OPENSSL_realloc(c, len + 1);
309
#endif
310
95.4M
        if (str->data == NULL) {
311
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
312
0
            str->data = c;
313
0
            return 0;
314
0
        }
315
95.4M
    }
316
95.4M
    str->length = len;
317
95.4M
    if (data != NULL) {
318
81.6M
        memcpy(str->data, data, len);
319
81.6M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
320
        /* Set the unused byte to something non NUL and printable. */
321
81.6M
        if (len == 0)
322
19.5M
            str->data[len] = '~';
323
#else
324
        /*
325
         * Add a NUL terminator. This should not be necessary - but we add it as
326
         * a safety precaution
327
         */
328
        str->data[len] = '\0';
329
#endif
330
81.6M
    }
331
95.4M
    return 1;
332
95.4M
}
333
334
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
335
23.6M
{
336
23.6M
    OPENSSL_free(str->data);
337
23.6M
    str->data = data;
338
23.6M
    str->length = len;
339
23.6M
}
340
341
ASN1_STRING *ASN1_STRING_new(void)
342
150k
{
343
150k
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
344
150k
}
345
346
ASN1_STRING *ASN1_STRING_type_new(int type)
347
121M
{
348
121M
    ASN1_STRING *ret;
349
350
121M
    ret = OPENSSL_zalloc(sizeof(*ret));
351
121M
    if (ret == NULL) {
352
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
353
0
        return NULL;
354
0
    }
355
121M
    ret->type = type;
356
121M
    return ret;
357
121M
}
358
359
void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
360
129M
{
361
129M
    if (a == NULL)
362
0
        return;
363
129M
    if (!(a->flags & ASN1_STRING_FLAG_NDEF))
364
129M
        OPENSSL_free(a->data);
365
129M
    if (embed == 0)
366
121M
        OPENSSL_free(a);
367
129M
}
368
369
void ASN1_STRING_free(ASN1_STRING *a)
370
26.3M
{
371
26.3M
    if (a == NULL)
372
20.3M
        return;
373
5.95M
    ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
374
5.95M
}
375
376
void ASN1_STRING_clear_free(ASN1_STRING *a)
377
3.79k
{
378
3.79k
    if (a == NULL)
379
133
        return;
380
3.66k
    if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
381
3.66k
        OPENSSL_cleanse(a->data, a->length);
382
3.66k
    ASN1_STRING_free(a);
383
3.66k
}
384
385
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
386
42.1k
{
387
42.1k
    int i;
388
389
42.1k
    i = (a->length - b->length);
390
42.1k
    if (i == 0) {
391
39.6k
        if (a->length != 0)
392
39.4k
            i = memcmp(a->data, b->data, a->length);
393
39.6k
        if (i == 0)
394
30.2k
            return a->type - b->type;
395
9.42k
        else
396
9.42k
            return i;
397
39.6k
    } else {
398
2.43k
        return i;
399
2.43k
    }
400
42.1k
}
401
402
int ASN1_STRING_length(const ASN1_STRING *x)
403
3.84M
{
404
3.84M
    return x->length;
405
3.84M
}
406
407
#ifndef OPENSSL_NO_DEPRECATED_3_0
408
void ASN1_STRING_length_set(ASN1_STRING *x, int len)
409
0
{
410
0
    x->length = len;
411
0
}
412
#endif
413
414
int ASN1_STRING_type(const ASN1_STRING *x)
415
0
{
416
0
    return x->type;
417
0
}
418
419
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
420
3.94M
{
421
3.94M
    return x->data;
422
3.94M
}
423
424
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
425
unsigned char *ASN1_STRING_data(ASN1_STRING *x)
426
0
{
427
0
    return x->data;
428
0
}
429
#endif
430
431
/* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */
432
char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
433
    const char *sep, size_t max_len)
434
0
{
435
0
    int i;
436
0
    ASN1_UTF8STRING *current;
437
0
    size_t length = 0, sep_len;
438
0
    char *result = NULL;
439
0
    char *p;
440
441
0
    if (sep == NULL)
442
0
        sep = "";
443
0
    sep_len = strlen(sep);
444
445
0
    for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
446
0
        current = sk_ASN1_UTF8STRING_value(text, i);
447
0
        if (i > 0)
448
0
            length += sep_len;
449
0
        length += ASN1_STRING_length(current);
450
0
        if (max_len != 0 && length > max_len)
451
0
            return NULL;
452
0
    }
453
0
    if ((result = OPENSSL_malloc(length + 1)) == NULL)
454
0
        return NULL;
455
456
0
    p = result;
457
0
    for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
458
0
        current = sk_ASN1_UTF8STRING_value(text, i);
459
0
        length = ASN1_STRING_length(current);
460
0
        if (i > 0 && sep_len > 0) {
461
0
            strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */
462
0
            p += sep_len;
463
0
        }
464
0
        strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
465
0
        p += length;
466
0
    }
467
0
    *p = '\0';
468
469
0
    return result;
470
0
}