2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/asn1.h>
12 #include <openssl/objects.h>
13 #include <openssl/err.h>
14 #include <openssl/asn1t.h>
16 #include "asn1_local.h"
18 DEFINE_STACK_OF(ASN1_VALUE)
20 static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
22 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
24 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
25 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
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);
29 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
31 ASN1_VALUE *ret = NULL;
32 if (ASN1_item_ex_new(&ret, it) > 0)
37 /* Allocate an ASN1 structure */
39 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
41 return asn1_item_embed_new(pval, it, 0);
44 int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
46 const ASN1_TEMPLATE *tt = NULL;
47 const ASN1_EXTERN_FUNCS *ef;
48 const ASN1_AUX *aux = it->funcs;
52 if (aux && aux->asn1_cb)
53 asn1_cb = aux->asn1_cb;
59 case ASN1_ITYPE_EXTERN:
61 if (ef && ef->asn1_ex_new) {
62 if (!ef->asn1_ex_new(pval, it))
67 case ASN1_ITYPE_PRIMITIVE:
69 if (!asn1_template_new(pval, it->templates))
71 } else if (!asn1_primitive_new(pval, it, embed))
75 case ASN1_ITYPE_MSTRING:
76 if (!asn1_primitive_new(pval, it, embed))
80 case ASN1_ITYPE_CHOICE:
82 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
90 memset(*pval, 0, it->size);
92 *pval = OPENSSL_zalloc(it->size);
96 asn1_set_choice_selector(pval, -1, it);
97 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
101 case ASN1_ITYPE_NDEF_SEQUENCE:
102 case ASN1_ITYPE_SEQUENCE:
104 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
112 memset(*pval, 0, it->size);
114 *pval = OPENSSL_zalloc(it->size);
119 if (asn1_do_lock(pval, 0, it) < 0) {
126 asn1_enc_init(pval, it);
127 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
128 pseqval = asn1_get_field_ptr(pval, tt);
129 if (!asn1_template_new(pseqval, tt))
132 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
139 asn1_item_embed_free(pval, it, embed);
141 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
145 asn1_item_embed_free(pval, it, embed);
147 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
152 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
154 const ASN1_EXTERN_FUNCS *ef;
158 case ASN1_ITYPE_EXTERN:
160 if (ef && ef->asn1_ex_clear)
161 ef->asn1_ex_clear(pval, it);
166 case ASN1_ITYPE_PRIMITIVE:
168 asn1_template_clear(pval, it->templates);
170 asn1_primitive_clear(pval, it);
173 case ASN1_ITYPE_MSTRING:
174 asn1_primitive_clear(pval, it);
177 case ASN1_ITYPE_CHOICE:
178 case ASN1_ITYPE_SEQUENCE:
179 case ASN1_ITYPE_NDEF_SEQUENCE:
185 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
187 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
188 int embed = tt->flags & ASN1_TFLG_EMBED;
192 tval = (ASN1_VALUE *)pval;
195 if (tt->flags & ASN1_TFLG_OPTIONAL) {
196 asn1_template_clear(pval, tt);
199 /* If ANY DEFINED BY nothing to do */
201 if (tt->flags & ASN1_TFLG_ADB_MASK) {
205 /* If SET OF or SEQUENCE OF, its a STACK */
206 if (tt->flags & ASN1_TFLG_SK_MASK) {
207 STACK_OF(ASN1_VALUE) *skval;
208 skval = sk_ASN1_VALUE_new_null();
210 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
214 *pval = (ASN1_VALUE *)skval;
218 /* Otherwise pass it back to the item routine */
219 ret = asn1_item_embed_new(pval, it, embed);
224 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
226 /* If ADB or STACK just NULL the field */
227 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
230 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
234 * NB: could probably combine most of the real XXX_new() behaviour and junk
235 * all the old functions.
238 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
249 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
251 if (pf->prim_clear) {
252 pf->prim_clear(pval, it);
255 } else if (pf->prim_new) {
256 return pf->prim_new(pval, it);
260 if (it->itype == ASN1_ITYPE_MSTRING)
266 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
270 *(ASN1_BOOLEAN *)pval = it->size;
274 *pval = (ASN1_VALUE *)1;
278 if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
279 ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
282 typ->value.ptr = NULL;
284 *pval = (ASN1_VALUE *)typ;
289 str = *(ASN1_STRING **)pval;
290 memset(str, 0, sizeof(*str));
292 str->flags = ASN1_STRING_FLAG_EMBED;
294 str = ASN1_STRING_type_new(utype);
295 *pval = (ASN1_VALUE *)str;
297 if (it->itype == ASN1_ITYPE_MSTRING && str)
298 str->flags |= ASN1_STRING_FLAG_MSTRING;
306 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
309 if (it && it->funcs) {
310 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
312 pf->prim_clear(pval, it);
317 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
321 if (utype == V_ASN1_BOOLEAN)
322 *(ASN1_BOOLEAN *)pval = it->size;