mirror of https://github.com/acidanthera/audk.git
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 <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
This commit is contained in:
parent
e9521b086a
commit
86b5c3ee54
|
@ -7,7 +7,7 @@
|
||||||
3) RsaSetKey
|
3) RsaSetKey
|
||||||
4) RsaPkcs1Verify
|
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
|
This program and the accompanying materials
|
||||||
are licensed and made available under the terms and conditions of the BSD License
|
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
|
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 "InternalCryptLib.h"
|
||||||
|
|
||||||
#include <openssl/rsa.h>
|
#include <openssl/rsa.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/objects.h>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Allocates and initializes one RSA context for subsequent use.
|
Allocates and initializes one RSA context for subsequent use.
|
||||||
|
@ -289,8 +288,8 @@ RsaPkcs1Verify (
|
||||||
IN UINTN SigSize
|
IN UINTN SigSize
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
INTN Length;
|
INT32 DigestType;
|
||||||
UINT8 *DecryptedSigature;
|
UINT8 *SigBuf;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check input parameters.
|
// Check input parameters.
|
||||||
|
@ -304,63 +303,33 @@ RsaPkcs1Verify (
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check for unsupported hash size:
|
// Determine the message digest algorithm according to digest size.
|
||||||
// Only MD5, SHA-1 or SHA-256 digest size is supported
|
// Only MD5, SHA-1 or SHA-256 algorithm is supported.
|
||||||
//
|
//
|
||||||
if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE) {
|
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;
|
||||||
|
|
||||||
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
SigBuf = (UINT8 *) Signature;
|
||||||
// Prepare buffer to store decrypted signature.
|
return (BOOLEAN) RSA_verify (
|
||||||
//
|
DigestType,
|
||||||
DecryptedSigature = (UINT8 *) malloc (SigSize);
|
MessageHash,
|
||||||
if (DecryptedSigature == NULL) {
|
(UINT32) HashSize,
|
||||||
return FALSE;
|
SigBuf,
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key
|
|
||||||
//
|
|
||||||
Length = RSA_public_decrypt (
|
|
||||||
(UINT32) SigSize,
|
(UINT32) SigSize,
|
||||||
Signature,
|
(RSA *) RsaContext
|
||||||
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);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
3) RsaCheckKey
|
3) RsaCheckKey
|
||||||
4) RsaPkcs1Sign
|
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
|
This program and the accompanying materials
|
||||||
are licensed and made available under the terms and conditions of the BSD License
|
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
|
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 <openssl/rsa.h>
|
#include <openssl/rsa.h>
|
||||||
#include <openssl/err.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.
|
Gets the tag-designated RSA key component from the established RSA context.
|
||||||
|
@ -306,75 +287,6 @@ RsaCheckKey (
|
||||||
return TRUE;
|
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.
|
Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
|
||||||
|
|
||||||
|
@ -412,13 +324,12 @@ RsaPkcs1Sign (
|
||||||
{
|
{
|
||||||
RSA *Rsa;
|
RSA *Rsa;
|
||||||
UINTN Size;
|
UINTN Size;
|
||||||
INTN ReturnVal;
|
INT32 DigestType;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check input parameters.
|
// Check input parameters.
|
||||||
//
|
//
|
||||||
if (RsaContext == NULL || MessageHash == NULL ||
|
if (RsaContext == NULL || MessageHash == NULL) {
|
||||||
(HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE)) {
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,23 +345,33 @@ RsaPkcs1Sign (
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!DigestInfoEncoding (MessageHash, HashSize, Signature, SigSize)) {
|
//
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnVal = RSA_private_encrypt (
|
return (BOOLEAN) RSA_sign (
|
||||||
(UINT32) *SigSize,
|
DigestType,
|
||||||
|
MessageHash,
|
||||||
|
(UINT32) HashSize,
|
||||||
Signature,
|
Signature,
|
||||||
Signature,
|
(UINT32 *) SigSize,
|
||||||
Rsa,
|
(RSA *) RsaContext
|
||||||
RSA_PKCS1_PADDING
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (ReturnVal < (INTN) *SigSize) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
*SigSize = (UINTN) ReturnVal;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue