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