2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
68 static int asn1_check_eoc(unsigned char **in, long len);
69 static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass);
70 static int collect_data(BUF_MEM *buf, unsigned char **p, long plen);
71 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst,
72 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx);
73 static int asn1_template_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
74 static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
75 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long len,
76 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx);
78 /* Macro to initialize and invalidate the cache */
80 #define asn1_tlc_clear(c) if(c) (c)->valid = 0
82 /* Decode an ASN1 item, this currently behaves just
83 * like a standard 'd2i' function. 'in' points to
84 * a buffer to read the data from, in future we will
85 * have more advanced versions that can input data
86 * a piece at a time and this will simply be a special
90 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it)
93 ASN1_VALUE *ptmpval = NULL;
94 if(!pval) pval = &ptmpval;
96 if(ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
101 int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt)
105 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
109 /* Decode an item, taking care of IMPLICIT tagging, if any.
110 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
113 int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
114 int tag, int aclass, char opt, ASN1_TLC *ctx)
116 const ASN1_TEMPLATE *tt, *errtt = NULL;
117 const ASN1_COMPAT_FUNCS *cf;
118 const ASN1_EXTERN_FUNCS *ef;
119 const ASN1_AUX *aux = it->funcs;
120 ASN1_aux_cb *asn1_cb;
121 unsigned char *p, *q, imphack = 0, oclass;
122 char seq_eoc, seq_nolen, cst, isopt;
127 ASN1_VALUE *pchval, **pchptr, *ptmpval;
129 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
134 case ASN1_ITYPE_PRIMITIVE:
136 /* tagging or OPTIONAL is currently illegal on an item template
137 * because the flags can't get passed down. In practice this isn't
138 * a problem: we include the relevant flags from the item template
139 * in the template itself.
141 if ((tag != -1) || opt) {
142 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
145 return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx);
147 return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt, ctx);
150 case ASN1_ITYPE_MSTRING:
152 /* Just read in tag and class */
153 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, &p, len, -1, 0, 1, ctx);
155 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
158 /* Must be UNIVERSAL class */
159 if(oclass != V_ASN1_UNIVERSAL) {
160 /* If OPTIONAL, assume this is OK */
162 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
165 /* Check tag matches bit map */
166 if(!(ASN1_tag2bit(otag) & it->utype)) {
167 /* If OPTIONAL, assume this is OK */
169 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
172 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
174 case ASN1_ITYPE_EXTERN:
175 /* Use new style d2i */
177 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
179 case ASN1_ITYPE_COMPAT:
180 /* we must resort to old style evil hackery */
183 /* If OPTIONAL see if it is there */
187 if(tag == -1) exptag = it->utype;
189 /* Don't care about anything other than presence of expected tag */
190 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, &p, len, exptag, aclass, 1, ctx);
192 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
195 if(ret == -1) return -1;
197 /* This is the old style evil hack IMPLICIT handling:
198 * since the underlying code is expecting a tag and
199 * class other than the one present we change the
200 * buffer temporarily then change it back afterwards.
201 * This doesn't and never did work for tags > 30.
203 * Yes this is *horrible* but it is only needed for
204 * old style d2i which will hopefully not be around
206 * FIXME: should copy the buffer then modify it so
207 * the input buffer can be const: we should *always*
208 * copy because the old style d2i might modify the
215 *p = (unsigned char)((*p & V_ASN1_CONSTRUCTED) | it->utype);
218 ptmpval = cf->asn1_d2i(pval, in, len);
220 if(tag != -1) *p = imphack;
222 if(ptmpval) return 1;
223 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
227 case ASN1_ITYPE_CHOICE:
228 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
231 /* Allocate structure */
233 if(!ASN1_item_ex_new(pval, it)) {
235 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
239 /* CHOICE type, try each possibility in turn */
242 for(i = 0, tt=it->templates; i < it->tcount; i++, tt++) {
243 pchptr = asn1_get_field_ptr(pval, tt);
244 /* We mark field as OPTIONAL so its absence
247 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
248 /* If field not present, try the next one */
249 if(ret == -1) continue;
250 /* If positive return, read OK, break loop */
252 /* Otherwise must be an ASN1 parsing error */
254 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
257 /* Did we fall off the end without reading anything? */
258 if(i == it->tcount) {
259 /* If OPTIONAL, this is OK */
261 /* Free and zero it */
262 ASN1_item_ex_free(pval, it);
265 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
268 asn1_set_choice_selector(pval, i, it);
270 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
274 case ASN1_ITYPE_SEQUENCE:
278 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
280 tag = V_ASN1_SEQUENCE;
281 aclass = V_ASN1_UNIVERSAL;
283 /* Get SEQUENCE length and update len, p */
284 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, &p, len, tag, aclass, opt, ctx);
286 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
288 } else if(ret == -1) return -1;
289 if(aux && (aux->flags & ASN1_AFLG_BROKEN)) {
290 len = tmplen - (p - *in);
292 } else seq_nolen = seq_eoc; /* If indefinite we don't do a length check */
294 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
299 if(!ASN1_item_ex_new(pval, it)) {
300 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
304 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
307 /* Get each field entry */
308 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
309 const ASN1_TEMPLATE *seqtt;
310 ASN1_VALUE **pseqval;
311 seqtt = asn1_do_adb(pval, tt, 1);
313 pseqval = asn1_get_field_ptr(pval, seqtt);
314 /* Have we ran out of data? */
317 if(asn1_check_eoc(&p, len)) {
319 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
327 /* This determines the OPTIONAL flag value. The field cannot
328 * be omitted if it is the last of a SEQUENCE and there is
329 * still data to be read. This isn't strictly necessary but
330 * it increases efficiency in some cases.
332 if(i == (it->tcount - 1)) isopt = 0;
333 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
334 /* attempt to read in field, allowing each to be OPTIONAL */
335 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
339 } else if(ret == -1) {
340 /* OPTIONAL component absent. Free and zero the field
342 ASN1_template_free(pseqval, seqtt);
348 /* Check for EOC if expecting one */
349 if(seq_eoc && !asn1_check_eoc(&p, len)) {
350 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
353 /* Check all data read */
354 if(!seq_nolen && len) {
355 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
359 /* If we get here we've got no more data in the SEQUENCE,
360 * however we may not have read all fields so check all
361 * remaining are OPTIONAL and clear any that are.
363 for(; i < it->tcount; tt++, i++) {
364 const ASN1_TEMPLATE *seqtt;
365 seqtt = asn1_do_adb(pval, tt, 1);
367 if(seqtt->flags & ASN1_TFLG_OPTIONAL) {
368 ASN1_VALUE **pseqval;
369 pseqval = asn1_get_field_ptr(pval, seqtt);
370 ASN1_template_free(pseqval, seqtt);
373 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
378 if(!asn1_enc_save(pval, *in, p - *in, it)) goto auxerr;
380 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
388 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
390 ASN1_item_ex_free(pval, it);
391 if(errtt) ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname);
392 else ERR_add_error_data(2, "Type=", it->sname);
396 /* Templates are handled with two separate functions. One handles any EXPLICIT tag and the other handles the
400 int asn1_template_ex_d2i(ASN1_VALUE **val, unsigned char **in, long inlen, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
405 unsigned char *p, *q;
409 aclass = flags & ASN1_TFLG_TAG_CLASS;
413 /* Check if EXPLICIT tag expected */
414 if(flags & ASN1_TFLG_EXPTAG) {
416 /* Need to work out amount of data available to the inner content and where it
417 * starts: so read in EXPLICIT header to get the info.
419 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, &p, inlen, tt->tag, aclass, opt, ctx);
422 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
424 } else if(ret == -1) return -1;
426 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
429 /* We've found the field so it can't be OPTIONAL now */
430 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
432 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
435 /* We read the field in OK so update length */
438 /* If NDEF we must have an EOC here */
439 if(!asn1_check_eoc(&p, len)) {
440 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
444 /* Otherwise we must hit the EXPLICIT tag end or its an error */
446 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
451 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
457 ASN1_template_free(val, tt);
462 static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
466 unsigned char *p, *q;
469 aclass = flags & ASN1_TFLG_TAG_CLASS;
474 if(flags & ASN1_TFLG_SK_MASK) {
475 /* SET OF, SEQUENCE OF */
478 /* First work out expected inner tag value */
479 if(flags & ASN1_TFLG_IMPTAG) {
483 skaclass = V_ASN1_UNIVERSAL;
484 if(flags & ASN1_TFLG_SET_OF) sktag = V_ASN1_SET;
485 else sktag = V_ASN1_SEQUENCE;
488 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, &p, len, sktag, skaclass, opt, ctx);
490 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
492 } else if(ret == -1) return -1;
493 if(!*val) *val = (ASN1_VALUE *)sk_new_null();
495 /* We've got a valid STACK: free up any items present */
496 STACK *sktmp = (STACK *)*val;
498 while(sk_num(sktmp) > 0) {
499 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
500 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
505 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_MALLOC_FAILURE);
508 /* Read as many items as we can */
512 /* See if EOC found */
513 if(asn1_check_eoc(&p, len)) {
515 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_UNEXPECTED_EOC);
523 if(!ASN1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
524 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
528 if(!sk_push((STACK *)*val, (char *)skfield)) {
529 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_MALLOC_FAILURE);
534 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
537 } else if(flags & ASN1_TFLG_IMPTAG) {
538 /* IMPLICIT tagging */
539 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
541 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
543 } else if(ret == -1) return -1;
545 /* Nothing special */
546 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0, opt, ctx);
548 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
550 } else if(ret == -1) return -1;
557 ASN1_template_free(val, tt);
562 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long inlen,
564 int tag, int aclass, char opt, ASN1_TLC *ctx)
568 char cst, inf, free_cont = 0;
571 unsigned char *cont = NULL;
574 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
575 return 0; /* Should never happen */
578 if(it->itype == ASN1_ITYPE_MSTRING) {
581 } else utype = it->utype;
583 if(utype == V_ASN1_ANY) {
584 /* If type is ANY need to figure out type from tag */
585 unsigned char oclass;
587 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
591 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_OPTIONAL_ANY);
595 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, &p, inlen, -1, 0, 0, ctx);
597 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
600 if(oclass != V_ASN1_UNIVERSAL) utype = V_ASN1_OTHER;
604 aclass = V_ASN1_UNIVERSAL;
608 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, &p, inlen, tag, aclass, opt, ctx);
610 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
612 } else if(ret == -1) return -1;
613 /* SEQUENCE, SET and "OTHER" are left in encoded form */
614 if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
615 /* SEQUENCE and SET must be constructed */
616 if((utype != V_ASN1_OTHER) && !cst) {
617 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_CONSTRUCTED);
622 /* If indefinite length constructed find the real end */
624 if(!asn1_collect(NULL, &p, plen, inf, -1, -1)) goto err;
627 len = p - cont + plen;
635 /* Should really check the internal tags are correct but
636 * some things may get this wrong. The relevant specs
637 * say that constructed string types should be OCTET STRINGs
638 * internally irrespective of the type. So instead just check
639 * for UNIVERSAL class and ignore the tag.
641 if(!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL)) goto err;
643 /* Append a final null to string */
644 if(!BUF_MEM_grow(&buf, len + 1)) {
645 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
649 cont = (unsigned char *)buf.data;
657 /* We now have content length and type: translate into a structure */
658 if(!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) goto err;
663 if(free_cont && buf.data) OPENSSL_free(buf.data);
667 /* Translate ASN1 content octets into a structure */
669 int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it)
672 ASN1_TYPE *typ = NULL;
674 const ASN1_PRIMITIVE_FUNCS *pf;
677 if(pf && pf->prim_c2i) return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
678 /* If ANY type clear type and set pointer to internal value */
679 if(it->utype == V_ASN1_ANY) {
681 typ = ASN1_TYPE_new();
682 *pval = (ASN1_VALUE *)typ;
683 } else typ = (ASN1_TYPE *)*pval;
684 if(utype != typ->type) ASN1_TYPE_set(typ, utype, NULL);
685 pval = (ASN1_VALUE **)&typ->value.ptr;
689 if(!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) goto err;
694 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_NULL_IS_WRONG_LENGTH);
697 *pval = (ASN1_VALUE *)1;
702 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
706 tbool = (ASN1_BOOLEAN *)pval;
711 case V_ASN1_BIT_STRING:
712 if(!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) goto err;
716 case V_ASN1_NEG_INTEGER:
717 case V_ASN1_ENUMERATED:
718 case V_ASN1_NEG_ENUMERATED:
719 tint = (ASN1_INTEGER **)pval;
720 if(!c2i_ASN1_INTEGER(tint, &cont, len)) goto err;
721 /* Fixup type to match the expected form */
722 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
725 case V_ASN1_OCTET_STRING:
726 case V_ASN1_NUMERICSTRING:
727 case V_ASN1_PRINTABLESTRING:
728 case V_ASN1_T61STRING:
729 case V_ASN1_VIDEOTEXSTRING:
730 case V_ASN1_IA5STRING:
732 case V_ASN1_GENERALIZEDTIME:
733 case V_ASN1_GRAPHICSTRING:
734 case V_ASN1_VISIBLESTRING:
735 case V_ASN1_GENERALSTRING:
736 case V_ASN1_UNIVERSALSTRING:
737 case V_ASN1_BMPSTRING:
738 case V_ASN1_UTF8STRING:
741 case V_ASN1_SEQUENCE:
743 /* All based on ASN1_STRING and handled the same */
745 stmp = ASN1_STRING_type_new(utype);
747 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
750 *pval = (ASN1_VALUE *)stmp;
752 stmp = (ASN1_STRING *)*pval;
755 /* If we've already allocated a buffer use it */
757 if(stmp->data) OPENSSL_free(stmp->data);
762 if(!ASN1_STRING_set(stmp, cont, len)) {
763 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
764 ASN1_STRING_free(stmp);
771 /* If ASN1_ANY and NULL type fix up value */
772 if(typ && utype==V_ASN1_NULL) typ->value.ptr = NULL;
776 if(!ret) ASN1_TYPE_free(typ);
780 /* This function collects the asn1 data from a constructred string
781 * type into a buffer. The values of 'in' and 'len' should refer
782 * to the contents of the constructed type and 'inf' should be set
783 * if it is indefinite length. If 'buf' is NULL then we just want
784 * to find the end of the current structure: useful for indefinite
785 * length constructed stuff.
788 static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass)
790 unsigned char *p, *q;
795 /* If no buffer and not indefinite length constructed just pass over the encoded data */
803 if(asn1_check_eoc(&p, len)) {
804 /* EOC is illegal outside indefinite length constructed form */
806 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
812 if(!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, len, tag, aclass, 0, NULL)) {
813 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
816 /* If indefinite length constructed update max length */
818 if(!asn1_collect(buf, &p, plen, ininf, tag, aclass)) return 0;
820 if(!collect_data(buf, &p, plen)) return 0;
825 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
832 static int collect_data(BUF_MEM *buf, unsigned char **p, long plen)
837 if(!BUF_MEM_grow(buf, len + plen)) {
838 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
841 memcpy(buf->data + len, *p, plen);
847 /* Check for ASN1 EOC and swallow it if found */
849 static int asn1_check_eoc(unsigned char **in, long len)
852 if(len < 2) return 0;
861 /* Check an ASN1 tag and length: a bit like ASN1_get_object
862 * but it sets the length for indefinite length constructed
863 * form, we don't know the exact length but we can set an
864 * upper bound to the amount of data available minus the
865 * header length just read.
868 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst,
869 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx)
874 unsigned char *p, *q;
878 if(ctx && ctx->valid) {
881 pclass = ctx->pclass;
885 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
889 ctx->pclass = pclass;
893 /* If definite length, length + header can't exceed total
894 * amount of data available.
896 if(!(i & 1) && ((plen + ctx->hdrlen) > len)) {
897 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
905 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
910 if((exptag != ptag) || (expclass != pclass)) {
911 /* If type is OPTIONAL, not an error, but indicate missing
916 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
919 /* We have a tag and class match, so assume we are going to do something with it */
923 if(i & 1) plen = len - (p - q);
925 if(inf) *inf = i & 1;
927 if(cst) *cst = i & V_ASN1_CONSTRUCTED;
929 if(olen) *olen = plen;
930 if(oclass) *oclass = pclass;
931 if(otag) *otag = ptag;