Update from stable branch.
[openssl.git] / crypto / cms / cms_smime.c
index 6f44c84a5f75b5bc2480b46d3f944cb3acdf627c..4a799eb89765c6c5cba7f48a347a86a720104d1f 100644 (file)
@@ -68,7 +68,10 @@ static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
        if (out == NULL)
                tmpout = BIO_new(BIO_s_null());
        else if (flags & CMS_TEXT)
+               {
                tmpout = BIO_new(BIO_s_mem());
+               BIO_set_mem_eof_return(tmpout, 0);
+               }
        else
                tmpout = out;
 
@@ -89,11 +92,13 @@ static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
                                if (!BIO_get_cipher_status(in))
                                        goto err;
                                }
+                       if (i < 0)
+                               goto err;
                        break;
                        }
                                
-               if (tmpout)
-                       BIO_write(tmpout, buf, i);
+               if (tmpout && (BIO_write(tmpout, buf, i) != i))
+                       goto err;
        }
 
        if(flags & CMS_TEXT)
@@ -125,6 +130,23 @@ static int check_content(CMS_ContentInfo *cms)
        return 1;
        }
 
+static void do_free_upto(BIO *f, BIO *upto)
+       {
+       if (upto)
+               {
+               BIO *tbio;
+               do 
+                       {
+                       tbio = BIO_pop(f);
+                       BIO_free(f);
+                       f = tbio;
+                       }
+               while (f != upto);
+               }
+       else
+               BIO_free_all(f);
+       }
+
 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
        {
        BIO *cont;
@@ -177,7 +199,7 @@ int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
        r = cms_copy_content(out, cont, flags);
        if (r)
                r = cms_DigestedData_do_final(cms, cont, 1);
-       BIO_free_all(cont);
+       do_free_upto(cont, dcont);
        return r;
        }
 
@@ -223,7 +245,7 @@ int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
        if (!cont)
                return 0;
        r = cms_copy_content(out, cont, flags);
-       BIO_free_all(cont);
+       do_free_upto(cont, dcont);
        return r;
        }
 
@@ -270,7 +292,7 @@ static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
                                                CMS_R_STORE_INIT_ERROR);
                goto err;
                }
-       X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
+       X509_STORE_CTX_set_default(&ctx, "smime_sign");
        if (crls)
                X509_STORE_CTX_set0_crls(&ctx, crls);
 
@@ -397,7 +419,7 @@ int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
                for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
                        {
                        si = sk_CMS_SignerInfo_value(sinfos, i);
-                       if (!CMS_SignerInfo_verify_content(si, cmsbio))
+                       if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0)
                                {
                                CMSerr(CMS_F_CMS_VERIFY,
                                        CMS_R_CONTENT_VERIFY_ERROR);
@@ -411,8 +433,9 @@ int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
        err:
        
        if (dcont && (tmpin == dcont))
-               BIO_pop(cmsbio);
-       BIO_free_all(cmsbio);
+               do_free_upto(cmsbio, dcont);
+       else
+               BIO_free_all(cmsbio);
 
        if (cms_certs)
                sk_X509_pop_free(cms_certs, X509_free);
@@ -439,23 +462,23 @@ CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
        {
        CMS_ContentInfo *cms;
        int i;
+
        cms = CMS_ContentInfo_new();
-       if (!cms)
+       if (!cms || !CMS_SignedData_init(cms))
                goto merr;
+
        if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
                {
                CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
                goto err;
                }
+
        for (i = 0; i < sk_X509_num(certs); i++)
                {
                X509 *x = sk_X509_value(certs, i);
                if (!CMS_add1_cert(cms, x))
                        goto merr;
                }
-       /* If no signer or certs initialize signedData */
-       if (!pkey && !i && !CMS_SignedData_init(cms))
-               goto merr;
 
        if(!(flags & CMS_DETACHED))
                CMS_set_detached(cms, 0);
@@ -678,7 +701,7 @@ int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
        if (!cont)
                return 0;
        r = cms_copy_content(out, cont, flags);
-       BIO_free_all(cont);
+       do_free_upto(cont, dcont);
        return r;
        }
 
@@ -706,7 +729,7 @@ int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
        ret = 1;
 
        err:
-       BIO_free_all(cmsbio);
+       do_free_upto(cmsbio, dcont);
 
        return ret;
 
@@ -733,7 +756,7 @@ int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
        if (!cont)
                return 0;
        r = cms_copy_content(out, cont, flags);
-       BIO_free_all(cont);
+       do_free_upto(cont, dcont);
        return r;
        }