2 * Copyright 2000-2018 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_locl.h"
18 static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
20 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
22 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
23 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
24 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
25 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
27 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
29 ASN1_VALUE *ret = NULL;
30 if (ASN1_item_ex_new(&ret, it) > 0)
35 /* Allocate an ASN1 structure */
37 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
39 return asn1_item_embed_new(pval, it, 0);
42 int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
44 const ASN1_TEMPLATE *tt = NULL;
45 const ASN1_EXTERN_FUNCS *ef;
46 const ASN1_AUX *aux = it->funcs;
50 if (aux && aux->asn1_cb)
51 asn1_cb = aux->asn1_cb;
55 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
56 OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
61 case ASN1_ITYPE_EXTERN:
63 if (ef && ef->asn1_ex_new) {
64 if (!ef->asn1_ex_new(pval, it))
69 case ASN1_ITYPE_PRIMITIVE:
71 if (!asn1_template_new(pval, it->templates))
73 } else if (!asn1_primitive_new(pval, it, embed))
77 case ASN1_ITYPE_MSTRING:
78 if (!asn1_primitive_new(pval, it, embed))
82 case ASN1_ITYPE_CHOICE:
84 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
88 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
89 OPENSSL_mem_debug_pop();
95 memset(*pval, 0, it->size);
97 *pval = OPENSSL_zalloc(it->size);
101 asn1_set_choice_selector(pval, -1, it);
102 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
106 case ASN1_ITYPE_NDEF_SEQUENCE:
107 case ASN1_ITYPE_SEQUENCE:
109 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
113 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
114 OPENSSL_mem_debug_pop();
120 memset(*pval, 0, it->size);
122 *pval = OPENSSL_zalloc(it->size);
127 if (asn1_do_lock(pval, 0, it) < 0) {
134 asn1_enc_init(pval, it);
135 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
136 pseqval = asn1_get_field_ptr(pval, tt);
137 if (!asn1_template_new(pseqval, tt))
140 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
144 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
145 OPENSSL_mem_debug_pop();
150 asn1_item_embed_free(pval, it, embed);
152 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
153 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
154 OPENSSL_mem_debug_pop();
159 asn1_item_embed_free(pval, it, embed);
161 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
162 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
163 OPENSSL_mem_debug_pop();
169 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
171 const ASN1_EXTERN_FUNCS *ef;
175 case ASN1_ITYPE_EXTERN:
177 if (ef && ef->asn1_ex_clear)
178 ef->asn1_ex_clear(pval, it);
183 case ASN1_ITYPE_PRIMITIVE:
185 asn1_template_clear(pval, it->templates);
187 asn1_primitive_clear(pval, it);
190 case ASN1_ITYPE_MSTRING:
191 asn1_primitive_clear(pval, it);
194 case ASN1_ITYPE_CHOICE:
195 case ASN1_ITYPE_SEQUENCE:
196 case ASN1_ITYPE_NDEF_SEQUENCE:
202 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
204 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
205 int embed = tt->flags & ASN1_TFLG_EMBED;
209 tval = (ASN1_VALUE *)pval;
212 if (tt->flags & ASN1_TFLG_OPTIONAL) {
213 asn1_template_clear(pval, tt);
216 /* If ANY DEFINED BY nothing to do */
218 if (tt->flags & ASN1_TFLG_ADB_MASK) {
222 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
223 OPENSSL_mem_debug_push(tt->field_name
224 ? tt->field_name : "asn1_template_new");
226 /* If SET OF or SEQUENCE OF, its a STACK */
227 if (tt->flags & ASN1_TFLG_SK_MASK) {
228 STACK_OF(ASN1_VALUE) *skval;
229 skval = sk_ASN1_VALUE_new_null();
231 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
235 *pval = (ASN1_VALUE *)skval;
239 /* Otherwise pass it back to the item routine */
240 ret = asn1_item_embed_new(pval, it, embed);
242 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
243 OPENSSL_mem_debug_pop();
248 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
250 /* If ADB or STACK just NULL the field */
251 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
254 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
258 * NB: could probably combine most of the real XXX_new() behaviour and junk
259 * all the old functions.
262 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
273 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
275 if (pf->prim_clear) {
276 pf->prim_clear(pval, it);
279 } else if (pf->prim_new) {
280 return pf->prim_new(pval, it);
284 if (it->itype == ASN1_ITYPE_MSTRING)
290 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
294 *(ASN1_BOOLEAN *)pval = it->size;
298 *pval = (ASN1_VALUE *)1;
302 if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
303 ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
306 typ->value.ptr = NULL;
308 *pval = (ASN1_VALUE *)typ;
313 str = *(ASN1_STRING **)pval;
314 memset(str, 0, sizeof(*str));
316 str->flags = ASN1_STRING_FLAG_EMBED;
318 str = ASN1_STRING_type_new(utype);
319 *pval = (ASN1_VALUE *)str;
321 if (it->itype == ASN1_ITYPE_MSTRING && str)
322 str->flags |= ASN1_STRING_FLAG_MSTRING;
330 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
333 if (it && it->funcs) {
334 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
336 pf->prim_clear(pval, it);
341 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
345 if (utype == V_ASN1_BOOLEAN)
346 *(ASN1_BOOLEAN *)pval = it->size;