1 /* $OpenBSD: tasn_new.c,v 1.17 2017/01/29 17:49:22 beck Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1.h>
62 #include <openssl/objects.h>
63 #include <openssl/err.h>
64 #include <openssl/asn1t.h>
67 static int asn1_item_ex_combine_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
69 static void asn1_item_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
70 static void asn1_template_clear(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
);
71 static void asn1_primitive_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
74 ASN1_item_new(const ASN1_ITEM
*it
)
76 ASN1_VALUE
*ret
= NULL
;
77 if (ASN1_item_ex_new(&ret
, it
) > 0)
82 /* Allocate an ASN1 structure */
85 ASN1_item_ex_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
87 return asn1_item_ex_combine_new(pval
, it
, 0);
91 asn1_item_ex_combine_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
, int combine
)
93 const ASN1_TEMPLATE
*tt
= NULL
;
94 const ASN1_EXTERN_FUNCS
*ef
;
95 const ASN1_AUX
*aux
= it
->funcs
;
96 ASN1_aux_cb
*asn1_cb
= NULL
;
100 if (aux
!= NULL
&& aux
->asn1_cb
!= NULL
)
101 asn1_cb
= aux
->asn1_cb
;
108 CRYPTO_push_info(it
->sname
);
112 case ASN1_ITYPE_EXTERN
:
114 if (ef
&& ef
->asn1_ex_new
) {
115 if (!ef
->asn1_ex_new(pval
, it
))
120 case ASN1_ITYPE_PRIMITIVE
:
122 if (!ASN1_template_new(pval
, it
->templates
))
124 } else if (!ASN1_primitive_new(pval
, it
))
128 case ASN1_ITYPE_MSTRING
:
129 if (!ASN1_primitive_new(pval
, it
))
133 case ASN1_ITYPE_CHOICE
:
135 i
= asn1_cb(ASN1_OP_NEW_PRE
, pval
, it
, NULL
);
147 *pval
= calloc(1, it
->size
);
151 asn1_set_choice_selector(pval
, -1, it
);
152 if (asn1_cb
&& !asn1_cb(ASN1_OP_NEW_POST
, pval
, it
, NULL
))
156 case ASN1_ITYPE_NDEF_SEQUENCE
:
157 case ASN1_ITYPE_SEQUENCE
:
159 i
= asn1_cb(ASN1_OP_NEW_PRE
, pval
, it
, NULL
);
171 *pval
= calloc(1, it
->size
);
174 asn1_do_lock(pval
, 0, it
);
175 asn1_enc_init(pval
, it
);
177 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; tt
++, i
++) {
178 pseqval
= asn1_get_field_ptr(pval
, tt
);
179 if (!ASN1_template_new(pseqval
, tt
))
182 if (asn1_cb
&& !asn1_cb(ASN1_OP_NEW_POST
, pval
, it
, NULL
))
193 ASN1error(ERR_R_MALLOC_FAILURE
);
201 ASN1error(ASN1_R_AUX_ERROR
);
202 ASN1_item_ex_free(pval
, it
);
212 asn1_item_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
214 const ASN1_EXTERN_FUNCS
*ef
;
217 case ASN1_ITYPE_EXTERN
:
219 if (ef
&& ef
->asn1_ex_clear
)
220 ef
->asn1_ex_clear(pval
, it
);
225 case ASN1_ITYPE_PRIMITIVE
:
227 asn1_template_clear(pval
, it
->templates
);
229 asn1_primitive_clear(pval
, it
);
232 case ASN1_ITYPE_MSTRING
:
233 asn1_primitive_clear(pval
, it
);
236 case ASN1_ITYPE_CHOICE
:
237 case ASN1_ITYPE_SEQUENCE
:
238 case ASN1_ITYPE_NDEF_SEQUENCE
:
245 ASN1_template_new(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
)
247 const ASN1_ITEM
*it
= tt
->item
;
250 if (tt
->flags
& ASN1_TFLG_OPTIONAL
) {
251 asn1_template_clear(pval
, tt
);
254 /* If ANY DEFINED BY nothing to do */
256 if (tt
->flags
& ASN1_TFLG_ADB_MASK
) {
262 CRYPTO_push_info(tt
->field_name
);
264 /* If SET OF or SEQUENCE OF, its a STACK */
265 if (tt
->flags
& ASN1_TFLG_SK_MASK
) {
266 STACK_OF(ASN1_VALUE
) *skval
;
267 skval
= sk_ASN1_VALUE_new_null();
269 ASN1error(ERR_R_MALLOC_FAILURE
);
273 *pval
= (ASN1_VALUE
*)skval
;
277 /* Otherwise pass it back to the item routine */
278 ret
= asn1_item_ex_combine_new(pval
, it
, tt
->flags
& ASN1_TFLG_COMBINE
);
288 asn1_template_clear(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
)
290 /* If ADB or STACK just NULL the field */
291 if (tt
->flags
& (ASN1_TFLG_ADB_MASK
|ASN1_TFLG_SK_MASK
))
294 asn1_item_clear(pval
, tt
->item
);
298 /* NB: could probably combine most of the real XXX_new() behaviour and junk
299 * all the old functions.
303 ASN1_primitive_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
309 if (it
&& it
->funcs
) {
310 const ASN1_PRIMITIVE_FUNCS
*pf
= it
->funcs
;
312 return pf
->prim_new(pval
, it
);
315 if (!it
|| (it
->itype
== ASN1_ITYPE_MSTRING
))
316 utype
= V_ASN1_UNDEF
;
321 *pval
= (ASN1_VALUE
*)OBJ_nid2obj(NID_undef
);
325 *(ASN1_BOOLEAN
*)pval
= it
->size
;
329 *pval
= (ASN1_VALUE
*)1;
333 typ
= malloc(sizeof(ASN1_TYPE
));
335 typ
->value
.ptr
= NULL
;
336 typ
->type
= V_ASN1_UNDEF
;
338 *pval
= (ASN1_VALUE
*)typ
;
342 str
= ASN1_STRING_type_new(utype
);
343 if (it
!= NULL
&& it
->itype
== ASN1_ITYPE_MSTRING
&&
345 str
->flags
|= ASN1_STRING_FLAG_MSTRING
;
346 *pval
= (ASN1_VALUE
*)str
;
355 asn1_primitive_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
358 if (it
&& it
->funcs
) {
359 const ASN1_PRIMITIVE_FUNCS
*pf
= it
->funcs
;
361 pf
->prim_clear(pval
, it
);
366 if (!it
|| (it
->itype
== ASN1_ITYPE_MSTRING
))
367 utype
= V_ASN1_UNDEF
;
370 if (utype
== V_ASN1_BOOLEAN
)
371 *(ASN1_BOOLEAN
*)pval
= it
->size
;