]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
ShellPkg SmbiosView: Add decoding of SMBIOS spec 3.1.0
[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
91422384 15Copyright (c) 2009 - 2016, 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
394 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.CheckSum) - 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
400 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.CheckSum) - 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
428 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);\r
429 } else {\r
430 //\r
431 // Use PE32+ offset.\r
432 //\r
433 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
434 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);\r
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
452 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
453 } else {\r
454 //\r
455 // Use PE32+ offset.\r
456 //\r
457 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
458 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
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
477 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);\r
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
483 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);\r
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
c1d93242 1029 SecureBootHook (VariableName, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, Cert);\r
0c18794e 1030 break;\r
1031 }\r
1032\r
1033 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
1034 }\r
1035\r
1036 if (IsFound) {\r
1037 break;\r
1038 }\r
1039 }\r
1040\r
1041 DataSize -= CertList->SignatureListSize;\r
1042 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1043 }\r
1044\r
1045Done:\r
1046 if (Data != NULL) {\r
1047 FreePool (Data);\r
1048 }\r
1049\r
1050 return IsFound;\r
1051}\r
1052\r
1053/**\r
20333c6d 1054 Check whether the timestamp is valid by comparing the signing time and the revocation time.\r
0c18794e 1055\r
20333c6d
QL
1056 @param SigningTime A pointer to the signing time.\r
1057 @param RevocationTime A pointer to the revocation time.\r
45bf2c47 1058\r
20333c6d
QL
1059 @retval TRUE The SigningTime is not later than the RevocationTime.\r
1060 @retval FALSE The SigningTime is later than the RevocationTime.\r
0c18794e 1061\r
1062**/\r
45bf2c47 1063BOOLEAN\r
20333c6d
QL
1064IsValidSignatureByTimestamp (\r
1065 IN EFI_TIME *SigningTime,\r
1066 IN EFI_TIME *RevocationTime\r
1067 )\r
1068{\r
1069 if (SigningTime->Year != RevocationTime->Year) {\r
1070 return (BOOLEAN) (SigningTime->Year < RevocationTime->Year);\r
1071 } else if (SigningTime->Month != RevocationTime->Month) {\r
1072 return (BOOLEAN) (SigningTime->Month < RevocationTime->Month);\r
1073 } else if (SigningTime->Day != RevocationTime->Day) {\r
1074 return (BOOLEAN) (SigningTime->Day < RevocationTime->Day);\r
1075 } else if (SigningTime->Hour != RevocationTime->Hour) {\r
1076 return (BOOLEAN) (SigningTime->Hour < RevocationTime->Hour);\r
1077 } else if (SigningTime->Minute != RevocationTime->Minute) {\r
1078 return (BOOLEAN) (SigningTime->Minute < RevocationTime->Minute);\r
1079 }\r
1080\r
1081 return (BOOLEAN) (SigningTime->Second <= RevocationTime->Second);\r
1082}\r
1083\r
1084/**\r
1085 Check if the given time value is zero.\r
1086\r
1087 @param[in] Time Pointer of a time value.\r
1088\r
1089 @retval TRUE The Time is Zero.\r
1090 @retval FALSE The Time is not Zero.\r
1091\r
1092**/\r
1093BOOLEAN\r
1094IsTimeZero (\r
1095 IN EFI_TIME *Time\r
1096 )\r
1097{\r
1098 if ((Time->Year == 0) && (Time->Month == 0) && (Time->Day == 0) &&\r
1099 (Time->Hour == 0) && (Time->Minute == 0) && (Time->Second == 0)) {\r
1100 return TRUE;\r
1101 }\r
1102\r
1103 return FALSE;\r
1104}\r
1105\r
1106/**\r
1107 Check whether the timestamp signature is valid and the signing time is also earlier than \r
1108 the revocation time.\r
1109\r
1110 @param[in] AuthData Pointer to the Authenticode signature retrieved from signed image.\r
1111 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
1112 @param[in] RevocationTime The time that the certificate was revoked.\r
1113\r
1114 @retval TRUE Timestamp signature is valid and signing time is no later than the \r
1115 revocation time.\r
1116 @retval FALSE Timestamp signature is not valid or the signing time is later than the\r
1117 revocation time.\r
1118\r
1119**/\r
1120BOOLEAN\r
1121PassTimestampCheck (\r
1122 IN UINT8 *AuthData,\r
1123 IN UINTN AuthDataSize,\r
1124 IN EFI_TIME *RevocationTime\r
1125 )\r
1126{\r
1127 EFI_STATUS Status;\r
1128 BOOLEAN VerifyStatus;\r
1129 EFI_SIGNATURE_LIST *CertList;\r
1130 EFI_SIGNATURE_DATA *Cert;\r
1131 UINT8 *DbtData;\r
1132 UINTN DbtDataSize;\r
1133 UINT8 *RootCert;\r
1134 UINTN RootCertSize;\r
1135 UINTN Index;\r
1136 UINTN CertCount;\r
1137 EFI_TIME SigningTime;\r
1138\r
1139 //\r
1140 // Variable Initialization\r
1141 //\r
1142 VerifyStatus = FALSE;\r
1143 DbtData = NULL;\r
1144 CertList = NULL;\r
1145 Cert = NULL;\r
1146 RootCert = NULL;\r
1147 RootCertSize = 0;\r
1148\r
1149 //\r
1150 // If RevocationTime is zero, the certificate shall be considered to always be revoked.\r
1151 //\r
1152 if (IsTimeZero (RevocationTime)) {\r
1153 return FALSE;\r
1154 }\r
1155\r
1156 //\r
1157 // RevocationTime is non-zero, the certificate should be considered to be revoked from that time and onwards.\r
1158 // Using the dbt to get the trusted TSA certificates.\r
1159 //\r
1160 DbtDataSize = 0;\r
1161 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, NULL);\r
7e0699c0
QL
1162 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1163 goto Done;\r
1164 }\r
1165 DbtData = (UINT8 *) AllocateZeroPool (DbtDataSize);\r
1166 if (DbtData == NULL) {\r
1167 goto Done;\r
1168 }\r
1169 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, (VOID *) DbtData);\r
1170 if (EFI_ERROR (Status)) {\r
1171 goto Done;\r
20333c6d
QL
1172 }\r
1173\r
1174 CertList = (EFI_SIGNATURE_LIST *) DbtData;\r
1175 while ((DbtDataSize > 0) && (DbtDataSize >= CertList->SignatureListSize)) {\r
1176 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
1177 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1178 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
1179 for (Index = 0; Index < CertCount; Index++) {\r
1180 //\r
1181 // Iterate each Signature Data Node within this CertList for verify.\r
1182 //\r
1183 RootCert = Cert->SignatureData;\r
1184 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
1185 //\r
1186 // Get the signing time if the timestamp signature is valid.\r
1187 //\r
1188 if (ImageTimestampVerify (AuthData, AuthDataSize, RootCert, RootCertSize, &SigningTime)) {\r
1189 //\r
1190 // The signer signature is valid only when the signing time is earlier than revocation time.\r
1191 //\r
1192 if (IsValidSignatureByTimestamp (&SigningTime, RevocationTime)) {\r
1193 VerifyStatus = TRUE;\r
1194 goto Done;\r
1195 }\r
1196 }\r
1197 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
1198 }\r
1199 }\r
1200 DbtDataSize -= CertList->SignatureListSize;\r
1201 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1202 }\r
1203\r
1204Done:\r
1205 if (DbtData != NULL) {\r
1206 FreePool (DbtData);\r
1207 }\r
1208\r
1209 return VerifyStatus;\r
1210}\r
1211\r
1212/**\r
1213 Check whether the image signature is forbidden by the forbidden database (dbx).\r
1214 The image is forbidden to load if any certificates for signing are revoked before signing time.\r
1215\r
560ac77e
ZC
1216 @param[in] AuthData Pointer to the Authenticode signature retrieved from the signed image.\r
1217 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
20333c6d
QL
1218\r
1219 @retval TRUE Image is forbidden by dbx.\r
1220 @retval FALSE Image is not forbidden by dbx.\r
1221\r
1222**/\r
1223BOOLEAN\r
560ac77e
ZC
1224IsForbiddenByDbx ( \r
1225 IN UINT8 *AuthData,\r
1226 IN UINTN AuthDataSize \r
20333c6d
QL
1227 )\r
1228{\r
1229 EFI_STATUS Status;\r
1230 BOOLEAN IsForbidden;\r
1231 UINT8 *Data;\r
1232 UINTN DataSize;\r
27c93c06
LQ
1233 EFI_SIGNATURE_LIST *CertList;\r
1234 UINTN CertListSize;\r
1235 EFI_SIGNATURE_DATA *CertData;\r
1236 UINT8 *RootCert;\r
1237 UINTN RootCertSize;\r
1238 UINTN CertCount;\r
20333c6d
QL
1239 UINTN Index;\r
1240 UINT8 *CertBuffer;\r
1241 UINTN BufferLength;\r
1242 UINT8 *TrustedCert;\r
1243 UINTN TrustedCertLength;\r
1244 UINT8 CertNumber;\r
1245 UINT8 *CertPtr;\r
1246 UINT8 *Cert;\r
1247 UINTN CertSize;\r
1248 EFI_TIME RevocationTime;\r
20333c6d
QL
1249 //\r
1250 // Variable Initialization\r
1251 //\r
1252 IsForbidden = FALSE;\r
1253 Data = NULL;\r
27c93c06
LQ
1254 CertList = NULL;\r
1255 CertData = NULL;\r
1256 RootCert = NULL;\r
1257 RootCertSize = 0;\r
20333c6d
QL
1258 Cert = NULL;\r
1259 CertBuffer = NULL;\r
1260 BufferLength = 0;\r
1261 TrustedCert = NULL;\r
1262 TrustedCertLength = 0;\r
1263\r
1264 //\r
1265 // The image will not be forbidden if dbx can't be got.\r
1266 //\r
1267 DataSize = 0;\r
1268 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
7e0699c0
QL
1269 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1270 return IsForbidden;\r
20333c6d 1271 }\r
7e0699c0
QL
1272 Data = (UINT8 *) AllocateZeroPool (DataSize);\r
1273 if (Data == NULL) {\r
1274 return IsForbidden;\r
1275 }\r
1276\r
1277 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
20333c6d
QL
1278 if (EFI_ERROR (Status)) {\r
1279 return IsForbidden;\r
1280 }\r
1281\r
27c93c06
LQ
1282 //\r
1283 // Verify image signature with RAW X509 certificates in DBX database.\r
1284 // If passed, the image will be forbidden.\r
1285 //\r
1286 CertList = (EFI_SIGNATURE_LIST *) Data;\r
1287 CertListSize = DataSize;\r
1288 while ((CertListSize > 0) && (CertListSize >= CertList->SignatureListSize)) {\r
1289 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
1290 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1291 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
1292\r
1293 for (Index = 0; Index < CertCount; Index++) {\r
1294 //\r
1295 // Iterate each Signature Data Node within this CertList for verify.\r
1296 //\r
1297 RootCert = CertData->SignatureData;\r
1298 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
1299\r
1300 //\r
1301 // Call AuthenticodeVerify library to Verify Authenticode struct.\r
1302 //\r
1303 IsForbidden = AuthenticodeVerify (\r
1304 AuthData,\r
1305 AuthDataSize,\r
1306 RootCert,\r
1307 RootCertSize,\r
1308 mImageDigest,\r
1309 mImageDigestSize\r
1310 );\r
1311 if (IsForbidden) {\r
d863e127 1312 SecureBootHook (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, CertData);\r
531c89a1 1313 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but signature is forbidden by DBX.\n"));\r
27c93c06
LQ
1314 goto Done;\r
1315 }\r
1316\r
1317 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
1318 }\r
1319 }\r
1320\r
1321 CertListSize -= CertList->SignatureListSize;\r
1322 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1323 }\r
1324\r
1325 //\r
1326 // Check X.509 Certificate Hash & Possible Timestamp.\r
1327 //\r
1328\r
20333c6d
QL
1329 //\r
1330 // Retrieve the certificate stack from AuthData\r
1331 // The output CertStack format will be:\r
1332 // UINT8 CertNumber;\r
1333 // UINT32 Cert1Length;\r
1334 // UINT8 Cert1[];\r
1335 // UINT32 Cert2Length;\r
1336 // UINT8 Cert2[];\r
1337 // ...\r
1338 // UINT32 CertnLength;\r
1339 // UINT8 Certn[];\r
1340 //\r
1341 Pkcs7GetSigners (AuthData, AuthDataSize, &CertBuffer, &BufferLength, &TrustedCert, &TrustedCertLength);\r
7e0699c0 1342 if ((BufferLength == 0) || (CertBuffer == NULL)) {\r
20333c6d
QL
1343 IsForbidden = TRUE;\r
1344 goto Done;\r
1345 }\r
1346\r
1347 //\r
27c93c06 1348 // Check if any hash of certificates embedded in AuthData is in the forbidden database.\r
20333c6d
QL
1349 //\r
1350 CertNumber = (UINT8) (*CertBuffer);\r
1351 CertPtr = CertBuffer + 1;\r
1352 for (Index = 0; Index < CertNumber; Index++) {\r
1353 CertSize = (UINTN) ReadUnaligned32 ((UINT32 *)CertPtr);\r
1354 Cert = (UINT8 *)CertPtr + sizeof (UINT32);\r
91422384
ZC
1355 //\r
1356 // Advance CertPtr to the next cert in image signer's cert list\r
1357 //\r
1358 CertPtr = CertPtr + sizeof (UINT32) + CertSize;\r
20333c6d
QL
1359\r
1360 if (IsCertHashFoundInDatabase (Cert, CertSize, (EFI_SIGNATURE_LIST *)Data, DataSize, &RevocationTime)) {\r
1361 //\r
1362 // Check the timestamp signature and signing time to determine if the image can be trusted.\r
1363 //\r
1364 IsForbidden = TRUE;\r
1365 if (PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime)) {\r
1366 IsForbidden = FALSE;\r
91422384
ZC
1367 //\r
1368 // Pass DBT check. Continue to check other certs in image signer's cert list against DBX, DBT\r
1369 //\r
1370 continue;\r
20333c6d 1371 }\r
531c89a1 1372 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but signature failed the timestamp check.\n"));\r
20333c6d
QL
1373 goto Done;\r
1374 }\r
1375\r
20333c6d
QL
1376 }\r
1377\r
1378Done:\r
1379 if (Data != NULL) {\r
1380 FreePool (Data);\r
1381 }\r
1382\r
1383 Pkcs7FreeSigners (CertBuffer);\r
1384 Pkcs7FreeSigners (TrustedCert);\r
1385\r
1386 return IsForbidden;\r
1387}\r
1388\r
4fc08e8d 1389\r
20333c6d
QL
1390/**\r
1391 Check whether the image signature can be verified by the trusted certificates in DB database.\r
1392\r
560ac77e
ZC
1393 @param[in] AuthData Pointer to the Authenticode signature retrieved from signed image.\r
1394 @param[in] AuthDataSize Size of the Authenticode signature in bytes.\r
20333c6d
QL
1395\r
1396 @retval TRUE Image passed verification using certificate in db.\r
1397 @retval FALSE Image didn't pass verification using certificate in db.\r
1398\r
1399**/\r
1400BOOLEAN\r
1401IsAllowedByDb (\r
560ac77e
ZC
1402 IN UINT8 *AuthData,\r
1403 IN UINTN AuthDataSize\r
0c18794e 1404 )\r
1405{\r
1406 EFI_STATUS Status;\r
1407 BOOLEAN VerifyStatus;\r
0c18794e 1408 EFI_SIGNATURE_LIST *CertList;\r
4fc08e8d 1409 EFI_SIGNATURE_DATA *CertData;\r
0c18794e 1410 UINTN DataSize;\r
45bf2c47 1411 UINT8 *Data;\r
0c18794e 1412 UINT8 *RootCert;\r
1413 UINTN RootCertSize;\r
1414 UINTN Index;\r
1415 UINTN CertCount;\r
27c93c06
LQ
1416 UINTN DbxDataSize;\r
1417 UINT8 *DbxData;\r
1418 EFI_TIME RevocationTime;\r
0c18794e 1419\r
4fc08e8d
CZ
1420 Data = NULL;\r
1421 CertList = NULL;\r
1422 CertData = NULL;\r
1423 RootCert = NULL;\r
1424 DbxData = NULL;\r
1425 RootCertSize = 0;\r
1426 VerifyStatus = FALSE;\r
0c18794e 1427\r
0c18794e 1428 DataSize = 0;\r
20333c6d 1429 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
0c18794e 1430 if (Status == EFI_BUFFER_TOO_SMALL) {\r
45bf2c47 1431 Data = (UINT8 *) AllocateZeroPool (DataSize);\r
1432 if (Data == NULL) {\r
1433 return VerifyStatus;\r
570b3d1a 1434 }\r
0c18794e 1435\r
20333c6d 1436 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
0c18794e 1437 if (EFI_ERROR (Status)) {\r
1438 goto Done;\r
1439 }\r
45bf2c47 1440\r
1441 //\r
1442 // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.\r
0c18794e 1443 //\r
45bf2c47 1444 CertList = (EFI_SIGNATURE_LIST *) Data;\r
0c18794e 1445 while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {\r
1446 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
4fc08e8d
CZ
1447 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
1448 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
20333c6d 1449\r
0c18794e 1450 for (Index = 0; Index < CertCount; Index++) {\r
1451 //\r
45bf2c47 1452 // Iterate each Signature Data Node within this CertList for verify.\r
1453 //\r
4fc08e8d 1454 RootCert = CertData->SignatureData;\r
20333c6d 1455 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
45bf2c47 1456\r
0c18794e 1457 //\r
45bf2c47 1458 // Call AuthenticodeVerify library to Verify Authenticode struct.\r
0c18794e 1459 //\r
1460 VerifyStatus = AuthenticodeVerify (\r
f6f9031f 1461 AuthData,\r
1462 AuthDataSize,\r
0c18794e 1463 RootCert,\r
1464 RootCertSize,\r
1465 mImageDigest,\r
1466 mImageDigestSize\r
1467 );\r
0c18794e 1468 if (VerifyStatus) {\r
27c93c06
LQ
1469 //\r
1470 // Here We still need to check if this RootCert's Hash is revoked\r
1471 //\r
1472 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, NULL);\r
1473 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1474 goto Done;\r
1475 }\r
1ca3a099 1476 DbxData = (UINT8 *) AllocateZeroPool (DbxDataSize);\r
27c93c06
LQ
1477 if (DbxData == NULL) {\r
1478 goto Done;\r
1479 }\r
1480\r
1481 Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, (VOID *) DbxData);\r
1482 if (EFI_ERROR (Status)) {\r
1483 goto Done;\r
1484 }\r
1485\r
1486 if (IsCertHashFoundInDatabase (RootCert, RootCertSize, (EFI_SIGNATURE_LIST *)DbxData, DbxDataSize, &RevocationTime)) {\r
1487 //\r
531c89a1 1488 // Check the timestamp signature and signing time to determine if the RootCert can be trusted.\r
27c93c06
LQ
1489 //\r
1490 VerifyStatus = PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime);\r
531c89a1
CS
1491 if (!VerifyStatus) {\r
1492 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed and signature is accepted by DB, but its root cert failed the timestamp check.\n"));\r
1493 }\r
27c93c06
LQ
1494 }\r
1495\r
0c18794e 1496 goto Done;\r
1497 }\r
20333c6d 1498\r
4fc08e8d 1499 CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
45bf2c47 1500 }\r
0c18794e 1501 }\r
20333c6d 1502\r
0c18794e 1503 DataSize -= CertList->SignatureListSize;\r
1504 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
1505 }\r
1506 }\r
1507\r
45bf2c47 1508Done:\r
4fc08e8d 1509\r
27c93c06 1510 if (VerifyStatus) {\r
4fc08e8d 1511 SecureBootHook (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, CertData);\r
27c93c06
LQ
1512 }\r
1513\r
45bf2c47 1514 if (Data != NULL) {\r
1515 FreePool (Data);\r
1516 }\r
27c93c06
LQ
1517 if (DbxData != NULL) {\r
1518 FreePool (DbxData);\r
1519 }\r
0c18794e 1520\r
45bf2c47 1521 return VerifyStatus;\r
1522}\r
0c18794e 1523\r
0c18794e 1524/**\r
1525 Provide verification service for signed images, which include both signature validation\r
45bf2c47 1526 and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and\r
0c18794e 1527 MSFT Authenticode type signatures are supported.\r
0c18794e 1528\r
45bf2c47 1529 In this implementation, only verify external executables when in USER MODE.\r
1530 Executables from FV is bypass, so pass in AuthenticationStatus is ignored.\r
1531\r
6de4c35f 1532 The image verification policy is:\r
50fe73a1 1533 If the image is signed,\r
6de4c35f 1534 At least one valid signature or at least one hash value of the image must match a record\r
1535 in the security database "db", and no valid signature nor any hash value of the image may\r
1536 be reflected in the security database "dbx".\r
50fe73a1 1537 Otherwise, the image is not signed,\r
6de4c35f 1538 The SHA256 hash value of the image must match a record in the security database "db", and\r
1539 not be reflected in the security data base "dbx".\r
45bf2c47 1540\r
dc204d5a
JY
1541 Caution: This function may receive untrusted input.\r
1542 PE/COFF image is external input, so this function will validate its data structure\r
1543 within this image buffer before use.\r
1544\r
45bf2c47 1545 @param[in] AuthenticationStatus\r
0c18794e 1546 This is the authentication status returned from the security\r
1547 measurement services for the input file.\r
1548 @param[in] File This is a pointer to the device path of the file that is\r
1549 being dispatched. This will optionally be used for logging.\r
1550 @param[in] FileBuffer File buffer matches the input file device path.\r
1551 @param[in] FileSize Size of File buffer matches the input file device path.\r
5db28a67
LG
1552 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
1553\r
1554 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL\r
1555 FileBuffer did authenticate, and the platform policy dictates\r
1556 that the DXE Foundation may use the file.\r
1557 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath\r
1558 and non-NULL FileBuffer did authenticate, and the platform\r
1559 policy dictates that the DXE Foundation may execute the image in\r
1560 FileBuffer.\r
570b3d1a 1561 @retval EFI_OUT_RESOURCE Fail to allocate memory.\r
0c18794e 1562 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
1563 the platform policy dictates that File should be placed\r
5db28a67
LG
1564 in the untrusted state. The image has been added to the file\r
1565 execution table.\r
1566 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not\r
1567 authenticate, and the platform policy dictates that the DXE\r
1568 Foundation many not use File.\r
0c18794e 1569\r
1570**/\r
1571EFI_STATUS\r
1572EFIAPI\r
1573DxeImageVerificationHandler (\r
1574 IN UINT32 AuthenticationStatus,\r
1575 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
1576 IN VOID *FileBuffer,\r
5db28a67
LG
1577 IN UINTN FileSize,\r
1578 IN BOOLEAN BootPolicy\r
0c18794e 1579 )\r
0c18794e 1580{\r
551d8081 1581 EFI_STATUS Status;\r
1582 UINT16 Magic;\r
1583 EFI_IMAGE_DOS_HEADER *DosHdr;\r
1584 EFI_STATUS VerifyStatus;\r
551d8081 1585 EFI_SIGNATURE_LIST *SignatureList;\r
1586 UINTN SignatureListSize;\r
1587 EFI_SIGNATURE_DATA *Signature;\r
1588 EFI_IMAGE_EXECUTION_ACTION Action;\r
1589 WIN_CERTIFICATE *WinCertificate;\r
1590 UINT32 Policy;\r
560ac77e 1591 UINT8 *SecureBoot;\r
551d8081 1592 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
1593 UINT32 NumberOfRvaAndSizes;\r
f6f9031f 1594 WIN_CERTIFICATE_EFI_PKCS *PkcsCertData;\r
1595 WIN_CERTIFICATE_UEFI_GUID *WinCertUefiGuid;\r
1596 UINT8 *AuthData;\r
1597 UINTN AuthDataSize;\r
1598 EFI_IMAGE_DATA_DIRECTORY *SecDataDir;\r
6de4c35f 1599 UINT32 OffSet;\r
213cc100 1600 CHAR16 *NameStr;\r
0c18794e 1601\r
0c18794e 1602 SignatureList = NULL;\r
1603 SignatureListSize = 0;\r
1604 WinCertificate = NULL;\r
f6f9031f 1605 SecDataDir = NULL;\r
1606 PkcsCertData = NULL;\r
0c18794e 1607 Action = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;\r
1608 Status = EFI_ACCESS_DENIED;\r
6de4c35f 1609 VerifyStatus = EFI_ACCESS_DENIED;\r
1610\r
4fc08e8d 1611\r
0c18794e 1612 //\r
1613 // Check the image type and get policy setting.\r
1614 //\r
1615 switch (GetImageType (File)) {\r
45bf2c47 1616\r
0c18794e 1617 case IMAGE_FROM_FV:\r
1618 Policy = ALWAYS_EXECUTE;\r
1619 break;\r
1620\r
1621 case IMAGE_FROM_OPTION_ROM:\r
1622 Policy = PcdGet32 (PcdOptionRomImageVerificationPolicy);\r
1623 break;\r
1624\r
1625 case IMAGE_FROM_REMOVABLE_MEDIA:\r
1626 Policy = PcdGet32 (PcdRemovableMediaImageVerificationPolicy);\r
1627 break;\r
1628\r
1629 case IMAGE_FROM_FIXED_MEDIA:\r
1630 Policy = PcdGet32 (PcdFixedMediaImageVerificationPolicy);\r
1631 break;\r
1632\r
1633 default:\r
45bf2c47 1634 Policy = DENY_EXECUTE_ON_SECURITY_VIOLATION;\r
0c18794e 1635 break;\r
1636 }\r
1637 //\r
1638 // If policy is always/never execute, return directly.\r
1639 //\r
1640 if (Policy == ALWAYS_EXECUTE) {\r
1641 return EFI_SUCCESS;\r
1642 } else if (Policy == NEVER_EXECUTE) {\r
1643 return EFI_ACCESS_DENIED;\r
1644 }\r
beda2356 1645\r
db44ea6c 1646 //\r
20333c6d 1647 // The policy QUERY_USER_ON_SECURITY_VIOLATION and ALLOW_EXECUTE_ON_SECURITY_VIOLATION\r
68fc0c73 1648 // violates the UEFI spec and has been removed.\r
db44ea6c 1649 //\r
68fc0c73
FS
1650 ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION && Policy != ALLOW_EXECUTE_ON_SECURITY_VIOLATION);\r
1651 if (Policy == QUERY_USER_ON_SECURITY_VIOLATION || Policy == ALLOW_EXECUTE_ON_SECURITY_VIOLATION) {\r
db44ea6c
FS
1652 CpuDeadLoop ();\r
1653 }\r
1654\r
560ac77e 1655 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);\r
beda2356 1656 //\r
8f8ca22e 1657 // Skip verification if SecureBoot variable doesn't exist.\r
beda2356 1658 //\r
560ac77e 1659 if (SecureBoot == NULL) {\r
beda2356 1660 return EFI_SUCCESS;\r
1661 }\r
1662\r
1663 //\r
4fc08e8d 1664 // Skip verification if SecureBoot is disabled but not AuditMode\r
beda2356 1665 //\r
560ac77e
ZC
1666 if (*SecureBoot == SECURE_BOOT_MODE_DISABLE) {\r
1667 FreePool (SecureBoot);\r
beda2356 1668 return EFI_SUCCESS;\r
45bf2c47 1669 }\r
560ac77e 1670 FreePool (SecureBoot);\r
551d8081 1671\r
0c18794e 1672 //\r
1673 // Read the Dos header.\r
1674 //\r
570b3d1a 1675 if (FileBuffer == NULL) {\r
570b3d1a 1676 return EFI_INVALID_PARAMETER;\r
1677 }\r
551d8081 1678\r
0c18794e 1679 mImageBase = (UINT8 *) FileBuffer;\r
1680 mImageSize = FileSize;\r
28186d45
ED
1681\r
1682 ZeroMem (&ImageContext, sizeof (ImageContext));\r
1683 ImageContext.Handle = (VOID *) FileBuffer;\r
e0192326 1684 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeImageVerificationLibImageRead;\r
28186d45
ED
1685\r
1686 //\r
1687 // Get information about the image being loaded\r
1688 //\r
1689 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
1690 if (EFI_ERROR (Status)) {\r
1691 //\r
1692 // The information can't be got from the invalid PeImage\r
1693 //\r
531c89a1 1694 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: PeImage invalid. Cannot retrieve image information.\n"));\r
28186d45
ED
1695 goto Done;\r
1696 }\r
1697\r
badd40f9 1698 Status = EFI_ACCESS_DENIED;\r
1699\r
1700 DosHdr = (EFI_IMAGE_DOS_HEADER *) mImageBase;\r
0c18794e 1701 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
1702 //\r
45bf2c47 1703 // DOS image header is present,\r
0c18794e 1704 // so read the PE header after the DOS image header.\r
1705 //\r
1706 mPeCoffHeaderOffset = DosHdr->e_lfanew;\r
1707 } else {\r
1708 mPeCoffHeaderOffset = 0;\r
1709 }\r
1710 //\r
1711 // Check PE/COFF image.\r
1712 //\r
1713 mNtHeader.Pe32 = (EFI_IMAGE_NT_HEADERS32 *) (mImageBase + mPeCoffHeaderOffset);\r
1714 if (mNtHeader.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
1715 //\r
1716 // It is not a valid Pe/Coff file.\r
1717 //\r
531c89a1 1718 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Not a valid PE/COFF image.\n"));\r
551d8081 1719 goto Done;\r
0c18794e 1720 }\r
1721\r
de2447dd 1722 if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
1723 //\r
20333c6d
QL
1724 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
1725 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
de2447dd 1726 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
1727 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
1728 //\r
1729 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
1730 } else {\r
1731 //\r
1732 // Get the magic value from the PE/COFF Optional Header\r
1733 //\r
1734 Magic = mNtHeader.Pe32->OptionalHeader.Magic;\r
1735 }\r
20333c6d 1736\r
0c18794e 1737 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
1738 //\r
1739 // Use PE32 offset.\r
1740 //\r
551d8081 1741 NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
1742 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
f6f9031f 1743 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
20333c6d 1744 }\r
570b3d1a 1745 } else {\r
1746 //\r
551d8081 1747 // Use PE32+ offset.\r
570b3d1a 1748 //\r
551d8081 1749 NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
1750 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
f6f9031f 1751 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
551d8081 1752 }\r
0c18794e 1753 }\r
1754\r
6de4c35f 1755 //\r
1756 // Start Image Validation.\r
1757 //\r
1758 if (SecDataDir == NULL || SecDataDir->Size == 0) {\r
0c18794e 1759 //\r
20333c6d 1760 // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db",\r
6de4c35f 1761 // and not be reflected in the security data base "dbx".\r
0c18794e 1762 //\r
45bf2c47 1763 if (!HashPeImage (HASHALG_SHA256)) {\r
531c89a1 1764 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Failed to hash this image using %s.\n", mHashTypeStr));\r
45bf2c47 1765 goto Done;\r
1766 }\r
1767\r
1768 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
1769 //\r
1770 // Image Hash is in forbidden database (DBX).\r
1771 //\r
531c89a1 1772 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is not signed and %s hash of image is forbidden by DBX.\n", mHashTypeStr));\r
45bf2c47 1773 goto Done;\r
1774 }\r
1775\r
1776 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
1777 //\r
1778 // Image Hash is in allowed database (DB).\r
1779 //\r
1780 return EFI_SUCCESS;\r
1781 }\r
1782\r
1783 //\r
1784 // Image Hash is not found in both forbidden and allowed database.\r
1785 //\r
531c89a1 1786 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is not signed and %s hash of image is not found in DB/DBX.\n", mHashTypeStr));\r
45bf2c47 1787 goto Done;\r
0c18794e 1788 }\r
45bf2c47 1789\r
0c18794e 1790 //\r
20333c6d 1791 // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7\r
6de4c35f 1792 // "Attribute Certificate Table".\r
1793 // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.\r
0c18794e 1794 //\r
6de4c35f 1795 for (OffSet = SecDataDir->VirtualAddress;\r
1796 OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);\r
2bf41ed7 1797 OffSet += (WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {\r
6de4c35f 1798 WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);\r
1799 if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||\r
1800 (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {\r
1801 break;\r
1802 }\r
20333c6d 1803\r
0c18794e 1804 //\r
6de4c35f 1805 // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.\r
0c18794e 1806 //\r
6de4c35f 1807 if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {\r
1808 //\r
20333c6d 1809 // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the\r
6de4c35f 1810 // Authenticode specification.\r
1811 //\r
1812 PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;\r
1813 if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {\r
1814 break;\r
1815 }\r
1816 AuthData = PkcsCertData->CertData;\r
1817 AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);\r
1818 } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {\r
1819 //\r
1820 // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.\r
1821 //\r
1822 WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;\r
1823 if (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
1824 break;\r
1825 }\r
1826 if (!CompareGuid (&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid)) {\r
1827 continue;\r
1828 }\r
1829 AuthData = WinCertUefiGuid->CertData;\r
1830 AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);\r
1831 } else {\r
1832 if (WinCertificate->dwLength < sizeof (WIN_CERTIFICATE)) {\r
1833 break;\r
1834 }\r
1835 continue;\r
84bce75b 1836 }\r
6de4c35f 1837\r
f6f9031f 1838 Status = HashPeImageByType (AuthData, AuthDataSize);\r
45bf2c47 1839 if (EFI_ERROR (Status)) {\r
6de4c35f 1840 continue;\r
0c18794e 1841 }\r
20333c6d 1842\r
f6f9031f 1843 //\r
6de4c35f 1844 // Check the digital signature against the revoked certificate in forbidden database (dbx).\r
f6f9031f 1845 //\r
560ac77e 1846 if (IsForbiddenByDbx (AuthData, AuthDataSize)) {\r
6de4c35f 1847 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;\r
1848 VerifyStatus = EFI_ACCESS_DENIED;\r
1849 break;\r
f6f9031f 1850 }\r
0c18794e 1851\r
1852 //\r
6de4c35f 1853 // Check the digital signature against the valid certificate in allowed database (db).\r
0c18794e 1854 //\r
6de4c35f 1855 if (EFI_ERROR (VerifyStatus)) {\r
560ac77e 1856 if (IsAllowedByDb (AuthData, AuthDataSize)) {\r
6de4c35f 1857 VerifyStatus = EFI_SUCCESS;\r
1858 }\r
0c18794e 1859 }\r
6de4c35f 1860\r
0c18794e 1861 //\r
6de4c35f 1862 // Check the image's hash value.\r
0c18794e 1863 //\r
6de4c35f 1864 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
1865 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;\r
531c89a1 1866 DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but %s hash of image is found in DBX.\n", mHashTypeStr));\r
6de4c35f 1867 VerifyStatus = EFI_ACCESS_DENIED;\r
1868 break;\r
1869 } else if (EFI_ERROR (VerifyStatus)) {\r
1870 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
1871 VerifyStatus = EFI_SUCCESS;\r
531c89a1
CS
1872 } else {\r
1873 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 1874 }\r
45bf2c47 1875 }\r
50fe73a1 1876 }\r
1877\r
6de4c35f 1878 if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {\r
0c18794e 1879 //\r
6de4c35f 1880 // The Size in Certificate Table or the attribute certicate table is corrupted.\r
0c18794e 1881 //\r
6de4c35f 1882 VerifyStatus = EFI_ACCESS_DENIED;\r
1883 }\r
20333c6d 1884\r
6de4c35f 1885 if (!EFI_ERROR (VerifyStatus)) {\r
1886 return EFI_SUCCESS;\r
1887 } else {\r
1888 Status = EFI_ACCESS_DENIED;\r
1889 if (Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED || Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {\r
1890 //\r
1891 // Get image hash value as executable's signature.\r
1892 //\r
1893 SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;\r
1894 SignatureList = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);\r
1895 if (SignatureList == NULL) {\r
1896 Status = EFI_OUT_OF_RESOURCES;\r
1897 goto Done;\r
1898 }\r
1899 SignatureList->SignatureHeaderSize = 0;\r
1900 SignatureList->SignatureListSize = (UINT32) SignatureListSize;\r
13a220a9 1901 SignatureList->SignatureSize = (UINT32) (sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize);\r
6de4c35f 1902 CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));\r
1903 Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));\r
1904 CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);\r
50fe73a1 1905 }\r
0c18794e 1906 }\r
1907\r
1908Done:\r
1909 if (Status != EFI_SUCCESS) {\r
1910 //\r
1911 // Policy decides to defer or reject the image; add its information in image executable information table.\r
1912 //\r
213cc100
DG
1913 NameStr = ConvertDevicePathToText (File, FALSE, TRUE);\r
1914 AddImageExeInfo (Action, NameStr, File, SignatureList, SignatureListSize);\r
1915 if (NameStr != NULL) {\r
1916 DEBUG((EFI_D_INFO, "The image doesn't pass verification: %s\n", NameStr));\r
1917 FreePool(NameStr);\r
1918 }\r
5db28a67 1919 Status = EFI_SECURITY_VIOLATION;\r
0c18794e 1920 }\r
1921\r
1922 if (SignatureList != NULL) {\r
1923 FreePool (SignatureList);\r
1924 }\r
1925\r
0c18794e 1926 return Status;\r
1927}\r
1928\r
ffccb935
DG
1929/**\r
1930 On Ready To Boot Services Event notification handler.\r
1931\r
1932 Add the image execution information table if it is not in system configuration table.\r
1933\r
1934 @param[in] Event Event whose notification function is being invoked\r
1935 @param[in] Context Pointer to the notification function's context\r
1936\r
1937**/\r
1938VOID\r
1939EFIAPI\r
1940OnReadyToBoot (\r
1941 IN EFI_EVENT Event,\r
1942 IN VOID *Context\r
1943 )\r
1944{\r
1945 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable;\r
1946 UINTN ImageExeInfoTableSize;\r
1947\r
1948 EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);\r
1949 if (ImageExeInfoTable != NULL) {\r
1950 return;\r
1951 }\r
1952\r
1953 ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);\r
1954 ImageExeInfoTable = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize);\r
1955 if (ImageExeInfoTable == NULL) {\r
1956 return ;\r
1957 }\r
1958\r
20333c6d 1959 ImageExeInfoTable->NumberOfImages = 0;\r
ffccb935
DG
1960 gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) ImageExeInfoTable);\r
1961\r
1962}\r
1963\r
0c18794e 1964/**\r
1965 Register security measurement handler.\r
1966\r
1967 @param ImageHandle ImageHandle of the loaded driver.\r
1968 @param SystemTable Pointer to the EFI System Table.\r
1969\r
1970 @retval EFI_SUCCESS The handlers were registered successfully.\r
1971**/\r
1972EFI_STATUS\r
1973EFIAPI\r
1974DxeImageVerificationLibConstructor (\r
1975 IN EFI_HANDLE ImageHandle,\r
1976 IN EFI_SYSTEM_TABLE *SystemTable\r
1977 )\r
1978{\r
ffccb935
DG
1979 EFI_EVENT Event;\r
1980\r
1981 //\r
1982 // Register the event to publish the image execution table.\r
1983 //\r
1984 EfiCreateEventReadyToBootEx (\r
1985 TPL_CALLBACK,\r
20333c6d
QL
1986 OnReadyToBoot,\r
1987 NULL,\r
ffccb935 1988 &Event\r
20333c6d 1989 );\r
ffccb935 1990\r
5db28a67 1991 return RegisterSecurity2Handler (\r
0c18794e 1992 DxeImageVerificationHandler,\r
1993 EFI_AUTH_OPERATION_VERIFY_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r
45bf2c47 1994 );\r
0c18794e 1995}\r