PeiRsa2048Sha256GuidedSectionExtractLib.c 14 KB

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