]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
SecurityPkg Tpm2DeviceLibDTpm: Update enum type name to match the one in lib
[mirror_edk2.git] / SecurityPkg / Library / DxeImageVerificationLib / DxeImageVerificationLib.c
CommitLineData
0c18794e 1/** @file\r
3cd2484e 2 Implement image verification services for secure boot service\r
0c18794e 3\r
dc204d5a
JY
4 Caution: This file requires additional review when modified.\r
5 This library will have external input - PE/COFF image.\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8\r
9 DxeImageVerificationLibImageRead() function will make sure the PE/COFF image content\r
10 read is within the image buffer.\r
11\r
12 DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept\r
13 untrusted PE/COFF image and validate its data structure within this image buffer before use.\r
14\r
5b196b06 15Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
531c89a1 16(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
45bf2c47 17This program and the accompanying materials\r
18are licensed and made available under the terms and conditions of the BSD License\r
19which accompanies this distribution. The full text of the license may be found at\r
0c18794e 20http://opensource.org/licenses/bsd-license.php\r
21\r
45bf2c47 22THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
0c18794e 23WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
24\r
25**/\r
26\r
27#include "DxeImageVerificationLib.h"\r
28\r
dc204d5a
JY
29//\r
30// Caution: This is used by a function which may receive untrusted input.\r
31// These global variables hold PE/COFF image data, and they should be validated before use.\r
32//\r
0c18794e 33EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader;\r
45bf2c47 34UINT32 mPeCoffHeaderOffset;\r
0c18794e 35EFI_GUID mCertType;\r
36\r
dc204d5a
JY
37//\r
38// Information on current PE/COFF image\r
39//\r
40UINTN mImageSize;\r
41UINT8 *mImageBase = NULL;\r
42UINT8 mImageDigest[MAX_DIGEST_SIZE];\r
43UINTN mImageDigestSize;\r
44\r
0c18794e 45//\r
46// Notify string for authorization UI.\r
47//\r
48CHAR16 mNotifyString1[MAX_NOTIFY_STRING_LEN] = L"Image verification pass but not found in authorized database!";\r
49CHAR16 mNotifyString2[MAX_NOTIFY_STRING_LEN] = L"Launch this image anyway? (Yes/Defer/No)";\r
50//\r
51// Public Exponent of RSA Key.\r
52//\r
53CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
54\r
55\r
56//\r
57// OID ASN.1 Value for Hash Algorithms\r
58//\r
59UINT8 mHashOidValue[] = {\r
0c18794e 60 0x2B, 0x0E, 0x03, 0x02, 0x1A, // OBJ_sha1\r
61 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, // OBJ_sha224\r
62 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, // OBJ_sha256\r
63 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, // OBJ_sha384\r
64 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, // OBJ_sha512\r
65 };\r
66\r
67HASH_TABLE mHash[] = {\r
20333c6d
QL
68 { L"SHA1", 20, &mHashOidValue[0], 5, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1Final },\r
69 { L"SHA224", 28, &mHashOidValue[5], 9, NULL, NULL, NULL, NULL },\r
70 { L"SHA256", 32, &mHashOidValue[14], 9, Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final},\r
71 { L"SHA384", 48, &mHashOidValue[23], 9, Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final},\r
72 { L"SHA512", 64, &mHashOidValue[32], 9, Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final}\r
0c18794e 73};\r
74\r
531c89a1
CS
75EFI_STRING mHashTypeStr;\r
76\r
c1d93242
JY
77/**\r
78 SecureBoot Hook for processing image verification.\r
79\r
80 @param[in] VariableName Name of Variable to be found.\r
81 @param[in] VendorGuid Variable vendor GUID.\r
82 @param[in] DataSize Size of Data found. If size is less than the\r
83 data, this value contains the required size.\r
84 @param[in] Data Data pointer.\r
85\r
86**/\r
87VOID\r
88EFIAPI\r
89SecureBootHook (\r
90 IN CHAR16 *VariableName,\r
91 IN EFI_GUID *VendorGuid,\r
92 IN UINTN DataSize,\r
93 IN VOID *Data\r
94 );\r
95\r
28186d45
ED
96/**\r
97 Reads contents of a PE/COFF image in memory buffer.\r
98\r
dc204d5a
JY
99 Caution: This function may receive untrusted input.\r
100 PE/COFF image is external input, so this function will make sure the PE/COFF image content\r
101 read is within the image buffer.\r
102\r
28186d45
ED
103 @param FileHandle Pointer to the file handle to read the PE/COFF image.\r
104 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
20333c6d 105 @param ReadSize On input, the size in bytes of the requested read operation.\r
28186d45
ED
106 On output, the number of bytes actually read.\r
107 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
20333c6d
QL
108\r
109 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size\r
28186d45
ED
110**/\r
111EFI_STATUS\r
112EFIAPI\r
e0192326 113DxeImageVerificationLibImageRead (\r
28186d45
ED
114 IN VOID *FileHandle,\r
115 IN UINTN FileOffset,\r
116 IN OUT UINTN *ReadSize,\r
117 OUT VOID *Buffer\r
118 )\r
119{\r
120 UINTN EndPosition;\r
121\r
122 if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
20333c6d 123 return EFI_INVALID_PARAMETER;\r
28186d45
ED
124 }\r
125\r
126 if (MAX_ADDRESS - FileOffset < *ReadSize) {\r
127 return EFI_INVALID_PARAMETER;\r
128 }\r
129\r
130 EndPosition = FileOffset + *ReadSize;\r
131 if (EndPosition > mImageSize) {\r
132 *ReadSize = (UINT32)(mImageSize - FileOffset);\r
133 }\r
134\r
135 if (FileOffset >= mImageSize) {\r
136 *ReadSize = 0;\r
137 }\r
138\r
139 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);\r
140\r
141 return EFI_SUCCESS;\r
142}\r
143\r
0c18794e 144\r
145/**\r
146 Get the image type.\r
147\r
148 @param[in] File This is a pointer to the device path of the file that is\r
45bf2c47 149 being dispatched.\r
0c18794e 150\r
45bf2c47 151 @return UINT32 Image Type\r
0c18794e 152\r
153**/\r
154UINT32\r
155GetImageType (\r
156 IN CONST EFI_DEVICE_PATH_PROTOCOL *File\r
157 )\r
158{\r
159 EFI_STATUS Status;\r
45bf2c47 160 EFI_HANDLE DeviceHandle;\r
0c18794e 161 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
162 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
163\r
5db28a67
LG
164 if (File == NULL) {\r
165 return IMAGE_UNKNOWN;\r
166 }\r
167\r
0c18794e 168 //\r
169 // First check to see if File is from a Firmware Volume\r
170 //\r
171 DeviceHandle = NULL;\r
45bf2c47 172 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
0c18794e 173 Status = gBS->LocateDevicePath (\r
174 &gEfiFirmwareVolume2ProtocolGuid,\r
175 &TempDevicePath,\r
176 &DeviceHandle\r
177 );\r
178 if (!EFI_ERROR (Status)) {\r
179 Status = gBS->OpenProtocol (\r
180 DeviceHandle,\r
181 &gEfiFirmwareVolume2ProtocolGuid,\r
182 NULL,\r
183 NULL,\r
184 NULL,\r
185 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
186 );\r
187 if (!EFI_ERROR (Status)) {\r
188 return IMAGE_FROM_FV;\r
189 }\r
190 }\r
191\r
192 //\r
193 // Next check to see if File is from a Block I/O device\r
194 //\r
195 DeviceHandle = NULL;\r
45bf2c47 196 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
0c18794e 197 Status = gBS->LocateDevicePath (\r
198 &gEfiBlockIoProtocolGuid,\r
199 &TempDevicePath,\r
200 &DeviceHandle\r
201 );\r
202 if (!EFI_ERROR (Status)) {\r
203 BlockIo = NULL;\r
204 Status = gBS->OpenProtocol (\r
205 DeviceHandle,\r
206 &gEfiBlockIoProtocolGuid,\r
207 (VOID **) &BlockIo,\r
208 NULL,\r
209 NULL,\r
210 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
211 );\r
212 if (!EFI_ERROR (Status) && BlockIo != NULL) {\r
213 if (BlockIo->Media != NULL) {\r
214 if (BlockIo->Media->RemovableMedia) {\r
215 //\r
216 // Block I/O is present and specifies the media is removable\r
217 //\r
218 return IMAGE_FROM_REMOVABLE_MEDIA;\r
219 } else {\r
220 //\r
221 // Block I/O is present and specifies the media is not removable\r
222 //\r
223 return IMAGE_FROM_FIXED_MEDIA;\r
224 }\r
225 }\r
226 }\r
227 }\r
228\r
229 //\r
45bf2c47 230 // File is not in a Firmware Volume or on a Block I/O device, so check to see if\r
0c18794e 231 // the device path supports the Simple File System Protocol.\r
232 //\r
233 DeviceHandle = NULL;\r
45bf2c47 234 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
0c18794e 235 Status = gBS->LocateDevicePath (\r
236 &gEfiSimpleFileSystemProtocolGuid,\r
237 &TempDevicePath,\r
238 &DeviceHandle\r
239 );\r
240 if (!EFI_ERROR (Status)) {\r
241 //\r
242 // Simple File System is present without Block I/O, so assume media is fixed.\r
243 //\r
244 return IMAGE_FROM_FIXED_MEDIA;\r
245 }\r
246\r
247 //\r
248 // File is not from an FV, Block I/O or Simple File System, so the only options\r
45bf2c47 249 // left are a PCI Option ROM and a Load File Protocol such as a PXE Boot from a NIC.\r
0c18794e 250 //\r
45bf2c47 251 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
0c18794e 252 while (!IsDevicePathEndType (TempDevicePath)) {\r
253 switch (DevicePathType (TempDevicePath)) {\r
45bf2c47 254\r
0c18794e 255 case MEDIA_DEVICE_PATH:\r
256 if (DevicePathSubType (TempDevicePath) == MEDIA_RELATIVE_OFFSET_RANGE_DP) {\r
257 return IMAGE_FROM_OPTION_ROM;\r
258 }\r
259 break;\r
260\r
261 case MESSAGING_DEVICE_PATH:\r
262 if (DevicePathSubType(TempDevicePath) == MSG_MAC_ADDR_DP) {\r
263 return IMAGE_FROM_REMOVABLE_MEDIA;\r
45bf2c47 264 }\r
0c18794e 265 break;\r
266\r
267 default:\r
268 break;\r
269 }\r
270 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
271 }\r
45bf2c47 272 return IMAGE_UNKNOWN;\r
0c18794e 273}\r
274\r
275/**\r
69f8bb52 276 Calculate hash of Pe/Coff image based on the authenticode image hashing in\r
0c18794e 277 PE/COFF Specification 8.0 Appendix A\r
89fb5aef 278 \r
dc204d5a
JY
279 Caution: This function may receive untrusted input.\r
280 PE/COFF image is external input, so this function will validate its data structure\r
281 within this image buffer before use.\r
282\r
89fb5aef
LG
283 Notes: PE/COFF image has been checked by BasePeCoffLib PeCoffLoaderGetImageInfo() in \r
284 its caller function DxeImageVerificationHandler().\r
285\r
0c18794e 286 @param[in] HashAlg Hash algorithm type.\r
45bf2c47 287\r
0c18794e 288 @retval TRUE Successfully hash image.\r
289 @retval FALSE Fail in hash image.\r
290\r
291**/\r
45bf2c47 292BOOLEAN\r
0c18794e 293HashPeImage (\r
294 IN UINT32 HashAlg\r
295 )\r
296{\r
297 BOOLEAN Status;\r
298 UINT16 Magic;\r
299 EFI_IMAGE_SECTION_HEADER *Section;\r
300 VOID *HashCtx;\r
301 UINTN CtxSize;\r
302 UINT8 *HashBase;\r
303 UINTN HashSize;\r
304 UINTN SumOfBytesHashed;\r
305 EFI_IMAGE_SECTION_HEADER *SectionHeader;\r
306 UINTN Index;\r
307 UINTN Pos;\r
551d8081 308 UINT32 CertSize;\r
309 UINT32 NumberOfRvaAndSizes;\r
45bf2c47 310\r
0c18794e 311 HashCtx = NULL;\r
312 SectionHeader = NULL;\r
313 Status = FALSE;\r
314\r
20333c6d 315 if ((HashAlg >= HASHALG_MAX)) {\r
0c18794e 316 return FALSE;\r
317 }\r
45bf2c47 318\r
0c18794e 319 //\r
320 // Initialize context of hash.\r
321 //\r
322 ZeroMem (mImageDigest, MAX_DIGEST_SIZE);\r
323\r
20333c6d
QL
324 switch (HashAlg) {\r
325 case HASHALG_SHA1:\r
326 mImageDigestSize = SHA1_DIGEST_SIZE;\r
327 mCertType = gEfiCertSha1Guid;\r
328 break;\r
329\r
330 case HASHALG_SHA256:\r
331 mImageDigestSize = SHA256_DIGEST_SIZE;\r
332 mCertType = gEfiCertSha256Guid;\r
333 break;\r
334\r
335 case HASHALG_SHA384:\r
336 mImageDigestSize = SHA384_DIGEST_SIZE;\r
337 mCertType = gEfiCertSha384Guid;\r
338 break;\r
339\r
340 case HASHALG_SHA512:\r
341 mImageDigestSize = SHA512_DIGEST_SIZE;\r
342 mCertType = gEfiCertSha512Guid;\r
343 break;\r
344\r
345 default:\r
0c18794e 346 return FALSE;\r
347 }\r
348\r
531c89a1 349 mHashTypeStr = mHash[HashAlg].Name;\r
0c18794e 350 CtxSize = mHash[HashAlg].GetContextSize();\r
45bf2c47 351\r
0c18794e 352 HashCtx = AllocatePool (CtxSize);\r
570b3d1a 353 if (HashCtx == NULL) {\r
354 return FALSE;\r
355 }\r
0c18794e 356\r
357 // 1. Load the image header into memory.\r
358\r
359 // 2. Initialize a SHA hash context.\r
360 Status = mHash[HashAlg].HashInit(HashCtx);\r
45bf2c47 361\r
0c18794e 362 if (!Status) {\r
363 goto Done;\r
364 }\r
551d8081 365\r
0c18794e 366 //\r
367 // Measuring PE/COFF Image Header;\r
368 // But CheckSum field and SECURITY data directory (certificate) are excluded\r
369 //\r
de2447dd 370 if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
371 //\r
20333c6d
QL
372 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
373 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
de2447dd 374 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
375 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
376 //\r
377 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
378 } else {\r
379 //\r
380 // Get the magic value from the PE/COFF Optional Header\r
381 //\r
382 Magic = mNtHeader.Pe32->OptionalHeader.Magic;\r
383 }\r
20333c6d 384\r
0c18794e 385 //\r
386 // 3. Calculate the distance from the base of the image header to the image checksum address.\r
387 // 4. Hash the image header from its base to beginning of the image checksum.\r
388 //\r
389 HashBase = mImageBase;\r
390 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
391 //\r
392 // Use PE32 offset.\r
393 //\r
4333b99d 394 HashSize = (UINTN) (&mNtHeader.Pe32->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
551d8081 395 NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
570b3d1a 396 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
0c18794e 397 //\r
398 // Use PE32+ offset.\r
399 //\r
4333b99d 400 HashSize = (UINTN) (&mNtHeader.Pe32Plus->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
551d8081 401 NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
570b3d1a 402 } else {\r
403 //\r
404 // Invalid header magic number.\r
405 //\r
406 Status = FALSE;\r
407 goto Done;\r
0c18794e 408 }\r
409\r
410 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
411 if (!Status) {\r
412 goto Done;\r
413 }\r
551d8081 414\r
0c18794e 415 //\r
416 // 5. Skip over the image checksum (it occupies a single ULONG).\r
0c18794e 417 //\r
551d8081 418 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
0c18794e 419 //\r
551d8081 420 // 6. Since there is no Cert Directory in optional header, hash everything\r
421 // from the end of the checksum to the end of image header.\r
0c18794e 422 //\r
551d8081 423 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
424 //\r
425 // Use PE32 offset.\r
426 //\r
427 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
4333b99d 428 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - ((UINTN) HashBase - (UINTN) mImageBase);\r
551d8081 429 } else {\r
430 //\r
431 // Use PE32+ offset.\r
432 //\r
433 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
4333b99d 434 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - ((UINTN) HashBase - (UINTN) mImageBase);\r
551d8081 435 }\r
436\r
437 if (HashSize != 0) {\r
438 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
439 if (!Status) {\r
440 goto Done;\r
441 }\r
442 }\r
0c18794e 443 } else {\r
444 //\r
551d8081 445 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.\r
45bf2c47 446 //\r
551d8081 447 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
448 //\r
449 // Use PE32 offset.\r
450 //\r
451 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
4333b99d 452 HashSize = (UINTN) (&mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
551d8081 453 } else {\r
454 //\r
455 // Use PE32+ offset.\r
456 //\r
457 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
4333b99d 458 HashSize = (UINTN) (&mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
551d8081 459 }\r
460\r
461 if (HashSize != 0) {\r
462 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
463 if (!Status) {\r
464 goto Done;\r
465 }\r
466 }\r
0c18794e 467\r
0c18794e 468 //\r
551d8081 469 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)\r
470 // 9. Hash everything from the end of the Cert Directory to the end of image header.\r
0c18794e 471 //\r
551d8081 472 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
473 //\r
474 // Use PE32 offset\r
475 //\r
476 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
4333b99d 477 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - ((UINTN) HashBase - (UINTN) mImageBase);\r
551d8081 478 } else {\r
479 //\r
480 // Use PE32+ offset.\r
481 //\r
482 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
4333b99d 483 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - ((UINTN) HashBase - (UINTN) mImageBase);\r
551d8081 484 }\r
0c18794e 485\r
551d8081 486 if (HashSize != 0) {\r
487 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
488 if (!Status) {\r
489 goto Done;\r
490 }\r
20333c6d 491 }\r
0c18794e 492 }\r
551d8081 493\r
0c18794e 494 //\r
495 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.\r
496 //\r
497 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
498 //\r
499 // Use PE32 offset.\r
500 //\r
501 SumOfBytesHashed = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders;\r
502 } else {\r
503 //\r
504 // Use PE32+ offset\r
505 //\r
506 SumOfBytesHashed = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders;\r
507 }\r
508\r
570b3d1a 509\r
510 Section = (EFI_IMAGE_SECTION_HEADER *) (\r
511 mImageBase +\r
512 mPeCoffHeaderOffset +\r
513 sizeof (UINT32) +\r
514 sizeof (EFI_IMAGE_FILE_HEADER) +\r
515 mNtHeader.Pe32->FileHeader.SizeOfOptionalHeader\r
516 );\r
517\r
0c18794e 518 //\r
519 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER\r
520 // structures in the image. The 'NumberOfSections' field of the image\r
521 // header indicates how big the table should be. Do not include any\r
522 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.\r
523 //\r
524 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER) * mNtHeader.Pe32->FileHeader.NumberOfSections);\r
570b3d1a 525 if (SectionHeader == NULL) {\r
526 Status = FALSE;\r
527 goto Done;\r
528 }\r
0c18794e 529 //\r
530 // 12. Using the 'PointerToRawData' in the referenced section headers as\r
531 // a key, arrange the elements in the table in ascending order. In other\r
532 // words, sort the section headers according to the disk-file offset of\r
533 // the section.\r
534 //\r
0c18794e 535 for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {\r
536 Pos = Index;\r
537 while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {\r
538 CopyMem (&SectionHeader[Pos], &SectionHeader[Pos - 1], sizeof (EFI_IMAGE_SECTION_HEADER));\r
539 Pos--;\r
540 }\r
541 CopyMem (&SectionHeader[Pos], Section, sizeof (EFI_IMAGE_SECTION_HEADER));\r
542 Section += 1;\r
543 }\r
544\r
545 //\r
546 // 13. Walk through the sorted table, bring the corresponding section\r
547 // into memory, and hash the entire section (using the 'SizeOfRawData'\r
548 // field in the section header to determine the amount of data to hash).\r
549 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .\r
550 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.\r
551 //\r
552 for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {\r
553 Section = &SectionHeader[Index];\r
554 if (Section->SizeOfRawData == 0) {\r
555 continue;\r
556 }\r
557 HashBase = mImageBase + Section->PointerToRawData;\r
558 HashSize = (UINTN) Section->SizeOfRawData;\r
559\r
560 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
561 if (!Status) {\r
562 goto Done;\r
563 }\r
564\r
565 SumOfBytesHashed += HashSize;\r
566 }\r
567\r
568 //\r
569 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra\r
570 // data in the file that needs to be added to the hash. This data begins\r
571 // at file offset SUM_OF_BYTES_HASHED and its length is:\r
572 // FileSize - (CertDirectory->Size)\r
573 //\r
574 if (mImageSize > SumOfBytesHashed) {\r
575 HashBase = mImageBase + SumOfBytesHashed;\r
551d8081 576\r
577 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
578 CertSize = 0;\r
0c18794e 579 } else {\r
551d8081 580 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
581 //\r
582 // Use PE32 offset.\r
583 //\r
584 CertSize = mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;\r
585 } else {\r
586 //\r
587 // Use PE32+ offset.\r
588 //\r
589 CertSize = mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;\r
28186d45 590 }\r
0c18794e 591 }\r
592\r
551d8081 593 if (mImageSize > CertSize + SumOfBytesHashed) {\r
594 HashSize = (UINTN) (mImageSize - CertSize - SumOfBytesHashed);\r
595\r
596 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);\r
597 if (!Status) {\r
598 goto Done;\r
599 }\r
600 } else if (mImageSize < CertSize + SumOfBytesHashed) {\r
601 Status = FALSE;\r
0c18794e 602 goto Done;\r
603 }\r
604 }\r
551d8081 605\r
0c18794e 606 Status = mHash[HashAlg].HashFinal(HashCtx, mImageDigest);\r
607\r
608Done:\r
609 if (HashCtx != NULL) {\r
610 FreePool (HashCtx);\r
611 }\r
612 if (SectionHeader != NULL) {\r
613 FreePool (SectionHeader);\r
614 }\r
615 return Status;\r
616}\r
617\r
618/**\r
69f8bb52 619 Recognize the Hash algorithm in PE/COFF Authenticode and calculate hash of\r
45bf2c47 620 Pe/Coff image based on the authenticode image hashing in PE/COFF Specification\r
0c18794e 621 8.0 Appendix A\r
622\r
dc204d5a
JY
623 Caution: This function may receive untrusted input.\r
624 PE/COFF image is external input, so this function will validate its data structure\r
625 within this image buffer before use.\r
626\r
f6f9031f 627 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.\r
628 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.\r
20333c6d 629\r
0c18794e 630 @retval EFI_UNSUPPORTED Hash algorithm is not supported.\r
631 @retval EFI_SUCCESS Hash successfully.\r
632\r
633**/\r
45bf2c47 634EFI_STATUS\r
0c18794e 635HashPeImageByType (\r
f6f9031f 636 IN UINT8 *AuthData,\r
637 IN UINTN AuthDataSize\r
0c18794e 638 )\r
639{\r
640 UINT8 Index;\r
badd40f9 641\r
45bf2c47 642 for (Index = 0; Index < HASHALG_MAX; Index++) {\r
0c18794e 643 //\r
644 // Check the Hash algorithm in PE/COFF Authenticode.\r
45bf2c47 645 // According to PKCS#7 Definition:\r
0c18794e 646 // SignedData ::= SEQUENCE {\r
647 // version Version,\r
648 // digestAlgorithms DigestAlgorithmIdentifiers,\r
649 // contentInfo ContentInfo,\r
650 // .... }\r
651 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing\r
652 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.\r
bd0de396 653 // Fixed offset (+32) is calculated based on two bytes of length encoding.\r
45bf2c47 654 //\r
f6f9031f 655 if ((*(AuthData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE) {\r
bd0de396 656 //\r
657 // Only support two bytes of Long Form of Length Encoding.\r
658 //\r
659 continue;\r
660 }\r
661\r
f6f9031f 662 if (AuthDataSize < 32 + mHash[Index].OidLength) {\r
badd40f9 663 return EFI_UNSUPPORTED;\r
664 }\r
665\r
f6f9031f 666 if (CompareMem (AuthData + 32, mHash[Index].OidValue, mHash[Index].OidLength) == 0) {\r
0c18794e 667 break;\r
668 }\r
669 }\r
670\r
671 if (Index == HASHALG_MAX) {\r
672 return EFI_UNSUPPORTED;\r
673 }\r
674\r
675 //\r
676 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.\r
677 //\r
678 if (!HashPeImage(Index)) {\r
679 return EFI_UNSUPPORTED;\r
680 }\r
681\r
682 return EFI_SUCCESS;\r
683}\r
684\r
685\r
686/**\r
687 Returns the size of a given image execution info table in bytes.\r
688\r
689 This function returns the size, in bytes, of the image execution info table specified by\r
690 ImageExeInfoTable. If ImageExeInfoTable is NULL, then 0 is returned.\r
691\r
692 @param ImageExeInfoTable A pointer to a image execution info table structure.\r
45bf2c47 693\r
0c18794e 694 @retval 0 If ImageExeInfoTable is NULL.\r
695 @retval Others The size of a image execution info table in bytes.\r
696\r
697**/\r
698UINTN\r
699GetImageExeInfoTableSize (\r
700 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable\r
701 )\r
702{\r
703 UINTN Index;\r
704 EFI_IMAGE_EXECUTION_INFO *ImageExeInfoItem;\r
705 UINTN TotalSize;\r
706\r
707 if (ImageExeInfoTable == NULL) {\r
708 return 0;\r
709 }\r
710\r
711 ImageExeInfoItem = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoTable + sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE));\r
712 TotalSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);\r
713 for (Index = 0; Index < ImageExeInfoTable->NumberOfImages; Index++) {\r
714 TotalSize += ReadUnaligned32 ((UINT32 *) &ImageExeInfoItem->InfoSize);\r
715 ImageExeInfoItem = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoItem + ReadUnaligned32 ((UINT32 *) &ImageExeInfoItem->InfoSize));\r
716 }\r
717\r
718 return TotalSize;\r
719}\r
720\r
721/**\r
722 Create an Image Execution Information Table entry and add it to system configuration table.\r
723\r
724 @param[in] Action Describes the action taken by the firmware regarding this image.\r
725 @param[in] Name Input a null-terminated, user-friendly name.\r
726 @param[in] DevicePath Input device path pointer.\r
727 @param[in] Signature Input signature info in EFI_SIGNATURE_LIST data structure.\r
728 @param[in] SignatureSize Size of signature.\r
45bf2c47 729\r
0c18794e 730**/\r
731VOID\r
732AddImageExeInfo (\r
45bf2c47 733 IN EFI_IMAGE_EXECUTION_ACTION Action,\r
734 IN CHAR16 *Name OPTIONAL,\r
0c18794e 735 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
736 IN EFI_SIGNATURE_LIST *Signature OPTIONAL,\r
737 IN UINTN SignatureSize\r
738 )\r
739{\r
0c18794e 740 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable;\r
741 EFI_IMAGE_EXECUTION_INFO_TABLE *NewImageExeInfoTable;\r
742 EFI_IMAGE_EXECUTION_INFO *ImageExeInfoEntry;\r
743 UINTN ImageExeInfoTableSize;\r
744 UINTN NewImageExeInfoEntrySize;\r
745 UINTN NameStringLen;\r
746 UINTN DevicePathSize;\r
4fc08e8d 747 CHAR16 *NameStr;\r
0c18794e 748\r
0c18794e 749 ImageExeInfoTable = NULL;\r
750 NewImageExeInfoTable = NULL;\r
751 ImageExeInfoEntry = NULL;\r
752 NameStringLen = 0;\r
4fc08e8d 753 NameStr = NULL;\r
0c18794e 754\r
570b3d1a 755 if (DevicePath == NULL) {\r
756 return ;\r
757 }\r
45bf2c47 758\r
0c18794e 759 if (Name != NULL) {\r
760 NameStringLen = StrSize (Name);\r
b3d42170 761 } else {\r
762 NameStringLen = sizeof (CHAR16);\r
0c18794e 763 }\r
764\r
45bf2c47 765 EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);\r
0c18794e 766 if (ImageExeInfoTable != NULL) {\r
767 //\r
768 // The table has been found!\r
b3d42170 769 // We must enlarge the table to accomodate the new exe info entry.\r
0c18794e 770 //\r
771 ImageExeInfoTableSize = GetImageExeInfoTableSize (ImageExeInfoTable);\r
772 } else {\r
773 //\r
774 // Not Found!\r
775 // We should create a new table to append to the configuration table.\r
776 //\r
777 ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);\r
778 }\r
779\r
780 DevicePathSize = GetDevicePathSize (DevicePath);\r
4fc08e8d
CZ
781\r
782 //\r
783 // Signature size can be odd. Pad after signature to ensure next EXECUTION_INFO entry align\r
784 //\r
785 NewImageExeInfoEntrySize = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;\r
786\r
0c18794e 787 NewImageExeInfoTable = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize + NewImageExeInfoEntrySize);\r
570b3d1a 788 if (NewImageExeInfoTable == NULL) {\r
789 return ;\r
790 }\r
0c18794e 791\r
792 if (ImageExeInfoTable != NULL) {\r
793 CopyMem (NewImageExeInfoTable, ImageExeInfoTable, ImageExeInfoTableSize);\r
794 } else {\r
795 NewImageExeInfoTable->NumberOfImages = 0;\r
796 }\r
797 NewImageExeInfoTable->NumberOfImages++;\r
798 ImageExeInfoEntry = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) NewImageExeInfoTable + ImageExeInfoTableSize);\r
799 //\r
ffccb935 800 // Update new item's information.\r
0c18794e 801 //\r
1fee5304
ED
802 WriteUnaligned32 ((UINT32 *) ImageExeInfoEntry, Action);\r
803 WriteUnaligned32 ((UINT32 *) ((UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION)), (UINT32) NewImageExeInfoEntrySize);\r
0c18794e 804\r
4fc08e8d 805 NameStr = (CHAR16 *)(ImageExeInfoEntry + 1);\r
0c18794e 806 if (Name != NULL) {\r
4fc08e8d 807 CopyMem ((UINT8 *) NameStr, Name, NameStringLen);\r
b3d42170 808 } else {\r
4fc08e8d 809 ZeroMem ((UINT8 *) NameStr, sizeof (CHAR16));\r
0c18794e 810 }\r
4fc08e8d 811\r
0c18794e 812 CopyMem (\r
4fc08e8d 813 (UINT8 *) NameStr + NameStringLen,\r
0c18794e 814 DevicePath,\r
815 DevicePathSize\r
816 );\r
817 if (Signature != NULL) {\r
818 CopyMem (\r
4fc08e8d 819 (UINT8 *) NameStr + NameStringLen + DevicePathSize,\r
0c18794e 820 Signature,\r
821 SignatureSize\r
822 );\r
823 }\r
824 //\r
825 // Update/replace the image execution table.\r
826 //\r
570b3d1a 827 gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) NewImageExeInfoTable);\r
45bf2c47 828\r
0c18794e 829 //\r
830 // Free Old table data!\r
831 //\r
832 if (ImageExeInfoTable != NULL) {\r
833 FreePool (ImageExeInfoTable);\r
834 }\r
835}\r
836\r
20333c6d
QL
837/**\r
838 Check whether the hash of an given X.509 certificate is in forbidden database (DBX).\r
839\r
840 @param[in] Certificate Pointer to X.509 Certificate that is searched for.\r
841 @param[in] CertSize Size of X.509 Certificate.\r
842 @param[in] SignatureList Pointer to the Signature List in forbidden database.\r
843 @param[in] SignatureListSize Size of Signature List.\r
844 @param[out] RevocationTime Return the time that the certificate was revoked.\r
845\r
846 @return TRUE The certificate hash is found in the forbidden database.\r
847 @return FALSE The certificate hash is not found in the forbidden database.\r
848\r
849**/\r
850BOOLEAN\r
851IsCertHashFoundInDatabase (\r
852 IN UINT8 *Certificate,\r
853 IN UINTN CertSize,\r
854 IN EFI_SIGNATURE_LIST *SignatureList,\r
855 IN UINTN SignatureListSize,\r
856 OUT EFI_TIME *RevocationTime\r
857 )\r
858{\r
859 BOOLEAN IsFound;\r
5789fe35 860 BOOLEAN Status;\r
20333c6d
QL
861 EFI_SIGNATURE_LIST *DbxList;\r
862 UINTN DbxSize;\r
863 EFI_SIGNATURE_DATA *CertHash;\r
864 UINTN CertHashCount;\r
865 UINTN Index;\r
866 UINT32 HashAlg;\r
867 VOID *HashCtx;\r
868 UINT8 CertDigest[MAX_DIGEST_SIZE];\r
869 UINT8 *DbxCertHash;\r
870 UINTN SiglistHeaderSize;\r
12d95665
LQ
871 UINT8 *TBSCert;\r
872 UINTN TBSCertSize;\r
20333c6d
QL
873\r
874 IsFound = FALSE;\r
875 DbxList = SignatureList;\r
876 DbxSize = SignatureListSize;\r
877 HashCtx = NULL;\r
878 HashAlg = HASHALG_MAX;\r
879\r
12d95665
LQ
880 if ((RevocationTime == NULL) || (DbxList == NULL)) {\r
881 return FALSE;\r
882 }\r
883\r
884 //\r
885 // Retrieve the TBSCertificate from the X.509 Certificate.\r
886 //\r
887 if (!X509GetTBSCert (Certificate, CertSize, &TBSCert, &TBSCertSize)) {\r
888 return FALSE;\r
889 }\r
20333c6d
QL
890\r
891 while ((DbxSize > 0) && (SignatureListSize >= DbxList->SignatureListSize)) {\r
892 //\r
893 // Determine Hash Algorithm of Certificate in the forbidden database.\r
894 //\r
895 if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha256Guid)) {\r
896 HashAlg = HASHALG_SHA256;\r
897 } else if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha384Guid)) {\r
898 HashAlg = HASHALG_SHA384;\r
899 } else if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha512Guid)) {\r
900 HashAlg = HASHALG_SHA512;\r
901 } else {\r
902 DbxSize -= DbxList->SignatureListSize;\r
903 DbxList = (EFI_SIGNATURE_LIST *) ((UINT8 *) DbxList + DbxList->SignatureListSize);\r
904 continue;\r
905 }\r
906\r
907 //\r
12d95665 908 // Calculate the hash value of current TBSCertificate for comparision.\r
20333c6d
QL
909 //\r
910 if (mHash[HashAlg].GetContextSize == NULL) {\r
911 goto Done;\r
912 }\r
913 ZeroMem (CertDigest, MAX_DIGEST_SIZE);\r
914 HashCtx = AllocatePool (mHash[HashAlg].GetContextSize ());\r
915 if (HashCtx == NULL) {\r
916 goto Done;\r
917 }\r
918 Status = mHash[HashAlg].HashInit (HashCtx);\r
919 if (!Status) {\r
920 goto Done;\r
921 }\r
12d95665 922 Status = mHash[HashAlg].HashUpdate (HashCtx, TBSCert, TBSCertSize);\r
20333c6d
QL
923 if (!Status) {\r
924 goto Done;\r
925 }\r
926 Status = mHash[HashAlg].HashFinal (HashCtx, CertDigest);\r
927 if (!Status) {\r
928 goto Done;\r
929 }\r
930\r
931 SiglistHeaderSize = sizeof (EFI_SIGNATURE_LIST) + DbxList->SignatureHeaderSize;\r
932 CertHash = (EFI_SIGNATURE_DATA *) ((UINT8 *) DbxList + SiglistHeaderSize);\r
933 CertHashCount = (DbxList->SignatureListSize - SiglistHeaderSize) / DbxList->SignatureSize;\r
934 for (Index = 0; Index < CertHashCount; Index++) {\r
935 //\r
936 // Iterate each Signature Data Node within this CertList for verify.\r
937 //\r
938 DbxCertHash = CertHash->SignatureData;\r
939 if (CompareMem (DbxCertHash, CertDigest, mHash[HashAlg].DigestLength) == 0) {\r
940 //\r
941 // Hash of Certificate is found in forbidden database.\r
942 //\r
943 IsFound = TRUE;\r
944\r
945 //\r
946 // Return the revocation time.\r
947 //\r
948 CopyMem (RevocationTime, (EFI_TIME *)(DbxCertHash + mHash[HashAlg].DigestLength), sizeof (EFI_TIME));\r
949 goto Done;\r
950 }\r
951 CertHash = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertHash + DbxList->SignatureSize);\r
952 }\r
953\r
954 DbxSize -= DbxList->SignatureListSize;\r
955 DbxList = (EFI_SIGNATURE_LIST *) ((UINT8 *) DbxList + DbxList->SignatureListSize);\r
956 }\r
957\r
958Done:\r
959 if (HashCtx != NULL) {\r
960 FreePool (HashCtx);\r
961 }\r
962\r
963 return IsFound;\r
964}\r
965\r
0c18794e 966/**\r
967 Check whether signature is in specified database.\r
968\r
969 @param[in] VariableName Name of database variable that is searched in.\r
970 @param[in] Signature Pointer to signature that is searched for.\r
971 @param[in] CertType Pointer to hash algrithom.\r
972 @param[in] SignatureSize Size of Signature.\r
973\r
974 @return TRUE Found the signature in the variable database.\r
975 @return FALSE Not found the signature in the variable database.\r
976\r
977**/\r
978BOOLEAN\r
979IsSignatureFoundInDatabase (\r
980 IN CHAR16 *VariableName,\r
45bf2c47 981 IN UINT8 *Signature,\r
0c18794e 982 IN EFI_GUID *CertType,\r
983 IN UINTN SignatureSize\r
984 )\r
985{\r
986 EFI_STATUS Status;\r
987 EFI_SIGNATURE_LIST *CertList;\r
988 EFI_SIGNATURE_DATA *Cert;\r
989 UINTN DataSize;\r
990 UINT8 *Data;\r
991 UINTN Index;\r
992 UINTN CertCount;\r
993 BOOLEAN IsFound;\r
20333c6d 994\r
0c18794e 995 //\r
996 // Read signature database variable.\r
997 //\r
998 IsFound = FALSE;\r
999 Data = NULL;\r
1000 DataSize = 0;\r
1001 Status = gRT->GetVariable (VariableName, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
1002 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1003 return FALSE;\r
1004 }\r
1005\r
1006 Data = (UINT8 *) AllocateZeroPool (DataSize);\r
570b3d1a 1007 if (Data == NULL) {\r
1008 return FALSE;\r
1009 }\r
0c18794e 1010\r
1011 Status = gRT->GetVariable (VariableName, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, Data);\r
1012 if (EFI_ERROR (Status)) {\r
1013 goto Done;\r
1014 }\r
1015 //\r
1016 // Enumerate all signature data in SigDB to check if executable's signature exists.\r
1017 //\r
1018 CertList = (EFI_SIGNATURE_LIST *) Data;\r
1019 while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {\r
7403ff5b 1020 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
0c18794e 1021 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1022 if ((CertList->SignatureSize == sizeof(EFI_SIGNATURE_DATA) - 1 + SignatureSize) && (CompareGuid(&CertList->SignatureType, CertType))) {\r
1023 for (Index = 0; Index < CertCount; Index++) {\r
1024 if (CompareMem (Cert->SignatureData, Signature, SignatureSize) == 0) {\r
1025 //\r
1026 // Find the signature in database.\r
1027 //\r
1028 IsFound = TRUE;\r
5b196b06
ZC
1029 //\r
1030 // Entries in UEFI_IMAGE_SECURITY_DATABASE that are used to validate image should be measured\r
1031 //\r
1032 if (StrCmp(VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) {\r
1033 SecureBootHook (VariableName, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, Cert);\r
1034 }\r
0c18794e 1035 break;\r
1036 }\r
1037\r
1038 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
1039 }\r
1040\r
1041 if (IsFound) {\r
1042 break;\r
1043 }\r
1044 }\r
1045\r
1046 DataSize -= CertList->SignatureListSize;\r
1047 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1048 }\r
1049\r
1050Done:\r
1051 if (Data != NULL) {\r
1052 FreePool (Data);\r
1053 }\r
1054\r
1055 return IsFound;\r
1056}\r
1057\r
1058/**\r
20333c6d 1059 Check whether the timestamp is valid by comparing the signing time and the revocation time.\r
0c18794e 1060\r
20333c6d
QL
1061 @param SigningTime A pointer to the signing time.\r
1062 @param RevocationTime A pointer to the revocation time.\r
45bf2c47 1063\r
20333c6d
QL
1064 @retval TRUE The SigningTime is not later than the RevocationTime.\r
1065 @retval FALSE The SigningTime is later than the RevocationTime.\r
0c18794e 1066\r
1067**/\r
45bf2c47 1068BOOLEAN\r
20333c6d
QL
1069IsValidSignatureByTimestamp (\r
1070 IN EFI_TIME *SigningTime,\r
1071 IN EFI_TIME *RevocationTime\r
1072 )\r
1073{\r
1074 if (SigningTime->Year != RevocationTime->Year) {\r
1075 return (BOOLEAN) (SigningTime->Year < RevocationTime->Year);\r
1076 } else if (SigningTime->Month != RevocationTime->Month) {\r
1077 return (BOOLEAN) (SigningTime->Month < RevocationTime->Month);\r
1078 } else if (SigningTime->Day != RevocationTime->Day) {\r
1079 return (BOOLEAN) (SigningTime->Day < RevocationTime->Day);\r
1080 } else if (SigningTime->Hour != RevocationTime->Hour) {\r
1081 return (BOOLEAN) (SigningTime->Hour < RevocationTime->Hour);\r
1082 } else if (SigningTime->Minute != RevocationTime->Minute) {\r
1083 return (BOOLEAN) (SigningTime->Minute < RevocationTime->Minute);\r
1084 }\r
1085\r
1086 return (BOOLEAN) (SigningTime->Second <= RevocationTime->Second);\r
1087}\r
1088\r
1089/**\r
1090 Check if the given time value is zero.\r
1091\r
1092 @param[in] Time Pointer of a time value.\r
1093\r
1094 @retval TRUE The Time is Zero.\r
1095 @retval FALSE The Time is not Zero.\r
1096\r
1097**/\r
1098BOOLEAN\r
1099IsTimeZero (\r
1100 IN EFI_TIME *Time\r
1101 )\r
1102{\r
1103 if ((Time->Year == 0) && (Time->Month == 0) && (Time->Day == 0) &&\r
1104 (Time->Hour == 0) && (Time->Minute == 0) && (Time->Second == 0)) {\r
1105 return TRUE;\r
1106 }\r
1107\r
1108 return FALSE;\r
1109}\r
1110\r
1111/**\r
1112 Check whether the timestamp signature is valid and the signing time is also earlier than \r
1113 the revocation time.\r
1114\r
1115 @param[in] AuthData Pointer to the Authenticode signature retrieved from signed image.\r
1116 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
1117 @param[in] RevocationTime The time that the certificate was revoked.\r
1118\r
1119 @retval TRUE Timestamp signature is valid and signing time is no later than the \r
1120 revocation time.\r
1121 @retval FALSE Timestamp signature is not valid or the signing time is later than the\r
1122 revocation time.\r
1123\r
1124**/\r
1125BOOLEAN\r
1126PassTimestampCheck (\r
1127 IN UINT8 *AuthData,\r
1128 IN UINTN AuthDataSize,\r
1129 IN EFI_TIME *RevocationTime\r
1130 )\r
1131{\r
1132 EFI_STATUS Status;\r
1133 BOOLEAN VerifyStatus;\r
1134 EFI_SIGNATURE_LIST *CertList;\r
1135 EFI_SIGNATURE_DATA *Cert;\r
1136 UINT8 *DbtData;\r
1137 UINTN DbtDataSize;\r
1138 UINT8 *RootCert;\r
1139 UINTN RootCertSize;\r
1140 UINTN Index;\r
1141 UINTN CertCount;\r
1142 EFI_TIME SigningTime;\r
1143\r
1144 //\r
1145 // Variable Initialization\r
1146 //\r
1147 VerifyStatus = FALSE;\r
1148 DbtData = NULL;\r
1149 CertList = NULL;\r
1150 Cert = NULL;\r
1151 RootCert = NULL;\r
1152 RootCertSize = 0;\r
1153\r
1154 //\r
1155 // If RevocationTime is zero, the certificate shall be considered to always be revoked.\r
1156 //\r
1157 if (IsTimeZero (RevocationTime)) {\r
1158 return FALSE;\r
1159 }\r
1160\r
1161 //\r
1162 // RevocationTime is non-zero, the certificate should be considered to be revoked from that time and onwards.\r
1163 // Using the dbt to get the trusted TSA certificates.\r
1164 //\r
1165 DbtDataSize = 0;\r
1166 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, NULL);\r
7e0699c0
QL
1167 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1168 goto Done;\r
1169 }\r
1170 DbtData = (UINT8 *) AllocateZeroPool (DbtDataSize);\r
1171 if (DbtData == NULL) {\r
1172 goto Done;\r
1173 }\r
1174 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, (VOID *) DbtData);\r
1175 if (EFI_ERROR (Status)) {\r
1176 goto Done;\r
20333c6d
QL
1177 }\r
1178\r
1179 CertList = (EFI_SIGNATURE_LIST *) DbtData;\r
1180 while ((DbtDataSize > 0) && (DbtDataSize >= CertList->SignatureListSize)) {\r
1181 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
1182 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1183 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
1184 for (Index = 0; Index < CertCount; Index++) {\r
1185 //\r
1186 // Iterate each Signature Data Node within this CertList for verify.\r
1187 //\r
1188 RootCert = Cert->SignatureData;\r
1189 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
1190 //\r
1191 // Get the signing time if the timestamp signature is valid.\r
1192 //\r
1193 if (ImageTimestampVerify (AuthData, AuthDataSize, RootCert, RootCertSize, &SigningTime)) {\r
1194 //\r
1195 // The signer signature is valid only when the signing time is earlier than revocation time.\r
1196 //\r
1197 if (IsValidSignatureByTimestamp (&SigningTime, RevocationTime)) {\r
1198 VerifyStatus = TRUE;\r
1199 goto Done;\r
1200 }\r
1201 }\r
1202 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
1203 }\r
1204 }\r
1205 DbtDataSize -= CertList->SignatureListSize;\r
1206 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1207 }\r
1208\r
1209Done:\r
1210 if (DbtData != NULL) {\r
1211 FreePool (DbtData);\r
1212 }\r
1213\r
1214 return VerifyStatus;\r
1215}\r
1216\r
1217/**\r
1218 Check whether the image signature is forbidden by the forbidden database (dbx).\r
1219 The image is forbidden to load if any certificates for signing are revoked before signing time.\r
1220\r
560ac77e
ZC
1221 @param[in] AuthData Pointer to the Authenticode signature retrieved from the signed image.\r
1222 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
20333c6d
QL
1223\r
1224 @retval TRUE Image is forbidden by dbx.\r
1225 @retval FALSE Image is not forbidden by dbx.\r
1226\r
1227**/\r
1228BOOLEAN\r
560ac77e
ZC
1229IsForbiddenByDbx ( \r
1230 IN UINT8 *AuthData,\r
1231 IN UINTN AuthDataSize \r
20333c6d
QL
1232 )\r
1233{\r
1234 EFI_STATUS Status;\r
1235 BOOLEAN IsForbidden;\r
1236 UINT8 *Data;\r
1237 UINTN DataSize;\r
27c93c06
LQ
1238 EFI_SIGNATURE_LIST *CertList;\r
1239 UINTN CertListSize;\r
1240 EFI_SIGNATURE_DATA *CertData;\r
1241 UINT8 *RootCert;\r
1242 UINTN RootCertSize;\r
1243 UINTN CertCount;\r
20333c6d
QL
1244 UINTN Index;\r
1245 UINT8 *CertBuffer;\r
1246 UINTN BufferLength;\r
1247 UINT8 *TrustedCert;\r
1248 UINTN TrustedCertLength;\r
1249 UINT8 CertNumber;\r
1250 UINT8 *CertPtr;\r
1251 UINT8 *Cert;\r
1252 UINTN CertSize;\r
1253 EFI_TIME RevocationTime;\r
20333c6d
QL
1254 //\r
1255 // Variable Initialization\r
1256 //\r
1257 IsForbidden = FALSE;\r
1258 Data = NULL;\r
27c93c06
LQ
1259 CertList = NULL;\r
1260 CertData = NULL;\r
1261 RootCert = NULL;\r
1262 RootCertSize = 0;\r
20333c6d
QL
1263 Cert = NULL;\r
1264 CertBuffer = NULL;\r
1265 BufferLength = 0;\r
1266 TrustedCert = NULL;\r
1267 TrustedCertLength = 0;\r
1268\r
1269 //\r
1270 // The image will not be forbidden if dbx can't be got.\r
1271 //\r
1272 DataSize = 0;\r
1273 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
7e0699c0
QL
1274 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1275 return IsForbidden;\r
20333c6d 1276 }\r
7e0699c0
QL
1277 Data = (UINT8 *) AllocateZeroPool (DataSize);\r
1278 if (Data == NULL) {\r
1279 return IsForbidden;\r
1280 }\r
1281\r
1282 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
20333c6d
QL
1283 if (EFI_ERROR (Status)) {\r
1284 return IsForbidden;\r
1285 }\r
1286\r
27c93c06
LQ
1287 //\r
1288 // Verify image signature with RAW X509 certificates in DBX database.\r
1289 // If passed, the image will be forbidden.\r
1290 //\r
1291 CertList = (EFI_SIGNATURE_LIST *) Data;\r
1292 CertListSize = DataSize;\r
1293 while ((CertListSize > 0) && (CertListSize >= CertList->SignatureListSize)) {\r
1294 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
1295 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1296 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
1297\r
1298 for (Index = 0; Index < CertCount; Index++) {\r
1299 //\r
1300 // Iterate each Signature Data Node within this CertList for verify.\r
1301 //\r
1302 RootCert = CertData->SignatureData;\r
1303 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
1304\r
1305 //\r
1306 // Call AuthenticodeVerify library to Verify Authenticode struct.\r
1307 //\r
1308 IsForbidden = AuthenticodeVerify (\r
1309 AuthData,\r
1310 AuthDataSize,\r
1311 RootCert,\r
1312 RootCertSize,\r
1313 mImageDigest,\r
1314 mImageDigestSize\r
1315 );\r
1316 if (IsForbidden) {\r
531c89a1 1317 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but signature is forbidden by DBX.\n"));\r
27c93c06
LQ
1318 goto Done;\r
1319 }\r
1320\r
1321 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
1322 }\r
1323 }\r
1324\r
1325 CertListSize -= CertList->SignatureListSize;\r
1326 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1327 }\r
1328\r
1329 //\r
1330 // Check X.509 Certificate Hash & Possible Timestamp.\r
1331 //\r
1332\r
20333c6d
QL
1333 //\r
1334 // Retrieve the certificate stack from AuthData\r
1335 // The output CertStack format will be:\r
1336 // UINT8 CertNumber;\r
1337 // UINT32 Cert1Length;\r
1338 // UINT8 Cert1[];\r
1339 // UINT32 Cert2Length;\r
1340 // UINT8 Cert2[];\r
1341 // ...\r
1342 // UINT32 CertnLength;\r
1343 // UINT8 Certn[];\r
1344 //\r
1345 Pkcs7GetSigners (AuthData, AuthDataSize, &CertBuffer, &BufferLength, &TrustedCert, &TrustedCertLength);\r
7e0699c0 1346 if ((BufferLength == 0) || (CertBuffer == NULL)) {\r
20333c6d
QL
1347 IsForbidden = TRUE;\r
1348 goto Done;\r
1349 }\r
1350\r
1351 //\r
27c93c06 1352 // Check if any hash of certificates embedded in AuthData is in the forbidden database.\r
20333c6d
QL
1353 //\r
1354 CertNumber = (UINT8) (*CertBuffer);\r
1355 CertPtr = CertBuffer + 1;\r
1356 for (Index = 0; Index < CertNumber; Index++) {\r
1357 CertSize = (UINTN) ReadUnaligned32 ((UINT32 *)CertPtr);\r
1358 Cert = (UINT8 *)CertPtr + sizeof (UINT32);\r
91422384
ZC
1359 //\r
1360 // Advance CertPtr to the next cert in image signer's cert list\r
1361 //\r
1362 CertPtr = CertPtr + sizeof (UINT32) + CertSize;\r
20333c6d
QL
1363\r
1364 if (IsCertHashFoundInDatabase (Cert, CertSize, (EFI_SIGNATURE_LIST *)Data, DataSize, &RevocationTime)) {\r
1365 //\r
1366 // Check the timestamp signature and signing time to determine if the image can be trusted.\r
1367 //\r
1368 IsForbidden = TRUE;\r
1369 if (PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime)) {\r
1370 IsForbidden = FALSE;\r
91422384
ZC
1371 //\r
1372 // Pass DBT check. Continue to check other certs in image signer's cert list against DBX, DBT\r
1373 //\r
1374 continue;\r
20333c6d 1375 }\r
531c89a1 1376 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but signature failed the timestamp check.\n"));\r
20333c6d
QL
1377 goto Done;\r
1378 }\r
1379\r
20333c6d
QL
1380 }\r
1381\r
1382Done:\r
1383 if (Data != NULL) {\r
1384 FreePool (Data);\r
1385 }\r
1386\r
1387 Pkcs7FreeSigners (CertBuffer);\r
1388 Pkcs7FreeSigners (TrustedCert);\r
1389\r
1390 return IsForbidden;\r
1391}\r
1392\r
4fc08e8d 1393\r
20333c6d
QL
1394/**\r
1395 Check whether the image signature can be verified by the trusted certificates in DB database.\r
1396\r
560ac77e
ZC
1397 @param[in] AuthData Pointer to the Authenticode signature retrieved from signed image.\r
1398 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
20333c6d
QL
1399\r
1400 @retval TRUE Image passed verification using certificate in db.\r
1401 @retval FALSE Image didn't pass verification using certificate in db.\r
1402\r
1403**/\r
1404BOOLEAN\r
1405IsAllowedByDb (\r
560ac77e
ZC
1406 IN UINT8 *AuthData,\r
1407 IN UINTN AuthDataSize\r
0c18794e 1408 )\r
1409{\r
1410 EFI_STATUS Status;\r
1411 BOOLEAN VerifyStatus;\r
0c18794e 1412 EFI_SIGNATURE_LIST *CertList;\r
4fc08e8d 1413 EFI_SIGNATURE_DATA *CertData;\r
0c18794e 1414 UINTN DataSize;\r
45bf2c47 1415 UINT8 *Data;\r
0c18794e 1416 UINT8 *RootCert;\r
1417 UINTN RootCertSize;\r
1418 UINTN Index;\r
1419 UINTN CertCount;\r
27c93c06
LQ
1420 UINTN DbxDataSize;\r
1421 UINT8 *DbxData;\r
1422 EFI_TIME RevocationTime;\r
0c18794e 1423\r
4fc08e8d
CZ
1424 Data = NULL;\r
1425 CertList = NULL;\r
1426 CertData = NULL;\r
1427 RootCert = NULL;\r
1428 DbxData = NULL;\r
1429 RootCertSize = 0;\r
1430 VerifyStatus = FALSE;\r
0c18794e 1431\r
0c18794e 1432 DataSize = 0;\r
20333c6d 1433 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
0c18794e 1434 if (Status == EFI_BUFFER_TOO_SMALL) {\r
45bf2c47 1435 Data = (UINT8 *) AllocateZeroPool (DataSize);\r
1436 if (Data == NULL) {\r
1437 return VerifyStatus;\r
570b3d1a 1438 }\r
0c18794e 1439\r
20333c6d 1440 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
0c18794e 1441 if (EFI_ERROR (Status)) {\r
1442 goto Done;\r
1443 }\r
45bf2c47 1444\r
1445 //\r
1446 // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.\r
0c18794e 1447 //\r
45bf2c47 1448 CertList = (EFI_SIGNATURE_LIST *) Data;\r
0c18794e 1449 while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {\r
1450 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
4fc08e8d
CZ
1451 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1452 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
20333c6d 1453\r
0c18794e 1454 for (Index = 0; Index < CertCount; Index++) {\r
1455 //\r
45bf2c47 1456 // Iterate each Signature Data Node within this CertList for verify.\r
1457 //\r
4fc08e8d 1458 RootCert = CertData->SignatureData;\r
20333c6d 1459 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
45bf2c47 1460\r
0c18794e 1461 //\r
45bf2c47 1462 // Call AuthenticodeVerify library to Verify Authenticode struct.\r
0c18794e 1463 //\r
1464 VerifyStatus = AuthenticodeVerify (\r
f6f9031f 1465 AuthData,\r
1466 AuthDataSize,\r
0c18794e 1467 RootCert,\r
1468 RootCertSize,\r
1469 mImageDigest,\r
1470 mImageDigestSize\r
1471 );\r
0c18794e 1472 if (VerifyStatus) {\r
27c93c06
LQ
1473 //\r
1474 // Here We still need to check if this RootCert's Hash is revoked\r
1475 //\r
1476 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, NULL);\r
1477 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1478 goto Done;\r
1479 }\r
1ca3a099 1480 DbxData = (UINT8 *) AllocateZeroPool (DbxDataSize);\r
27c93c06
LQ
1481 if (DbxData == NULL) {\r
1482 goto Done;\r
1483 }\r
1484\r
1485 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, (VOID *) DbxData);\r
1486 if (EFI_ERROR (Status)) {\r
1487 goto Done;\r
1488 }\r
1489\r
1490 if (IsCertHashFoundInDatabase (RootCert, RootCertSize, (EFI_SIGNATURE_LIST *)DbxData, DbxDataSize, &RevocationTime)) {\r
1491 //\r
531c89a1 1492 // Check the timestamp signature and signing time to determine if the RootCert can be trusted.\r
27c93c06
LQ
1493 //\r
1494 VerifyStatus = PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime);\r
531c89a1
CS
1495 if (!VerifyStatus) {\r
1496 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed and signature is accepted by DB, but its root cert failed the timestamp check.\n"));\r
1497 }\r
27c93c06
LQ
1498 }\r
1499\r
0c18794e 1500 goto Done;\r
1501 }\r
20333c6d 1502\r
4fc08e8d 1503 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
45bf2c47 1504 }\r
0c18794e 1505 }\r
20333c6d 1506\r
0c18794e 1507 DataSize -= CertList->SignatureListSize;\r
1508 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1509 }\r
1510 }\r
1511\r
45bf2c47 1512Done:\r
4fc08e8d 1513\r
27c93c06 1514 if (VerifyStatus) {\r
4fc08e8d 1515 SecureBootHook (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, CertData);\r
27c93c06
LQ
1516 }\r
1517\r
45bf2c47 1518 if (Data != NULL) {\r
1519 FreePool (Data);\r
1520 }\r
27c93c06
LQ
1521 if (DbxData != NULL) {\r
1522 FreePool (DbxData);\r
1523 }\r
0c18794e 1524\r
45bf2c47 1525 return VerifyStatus;\r
1526}\r
0c18794e 1527\r
0c18794e 1528/**\r
1529 Provide verification service for signed images, which include both signature validation\r
45bf2c47 1530 and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and\r
0c18794e 1531 MSFT Authenticode type signatures are supported.\r
0c18794e 1532\r
45bf2c47 1533 In this implementation, only verify external executables when in USER MODE.\r
1534 Executables from FV is bypass, so pass in AuthenticationStatus is ignored.\r
1535\r
6de4c35f 1536 The image verification policy is:\r
50fe73a1 1537 If the image is signed,\r
6de4c35f 1538 At least one valid signature or at least one hash value of the image must match a record\r
1539 in the security database "db", and no valid signature nor any hash value of the image may\r
1540 be reflected in the security database "dbx".\r
50fe73a1 1541 Otherwise, the image is not signed,\r
6de4c35f 1542 The SHA256 hash value of the image must match a record in the security database "db", and\r
1543 not be reflected in the security data base "dbx".\r
45bf2c47 1544\r
dc204d5a
JY
1545 Caution: This function may receive untrusted input.\r
1546 PE/COFF image is external input, so this function will validate its data structure\r
1547 within this image buffer before use.\r
1548\r
45bf2c47 1549 @param[in] AuthenticationStatus\r
0c18794e 1550 This is the authentication status returned from the security\r
1551 measurement services for the input file.\r
1552 @param[in] File This is a pointer to the device path of the file that is\r
1553 being dispatched. This will optionally be used for logging.\r
1554 @param[in] FileBuffer File buffer matches the input file device path.\r
1555 @param[in] FileSize Size of File buffer matches the input file device path.\r
5db28a67
LG
1556 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
1557\r
1558 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL\r
1559 FileBuffer did authenticate, and the platform policy dictates\r
1560 that the DXE Foundation may use the file.\r
1561 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath\r
1562 and non-NULL FileBuffer did authenticate, and the platform\r
1563 policy dictates that the DXE Foundation may execute the image in\r
1564 FileBuffer.\r
570b3d1a 1565 @retval EFI_OUT_RESOURCE Fail to allocate memory.\r
0c18794e 1566 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
1567 the platform policy dictates that File should be placed\r
5db28a67
LG
1568 in the untrusted state. The image has been added to the file\r
1569 execution table.\r
1570 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not\r
1571 authenticate, and the platform policy dictates that the DXE\r
1572 Foundation many not use File.\r
0c18794e 1573\r
1574**/\r
1575EFI_STATUS\r
1576EFIAPI\r
1577DxeImageVerificationHandler (\r
1578 IN UINT32 AuthenticationStatus,\r
1579 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
1580 IN VOID *FileBuffer,\r
5db28a67
LG
1581 IN UINTN FileSize,\r
1582 IN BOOLEAN BootPolicy\r
0c18794e 1583 )\r
0c18794e 1584{\r
551d8081 1585 EFI_STATUS Status;\r
1586 UINT16 Magic;\r
1587 EFI_IMAGE_DOS_HEADER *DosHdr;\r
1588 EFI_STATUS VerifyStatus;\r
551d8081 1589 EFI_SIGNATURE_LIST *SignatureList;\r
1590 UINTN SignatureListSize;\r
1591 EFI_SIGNATURE_DATA *Signature;\r
1592 EFI_IMAGE_EXECUTION_ACTION Action;\r
1593 WIN_CERTIFICATE *WinCertificate;\r
1594 UINT32 Policy;\r
560ac77e 1595 UINT8 *SecureBoot;\r
551d8081 1596 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
1597 UINT32 NumberOfRvaAndSizes;\r
f6f9031f 1598 WIN_CERTIFICATE_EFI_PKCS *PkcsCertData;\r
1599 WIN_CERTIFICATE_UEFI_GUID *WinCertUefiGuid;\r
1600 UINT8 *AuthData;\r
1601 UINTN AuthDataSize;\r
1602 EFI_IMAGE_DATA_DIRECTORY *SecDataDir;\r
6de4c35f 1603 UINT32 OffSet;\r
213cc100 1604 CHAR16 *NameStr;\r
0c18794e 1605\r
0c18794e 1606 SignatureList = NULL;\r
1607 SignatureListSize = 0;\r
1608 WinCertificate = NULL;\r
f6f9031f 1609 SecDataDir = NULL;\r
1610 PkcsCertData = NULL;\r
0c18794e 1611 Action = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;\r
1612 Status = EFI_ACCESS_DENIED;\r
6de4c35f 1613 VerifyStatus = EFI_ACCESS_DENIED;\r
1614\r
4fc08e8d 1615\r
0c18794e 1616 //\r
1617 // Check the image type and get policy setting.\r
1618 //\r
1619 switch (GetImageType (File)) {\r
45bf2c47 1620\r
0c18794e 1621 case IMAGE_FROM_FV:\r
1622 Policy = ALWAYS_EXECUTE;\r
1623 break;\r
1624\r
1625 case IMAGE_FROM_OPTION_ROM:\r
1626 Policy = PcdGet32 (PcdOptionRomImageVerificationPolicy);\r
1627 break;\r
1628\r
1629 case IMAGE_FROM_REMOVABLE_MEDIA:\r
1630 Policy = PcdGet32 (PcdRemovableMediaImageVerificationPolicy);\r
1631 break;\r
1632\r
1633 case IMAGE_FROM_FIXED_MEDIA:\r
1634 Policy = PcdGet32 (PcdFixedMediaImageVerificationPolicy);\r
1635 break;\r
1636\r
1637 default:\r
45bf2c47 1638 Policy = DENY_EXECUTE_ON_SECURITY_VIOLATION;\r
0c18794e 1639 break;\r
1640 }\r
1641 //\r
1642 // If policy is always/never execute, return directly.\r
1643 //\r
1644 if (Policy == ALWAYS_EXECUTE) {\r
1645 return EFI_SUCCESS;\r
1646 } else if (Policy == NEVER_EXECUTE) {\r
1647 return EFI_ACCESS_DENIED;\r
1648 }\r
beda2356 1649\r
db44ea6c 1650 //\r
20333c6d 1651 // The policy QUERY_USER_ON_SECURITY_VIOLATION and ALLOW_EXECUTE_ON_SECURITY_VIOLATION\r
68fc0c73 1652 // violates the UEFI spec and has been removed.\r
db44ea6c 1653 //\r
68fc0c73
FS
1654 ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION && Policy != ALLOW_EXECUTE_ON_SECURITY_VIOLATION);\r
1655 if (Policy == QUERY_USER_ON_SECURITY_VIOLATION || Policy == ALLOW_EXECUTE_ON_SECURITY_VIOLATION) {\r
db44ea6c
FS
1656 CpuDeadLoop ();\r
1657 }\r
1658\r
560ac77e 1659 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);\r
beda2356 1660 //\r
8f8ca22e 1661 // Skip verification if SecureBoot variable doesn't exist.\r
beda2356 1662 //\r
560ac77e 1663 if (SecureBoot == NULL) {\r
beda2356 1664 return EFI_SUCCESS;\r
1665 }\r
1666\r
1667 //\r
4fc08e8d 1668 // Skip verification if SecureBoot is disabled but not AuditMode\r
beda2356 1669 //\r
560ac77e
ZC
1670 if (*SecureBoot == SECURE_BOOT_MODE_DISABLE) {\r
1671 FreePool (SecureBoot);\r
beda2356 1672 return EFI_SUCCESS;\r
45bf2c47 1673 }\r
560ac77e 1674 FreePool (SecureBoot);\r
551d8081 1675\r
0c18794e 1676 //\r
1677 // Read the Dos header.\r
1678 //\r
570b3d1a 1679 if (FileBuffer == NULL) {\r
570b3d1a 1680 return EFI_INVALID_PARAMETER;\r
1681 }\r
551d8081 1682\r
0c18794e 1683 mImageBase = (UINT8 *) FileBuffer;\r
1684 mImageSize = FileSize;\r
28186d45
ED
1685\r
1686 ZeroMem (&ImageContext, sizeof (ImageContext));\r
1687 ImageContext.Handle = (VOID *) FileBuffer;\r
e0192326 1688 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeImageVerificationLibImageRead;\r
28186d45
ED
1689\r
1690 //\r
1691 // Get information about the image being loaded\r
1692 //\r
1693 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
1694 if (EFI_ERROR (Status)) {\r
1695 //\r
1696 // The information can't be got from the invalid PeImage\r
1697 //\r
531c89a1 1698 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: PeImage invalid. Cannot retrieve image information.\n"));\r
28186d45
ED
1699 goto Done;\r
1700 }\r
1701\r
badd40f9 1702 Status = EFI_ACCESS_DENIED;\r
1703\r
1704 DosHdr = (EFI_IMAGE_DOS_HEADER *) mImageBase;\r
0c18794e 1705 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
1706 //\r
45bf2c47 1707 // DOS image header is present,\r
0c18794e 1708 // so read the PE header after the DOS image header.\r
1709 //\r
1710 mPeCoffHeaderOffset = DosHdr->e_lfanew;\r
1711 } else {\r
1712 mPeCoffHeaderOffset = 0;\r
1713 }\r
1714 //\r
1715 // Check PE/COFF image.\r
1716 //\r
1717 mNtHeader.Pe32 = (EFI_IMAGE_NT_HEADERS32 *) (mImageBase + mPeCoffHeaderOffset);\r
1718 if (mNtHeader.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
1719 //\r
1720 // It is not a valid Pe/Coff file.\r
1721 //\r
531c89a1 1722 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Not a valid PE/COFF image.\n"));\r
551d8081 1723 goto Done;\r
0c18794e 1724 }\r
1725\r
de2447dd 1726 if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
1727 //\r
20333c6d
QL
1728 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
1729 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
de2447dd 1730 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
1731 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
1732 //\r
1733 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
1734 } else {\r
1735 //\r
1736 // Get the magic value from the PE/COFF Optional Header\r
1737 //\r
1738 Magic = mNtHeader.Pe32->OptionalHeader.Magic;\r
1739 }\r
20333c6d 1740\r
0c18794e 1741 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
1742 //\r
1743 // Use PE32 offset.\r
1744 //\r
551d8081 1745 NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
1746 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
f6f9031f 1747 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
20333c6d 1748 }\r
570b3d1a 1749 } else {\r
1750 //\r
551d8081 1751 // Use PE32+ offset.\r
570b3d1a 1752 //\r
551d8081 1753 NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
1754 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
f6f9031f 1755 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
551d8081 1756 }\r
0c18794e 1757 }\r
1758\r
6de4c35f 1759 //\r
1760 // Start Image Validation.\r
1761 //\r
1762 if (SecDataDir == NULL || SecDataDir->Size == 0) {\r
0c18794e 1763 //\r
20333c6d 1764 // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db",\r
6de4c35f 1765 // and not be reflected in the security data base "dbx".\r
0c18794e 1766 //\r
45bf2c47 1767 if (!HashPeImage (HASHALG_SHA256)) {\r
531c89a1 1768 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Failed to hash this image using %s.\n", mHashTypeStr));\r
45bf2c47 1769 goto Done;\r
1770 }\r
1771\r
1772 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
1773 //\r
1774 // Image Hash is in forbidden database (DBX).\r
1775 //\r
531c89a1 1776 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is not signed and %s hash of image is forbidden by DBX.\n", mHashTypeStr));\r
45bf2c47 1777 goto Done;\r
1778 }\r
1779\r
1780 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
1781 //\r
1782 // Image Hash is in allowed database (DB).\r
1783 //\r
1784 return EFI_SUCCESS;\r
1785 }\r
1786\r
1787 //\r
1788 // Image Hash is not found in both forbidden and allowed database.\r
1789 //\r
531c89a1 1790 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is not signed and %s hash of image is not found in DB/DBX.\n", mHashTypeStr));\r
45bf2c47 1791 goto Done;\r
0c18794e 1792 }\r
45bf2c47 1793\r
0c18794e 1794 //\r
20333c6d 1795 // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7\r
6de4c35f 1796 // "Attribute Certificate Table".\r
1797 // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.\r
0c18794e 1798 //\r
6de4c35f 1799 for (OffSet = SecDataDir->VirtualAddress;\r
1800 OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);\r
2bf41ed7 1801 OffSet += (WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {\r
6de4c35f 1802 WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);\r
1803 if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||\r
1804 (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {\r
1805 break;\r
1806 }\r
20333c6d 1807\r
0c18794e 1808 //\r
6de4c35f 1809 // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.\r
0c18794e 1810 //\r
6de4c35f 1811 if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {\r
1812 //\r
20333c6d 1813 // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the\r
6de4c35f 1814 // Authenticode specification.\r
1815 //\r
1816 PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;\r
1817 if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {\r
1818 break;\r
1819 }\r
1820 AuthData = PkcsCertData->CertData;\r
1821 AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);\r
1822 } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {\r
1823 //\r
1824 // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.\r
1825 //\r
1826 WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;\r
1827 if (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
1828 break;\r
1829 }\r
1830 if (!CompareGuid (&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid)) {\r
1831 continue;\r
1832 }\r
1833 AuthData = WinCertUefiGuid->CertData;\r
1834 AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);\r
1835 } else {\r
1836 if (WinCertificate->dwLength < sizeof (WIN_CERTIFICATE)) {\r
1837 break;\r
1838 }\r
1839 continue;\r
84bce75b 1840 }\r
6de4c35f 1841\r
f6f9031f 1842 Status = HashPeImageByType (AuthData, AuthDataSize);\r
45bf2c47 1843 if (EFI_ERROR (Status)) {\r
6de4c35f 1844 continue;\r
0c18794e 1845 }\r
20333c6d 1846\r
f6f9031f 1847 //\r
6de4c35f 1848 // Check the digital signature against the revoked certificate in forbidden database (dbx).\r
f6f9031f 1849 //\r
560ac77e 1850 if (IsForbiddenByDbx (AuthData, AuthDataSize)) {\r
6de4c35f 1851 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;\r
1852 VerifyStatus = EFI_ACCESS_DENIED;\r
1853 break;\r
f6f9031f 1854 }\r
0c18794e 1855\r
1856 //\r
6de4c35f 1857 // Check the digital signature against the valid certificate in allowed database (db).\r
0c18794e 1858 //\r
6de4c35f 1859 if (EFI_ERROR (VerifyStatus)) {\r
560ac77e 1860 if (IsAllowedByDb (AuthData, AuthDataSize)) {\r
6de4c35f 1861 VerifyStatus = EFI_SUCCESS;\r
1862 }\r
0c18794e 1863 }\r
6de4c35f 1864\r
0c18794e 1865 //\r
6de4c35f 1866 // Check the image's hash value.\r
0c18794e 1867 //\r
6de4c35f 1868 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
1869 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;\r
531c89a1 1870 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but %s hash of image is found in DBX.\n", mHashTypeStr));\r
6de4c35f 1871 VerifyStatus = EFI_ACCESS_DENIED;\r
1872 break;\r
1873 } else if (EFI_ERROR (VerifyStatus)) {\r
1874 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
1875 VerifyStatus = EFI_SUCCESS;\r
531c89a1
CS
1876 } else {\r
1877 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but signature is not allowed by DB and %s hash of image is not found in DB/DBX.\n", mHashTypeStr));\r
6de4c35f 1878 }\r
45bf2c47 1879 }\r
50fe73a1 1880 }\r
1881\r
6de4c35f 1882 if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {\r
0c18794e 1883 //\r
6de4c35f 1884 // The Size in Certificate Table or the attribute certicate table is corrupted.\r
0c18794e 1885 //\r
6de4c35f 1886 VerifyStatus = EFI_ACCESS_DENIED;\r
1887 }\r
20333c6d 1888\r
6de4c35f 1889 if (!EFI_ERROR (VerifyStatus)) {\r
1890 return EFI_SUCCESS;\r
1891 } else {\r
1892 Status = EFI_ACCESS_DENIED;\r
1893 if (Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED || Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {\r
1894 //\r
1895 // Get image hash value as executable's signature.\r
1896 //\r
1897 SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;\r
1898 SignatureList = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);\r
1899 if (SignatureList == NULL) {\r
1900 Status = EFI_OUT_OF_RESOURCES;\r
1901 goto Done;\r
1902 }\r
1903 SignatureList->SignatureHeaderSize = 0;\r
1904 SignatureList->SignatureListSize = (UINT32) SignatureListSize;\r
13a220a9 1905 SignatureList->SignatureSize = (UINT32) (sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize);\r
6de4c35f 1906 CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));\r
1907 Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));\r
1908 CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);\r
50fe73a1 1909 }\r
0c18794e 1910 }\r
1911\r
1912Done:\r
1913 if (Status != EFI_SUCCESS) {\r
1914 //\r
1915 // Policy decides to defer or reject the image; add its information in image executable information table.\r
1916 //\r
213cc100
DG
1917 NameStr = ConvertDevicePathToText (File, FALSE, TRUE);\r
1918 AddImageExeInfo (Action, NameStr, File, SignatureList, SignatureListSize);\r
1919 if (NameStr != NULL) {\r
1920 DEBUG((EFI_D_INFO, "The image doesn't pass verification: %s\n", NameStr));\r
1921 FreePool(NameStr);\r
1922 }\r
5db28a67 1923 Status = EFI_SECURITY_VIOLATION;\r
0c18794e 1924 }\r
1925\r
1926 if (SignatureList != NULL) {\r
1927 FreePool (SignatureList);\r
1928 }\r
1929\r
0c18794e 1930 return Status;\r
1931}\r
1932\r
ffccb935
DG
1933/**\r
1934 On Ready To Boot Services Event notification handler.\r
1935\r
1936 Add the image execution information table if it is not in system configuration table.\r
1937\r
1938 @param[in] Event Event whose notification function is being invoked\r
1939 @param[in] Context Pointer to the notification function's context\r
1940\r
1941**/\r
1942VOID\r
1943EFIAPI\r
1944OnReadyToBoot (\r
1945 IN EFI_EVENT Event,\r
1946 IN VOID *Context\r
1947 )\r
1948{\r
1949 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable;\r
1950 UINTN ImageExeInfoTableSize;\r
1951\r
1952 EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);\r
1953 if (ImageExeInfoTable != NULL) {\r
1954 return;\r
1955 }\r
1956\r
1957 ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);\r
1958 ImageExeInfoTable = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize);\r
1959 if (ImageExeInfoTable == NULL) {\r
1960 return ;\r
1961 }\r
1962\r
20333c6d 1963 ImageExeInfoTable->NumberOfImages = 0;\r
ffccb935
DG
1964 gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) ImageExeInfoTable);\r
1965\r
1966}\r
1967\r
0c18794e 1968/**\r
1969 Register security measurement handler.\r
1970\r
1971 @param ImageHandle ImageHandle of the loaded driver.\r
1972 @param SystemTable Pointer to the EFI System Table.\r
1973\r
1974 @retval EFI_SUCCESS The handlers were registered successfully.\r
1975**/\r
1976EFI_STATUS\r
1977EFIAPI\r
1978DxeImageVerificationLibConstructor (\r
1979 IN EFI_HANDLE ImageHandle,\r
1980 IN EFI_SYSTEM_TABLE *SystemTable\r
1981 )\r
1982{\r
ffccb935
DG
1983 EFI_EVENT Event;\r
1984\r
1985 //\r
1986 // Register the event to publish the image execution table.\r
1987 //\r
1988 EfiCreateEventReadyToBootEx (\r
1989 TPL_CALLBACK,\r
20333c6d
QL
1990 OnReadyToBoot,\r
1991 NULL,\r
ffccb935 1992 &Event\r
20333c6d 1993 );\r
ffccb935 1994\r
5db28a67 1995 return RegisterSecurity2Handler (\r
0c18794e 1996 DxeImageVerificationHandler,\r
1997 EFI_AUTH_OPERATION_VERIFY_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r
45bf2c47 1998 );\r
0c18794e 1999}\r