Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
{
53
0
    int i, first, len = 0, use_bn;
54
0
    char c;
55
0
    char ftmp[24], *tmp = ftmp;
56
0
    int tmpsize = sizeof(ftmp);
57
0
    const char *p;
58
0
    unsigned long l;
59
0
    BIGNUM *bl = NULL;
60
61
0
    if (num == 0) {
62
0
        return 0;
63
0
    } else if (num == -1) {
64
0
        size_t num_s = strlen(buf);
65
66
0
        if (num_s >= INT_MAX) {
67
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
68
0
            goto err;
69
0
        }
70
0
        num = (int)num_s;
71
0
    }
72
73
0
    p = buf;
74
0
    c = *(p++);
75
0
    num--;
76
0
    if ((c >= '0') && (c <= '2')) {
77
0
        first = c - '0';
78
0
    } else {
79
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);
80
0
        goto err;
81
0
    }
82
83
0
    if (num <= 0) {
84
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);
85
0
        goto err;
86
0
    }
87
0
    c = *(p++);
88
0
    num--;
89
0
    for (;;) {
90
0
        if (num <= 0)
91
0
            break;
92
0
        if ((c != '.') && (c != ' ')) {
93
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);
94
0
            goto err;
95
0
        }
96
0
        l = 0;
97
0
        use_bn = 0;
98
0
        for (;;) {
99
0
            if (num <= 0)
100
0
                break;
101
0
            num--;
102
0
            c = *(p++);
103
0
            if ((c == ' ') || (c == '.'))
104
0
                break;
105
0
            if (!ossl_isdigit(c)) {
106
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);
107
0
                goto err;
108
0
            }
109
0
            if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
110
0
                use_bn = 1;
111
0
                if (bl == NULL)
112
0
                    bl = BN_new();
113
0
                if (bl == NULL || !BN_set_word(bl, l))
114
0
                    goto err;
115
0
            }
116
0
            if (use_bn) {
117
0
                if (!BN_mul_word(bl, 10L)
118
0
                    || !BN_add_word(bl, c - '0'))
119
0
                    goto err;
120
0
            } else
121
0
                l = l * 10L + (long)(c - '0');
122
0
        }
123
0
        if (len == 0) {
124
0
            if ((first < 2) && (l >= 40)) {
125
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE);
126
0
                goto err;
127
0
            }
128
0
            if (use_bn) {
129
0
                if (!BN_add_word(bl, first * 40))
130
0
                    goto err;
131
0
            } else
132
0
                l += (long)first * 40;
133
0
        }
134
0
        i = 0;
135
0
        if (use_bn) {
136
0
            int blsize;
137
0
            blsize = BN_num_bits(bl);
138
0
            blsize = (blsize + 6) / 7;
139
0
            if (blsize > tmpsize) {
140
0
                if (tmp != ftmp)
141
0
                    OPENSSL_free(tmp);
142
0
                tmpsize = blsize + 32;
143
0
                tmp = OPENSSL_malloc(tmpsize);
144
0
                if (tmp == NULL)
145
0
                    goto err;
146
0
            }
147
0
            while (blsize--) {
148
0
                BN_ULONG t = BN_div_word(bl, 0x80L);
149
0
                if (t == (BN_ULONG)-1)
150
0
                    goto err;
151
0
                tmp[i++] = (unsigned char)t;
152
0
            }
153
0
        } else {
154
155
0
            for (;;) {
156
0
                tmp[i++] = (unsigned char)l & 0x7f;
157
0
                l >>= 7L;
158
0
                if (l == 0L)
159
0
                    break;
160
0
            }
161
0
        }
162
0
        if (out != NULL) {
163
0
            if (len + i > olen) {
164
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
165
0
                goto err;
166
0
            }
167
0
            while (--i > 0)
168
0
                out[len++] = tmp[i] | 0x80;
169
0
            out[len++] = tmp[0];
170
0
        } else
171
0
            len += i;
172
0
    }
173
0
    if (tmp != ftmp)
174
0
        OPENSSL_free(tmp);
175
0
    BN_free(bl);
176
0
    return len;
177
0
err:
178
0
    if (tmp != ftmp)
179
0
        OPENSSL_free(tmp);
180
0
    BN_free(bl);
181
0
    return 0;
182
0
}
183
184
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
185
0
{
186
0
    return OBJ_obj2txt(buf, buf_len, a, 0);
187
0
}
188
189
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
190
0
{
191
0
    char buf[80], *p = buf;
192
0
    int i;
193
194
0
    if ((a == NULL) || (a->data == NULL))
195
0
        return BIO_write(bp, "NULL", 4);
196
0
    i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
197
0
    if (i > (int)(sizeof(buf) - 1)) {
198
0
        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
0
        if ((p = OPENSSL_malloc(i + 1)) == NULL)
203
0
            return -1;
204
0
        i2t_ASN1_OBJECT(p, i + 1, a);
205
0
    }
206
0
    if (i <= 0) {
207
0
        i = BIO_write(bp, "<INVALID>", 9);
208
0
        if (i > 0)
209
0
            i += BIO_dump(bp, (const char *)a->data, a->length);
210
0
        return i;
211
0
    }
212
0
    BIO_write(bp, p, i);
213
0
    if (p != buf)
214
0
        OPENSSL_free(p);
215
0
    return i;
216
0
}
217
218
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
219
    long length)
220
0
{
221
0
    const unsigned char *p;
222
0
    long len;
223
0
    int tag, xclass;
224
0
    int inf, i;
225
0
    ASN1_OBJECT *ret = NULL;
226
0
    p = *pp;
227
0
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
228
0
    if (inf & 0x80) {
229
0
        i = ASN1_R_BAD_OBJECT_HEADER;
230
0
        goto err;
231
0
    }
232
233
0
    if (tag != V_ASN1_OBJECT) {
234
0
        i = ASN1_R_EXPECTING_AN_OBJECT;
235
0
        goto err;
236
0
    }
237
0
    ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
238
0
    if (ret)
239
0
        *pp = p;
240
0
    return ret;
241
0
err:
242
0
    ERR_raise(ERR_LIB_ASN1, i);
243
0
    return NULL;
244
0
}
245
246
ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
247
    long len)
248
0
{
249
0
    ASN1_OBJECT *ret = NULL, tobj;
250
0
    const unsigned char *p;
251
0
    unsigned char *data;
252
0
    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
0
    if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL || p[len - 1] & 0x80) {
260
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
261
0
        return NULL;
262
0
    }
263
    /* Now 0 < len <= INT_MAX, so the cast is safe. */
264
0
    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
0
    tobj.nid = NID_undef;
270
0
    tobj.data = p;
271
0
    tobj.length = length;
272
0
    tobj.flags = 0;
273
0
    i = OBJ_obj2nid(&tobj);
274
0
    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
0
        ret = OBJ_nid2obj(i);
281
0
        if (a) {
282
0
            ASN1_OBJECT_free(*a);
283
0
            *a = ret;
284
0
        }
285
0
        *pp += len;
286
0
        return ret;
287
0
    }
288
0
    for (i = 0; i < length; i++, p++) {
289
0
        if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
290
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
291
0
            return NULL;
292
0
        }
293
0
    }
294
295
0
    if ((a == NULL) || ((*a) == NULL) || !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
296
0
        if ((ret = ossl_asn1_object_new()) == NULL)
297
0
            return NULL;
298
0
    } else {
299
0
        ret = (*a);
300
0
    }
301
302
0
    p = *pp;
303
    /* detach data from object */
304
0
    data = (unsigned char *)ret->data;
305
0
    ret->data = NULL;
306
    /* once detached we can change it */
307
0
    if ((data == NULL) || (ret->length < length)) {
308
0
        ret->length = 0;
309
0
        OPENSSL_free(data);
310
0
        data = OPENSSL_malloc(length);
311
0
        if (data == NULL)
312
0
            goto err;
313
0
        ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
314
0
    }
315
0
    memcpy(data, p, length);
316
    /* If there are dynamic strings, free them here, and clear the flag */
317
0
    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
0
    ret->data = data;
324
0
    ret->length = length;
325
0
    ret->sn = NULL;
326
0
    ret->ln = NULL;
327
    /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
328
0
    p += length;
329
330
0
    if (a != NULL)
331
0
        (*a) = ret;
332
0
    *pp = p;
333
0
    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
0
}
340
341
#ifndef OPENSSL_NO_DEPRECATED_4_0
342
ASN1_OBJECT *ASN1_OBJECT_new(void)
343
0
{
344
0
    return NULL;
345
0
}
346
#endif /* OPENSSL_NO_DEPRECATED_4_0 */
347
348
ASN1_OBJECT *ossl_asn1_object_new(void)
349
0
{
350
0
    ASN1_OBJECT *ret;
351
352
0
    ret = OPENSSL_zalloc(sizeof(*ret));
353
0
    if (ret == NULL)
354
0
        return NULL;
355
0
    ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
356
0
    return ret;
357
0
}
358
359
void ASN1_OBJECT_free(ASN1_OBJECT *a)
360
2.54k
{
361
2.54k
    if (a == NULL)
362
0
        return;
363
2.54k
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
364
0
#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
0
        OPENSSL_free((void *)a->sn);
370
0
        OPENSSL_free((void *)a->ln);
371
0
#endif
372
0
        a->sn = a->ln = NULL;
373
0
    }
374
2.54k
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
375
0
        OPENSSL_free((void *)a->data);
376
0
        a->data = NULL;
377
0
        a->length = 0;
378
0
    }
379
2.54k
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
380
0
        OPENSSL_free(a);
381
2.54k
}
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
}