]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7Sign.c
CryptoPkg/BaseCryptLib: Add NULL pointer checks in DH and P7Verify
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7Sign.c
index b275ab851ed192e1fc08c2f5cf1d19ef0a9cde6a..d3b1a907aad9c5ae8138f0c242f15e56e633da0b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PKCS#7 SignedData Sign Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <openssl/x509.h>\r
 #include <openssl/pkcs7.h>\r
 \r
-\r
 /**\r
   Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message\r
   Syntax Standard, version 1.5". This interface is only intended to be used for\r
@@ -92,12 +91,21 @@ Pkcs7Sign (
     return Status;\r
   }\r
 \r
+  Status = FALSE;\r
+\r
   //\r
   // Register & Initialize necessary digest algorithms and PRNG for PKCS#7 Handling\r
   //\r
-  EVP_add_digest (EVP_md5());\r
-  EVP_add_digest (EVP_sha1());\r
-  EVP_add_digest (EVP_sha256());\r
+  if (EVP_add_digest (EVP_md5 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
+  if (EVP_add_digest (EVP_sha1 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
+  if (EVP_add_digest (EVP_sha256 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
+\r
   RandomSeed (NULL, 0);\r
 \r
   //\r
@@ -105,18 +113,23 @@ Pkcs7Sign (
   //\r
   Key = EVP_PKEY_new ();\r
   if (Key == NULL) {\r
-    Status = FALSE;\r
     goto _Exit;\r
   }\r
-  Key->save_type = EVP_PKEY_RSA;\r
-  Key->type      = EVP_PKEY_type (EVP_PKEY_RSA);\r
-  Key->pkey.rsa  = (RSA *) RsaContext;\r
+  if (EVP_PKEY_assign_RSA (Key, (RSA *) RsaContext) == 0) {\r
+    goto _Exit;\r
+  }\r
 \r
   //\r
   // Convert the data to be signed to BIO format. \r
   //\r
   DataBio = BIO_new (BIO_s_mem ());\r
-  BIO_write (DataBio, InData, (int) InDataSize);\r
+  if (DataBio == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  if (BIO_write (DataBio, InData, (int) InDataSize) <= 0) {\r
+    goto _Exit;\r
+  }\r
 \r
   //\r
   // Create the PKCS#7 signedData structure.\r
@@ -129,7 +142,6 @@ Pkcs7Sign (
             PKCS7_BINARY | PKCS7_NOATTR | PKCS7_DETACHED\r
             );\r
   if (Pkcs7 == NULL) {\r
-    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
@@ -138,18 +150,17 @@ Pkcs7Sign (
   //\r
   P7DataSize = i2d_PKCS7 (Pkcs7, NULL);\r
   if (P7DataSize <= 19) {\r
-    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
   P7Data     = malloc (P7DataSize);\r
   if (P7Data == NULL) {\r
-    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
   Tmp        = P7Data;\r
   P7DataSize = i2d_PKCS7 (Pkcs7, (unsigned char **) &Tmp);\r
+  ASSERT (P7DataSize > 19);\r
 \r
   //\r
   // Strip ContentInfo to content only for signeddata. The data be trimmed off\r
@@ -158,13 +169,12 @@ Pkcs7Sign (
   *SignedDataSize = P7DataSize - 19;\r
   *SignedData     = malloc (*SignedDataSize);\r
   if (*SignedData == NULL) {\r
-    Status = FALSE;\r
     OPENSSL_free (P7Data);\r
     goto _Exit;\r
   }\r
 \r
   CopyMem (*SignedData, P7Data + 19, *SignedDataSize);\r
-  \r
+\r
   OPENSSL_free (P7Data);\r
 \r
   Status = TRUE;\r
@@ -173,13 +183,6 @@ _Exit:
   //\r
   // Release Resources\r
   //\r
-  if (RsaContext != NULL) {\r
-    RsaFree (RsaContext);\r
-    if (Key != NULL) {\r
-      Key->pkey.rsa = NULL;\r
-    }\r
-  }\r
-\r
   if (Key != NULL) {\r
     EVP_PKEY_free (Key);\r
   }\r
@@ -194,4 +197,3 @@ _Exit:
 \r
   return Status;\r
 }\r
-\r