2 Implement image verification services for secure boot service in UEFI2.3.1.
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.
9 DxeImageVerificationLibImageRead() function will make sure the PE/COFF image content
10 read is within the image buffer.
12 DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept
13 untrusted PE/COFF image and validate its data structure within this image buffer before use.
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
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.
26 #include "DxeImageVerificationLib.h"
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.
32 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader
;
33 UINT32 mPeCoffHeaderOffset
;
37 // Information on current PE/COFF image
40 UINT8
*mImageBase
= NULL
;
41 UINT8 mImageDigest
[MAX_DIGEST_SIZE
];
42 UINTN mImageDigestSize
;
45 // Notify string for authorization UI.
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)";
50 // Public Exponent of RSA Key.
52 CONST UINT8 mRsaE
[] = { 0x01, 0x00, 0x01 };
56 // OID ASN.1 Value for Hash Algorithms
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
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
}
75 Reads contents of a PE/COFF image in memory buffer.
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.
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.
87 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
91 DxeImageVerificationLibImageRead (
94 IN OUT UINTN
*ReadSize
,
100 if (FileHandle
== NULL
|| ReadSize
== NULL
|| Buffer
== NULL
) {
101 return EFI_INVALID_PARAMETER
;
104 if (MAX_ADDRESS
- FileOffset
< *ReadSize
) {
105 return EFI_INVALID_PARAMETER
;
108 EndPosition
= FileOffset
+ *ReadSize
;
109 if (EndPosition
> mImageSize
) {
110 *ReadSize
= (UINT32
)(mImageSize
- FileOffset
);
113 if (FileOffset
>= mImageSize
) {
117 CopyMem (Buffer
, (UINT8
*)((UINTN
) FileHandle
+ FileOffset
), *ReadSize
);
126 @param[in] File This is a pointer to the device path of the file that is
129 @return UINT32 Image Type
134 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
138 EFI_HANDLE DeviceHandle
;
139 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
140 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
143 return IMAGE_UNKNOWN
;
147 // First check to see if File is from a Firmware Volume
150 TempDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) File
;
151 Status
= gBS
->LocateDevicePath (
152 &gEfiFirmwareVolume2ProtocolGuid
,
156 if (!EFI_ERROR (Status
)) {
157 Status
= gBS
->OpenProtocol (
159 &gEfiFirmwareVolume2ProtocolGuid
,
163 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
165 if (!EFI_ERROR (Status
)) {
166 return IMAGE_FROM_FV
;
171 // Next check to see if File is from a Block I/O device
174 TempDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) File
;
175 Status
= gBS
->LocateDevicePath (
176 &gEfiBlockIoProtocolGuid
,
180 if (!EFI_ERROR (Status
)) {
182 Status
= gBS
->OpenProtocol (
184 &gEfiBlockIoProtocolGuid
,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
190 if (!EFI_ERROR (Status
) && BlockIo
!= NULL
) {
191 if (BlockIo
->Media
!= NULL
) {
192 if (BlockIo
->Media
->RemovableMedia
) {
194 // Block I/O is present and specifies the media is removable
196 return IMAGE_FROM_REMOVABLE_MEDIA
;
199 // Block I/O is present and specifies the media is not removable
201 return IMAGE_FROM_FIXED_MEDIA
;
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.
212 TempDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) File
;
213 Status
= gBS
->LocateDevicePath (
214 &gEfiSimpleFileSystemProtocolGuid
,
218 if (!EFI_ERROR (Status
)) {
220 // Simple File System is present without Block I/O, so assume media is fixed.
222 return IMAGE_FROM_FIXED_MEDIA
;
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.
229 TempDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) File
;
230 while (!IsDevicePathEndType (TempDevicePath
)) {
231 switch (DevicePathType (TempDevicePath
)) {
233 case MEDIA_DEVICE_PATH
:
234 if (DevicePathSubType (TempDevicePath
) == MEDIA_RELATIVE_OFFSET_RANGE_DP
) {
235 return IMAGE_FROM_OPTION_ROM
;
239 case MESSAGING_DEVICE_PATH
:
240 if (DevicePathSubType(TempDevicePath
) == MSG_MAC_ADDR_DP
) {
241 return IMAGE_FROM_REMOVABLE_MEDIA
;
248 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
250 return IMAGE_UNKNOWN
;
254 Caculate hash of Pe/Coff image based on the authenticode image hashing in
255 PE/COFF Specification 8.0 Appendix A
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.
261 @param[in] HashAlg Hash algorithm type.
263 @retval TRUE Successfully hash image.
264 @retval FALSE Fail in hash image.
274 EFI_IMAGE_SECTION_HEADER
*Section
;
279 UINTN SumOfBytesHashed
;
280 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
284 UINT32 NumberOfRvaAndSizes
;
287 SectionHeader
= NULL
;
290 if ((HashAlg
!= HASHALG_SHA1
) && (HashAlg
!= HASHALG_SHA256
)) {
295 // Initialize context of hash.
297 ZeroMem (mImageDigest
, MAX_DIGEST_SIZE
);
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
;
309 CtxSize
= mHash
[HashAlg
].GetContextSize();
311 HashCtx
= AllocatePool (CtxSize
);
312 if (HashCtx
== NULL
) {
316 // 1. Load the image header into memory.
318 // 2. Initialize a SHA hash context.
319 Status
= mHash
[HashAlg
].HashInit(HashCtx
);
326 // Measuring PE/COFF Image Header;
327 // But CheckSum field and SECURITY data directory (certificate) are excluded
329 if (mNtHeader
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& mNtHeader
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
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
336 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
339 // Get the magic value from the PE/COFF Optional Header
341 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
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.
348 HashBase
= mImageBase
;
349 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
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
) {
359 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
360 NumberOfRvaAndSizes
= mNtHeader
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
363 // Invalid header magic number.
369 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
375 // 5. Skip over the image checksum (it occupies a single ULONG).
377 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
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.
382 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
386 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
387 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- mImageBase
);
392 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
393 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- mImageBase
);
397 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
404 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
406 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
410 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
411 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
416 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
417 HashSize
= (UINTN
) ((UINT8
*) (&mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
421 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
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.
431 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
435 HashBase
= (UINT8
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
436 HashSize
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- mImageBase
);
441 HashBase
= (UINT8
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
442 HashSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- mImageBase
);
446 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
454 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header.
456 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
460 SumOfBytesHashed
= mNtHeader
.Pe32
->OptionalHeader
.SizeOfHeaders
;
465 SumOfBytesHashed
= mNtHeader
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
469 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
471 mPeCoffHeaderOffset
+
473 sizeof (EFI_IMAGE_FILE_HEADER
) +
474 mNtHeader
.Pe32
->FileHeader
.SizeOfOptionalHeader
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.
483 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * mNtHeader
.Pe32
->FileHeader
.NumberOfSections
);
484 if (SectionHeader
== NULL
) {
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
494 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
496 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
497 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof (EFI_IMAGE_SECTION_HEADER
));
500 CopyMem (&SectionHeader
[Pos
], Section
, sizeof (EFI_IMAGE_SECTION_HEADER
));
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.
511 for (Index
= 0; Index
< mNtHeader
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
512 Section
= &SectionHeader
[Index
];
513 if (Section
->SizeOfRawData
== 0) {
516 HashBase
= mImageBase
+ Section
->PointerToRawData
;
517 HashSize
= (UINTN
) Section
->SizeOfRawData
;
519 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
524 SumOfBytesHashed
+= HashSize
;
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)
533 if (mImageSize
> SumOfBytesHashed
) {
534 HashBase
= mImageBase
+ SumOfBytesHashed
;
536 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
539 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
543 CertSize
= mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
548 CertSize
= mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
552 if (mImageSize
> CertSize
+ SumOfBytesHashed
) {
553 HashSize
= (UINTN
) (mImageSize
- CertSize
- SumOfBytesHashed
);
555 Status
= mHash
[HashAlg
].HashUpdate(HashCtx
, HashBase
, HashSize
);
559 } else if (mImageSize
< CertSize
+ SumOfBytesHashed
) {
565 Status
= mHash
[HashAlg
].HashFinal(HashCtx
, mImageDigest
);
568 if (HashCtx
!= NULL
) {
571 if (SectionHeader
!= NULL
) {
572 FreePool (SectionHeader
);
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
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.
586 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
587 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
589 @retval EFI_UNSUPPORTED Hash algorithm is not supported.
590 @retval EFI_SUCCESS Hash successfully.
596 IN UINTN AuthDataSize
601 for (Index
= 0; Index
< HASHALG_MAX
; Index
++) {
603 // Check the Hash algorithm in PE/COFF Authenticode.
604 // According to PKCS#7 Definition:
605 // SignedData ::= SEQUENCE {
607 // digestAlgorithms DigestAlgorithmIdentifiers,
608 // contentInfo ContentInfo,
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.
614 if ((*(AuthData
+ 1) & TWO_BYTE_ENCODE
) != TWO_BYTE_ENCODE
) {
616 // Only support two bytes of Long Form of Length Encoding.
621 if (AuthDataSize
< 32 + mHash
[Index
].OidLength
) {
622 return EFI_UNSUPPORTED
;
625 if (CompareMem (AuthData
+ 32, mHash
[Index
].OidValue
, mHash
[Index
].OidLength
) == 0) {
630 if (Index
== HASHALG_MAX
) {
631 return EFI_UNSUPPORTED
;
635 // HASH PE Image based on Hash algorithm in PE/COFF Authenticode.
637 if (!HashPeImage(Index
)) {
638 return EFI_UNSUPPORTED
;
646 Returns the size of a given image execution info table in bytes.
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.
651 @param ImageExeInfoTable A pointer to a image execution info table structure.
653 @retval 0 If ImageExeInfoTable is NULL.
654 @retval Others The size of a image execution info table in bytes.
658 GetImageExeInfoTableSize (
659 EFI_IMAGE_EXECUTION_INFO_TABLE
*ImageExeInfoTable
663 EFI_IMAGE_EXECUTION_INFO
*ImageExeInfoItem
;
666 if (ImageExeInfoTable
== NULL
) {
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
));
681 Create an Image Execution Information Table entry and add it to system configuration table.
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.
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
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
;
705 UINTN DevicePathSize
;
707 ImageExeInfoTable
= NULL
;
708 NewImageExeInfoTable
= NULL
;
709 ImageExeInfoEntry
= NULL
;
712 if (DevicePath
== NULL
) {
717 NameStringLen
= StrSize (Name
);
719 NameStringLen
= sizeof (CHAR16
);
722 EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid
, (VOID
**) &ImageExeInfoTable
);
723 if (ImageExeInfoTable
!= NULL
) {
725 // The table has been found!
726 // We must enlarge the table to accomodate the new exe info entry.
728 ImageExeInfoTableSize
= GetImageExeInfoTableSize (ImageExeInfoTable
);
732 // We should create a new table to append to the configuration table.
734 ImageExeInfoTableSize
= sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE
);
737 DevicePathSize
= GetDevicePathSize (DevicePath
);
738 NewImageExeInfoEntrySize
= sizeof (EFI_IMAGE_EXECUTION_INFO
) + NameStringLen
+ DevicePathSize
+ SignatureSize
;
739 NewImageExeInfoTable
= (EFI_IMAGE_EXECUTION_INFO_TABLE
*) AllocateRuntimePool (ImageExeInfoTableSize
+ NewImageExeInfoEntrySize
);
740 if (NewImageExeInfoTable
== NULL
) {
744 if (ImageExeInfoTable
!= NULL
) {
745 CopyMem (NewImageExeInfoTable
, ImageExeInfoTable
, ImageExeInfoTableSize
);
747 NewImageExeInfoTable
->NumberOfImages
= 0;
749 NewImageExeInfoTable
->NumberOfImages
++;
750 ImageExeInfoEntry
= (EFI_IMAGE_EXECUTION_INFO
*) ((UINT8
*) NewImageExeInfoTable
+ ImageExeInfoTableSize
);
752 // Update new item's infomation.
754 WriteUnaligned32 ((UINT32
*) &ImageExeInfoEntry
->Action
, Action
);
755 WriteUnaligned32 ((UINT32
*) &ImageExeInfoEntry
->InfoSize
, (UINT32
) NewImageExeInfoEntrySize
);
758 CopyMem ((UINT8
*) &ImageExeInfoEntry
->InfoSize
+ sizeof (UINT32
), Name
, NameStringLen
);
760 ZeroMem ((UINT8
*) &ImageExeInfoEntry
->InfoSize
+ sizeof (UINT32
), sizeof (CHAR16
));
763 (UINT8
*) &ImageExeInfoEntry
->InfoSize
+ sizeof (UINT32
) + NameStringLen
,
767 if (Signature
!= NULL
) {
769 (UINT8
*) &ImageExeInfoEntry
->InfoSize
+ sizeof (UINT32
) + NameStringLen
+ DevicePathSize
,
775 // Update/replace the image execution table.
777 gBS
->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid
, (VOID
*) NewImageExeInfoTable
);
780 // Free Old table data!
782 if (ImageExeInfoTable
!= NULL
) {
783 FreePool (ImageExeInfoTable
);
788 Check whether signature is in specified database.
790 @param[in] VariableName Name of database variable that is searched in.
791 @param[in] Signature Pointer to signature that is searched for.
792 @param[in] CertType Pointer to hash algrithom.
793 @param[in] SignatureSize Size of Signature.
795 @return TRUE Found the signature in the variable database.
796 @return FALSE Not found the signature in the variable database.
800 IsSignatureFoundInDatabase (
801 IN CHAR16
*VariableName
,
803 IN EFI_GUID
*CertType
,
804 IN UINTN SignatureSize
808 EFI_SIGNATURE_LIST
*CertList
;
809 EFI_SIGNATURE_DATA
*Cert
;
816 // Read signature database variable.
821 Status
= gRT
->GetVariable (VariableName
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, NULL
);
822 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
826 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
831 Status
= gRT
->GetVariable (VariableName
, &gEfiImageSecurityDatabaseGuid
, NULL
, &DataSize
, Data
);
832 if (EFI_ERROR (Status
)) {
836 // Enumerate all signature data in SigDB to check if executable's signature exists.
838 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
839 while ((DataSize
> 0) && (DataSize
>= CertList
->SignatureListSize
)) {
840 CertCount
= (CertList
->SignatureListSize
- CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
841 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
842 if ((CertList
->SignatureSize
== sizeof(EFI_SIGNATURE_DATA
) - 1 + SignatureSize
) && (CompareGuid(&CertList
->SignatureType
, CertType
))) {
843 for (Index
= 0; Index
< CertCount
; Index
++) {
844 if (CompareMem (Cert
->SignatureData
, Signature
, SignatureSize
) == 0) {
846 // Find the signature in database.
852 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
860 DataSize
-= CertList
->SignatureListSize
;
861 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
873 Verify PKCS#7 SignedData using certificate found in Variable which formatted
874 as EFI_SIGNATURE_LIST. The Variable may be PK, KEK, DB or DBX.
876 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
877 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
878 @param[in] VariableName Name of Variable to search for Certificate.
879 @param[in] VendorGuid Variable vendor GUID.
881 @retval TRUE Image pass verification.
882 @retval FALSE Image fail verification.
886 IsPkcsSignedDataVerifiedBySignatureList (
888 IN UINTN AuthDataSize
,
889 IN CHAR16
*VariableName
,
890 IN EFI_GUID
*VendorGuid
894 BOOLEAN VerifyStatus
;
895 EFI_SIGNATURE_LIST
*CertList
;
896 EFI_SIGNATURE_DATA
*Cert
;
909 VerifyStatus
= FALSE
;
912 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
913 if (Status
== EFI_BUFFER_TOO_SMALL
) {
914 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
919 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, (VOID
*) Data
);
920 if (EFI_ERROR (Status
)) {
925 // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.
927 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
928 while ((DataSize
> 0) && (DataSize
>= CertList
->SignatureListSize
)) {
929 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
930 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
931 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
932 for (Index
= 0; Index
< CertCount
; Index
++) {
934 // Iterate each Signature Data Node within this CertList for verify.
936 RootCert
= Cert
->SignatureData
;
937 RootCertSize
= CertList
->SignatureSize
- sizeof (EFI_GUID
);
940 // Call AuthenticodeVerify library to Verify Authenticode struct.
942 VerifyStatus
= AuthenticodeVerify (
953 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
956 DataSize
-= CertList
->SignatureListSize
;
957 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
970 Verify certificate in WIN_CERT_TYPE_PKCS_SIGNED_DATA format.
972 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed image.
973 @param[in] AuthDataSize Size of the Authenticode Signature in bytes.
975 @retval EFI_SUCCESS Image pass verification.
976 @retval EFI_SECURITY_VIOLATION Image fail verification.
980 VerifyCertPkcsSignedData (
982 IN UINTN AuthDataSize
986 // 1: Find certificate from DBX forbidden database for revoked certificate.
988 if (IsPkcsSignedDataVerifiedBySignatureList (AuthData
, AuthDataSize
, EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
)) {
990 // DBX is forbidden database, if Authenticode verification pass with
991 // one of the certificate in DBX, this image should be rejected.
993 return EFI_SECURITY_VIOLATION
;
997 // 2: Find certificate from DB database and try to verify authenticode struct.
999 if (IsPkcsSignedDataVerifiedBySignatureList (AuthData
, AuthDataSize
, EFI_IMAGE_SECURITY_DATABASE
, &gEfiImageSecurityDatabaseGuid
)) {
1002 return EFI_SECURITY_VIOLATION
;
1007 Provide verification service for signed images, which include both signature validation
1008 and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and
1009 MSFT Authenticode type signatures are supported.
1011 In this implementation, only verify external executables when in USER MODE.
1012 Executables from FV is bypass, so pass in AuthenticationStatus is ignored.
1014 The image verification process is:
1015 If the image is signed,
1016 If the image's certificate verifies against a certificate (root or intermediate) in the allowed
1017 database (DB) and not in the forbidden database (DBX), the certificate verification is passed.
1018 If the image's hash digest is in DBX,
1022 If the Image's certificate verification failed.
1023 If the Image's Hash is in DB and not in DBX,
1027 Otherwise, the image is not signed,
1028 Is the Image's Hash in DBX?
1029 If yes, deny execution.
1030 If not, is the Image's Hash in DB?
1032 If not, deny execution.
1034 Caution: This function may receive untrusted input.
1035 PE/COFF image is external input, so this function will validate its data structure
1036 within this image buffer before use.
1038 @param[in] AuthenticationStatus
1039 This is the authentication status returned from the security
1040 measurement services for the input file.
1041 @param[in] File This is a pointer to the device path of the file that is
1042 being dispatched. This will optionally be used for logging.
1043 @param[in] FileBuffer File buffer matches the input file device path.
1044 @param[in] FileSize Size of File buffer matches the input file device path.
1045 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.
1047 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL
1048 FileBuffer did authenticate, and the platform policy dictates
1049 that the DXE Foundation may use the file.
1050 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath
1051 and non-NULL FileBuffer did authenticate, and the platform
1052 policy dictates that the DXE Foundation may execute the image in
1054 @retval EFI_OUT_RESOURCE Fail to allocate memory.
1055 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
1056 the platform policy dictates that File should be placed
1057 in the untrusted state. The image has been added to the file
1059 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not
1060 authenticate, and the platform policy dictates that the DXE
1061 Foundation many not use File.
1066 DxeImageVerificationHandler (
1067 IN UINT32 AuthenticationStatus
,
1068 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
,
1069 IN VOID
*FileBuffer
,
1071 IN BOOLEAN BootPolicy
1076 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1077 EFI_STATUS VerifyStatus
;
1078 EFI_SIGNATURE_LIST
*SignatureList
;
1079 UINTN SignatureListSize
;
1080 EFI_SIGNATURE_DATA
*Signature
;
1081 EFI_IMAGE_EXECUTION_ACTION Action
;
1082 WIN_CERTIFICATE
*WinCertificate
;
1085 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1086 UINT32 NumberOfRvaAndSizes
;
1088 WIN_CERTIFICATE_EFI_PKCS
*PkcsCertData
;
1089 WIN_CERTIFICATE_UEFI_GUID
*WinCertUefiGuid
;
1092 EFI_IMAGE_DATA_DIRECTORY
*SecDataDir
;
1094 SignatureList
= NULL
;
1095 SignatureListSize
= 0;
1096 WinCertificate
= NULL
;
1098 PkcsCertData
= NULL
;
1099 Action
= EFI_IMAGE_EXECUTION_AUTH_UNTESTED
;
1100 Status
= EFI_ACCESS_DENIED
;
1102 // Check the image type and get policy setting.
1104 switch (GetImageType (File
)) {
1107 Policy
= ALWAYS_EXECUTE
;
1110 case IMAGE_FROM_OPTION_ROM
:
1111 Policy
= PcdGet32 (PcdOptionRomImageVerificationPolicy
);
1114 case IMAGE_FROM_REMOVABLE_MEDIA
:
1115 Policy
= PcdGet32 (PcdRemovableMediaImageVerificationPolicy
);
1118 case IMAGE_FROM_FIXED_MEDIA
:
1119 Policy
= PcdGet32 (PcdFixedMediaImageVerificationPolicy
);
1123 Policy
= DENY_EXECUTE_ON_SECURITY_VIOLATION
;
1127 // If policy is always/never execute, return directly.
1129 if (Policy
== ALWAYS_EXECUTE
) {
1131 } else if (Policy
== NEVER_EXECUTE
) {
1132 return EFI_ACCESS_DENIED
;
1135 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME
, (VOID
**)&SecureBoot
, NULL
);
1137 // Skip verification if SecureBoot variable doesn't exist.
1139 if (SecureBoot
== NULL
) {
1144 // Skip verification if SecureBoot is disabled.
1146 if (*SecureBoot
== SECURE_BOOT_MODE_DISABLE
) {
1147 FreePool (SecureBoot
);
1150 FreePool (SecureBoot
);
1153 // Read the Dos header.
1155 if (FileBuffer
== NULL
) {
1156 return EFI_INVALID_PARAMETER
;
1159 mImageBase
= (UINT8
*) FileBuffer
;
1160 mImageSize
= FileSize
;
1162 ZeroMem (&ImageContext
, sizeof (ImageContext
));
1163 ImageContext
.Handle
= (VOID
*) FileBuffer
;
1164 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) DxeImageVerificationLibImageRead
;
1167 // Get information about the image being loaded
1169 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1170 if (EFI_ERROR (Status
)) {
1172 // The information can't be got from the invalid PeImage
1177 Status
= EFI_ACCESS_DENIED
;
1179 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) mImageBase
;
1180 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1182 // DOS image header is present,
1183 // so read the PE header after the DOS image header.
1185 mPeCoffHeaderOffset
= DosHdr
->e_lfanew
;
1187 mPeCoffHeaderOffset
= 0;
1190 // Check PE/COFF image.
1192 mNtHeader
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*) (mImageBase
+ mPeCoffHeaderOffset
);
1193 if (mNtHeader
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1195 // It is not a valid Pe/Coff file.
1200 if (mNtHeader
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& mNtHeader
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1202 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1203 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1204 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1205 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1207 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1210 // Get the magic value from the PE/COFF Optional Header
1212 Magic
= mNtHeader
.Pe32
->OptionalHeader
.Magic
;
1215 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1219 NumberOfRvaAndSizes
= mNtHeader
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1220 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
1221 SecDataDir
= (EFI_IMAGE_DATA_DIRECTORY
*) &mNtHeader
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
];
1225 // Use PE32+ offset.
1227 NumberOfRvaAndSizes
= mNtHeader
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1228 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
1229 SecDataDir
= (EFI_IMAGE_DATA_DIRECTORY
*) &mNtHeader
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
];
1233 if ((SecDataDir
== NULL
) || ((SecDataDir
!= NULL
) && (SecDataDir
->Size
== 0))) {
1235 // This image is not signed.
1237 if (!HashPeImage (HASHALG_SHA256
)) {
1241 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1
, mImageDigest
, &mCertType
, mImageDigestSize
)) {
1243 // Image Hash is in forbidden database (DBX).
1248 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE
, mImageDigest
, &mCertType
, mImageDigestSize
)) {
1250 // Image Hash is in allowed database (DB).
1256 // Image Hash is not found in both forbidden and allowed database.
1262 // Verify signature of executables.
1264 WinCertificate
= (WIN_CERTIFICATE
*) (mImageBase
+ SecDataDir
->VirtualAddress
);
1266 CertSize
= sizeof (WIN_CERTIFICATE
);
1268 if ((SecDataDir
->Size
<= CertSize
) || (SecDataDir
->Size
< WinCertificate
->dwLength
)) {
1273 // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.
1275 if (WinCertificate
->wCertificateType
== WIN_CERT_TYPE_PKCS_SIGNED_DATA
) {
1277 // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the
1278 // Authenticode specification.
1280 PkcsCertData
= (WIN_CERTIFICATE_EFI_PKCS
*) WinCertificate
;
1281 if (PkcsCertData
->Hdr
.dwLength
<= sizeof (PkcsCertData
->Hdr
)) {
1284 AuthData
= PkcsCertData
->CertData
;
1285 AuthDataSize
= PkcsCertData
->Hdr
.dwLength
- sizeof(PkcsCertData
->Hdr
);
1287 Status
= HashPeImageByType (AuthData
, AuthDataSize
);
1288 if (EFI_ERROR (Status
)) {
1292 VerifyStatus
= VerifyCertPkcsSignedData (AuthData
, AuthDataSize
);
1293 } else if (WinCertificate
->wCertificateType
== WIN_CERT_TYPE_EFI_GUID
) {
1295 // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.
1297 WinCertUefiGuid
= (WIN_CERTIFICATE_UEFI_GUID
*) WinCertificate
;
1298 if (!CompareGuid(&WinCertUefiGuid
->CertType
, &gEfiCertPkcs7Guid
) ||
1299 (WinCertUefiGuid
->Hdr
.dwLength
<= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID
, CertData
))) {
1302 AuthData
= WinCertUefiGuid
->CertData
;
1303 AuthDataSize
= WinCertUefiGuid
->Hdr
.dwLength
- OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID
, CertData
);
1305 Status
= HashPeImageByType (AuthData
, AuthDataSize
);
1306 if (EFI_ERROR (Status
)) {
1309 VerifyStatus
= VerifyCertPkcsSignedData (AuthData
, AuthDataSize
);
1314 if (!EFI_ERROR (VerifyStatus
)) {
1316 // Verification is passed.
1317 // Continue to check the image digest in signature database.
1319 if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1
, mImageDigest
, &mCertType
, mImageDigestSize
)) {
1321 // Executable signature verification passes, but is found in forbidden signature database.
1323 Action
= EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND
;
1324 Status
= EFI_ACCESS_DENIED
;
1327 // For image verification against enrolled X.509 certificate(root or intermediate),
1328 // no need to check image's hash in the allowed database.
1334 // Verification failure.
1336 if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1
, mImageDigest
, &mCertType
, mImageDigestSize
) &&
1337 IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE
, mImageDigest
, &mCertType
, mImageDigestSize
)) {
1339 // Verification fail, Image Hash is not in forbidden database (DBX),
1340 // and Image Hash is in allowed database (DB).
1342 Status
= EFI_SUCCESS
;
1344 Action
= EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED
;
1345 Status
= EFI_ACCESS_DENIED
;
1349 if (EFI_ERROR (Status
)) {
1351 // Get image hash value as executable's signature.
1353 SignatureListSize
= sizeof (EFI_SIGNATURE_LIST
) + sizeof (EFI_SIGNATURE_DATA
) - 1 + mImageDigestSize
;
1354 SignatureList
= (EFI_SIGNATURE_LIST
*) AllocateZeroPool (SignatureListSize
);
1355 if (SignatureList
== NULL
) {
1356 Status
= EFI_OUT_OF_RESOURCES
;
1359 SignatureList
->SignatureHeaderSize
= 0;
1360 SignatureList
->SignatureListSize
= (UINT32
) SignatureListSize
;
1361 SignatureList
->SignatureSize
= (UINT32
) mImageDigestSize
;
1362 CopyMem (&SignatureList
->SignatureType
, &mCertType
, sizeof (EFI_GUID
));
1363 Signature
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) SignatureList
+ sizeof (EFI_SIGNATURE_LIST
));
1364 CopyMem (Signature
->SignatureData
, mImageDigest
, mImageDigestSize
);
1368 if (Status
!= EFI_SUCCESS
) {
1370 // Policy decides to defer or reject the image; add its information in image executable information table.
1372 AddImageExeInfo (Action
, NULL
, File
, SignatureList
, SignatureListSize
);
1373 Status
= EFI_SECURITY_VIOLATION
;
1376 if (SignatureList
!= NULL
) {
1377 FreePool (SignatureList
);
1384 When VariableWriteArchProtocol install, create "SecureBoot" variable.
1386 @param[in] Event Event whose notification function is being invoked.
1387 @param[in] Context Pointer to the notification function's context.
1392 VariableWriteCallBack (
1397 UINT8 SecureBootMode
;
1398 UINT8
*SecureBootModePtr
;
1400 VOID
*ProtocolPointer
;
1402 Status
= gBS
->LocateProtocol (&gEfiVariableWriteArchProtocolGuid
, NULL
, &ProtocolPointer
);
1403 if (EFI_ERROR (Status
)) {
1408 // Check whether "SecureBoot" variable exists.
1409 // If this library is built-in, it means firmware has capability to perform
1410 // driver signing verification.
1412 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME
, (VOID
**)&SecureBootModePtr
, NULL
);
1413 if (SecureBootModePtr
== NULL
) {
1414 SecureBootMode
= SECURE_BOOT_MODE_DISABLE
;
1416 // Authenticated variable driver will update "SecureBoot" depending on SetupMode variable.
1419 EFI_SECURE_BOOT_MODE_NAME
,
1420 &gEfiGlobalVariableGuid
,
1421 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1426 FreePool (SecureBootModePtr
);
1431 Register security measurement handler.
1433 @param ImageHandle ImageHandle of the loaded driver.
1434 @param SystemTable Pointer to the EFI System Table.
1436 @retval EFI_SUCCESS The handlers were registered successfully.
1440 DxeImageVerificationLibConstructor (
1441 IN EFI_HANDLE ImageHandle
,
1442 IN EFI_SYSTEM_TABLE
*SystemTable
1448 // Register callback function upon VariableWriteArchProtocol.
1450 EfiCreateProtocolNotifyEvent (
1451 &gEfiVariableWriteArchProtocolGuid
,
1453 VariableWriteCallBack
,
1458 return RegisterSecurity2Handler (
1459 DxeImageVerificationHandler
,
1460 EFI_AUTH_OPERATION_VERIFY_IMAGE
| EFI_AUTH_OPERATION_IMAGE_REQUIRED