Tolerate a SEQUENCE in DN components.
[openssl.git] / crypto / asn1 / tasn_dec.c
index e5df769..0eefcfb 100644 (file)
@@ -3,7 +3,7 @@
  * project 2000.
  */
 /* ====================================================================
- * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -66,6 +66,7 @@
 #include <openssl/err.h>
 
 static int asn1_check_eoc(const unsigned char **in, long len);
+static int asn1_find_end(const unsigned char **in, long len, char inf);
 
 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
                                char inf, int tag, int aclass);
@@ -97,7 +98,7 @@ static unsigned long tag2bit[32] = {
 B_ASN1_OCTET_STRING,   0,      0,              B_ASN1_UNKNOWN,/* tags  4- 7 */
 B_ASN1_UNKNOWN,        B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags  8-11 */
 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
-0,     0,      B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING,   /* tags 16-19 */
+B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,       /* tags 20-22 */
 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,                               /* tags 23-24 */ 
 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,  /* tags 25-27 */
@@ -157,7 +158,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
        const ASN1_EXTERN_FUNCS *ef;
        const ASN1_AUX *aux = it->funcs;
        ASN1_aux_cb *asn1_cb;
-       const unsigned char *p, *q;
+       const unsigned char *p = NULL, *q;
        unsigned char *wp=NULL; /* BIG FAT WARNING!  BREAKS CONST WHERE USED */
        unsigned char imphack = 0, oclass;
        char seq_eoc, seq_nolen, cst, isopt;
@@ -282,6 +283,12 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                        {
                        wp = *(unsigned char **)in;
                        imphack = *wp;
+                       if (p == NULL)
+                               {
+                               ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
+                                       ERR_R_NESTED_ASN1_ERROR);
+                               goto err;
+                               }
                        *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
                                                                | it->utype);
                        }
@@ -299,7 +306,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
 
 
                case ASN1_ITYPE_CHOICE:
-               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
+               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
                                goto auxerr;
 
                /* Allocate structure */
@@ -312,7 +319,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                /* CHOICE type, try each possibility in turn */
                pchval = NULL;
                p = *in;
-               for(i = 0, tt=it->templates; i < it->tcount; i++, tt++)
+               for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
                        {
                        pchptr = asn1_get_field_ptr(pval, tt);
                        /* We mark field as OPTIONAL so its absence
@@ -349,7 +356,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
 
                asn1_set_choice_selector(pval, i, it);
                *in = p;
-               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
+               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
                                goto auxerr;
                return 1;
 
@@ -389,14 +396,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                        goto err;
                        }
 
-               if (!*pval && !!ASN1_item_ex_new(pval, it))
+               if (!*pval && !ASN1_item_ex_new(pval, it))
                        {
                        ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
                                ERR_R_NESTED_ASN1_ERROR);
                        goto err;
                        }
 
-               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
+               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
                                goto auxerr;
 
                /* Get each field entry */
@@ -498,7 +505,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                if (!asn1_enc_save(pval, *in, p - *in, it))
                        goto auxerr;
                *in = p;
-               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
+               if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
                                goto auxerr;
                return 1;
 
@@ -578,7 +585,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
                        /* If NDEF we must have an EOC here */
                        if (!asn1_check_eoc(&p, len))
                                {
-                               ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                               ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
                                                ASN1_R_MISSING_EOC);
                                goto err;
                                }
@@ -589,7 +596,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
                         * an error */
                        if (len)
                                {
-                               ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                               ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
                                        ASN1_R_EXPLICIT_LENGTH_MISMATCH);
                                goto err;
                                }
@@ -648,7 +655,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                        &p, len, sktag, skaclass, opt, ctx);
                if (!ret)
                        {
-                       ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
+                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                                ERR_R_NESTED_ASN1_ERROR);
                        return 0;
                        }
@@ -671,7 +678,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                
                if (!*val)
                        {
-                       ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
+                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                                ERR_R_MALLOC_FAILURE);
                        goto err;
                        }
@@ -686,7 +693,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                {
                                if (!sk_eoc)
                                        {
-                                       ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                                        ASN1_R_UNEXPECTED_EOC);
                                        goto err;
                                        }
@@ -699,21 +706,21 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                                ASN1_ITEM_ptr(tt->item),
                                                -1, 0, 0, ctx))
                                {
-                               ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                               ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                        ERR_R_NESTED_ASN1_ERROR);
                                goto err;
                                }
                        len -= p - q;
                        if (!sk_push((STACK *)*val, (char *)skfield))
                                {
-                               ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                               ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                                ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
                        }
                if (sk_eoc)
                        {
-                       ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
+                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
                        goto err;
                        }
                }
@@ -724,7 +731,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                        ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
                if (!ret)
                        {
-                       ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                                ERR_R_NESTED_ASN1_ERROR);
                        goto err;
                        }
@@ -738,7 +745,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                                        -1, 0, opt, ctx);
                if (!ret)
                        {
-                       ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
+                       ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                                        ERR_R_NESTED_ASN1_ERROR);
                        goto err;
                        }
@@ -848,7 +855,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
                /* If indefinite length constructed find the real end */
                if (inf)
                        {
-                       if (!asn1_collect(NULL, &p, plen, inf, -1, -1))
+                       if (!asn1_find_end(&p, plen, inf))
                                 goto err;
                        len = p - cont;
                        }
@@ -923,6 +930,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                if (!*pval)
                        {
                        typ = ASN1_TYPE_new();
+                       if (typ == NULL)
+                               goto err;
                        *pval = (ASN1_VALUE *)typ;
                        }
                else
@@ -943,7 +952,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                case V_ASN1_NULL:
                if (len)
                        {
-                       ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
+                       ASN1err(ASN1_F_ASN1_EX_C2I,
                                                ASN1_R_NULL_IS_WRONG_LENGTH);
                        goto err;
                        }
@@ -953,7 +962,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                case V_ASN1_BOOLEAN:
                if (len != 1)
                        {
-                       ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
+                       ASN1err(ASN1_F_ASN1_EX_C2I,
                                                ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
                        goto err;
                        }
@@ -1005,7 +1014,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                        stmp = ASN1_STRING_type_new(utype);
                        if (!stmp)
                                {
-                               ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
+                               ASN1err(ASN1_F_ASN1_EX_C2I,
                                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
@@ -1029,7 +1038,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                        {
                        if (!ASN1_STRING_set(stmp, cont, len))
                                {
-                               ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
+                               ASN1err(ASN1_F_ASN1_EX_C2I,
                                                        ERR_R_MALLOC_FAILURE);
                                ASN1_STRING_free(stmp); 
                                *pval = NULL;
@@ -1053,12 +1062,66 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
        return ret;
        }
 
+
+/* This function finds the end of an ASN1 structure when passed its maximum
+ * length, whether it is indefinite length and a pointer to the content.
+ * This is more efficient than calling asn1_collect because it does not
+ * recurse on each indefinite length header.
+ */
+
+static int asn1_find_end(const unsigned char **in, long len, char inf)
+       {
+       int expected_eoc;
+       long plen;
+       const unsigned char *p = *in, *q;
+       /* If not indefinite length constructed just add length */
+       if (inf == 0)
+               {
+               *in += len;
+               return 1;
+               }
+       expected_eoc = 1;
+       /* Indefinite length constructed form. Find the end when enough EOCs
+        * are found. If more indefinite length constructed headers
+        * are encountered increment the expected eoc count otherwise just
+        * skip to the end of the data.
+        */
+       while (len > 0)
+               {
+               if(asn1_check_eoc(&p, len))
+                       {
+                       expected_eoc--;
+                       if (expected_eoc == 0)
+                               break;
+                       len -= 2;
+                       continue;
+                       }
+               q = p;
+               /* Just read in a header: only care about the length */
+               if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
+                               -1, 0, 0, NULL))
+                       {
+                       ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
+                       return 0;
+                       }
+               if (inf)
+                       expected_eoc++;
+               else
+                       p += plen;
+               len -= p - q;
+               }
+       if (expected_eoc)
+               {
+               ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
+               return 0;
+               }
+       *in = p;
+       return 1;
+       }
 /* This function collects the asn1 data from a constructred string
  * type into a buffer. The values of 'in' and 'len' should refer
  * to the contents of the constructed type and 'inf' should be set
- * if it is indefinite length. If 'buf' is NULL then we just want
- * to find the end of the current structure: useful for indefinite
- * length constructed stuff.
+ * if it is indefinite length.
  */
 
 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
@@ -1104,10 +1167,15 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
                /* If indefinite length constructed update max length */
                if (cst)
                        {
+#ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
                        if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
                                return 0;
+#else
+                       ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
+                       return 0;
+#endif
                        }
-               else if (!collect_data(buf, &p, plen))
+               else if (plen && !collect_data(buf, &p, plen))
                        return 0;
                len -= p - q;
                }