3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * 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(const unsigned char **in, long len);
69 static int asn1_find_end(const unsigned char **in, long len, char inf);
71 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth);
74 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
76 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
78 const unsigned char **in, long len,
79 int exptag, int expclass, char opt, ASN1_TLC *ctx);
81 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
82 const unsigned char **in, long len,
83 const ASN1_TEMPLATE *tt, char opt,
85 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
86 const unsigned char **in, long len,
87 const ASN1_TEMPLATE *tt, char opt,
89 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
90 const unsigned char **in, long len,
92 int tag, int aclass, char opt,
95 /* Table to convert tags to bit values, used for MSTRING type */
96 static const unsigned long tag2bit[32] = {
98 0, 0, 0, B_ASN1_BIT_STRING,
100 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
102 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
104 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
106 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
108 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
110 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
112 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
114 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
117 unsigned long ASN1_tag2bit(int tag)
119 if ((tag < 0) || (tag > 30))
124 /* Macro to initialize and invalidate the cache */
126 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
127 /* Version to avoid compiler warning about 'c' always non-NULL */
128 #define asn1_tlc_clear_nc(c) (c)->valid = 0
131 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
132 * function. 'in' points to a buffer to read the data from, in future we
133 * will have more advanced versions that can input data a piece at a time and
134 * this will simply be a special case.
137 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
138 const unsigned char **in, long len,
142 ASN1_VALUE *ptmpval = NULL;
145 asn1_tlc_clear_nc(&c);
146 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
151 int ASN1_template_d2i(ASN1_VALUE **pval,
152 const unsigned char **in, long len,
153 const ASN1_TEMPLATE *tt)
156 asn1_tlc_clear_nc(&c);
157 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
161 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
162 * tag mismatch return -1 to handle OPTIONAL
165 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
167 int tag, int aclass, char opt, ASN1_TLC *ctx)
169 const ASN1_TEMPLATE *tt, *errtt = NULL;
170 const ASN1_COMPAT_FUNCS *cf;
171 const ASN1_EXTERN_FUNCS *ef;
172 const ASN1_AUX *aux = it->funcs;
173 ASN1_aux_cb *asn1_cb;
174 const unsigned char *p = NULL, *q;
175 unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
176 unsigned char imphack = 0, oclass;
177 char seq_eoc, seq_nolen, cst, isopt;
182 ASN1_VALUE **pchptr, *ptmpval;
185 if (aux && aux->asn1_cb)
186 asn1_cb = aux->asn1_cb;
191 case ASN1_ITYPE_PRIMITIVE:
194 * tagging or OPTIONAL is currently illegal on an item template
195 * because the flags can't get passed down. In practice this
196 * isn't a problem: we include the relevant flags from the item
197 * template in the template itself.
199 if ((tag != -1) || opt) {
200 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
201 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
204 return asn1_template_ex_d2i(pval, in, len,
205 it->templates, opt, ctx);
207 return asn1_d2i_ex_primitive(pval, in, len, it,
208 tag, aclass, opt, ctx);
211 case ASN1_ITYPE_MSTRING:
213 /* Just read in tag and class */
214 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
215 &p, len, -1, 0, 1, ctx);
217 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
221 /* Must be UNIVERSAL class */
222 if (oclass != V_ASN1_UNIVERSAL) {
223 /* If OPTIONAL, assume this is OK */
226 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
229 /* Check tag matches bit map */
230 if (!(ASN1_tag2bit(otag) & it->utype)) {
231 /* If OPTIONAL, assume this is OK */
234 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
237 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
239 case ASN1_ITYPE_EXTERN:
240 /* Use new style d2i */
242 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
244 case ASN1_ITYPE_COMPAT:
245 /* we must resort to old style evil hackery */
248 /* If OPTIONAL see if it is there */
257 * Don't care about anything other than presence of expected tag
260 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
261 &p, len, exptag, aclass, 1, ctx);
263 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
271 * This is the old style evil hack IMPLICIT handling: since the
272 * underlying code is expecting a tag and class other than the one
273 * present we change the buffer temporarily then change it back
274 * afterwards. This doesn't and never did work for tags > 30. Yes
275 * this is *horrible* but it is only needed for old style d2i which
276 * will hopefully not be around for much longer. FIXME: should copy
277 * the buffer then modify it so the input buffer can be const: we
278 * should *always* copy because the old style d2i might modify the
283 wp = *(unsigned char **)in;
286 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
289 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
293 ptmpval = cf->asn1_d2i(pval, in, len);
301 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
304 case ASN1_ITYPE_CHOICE:
305 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
308 /* Free up and zero CHOICE value if initialised */
309 i = asn1_get_choice_selector(pval, it);
310 if ((i >= 0) && (i < it->tcount)) {
311 tt = it->templates + i;
312 pchptr = asn1_get_field_ptr(pval, tt);
313 ASN1_template_free(pchptr, tt);
314 asn1_set_choice_selector(pval, -1, it);
316 } else if (!ASN1_item_ex_new(pval, it)) {
317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
320 /* CHOICE type, try each possibility in turn */
322 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
323 pchptr = asn1_get_field_ptr(pval, tt);
325 * We mark field as OPTIONAL so its absence can be recognised.
327 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
328 /* If field not present, try the next one */
331 /* If positive return, read OK, break loop */
334 /* Otherwise must be an ASN1 parsing error */
336 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
340 /* Did we fall off the end without reading anything? */
341 if (i == it->tcount) {
342 /* If OPTIONAL, this is OK */
344 /* Free and zero it */
345 ASN1_item_ex_free(pval, it);
348 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
352 asn1_set_choice_selector(pval, i, it);
353 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
358 case ASN1_ITYPE_NDEF_SEQUENCE:
359 case ASN1_ITYPE_SEQUENCE:
363 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
365 tag = V_ASN1_SEQUENCE;
366 aclass = V_ASN1_UNIVERSAL;
368 /* Get SEQUENCE length and update len, p */
369 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
370 &p, len, tag, aclass, opt, ctx);
372 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
374 } else if (ret == -1)
376 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
377 len = tmplen - (p - *in);
380 /* If indefinite we don't do a length check */
384 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388 if (!*pval && !ASN1_item_ex_new(pval, it)) {
389 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
393 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
396 /* Free up and zero any ADB found */
397 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
398 if (tt->flags & ASN1_TFLG_ADB_MASK) {
399 const ASN1_TEMPLATE *seqtt;
400 ASN1_VALUE **pseqval;
401 seqtt = asn1_do_adb(pval, tt, 1);
402 pseqval = asn1_get_field_ptr(pval, seqtt);
403 ASN1_template_free(pseqval, seqtt);
407 /* Get each field entry */
408 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
409 const ASN1_TEMPLATE *seqtt;
410 ASN1_VALUE **pseqval;
411 seqtt = asn1_do_adb(pval, tt, 1);
414 pseqval = asn1_get_field_ptr(pval, seqtt);
415 /* Have we ran out of data? */
419 if (asn1_check_eoc(&p, len)) {
421 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
430 * This determines the OPTIONAL flag value. The field cannot be
431 * omitted if it is the last of a SEQUENCE and there is still
432 * data to be read. This isn't strictly necessary but it
433 * increases efficiency in some cases.
435 if (i == (it->tcount - 1))
438 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
440 * attempt to read in field, allowing each to be OPTIONAL
443 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
447 } else if (ret == -1) {
449 * OPTIONAL component absent. Free and zero the field.
451 ASN1_template_free(pseqval, seqtt);
458 /* Check for EOC if expecting one */
459 if (seq_eoc && !asn1_check_eoc(&p, len)) {
460 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
463 /* Check all data read */
464 if (!seq_nolen && len) {
465 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
470 * If we get here we've got no more data in the SEQUENCE, however we
471 * may not have read all fields so check all remaining are OPTIONAL
472 * and clear any that are.
474 for (; i < it->tcount; tt++, i++) {
475 const ASN1_TEMPLATE *seqtt;
476 seqtt = asn1_do_adb(pval, tt, 1);
479 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
480 ASN1_VALUE **pseqval;
481 pseqval = asn1_get_field_ptr(pval, seqtt);
482 ASN1_template_free(pseqval, seqtt);
485 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
490 if (!asn1_enc_save(pval, *in, p - *in, it))
492 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
501 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
503 ASN1_item_ex_free(pval, it);
505 ERR_add_error_data(4, "Field=", errtt->field_name,
506 ", Type=", it->sname);
508 ERR_add_error_data(2, "Type=", it->sname);
513 * Templates are handled with two separate functions. One handles any
514 * EXPLICIT tag and the other handles the rest.
517 static int asn1_template_ex_d2i(ASN1_VALUE **val,
518 const unsigned char **in, long inlen,
519 const ASN1_TEMPLATE *tt, char opt,
525 const unsigned char *p, *q;
530 aclass = flags & ASN1_TFLG_TAG_CLASS;
534 /* Check if EXPLICIT tag expected */
535 if (flags & ASN1_TFLG_EXPTAG) {
538 * Need to work out amount of data available to the inner content and
539 * where it starts: so read in EXPLICIT header to get the info.
541 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
542 &p, inlen, tt->tag, aclass, opt, ctx);
545 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
547 } else if (ret == -1)
550 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
551 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
554 /* We've found the field so it can't be OPTIONAL now */
555 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
557 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
560 /* We read the field in OK so update length */
563 /* If NDEF we must have an EOC here */
564 if (!asn1_check_eoc(&p, len)) {
565 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
570 * Otherwise we must hit the EXPLICIT tag end or its an error
573 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
574 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
579 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
585 ASN1_template_free(val, tt);
589 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
590 const unsigned char **in, long len,
591 const ASN1_TEMPLATE *tt, char opt,
596 const unsigned char *p, *q;
600 aclass = flags & ASN1_TFLG_TAG_CLASS;
605 if (flags & ASN1_TFLG_SK_MASK) {
606 /* SET OF, SEQUENCE OF */
609 /* First work out expected inner tag value */
610 if (flags & ASN1_TFLG_IMPTAG) {
614 skaclass = V_ASN1_UNIVERSAL;
615 if (flags & ASN1_TFLG_SET_OF)
618 sktag = V_ASN1_SEQUENCE;
621 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
622 &p, len, sktag, skaclass, opt, ctx);
624 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
626 } else if (ret == -1)
629 *val = (ASN1_VALUE *)sk_new_null();
632 * We've got a valid STACK: free up any items present
634 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
636 while (sk_ASN1_VALUE_num(sktmp) > 0) {
637 vtmp = sk_ASN1_VALUE_pop(sktmp);
638 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
643 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
647 /* Read as many items as we can */
651 /* See if EOC found */
652 if (asn1_check_eoc(&p, len)) {
654 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
655 ASN1_R_UNEXPECTED_EOC);
663 if (!ASN1_item_ex_d2i(&skfield, &p, len,
664 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
665 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
666 ERR_R_NESTED_ASN1_ERROR);
670 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
671 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
676 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
679 } else if (flags & ASN1_TFLG_IMPTAG) {
680 /* IMPLICIT tagging */
681 ret = ASN1_item_ex_d2i(val, &p, len,
682 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
685 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
687 } else if (ret == -1)
690 /* Nothing special */
691 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
694 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
696 } else if (ret == -1)
704 ASN1_template_free(val, tt);
708 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
709 const unsigned char **in, long inlen,
711 int tag, int aclass, char opt, ASN1_TLC *ctx)
715 char cst, inf, free_cont = 0;
716 const unsigned char *p;
718 const unsigned char *cont = NULL;
721 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
722 return 0; /* Should never happen */
725 if (it->itype == ASN1_ITYPE_MSTRING) {
731 if (utype == V_ASN1_ANY) {
732 /* If type is ANY need to figure out type from tag */
733 unsigned char oclass;
735 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
739 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
740 ASN1_R_ILLEGAL_OPTIONAL_ANY);
744 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
745 &p, inlen, -1, 0, 0, ctx);
747 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
750 if (oclass != V_ASN1_UNIVERSAL)
751 utype = V_ASN1_OTHER;
755 aclass = V_ASN1_UNIVERSAL;
759 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
760 &p, inlen, tag, aclass, opt, ctx);
762 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
764 } else if (ret == -1)
767 /* SEQUENCE, SET and "OTHER" are left in encoded form */
768 if ((utype == V_ASN1_SEQUENCE)
769 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
771 * Clear context cache for type OTHER because the auto clear when we
772 * have a exact match wont work
774 if (utype == V_ASN1_OTHER) {
777 /* SEQUENCE and SET must be constructed */
779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
780 ASN1_R_TYPE_NOT_CONSTRUCTED);
785 /* If indefinite length constructed find the real end */
787 if (!asn1_find_end(&p, plen, inf))
791 len = p - cont + plen;
796 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
797 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
798 || utype == V_ASN1_ENUMERATED) {
799 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
806 * Should really check the internal tags are correct but some things
807 * may get this wrong. The relevant specs say that constructed string
808 * types should be OCTET STRINGs internally irrespective of the type.
809 * So instead just check for UNIVERSAL class and ignore the tag.
811 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
816 /* Append a final null to string */
817 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
818 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
822 cont = (const unsigned char *)buf.data;
830 /* We now have content length and type: translate into a structure */
831 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
837 if (free_cont && buf.data)
838 OPENSSL_free(buf.data);
842 /* Translate ASN1 content octets into a structure */
844 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
845 int utype, char *free_cont, const ASN1_ITEM *it)
847 ASN1_VALUE **opval = NULL;
849 ASN1_TYPE *typ = NULL;
851 const ASN1_PRIMITIVE_FUNCS *pf;
855 if (pf && pf->prim_c2i)
856 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
857 /* If ANY type clear type and set pointer to internal value */
858 if (it->utype == V_ASN1_ANY) {
860 typ = ASN1_TYPE_new();
863 *pval = (ASN1_VALUE *)typ;
865 typ = (ASN1_TYPE *)*pval;
867 if (utype != typ->type)
868 ASN1_TYPE_set(typ, utype, NULL);
870 pval = &typ->value.asn1_value;
874 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
880 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
883 *pval = (ASN1_VALUE *)1;
888 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
892 tbool = (ASN1_BOOLEAN *)pval;
897 case V_ASN1_BIT_STRING:
898 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
903 case V_ASN1_NEG_INTEGER:
904 case V_ASN1_ENUMERATED:
905 case V_ASN1_NEG_ENUMERATED:
906 tint = (ASN1_INTEGER **)pval;
907 if (!c2i_ASN1_INTEGER(tint, &cont, len))
909 /* Fixup type to match the expected form */
910 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
913 case V_ASN1_OCTET_STRING:
914 case V_ASN1_NUMERICSTRING:
915 case V_ASN1_PRINTABLESTRING:
916 case V_ASN1_T61STRING:
917 case V_ASN1_VIDEOTEXSTRING:
918 case V_ASN1_IA5STRING:
920 case V_ASN1_GENERALIZEDTIME:
921 case V_ASN1_GRAPHICSTRING:
922 case V_ASN1_VISIBLESTRING:
923 case V_ASN1_GENERALSTRING:
924 case V_ASN1_UNIVERSALSTRING:
925 case V_ASN1_BMPSTRING:
926 case V_ASN1_UTF8STRING:
929 case V_ASN1_SEQUENCE:
931 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
932 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
935 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
936 ASN1err(ASN1_F_ASN1_EX_C2I,
937 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
940 /* All based on ASN1_STRING and handled the same */
942 stmp = ASN1_STRING_type_new(utype);
944 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
947 *pval = (ASN1_VALUE *)stmp;
949 stmp = (ASN1_STRING *)*pval;
952 /* If we've already allocated a buffer use it */
955 OPENSSL_free(stmp->data);
956 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
960 if (!ASN1_STRING_set(stmp, cont, len)) {
961 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
962 ASN1_STRING_free(stmp);
969 /* If ASN1_ANY and NULL type fix up value */
970 if (typ && (utype == V_ASN1_NULL))
971 typ->value.ptr = NULL;
984 * This function finds the end of an ASN1 structure when passed its maximum
985 * length, whether it is indefinite length and a pointer to the content. This
986 * is more efficient than calling asn1_collect because it does not recurse on
987 * each indefinite length header.
990 static int asn1_find_end(const unsigned char **in, long len, char inf)
994 const unsigned char *p = *in, *q;
995 /* If not indefinite length constructed just add length */
1002 * Indefinite length constructed form. Find the end when enough EOCs are
1003 * found. If more indefinite length constructed headers are encountered
1004 * increment the expected eoc count otherwise just skip to the end of the
1008 if (asn1_check_eoc(&p, len)) {
1010 if (expected_eoc == 0)
1016 /* Just read in a header: only care about the length */
1017 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1019 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1029 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1037 * This function collects the asn1 data from a constructred string type into
1038 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1039 * constructed type and 'inf' should be set if it is indefinite length.
1042 #ifndef ASN1_MAX_STRING_NEST
1044 * This determines how many levels of recursion are permitted in ASN1 string
1045 * types. If it is not limited stack overflows can occur. If set to zero no
1046 * recursion is allowed at all. Although zero should be adequate examples
1047 * exist that require a value of 1. So 5 should be more than enough.
1049 # define ASN1_MAX_STRING_NEST 5
1052 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1053 char inf, int tag, int aclass, int depth)
1055 const unsigned char *p, *q;
1061 * If no buffer and not indefinite length constructed just pass over the
1071 if (asn1_check_eoc(&p, len)) {
1073 * EOC is illegal outside indefinite length constructed form
1076 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1083 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1084 len, tag, aclass, 0, NULL)) {
1085 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1089 /* If indefinite length constructed update max length */
1091 if (depth >= ASN1_MAX_STRING_NEST) {
1092 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1095 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1097 } else if (plen && !collect_data(buf, &p, plen))
1102 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1109 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1114 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1115 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1118 memcpy(buf->data + len, *p, plen);
1124 /* Check for ASN1 EOC and swallow it if found */
1126 static int asn1_check_eoc(const unsigned char **in, long len)
1128 const unsigned char *p;
1132 if (!p[0] && !p[1]) {
1140 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1141 * length for indefinite length constructed form, we don't know the exact
1142 * length but we can set an upper bound to the amount of data available minus
1143 * the header length just read.
1146 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1147 char *inf, char *cst,
1148 const unsigned char **in, long len,
1149 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1154 const unsigned char *p, *q;
1158 if (ctx && ctx->valid) {
1161 pclass = ctx->pclass;
1165 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1169 ctx->pclass = pclass;
1171 ctx->hdrlen = p - q;
1174 * If definite length, and no error, length + header can't exceed
1175 * total amount of data available.
1177 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1178 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1179 asn1_tlc_clear(ctx);
1186 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1187 asn1_tlc_clear(ctx);
1191 if ((exptag != ptag) || (expclass != pclass)) {
1193 * If type is OPTIONAL, not an error: indicate missing type.
1197 asn1_tlc_clear(ctx);
1198 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1202 * We have a tag and class match: assume we are going to do something
1205 asn1_tlc_clear(ctx);
1209 plen = len - (p - q);
1215 *cst = i & V_ASN1_CONSTRUCTED;