Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
82.3M
{
31
82.3M
    ASN1_VALUE *ret = NULL;
32
82.3M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
82.3M
        return ret;
34
0
    return NULL;
35
82.3M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
                             const char *propq)
39
100k
{
40
100k
    ASN1_VALUE *ret = NULL;
41
100k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
100k
        return ret;
43
0
    return NULL;
44
100k
}
45
46
/* Allocate an ASN1 structure */
47
48
49
int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it,
50
                                 OSSL_LIB_CTX *libctx, const char *propq)
51
41.8M
{
52
41.8M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
53
41.8M
}
54
55
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
56
82.3M
{
57
82.3M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
58
82.3M
}
59
60
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed,
61
                        OSSL_LIB_CTX *libctx, const char *propq)
62
214M
{
63
214M
    const ASN1_TEMPLATE *tt = NULL;
64
214M
    const ASN1_EXTERN_FUNCS *ef;
65
214M
    const ASN1_AUX *aux = it->funcs;
66
214M
    ASN1_aux_cb *asn1_cb;
67
214M
    ASN1_VALUE **pseqval;
68
214M
    int i;
69
214M
    if (aux && aux->asn1_cb)
70
6.55M
        asn1_cb = aux->asn1_cb;
71
208M
    else
72
208M
        asn1_cb = 0;
73
74
214M
    switch (it->itype) {
75
76
3.11M
    case ASN1_ITYPE_EXTERN:
77
3.11M
        ef = it->funcs;
78
3.11M
        if (ef != NULL) {
79
3.11M
            if (ef->asn1_ex_new_ex != NULL) {
80
937k
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
81
0
                    goto asn1err;
82
2.18M
            } else if (ef->asn1_ex_new != NULL) {
83
2.18M
                if (!ef->asn1_ex_new(pval, it))
84
0
                    goto asn1err;
85
2.18M
            }
86
3.11M
        }
87
3.11M
        break;
88
89
117M
    case ASN1_ITYPE_PRIMITIVE:
90
117M
        if (it->templates) {
91
103k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
92
0
                goto asn1err;
93
116M
        } else if (!asn1_primitive_new(pval, it, embed))
94
0
            goto asn1err;
95
117M
        break;
96
97
117M
    case ASN1_ITYPE_MSTRING:
98
31.9M
        if (!asn1_primitive_new(pval, it, embed))
99
0
            goto asn1err;
100
31.9M
        break;
101
102
31.9M
    case ASN1_ITYPE_CHOICE:
103
6.51M
        if (asn1_cb) {
104
389k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
105
389k
            if (!i)
106
0
                goto auxerr;
107
389k
            if (i == 2) {
108
0
                return 1;
109
0
            }
110
389k
        }
111
6.51M
        if (embed) {
112
112k
            memset(*pval, 0, it->size);
113
6.40M
        } else {
114
6.40M
            *pval = OPENSSL_zalloc(it->size);
115
6.40M
            if (*pval == NULL)
116
0
                return 0;
117
6.40M
        }
118
6.51M
        ossl_asn1_set_choice_selector(pval, -1, it);
119
6.51M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
120
0
            goto auxerr2;
121
6.51M
        break;
122
123
6.51M
    case ASN1_ITYPE_NDEF_SEQUENCE:
124
56.0M
    case ASN1_ITYPE_SEQUENCE:
125
56.0M
        if (asn1_cb) {
126
4.16M
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
127
4.16M
            if (!i)
128
0
                goto auxerr;
129
4.16M
            if (i == 2) {
130
907k
                return 1;
131
907k
            }
132
4.16M
        }
133
55.1M
        if (embed) {
134
4.12M
            memset(*pval, 0, it->size);
135
51.0M
        } else {
136
51.0M
            *pval = OPENSSL_zalloc(it->size);
137
51.0M
            if (*pval == NULL)
138
0
                return 0;
139
51.0M
        }
140
        /* 0 : init. lock */
141
55.1M
        if (ossl_asn1_do_lock(pval, 0, it) < 0) {
142
0
            if (!embed) {
143
0
                OPENSSL_free(*pval);
144
0
                *pval = NULL;
145
0
            }
146
0
            goto asn1err;
147
0
        }
148
55.1M
        ossl_asn1_enc_init(pval, it);
149
195M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
150
140M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
151
140M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
152
0
                goto asn1err2;
153
140M
        }
154
55.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
155
0
            goto auxerr2;
156
55.1M
        break;
157
214M
    }
158
213M
    return 1;
159
160
0
 asn1err2:
161
0
    ossl_asn1_item_embed_free(pval, it, embed);
162
0
 asn1err:
163
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
164
0
    return 0;
165
166
0
 auxerr2:
167
0
    ossl_asn1_item_embed_free(pval, it, embed);
168
0
 auxerr:
169
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
170
0
    return 0;
171
172
0
}
173
174
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
175
35.0M
{
176
35.0M
    const ASN1_EXTERN_FUNCS *ef;
177
178
35.0M
    switch (it->itype) {
179
180
1.38M
    case ASN1_ITYPE_EXTERN:
181
1.38M
        ef = it->funcs;
182
1.38M
        if (ef && ef->asn1_ex_clear)
183
0
            ef->asn1_ex_clear(pval, it);
184
1.38M
        else
185
1.38M
            *pval = NULL;
186
1.38M
        break;
187
188
29.0M
    case ASN1_ITYPE_PRIMITIVE:
189
29.0M
        if (it->templates)
190
449k
            asn1_template_clear(pval, it->templates);
191
28.6M
        else
192
28.6M
            asn1_primitive_clear(pval, it);
193
29.0M
        break;
194
195
498k
    case ASN1_ITYPE_MSTRING:
196
498k
        asn1_primitive_clear(pval, it);
197
498k
        break;
198
199
1.68M
    case ASN1_ITYPE_CHOICE:
200
4.05M
    case ASN1_ITYPE_SEQUENCE:
201
4.10M
    case ASN1_ITYPE_NDEF_SEQUENCE:
202
4.10M
        *pval = NULL;
203
4.10M
        break;
204
35.0M
    }
205
35.0M
}
206
207
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
208
                             OSSL_LIB_CTX *libctx, const char *propq)
209
169M
{
210
169M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
211
169M
    int embed = tt->flags & ASN1_TFLG_EMBED;
212
169M
    ASN1_VALUE *tval;
213
169M
    int ret;
214
169M
    if (embed) {
215
13.6M
        tval = (ASN1_VALUE *)pval;
216
13.6M
        pval = &tval;
217
13.6M
    }
218
169M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
219
42.8M
        asn1_template_clear(pval, tt);
220
42.8M
        return 1;
221
42.8M
    }
222
    /* If ANY DEFINED BY nothing to do */
223
224
126M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
225
1.11M
        *pval = NULL;
226
1.11M
        return 1;
227
1.11M
    }
228
    /* If SET OF or SEQUENCE OF, its a STACK */
229
125M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
230
1.57M
        STACK_OF(ASN1_VALUE) *skval;
231
1.57M
        skval = sk_ASN1_VALUE_new_null();
232
1.57M
        if (!skval) {
233
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
234
0
            ret = 0;
235
0
            goto done;
236
0
        }
237
1.57M
        *pval = (ASN1_VALUE *)skval;
238
1.57M
        ret = 1;
239
1.57M
        goto done;
240
1.57M
    }
241
    /* Otherwise pass it back to the item routine */
242
123M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
243
125M
 done:
244
125M
    return ret;
245
123M
}
246
247
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
248
43.3M
{
249
    /* If ADB or STACK just NULL the field */
250
43.3M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
251
8.26M
        *pval = NULL;
252
35.0M
    else
253
35.0M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
254
43.3M
}
255
256
/*
257
 * NB: could probably combine most of the real XXX_new() behaviour and junk
258
 * all the old functions.
259
 */
260
261
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
262
                              int embed)
263
169M
{
264
169M
    ASN1_TYPE *typ;
265
169M
    ASN1_STRING *str;
266
169M
    int utype;
267
268
169M
    if (!it)
269
0
        return 0;
270
271
169M
    if (it->funcs) {
272
2.30M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
273
2.30M
        if (embed) {
274
926k
            if (pf->prim_clear) {
275
926k
                pf->prim_clear(pval, it);
276
926k
                return 1;
277
926k
            }
278
1.38M
        } else if (pf->prim_new) {
279
1.38M
            return pf->prim_new(pval, it);
280
1.38M
        }
281
2.30M
    }
282
283
166M
    if (it->itype == ASN1_ITYPE_MSTRING)
284
38.7M
        utype = -1;
285
127M
    else
286
127M
        utype = it->utype;
287
166M
    switch (utype) {
288
51.5M
    case V_ASN1_OBJECT:
289
51.5M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290
51.5M
        return 1;
291
292
0
    case V_ASN1_BOOLEAN:
293
0
        *(ASN1_BOOLEAN *)pval = it->size;
294
0
        return 1;
295
296
0
    case V_ASN1_NULL:
297
0
        *pval = (ASN1_VALUE *)1;
298
0
        return 1;
299
300
60.6M
    case V_ASN1_ANY:
301
60.6M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
302
0
            return 0;
303
60.6M
        typ->value.ptr = NULL;
304
60.6M
        typ->type = -1;
305
60.6M
        *pval = (ASN1_VALUE *)typ;
306
60.6M
        break;
307
308
54.4M
    default:
309
54.4M
        if (embed) {
310
6.95M
            str = *(ASN1_STRING **)pval;
311
6.95M
            memset(str, 0, sizeof(*str));
312
6.95M
            str->type = utype;
313
6.95M
            str->flags = ASN1_STRING_FLAG_EMBED;
314
47.5M
        } else {
315
47.5M
            str = ASN1_STRING_type_new(utype);
316
47.5M
            *pval = (ASN1_VALUE *)str;
317
47.5M
        }
318
54.4M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
319
38.7M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
320
54.4M
        break;
321
166M
    }
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.1M
{
329
29.1M
    int utype;
330
29.1M
    if (it && it->funcs) {
331
661k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
332
661k
        if (pf->prim_clear)
333
521k
            pf->prim_clear(pval, it);
334
139k
        else
335
139k
            *pval = NULL;
336
661k
        return;
337
661k
    }
338
28.4M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
339
498k
        utype = -1;
340
27.9M
    else
341
27.9M
        utype = it->utype;
342
28.4M
    if (utype == V_ASN1_BOOLEAN)
343
5.98M
        *(ASN1_BOOLEAN *)pval = it->size;
344
22.4M
    else
345
22.4M
        *pval = NULL;
346
28.4M
}