Coverage Report

Created: 2025-12-31 06:58

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