Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/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 OpenSSL license (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 if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
28
0
        (*p) += 2;
29
0
        return 1;
30
0
    }
31
0
    return 0;
32
0
}
33
34
int ASN1_check_infinite_end(unsigned char **p, long len)
35
0
{
36
0
    return _asn1_check_infinite_end((const unsigned char **)p, len);
37
0
}
38
39
int ASN1_const_check_infinite_end(const unsigned char **p, long len)
40
0
{
41
0
    return _asn1_check_infinite_end(p, len);
42
0
}
43
44
int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
45
                    int *pclass, long omax)
46
136M
{
47
136M
    int i, ret;
48
136M
    long l;
49
136M
    const unsigned char *p = *pp;
50
136M
    int tag, xclass, inf;
51
136M
    long max = omax;
52
53
136M
    if (!max)
54
6.06k
        goto err;
55
136M
    ret = (*p & V_ASN1_CONSTRUCTED);
56
136M
    xclass = (*p & V_ASN1_PRIVATE);
57
136M
    i = *p & V_ASN1_PRIMITIVE_TAG;
58
136M
    if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
59
180k
        p++;
60
180k
        if (--max == 0)
61
1.92k
            goto err;
62
178k
        l = 0;
63
298k
        while (*p & 0x80) {
64
122k
            l <<= 7L;
65
122k
            l |= *(p++) & 0x7f;
66
122k
            if (--max == 0)
67
2.43k
                goto err;
68
120k
            if (l > (INT_MAX >> 7L))
69
1.27k
                goto err;
70
120k
        }
71
175k
        l <<= 7L;
72
175k
        l |= *(p++) & 0x7f;
73
175k
        tag = (int)l;
74
175k
        if (--max == 0)
75
2.33k
            goto err;
76
136M
    } else {
77
136M
        tag = i;
78
136M
        p++;
79
136M
        if (--max == 0)
80
17.0k
            goto err;
81
136M
    }
82
136M
    *ptag = tag;
83
136M
    *pclass = xclass;
84
136M
    if (!asn1_get_length(&p, &inf, plength, max))
85
37.7k
        goto err;
86
87
136M
    if (inf && !(ret & V_ASN1_CONSTRUCTED))
88
2.52k
        goto err;
89
90
136M
    if (*plength > (omax - (p - *pp))) {
91
619k
        ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
92
        /*
93
         * Set this so that even if things are not long enough the values are
94
         * set correctly
95
         */
96
619k
        ret |= 0x80;
97
619k
    }
98
136M
    *pp = p;
99
136M
    return ret | inf;
100
71.4k
 err:
101
71.4k
    ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
102
71.4k
    return 0x80;
103
136M
}
104
105
/*
106
 * Decode a length field.
107
 * The short form is a single byte defining a length 0 - 127.
108
 * The long form is a byte 0 - 127 with the top bit set and this indicates
109
 * the number of following octets that contain the length.  These octets
110
 * are stored most significant digit first.
111
 */
112
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
113
                           long max)
114
997M
{
115
997M
    const unsigned char *p = *pp;
116
997M
    unsigned long ret = 0;
117
997M
    int i;
118
119
997M
    if (max-- < 1)
120
0
        return 0;
121
997M
    if (*p == 0x80) {
122
182M
        *inf = 1;
123
182M
        p++;
124
815M
    } else {
125
815M
        *inf = 0;
126
815M
        i = *p & 0x7f;
127
815M
        if (*p++ & 0x80) {
128
11.8M
            if (max < i + 1)
129
138k
                return 0;
130
            /* Skip leading zeroes */
131
12.6M
            while (i > 0 && *p == 0) {
132
940k
                p++;
133
940k
                i--;
134
940k
            }
135
11.7M
            if (i > (int)sizeof(long))
136
11.3k
                return 0;
137
24.7M
            while (i > 0) {
138
13.0M
                ret <<= 8;
139
13.0M
                ret |= *p++;
140
13.0M
                i--;
141
13.0M
            }
142
11.6M
            if (ret > LONG_MAX)
143
35.3k
                return 0;
144
11.6M
        } else
145
803M
            ret = i;
146
815M
    }
147
997M
    *pp = p;
148
997M
    *rl = (long)ret;
149
997M
    return 1;
150
997M
}
151
152
/*
153
 * class 0 is constructed constructed == 2 for indefinite length constructed
154
 */
155
void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
156
                     int xclass)
157
45.8M
{
158
45.8M
    unsigned char *p = *pp;
159
45.8M
    int i, ttag;
160
161
45.8M
    i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
162
45.8M
    i |= (xclass & V_ASN1_PRIVATE);
163
45.8M
    if (tag < 31)
164
45.6M
        *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
165
222k
    else {
166
222k
        *(p++) = i | V_ASN1_PRIMITIVE_TAG;
167
669k
        for (i = 0, ttag = tag; ttag > 0; i++)
168
446k
            ttag >>= 7;
169
222k
        ttag = i;
170
669k
        while (i-- > 0) {
171
446k
            p[i] = tag & 0x7f;
172
446k
            if (i != (ttag - 1))
173
223k
                p[i] |= 0x80;
174
446k
            tag >>= 7;
175
446k
        }
176
222k
        p += ttag;
177
222k
    }
178
45.8M
    if (constructed == 2)
179
0
        *(p++) = 0x80;
180
45.8M
    else
181
45.8M
        asn1_put_length(&p, length);
182
45.8M
    *pp = p;
183
45.8M
}
184
185
int ASN1_put_eoc(unsigned char **pp)
186
0
{
187
0
    unsigned char *p = *pp;
188
0
    *p++ = 0;
189
0
    *p++ = 0;
190
0
    *pp = p;
191
0
    return 2;
192
0
}
193
194
static void asn1_put_length(unsigned char **pp, int length)
195
45.8M
{
196
45.8M
    unsigned char *p = *pp;
197
45.8M
    int i, l;
198
45.8M
    if (length <= 127)
199
45.2M
        *(p++) = (unsigned char)length;
200
562k
    else {
201
562k
        l = length;
202
1.52M
        for (i = 0; l > 0; i++)
203
961k
            l >>= 8;
204
562k
        *(p++) = i | 0x80;
205
562k
        l = i;
206
1.52M
        while (i-- > 0) {
207
961k
            p[i] = length & 0xff;
208
961k
            length >>= 8;
209
961k
        }
210
562k
        p += l;
211
562k
    }
212
45.8M
    *pp = p;
213
45.8M
}
214
215
int ASN1_object_size(int constructed, int length, int tag)
216
186M
{
217
186M
    int ret = 1;
218
186M
    if (length < 0)
219
0
        return -1;
220
186M
    if (tag >= 31) {
221
2.89M
        while (tag > 0) {
222
1.92M
            tag >>= 7;
223
1.92M
            ret++;
224
1.92M
        }
225
966k
    }
226
186M
    if (constructed == 2) {
227
0
        ret += 3;
228
186M
    } else {
229
186M
        ret++;
230
186M
        if (length > 127) {
231
1.94M
            int tmplen = length;
232
5.28M
            while (tmplen > 0) {
233
3.33M
                tmplen >>= 8;
234
3.33M
                ret++;
235
3.33M
            }
236
1.94M
        }
237
186M
    }
238
186M
    if (ret >= INT_MAX - length)
239
0
        return -1;
240
186M
    return ret + length;
241
186M
}
242
243
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
244
4.67M
{
245
4.67M
    if (str == NULL)
246
0
        return 0;
247
4.67M
    dst->type = str->type;
248
4.67M
    if (!ASN1_STRING_set(dst, str->data, str->length))
249
0
        return 0;
250
    /* Copy flags but preserve embed value */
251
4.67M
    dst->flags &= ASN1_STRING_FLAG_EMBED;
252
4.67M
    dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
253
4.67M
    return 1;
254
4.67M
}
255
256
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
257
30.4k
{
258
30.4k
    ASN1_STRING *ret;
259
30.4k
    if (!str)
260
0
        return NULL;
261
30.4k
    ret = ASN1_STRING_new();
262
30.4k
    if (ret == NULL)
263
0
        return NULL;
264
30.4k
    if (!ASN1_STRING_copy(ret, str)) {
265
0
        ASN1_STRING_free(ret);
266
0
        return NULL;
267
0
    }
268
30.4k
    return ret;
269
30.4k
}
270
271
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
272
23.0M
{
273
23.0M
    unsigned char *c;
274
23.0M
    const char *data = _data;
275
23.0M
    size_t len;
276
277
23.0M
    if (len_in < 0) {
278
34.8k
        if (data == NULL)
279
0
            return 0;
280
34.8k
        len = strlen(data);
281
22.9M
    } else {
282
22.9M
        len = (size_t)len_in;
283
22.9M
    }
284
    /*
285
     * Verify that the length fits within an integer for assignment to
286
     * str->length below.  The additional 1 is subtracted to allow for the
287
     * '\0' terminator even though this isn't strictly necessary.
288
     */
289
23.0M
    if (len > INT_MAX - 1) {
290
0
        ASN1err(0, ASN1_R_TOO_LARGE);
291
0
        return 0;
292
0
    }
293
23.0M
    if ((size_t)str->length <= len || str->data == NULL) {
294
23.0M
        c = str->data;
295
23.0M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
296
        /* No NUL terminator in fuzzing builds */
297
23.0M
        str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
298
#else
299
        str->data = OPENSSL_realloc(c, len + 1);
300
#endif
301
23.0M
        if (str->data == NULL) {
302
0
            ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
303
0
            str->data = c;
304
0
            return 0;
305
0
        }
306
23.0M
    }
307
23.0M
    str->length = len;
308
23.0M
    if (data != NULL) {
309
18.7M
        memcpy(str->data, data, len);
310
18.7M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
311
        /* Set the unused byte to something non NUL and printable. */
312
18.7M
        if (len == 0)
313
5.36M
            str->data[len] = '~';
314
#else
315
        /*
316
         * Add a NUL terminator. This should not be necessary - but we add it as
317
         * a safety precaution
318
         */
319
        str->data[len] = '\0';
320
#endif
321
18.7M
    }
322
23.0M
    return 1;
323
23.0M
}
324
325
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
326
3.86M
{
327
3.86M
    OPENSSL_free(str->data);
328
3.86M
    str->data = data;
329
3.86M
    str->length = len;
330
3.86M
}
331
332
ASN1_STRING *ASN1_STRING_new(void)
333
30.8k
{
334
30.8k
    return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
335
30.8k
}
336
337
ASN1_STRING *ASN1_STRING_type_new(int type)
338
36.9M
{
339
36.9M
    ASN1_STRING *ret;
340
341
36.9M
    ret = OPENSSL_zalloc(sizeof(*ret));
342
36.9M
    if (ret == NULL) {
343
0
        ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
344
0
        return NULL;
345
0
    }
346
36.9M
    ret->type = type;
347
36.9M
    return ret;
348
36.9M
}
349
350
void asn1_string_embed_free(ASN1_STRING *a, int embed)
351
14.9M
{
352
14.9M
    if (a == NULL)
353
0
        return;
354
14.9M
    if (!(a->flags & ASN1_STRING_FLAG_NDEF))
355
14.9M
        OPENSSL_free(a->data);
356
14.9M
    if (embed == 0)
357
14.0M
        OPENSSL_free(a);
358
14.9M
}
359
360
void ASN1_STRING_free(ASN1_STRING *a)
361
19.2M
{
362
19.2M
    if (a == NULL)
363
16.7M
        return;
364
2.49M
    asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
365
2.49M
}
366
367
void ASN1_STRING_clear_free(ASN1_STRING *a)
368
958
{
369
958
    if (a == NULL)
370
58
        return;
371
900
    if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
372
900
        OPENSSL_cleanse(a->data, a->length);
373
900
    ASN1_STRING_free(a);
374
900
}
375
376
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
377
1.42k
{
378
1.42k
    int i;
379
380
1.42k
    i = (a->length - b->length);
381
1.42k
    if (i == 0) {
382
1.30k
        if (a->length != 0)
383
1.27k
            i = memcmp(a->data, b->data, a->length);
384
1.30k
        if (i == 0)
385
811
            return a->type - b->type;
386
495
        else
387
495
            return i;
388
1.30k
    } else
389
117
        return i;
390
1.42k
}
391
392
int ASN1_STRING_length(const ASN1_STRING *x)
393
1.12M
{
394
1.12M
    return x->length;
395
1.12M
}
396
397
void ASN1_STRING_length_set(ASN1_STRING *x, int len)
398
0
{
399
0
    x->length = len;
400
0
}
401
402
int ASN1_STRING_type(const ASN1_STRING *x)
403
0
{
404
0
    return x->type;
405
0
}
406
407
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
408
1.17M
{
409
1.17M
    return x->data;
410
1.17M
}
411
412
# if OPENSSL_API_COMPAT < 0x10100000L
413
unsigned char *ASN1_STRING_data(ASN1_STRING *x)
414
0
{
415
0
    return x->data;
416
0
}
417
#endif