From 86b5c3ee549bbdeefa72115c2b7ca7f6f625ebed Mon Sep 17 00:00:00 2001 From: sfu5 Date: Tue, 23 Apr 2013 01:52:17 +0000 Subject: The openssl API RSA_public_decrypt() and RSA_private_encrypt() are deprecated, use RSA_sign(), RSA_verify() instead. Signed-off-by: Long Qin < qin.long@intel.com > Reviewed-by: Ye Ting Reviewed-by: Dong Guo git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14309 6f19259b-4bc3-4df7-8a09-765794883524 --- CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c | 87 +++++-------- CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c | 145 +++++----------------- 2 files changed, 61 insertions(+), 171 deletions(-) (limited to 'CryptoPkg/Library/BaseCryptLib/Pk') diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c index cbe3c50fed..3e4309899c 100644 --- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c +++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c @@ -7,7 +7,7 @@ 3) RsaSetKey 4) RsaPkcs1Verify -Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
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,8 +21,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "InternalCryptLib.h" #include -#include - +#include /** Allocates and initializes one RSA context for subsequent use. @@ -289,8 +288,8 @@ RsaPkcs1Verify ( IN UINTN SigSize ) { - INTN Length; - UINT8 *DecryptedSigature; + INT32 DigestType; + UINT8 *SigBuf; // // Check input parameters. @@ -302,65 +301,35 @@ RsaPkcs1Verify ( 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. - // - DecryptedSigature = (UINT8 *) malloc (SigSize); - if (DecryptedSigature == NULL) { - return FALSE; - } // - // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key + // Determine the message digest algorithm according to digest size. + // Only MD5, SHA-1 or SHA-256 algorithm is supported. // - Length = RSA_public_decrypt ( - (UINT32) SigSize, - Signature, - DecryptedSigature, - RsaContext, - RSA_PKCS1_PADDING - ); + 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; - // - // 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 + ); } diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c index b4faafa0c3..5c21d121f5 100644 --- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c +++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c @@ -7,7 +7,7 @@ 3) RsaCheckKey 4) RsaPkcs1Sign -Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
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,26 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include - -// -// 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 - }; +#include /** Gets the tag-designated RSA key component from the established RSA context. @@ -306,75 +287,6 @@ RsaCheckKey ( return TRUE; } -/** - 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. @@ -412,13 +324,12 @@ RsaPkcs1Sign ( { 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 @@ RsaPkcs1Sign ( *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)) { - return FALSE; - } - - ReturnVal = RSA_private_encrypt ( - (UINT32) *SigSize, - Signature, - Signature, - Rsa, - RSA_PKCS1_PADDING - ); - - if (ReturnVal < (INTN) *SigSize) { + default: return FALSE; - } - - *SigSize = (UINTN) ReturnVal; - return TRUE; + } + + return (BOOLEAN) RSA_sign ( + DigestType, + MessageHash, + (UINT32) HashSize, + Signature, + (UINT32 *) SigSize, + (RSA *) RsaContext + ); } - -- cgit v1.2.3