Debian Codegraph @

external/boringssl/crypto/asn1/tasn_new.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <string.h>
60
61 #include <openssl/asn1t.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65
66 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
67 int combine);
68 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
69 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
70 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
71
72 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
73 {
74 ASN1_VALUE *ret = NULL;
75 if (ASN1_item_ex_new(&ret, it) > 0)
76 return ret;
77 return NULL;
78 }
79
80 /* Allocate an ASN1 structure */
81
82 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
83 {
84 return asn1_item_ex_combine_new(pval, it, 0);
85 }
86
87 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
88 int combine)
89 {
90 const ASN1_TEMPLATE *tt = NULL;
91 const ASN1_COMPAT_FUNCS *cf;
92 const ASN1_EXTERN_FUNCS *ef;
93 const ASN1_AUX *aux = it->funcs;
94 ASN1_aux_cb *asn1_cb;
95 ASN1_VALUE **pseqval;
96 int i;
97 if (aux && aux->asn1_cb)
98 asn1_cb = aux->asn1_cb;
99 else
100 asn1_cb = 0;
101
102 #ifdef CRYPTO_MDEBUG
103 if (it->sname)
104 CRYPTO_push_info(it->sname);
105 #endif
106
107 switch (it->itype) {
108
109 case ASN1_ITYPE_EXTERN:
110 ef = it->funcs;
111 if (ef && ef->asn1_ex_new) {
112 if (!ef->asn1_ex_new(pval, it))
113 goto memerr;
114 }
115 break;
116
117 case ASN1_ITYPE_COMPAT:
118 cf = it->funcs;
119 if (cf && cf->asn1_new) {
120 *pval = cf->asn1_new();
121 if (!*pval)
122 goto memerr;
123 }
124 break;
125
126 case ASN1_ITYPE_PRIMITIVE:
127 if (it->templates) {
128 if (!ASN1_template_new(pval, it->templates))
129 goto memerr;
130 } else if (!ASN1_primitive_new(pval, it))
131 goto memerr;
132 break;
133
134 case ASN1_ITYPE_MSTRING:
135 if (!ASN1_primitive_new(pval, it))
136 goto memerr;
137 break;
138
139 case ASN1_ITYPE_CHOICE:
140 if (asn1_cb) {
141 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
142 if (!i)
143 goto auxerr;
144 if (i == 2) {
145 #ifdef CRYPTO_MDEBUG
146 if (it->sname)
147 CRYPTO_pop_info();
148 #endif
149 return 1;
150 }
151 }
152 if (!combine) {
153 *pval = OPENSSL_malloc(it->size);
154 if (!*pval)
155 goto memerr;
156 memset(*pval, 0, it->size);
157 }
158 asn1_set_choice_selector(pval, -1, it);
159 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
160 goto auxerr;
161 break;
162
163 case ASN1_ITYPE_NDEF_SEQUENCE:
164 case ASN1_ITYPE_SEQUENCE:
165 if (asn1_cb) {
166 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
167 if (!i)
168 goto auxerr;
169 if (i == 2) {
170 #ifdef CRYPTO_MDEBUG
171 if (it->sname)
172 CRYPTO_pop_info();
173 #endif
174 return 1;
175 }
176 }
177 if (!combine) {
178 *pval = OPENSSL_malloc(it->size);
179 if (!*pval)
180 goto memerr;
181 memset(*pval, 0, it->size);
182 asn1_refcount_set_one(pval, it);
183 asn1_enc_init(pval, it);
184 }
185 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
186 pseqval = asn1_get_field_ptr(pval, tt);
187 if (!ASN1_template_new(pseqval, tt))
188 goto memerr;
189 }
190 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
191 goto auxerr;
192 break;
193 }
194 #ifdef CRYPTO_MDEBUG
195 if (it->sname)
196 CRYPTO_pop_info();
197 #endif
198 return 1;
199
200 memerr:
201 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
202 ASN1_item_ex_free(pval, it);
203 #ifdef CRYPTO_MDEBUG
204 if (it->sname)
205 CRYPTO_pop_info();
206 #endif
207 return 0;
208
209 auxerr:
210 OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
211 ASN1_item_ex_free(pval, it);
212 #ifdef CRYPTO_MDEBUG
213 if (it->sname)
214 CRYPTO_pop_info();
215 #endif
216 return 0;
217
218 }
219
220 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
221 {
222 const ASN1_EXTERN_FUNCS *ef;
223
224 switch (it->itype) {
225
226 case ASN1_ITYPE_EXTERN:
227 ef = it->funcs;
228 if (ef && ef->asn1_ex_clear)
229 ef->asn1_ex_clear(pval, it);
230 else
231 *pval = NULL;
232 break;
233
234 case ASN1_ITYPE_PRIMITIVE:
235 if (it->templates)
236 asn1_template_clear(pval, it->templates);
237 else
238 asn1_primitive_clear(pval, it);
239 break;
240
241 case ASN1_ITYPE_MSTRING:
242 asn1_primitive_clear(pval, it);
243 break;
244
245 case ASN1_ITYPE_COMPAT:
246 case ASN1_ITYPE_CHOICE:
247 case ASN1_ITYPE_SEQUENCE:
248 case ASN1_ITYPE_NDEF_SEQUENCE:
249 *pval = NULL;
250 break;
251 }
252 }
253
254 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
255 {
256 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
257 int ret;
258 if (tt->flags & ASN1_TFLG_OPTIONAL) {
259 asn1_template_clear(pval, tt);
260 return 1;
261 }
262 /* If ANY DEFINED BY nothing to do */
263
264 if (tt->flags & ASN1_TFLG_ADB_MASK) {
265 *pval = NULL;
266 return 1;
267 }
268 #ifdef CRYPTO_MDEBUG
269 if (tt->field_name)
270 CRYPTO_push_info(tt->field_name);
271 #endif
272 /* If SET OF or SEQUENCE OF, its a STACK */
273 if (tt->flags & ASN1_TFLG_SK_MASK) {
274 STACK_OF(ASN1_VALUE) *skval;
275 skval = sk_ASN1_VALUE_new_null();
276 if (!skval) {
277 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
278 ret = 0;
279 goto done;
280 }
281 *pval = (ASN1_VALUE *)skval;
282 ret = 1;
283 goto done;
284 }
285 /* Otherwise pass it back to the item routine */
286 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
287 done:
288 #ifdef CRYPTO_MDEBUG
289 if (it->sname)
290 CRYPTO_pop_info();
291 #endif
292 return ret;
293 }
294
295 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
296 {
297 /* If ADB or STACK just NULL the field */
298 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
299 *pval = NULL;
300 else
301 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
302 }
303
304 /*
305 * NB: could probably combine most of the real XXX_new() behaviour and junk
306 * all the old functions.
307 */
308
309 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
310 {
311 ASN1_TYPE *typ;
312 ASN1_STRING *str;
313 int utype;
314
315 if (!it)
316 return 0;
317
318 if (it->funcs) {
319 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
320 if (pf->prim_new)
321 return pf->prim_new(pval, it);
322 }
323
324 if (it->itype == ASN1_ITYPE_MSTRING)
325 utype = -1;
326 else
327 utype = it->utype;
328 switch (utype) {
329 case V_ASN1_OBJECT:
330 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
331 return 1;
332
333 case V_ASN1_BOOLEAN:
334 *(ASN1_BOOLEAN *)pval = it->size;
335 return 1;
336
337 case V_ASN1_NULL:
338 *pval = (ASN1_VALUE *)1;
339 return 1;
340
341 case V_ASN1_ANY:
342 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
343 if (!typ)
344 return 0;
345 typ->value.ptr = NULL;
346 typ->type = -1;
347 *pval = (ASN1_VALUE *)typ;
348 break;
349
350 default:
351 str = ASN1_STRING_type_new(utype);
352 if (it->itype == ASN1_ITYPE_MSTRING && str)
353 str->flags |= ASN1_STRING_FLAG_MSTRING;
354 *pval = (ASN1_VALUE *)str;
355 break;
356 }
357 if (*pval)
358 return 1;
359 return 0;
360 }
361
362 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
363 {
364 int utype;
365 if (it && it->funcs) {
366 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
367 if (pf->prim_clear)
368 pf->prim_clear(pval, it);
369 else
370 *pval = NULL;
371 return;
372 }
373 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
374 utype = -1;
375 else
376 utype = it->utype;
377 if (utype == V_ASN1_BOOLEAN)
378 *(ASN1_BOOLEAN *)pval = it->size;
379 else
380 *pval = NULL;
381 }