Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
80.5M
{
31
80.5M
    ASN1_VALUE *ret = NULL;
32
80.5M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
80.5M
        return ret;
34
0
    return NULL;
35
80.5M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
    const char *propq)
39
111k
{
40
111k
    ASN1_VALUE *ret = NULL;
41
111k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
111k
        return ret;
43
0
    return NULL;
44
111k
}
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
41.9M
{
51
41.9M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
52
41.9M
}
53
54
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
55
80.5M
{
56
80.5M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
57
80.5M
}
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
33.3M
{
62
33.3M
    const ASN1_TEMPLATE *tt = NULL;
63
33.3M
    const ASN1_EXTERN_FUNCS *ef;
64
33.3M
    const ASN1_AUX *aux = it->funcs;
65
33.3M
    ASN1_aux_cb *asn1_cb;
66
33.3M
    ASN1_VALUE **pseqval;
67
33.3M
    int i;
68
33.3M
    if (aux && aux->asn1_cb)
69
1.34M
        asn1_cb = aux->asn1_cb;
70
32.0M
    else
71
32.0M
        asn1_cb = 0;
72
73
33.3M
    switch (it->itype) {
74
75
719k
    case ASN1_ITYPE_EXTERN:
76
719k
        ef = it->funcs;
77
719k
        if (ef != NULL) {
78
719k
            if (ef->asn1_ex_new_ex != NULL) {
79
215k
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
80
0
                    goto memerr;
81
503k
            } else if (ef->asn1_ex_new != NULL) {
82
503k
                if (!ef->asn1_ex_new(pval, it))
83
0
                    goto memerr;
84
503k
            }
85
719k
        }
86
719k
        break;
87
88
13.3M
    case ASN1_ITYPE_PRIMITIVE:
89
13.3M
        if (it->templates) {
90
21.8k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
91
0
                goto memerr;
92
13.3M
        } else if (!asn1_primitive_new(pval, it, embed))
93
0
            goto memerr;
94
13.3M
        break;
95
96
13.3M
    case ASN1_ITYPE_MSTRING:
97
6.78M
        if (!asn1_primitive_new(pval, it, embed))
98
0
            goto memerr;
99
6.78M
        break;
100
101
6.78M
    case ASN1_ITYPE_CHOICE:
102
1.12M
        if (asn1_cb) {
103
72.1k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
104
72.1k
            if (!i)
105
0
                goto auxerr;
106
72.1k
            if (i == 2) {
107
0
                return 1;
108
0
            }
109
72.1k
        }
110
1.12M
        if (embed) {
111
19.7k
            memset(*pval, 0, it->size);
112
1.10M
        } else {
113
1.10M
            *pval = OPENSSL_zalloc(it->size);
114
1.10M
            if (*pval == NULL)
115
0
                goto memerr;
116
1.10M
        }
117
1.12M
        ossl_asn1_set_choice_selector(pval, -1, it);
118
1.12M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
119
0
            goto auxerr2;
120
1.12M
        break;
121
122
1.12M
    case ASN1_ITYPE_NDEF_SEQUENCE:
123
11.3M
    case ASN1_ITYPE_SEQUENCE:
124
11.3M
        if (asn1_cb) {
125
910k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
126
910k
            if (!i)
127
0
                goto auxerr;
128
910k
            if (i == 2) {
129
208k
                return 1;
130
208k
            }
131
910k
        }
132
11.1M
        if (embed) {
133
963k
            memset(*pval, 0, it->size);
134
10.1M
        } else {
135
10.1M
            *pval = OPENSSL_zalloc(it->size);
136
10.1M
            if (*pval == NULL)
137
0
                goto memerr;
138
10.1M
        }
139
        /* 0 : init. lock */
140
11.1M
        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 memerr;
146
0
        }
147
11.1M
        ossl_asn1_enc_init(pval, it);
148
39.3M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
149
28.2M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
150
28.2M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
151
0
                goto memerr2;
152
28.2M
        }
153
11.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
154
0
            goto auxerr2;
155
11.1M
        break;
156
33.3M
    }
157
33.1M
    return 1;
158
159
0
memerr2:
160
0
    ossl_asn1_item_embed_free(pval, it, embed);
161
0
memerr:
162
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
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
36.2M
{
174
36.2M
    const ASN1_EXTERN_FUNCS *ef;
175
176
36.2M
    switch (it->itype) {
177
178
1.78M
    case ASN1_ITYPE_EXTERN:
179
1.78M
        ef = it->funcs;
180
1.78M
        if (ef && ef->asn1_ex_clear)
181
0
            ef->asn1_ex_clear(pval, it);
182
1.78M
        else
183
1.78M
            *pval = NULL;
184
1.78M
        break;
185
186
29.8M
    case ASN1_ITYPE_PRIMITIVE:
187
29.8M
        if (it->templates)
188
584k
            asn1_template_clear(pval, it->templates);
189
29.2M
        else
190
29.2M
            asn1_primitive_clear(pval, it);
191
29.8M
        break;
192
193
490k
    case ASN1_ITYPE_MSTRING:
194
490k
        asn1_primitive_clear(pval, it);
195
490k
        break;
196
197
1.54M
    case ASN1_ITYPE_CHOICE:
198
4.12M
    case ASN1_ITYPE_SEQUENCE:
199
4.17M
    case ASN1_ITYPE_NDEF_SEQUENCE:
200
4.17M
        *pval = NULL;
201
4.17M
        break;
202
36.2M
    }
203
36.2M
}
204
205
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
206
    OSSL_LIB_CTX *libctx, const char *propq)
207
183M
{
208
183M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
209
183M
    int embed = tt->flags & ASN1_TFLG_EMBED;
210
183M
    ASN1_VALUE *tval;
211
183M
    int ret;
212
183M
    if (embed) {
213
14.3M
        tval = (ASN1_VALUE *)pval;
214
14.3M
        pval = &tval;
215
14.3M
    }
216
183M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
217
44.0M
        asn1_template_clear(pval, tt);
218
44.0M
        return 1;
219
44.0M
    }
220
    /* If ANY DEFINED BY nothing to do */
221
222
139M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
223
1.04M
        *pval = NULL;
224
1.04M
        return 1;
225
1.04M
    }
226
    /* If SET OF or SEQUENCE OF, its a STACK */
227
138M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
228
1.60M
        STACK_OF(ASN1_VALUE) *skval;
229
1.60M
        skval = sk_ASN1_VALUE_new_null();
230
1.60M
        if (!skval) {
231
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
232
0
            ret = 0;
233
0
            goto done;
234
0
        }
235
1.60M
        *pval = (ASN1_VALUE *)skval;
236
1.60M
        ret = 1;
237
1.60M
        goto done;
238
1.60M
    }
239
    /* Otherwise pass it back to the item routine */
240
136M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
241
138M
done:
242
138M
    return ret;
243
136M
}
244
245
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
246
44.6M
{
247
    /* If ADB or STACK just NULL the field */
248
44.6M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
249
8.40M
        *pval = NULL;
250
36.2M
    else
251
36.2M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
252
44.6M
}
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
175M
{
262
175M
    ASN1_TYPE *typ;
263
175M
    ASN1_STRING *str;
264
175M
    int utype;
265
266
175M
    if (!it)
267
0
        return 0;
268
269
175M
    if (it->funcs) {
270
2.31M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
271
2.31M
        if (embed) {
272
853k
            if (pf->prim_clear) {
273
853k
                pf->prim_clear(pval, it);
274
853k
                return 1;
275
853k
            }
276
1.45M
        } else if (pf->prim_new) {
277
1.45M
            return pf->prim_new(pval, it);
278
1.45M
        }
279
2.31M
    }
280
281
173M
    if (it->itype == ASN1_ITYPE_MSTRING)
282
45.1M
        utype = -1;
283
128M
    else
284
128M
        utype = it->utype;
285
173M
    switch (utype) {
286
58.1M
    case V_ASN1_OBJECT:
287
58.1M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
288
58.1M
        return 1;
289
290
0
    case V_ASN1_BOOLEAN:
291
0
        *(ASN1_BOOLEAN *)pval = it->size;
292
0
        return 1;
293
294
0
    case V_ASN1_NULL:
295
0
        *pval = (ASN1_VALUE *)1;
296
0
        return 1;
297
298
54.6M
    case V_ASN1_ANY:
299
54.6M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
300
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
301
0
            return 0;
302
0
        }
303
54.6M
        typ->value.ptr = NULL;
304
54.6M
        typ->type = -1;
305
54.6M
        *pval = (ASN1_VALUE *)typ;
306
54.6M
        break;
307
308
60.4M
    default:
309
60.4M
        if (embed) {
310
7.23M
            str = *(ASN1_STRING **)pval;
311
7.23M
            memset(str, 0, sizeof(*str));
312
7.23M
            str->type = utype;
313
7.23M
            str->flags = ASN1_STRING_FLAG_EMBED;
314
53.1M
        } else {
315
53.1M
            str = ASN1_STRING_type_new(utype);
316
53.1M
            *pval = (ASN1_VALUE *)str;
317
53.1M
        }
318
60.4M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
319
45.1M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
320
60.4M
        break;
321
173M
    }
322
115M
    if (*pval)
323
115M
        return 1;
324
0
    return 0;
325
115M
}
326
327
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
328
29.7M
{
329
29.7M
    int utype;
330
29.7M
    if (it && it->funcs) {
331
600k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
332
600k
        if (pf->prim_clear)
333
443k
            pf->prim_clear(pval, it);
334
156k
        else
335
156k
            *pval = NULL;
336
600k
        return;
337
600k
    }
338
29.1M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
339
490k
        utype = -1;
340
28.6M
    else
341
28.6M
        utype = it->utype;
342
29.1M
    if (utype == V_ASN1_BOOLEAN)
343
5.67M
        *(ASN1_BOOLEAN *)pval = it->size;
344
23.4M
    else
345
23.4M
        *pval = NULL;
346
29.1M
}