Revision: 14309
          http://edk2.svn.sourceforge.net/edk2/?rev=14309&view=rev
Author:   sfu5
Date:     2013-04-23 01:52:17 +0000 (Tue, 23 Apr 2013)
Log Message:
-----------
The openssl API RSA_public_decrypt() and RSA_private_encrypt() are deprecated, 
use RSA_sign(), RSA_verify() instead.
Signed-off-by: Long Qin < [email protected] >
Reviewed-by: Ye Ting  <[email protected]>
Reviewed-by: Dong Guo <[email protected]>

Modified Paths:
--------------
    trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
    trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c

Modified: trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
===================================================================
--- trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c        
2013-04-23 01:42:35 UTC (rev 14308)
+++ trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c        
2013-04-23 01:52:17 UTC (rev 14309)
@@ -7,7 +7,7 @@
   3) RsaSetKey
   4) RsaPkcs1Verify
 
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -21,9 +21,8 @@
 #include "InternalCryptLib.h"
 
 #include <openssl/rsa.h>
-#include <openssl/err.h>
+#include <openssl/objects.h>
 
-
 /**
   Allocates and initializes one RSA context for subsequent use.
 
@@ -289,8 +288,8 @@
   IN  UINTN        SigSize
   )
 {
-  INTN     Length;
-  UINT8    *DecryptedSigature;
+  INT32    DigestType;
+  UINT8    *SigBuf;
 
   //
   // Check input parameters.
@@ -302,65 +301,35 @@
   if (SigSize > INT_MAX || SigSize == 0) {
     return FALSE;
   }
-  
-  //
-  // Check for unsupported hash size:
-  //    Only MD5, SHA-1 or SHA-256 digest size is supported
-  //
-  if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize 
!= SHA256_DIGEST_SIZE) {
-    return FALSE;
-  }
 
   //
-  // Prepare buffer to store decrypted signature.
+  // Determine the message digest algorithm according to digest size.
+  //   Only MD5, SHA-1 or SHA-256 algorithm is supported. 
   //
-  DecryptedSigature = (UINT8 *) malloc (SigSize);
-  if (DecryptedSigature == NULL) {
-    return FALSE;
-  }
+  switch (HashSize) {
+  case MD5_DIGEST_SIZE:
+    DigestType = NID_md5;
+    break;
+    
+  case SHA1_DIGEST_SIZE:
+    DigestType = NID_sha1;
+    break;
+    
+  case SHA256_DIGEST_SIZE:
+    DigestType = NID_sha256;
+    break;
 
-  //
-  // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key
-  //
-  Length = RSA_public_decrypt (
-             (UINT32) SigSize,
-             Signature,
-             DecryptedSigature,
-             RsaContext,
-             RSA_PKCS1_PADDING
-             );
-
-  //
-  // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)
-  // NOTE: Length should be the addition of HashSize and some DER value.
-  //       Ignore more strict length checking here.
-  //
-  if (Length < (INTN) HashSize) {
-    free (DecryptedSigature);
+  default:
     return FALSE;
   }
 
-  //
-  // Validate the MessageHash and Decoded Signature
-  // NOTE: The decoded Signature should be the DER encoding of the DigestInfo 
value
-  //       DigestInfo ::= SEQUENCE {
-  //           digestAlgorithm AlgorithmIdentifier
-  //           digest OCTET STRING
-  //       }
-  //       Then Memory Comparing should skip the DER value of the underlying 
SEQUENCE
-  //       type and AlgorithmIdentifier.
-  //
-  if (CompareMem (MessageHash, DecryptedSigature + Length - HashSize, 
HashSize) == 0) {
-    //
-    // Valid RSA PKCS#1 Signature
-    //
-    free (DecryptedSigature);
-    return TRUE;
-  } else {
-    //
-    // Failed to verification
-    //
-    free (DecryptedSigature);
-    return FALSE;
-  }
+  SigBuf = (UINT8 *) Signature;
+  return (BOOLEAN) RSA_verify (
+                     DigestType,
+                     MessageHash,
+                     (UINT32) HashSize,
+                     SigBuf,
+                     (UINT32) SigSize,
+                     (RSA *) RsaContext
+                     );
 }

Modified: trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c
===================================================================
--- trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c  2013-04-23 
01:42:35 UTC (rev 14308)
+++ trunk/edk2/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c  2013-04-23 
01:52:17 UTC (rev 14309)
@@ -7,7 +7,7 @@
   3) RsaCheckKey
   4) RsaPkcs1Sign
 
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -22,27 +22,8 @@
 
 #include <openssl/rsa.h>
 #include <openssl/err.h>
+#include <openssl/objects.h>
 
-//
-// ASN.1 value for Hash Algorithm ID with the Distringuished Encoding Rules 
(DER)
-// Refer to Section 9.2 of PKCS#1 v2.1
-//                           
-CONST UINT8  Asn1IdMd5[] = {
-  0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,
-  0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
-  };
-
-CONST UINT8  Asn1IdSha1[] = {
-  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
-  0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
-  };
-
-CONST UINT8  Asn1IdSha256[] = {
-  0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
-  0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
-  0x00, 0x04, 0x20
-  };
-
 /**
   Gets the tag-designated RSA key component from the established RSA context.
 
@@ -307,75 +288,6 @@
 }
 
 /**
-  Performs the PKCS1-v1_5 encoding methods defined in RSA PKCS #1.
-
-  @param[in]     Message        Message buffer to be encoded.
-  @param[in]     MessageSize    Size of message buffer in bytes.
-  @param[out]    DigestInfo     Pointer to buffer of digest info for output.
-  @param[in,out] DigestInfoSize On input, the size of DigestInfo buffer in 
bytes.
-                                On output, the size of data returned in 
DigestInfo
-                                buffer in bytes.
-
-  @retval TRUE   PKCS1-v1_5 encoding finished successfully.
-  @retval FALSE  Any input parameter is invalid.
-  @retval FALSE  DigestInfo buffer is not large enough.
-
-**/  
-BOOLEAN
-DigestInfoEncoding (
-  IN CONST UINT8  *Message,
-  IN       UINTN  MessageSize,
-  OUT      UINT8  *DigestInfo,
-  IN OUT   UINTN  *DigestInfoSize
-  )
-{
-  CONST UINT8  *HashDer;
-  UINTN        DerSize;
-
-  //
-  // Check input parameters.
-  //
-  if (Message == NULL || DigestInfo == NULL || DigestInfoSize == NULL) {
-    return FALSE;
-  }
-
-  //
-  // The original message length is used to determine the hash algorithm since
-  // message is digest value hashed by the specified algorithm.
-  //
-  switch (MessageSize) {
-  case MD5_DIGEST_SIZE:
-    HashDer = Asn1IdMd5;
-    DerSize = sizeof (Asn1IdMd5);
-    break;
-  
-  case SHA1_DIGEST_SIZE:
-    HashDer = Asn1IdSha1;
-    DerSize = sizeof (Asn1IdSha1);
-    break;
-   
-  case SHA256_DIGEST_SIZE:
-    HashDer = Asn1IdSha256;
-    DerSize = sizeof (Asn1IdSha256);
-    break;
-  
-  default:
-    return FALSE;
-  }
-
-  if (*DigestInfoSize < DerSize + MessageSize) {
-    *DigestInfoSize = DerSize + MessageSize;
-    return FALSE;
-  }
-
-  CopyMem (DigestInfo, HashDer, DerSize);
-  CopyMem (DigestInfo + DerSize, Message, MessageSize);
-
-  *DigestInfoSize = DerSize + MessageSize;
-  return TRUE;
-}
-
-/**
   Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding 
scheme.
 
   This function carries out the RSA-SSA signature generation with 
EMSA-PKCS1-v1_5 encoding scheme defined in
@@ -412,13 +324,12 @@
 {
   RSA      *Rsa;
   UINTN    Size;
-  INTN     ReturnVal;
+  INT32    DigestType;
 
   //
   // Check input parameters.
   //
-  if (RsaContext == NULL || MessageHash == NULL ||
-    (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize 
!= SHA256_DIGEST_SIZE)) {
+  if (RsaContext == NULL || MessageHash == NULL) {
     return FALSE;
   }
 
@@ -429,28 +340,38 @@
     *SigSize = Size;
     return FALSE;
   }
-
+  
   if (Signature == NULL) {
     return FALSE;
   }
+  
+  //
+  // Determine the message digest algorithm according to digest size.
+  //   Only MD5, SHA-1 or SHA-256 algorithm is supported. 
+  //
+  switch (HashSize) {
+  case MD5_DIGEST_SIZE:
+    DigestType = NID_md5;
+    break;
+    
+  case SHA1_DIGEST_SIZE:
+    DigestType = NID_sha1;
+    break;
+    
+  case SHA256_DIGEST_SIZE:
+    DigestType = NID_sha256;
+    break;
 
-  if (!DigestInfoEncoding (MessageHash, HashSize, Signature, SigSize)) {
+  default:
     return FALSE;
-  }
+  }  
 
-  ReturnVal = RSA_private_encrypt (
-                (UINT32) *SigSize,
-                Signature,
-                Signature,
-                Rsa,
-                RSA_PKCS1_PADDING
-                );
-
-  if (ReturnVal < (INTN) *SigSize) {
-    return FALSE;
-  }
-
-  *SigSize = (UINTN) ReturnVal;
-  return TRUE;
+  return (BOOLEAN) RSA_sign (
+                     DigestType,
+                     MessageHash,
+                     (UINT32) HashSize,
+                     Signature,
+                     (UINT32 *) SigSize,
+                     (RSA *) RsaContext
+                     );
 }
-

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
edk2-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-commits

Reply via email to