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