DxeRsa2048Sha256GuidedSectionExtractLib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /** @file
  2. This library registers RSA 2048 SHA 256 guided section handler
  3. to parse RSA 2048 SHA 256 encapsulation section and extract raw data.
  4. It uses the BaseCrypyLib based on OpenSSL to authenticate the signature.
  5. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <PiDxe.h>
  9. #include <Protocol/Hash.h>
  10. #include <Protocol/SecurityPolicy.h>
  11. #include <Library/ExtractGuidedSectionLib.h>
  12. #include <Library/DebugLib.h>
  13. #include <Library/BaseMemoryLib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include <Library/UefiBootServicesTableLib.h>
  16. #include <Library/PcdLib.h>
  17. #include <Guid/WinCertificate.h>
  18. #include <Library/BaseCryptLib.h>
  19. #include <Library/PerformanceLib.h>
  20. #include <Guid/SecurityPkgTokenSpace.h>
  21. ///
  22. /// RSA 2048 SHA 256 Guided Section header
  23. ///
  24. typedef struct {
  25. EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header
  26. EFI_CERT_BLOCK_RSA_2048_SHA256 CertBlockRsa2048Sha256; ///< RSA 2048-bit Signature
  27. } RSA_2048_SHA_256_SECTION_HEADER;
  28. typedef struct {
  29. EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header
  30. EFI_CERT_BLOCK_RSA_2048_SHA256 CertBlockRsa2048Sha256; ///< RSA 2048-bit Signature
  31. } RSA_2048_SHA_256_SECTION2_HEADER;
  32. ///
  33. /// Public Exponent of RSA Key.
  34. ///
  35. CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };
  36. /**
  37. GetInfo gets raw data size and attribute of the input guided section.
  38. It first checks whether the input guid section is supported.
  39. If not, EFI_INVALID_PARAMETER will return.
  40. @param InputSection Buffer containing the input GUIDed section to be processed.
  41. @param OutputBufferSize The size of OutputBuffer.
  42. @param ScratchBufferSize The size of ScratchBuffer.
  43. @param SectionAttribute The attribute of the input guided section.
  44. @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
  45. the attribute of the input section are successfully retrieved.
  46. @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
  47. **/
  48. EFI_STATUS
  49. EFIAPI
  50. Rsa2048Sha256GuidedSectionGetInfo (
  51. IN CONST VOID *InputSection,
  52. OUT UINT32 *OutputBufferSize,
  53. OUT UINT32 *ScratchBufferSize,
  54. OUT UINT16 *SectionAttribute
  55. )
  56. {
  57. if (IS_SECTION2 (InputSection)) {
  58. //
  59. // Check whether the input guid section is recognized.
  60. //
  61. if (!CompareGuid (
  62. &gEfiCertTypeRsa2048Sha256Guid,
  63. &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
  64. return EFI_INVALID_PARAMETER;
  65. }
  66. //
  67. // Retrieve the size and attribute of the input section data.
  68. //
  69. *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
  70. *ScratchBufferSize = 0;
  71. *OutputBufferSize = SECTION2_SIZE (InputSection) - sizeof(RSA_2048_SHA_256_SECTION2_HEADER);
  72. } else {
  73. //
  74. // Check whether the input guid section is recognized.
  75. //
  76. if (!CompareGuid (
  77. &gEfiCertTypeRsa2048Sha256Guid,
  78. &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
  79. return EFI_INVALID_PARAMETER;
  80. }
  81. //
  82. // Retrieve the size and attribute of the input section data.
  83. //
  84. *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
  85. *ScratchBufferSize = 0;
  86. *OutputBufferSize = SECTION_SIZE (InputSection) - sizeof(RSA_2048_SHA_256_SECTION_HEADER);
  87. }
  88. return EFI_SUCCESS;
  89. }
  90. /**
  91. Extraction handler tries to extract raw data from the input guided section.
  92. It also does authentication check for RSA 2048 SHA 256 signature in the input guided section.
  93. It first checks whether the input guid section is supported.
  94. If not, EFI_INVALID_PARAMETER will return.
  95. @param InputSection Buffer containing the input GUIDed section to be processed.
  96. @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
  97. @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
  98. @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
  99. authentication status of the output buffer.
  100. @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
  101. @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
  102. **/
  103. EFI_STATUS
  104. EFIAPI
  105. Rsa2048Sha256GuidedSectionHandler (
  106. IN CONST VOID *InputSection,
  107. OUT VOID **OutputBuffer,
  108. IN VOID *ScratchBuffer, OPTIONAL
  109. OUT UINT32 *AuthenticationStatus
  110. )
  111. {
  112. EFI_STATUS Status;
  113. UINT32 OutputBufferSize;
  114. VOID *DummyInterface;
  115. EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlockRsa2048Sha256;
  116. BOOLEAN CryptoStatus;
  117. UINT8 Digest[SHA256_DIGEST_SIZE];
  118. UINT8 *PublicKey;
  119. UINTN PublicKeyBufferSize;
  120. VOID *HashContext;
  121. VOID *Rsa;
  122. HashContext = NULL;
  123. Rsa = NULL;
  124. if (IS_SECTION2 (InputSection)) {
  125. //
  126. // Check whether the input guid section is recognized.
  127. //
  128. if (!CompareGuid (
  129. &gEfiCertTypeRsa2048Sha256Guid,
  130. &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid))) {
  131. return EFI_INVALID_PARAMETER;
  132. }
  133. //
  134. // Get the RSA 2048 SHA 256 information.
  135. //
  136. CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION2_HEADER *) InputSection)->CertBlockRsa2048Sha256;
  137. OutputBufferSize = SECTION2_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION2_HEADER);
  138. if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {
  139. PERF_INMODULE_BEGIN ("DxeRsaCopy");
  140. CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER), OutputBufferSize);
  141. PERF_INMODULE_END ("DxeRsaCopy");
  142. } else {
  143. *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION2_HEADER);
  144. }
  145. //
  146. // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set
  147. //
  148. ASSERT ((((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);
  149. *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
  150. } else {
  151. //
  152. // Check whether the input guid section is recognized.
  153. //
  154. if (!CompareGuid (
  155. &gEfiCertTypeRsa2048Sha256Guid,
  156. &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid))) {
  157. return EFI_INVALID_PARAMETER;
  158. }
  159. //
  160. // Get the RSA 2048 SHA 256 information.
  161. //
  162. CertBlockRsa2048Sha256 = &((RSA_2048_SHA_256_SECTION_HEADER *)InputSection)->CertBlockRsa2048Sha256;
  163. OutputBufferSize = SECTION_SIZE (InputSection) - sizeof (RSA_2048_SHA_256_SECTION_HEADER);
  164. if ((((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {
  165. PERF_INMODULE_BEGIN ("DxeRsaCopy");
  166. CopyMem (*OutputBuffer, (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER), OutputBufferSize);
  167. PERF_INMODULE_END ("DxeRsaCopy");
  168. } else {
  169. *OutputBuffer = (UINT8 *)InputSection + sizeof (RSA_2048_SHA_256_SECTION_HEADER);
  170. }
  171. //
  172. // Implicitly RSA 2048 SHA 256 GUIDed section should have STATUS_VALID bit set
  173. //
  174. ASSERT ((((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0);
  175. *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
  176. }
  177. //
  178. // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
  179. //
  180. Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
  181. if (!EFI_ERROR (Status)) {
  182. //
  183. // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
  184. //
  185. *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
  186. return EFI_SUCCESS;
  187. }
  188. //
  189. // All paths from here return EFI_SUCESS and result is returned in AuthenticationStatus
  190. //
  191. Status = EFI_SUCCESS;
  192. //
  193. // Fail if the HashType is not SHA 256
  194. //
  195. if (!CompareGuid (&gEfiHashAlgorithmSha256Guid, &CertBlockRsa2048Sha256->HashType)) {
  196. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: HASH type of section is not supported\n"));
  197. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  198. goto Done;
  199. }
  200. //
  201. // Allocate hash context buffer required for SHA 256
  202. //
  203. HashContext = AllocatePool (Sha256GetContextSize ());
  204. if (HashContext == NULL) {
  205. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Can not allocate hash context\n"));
  206. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  207. goto Done;
  208. }
  209. //
  210. // Hash public key from data payload with SHA256.
  211. //
  212. ZeroMem (Digest, SHA256_DIGEST_SIZE);
  213. CryptoStatus = Sha256Init (HashContext);
  214. if (!CryptoStatus) {
  215. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Init() failed\n"));
  216. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  217. goto Done;
  218. }
  219. CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));
  220. if (!CryptoStatus) {
  221. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Update() failed\n"));
  222. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  223. goto Done;
  224. }
  225. CryptoStatus = Sha256Final (HashContext, Digest);
  226. if (!CryptoStatus) {
  227. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Final() failed\n"));
  228. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  229. goto Done;
  230. }
  231. //
  232. // Fail if the PublicKey is not one of the public keys in PcdRsa2048Sha256PublicKeyBuffer
  233. //
  234. PublicKey = (UINT8 *)PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer);
  235. DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer = %p\n", PublicKey));
  236. ASSERT (PublicKey != NULL);
  237. DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer Token = %08x\n", PcdToken (PcdRsa2048Sha256PublicKeyBuffer)));
  238. PublicKeyBufferSize = PcdGetSize (PcdRsa2048Sha256PublicKeyBuffer);
  239. DEBUG ((DEBUG_VERBOSE, "DxePcdRsa2048Sha256: PublicKeyBuffer Size = %08x\n", PublicKeyBufferSize));
  240. ASSERT ((PublicKeyBufferSize % SHA256_DIGEST_SIZE) == 0);
  241. CryptoStatus = FALSE;
  242. while (PublicKeyBufferSize != 0) {
  243. if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) {
  244. CryptoStatus = TRUE;
  245. break;
  246. }
  247. PublicKey = PublicKey + SHA256_DIGEST_SIZE;
  248. PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE;
  249. }
  250. if (!CryptoStatus) {
  251. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Public key in section is not supported\n"));
  252. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  253. goto Done;
  254. }
  255. //
  256. // Generate & Initialize RSA Context.
  257. //
  258. Rsa = RsaNew ();
  259. if (Rsa == NULL) {
  260. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaNew() failed\n"));
  261. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  262. goto Done;
  263. }
  264. //
  265. // Set RSA Key Components.
  266. // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
  267. //
  268. CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));
  269. if (!CryptoStatus) {
  270. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));
  271. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  272. goto Done;
  273. }
  274. CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));
  275. if (!CryptoStatus) {
  276. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));
  277. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  278. goto Done;
  279. }
  280. //
  281. // Hash data payload with SHA256.
  282. //
  283. ZeroMem (Digest, SHA256_DIGEST_SIZE);
  284. CryptoStatus = Sha256Init (HashContext);
  285. if (!CryptoStatus) {
  286. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Init() failed\n"));
  287. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  288. goto Done;
  289. }
  290. PERF_INMODULE_BEGIN ("DxeRsaShaData");
  291. CryptoStatus = Sha256Update (HashContext, *OutputBuffer, OutputBufferSize);
  292. PERF_INMODULE_END ("DxeRsaShaData");
  293. if (!CryptoStatus) {
  294. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Update() failed\n"));
  295. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  296. goto Done;
  297. }
  298. CryptoStatus = Sha256Final (HashContext, Digest);
  299. if (!CryptoStatus) {
  300. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: Sha256Final() failed\n"));
  301. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  302. goto Done;
  303. }
  304. //
  305. // Verify the RSA 2048 SHA 256 signature.
  306. //
  307. PERF_INMODULE_BEGIN ("DxeRsaVerify");
  308. CryptoStatus = RsaPkcs1Verify (
  309. Rsa,
  310. Digest,
  311. SHA256_DIGEST_SIZE,
  312. CertBlockRsa2048Sha256->Signature,
  313. sizeof (CertBlockRsa2048Sha256->Signature)
  314. );
  315. PERF_INMODULE_END ("DxeRsaVerify");
  316. if (!CryptoStatus) {
  317. //
  318. // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.
  319. //
  320. DEBUG ((DEBUG_ERROR, "DxeRsa2048Sha256: RsaPkcs1Verify() failed\n"));
  321. *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
  322. }
  323. Done:
  324. //
  325. // Free allocated resources used to perform RSA 2048 SHA 256 signature verification
  326. //
  327. if (Rsa != NULL) {
  328. RsaFree (Rsa);
  329. }
  330. if (HashContext != NULL) {
  331. FreePool (HashContext);
  332. }
  333. DEBUG ((DEBUG_VERBOSE, "DxeRsa2048Sha256: Status = %r AuthenticationStatus = %08x\n", Status, *AuthenticationStatus));
  334. return Status;
  335. }
  336. /**
  337. Register the handler to extract RSA 2048 SHA 256 guided section.
  338. @param ImageHandle ImageHandle of the loaded driver.
  339. @param SystemTable Pointer to the EFI System Table.
  340. @retval EFI_SUCCESS Register successfully.
  341. @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
  342. **/
  343. EFI_STATUS
  344. EFIAPI
  345. DxeRsa2048Sha256GuidedSectionExtractLibConstructor (
  346. IN EFI_HANDLE ImageHandle,
  347. IN EFI_SYSTEM_TABLE *SystemTable
  348. )
  349. {
  350. return ExtractGuidedSectionRegisterHandlers (
  351. &gEfiCertTypeRsa2048Sha256Guid,
  352. Rsa2048Sha256GuidedSectionGetInfo,
  353. Rsa2048Sha256GuidedSectionHandler
  354. );
  355. }