Coverage Report

Created: 2026-06-08 06:07

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
0
{
49
0
    int i, ret;
50
0
    long len;
51
0
    const unsigned char *p = *pp;
52
0
    int tag, xclass, inf;
53
0
    long max = omax;
54
55
0
    if (omax <= 0) {
56
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57
0
        return 0x80;
58
0
    }
59
0
    ret = (*p & V_ASN1_CONSTRUCTED);
60
0
    xclass = (*p & V_ASN1_PRIVATE);
61
0
    i = *p & V_ASN1_PRIMITIVE_TAG;
62
0
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63
0
        p++;
64
0
        if (--max == 0)
65
0
            goto err;
66
0
        len = 0;
67
0
        while (*p & 0x80) {
68
0
            len <<= 7L;
69
0
            len |= *(p++) & 0x7f;
70
0
            if (--max == 0)
71
0
                goto err;
72
0
            if (len > (INT_MAX >> 7L))
73
0
                goto err;
74
0
        }
75
0
        len <<= 7L;
76
0
        len |= *(p++) & 0x7f;
77
0
        tag = (int)len;
78
0
        if (--max == 0)
79
0
            goto err;
80
0
    } else {
81
0
        tag = i;
82
0
        p++;
83
0
        if (--max == 0)
84
0
            goto err;
85
0
    }
86
0
    *ptag = tag;
87
0
    *pclass = xclass;
88
0
    if (!asn1_get_length(&p, &inf, plength, max))
89
0
        goto err;
90
91
0
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
92
0
        goto err;
93
94
0
    if (*plength > (omax - (p - *pp))) {
95
0
        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
0
        ret |= 0x80;
101
0
    }
102
0
    *pp = p;
103
0
    return ret | inf;
104
0
err:
105
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106
0
    return 0x80;
107
0
}
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
0
{
119
0
    const unsigned char *p = *pp;
120
0
    unsigned long ret = 0;
121
0
    int i;
122
123
0
    if (max-- < 1)
124
0
        return 0;
125
0
    if (*p == 0x80) {
126
0
        *inf = 1;
127
0
        p++;
128
0
    } else {
129
0
        *inf = 0;
130
0
        i = *p & 0x7f;
131
0
        if (*p++ & 0x80) {
132
0
            if (max < i)
133
0
                return 0;
134
            /* Skip leading zeroes */
135
0
            while (i > 0 && *p == 0) {
136
0
                p++;
137
0
                i--;
138
0
            }
139
0
            if (i > (int)sizeof(long))
140
0
                return 0;
141
0
            while (i > 0) {
142
0
                ret <<= 8;
143
0
                ret |= *p++;
144
0
                i--;
145
0
            }
146
0
            if (ret > LONG_MAX)
147
0
                return 0;
148
0
        } else {
149
0
            ret = i;
150
0
        }
151
0
    }
152
0
    *pp = p;
153
0
    *rl = (long)ret;
154
0
    return 1;
155
0
}
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
0
{
163
0
    unsigned char *p = *pp;
164
0
    int i, ttag;
165
166
0
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167
0
    i |= (xclass & V_ASN1_PRIVATE);
168
0
    if (tag < 31) {
169
0
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170
0
    } 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
0
    if (constructed == 2)
184
0
        *(p++) = 0x80;
185
0
    else
186
0
        asn1_put_length(&p, length);
187
0
    *pp = p;
188
0
}
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
0
{
202
0
    unsigned char *p = *pp;
203
0
    int i, len;
204
205
0
    if (length <= 127) {
206
0
        *(p++) = (unsigned char)length;
207
0
    } else {
208
0
        len = length;
209
0
        for (i = 0; len > 0; i++)
210
0
            len >>= 8;
211
0
        *(p++) = i | 0x80;
212
0
        len = i;
213
0
        while (i-- > 0) {
214
0
            p[i] = length & 0xff;
215
0
            length >>= 8;
216
0
        }
217
0
        p += len;
218
0
    }
219
0
    *pp = p;
220
0
}
221
222
int ASN1_object_size(int constructed, int length, int tag)
223
0
{
224
0
    int ret = 1;
225
226
0
    if (length < 0)
227
0
        return -1;
228
0
    if (tag >= 31) {
229
0
        while (tag > 0) {
230
0
            tag >>= 7;
231
0
            ret++;
232
0
        }
233
0
    }
234
0
    if (constructed == 2) {
235
0
        ret += 3;
236
0
    } else {
237
0
        ret++;
238
0
        if (length > 127) {
239
0
            int tmplen = length;
240
0
            while (tmplen > 0) {
241
0
                tmplen >>= 8;
242
0
                ret++;
243
0
            }
244
0
        }
245
0
    }
246
0
    if (ret >= INT_MAX - length)
247
0
        return -1;
248
0
    return ret + length;
249
0
}
250
251
void ossl_asn1_bit_string_clear_unused_bits(ASN1_STRING *str)
252
0
{
253
0
    str->flags &= ~0x07;
254
0
    str->flags &= ~ASN1_STRING_FLAG_BITS_LEFT;
255
0
}
256
257
void ossl_asn1_bit_string_set_unused_bits(ASN1_STRING *str, unsigned int num)
258
0
{
259
0
    ossl_asn1_bit_string_clear_unused_bits(str);
260
0
    str->flags |= ASN1_STRING_FLAG_BITS_LEFT | (num & 0x07);
261
0
}
262
263
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
264
0
{
265
0
    if (str == NULL)
266
0
        return 0;
267
0
    dst->type = str->type;
268
0
    if (!ASN1_STRING_set(dst, str->data, str->length))
269
0
        return 0;
270
    /* Copy flags but preserve embed value */
271
0
    dst->flags &= ASN1_STRING_FLAG_EMBED;
272
0
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
273
0
    return 1;
274
0
}
275
276
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
277
0
{
278
0
    ASN1_STRING *ret;
279
280
0
    if (!str)
281
0
        return NULL;
282
0
    ret = ASN1_STRING_new();
283
0
    if (ret == NULL)
284
0
        return NULL;
285
0
    if (!ASN1_STRING_copy(ret, str)) {
286
0
        ASN1_STRING_free(ret);
287
0
        return NULL;
288
0
    }
289
0
    return ret;
290
0
}
291
292
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
293
0
{
294
0
    unsigned char *c;
295
0
    const char *data = _data;
296
0
    size_t len;
297
298
0
    if (len_in < -1) {
299
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
300
0
        return 0;
301
0
    }
302
0
    if (len_in == -1) {
303
0
        if (data == NULL)
304
0
            return 0;
305
0
        len = strlen(data);
306
0
    } else {
307
0
        len = (size_t)len_in;
308
0
    }
309
    /*
310
     * Verify that the length fits within an integer for assignment to
311
     * str->length below.  The additional 1 is subtracted to allow for the
312
     * '\0' terminator even though this isn't strictly necessary.
313
     */
314
0
    if (len > INT_MAX - 1) {
315
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
316
0
        return 0;
317
0
    }
318
319
0
    if ((str->flags & ASN1_STRING_FLAG_DATA_NOT_OWNED)) {
320
0
        str->data = NULL;
321
0
        str->length = 0;
322
0
        str->flags &= ~ASN1_STRING_FLAG_DATA_NOT_OWNED;
323
0
    }
324
325
0
    if ((size_t)str->length <= len || str->data == NULL) {
326
0
        c = str->data;
327
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
328
        /* No NUL terminator in fuzzing builds */
329
0
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
330
#else
331
        str->data = OPENSSL_realloc(c, len + 1);
332
#endif
333
0
        if (str->data == NULL) {
334
0
            str->data = c;
335
0
            return 0;
336
0
        }
337
0
    }
338
0
    str->length = (int)len;
339
0
    if (data != NULL) {
340
0
        memcpy(str->data, data, len);
341
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
342
        /* Set the unused byte to something non NUL and printable. */
343
0
        if (len == 0)
344
0
            str->data[len] = '~';
345
#else
346
        /*
347
         * Add a NUL terminator. This should not be necessary - but we add it as
348
         * a safety precaution
349
         */
350
        str->data[len] = '\0';
351
#endif
352
0
    }
353
0
    ossl_asn1_bit_string_clear_unused_bits(str);
354
355
0
    return 1;
356
0
}
357
358
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
359
0
{
360
0
    if (!(str->flags & ASN1_STRING_FLAG_DATA_NOT_OWNED)) {
361
0
        OPENSSL_clear_free(str->data, str->length);
362
0
    }
363
0
    str->flags &= ~ASN1_STRING_FLAG_DATA_NOT_OWNED;
364
0
    str->data = data;
365
0
    str->length = len;
366
0
}
367
368
ASN1_STRING *ASN1_STRING_new(void)
369
0
{
370
0
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
371
0
}
372
373
ASN1_STRING *ASN1_STRING_type_new(int type)
374
0
{
375
0
    ASN1_STRING *ret;
376
377
0
    ret = OPENSSL_zalloc(sizeof(*ret));
378
0
    if (ret == NULL)
379
0
        return NULL;
380
0
    ret->type = type;
381
0
    return ret;
382
0
}
383
384
ASN1_STRING *ASN1_STRING_new_not_owned(int type, const uint8_t *data,
385
    size_t length)
386
0
{
387
0
    ASN1_STRING *ret;
388
389
0
    if (type == V_ASN1_BIT_STRING)
390
0
        return NULL;
391
392
0
    if (data == NULL || length == 0)
393
0
        return NULL;
394
395
0
    if (length > INT_MAX)
396
0
        return NULL;
397
398
0
    ret = OPENSSL_zalloc(sizeof(*ret));
399
0
    if (ret == NULL)
400
0
        return NULL;
401
402
0
    ret->type = type;
403
0
    ret->data = (unsigned char *)data;
404
0
    ret->length = (int)length;
405
0
    ret->flags |= ASN1_STRING_FLAG_DATA_NOT_OWNED;
406
407
0
    return ret;
408
0
}
409
410
void ossl_asn1_string_free_internal(ASN1_STRING *a, int clear, int embed)
411
0
{
412
0
    if (a == NULL)
413
0
        return;
414
415
0
    if ((a->flags & ASN1_STRING_FLAG_DATA_NOT_OWNED)) {
416
0
        a->data = NULL;
417
0
        a->length = 0;
418
0
        a->flags &= ~ASN1_STRING_FLAG_DATA_NOT_OWNED;
419
0
    }
420
421
0
    if (!(a->flags & ASN1_STRING_FLAG_NDEF)) {
422
0
        if (clear)
423
0
            OPENSSL_clear_free(a->data, a->length);
424
0
        else
425
0
            OPENSSL_free(a->data);
426
0
    }
427
    /*
428
     * TODO(beck): Add an assert here to verify that the embed arg is
429
     * always set to match the flag, and then get rid of the arg.
430
     */
431
0
    if (!embed && !(a->flags & ASN1_STRING_FLAG_EMBED)) {
432
0
        if (clear)
433
0
            OPENSSL_clear_free(a, sizeof(*a));
434
0
        else
435
0
            OPENSSL_free(a);
436
0
    }
437
0
}
438
439
void ASN1_STRING_free(ASN1_STRING *a)
440
0
{
441
0
    if (a == NULL)
442
0
        return;
443
444
0
    ossl_asn1_string_free_internal(a, 0, a->flags & ASN1_STRING_FLAG_EMBED);
445
0
}
446
447
void ASN1_STRING_clear_free(ASN1_STRING *a)
448
0
{
449
0
    if (a == NULL)
450
0
        return;
451
452
0
    ossl_asn1_string_free_internal(a, 1, a->flags & ASN1_STRING_FLAG_EMBED);
453
0
}
454
455
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
456
0
{
457
0
    int i;
458
459
0
    i = (a->length - b->length);
460
0
    if (i == 0) {
461
0
        if (a->length != 0)
462
0
            i = memcmp(a->data, b->data, a->length);
463
0
        if (i == 0)
464
0
            return a->type - b->type;
465
0
        else
466
0
            return i;
467
0
    } else {
468
0
        return i;
469
0
    }
470
0
}
471
472
int ASN1_STRING_length(const ASN1_STRING *x)
473
0
{
474
0
    return x->length;
475
0
}
476
477
#ifndef OPENSSL_NO_DEPRECATED_3_0
478
void ASN1_STRING_length_set(ASN1_STRING *x, int len)
479
0
{
480
0
    x->length = len;
481
0
}
482
#endif
483
484
int ASN1_STRING_type(const ASN1_STRING *x)
485
0
{
486
0
    return x->type;
487
0
}
488
489
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
490
0
{
491
0
    return x->data;
492
0
}
493
494
/* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */
495
char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
496
    const char *sep, size_t max_len)
497
0
{
498
0
    int i;
499
0
    ASN1_UTF8STRING *current;
500
0
    size_t length = 0, sep_len;
501
0
    char *result = NULL;
502
0
    char *p;
503
504
0
    if (sep == NULL)
505
0
        sep = "";
506
0
    sep_len = strlen(sep);
507
508
0
    for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
509
0
        current = sk_ASN1_UTF8STRING_value(text, i);
510
0
        if (i > 0)
511
0
            length += sep_len;
512
0
        length += ASN1_STRING_length(current);
513
0
        if (max_len != 0 && length > max_len)
514
0
            return NULL;
515
0
    }
516
0
    if ((result = OPENSSL_malloc(length + 1)) == NULL)
517
0
        return NULL;
518
519
0
    p = result;
520
0
    for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
521
0
        current = sk_ASN1_UTF8STRING_value(text, i);
522
0
        length = ASN1_STRING_length(current);
523
0
        if (i > 0 && sep_len > 0) {
524
0
            strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */
525
0
            p += sep_len;
526
0
        }
527
0
        strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
528
0
        p += length;
529
0
    }
530
0
    *p = '\0';
531
532
0
    return result;
533
0
}