Coverage Report

Created: 2023-06-08 06:41

/src/openssl30/crypto/asn1/tasn_new.c
Line
Count
Source (jump to first uncovered line)
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
115k
{
31
115k
    ASN1_VALUE *ret = NULL;
32
115k
    if (ASN1_item_ex_new(&ret, it) > 0)
33
115k
        return ret;
34
0
    return NULL;
35
115k
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
                             const char *propq)
39
0
{
40
0
    ASN1_VALUE *ret = NULL;
41
0
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
0
        return ret;
43
0
    return NULL;
44
0
}
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
181k
{
52
181k
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
53
181k
}
54
55
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
56
115k
{
57
115k
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
58
115k
}
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
791k
{
63
791k
    const ASN1_TEMPLATE *tt = NULL;
64
791k
    const ASN1_EXTERN_FUNCS *ef;
65
791k
    const ASN1_AUX *aux = it->funcs;
66
791k
    ASN1_aux_cb *asn1_cb;
67
791k
    ASN1_VALUE **pseqval;
68
791k
    int i;
69
791k
    if (aux && aux->asn1_cb)
70
40.4k
        asn1_cb = aux->asn1_cb;
71
750k
    else
72
750k
        asn1_cb = 0;
73
74
791k
    switch (it->itype) {
75
76
45.1k
    case ASN1_ITYPE_EXTERN:
77
45.1k
        ef = it->funcs;
78
45.1k
        if (ef != NULL) {
79
45.1k
            if (ef->asn1_ex_new_ex != NULL) {
80
15.0k
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
81
0
                    goto memerr;
82
30.1k
            } else if (ef->asn1_ex_new != NULL) {
83
30.1k
                if (!ef->asn1_ex_new(pval, it))
84
0
                    goto memerr;
85
30.1k
            }
86
45.1k
        }
87
45.1k
        break;
88
89
352k
    case ASN1_ITYPE_PRIMITIVE:
90
352k
        if (it->templates) {
91
0
            if (!asn1_template_new(pval, it->templates, libctx, propq))
92
0
                goto memerr;
93
352k
        } else if (!asn1_primitive_new(pval, it, embed))
94
0
            goto memerr;
95
352k
        break;
96
97
352k
    case ASN1_ITYPE_MSTRING:
98
90.9k
        if (!asn1_primitive_new(pval, it, embed))
99
0
            goto memerr;
100
90.9k
        break;
101
102
90.9k
    case ASN1_ITYPE_CHOICE:
103
20.5k
        if (asn1_cb) {
104
0
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
105
0
            if (!i)
106
0
                goto auxerr;
107
0
            if (i == 2) {
108
0
                return 1;
109
0
            }
110
0
        }
111
20.5k
        if (embed) {
112
0
            memset(*pval, 0, it->size);
113
20.5k
        } else {
114
20.5k
            *pval = OPENSSL_zalloc(it->size);
115
20.5k
            if (*pval == NULL)
116
0
                goto memerr;
117
20.5k
        }
118
20.5k
        ossl_asn1_set_choice_selector(pval, -1, it);
119
20.5k
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
120
0
            goto auxerr2;
121
20.5k
        break;
122
123
20.5k
    case ASN1_ITYPE_NDEF_SEQUENCE:
124
281k
    case ASN1_ITYPE_SEQUENCE:
125
281k
        if (asn1_cb) {
126
35.1k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
127
35.1k
            if (!i)
128
0
                goto auxerr;
129
35.1k
            if (i == 2) {
130
20.0k
                return 1;
131
20.0k
            }
132
35.1k
        }
133
261k
        if (embed) {
134
60.2k
            memset(*pval, 0, it->size);
135
201k
        } else {
136
201k
            *pval = OPENSSL_zalloc(it->size);
137
201k
            if (*pval == NULL)
138
0
                goto memerr;
139
201k
        }
140
        /* 0 : init. lock */
141
261k
        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 memerr;
147
0
        }
148
261k
        ossl_asn1_enc_init(pval, it);
149
994k
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
150
732k
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
151
732k
            if (!asn1_template_new(pseqval, tt, libctx, propq))
152
0
                goto memerr2;
153
732k
        }
154
261k
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
155
0
            goto auxerr2;
156
261k
        break;
157
791k
    }
158
771k
    return 1;
159
160
0
 memerr2:
161
0
    ossl_asn1_item_embed_free(pval, it, embed);
162
0
 memerr:
163
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
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
223k
{
176
223k
    const ASN1_EXTERN_FUNCS *ef;
177
178
223k
    switch (it->itype) {
179
180
0
    case ASN1_ITYPE_EXTERN:
181
0
        ef = it->funcs;
182
0
        if (ef && ef->asn1_ex_clear)
183
0
            ef->asn1_ex_clear(pval, it);
184
0
        else
185
0
            *pval = NULL;
186
0
        break;
187
188
218k
    case ASN1_ITYPE_PRIMITIVE:
189
218k
        if (it->templates)
190
0
            asn1_template_clear(pval, it->templates);
191
218k
        else
192
218k
            asn1_primitive_clear(pval, it);
193
218k
        break;
194
195
0
    case ASN1_ITYPE_MSTRING:
196
0
        asn1_primitive_clear(pval, it);
197
0
        break;
198
199
5.01k
    case ASN1_ITYPE_CHOICE:
200
5.15k
    case ASN1_ITYPE_SEQUENCE:
201
5.15k
    case ASN1_ITYPE_NDEF_SEQUENCE:
202
5.15k
        *pval = NULL;
203
5.15k
        break;
204
223k
    }
205
223k
}
206
207
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
208
                             OSSL_LIB_CTX *libctx, const char *propq)
209
732k
{
210
732k
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
211
732k
    int embed = tt->flags & ASN1_TFLG_EMBED;
212
732k
    ASN1_VALUE *tval;
213
732k
    int ret;
214
732k
    if (embed) {
215
157k
        tval = (ASN1_VALUE *)pval;
216
157k
        pval = &tval;
217
157k
    }
218
732k
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
219
238k
        asn1_template_clear(pval, tt);
220
238k
        return 1;
221
238k
    }
222
    /* If ANY DEFINED BY nothing to do */
223
224
493k
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
225
0
        *pval = NULL;
226
0
        return 1;
227
0
    }
228
    /* If SET OF or SEQUENCE OF, its a STACK */
229
493k
    if (tt->flags & ASN1_TFLG_SK_MASK) {
230
0
        STACK_OF(ASN1_VALUE) *skval;
231
0
        skval = sk_ASN1_VALUE_new_null();
232
0
        if (!skval) {
233
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
234
0
            ret = 0;
235
0
            goto done;
236
0
        }
237
0
        *pval = (ASN1_VALUE *)skval;
238
0
        ret = 1;
239
0
        goto done;
240
0
    }
241
    /* Otherwise pass it back to the item routine */
242
493k
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
243
493k
 done:
244
493k
    return ret;
245
493k
}
246
247
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
248
238k
{
249
    /* If ADB or STACK just NULL the field */
250
238k
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
251
15.0k
        *pval = NULL;
252
223k
    else
253
223k
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
254
238k
}
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
443k
{
264
443k
    ASN1_TYPE *typ;
265
443k
    ASN1_STRING *str;
266
443k
    int utype;
267
268
443k
    if (!it)
269
0
        return 0;
270
271
443k
    if (it->funcs) {
272
5.29k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
273
5.29k
        if (embed) {
274
5.29k
            if (pf->prim_clear) {
275
5.29k
                pf->prim_clear(pval, it);
276
5.29k
                return 1;
277
5.29k
            }
278
5.29k
        } else if (pf->prim_new) {
279
0
            return pf->prim_new(pval, it);
280
0
        }
281
5.29k
    }
282
283
438k
    if (it->itype == ASN1_ITYPE_MSTRING)
284
90.9k
        utype = -1;
285
347k
    else
286
347k
        utype = it->utype;
287
438k
    switch (utype) {
288
196k
    case V_ASN1_OBJECT:
289
196k
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290
196k
        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
55.2k
    case V_ASN1_ANY:
301
55.2k
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
302
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
303
0
            return 0;
304
0
        }
305
55.2k
        typ->value.ptr = NULL;
306
55.2k
        typ->type = -1;
307
55.2k
        *pval = (ASN1_VALUE *)typ;
308
55.2k
        break;
309
310
186k
    default:
311
186k
        if (embed) {
312
90.3k
            str = *(ASN1_STRING **)pval;
313
90.3k
            memset(str, 0, sizeof(*str));
314
90.3k
            str->type = utype;
315
90.3k
            str->flags = ASN1_STRING_FLAG_EMBED;
316
96.3k
        } else {
317
96.3k
            str = ASN1_STRING_type_new(utype);
318
96.3k
            *pval = (ASN1_VALUE *)str;
319
96.3k
        }
320
186k
        if (it->itype == ASN1_ITYPE_MSTRING && str)
321
90.9k
            str->flags |= ASN1_STRING_FLAG_MSTRING;
322
186k
        break;
323
438k
    }
324
241k
    if (*pval)
325
241k
        return 1;
326
0
    return 0;
327
241k
}
328
329
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
330
218k
{
331
218k
    int utype;
332
218k
    if (it && it->funcs) {
333
1.26k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
334
1.26k
        if (pf->prim_clear)
335
1.26k
            pf->prim_clear(pval, it);
336
0
        else
337
0
            *pval = NULL;
338
1.26k
        return;
339
1.26k
    }
340
217k
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
341
0
        utype = -1;
342
217k
    else
343
217k
        utype = it->utype;
344
217k
    if (utype == V_ASN1_BOOLEAN)
345
75.2k
        *(ASN1_BOOLEAN *)pval = it->size;
346
141k
    else
347
141k
        *pval = NULL;
348
217k
}