]> git.proxmox.com Git - mirror_edk2.git/commitdiff
The openssl API RSA_public_decrypt() and RSA_private_encrypt() are deprecated, use...
authorsfu5 <sfu5@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 23 Apr 2013 01:52:17 +0000 (01:52 +0000)
committersfu5 <sfu5@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 23 Apr 2013 01:52:17 +0000 (01:52 +0000)
Signed-off-by: Long Qin < qin.long@intel.com >
Reviewed-by: Ye Ting <ting.ye@intel.com>
Reviewed-by: Dong Guo <guo.dong@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14309 6f19259b-4bc3-4df7-8a09-765794883524

CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c

index cbe3c50fedbce1f604745554e6207259eeff6d29..3e4309899c94b49fc1dba85b8fe6c2b971d861ee 100644 (file)
@@ -7,7 +7,7 @@
   3) RsaSetKey\r
   4) RsaPkcs1Verify\r
 \r
   3) RsaSetKey\r
   4) RsaPkcs1Verify\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 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
 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
@@ -21,8 +21,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "InternalCryptLib.h"\r
 \r
 #include <openssl/rsa.h>\r
 #include "InternalCryptLib.h"\r
 \r
 #include <openssl/rsa.h>\r
-#include <openssl/err.h>\r
-\r
+#include <openssl/objects.h>\r
 \r
 /**\r
   Allocates and initializes one RSA context for subsequent use.\r
 \r
 /**\r
   Allocates and initializes one RSA context for subsequent use.\r
@@ -289,8 +288,8 @@ RsaPkcs1Verify (
   IN  UINTN        SigSize\r
   )\r
 {\r
   IN  UINTN        SigSize\r
   )\r
 {\r
-  INTN     Length;\r
-  UINT8    *DecryptedSigature;\r
+  INT32    DigestType;\r
+  UINT8    *SigBuf;\r
 \r
   //\r
   // Check input parameters.\r
 \r
   //\r
   // Check input parameters.\r
@@ -302,65 +301,35 @@ RsaPkcs1Verify (
   if (SigSize > INT_MAX || SigSize == 0) {\r
     return FALSE;\r
   }\r
   if (SigSize > INT_MAX || SigSize == 0) {\r
     return FALSE;\r
   }\r
-  \r
-  //\r
-  // Check for unsupported hash size:\r
-  //    Only MD5, SHA-1 or SHA-256 digest size is supported\r
-  //\r
-  if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE) {\r
-    return FALSE;\r
-  }\r
-\r
-  //\r
-  // Prepare buffer to store decrypted signature.\r
-  //\r
-  DecryptedSigature = (UINT8 *) malloc (SigSize);\r
-  if (DecryptedSigature == NULL) {\r
-    return FALSE;\r
-  }\r
 \r
   //\r
 \r
   //\r
-  // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key\r
+  // Determine the message digest algorithm according to digest size.\r
+  //   Only MD5, SHA-1 or SHA-256 algorithm is supported. \r
   //\r
   //\r
-  Length = RSA_public_decrypt (\r
-             (UINT32) SigSize,\r
-             Signature,\r
-             DecryptedSigature,\r
-             RsaContext,\r
-             RSA_PKCS1_PADDING\r
-             );\r
+  switch (HashSize) {\r
+  case MD5_DIGEST_SIZE:\r
+    DigestType = NID_md5;\r
+    break;\r
+    \r
+  case SHA1_DIGEST_SIZE:\r
+    DigestType = NID_sha1;\r
+    break;\r
+    \r
+  case SHA256_DIGEST_SIZE:\r
+    DigestType = NID_sha256;\r
+    break;\r
 \r
 \r
-  //\r
-  // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)\r
-  // NOTE: Length should be the addition of HashSize and some DER value.\r
-  //       Ignore more strict length checking here.\r
-  //\r
-  if (Length < (INTN) HashSize) {\r
-    free (DecryptedSigature);\r
+  default:\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
-  //\r
-  // Validate the MessageHash and Decoded Signature\r
-  // NOTE: The decoded Signature should be the DER encoding of the DigestInfo value\r
-  //       DigestInfo ::= SEQUENCE {\r
-  //           digestAlgorithm AlgorithmIdentifier\r
-  //           digest OCTET STRING\r
-  //       }\r
-  //       Then Memory Comparing should skip the DER value of the underlying SEQUENCE\r
-  //       type and AlgorithmIdentifier.\r
-  //\r
-  if (CompareMem (MessageHash, DecryptedSigature + Length - HashSize, HashSize) == 0) {\r
-    //\r
-    // Valid RSA PKCS#1 Signature\r
-    //\r
-    free (DecryptedSigature);\r
-    return TRUE;\r
-  } else {\r
-    //\r
-    // Failed to verification\r
-    //\r
-    free (DecryptedSigature);\r
-    return FALSE;\r
-  }\r
+  SigBuf = (UINT8 *) Signature;\r
+  return (BOOLEAN) RSA_verify (\r
+                     DigestType,\r
+                     MessageHash,\r
+                     (UINT32) HashSize,\r
+                     SigBuf,\r
+                     (UINT32) SigSize,\r
+                     (RSA *) RsaContext\r
+                     );\r
 }\r
 }\r
index b4faafa0c350e7db61442efc85d6e58c9d7d616b..5c21d121f57fb44f097717c0b8b2315e4246fa80 100644 (file)
@@ -7,7 +7,7 @@
   3) RsaCheckKey\r
   4) RsaPkcs1Sign\r
 \r
   3) RsaCheckKey\r
   4) RsaPkcs1Sign\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 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
 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
@@ -22,26 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <openssl/rsa.h>\r
 #include <openssl/err.h>\r
 \r
 #include <openssl/rsa.h>\r
 #include <openssl/err.h>\r
-\r
-//\r
-// ASN.1 value for Hash Algorithm ID with the Distringuished Encoding Rules (DER)\r
-// Refer to Section 9.2 of PKCS#1 v2.1\r
-//                           \r
-CONST UINT8  Asn1IdMd5[] = {\r
-  0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,\r
-  0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10\r
-  };\r
-\r
-CONST UINT8  Asn1IdSha1[] = {\r
-  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,\r
-  0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14\r
-  };\r
-\r
-CONST UINT8  Asn1IdSha256[] = {\r
-  0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,\r
-  0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,\r
-  0x00, 0x04, 0x20\r
-  };\r
+#include <openssl/objects.h>\r
 \r
 /**\r
   Gets the tag-designated RSA key component from the established RSA context.\r
 \r
 /**\r
   Gets the tag-designated RSA key component from the established RSA context.\r
@@ -306,75 +287,6 @@ RsaCheckKey (
   return TRUE;\r
 }\r
 \r
   return TRUE;\r
 }\r
 \r
-/**\r
-  Performs the PKCS1-v1_5 encoding methods defined in RSA PKCS #1.\r
-\r
-  @param[in]     Message        Message buffer to be encoded.\r
-  @param[in]     MessageSize    Size of message buffer in bytes.\r
-  @param[out]    DigestInfo     Pointer to buffer of digest info for output.\r
-  @param[in,out] DigestInfoSize On input, the size of DigestInfo buffer in bytes.\r
-                                On output, the size of data returned in DigestInfo\r
-                                buffer in bytes.\r
-\r
-  @retval TRUE   PKCS1-v1_5 encoding finished successfully.\r
-  @retval FALSE  Any input parameter is invalid.\r
-  @retval FALSE  DigestInfo buffer is not large enough.\r
-\r
-**/  \r
-BOOLEAN\r
-DigestInfoEncoding (\r
-  IN CONST UINT8  *Message,\r
-  IN       UINTN  MessageSize,\r
-  OUT      UINT8  *DigestInfo,\r
-  IN OUT   UINTN  *DigestInfoSize\r
-  )\r
-{\r
-  CONST UINT8  *HashDer;\r
-  UINTN        DerSize;\r
-\r
-  //\r
-  // Check input parameters.\r
-  //\r
-  if (Message == NULL || DigestInfo == NULL || DigestInfoSize == NULL) {\r
-    return FALSE;\r
-  }\r
-\r
-  //\r
-  // The original message length is used to determine the hash algorithm since\r
-  // message is digest value hashed by the specified algorithm.\r
-  //\r
-  switch (MessageSize) {\r
-  case MD5_DIGEST_SIZE:\r
-    HashDer = Asn1IdMd5;\r
-    DerSize = sizeof (Asn1IdMd5);\r
-    break;\r
-  \r
-  case SHA1_DIGEST_SIZE:\r
-    HashDer = Asn1IdSha1;\r
-    DerSize = sizeof (Asn1IdSha1);\r
-    break;\r
-   \r
-  case SHA256_DIGEST_SIZE:\r
-    HashDer = Asn1IdSha256;\r
-    DerSize = sizeof (Asn1IdSha256);\r
-    break;\r
-  \r
-  default:\r
-    return FALSE;\r
-  }\r
-\r
-  if (*DigestInfoSize < DerSize + MessageSize) {\r
-    *DigestInfoSize = DerSize + MessageSize;\r
-    return FALSE;\r
-  }\r
-\r
-  CopyMem (DigestInfo, HashDer, DerSize);\r
-  CopyMem (DigestInfo + DerSize, Message, MessageSize);\r
-\r
-  *DigestInfoSize = DerSize + MessageSize;\r
-  return TRUE;\r
-}\r
-\r
 /**\r
   Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.\r
 \r
 /**\r
   Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.\r
 \r
@@ -412,13 +324,12 @@ RsaPkcs1Sign (
 {\r
   RSA      *Rsa;\r
   UINTN    Size;\r
 {\r
   RSA      *Rsa;\r
   UINTN    Size;\r
-  INTN     ReturnVal;\r
+  INT32    DigestType;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (RsaContext == NULL || MessageHash == NULL ||\r
-    (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE)) {\r
+  if (RsaContext == NULL || MessageHash == NULL) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
@@ -429,28 +340,38 @@ RsaPkcs1Sign (
     *SigSize = Size;\r
     return FALSE;\r
   }\r
     *SigSize = Size;\r
     return FALSE;\r
   }\r
-\r
+  \r
   if (Signature == NULL) {\r
     return FALSE;\r
   }\r
   if (Signature == NULL) {\r
     return FALSE;\r
   }\r
+  \r
+  //\r
+  // Determine the message digest algorithm according to digest size.\r
+  //   Only MD5, SHA-1 or SHA-256 algorithm is supported. \r
+  //\r
+  switch (HashSize) {\r
+  case MD5_DIGEST_SIZE:\r
+    DigestType = NID_md5;\r
+    break;\r
+    \r
+  case SHA1_DIGEST_SIZE:\r
+    DigestType = NID_sha1;\r
+    break;\r
+    \r
+  case SHA256_DIGEST_SIZE:\r
+    DigestType = NID_sha256;\r
+    break;\r
 \r
 \r
-  if (!DigestInfoEncoding (MessageHash, HashSize, Signature, SigSize)) {\r
-    return FALSE;\r
-  }\r
-\r
-  ReturnVal = RSA_private_encrypt (\r
-                (UINT32) *SigSize,\r
-                Signature,\r
-                Signature,\r
-                Rsa,\r
-                RSA_PKCS1_PADDING\r
-                );\r
-\r
-  if (ReturnVal < (INTN) *SigSize) {\r
+  default:\r
     return FALSE;\r
     return FALSE;\r
-  }\r
-\r
-  *SigSize = (UINTN) ReturnVal;\r
-  return TRUE;\r
+  }  \r
+\r
+  return (BOOLEAN) RSA_sign (\r
+                     DigestType,\r
+                     MessageHash,\r
+                     (UINT32) HashSize,\r
+                     Signature,\r
+                     (UINT32 *) SigSize,\r
+                     (RSA *) RsaContext\r
+                     );\r
 }\r
 }\r
-\r