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