Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/asn1/a_object.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 "crypto/ctype.h"
13
#include "internal/cryptlib.h"
14
#include <openssl/buffer.h>
15
#include <openssl/asn1.h>
16
#include <openssl/objects.h>
17
#include <openssl/bn.h>
18
#include "crypto/asn1.h"
19
#include "asn1_local.h"
20
21
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
22
0
{
23
0
    unsigned char *p, *allocated = NULL;
24
0
    int objsize;
25
26
0
    if ((a == NULL) || (a->data == NULL))
27
0
        return 0;
28
29
0
    objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
30
0
    if (pp == NULL || objsize == -1)
31
0
        return objsize;
32
33
0
    if (*pp == NULL) {
34
0
        if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
35
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
36
0
            return 0;
37
0
        }
38
0
    } else {
39
0
        p = *pp;
40
0
    }
41
42
0
    ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
43
0
    memcpy(p, a->data, a->length);
44
45
    /*
46
     * If a new buffer was allocated, just return it back.
47
     * If not, return the incremented buffer pointer.
48
     */
49
0
    *pp = allocated != NULL ? allocated : p + a->length;
50
0
    return objsize;
51
0
}
52
53
int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
54
186k
{
55
186k
    int i, first, len = 0, c, use_bn;
56
186k
    char ftmp[24], *tmp = ftmp;
57
186k
    int tmpsize = sizeof(ftmp);
58
186k
    const char *p;
59
186k
    unsigned long l;
60
186k
    BIGNUM *bl = NULL;
61
62
186k
    if (num == 0)
63
0
        return 0;
64
186k
    else if (num == -1)
65
186k
        num = strlen(buf);
66
67
186k
    p = buf;
68
186k
    c = *(p++);
69
186k
    num--;
70
186k
    if ((c >= '0') && (c <= '2')) {
71
186k
        first = c - '0';
72
186k
    } else {
73
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);
74
0
        goto err;
75
0
    }
76
77
186k
    if (num <= 0) {
78
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);
79
0
        goto err;
80
0
    }
81
186k
    c = *(p++);
82
186k
    num--;
83
1.67M
    for (;;) {
84
1.67M
        if (num <= 0)
85
186k
            break;
86
1.49M
        if ((c != '.') && (c != ' ')) {
87
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);
88
0
            goto err;
89
0
        }
90
1.49M
        l = 0;
91
1.49M
        use_bn = 0;
92
4.10M
        for (;;) {
93
4.10M
            if (num <= 0)
94
186k
                break;
95
3.91M
            num--;
96
3.91M
            c = *(p++);
97
3.91M
            if ((c == ' ') || (c == '.'))
98
1.30M
                break;
99
2.60M
            if (!ossl_isdigit(c)) {
100
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);
101
0
                goto err;
102
0
            }
103
2.60M
            if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
104
0
                use_bn = 1;
105
0
                if (bl == NULL)
106
0
                    bl = BN_new();
107
0
                if (bl == NULL || !BN_set_word(bl, l))
108
0
                    goto err;
109
0
            }
110
2.60M
            if (use_bn) {
111
0
                if (!BN_mul_word(bl, 10L)
112
0
                    || !BN_add_word(bl, c - '0'))
113
0
                    goto err;
114
0
            } else
115
2.60M
                l = l * 10L + (long)(c - '0');
116
2.60M
        }
117
1.49M
        if (len == 0) {
118
186k
            if ((first < 2) && (l >= 40)) {
119
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE);
120
0
                goto err;
121
0
            }
122
186k
            if (use_bn) {
123
0
                if (!BN_add_word(bl, first * 40))
124
0
                    goto err;
125
0
            } else
126
186k
                l += (long)first *40;
127
186k
        }
128
1.49M
        i = 0;
129
1.49M
        if (use_bn) {
130
0
            int blsize;
131
0
            blsize = BN_num_bits(bl);
132
0
            blsize = (blsize + 6) / 7;
133
0
            if (blsize > tmpsize) {
134
0
                if (tmp != ftmp)
135
0
                    OPENSSL_free(tmp);
136
0
                tmpsize = blsize + 32;
137
0
                tmp = OPENSSL_malloc(tmpsize);
138
0
                if (tmp == NULL) {
139
0
                    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
140
0
                    goto err;
141
0
                }
142
0
            }
143
0
            while (blsize--) {
144
0
                BN_ULONG t = BN_div_word(bl, 0x80L);
145
0
                if (t == (BN_ULONG)-1)
146
0
                    goto err;
147
0
                tmp[i++] = (unsigned char)t;
148
0
            }
149
1.49M
        } else {
150
151
1.67M
            for (;;) {
152
1.67M
                tmp[i++] = (unsigned char)l & 0x7f;
153
1.67M
                l >>= 7L;
154
1.67M
                if (l == 0L)
155
1.49M
                    break;
156
1.67M
            }
157
158
1.49M
        }
159
1.49M
        if (out != NULL) {
160
745k
            if (len + i > olen) {
161
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
162
0
                goto err;
163
0
            }
164
838k
            while (--i > 0)
165
93.1k
                out[len++] = tmp[i] | 0x80;
166
745k
            out[len++] = tmp[0];
167
745k
        } else
168
745k
            len += i;
169
1.49M
    }
170
186k
    if (tmp != ftmp)
171
0
        OPENSSL_free(tmp);
172
186k
    BN_free(bl);
173
186k
    return len;
174
0
 err:
175
0
    if (tmp != ftmp)
176
0
        OPENSSL_free(tmp);
177
0
    BN_free(bl);
178
0
    return 0;
179
186k
}
180
181
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
182
6.68M
{
183
6.68M
    return OBJ_obj2txt(buf, buf_len, a, 0);
184
6.68M
}
185
186
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
187
1.81M
{
188
1.81M
    char buf[80], *p = buf;
189
1.81M
    int i;
190
191
1.81M
    if ((a == NULL) || (a->data == NULL))
192
0
        return BIO_write(bp, "NULL", 4);
193
1.81M
    i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
194
1.81M
    if (i > (int)(sizeof(buf) - 1)) {
195
14.8k
        if (i > INT_MAX - 1) {  /* catch an integer overflow */
196
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
197
0
            return -1;
198
0
        }
199
14.8k
        if ((p = OPENSSL_malloc(i + 1)) == NULL) {
200
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
201
0
            return -1;
202
0
        }
203
14.8k
        i2t_ASN1_OBJECT(p, i + 1, a);
204
14.8k
    }
205
1.81M
    if (i <= 0) {
206
3.79k
        i = BIO_write(bp, "<INVALID>", 9);
207
3.79k
        i += BIO_dump(bp, (const char *)a->data, a->length);
208
3.79k
        return i;
209
3.79k
    }
210
1.81M
    BIO_write(bp, p, i);
211
1.81M
    if (p != buf)
212
14.8k
        OPENSSL_free(p);
213
1.81M
    return i;
214
1.81M
}
215
216
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
217
                             long length)
218
1.35M
{
219
1.35M
    const unsigned char *p;
220
1.35M
    long len;
221
1.35M
    int tag, xclass;
222
1.35M
    int inf, i;
223
1.35M
    ASN1_OBJECT *ret = NULL;
224
1.35M
    p = *pp;
225
1.35M
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
226
1.35M
    if (inf & 0x80) {
227
10.8k
        i = ASN1_R_BAD_OBJECT_HEADER;
228
10.8k
        goto err;
229
10.8k
    }
230
231
1.34M
    if (tag != V_ASN1_OBJECT) {
232
0
        i = ASN1_R_EXPECTING_AN_OBJECT;
233
0
        goto err;
234
0
    }
235
1.34M
    ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
236
1.34M
    if (ret)
237
1.29M
        *pp = p;
238
1.34M
    return ret;
239
10.8k
 err:
240
10.8k
    ERR_raise(ERR_LIB_ASN1, i);
241
10.8k
    return NULL;
242
1.34M
}
243
244
ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
245
                                  long len)
246
26.5M
{
247
26.5M
    ASN1_OBJECT *ret = NULL, tobj;
248
26.5M
    const unsigned char *p;
249
26.5M
    unsigned char *data;
250
26.5M
    int i, length;
251
252
    /*
253
     * Sanity check OID encoding. Need at least one content octet. MSB must
254
     * be clear in the last octet. can't have leading 0x80 in subidentifiers,
255
     * see: X.690 8.19.2
256
     */
257
26.5M
    if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
258
26.5M
        p[len - 1] & 0x80) {
259
62.3k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
260
62.3k
        return NULL;
261
62.3k
    }
262
    /* Now 0 < len <= INT_MAX, so the cast is safe. */
263
26.4M
    length = (int)len;
264
    /*
265
     * Try to lookup OID in table: these are all valid encodings so if we get
266
     * a match we know the OID is valid.
267
     */
268
26.4M
    tobj.nid = NID_undef;
269
26.4M
    tobj.data = p;
270
26.4M
    tobj.length = length;
271
26.4M
    tobj.flags = 0;
272
26.4M
    i = OBJ_obj2nid(&tobj);
273
26.4M
    if (i != NID_undef) {
274
        /*
275
         * Return shared registered OID object: this improves efficiency
276
         * because we don't have to return a dynamically allocated OID
277
         * and NID lookups can use the cached value.
278
         */
279
8.36M
        ret = OBJ_nid2obj(i);
280
8.36M
        if (a) {
281
8.26M
            ASN1_OBJECT_free(*a);
282
8.26M
            *a = ret;
283
8.26M
        }
284
8.36M
        *pp += len;
285
8.36M
        return ret;
286
8.36M
    }
287
227M
    for (i = 0; i < length; i++, p++) {
288
209M
        if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
289
17.5k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
290
17.5k
            return NULL;
291
17.5k
        }
292
209M
    }
293
294
18.0M
    if ((a == NULL) || ((*a) == NULL) ||
295
18.0M
        !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
296
17.9M
        if ((ret = ASN1_OBJECT_new()) == NULL)
297
0
            return NULL;
298
17.9M
    } else {
299
89.3k
        ret = (*a);
300
89.3k
    }
301
302
18.0M
    p = *pp;
303
    /* detach data from object */
304
18.0M
    data = (unsigned char *)ret->data;
305
18.0M
    ret->data = NULL;
306
    /* once detached we can change it */
307
18.0M
    if ((data == NULL) || (ret->length < length)) {
308
18.0M
        ret->length = 0;
309
18.0M
        OPENSSL_free(data);
310
18.0M
        data = OPENSSL_malloc(length);
311
18.0M
        if (data == NULL) {
312
0
            i = ERR_R_MALLOC_FAILURE;
313
0
            goto err;
314
0
        }
315
18.0M
        ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
316
18.0M
    }
317
18.0M
    memcpy(data, p, length);
318
    /* If there are dynamic strings, free them here, and clear the flag */
319
18.0M
    if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
320
0
        OPENSSL_free((char *)ret->sn);
321
0
        OPENSSL_free((char *)ret->ln);
322
0
        ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
323
0
    }
324
    /* reattach data to object, after which it remains const */
325
18.0M
    ret->data = data;
326
18.0M
    ret->length = length;
327
18.0M
    ret->sn = NULL;
328
18.0M
    ret->ln = NULL;
329
    /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
330
18.0M
    p += length;
331
332
18.0M
    if (a != NULL)
333
18.0M
        (*a) = ret;
334
18.0M
    *pp = p;
335
18.0M
    return ret;
336
0
 err:
337
0
    ERR_raise(ERR_LIB_ASN1, i);
338
0
    if ((a == NULL) || (*a != ret))
339
0
        ASN1_OBJECT_free(ret);
340
0
    return NULL;
341
18.0M
}
342
343
ASN1_OBJECT *ASN1_OBJECT_new(void)
344
36.9M
{
345
36.9M
    ASN1_OBJECT *ret;
346
347
36.9M
    ret = OPENSSL_zalloc(sizeof(*ret));
348
36.9M
    if (ret == NULL) {
349
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
350
0
        return NULL;
351
0
    }
352
36.9M
    ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
353
36.9M
    return ret;
354
36.9M
}
355
356
void ASN1_OBJECT_free(ASN1_OBJECT *a)
357
61.1M
{
358
61.1M
    if (a == NULL)
359
3.81M
        return;
360
57.3M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
361
18.9M
#ifndef CONST_STRICT
362
        /*
363
         * Disable purely for compile-time strict const checking.  Doing this
364
         * on a "real" compile will cause memory leaks
365
         */
366
18.9M
        OPENSSL_free((void*)a->sn);
367
18.9M
        OPENSSL_free((void*)a->ln);
368
18.9M
#endif
369
18.9M
        a->sn = a->ln = NULL;
370
18.9M
    }
371
57.3M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
372
36.9M
        OPENSSL_free((void*)a->data);
373
36.9M
        a->data = NULL;
374
36.9M
        a->length = 0;
375
36.9M
    }
376
57.3M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
377
36.9M
        OPENSSL_free(a);
378
57.3M
}
379
380
ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
381
                                const char *sn, const char *ln)
382
0
{
383
0
    ASN1_OBJECT o;
384
385
0
    o.sn = sn;
386
0
    o.ln = ln;
387
0
    o.data = data;
388
0
    o.nid = nid;
389
0
    o.length = len;
390
0
    o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
391
0
        ASN1_OBJECT_FLAG_DYNAMIC_DATA;
392
0
    return OBJ_dup(&o);
393
0
}