0604c51eca38bde9f912d7217d79b447cdb80253
[openssl.git] / crypto / asn1 / tasn_dec.c
1 /* tasn_dec.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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);
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 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 */
106         };
107
108 unsigned long ASN1_tag2bit(int tag)
109         {
110         if ((tag < 0) || (tag > 30)) return 0;
111         return tag2bit[tag];
112         }
113
114 /* Macro to initialize and invalidate the cache */
115
116 #define asn1_tlc_clear(c)       if (c) (c)->valid = 0
117 /* Version to avoid compiler warning about 'c' always non-NULL */
118 #define asn1_tlc_clear_nc(c)    (c)->valid = 0
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
128 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
129                           const unsigned char **in, size_t len,
130                           const ASN1_ITEM *it)
131         {
132         ASN1_TLC c;
133         ASN1_VALUE *ptmpval = NULL;
134         if (!pval)
135                 pval = &ptmpval;
136         asn1_tlc_clear_nc(&c);
137         if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 
138                 return *pval;
139         return NULL;
140         }
141
142 int ASN1_template_d2i(ASN1_VALUE **pval,
143                 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
144         {
145         ASN1_TLC c;
146         asn1_tlc_clear_nc(&c);
147         return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
148         }
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
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         {
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;
164         const unsigned char *p = NULL, *q;
165         unsigned char *wp=NULL; /* BIG FAT WARNING!  BREAKS CONST WHERE USED */
166         unsigned char imphack = 0, oclass;
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;
173         if (!pval)
174                 return 0;
175         if (aux && aux->asn1_cb)
176                 asn1_cb = aux->asn1_cb;
177         else asn1_cb = 0;
178
179         switch(it->itype)
180                 {
181                 case ASN1_ITYPE_PRIMITIVE:
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.
189                          */
190                         if ((tag != -1) || opt)
191                                 {
192                                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
193                                 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
194                                 goto err;
195                                 }
196                         return asn1_template_ex_d2i(pval, in, len,
197                                         it->templates, opt, ctx);
198                 }
199                 return asn1_d2i_ex_primitive(pval, in, len, it,
200                                                 tag, aclass, opt, ctx);
201                 break;
202
203                 case ASN1_ITYPE_MSTRING:
204                 p = *in;
205                 /* Just read in tag and class */
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);
212                         goto err;
213                         }
214
215                 /* Must be UNIVERSAL class */
216                 if (oclass != V_ASN1_UNIVERSAL)
217                         {
218                         /* If OPTIONAL, assume this is OK */
219                         if (opt) return -1;
220                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
221                                         ASN1_R_MSTRING_NOT_UNIVERSAL);
222                         goto err;
223                         }
224                 /* Check tag matches bit map */
225                 if (!(ASN1_tag2bit(otag) & it->utype))
226                         {
227                         /* If OPTIONAL, assume this is OK */
228                         if (opt)
229                                 return -1;
230                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
231                                         ASN1_R_MSTRING_WRONG_TAG);
232                         goto err;
233                         }
234                 return asn1_d2i_ex_primitive(pval, in, len,
235                                                 it, otag, 0, 0, ctx);
236
237                 case ASN1_ITYPE_EXTERN:
238                 /* Use new style d2i */
239                 ef = it->funcs;
240                 return ef->asn1_ex_d2i(pval, in, len,
241                                                 it, tag, aclass, opt, ctx);
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 */
248                 if (opt)
249                         {
250                         int exptag;
251                         p = *in;
252                         if (tag == -1)
253                                 exptag = it->utype;
254                         else exptag = tag;
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);
264                                 goto err;
265                                 }
266                         if (ret == -1)
267                                 return -1;
268                         }
269
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
285                 if (tag != -1)
286                         {
287                         wp = *(unsigned char **)in;
288                         imphack = *wp;
289                         if (p == NULL)
290                                 {
291                                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
292                                         ERR_R_NESTED_ASN1_ERROR);
293                                 goto err;
294                                 }
295                         *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
296                                                                 | it->utype);
297                         }
298
299                 ptmpval = cf->asn1_d2i(pval, in, len);
300
301                 if (tag != -1)
302                         *wp = imphack;
303
304                 if (ptmpval)
305                         return 1;
306
307                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
308                 goto err;
309
310
311                 case ASN1_ITYPE_CHOICE:
312                 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
313                                 goto auxerr;
314
315                 /* Allocate structure */
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;
321                         }
322                 /* CHOICE type, try each possibility in turn */
323                 pchval = NULL;
324                 p = *in;
325                 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
326                         {
327                         pchptr = asn1_get_field_ptr(pval, tt);
328                         /* We mark field as OPTIONAL so its absence
329                          * can be recognised.
330                          */
331                         ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
332                         /* If field not present, try the next one */
333                         if (ret == -1)
334                                 continue;
335                         /* If positive return, read OK, break loop */
336                         if (ret > 0)
337                                 break;
338                         /* Otherwise must be an ASN1 parsing error */
339                         errtt = tt;
340                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
341                                                 ERR_R_NESTED_ASN1_ERROR);
342                         goto err;
343                         }
344
345                 /* Did we fall off the end without reading anything? */
346                 if (i == it->tcount)
347                         {
348                         /* If OPTIONAL, this is OK */
349                         if (opt)
350                                 {
351                                 /* Free and zero it */
352                                 ASN1_item_ex_free(pval, it);
353                                 return -1;
354                                 }
355                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
356                                         ASN1_R_NO_MATCHING_CHOICE_TYPE);
357                         goto err;
358                         }
359
360                 asn1_set_choice_selector(pval, i, it);
361                 *in = p;
362                 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
363                                 goto auxerr;
364                 return 1;
365
366                 case ASN1_ITYPE_NDEF_SEQUENCE:
367                 case ASN1_ITYPE_SEQUENCE:
368                 p = *in;
369                 tmplen = len;
370
371                 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
372                 if (tag == -1)
373                         {
374                         tag = V_ASN1_SEQUENCE;
375                         aclass = V_ASN1_UNIVERSAL;
376                         }
377                 /* Get SEQUENCE length and update len, p */
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);
384                         goto err;
385                         }
386                 else if (ret == -1)
387                         return -1;
388                 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
389                         {
390                         len = tmplen - (p - *in);
391                         seq_nolen = 1;
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);
399                         goto err;
400                         }
401
402                 if (!*pval && !ASN1_item_ex_new(pval, it))
403                         {
404                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
405                                 ERR_R_NESTED_ASN1_ERROR);
406                         goto err;
407                         }
408
409                 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
410                                 goto auxerr;
411
412                 /* Get each field entry */
413                 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
414                         {
415                         const ASN1_TEMPLATE *seqtt;
416                         ASN1_VALUE **pseqval;
417                         seqtt = asn1_do_adb(pval, tt, 1);
418                         if (!seqtt)
419                                 goto err;
420                         pseqval = asn1_get_field_ptr(pval, seqtt);
421                         /* Have we ran out of data? */
422                         if (!len)
423                                 break;
424                         q = p;
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);
431                                         goto err;
432                                         }
433                                 len -= p - q;
434                                 seq_eoc = 0;
435                                 q = p;
436                                 break;
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.
443                          */
444                         if (i == (it->tcount - 1))
445                                 isopt = 0;
446                         else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
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                                 {
454                                 errtt = seqtt;
455                                 goto err;
456                                 }
457                         else if (ret == -1)
458                                 {
459                                 /* OPTIONAL component absent.
460                                  * Free and zero the field.
461                                  */
462                                 ASN1_template_free(pseqval, seqtt);
463                                 continue;
464                                 }
465                         /* Update length */
466                         len -= p - q;
467                         }
468
469                 /* Check for EOC if expecting one */
470                 if (seq_eoc && !asn1_check_eoc(&p, len))
471                         {
472                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
473                         goto err;
474                         }
475                 /* Check all data read */
476                 if (!seq_nolen && len)
477                         {
478                         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
479                                         ASN1_R_SEQUENCE_LENGTH_MISMATCH);
480                         goto err;
481                         }
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                  */
487                 for (; i < it->tcount; tt++, i++)
488                         {
489                         const ASN1_TEMPLATE *seqtt;
490                         seqtt = asn1_do_adb(pval, tt, 1);
491                         if (!seqtt)
492                                 goto err;
493                         if (seqtt->flags & ASN1_TFLG_OPTIONAL)
494                                 {
495                                 ASN1_VALUE **pseqval;
496                                 pseqval = asn1_get_field_ptr(pval, seqtt);
497                                 ASN1_template_free(pseqval, seqtt);
498                                 }
499                         else
500                                 {
501                                 errtt = seqtt;
502                                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
503                                                         ASN1_R_FIELD_MISSING);
504                                 goto err;
505                                 }
506                         }
507                 /* Save encoding */
508                 if (!asn1_enc_save(pval, *in, p - *in, it))
509                         goto auxerr;
510                 *in = p;
511                 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
512                                 goto auxerr;
513                 return 1;
514
515                 default:
516                 return 0;
517                 }
518         auxerr:
519         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
520         err:
521         ASN1_item_ex_free(pval, it);
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);
527         return 0;
528         }
529
530 /* Templates are handled with two separate functions.
531  * One handles any EXPLICIT tag and the other handles the rest.
532  */
533
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         {
539         int flags, aclass;
540         int ret;
541         long len;
542         const unsigned char *p, *q;
543         char exp_eoc;
544         if (!val)
545                 return 0;
546         flags = tt->flags;
547         aclass = flags & ASN1_TFLG_TAG_CLASS;
548
549         p = *in;
550
551         /* Check if EXPLICIT tag expected */
552         if (flags & ASN1_TFLG_EXPTAG)
553                 {
554                 char cst;
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.
558                  */
559                 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
560                                         &p, inlen, tt->tag, aclass, opt, ctx);
561                 q = p;
562                 if (!ret)
563                         {
564                         ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
565                                         ERR_R_NESTED_ASN1_ERROR);
566                         return 0;
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);
574                         return 0;
575                         }
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);
578                 if (!ret)
579                         {
580                         ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
581                                         ERR_R_NESTED_ASN1_ERROR);
582                         return 0;
583                         }
584                 /* We read the field in OK so update length */
585                 len -= p - q;
586                 if (exp_eoc)
587                         {
588                         /* If NDEF we must have an EOC here */
589                         if (!asn1_check_eoc(&p, len))
590                                 {
591                                 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
592                                                 ASN1_R_MISSING_EOC);
593                                 goto err;
594                                 }
595                         }
596                 else
597                         {
598                         /* Otherwise we must hit the EXPLICIT tag end or its
599                          * an error */
600                         if (len)
601                                 {
602                                 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
603                                         ASN1_R_EXPLICIT_LENGTH_MISMATCH);
604                                 goto err;
605                                 }
606                         }
607                 }
608                 else 
609                         return asn1_template_noexp_d2i(val, in, inlen,
610                                                                 tt, opt, ctx);
611
612         *in = p;
613         return 1;
614
615         err:
616         ASN1_template_free(val, tt);
617         *val = NULL;
618         return 0;
619         }
620
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         {
626         int flags, aclass;
627         int ret;
628         const unsigned char *p, *q;
629         if (!val)
630                 return 0;
631         flags = tt->flags;
632         aclass = flags & ASN1_TFLG_TAG_CLASS;
633
634         p = *in;
635         q = p;
636
637         if (flags & ASN1_TFLG_SK_MASK)
638                 {
639                 /* SET OF, SEQUENCE OF */
640                 int sktag, skaclass;
641                 char sk_eoc;
642                 /* First work out expected inner tag value */
643                 if (flags & ASN1_TFLG_IMPTAG)
644                         {
645                         sktag = tt->tag;
646                         skaclass = aclass;
647                         }
648                 else
649                         {
650                         skaclass = V_ASN1_UNIVERSAL;
651                         if (flags & ASN1_TFLG_SET_OF)
652                                 sktag = V_ASN1_SET;
653                         else
654                                 sktag = V_ASN1_SEQUENCE;
655                         }
656                 /* Get the tag */
657                 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
658                                         &p, len, sktag, skaclass, opt, ctx);
659                 if (!ret)
660                         {
661                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
662                                                 ERR_R_NESTED_ASN1_ERROR);
663                         return 0;
664                         }
665                 else if (ret == -1)
666                         return -1;
667                 if (!*val)
668                         *val = (ASN1_VALUE *)sk_new_null();
669                 else
670                         {
671                         /* We've got a valid STACK: free up any items present */
672                         STACK_OF(ASN1_VALUE) *sktmp
673                             = (STACK_OF(ASN1_VALUE) *)*val;
674                         ASN1_VALUE *vtmp;
675                         while(sk_ASN1_VALUE_num(sktmp) > 0)
676                                 {
677                                 vtmp = sk_ASN1_VALUE_pop(sktmp);
678                                 ASN1_item_ex_free(&vtmp,
679                                                 ASN1_ITEM_ptr(tt->item));
680                                 }
681                         }
682                                 
683                 if (!*val)
684                         {
685                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
686                                                 ERR_R_MALLOC_FAILURE);
687                         goto err;
688                         }
689
690                 /* Read as many items as we can */
691                 while(len > 0)
692                         {
693                         ASN1_VALUE *skfield;
694                         q = p;
695                         /* See if EOC found */
696                         if (asn1_check_eoc(&p, len))
697                                 {
698                                 if (!sk_eoc)
699                                         {
700                                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
701                                                         ASN1_R_UNEXPECTED_EOC);
702                                         goto err;
703                                         }
704                                 len -= p - q;
705                                 sk_eoc = 0;
706                                 break;
707                                 }
708                         skfield = NULL;
709                         if (!ASN1_item_ex_d2i(&skfield, &p, len,
710                                                 ASN1_ITEM_ptr(tt->item),
711                                                 -1, 0, 0, ctx))
712                                 {
713                                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
714                                         ERR_R_NESTED_ASN1_ERROR);
715                                 goto err;
716                                 }
717                         len -= p - q;
718                         if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
719                                                 skfield))
720                                 {
721                                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
722                                                 ERR_R_MALLOC_FAILURE);
723                                 goto err;
724                                 }
725                         }
726                 if (sk_eoc)
727                         {
728                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
729                         goto err;
730                         }
731                 }
732         else if (flags & ASN1_TFLG_IMPTAG)
733                 {
734                 /* IMPLICIT tagging */
735                 ret = ASN1_item_ex_d2i(val, &p, len,
736                         ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
737                 if (!ret)
738                         {
739                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
740                                                 ERR_R_NESTED_ASN1_ERROR);
741                         goto err;
742                         }
743                 else if (ret == -1)
744                         return -1;
745                 }
746         else
747                 {
748                 /* Nothing special */
749                 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
750                                                         -1, 0, opt, ctx);
751                 if (!ret)
752                         {
753                         ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
754                                         ERR_R_NESTED_ASN1_ERROR);
755                         goto err;
756                         }
757                 else if (ret == -1)
758                         return -1;
759                 }
760
761         *in = p;
762         return 1;
763
764         err:
765         ASN1_template_free(val, tt);
766         *val = NULL;
767         return 0;
768         }
769
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         {
775         int ret = 0, utype;
776         long plen;
777         char cst, inf, free_cont = 0;
778         const unsigned char *p;
779         BUF_MEM buf;
780         const unsigned char *cont = NULL;
781         long len; 
782         if (!pval)
783                 {
784                 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
785                 return 0; /* Should never happen */
786                 }
787
788         if (it->itype == ASN1_ITYPE_MSTRING)
789                 {
790                 utype = tag;
791                 tag = -1;
792                 }
793         else
794                 utype = it->utype;
795
796         if (utype == V_ASN1_ANY)
797                 {
798                 /* If type is ANY need to figure out type from tag */
799                 unsigned char oclass;
800                 if (tag >= 0)
801                         {
802                         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
803                                         ASN1_R_ILLEGAL_TAGGED_ANY);
804                         return 0;
805                         }
806                 if (opt)
807                         {
808                         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
809                                         ASN1_R_ILLEGAL_OPTIONAL_ANY);
810                         return 0;
811                         }
812                 p = *in;
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);
819                         return 0;
820                         }
821                 if (oclass != V_ASN1_UNIVERSAL)
822                         utype = V_ASN1_OTHER;
823                 }
824         if (tag == -1)
825                 {
826                 tag = utype;
827                 aclass = V_ASN1_UNIVERSAL;
828                 }
829         p = *in;
830         /* Check header */
831         ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
832                                 &p, inlen, tag, aclass, opt, ctx);
833         if (!ret)
834                 {
835                 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
836                 return 0;
837                 }
838         else if (ret == -1)
839                 return -1;
840         ret = 0;
841         /* SEQUENCE, SET and "OTHER" are left in encoded form */
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
847                  */
848                 if (utype == V_ASN1_OTHER)
849                         {
850                         asn1_tlc_clear(ctx);
851                         }
852                 /* SEQUENCE and SET must be constructed */
853                 else if (!cst)
854                         {
855                         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
856                                 ASN1_R_TYPE_NOT_CONSTRUCTED);
857                         return 0;
858                         }
859
860                 cont = *in;
861                 /* If indefinite length constructed find the real end */
862                 if (inf)
863                         {
864                         if (!asn1_find_end(&p, plen, inf))
865                                  goto err;
866                         len = p - cont;
867                         }
868                 else
869                         {
870                         len = p - cont + plen;
871                         p += plen;
872                         buf.data = NULL;
873                         }
874                 }
875         else if (cst)
876                 {
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                  */
886                 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
887                         {
888                         free_cont = 1;
889                         goto err;
890                         }
891                 len = buf.length;
892                 /* Append a final null to string */
893                 if (!BUF_MEM_grow_clean(&buf, len + 1))
894                         {
895                         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
896                                                 ERR_R_MALLOC_FAILURE);
897                         return 0;
898                         }
899                 buf.data[len] = 0;
900                 cont = (const unsigned char *)buf.data;
901                 free_cont = 1;
902                 }
903         else
904                 {
905                 cont = p;
906                 len = plen;
907                 p += plen;
908                 }
909
910         /* We now have content length and type: translate into a structure */
911         if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
912                 goto err;
913
914         *in = p;
915         ret = 1;
916         err:
917         if (free_cont && buf.data) OPENSSL_free(buf.data);
918         return ret;
919         }
920
921 /* Translate ASN1 content octets into a structure */
922
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         {
926         ASN1_VALUE **opval = NULL;
927         ASN1_STRING *stmp;
928         ASN1_TYPE *typ = NULL;
929         int ret = 0;
930         const ASN1_PRIMITIVE_FUNCS *pf;
931         ASN1_INTEGER **tint;
932         pf = it->funcs;
933
934         if (pf && pf->prim_c2i)
935                 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
936         /* If ANY type clear type and set pointer to internal value */
937         if (it->utype == V_ASN1_ANY)
938                 {
939                 if (!*pval)
940                         {
941                         typ = ASN1_TYPE_new();
942                         if (typ == NULL)
943                                 goto err;
944                         *pval = (ASN1_VALUE *)typ;
945                         }
946                 else
947                         typ = (ASN1_TYPE *)*pval;
948
949                 if (utype != typ->type)
950                         ASN1_TYPE_set(typ, utype, NULL);
951                 opval = pval;
952                 pval = &typ->value.asn1_value;
953                 }
954         switch(utype)
955                 {
956                 case V_ASN1_OBJECT:
957                 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
958                         goto err;
959                 break;
960
961                 case V_ASN1_NULL:
962                 if (len)
963                         {
964                         ASN1err(ASN1_F_ASN1_EX_C2I,
965                                                 ASN1_R_NULL_IS_WRONG_LENGTH);
966                         goto err;
967                         }
968                 *pval = (ASN1_VALUE *)1;
969                 break;
970
971                 case V_ASN1_BOOLEAN:
972                 if (len != 1)
973                         {
974                         ASN1err(ASN1_F_ASN1_EX_C2I,
975                                                 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
976                         goto err;
977                         }
978                 else
979                         {
980                         ASN1_BOOLEAN *tbool;
981                         tbool = (ASN1_BOOLEAN *)pval;
982                         *tbool = *cont;
983                         }
984                 break;
985
986                 case V_ASN1_BIT_STRING:
987                 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
988                         goto err;
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:
995                 tint = (ASN1_INTEGER **)pval;
996                 if (!c2i_ASN1_INTEGER(tint, &cont, len))
997                         goto err;
998                 /* Fixup type to match the expected form */
999                 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
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 */
1021                 if (!*pval)
1022                         {
1023                         stmp = ASN1_STRING_type_new(utype);
1024                         if (!stmp)
1025                                 {
1026                                 ASN1err(ASN1_F_ASN1_EX_C2I,
1027                                                         ERR_R_MALLOC_FAILURE);
1028                                 goto err;
1029                                 }
1030                         *pval = (ASN1_VALUE *)stmp;
1031                         }
1032                 else
1033                         {
1034                         stmp = (ASN1_STRING *)*pval;
1035                         stmp->type = utype;
1036                         }
1037                 /* If we've already allocated a buffer use it */
1038                 if (*free_cont)
1039                         {
1040                         if (stmp->data)
1041                                 OPENSSL_free(stmp->data);
1042                         stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1043                         stmp->length = len;
1044                         *free_cont = 0;
1045                         }
1046                 else
1047                         {
1048                         if (!ASN1_STRING_set(stmp, cont, len))
1049                                 {
1050                                 ASN1err(ASN1_F_ASN1_EX_C2I,
1051                                                         ERR_R_MALLOC_FAILURE);
1052                                 ASN1_STRING_free(stmp); 
1053                                 *pval = NULL;
1054                                 goto err;
1055                                 }
1056                         }
1057                 break;
1058                 }
1059         /* If ASN1_ANY and NULL type fix up value */
1060         if (typ && (utype == V_ASN1_NULL))
1061                  typ->value.ptr = NULL;
1062
1063         ret = 1;
1064         err:
1065         if (!ret)
1066                 {
1067                 ASN1_TYPE_free(typ);
1068                 if (opval)
1069                         *opval = NULL;
1070                 }
1071         return ret;
1072         }
1073
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
1095          * are encountered increment the expected eoc count otherwise just
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         }
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
1133  * if it is indefinite length.
1134  */
1135
1136 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1137                                 char inf, int tag, int aclass)
1138         {
1139         const unsigned char *p, *q;
1140         long plen;
1141         char cst, ininf;
1142         p = *in;
1143         inf &= 1;
1144         /* If no buffer and not indefinite length constructed just pass over
1145          * the encoded data */
1146         if (!buf && !inf)
1147                 {
1148                 *in += len;
1149                 return 1;
1150                 }
1151         while(len > 0)
1152                 {
1153                 q = p;
1154                 /* Check for EOC */
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);
1163                                 return 0;
1164                                 }
1165                         inf = 0;
1166                         break;
1167                         }
1168
1169                 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1170                                         len, tag, aclass, 0, NULL))
1171                         {
1172                         ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1173                         return 0;
1174                         }
1175
1176                 /* If indefinite length constructed update max length */
1177                 if (cst)
1178                         {
1179 #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
1180                         if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
1181                                 return 0;
1182 #else
1183                         ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1184                         return 0;
1185 #endif
1186                         }
1187                 else if (plen && !collect_data(buf, &p, plen))
1188                         return 0;
1189                 len -= p - q;
1190                 }
1191         if (inf)
1192                 {
1193                 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1194                 return 0;
1195                 }
1196         *in = p;
1197         return 1;
1198         }
1199
1200 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
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;
1210                         }
1211                 memcpy(buf->data + len, *p, plen);
1212                 }
1213         *p += plen;
1214         return 1;
1215         }
1216
1217 /* Check for ASN1 EOC and swallow it if found */
1218
1219 static int asn1_check_eoc(const unsigned char **in, long len)
1220         {
1221         const unsigned char *p;
1222         if (len < 2) return 0;
1223         p = *in;
1224         if (!p[0] && !p[1])
1225                 {
1226                 *in += 2;
1227                 return 1;
1228                 }
1229         return 0;
1230         }
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
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         {
1245         int i;
1246         int ptag, pclass;
1247         size_t plen;
1248         const unsigned char *p, *q;
1249         p = *in;
1250         q = p;
1251
1252         if (ctx && ctx->valid)
1253                 {
1254                 i = ctx->ret;
1255                 plen = ctx->plen;
1256                 pclass = ctx->pclass;
1257                 ptag = ctx->ptag;
1258                 p += ctx->hdrlen;
1259                 }
1260         else
1261                 {
1262                 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1263                 if (ctx)
1264                         {
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;
1271                         /* If definite length, and no error, length +
1272                          * header can't exceed total amount of data available. 
1273                          */
1274                         if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1275                                 {
1276                                 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1277                                                         ASN1_R_TOO_LONG);
1278                                 asn1_tlc_clear(ctx);
1279                                 return 0;
1280                                 }
1281                         }
1282                 }
1283
1284         if (i & 0x80)
1285                 {
1286                 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1287                 asn1_tlc_clear(ctx);
1288                 return 0;
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.
1296                          */
1297                         if (opt) return -1;
1298                         asn1_tlc_clear(ctx);
1299                         ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1300                         return 0;
1301                         }
1302                 /* We have a tag and class match:
1303                  * assume we are going to do something with it */
1304                 asn1_tlc_clear(ctx);
1305                 }
1306
1307         if (i & 1)
1308                 plen = len - (p - q);
1309
1310         if (inf)
1311                 *inf = i & 1;
1312
1313         if (cst)
1314                 *cst = i & V_ASN1_CONSTRUCTED;
1315
1316         if (olen)
1317                 *olen = plen;
1318
1319         if (oclass)
1320                 *oclass = pclass;
1321
1322         if (otag)
1323                 *otag = ptag;
1324
1325         *in = p;
1326         return 1;
1327         }