]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
Remove useless MD5 OID ASN.1 value from DxeImageVerificationLib.
[mirror_edk2.git] / SecurityPkg / Library / DxeImageVerificationLib / DxeImageVerificationLib.c
1 /** @file
2 Implement image verification services for secure boot service in UEFI2.3.1.
3
4 Caution: This file requires additional review when modified.
5 This library will have external input - PE/COFF image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8
9 DxeImageVerificationLibImageRead() function will make sure the PE/COFF image content
10 read is within the image buffer.
11
12 DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept
13 untrusted PE/COFF image and validate its data structure within this image buffer before use.
14
15 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
16 This program and the accompanying materials
17 are licensed and made available under the terms and conditions of the BSD License
18 which accompanies this distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
20
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23
24 **/
25
26 #include "DxeImageVerificationLib.h"
27
28 //
29 // Caution: This is used by a function which may receive untrusted input.
30 // These global variables hold PE/COFF image data, and they should be validated before use.
31 //
32 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader;
33 UINT32 mPeCoffHeaderOffset;
34 EFI_GUID mCertType;
35
36 //
37 // Information on current PE/COFF image
38 //
39 UINTN mImageSize;
40 UINT8 *mImageBase = NULL;
41 UINT8 mImageDigest[MAX_DIGEST_SIZE];
42 UINTN mImageDigestSize;
43
44 //
45 // Notify string for authorization UI.
46 //
47 CHAR16 mNotifyString1[MAX_NOTIFY_STRING_LEN] = L"Image verification pass but not found in authorized database!";
48 CHAR16 mNotifyString2[MAX_NOTIFY_STRING_LEN] = L"Launch this image anyway? (Yes/Defer/No)";
49 //
50 // Public Exponent of RSA Key.
51 //
52 CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };
53
54
55 //
56 // OID ASN.1 Value for Hash Algorithms
57 //
58 UINT8 mHashOidValue[] = {
59 0x2B, 0x0E, 0x03, 0x02, 0x1A, // OBJ_sha1
60 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, // OBJ_sha224
61 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, // OBJ_sha256
62 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, // OBJ_sha384
63 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, // OBJ_sha512
64 };
65
66 HASH_TABLE mHash[] = {
67 { L"SHA1", 20, &mHashOidValue[0], 5, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1Final },
68 { L"SHA224", 28, &mHashOidValue[5], 9, NULL, NULL, NULL, NULL },
69 { L"SHA256", 32, &mHashOidValue[14], 9, Sha256GetContextSize,Sha256Init, Sha256Update, Sha256Final},
70 { L"SHA384", 48, &mHashOidValue[23], 9, NULL, NULL, NULL, NULL },
71 { L"SHA512", 64, &mHashOidValue[32], 9, NULL, NULL, NULL, NULL }
72 };
73
74 /**
75 Reads contents of a PE/COFF image in memory buffer.
76
77 Caution: This function may receive untrusted input.
78 PE/COFF image is external input, so this function will make sure the PE/COFF image content
79 read is within the image buffer.
80
81 @param FileHandle Pointer to the file handle to read the PE/COFF image.
82 @param FileOffset Offset into the PE/COFF image to begin the read operation.
83 @param ReadSize On input, the size in bytes of the requested read operation.
84 On output, the number of bytes actually read.
85 @param Buffer Output buffer that contains the data read from the PE/COFF image.
86
87 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
88 **/
89 EFI_STATUS
90 EFIAPI
91 DxeImageVerificationLibImageRead (
92 IN VOID *FileHandle,
93 IN UINTN FileOffset,
94 IN OUT UINTN *ReadSize,
95 OUT VOID *Buffer
96 )
97 {
98 UINTN EndPosition;
99
100 if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {
101 return EFI_INVALID_PARAMETER;
102 }
103
104 if (MAX_ADDRESS - FileOffset < *ReadSize) {
105 return EFI_INVALID_PARAMETER;
106 }
107
108 EndPosition = FileOffset + *ReadSize;
109 if (EndPosition > mImageSize) {
110 *ReadSize = (UINT32)(mImageSize - FileOffset);
111 }
112
113 if (FileOffset >= mImageSize) {
114 *ReadSize = 0;
115 }
116
117 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);
118
119 return EFI_SUCCESS;
120 }
121
122
123 /**
124 Get the image type.
125
126 @param[in] File This is a pointer to the device path of the file that is
127 being dispatched.
128
129 @return UINT32 Image Type
130
131 **/
132 UINT32
133 GetImageType (
134 IN CONST EFI_DEVICE_PATH_PROTOCOL *File
135 )
136 {
137 EFI_STATUS Status;
138 EFI_HANDLE DeviceHandle;
139 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
140 EFI_BLOCK_IO_PROTOCOL *BlockIo;
141
142 if (File == NULL) {
143 return IMAGE_UNKNOWN;
144 }
145
146 //
147 // First check to see if File is from a Firmware Volume
148 //
149 DeviceHandle = NULL;
150 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
151 Status = gBS->LocateDevicePath (
152 &gEfiFirmwareVolume2ProtocolGuid,
153 &TempDevicePath,
154 &DeviceHandle
155 );
156 if (!EFI_ERROR (Status)) {
157 Status = gBS->OpenProtocol (
158 DeviceHandle,
159 &gEfiFirmwareVolume2ProtocolGuid,
160 NULL,
161 NULL,
162 NULL,
163 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
164 );
165 if (!EFI_ERROR (Status)) {
166 return IMAGE_FROM_FV;
167 }
168 }
169
170 //
171 // Next check to see if File is from a Block I/O device
172 //
173 DeviceHandle = NULL;
174 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
175 Status = gBS->LocateDevicePath (
176 &gEfiBlockIoProtocolGuid,
177 &TempDevicePath,
178 &DeviceHandle
179 );
180 if (!EFI_ERROR (Status)) {
181 BlockIo = NULL;
182 Status = gBS->OpenProtocol (
183 DeviceHandle,
184 &gEfiBlockIoProtocolGuid,
185 (VOID **) &BlockIo,
186 NULL,
187 NULL,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
189 );
190 if (!EFI_ERROR (Status) && BlockIo != NULL) {
191 if (BlockIo->Media != NULL) {
192 if (BlockIo->Media->RemovableMedia) {
193 //
194 // Block I/O is present and specifies the media is removable
195 //
196 return IMAGE_FROM_REMOVABLE_MEDIA;
197 } else {
198 //
199 // Block I/O is present and specifies the media is not removable
200 //
201 return IMAGE_FROM_FIXED_MEDIA;
202 }
203 }
204 }
205 }
206
207 //
208 // File is not in a Firmware Volume or on a Block I/O device, so check to see if
209 // the device path supports the Simple File System Protocol.
210 //
211 DeviceHandle = NULL;
212 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
213 Status = gBS->LocateDevicePath (
214 &gEfiSimpleFileSystemProtocolGuid,
215 &TempDevicePath,
216 &DeviceHandle
217 );
218 if (!EFI_ERROR (Status)) {
219 //
220 // Simple File System is present without Block I/O, so assume media is fixed.
221 //
222 return IMAGE_FROM_FIXED_MEDIA;
223 }
224
225 //
226 // File is not from an FV, Block I/O or Simple File System, so the only options
227 // left are a PCI Option ROM and a Load File Protocol such as a PXE Boot from a NIC.
228 //
229 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
230 while (!IsDevicePathEndType (TempDevicePath)) {
231 switch (DevicePathType (TempDevicePath)) {
232
233 case MEDIA_DEVICE_PATH:
234 if (DevicePathSubType (TempDevicePath) == MEDIA_RELATIVE_OFFSET_RANGE_DP) {
235 return IMAGE_FROM_OPTION_ROM;
236 }
237 break;
238
239 case MESSAGING_DEVICE_PATH:
240 if (DevicePathSubType(TempDevicePath) == MSG_MAC_ADDR_DP) {
241 return IMAGE_FROM_REMOVABLE_MEDIA;
242 }
243 break;
244
245 default:
246 break;
247 }
248 TempDevicePath = NextDevicePathNode (TempDevicePath);
249 }
250 return IMAGE_UNKNOWN;
251 }
252
253 /**
254 Caculate hash of Pe/Coff image based on the authenticode image hashing in
255 PE/COFF Specification 8.0 Appendix A
256
257 Caution: This function may receive untrusted input.
258 PE/COFF image is external input, so this function will validate its data structure
259 within this image buffer before use.
260
261 @param[in] HashAlg Hash algorithm type.
262
263 @retval TRUE Successfully hash image.
264 @retval FALSE Fail in hash image.
265
266 **/
267 BOOLEAN
268 HashPeImage (
269 IN UINT32 HashAlg
270 )
271 {
272 BOOLEAN Status;
273 UINT16 Magic;
274 EFI_IMAGE_SECTION_HEADER *Section;
275 VOID *HashCtx;
276 UINTN CtxSize;
277 UINT8 *HashBase;
278 UINTN HashSize;
279 UINTN SumOfBytesHashed;
280 EFI_IMAGE_SECTION_HEADER *SectionHeader;
281 UINTN Index;
282 UINTN Pos;
283 UINT32 CertSize;
284 UINT32 NumberOfRvaAndSizes;
285
286 HashCtx = NULL;
287 SectionHeader = NULL;
288 Status = FALSE;
289
290 if ((HashAlg != HASHALG_SHA1) && (HashAlg != HASHALG_SHA256)) {
291 return FALSE;
292 }
293
294 //
295 // Initialize context of hash.
296 //
297 ZeroMem (mImageDigest, MAX_DIGEST_SIZE);
298
299 if (HashAlg == HASHALG_SHA1) {
300 mImageDigestSize = SHA1_DIGEST_SIZE;
301 mCertType = gEfiCertSha1Guid;
302 } else if (HashAlg == HASHALG_SHA256) {
303 mImageDigestSize = SHA256_DIGEST_SIZE;
304 mCertType = gEfiCertSha256Guid;
305 } else {
306 return FALSE;
307 }
308
309 CtxSize = mHash[HashAlg].GetContextSize();
310
311 HashCtx = AllocatePool (CtxSize);
312 if (HashCtx == NULL) {
313 return FALSE;
314 }
315
316 // 1. Load the image header into memory.
317
318 // 2. Initialize a SHA hash context.
319 Status = mHash[HashAlg].HashInit(HashCtx);
320
321 if (!Status) {
322 goto Done;
323 }
324
325 //
326 // Measuring PE/COFF Image Header;
327 // But CheckSum field and SECURITY data directory (certificate) are excluded
328 //
329 if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
330 //
331 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
332 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
333 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
334 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
335 //
336 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
337 } else {
338 //
339 // Get the magic value from the PE/COFF Optional Header
340 //
341 Magic = mNtHeader.Pe32->OptionalHeader.Magic;
342 }
343
344 //
345 // 3. Calculate the distance from the base of the image header to the image checksum address.
346 // 4. Hash the image header from its base to beginning of the image checksum.
347 //
348 HashBase = mImageBase;
349 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
350 //
351 // Use PE32 offset.
352 //
353 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.CheckSum) - HashBase);
354 NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;
355 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
356 //
357 // Use PE32+ offset.
358 //
359 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.CheckSum) - HashBase);
360 NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
361 } else {
362 //
363 // Invalid header magic number.
364 //
365 Status = FALSE;
366 goto Done;
367 }
368
369 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
370 if (!Status) {
371 goto Done;
372 }
373
374 //
375 // 5. Skip over the image checksum (it occupies a single ULONG).
376 //
377 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
378 //
379 // 6. Since there is no Cert Directory in optional header, hash everything
380 // from the end of the checksum to the end of image header.
381 //
382 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
383 //
384 // Use PE32 offset.
385 //
386 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
387 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
388 } else {
389 //
390 // Use PE32+ offset.
391 //
392 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
393 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
394 }
395
396 if (HashSize != 0) {
397 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
398 if (!Status) {
399 goto Done;
400 }
401 }
402 } else {
403 //
404 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
405 //
406 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
407 //
408 // Use PE32 offset.
409 //
410 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
411 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
412 } else {
413 //
414 // Use PE32+ offset.
415 //
416 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
417 HashSize = (UINTN) ((UINT8 *) (&mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
418 }
419
420 if (HashSize != 0) {
421 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
422 if (!Status) {
423 goto Done;
424 }
425 }
426
427 //
428 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
429 // 9. Hash everything from the end of the Cert Directory to the end of image header.
430 //
431 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
432 //
433 // Use PE32 offset
434 //
435 HashBase = (UINT8 *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
436 HashSize = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
437 } else {
438 //
439 // Use PE32+ offset.
440 //
441 HashBase = (UINT8 *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
442 HashSize = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - mImageBase);
443 }
444
445 if (HashSize != 0) {
446 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
447 if (!Status) {
448 goto Done;
449 }
450 }
451 }
452
453 //
454 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
455 //
456 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
457 //
458 // Use PE32 offset.
459 //
460 SumOfBytesHashed = mNtHeader.Pe32->OptionalHeader.SizeOfHeaders;
461 } else {
462 //
463 // Use PE32+ offset
464 //
465 SumOfBytesHashed = mNtHeader.Pe32Plus->OptionalHeader.SizeOfHeaders;
466 }
467
468
469 Section = (EFI_IMAGE_SECTION_HEADER *) (
470 mImageBase +
471 mPeCoffHeaderOffset +
472 sizeof (UINT32) +
473 sizeof (EFI_IMAGE_FILE_HEADER) +
474 mNtHeader.Pe32->FileHeader.SizeOfOptionalHeader
475 );
476
477 //
478 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
479 // structures in the image. The 'NumberOfSections' field of the image
480 // header indicates how big the table should be. Do not include any
481 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
482 //
483 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER) * mNtHeader.Pe32->FileHeader.NumberOfSections);
484 if (SectionHeader == NULL) {
485 Status = FALSE;
486 goto Done;
487 }
488 //
489 // 12. Using the 'PointerToRawData' in the referenced section headers as
490 // a key, arrange the elements in the table in ascending order. In other
491 // words, sort the section headers according to the disk-file offset of
492 // the section.
493 //
494 for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {
495 Pos = Index;
496 while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {
497 CopyMem (&SectionHeader[Pos], &SectionHeader[Pos - 1], sizeof (EFI_IMAGE_SECTION_HEADER));
498 Pos--;
499 }
500 CopyMem (&SectionHeader[Pos], Section, sizeof (EFI_IMAGE_SECTION_HEADER));
501 Section += 1;
502 }
503
504 //
505 // 13. Walk through the sorted table, bring the corresponding section
506 // into memory, and hash the entire section (using the 'SizeOfRawData'
507 // field in the section header to determine the amount of data to hash).
508 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
509 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
510 //
511 for (Index = 0; Index < mNtHeader.Pe32->FileHeader.NumberOfSections; Index++) {
512 Section = &SectionHeader[Index];
513 if (Section->SizeOfRawData == 0) {
514 continue;
515 }
516 HashBase = mImageBase + Section->PointerToRawData;
517 HashSize = (UINTN) Section->SizeOfRawData;
518
519 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
520 if (!Status) {
521 goto Done;
522 }
523
524 SumOfBytesHashed += HashSize;
525 }
526
527 //
528 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
529 // data in the file that needs to be added to the hash. This data begins
530 // at file offset SUM_OF_BYTES_HASHED and its length is:
531 // FileSize - (CertDirectory->Size)
532 //
533 if (mImageSize > SumOfBytesHashed) {
534 HashBase = mImageBase + SumOfBytesHashed;
535
536 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
537 CertSize = 0;
538 } else {
539 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
540 //
541 // Use PE32 offset.
542 //
543 CertSize = mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
544 } else {
545 //
546 // Use PE32+ offset.
547 //
548 CertSize = mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
549 }
550 }
551
552 if (mImageSize > CertSize + SumOfBytesHashed) {
553 HashSize = (UINTN) (mImageSize - CertSize - SumOfBytesHashed);
554
555 Status = mHash[HashAlg].HashUpdate(HashCtx, HashBase, HashSize);
556 if (!Status) {
557 goto Done;
558 }
559 } else if (mImageSize < CertSize + SumOfBytesHashed) {
560 Status = FALSE;
561 goto Done;
562 }
563 }
564
565 Status = mHash[HashAlg].HashFinal(HashCtx, mImageDigest);
566
567 Done:
568 if (HashCtx != NULL) {
569 FreePool (HashCtx);
570 }
571 if (SectionHeader != NULL) {
572 FreePool (SectionHeader);
573 }
574 return Status;
575 }
576
577 /**
578 Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of
579 Pe/Coff image based on the authenticode image hashing in PE/COFF Specification
580 8.0 Appendix A
581
582 Caution: This function may receive untrusted input.
583 PE/COFF image is external input, so this function will validate its data structure
584 within this image buffer before use.
585
586 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
587 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
588
589 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
590 @retval EFI_SUCCESS Hash successfully.
591
592 **/
593 EFI_STATUS
594 HashPeImageByType (
595 IN UINT8 *AuthData,
596 IN UINTN AuthDataSize
597 )
598 {
599 UINT8 Index;
600
601 for (Index = 0; Index < HASHALG_MAX; Index++) {
602 //
603 // Check the Hash algorithm in PE/COFF Authenticode.
604 // According to PKCS#7 Definition:
605 // SignedData ::= SEQUENCE {
606 // version Version,
607 // digestAlgorithms DigestAlgorithmIdentifiers,
608 // contentInfo ContentInfo,
609 // .... }
610 // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
611 // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
612 // Fixed offset (+32) is calculated based on two bytes of length encoding.
613 //
614 if ((*(AuthData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE) {
615 //
616 // Only support two bytes of Long Form of Length Encoding.
617 //
618 continue;
619 }
620
621 if (AuthDataSize < 32 + mHash[Index].OidLength) {
622 return EFI_UNSUPPORTED;
623 }
624
625 if (CompareMem (AuthData + 32, mHash[Index].OidValue, mHash[Index].OidLength) == 0) {
626 break;
627 }
628 }
629
630 if (Index == HASHALG_MAX) {
631 return EFI_UNSUPPORTED;
632 }
633
634 //
635 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
636 //
637 if (!HashPeImage(Index)) {
638 return EFI_UNSUPPORTED;
639 }
640
641 return EFI_SUCCESS;
642 }
643
644
645 /**
646 Returns the size of a given image execution info table in bytes.
647
648 This function returns the size, in bytes, of the image execution info table specified by
649 ImageExeInfoTable. If ImageExeInfoTable is NULL, then 0 is returned.
650
651 @param ImageExeInfoTable A pointer to a image execution info table structure.
652
653 @retval 0 If ImageExeInfoTable is NULL.
654 @retval Others The size of a image execution info table in bytes.
655
656 **/
657 UINTN
658 GetImageExeInfoTableSize (
659 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable
660 )
661 {
662 UINTN Index;
663 EFI_IMAGE_EXECUTION_INFO *ImageExeInfoItem;
664 UINTN TotalSize;
665
666 if (ImageExeInfoTable == NULL) {
667 return 0;
668 }
669
670 ImageExeInfoItem = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoTable + sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE));
671 TotalSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);
672 for (Index = 0; Index < ImageExeInfoTable->NumberOfImages; Index++) {
673 TotalSize += ReadUnaligned32 ((UINT32 *) &ImageExeInfoItem->InfoSize);
674 ImageExeInfoItem = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) ImageExeInfoItem + ReadUnaligned32 ((UINT32 *) &ImageExeInfoItem->InfoSize));
675 }
676
677 return TotalSize;
678 }
679
680 /**
681 Create an Image Execution Information Table entry and add it to system configuration table.
682
683 @param[in] Action Describes the action taken by the firmware regarding this image.
684 @param[in] Name Input a null-terminated, user-friendly name.
685 @param[in] DevicePath Input device path pointer.
686 @param[in] Signature Input signature info in EFI_SIGNATURE_LIST data structure.
687 @param[in] SignatureSize Size of signature.
688
689 **/
690 VOID
691 AddImageExeInfo (
692 IN EFI_IMAGE_EXECUTION_ACTION Action,
693 IN CHAR16 *Name OPTIONAL,
694 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
695 IN EFI_SIGNATURE_LIST *Signature OPTIONAL,
696 IN UINTN SignatureSize
697 )
698 {
699 EFI_IMAGE_EXECUTION_INFO_TABLE *ImageExeInfoTable;
700 EFI_IMAGE_EXECUTION_INFO_TABLE *NewImageExeInfoTable;
701 EFI_IMAGE_EXECUTION_INFO *ImageExeInfoEntry;
702 UINTN ImageExeInfoTableSize;
703 UINTN NewImageExeInfoEntrySize;
704 UINTN NameStringLen;
705 UINTN DevicePathSize;
706
707 ImageExeInfoTable = NULL;
708 NewImageExeInfoTable = NULL;
709 ImageExeInfoEntry = NULL;
710 NameStringLen = 0;
711
712 if (DevicePath == NULL) {
713 return ;
714 }
715
716 if (Name != NULL) {
717 NameStringLen = StrSize (Name);
718 }
719
720 ImageExeInfoTable = NULL;
721 EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);
722 if (ImageExeInfoTable != NULL) {
723 //
724 // The table has been found!
725 // We must enlarge the table to accmodate the new exe info entry.
726 //
727 ImageExeInfoTableSize = GetImageExeInfoTableSize (ImageExeInfoTable);
728 } else {
729 //
730 // Not Found!
731 // We should create a new table to append to the configuration table.
732 //
733 ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);
734 }
735
736 DevicePathSize = GetDevicePathSize (DevicePath);
737 NewImageExeInfoEntrySize = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;
738 NewImageExeInfoTable = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize + NewImageExeInfoEntrySize);
739 if (NewImageExeInfoTable == NULL) {
740 return ;
741 }
742
743 if (ImageExeInfoTable != NULL) {
744 CopyMem (NewImageExeInfoTable, ImageExeInfoTable, ImageExeInfoTableSize);
745 } else {
746 NewImageExeInfoTable->NumberOfImages = 0;
747 }
748 NewImageExeInfoTable->NumberOfImages++;
749 ImageExeInfoEntry = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) NewImageExeInfoTable + ImageExeInfoTableSize);
750 //
751 // Update new item's infomation.
752 //
753 WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->Action, Action);
754 WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->InfoSize, (UINT32) NewImageExeInfoEntrySize);
755
756 if (Name != NULL) {
757 CopyMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), Name, NameStringLen);
758 }
759 CopyMem (
760 (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen,
761 DevicePath,
762 DevicePathSize
763 );
764 if (Signature != NULL) {
765 CopyMem (
766 (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen + DevicePathSize,
767 Signature,
768 SignatureSize
769 );
770 }
771 //
772 // Update/replace the image execution table.
773 //
774 gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) NewImageExeInfoTable);
775
776 //
777 // Free Old table data!
778 //
779 if (ImageExeInfoTable != NULL) {
780 FreePool (ImageExeInfoTable);
781 }
782 }
783
784 /**
785 Check whether signature is in specified database.
786
787 @param[in] VariableName Name of database variable that is searched in.
788 @param[in] Signature Pointer to signature that is searched for.
789 @param[in] CertType Pointer to hash algrithom.
790 @param[in] SignatureSize Size of Signature.
791
792 @return TRUE Found the signature in the variable database.
793 @return FALSE Not found the signature in the variable database.
794
795 **/
796 BOOLEAN
797 IsSignatureFoundInDatabase (
798 IN CHAR16 *VariableName,
799 IN UINT8 *Signature,
800 IN EFI_GUID *CertType,
801 IN UINTN SignatureSize
802 )
803 {
804 EFI_STATUS Status;
805 EFI_SIGNATURE_LIST *CertList;
806 EFI_SIGNATURE_DATA *Cert;
807 UINTN DataSize;
808 UINT8 *Data;
809 UINTN Index;
810 UINTN CertCount;
811 BOOLEAN IsFound;
812 //
813 // Read signature database variable.
814 //
815 IsFound = FALSE;
816 Data = NULL;
817 DataSize = 0;
818 Status = gRT->GetVariable (VariableName, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);
819 if (Status != EFI_BUFFER_TOO_SMALL) {
820 return FALSE;
821 }
822
823 Data = (UINT8 *) AllocateZeroPool (DataSize);
824 if (Data == NULL) {
825 return FALSE;
826 }
827
828 Status = gRT->GetVariable (VariableName, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, Data);
829 if (EFI_ERROR (Status)) {
830 goto Done;
831 }
832 //
833 // Enumerate all signature data in SigDB to check if executable's signature exists.
834 //
835 CertList = (EFI_SIGNATURE_LIST *) Data;
836 while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {
837 CertCount = (CertList->SignatureListSize - CertList->SignatureHeaderSize) / CertList->SignatureSize;
838 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
839 if ((CertList->SignatureSize == sizeof(EFI_SIGNATURE_DATA) - 1 + SignatureSize) && (CompareGuid(&CertList->SignatureType, CertType))) {
840 for (Index = 0; Index < CertCount; Index++) {
841 if (CompareMem (Cert->SignatureData, Signature, SignatureSize) == 0) {
842 //
843 // Find the signature in database.
844 //
845 IsFound = TRUE;
846 break;
847 }
848
849 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);
850 }
851
852 if (IsFound) {
853 break;
854 }
855 }
856
857 DataSize -= CertList->SignatureListSize;
858 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);
859 }
860
861 Done:
862 if (Data != NULL) {
863 FreePool (Data);
864 }
865
866 return IsFound;
867 }
868
869 /**
870 Verify PKCS#7 SignedData using certificate found in Variable which formatted
871 as EFI_SIGNATURE_LIST. The Variable may be PK, KEK, DB or DBX.
872
873 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
874 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
875 @param[in] VariableName Name of Variable to search for Certificate.
876 @param[in] VendorGuid Variable vendor GUID.
877
878 @retval TRUE Image pass verification.
879 @retval FALSE Image fail verification.
880
881 **/
882 BOOLEAN
883 IsPkcsSignedDataVerifiedBySignatureList (
884 IN UINT8 *AuthData,
885 IN UINTN AuthDataSize,
886 IN CHAR16 *VariableName,
887 IN EFI_GUID *VendorGuid
888 )
889 {
890 EFI_STATUS Status;
891 BOOLEAN VerifyStatus;
892 EFI_SIGNATURE_LIST *CertList;
893 EFI_SIGNATURE_DATA *Cert;
894 UINTN DataSize;
895 UINT8 *Data;
896 UINT8 *RootCert;
897 UINTN RootCertSize;
898 UINTN Index;
899 UINTN CertCount;
900
901 Data = NULL;
902 CertList = NULL;
903 Cert = NULL;
904 RootCert = NULL;
905 RootCertSize = 0;
906 VerifyStatus = FALSE;
907
908 DataSize = 0;
909 Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, NULL);
910 if (Status == EFI_BUFFER_TOO_SMALL) {
911 Data = (UINT8 *) AllocateZeroPool (DataSize);
912 if (Data == NULL) {
913 return VerifyStatus;
914 }
915
916 Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, (VOID *) Data);
917 if (EFI_ERROR (Status)) {
918 goto Done;
919 }
920
921 //
922 // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.
923 //
924 CertList = (EFI_SIGNATURE_LIST *) Data;
925 while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {
926 if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {
927 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
928 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
929 for (Index = 0; Index < CertCount; Index++) {
930 //
931 // Iterate each Signature Data Node within this CertList for verify.
932 //
933 RootCert = Cert->SignatureData;
934 RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);
935
936 //
937 // Call AuthenticodeVerify library to Verify Authenticode struct.
938 //
939 VerifyStatus = AuthenticodeVerify (
940 AuthData,
941 AuthDataSize,
942 RootCert,
943 RootCertSize,
944 mImageDigest,
945 mImageDigestSize
946 );
947 if (VerifyStatus) {
948 goto Done;
949 }
950 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);
951 }
952 }
953 DataSize -= CertList->SignatureListSize;
954 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);
955 }
956 }
957
958 Done:
959 if (Data != NULL) {
960 FreePool (Data);
961 }
962
963 return VerifyStatus;
964 }
965
966 /**
967 Verify certificate in WIN_CERT_TYPE_PKCS_SIGNED_DATA format.
968
969 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
970 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
971
972 @retval EFI_SUCCESS Image pass verification.
973 @retval EFI_SECURITY_VIOLATION Image fail verification.
974
975 **/
976 EFI_STATUS
977 VerifyCertPkcsSignedData (
978 IN UINT8 *AuthData,
979 IN UINTN AuthDataSize
980 )
981 {
982 //
983 // 1: Find certificate from DBX forbidden database for revoked certificate.
984 //
985 if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {
986 //
987 // DBX is forbidden database, if Authenticode verification pass with
988 // one of the certificate in DBX, this image should be rejected.
989 //
990 return EFI_SECURITY_VIOLATION;
991 }
992
993 //
994 // 2: Find certificate from DB database and try to verify authenticode struct.
995 //
996 if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {
997 return EFI_SUCCESS;
998 } else {
999 return EFI_SECURITY_VIOLATION;
1000 }
1001 }
1002
1003 /**
1004 Provide verification service for signed images, which include both signature validation
1005 and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and
1006 MSFT Authenticode type signatures are supported.
1007
1008 In this implementation, only verify external executables when in USER MODE.
1009 Executables from FV is bypass, so pass in AuthenticationStatus is ignored.
1010
1011 The image verification process is:
1012 If the image is signed,
1013 If the image's certificate verifies against a certificate (root or intermediate) in the allowed
1014 database (DB) and not in the forbidden database (DBX), the certificate verification is passed.
1015 If the image's hash digest is in DBX,
1016 deny execution.
1017 If not,
1018 run it.
1019 If the Image's certificate verification failed.
1020 If the Image's Hash is in DB and not in DBX,
1021 run it.
1022 Otherwise,
1023 deny execution.
1024 Otherwise, the image is not signed,
1025 Is the Image's Hash in DBX?
1026 If yes, deny execution.
1027 If not, is the Image's Hash in DB?
1028 If yes, run it.
1029 If not, deny execution.
1030
1031 Caution: This function may receive untrusted input.
1032 PE/COFF image is external input, so this function will validate its data structure
1033 within this image buffer before use.
1034
1035 @param[in] AuthenticationStatus
1036 This is the authentication status returned from the security
1037 measurement services for the input file.
1038 @param[in] File This is a pointer to the device path of the file that is
1039 being dispatched. This will optionally be used for logging.
1040 @param[in] FileBuffer File buffer matches the input file device path.
1041 @param[in] FileSize Size of File buffer matches the input file device path.
1042 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.
1043
1044 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL
1045 FileBuffer did authenticate, and the platform policy dictates
1046 that the DXE Foundation may use the file.
1047 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath
1048 and non-NULL FileBuffer did authenticate, and the platform
1049 policy dictates that the DXE Foundation may execute the image in
1050 FileBuffer.
1051 @retval EFI_OUT_RESOURCE Fail to allocate memory.
1052 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
1053 the platform policy dictates that File should be placed
1054 in the untrusted state. The image has been added to the file
1055 execution table.
1056 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not
1057 authenticate, and the platform policy dictates that the DXE
1058 Foundation many not use File.
1059
1060 **/
1061 EFI_STATUS
1062 EFIAPI
1063 DxeImageVerificationHandler (
1064 IN UINT32 AuthenticationStatus,
1065 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
1066 IN VOID *FileBuffer,
1067 IN UINTN FileSize,
1068 IN BOOLEAN BootPolicy
1069 )
1070 {
1071 EFI_STATUS Status;
1072 UINT16 Magic;
1073 EFI_IMAGE_DOS_HEADER *DosHdr;
1074 EFI_STATUS VerifyStatus;
1075 EFI_SIGNATURE_LIST *SignatureList;
1076 UINTN SignatureListSize;
1077 EFI_SIGNATURE_DATA *Signature;
1078 EFI_IMAGE_EXECUTION_ACTION Action;
1079 WIN_CERTIFICATE *WinCertificate;
1080 UINT32 Policy;
1081 UINT8 *SecureBoot;
1082 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
1083 UINT32 NumberOfRvaAndSizes;
1084 UINT32 CertSize;
1085 WIN_CERTIFICATE_EFI_PKCS *PkcsCertData;
1086 WIN_CERTIFICATE_UEFI_GUID *WinCertUefiGuid;
1087 UINT8 *AuthData;
1088 UINTN AuthDataSize;
1089 EFI_IMAGE_DATA_DIRECTORY *SecDataDir;
1090
1091 SignatureList = NULL;
1092 SignatureListSize = 0;
1093 WinCertificate = NULL;
1094 SecDataDir = NULL;
1095 PkcsCertData = NULL;
1096 Action = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;
1097 Status = EFI_ACCESS_DENIED;
1098 //
1099 // Check the image type and get policy setting.
1100 //
1101 switch (GetImageType (File)) {
1102
1103 case IMAGE_FROM_FV:
1104 Policy = ALWAYS_EXECUTE;
1105 break;
1106
1107 case IMAGE_FROM_OPTION_ROM:
1108 Policy = PcdGet32 (PcdOptionRomImageVerificationPolicy);
1109 break;
1110
1111 case IMAGE_FROM_REMOVABLE_MEDIA:
1112 Policy = PcdGet32 (PcdRemovableMediaImageVerificationPolicy);
1113 break;
1114
1115 case IMAGE_FROM_FIXED_MEDIA:
1116 Policy = PcdGet32 (PcdFixedMediaImageVerificationPolicy);
1117 break;
1118
1119 default:
1120 Policy = DENY_EXECUTE_ON_SECURITY_VIOLATION;
1121 break;
1122 }
1123 //
1124 // If policy is always/never execute, return directly.
1125 //
1126 if (Policy == ALWAYS_EXECUTE) {
1127 return EFI_SUCCESS;
1128 } else if (Policy == NEVER_EXECUTE) {
1129 return EFI_ACCESS_DENIED;
1130 }
1131
1132 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);
1133 //
1134 // Skip verification if SecureBoot variable doesn't exist.
1135 //
1136 if (SecureBoot == NULL) {
1137 return EFI_SUCCESS;
1138 }
1139
1140 //
1141 // Skip verification if SecureBoot is disabled.
1142 //
1143 if (*SecureBoot == SECURE_BOOT_MODE_DISABLE) {
1144 FreePool (SecureBoot);
1145 return EFI_SUCCESS;
1146 }
1147 FreePool (SecureBoot);
1148
1149 //
1150 // Read the Dos header.
1151 //
1152 if (FileBuffer == NULL) {
1153 return EFI_INVALID_PARAMETER;
1154 }
1155
1156 mImageBase = (UINT8 *) FileBuffer;
1157 mImageSize = FileSize;
1158
1159 ZeroMem (&ImageContext, sizeof (ImageContext));
1160 ImageContext.Handle = (VOID *) FileBuffer;
1161 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeImageVerificationLibImageRead;
1162
1163 //
1164 // Get information about the image being loaded
1165 //
1166 Status = PeCoffLoaderGetImageInfo (&ImageContext);
1167 if (EFI_ERROR (Status)) {
1168 //
1169 // The information can't be got from the invalid PeImage
1170 //
1171 goto Done;
1172 }
1173
1174 Status = EFI_ACCESS_DENIED;
1175
1176 DosHdr = (EFI_IMAGE_DOS_HEADER *) mImageBase;
1177 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1178 //
1179 // DOS image header is present,
1180 // so read the PE header after the DOS image header.
1181 //
1182 mPeCoffHeaderOffset = DosHdr->e_lfanew;
1183 } else {
1184 mPeCoffHeaderOffset = 0;
1185 }
1186 //
1187 // Check PE/COFF image.
1188 //
1189 mNtHeader.Pe32 = (EFI_IMAGE_NT_HEADERS32 *) (mImageBase + mPeCoffHeaderOffset);
1190 if (mNtHeader.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1191 //
1192 // It is not a valid Pe/Coff file.
1193 //
1194 goto Done;
1195 }
1196
1197 if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1198 //
1199 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1200 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1201 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1202 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1203 //
1204 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1205 } else {
1206 //
1207 // Get the magic value from the PE/COFF Optional Header
1208 //
1209 Magic = mNtHeader.Pe32->OptionalHeader.Magic;
1210 }
1211
1212 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1213 //
1214 // Use PE32 offset.
1215 //
1216 NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1217 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
1218 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
1219 }
1220 } else {
1221 //
1222 // Use PE32+ offset.
1223 //
1224 NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1225 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
1226 SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
1227 }
1228 }
1229
1230 if ((SecDataDir == NULL) || ((SecDataDir != NULL) && (SecDataDir->Size == 0))) {
1231 //
1232 // This image is not signed.
1233 //
1234 if (!HashPeImage (HASHALG_SHA256)) {
1235 goto Done;
1236 }
1237
1238 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {
1239 //
1240 // Image Hash is in forbidden database (DBX).
1241 //
1242 goto Done;
1243 }
1244
1245 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {
1246 //
1247 // Image Hash is in allowed database (DB).
1248 //
1249 return EFI_SUCCESS;
1250 }
1251
1252 //
1253 // Image Hash is not found in both forbidden and allowed database.
1254 //
1255 goto Done;
1256 }
1257
1258 //
1259 // Verify signature of executables.
1260 //
1261 WinCertificate = (WIN_CERTIFICATE *) (mImageBase + SecDataDir->VirtualAddress);
1262
1263 CertSize = sizeof (WIN_CERTIFICATE);
1264
1265 if ((SecDataDir->Size <= CertSize) || (SecDataDir->Size < WinCertificate->dwLength)) {
1266 goto Done;
1267 }
1268
1269 //
1270 // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.
1271 //
1272 if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
1273 //
1274 // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the
1275 // Authenticode specification.
1276 //
1277 PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;
1278 if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {
1279 goto Done;
1280 }
1281 AuthData = PkcsCertData->CertData;
1282 AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);
1283
1284 Status = HashPeImageByType (AuthData, AuthDataSize);
1285 if (EFI_ERROR (Status)) {
1286 goto Done;
1287 }
1288
1289 VerifyStatus = VerifyCertPkcsSignedData (AuthData, AuthDataSize);
1290 } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
1291 //
1292 // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.
1293 //
1294 WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;
1295 if (!CompareGuid(&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid) ||
1296 (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData))) {
1297 goto Done;
1298 }
1299 AuthData = WinCertUefiGuid->CertData;
1300 AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);
1301
1302 Status = HashPeImageByType (AuthData, AuthDataSize);
1303 if (EFI_ERROR (Status)) {
1304 goto Done;
1305 }
1306 VerifyStatus = VerifyCertPkcsSignedData (AuthData, AuthDataSize);
1307 } else {
1308 goto Done;
1309 }
1310
1311 if (!EFI_ERROR (VerifyStatus)) {
1312 //
1313 // Verification is passed.
1314 // Continue to check the image digest in signature database.
1315 //
1316 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {
1317 //
1318 // Executable signature verification passes, but is found in forbidden signature database.
1319 //
1320 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;
1321 Status = EFI_ACCESS_DENIED;
1322 } else {
1323 //
1324 // For image verification against enrolled X.509 certificate(root or intermediate),
1325 // no need to check image's hash in the allowed database.
1326 //
1327 return EFI_SUCCESS;
1328 }
1329 } else {
1330 //
1331 // Verification failure.
1332 //
1333 if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize) &&
1334 IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {
1335 //
1336 // Verification fail, Image Hash is not in forbidden database (DBX),
1337 // and Image Hash is in allowed database (DB).
1338 //
1339 Status = EFI_SUCCESS;
1340 } else {
1341 Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;
1342 Status = EFI_ACCESS_DENIED;
1343 }
1344 }
1345
1346 if (EFI_ERROR (Status)) {
1347 //
1348 // Get image hash value as executable's signature.
1349 //
1350 SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;
1351 SignatureList = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);
1352 if (SignatureList == NULL) {
1353 Status = EFI_OUT_OF_RESOURCES;
1354 goto Done;
1355 }
1356 SignatureList->SignatureHeaderSize = 0;
1357 SignatureList->SignatureListSize = (UINT32) SignatureListSize;
1358 SignatureList->SignatureSize = (UINT32) mImageDigestSize;
1359 CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));
1360 Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));
1361 CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);
1362 }
1363
1364 Done:
1365 if (Status != EFI_SUCCESS) {
1366 //
1367 // Policy decides to defer or reject the image; add its information in image executable information table.
1368 //
1369 AddImageExeInfo (Action, NULL, File, SignatureList, SignatureListSize);
1370 Status = EFI_SECURITY_VIOLATION;
1371 }
1372
1373 if (SignatureList != NULL) {
1374 FreePool (SignatureList);
1375 }
1376
1377 return Status;
1378 }
1379
1380 /**
1381 When VariableWriteArchProtocol install, create "SecureBoot" variable.
1382
1383 @param[in] Event Event whose notification function is being invoked.
1384 @param[in] Context Pointer to the notification function's context.
1385
1386 **/
1387 VOID
1388 EFIAPI
1389 VariableWriteCallBack (
1390 IN EFI_EVENT Event,
1391 IN VOID *Context
1392 )
1393 {
1394 UINT8 SecureBootMode;
1395 UINT8 *SecureBootModePtr;
1396 EFI_STATUS Status;
1397 VOID *ProtocolPointer;
1398
1399 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, &ProtocolPointer);
1400 if (EFI_ERROR (Status)) {
1401 return;
1402 }
1403
1404 //
1405 // Check whether "SecureBoot" variable exists.
1406 // If this library is built-in, it means firmware has capability to perform
1407 // driver signing verification.
1408 //
1409 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBootModePtr, NULL);
1410 if (SecureBootModePtr == NULL) {
1411 SecureBootMode = SECURE_BOOT_MODE_DISABLE;
1412 //
1413 // Authenticated variable driver will update "SecureBoot" depending on SetupMode variable.
1414 //
1415 gRT->SetVariable (
1416 EFI_SECURE_BOOT_MODE_NAME,
1417 &gEfiGlobalVariableGuid,
1418 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1419 sizeof (UINT8),
1420 &SecureBootMode
1421 );
1422 } else {
1423 FreePool (SecureBootModePtr);
1424 }
1425 }
1426
1427 /**
1428 Register security measurement handler.
1429
1430 @param ImageHandle ImageHandle of the loaded driver.
1431 @param SystemTable Pointer to the EFI System Table.
1432
1433 @retval EFI_SUCCESS The handlers were registered successfully.
1434 **/
1435 EFI_STATUS
1436 EFIAPI
1437 DxeImageVerificationLibConstructor (
1438 IN EFI_HANDLE ImageHandle,
1439 IN EFI_SYSTEM_TABLE *SystemTable
1440 )
1441 {
1442 VOID *Registration;
1443
1444 //
1445 // Register callback function upon VariableWriteArchProtocol.
1446 //
1447 EfiCreateProtocolNotifyEvent (
1448 &gEfiVariableWriteArchProtocolGuid,
1449 TPL_CALLBACK,
1450 VariableWriteCallBack,
1451 NULL,
1452 &Registration
1453 );
1454
1455 return RegisterSecurity2Handler (
1456 DxeImageVerificationHandler,
1457 EFI_AUTH_OPERATION_VERIFY_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED
1458 );
1459 }