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