1/* tasn_new.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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
18 *    distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by the OpenSSL Project
37 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58
59
60#include <stddef.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <time.h>
64
65#if 1
66#include "cs-asn1.h"
67#include "cs-objects.h"
68#include "cs-err.h"
69#include "cs-asn1t.h"
70
71#else
72
73#include <openssl/asn1.h>
74#include <openssl/objects.h>
75#include <openssl/err.h>
76#include <openssl/asn1t.h>
77#endif
78
79#include <string.h>
80
81static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
82								int combine);
83static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
84static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
85void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
86
87ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
88	{
89	ASN1_VALUE *ret = NULL;
90	if (ASN1_item_ex_new(&ret, it) > 0)
91		return ret;
92	return NULL;
93	}
94
95/* Allocate an ASN1 structure */
96
97int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
98	{
99	return asn1_item_ex_combine_new(pval, it, 0);
100	}
101
102static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
103								int combine)
104	{
105	const ASN1_TEMPLATE *tt = NULL;
106	const ASN1_COMPAT_FUNCS *cf;
107	const ASN1_EXTERN_FUNCS *ef;
108	const ASN1_AUX *aux = it->funcs;
109	ASN1_aux_cb *asn1_cb;
110	ASN1_VALUE **pseqval;
111	int i;
112	if (aux && aux->asn1_cb)
113		asn1_cb = aux->asn1_cb;
114	else
115		asn1_cb = 0;
116
117	if (!combine) *pval = NULL;
118
119#ifdef CRYPTO_MDEBUG
120	if (it->sname)
121		CRYPTO_push_info(it->sname);
122#endif
123
124	switch(it->itype)
125		{
126
127		case ASN1_ITYPE_EXTERN:
128		ef = it->funcs;
129		if (ef && ef->asn1_ex_new)
130			{
131			if (!ef->asn1_ex_new(pval, it))
132				goto memerr;
133			}
134		break;
135
136		case ASN1_ITYPE_COMPAT:
137		cf = it->funcs;
138		if (cf && cf->asn1_new) {
139			*pval = cf->asn1_new();
140			if (!*pval)
141				goto memerr;
142		}
143		break;
144
145		case ASN1_ITYPE_PRIMITIVE:
146		if (it->templates)
147			{
148			if (!ASN1_template_new(pval, it->templates))
149				goto memerr;
150			}
151		else if (!ASN1_primitive_new(pval, it))
152				goto memerr;
153		break;
154
155		case ASN1_ITYPE_MSTRING:
156		if (!ASN1_primitive_new(pval, it))
157				goto memerr;
158		break;
159
160		case ASN1_ITYPE_CHOICE:
161		if (asn1_cb)
162			{
163			i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
164			if (!i)
165				goto auxerr;
166			if (i==2)
167				{
168#ifdef CRYPTO_MDEBUG
169				if (it->sname)
170					CRYPTO_pop_info();
171#endif
172				return 1;
173				}
174			}
175		if (!combine)
176			{
177			*pval = malloc(it->size);
178			if (!*pval)
179				goto memerr;
180			memset(*pval, 0, it->size);
181			}
182		asn1_set_choice_selector(pval, -1, it);
183		if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
184				goto auxerr;
185		break;
186
187		case ASN1_ITYPE_NDEF_SEQUENCE:
188		case ASN1_ITYPE_SEQUENCE:
189		if (asn1_cb)
190			{
191			i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
192			if (!i)
193				goto auxerr;
194			if (i==2)
195				{
196#ifdef CRYPTO_MDEBUG
197				if (it->sname)
198					CRYPTO_pop_info();
199#endif
200				return 1;
201				}
202			}
203		if (!combine)
204			{
205			*pval = malloc(it->size);
206			if (!*pval)
207				goto memerr;
208			memset(*pval, 0, it->size);
209			asn1_do_lock(pval, 0, it);
210			asn1_enc_init(pval, it);
211			}
212		for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
213			{
214			pseqval = asn1_get_field_ptr(pval, tt);
215			if (!ASN1_template_new(pseqval, tt))
216				goto memerr;
217			}
218		if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
219				goto auxerr;
220		break;
221	}
222#ifdef CRYPTO_MDEBUG
223	if (it->sname) CRYPTO_pop_info();
224#endif
225	return 1;
226
227	memerr:
228	/* ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE); */
229#ifdef CRYPTO_MDEBUG
230	if (it->sname) CRYPTO_pop_info();
231#endif
232	return 0;
233
234	auxerr:
235	/* ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR); */
236	ASN1_item_ex_free(pval, it);
237#ifdef CRYPTO_MDEBUG
238	if (it->sname) CRYPTO_pop_info();
239#endif
240	return 0;
241
242	}
243
244static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
245	{
246	const ASN1_EXTERN_FUNCS *ef;
247
248	switch(it->itype)
249		{
250
251		case ASN1_ITYPE_EXTERN:
252		ef = it->funcs;
253		if (ef && ef->asn1_ex_clear)
254			ef->asn1_ex_clear(pval, it);
255		else *pval = NULL;
256		break;
257
258
259		case ASN1_ITYPE_PRIMITIVE:
260		if (it->templates)
261			asn1_template_clear(pval, it->templates);
262		else
263			asn1_primitive_clear(pval, it);
264		break;
265
266		case ASN1_ITYPE_MSTRING:
267		asn1_primitive_clear(pval, it);
268		break;
269
270		case ASN1_ITYPE_COMPAT:
271		case ASN1_ITYPE_CHOICE:
272		case ASN1_ITYPE_SEQUENCE:
273		case ASN1_ITYPE_NDEF_SEQUENCE:
274		*pval = NULL;
275		break;
276		}
277	}
278
279
280int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
281	{
282	const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
283	int ret;
284	if (tt->flags & ASN1_TFLG_OPTIONAL)
285		{
286		asn1_template_clear(pval, tt);
287		return 1;
288		}
289	/* If ANY DEFINED BY nothing to do */
290
291	if (tt->flags & ASN1_TFLG_ADB_MASK)
292		{
293		*pval = NULL;
294		return 1;
295		}
296#ifdef CRYPTO_MDEBUG
297	if (tt->field_name)
298		CRYPTO_push_info(tt->field_name);
299#endif
300	/* If SET OF or SEQUENCE OF, its a STACK */
301	if (tt->flags & ASN1_TFLG_SK_MASK)
302		{
303		STACK_OF(ASN1_VALUE) *skval;
304		skval = sk_ASN1_VALUE_new_null();
305		if (!skval)
306			{
307			/* ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE); */
308			ret = 0;
309			goto done;
310			}
311		*pval = (ASN1_VALUE *)skval;
312		ret = 1;
313		goto done;
314		}
315	/* Otherwise pass it back to the item routine */
316	ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
317	done:
318#ifdef CRYPTO_MDEBUG
319	if (it->sname)
320		CRYPTO_pop_info();
321#endif
322	return ret;
323	}
324
325static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
326	{
327	/* If ADB or STACK just NULL the field */
328	if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
329		*pval = NULL;
330	else
331		asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
332	}
333
334
335/* NB: could probably combine most of the real XXX_new() behaviour and junk
336 * all the old functions.
337 */
338
339int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
340	{
341	ASN1_TYPE *typ;
342	int utype;
343
344	if (it && it->funcs)
345		{
346		const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
347		if (pf->prim_new)
348			return pf->prim_new(pval, it);
349		}
350
351	if (!it || (it->itype == ASN1_ITYPE_MSTRING))
352		utype = -1;
353	else
354		utype = it->utype;
355	switch(utype)
356		{
357		case V_ASN1_OBJECT:
358		*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
359		return 1;
360
361		case V_ASN1_BOOLEAN:
362		if (it)
363			*(ASN1_BOOLEAN *)pval = it->size;
364		else
365			*(ASN1_BOOLEAN *)pval = -1;
366		return 1;
367
368		case V_ASN1_NULL:
369		*pval = (ASN1_VALUE *)1;
370		return 1;
371
372		case V_ASN1_ANY:
373		typ = malloc(sizeof(ASN1_TYPE));
374		if (!typ)
375			return 0;
376		typ->value.ptr = NULL;
377		typ->type = -1;
378		*pval = (ASN1_VALUE *)typ;
379		break;
380
381		default:
382		*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
383		break;
384		}
385	if (*pval)
386		return 1;
387	return 0;
388	}
389
390void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
391	{
392	int utype;
393	if (it && it->funcs)
394		{
395		const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
396		if (pf->prim_clear)
397			pf->prim_clear(pval, it);
398		else
399			*pval = NULL;
400		return;
401		}
402	if (!it || (it->itype == ASN1_ITYPE_MSTRING))
403		utype = -1;
404	else
405		utype = it->utype;
406	if (utype == V_ASN1_BOOLEAN)
407		*(ASN1_BOOLEAN *)pval = it->size;
408	else *pval = NULL;
409	}
410