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