Coverage Report

Created: 2026-10-28 06:28

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