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