Kill evil casts, fix PKCS#7 and add new X509V3 Function.
authorDr. Stephen Henson <steve@openssl.org>
Sun, 9 May 1999 16:39:11 +0000 (16:39 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sun, 9 May 1999 16:39:11 +0000 (16:39 +0000)
15 files changed:
CHANGES
crypto/asn1/a_type.c
crypto/asn1/asn1.h
crypto/asn1/evp_asn1.c
crypto/asn1/x_attrib.c
crypto/pkcs7/pk7_doit.c
crypto/pkcs7/pk7_lib.c
crypto/pkcs7/pkcs7.h
crypto/x509/x509.h
crypto/x509v3/v3_bitst.c
crypto/x509v3/v3_conf.c
crypto/x509v3/v3_enum.c
crypto/x509v3/v3err.c
crypto/x509v3/x509v3.h
util/libeay.num

diff --git a/CHANGES b/CHANGES
index ecfb0c01a3f12331a95d0f58313ef76c0274d09d..61553dba43095e1e0fc36c552a182f227c5feef3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,12 @@
 
  Changes between 0.9.2b and 0.9.3
 
+  *) New function X509V3_EXT_i2d() to create an X509_EXTENSION structure
+     from the internal representation. Various PKCS#7 fixes: remove some
+     evil casts and set the enc_dig_alg field properly based on the signing
+     key type.
+     [Steve Henson]
+
   *) Allow PKCS#12 password to be set from the command line or the
      environment. Let 'ca' get its config file name from the environment
      variables "OPENSSL_CONF" or "SSLEAY_CONF" (for consistency with 'req'
index 72240a0b2572610178c177dfe661f6a6842fed84..3f2ecee5c2f58c8ad87ee2505a3d37aaea75b353 100644 (file)
@@ -293,7 +293,7 @@ int ASN1_TYPE_get(ASN1_TYPE *a)
                return(0);
        }
 
-void ASN1_TYPE_set(ASN1_TYPE *a, int type, char *value)
+void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
        {
        if (a->value.ptr != NULL)
                ASN1_TYPE_component_free(a);
index 5050d572e47dd3810377aab8e840fb77dc99a2b3..8dc61e1e63ba4dcae1718cb6bcc3f42541a5bc63 100644 (file)
@@ -488,7 +488,7 @@ void                ASN1_TYPE_free(ASN1_TYPE *a);
 int            i2d_ASN1_TYPE(ASN1_TYPE *a,unsigned char **pp);
 ASN1_TYPE *    d2i_ASN1_TYPE(ASN1_TYPE **a,unsigned char **pp,long length);
 int ASN1_TYPE_get(ASN1_TYPE *a);
-void ASN1_TYPE_set(ASN1_TYPE *a, int type, char *value);
+void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value);
 
 ASN1_OBJECT *  ASN1_OBJECT_new(void );
 void           ASN1_OBJECT_free(ASN1_OBJECT *a);
index 7528b2d97f220b4508fe674bd1d107663cf451c5..41ced49c1906c399edf119a416ecf79472309d8b 100644 (file)
@@ -67,7 +67,7 @@ int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
 
        if ((os=ASN1_OCTET_STRING_new()) == NULL) return(0);
        if (!ASN1_OCTET_STRING_set(os,data,len)) return(0);
-       ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,(char *)os);
+       ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
        return(1);
        }
 
@@ -124,7 +124,7 @@ int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
          i2d_ASN1_INTEGER(&in,&p);
        M_i2d_ASN1_OCTET_STRING(&os,&p);
 
-       ASN1_TYPE_set(a,V_ASN1_SEQUENCE,(char *)osp);
+       ASN1_TYPE_set(a,V_ASN1_SEQUENCE,osp);
        return(1);
        }
 
index 274c9a920d9956ae299776372684fb264db03ad9..ae88b2741b08dde9f33871da3bb1948ac57d3690 100644 (file)
@@ -117,7 +117,7 @@ X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **a, unsigned char **pp,
        M_ASN1_D2I_Finish(a,X509_ATTRIBUTE_free,ASN1_F_D2I_X509_ATTRIBUTE);
        }
 
-X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, char *value)
+X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
        {
        X509_ATTRIBUTE *ret=NULL;
        ASN1_TYPE *val=NULL;
index 67085e39567dd314843baa1c22408e8db62550ce..1536aa8c1d3b9adf5268375596814d290d3fc2ba 100644 (file)
@@ -62,7 +62,7 @@
 #include <openssl/objects.h>
 #include <openssl/x509.h>
 
-static int add_attribute(STACK **sk, int nid, int atrtype, char *value);
+static int add_attribute(STACK **sk, int nid, int atrtype, void *value);
 static ASN1_TYPE *get_attribute(STACK *sk, int nid);
 
 #if 1
@@ -567,7 +567,7 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
                                sign_time=X509_gmtime_adj(NULL,0);
                                PKCS7_add_signed_attribute(si,
                                        NID_pkcs9_signingTime,
-                                       V_ASN1_UTCTIME,(char *)sign_time);
+                                       V_ASN1_UTCTIME,sign_time);
 
                                /* Add digest */
                                md_tmp=EVP_MD_CTX_type(&ctx_tmp);
@@ -575,7 +575,7 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
                                digest=ASN1_OCTET_STRING_new();
                                ASN1_OCTET_STRING_set(digest,md_data,md_len);
                                PKCS7_add_signed_attribute(si,NID_pkcs9_messageDigest,
-                                       V_ASN1_OCTET_STRING,(char *)digest);
+                                       V_ASN1_OCTET_STRING,digest);
 
                                /* Now sign the mess */
                                EVP_SignInit(&ctx_tmp,md_tmp);
@@ -874,18 +874,18 @@ int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK *sk)
        }
 
 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
-            char *value)
+            void *value)
        {
        return(add_attribute(&(p7si->auth_attr),nid,atrtype,value));
        }
 
 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
-            char *value)
+            void *value)
        {
        return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value));
        }
 
-static int add_attribute(STACK **sk, int nid, int atrtype, char *value)
+static int add_attribute(STACK **sk, int nid, int atrtype, void *value)
        {
        X509_ATTRIBUTE *attr=NULL;
 
index e8c279dc93b82801f04778894d3f1b0a64a0b26d..7bba23e3d4bcb420083f16c8765443f4b5485593 100644 (file)
@@ -309,15 +309,14 @@ int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
                p7i->digest_alg->algorithm=OBJ_nid2obj(NID_sha1);
        else    
                p7i->digest_alg->algorithm=OBJ_nid2obj(EVP_MD_type(dgst));
-       p7i->digest_enc_alg->algorithm=OBJ_nid2obj(EVP_MD_pkey_type(dgst));
 
-#if 1
+       p7i->digest_enc_alg->algorithm=OBJ_nid2obj(EVP_PKEY_type(pkey->type));
+
        if (p7i->digest_enc_alg->parameter != NULL)
                ASN1_TYPE_free(p7i->digest_enc_alg->parameter);
        if ((p7i->digest_enc_alg->parameter=ASN1_TYPE_new()) == NULL)
                goto err;
        p7i->digest_enc_alg->parameter->type=V_ASN1_NULL;
-#endif
 
        return(1);
 err:
index 8541fdc89df867b4672cc3ab95a68f2aab2b09e4..4afde8274b4279932789d66cf08f6918fcf8d5d8 100644 (file)
@@ -340,9 +340,9 @@ int PKCS7_set_cipher(PKCS7 *p7, EVP_CIPHER *cipher);
 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx);
 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK *sk);
 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si,int nid,int type,
-       char *data);
+       void *data);
 int PKCS7_add_attribute (PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
-       char *value);
+       void *value);
 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid);
 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid);
 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, STACK *sk);
index fb9be71079ecf031fa8474ac6058973d2f457513..ac95f8a1393b0265c30592e3e65be79e8e676d79 100644 (file)
@@ -655,7 +655,7 @@ void                X509_ATTRIBUTE_free(X509_ATTRIBUTE *a);
 int            i2d_X509_ATTRIBUTE(X509_ATTRIBUTE *a,unsigned char **pp);
 X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **a,unsigned char **pp,
                        long length);
-X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, char *value);
+X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value);
 
 
 X509_EXTENSION *X509_EXTENSION_new(void );
index 29d8bd44ca79f1bf3cdb25a8041d36f78c74fd7c..641f877c130553b66a3a70eae12ce0335df0561a 100644 (file)
@@ -103,7 +103,7 @@ static STACK *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
             ASN1_BIT_STRING *bits, STACK *ret)
 {
        BIT_STRING_BITNAME *bnam;
-       for(bnam =(BIT_STRING_BITNAME *)method->usr_data; bnam->lname; bnam++) {
+       for(bnam =method->usr_data; bnam->lname; bnam++) {
                if(ASN1_BIT_STRING_get_bit(bits, bnam->bitnum)) 
                        X509V3_add_value(bnam->lname, NULL, &ret);
        }
@@ -123,7 +123,7 @@ static ASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
        }
        for(i = 0; i < sk_num(nval); i++) {
                val = (CONF_VALUE *)sk_value(nval, i);
-               for(bnam = (BIT_STRING_BITNAME *)method->usr_data; bnam->lname;
+               for(bnam = method->usr_data; bnam->lname;
                                                                       bnam++) {
                        if(!strcmp(bnam->sname, val->name) ||
                                !strcmp(bnam->lname, val->name) ) {
index d63db97340557a980b57ac16823d873d5075b54a..0460fbedfc6529f93e160a962d2c368bf6b30b12 100644 (file)
@@ -72,6 +72,8 @@ static X509_EXTENSION *do_ext_conf(LHASH *conf, X509V3_CTX *ctx, int ext_nid, in
 static X509_EXTENSION *v3_generic_extension(const char *ext, char *value, int crit, int type);
 static char *conf_lhash_get_string(void *db, char *section, char *value);
 static STACK *conf_lhash_get_section(void *db, char *section);
+static X509_EXTENSION *do_ext_i2d(X509V3_EXT_METHOD *method, int ext_nid,
+                                                int crit, void *ext_struc);
 /* LHASH *conf:  Config file    */
 /* char *name:  Name    */
 /* char *value:  Value    */
@@ -111,13 +113,10 @@ X509_EXTENSION *X509V3_EXT_conf_nid(LHASH *conf, X509V3_CTX *ctx, int ext_nid,
 static X509_EXTENSION *do_ext_conf(LHASH *conf, X509V3_CTX *ctx, int ext_nid,
             int crit, char *value)
 {
-       X509_EXTENSION *ext = NULL;
        X509V3_EXT_METHOD *method;
+       X509_EXTENSION *ext;
        STACK *nval;
-       char *ext_struc;
-       unsigned char *ext_der, *p;
-       int ext_len;
-       ASN1_OCTET_STRING *ext_oct;
+       void *ext_struc;
        if(ext_nid == NID_undef) {
                X509V3err(X509V3_F_DO_EXT_CONF,X509V3_R_UNKNOWN_EXTENSION_NAME);
                return NULL;
@@ -152,21 +151,50 @@ static X509_EXTENSION *do_ext_conf(LHASH *conf, X509V3_CTX *ctx, int ext_nid,
                return NULL;
        }
 
-       /* We've now got the internal representation: convert to DER */
+       ext  = do_ext_i2d(method, ext_nid, crit, ext_struc);
+       method->ext_free(ext_struc);
+       return ext;
+
+}
+
+static X509_EXTENSION *do_ext_i2d(X509V3_EXT_METHOD *method, int ext_nid,
+                                                int crit, void *ext_struc)
+{
+       unsigned char *ext_der, *p;
+       int ext_len;
+       ASN1_OCTET_STRING *ext_oct;
+       X509_EXTENSION *ext;
+       /* Convert internal representation to DER */
        ext_len = method->i2d(ext_struc, NULL);
-       ext_der = Malloc(ext_len);
+       if(!(ext_der = Malloc(ext_len))) goto merr;
        p = ext_der;
        method->i2d(ext_struc, &p);
-       method->ext_free(ext_struc);
-       ext_oct = ASN1_OCTET_STRING_new();
+       if(!(ext_oct = ASN1_OCTET_STRING_new())) goto merr;
        ext_oct->data = ext_der;
        ext_oct->length = ext_len;
        
        ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
+       if(!ext) goto merr;
        ASN1_OCTET_STRING_free(ext_oct);
 
        return ext;
 
+       merr:
+       X509V3err(X509V3_F_DO_EXT_I2D,ERR_R_MALLOC_FAILURE);
+       return NULL;
+
+}
+
+/* Given an internal structure, nid and critical flag create an extension */
+
+X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
+{
+       X509V3_EXT_METHOD *method;
+       if(!(method = X509V3_EXT_get_nid(ext_nid))) {
+               X509V3err(X509V3_F_X509V3_EXT_I2D,X509V3_R_UNKNOWN_EXTENSION);
+               return NULL;
+       }
+       return do_ext_i2d(method, ext_nid, crit, ext_struc);
 }
 
 /* Check the extension string for critical flag */
index e68e04f401d57d6cc4860a8e6bb68560c17ffbeb..e76229da76782fa26ad4f04090b1759788b061ff 100644 (file)
@@ -95,7 +95,7 @@ char *i2s_ASN1_ENUMERATED_TABLE(X509V3_EXT_METHOD *method,
        ENUMERATED_NAMES *enam;
        long strval;
        strval = ASN1_ENUMERATED_get(e);
-       for(enam =(ENUMERATED_NAMES *)method->usr_data; enam->lname; enam++) {
+       for(enam = method->usr_data; enam->lname; enam++) {
                if(strval == enam->bitnum) return BUF_strdup(enam->lname);
        }
        return i2s_ASN1_ENUMERATED(method, e);
index b20b59c25025bd9ee9c4d8f3ba257e235cf656f4..50efa8d99d8534a76e436b3172655c8a09e9e78f 100644 (file)
@@ -68,6 +68,7 @@ static ERR_STRING_DATA X509V3_str_functs[]=
 {ERR_PACK(0,X509V3_F_COPY_EMAIL,0),    "COPY_EMAIL"},
 {ERR_PACK(0,X509V3_F_COPY_ISSUER,0),   "COPY_ISSUER"},
 {ERR_PACK(0,X509V3_F_DO_EXT_CONF,0),   "DO_EXT_CONF"},
+{ERR_PACK(0,X509V3_F_DO_EXT_I2D,0),    "DO_EXT_I2D"},
 {ERR_PACK(0,X509V3_F_HEX_TO_STRING,0), "hex_to_string"},
 {ERR_PACK(0,X509V3_F_I2S_ASN1_ENUMERATED,0),   "i2s_ASN1_ENUMERATED"},
 {ERR_PACK(0,X509V3_F_I2S_ASN1_INTEGER,0),      "i2s_ASN1_INTEGER"},
@@ -98,6 +99,7 @@ static ERR_STRING_DATA X509V3_str_functs[]=
 {ERR_PACK(0,X509V3_F_X509V3_EXT_ADD,0),        "X509V3_EXT_add"},
 {ERR_PACK(0,X509V3_F_X509V3_EXT_ADD_ALIAS,0),  "X509V3_EXT_add_alias"},
 {ERR_PACK(0,X509V3_F_X509V3_EXT_CONF,0),       "X509V3_EXT_conf"},
+{ERR_PACK(0,X509V3_F_X509V3_EXT_I2D,0),        "X509V3_EXT_i2d"},
 {ERR_PACK(0,X509V3_F_X509V3_GET_VALUE_BOOL,0), "X509V3_get_value_bool"},
 {ERR_PACK(0,X509V3_F_X509V3_PARSE_LIST,0),     "X509V3_parse_list"},
 {0,NULL}
index 80bb36e78e16b6c1ba17d3e91beb78fb5eb2cda2..d294a3cff1b97b46fd114b69ff611d37da1ab7a1 100644 (file)
@@ -104,7 +104,7 @@ X509V3_EXT_V2I v2i;
 X509V3_EXT_I2R i2r;
 X509V3_EXT_R2I r2i;
 
-char *usr_data;        /* Any extension specific data */
+void *usr_data;        /* Any extension specific data */
 };
 
 typedef struct X509V3_CONF_METHOD_st {
@@ -411,6 +411,7 @@ X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid);
 int X509V3_add_standard_extensions(void);
 STACK *X509V3_parse_list(char *line);
 void *X509V3_EXT_d2i(X509_EXTENSION *ext);
+X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc);
 
 char *hex_to_string(unsigned char *buffer, long len);
 unsigned char *string_to_hex(char *str, long *len);
@@ -430,6 +431,7 @@ int X509V3_EXT_print_fp(FILE *out, X509_EXTENSION *ext, int flag, int indent);
 #define X509V3_F_COPY_EMAIL                             122
 #define X509V3_F_COPY_ISSUER                            123
 #define X509V3_F_DO_EXT_CONF                            124
+#define X509V3_F_DO_EXT_I2D                             135
 #define X509V3_F_HEX_TO_STRING                          111
 #define X509V3_F_I2S_ASN1_ENUMERATED                    121
 #define X509V3_F_I2S_ASN1_INTEGER                       120
@@ -460,6 +462,7 @@ int X509V3_EXT_print_fp(FILE *out, X509_EXTENSION *ext, int flag, int indent);
 #define X509V3_F_X509V3_EXT_ADD                                 104
 #define X509V3_F_X509V3_EXT_ADD_ALIAS                   106
 #define X509V3_F_X509V3_EXT_CONF                        107
+#define X509V3_F_X509V3_EXT_I2D                                 136
 #define X509V3_F_X509V3_GET_VALUE_BOOL                  110
 #define X509V3_F_X509V3_PARSE_LIST                      109
 
index 12cebd38bd62705cf5c771bc91d9bb76940bb0be..1a1b21ad6a482b887e53263a461ebd51f3d98ff6 100755 (executable)
@@ -1618,3 +1618,4 @@ sk_X509_EXTENSION_delete                1642
 sk_X509_EXTENSION_shift                 1643
 sk_X509_EXTENSION_push                  1644
 sk_X509_NAME_ENTRY_find                 1645
+X509V3_EXT_i2d                          1646