]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Contributed-under: TianoCore Contribution Agreement 1.0
authorMichael Kinney <michael.d.kinney@intel.com>
Thu, 14 Aug 2014 06:31:34 +0000 (06:31 +0000)
committermdkinney <mdkinney@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 14 Aug 2014 06:31:34 +0000 (06:31 +0000)
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
Reviewed-by: Dong, Guo <guo.dong@intel.com>
Add support for RSA 2048 SHA 256 signing and verification encoded in a PI FFS GUIDED Encapsulation Section.  The primary use case of this feature is in support of signing and verification of encapsulated FVs for Recovery and Capsule Update, but can potentially be used for signing and verification of any content that can be stored in a PI conformant FFS file.  Signing operations are performed from python scripts that wrap OpenSsl command line utilities.  Verification operations are performed using the OpenSsl libraries in the CryptoPkg.

The guided encapsulation sections uses the UEFI 2.4 Specification defined GUID called EFI_CERT_TYPE_RSA2048_SHA256_GUID.  The data layout for the encapsulation section starts with the UEFI 2.4 Specification defined structure called EFI_CERT_BLOCK_RSA_2048_SHA256 followed immediately by the data.  The signing tool included in these patches performs encode/decode operations using this data layout.  HashType is set to the UEFI 2.4 Specification defined GUID called EFI_HASH_ALGORITHM_SHA256_GUID.

MdePkg/Include/Guid/WinCertificate.h
=================================
//
// WIN_CERTIFICATE_UEFI_GUID.CertType
//
#define EFI_CERT_TYPE_RSA2048_SHA256_GUID \
  {0xa7717414, 0xc616, 0x4977, {0x94, 0x20, 0x84, 0x47, 0x12, 0xa7, 0x35, 0xbf } }

///
/// WIN_CERTIFICATE_UEFI_GUID.CertData
///
typedef struct {
  EFI_GUID  HashType;
  UINT8     PublicKey[256];
  UINT8     Signature[256];
} EFI_CERT_BLOCK_RSA_2048_SHA256;

MdePkg/Include/Protocol/Hash.h
=================================
#define EFI_HASH_ALGORITHM_SHA256_GUID \
  { \
    0x51aa59de, 0xfdf2, 0x4ea3, {0xbc, 0x63, 0x87, 0x5f, 0xb7, 0x84, 0x2e, 0xe9 } \
  }

The verification operations require the use of public key(s).  A new PCD called gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer is added to the SecurityPkg that supports one or more SHA 256 hashes of the public keys.  A SHA 256 hash is performed to minimize the FLASH overhead of storing the public keys.  When a verification operation is performed, a SHA 256 hash is performed on EFI_CERT_BLOCK_RSA_2048_SHA256.PublicKey and a check is made to see if that hash matches any of the hashes in the new PCD.  It is recommended that this PCD always be configured in the DSC file as storage type of [PcdsDynamixExVpd], so the public keys are stored in a protected read-only region.

While working on this feature, I noticed that the CRC32 signing and verification feature was incomplete.  It only supported CRC32 based verification in the DXE Phase, so the attached patches also provide support for CRC32 based verification in the PEI Phase.

I also noticed that the most common method for incorporating guided section extraction libraries was to directly link them to the DXE Core, which is not very flexible.  The attached patches also add a generic section extraction PEIM and a generic section extraction DXE driver that can each be linked against one or more section extraction libraries.  This provides a platform developer with the option of providing section extraction services with the DXE Core or providing section extraction services with these generic PEIM/DXE Drivers.

Patch Summary
==============
1) BaseTools - Rsa2049Sha256Sign python script that can perform test signing or custom signing of PI FFS file GUIDed sections
  a. Wrapper for a set of OpenSsl command line utility operations
  b. OpenSsl command line tool must be installed in location that is in standard OS path or in path specified by OS environment variable called OPENSSL_PATH
  c. Provides standard EDK II command line arguments for a tool that encodes/decodes guided encapsulation section

Rsa2048Sha256Sign - Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.
usage: Rsa2048Sha256Sign -e|-d [options] <input_file>

positional arguments:
  input_file            specify the input filename

optional arguments:
  -e                    encode file
  -d                    decode file
  -o filename, --output filename
                        specify the output filename
  --private-key PRIVATEKEYFILE
                        specify the private key filename. If not specified, a
                        test signing key is used.
  -v, --verbose         increase output messages
  -q, --quiet           reduce output messages
  --debug [0-9]         set debug level
  --version             display the program version and exit
  -h, --help            display this help text

2) BaseTools - Rsa2049Sha256GenerateKeys python script that can generate new private/public key and PCD value that is SHA 256 hash of public key using OpenSsl command line utilities.
  a. Wrapper for a set of OpenSsl command line utility operations
  b. OpenSsl command line tool must be installed in location that is in standard path or in path specified by OS environment variable called OPENSSL_PATH

Rsa2048Sha256GenerateKeys - Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.
usage: Rsa2048Sha256GenerateKeys [options]

optional arguments:
  -o [filename [filename ...]], --output [filename [filename ...]]
                        specify the output private key filename in PEM format
  -i [filename [filename ...]], --input [filename [filename ...]]
                        specify the input private key filename in PEM format
  --public-key-hash PUBLICKEYHASHFILE
                        specify the public key hash filename that is SHA 256
                        hash of 2048 bit RSA public key in binary format
  --public-key-hash-c PUBLICKEYHASHCFILE
                        specify the public key hash filename that is SHA 256
                        hash of 2048 bit RSA public key in C structure format
  -v, --verbose         increase output messages
  -q, --quiet           reduce output messages
  --debug [0-9]         set debug level
  --version             display the program version and exit
  -h, --help            display this help text

3) BaseTools\Conf\tools_def.template
  a. Define GUID/Tool to perform RSA 2048 SHA 256 test signing and instructions on how to use alternate private/public key
b. GUID is EFI_CERT_TYPE_RSA2048_SHA256_GUID
  c. Tool is Rsa2049Sha256Sign
4) MdeModulePkg\Library\PeiCrc32GuidedSectionExtractionLib
  a. Add peer for DxeCrc32GuidedSectionExtractionLib so both PEI and DXE phases can perform basic integrity checks of PEI and DXE components
5) MdeModulePkg\Universal\SectionExtractionPei
  a. Generic PEIM that can link against one or more NULL section extraction library instances to provided one or more GUIDED Section Extraction PPIs
6) MdeModulePkg\Universal\SectionExtractionDxe
  a. Generic DXE Driver that can link against one or more NULL section extraction library instances to provide one or more GUIDED Section Extraction Protocols.
7) SecurityPkg\Library\PeiRsa2048Sha256GuidedSectionExtractLib
  a. NULL library instances that performs PEI phase RSA 2048 SHA 256 signature verification using OpenSsl libraries from CryptoPkg.
  b. Based on algorithms from SecurityPkg Authenticated Variable services
  c. Uses public key from gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer.
8) SecurityPkg\Library\DxeRsa2048Sha256GuidedSectionExtractLib
  a. NULL library instances that performs DXE phase RSA 2048 SHA 256 signature verification using OpenSsl libraries from CryptoPkg.
  b. Based on algorithms from SecurityPkg Authenticated Variable services
  c. Uses public key from gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer.

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15801 6f19259b-4bc3-4df7-8a09-765794883524

SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.c [new file with mode: 0644]
SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.inf [new file with mode: 0644]
SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.uni [new file with mode: 0644]
SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.c [new file with mode: 0644]
SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.inf [new file with mode: 0644]
SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.uni [new file with mode: 0644]
SecurityPkg/SecurityPkg.dec
SecurityPkg/SecurityPkg.dsc

diff --git a/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.c b/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.c
new file mode 100644 (file)
index 0000000..2b61014
--- /dev/null
@@ -0,0 +1,402 @@
+/** @file\r
+\r
+  This library registers RSA 2048 SHA 256 guided section handler \r
+  to parse RSA 2048 SHA 256 encapsulation section and extract raw data.\r
+  It uses the BaseCrypyLib based on OpenSSL to authenticate the signature.\r
+\r
+Copyright (c) 2013 - 2014, 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
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Protocol/Hash.h>\r
+#include <Protocol/SecurityPolicy.h>\r
+#include <Library/ExtractGuidedSectionLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Guid/WinCertificate.h>\r
+#include <Library/BaseCryptLib.h>\r
+#include <Library/PerformanceLib.h>\r
+#include <Guid/SecurityPkgTokenSpace.h>\r
+\r
+///\r
+/// RSA 2048 SHA 256 Guided Section header\r
+///\r
+typedef struct {\r
+  EFI_GUID_DEFINED_SECTION        GuidedSectionHeader;     ///< EFI guided section header\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  CertBlockRsa2048Sha256;  ///< RSA 2048-bit Signature\r
+} RSA_2048_SHA_256_SECTION_HEADER;\r
+\r
+typedef struct {\r
+  EFI_GUID_DEFINED_SECTION2       GuidedSectionHeader;     ///< EFI guided section header\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  CertBlockRsa2048Sha256;  ///< RSA 2048-bit Signature\r
+} RSA_2048_SHA_256_SECTION2_HEADER;\r
+\r
+///\r
+/// Public Exponent of RSA Key.\r
+///\r
+CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
+\r
+/**\r
+\r
+  GetInfo gets raw data size and attribute of the input guided section.\r
+  It first checks whether the input guid section is supported. \r
+  If not, EFI_INVALID_PARAMETER will return.\r
+\r
+  @param InputSection       Buffer containing the input GUIDed section to be processed.\r
+  @param OutputBufferSize   The size of OutputBuffer.\r
+  @param ScratchBufferSize  The size of ScratchBuffer.\r
+  @param SectionAttribute   The attribute of the input guided section.\r
+\r
+  @retval EFI_SUCCESS            The size of destination buffer, the size of scratch buffer and \r
+                                 the attribute of the input section are successull retrieved.\r
+  @retval EFI_INVALID_PARAMETER  The GUID in InputSection does not match this instance guid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Rsa2048Sha256GuidedSectionGetInfo (\r
+  IN  CONST VOID  *InputSection,\r
+  OUT UINT32      *OutputBufferSize,\r
+  OUT UINT32      *ScratchBufferSize,\r
+  OUT UINT16      *SectionAttribute\r
+  )\r
+{\r
+  if (IS_SECTION2 (InputSection)) {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Retrieve the size and attribute of the input section data.\r
+    //\r
+    *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
+    *ScratchBufferSize = 0;\r
+    *OutputBufferSize  = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
+  } else {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Retrieve the size and attribute of the input section data.\r
+    //\r
+    *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
+    *ScratchBufferSize = 0;\r
+    *OutputBufferSize  = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+\r
+  Extraction handler tries to extract raw data from the input guided section.\r
+  It also does authentication check for RSA 2048 SHA 256 signature in the input guided section.\r
+  It first checks whether the input guid section is supported. \r
+  If not, EFI_INVALID_PARAMETER will return.\r
+\r
+  @param InputSection    Buffer containing the input GUIDed section to be processed.\r
+  @param OutputBuffer    Buffer to contain the output raw data allocated by the caller.\r
+  @param ScratchBuffer   A pointer to a caller-allocated buffer for function internal use.\r
+  @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
+                              authentication status of the output buffer.\r
+\r
+  @retval EFI_SUCCESS            Section Data and Auth Status is extracted successfully.\r
+  @retval EFI_INVALID_PARAMETER  The GUID in InputSection does not match this instance guid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Rsa2048Sha256GuidedSectionHandler (\r
+  IN CONST  VOID    *InputSection,\r
+  OUT       VOID    **OutputBuffer,\r
+  IN        VOID    *ScratchBuffer,        OPTIONAL\r
+  OUT       UINT32  *AuthenticationStatus\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINT32                          OutputBufferSize;\r
+  VOID                            *DummyInterface;\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlockRsa2048Sha256;\r
+  BOOLEAN                         CryptoStatus;\r
+  UINT8                           Digest[SHA256_DIGEST_SIZE];\r
+  UINT8                           *PublicKey;\r
+  UINTN                           PublicKeyBufferSize;\r
+  VOID                            *HashContext;\r
+  VOID                            *Rsa;\r
+  \r
+  HashContext = NULL;\r
+  Rsa         = NULL;\r
+  \r
+  if (IS_SECTION2 (InputSection)) {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  \r
+    //\r
+    // Get the RSA 2048 SHA 256 information.\r
+    //\r
+    CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION2_HEADER *) InputSection)->CertBlockRsa2048Sha256;\r
+    OutputBufferSize       = SECTION2_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION2_HEADER);\r
+    if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
+      PERF_START (NULL, "RsaCopy", "DXE", 0);\r
+      CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER), OutputBufferSize);\r
+      PERF_END (NULL, "RsaCopy", "DXE", 0);\r
+    } else {\r
+      *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER);\r
+    }\r
+\r
+    //\r
+    // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set\r
+    //\r
+    ASSERT ((((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);\r
+    *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+  } else {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  \r
+    //\r
+    // Get the RSA 2048 SHA 256 information.\r
+    //\r
+    CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION_HEADER *)InputSection)->CertBlockRsa2048Sha256;\r
+    OutputBufferSize       = SECTION_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION_HEADER);\r
+    if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
+      PERF_START (NULL, "RsaCopy", "DXE", 0);\r
+      CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER), OutputBufferSize);\r
+      PERF_END (NULL, "RsaCopy", "DXE", 0);\r
+    } else {\r
+      *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER);\r
+    }\r
+\r
+    //\r
+    // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set\r
+    //\r
+    ASSERT ((((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);\r
+    *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+  }\r
+\r
+  //\r
+  // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // If SecurityPolicy Protocol exist, AUTH platform override bit is set.\r
+    //\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
+    \r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // All paths from here return EFI_SUCESS and result is returned in AuthenticationStatus\r
+  //\r
+  Status = EFI_SUCCESS;\r
+  \r
+  //\r
+  // Fail if the HashType is not SHA 256\r
+  //\r
+  if (!CompareGuid (&gEfiHashAlgorithmSha256Guid, &CertBlockRsa2048Sha256->HashType)) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: HASH type of section is not supported\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Allocate hash context buffer required for SHA 256\r
+  //\r
+  HashContext = AllocatePool (Sha256GetContextSize ());\r
+  if (HashContext == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Can not allocate hash context\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Hash public key from data payload with SHA256.\r
+  //\r
+  ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
+  CryptoStatus = Sha256Init (HashContext);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Init() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Update() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus  = Sha256Final (HashContext, Digest);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Final() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  \r
+  //\r
+  // Fail if the PublicKey is not one of the public keys in PcdRsa2048Sha256PublicKeyBuffer\r
+  //\r
+  PublicKey = (UINT8 *)PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer);\r
+  DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer = %p\n", PublicKey));\r
+  ASSERT (PublicKey != NULL);\r
+  DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer Token = %08x\n", PcdToken (PcdRsa2048Sha256PublicKeyBuffer)));\r
+  PublicKeyBufferSize = LibPcdGetExSize (&gEfiSecurityPkgTokenSpaceGuid, PcdToken (PcdRsa2048Sha256PublicKeyBuffer));\r
+  DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer Size = %08x\n", PublicKeyBufferSize));\r
+  ASSERT ((PublicKeyBufferSize % SHA256_DIGEST_SIZE) == 0);\r
+  CryptoStatus = FALSE;\r
+  while (PublicKeyBufferSize != 0) {\r
+    if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) {\r
+      CryptoStatus = TRUE;\r
+      break;\r
+    }\r
+    PublicKey = PublicKey + SHA256_DIGEST_SIZE;\r
+    PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE;\r
+  }\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Public key in section is not supported\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Generate & Initialize RSA Context.\r
+  //\r
+  Rsa = RsaNew ();\r
+  if (Rsa == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaNew() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  \r
+  // \r
+  // Set RSA Key Components.\r
+  // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r
+  //\r
+  CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Hash data payload with SHA256.\r
+  //\r
+  ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
+  CryptoStatus = Sha256Init (HashContext);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Init() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  PERF_START (NULL, "RsaShaData", "DXE", 0);\r
+  CryptoStatus = Sha256Update (HashContext, *OutputBuffer, OutputBufferSize);\r
+  PERF_END (NULL, "RsaShaData", "DXE", 0);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Update() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus  = Sha256Final (HashContext, Digest);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Final() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Verify the RSA 2048 SHA 256 signature.\r
+  //\r
+  PERF_START (NULL, "RsaVerify", "DXE", 0);\r
+  CryptoStatus = RsaPkcs1Verify (\r
+                   Rsa, \r
+                   Digest, \r
+                   SHA256_DIGEST_SIZE, \r
+                   CertBlockRsa2048Sha256->Signature, \r
+                   sizeof (CertBlockRsa2048Sha256->Signature)\r
+                   );\r
+  PERF_END (NULL, "RsaVerify", "DXE", 0);\r
+  if (!CryptoStatus) {\r
+    //\r
+    // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.\r
+    //\r
+    DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaPkcs1Verify() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+  }\r
+\r
+Done:\r
+  //\r
+  // Free allocated resources used to perform RSA 2048 SHA 256 signature verification\r
+  //\r
+  if (Rsa != NULL) {\r
+    RsaFree (Rsa);\r
+  }\r
+  if (HashContext != NULL) {\r
+    FreePool (HashContext);\r
+  }\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "DxeRsa2048Sha256: Status = %r  AuthenticationStatus = %08x\n", Status, *AuthenticationStatus));\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Register the handler to extract RSA 2048 SHA 256 guided section.\r
+\r
+  @param  ImageHandle  ImageHandle of the loaded driver.\r
+  @param  SystemTable  Pointer to the EFI System Table.\r
+\r
+  @retval  EFI_SUCCESS            Register successfully.\r
+  @retval  EFI_OUT_OF_RESOURCES   Not enough memory to register this handler.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DxeRsa2048Sha256GuidedSectionExtractLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return ExtractGuidedSectionRegisterHandlers (\r
+           &gEfiCertTypeRsa2048Sha256Guid,\r
+           Rsa2048Sha256GuidedSectionGetInfo,\r
+           Rsa2048Sha256GuidedSectionHandler\r
+           );\r
+}\r
diff --git a/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.inf b/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.inf
new file mode 100644 (file)
index 0000000..3ce4a60
--- /dev/null
@@ -0,0 +1,62 @@
+## @file\r
+#  This library doesn't produce any library class. The constructor function uses \r
+#  ExtractGuidedSectionLib service to register an RSA 2048 SHA 256 guided section handler\r
+#  that parses RSA 2048 SHA 256 encapsulation section and extracts raw data.\r
+#\r
+#  It uses the BaseCrypyLib based on OpenSSL to authenticate the signature.\r
+#\r
+# Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
+#\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
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = DxeRsa2048Sha256GuidedSectionExtractLib\r
+  FILE_GUID                      = 0AD6C423-4732-4cf3-9CE3-0A5416D634A5\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = NULL|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+  CONSTRUCTOR                    = DxeRsa2048Sha256GuidedSectionExtractLibConstructor\r
+  MODULE_UNI_FILE                = DxeRsa2048Sha256GuidedSectionExtractLib.uni\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF\r
+#\r
+\r
+[Sources]\r
+  DxeRsa2048Sha256GuidedSectionExtractLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  CryptoPkg/CryptoPkg.dec\r
+  SecurityPkg/SecurityPkg.dec\r
+  \r
+[LibraryClasses]\r
+  ExtractGuidedSectionLib\r
+  UefiBootServicesTableLib\r
+  DebugLib\r
+  BaseMemoryLib\r
+  MemoryAllocationLib\r
+  BaseCryptLib\r
+  PcdLib\r
+  PerformanceLib\r
+\r
+[PcdEx]  \r
+  gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer    ## SOMETIMES_CONSUMES\r
+  \r
+[Protocols]\r
+  gEfiSecurityPolicyProtocolGuid                   ## SOMETIMES_CONSUMES (Set platform override AUTH status if exist)\r
+  \r
+[Guids]\r
+  gEfiCertTypeRsa2048Sha256Guid  ## PRODUCES       ## UNDEFINED  # Specifies RSA 2048 SHA 256 authentication algorithm.\r
+  gEfiHashAlgorithmSha256Guid    ## SOMETIMES_CONSUMES\r
+  
\ No newline at end of file
diff --git a/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.uni b/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.uni
new file mode 100644 (file)
index 0000000..f3d5a5f
Binary files /dev/null and b/SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.uni differ
diff --git a/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.c b/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.c
new file mode 100644 (file)
index 0000000..8c0047e
--- /dev/null
@@ -0,0 +1,393 @@
+/** @file\r
+\r
+  This library registers RSA 2048 SHA 256 guided section handler \r
+  to parse RSA 2048 SHA 256 encapsulation section and extract raw data.\r
+  It uses the BaseCrypyLib based on OpenSSL to authenticate the signature.\r
+\r
+Copyright (c) 2013 - 2014, 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
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+**/\r
+\r
+#include <PiPei.h>\r
+#include <Protocol/Hash.h>\r
+#include <Library/ExtractGuidedSectionLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Guid/WinCertificate.h>\r
+#include <Library/BaseCryptLib.h>\r
+#include <Library/PerformanceLib.h>\r
+#include <Guid/SecurityPkgTokenSpace.h>\r
+\r
+///\r
+/// RSA 2048 SHA 256 Guided Section header\r
+///\r
+typedef struct {\r
+  EFI_GUID_DEFINED_SECTION        GuidedSectionHeader;     ///< EFI guided section header\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  CertBlockRsa2048Sha256;  ///< RSA 2048-bit Signature\r
+} RSA_2048_SHA_256_SECTION_HEADER;\r
+\r
+typedef struct {\r
+  EFI_GUID_DEFINED_SECTION2       GuidedSectionHeader;     ///< EFI guided section header\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  CertBlockRsa2048Sha256;  ///< RSA 2048-bit Signature\r
+} RSA_2048_SHA_256_SECTION2_HEADER;\r
+\r
+///\r
+/// Public Exponent of RSA Key.\r
+///\r
+CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
+\r
+/**\r
+\r
+  GetInfo gets raw data size and attribute of the input guided section.\r
+  It first checks whether the input guid section is supported. \r
+  If not, EFI_INVALID_PARAMETER will return.\r
+\r
+  @param InputSection       Buffer containing the input GUIDed section to be processed.\r
+  @param OutputBufferSize   The size of OutputBuffer.\r
+  @param ScratchBufferSize  The size of ScratchBuffer.\r
+  @param SectionAttribute   The attribute of the input guided section.\r
+\r
+  @retval EFI_SUCCESS            The size of destination buffer, the size of scratch buffer and \r
+                                 the attribute of the input section are successull retrieved.\r
+  @retval EFI_INVALID_PARAMETER  The GUID in InputSection does not match this instance guid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Rsa2048Sha256GuidedSectionGetInfo (\r
+  IN  CONST VOID  *InputSection,\r
+  OUT UINT32      *OutputBufferSize,\r
+  OUT UINT32      *ScratchBufferSize,\r
+  OUT UINT16      *SectionAttribute\r
+  )\r
+{\r
+  if (IS_SECTION2 (InputSection)) {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Retrieve the size and attribute of the input section data.\r
+    //\r
+    *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
+    *ScratchBufferSize = 0;\r
+    *OutputBufferSize  = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
+  } else {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Retrieve the size and attribute of the input section data.\r
+    //\r
+    *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
+    *ScratchBufferSize = 0;\r
+    *OutputBufferSize  = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+\r
+  Extraction handler tries to extract raw data from the input guided section.\r
+  It also does authentication check for RSA 2048 SHA 256 signature in the input guided section.\r
+  It first checks whether the input guid section is supported. \r
+  If not, EFI_INVALID_PARAMETER will return.\r
+\r
+  @param InputSection    Buffer containing the input GUIDed section to be processed.\r
+  @param OutputBuffer    Buffer to contain the output raw data allocated by the caller.\r
+  @param ScratchBuffer   A pointer to a caller-allocated buffer for function internal use.\r
+  @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
+                              authentication status of the output buffer.\r
+\r
+  @retval EFI_SUCCESS            Section Data and Auth Status is extracted successfully.\r
+  @retval EFI_INVALID_PARAMETER  The GUID in InputSection does not match this instance guid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Rsa2048Sha256GuidedSectionHandler (\r
+  IN CONST  VOID    *InputSection,\r
+  OUT       VOID    **OutputBuffer,\r
+  IN        VOID    *ScratchBuffer,        OPTIONAL\r
+  OUT       UINT32  *AuthenticationStatus\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINT32                          OutputBufferSize;\r
+  EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlockRsa2048Sha256;\r
+  BOOLEAN                         CryptoStatus;\r
+  UINT8                           Digest[SHA256_DIGEST_SIZE];\r
+  UINT8                           *PublicKey;\r
+  UINTN                           PublicKeyBufferSize;\r
+  VOID                            *HashContext;\r
+  VOID                            *Rsa;\r
+  \r
+  HashContext = NULL;\r
+  Rsa         = NULL;\r
+  \r
+  if (IS_SECTION2 (InputSection)) {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  \r
+    //\r
+    // Get the RSA 2048 SHA 256 information.\r
+    //\r
+    CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION2_HEADER *) InputSection)->CertBlockRsa2048Sha256;\r
+    OutputBufferSize       = SECTION2_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION2_HEADER);\r
+    if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
+      PERF_START (NULL, "RsaCopy", "PEI", 0);\r
+      CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER), OutputBufferSize);\r
+      PERF_END (NULL, "RsaCopy", "PEI", 0);\r
+    } else {\r
+      *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER);\r
+    }\r
+\r
+    //\r
+    // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set\r
+    //\r
+    ASSERT ((((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);\r
+    *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+  } else {\r
+    //\r
+    // Check whether the input guid section is recognized.\r
+    //\r
+    if (!CompareGuid (\r
+        &gEfiCertTypeRsa2048Sha256Guid,\r
+        &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  \r
+    //\r
+    // Get the RSA 2048 SHA 256 information.\r
+    //\r
+    CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION_HEADER *)InputSection)->CertBlockRsa2048Sha256;\r
+    OutputBufferSize       = SECTION_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION_HEADER);\r
+    if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
+      PERF_START (NULL, "RsaCopy", "PEI", 0);\r
+      CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER), OutputBufferSize);\r
+      PERF_END (NULL, "RsaCopy", "PEI", 0);\r
+    } else {\r
+      *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER);\r
+    }\r
+\r
+    //\r
+    // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set\r
+    //\r
+    ASSERT ((((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);\r
+    *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+  }\r
+\r
+  //\r
+  // All paths from here return EFI_SUCESS and result is returned in AuthenticationStatus\r
+  //\r
+  Status = EFI_SUCCESS;\r
+  \r
+  //\r
+  // Fail if the HashType is not SHA 256\r
+  //\r
+  if (!CompareGuid (&gEfiHashAlgorithmSha256Guid, &CertBlockRsa2048Sha256->HashType)) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: HASH type of section is not supported\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Allocate hash context buffer required for SHA 256\r
+  //\r
+  HashContext = AllocatePool (Sha256GetContextSize ());\r
+  if (HashContext == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Can not allocate hash context\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Hash public key from data payload with SHA256.\r
+  //\r
+  ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
+  CryptoStatus = Sha256Init (HashContext);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Init() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Update() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus  = Sha256Final (HashContext, Digest);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Final() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  \r
+  //\r
+  // Fail if the PublicKey is not one of the public keys in PcdRsa2048Sha256PublicKeyBuffer\r
+  //\r
+  PublicKey = (UINT8 *)PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer);\r
+  DEBUG ((DEBUG_VERBOSE, "PeiPcdRsa2048Sha256: PublicKeyBuffer = %p\n", PublicKey));\r
+  ASSERT (PublicKey != NULL);\r
+  DEBUG ((DEBUG_VERBOSE, "PeiPcdRsa2048Sha256: PublicKeyBuffer Token = %08x\n", PcdToken (PcdRsa2048Sha256PublicKeyBuffer)));\r
+  PublicKeyBufferSize = LibPcdGetExSize (&gEfiSecurityPkgTokenSpaceGuid, PcdToken (PcdRsa2048Sha256PublicKeyBuffer));\r
+  DEBUG ((DEBUG_VERBOSE, "PeiPcdRsa2048Sha256: PublicKeyBuffer Size = %08x\n", PublicKeyBufferSize));\r
+  ASSERT ((PublicKeyBufferSize % SHA256_DIGEST_SIZE) == 0);\r
+  CryptoStatus = FALSE;\r
+  while (PublicKeyBufferSize != 0) {\r
+    if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) {\r
+      CryptoStatus = TRUE;\r
+      break;\r
+    }\r
+    PublicKey = PublicKey + SHA256_DIGEST_SIZE;\r
+    PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE;\r
+  }\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Public key in section is not supported\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Generate & Initialize RSA Context.\r
+  //\r
+  Rsa = RsaNew ();\r
+  if (Rsa == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: RsaNew() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  \r
+  // \r
+  // Set RSA Key Components.\r
+  // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r
+  //\r
+  CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Hash data payload with SHA256.\r
+  //\r
+  ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
+  CryptoStatus = Sha256Init (HashContext);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Init() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  PERF_START (NULL, "RsaShaData", "PEI", 0);\r
+  CryptoStatus = Sha256Update (HashContext, *OutputBuffer, OutputBufferSize);\r
+  PERF_END (NULL, "RsaShaData", "PEI", 0);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Update() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+  CryptoStatus  = Sha256Final (HashContext, Digest);\r
+  if (!CryptoStatus) {\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: Sha256Final() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Verify the RSA 2048 SHA 256 signature.\r
+  //\r
+  PERF_START (NULL, "RsaVerify", "PEI", 0);\r
+  CryptoStatus = RsaPkcs1Verify (\r
+                   Rsa, \r
+                   Digest, \r
+                   SHA256_DIGEST_SIZE, \r
+                   CertBlockRsa2048Sha256->Signature, \r
+                   sizeof (CertBlockRsa2048Sha256->Signature)\r
+                   );\r
+  PERF_END (NULL, "RsaVerify", "PEI", 0);\r
+  if (!CryptoStatus) {\r
+    //\r
+    // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.\r
+    //\r
+    DEBUG ((DEBUG_ERROR, "PeiRsa2048Sha256: RsaPkcs1Verify() failed\n"));\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+  }\r
+\r
+Done:\r
+  //\r
+  // Free allocated resources used to perform RSA 2048 SHA 256 signature verification\r
+  //\r
+  if (Rsa != NULL) {\r
+    RsaFree (Rsa);\r
+  }\r
+  if (HashContext != NULL) {\r
+    FreePool (HashContext);\r
+  }\r
+\r
+  //\r
+  // Temp solution until PeiCore checks AUTH Status.\r
+  //\r
+  if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {\r
+    Status = EFI_ACCESS_DENIED;\r
+  }\r
+  DEBUG ((DEBUG_VERBOSE, "PeiRsa2048Sha256: Status = %r  AuthenticationStatus = %08x\n", Status, *AuthenticationStatus));\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Register the handler to extract RSA 2048 SHA 256 guided section.\r
+\r
+  @param  FileHandle   The handle of FFS header the loaded driver.\r
+  @param  PeiServices  The pointer to the PEI services.\r
+\r
+  @retval  EFI_SUCCESS           Register successfully.\r
+  @retval  EFI_OUT_OF_RESOURCES  Not enough memory to register this handler.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiRsa2048Sha256GuidedSectionExtractLibConstructor (\r
+  IN EFI_PEI_FILE_HANDLE        FileHandle,\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices\r
+  )\r
+{\r
+  return ExtractGuidedSectionRegisterHandlers (\r
+           &gEfiCertTypeRsa2048Sha256Guid,\r
+           Rsa2048Sha256GuidedSectionGetInfo,\r
+           Rsa2048Sha256GuidedSectionHandler\r
+           );\r
+}\r
diff --git a/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.inf b/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.inf
new file mode 100644 (file)
index 0000000..ebd4a16
--- /dev/null
@@ -0,0 +1,58 @@
+## @file\r
+#  This library doesn't produce any library class. The constructor function uses \r
+#  ExtractGuidedSectionLib service to register an RSA 2048 SHA 256 guided section handler\r
+#  that parses RSA 2048 SHA 256 encapsulation section and extracts raw data.\r
+#\r
+#  It uses the BaseCrypyLib based on OpenSSL to authenticate the signature.\r
+#\r
+# Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
+#\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
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = PeiRsa2048Sha256GuidedSectionExtractLib\r
+  FILE_GUID                      = FD5F2C91-4878-4007-BBA1-1B91DD325438\r
+  MODULE_TYPE                    = PEIM\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = NULL|PEI_CORE PEIM\r
+  CONSTRUCTOR                    = PeiRsa2048Sha256GuidedSectionExtractLibConstructor\r
+  MODULE_UNI_FILE                = PeiRsa2048Sha256GuidedSectionExtractLib.uni\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF\r
+#\r
+\r
+[Sources]\r
+  PeiRsa2048Sha256GuidedSectionExtractLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  CryptoPkg/CryptoPkg.dec\r
+  SecurityPkg/SecurityPkg.dec\r
+\r
+[LibraryClasses]\r
+  ExtractGuidedSectionLib\r
+  DebugLib\r
+  BaseMemoryLib\r
+  MemoryAllocationLib\r
+  BaseCryptLib\r
+  PcdLib\r
+  PerformanceLib\r
+\r
+[PcdEx]  \r
+  gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer      ## SOMETIMES_CONSUMES\r
+  \r
+[Guids]\r
+  gEfiCertTypeRsa2048Sha256Guid  ## PRODUCES     ## UNDEFINED  # Specifies RSA 2048 SHA 256 authentication algorithm.\r
+  gEfiHashAlgorithmSha256Guid    ## SOMETIMES_CONSUMES\r
+  
\ No newline at end of file
diff --git a/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.uni b/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.uni
new file mode 100644 (file)
index 0000000..b1ec0be
Binary files /dev/null and b/SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.uni differ
index bc058731fbd8f336ddb59d1c793c7eeae34d06ca..68ac464ac61fded2c668ab11cab83cd377801c33 100644 (file)
 \r
   ## This PCD indicates TPM base address.\r
   gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress|0xFED40000|UINT64|0x00010012\r
 \r
   ## This PCD indicates TPM base address.\r
   gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress|0xFED40000|UINT64|0x00010012\r
+\r
+  ## Provides one or more SHA 256 Hashes of the RSA 2048 public keys used to verify Recovery and Capsule Update images\r
+  #\r
+  # @Prompt One or more SHA 256 Hashes of RSA 2048 bit public keys used to verify Recovery and Capsule Update images\r
+  #\r
+  gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer|{0x91, 0x29, 0xc4, 0xbd, 0xea, 0x6d, 0xda, 0xb3, 0xaa, 0x6f, 0x50, 0x16, 0xfc, 0xdb, 0x4b, 0x7e, 0x3c, 0xd6, 0xdc, 0xa4, 0x7a, 0x0e, 0xdd, 0xe6, 0x15, 0x8c, 0x73, 0x96, 0xa2, 0xd4, 0xa6, 0x4d}|VOID*|0x00010013\r
+    
\ No newline at end of file
index 0a01050a8c285871c175913c60da23a3723b74b1..4143cb2c8cc460c601ccc9f16380d20272a3329d 100644 (file)
 \r
   SecurityPkg/Library/HashLibTpm2/HashLibTpm2.inf\r
 \r
 \r
   SecurityPkg/Library/HashLibTpm2/HashLibTpm2.inf\r
 \r
+  SecurityPkg/Library/DxeRsa2048Sha256GuidedSectionExtractLib/DxeRsa2048Sha256GuidedSectionExtractLib.inf\r
+  SecurityPkg/Library/PeiRsa2048Sha256GuidedSectionExtractLib/PeiRsa2048Sha256GuidedSectionExtractLib.inf\r
+  \r
 [Components.IA32, Components.X64, Components.IPF]\r
 #  SecurityPkg/UserIdentification/PwdCredentialProviderDxe/PwdCredentialProviderDxe.inf\r
 #  SecurityPkg/UserIdentification/UsbCredentialProviderDxe/UsbCredentialProviderDxe.inf\r
 [Components.IA32, Components.X64, Components.IPF]\r
 #  SecurityPkg/UserIdentification/PwdCredentialProviderDxe/PwdCredentialProviderDxe.inf\r
 #  SecurityPkg/UserIdentification/UsbCredentialProviderDxe/UsbCredentialProviderDxe.inf\r