]>
Commit | Line | Data |
---|---|---|
9d6b1ce6 | 1 | /* tasn_dec.c */ |
2e597528 | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
9d6b1ce6 DSH |
3 | * project 2000. |
4 | */ | |
5 | /* ==================================================================== | |
7bdeeb64 | 6 | * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. |
9d6b1ce6 DSH |
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> | |
a9daa467 | 61 | #include <string.h> |
9d6b1ce6 DSH |
62 | #include <openssl/asn1.h> |
63 | #include <openssl/asn1t.h> | |
64 | #include <openssl/objects.h> | |
65 | #include <openssl/buffer.h> | |
66 | #include <openssl/err.h> | |
67 | ||
875a644a | 68 | static int asn1_check_eoc(const unsigned char **in, long len); |
e1cc0671 | 69 | static int asn1_find_end(const unsigned char **in, long len, char inf); |
8845420f DSH |
70 | |
71 | static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, | |
854a225a | 72 | char inf, int tag, int aclass, int depth); |
8845420f | 73 | |
875a644a | 74 | static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); |
8845420f DSH |
75 | |
76 | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, | |
77 | char *inf, char *cst, | |
78 | const unsigned char **in, long len, | |
79 | int exptag, int expclass, char opt, | |
80 | ASN1_TLC *ctx); | |
81 | ||
82 | static int asn1_template_ex_d2i(ASN1_VALUE **pval, | |
83 | const unsigned char **in, long len, | |
84 | const ASN1_TEMPLATE *tt, char opt, | |
85 | ASN1_TLC *ctx); | |
86 | static int asn1_template_noexp_d2i(ASN1_VALUE **val, | |
87 | const unsigned char **in, long len, | |
88 | const ASN1_TEMPLATE *tt, char opt, | |
89 | ASN1_TLC *ctx); | |
90 | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, | |
91 | const unsigned char **in, long len, | |
92 | const ASN1_ITEM *it, | |
93 | int tag, int aclass, char opt, ASN1_TLC *ctx); | |
9d6b1ce6 | 94 | |
1241126a | 95 | /* Table to convert tags to bit values, used for MSTRING type */ |
2d3e956a | 96 | static const unsigned long tag2bit[32] = { |
1241126a DSH |
97 | 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ |
98 | B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ | |
99 | B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ | |
100 | B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */ | |
827c5574 | 101 | B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */ |
1241126a DSH |
102 | B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */ |
103 | B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ | |
104 | B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ | |
105 | B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */ | |
106 | }; | |
107 | ||
108 | unsigned long ASN1_tag2bit(int tag) | |
8845420f DSH |
109 | { |
110 | if ((tag < 0) || (tag > 30)) return 0; | |
1241126a | 111 | return tag2bit[tag]; |
8845420f | 112 | } |
1241126a | 113 | |
9d6b1ce6 DSH |
114 | /* Macro to initialize and invalidate the cache */ |
115 | ||
8845420f | 116 | #define asn1_tlc_clear(c) if (c) (c)->valid = 0 |
de121164 DSH |
117 | /* Version to avoid compiler warning about 'c' always non-NULL */ |
118 | #define asn1_tlc_clear_nc(c) (c)->valid = 0 | |
9d6b1ce6 DSH |
119 | |
120 | /* Decode an ASN1 item, this currently behaves just | |
121 | * like a standard 'd2i' function. 'in' points to | |
122 | * a buffer to read the data from, in future we will | |
123 | * have more advanced versions that can input data | |
124 | * a piece at a time and this will simply be a special | |
125 | * case. | |
126 | */ | |
127 | ||
8845420f | 128 | ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, |
6343829a | 129 | const unsigned char **in, long len, const ASN1_ITEM *it) |
8845420f | 130 | { |
9d6b1ce6 DSH |
131 | ASN1_TLC c; |
132 | ASN1_VALUE *ptmpval = NULL; | |
8845420f DSH |
133 | if (!pval) |
134 | pval = &ptmpval; | |
de121164 | 135 | asn1_tlc_clear_nc(&c); |
8845420f | 136 | if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) |
9d6b1ce6 DSH |
137 | return *pval; |
138 | return NULL; | |
8845420f | 139 | } |
9d6b1ce6 | 140 | |
8845420f DSH |
141 | int ASN1_template_d2i(ASN1_VALUE **pval, |
142 | const unsigned char **in, long len, const ASN1_TEMPLATE *tt) | |
143 | { | |
9d6b1ce6 | 144 | ASN1_TLC c; |
de121164 | 145 | asn1_tlc_clear_nc(&c); |
9d6b1ce6 | 146 | return asn1_template_ex_d2i(pval, in, len, tt, 0, &c); |
8845420f | 147 | } |
9d6b1ce6 DSH |
148 | |
149 | ||
150 | /* Decode an item, taking care of IMPLICIT tagging, if any. | |
151 | * If 'opt' set and tag mismatch return -1 to handle OPTIONAL | |
152 | */ | |
153 | ||
8845420f DSH |
154 | int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
155 | const ASN1_ITEM *it, | |
156 | int tag, int aclass, char opt, ASN1_TLC *ctx) | |
157 | { | |
9d6b1ce6 DSH |
158 | const ASN1_TEMPLATE *tt, *errtt = NULL; |
159 | const ASN1_COMPAT_FUNCS *cf; | |
160 | const ASN1_EXTERN_FUNCS *ef; | |
161 | const ASN1_AUX *aux = it->funcs; | |
162 | ASN1_aux_cb *asn1_cb; | |
82e8372f | 163 | const unsigned char *p = NULL, *q; |
875a644a RL |
164 | unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */ |
165 | unsigned char imphack = 0, oclass; | |
9d6b1ce6 DSH |
166 | char seq_eoc, seq_nolen, cst, isopt; |
167 | long tmplen; | |
168 | int i; | |
169 | int otag; | |
170 | int ret = 0; | |
c8bbd98a | 171 | ASN1_VALUE **pchptr, *ptmpval; |
8845420f DSH |
172 | if (!pval) |
173 | return 0; | |
174 | if (aux && aux->asn1_cb) | |
175 | asn1_cb = aux->asn1_cb; | |
9d6b1ce6 DSH |
176 | else asn1_cb = 0; |
177 | ||
8845420f DSH |
178 | switch(it->itype) |
179 | { | |
9d6b1ce6 | 180 | case ASN1_ITYPE_PRIMITIVE: |
8845420f DSH |
181 | if (it->templates) |
182 | { | |
183 | /* tagging or OPTIONAL is currently illegal on an item | |
184 | * template because the flags can't get passed down. | |
185 | * In practice this isn't a problem: we include the | |
186 | * relevant flags from the item template in the | |
187 | * template itself. | |
b31cc2d9 | 188 | */ |
8845420f DSH |
189 | if ((tag != -1) || opt) |
190 | { | |
191 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
192 | ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); | |
b31cc2d9 | 193 | goto err; |
8845420f DSH |
194 | } |
195 | return asn1_template_ex_d2i(pval, in, len, | |
196 | it->templates, opt, ctx); | |
b31cc2d9 | 197 | } |
8845420f DSH |
198 | return asn1_d2i_ex_primitive(pval, in, len, it, |
199 | tag, aclass, opt, ctx); | |
9d6b1ce6 DSH |
200 | break; |
201 | ||
202 | case ASN1_ITYPE_MSTRING: | |
203 | p = *in; | |
204 | /* Just read in tag and class */ | |
8845420f DSH |
205 | ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, |
206 | &p, len, -1, 0, 1, ctx); | |
207 | if (!ret) | |
208 | { | |
209 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
210 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 211 | goto err; |
8845420f DSH |
212 | } |
213 | ||
9d6b1ce6 | 214 | /* Must be UNIVERSAL class */ |
8845420f DSH |
215 | if (oclass != V_ASN1_UNIVERSAL) |
216 | { | |
9d6b1ce6 | 217 | /* If OPTIONAL, assume this is OK */ |
8845420f DSH |
218 | if (opt) return -1; |
219 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
220 | ASN1_R_MSTRING_NOT_UNIVERSAL); | |
9d6b1ce6 | 221 | goto err; |
8845420f | 222 | } |
9d6b1ce6 | 223 | /* Check tag matches bit map */ |
8845420f DSH |
224 | if (!(ASN1_tag2bit(otag) & it->utype)) |
225 | { | |
9d6b1ce6 | 226 | /* If OPTIONAL, assume this is OK */ |
8845420f DSH |
227 | if (opt) |
228 | return -1; | |
229 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
230 | ASN1_R_MSTRING_WRONG_TAG); | |
9d6b1ce6 | 231 | goto err; |
8845420f DSH |
232 | } |
233 | return asn1_d2i_ex_primitive(pval, in, len, | |
234 | it, otag, 0, 0, ctx); | |
9d6b1ce6 DSH |
235 | |
236 | case ASN1_ITYPE_EXTERN: | |
237 | /* Use new style d2i */ | |
238 | ef = it->funcs; | |
8845420f DSH |
239 | return ef->asn1_ex_d2i(pval, in, len, |
240 | it, tag, aclass, opt, ctx); | |
9d6b1ce6 DSH |
241 | |
242 | case ASN1_ITYPE_COMPAT: | |
243 | /* we must resort to old style evil hackery */ | |
244 | cf = it->funcs; | |
245 | ||
246 | /* If OPTIONAL see if it is there */ | |
8845420f DSH |
247 | if (opt) |
248 | { | |
9d6b1ce6 DSH |
249 | int exptag; |
250 | p = *in; | |
8845420f DSH |
251 | if (tag == -1) |
252 | exptag = it->utype; | |
9d6b1ce6 | 253 | else exptag = tag; |
8845420f DSH |
254 | /* Don't care about anything other than presence |
255 | * of expected tag */ | |
256 | ||
257 | ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, | |
258 | &p, len, exptag, aclass, 1, ctx); | |
259 | if (!ret) | |
260 | { | |
261 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
262 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 263 | goto err; |
8845420f DSH |
264 | } |
265 | if (ret == -1) | |
266 | return -1; | |
9d6b1ce6 | 267 | } |
8845420f | 268 | |
9d6b1ce6 DSH |
269 | /* This is the old style evil hack IMPLICIT handling: |
270 | * since the underlying code is expecting a tag and | |
271 | * class other than the one present we change the | |
272 | * buffer temporarily then change it back afterwards. | |
273 | * This doesn't and never did work for tags > 30. | |
274 | * | |
275 | * Yes this is *horrible* but it is only needed for | |
276 | * old style d2i which will hopefully not be around | |
277 | * for much longer. | |
278 | * FIXME: should copy the buffer then modify it so | |
279 | * the input buffer can be const: we should *always* | |
280 | * copy because the old style d2i might modify the | |
281 | * buffer. | |
282 | */ | |
283 | ||
8845420f DSH |
284 | if (tag != -1) |
285 | { | |
875a644a RL |
286 | wp = *(unsigned char **)in; |
287 | imphack = *wp; | |
82e8372f NL |
288 | if (p == NULL) |
289 | { | |
290 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
291 | ERR_R_NESTED_ASN1_ERROR); | |
292 | goto err; | |
293 | } | |
8845420f DSH |
294 | *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) |
295 | | it->utype); | |
296 | } | |
9d6b1ce6 DSH |
297 | |
298 | ptmpval = cf->asn1_d2i(pval, in, len); | |
299 | ||
8845420f DSH |
300 | if (tag != -1) |
301 | *wp = imphack; | |
302 | ||
303 | if (ptmpval) | |
304 | return 1; | |
9d6b1ce6 | 305 | |
9d6b1ce6 DSH |
306 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); |
307 | goto err; | |
308 | ||
309 | ||
310 | case ASN1_ITYPE_CHOICE: | |
24484759 | 311 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
9d6b1ce6 | 312 | goto auxerr; |
722ca278 DSH |
313 | |
314 | /* Allocate structure */ | |
8845420f DSH |
315 | if (!*pval && !ASN1_item_ex_new(pval, it)) |
316 | { | |
317 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
318 | ERR_R_NESTED_ASN1_ERROR); | |
319 | goto err; | |
722ca278 | 320 | } |
9d6b1ce6 | 321 | /* CHOICE type, try each possibility in turn */ |
9d6b1ce6 | 322 | p = *in; |
f3f52d7f | 323 | for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) |
8845420f | 324 | { |
722ca278 | 325 | pchptr = asn1_get_field_ptr(pval, tt); |
9d6b1ce6 DSH |
326 | /* We mark field as OPTIONAL so its absence |
327 | * can be recognised. | |
328 | */ | |
722ca278 | 329 | ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx); |
9d6b1ce6 | 330 | /* If field not present, try the next one */ |
8845420f DSH |
331 | if (ret == -1) |
332 | continue; | |
9d6b1ce6 | 333 | /* If positive return, read OK, break loop */ |
8845420f DSH |
334 | if (ret > 0) |
335 | break; | |
9d6b1ce6 DSH |
336 | /* Otherwise must be an ASN1 parsing error */ |
337 | errtt = tt; | |
8845420f DSH |
338 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, |
339 | ERR_R_NESTED_ASN1_ERROR); | |
b31cc2d9 | 340 | goto err; |
8845420f DSH |
341 | } |
342 | ||
9d6b1ce6 | 343 | /* Did we fall off the end without reading anything? */ |
8845420f DSH |
344 | if (i == it->tcount) |
345 | { | |
9d6b1ce6 | 346 | /* If OPTIONAL, this is OK */ |
8845420f DSH |
347 | if (opt) |
348 | { | |
722ca278 DSH |
349 | /* Free and zero it */ |
350 | ASN1_item_ex_free(pval, it); | |
351 | return -1; | |
8845420f DSH |
352 | } |
353 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
354 | ASN1_R_NO_MATCHING_CHOICE_TYPE); | |
b31cc2d9 | 355 | goto err; |
8845420f DSH |
356 | } |
357 | ||
9d6b1ce6 DSH |
358 | asn1_set_choice_selector(pval, i, it); |
359 | *in = p; | |
24484759 | 360 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
9d6b1ce6 DSH |
361 | goto auxerr; |
362 | return 1; | |
363 | ||
230fd6b7 | 364 | case ASN1_ITYPE_NDEF_SEQUENCE: |
9d6b1ce6 DSH |
365 | case ASN1_ITYPE_SEQUENCE: |
366 | p = *in; | |
367 | tmplen = len; | |
368 | ||
369 | /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ | |
8845420f DSH |
370 | if (tag == -1) |
371 | { | |
9d6b1ce6 DSH |
372 | tag = V_ASN1_SEQUENCE; |
373 | aclass = V_ASN1_UNIVERSAL; | |
8845420f | 374 | } |
9d6b1ce6 | 375 | /* Get SEQUENCE length and update len, p */ |
8845420f DSH |
376 | ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, |
377 | &p, len, tag, aclass, opt, ctx); | |
378 | if (!ret) | |
379 | { | |
380 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
381 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 382 | goto err; |
8845420f DSH |
383 | } |
384 | else if (ret == -1) | |
385 | return -1; | |
386 | if (aux && (aux->flags & ASN1_AFLG_BROKEN)) | |
387 | { | |
9d6b1ce6 DSH |
388 | len = tmplen - (p - *in); |
389 | seq_nolen = 1; | |
8845420f DSH |
390 | } |
391 | /* If indefinite we don't do a length check */ | |
392 | else seq_nolen = seq_eoc; | |
393 | if (!cst) | |
394 | { | |
395 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
396 | ASN1_R_SEQUENCE_NOT_CONSTRUCTED); | |
9d6b1ce6 | 397 | goto err; |
8845420f | 398 | } |
9d6b1ce6 | 399 | |
bd1640bb | 400 | if (!*pval && !ASN1_item_ex_new(pval, it)) |
8845420f DSH |
401 | { |
402 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
403 | ERR_R_NESTED_ASN1_ERROR); | |
404 | goto err; | |
9d6b1ce6 | 405 | } |
8845420f | 406 | |
24484759 | 407 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
9d6b1ce6 DSH |
408 | goto auxerr; |
409 | ||
410 | /* Get each field entry */ | |
8845420f DSH |
411 | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) |
412 | { | |
9d6b1ce6 DSH |
413 | const ASN1_TEMPLATE *seqtt; |
414 | ASN1_VALUE **pseqval; | |
415 | seqtt = asn1_do_adb(pval, tt, 1); | |
8845420f DSH |
416 | if (!seqtt) |
417 | goto err; | |
9d6b1ce6 DSH |
418 | pseqval = asn1_get_field_ptr(pval, seqtt); |
419 | /* Have we ran out of data? */ | |
8845420f DSH |
420 | if (!len) |
421 | break; | |
9d6b1ce6 | 422 | q = p; |
8845420f DSH |
423 | if (asn1_check_eoc(&p, len)) |
424 | { | |
425 | if (!seq_eoc) | |
426 | { | |
427 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
428 | ASN1_R_UNEXPECTED_EOC); | |
9d6b1ce6 | 429 | goto err; |
8845420f | 430 | } |
9d6b1ce6 DSH |
431 | len -= p - q; |
432 | seq_eoc = 0; | |
433 | q = p; | |
434 | break; | |
8845420f DSH |
435 | } |
436 | /* This determines the OPTIONAL flag value. The field | |
437 | * cannot be omitted if it is the last of a SEQUENCE | |
438 | * and there is still data to be read. This isn't | |
439 | * strictly necessary but it increases efficiency in | |
440 | * some cases. | |
9d6b1ce6 | 441 | */ |
8845420f DSH |
442 | if (i == (it->tcount - 1)) |
443 | isopt = 0; | |
3c07b4c2 | 444 | else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); |
8845420f DSH |
445 | /* attempt to read in field, allowing each to be |
446 | * OPTIONAL */ | |
447 | ||
448 | ret = asn1_template_ex_d2i(pseqval, &p, len, | |
449 | seqtt, isopt, ctx); | |
450 | if (!ret) | |
451 | { | |
9d6b1ce6 DSH |
452 | errtt = seqtt; |
453 | goto err; | |
8845420f DSH |
454 | } |
455 | else if (ret == -1) | |
456 | { | |
457 | /* OPTIONAL component absent. | |
458 | * Free and zero the field. | |
9d6b1ce6 DSH |
459 | */ |
460 | ASN1_template_free(pseqval, seqtt); | |
461 | continue; | |
8845420f | 462 | } |
9d6b1ce6 DSH |
463 | /* Update length */ |
464 | len -= p - q; | |
8845420f DSH |
465 | } |
466 | ||
9d6b1ce6 | 467 | /* Check for EOC if expecting one */ |
8845420f DSH |
468 | if (seq_eoc && !asn1_check_eoc(&p, len)) |
469 | { | |
9d6b1ce6 DSH |
470 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC); |
471 | goto err; | |
8845420f | 472 | } |
9d6b1ce6 | 473 | /* Check all data read */ |
8845420f DSH |
474 | if (!seq_nolen && len) |
475 | { | |
476 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, | |
477 | ASN1_R_SEQUENCE_LENGTH_MISMATCH); | |
9d6b1ce6 | 478 | goto err; |
8845420f | 479 | } |
9d6b1ce6 DSH |
480 | |
481 | /* If we get here we've got no more data in the SEQUENCE, | |
482 | * however we may not have read all fields so check all | |
483 | * remaining are OPTIONAL and clear any that are. | |
484 | */ | |
8845420f DSH |
485 | for (; i < it->tcount; tt++, i++) |
486 | { | |
9d6b1ce6 DSH |
487 | const ASN1_TEMPLATE *seqtt; |
488 | seqtt = asn1_do_adb(pval, tt, 1); | |
8845420f DSH |
489 | if (!seqtt) |
490 | goto err; | |
491 | if (seqtt->flags & ASN1_TFLG_OPTIONAL) | |
492 | { | |
9d6b1ce6 DSH |
493 | ASN1_VALUE **pseqval; |
494 | pseqval = asn1_get_field_ptr(pval, seqtt); | |
495 | ASN1_template_free(pseqval, seqtt); | |
8845420f DSH |
496 | } |
497 | else | |
498 | { | |
9d6b1ce6 | 499 | errtt = seqtt; |
8845420f DSH |
500 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, |
501 | ASN1_R_FIELD_MISSING); | |
9d6b1ce6 | 502 | goto err; |
8845420f | 503 | } |
9d6b1ce6 | 504 | } |
9d6b1ce6 | 505 | /* Save encoding */ |
8845420f DSH |
506 | if (!asn1_enc_save(pval, *in, p - *in, it)) |
507 | goto auxerr; | |
9d6b1ce6 | 508 | *in = p; |
24484759 | 509 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
9d6b1ce6 DSH |
510 | goto auxerr; |
511 | return 1; | |
512 | ||
513 | default: | |
514 | return 0; | |
8845420f | 515 | } |
9d6b1ce6 DSH |
516 | auxerr: |
517 | ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); | |
518 | err: | |
519 | ASN1_item_ex_free(pval, it); | |
8845420f DSH |
520 | if (errtt) |
521 | ERR_add_error_data(4, "Field=", errtt->field_name, | |
522 | ", Type=", it->sname); | |
523 | else | |
524 | ERR_add_error_data(2, "Type=", it->sname); | |
9d6b1ce6 | 525 | return 0; |
8845420f | 526 | } |
9d6b1ce6 | 527 | |
8845420f DSH |
528 | /* Templates are handled with two separate functions. |
529 | * One handles any EXPLICIT tag and the other handles the rest. | |
9d6b1ce6 DSH |
530 | */ |
531 | ||
8845420f DSH |
532 | static int asn1_template_ex_d2i(ASN1_VALUE **val, |
533 | const unsigned char **in, long inlen, | |
534 | const ASN1_TEMPLATE *tt, char opt, | |
535 | ASN1_TLC *ctx) | |
536 | { | |
9d6b1ce6 DSH |
537 | int flags, aclass; |
538 | int ret; | |
539 | long len; | |
875a644a | 540 | const unsigned char *p, *q; |
9d6b1ce6 | 541 | char exp_eoc; |
8845420f DSH |
542 | if (!val) |
543 | return 0; | |
9d6b1ce6 DSH |
544 | flags = tt->flags; |
545 | aclass = flags & ASN1_TFLG_TAG_CLASS; | |
546 | ||
547 | p = *in; | |
548 | ||
549 | /* Check if EXPLICIT tag expected */ | |
8845420f DSH |
550 | if (flags & ASN1_TFLG_EXPTAG) |
551 | { | |
9d6b1ce6 | 552 | char cst; |
8845420f DSH |
553 | /* Need to work out amount of data available to the inner |
554 | * content and where it starts: so read in EXPLICIT header to | |
555 | * get the info. | |
9d6b1ce6 | 556 | */ |
8845420f DSH |
557 | ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, |
558 | &p, inlen, tt->tag, aclass, opt, ctx); | |
9d6b1ce6 | 559 | q = p; |
8845420f DSH |
560 | if (!ret) |
561 | { | |
562 | ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, | |
563 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 564 | return 0; |
8845420f DSH |
565 | } |
566 | else if (ret == -1) | |
567 | return -1; | |
568 | if (!cst) | |
569 | { | |
570 | ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, | |
571 | ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); | |
9d6b1ce6 | 572 | return 0; |
8845420f | 573 | } |
9d6b1ce6 DSH |
574 | /* We've found the field so it can't be OPTIONAL now */ |
575 | ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); | |
8845420f DSH |
576 | if (!ret) |
577 | { | |
578 | ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, | |
579 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 580 | return 0; |
8845420f | 581 | } |
9d6b1ce6 DSH |
582 | /* We read the field in OK so update length */ |
583 | len -= p - q; | |
8845420f DSH |
584 | if (exp_eoc) |
585 | { | |
9d6b1ce6 | 586 | /* If NDEF we must have an EOC here */ |
8845420f DSH |
587 | if (!asn1_check_eoc(&p, len)) |
588 | { | |
8afca8d9 | 589 | ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, |
8845420f | 590 | ASN1_R_MISSING_EOC); |
9d6b1ce6 | 591 | goto err; |
8845420f | 592 | } |
9d6b1ce6 | 593 | } |
8845420f DSH |
594 | else |
595 | { | |
596 | /* Otherwise we must hit the EXPLICIT tag end or its | |
597 | * an error */ | |
598 | if (len) | |
599 | { | |
8afca8d9 | 600 | ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, |
8845420f | 601 | ASN1_R_EXPLICIT_LENGTH_MISMATCH); |
9d6b1ce6 | 602 | goto err; |
8845420f | 603 | } |
9d6b1ce6 DSH |
604 | } |
605 | } | |
8845420f DSH |
606 | else |
607 | return asn1_template_noexp_d2i(val, in, inlen, | |
608 | tt, opt, ctx); | |
9d6b1ce6 DSH |
609 | |
610 | *in = p; | |
611 | return 1; | |
612 | ||
613 | err: | |
614 | ASN1_template_free(val, tt); | |
9d6b1ce6 | 615 | return 0; |
8845420f | 616 | } |
9d6b1ce6 | 617 | |
8845420f DSH |
618 | static int asn1_template_noexp_d2i(ASN1_VALUE **val, |
619 | const unsigned char **in, long len, | |
620 | const ASN1_TEMPLATE *tt, char opt, | |
621 | ASN1_TLC *ctx) | |
622 | { | |
9d6b1ce6 DSH |
623 | int flags, aclass; |
624 | int ret; | |
875a644a | 625 | const unsigned char *p, *q; |
8845420f DSH |
626 | if (!val) |
627 | return 0; | |
9d6b1ce6 DSH |
628 | flags = tt->flags; |
629 | aclass = flags & ASN1_TFLG_TAG_CLASS; | |
630 | ||
631 | p = *in; | |
632 | q = p; | |
633 | ||
8845420f DSH |
634 | if (flags & ASN1_TFLG_SK_MASK) |
635 | { | |
9d6b1ce6 DSH |
636 | /* SET OF, SEQUENCE OF */ |
637 | int sktag, skaclass; | |
638 | char sk_eoc; | |
639 | /* First work out expected inner tag value */ | |
8845420f DSH |
640 | if (flags & ASN1_TFLG_IMPTAG) |
641 | { | |
9d6b1ce6 DSH |
642 | sktag = tt->tag; |
643 | skaclass = aclass; | |
8845420f DSH |
644 | } |
645 | else | |
646 | { | |
9d6b1ce6 | 647 | skaclass = V_ASN1_UNIVERSAL; |
8845420f DSH |
648 | if (flags & ASN1_TFLG_SET_OF) |
649 | sktag = V_ASN1_SET; | |
650 | else | |
651 | sktag = V_ASN1_SEQUENCE; | |
652 | } | |
9d6b1ce6 | 653 | /* Get the tag */ |
8845420f DSH |
654 | ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, |
655 | &p, len, sktag, skaclass, opt, ctx); | |
656 | if (!ret) | |
657 | { | |
8afca8d9 | 658 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 659 | ERR_R_NESTED_ASN1_ERROR); |
9d6b1ce6 | 660 | return 0; |
8845420f DSH |
661 | } |
662 | else if (ret == -1) | |
663 | return -1; | |
664 | if (!*val) | |
665 | *val = (ASN1_VALUE *)sk_new_null(); | |
666 | else | |
667 | { | |
9d6b1ce6 | 668 | /* We've got a valid STACK: free up any items present */ |
5ce278a7 BL |
669 | STACK_OF(ASN1_VALUE) *sktmp |
670 | = (STACK_OF(ASN1_VALUE) *)*val; | |
9d6b1ce6 | 671 | ASN1_VALUE *vtmp; |
5ce278a7 | 672 | while(sk_ASN1_VALUE_num(sktmp) > 0) |
8845420f | 673 | { |
5ce278a7 | 674 | vtmp = sk_ASN1_VALUE_pop(sktmp); |
8845420f DSH |
675 | ASN1_item_ex_free(&vtmp, |
676 | ASN1_ITEM_ptr(tt->item)); | |
677 | } | |
9d6b1ce6 | 678 | } |
9d6b1ce6 | 679 | |
8845420f DSH |
680 | if (!*val) |
681 | { | |
8afca8d9 | 682 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 683 | ERR_R_MALLOC_FAILURE); |
9d6b1ce6 | 684 | goto err; |
8845420f DSH |
685 | } |
686 | ||
9d6b1ce6 | 687 | /* Read as many items as we can */ |
8845420f DSH |
688 | while(len > 0) |
689 | { | |
9d6b1ce6 DSH |
690 | ASN1_VALUE *skfield; |
691 | q = p; | |
692 | /* See if EOC found */ | |
8845420f DSH |
693 | if (asn1_check_eoc(&p, len)) |
694 | { | |
695 | if (!sk_eoc) | |
696 | { | |
8afca8d9 | 697 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 698 | ASN1_R_UNEXPECTED_EOC); |
9d6b1ce6 | 699 | goto err; |
8845420f | 700 | } |
9d6b1ce6 DSH |
701 | len -= p - q; |
702 | sk_eoc = 0; | |
703 | break; | |
8845420f | 704 | } |
9d6b1ce6 | 705 | skfield = NULL; |
8845420f DSH |
706 | if (!ASN1_item_ex_d2i(&skfield, &p, len, |
707 | ASN1_ITEM_ptr(tt->item), | |
708 | -1, 0, 0, ctx)) | |
709 | { | |
8afca8d9 | 710 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 711 | ERR_R_NESTED_ASN1_ERROR); |
9d6b1ce6 | 712 | goto err; |
8845420f | 713 | } |
9d6b1ce6 | 714 | len -= p - q; |
5ce278a7 BL |
715 | if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, |
716 | skfield)) | |
8845420f | 717 | { |
8afca8d9 | 718 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 719 | ERR_R_MALLOC_FAILURE); |
9d6b1ce6 | 720 | goto err; |
8845420f | 721 | } |
9d6b1ce6 | 722 | } |
8845420f DSH |
723 | if (sk_eoc) |
724 | { | |
8afca8d9 | 725 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); |
9d6b1ce6 | 726 | goto err; |
8845420f | 727 | } |
9d6b1ce6 | 728 | } |
8845420f DSH |
729 | else if (flags & ASN1_TFLG_IMPTAG) |
730 | { | |
9d6b1ce6 | 731 | /* IMPLICIT tagging */ |
8845420f DSH |
732 | ret = ASN1_item_ex_d2i(val, &p, len, |
733 | ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); | |
734 | if (!ret) | |
735 | { | |
8afca8d9 | 736 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 737 | ERR_R_NESTED_ASN1_ERROR); |
9d6b1ce6 | 738 | goto err; |
8845420f DSH |
739 | } |
740 | else if (ret == -1) | |
741 | return -1; | |
742 | } | |
743 | else | |
744 | { | |
9d6b1ce6 | 745 | /* Nothing special */ |
8845420f DSH |
746 | ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), |
747 | -1, 0, opt, ctx); | |
748 | if (!ret) | |
749 | { | |
8afca8d9 | 750 | ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, |
8845420f | 751 | ERR_R_NESTED_ASN1_ERROR); |
9d6b1ce6 | 752 | goto err; |
8845420f DSH |
753 | } |
754 | else if (ret == -1) | |
755 | return -1; | |
756 | } | |
9d6b1ce6 DSH |
757 | |
758 | *in = p; | |
759 | return 1; | |
760 | ||
761 | err: | |
762 | ASN1_template_free(val, tt); | |
9d6b1ce6 | 763 | return 0; |
8845420f | 764 | } |
9d6b1ce6 | 765 | |
8845420f DSH |
766 | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, |
767 | const unsigned char **in, long inlen, | |
768 | const ASN1_ITEM *it, | |
769 | int tag, int aclass, char opt, ASN1_TLC *ctx) | |
770 | { | |
9d6b1ce6 DSH |
771 | int ret = 0, utype; |
772 | long plen; | |
773 | char cst, inf, free_cont = 0; | |
875a644a | 774 | const unsigned char *p; |
9d6b1ce6 | 775 | BUF_MEM buf; |
875a644a | 776 | const unsigned char *cont = NULL; |
9d6b1ce6 | 777 | long len; |
8845420f DSH |
778 | if (!pval) |
779 | { | |
9d6b1ce6 DSH |
780 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); |
781 | return 0; /* Should never happen */ | |
8845420f | 782 | } |
9d6b1ce6 | 783 | |
8845420f DSH |
784 | if (it->itype == ASN1_ITYPE_MSTRING) |
785 | { | |
9d6b1ce6 DSH |
786 | utype = tag; |
787 | tag = -1; | |
8845420f DSH |
788 | } |
789 | else | |
790 | utype = it->utype; | |
9d6b1ce6 | 791 | |
8845420f DSH |
792 | if (utype == V_ASN1_ANY) |
793 | { | |
9d6b1ce6 DSH |
794 | /* If type is ANY need to figure out type from tag */ |
795 | unsigned char oclass; | |
8845420f DSH |
796 | if (tag >= 0) |
797 | { | |
798 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
799 | ASN1_R_ILLEGAL_TAGGED_ANY); | |
9d6b1ce6 | 800 | return 0; |
8845420f DSH |
801 | } |
802 | if (opt) | |
803 | { | |
804 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
805 | ASN1_R_ILLEGAL_OPTIONAL_ANY); | |
9d6b1ce6 | 806 | return 0; |
8845420f | 807 | } |
9d6b1ce6 | 808 | p = *in; |
8845420f DSH |
809 | ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, |
810 | &p, inlen, -1, 0, 0, ctx); | |
811 | if (!ret) | |
812 | { | |
813 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
814 | ERR_R_NESTED_ASN1_ERROR); | |
9d6b1ce6 | 815 | return 0; |
8845420f DSH |
816 | } |
817 | if (oclass != V_ASN1_UNIVERSAL) | |
818 | utype = V_ASN1_OTHER; | |
9d6b1ce6 | 819 | } |
8845420f DSH |
820 | if (tag == -1) |
821 | { | |
9d6b1ce6 DSH |
822 | tag = utype; |
823 | aclass = V_ASN1_UNIVERSAL; | |
8845420f | 824 | } |
9d6b1ce6 DSH |
825 | p = *in; |
826 | /* Check header */ | |
8845420f DSH |
827 | ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, |
828 | &p, inlen, tag, aclass, opt, ctx); | |
829 | if (!ret) | |
830 | { | |
9d6b1ce6 DSH |
831 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); |
832 | return 0; | |
8845420f DSH |
833 | } |
834 | else if (ret == -1) | |
835 | return -1; | |
348be7ec | 836 | ret = 0; |
9d6b1ce6 | 837 | /* SEQUENCE, SET and "OTHER" are left in encoded form */ |
8845420f DSH |
838 | if ((utype == V_ASN1_SEQUENCE) |
839 | || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) | |
840 | { | |
841 | /* Clear context cache for type OTHER because the auto clear | |
842 | * when we have a exact match wont work | |
c962479b | 843 | */ |
8845420f DSH |
844 | if (utype == V_ASN1_OTHER) |
845 | { | |
c962479b | 846 | asn1_tlc_clear(ctx); |
8845420f | 847 | } |
9d6b1ce6 | 848 | /* SEQUENCE and SET must be constructed */ |
8845420f DSH |
849 | else if (!cst) |
850 | { | |
851 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
852 | ASN1_R_TYPE_NOT_CONSTRUCTED); | |
9d6b1ce6 | 853 | return 0; |
8845420f | 854 | } |
9d6b1ce6 DSH |
855 | |
856 | cont = *in; | |
857 | /* If indefinite length constructed find the real end */ | |
8845420f DSH |
858 | if (inf) |
859 | { | |
e1cc0671 | 860 | if (!asn1_find_end(&p, plen, inf)) |
8845420f | 861 | goto err; |
9d6b1ce6 | 862 | len = p - cont; |
8845420f DSH |
863 | } |
864 | else | |
865 | { | |
9d6b1ce6 DSH |
866 | len = p - cont + plen; |
867 | p += plen; | |
868 | buf.data = NULL; | |
8845420f | 869 | } |
9d6b1ce6 | 870 | } |
8845420f DSH |
871 | else if (cst) |
872 | { | |
89f40f36 DSH |
873 | if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN |
874 | || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER | |
875 | || utype == V_ASN1_ENUMERATED) | |
876 | { | |
877 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
878 | ASN1_R_TYPE_NOT_PRIMITIVE); | |
879 | return 0; | |
880 | } | |
9d6b1ce6 DSH |
881 | buf.length = 0; |
882 | buf.max = 0; | |
883 | buf.data = NULL; | |
884 | /* Should really check the internal tags are correct but | |
885 | * some things may get this wrong. The relevant specs | |
886 | * say that constructed string types should be OCTET STRINGs | |
887 | * internally irrespective of the type. So instead just check | |
888 | * for UNIVERSAL class and ignore the tag. | |
889 | */ | |
854a225a | 890 | if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) |
539d4c10 DSH |
891 | { |
892 | free_cont = 1; | |
8845420f | 893 | goto err; |
539d4c10 | 894 | } |
9d6b1ce6 | 895 | len = buf.length; |
bf0d176e | 896 | /* Append a final null to string */ |
8845420f DSH |
897 | if (!BUF_MEM_grow_clean(&buf, len + 1)) |
898 | { | |
899 | ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, | |
900 | ERR_R_MALLOC_FAILURE); | |
bf0d176e | 901 | return 0; |
8845420f | 902 | } |
bf0d176e | 903 | buf.data[len] = 0; |
875a644a | 904 | cont = (const unsigned char *)buf.data; |
9d6b1ce6 | 905 | free_cont = 1; |
8845420f DSH |
906 | } |
907 | else | |
908 | { | |
9d6b1ce6 DSH |
909 | cont = p; |
910 | len = plen; | |
911 | p += plen; | |
8845420f | 912 | } |
9d6b1ce6 DSH |
913 | |
914 | /* We now have content length and type: translate into a structure */ | |
8845420f DSH |
915 | if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) |
916 | goto err; | |
9d6b1ce6 DSH |
917 | |
918 | *in = p; | |
919 | ret = 1; | |
920 | err: | |
8845420f | 921 | if (free_cont && buf.data) OPENSSL_free(buf.data); |
9d6b1ce6 | 922 | return ret; |
8845420f | 923 | } |
9d6b1ce6 DSH |
924 | |
925 | /* Translate ASN1 content octets into a structure */ | |
926 | ||
8845420f DSH |
927 | int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
928 | int utype, char *free_cont, const ASN1_ITEM *it) | |
929 | { | |
29902449 | 930 | ASN1_VALUE **opval = NULL; |
9d6b1ce6 DSH |
931 | ASN1_STRING *stmp; |
932 | ASN1_TYPE *typ = NULL; | |
933 | int ret = 0; | |
934 | const ASN1_PRIMITIVE_FUNCS *pf; | |
a8312c0e | 935 | ASN1_INTEGER **tint; |
9d6b1ce6 | 936 | pf = it->funcs; |
8845420f DSH |
937 | |
938 | if (pf && pf->prim_c2i) | |
939 | return pf->prim_c2i(pval, cont, len, utype, free_cont, it); | |
9d6b1ce6 | 940 | /* If ANY type clear type and set pointer to internal value */ |
8845420f DSH |
941 | if (it->utype == V_ASN1_ANY) |
942 | { | |
943 | if (!*pval) | |
944 | { | |
9d6b1ce6 | 945 | typ = ASN1_TYPE_new(); |
8c5a2bd6 NL |
946 | if (typ == NULL) |
947 | goto err; | |
9d6b1ce6 | 948 | *pval = (ASN1_VALUE *)typ; |
8845420f DSH |
949 | } |
950 | else | |
951 | typ = (ASN1_TYPE *)*pval; | |
952 | ||
953 | if (utype != typ->type) | |
954 | ASN1_TYPE_set(typ, utype, NULL); | |
29902449 | 955 | opval = pval; |
de121164 | 956 | pval = &typ->value.asn1_value; |
8845420f DSH |
957 | } |
958 | switch(utype) | |
959 | { | |
9d6b1ce6 | 960 | case V_ASN1_OBJECT: |
8845420f DSH |
961 | if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) |
962 | goto err; | |
9d6b1ce6 DSH |
963 | break; |
964 | ||
965 | case V_ASN1_NULL: | |
8845420f DSH |
966 | if (len) |
967 | { | |
8afca8d9 | 968 | ASN1err(ASN1_F_ASN1_EX_C2I, |
8845420f | 969 | ASN1_R_NULL_IS_WRONG_LENGTH); |
9d6b1ce6 | 970 | goto err; |
8845420f | 971 | } |
9d6b1ce6 DSH |
972 | *pval = (ASN1_VALUE *)1; |
973 | break; | |
974 | ||
975 | case V_ASN1_BOOLEAN: | |
8845420f DSH |
976 | if (len != 1) |
977 | { | |
8afca8d9 | 978 | ASN1err(ASN1_F_ASN1_EX_C2I, |
8845420f | 979 | ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
9d6b1ce6 | 980 | goto err; |
8845420f DSH |
981 | } |
982 | else | |
983 | { | |
9d6b1ce6 DSH |
984 | ASN1_BOOLEAN *tbool; |
985 | tbool = (ASN1_BOOLEAN *)pval; | |
986 | *tbool = *cont; | |
8845420f | 987 | } |
9d6b1ce6 DSH |
988 | break; |
989 | ||
990 | case V_ASN1_BIT_STRING: | |
8845420f DSH |
991 | if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) |
992 | goto err; | |
9d6b1ce6 DSH |
993 | break; |
994 | ||
995 | case V_ASN1_INTEGER: | |
996 | case V_ASN1_NEG_INTEGER: | |
997 | case V_ASN1_ENUMERATED: | |
998 | case V_ASN1_NEG_ENUMERATED: | |
a8312c0e | 999 | tint = (ASN1_INTEGER **)pval; |
8845420f DSH |
1000 | if (!c2i_ASN1_INTEGER(tint, &cont, len)) |
1001 | goto err; | |
a8312c0e DSH |
1002 | /* Fixup type to match the expected form */ |
1003 | (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); | |
9d6b1ce6 DSH |
1004 | break; |
1005 | ||
1006 | case V_ASN1_OCTET_STRING: | |
1007 | case V_ASN1_NUMERICSTRING: | |
1008 | case V_ASN1_PRINTABLESTRING: | |
1009 | case V_ASN1_T61STRING: | |
1010 | case V_ASN1_VIDEOTEXSTRING: | |
1011 | case V_ASN1_IA5STRING: | |
1012 | case V_ASN1_UTCTIME: | |
1013 | case V_ASN1_GENERALIZEDTIME: | |
1014 | case V_ASN1_GRAPHICSTRING: | |
1015 | case V_ASN1_VISIBLESTRING: | |
1016 | case V_ASN1_GENERALSTRING: | |
1017 | case V_ASN1_UNIVERSALSTRING: | |
1018 | case V_ASN1_BMPSTRING: | |
1019 | case V_ASN1_UTF8STRING: | |
1020 | case V_ASN1_OTHER: | |
1021 | case V_ASN1_SET: | |
1022 | case V_ASN1_SEQUENCE: | |
1023 | default: | |
80b2ff97 DSH |
1024 | if (utype == V_ASN1_BMPSTRING && (len & 1)) |
1025 | { | |
1026 | ASN1err(ASN1_F_ASN1_EX_C2I, | |
1027 | ASN1_R_BMPSTRING_IS_WRONG_LENGTH); | |
1028 | goto err; | |
1029 | } | |
1030 | if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) | |
1031 | { | |
1032 | ASN1err(ASN1_F_ASN1_EX_C2I, | |
1033 | ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); | |
1034 | goto err; | |
1035 | } | |
9d6b1ce6 | 1036 | /* All based on ASN1_STRING and handled the same */ |
8845420f DSH |
1037 | if (!*pval) |
1038 | { | |
9d6b1ce6 | 1039 | stmp = ASN1_STRING_type_new(utype); |
8845420f DSH |
1040 | if (!stmp) |
1041 | { | |
8afca8d9 | 1042 | ASN1err(ASN1_F_ASN1_EX_C2I, |
8845420f | 1043 | ERR_R_MALLOC_FAILURE); |
9d6b1ce6 | 1044 | goto err; |
8845420f | 1045 | } |
9d6b1ce6 | 1046 | *pval = (ASN1_VALUE *)stmp; |
8845420f DSH |
1047 | } |
1048 | else | |
1049 | { | |
9d6b1ce6 DSH |
1050 | stmp = (ASN1_STRING *)*pval; |
1051 | stmp->type = utype; | |
8845420f | 1052 | } |
9d6b1ce6 | 1053 | /* If we've already allocated a buffer use it */ |
8845420f DSH |
1054 | if (*free_cont) |
1055 | { | |
1056 | if (stmp->data) | |
1057 | OPENSSL_free(stmp->data); | |
875a644a | 1058 | stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */ |
9d6b1ce6 DSH |
1059 | stmp->length = len; |
1060 | *free_cont = 0; | |
8845420f DSH |
1061 | } |
1062 | else | |
1063 | { | |
1064 | if (!ASN1_STRING_set(stmp, cont, len)) | |
1065 | { | |
8afca8d9 | 1066 | ASN1err(ASN1_F_ASN1_EX_C2I, |
8845420f | 1067 | ERR_R_MALLOC_FAILURE); |
9d6b1ce6 DSH |
1068 | ASN1_STRING_free(stmp); |
1069 | *pval = NULL; | |
1070 | goto err; | |
8845420f | 1071 | } |
9d6b1ce6 | 1072 | } |
9d6b1ce6 | 1073 | break; |
8845420f | 1074 | } |
9d6b1ce6 | 1075 | /* If ASN1_ANY and NULL type fix up value */ |
8845420f DSH |
1076 | if (typ && (utype == V_ASN1_NULL)) |
1077 | typ->value.ptr = NULL; | |
9d6b1ce6 DSH |
1078 | |
1079 | ret = 1; | |
1080 | err: | |
8845420f | 1081 | if (!ret) |
29902449 DSH |
1082 | { |
1083 | ASN1_TYPE_free(typ); | |
1084 | if (opval) | |
1085 | *opval = NULL; | |
1086 | } | |
9d6b1ce6 | 1087 | return ret; |
8845420f | 1088 | } |
9d6b1ce6 | 1089 | |
e1cc0671 DSH |
1090 | |
1091 | /* This function finds the end of an ASN1 structure when passed its maximum | |
1092 | * length, whether it is indefinite length and a pointer to the content. | |
1093 | * This is more efficient than calling asn1_collect because it does not | |
1094 | * recurse on each indefinite length header. | |
1095 | */ | |
1096 | ||
1097 | static int asn1_find_end(const unsigned char **in, long len, char inf) | |
1098 | { | |
1099 | int expected_eoc; | |
1100 | long plen; | |
1101 | const unsigned char *p = *in, *q; | |
1102 | /* If not indefinite length constructed just add length */ | |
1103 | if (inf == 0) | |
1104 | { | |
1105 | *in += len; | |
1106 | return 1; | |
1107 | } | |
1108 | expected_eoc = 1; | |
1109 | /* Indefinite length constructed form. Find the end when enough EOCs | |
1110 | * are found. If more indefinite length constructed headers | |
98a2fd32 | 1111 | * are encountered increment the expected eoc count otherwise just |
e1cc0671 DSH |
1112 | * skip to the end of the data. |
1113 | */ | |
1114 | while (len > 0) | |
1115 | { | |
1116 | if(asn1_check_eoc(&p, len)) | |
1117 | { | |
1118 | expected_eoc--; | |
1119 | if (expected_eoc == 0) | |
1120 | break; | |
1121 | len -= 2; | |
1122 | continue; | |
1123 | } | |
1124 | q = p; | |
1125 | /* Just read in a header: only care about the length */ | |
1126 | if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, | |
1127 | -1, 0, 0, NULL)) | |
1128 | { | |
1129 | ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); | |
1130 | return 0; | |
1131 | } | |
1132 | if (inf) | |
1133 | expected_eoc++; | |
1134 | else | |
1135 | p += plen; | |
1136 | len -= p - q; | |
1137 | } | |
1138 | if (expected_eoc) | |
1139 | { | |
1140 | ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC); | |
1141 | return 0; | |
1142 | } | |
1143 | *in = p; | |
1144 | return 1; | |
1145 | } | |
9d6b1ce6 DSH |
1146 | /* This function collects the asn1 data from a constructred string |
1147 | * type into a buffer. The values of 'in' and 'len' should refer | |
1148 | * to the contents of the constructed type and 'inf' should be set | |
e1cc0671 | 1149 | * if it is indefinite length. |
9d6b1ce6 DSH |
1150 | */ |
1151 | ||
854a225a DSH |
1152 | #ifndef ASN1_MAX_STRING_NEST |
1153 | /* This determines how many levels of recursion are permitted in ASN1 | |
1154 | * string types. If it is not limited stack overflows can occur. If set | |
1155 | * to zero no recursion is allowed at all. Although zero should be adequate | |
1156 | * examples exist that require a value of 1. So 5 should be more than enough. | |
1157 | */ | |
1158 | #define ASN1_MAX_STRING_NEST 5 | |
1159 | #endif | |
1160 | ||
1161 | ||
8845420f | 1162 | static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, |
854a225a | 1163 | char inf, int tag, int aclass, int depth) |
8845420f | 1164 | { |
875a644a | 1165 | const unsigned char *p, *q; |
9d6b1ce6 DSH |
1166 | long plen; |
1167 | char cst, ininf; | |
1168 | p = *in; | |
1169 | inf &= 1; | |
8845420f DSH |
1170 | /* If no buffer and not indefinite length constructed just pass over |
1171 | * the encoded data */ | |
1172 | if (!buf && !inf) | |
1173 | { | |
9d6b1ce6 DSH |
1174 | *in += len; |
1175 | return 1; | |
8845420f DSH |
1176 | } |
1177 | while(len > 0) | |
1178 | { | |
9d6b1ce6 DSH |
1179 | q = p; |
1180 | /* Check for EOC */ | |
8845420f DSH |
1181 | if (asn1_check_eoc(&p, len)) |
1182 | { | |
1183 | /* EOC is illegal outside indefinite length | |
1184 | * constructed form */ | |
1185 | if (!inf) | |
1186 | { | |
1187 | ASN1err(ASN1_F_ASN1_COLLECT, | |
1188 | ASN1_R_UNEXPECTED_EOC); | |
9d6b1ce6 | 1189 | return 0; |
8845420f | 1190 | } |
9d6b1ce6 DSH |
1191 | inf = 0; |
1192 | break; | |
8845420f DSH |
1193 | } |
1194 | ||
1195 | if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, | |
1196 | len, tag, aclass, 0, NULL)) | |
1197 | { | |
9d6b1ce6 DSH |
1198 | ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); |
1199 | return 0; | |
8845420f DSH |
1200 | } |
1201 | ||
9d6b1ce6 | 1202 | /* If indefinite length constructed update max length */ |
8845420f DSH |
1203 | if (cst) |
1204 | { | |
854a225a DSH |
1205 | if (depth >= ASN1_MAX_STRING_NEST) |
1206 | { | |
1207 | ASN1err(ASN1_F_ASN1_COLLECT, | |
1208 | ASN1_R_NESTED_ASN1_STRING); | |
1209 | return 0; | |
1210 | } | |
1211 | if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, | |
1212 | depth + 1)) | |
8845420f DSH |
1213 | return 0; |
1214 | } | |
c7474d07 | 1215 | else if (plen && !collect_data(buf, &p, plen)) |
8845420f | 1216 | return 0; |
9d6b1ce6 | 1217 | len -= p - q; |
8845420f DSH |
1218 | } |
1219 | if (inf) | |
1220 | { | |
9d6b1ce6 DSH |
1221 | ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC); |
1222 | return 0; | |
8845420f | 1223 | } |
9d6b1ce6 DSH |
1224 | *in = p; |
1225 | return 1; | |
8845420f | 1226 | } |
9d6b1ce6 | 1227 | |
875a644a | 1228 | static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) |
8845420f DSH |
1229 | { |
1230 | int len; | |
1231 | if (buf) | |
1232 | { | |
1233 | len = buf->length; | |
1234 | if (!BUF_MEM_grow_clean(buf, len + plen)) | |
1235 | { | |
1236 | ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE); | |
1237 | return 0; | |
9d6b1ce6 | 1238 | } |
8845420f | 1239 | memcpy(buf->data + len, *p, plen); |
9d6b1ce6 | 1240 | } |
8845420f DSH |
1241 | *p += plen; |
1242 | return 1; | |
1243 | } | |
9d6b1ce6 DSH |
1244 | |
1245 | /* Check for ASN1 EOC and swallow it if found */ | |
1246 | ||
875a644a | 1247 | static int asn1_check_eoc(const unsigned char **in, long len) |
8845420f | 1248 | { |
875a644a | 1249 | const unsigned char *p; |
8845420f | 1250 | if (len < 2) return 0; |
9d6b1ce6 | 1251 | p = *in; |
8845420f DSH |
1252 | if (!p[0] && !p[1]) |
1253 | { | |
9d6b1ce6 DSH |
1254 | *in += 2; |
1255 | return 1; | |
8845420f | 1256 | } |
9d6b1ce6 | 1257 | return 0; |
8845420f | 1258 | } |
9d6b1ce6 DSH |
1259 | |
1260 | /* Check an ASN1 tag and length: a bit like ASN1_get_object | |
1261 | * but it sets the length for indefinite length constructed | |
1262 | * form, we don't know the exact length but we can set an | |
1263 | * upper bound to the amount of data available minus the | |
1264 | * header length just read. | |
1265 | */ | |
1266 | ||
8845420f DSH |
1267 | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
1268 | char *inf, char *cst, | |
1269 | const unsigned char **in, long len, | |
1270 | int exptag, int expclass, char opt, | |
1271 | ASN1_TLC *ctx) | |
1272 | { | |
9d6b1ce6 DSH |
1273 | int i; |
1274 | int ptag, pclass; | |
6343829a | 1275 | long plen; |
875a644a | 1276 | const unsigned char *p, *q; |
9d6b1ce6 DSH |
1277 | p = *in; |
1278 | q = p; | |
1279 | ||
8845420f DSH |
1280 | if (ctx && ctx->valid) |
1281 | { | |
9d6b1ce6 DSH |
1282 | i = ctx->ret; |
1283 | plen = ctx->plen; | |
1284 | pclass = ctx->pclass; | |
1285 | ptag = ctx->ptag; | |
1286 | p += ctx->hdrlen; | |
8845420f DSH |
1287 | } |
1288 | else | |
1289 | { | |
9d6b1ce6 | 1290 | i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); |
8845420f DSH |
1291 | if (ctx) |
1292 | { | |
9d6b1ce6 DSH |
1293 | ctx->ret = i; |
1294 | ctx->plen = plen; | |
1295 | ctx->pclass = pclass; | |
1296 | ctx->ptag = ptag; | |
1297 | ctx->hdrlen = p - q; | |
1298 | ctx->valid = 1; | |
41ab00be DSH |
1299 | /* If definite length, and no error, length + |
1300 | * header can't exceed total amount of data available. | |
ecbe0781 | 1301 | */ |
8845420f DSH |
1302 | if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) |
1303 | { | |
1304 | ASN1err(ASN1_F_ASN1_CHECK_TLEN, | |
1305 | ASN1_R_TOO_LONG); | |
ecbe0781 DSH |
1306 | asn1_tlc_clear(ctx); |
1307 | return 0; | |
8845420f | 1308 | } |
ecbe0781 | 1309 | } |
9d6b1ce6 | 1310 | } |
c962479b | 1311 | |
8845420f DSH |
1312 | if (i & 0x80) |
1313 | { | |
9d6b1ce6 DSH |
1314 | ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER); |
1315 | asn1_tlc_clear(ctx); | |
1316 | return 0; | |
8845420f DSH |
1317 | } |
1318 | if (exptag >= 0) | |
1319 | { | |
1320 | if ((exptag != ptag) || (expclass != pclass)) | |
1321 | { | |
1322 | /* If type is OPTIONAL, not an error: | |
1323 | * indicate missing type. | |
9d6b1ce6 | 1324 | */ |
8845420f | 1325 | if (opt) return -1; |
9d6b1ce6 DSH |
1326 | asn1_tlc_clear(ctx); |
1327 | ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); | |
1328 | return 0; | |
8845420f DSH |
1329 | } |
1330 | /* We have a tag and class match: | |
1331 | * assume we are going to do something with it */ | |
9d6b1ce6 | 1332 | asn1_tlc_clear(ctx); |
8845420f | 1333 | } |
9d6b1ce6 | 1334 | |
8845420f DSH |
1335 | if (i & 1) |
1336 | plen = len - (p - q); | |
9d6b1ce6 | 1337 | |
8845420f DSH |
1338 | if (inf) |
1339 | *inf = i & 1; | |
9d6b1ce6 | 1340 | |
8845420f DSH |
1341 | if (cst) |
1342 | *cst = i & V_ASN1_CONSTRUCTED; | |
9d6b1ce6 | 1343 | |
8845420f DSH |
1344 | if (olen) |
1345 | *olen = plen; | |
1346 | ||
1347 | if (oclass) | |
1348 | *oclass = pclass; | |
1349 | ||
1350 | if (otag) | |
1351 | *otag = ptag; | |
9d6b1ce6 DSH |
1352 | |
1353 | *in = p; | |
1354 | return 1; | |
8845420f | 1355 | } |