Coverage Report

Created: 2023-06-08 06:41

/src/openssl30/crypto/asn1/asn1_lib.c
Line
Count
Source (jump to first uncovered line)
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
25.5M
{
49
25.5M
    int i, ret;
50
25.5M
    long len;
51
25.5M
    const unsigned char *p = *pp;
52
25.5M
    int tag, xclass, inf;
53
25.5M
    long max = omax;
54
55
25.5M
    if (omax <= 0) {
56
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57
0
        return 0x80;
58
0
    }
59
25.5M
    ret = (*p & V_ASN1_CONSTRUCTED);
60
25.5M
    xclass = (*p & V_ASN1_PRIVATE);
61
25.5M
    i = *p & V_ASN1_PRIMITIVE_TAG;
62
25.5M
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63
28.5k
        p++;
64
28.5k
        if (--max == 0)
65
26
            goto err;
66
28.5k
        len = 0;
67
57.4k
        while (*p & 0x80) {
68
29.0k
            len <<= 7L;
69
29.0k
            len |= *(p++) & 0x7f;
70
29.0k
            if (--max == 0)
71
89
                goto err;
72
29.0k
            if (len > (INT_MAX >> 7L))
73
70
                goto err;
74
29.0k
        }
75
28.4k
        len <<= 7L;
76
28.4k
        len |= *(p++) & 0x7f;
77
28.4k
        tag = (int)len;
78
28.4k
        if (--max == 0)
79
76
            goto err;
80
25.5M
    } else {
81
25.5M
        tag = i;
82
25.5M
        p++;
83
25.5M
        if (--max == 0)
84
498
            goto err;
85
25.5M
    }
86
25.5M
    *ptag = tag;
87
25.5M
    *pclass = xclass;
88
25.5M
    if (!asn1_get_length(&p, &inf, plength, max))
89
5.06k
        goto err;
90
91
25.5M
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
92
76
        goto err;
93
94
25.5M
    if (*plength > (omax - (p - *pp))) {
95
1.38M
        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
1.38M
        ret |= 0x80;
101
1.38M
    }
102
25.5M
    *pp = p;
103
25.5M
    return ret | inf;
104
5.90k
 err:
105
5.90k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106
5.90k
    return 0x80;
107
25.5M
}
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
25.5M
{
119
25.5M
    const unsigned char *p = *pp;
120
25.5M
    unsigned long ret = 0;
121
25.5M
    int i;
122
123
25.5M
    if (max-- < 1)
124
0
        return 0;
125
25.5M
    if (*p == 0x80) {
126
1.95M
        *inf = 1;
127
1.95M
        p++;
128
23.5M
    } else {
129
23.5M
        *inf = 0;
130
23.5M
        i = *p & 0x7f;
131
23.5M
        if (*p++ & 0x80) {
132
210k
            if (max < i + 1)
133
4.94k
                return 0;
134
            /* Skip leading zeroes */
135
254k
            while (i > 0 && *p == 0) {
136
49.0k
                p++;
137
49.0k
                i--;
138
49.0k
            }
139
205k
            if (i > (int)sizeof(long))
140
58
                return 0;
141
480k
            while (i > 0) {
142
274k
                ret <<= 8;
143
274k
                ret |= *p++;
144
274k
                i--;
145
274k
            }
146
205k
            if (ret > LONG_MAX)
147
65
                return 0;
148
23.3M
        } else {
149
23.3M
            ret = i;
150
23.3M
        }
151
23.5M
    }
152
25.5M
    *pp = p;
153
25.5M
    *rl = (long)ret;
154
25.5M
    return 1;
155
25.5M
}
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
1.43M
{
163
1.43M
    unsigned char *p = *pp;
164
1.43M
    int i, ttag;
165
166
1.43M
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167
1.43M
    i |= (xclass & V_ASN1_PRIVATE);
168
1.43M
    if (tag < 31) {
169
1.43M
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170
1.43M
    } else {
171
261
        *(p++) = i | V_ASN1_PRIMITIVE_TAG;
172
924
        for (i = 0, ttag = tag; ttag > 0; i++)
173
663
            ttag >>= 7;
174
261
        ttag = i;
175
924
        while (i-- > 0) {
176
663
            p[i] = tag & 0x7f;
177
663
            if (i != (ttag - 1))
178
402
                p[i] |= 0x80;
179
663
            tag >>= 7;
180
663
        }
181
261
        p += ttag;
182
261
    }
183
1.43M
    if (constructed == 2)
184
0
        *(p++) = 0x80;
185
1.43M
    else
186
1.43M
        asn1_put_length(&p, length);
187
1.43M
    *pp = p;
188
1.43M
}
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
1.43M
{
202
1.43M
    unsigned char *p = *pp;
203
1.43M
    int i, len;
204
205
1.43M
    if (length <= 127) {
206
1.37M
        *(p++) = (unsigned char)length;
207
1.37M
    } else {
208
57.4k
        len = length;
209
165k
        for (i = 0; len > 0; i++)
210
107k
            len >>= 8;
211
57.4k
        *(p++) = i | 0x80;
212
57.4k
        len = i;
213
165k
        while (i-- > 0) {
214
107k
            p[i] = length & 0xff;
215
107k
            length >>= 8;
216
107k
        }
217
57.4k
        p += len;
218
57.4k
    }
219
1.43M
    *pp = p;
220
1.43M
}
221
222
int ASN1_object_size(int constructed, int length, int tag)
223
7.82M
{
224
7.82M
    int ret = 1;
225
226
7.82M
    if (length < 0)
227
0
        return -1;
228
7.82M
    if (tag >= 31) {
229
8.31k
        while (tag > 0) {
230
5.60k
            tag >>= 7;
231
5.60k
            ret++;
232
5.60k
        }
233
2.71k
    }
234
7.82M
    if (constructed == 2) {
235
0
        ret += 3;
236
7.82M
    } else {
237
7.82M
        ret++;
238
7.82M
        if (length > 127) {
239
373k
            int tmplen = length;
240
1.05M
            while (tmplen > 0) {
241
680k
                tmplen >>= 8;
242
680k
                ret++;
243
680k
            }
244
373k
        }
245
7.82M
    }
246
7.82M
    if (ret >= INT_MAX - length)
247
0
        return -1;
248
7.82M
    return ret + length;
249
7.82M
}
250
251
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
252
60.3k
{
253
60.3k
    if (str == NULL)
254
0
        return 0;
255
60.3k
    dst->type = str->type;
256
60.3k
    if (!ASN1_STRING_set(dst, str->data, str->length))
257
0
        return 0;
258
    /* Copy flags but preserve embed value */
259
60.3k
    dst->flags &= ASN1_STRING_FLAG_EMBED;
260
60.3k
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
261
60.3k
    return 1;
262
60.3k
}
263
264
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
265
16.4k
{
266
16.4k
    ASN1_STRING *ret;
267
268
16.4k
    if (!str)
269
0
        return NULL;
270
16.4k
    ret = ASN1_STRING_new();
271
16.4k
    if (ret == NULL)
272
0
        return NULL;
273
16.4k
    if (!ASN1_STRING_copy(ret, str)) {
274
0
        ASN1_STRING_free(ret);
275
0
        return NULL;
276
0
    }
277
16.4k
    return ret;
278
16.4k
}
279
280
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
281
1.33M
{
282
1.33M
    unsigned char *c;
283
1.33M
    const char *data = _data;
284
1.33M
    size_t len;
285
286
1.33M
    if (len_in < 0) {
287
19.9k
        if (data == NULL)
288
0
            return 0;
289
19.9k
        len = strlen(data);
290
1.31M
    } else {
291
1.31M
        len = (size_t)len_in;
292
1.31M
    }
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
1.33M
    if (len > INT_MAX - 1) {
299
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
300
0
        return 0;
301
0
    }
302
1.33M
    if ((size_t)str->length <= len || str->data == NULL) {
303
1.33M
        c = str->data;
304
1.33M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
305
        /* No NUL terminator in fuzzing builds */
306
1.33M
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
307
#else
308
        str->data = OPENSSL_realloc(c, len + 1);
309
#endif
310
1.33M
        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
1.33M
    }
316
1.33M
    str->length = len;
317
1.33M
    if (data != NULL) {
318
303k
        memcpy(str->data, data, len);
319
303k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
320
        /* Set the unused byte to something non NUL and printable. */
321
303k
        if (len == 0)
322
21.9k
            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
303k
    }
331
1.33M
    return 1;
332
1.33M
}
333
334
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
335
0
{
336
0
    OPENSSL_free(str->data);
337
0
    str->data = data;
338
0
    str->length = len;
339
0
}
340
341
ASN1_STRING *ASN1_STRING_new(void)
342
16.4k
{
343
16.4k
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
344
16.4k
}
345
346
ASN1_STRING *ASN1_STRING_type_new(int type)
347
1.64M
{
348
1.64M
    ASN1_STRING *ret;
349
350
1.64M
    ret = OPENSSL_zalloc(sizeof(*ret));
351
1.64M
    if (ret == NULL) {
352
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
353
0
        return NULL;
354
0
    }
355
1.64M
    ret->type = type;
356
1.64M
    return ret;
357
1.64M
}
358
359
void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
360
1.67M
{
361
1.67M
    if (a == NULL)
362
0
        return;
363
1.67M
    if (!(a->flags & ASN1_STRING_FLAG_NDEF))
364
1.67M
        OPENSSL_free(a->data);
365
1.67M
    if (embed == 0)
366
1.64M
        OPENSSL_free(a);
367
1.67M
}
368
369
void ASN1_STRING_free(ASN1_STRING *a)
370
487k
{
371
487k
    if (a == NULL)
372
249k
        return;
373
238k
    ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
374
238k
}
375
376
void ASN1_STRING_clear_free(ASN1_STRING *a)
377
0
{
378
0
    if (a == NULL)
379
0
        return;
380
0
    if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
381
0
        OPENSSL_cleanse(a->data, a->length);
382
0
    ASN1_STRING_free(a);
383
0
}
384
385
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
386
125
{
387
125
    int i;
388
389
125
    i = (a->length - b->length);
390
125
    if (i == 0) {
391
112
        if (a->length != 0)
392
112
            i = memcmp(a->data, b->data, a->length);
393
112
        if (i == 0)
394
80
            return a->type - b->type;
395
32
        else
396
32
            return i;
397
112
    } else {
398
13
        return i;
399
13
    }
400
125
}
401
402
int ASN1_STRING_length(const ASN1_STRING *x)
403
2.45k
{
404
2.45k
    return x->length;
405
2.45k
}
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
2.45k
{
421
2.45k
    return x->data;
422
2.45k
}
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
}