New config module for string tables. This can be used to add new
[openssl.git] / crypto / asn1 / asn1_gen.c
index 62c2c154d1484d0bcf20037ed12a8fa0ce805738..7c2c337a1a6670807521c8ddc35a113e4f3b0726 100644 (file)
@@ -1,5 +1,5 @@
 /* asn1_gen.c */
 /* asn1_gen.c */
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2002.
  */
 /* ====================================================================
  * project 2002.
  */
 /* ====================================================================
@@ -67,7 +67,8 @@
 #define ASN1_GEN_FLAG_BITWRAP  (ASN1_GEN_FLAG|4)
 #define ASN1_GEN_FLAG_OCTWRAP  (ASN1_GEN_FLAG|5)
 #define ASN1_GEN_FLAG_SEQWRAP  (ASN1_GEN_FLAG|6)
 #define ASN1_GEN_FLAG_BITWRAP  (ASN1_GEN_FLAG|4)
 #define ASN1_GEN_FLAG_OCTWRAP  (ASN1_GEN_FLAG|5)
 #define ASN1_GEN_FLAG_SEQWRAP  (ASN1_GEN_FLAG|6)
-#define ASN1_GEN_FLAG_FORMAT   (ASN1_GEN_FLAG|7)
+#define ASN1_GEN_FLAG_SETWRAP  (ASN1_GEN_FLAG|7)
+#define ASN1_GEN_FLAG_FORMAT   (ASN1_GEN_FLAG|8)
 
 #define ASN1_GEN_STR(str,val)  {str, sizeof(str) - 1, val}
 
 
 #define ASN1_GEN_STR(str,val)  {str, sizeof(str) - 1, val}
 
@@ -87,7 +88,7 @@
 
 struct tag_name_st
        {
 
 struct tag_name_st
        {
-       char *strnam;
+       const char *strnam;
        int len;
        int tag;
        };
        int len;
        int tag;
        };
@@ -140,10 +141,12 @@ ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
        int i, len;
 
        unsigned char *orig_der = NULL, *new_der = NULL;
        int i, len;
 
        unsigned char *orig_der = NULL, *new_der = NULL;
-       unsigned char *cpy_start, *p;
+       const unsigned char *cpy_start;
+       unsigned char *p;
+       const unsigned char *cp;
        int cpy_len;
        long hdr_len;
        int cpy_len;
        long hdr_len;
-       int hdr_constructed, hdr_tag, hdr_class;
+       int hdr_constructed = 0, hdr_tag, hdr_class;
        int r;
 
        asn1_tags.imp_tag = -1;
        int r;
 
        asn1_tags.imp_tag = -1;
@@ -224,6 +227,8 @@ ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
        /* Allocate buffer for new encoding */
 
        new_der = OPENSSL_malloc(len);
        /* Allocate buffer for new encoding */
 
        new_der = OPENSSL_malloc(len);
+       if (!new_der)
+               goto err;
 
        /* Generate tagged encoding */
 
 
        /* Generate tagged encoding */
 
@@ -242,16 +247,22 @@ ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
        /* If IMPLICIT, output tag */
 
        if (asn1_tags.imp_tag != -1)
        /* If IMPLICIT, output tag */
 
        if (asn1_tags.imp_tag != -1)
+               {
+               if (asn1_tags.imp_class == V_ASN1_UNIVERSAL 
+                   && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
+                    || asn1_tags.imp_tag == V_ASN1_SET) )
+                       hdr_constructed = V_ASN1_CONSTRUCTED;
                ASN1_put_object(&p, hdr_constructed, hdr_len,
                                        asn1_tags.imp_tag, asn1_tags.imp_class);
                ASN1_put_object(&p, hdr_constructed, hdr_len,
                                        asn1_tags.imp_tag, asn1_tags.imp_class);
+               }
 
        /* Copy across original encoding */
        memcpy(p, cpy_start, cpy_len);
 
 
        /* Copy across original encoding */
        memcpy(p, cpy_start, cpy_len);
 
-       p = new_der;
+       cp = new_der;
 
        /* Obtain new ASN1_TYPE structure */
 
        /* Obtain new ASN1_TYPE structure */
-       ret = d2i_ASN1_TYPE(NULL, &p, len);
+       ret = d2i_ASN1_TYPE(NULL, &cp, len);
 
        err:
        if (orig_der)
 
        err:
        if (orig_der)
@@ -268,7 +279,7 @@ static int asn1_cb(const char *elem, int len, void *bitstr)
        tag_exp_arg *arg = bitstr;
        int i;
        int utype;
        tag_exp_arg *arg = bitstr;
        int i;
        int utype;
-       int vlen;
+       int vlen = 0;
        const char *p, *vstart = NULL;
 
        int tmp_tag, tmp_class;
        const char *p, *vstart = NULL;
 
        int tmp_tag, tmp_class;
@@ -335,6 +346,11 @@ static int asn1_cb(const char *elem, int len, void *bitstr)
                        return -1;
                break;
 
                        return -1;
                break;
 
+               case ASN1_GEN_FLAG_SETWRAP:
+               if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
+                       return -1;
+               break;
+
                case ASN1_GEN_FLAG_BITWRAP:
                if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
                        return -1;
                case ASN1_GEN_FLAG_BITWRAP:
                if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
                        return -1;
@@ -431,13 +447,15 @@ static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
 
 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
        {
 
 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
        {
-       ASN1_TYPE *ret = NULL, *typ = NULL;
+       ASN1_TYPE *ret = NULL;
        STACK_OF(ASN1_TYPE) *sk = NULL;
        STACK_OF(CONF_VALUE) *sect = NULL;
        STACK_OF(ASN1_TYPE) *sk = NULL;
        STACK_OF(CONF_VALUE) *sect = NULL;
-       unsigned char *der = NULL, *p;
+       unsigned char *der = NULL;
        int derlen;
        int derlen;
-       int i, is_set;
+       int i;
        sk = sk_ASN1_TYPE_new_null();
        sk = sk_ASN1_TYPE_new_null();
+       if (!sk)
+               goto bad;
        if (section)
                {
                if (!cnf)
        if (section)
                {
                if (!cnf)
@@ -447,26 +465,23 @@ static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
                        goto bad;
                for (i = 0; i < sk_CONF_VALUE_num(sect); i++)
                        {
                        goto bad;
                for (i = 0; i < sk_CONF_VALUE_num(sect); i++)
                        {
-                       typ = ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
+                       ASN1_TYPE *typ = ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
                        if (!typ)
                                goto bad;
                        if (!typ)
                                goto bad;
-                       sk_ASN1_TYPE_push(sk, typ);
-                       typ = NULL;
+                       if (!sk_ASN1_TYPE_push(sk, typ))
+                               goto bad;
                        }
                }
 
        /* Now we has a STACK of the components, convert to the correct form */
 
        if (utype == V_ASN1_SET)
                        }
                }
 
        /* Now we has a STACK of the components, convert to the correct form */
 
        if (utype == V_ASN1_SET)
-               is_set = 1;
+               derlen = i2d_ASN1_SET_ANY(sk, &der);
        else
        else
-               is_set = 0;
+               derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
 
 
-
-       derlen = i2d_ASN1_SET((STACK *)sk, NULL, i2d_ASN1_TYPE, utype, V_ASN1_UNIVERSAL, is_set);
-       der = OPENSSL_malloc(derlen);
-       p = der;
-       i2d_ASN1_SET((STACK *)sk, &p, i2d_ASN1_TYPE, utype, V_ASN1_UNIVERSAL, is_set);
+       if (derlen < 0)
+               goto bad;
 
        if (!(ret = ASN1_TYPE_new()))
                goto bad;
 
        if (!(ret = ASN1_TYPE_new()))
                goto bad;
@@ -488,8 +503,6 @@ static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
 
        if (sk)
                sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
 
        if (sk)
                sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
-       if (typ)
-               ASN1_TYPE_free(typ);
        if (sect)
                X509V3_section_free(cnf, sect);
 
        if (sect)
                X509V3_section_free(cnf, sect);
 
@@ -502,13 +515,13 @@ static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_cons
        /* Can only have IMPLICIT if permitted */
        if ((arg->imp_tag != -1) && !imp_ok)
                {
        /* Can only have IMPLICIT if permitted */
        if ((arg->imp_tag != -1) && !imp_ok)
                {
-               ASN1err(ASN1_F_APPEND_TAG, ASN1_R_ILLEGAL_IMPLICIT_TAG);
+               ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
                return 0;
                }
 
        if (arg->exp_count == ASN1_FLAG_EXP_MAX)
                {
                return 0;
                }
 
        if (arg->exp_count == ASN1_FLAG_EXP_MAX)
                {
-               ASN1err(ASN1_F_APPEND_TAG, ASN1_R_DEPTH_EXCEEDED);
+               ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
                return 0;
                }
 
                return 0;
                }
 
@@ -538,8 +551,8 @@ static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_cons
 
 static int asn1_str2tag(const char *tagstr, int len)
        {
 
 static int asn1_str2tag(const char *tagstr, int len)
        {
-       int i;
-       static struct tag_name_st *tntmp, tnst [] = {
+       unsigned int i;
+       static const struct tag_name_st *tntmp, tnst [] = {
                ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
                ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
                ASN1_GEN_STR("NULL", V_ASN1_NULL),
                ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
                ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
                ASN1_GEN_STR("NULL", V_ASN1_NULL),
@@ -572,6 +585,10 @@ static int asn1_str2tag(const char *tagstr, int len)
                ASN1_GEN_STR("T61", V_ASN1_T61STRING),
                ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
                ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
                ASN1_GEN_STR("T61", V_ASN1_T61STRING),
                ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
                ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
+               ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
+               ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
+               ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
+               ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
 
                /* Special cases */
                ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
 
                /* Special cases */
                ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
@@ -588,6 +605,8 @@ static int asn1_str2tag(const char *tagstr, int len)
                ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
                /* SEQUENCE wrapper */
                ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
                ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
                /* SEQUENCE wrapper */
                ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
+               /* SET wrapper */
+               ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
                /* BIT STRING wrapper */
                ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
                ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
                /* BIT STRING wrapper */
                ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
                ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
@@ -644,6 +663,8 @@ static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
                        ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
                        goto bad_form;
                        }
                        ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
                        goto bad_form;
                        }
+               vtmp.name = NULL;
+               vtmp.section = NULL;
                vtmp.value = (char *)str;
                if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
                        {
                vtmp.value = (char *)str;
                if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
                        {
@@ -712,6 +733,8 @@ static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
                case V_ASN1_UTF8STRING:
                case V_ASN1_VISIBLESTRING:
                case V_ASN1_UNIVERSALSTRING:
                case V_ASN1_UTF8STRING:
                case V_ASN1_VISIBLESTRING:
                case V_ASN1_UNIVERSALSTRING:
+               case V_ASN1_GENERALSTRING:
+               case V_ASN1_NUMERICSTRING:
 
                if (format == ASN1_GEN_FORMAT_ASCII)
                        format = MBSTRING_ASC;
 
                if (format == ASN1_GEN_FORMAT_ASCII)
                        format = MBSTRING_ASC;
@@ -829,3 +852,27 @@ static int bitstr_cb(const char *elem, int len, void *bitstr)
        return 1;
        }
 
        return 1;
        }
 
+static int mask_cb(const char *elem, int len, void *arg)
+       {
+       unsigned long *pmask = arg, tmpmask;
+       int tag;
+       if (len == 3 && !strncmp(elem, "DIR", 3))
+               {
+               *pmask |= B_ASN1_DIRECTORYSTRING;
+               return 1;
+               }
+       tag = asn1_str2tag(elem, len);
+       if (!tag || (tag & ASN1_GEN_FLAG))
+               return 0;
+       tmpmask = ASN1_tag2bit(tag);
+       if (!tmpmask)
+               return 0;
+       *pmask |= tmpmask;
+       return 1;
+       }
+
+int ASN1_str2mask(const char *str, unsigned long *pmask)
+       {
+       *pmask = 0;
+       return CONF_parse_list(str, '|', 1, mask_cb, pmask);
+       }