Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
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
3.71M
{
183
3.71M
    char buf[80], *p = buf;
184
3.71M
    int i;
185
186
3.71M
    if ((a == NULL) || (a->data == NULL))
187
0
        return BIO_write(bp, "NULL", 4);
188
3.71M
    i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
189
3.71M
    if (i > (int)(sizeof(buf) - 1)) {
190
31.2k
        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
31.2k
        if ((p = OPENSSL_malloc(i + 1)) == NULL)
195
0
            return -1;
196
31.2k
        i2t_ASN1_OBJECT(p, i + 1, a);
197
31.2k
    }
198
3.71M
    if (i <= 0) {
199
5.12k
        i = BIO_write(bp, "<INVALID>", 9);
200
5.12k
        if (i > 0)
201
5.12k
            i += BIO_dump(bp, (const char *)a->data, a->length);
202
5.12k
        return i;
203
5.12k
    }
204
3.70M
    BIO_write(bp, p, i);
205
3.70M
    if (p != buf)
206
31.2k
        OPENSSL_free(p);
207
3.70M
    return i;
208
3.71M
}
209
210
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
211
    long length)
212
2.27M
{
213
2.27M
    const unsigned char *p;
214
2.27M
    long len;
215
2.27M
    int tag, xclass;
216
2.27M
    int inf, i;
217
2.27M
    ASN1_OBJECT *ret = NULL;
218
2.27M
    p = *pp;
219
2.27M
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
220
2.27M
    if (inf & 0x80) {
221
42.5k
        i = ASN1_R_BAD_OBJECT_HEADER;
222
42.5k
        goto err;
223
42.5k
    }
224
225
2.23M
    if (tag != V_ASN1_OBJECT) {
226
0
        i = ASN1_R_EXPECTING_AN_OBJECT;
227
0
        goto err;
228
0
    }
229
2.23M
    ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
230
2.23M
    if (ret)
231
2.11M
        *pp = p;
232
2.23M
    return ret;
233
42.5k
err:
234
42.5k
    ERR_raise(ERR_LIB_ASN1, i);
235
42.5k
    return NULL;
236
2.23M
}
237
238
ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
239
    long len)
240
33.8M
{
241
33.8M
    ASN1_OBJECT *ret = NULL, tobj;
242
33.8M
    const unsigned char *p;
243
33.8M
    unsigned char *data;
244
33.8M
    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.8M
    if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL || p[len - 1] & 0x80) {
252
131k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
253
131k
        return NULL;
254
131k
    }
255
    /* Now 0 < len <= INT_MAX, so the cast is safe. */
256
33.7M
    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.7M
    tobj.nid = NID_undef;
262
33.7M
    tobj.data = p;
263
33.7M
    tobj.length = length;
264
33.7M
    tobj.flags = 0;
265
33.7M
    i = OBJ_obj2nid(&tobj);
266
33.7M
    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.1M
        ret = OBJ_nid2obj(i);
273
14.1M
        if (a) {
274
13.8M
            ASN1_OBJECT_free(*a);
275
13.8M
            *a = ret;
276
13.8M
        }
277
14.1M
        *pp += len;
278
14.1M
        return ret;
279
14.1M
    }
280
228M
    for (i = 0; i < length; i++, p++) {
281
208M
        if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
282
44.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
283
44.4k
            return NULL;
284
44.4k
        }
285
208M
    }
286
287
19.5M
    if ((a == NULL) || ((*a) == NULL) || !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
288
19.3M
        if ((ret = ASN1_OBJECT_new()) == NULL)
289
0
            return NULL;
290
19.3M
    } else {
291
209k
        ret = (*a);
292
209k
    }
293
294
19.5M
    p = *pp;
295
    /* detach data from object */
296
19.5M
    data = (unsigned char *)ret->data;
297
19.5M
    ret->data = NULL;
298
    /* once detached we can change it */
299
19.5M
    if ((data == NULL) || (ret->length < length)) {
300
19.3M
        ret->length = 0;
301
19.3M
        OPENSSL_free(data);
302
19.3M
        data = OPENSSL_malloc(length);
303
19.3M
        if (data == NULL)
304
0
            goto err;
305
19.3M
        ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
306
19.3M
    }
307
19.5M
    memcpy(data, p, length);
308
    /* If there are dynamic strings, free them here, and clear the flag */
309
19.5M
    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
19.5M
    ret->data = data;
316
19.5M
    ret->length = length;
317
19.5M
    ret->sn = NULL;
318
19.5M
    ret->ln = NULL;
319
    /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
320
19.5M
    p += length;
321
322
19.5M
    if (a != NULL)
323
19.5M
        (*a) = ret;
324
19.5M
    *pp = p;
325
19.5M
    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
19.5M
}
332
333
ASN1_OBJECT *ASN1_OBJECT_new(void)
334
37.0M
{
335
37.0M
    ASN1_OBJECT *ret;
336
337
37.0M
    ret = OPENSSL_zalloc(sizeof(*ret));
338
37.0M
    if (ret == NULL)
339
0
        return NULL;
340
37.0M
    ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
341
37.0M
    return ret;
342
37.0M
}
343
344
void ASN1_OBJECT_free(ASN1_OBJECT *a)
345
78.8M
{
346
78.8M
    if (a == NULL)
347
6.39M
        return;
348
72.4M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
349
17.6M
#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
17.6M
        OPENSSL_free((void *)a->sn);
355
17.6M
        OPENSSL_free((void *)a->ln);
356
17.6M
#endif
357
17.6M
        a->sn = a->ln = NULL;
358
17.6M
    }
359
72.4M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
360
37.0M
        OPENSSL_free((void *)a->data);
361
37.0M
        a->data = NULL;
362
37.0M
        a->length = 0;
363
37.0M
    }
364
72.4M
    if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
365
37.0M
        OPENSSL_free(a);
366
72.4M
}
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
}