Coverage Report

Created: 2026-02-14 07:20

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