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