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