Coverage Report

Created: 2023-06-08 06:40

/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
78.4k
{
49
78.4k
    int i, ret;
50
78.4k
    long len;
51
78.4k
    const unsigned char *p = *pp;
52
78.4k
    int tag, xclass, inf;
53
78.4k
    long max = omax;
54
55
78.4k
    if (omax <= 0) {
56
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57
0
        return 0x80;
58
0
    }
59
78.4k
    ret = (*p & V_ASN1_CONSTRUCTED);
60
78.4k
    xclass = (*p & V_ASN1_PRIVATE);
61
78.4k
    i = *p & V_ASN1_PRIMITIVE_TAG;
62
78.4k
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63
1.45k
        p++;
64
1.45k
        if (--max == 0)
65
5
            goto err;
66
1.45k
        len = 0;
67
2.17k
        while (*p & 0x80) {
68
753
            len <<= 7L;
69
753
            len |= *(p++) & 0x7f;
70
753
            if (--max == 0)
71
17
                goto err;
72
736
            if (len > (INT_MAX >> 7L))
73
15
                goto err;
74
736
        }
75
1.41k
        len <<= 7L;
76
1.41k
        len |= *(p++) & 0x7f;
77
1.41k
        tag = (int)len;
78
1.41k
        if (--max == 0)
79
11
            goto err;
80
76.9k
    } else {
81
76.9k
        tag = i;
82
76.9k
        p++;
83
76.9k
        if (--max == 0)
84
49
            goto err;
85
76.9k
    }
86
78.3k
    *ptag = tag;
87
78.3k
    *pclass = xclass;
88
78.3k
    if (!asn1_get_length(&p, &inf, plength, max))
89
117
        goto err;
90
91
78.2k
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
92
1
        goto err;
93
94
78.2k
    if (*plength > (omax - (p - *pp))) {
95
206
        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
206
        ret |= 0x80;
101
206
    }
102
78.2k
    *pp = p;
103
78.2k
    return ret | inf;
104
215
 err:
105
215
    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106
215
    return 0x80;
107
78.2k
}
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
78.3k
{
119
78.3k
    const unsigned char *p = *pp;
120
78.3k
    unsigned long ret = 0;
121
78.3k
    int i;
122
123
78.3k
    if (max-- < 1)
124
0
        return 0;
125
78.3k
    if (*p == 0x80) {
126
931
        *inf = 1;
127
931
        p++;
128
77.4k
    } else {
129
77.4k
        *inf = 0;
130
77.4k
        i = *p & 0x7f;
131
77.4k
        if (*p++ & 0x80) {
132
9.37k
            if (max < i + 1)
133
20
                return 0;
134
            /* Skip leading zeroes */
135
10.9k
            while (i > 0 && *p == 0) {
136
1.60k
                p++;
137
1.60k
                i--;
138
1.60k
            }
139
9.35k
            if (i > (int)sizeof(long))
140
30
                return 0;
141
20.4k
            while (i > 0) {
142
11.0k
                ret <<= 8;
143
11.0k
                ret |= *p++;
144
11.0k
                i--;
145
11.0k
            }
146
9.32k
            if (ret > LONG_MAX)
147
67
                return 0;
148
68.0k
        } else {
149
68.0k
            ret = i;
150
68.0k
        }
151
77.4k
    }
152
78.2k
    *pp = p;
153
78.2k
    *rl = (long)ret;
154
78.2k
    return 1;
155
78.3k
}
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
333
{
163
333
    unsigned char *p = *pp;
164
333
    int i, ttag;
165
166
333
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167
333
    i |= (xclass & V_ASN1_PRIVATE);
168
333
    if (tag < 31) {
169
333
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170
333
    } 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
333
    if (constructed == 2)
184
0
        *(p++) = 0x80;
185
333
    else
186
333
        asn1_put_length(&p, length);
187
333
    *pp = p;
188
333
}
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
333
{
202
333
    unsigned char *p = *pp;
203
333
    int i, len;
204
205
333
    if (length <= 127) {
206
154
        *(p++) = (unsigned char)length;
207
179
    } else {
208
179
        len = length;
209
493
        for (i = 0; len > 0; i++)
210
314
            len >>= 8;
211
179
        *(p++) = i | 0x80;
212
179
        len = i;
213
493
        while (i-- > 0) {
214
314
            p[i] = length & 0xff;
215
314
            length >>= 8;
216
314
        }
217
179
        p += len;
218
179
    }
219
333
    *pp = p;
220
333
}
221
222
int ASN1_object_size(int constructed, int length, int tag)
223
666
{
224
666
    int ret = 1;
225
226
666
    if (length < 0)
227
0
        return -1;
228
666
    if (tag >= 31) {
229
0
        while (tag > 0) {
230
0
            tag >>= 7;
231
0
            ret++;
232
0
        }
233
0
    }
234
666
    if (constructed == 2) {
235
0
        ret += 3;
236
666
    } else {
237
666
        ret++;
238
666
        if (length > 127) {
239
358
            int tmplen = length;
240
986
            while (tmplen > 0) {
241
628
                tmplen >>= 8;
242
628
                ret++;
243
628
            }
244
358
        }
245
666
    }
246
666
    if (ret >= INT_MAX - length)
247
0
        return -1;
248
666
    return ret + length;
249
666
}
250
251
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
252
4.57k
{
253
4.57k
    if (str == NULL)
254
0
        return 0;
255
4.57k
    dst->type = str->type;
256
4.57k
    if (!ASN1_STRING_set(dst, str->data, str->length))
257
0
        return 0;
258
    /* Copy flags but preserve embed value */
259
4.57k
    dst->flags &= ASN1_STRING_FLAG_EMBED;
260
4.57k
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
261
4.57k
    return 1;
262
4.57k
}
263
264
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
265
0
{
266
0
    ASN1_STRING *ret;
267
268
0
    if (!str)
269
0
        return NULL;
270
0
    ret = ASN1_STRING_new();
271
0
    if (ret == NULL)
272
0
        return NULL;
273
0
    if (!ASN1_STRING_copy(ret, str)) {
274
0
        ASN1_STRING_free(ret);
275
0
        return NULL;
276
0
    }
277
0
    return ret;
278
0
}
279
280
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
281
9.89k
{
282
9.89k
    unsigned char *c;
283
9.89k
    const char *data = _data;
284
9.89k
    size_t len;
285
286
9.89k
    if (len_in < 0) {
287
0
        if (data == NULL)
288
0
            return 0;
289
0
        len = strlen(data);
290
9.89k
    } else {
291
9.89k
        len = (size_t)len_in;
292
9.89k
    }
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
9.89k
    if (len > INT_MAX - 1) {
299
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
300
0
        return 0;
301
0
    }
302
9.89k
    if ((size_t)str->length <= len || str->data == NULL) {
303
9.89k
        c = str->data;
304
9.89k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
305
        /* No NUL terminator in fuzzing builds */
306
9.89k
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
307
#else
308
        str->data = OPENSSL_realloc(c, len + 1);
309
#endif
310
9.89k
        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
9.89k
    }
316
9.89k
    str->length = len;
317
9.89k
    if (data != NULL) {
318
5.31k
        memcpy(str->data, data, len);
319
5.31k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
320
        /* Set the unused byte to something non NUL and printable. */
321
5.31k
        if (len == 0)
322
2
            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
5.31k
    }
331
9.89k
    return 1;
332
9.89k
}
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
0
{
343
0
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
344
0
}
345
346
ASN1_STRING *ASN1_STRING_type_new(int type)
347
8.25k
{
348
8.25k
    ASN1_STRING *ret;
349
350
8.25k
    ret = OPENSSL_zalloc(sizeof(*ret));
351
8.25k
    if (ret == NULL) {
352
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
353
0
        return NULL;
354
0
    }
355
8.25k
    ret->type = type;
356
8.25k
    return ret;
357
8.25k
}
358
359
void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
360
8.25k
{
361
8.25k
    if (a == NULL)
362
0
        return;
363
8.25k
    if (!(a->flags & ASN1_STRING_FLAG_NDEF))
364
8.25k
        OPENSSL_free(a->data);
365
8.25k
    if (embed == 0)
366
8.25k
        OPENSSL_free(a);
367
8.25k
}
368
369
void ASN1_STRING_free(ASN1_STRING *a)
370
8.25k
{
371
8.25k
    if (a == NULL)
372
0
        return;
373
8.25k
    ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
374
8.25k
}
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
0
{
387
0
    int i;
388
389
0
    i = (a->length - b->length);
390
0
    if (i == 0) {
391
0
        if (a->length != 0)
392
0
            i = memcmp(a->data, b->data, a->length);
393
0
        if (i == 0)
394
0
            return a->type - b->type;
395
0
        else
396
0
            return i;
397
0
    } else {
398
0
        return i;
399
0
    }
400
0
}
401
402
int ASN1_STRING_length(const ASN1_STRING *x)
403
0
{
404
0
    return x->length;
405
0
}
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
7.31k
{
421
7.31k
    return x->data;
422
7.31k
}
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
}