More S/MIME tidy. Place some common attribute operations in utility
authorDr. Stephen Henson <steve@openssl.org>
Thu, 18 May 2006 17:20:23 +0000 (17:20 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Thu, 18 May 2006 17:20:23 +0000 (17:20 +0000)
functions.

crypto/pkcs7/pk7_attr.c
crypto/pkcs7/pk7_doit.c
crypto/pkcs7/pk7_smime.c
crypto/pkcs7/pkcs7.h
crypto/pkcs7/pkcs7err.c

index 735c8800e1028f1ab83a59f65e0e9cd2e6d3f169..5ad6670a4e11c5c283d5f2a070db01a56e41e18c 100644 (file)
@@ -139,3 +139,42 @@ int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
        sk_X509_ALGOR_push (sk, alg);
        return 1;
 }
+
+int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
+       {
+       if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
+               return 0;
+       if (!coid)
+               coid = OBJ_nid2obj(NID_pkcs7_data);
+       return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
+                               V_ASN1_OBJECT, coid);
+       }
+
+int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
+       {
+       if (!t && !(t=X509_gmtime_adj(NULL,0)))
+               {
+               PKCS7err(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME,
+                               ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
+       return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
+                                               V_ASN1_UTCTIME, t);
+       }
+
+int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
+                               const unsigned char *md, int mdlen)
+       {
+       ASN1_OCTET_STRING *os;
+       os = ASN1_OCTET_STRING_new();
+       if (!os)
+               return 0;
+       if (!ASN1_STRING_set(os, md, mdlen)
+               || PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
+                                               V_ASN1_OCTET_STRING, os))
+               {
+               ASN1_OCTET_STRING_free(os);
+               return 0;
+               }
+       return 1;
+       }
index 60cffb0782c053245254c20efa1ec750c89759f6..0b441fe071da420000384264e0c8efe2d6df66b0 100644 (file)
@@ -648,13 +648,43 @@ static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
        return NULL;
        }
 
+static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
+       {
+       unsigned char md_data[EVP_MAX_MD_SIZE];
+       unsigned int md_len;
+
+       /* Add signing time if not already present */
+       if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime))
+               {
+               if (!PKCS7_add0_attrib_signing_time(si, NULL))
+                       {
+                       PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB,
+                                       ERR_R_MALLOC_FAILURE);
+                       return 0;
+                       }
+               }
+
+       /* Add digest */
+       EVP_DigestFinal_ex(mctx, md_data,&md_len);
+       if (!PKCS7_add1_attrib_digest(si, md_data, md_len))
+               {
+               PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
+
+       /* Now sign the attributes */
+       if (!PKCS7_SIGNER_INFO_sign(si))
+                       return 0;
+
+       return 1;
+       }
+       
+                               
 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
        {
        int ret=0;
        int i,j;
        BIO *btmp;
-       BUF_MEM *buf_mem=NULL;
-       BUF_MEM *buf=NULL;
        PKCS7_SIGNER_INFO *si;
        EVP_MD_CTX *mdc,ctx_tmp;
        STACK_OF(X509_ATTRIBUTE) *sk;
@@ -710,17 +740,13 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
 
        if (si_sk != NULL)
                {
-               if ((buf=BUF_MEM_new()) == NULL)
-                       {
-                       PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB);
-                       goto err;
-                       }
                for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++)
                        {
                        si=sk_PKCS7_SIGNER_INFO_value(si_sk,i);
-                       if (si->pkey == NULL) continue;
+                       if (si->pkey == NULL)
+                               continue;
 
-                       j=OBJ_obj2nid(si->digest_alg->algorithm);
+                       j = OBJ_obj2nid(si->digest_alg->algorithm);
 
                        btmp=bio;
 
@@ -732,79 +758,33 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
                        /* We now have the EVP_MD_CTX, lets do the
                         * signing. */
                        EVP_MD_CTX_copy_ex(&ctx_tmp,mdc);
-                       if (!BUF_MEM_grow_clean(buf,EVP_PKEY_size(si->pkey)))
-                               {
-                               PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB);
-                               goto err;
-                               }
 
                        sk=si->auth_attr;
 
                        /* If there are attributes, we add the digest
                         * attribute and only sign the attributes */
-                       if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0))
+                       if (sk_X509_ATTRIBUTE_num(sk) > 0)
                                {
-                               unsigned char md_data[EVP_MAX_MD_SIZE], *abuf=NULL;
-                               unsigned int md_len, alen;
-                               ASN1_OCTET_STRING *digest;
-                               ASN1_UTCTIME *sign_time;
-                               const EVP_MD *md_tmp;
-
-                               /* Add signing time if not already present */
-                               if (!PKCS7_get_signed_attribute(si,
-                                                       NID_pkcs9_signingTime))
-                                       {
-                                       if (!(sign_time=X509_gmtime_adj(NULL,0)))
-                                               {
-                                               PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
-                                                       ERR_R_MALLOC_FAILURE);
-                                               goto err;
-                                               }
-                                       PKCS7_add_signed_attribute(si,
-                                               NID_pkcs9_signingTime,
-                                               V_ASN1_UTCTIME,sign_time);
-                                       }
-
-                               /* Add digest */
-                               md_tmp=EVP_MD_CTX_md(&ctx_tmp);
-                               EVP_DigestFinal_ex(&ctx_tmp,md_data,&md_len);
-                               if (!(digest=M_ASN1_OCTET_STRING_new()))
-                                       {
-                                       PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
-                                               ERR_R_MALLOC_FAILURE);
+                               if (!do_pkcs7_signed_attrib(si, &ctx_tmp))
                                        goto err;
-                                       }
-                               if (!M_ASN1_OCTET_STRING_set(digest,md_data,
-                                                               md_len))
+                               }
+                       else
+                               {
+                               unsigned char *abuf = NULL;
+                               unsigned int abuflen;
+                               abuflen = EVP_PKEY_size(si->pkey);
+                               abuf = OPENSSL_malloc(abuflen);
+                               if (!abuf)
+                                       goto err;
+
+                               if (!EVP_SignFinal(&ctx_tmp, abuf, &abuflen,
+                                                       si->pkey))
                                        {
                                        PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
-                                               ERR_R_MALLOC_FAILURE);
+                                                       ERR_R_EVP_LIB);
                                        goto err;
                                        }
-                               PKCS7_add_signed_attribute(si,
-                                       NID_pkcs9_messageDigest,
-                                       V_ASN1_OCTET_STRING,digest);
-
-                               /* Now sign the attributes */
-                               EVP_SignInit_ex(&ctx_tmp,md_tmp,NULL);
-                               alen = ASN1_item_i2d((ASN1_VALUE *)sk,&abuf,
-                                                       ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
-                               if(!abuf) goto err;
-                               EVP_SignUpdate(&ctx_tmp,abuf,alen);
-                               OPENSSL_free(abuf);
-                               }
-
-                       if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data,
-                               (unsigned int *)&buf->length,si->pkey))
-                               {
-                               PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_EVP_LIB);
-                               goto err;
-                               }
-                       if (!ASN1_STRING_set(si->enc_digest,
-                               (unsigned char *)buf->data,buf->length))
-                               {
-                               PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_ASN1_LIB);
-                               goto err;
+                               ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
                                }
                        }
                }
@@ -821,32 +801,75 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
 
        if (!PKCS7_is_detached(p7))
                {
+               char *cont;
+               long contlen;
                btmp=BIO_find_type(bio,BIO_TYPE_MEM);
                if (btmp == NULL)
                        {
                        PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
                        goto err;
                        }
-               BIO_get_mem_ptr(btmp,&buf_mem);
+               contlen = BIO_get_mem_data(btmp, &cont);
                /* Mark the BIO read only then we can use its copy of the data
                 * instead of making an extra copy.
                 */
                BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
                BIO_set_mem_eof_return(btmp, 0);
-               os->data = (unsigned char *)buf_mem->data;
-               os->length = buf_mem->length;
-#if 0
-               M_ASN1_OCTET_STRING_set(os,
-                       (unsigned char *)buf_mem->data,buf_mem->length);
-#endif
+               ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
                }
        ret=1;
 err:
        EVP_MD_CTX_cleanup(&ctx_tmp);
-       if (buf != NULL) BUF_MEM_free(buf);
        return(ret);
        }
 
+int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
+       {
+       EVP_MD_CTX mctx;
+       unsigned char *abuf = NULL;
+       int alen;
+       unsigned int siglen;
+       const EVP_MD *md = NULL;
+
+       md = EVP_get_digestbyobj(si->digest_alg->algorithm);
+       if (md == NULL)
+               return 0;
+
+       EVP_MD_CTX_init(&mctx);
+       if (!EVP_SignInit_ex(&mctx,md,NULL))
+               goto err;
+       alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr,&abuf,
+                               ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
+       if(!abuf)
+               goto err;
+       if (!EVP_SignUpdate(&mctx,abuf,alen))
+               goto err;
+       OPENSSL_free(abuf);
+       abuf = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
+       if(!abuf)
+               goto err;
+       if (!EVP_SignFinal(&mctx, abuf, &siglen, si->pkey))
+               goto err;
+
+       EVP_MD_CTX_cleanup(&mctx);
+
+       ASN1_STRING_set0(si->enc_digest, abuf, siglen);
+       abuf = NULL;
+
+       return 1;
+
+       err:
+       if (abuf)
+               OPENSSL_free(abuf);
+       EVP_MD_CTX_cleanup(&mctx);
+       return 0;
+
+       }
+       
+
+       
+       
+
 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
             PKCS7 *p7, PKCS7_SIGNER_INFO *si)
        {
index 10acec9cde3c51bf58639a7e0c7ddfce156a4c00..e09fb38dc5f1884dd90c87ed31199f46112e5c5e 100644 (file)
@@ -155,14 +155,14 @@ PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
        STACK_OF(X509_ALGOR) *smcap = NULL;
        if(!X509_check_private_key(signcert, pkey))
                {
-               PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,
+               PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
                        PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
                 return NULL;
                }
 
        if (!(si = PKCS7_add_signature(p7,signcert,pkey, md)))
                {
-               PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,
+               PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
                                PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
                return NULL;
                }
@@ -179,15 +179,14 @@ PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
 
        if(!(flags & PKCS7_NOATTR))
                {
-               if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
-                               V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data)))
+               if (!PKCS7_add_attrib_content_type(si, NULL))
                        goto err;
                /* Add SMIMECapabilities */
                if(!(flags & PKCS7_NOSMIMECAP))
                        {
                        if(!(smcap = sk_X509_ALGOR_new_null()))
                                {
-                               PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,
+                               PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
index 313fdaf147ca4939babd0bb93d4a01d918f4cad1..7d31b689a728a9e0586f1ad88290234a380cf905 100644 (file)
@@ -320,6 +320,7 @@ int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other);
 int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data);
 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
        const EVP_MD *dgst);
+int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si);
 int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i);
 int PKCS7_add_certificate(PKCS7 *p7, X509 *x509);
 int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509);
@@ -381,6 +382,11 @@ int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
 STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si);
 int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg);
 
+int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid);
+int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t);
+int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
+                               const unsigned char *md, int mdlen);
+
 int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags);
 PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont);
 int SMIME_crlf_copy(BIO *in, BIO *out, int flags);
@@ -397,6 +403,8 @@ void ERR_load_PKCS7_strings(void);
 /* Function codes. */
 #define PKCS7_F_B64_READ_PKCS7                          120
 #define PKCS7_F_B64_WRITE_PKCS7                                 121
+#define PKCS7_F_DO_PKCS7_SIGNED_ATTRIB                  136
+#define PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME          135
 #define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP               118
 #define PKCS7_F_PKCS7_ADD_CERTIFICATE                   100
 #define PKCS7_F_PKCS7_ADD_CRL                           101
@@ -425,6 +433,7 @@ void ERR_load_PKCS7_strings(void);
 #define PKCS7_F_PKCS7_SIGN                              116
 #define PKCS7_F_PKCS7_SIGNATUREVERIFY                   113
 #define PKCS7_F_PKCS7_SIGNER_INFO_SET                   129
+#define PKCS7_F_PKCS7_SIGN_ADD_SIGNER                   137
 #define PKCS7_F_PKCS7_SIMPLE_SMIMECAP                   119
 #define PKCS7_F_PKCS7_VERIFY                            117
 #define PKCS7_F_SMIME_READ_PKCS7                        122
index f8064e86ad57d1b646df765a7b1a214335de51ee..9b2bec9650475c35ce96b1ba53eae38e7988623a 100644 (file)
@@ -72,6 +72,8 @@ static ERR_STRING_DATA PKCS7_str_functs[]=
        {
 {ERR_FUNC(PKCS7_F_B64_READ_PKCS7),     "B64_READ_PKCS7"},
 {ERR_FUNC(PKCS7_F_B64_WRITE_PKCS7),    "B64_WRITE_PKCS7"},
+{ERR_FUNC(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB),     "DO_PKCS7_SIGNED_ATTRIB"},
+{ERR_FUNC(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME),     "PKCS7_add0_attrib_signing_time"},
 {ERR_FUNC(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP),  "PKCS7_add_attrib_smimecap"},
 {ERR_FUNC(PKCS7_F_PKCS7_ADD_CERTIFICATE),      "PKCS7_add_certificate"},
 {ERR_FUNC(PKCS7_F_PKCS7_ADD_CRL),      "PKCS7_add_crl"},
@@ -100,6 +102,7 @@ static ERR_STRING_DATA PKCS7_str_functs[]=
 {ERR_FUNC(PKCS7_F_PKCS7_SIGN), "PKCS7_sign"},
 {ERR_FUNC(PKCS7_F_PKCS7_SIGNATUREVERIFY),      "PKCS7_signatureVerify"},
 {ERR_FUNC(PKCS7_F_PKCS7_SIGNER_INFO_SET),      "PKCS7_SIGNER_INFO_set"},
+{ERR_FUNC(PKCS7_F_PKCS7_SIGN_ADD_SIGNER),      "PKCS7_sign_add_signer"},
 {ERR_FUNC(PKCS7_F_PKCS7_SIMPLE_SMIMECAP),      "PKCS7_simple_smimecap"},
 {ERR_FUNC(PKCS7_F_PKCS7_VERIFY),       "PKCS7_verify"},
 {ERR_FUNC(PKCS7_F_SMIME_READ_PKCS7),   "SMIME_read_PKCS7"},