/src/boringssl/crypto/asn1/tasn_new.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
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 (tjh@cryptsoft.com). |
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 (eay@cryptsoft.com)" |
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 (tjh@cryptsoft.com)" |
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 | | #include "../internal.h" |
67 | | #include "internal.h" |
68 | | |
69 | | |
70 | | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
71 | | static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
72 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
73 | | static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it); |
74 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
75 | | |
76 | 1.54M | ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) { |
77 | 1.54M | ASN1_VALUE *ret = NULL; |
78 | 1.54M | if (ASN1_item_ex_new(&ret, it) > 0) { |
79 | 1.54M | return ret; |
80 | 1.54M | } |
81 | 0 | return NULL; |
82 | 1.54M | } |
83 | | |
84 | | // Allocate an ASN1 structure |
85 | | |
86 | 9.21M | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
87 | 9.21M | const ASN1_TEMPLATE *tt = NULL; |
88 | 9.21M | const ASN1_EXTERN_FUNCS *ef; |
89 | 9.21M | ASN1_VALUE **pseqval; |
90 | 9.21M | int i; |
91 | | |
92 | 9.21M | switch (it->itype) { |
93 | 351k | case ASN1_ITYPE_EXTERN: |
94 | 351k | ef = it->funcs; |
95 | 351k | if (ef && ef->asn1_ex_new) { |
96 | 351k | if (!ef->asn1_ex_new(pval, it)) { |
97 | 0 | goto memerr; |
98 | 0 | } |
99 | 351k | } |
100 | 351k | break; |
101 | | |
102 | 3.57M | case ASN1_ITYPE_PRIMITIVE: |
103 | 3.57M | if (it->templates) { |
104 | 946 | if (!ASN1_template_new(pval, it->templates)) { |
105 | 0 | goto memerr; |
106 | 0 | } |
107 | 3.56M | } else if (!ASN1_primitive_new(pval, it)) { |
108 | 0 | goto memerr; |
109 | 0 | } |
110 | 3.57M | break; |
111 | | |
112 | 3.57M | case ASN1_ITYPE_MSTRING: |
113 | 2.03M | if (!ASN1_primitive_new(pval, it)) { |
114 | 0 | goto memerr; |
115 | 0 | } |
116 | 2.03M | break; |
117 | | |
118 | 2.03M | case ASN1_ITYPE_CHOICE: { |
119 | 201k | const ASN1_AUX *aux = it->funcs; |
120 | 201k | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
121 | 201k | if (asn1_cb) { |
122 | 2.47k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
123 | 2.47k | if (!i) { |
124 | 0 | goto auxerr; |
125 | 0 | } |
126 | 2.47k | if (i == 2) { |
127 | 0 | return 1; |
128 | 0 | } |
129 | 2.47k | } |
130 | 201k | *pval = OPENSSL_malloc(it->size); |
131 | 201k | if (!*pval) { |
132 | 0 | goto memerr; |
133 | 0 | } |
134 | 201k | OPENSSL_memset(*pval, 0, it->size); |
135 | 201k | asn1_set_choice_selector(pval, -1, it); |
136 | 201k | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) { |
137 | 0 | goto auxerr2; |
138 | 0 | } |
139 | 201k | break; |
140 | 201k | } |
141 | | |
142 | 3.05M | case ASN1_ITYPE_SEQUENCE: { |
143 | 3.05M | const ASN1_AUX *aux = it->funcs; |
144 | 3.05M | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
145 | 3.05M | if (asn1_cb) { |
146 | 143k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
147 | 143k | if (!i) { |
148 | 0 | goto auxerr; |
149 | 0 | } |
150 | 143k | if (i == 2) { |
151 | 0 | return 1; |
152 | 0 | } |
153 | 143k | } |
154 | 3.05M | *pval = OPENSSL_malloc(it->size); |
155 | 3.05M | if (!*pval) { |
156 | 0 | goto memerr; |
157 | 0 | } |
158 | 3.05M | OPENSSL_memset(*pval, 0, it->size); |
159 | 3.05M | asn1_refcount_set_one(pval, it); |
160 | 3.05M | asn1_enc_init(pval, it); |
161 | 10.7M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
162 | 7.67M | pseqval = asn1_get_field_ptr(pval, tt); |
163 | 7.67M | if (!ASN1_template_new(pseqval, tt)) { |
164 | 0 | goto memerr2; |
165 | 0 | } |
166 | 7.67M | } |
167 | 3.05M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) { |
168 | 0 | goto auxerr2; |
169 | 0 | } |
170 | 3.05M | break; |
171 | 3.05M | } |
172 | 9.21M | } |
173 | 9.21M | return 1; |
174 | | |
175 | 0 | memerr2: |
176 | 0 | ASN1_item_ex_free(pval, it); |
177 | 0 | memerr: |
178 | 0 | return 0; |
179 | | |
180 | 0 | auxerr2: |
181 | 0 | ASN1_item_ex_free(pval, it); |
182 | 0 | auxerr: |
183 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); |
184 | 0 | return 0; |
185 | 0 | } |
186 | | |
187 | 1.31M | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
188 | 1.31M | const ASN1_EXTERN_FUNCS *ef; |
189 | | |
190 | 1.31M | switch (it->itype) { |
191 | 0 | case ASN1_ITYPE_EXTERN: |
192 | 0 | ef = it->funcs; |
193 | 0 | if (ef && ef->asn1_ex_clear) { |
194 | 0 | ef->asn1_ex_clear(pval, it); |
195 | 0 | } else { |
196 | 0 | *pval = NULL; |
197 | 0 | } |
198 | 0 | break; |
199 | | |
200 | 1.28M | case ASN1_ITYPE_PRIMITIVE: |
201 | 1.28M | if (it->templates) { |
202 | 0 | asn1_template_clear(pval, it->templates); |
203 | 1.28M | } else { |
204 | 1.28M | asn1_primitive_clear(pval, it); |
205 | 1.28M | } |
206 | 1.28M | break; |
207 | | |
208 | 4.44k | case ASN1_ITYPE_MSTRING: |
209 | 4.44k | asn1_primitive_clear(pval, it); |
210 | 4.44k | break; |
211 | | |
212 | 22.4k | case ASN1_ITYPE_CHOICE: |
213 | 26.8k | case ASN1_ITYPE_SEQUENCE: |
214 | 26.8k | *pval = NULL; |
215 | 26.8k | break; |
216 | 1.31M | } |
217 | 1.31M | } |
218 | | |
219 | 7.67M | static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { |
220 | 7.67M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
221 | 7.67M | int ret; |
222 | 7.67M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
223 | 1.48M | asn1_template_clear(pval, tt); |
224 | 1.48M | return 1; |
225 | 1.48M | } |
226 | | // If ANY DEFINED BY nothing to do |
227 | | |
228 | 6.18M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
229 | 5.85k | *pval = NULL; |
230 | 5.85k | return 1; |
231 | 5.85k | } |
232 | | // If SET OF or SEQUENCE OF, its a STACK |
233 | 6.18M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
234 | 1.83k | STACK_OF(ASN1_VALUE) *skval; |
235 | 1.83k | skval = sk_ASN1_VALUE_new_null(); |
236 | 1.83k | if (!skval) { |
237 | 0 | ret = 0; |
238 | 0 | goto done; |
239 | 0 | } |
240 | 1.83k | *pval = (ASN1_VALUE *)skval; |
241 | 1.83k | ret = 1; |
242 | 1.83k | goto done; |
243 | 1.83k | } |
244 | | // Otherwise pass it back to the item routine |
245 | 6.18M | ret = ASN1_item_ex_new(pval, it); |
246 | 6.18M | done: |
247 | 6.18M | return ret; |
248 | 6.18M | } |
249 | | |
250 | 1.48M | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { |
251 | | // If ADB or STACK just NULL the field |
252 | 1.48M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) { |
253 | 173k | *pval = NULL; |
254 | 1.31M | } else { |
255 | 1.31M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
256 | 1.31M | } |
257 | 1.48M | } |
258 | | |
259 | | // NB: could probably combine most of the real XXX_new() behaviour and junk |
260 | | // all the old functions. |
261 | | |
262 | 5.60M | static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
263 | 5.60M | if (!it) { |
264 | 0 | return 0; |
265 | 0 | } |
266 | | |
267 | | // Historically, |it->funcs| for primitive types contained an |
268 | | // |ASN1_PRIMITIVE_FUNCS| table of calbacks. |
269 | 5.60M | assert(it->funcs == NULL); |
270 | | |
271 | 5.60M | int utype; |
272 | 5.60M | if (it->itype == ASN1_ITYPE_MSTRING) { |
273 | 2.03M | utype = -1; |
274 | 3.56M | } else { |
275 | 3.56M | utype = it->utype; |
276 | 3.56M | } |
277 | 5.60M | switch (utype) { |
278 | 2.58M | case V_ASN1_OBJECT: |
279 | 2.58M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
280 | 2.58M | return 1; |
281 | | |
282 | 0 | case V_ASN1_BOOLEAN: |
283 | 0 | *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; |
284 | 0 | return 1; |
285 | | |
286 | 22 | case V_ASN1_NULL: |
287 | 22 | *pval = (ASN1_VALUE *)1; |
288 | 22 | return 1; |
289 | | |
290 | 299k | case V_ASN1_ANY: { |
291 | 299k | ASN1_TYPE *typ = OPENSSL_malloc(sizeof(ASN1_TYPE)); |
292 | 299k | if (!typ) { |
293 | 0 | return 0; |
294 | 0 | } |
295 | 299k | typ->value.ptr = NULL; |
296 | 299k | typ->type = -1; |
297 | 299k | *pval = (ASN1_VALUE *)typ; |
298 | 299k | break; |
299 | 299k | } |
300 | | |
301 | 2.71M | default: |
302 | 2.71M | *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype); |
303 | 2.71M | break; |
304 | 5.60M | } |
305 | 3.01M | if (*pval) { |
306 | 3.01M | return 1; |
307 | 3.01M | } |
308 | 0 | return 0; |
309 | 3.01M | } |
310 | | |
311 | 1.28M | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
312 | 1.28M | int utype; |
313 | | // Historically, |it->funcs| for primitive types contained an |
314 | | // |ASN1_PRIMITIVE_FUNCS| table of calbacks. |
315 | 1.28M | assert(it == NULL || it->funcs == NULL); |
316 | 1.28M | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) { |
317 | 4.44k | utype = -1; |
318 | 1.28M | } else { |
319 | 1.28M | utype = it->utype; |
320 | 1.28M | } |
321 | 1.28M | if (utype == V_ASN1_BOOLEAN) { |
322 | 399k | *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; |
323 | 885k | } else { |
324 | 885k | *pval = NULL; |
325 | 885k | } |
326 | 1.28M | } |