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_local.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;
57 case ASN1_ITYPE_EXTERN:
59 if (ef && ef->asn1_ex_new) {
60 if (!ef->asn1_ex_new(pval, it))
65 case ASN1_ITYPE_PRIMITIVE:
67 if (!asn1_template_new(pval, it->templates))
69 } else if (!asn1_primitive_new(pval, it, embed))
73 case ASN1_ITYPE_MSTRING:
74 if (!asn1_primitive_new(pval, it, embed))
78 case ASN1_ITYPE_CHOICE:
80 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
88 memset(*pval, 0, it->size);
90 *pval = OPENSSL_zalloc(it->size);
94 asn1_set_choice_selector(pval, -1, it);
95 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
99 case ASN1_ITYPE_NDEF_SEQUENCE:
100 case ASN1_ITYPE_SEQUENCE:
102 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
110 memset(*pval, 0, it->size);
112 *pval = OPENSSL_zalloc(it->size);
117 if (asn1_do_lock(pval, 0, it) < 0) {
124 asn1_enc_init(pval, it);
125 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
126 pseqval = asn1_get_field_ptr(pval, tt);
127 if (!asn1_template_new(pseqval, tt))
130 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
137 asn1_item_embed_free(pval, it, embed);
139 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
143 asn1_item_embed_free(pval, it, embed);
145 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
150 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
152 const ASN1_EXTERN_FUNCS *ef;
156 case ASN1_ITYPE_EXTERN:
158 if (ef && ef->asn1_ex_clear)
159 ef->asn1_ex_clear(pval, it);
164 case ASN1_ITYPE_PRIMITIVE:
166 asn1_template_clear(pval, it->templates);
168 asn1_primitive_clear(pval, it);
171 case ASN1_ITYPE_MSTRING:
172 asn1_primitive_clear(pval, it);
175 case ASN1_ITYPE_CHOICE:
176 case ASN1_ITYPE_SEQUENCE:
177 case ASN1_ITYPE_NDEF_SEQUENCE:
183 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
185 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
186 int embed = tt->flags & ASN1_TFLG_EMBED;
190 tval = (ASN1_VALUE *)pval;
193 if (tt->flags & ASN1_TFLG_OPTIONAL) {
194 asn1_template_clear(pval, tt);
197 /* If ANY DEFINED BY nothing to do */
199 if (tt->flags & ASN1_TFLG_ADB_MASK) {
203 /* If SET OF or SEQUENCE OF, its a STACK */
204 if (tt->flags & ASN1_TFLG_SK_MASK) {
205 STACK_OF(ASN1_VALUE) *skval;
206 skval = sk_ASN1_VALUE_new_null();
208 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
212 *pval = (ASN1_VALUE *)skval;
216 /* Otherwise pass it back to the item routine */
217 ret = asn1_item_embed_new(pval, it, embed);
222 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
224 /* If ADB or STACK just NULL the field */
225 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
228 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
232 * NB: could probably combine most of the real XXX_new() behaviour and junk
233 * all the old functions.
236 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
247 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
249 if (pf->prim_clear) {
250 pf->prim_clear(pval, it);
253 } else if (pf->prim_new) {
254 return pf->prim_new(pval, it);
258 if (it->itype == ASN1_ITYPE_MSTRING)
264 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
268 *(ASN1_BOOLEAN *)pval = it->size;
272 *pval = (ASN1_VALUE *)1;
276 if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
277 ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
280 typ->value.ptr = NULL;
282 *pval = (ASN1_VALUE *)typ;
287 str = *(ASN1_STRING **)pval;
288 memset(str, 0, sizeof(*str));
290 str->flags = ASN1_STRING_FLAG_EMBED;
292 str = ASN1_STRING_type_new(utype);
293 *pval = (ASN1_VALUE *)str;
295 if (it->itype == ASN1_ITYPE_MSTRING && str)
296 str->flags |= ASN1_STRING_FLAG_MSTRING;
304 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
307 if (it && it->funcs) {
308 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
310 pf->prim_clear(pval, it);
315 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
319 if (utype == V_ASN1_BOOLEAN)
320 *(ASN1_BOOLEAN *)pval = it->size;