Upgrade to OpenSSL-0.9.8k.
[dragonfly.git] / crypto / openssl / crypto / asn1 / tasn_dec.c
blob48bc1c0d4d090be4c43fa6910b907157a05b5fad
1 /* tasn_dec.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5 /* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
60 #include <stddef.h>
61 #include <string.h>
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>
68 static int asn1_check_eoc(const unsigned char **in, long len);
69 static int asn1_find_end(const unsigned char **in, long len, char inf);
71 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth);
74 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
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);
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);
95 /* Table to convert tags to bit values, used for MSTRING type */
96 static const unsigned long tag2bit[32] = {
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 */
101 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
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 */
108 unsigned long ASN1_tag2bit(int tag)
110 if ((tag < 0) || (tag > 30)) return 0;
111 return tag2bit[tag];
114 /* Macro to initialize and invalidate the cache */
116 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
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.
126 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
127 const unsigned char **in, long len, const ASN1_ITEM *it)
129 ASN1_TLC c;
130 ASN1_VALUE *ptmpval = NULL;
131 if (!pval)
132 pval = &ptmpval;
133 c.valid = 0;
134 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
135 return *pval;
136 return NULL;
139 int ASN1_template_d2i(ASN1_VALUE **pval,
140 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
142 ASN1_TLC c;
143 c.valid = 0;
144 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
148 /* Decode an item, taking care of IMPLICIT tagging, if any.
149 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
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)
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;
161 const unsigned char *p = NULL, *q;
162 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
163 unsigned char imphack = 0, oclass;
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;
170 if (!pval)
171 return 0;
172 if (aux && aux->asn1_cb)
173 asn1_cb = aux->asn1_cb;
174 else asn1_cb = 0;
176 switch(it->itype)
178 case ASN1_ITYPE_PRIMITIVE:
179 if (it->templates)
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.
187 if ((tag != -1) || opt)
189 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
190 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
191 goto err;
193 return asn1_template_ex_d2i(pval, in, len,
194 it->templates, opt, ctx);
196 return asn1_d2i_ex_primitive(pval, in, len, it,
197 tag, aclass, opt, ctx);
198 break;
200 case ASN1_ITYPE_MSTRING:
201 p = *in;
202 /* Just read in tag and class */
203 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
204 &p, len, -1, 0, 1, ctx);
205 if (!ret)
207 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
208 ERR_R_NESTED_ASN1_ERROR);
209 goto err;
212 /* Must be UNIVERSAL class */
213 if (oclass != V_ASN1_UNIVERSAL)
215 /* If OPTIONAL, assume this is OK */
216 if (opt) return -1;
217 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
218 ASN1_R_MSTRING_NOT_UNIVERSAL);
219 goto err;
221 /* Check tag matches bit map */
222 if (!(ASN1_tag2bit(otag) & it->utype))
224 /* If OPTIONAL, assume this is OK */
225 if (opt)
226 return -1;
227 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
228 ASN1_R_MSTRING_WRONG_TAG);
229 goto err;
231 return asn1_d2i_ex_primitive(pval, in, len,
232 it, otag, 0, 0, ctx);
234 case ASN1_ITYPE_EXTERN:
235 /* Use new style d2i */
236 ef = it->funcs;
237 return ef->asn1_ex_d2i(pval, in, len,
238 it, tag, aclass, opt, ctx);
240 case ASN1_ITYPE_COMPAT:
241 /* we must resort to old style evil hackery */
242 cf = it->funcs;
244 /* If OPTIONAL see if it is there */
245 if (opt)
247 int exptag;
248 p = *in;
249 if (tag == -1)
250 exptag = it->utype;
251 else exptag = tag;
252 /* Don't care about anything other than presence
253 * of expected tag */
255 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
256 &p, len, exptag, aclass, 1, ctx);
257 if (!ret)
259 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
260 ERR_R_NESTED_ASN1_ERROR);
261 goto err;
263 if (ret == -1)
264 return -1;
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.
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.
282 if (tag != -1)
284 wp = *(unsigned char **)in;
285 imphack = *wp;
286 if (p == NULL)
288 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
289 ERR_R_NESTED_ASN1_ERROR);
290 goto err;
292 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
293 | it->utype);
296 ptmpval = cf->asn1_d2i(pval, in, len);
298 if (tag != -1)
299 *wp = imphack;
301 if (ptmpval)
302 return 1;
304 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
305 goto err;
308 case ASN1_ITYPE_CHOICE:
309 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
310 goto auxerr;
312 /* Allocate structure */
313 if (!*pval && !ASN1_item_ex_new(pval, it))
315 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
316 ERR_R_NESTED_ASN1_ERROR);
317 goto err;
319 /* CHOICE type, try each possibility in turn */
320 pchval = NULL;
321 p = *in;
322 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
324 pchptr = asn1_get_field_ptr(pval, tt);
325 /* We mark field as OPTIONAL so its absence
326 * can be recognised.
328 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
329 /* If field not present, try the next one */
330 if (ret == -1)
331 continue;
332 /* If positive return, read OK, break loop */
333 if (ret > 0)
334 break;
335 /* Otherwise must be an ASN1 parsing error */
336 errtt = tt;
337 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
338 ERR_R_NESTED_ASN1_ERROR);
339 goto err;
342 /* Did we fall off the end without reading anything? */
343 if (i == it->tcount)
345 /* If OPTIONAL, this is OK */
346 if (opt)
348 /* Free and zero it */
349 ASN1_item_ex_free(pval, it);
350 return -1;
352 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
353 ASN1_R_NO_MATCHING_CHOICE_TYPE);
354 goto err;
357 asn1_set_choice_selector(pval, i, it);
358 *in = p;
359 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
360 goto auxerr;
361 return 1;
363 case ASN1_ITYPE_NDEF_SEQUENCE:
364 case ASN1_ITYPE_SEQUENCE:
365 p = *in;
366 tmplen = len;
368 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
369 if (tag == -1)
371 tag = V_ASN1_SEQUENCE;
372 aclass = V_ASN1_UNIVERSAL;
374 /* Get SEQUENCE length and update len, p */
375 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
376 &p, len, tag, aclass, opt, ctx);
377 if (!ret)
379 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
380 ERR_R_NESTED_ASN1_ERROR);
381 goto err;
383 else if (ret == -1)
384 return -1;
385 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
387 len = tmplen - (p - *in);
388 seq_nolen = 1;
390 /* If indefinite we don't do a length check */
391 else seq_nolen = seq_eoc;
392 if (!cst)
394 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
395 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
396 goto err;
399 if (!*pval && !ASN1_item_ex_new(pval, it))
401 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
402 ERR_R_NESTED_ASN1_ERROR);
403 goto err;
406 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
407 goto auxerr;
409 /* Get each field entry */
410 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
412 const ASN1_TEMPLATE *seqtt;
413 ASN1_VALUE **pseqval;
414 seqtt = asn1_do_adb(pval, tt, 1);
415 if (!seqtt)
416 goto err;
417 pseqval = asn1_get_field_ptr(pval, seqtt);
418 /* Have we ran out of data? */
419 if (!len)
420 break;
421 q = p;
422 if (asn1_check_eoc(&p, len))
424 if (!seq_eoc)
426 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
427 ASN1_R_UNEXPECTED_EOC);
428 goto err;
430 len -= p - q;
431 seq_eoc = 0;
432 q = p;
433 break;
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.
441 if (i == (it->tcount - 1))
442 isopt = 0;
443 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444 /* attempt to read in field, allowing each to be
445 * OPTIONAL */
447 ret = asn1_template_ex_d2i(pseqval, &p, len,
448 seqtt, isopt, ctx);
449 if (!ret)
451 errtt = seqtt;
452 goto err;
454 else if (ret == -1)
456 /* OPTIONAL component absent.
457 * Free and zero the field.
459 ASN1_template_free(pseqval, seqtt);
460 continue;
462 /* Update length */
463 len -= p - q;
466 /* Check for EOC if expecting one */
467 if (seq_eoc && !asn1_check_eoc(&p, len))
469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
470 goto err;
472 /* Check all data read */
473 if (!seq_nolen && len)
475 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
476 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
477 goto err;
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.
484 for (; i < it->tcount; tt++, i++)
486 const ASN1_TEMPLATE *seqtt;
487 seqtt = asn1_do_adb(pval, tt, 1);
488 if (!seqtt)
489 goto err;
490 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
492 ASN1_VALUE **pseqval;
493 pseqval = asn1_get_field_ptr(pval, seqtt);
494 ASN1_template_free(pseqval, seqtt);
496 else
498 errtt = seqtt;
499 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
500 ASN1_R_FIELD_MISSING);
501 goto err;
504 /* Save encoding */
505 if (!asn1_enc_save(pval, *in, p - *in, it))
506 goto auxerr;
507 *in = p;
508 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
509 goto auxerr;
510 return 1;
512 default:
513 return 0;
515 auxerr:
516 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
517 err:
518 ASN1_item_ex_free(pval, it);
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);
524 return 0;
527 /* Templates are handled with two separate functions.
528 * One handles any EXPLICIT tag and the other handles the rest.
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)
536 int flags, aclass;
537 int ret;
538 long len;
539 const unsigned char *p, *q;
540 char exp_eoc;
541 if (!val)
542 return 0;
543 flags = tt->flags;
544 aclass = flags & ASN1_TFLG_TAG_CLASS;
546 p = *in;
548 /* Check if EXPLICIT tag expected */
549 if (flags & ASN1_TFLG_EXPTAG)
551 char cst;
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.
556 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
557 &p, inlen, tt->tag, aclass, opt, ctx);
558 q = p;
559 if (!ret)
561 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
562 ERR_R_NESTED_ASN1_ERROR);
563 return 0;
565 else if (ret == -1)
566 return -1;
567 if (!cst)
569 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
570 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
571 return 0;
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);
575 if (!ret)
577 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
578 ERR_R_NESTED_ASN1_ERROR);
579 return 0;
581 /* We read the field in OK so update length */
582 len -= p - q;
583 if (exp_eoc)
585 /* If NDEF we must have an EOC here */
586 if (!asn1_check_eoc(&p, len))
588 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
589 ASN1_R_MISSING_EOC);
590 goto err;
593 else
595 /* Otherwise we must hit the EXPLICIT tag end or its
596 * an error */
597 if (len)
599 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
600 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
601 goto err;
605 else
606 return asn1_template_noexp_d2i(val, in, inlen,
607 tt, opt, ctx);
609 *in = p;
610 return 1;
612 err:
613 ASN1_template_free(val, tt);
614 return 0;
617 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
618 const unsigned char **in, long len,
619 const ASN1_TEMPLATE *tt, char opt,
620 ASN1_TLC *ctx)
622 int flags, aclass;
623 int ret;
624 const unsigned char *p, *q;
625 if (!val)
626 return 0;
627 flags = tt->flags;
628 aclass = flags & ASN1_TFLG_TAG_CLASS;
630 p = *in;
631 q = p;
633 if (flags & ASN1_TFLG_SK_MASK)
635 /* SET OF, SEQUENCE OF */
636 int sktag, skaclass;
637 char sk_eoc;
638 /* First work out expected inner tag value */
639 if (flags & ASN1_TFLG_IMPTAG)
641 sktag = tt->tag;
642 skaclass = aclass;
644 else
646 skaclass = V_ASN1_UNIVERSAL;
647 if (flags & ASN1_TFLG_SET_OF)
648 sktag = V_ASN1_SET;
649 else
650 sktag = V_ASN1_SEQUENCE;
652 /* Get the tag */
653 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
654 &p, len, sktag, skaclass, opt, ctx);
655 if (!ret)
657 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
658 ERR_R_NESTED_ASN1_ERROR);
659 return 0;
661 else if (ret == -1)
662 return -1;
663 if (!*val)
664 *val = (ASN1_VALUE *)sk_new_null();
665 else
667 /* We've got a valid STACK: free up any items present */
668 STACK *sktmp = (STACK *)*val;
669 ASN1_VALUE *vtmp;
670 while(sk_num(sktmp) > 0)
672 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
673 ASN1_item_ex_free(&vtmp,
674 ASN1_ITEM_ptr(tt->item));
678 if (!*val)
680 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
681 ERR_R_MALLOC_FAILURE);
682 goto err;
685 /* Read as many items as we can */
686 while(len > 0)
688 ASN1_VALUE *skfield;
689 q = p;
690 /* See if EOC found */
691 if (asn1_check_eoc(&p, len))
693 if (!sk_eoc)
695 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
696 ASN1_R_UNEXPECTED_EOC);
697 goto err;
699 len -= p - q;
700 sk_eoc = 0;
701 break;
703 skfield = NULL;
704 if (!ASN1_item_ex_d2i(&skfield, &p, len,
705 ASN1_ITEM_ptr(tt->item),
706 -1, 0, 0, ctx))
708 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
709 ERR_R_NESTED_ASN1_ERROR);
710 goto err;
712 len -= p - q;
713 if (!sk_push((STACK *)*val, (char *)skfield))
715 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
716 ERR_R_MALLOC_FAILURE);
717 goto err;
720 if (sk_eoc)
722 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
723 goto err;
726 else if (flags & ASN1_TFLG_IMPTAG)
728 /* IMPLICIT tagging */
729 ret = ASN1_item_ex_d2i(val, &p, len,
730 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
731 if (!ret)
733 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
734 ERR_R_NESTED_ASN1_ERROR);
735 goto err;
737 else if (ret == -1)
738 return -1;
740 else
742 /* Nothing special */
743 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
744 -1, 0, opt, ctx);
745 if (!ret)
747 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
748 ERR_R_NESTED_ASN1_ERROR);
749 goto err;
751 else if (ret == -1)
752 return -1;
755 *in = p;
756 return 1;
758 err:
759 ASN1_template_free(val, tt);
760 return 0;
763 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
764 const unsigned char **in, long inlen,
765 const ASN1_ITEM *it,
766 int tag, int aclass, char opt, ASN1_TLC *ctx)
768 int ret = 0, utype;
769 long plen;
770 char cst, inf, free_cont = 0;
771 const unsigned char *p;
772 BUF_MEM buf;
773 const unsigned char *cont = NULL;
774 long len;
775 if (!pval)
777 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
778 return 0; /* Should never happen */
781 if (it->itype == ASN1_ITYPE_MSTRING)
783 utype = tag;
784 tag = -1;
786 else
787 utype = it->utype;
789 if (utype == V_ASN1_ANY)
791 /* If type is ANY need to figure out type from tag */
792 unsigned char oclass;
793 if (tag >= 0)
795 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
796 ASN1_R_ILLEGAL_TAGGED_ANY);
797 return 0;
799 if (opt)
801 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
802 ASN1_R_ILLEGAL_OPTIONAL_ANY);
803 return 0;
805 p = *in;
806 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
807 &p, inlen, -1, 0, 0, ctx);
808 if (!ret)
810 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
811 ERR_R_NESTED_ASN1_ERROR);
812 return 0;
814 if (oclass != V_ASN1_UNIVERSAL)
815 utype = V_ASN1_OTHER;
817 if (tag == -1)
819 tag = utype;
820 aclass = V_ASN1_UNIVERSAL;
822 p = *in;
823 /* Check header */
824 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
825 &p, inlen, tag, aclass, opt, ctx);
826 if (!ret)
828 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
829 return 0;
831 else if (ret == -1)
832 return -1;
833 ret = 0;
834 /* SEQUENCE, SET and "OTHER" are left in encoded form */
835 if ((utype == V_ASN1_SEQUENCE)
836 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
838 /* Clear context cache for type OTHER because the auto clear
839 * when we have a exact match wont work
841 if (utype == V_ASN1_OTHER)
843 asn1_tlc_clear(ctx);
845 /* SEQUENCE and SET must be constructed */
846 else if (!cst)
848 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
849 ASN1_R_TYPE_NOT_CONSTRUCTED);
850 return 0;
853 cont = *in;
854 /* If indefinite length constructed find the real end */
855 if (inf)
857 if (!asn1_find_end(&p, plen, inf))
858 goto err;
859 len = p - cont;
861 else
863 len = p - cont + plen;
864 p += plen;
865 buf.data = NULL;
868 else if (cst)
870 buf.length = 0;
871 buf.max = 0;
872 buf.data = NULL;
873 /* Should really check the internal tags are correct but
874 * some things may get this wrong. The relevant specs
875 * say that constructed string types should be OCTET STRINGs
876 * internally irrespective of the type. So instead just check
877 * for UNIVERSAL class and ignore the tag.
879 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
881 free_cont = 1;
882 goto err;
884 len = buf.length;
885 /* Append a final null to string */
886 if (!BUF_MEM_grow_clean(&buf, len + 1))
888 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
889 ERR_R_MALLOC_FAILURE);
890 return 0;
892 buf.data[len] = 0;
893 cont = (const unsigned char *)buf.data;
894 free_cont = 1;
896 else
898 cont = p;
899 len = plen;
900 p += plen;
903 /* We now have content length and type: translate into a structure */
904 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
905 goto err;
907 *in = p;
908 ret = 1;
909 err:
910 if (free_cont && buf.data) OPENSSL_free(buf.data);
911 return ret;
914 /* Translate ASN1 content octets into a structure */
916 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
917 int utype, char *free_cont, const ASN1_ITEM *it)
919 ASN1_VALUE **opval = NULL;
920 ASN1_STRING *stmp;
921 ASN1_TYPE *typ = NULL;
922 int ret = 0;
923 const ASN1_PRIMITIVE_FUNCS *pf;
924 ASN1_INTEGER **tint;
925 pf = it->funcs;
927 if (pf && pf->prim_c2i)
928 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
929 /* If ANY type clear type and set pointer to internal value */
930 if (it->utype == V_ASN1_ANY)
932 if (!*pval)
934 typ = ASN1_TYPE_new();
935 if (typ == NULL)
936 goto err;
937 *pval = (ASN1_VALUE *)typ;
939 else
940 typ = (ASN1_TYPE *)*pval;
942 if (utype != typ->type)
943 ASN1_TYPE_set(typ, utype, NULL);
944 opval = pval;
945 pval = &typ->value.asn1_value;
947 switch(utype)
949 case V_ASN1_OBJECT:
950 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
951 goto err;
952 break;
954 case V_ASN1_NULL:
955 if (len)
957 ASN1err(ASN1_F_ASN1_EX_C2I,
958 ASN1_R_NULL_IS_WRONG_LENGTH);
959 goto err;
961 *pval = (ASN1_VALUE *)1;
962 break;
964 case V_ASN1_BOOLEAN:
965 if (len != 1)
967 ASN1err(ASN1_F_ASN1_EX_C2I,
968 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
969 goto err;
971 else
973 ASN1_BOOLEAN *tbool;
974 tbool = (ASN1_BOOLEAN *)pval;
975 *tbool = *cont;
977 break;
979 case V_ASN1_BIT_STRING:
980 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
981 goto err;
982 break;
984 case V_ASN1_INTEGER:
985 case V_ASN1_NEG_INTEGER:
986 case V_ASN1_ENUMERATED:
987 case V_ASN1_NEG_ENUMERATED:
988 tint = (ASN1_INTEGER **)pval;
989 if (!c2i_ASN1_INTEGER(tint, &cont, len))
990 goto err;
991 /* Fixup type to match the expected form */
992 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
993 break;
995 case V_ASN1_OCTET_STRING:
996 case V_ASN1_NUMERICSTRING:
997 case V_ASN1_PRINTABLESTRING:
998 case V_ASN1_T61STRING:
999 case V_ASN1_VIDEOTEXSTRING:
1000 case V_ASN1_IA5STRING:
1001 case V_ASN1_UTCTIME:
1002 case V_ASN1_GENERALIZEDTIME:
1003 case V_ASN1_GRAPHICSTRING:
1004 case V_ASN1_VISIBLESTRING:
1005 case V_ASN1_GENERALSTRING:
1006 case V_ASN1_UNIVERSALSTRING:
1007 case V_ASN1_BMPSTRING:
1008 case V_ASN1_UTF8STRING:
1009 case V_ASN1_OTHER:
1010 case V_ASN1_SET:
1011 case V_ASN1_SEQUENCE:
1012 default:
1013 if (utype == V_ASN1_BMPSTRING && (len & 1))
1015 ASN1err(ASN1_F_ASN1_EX_C2I,
1016 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1017 goto err;
1019 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1021 ASN1err(ASN1_F_ASN1_EX_C2I,
1022 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1023 goto err;
1025 /* All based on ASN1_STRING and handled the same */
1026 if (!*pval)
1028 stmp = ASN1_STRING_type_new(utype);
1029 if (!stmp)
1031 ASN1err(ASN1_F_ASN1_EX_C2I,
1032 ERR_R_MALLOC_FAILURE);
1033 goto err;
1035 *pval = (ASN1_VALUE *)stmp;
1037 else
1039 stmp = (ASN1_STRING *)*pval;
1040 stmp->type = utype;
1042 /* If we've already allocated a buffer use it */
1043 if (*free_cont)
1045 if (stmp->data)
1046 OPENSSL_free(stmp->data);
1047 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1048 stmp->length = len;
1049 *free_cont = 0;
1051 else
1053 if (!ASN1_STRING_set(stmp, cont, len))
1055 ASN1err(ASN1_F_ASN1_EX_C2I,
1056 ERR_R_MALLOC_FAILURE);
1057 ASN1_STRING_free(stmp);
1058 *pval = NULL;
1059 goto err;
1062 break;
1064 /* If ASN1_ANY and NULL type fix up value */
1065 if (typ && (utype == V_ASN1_NULL))
1066 typ->value.ptr = NULL;
1068 ret = 1;
1069 err:
1070 if (!ret)
1072 ASN1_TYPE_free(typ);
1073 if (opval)
1074 *opval = NULL;
1076 return ret;
1080 /* This function finds the end of an ASN1 structure when passed its maximum
1081 * length, whether it is indefinite length and a pointer to the content.
1082 * This is more efficient than calling asn1_collect because it does not
1083 * recurse on each indefinite length header.
1086 static int asn1_find_end(const unsigned char **in, long len, char inf)
1088 int expected_eoc;
1089 long plen;
1090 const unsigned char *p = *in, *q;
1091 /* If not indefinite length constructed just add length */
1092 if (inf == 0)
1094 *in += len;
1095 return 1;
1097 expected_eoc = 1;
1098 /* Indefinite length constructed form. Find the end when enough EOCs
1099 * are found. If more indefinite length constructed headers
1100 * are encountered increment the expected eoc count otherwise just
1101 * skip to the end of the data.
1103 while (len > 0)
1105 if(asn1_check_eoc(&p, len))
1107 expected_eoc--;
1108 if (expected_eoc == 0)
1109 break;
1110 len -= 2;
1111 continue;
1113 q = p;
1114 /* Just read in a header: only care about the length */
1115 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1116 -1, 0, 0, NULL))
1118 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1119 return 0;
1121 if (inf)
1122 expected_eoc++;
1123 else
1124 p += plen;
1125 len -= p - q;
1127 if (expected_eoc)
1129 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1130 return 0;
1132 *in = p;
1133 return 1;
1135 /* This function collects the asn1 data from a constructred string
1136 * type into a buffer. The values of 'in' and 'len' should refer
1137 * to the contents of the constructed type and 'inf' should be set
1138 * if it is indefinite length.
1141 #ifndef ASN1_MAX_STRING_NEST
1142 /* This determines how many levels of recursion are permitted in ASN1
1143 * string types. If it is not limited stack overflows can occur. If set
1144 * to zero no recursion is allowed at all. Although zero should be adequate
1145 * examples exist that require a value of 1. So 5 should be more than enough.
1147 #define ASN1_MAX_STRING_NEST 5
1148 #endif
1151 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1152 char inf, int tag, int aclass, int depth)
1154 const unsigned char *p, *q;
1155 long plen;
1156 char cst, ininf;
1157 p = *in;
1158 inf &= 1;
1159 /* If no buffer and not indefinite length constructed just pass over
1160 * the encoded data */
1161 if (!buf && !inf)
1163 *in += len;
1164 return 1;
1166 while(len > 0)
1168 q = p;
1169 /* Check for EOC */
1170 if (asn1_check_eoc(&p, len))
1172 /* EOC is illegal outside indefinite length
1173 * constructed form */
1174 if (!inf)
1176 ASN1err(ASN1_F_ASN1_COLLECT,
1177 ASN1_R_UNEXPECTED_EOC);
1178 return 0;
1180 inf = 0;
1181 break;
1184 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1185 len, tag, aclass, 0, NULL))
1187 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1188 return 0;
1191 /* If indefinite length constructed update max length */
1192 if (cst)
1194 if (depth >= ASN1_MAX_STRING_NEST)
1196 ASN1err(ASN1_F_ASN1_COLLECT,
1197 ASN1_R_NESTED_ASN1_STRING);
1198 return 0;
1200 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1201 depth + 1))
1202 return 0;
1204 else if (plen && !collect_data(buf, &p, plen))
1205 return 0;
1206 len -= p - q;
1208 if (inf)
1210 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1211 return 0;
1213 *in = p;
1214 return 1;
1217 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1219 int len;
1220 if (buf)
1222 len = buf->length;
1223 if (!BUF_MEM_grow_clean(buf, len + plen))
1225 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1226 return 0;
1228 memcpy(buf->data + len, *p, plen);
1230 *p += plen;
1231 return 1;
1234 /* Check for ASN1 EOC and swallow it if found */
1236 static int asn1_check_eoc(const unsigned char **in, long len)
1238 const unsigned char *p;
1239 if (len < 2) return 0;
1240 p = *in;
1241 if (!p[0] && !p[1])
1243 *in += 2;
1244 return 1;
1246 return 0;
1249 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1250 * but it sets the length for indefinite length constructed
1251 * form, we don't know the exact length but we can set an
1252 * upper bound to the amount of data available minus the
1253 * header length just read.
1256 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1257 char *inf, char *cst,
1258 const unsigned char **in, long len,
1259 int exptag, int expclass, char opt,
1260 ASN1_TLC *ctx)
1262 int i;
1263 int ptag, pclass;
1264 long plen;
1265 const unsigned char *p, *q;
1266 p = *in;
1267 q = p;
1269 if (ctx && ctx->valid)
1271 i = ctx->ret;
1272 plen = ctx->plen;
1273 pclass = ctx->pclass;
1274 ptag = ctx->ptag;
1275 p += ctx->hdrlen;
1277 else
1279 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1280 if (ctx)
1282 ctx->ret = i;
1283 ctx->plen = plen;
1284 ctx->pclass = pclass;
1285 ctx->ptag = ptag;
1286 ctx->hdrlen = p - q;
1287 ctx->valid = 1;
1288 /* If definite length, and no error, length +
1289 * header can't exceed total amount of data available.
1291 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1293 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1294 ASN1_R_TOO_LONG);
1295 asn1_tlc_clear(ctx);
1296 return 0;
1301 if (i & 0x80)
1303 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1304 asn1_tlc_clear(ctx);
1305 return 0;
1307 if (exptag >= 0)
1309 if ((exptag != ptag) || (expclass != pclass))
1311 /* If type is OPTIONAL, not an error:
1312 * indicate missing type.
1314 if (opt) return -1;
1315 asn1_tlc_clear(ctx);
1316 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1317 return 0;
1319 /* We have a tag and class match:
1320 * assume we are going to do something with it */
1321 asn1_tlc_clear(ctx);
1324 if (i & 1)
1325 plen = len - (p - q);
1327 if (inf)
1328 *inf = i & 1;
1330 if (cst)
1331 *cst = i & V_ASN1_CONSTRUCTED;
1333 if (olen)
1334 *olen = plen;
1336 if (oclass)
1337 *oclass = pclass;
1339 if (otag)
1340 *otag = ptag;
1342 *in = p;
1343 return 1;