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).
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
67 static int asn1_check_eoc(unsigned char **in, long len);
68 static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass);
69 static int collect_data(BUF_MEM *buf, unsigned char **p, long plen);
70 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst,
71 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx);
72 static int asn1_template_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
73 static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
74 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long len,
75 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx);
77 /* Macro to initialize and invalidate the cache */
79 #define asn1_tlc_clear(c) if(c) (c)->valid = 0
81 /* Decode an ASN1 item, this currently behaves just
82 * like a standard 'd2i' function. 'in' points to
83 * a buffer to read the data from, in future we will
84 * have more advanced versions that can input data
85 * a piece at a time and this will simply be a special
89 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it)
92 ASN1_VALUE *ptmpval = NULL;
93 if(!pval) pval = &ptmpval;
95 if(ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
100 int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt)
104 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
108 /* Decode an item, taking care of IMPLICIT tagging, if any.
109 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
112 int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
113 int tag, int aclass, char opt, ASN1_TLC *ctx)
115 const ASN1_TEMPLATE *tt, *errtt = NULL;
116 const ASN1_COMPAT_FUNCS *cf;
117 const ASN1_EXTERN_FUNCS *ef;
118 const ASN1_AUX *aux = it->funcs;
119 ASN1_aux_cb *asn1_cb;
120 unsigned char *p, *q, imphack = 0, oclass;
121 char seq_eoc, seq_nolen, cst, isopt;
126 ASN1_VALUE *pchval, **pchptr, *ptmpval;
128 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
133 case ASN1_ITYPE_PRIMITIVE:
135 return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx);
136 return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt, ctx);
139 case ASN1_ITYPE_MSTRING:
141 /* Just read in tag and class */
142 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, &p, len, -1, 0, 1, ctx);
144 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
147 /* Must be UNIVERSAL class */
148 if(oclass != V_ASN1_UNIVERSAL) {
149 /* If OPTIONAL, assume this is OK */
151 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
154 /* Check tag matches bit map */
155 if(!(ASN1_tag2bit(otag) & it->utype)) {
156 /* If OPTIONAL, assume this is OK */
158 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
161 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
163 case ASN1_ITYPE_EXTERN:
164 /* Use new style d2i */
166 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
168 case ASN1_ITYPE_COMPAT:
169 /* we must resort to old style evil hackery */
172 /* If OPTIONAL see if it is there */
176 if(tag == -1) exptag = it->utype;
178 /* Don't care about anything other than presence of expected tag */
179 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, &p, len, exptag, aclass, 1, ctx);
181 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
184 if(ret == -1) return -1;
186 /* This is the old style evil hack IMPLICIT handling:
187 * since the underlying code is expecting a tag and
188 * class other than the one present we change the
189 * buffer temporarily then change it back afterwards.
190 * This doesn't and never did work for tags > 30.
192 * Yes this is *horrible* but it is only needed for
193 * old style d2i which will hopefully not be around
195 * FIXME: should copy the buffer then modify it so
196 * the input buffer can be const: we should *always*
197 * copy because the old style d2i might modify the
204 *p = (unsigned char)((*p & V_ASN1_CONSTRUCTED) | it->utype);
207 ptmpval = cf->asn1_d2i(pval, in, len);
209 if(tag != -1) *p = imphack;
211 if(ptmpval) return 1;
212 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
216 case ASN1_ITYPE_CHOICE:
217 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
219 /* CHOICE type, try each possibility in turn */
222 for(i = 0, tt=it->templates; i < it->tcount; i++, tt++) {
223 /* We mark field as OPTIONAL so its absence
226 ret = asn1_template_ex_d2i(&pchval, &p, len, tt, 1, ctx);
227 /* If field not present, try the next one */
228 if(ret == -1) continue;
229 /* If positive return, read OK, break loop */
231 /* Otherwise must be an ASN1 parsing error */
233 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
236 /* Did we fall off the end without reading anything? */
237 if(i == it->tcount) {
238 /* If OPTIONAL, this is OK */
240 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
243 /* Otherwise we got a match, allocate structure and populate it */
245 if(!ASN1_item_ex_new(pval, it)) {
247 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
251 pchptr = asn1_get_field_ptr(pval, tt);
253 asn1_set_choice_selector(pval, i, it);
255 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
259 case ASN1_ITYPE_SEQUENCE:
263 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
265 tag = V_ASN1_SEQUENCE;
266 aclass = V_ASN1_UNIVERSAL;
268 /* Get SEQUENCE length and update len, p */
269 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, &p, len, tag, aclass, opt, ctx);
271 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
273 } else if(ret == -1) return -1;
274 if(aux && (aux->flags & ASN1_AFLG_BROKEN)) {
275 len = tmplen - (p - *in);
277 } else seq_nolen = seq_eoc; /* If indefinite we don't do a length check */
279 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
284 if(!ASN1_item_ex_new(pval, it)) {
285 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
289 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
292 /* Get each field entry */
293 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294 const ASN1_TEMPLATE *seqtt;
295 ASN1_VALUE **pseqval;
296 seqtt = asn1_do_adb(pval, tt, 1);
298 pseqval = asn1_get_field_ptr(pval, seqtt);
299 /* Have we ran out of data? */
302 if(asn1_check_eoc(&p, len)) {
304 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
312 /* This determines the OPTIONAL flag value. The field cannot
313 * be omitted if it is the last of a SEQUENCE and there is
314 * still data to be read. This isn't strictly necessary but
315 * it increases efficiency in some cases.
317 if(i == (it->tcount - 1)) isopt = 0;
318 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
319 /* attempt to read in field, allowing each to be OPTIONAL */
320 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
324 } else if(ret == -1) {
325 /* OPTIONAL component absent. Free and zero the field
327 ASN1_template_free(pseqval, seqtt);
333 /* Check for EOC if expecting one */
334 if(seq_eoc && !asn1_check_eoc(&p, len)) {
335 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
338 /* Check all data read */
339 if(!seq_nolen && len) {
340 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
344 /* If we get here we've got no more data in the SEQUENCE,
345 * however we may not have read all fields so check all
346 * remaining are OPTIONAL and clear any that are.
348 for(; i < it->tcount; tt++, i++) {
349 const ASN1_TEMPLATE *seqtt;
350 seqtt = asn1_do_adb(pval, tt, 1);
352 if(seqtt->flags & ASN1_TFLG_OPTIONAL) {
353 ASN1_VALUE **pseqval;
354 pseqval = asn1_get_field_ptr(pval, seqtt);
355 ASN1_template_free(pseqval, seqtt);
358 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
363 if(!asn1_enc_save(pval, *in, p - *in, it)) goto auxerr;
365 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
373 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
375 ASN1_item_ex_free(pval, it);
376 if(errtt) ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname);
377 else ERR_add_error_data(2, "Type=", it->sname);
381 /* Templates are handled with two separate functions. One handles any EXPLICIT tag and the other handles the
385 int asn1_template_ex_d2i(ASN1_VALUE **val, unsigned char **in, long inlen, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
390 unsigned char *p, *q;
394 aclass = flags & ASN1_TFLG_TAG_CLASS;
398 /* Check if EXPLICIT tag expected */
399 if(flags & ASN1_TFLG_EXPTAG) {
401 /* Need to work out amount of data available to the inner content and where it
402 * starts: so read in EXPLICIT header to get the info.
404 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, &p, inlen, tt->tag, aclass, opt, ctx);
407 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
409 } else if(ret == -1) return -1;
411 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
414 /* We've found the field so it can't be OPTIONAL now */
415 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
417 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
420 /* We read the field in OK so update length */
423 /* If NDEF we must have an EOC here */
424 if(!asn1_check_eoc(&p, len)) {
425 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
429 /* Otherwise we must hit the EXPLICIT tag end or its an error */
431 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
436 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
442 ASN1_template_free(val, tt);
447 static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
451 unsigned char *p, *q;
454 aclass = flags & ASN1_TFLG_TAG_CLASS;
459 if(flags & ASN1_TFLG_SK_MASK) {
460 /* SET OF, SEQUENCE OF */
463 /* First work out expected inner tag value */
464 if(flags & ASN1_TFLG_IMPTAG) {
468 skaclass = V_ASN1_UNIVERSAL;
469 if(flags & ASN1_TFLG_SET_OF) sktag = V_ASN1_SET;
470 else sktag = V_ASN1_SEQUENCE;
473 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, &p, len, sktag, skaclass, opt, ctx);
475 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
477 } else if(ret == -1) return -1;
478 if(!*val) *val = (ASN1_VALUE *)sk_new_null();
480 /* We've got a valid STACK: free up any items present */
481 STACK *sktmp = (STACK *)*val;
483 while(sk_num(sktmp) > 0) {
484 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
485 ASN1_item_ex_free(&vtmp, tt->item);
490 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_MALLOC_FAILURE);
493 /* Read as many items as we can */
497 /* See if EOC found */
498 if(asn1_check_eoc(&p, len)) {
500 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_UNEXPECTED_EOC);
508 if(!ASN1_item_ex_d2i(&skfield, &p, len, tt->item, -1, 0, 0, ctx)) {
509 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
513 if(!sk_push((STACK *)*val, (char *)skfield)) {
514 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_MALLOC_FAILURE);
519 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
522 } else if(flags & ASN1_TFLG_IMPTAG) {
523 /* IMPLICIT tagging */
524 ret = ASN1_item_ex_d2i(val, &p, len, tt->item, tt->tag, aclass, opt, ctx);
526 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
528 } else if(ret == -1) return -1;
530 /* Nothing special */
531 ret = ASN1_item_ex_d2i(val, &p, len, tt->item, -1, 0, opt, ctx);
533 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR);
535 } else if(ret == -1) return -1;
542 ASN1_template_free(val, tt);
547 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long inlen,
549 int tag, int aclass, char opt, ASN1_TLC *ctx)
553 char cst, inf, free_cont = 0;
556 unsigned char *cont = NULL;
559 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
560 return 0; /* Should never happen */
563 if(it->itype == ASN1_ITYPE_MSTRING) {
566 } else utype = it->utype;
568 if(utype == V_ASN1_ANY) {
569 /* If type is ANY need to figure out type from tag */
570 unsigned char oclass;
572 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
576 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_OPTIONAL_ANY);
580 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, &p, inlen, -1, 0, 0, ctx);
582 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
585 if(oclass != V_ASN1_UNIVERSAL) utype = V_ASN1_OTHER;
589 aclass = V_ASN1_UNIVERSAL;
593 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, &p, inlen, tag, aclass, opt, ctx);
595 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
597 } else if(ret == -1) return -1;
598 /* SEQUENCE, SET and "OTHER" are left in encoded form */
599 if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
600 /* SEQUENCE and SET must be constructed */
601 if((utype != V_ASN1_OTHER) && !cst) {
602 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_CONSTRUCTED);
607 /* If indefinite length constructed find the real end */
609 if(!asn1_collect(NULL, &p, plen, inf, -1, -1)) goto err;
612 len = p - cont + plen;
620 /* Should really check the internal tags are correct but
621 * some things may get this wrong. The relevant specs
622 * say that constructed string types should be OCTET STRINGs
623 * internally irrespective of the type. So instead just check
624 * for UNIVERSAL class and ignore the tag.
626 if(!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL)) goto err;
628 /* Append a final null to string */
629 if(!BUF_MEM_grow(&buf, len + 1)) {
630 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
634 cont = (unsigned char *)buf.data;
642 /* We now have content length and type: translate into a structure */
643 if(!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) goto err;
648 if(free_cont && buf.data) OPENSSL_free(buf.data);
652 /* Translate ASN1 content octets into a structure */
654 int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it)
657 ASN1_TYPE *typ = NULL;
659 const ASN1_PRIMITIVE_FUNCS *pf;
661 if(pf && pf->prim_c2i) return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
662 /* If ANY type clear type and set pointer to internal value */
663 if(it->utype == V_ASN1_ANY) {
665 typ = ASN1_TYPE_new();
666 *pval = (ASN1_VALUE *)typ;
667 } else typ = (ASN1_TYPE *)*pval;
668 if(utype != typ->type) ASN1_TYPE_set(typ, utype, NULL);
669 pval = (ASN1_VALUE **)&typ->value.ptr;
673 if(!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) goto err;
678 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_NULL_IS_WRONG_LENGTH);
681 *pval = (ASN1_VALUE *)1;
686 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
690 tbool = (ASN1_BOOLEAN *)pval;
695 case V_ASN1_BIT_STRING:
696 if(!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) goto err;
700 case V_ASN1_NEG_INTEGER:
701 case V_ASN1_ENUMERATED:
702 case V_ASN1_NEG_ENUMERATED:
703 if(!c2i_ASN1_INTEGER((ASN1_INTEGER **)pval, &cont, len)) goto err;
706 case V_ASN1_OCTET_STRING:
707 case V_ASN1_NUMERICSTRING:
708 case V_ASN1_PRINTABLESTRING:
709 case V_ASN1_T61STRING:
710 case V_ASN1_VIDEOTEXSTRING:
711 case V_ASN1_IA5STRING:
713 case V_ASN1_GENERALIZEDTIME:
714 case V_ASN1_GRAPHICSTRING:
715 case V_ASN1_VISIBLESTRING:
716 case V_ASN1_GENERALSTRING:
717 case V_ASN1_UNIVERSALSTRING:
718 case V_ASN1_BMPSTRING:
719 case V_ASN1_UTF8STRING:
722 case V_ASN1_SEQUENCE:
724 /* All based on ASN1_STRING and handled the same */
726 stmp = ASN1_STRING_type_new(utype);
728 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
731 *pval = (ASN1_VALUE *)stmp;
733 stmp = (ASN1_STRING *)*pval;
736 /* If we've already allocated a buffer use it */
738 if(stmp->data) OPENSSL_free(stmp->data);
743 if(!ASN1_STRING_set(stmp, cont, len)) {
744 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
745 ASN1_STRING_free(stmp);
752 /* If ASN1_ANY and NULL type fix up value */
753 if(typ && utype==V_ASN1_NULL) typ->value.ptr = NULL;
757 if(!ret) ASN1_TYPE_free(typ);
761 /* This function collects the asn1 data from a constructred string
762 * type into a buffer. The values of 'in' and 'len' should refer
763 * to the contents of the constructed type and 'inf' should be set
764 * if it is indefinite length. If 'buf' is NULL then we just want
765 * to find the end of the current structure: useful for indefinite
766 * length constructed stuff.
769 static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass)
771 unsigned char *p, *q;
776 /* If no buffer and not indefinite length constructed just pass over the encoded data */
784 if(asn1_check_eoc(&p, len)) {
785 /* EOC is illegal outside indefinite length constructed form */
787 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
793 if(!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, len, tag, aclass, 0, NULL)) {
794 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
797 /* If indefinite length constructed update max length */
799 if(!asn1_collect(buf, &p, plen, ininf, tag, aclass)) return 0;
801 if(!collect_data(buf, &p, plen)) return 0;
806 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
813 static int collect_data(BUF_MEM *buf, unsigned char **p, long plen)
818 if(!BUF_MEM_grow(buf, len + plen)) {
819 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
822 memcpy(buf->data + len, *p, plen);
828 /* Check for ASN1 EOC and swallow it if found */
830 static int asn1_check_eoc(unsigned char **in, long len)
833 if(len < 2) return 0;
842 /* Check an ASN1 tag and length: a bit like ASN1_get_object
843 * but it sets the length for indefinite length constructed
844 * form, we don't know the exact length but we can set an
845 * upper bound to the amount of data available minus the
846 * header length just read.
849 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst,
850 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx)
855 unsigned char *p, *q;
859 if(ctx && ctx->valid) {
862 pclass = ctx->pclass;
866 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
870 ctx->pclass = pclass;
874 /* If definite length, length + header can't exceed total
875 * amount of data available.
877 if(!(i & 1) && ((plen + ctx->hdrlen) > len)) {
878 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
886 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
891 if((exptag != ptag) || (expclass != pclass)) {
892 /* If type is OPTIONAL, not an error, but indicate missing
897 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
900 /* We have a tag and class match, so assume we are going to do something with it */
904 if(i & 1) plen = len - (p - q);
906 if(inf) *inf = i & 1;
908 if(cst) *cst = i & V_ASN1_CONSTRUCTED;
910 if(olen) *olen = plen;
911 if(oclass) *oclass = pclass;
912 if(otag) *otag = ptag;