Coverage Report

Created: 2026-10-28 06:28

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