xref: /openbsd-src/lib/libcrypto/asn1/tasn_new.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: tasn_new.c,v 1.11 2014/06/12 15:49:27 deraadt Exp $ */
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 <openssl/asn1.h>
62 #include <openssl/objects.h>
63 #include <openssl/err.h>
64 #include <openssl/asn1t.h>
65 #include <string.h>
66 
67 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68     int combine);
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);
72 
73 ASN1_VALUE *
74 ASN1_item_new(const ASN1_ITEM *it)
75 {
76 	ASN1_VALUE *ret = NULL;
77 	if (ASN1_item_ex_new(&ret, it) > 0)
78 		return ret;
79 	return NULL;
80 }
81 
82 /* Allocate an ASN1 structure */
83 
84 int
85 ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
86 {
87 	return asn1_item_ex_combine_new(pval, it, 0);
88 }
89 
90 static int
91 asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
92 {
93 	const ASN1_TEMPLATE *tt = NULL;
94 	const ASN1_COMPAT_FUNCS *cf;
95 	const ASN1_EXTERN_FUNCS *ef;
96 	const ASN1_AUX *aux = it->funcs;
97 	ASN1_aux_cb *asn1_cb;
98 	ASN1_VALUE **pseqval;
99 	int i;
100 
101 	if (aux && aux->asn1_cb)
102 		asn1_cb = aux->asn1_cb;
103 	else
104 		asn1_cb = 0;
105 
106 	if (!combine)
107 		*pval = NULL;
108 
109 #ifdef CRYPTO_MDEBUG
110 	if (it->sname)
111 		CRYPTO_push_info(it->sname);
112 #endif
113 
114 	switch (it->itype) {
115 	case ASN1_ITYPE_EXTERN:
116 		ef = it->funcs;
117 		if (ef && ef->asn1_ex_new) {
118 			if (!ef->asn1_ex_new(pval, it))
119 				goto memerr;
120 		}
121 		break;
122 
123 	case ASN1_ITYPE_COMPAT:
124 		cf = it->funcs;
125 		if (cf && cf->asn1_new) {
126 			*pval = cf->asn1_new();
127 			if (!*pval)
128 				goto memerr;
129 		}
130 		break;
131 
132 	case ASN1_ITYPE_PRIMITIVE:
133 		if (it->templates) {
134 			if (!ASN1_template_new(pval, it->templates))
135 				goto memerr;
136 		} else if (!ASN1_primitive_new(pval, it))
137 			goto memerr;
138 		break;
139 
140 	case ASN1_ITYPE_MSTRING:
141 		if (!ASN1_primitive_new(pval, it))
142 			goto memerr;
143 		break;
144 
145 	case ASN1_ITYPE_CHOICE:
146 		if (asn1_cb) {
147 			i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
148 			if (!i)
149 				goto auxerr;
150 			if (i == 2) {
151 #ifdef CRYPTO_MDEBUG
152 				if (it->sname)
153 					CRYPTO_pop_info();
154 #endif
155 				return 1;
156 			}
157 		}
158 		if (!combine) {
159 			*pval = calloc(1, it->size);
160 			if (!*pval)
161 				goto memerr;
162 		}
163 		asn1_set_choice_selector(pval, -1, it);
164 		if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
165 			goto auxerr;
166 		break;
167 
168 	case ASN1_ITYPE_NDEF_SEQUENCE:
169 	case ASN1_ITYPE_SEQUENCE:
170 		if (asn1_cb) {
171 			i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
172 			if (!i)
173 				goto auxerr;
174 			if (i == 2) {
175 #ifdef CRYPTO_MDEBUG
176 				if (it->sname)
177 					CRYPTO_pop_info();
178 #endif
179 				return 1;
180 			}
181 		}
182 		if (!combine) {
183 			*pval = calloc(1, it->size);
184 			if (!*pval)
185 				goto memerr;
186 			asn1_do_lock(pval, 0, it);
187 			asn1_enc_init(pval, it);
188 		}
189 		for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
190 			pseqval = asn1_get_field_ptr(pval, tt);
191 			if (!ASN1_template_new(pseqval, tt))
192 				goto memerr;
193 		}
194 		if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
195 			goto auxerr;
196 		break;
197 	}
198 #ifdef CRYPTO_MDEBUG
199 	if (it->sname)
200 		CRYPTO_pop_info();
201 #endif
202 	return 1;
203 
204 memerr:
205 	ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
206 #ifdef CRYPTO_MDEBUG
207 	if (it->sname)
208 		CRYPTO_pop_info();
209 #endif
210 	return 0;
211 
212 auxerr:
213 	ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
214 	ASN1_item_ex_free(pval, it);
215 #ifdef CRYPTO_MDEBUG
216 	if (it->sname)
217 		CRYPTO_pop_info();
218 #endif
219 	return 0;
220 
221 }
222 
223 static void
224 asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
225 {
226 	const ASN1_EXTERN_FUNCS *ef;
227 
228 	switch (it->itype) {
229 	case ASN1_ITYPE_EXTERN:
230 		ef = it->funcs;
231 		if (ef && ef->asn1_ex_clear)
232 			ef->asn1_ex_clear(pval, it);
233 		else
234 			*pval = NULL;
235 		break;
236 
237 	case ASN1_ITYPE_PRIMITIVE:
238 		if (it->templates)
239 			asn1_template_clear(pval, it->templates);
240 		else
241 			asn1_primitive_clear(pval, it);
242 		break;
243 
244 	case ASN1_ITYPE_MSTRING:
245 		asn1_primitive_clear(pval, it);
246 		break;
247 
248 	case ASN1_ITYPE_COMPAT:
249 	case ASN1_ITYPE_CHOICE:
250 	case ASN1_ITYPE_SEQUENCE:
251 	case ASN1_ITYPE_NDEF_SEQUENCE:
252 		*pval = NULL;
253 		break;
254 	}
255 }
256 
257 int
258 ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
259 {
260 	const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
261 	int ret;
262 
263 	if (tt->flags & ASN1_TFLG_OPTIONAL) {
264 		asn1_template_clear(pval, tt);
265 		return 1;
266 	}
267 	/* If ANY DEFINED BY nothing to do */
268 
269 	if (tt->flags & ASN1_TFLG_ADB_MASK) {
270 		*pval = NULL;
271 		return 1;
272 	}
273 #ifdef CRYPTO_MDEBUG
274 	if (tt->field_name)
275 		CRYPTO_push_info(tt->field_name);
276 #endif
277 	/* If SET OF or SEQUENCE OF, its a STACK */
278 	if (tt->flags & ASN1_TFLG_SK_MASK) {
279 		STACK_OF(ASN1_VALUE) *skval;
280 		skval = sk_ASN1_VALUE_new_null();
281 		if (!skval) {
282 			ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
283 			ret = 0;
284 			goto done;
285 		}
286 		*pval = (ASN1_VALUE *)skval;
287 		ret = 1;
288 		goto done;
289 	}
290 	/* Otherwise pass it back to the item routine */
291 	ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
292 done:
293 #ifdef CRYPTO_MDEBUG
294 	if (it->sname)
295 		CRYPTO_pop_info();
296 #endif
297 	return ret;
298 }
299 
300 static void
301 asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
302 {
303 	/* If ADB or STACK just NULL the field */
304 	if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
305 		*pval = NULL;
306 	else
307 		asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
308 }
309 
310 
311 /* NB: could probably combine most of the real XXX_new() behaviour and junk
312  * all the old functions.
313  */
314 
315 int
316 ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
317 {
318 	ASN1_TYPE *typ;
319 	ASN1_STRING *str;
320 	int utype;
321 
322 	if (it && it->funcs) {
323 		const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
324 		if (pf->prim_new)
325 			return pf->prim_new(pval, it);
326 	}
327 
328 	if (!it || (it->itype == ASN1_ITYPE_MSTRING))
329 		utype = -1;
330 	else
331 		utype = it->utype;
332 	switch (utype) {
333 	case V_ASN1_OBJECT:
334 		*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
335 		return 1;
336 
337 	case V_ASN1_BOOLEAN:
338 		*(ASN1_BOOLEAN *)pval = it->size;
339 		return 1;
340 
341 	case V_ASN1_NULL:
342 		*pval = (ASN1_VALUE *)1;
343 		return 1;
344 
345 	case V_ASN1_ANY:
346 		typ = malloc(sizeof(ASN1_TYPE));
347 		if (!typ)
348 			return 0;
349 		typ->value.ptr = NULL;
350 		typ->type = -1;
351 		*pval = (ASN1_VALUE *)typ;
352 		break;
353 
354 	default:
355 		str = ASN1_STRING_type_new(utype);
356 		if (it->itype == ASN1_ITYPE_MSTRING && str)
357 			str->flags |= ASN1_STRING_FLAG_MSTRING;
358 		*pval = (ASN1_VALUE *)str;
359 		break;
360 	}
361 	if (*pval)
362 		return 1;
363 	return 0;
364 }
365 
366 static void
367 asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
368 {
369 	int utype;
370 	if (it && it->funcs) {
371 		const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
372 		if (pf->prim_clear)
373 			pf->prim_clear(pval, it);
374 		else
375 			*pval = NULL;
376 		return;
377 	}
378 	if (!it || (it->itype == ASN1_ITYPE_MSTRING))
379 		utype = -1;
380 	else
381 		utype = it->utype;
382 	if (utype == V_ASN1_BOOLEAN)
383 		*(ASN1_BOOLEAN *)pval = it->size;
384 	else
385 		*pval = NULL;
386 }
387