2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include <openssl/buffer.h>
16 #include <openssl/err.h>
17 #include "internal/numbers.h"
18 #include "asn1_locl.h"
21 * Constructed types with a recursive definition (such as can be found in PKCS7)
22 * could eventually exceed the stack given malicious input with excessive
23 * recursion. Therefore we limit the stack depth. This is the maximum number of
24 * recursive invocations of asn1_item_embed_d2i().
26 #define ASN1_MAX_CONSTRUCTED_NEST 30
28 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
29 long len, const ASN1_ITEM *it,
30 int tag, int aclass, char opt, ASN1_TLC *ctx,
33 static int asn1_check_eoc(const unsigned char **in, long len);
34 static int asn1_find_end(const unsigned char **in, long len, char inf);
36 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
37 char inf, int tag, int aclass, int depth);
39 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
41 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
43 const unsigned char **in, long len,
44 int exptag, int expclass, char opt, ASN1_TLC *ctx);
46 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
47 const unsigned char **in, long len,
48 const ASN1_TEMPLATE *tt, char opt,
49 ASN1_TLC *ctx, int depth);
50 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
51 const unsigned char **in, long len,
52 const ASN1_TEMPLATE *tt, char opt,
53 ASN1_TLC *ctx, int depth);
54 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
55 const unsigned char **in, long len,
57 int tag, int aclass, char opt,
59 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
60 int utype, char *free_cont, const ASN1_ITEM *it);
62 /* Table to convert tags to bit values, used for MSTRING type */
63 static const unsigned long tag2bit[32] = {
65 0, 0, 0, B_ASN1_BIT_STRING,
67 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
69 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
71 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
73 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
75 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
77 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
79 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
81 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
84 unsigned long ASN1_tag2bit(int tag)
86 if ((tag < 0) || (tag > 30))
91 /* Macro to initialize and invalidate the cache */
93 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
94 /* Version to avoid compiler warning about 'c' always non-NULL */
95 #define asn1_tlc_clear_nc(c) (c)->valid = 0
98 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
99 * function. 'in' points to a buffer to read the data from, in future we
100 * will have more advanced versions that can input data a piece at a time and
101 * this will simply be a special case.
104 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
105 const unsigned char **in, long len,
109 ASN1_VALUE *ptmpval = NULL;
112 asn1_tlc_clear_nc(&c);
113 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
118 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
120 int tag, int aclass, char opt, ASN1_TLC *ctx)
123 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
125 ASN1_item_ex_free(pval, it);
130 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
131 * tag mismatch return -1 to handle OPTIONAL
134 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
135 long len, const ASN1_ITEM *it,
136 int tag, int aclass, char opt, ASN1_TLC *ctx,
139 const ASN1_TEMPLATE *tt, *errtt = NULL;
140 const ASN1_EXTERN_FUNCS *ef;
141 const ASN1_AUX *aux = it->funcs;
142 ASN1_aux_cb *asn1_cb;
143 const unsigned char *p = NULL, *q;
144 unsigned char oclass;
145 char seq_eoc, seq_nolen, cst, isopt;
153 if (aux && aux->asn1_cb)
154 asn1_cb = aux->asn1_cb;
158 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
159 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
164 case ASN1_ITYPE_PRIMITIVE:
167 * tagging or OPTIONAL is currently illegal on an item template
168 * because the flags can't get passed down. In practice this
169 * isn't a problem: we include the relevant flags from the item
170 * template in the template itself.
172 if ((tag != -1) || opt) {
173 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
174 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
177 return asn1_template_ex_d2i(pval, in, len,
178 it->templates, opt, ctx, depth);
180 return asn1_d2i_ex_primitive(pval, in, len, it,
181 tag, aclass, opt, ctx);
183 case ASN1_ITYPE_MSTRING:
185 /* Just read in tag and class */
186 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
187 &p, len, -1, 0, 1, ctx);
189 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
193 /* Must be UNIVERSAL class */
194 if (oclass != V_ASN1_UNIVERSAL) {
195 /* If OPTIONAL, assume this is OK */
198 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
201 /* Check tag matches bit map */
202 if (!(ASN1_tag2bit(otag) & it->utype)) {
203 /* If OPTIONAL, assume this is OK */
206 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
209 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
211 case ASN1_ITYPE_EXTERN:
212 /* Use new style d2i */
214 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
216 case ASN1_ITYPE_CHOICE:
217 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
220 /* Free up and zero CHOICE value if initialised */
221 i = asn1_get_choice_selector(pval, it);
222 if ((i >= 0) && (i < it->tcount)) {
223 tt = it->templates + i;
224 pchptr = asn1_get_field_ptr(pval, tt);
225 asn1_template_free(pchptr, tt);
226 asn1_set_choice_selector(pval, -1, it);
228 } else if (!ASN1_item_ex_new(pval, it)) {
229 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
232 /* CHOICE type, try each possibility in turn */
234 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
235 pchptr = asn1_get_field_ptr(pval, tt);
237 * We mark field as OPTIONAL so its absence can be recognised.
239 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
240 /* If field not present, try the next one */
243 /* If positive return, read OK, break loop */
247 * Must be an ASN1 parsing error.
248 * Free up any partial choice value
250 asn1_template_free(pchptr, tt);
252 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
256 /* Did we fall off the end without reading anything? */
257 if (i == it->tcount) {
258 /* If OPTIONAL, this is OK */
260 /* Free and zero it */
261 ASN1_item_ex_free(pval, it);
264 ASN1err(ASN1_F_ASN1_ITEM_EMBED_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, NULL))
275 case ASN1_ITYPE_NDEF_SEQUENCE:
276 case ASN1_ITYPE_SEQUENCE:
280 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
282 tag = V_ASN1_SEQUENCE;
283 aclass = V_ASN1_UNIVERSAL;
285 /* Get SEQUENCE length and update len, p */
286 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
287 &p, len, tag, aclass, opt, ctx);
289 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
291 } else if (ret == -1)
293 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
294 len = tmplen - (p - *in);
297 /* If indefinite we don't do a length check */
301 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
305 if (!*pval && !ASN1_item_ex_new(pval, it)) {
306 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
310 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
313 /* Free up and zero any ADB found */
314 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
315 if (tt->flags & ASN1_TFLG_ADB_MASK) {
316 const ASN1_TEMPLATE *seqtt;
317 ASN1_VALUE **pseqval;
318 seqtt = asn1_do_adb(pval, tt, 0);
321 pseqval = asn1_get_field_ptr(pval, seqtt);
322 asn1_template_free(pseqval, seqtt);
326 /* Get each field entry */
327 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
328 const ASN1_TEMPLATE *seqtt;
329 ASN1_VALUE **pseqval;
330 seqtt = asn1_do_adb(pval, tt, 1);
333 pseqval = asn1_get_field_ptr(pval, seqtt);
334 /* Have we ran out of data? */
338 if (asn1_check_eoc(&p, len)) {
340 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
349 * This determines the OPTIONAL flag value. The field cannot be
350 * omitted if it is the last of a SEQUENCE and there is still
351 * data to be read. This isn't strictly necessary but it
352 * increases efficiency in some cases.
354 if (i == (it->tcount - 1))
357 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
359 * attempt to read in field, allowing each to be OPTIONAL
362 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
367 } else if (ret == -1) {
369 * OPTIONAL component absent. Free and zero the field.
371 asn1_template_free(pseqval, seqtt);
378 /* Check for EOC if expecting one */
379 if (seq_eoc && !asn1_check_eoc(&p, len)) {
380 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
383 /* Check all data read */
384 if (!seq_nolen && len) {
385 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
390 * If we get here we've got no more data in the SEQUENCE, however we
391 * may not have read all fields so check all remaining are OPTIONAL
392 * and clear any that are.
394 for (; i < it->tcount; tt++, i++) {
395 const ASN1_TEMPLATE *seqtt;
396 seqtt = asn1_do_adb(pval, tt, 1);
399 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
400 ASN1_VALUE **pseqval;
401 pseqval = asn1_get_field_ptr(pval, seqtt);
402 asn1_template_free(pseqval, seqtt);
405 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
410 if (!asn1_enc_save(pval, *in, p - *in, it))
412 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
421 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
424 ERR_add_error_data(4, "Field=", errtt->field_name,
425 ", Type=", it->sname);
427 ERR_add_error_data(2, "Type=", it->sname);
432 * Templates are handled with two separate functions. One handles any
433 * EXPLICIT tag and the other handles the rest.
436 static int asn1_template_ex_d2i(ASN1_VALUE **val,
437 const unsigned char **in, long inlen,
438 const ASN1_TEMPLATE *tt, char opt,
439 ASN1_TLC *ctx, int depth)
444 const unsigned char *p, *q;
449 aclass = flags & ASN1_TFLG_TAG_CLASS;
453 /* Check if EXPLICIT tag expected */
454 if (flags & ASN1_TFLG_EXPTAG) {
457 * Need to work out amount of data available to the inner content and
458 * where it starts: so read in EXPLICIT header to get the info.
460 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
461 &p, inlen, tt->tag, aclass, opt, ctx);
464 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
466 } else if (ret == -1)
469 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
470 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
473 /* We've found the field so it can't be OPTIONAL now */
474 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
476 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
479 /* We read the field in OK so update length */
482 /* If NDEF we must have an EOC here */
483 if (!asn1_check_eoc(&p, len)) {
484 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
489 * Otherwise we must hit the EXPLICIT tag end or its an error
492 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
493 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
498 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
507 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
508 const unsigned char **in, long len,
509 const ASN1_TEMPLATE *tt, char opt,
510 ASN1_TLC *ctx, int depth)
515 const unsigned char *p, *q;
519 aclass = flags & ASN1_TFLG_TAG_CLASS;
525 * If field is embedded then val needs fixing so it is a pointer to
526 * a pointer to a field.
528 if (tt->flags & ASN1_TFLG_EMBED) {
529 tval = (ASN1_VALUE *)val;
533 if (flags & ASN1_TFLG_SK_MASK) {
534 /* SET OF, SEQUENCE OF */
537 /* First work out expected inner tag value */
538 if (flags & ASN1_TFLG_IMPTAG) {
542 skaclass = V_ASN1_UNIVERSAL;
543 if (flags & ASN1_TFLG_SET_OF)
546 sktag = V_ASN1_SEQUENCE;
549 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
550 &p, len, sktag, skaclass, opt, ctx);
552 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
554 } else if (ret == -1)
557 *val = (ASN1_VALUE *)OPENSSL_sk_new_null();
560 * We've got a valid STACK: free up any items present
562 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
564 while (sk_ASN1_VALUE_num(sktmp) > 0) {
565 vtmp = sk_ASN1_VALUE_pop(sktmp);
566 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
571 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
575 /* Read as many items as we can */
579 /* See if EOC found */
580 if (asn1_check_eoc(&p, len)) {
582 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
583 ASN1_R_UNEXPECTED_EOC);
591 if (!asn1_item_embed_d2i(&skfield, &p, len,
592 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
594 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
595 ERR_R_NESTED_ASN1_ERROR);
596 /* |skfield| may be partially allocated despite failure. */
597 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
601 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
602 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
603 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
608 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
611 } else if (flags & ASN1_TFLG_IMPTAG) {
612 /* IMPLICIT tagging */
613 ret = asn1_item_embed_d2i(val, &p, len,
614 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
617 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
619 } else if (ret == -1)
622 /* Nothing special */
623 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
624 -1, 0, opt, ctx, depth);
626 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
628 } else if (ret == -1)
639 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
640 const unsigned char **in, long inlen,
642 int tag, int aclass, char opt, ASN1_TLC *ctx)
646 char cst, inf, free_cont = 0;
647 const unsigned char *p;
648 BUF_MEM buf = { 0, NULL, 0, 0 };
649 const unsigned char *cont = NULL;
652 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
653 return 0; /* Should never happen */
656 if (it->itype == ASN1_ITYPE_MSTRING) {
662 if (utype == V_ASN1_ANY) {
663 /* If type is ANY need to figure out type from tag */
664 unsigned char oclass;
666 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
670 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
671 ASN1_R_ILLEGAL_OPTIONAL_ANY);
675 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
676 &p, inlen, -1, 0, 0, ctx);
678 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
681 if (oclass != V_ASN1_UNIVERSAL)
682 utype = V_ASN1_OTHER;
686 aclass = V_ASN1_UNIVERSAL;
690 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
691 &p, inlen, tag, aclass, opt, ctx);
693 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
695 } else if (ret == -1)
698 /* SEQUENCE, SET and "OTHER" are left in encoded form */
699 if ((utype == V_ASN1_SEQUENCE)
700 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
702 * Clear context cache for type OTHER because the auto clear when we
703 * have a exact match won't work
705 if (utype == V_ASN1_OTHER) {
708 /* SEQUENCE and SET must be constructed */
710 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
711 ASN1_R_TYPE_NOT_CONSTRUCTED);
716 /* If indefinite length constructed find the real end */
718 if (!asn1_find_end(&p, plen, inf))
722 len = p - cont + plen;
726 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
727 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
728 || utype == V_ASN1_ENUMERATED) {
729 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
733 /* Free any returned 'buf' content */
736 * Should really check the internal tags are correct but some things
737 * may get this wrong. The relevant specs say that constructed string
738 * types should be OCTET STRINGs internally irrespective of the type.
739 * So instead just check for UNIVERSAL class and ignore the tag.
741 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
745 /* Append a final null to string */
746 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
747 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
751 cont = (const unsigned char *)buf.data;
758 /* We now have content length and type: translate into a structure */
759 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
760 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
767 OPENSSL_free(buf.data);
771 /* Translate ASN1 content octets into a structure */
773 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
774 int utype, char *free_cont, const ASN1_ITEM *it)
776 ASN1_VALUE **opval = NULL;
778 ASN1_TYPE *typ = NULL;
780 const ASN1_PRIMITIVE_FUNCS *pf;
784 if (pf && pf->prim_c2i)
785 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
786 /* If ANY type clear type and set pointer to internal value */
787 if (it->utype == V_ASN1_ANY) {
789 typ = ASN1_TYPE_new();
792 *pval = (ASN1_VALUE *)typ;
794 typ = (ASN1_TYPE *)*pval;
796 if (utype != typ->type)
797 ASN1_TYPE_set(typ, utype, NULL);
799 pval = &typ->value.asn1_value;
803 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
809 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
812 *pval = (ASN1_VALUE *)1;
817 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
821 tbool = (ASN1_BOOLEAN *)pval;
826 case V_ASN1_BIT_STRING:
827 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
832 case V_ASN1_ENUMERATED:
833 tint = (ASN1_INTEGER **)pval;
834 if (!c2i_ASN1_INTEGER(tint, &cont, len))
836 /* Fixup type to match the expected form */
837 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
840 case V_ASN1_OCTET_STRING:
841 case V_ASN1_NUMERICSTRING:
842 case V_ASN1_PRINTABLESTRING:
843 case V_ASN1_T61STRING:
844 case V_ASN1_VIDEOTEXSTRING:
845 case V_ASN1_IA5STRING:
847 case V_ASN1_GENERALIZEDTIME:
848 case V_ASN1_GRAPHICSTRING:
849 case V_ASN1_VISIBLESTRING:
850 case V_ASN1_GENERALSTRING:
851 case V_ASN1_UNIVERSALSTRING:
852 case V_ASN1_BMPSTRING:
853 case V_ASN1_UTF8STRING:
856 case V_ASN1_SEQUENCE:
858 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
859 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
862 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
863 ASN1err(ASN1_F_ASN1_EX_C2I,
864 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
867 /* All based on ASN1_STRING and handled the same */
869 stmp = ASN1_STRING_type_new(utype);
871 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
874 *pval = (ASN1_VALUE *)stmp;
876 stmp = (ASN1_STRING *)*pval;
879 /* If we've already allocated a buffer use it */
881 OPENSSL_free(stmp->data);
882 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
886 if (!ASN1_STRING_set(stmp, cont, len)) {
887 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
888 ASN1_STRING_free(stmp);
895 /* If ASN1_ANY and NULL type fix up value */
896 if (typ && (utype == V_ASN1_NULL))
897 typ->value.ptr = NULL;
910 * This function finds the end of an ASN1 structure when passed its maximum
911 * length, whether it is indefinite length and a pointer to the content. This
912 * is more efficient than calling asn1_collect because it does not recurse on
913 * each indefinite length header.
916 static int asn1_find_end(const unsigned char **in, long len, char inf)
918 uint32_t expected_eoc;
920 const unsigned char *p = *in, *q;
921 /* If not indefinite length constructed just add length */
928 * Indefinite length constructed form. Find the end when enough EOCs are
929 * found. If more indefinite length constructed headers are encountered
930 * increment the expected eoc count otherwise just skip to the end of the
934 if (asn1_check_eoc(&p, len)) {
936 if (expected_eoc == 0)
942 /* Just read in a header: only care about the length */
943 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
945 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
949 if (expected_eoc == UINT32_MAX) {
950 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
960 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
968 * This function collects the asn1 data from a constructed string type into
969 * a buffer. The values of 'in' and 'len' should refer to the contents of the
970 * constructed type and 'inf' should be set if it is indefinite length.
973 #ifndef ASN1_MAX_STRING_NEST
975 * This determines how many levels of recursion are permitted in ASN1 string
976 * types. If it is not limited stack overflows can occur. If set to zero no
977 * recursion is allowed at all. Although zero should be adequate examples
978 * exist that require a value of 1. So 5 should be more than enough.
980 # define ASN1_MAX_STRING_NEST 5
983 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
984 char inf, int tag, int aclass, int depth)
986 const unsigned char *p, *q;
992 * If no buffer and not indefinite length constructed just pass over the
1002 if (asn1_check_eoc(&p, len)) {
1004 * EOC is illegal outside indefinite length constructed form
1007 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1014 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1015 len, tag, aclass, 0, NULL)) {
1016 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1020 /* If indefinite length constructed update max length */
1022 if (depth >= ASN1_MAX_STRING_NEST) {
1023 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1026 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1028 } else if (plen && !collect_data(buf, &p, plen))
1033 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1040 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1045 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1046 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1049 memcpy(buf->data + len, *p, plen);
1055 /* Check for ASN1 EOC and swallow it if found */
1057 static int asn1_check_eoc(const unsigned char **in, long len)
1059 const unsigned char *p;
1063 if (!p[0] && !p[1]) {
1071 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1072 * length for indefinite length constructed form, we don't know the exact
1073 * length but we can set an upper bound to the amount of data available minus
1074 * the header length just read.
1077 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1078 char *inf, char *cst,
1079 const unsigned char **in, long len,
1080 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1085 const unsigned char *p, *q;
1089 if (ctx && ctx->valid) {
1092 pclass = ctx->pclass;
1096 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1100 ctx->pclass = pclass;
1102 ctx->hdrlen = p - q;
1105 * If definite length, and no error, length + header can't exceed
1106 * total amount of data available.
1108 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1109 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1110 asn1_tlc_clear(ctx);
1117 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1118 asn1_tlc_clear(ctx);
1122 if ((exptag != ptag) || (expclass != pclass)) {
1124 * If type is OPTIONAL, not an error: indicate missing type.
1128 asn1_tlc_clear(ctx);
1129 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1133 * We have a tag and class match: assume we are going to do something
1136 asn1_tlc_clear(ctx);
1140 plen = len - (p - q);
1146 *cst = i & V_ASN1_CONSTRUCTED;