Coverage Report

Created: 2026-05-20 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/asn1_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 <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
151k
{
49
151k
    int i, ret;
50
151k
    long len;
51
151k
    const unsigned char *p = *pp;
52
151k
    int tag, xclass, inf;
53
151k
    long max = omax;
54
55
151k
    if (omax <= 0) {
56
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57
0
        return 0x80;
58
0
    }
59
151k
    ret = (*p & V_ASN1_CONSTRUCTED);
60
151k
    xclass = (*p & V_ASN1_PRIVATE);
61
151k
    i = *p & V_ASN1_PRIMITIVE_TAG;
62
151k
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63
6.64k
        p++;
64
6.64k
        if (--max == 0)
65
432
            goto err;
66
6.21k
        len = 0;
67
12.3k
        while (*p & 0x80) {
68
7.15k
            len <<= 7L;
69
7.15k
            len |= *(p++) & 0x7f;
70
7.15k
            if (--max == 0)
71
494
                goto err;
72
6.66k
            if (len > (INT_MAX >> 7L))
73
493
                goto err;
74
6.66k
        }
75
5.22k
        len <<= 7L;
76
5.22k
        len |= *(p++) & 0x7f;
77
5.22k
        tag = (int)len;
78
5.22k
        if (--max == 0)
79
516
            goto err;
80
145k
    } else {
81
145k
        tag = i;
82
145k
        p++;
83
145k
        if (--max == 0)
84
2.28k
            goto err;
85
145k
    }
86
147k
    *ptag = tag;
87
147k
    *pclass = xclass;
88
147k
    if (!asn1_get_length(&p, &inf, plength, max))
89
2.63k
        goto err;
90
91
144k
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
92
465
        goto err;
93
94
144k
    if (*plength > (omax - (p - *pp))) {
95
2.60k
        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
2.60k
        ret |= 0x80;
101
2.60k
    }
102
144k
    *pp = p;
103
144k
    return ret | inf;
104
7.31k
err:
105
7.31k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106
7.31k
    return 0x80;
107
144k
}
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
147k
{
119
147k
    const unsigned char *p = *pp;
120
147k
    unsigned long ret = 0;
121
147k
    int i;
122
123
147k
    if (max-- < 1)
124
0
        return 0;
125
147k
    if (*p == 0x80) {
126
21.9k
        *inf = 1;
127
21.9k
        p++;
128
125k
    } else {
129
125k
        *inf = 0;
130
125k
        i = *p & 0x7f;
131
125k
        if (*p++ & 0x80) {
132
34.9k
            if (max < i)
133
856
                return 0;
134
            /* Skip leading zeroes */
135
42.8k
            while (i > 0 && *p == 0) {
136
8.67k
                p++;
137
8.67k
                i--;
138
8.67k
            }
139
34.1k
            if (i > (int)sizeof(long))
140
728
                return 0;
141
89.3k
            while (i > 0) {
142
55.8k
                ret <<= 8;
143
55.8k
                ret |= *p++;
144
55.8k
                i--;
145
55.8k
            }
146
33.4k
            if (ret > LONG_MAX)
147
1.04k
                return 0;
148
90.6k
        } else {
149
90.6k
            ret = i;
150
90.6k
        }
151
125k
    }
152
144k
    *pp = p;
153
144k
    *rl = (long)ret;
154
144k
    return 1;
155
147k
}
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
18.5k
{
163
18.5k
    unsigned char *p = *pp;
164
18.5k
    int i, ttag;
165
166
18.5k
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167
18.5k
    i |= (xclass & V_ASN1_PRIVATE);
168
18.5k
    if (tag < 31) {
169
18.5k
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170
18.5k
    } else {
171
0
        *(p++) = i | V_ASN1_PRIMITIVE_TAG;
172
0
        for (i = 0, ttag = tag; ttag > 0; i++)
173
0
            ttag >>= 7;
174
0
        ttag = i;
175
0
        while (i-- > 0) {
176
0
            p[i] = tag & 0x7f;
177
0
            if (i != (ttag - 1))
178
0
                p[i] |= 0x80;
179
0
            tag >>= 7;
180
0
        }
181
0
        p += ttag;
182
0
    }
183
18.5k
    if (constructed == 2)
184
0
        *(p++) = 0x80;
185
18.5k
    else
186
18.5k
        asn1_put_length(&p, length);
187
18.5k
    *pp = p;
188
18.5k
}
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
18.5k
{
202
18.5k
    unsigned char *p = *pp;
203
18.5k
    int i, len;
204
205
18.5k
    if (length <= 127) {
206
6.50k
        *(p++) = (unsigned char)length;
207
12.0k
    } else {
208
12.0k
        len = length;
209
28.2k
        for (i = 0; len > 0; i++)
210
16.1k
            len >>= 8;
211
12.0k
        *(p++) = i | 0x80;
212
12.0k
        len = i;
213
28.2k
        while (i-- > 0) {
214
16.1k
            p[i] = length & 0xff;
215
16.1k
            length >>= 8;
216
16.1k
        }
217
12.0k
        p += len;
218
12.0k
    }
219
18.5k
    *pp = p;
220
18.5k
}
221
222
int ASN1_object_size(int constructed, int length, int tag)
223
49.4k
{
224
49.4k
    int ret = 1;
225
226
49.4k
    if (length < 0)
227
0
        return -1;
228
49.4k
    if (tag >= 31) {
229
0
        while (tag > 0) {
230
0
            tag >>= 7;
231
0
            ret++;
232
0
        }
233
0
    }
234
49.4k
    if (constructed == 2) {
235
0
        ret += 3;
236
49.4k
    } else {
237
49.4k
        ret++;
238
49.4k
        if (length > 127) {
239
30.0k
            int tmplen = length;
240
70.5k
            while (tmplen > 0) {
241
40.4k
                tmplen >>= 8;
242
40.4k
                ret++;
243
40.4k
            }
244
30.0k
        }
245
49.4k
    }
246
49.4k
    if (ret >= INT_MAX - length)
247
0
        return -1;
248
49.4k
    return ret + length;
249
49.4k
}
250
251
void ossl_asn1_bit_string_set_unused_bits(ASN1_STRING *str, unsigned int num)
252
0
{
253
0
    str->flags &= ~0x07;
254
0
    str->flags |= ASN1_STRING_FLAG_BITS_LEFT | (num & 0x07);
255
0
}
256
257
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
258
0
{
259
0
    if (str == NULL)
260
0
        return 0;
261
0
    dst->type = str->type;
262
0
    if (!ASN1_STRING_set(dst, str->data, str->length))
263
0
        return 0;
264
    /* Copy flags but preserve embed value */
265
0
    dst->flags &= ASN1_STRING_FLAG_EMBED;
266
0
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
267
0
    return 1;
268
0
}
269
270
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
271
0
{
272
0
    ASN1_STRING *ret;
273
274
0
    if (!str)
275
0
        return NULL;
276
0
    ret = ASN1_STRING_new();
277
0
    if (ret == NULL)
278
0
        return NULL;
279
0
    if (!ASN1_STRING_copy(ret, str)) {
280
0
        ASN1_STRING_free(ret);
281
0
        return NULL;
282
0
    }
283
0
    return ret;
284
0
}
285
286
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
287
0
{
288
0
    unsigned char *c;
289
0
    const char *data = _data;
290
0
    size_t len;
291
292
0
    if (len_in < -1) {
293
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
294
0
        return 0;
295
0
    }
296
0
    if (len_in == -1) {
297
0
        if (data == NULL)
298
0
            return 0;
299
0
        len = strlen(data);
300
0
    } else {
301
0
        len = (size_t)len_in;
302
0
    }
303
    /*
304
     * Verify that the length fits within an integer for assignment to
305
     * str->length below.  The additional 1 is subtracted to allow for the
306
     * '\0' terminator even though this isn't strictly necessary.
307
     */
308
0
    if (len > INT_MAX - 1) {
309
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
310
0
        return 0;
311
0
    }
312
0
    if ((size_t)str->length <= len || str->data == NULL) {
313
0
        c = str->data;
314
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
315
        /* No NUL terminator in fuzzing builds */
316
0
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
317
#else
318
        str->data = OPENSSL_realloc(c, len + 1);
319
#endif
320
0
        if (str->data == NULL) {
321
0
            str->data = c;
322
0
            return 0;
323
0
        }
324
0
    }
325
0
    str->length = (int)len;
326
0
    if (data != NULL) {
327
0
        memcpy(str->data, data, len);
328
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
329
        /* Set the unused byte to something non NUL and printable. */
330
0
        if (len == 0)
331
0
            str->data[len] = '~';
332
#else
333
        /*
334
         * Add a NUL terminator. This should not be necessary - but we add it as
335
         * a safety precaution
336
         */
337
        str->data[len] = '\0';
338
#endif
339
0
    }
340
0
    return 1;
341
0
}
342
343
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
344
0
{
345
0
    OPENSSL_free(str->data);
346
0
    str->data = data;
347
0
    str->length = len;
348
0
}
349
350
ASN1_STRING *ASN1_STRING_new(void)
351
0
{
352
0
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
353
0
}
354
355
ASN1_STRING *ASN1_STRING_type_new(int type)
356
0
{
357
0
    ASN1_STRING *ret;
358
359
0
    ret = OPENSSL_zalloc(sizeof(*ret));
360
0
    if (ret == NULL)
361
0
        return NULL;
362
0
    ret->type = type;
363
0
    return ret;
364
0
}
365
366
void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
367
0
{
368
0
    if (a == NULL)
369
0
        return;
370
0
    if (!(a->flags & ASN1_STRING_FLAG_NDEF))
371
0
        OPENSSL_free(a->data);
372
0
    if (embed == 0)
373
0
        OPENSSL_free(a);
374
0
}
375
376
void ASN1_STRING_free(ASN1_STRING *a)
377
0
{
378
0
    if (a == NULL)
379
0
        return;
380
0
    ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
381
0
}
382
383
void ASN1_STRING_clear_free(ASN1_STRING *a)
384
0
{
385
0
    if (a == NULL)
386
0
        return;
387
0
    if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
388
0
        OPENSSL_cleanse(a->data, a->length);
389
0
    ASN1_STRING_free(a);
390
0
}
391
392
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
393
0
{
394
0
    int i;
395
396
0
    i = (a->length - b->length);
397
0
    if (i == 0) {
398
0
        if (a->length != 0)
399
0
            i = memcmp(a->data, b->data, a->length);
400
0
        if (i == 0)
401
0
            return a->type - b->type;
402
0
        else
403
0
            return i;
404
0
    } else {
405
0
        return i;
406
0
    }
407
0
}
408
409
int ASN1_STRING_length(const ASN1_STRING *x)
410
0
{
411
0
    return x->length;
412
0
}
413
414
#ifndef OPENSSL_NO_DEPRECATED_3_0
415
void ASN1_STRING_length_set(ASN1_STRING *x, int len)
416
0
{
417
0
    x->length = len;
418
0
}
419
#endif
420
421
int ASN1_STRING_type(const ASN1_STRING *x)
422
0
{
423
0
    return x->type;
424
0
}
425
426
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
427
0
{
428
0
    return x->data;
429
0
}
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
}