]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pem/CryptPem.c
CryptoPkg: Add xxxxHashAll APIs to facilitate the digest computation
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pem / CryptPem.c
index e9de39a1058a1983856d97572a3679115583759f..51e648b736a51c9f41d3a24d67a235b937c8f95b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PEM (Privacy Enhanced Mail) Format Handler Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2013, 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
@@ -36,14 +36,14 @@ PasswordCallback (
 {\r
   INTN  KeyLength;\r
 \r
-  ZeroMem ((VOID *)Buf, (UINTN)Size);\r
+  ZeroMem ((VOID *) Buf, (UINTN) Size);\r
   if (Key != NULL) {\r
     //\r
     // Duplicate key phrase directly.\r
     //\r
-    KeyLength = AsciiStrLen ((CHAR8 *)Key);\r
+    KeyLength = (INTN) AsciiStrLen ((CHAR8 *)Key);\r
     KeyLength = (KeyLength > Size ) ? Size : KeyLength;\r
-    CopyMem (Buf, Key, KeyLength);\r
+    CopyMem (Buf, Key, (UINTN) KeyLength);\r
     return KeyLength;\r
   } else {\r
     return 0;\r
@@ -60,8 +60,8 @@ PasswordCallback (
                            RSA private key component. Use RsaFree() function to free the\r
                            resource.\r
 \r
-  If PemData is NULL, then ASSERT().\r
-  If RsaContext is NULL, then ASSERT().\r
+  If PemData is NULL, then return FALSE.\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @retval  TRUE   RSA Private Key was retrieved successfully.\r
   @retval  FALSE  Invalid PEM key data or incorrect password.\r
@@ -80,36 +80,47 @@ RsaGetPrivateKeyFromPem (
   BIO      *PemBio;\r
 \r
   //\r
-  // ASSERT if PemData is NULL or RsaContext is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (PemData    != NULL);\r
-  ASSERT (RsaContext != NULL);\r
-\r
-  Status = FALSE;\r
-  PemBio = NULL;\r
+  if (PemData == NULL || RsaContext == NULL || PemSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   //\r
   // Add possible block-cipher descriptor for PEM data decryption.\r
   // NOTE: Only support most popular ciphers (3DES, AES) for the encrypted PEM.\r
   //\r
-  EVP_add_cipher (EVP_des_ede3_cbc());\r
-  EVP_add_cipher (EVP_aes_128_cbc());\r
-  EVP_add_cipher (EVP_aes_192_cbc());\r
-  EVP_add_cipher (EVP_aes_256_cbc());\r
+  if (EVP_add_cipher (EVP_des_ede3_cbc ()) == 0) {\r
+    return FALSE;\r
+  }\r
+  if (EVP_add_cipher (EVP_aes_128_cbc ()) == 0) {\r
+    return FALSE;\r
+  }\r
+  if (EVP_add_cipher (EVP_aes_192_cbc ()) == 0) {\r
+    return FALSE;\r
+  }\r
+  if (EVP_add_cipher (EVP_aes_256_cbc ()) == 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  Status = FALSE;\r
 \r
   //\r
   // Read encrypted PEM Data.\r
   //\r
   PemBio = BIO_new (BIO_s_mem ());\r
-  BIO_write (PemBio, PemData, (int)PemSize);\r
   if (PemBio == NULL) {\r
     goto _Exit;\r
   }\r
 \r
+  if (BIO_write (PemBio, PemData, (int) PemSize) <= 0) {\r
+    goto _Exit;\r
+  }\r
+\r
   //\r
   // Retrieve RSA Private Key from encrypted PEM data.\r
   //\r
-  *RsaContext = PEM_read_bio_RSAPrivateKey (PemBio, NULL, (pem_password_cb *)&PasswordCallback, (void *)Password);\r
+  *RsaContext = PEM_read_bio_RSAPrivateKey (PemBio, NULL, (pem_password_cb *) &PasswordCallback, (void *) Password);\r
   if (*RsaContext != NULL) {\r
     Status = TRUE;\r
   }\r