/src/openssl/crypto/asn1/tasn_new.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* tasn_new.c */ |
2 | | /* |
3 | | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
4 | | * 2000. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * licensing@OpenSSL.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | * |
54 | | * This product includes cryptographic software written by Eric Young |
55 | | * (eay@cryptsoft.com). This product includes software written by Tim |
56 | | * Hudson (tjh@cryptsoft.com). |
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 | | #include "asn1_int.h" |
67 | | |
68 | | static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
69 | | int combine); |
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 *ASN1_item_new(const ASN1_ITEM *it) |
75 | 1.59M | { |
76 | 1.59M | ASN1_VALUE *ret = NULL; |
77 | 1.59M | if (ASN1_item_ex_new(&ret, it) > 0) |
78 | 1.59M | return ret; |
79 | 0 | return NULL; |
80 | 1.59M | } |
81 | | |
82 | | /* Allocate an ASN1 structure */ |
83 | | |
84 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
85 | 3.50M | { |
86 | 3.50M | return asn1_item_ex_combine_new(pval, it, 0); |
87 | 3.50M | } |
88 | | |
89 | | static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
90 | | int combine) |
91 | 11.8M | { |
92 | 11.8M | const ASN1_TEMPLATE *tt = NULL; |
93 | 11.8M | const ASN1_COMPAT_FUNCS *cf; |
94 | 11.8M | const ASN1_EXTERN_FUNCS *ef; |
95 | 11.8M | const ASN1_AUX *aux = it->funcs; |
96 | 11.8M | ASN1_aux_cb *asn1_cb; |
97 | 11.8M | ASN1_VALUE **pseqval; |
98 | 11.8M | int i; |
99 | 11.8M | if (aux && aux->asn1_cb) |
100 | 315k | asn1_cb = aux->asn1_cb; |
101 | 11.5M | else |
102 | 11.5M | asn1_cb = 0; |
103 | | |
104 | | #ifdef CRYPTO_MDEBUG |
105 | | if (it->sname) |
106 | | CRYPTO_push_info(it->sname); |
107 | | #endif |
108 | | |
109 | 11.8M | switch (it->itype) { |
110 | | |
111 | 315k | case ASN1_ITYPE_EXTERN: |
112 | 315k | ef = it->funcs; |
113 | 315k | if (ef && ef->asn1_ex_new) { |
114 | 315k | if (!ef->asn1_ex_new(pval, it)) |
115 | 0 | goto memerr; |
116 | 315k | } |
117 | 315k | break; |
118 | | |
119 | 315k | case ASN1_ITYPE_COMPAT: |
120 | 0 | cf = it->funcs; |
121 | 0 | if (cf && cf->asn1_new) { |
122 | 0 | *pval = cf->asn1_new(); |
123 | 0 | if (!*pval) |
124 | 0 | goto memerr; |
125 | 0 | } |
126 | 0 | break; |
127 | | |
128 | 4.85M | case ASN1_ITYPE_PRIMITIVE: |
129 | 4.85M | if (it->templates) { |
130 | 0 | if (!ASN1_template_new(pval, it->templates)) |
131 | 0 | goto memerr; |
132 | 4.85M | } else if (!ASN1_primitive_new(pval, it)) |
133 | 0 | goto memerr; |
134 | 4.85M | break; |
135 | | |
136 | 4.85M | case ASN1_ITYPE_MSTRING: |
137 | 2.69M | if (!ASN1_primitive_new(pval, it)) |
138 | 0 | goto memerr; |
139 | 2.69M | break; |
140 | | |
141 | 2.69M | case ASN1_ITYPE_CHOICE: |
142 | 0 | if (asn1_cb) { |
143 | 0 | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
144 | 0 | if (!i) |
145 | 0 | goto auxerr; |
146 | 0 | if (i == 2) { |
147 | | #ifdef CRYPTO_MDEBUG |
148 | | if (it->sname) |
149 | | CRYPTO_pop_info(); |
150 | | #endif |
151 | 0 | return 1; |
152 | 0 | } |
153 | 0 | } |
154 | 0 | if (!combine) { |
155 | 0 | *pval = OPENSSL_malloc(it->size); |
156 | 0 | if (!*pval) |
157 | 0 | goto memerr; |
158 | 0 | memset(*pval, 0, it->size); |
159 | 0 | } |
160 | 0 | asn1_set_choice_selector(pval, -1, it); |
161 | 0 | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
162 | 0 | goto auxerr2; |
163 | 0 | break; |
164 | | |
165 | 0 | case ASN1_ITYPE_NDEF_SEQUENCE: |
166 | 4.03M | case ASN1_ITYPE_SEQUENCE: |
167 | 4.03M | if (asn1_cb) { |
168 | 315k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
169 | 315k | if (!i) |
170 | 0 | goto auxerr; |
171 | 315k | if (i == 2) { |
172 | | #ifdef CRYPTO_MDEBUG |
173 | | if (it->sname) |
174 | | CRYPTO_pop_info(); |
175 | | #endif |
176 | 0 | return 1; |
177 | 0 | } |
178 | 315k | } |
179 | 4.03M | if (!combine) { |
180 | 4.03M | *pval = OPENSSL_malloc(it->size); |
181 | 4.03M | if (!*pval) |
182 | 0 | goto memerr; |
183 | 4.03M | memset(*pval, 0, it->size); |
184 | 4.03M | asn1_do_lock(pval, 0, it); |
185 | 4.03M | asn1_enc_init(pval, it); |
186 | 4.03M | } |
187 | 14.0M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
188 | 10.0M | pseqval = asn1_get_field_ptr(pval, tt); |
189 | 10.0M | if (!ASN1_template_new(pseqval, tt)) |
190 | 0 | goto memerr2; |
191 | 10.0M | } |
192 | 4.03M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
193 | 0 | goto auxerr2; |
194 | 4.03M | break; |
195 | 11.8M | } |
196 | | #ifdef CRYPTO_MDEBUG |
197 | | if (it->sname) |
198 | | CRYPTO_pop_info(); |
199 | | #endif |
200 | 11.8M | return 1; |
201 | | |
202 | 0 | memerr2: |
203 | 0 | asn1_item_combine_free(pval, it, combine); |
204 | 0 | memerr: |
205 | 0 | 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 | 0 | return 0; |
211 | | |
212 | 0 | auxerr2: |
213 | 0 | asn1_item_combine_free(pval, it, combine); |
214 | 0 | auxerr: |
215 | 0 | ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR); |
216 | | #ifdef CRYPTO_MDEBUG |
217 | | if (it->sname) |
218 | | CRYPTO_pop_info(); |
219 | | #endif |
220 | 0 | return 0; |
221 | |
|
222 | 0 | } |
223 | | |
224 | | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
225 | 1.50M | { |
226 | 1.50M | const ASN1_EXTERN_FUNCS *ef; |
227 | | |
228 | 1.50M | switch (it->itype) { |
229 | | |
230 | 0 | case ASN1_ITYPE_EXTERN: |
231 | 0 | ef = it->funcs; |
232 | 0 | if (ef && ef->asn1_ex_clear) |
233 | 0 | ef->asn1_ex_clear(pval, it); |
234 | 0 | else |
235 | 0 | *pval = NULL; |
236 | 0 | break; |
237 | | |
238 | 1.50M | case ASN1_ITYPE_PRIMITIVE: |
239 | 1.50M | if (it->templates) |
240 | 0 | asn1_template_clear(pval, it->templates); |
241 | 1.50M | else |
242 | 1.50M | asn1_primitive_clear(pval, it); |
243 | 1.50M | break; |
244 | | |
245 | 0 | case ASN1_ITYPE_MSTRING: |
246 | 0 | asn1_primitive_clear(pval, it); |
247 | 0 | break; |
248 | | |
249 | 0 | case ASN1_ITYPE_COMPAT: |
250 | 0 | case ASN1_ITYPE_CHOICE: |
251 | 0 | case ASN1_ITYPE_SEQUENCE: |
252 | 0 | case ASN1_ITYPE_NDEF_SEQUENCE: |
253 | 0 | *pval = NULL; |
254 | 0 | break; |
255 | 1.50M | } |
256 | 1.50M | } |
257 | | |
258 | | int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
259 | 10.0M | { |
260 | 10.0M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
261 | 10.0M | int ret; |
262 | 10.0M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
263 | 1.66M | asn1_template_clear(pval, tt); |
264 | 1.66M | return 1; |
265 | 1.66M | } |
266 | | /* If ANY DEFINED BY nothing to do */ |
267 | | |
268 | 8.39M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
269 | 0 | *pval = NULL; |
270 | 0 | return 1; |
271 | 0 | } |
272 | | #ifdef CRYPTO_MDEBUG |
273 | | if (tt->field_name) |
274 | | CRYPTO_push_info(tt->field_name); |
275 | | #endif |
276 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
277 | 8.39M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
278 | 0 | STACK_OF(ASN1_VALUE) *skval; |
279 | 0 | skval = sk_ASN1_VALUE_new_null(); |
280 | 0 | if (!skval) { |
281 | 0 | ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE); |
282 | 0 | ret = 0; |
283 | 0 | goto done; |
284 | 0 | } |
285 | 0 | *pval = (ASN1_VALUE *)skval; |
286 | 0 | ret = 1; |
287 | 0 | goto done; |
288 | 0 | } |
289 | | /* Otherwise pass it back to the item routine */ |
290 | 8.39M | ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE); |
291 | 8.39M | done: |
292 | | #ifdef CRYPTO_MDEBUG |
293 | | if (it->sname) |
294 | | CRYPTO_pop_info(); |
295 | | #endif |
296 | 8.39M | return ret; |
297 | 8.39M | } |
298 | | |
299 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
300 | 1.66M | { |
301 | | /* If ADB or STACK just NULL the field */ |
302 | 1.66M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
303 | 157k | *pval = NULL; |
304 | 1.50M | else |
305 | 1.50M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
306 | 1.66M | } |
307 | | |
308 | | /* |
309 | | * NB: could probably combine most of the real XXX_new() behaviour and junk |
310 | | * all the old functions. |
311 | | */ |
312 | | |
313 | | int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
314 | 7.54M | { |
315 | 7.54M | ASN1_TYPE *typ; |
316 | 7.54M | ASN1_STRING *str; |
317 | 7.54M | int utype; |
318 | | |
319 | 7.54M | if (!it) |
320 | 0 | return 0; |
321 | | |
322 | 7.54M | if (it->funcs) { |
323 | 0 | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
324 | 0 | if (pf->prim_new) |
325 | 0 | return pf->prim_new(pval, it); |
326 | 0 | } |
327 | | |
328 | 7.54M | if (it->itype == ASN1_ITYPE_MSTRING) |
329 | 2.69M | utype = -1; |
330 | 4.85M | else |
331 | 4.85M | utype = it->utype; |
332 | 7.54M | switch (utype) { |
333 | 3.40M | case V_ASN1_OBJECT: |
334 | 3.40M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
335 | 3.40M | return 1; |
336 | | |
337 | 0 | case V_ASN1_BOOLEAN: |
338 | 0 | *(ASN1_BOOLEAN *)pval = it->size; |
339 | 0 | return 1; |
340 | | |
341 | 0 | case V_ASN1_NULL: |
342 | 0 | *pval = (ASN1_VALUE *)1; |
343 | 0 | return 1; |
344 | | |
345 | 411k | case V_ASN1_ANY: |
346 | 411k | typ = OPENSSL_malloc(sizeof(ASN1_TYPE)); |
347 | 411k | if (!typ) |
348 | 0 | return 0; |
349 | 411k | typ->value.ptr = NULL; |
350 | 411k | typ->type = -1; |
351 | 411k | *pval = (ASN1_VALUE *)typ; |
352 | 411k | break; |
353 | | |
354 | 3.72M | default: |
355 | 3.72M | str = ASN1_STRING_type_new(utype); |
356 | 3.72M | if (it->itype == ASN1_ITYPE_MSTRING && str) |
357 | 2.69M | str->flags |= ASN1_STRING_FLAG_MSTRING; |
358 | 3.72M | *pval = (ASN1_VALUE *)str; |
359 | 3.72M | break; |
360 | 7.54M | } |
361 | 4.13M | if (*pval) |
362 | 4.13M | return 1; |
363 | 0 | return 0; |
364 | 4.13M | } |
365 | | |
366 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
367 | 1.50M | { |
368 | 1.50M | int utype; |
369 | 1.50M | if (it && it->funcs) { |
370 | 0 | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
371 | 0 | if (pf->prim_clear) |
372 | 0 | pf->prim_clear(pval, it); |
373 | 0 | else |
374 | 0 | *pval = NULL; |
375 | 0 | return; |
376 | 0 | } |
377 | 1.50M | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
378 | 0 | utype = -1; |
379 | 1.50M | else |
380 | 1.50M | utype = it->utype; |
381 | 1.50M | if (utype == V_ASN1_BOOLEAN) |
382 | 558k | *(ASN1_BOOLEAN *)pval = it->size; |
383 | 947k | else |
384 | 947k | *pval = NULL; |
385 | 1.50M | } |