Fix (most) WIN32 warnings and errors.
[openssl.git] / crypto / pkcs7 / pk7_smime.c
index 333a8aa38d46d9cd9bdcd4b08d8e91c19ec72e2b..1f4a0a17952fa7cc03994bbc5d8f0f515ffe183a 100644 (file)
@@ -3,7 +3,7 @@
  * project.
  */
 /* ====================================================================
- * Copyright (c) 1999-2003 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1999-2004 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
@@ -88,6 +88,7 @@ PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
 
        if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) {
                PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
+               PKCS7_free(p7);
                return NULL;
        }
 
@@ -105,6 +106,7 @@ PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
                {
                if(!(smcap = sk_X509_ALGOR_new_null())) {
                        PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE);
+                       PKCS7_free(p7);
                        return NULL;
                }
 #ifndef OPENSSL_NO_DES
@@ -130,6 +132,7 @@ PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
 
        if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
                PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE);
+               PKCS7_free(p7);
                return NULL;
        }
 
@@ -139,10 +142,12 @@ PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
 
         if (!PKCS7_dataFinal(p7,p7bio)) {
                PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN);
+               PKCS7_free(p7);
+               BIO_free_all(p7bio);
                return NULL;
        }
 
-        BIO_free_all(p7bio);
+       BIO_free_all(p7bio);
        return p7;
 }
 
@@ -157,7 +162,7 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
        char buf[4096];
        int i, j=0, k, ret = 0;
        BIO *p7bio;
-       BIO *tmpout;
+       BIO *tmpin, *tmpout;
 
        if(!p7) {
                PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER);
@@ -217,6 +222,8 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
                        sk_X509_free(signers);
                        return 0;
                }
+               if (!(flags & PKCS7_NOCRL))
+                       X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
                i = X509_verify_cert(&cert_ctx);
                if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx);
                X509_STORE_CTX_cleanup(&cert_ctx);
@@ -230,7 +237,30 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
                /* Check for revocation status here */
        }
 
-       p7bio=PKCS7_dataInit(p7,indata);
+       /* Performance optimization: if the content is a memory BIO then
+        * store its contents in a temporary read only memory BIO. This
+        * avoids potentially large numbers of slow copies of data which will
+        * occur when reading from a read write memory BIO when signatures
+        * are calculated.
+        */
+
+       if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM))
+               {
+               char *ptr;
+               long len;
+               len = BIO_get_mem_data(indata, &ptr);
+               tmpin = BIO_new_mem_buf(ptr, len);
+               if (tmpin == NULL)
+                       {
+                       PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE);
+                       return 0;
+                       }
+               }
+       else
+               tmpin = indata;
+               
+
+       p7bio=PKCS7_dataInit(p7,tmpin);
 
        if(flags & PKCS7_TEXT) {
                if(!(tmpout = BIO_new(BIO_s_mem()))) {
@@ -272,9 +302,13 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
        ret = 1;
 
        err:
-
-       if(indata) BIO_pop(p7bio);
+       
+       if (tmpin == indata)
+               {
+               if (indata) BIO_pop(p7bio);
+               }
        BIO_free_all(p7bio);
+
        sk_X509_free(signers);
 
        return ret;
@@ -298,10 +332,6 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags)
                PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_WRONG_CONTENT_TYPE);
                return NULL;
        }
-       if(!(signers = sk_X509_new_null())) {
-               PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE);
-               return NULL;
-       }
 
        /* Collect all the signers together */
 
@@ -312,6 +342,11 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags)
                return 0;
        }
 
+       if(!(signers = sk_X509_new_null())) {
+               PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE);
+               return NULL;
+       }
+
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++)
        {
            si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
@@ -406,7 +441,7 @@ int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
                return 0;
        }
 
-       if(!X509_check_private_key(cert, pkey)) {
+       if(cert && !X509_check_private_key(cert, pkey)) {
                PKCS7err(PKCS7_F_PKCS7_DECRYPT,
                                PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
                return 0;