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