3 This library registers RSA 2048 SHA 256 guided section handler
4 to parse RSA 2048 SHA 256 encapsulation section and extract raw data.
5 It uses the BaseCryptLib based on OpenSSL to authenticate the signature.
7 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Protocol/Hash.h>
14 #include <Protocol/SecurityPolicy.h>
15 #include <Library/ExtractGuidedSectionLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/PcdLib.h>
21 #include <Guid/WinCertificate.h>
22 #include <Library/BaseCryptLib.h>
23 #include <Library/PerformanceLib.h>
24 #include <Guid/SecurityPkgTokenSpace.h>
27 /// RSA 2048 SHA 256 Guided Section header
30 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
31 EFI_CERT_BLOCK_RSA_2048_SHA256 CertBlockRsa2048Sha256
; ///< RSA 2048-bit Signature
32 } RSA_2048_SHA_256_SECTION_HEADER
;
35 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader
; ///< EFI guided section header
36 EFI_CERT_BLOCK_RSA_2048_SHA256 CertBlockRsa2048Sha256
; ///< RSA 2048-bit Signature
37 } RSA_2048_SHA_256_SECTION2_HEADER
;
40 /// Public Exponent of RSA Key.
42 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
46 GetInfo gets raw data size and attribute of the input guided section.
47 It first checks whether the input guid section is supported.
48 If not, EFI_INVALID_PARAMETER will return.
50 @param InputSection Buffer containing the input GUIDed section to be processed.
51 @param OutputBufferSize The size of OutputBuffer.
52 @param ScratchBufferSize The size of ScratchBuffer.
53 @param SectionAttribute The attribute of the input guided section.
55 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
56 the attribute of the input section are successfully retrieved.
57 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
62 Rsa2048Sha256GuidedSectionGetInfo (
63 IN CONST VOID
*InputSection
,
64 OUT UINT32
*OutputBufferSize
,
65 OUT UINT32
*ScratchBufferSize
,
66 OUT UINT16
*SectionAttribute
69 if (IS_SECTION2 (InputSection
)) {
71 // Check whether the input guid section is recognized.
74 &gEfiCertTypeRsa2048Sha256Guid
,
75 &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
)
78 return EFI_INVALID_PARAMETER
;
82 // Retrieve the size and attribute of the input section data.
84 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->Attributes
;
85 *ScratchBufferSize
= 0;
86 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - sizeof (RSA_2048_SHA_256_SECTION2_HEADER
);
89 // Check whether the input guid section is recognized.
92 &gEfiCertTypeRsa2048Sha256Guid
,
93 &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
)
96 return EFI_INVALID_PARAMETER
;
100 // Retrieve the size and attribute of the input section data.
102 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
;
103 *ScratchBufferSize
= 0;
104 *OutputBufferSize
= SECTION_SIZE (InputSection
) - sizeof (RSA_2048_SHA_256_SECTION_HEADER
);
112 Extraction handler tries to extract raw data from the input guided section.
113 It also does authentication check for RSA 2048 SHA 256 signature in the input guided section.
114 It first checks whether the input guid section is supported.
115 If not, EFI_INVALID_PARAMETER will return.
117 @param InputSection Buffer containing the input GUIDed section to be processed.
118 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
119 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
120 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
121 authentication status of the output buffer.
123 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
124 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
129 Rsa2048Sha256GuidedSectionHandler (
130 IN CONST VOID
*InputSection
,
131 OUT VOID
**OutputBuffer
,
132 IN VOID
*ScratchBuffer OPTIONAL
,
133 OUT UINT32
*AuthenticationStatus
137 UINT32 OutputBufferSize
;
138 VOID
*DummyInterface
;
139 EFI_CERT_BLOCK_RSA_2048_SHA256
*CertBlockRsa2048Sha256
;
140 BOOLEAN CryptoStatus
;
141 UINT8 Digest
[SHA256_DIGEST_SIZE
];
143 UINTN PublicKeyBufferSize
;
150 if (IS_SECTION2 (InputSection
)) {
152 // Check whether the input guid section is recognized.
155 &gEfiCertTypeRsa2048Sha256Guid
,
156 &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
)
159 return EFI_INVALID_PARAMETER
;
163 // Get the RSA 2048 SHA 256 information.
165 CertBlockRsa2048Sha256
= &((RSA_2048_SHA_256_SECTION2_HEADER
*)InputSection
)->CertBlockRsa2048Sha256
;
166 OutputBufferSize
= SECTION2_SIZE (InputSection
) - sizeof (RSA_2048_SHA_256_SECTION2_HEADER
);
167 if ((((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) {
168 PERF_INMODULE_BEGIN ("DxeRsaCopy");
169 CopyMem (*OutputBuffer
, (UINT8
*)InputSection
+ sizeof (RSA_2048_SHA_256_SECTION2_HEADER
), OutputBufferSize
);
170 PERF_INMODULE_END ("DxeRsaCopy");
172 *OutputBuffer
= (UINT8
*)InputSection
+ sizeof (RSA_2048_SHA_256_SECTION2_HEADER
);
176 // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set
178 ASSERT ((((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) != 0);
179 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
182 // Check whether the input guid section is recognized.
185 &gEfiCertTypeRsa2048Sha256Guid
,
186 &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
)
189 return EFI_INVALID_PARAMETER
;
193 // Get the RSA 2048 SHA 256 information.
195 CertBlockRsa2048Sha256
= &((RSA_2048_SHA_256_SECTION_HEADER
*)InputSection
)->CertBlockRsa2048Sha256
;
196 OutputBufferSize
= SECTION_SIZE (InputSection
) - sizeof (RSA_2048_SHA_256_SECTION_HEADER
);
197 if ((((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) {
198 PERF_INMODULE_BEGIN ("DxeRsaCopy");
199 CopyMem (*OutputBuffer
, (UINT8
*)InputSection
+ sizeof (RSA_2048_SHA_256_SECTION_HEADER
), OutputBufferSize
);
200 PERF_INMODULE_END ("DxeRsaCopy");
202 *OutputBuffer
= (UINT8
*)InputSection
+ sizeof (RSA_2048_SHA_256_SECTION_HEADER
);
206 // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set
208 ASSERT ((((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) != 0);
209 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
213 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
215 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
216 if (!EFI_ERROR (Status
)) {
218 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
220 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
226 // All paths from here return EFI_SUCCESS and result is returned in AuthenticationStatus
228 Status
= EFI_SUCCESS
;
231 // Fail if the HashType is not SHA 256
233 if (!CompareGuid (&gEfiHashAlgorithmSha256Guid
, &CertBlockRsa2048Sha256
->HashType
)) {
234 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: HASH type of section is not supported\n"));
235 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
240 // Allocate hash context buffer required for SHA 256
242 HashContext
= AllocatePool (Sha256GetContextSize ());
243 if (HashContext
== NULL
) {
244 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Can not allocate hash context\n"));
245 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
250 // Hash public key from data payload with SHA256.
252 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
253 CryptoStatus
= Sha256Init (HashContext
);
255 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Init() failed\n"));
256 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
260 CryptoStatus
= Sha256Update (HashContext
, &CertBlockRsa2048Sha256
->PublicKey
, sizeof (CertBlockRsa2048Sha256
->PublicKey
));
262 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Update() failed\n"));
263 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
267 CryptoStatus
= Sha256Final (HashContext
, Digest
);
269 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Final() failed\n"));
270 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
275 // Fail if the PublicKey is not one of the public keys in PcdRsa2048Sha256PublicKeyBuffer
277 PublicKey
= (UINT8
*)PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer
);
278 DEBUG ((DEBUG_VERBOSE
, "DxePcdRsa2048Sha256: PublicKeyBuffer = %p\n", PublicKey
));
279 ASSERT (PublicKey
!= NULL
);
280 DEBUG ((DEBUG_VERBOSE
, "DxePcdRsa2048Sha256: PublicKeyBuffer Token = %08x\n", PcdToken (PcdRsa2048Sha256PublicKeyBuffer
)));
281 PublicKeyBufferSize
= PcdGetSize (PcdRsa2048Sha256PublicKeyBuffer
);
282 DEBUG ((DEBUG_VERBOSE
, "DxePcdRsa2048Sha256: PublicKeyBuffer Size = %08x\n", PublicKeyBufferSize
));
283 ASSERT ((PublicKeyBufferSize
% SHA256_DIGEST_SIZE
) == 0);
284 CryptoStatus
= FALSE
;
285 while (PublicKeyBufferSize
!= 0) {
286 if (CompareMem (Digest
, PublicKey
, SHA256_DIGEST_SIZE
) == 0) {
291 PublicKey
= PublicKey
+ SHA256_DIGEST_SIZE
;
292 PublicKeyBufferSize
= PublicKeyBufferSize
- SHA256_DIGEST_SIZE
;
296 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Public key in section is not supported\n"));
297 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
302 // Generate & Initialize RSA Context.
306 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: RsaNew() failed\n"));
307 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
312 // Set RSA Key Components.
313 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
315 CryptoStatus
= RsaSetKey (Rsa
, RsaKeyN
, CertBlockRsa2048Sha256
->PublicKey
, sizeof (CertBlockRsa2048Sha256
->PublicKey
));
317 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));
318 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
322 CryptoStatus
= RsaSetKey (Rsa
, RsaKeyE
, mRsaE
, sizeof (mRsaE
));
324 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));
325 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
330 // Hash data payload with SHA256.
332 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
333 CryptoStatus
= Sha256Init (HashContext
);
335 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Init() failed\n"));
336 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
340 PERF_INMODULE_BEGIN ("DxeRsaShaData");
341 CryptoStatus
= Sha256Update (HashContext
, *OutputBuffer
, OutputBufferSize
);
342 PERF_INMODULE_END ("DxeRsaShaData");
344 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Update() failed\n"));
345 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
349 CryptoStatus
= Sha256Final (HashContext
, Digest
);
351 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: Sha256Final() failed\n"));
352 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
357 // Verify the RSA 2048 SHA 256 signature.
359 PERF_INMODULE_BEGIN ("DxeRsaVerify");
360 CryptoStatus
= RsaPkcs1Verify (
364 CertBlockRsa2048Sha256
->Signature
,
365 sizeof (CertBlockRsa2048Sha256
->Signature
)
367 PERF_INMODULE_END ("DxeRsaVerify");
370 // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.
372 DEBUG ((DEBUG_ERROR
, "DxeRsa2048Sha256: RsaPkcs1Verify() failed\n"));
373 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
378 // Free allocated resources used to perform RSA 2048 SHA 256 signature verification
384 if (HashContext
!= NULL
) {
385 FreePool (HashContext
);
388 DEBUG ((DEBUG_VERBOSE
, "DxeRsa2048Sha256: Status = %r AuthenticationStatus = %08x\n", Status
, *AuthenticationStatus
));
394 Register the handler to extract RSA 2048 SHA 256 guided section.
396 @param ImageHandle ImageHandle of the loaded driver.
397 @param SystemTable Pointer to the EFI System Table.
399 @retval EFI_SUCCESS Register successfully.
400 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
404 DxeRsa2048Sha256GuidedSectionExtractLibConstructor (
405 IN EFI_HANDLE ImageHandle
,
406 IN EFI_SYSTEM_TABLE
*SystemTable
409 return ExtractGuidedSectionRegisterHandlers (
410 &gEfiCertTypeRsa2048Sha256Guid
,
411 Rsa2048Sha256GuidedSectionGetInfo
,
412 Rsa2048Sha256GuidedSectionHandler