From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 579EA1A1EC4 for ; Tue, 20 Sep 2016 23:46:46 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga105.jf.intel.com with ESMTP; 20 Sep 2016 23:46:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,372,1470726000"; d="scan'208";a="1059859009" Received: from jyao1-mobl.ccr.corp.intel.com ([10.239.192.109]) by fmsmga002.fm.intel.com with ESMTP; 20 Sep 2016 23:46:44 -0700 From: Jiewen Yao To: edk2-devel@lists.01.org Cc: Feng Tian , Star Zeng , Michael D Kinney , Liming Gao , Chao Zhang Date: Wed, 21 Sep 2016 14:45:03 +0800 Message-Id: <1474440326-9292-23-git-send-email-jiewen.yao@intel.com> X-Mailer: git-send-email 2.7.4.windows.1 In-Reply-To: <1474440326-9292-1-git-send-email-jiewen.yao@intel.com> References: <1474440326-9292-1-git-send-email-jiewen.yao@intel.com> Subject: [PATCH 22/45] SecurityPkg/FmpAuthenticationRsa2048Sha256Lib: Add NULL class for FMP. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Sep 2016 06:46:46 -0000 This is a NULL class for FmpAuthenticationLib. It provides Rsa2048Sha256 based FMP authentication. Cc: Feng Tian Cc: Star Zeng Cc: Michael D Kinney Cc: Liming Gao Cc: Chao Zhang Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jiewen Yao --- SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.c | 286 ++++++++++++++++++++ SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.inf | 53 ++++ SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.uni | 26 ++ 3 files changed, 365 insertions(+) diff --git a/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.c b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.c new file mode 100644 index 0000000..3a9c75f --- /dev/null +++ b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.c @@ -0,0 +1,286 @@ +/** @file + FMP Authentication RSA2048SHA256 handler. + + Caution: This module requires additional review when modified. + This module will have external input - capsule image. + This external input must be validated carefully to avoid security issue like + buffer overflow, integer overflow. + + FmpAuthenticatedHandlerRsa2048Sha256() will receive untrusted input and do basic + validation. + + Copyright (c) 2016, 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 + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +/// +/// Public Exponent of RSA Key. +/// +STATIC CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 }; + +STATIC UINT8 *mPublicKey; +STATIC UINTN mPublicKeyBufferSize; + +/** + The handler is used to do the authentication for FMP capsule based upon + EFI_FIRMWARE_IMAGE_AUTHENTICATION. + + Caution: This function may receive untrusted input. + + @param[in] Image Points to the new FMP authentication image, + start from EFI_FIRMWARE_IMAGE_AUTHENTICATION. + @param[in] ImageSize Size of the authentication image in bytes. + @param[out] LastAttemptStatus The last attempt status, which will be recorded + in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR. + + @retval RETURN_SUCCESS Authentication pass. + @retval RETURN_SECURITY_VIOLATION Authentication fail. + The detail reson is recorded in LastAttemptStatus. +**/ +RETURN_STATUS +EFIAPI +FmpAuthenticatedHandlerRsa2048Sha256( + IN VOID *Image, + IN UINTN ImageSize, + OUT UINT32 *LastAttemptStatus + ) +{ + RETURN_STATUS Status; + EFI_FIRMWARE_IMAGE_AUTHENTICATION *ImageAuthentication; + EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlockRsa2048Sha256; + BOOLEAN CryptoStatus; + UINT8 Digest[SHA256_DIGEST_SIZE]; + UINT8 *PublicKey; + UINTN PublicKeyBufferSize; + VOID *HashContext; + VOID *Rsa; + UINTN ImageOffset; + UINT64 MonotonicCount; + UINT64 Backup; + + DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256 - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize)); + + ImageAuthentication = (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image; + MonotonicCount = ImageAuthentication->MonotonicCount; + if (ImageAuthentication->AuthInfo.Hdr.dwLength != sizeof(WIN_CERTIFICATE) + sizeof(EFI_GUID) + sizeof(EFI_CERT_BLOCK_RSA_2048_SHA256)) { + DEBUG((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256 - dwLength: 0x%04x, dwLength - 0x%04x\n", (UINTN)ImageAuthentication->AuthInfo.Hdr.dwLength, (UINTN)sizeof(WIN_CERTIFICATE) + sizeof(EFI_GUID) + sizeof(EFI_CERT_BLOCK_RSA_2048_SHA256))); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT; + return RETURN_SECURITY_VIOLATION; + } + ImageOffset = ImageAuthentication->AuthInfo.Hdr.dwLength; + + CertBlockRsa2048Sha256 = (EFI_CERT_BLOCK_RSA_2048_SHA256 *)ImageAuthentication->AuthInfo.CertData; + if (!CompareGuid(&CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid)) { + DEBUG((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256 - HashType: %g, expect - %g\n", &CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid)); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT; + return RETURN_SECURITY_VIOLATION; + } + + HashContext = NULL; + Rsa = NULL; + + // + // Allocate hash context buffer required for SHA 256 + // + HashContext = AllocatePool (Sha256GetContextSize ()); + if (HashContext == NULL) { + CryptoStatus = FALSE; + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Can not allocate hash context\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + + // + // Hash public key from data payload with SHA256. + // + ZeroMem (Digest, SHA256_DIGEST_SIZE); + CryptoStatus = Sha256Init (HashContext); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Init() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey)); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + CryptoStatus = Sha256Final (HashContext, Digest); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + + // + // Fail if the PublicKey is not one of the public keys in PcdRsa2048Sha256PublicKeyBuffer + // + PublicKey = mPublicKey; + PublicKeyBufferSize = mPublicKeyBufferSize; + CryptoStatus = FALSE; + while (PublicKeyBufferSize != 0) { + if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) { + CryptoStatus = TRUE; + break; + } + PublicKey = PublicKey + SHA256_DIGEST_SIZE; + PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE; + } + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Public key in section is not supported\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR; + Status = RETURN_SECURITY_VIOLATION; + goto Done; + } + + // + // Generate & Initialize RSA Context. + // + Rsa = RsaNew (); + if (Rsa == NULL) { + CryptoStatus = FALSE; + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaNew() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + goto Done; + } + + // + // Set RSA Key Components. + // NOTE: Only N and E are needed to be set as RSA public key for signature verification. + // + CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey)); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE)); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + + // + // Hash data payload with SHA256. + // + ZeroMem (Digest, SHA256_DIGEST_SIZE); + CryptoStatus = Sha256Init (HashContext); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Init() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + // It is a signature across the variable data and the Monotonic Count value. + CopyMem(&Backup, (UINT8 *)Image + ImageOffset, sizeof(Backup)); + CopyMem((UINT8 *)Image + ImageOffset, &MonotonicCount, sizeof(MonotonicCount)); + CryptoStatus = Sha256Update (HashContext, (UINT8 *)Image + ImageOffset, ImageSize - ImageOffset); + CopyMem((UINT8 *)Image + ImageOffset, &Backup, sizeof(Backup)); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + CryptoStatus = Sha256Final (HashContext, Digest); + if (!CryptoStatus) { + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } + + // + // Verify the RSA 2048 SHA 256 signature. + // + CryptoStatus = RsaPkcs1Verify ( + Rsa, + Digest, + SHA256_DIGEST_SIZE, + CertBlockRsa2048Sha256->Signature, + sizeof (CertBlockRsa2048Sha256->Signature) + ); + if (!CryptoStatus) { + // + // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set. + // + DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaPkcs1Verify() failed\n")); + *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR; + Status = RETURN_SECURITY_VIOLATION; + goto Done; + } + DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256: PASS verification\n")); + + *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS; + Status = RETURN_SUCCESS; + +Done: + // + // Free allocated resources used to perform RSA 2048 SHA 256 signature verification + // + if (Rsa != NULL) { + RsaFree (Rsa); + } + if (HashContext != NULL) { + FreePool (HashContext); + } + + return Status; +} + +/** + The constructor function to register FMP authentication handler. + + @retval RETURN_SUCCESS The constructor successfully . +**/ +RETURN_STATUS +EFIAPI +FmpAuthenticationRsa2048Sha256LibConstructor( + VOID + ) +{ + RETURN_STATUS Status; + + mPublicKey = (UINT8 *)PcdGetPtr(PcdRsa2048Sha256PublicKeyBuffer); + ASSERT(mPublicKey != NULL); + mPublicKeyBufferSize = PcdGetSize(PcdRsa2048Sha256PublicKeyBuffer); + ASSERT((mPublicKeyBufferSize % SHA256_DIGEST_SIZE) == 0); + mPublicKey = AllocateCopyPool(mPublicKeyBufferSize, mPublicKey); + ASSERT(mPublicKey != NULL); + + Status = RegisterFmpAuthenticationHandler(&gEfiCertTypeRsa2048Sha256Guid, FmpAuthenticatedHandlerRsa2048Sha256); + ASSERT_EFI_ERROR(Status); + + return RETURN_SUCCESS; +} diff --git a/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.inf b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.inf new file mode 100644 index 0000000..acae202 --- /dev/null +++ b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.inf @@ -0,0 +1,53 @@ +## @file +# FMP Authentication RSA2048SHA256 handler. +# +# Copyright (c) 2016, 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 +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = FmpAuthenticationRsa2048Sha256Lib + MODULE_UNI_FILE = FmpAuthenticationRsa2048Sha256Lib.uni + FILE_GUID = 105FF0EA-A0C0-48A8-B8F7-E8B4D62A1019 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = NULL + CONSTRUCTOR = FmpAuthenticationRsa2048Sha256LibConstructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# + +[Sources] + FmpAuthenticationRsa2048Sha256Lib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + SecurityPkg/SecurityPkg.dec + CryptoPkg/CryptoPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + BaseCryptLib + FmpAuthenticationLib + +[Pcd] + gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer + +[Guids] + gEfiCertTypeRsa2048Sha256Guid + gEfiHashAlgorithmSha256Guid diff --git a/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.uni b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.uni new file mode 100644 index 0000000..902edef --- /dev/null +++ b/SecurityPkg/Library/FmpAuthenticationRsa2048Sha256Lib/FmpAuthenticationRsa2048Sha256Lib.uni @@ -0,0 +1,26 @@ +// /** @file +// FMP Authentication RSA2048SHA256 handler. +// +// This library provide FMP Authentication RSA2048SHA256 handler to verify EFI_FIRMWARE_IMAGE_AUTHENTICATION. +// +// Caution: This module requires additional review when modified. +// This library will have external input - capsule image. +// This external input must be validated carefully to avoid security issues such as +// buffer overflow or integer overflow. +// +// Copyright (c) 2016, 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 +// http://opensource.org/licenses/bsd-license.php +// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "FMP Authentication RSA2048SHA256 handler." + +#string STR_MODULE_DESCRIPTION #language en-US "This library provide FMP Authentication RSA2048SHA256 handler to verify EFI_FIRMWARE_IMAGE_AUTHENTICATION." + -- 2.7.4.windows.1