Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/asn1/tasn_new.c
Line
Count
Source
1
/*
2
 * Copyright 2000-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 <stddef.h>
11
#include <openssl/asn1.h>
12
#include <openssl/objects.h>
13
#include <openssl/err.h>
14
#include <openssl/asn1t.h>
15
#include <string.h>
16
#include "asn1_local.h"
17
18
static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
19
    int embed, OSSL_LIB_CTX *libctx,
20
    const char *propq);
21
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
22
    int embed);
23
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
24
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
25
    OSSL_LIB_CTX *libctx, const char *propq);
26
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
27
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
28
29
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
30
54.1M
{
31
54.1M
    ASN1_VALUE *ret = NULL;
32
54.1M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
54.1M
        return ret;
34
0
    return NULL;
35
54.1M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
    const char *propq)
39
86.1k
{
40
86.1k
    ASN1_VALUE *ret = NULL;
41
86.1k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
86.1k
        return ret;
43
0
    return NULL;
44
86.1k
}
45
46
/* Allocate an ASN1 structure */
47
48
int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it,
49
    OSSL_LIB_CTX *libctx, const char *propq)
50
32.3M
{
51
32.3M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
52
32.3M
}
53
54
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
55
54.1M
{
56
54.1M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
57
54.1M
}
58
59
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed,
60
    OSSL_LIB_CTX *libctx, const char *propq)
61
188M
{
62
188M
    const ASN1_TEMPLATE *tt = NULL;
63
188M
    const ASN1_EXTERN_FUNCS *ef;
64
188M
    const ASN1_AUX *aux = it->funcs;
65
188M
    ASN1_aux_cb *asn1_cb;
66
188M
    ASN1_VALUE **pseqval;
67
188M
    int i;
68
188M
    if (aux && aux->asn1_cb)
69
6.15M
        asn1_cb = aux->asn1_cb;
70
182M
    else
71
182M
        asn1_cb = 0;
72
73
188M
    switch (it->itype) {
74
75
3.32M
    case ASN1_ITYPE_EXTERN:
76
3.32M
        ef = it->funcs;
77
3.32M
        if (ef != NULL) {
78
3.32M
            if (ef->asn1_ex_new_ex != NULL) {
79
1.01M
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
80
0
                    goto asn1err;
81
2.31M
            } else if (ef->asn1_ex_new != NULL) {
82
2.31M
                if (!ef->asn1_ex_new(pval, it))
83
0
                    goto asn1err;
84
2.31M
            }
85
3.32M
        }
86
3.32M
        break;
87
88
92.9M
    case ASN1_ITYPE_PRIMITIVE:
89
92.9M
        if (it->templates) {
90
138k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
91
0
                goto asn1err;
92
92.8M
        } else if (!asn1_primitive_new(pval, it, embed))
93
0
            goto asn1err;
94
92.9M
        break;
95
96
92.9M
    case ASN1_ITYPE_MSTRING:
97
31.9M
        if (!asn1_primitive_new(pval, it, embed))
98
0
            goto asn1err;
99
31.9M
        break;
100
101
31.9M
    case ASN1_ITYPE_CHOICE:
102
4.60M
        if (asn1_cb) {
103
335k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
104
335k
            if (!i)
105
0
                goto auxerr;
106
335k
            if (i == 2) {
107
0
                return 1;
108
0
            }
109
335k
        }
110
4.60M
        if (embed) {
111
114k
            memset(*pval, 0, it->size);
112
4.49M
        } else {
113
4.49M
            *pval = OPENSSL_zalloc(it->size);
114
4.49M
            if (*pval == NULL)
115
0
                return 0;
116
4.49M
        }
117
4.60M
        ossl_asn1_set_choice_selector(pval, -1, it);
118
4.60M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
119
0
            goto auxerr2;
120
4.60M
        break;
121
122
4.60M
    case ASN1_ITYPE_NDEF_SEQUENCE:
123
55.9M
    case ASN1_ITYPE_SEQUENCE:
124
55.9M
        if (asn1_cb) {
125
4.19M
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
126
4.19M
            if (!i)
127
0
                goto auxerr;
128
4.19M
            if (i == 2) {
129
845k
                return 1;
130
845k
            }
131
4.19M
        }
132
55.0M
        if (embed) {
133
4.46M
            memset(*pval, 0, it->size);
134
50.6M
        } else {
135
50.6M
            *pval = OPENSSL_zalloc(it->size);
136
50.6M
            if (*pval == NULL)
137
0
                return 0;
138
50.6M
        }
139
        /* 0 : init. lock */
140
55.0M
        if (ossl_asn1_do_lock(pval, 0, it) < 0) {
141
0
            if (!embed) {
142
0
                OPENSSL_free(*pval);
143
0
                *pval = NULL;
144
0
            }
145
0
            goto asn1err;
146
0
        }
147
55.0M
        ossl_asn1_enc_init(pval, it);
148
197M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
149
142M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
150
142M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
151
0
                goto asn1err2;
152
142M
        }
153
55.0M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
154
0
            goto auxerr2;
155
55.0M
        break;
156
188M
    }
157
187M
    return 1;
158
159
0
asn1err2:
160
0
    ossl_asn1_item_embed_free(pval, it, embed);
161
0
asn1err:
162
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
163
0
    return 0;
164
165
0
auxerr2:
166
0
    ossl_asn1_item_embed_free(pval, it, embed);
167
0
auxerr:
168
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
169
0
    return 0;
170
0
}
171
172
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
173
30.9M
{
174
30.9M
    const ASN1_EXTERN_FUNCS *ef;
175
176
30.9M
    switch (it->itype) {
177
178
1.70M
    case ASN1_ITYPE_EXTERN:
179
1.70M
        ef = it->funcs;
180
1.70M
        if (ef && ef->asn1_ex_clear)
181
0
            ef->asn1_ex_clear(pval, it);
182
1.70M
        else
183
1.70M
            *pval = NULL;
184
1.70M
        break;
185
186
25.1M
    case ASN1_ITYPE_PRIMITIVE:
187
25.1M
        if (it->templates)
188
561k
            asn1_template_clear(pval, it->templates);
189
24.5M
        else
190
24.5M
            asn1_primitive_clear(pval, it);
191
25.1M
        break;
192
193
386k
    case ASN1_ITYPE_MSTRING:
194
386k
        asn1_primitive_clear(pval, it);
195
386k
        break;
196
197
1.36M
    case ASN1_ITYPE_CHOICE:
198
3.70M
    case ASN1_ITYPE_SEQUENCE:
199
3.74M
    case ASN1_ITYPE_NDEF_SEQUENCE:
200
3.74M
        *pval = NULL;
201
3.74M
        break;
202
30.9M
    }
203
30.9M
}
204
205
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
206
    OSSL_LIB_CTX *libctx, const char *propq)
207
142M
{
208
142M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
209
142M
    int embed = tt->flags & ASN1_TFLG_EMBED;
210
142M
    ASN1_VALUE *tval;
211
142M
    int ret;
212
142M
    if (embed) {
213
11.4M
        tval = (ASN1_VALUE *)pval;
214
11.4M
        pval = &tval;
215
11.4M
    }
216
142M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
217
37.6M
        asn1_template_clear(pval, tt);
218
37.6M
        return 1;
219
37.6M
    }
220
    /* If ANY DEFINED BY nothing to do */
221
222
104M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
223
919k
        *pval = NULL;
224
919k
        return 1;
225
919k
    }
226
    /* If SET OF or SEQUENCE OF, its a STACK */
227
103M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
228
1.46M
        STACK_OF(ASN1_VALUE) *skval;
229
1.46M
        skval = sk_ASN1_VALUE_new_null();
230
1.46M
        if (!skval) {
231
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
232
0
            ret = 0;
233
0
            goto done;
234
0
        }
235
1.46M
        *pval = (ASN1_VALUE *)skval;
236
1.46M
        ret = 1;
237
1.46M
        goto done;
238
1.46M
    }
239
    /* Otherwise pass it back to the item routine */
240
102M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
241
103M
done:
242
103M
    return ret;
243
102M
}
244
245
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
246
38.1M
{
247
    /* If ADB or STACK just NULL the field */
248
38.1M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
249
7.20M
        *pval = NULL;
250
30.9M
    else
251
30.9M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
252
38.1M
}
253
254
/*
255
 * NB: could probably combine most of the real XXX_new() behaviour and junk
256
 * all the old functions.
257
 */
258
259
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
260
    int embed)
261
124M
{
262
124M
    ASN1_TYPE *typ;
263
124M
    ASN1_STRING *str;
264
124M
    int utype;
265
266
124M
    if (!it)
267
0
        return 0;
268
269
124M
    if (it->funcs) {
270
1.62M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
271
1.62M
        if (embed) {
272
682k
            if (pf->prim_clear) {
273
682k
                pf->prim_clear(pval, it);
274
682k
                return 1;
275
682k
            }
276
944k
        } else if (pf->prim_new) {
277
944k
            return pf->prim_new(pval, it);
278
944k
        }
279
1.62M
    }
280
281
123M
    if (it->itype == ASN1_ITYPE_MSTRING)
282
31.9M
        utype = -1;
283
91.2M
    else
284
91.2M
        utype = it->utype;
285
123M
    switch (utype) {
286
42.8M
    case V_ASN1_OBJECT:
287
42.8M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
288
42.8M
        return 1;
289
290
0
    case V_ASN1_BOOLEAN:
291
0
        *(ASN1_BOOLEAN *)pval = it->size;
292
0
        return 1;
293
294
79
    case V_ASN1_NULL:
295
79
        *pval = (ASN1_VALUE *)1;
296
79
        return 1;
297
298
35.6M
    case V_ASN1_ANY:
299
35.6M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
300
0
            return 0;
301
35.6M
        typ->value.ptr = NULL;
302
35.6M
        typ->type = -1;
303
35.6M
        *pval = (ASN1_VALUE *)typ;
304
35.6M
        break;
305
306
44.6M
    default:
307
44.6M
        if (embed) {
308
5.80M
            str = *(ASN1_STRING **)pval;
309
5.80M
            memset(str, 0, sizeof(*str));
310
5.80M
            str->type = utype;
311
5.80M
            str->flags = ASN1_STRING_FLAG_EMBED;
312
38.8M
        } else {
313
38.8M
            str = ASN1_STRING_type_new(utype);
314
38.8M
            *pval = (ASN1_VALUE *)str;
315
38.8M
        }
316
44.6M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
317
31.9M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
318
44.6M
        break;
319
123M
    }
320
80.3M
    if (*pval)
321
80.3M
        return 1;
322
0
    return 0;
323
80.3M
}
324
325
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
326
24.9M
{
327
24.9M
    int utype;
328
24.9M
    if (it && it->funcs) {
329
514k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
330
514k
        if (pf->prim_clear)
331
384k
            pf->prim_clear(pval, it);
332
129k
        else
333
129k
            *pval = NULL;
334
514k
        return;
335
514k
    }
336
24.4M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
337
386k
        utype = -1;
338
24.0M
    else
339
24.0M
        utype = it->utype;
340
24.4M
    if (utype == V_ASN1_BOOLEAN)
341
4.68M
        *(ASN1_BOOLEAN *)pval = it->size;
342
19.7M
    else
343
19.7M
        *pval = NULL;
344
24.4M
}