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