]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
SecurityPkg: AuthVariableLib: Customized SecureBoot Mode transition.
[mirror_edk2.git] / SecurityPkg / Library / DxeImageVerificationLib / DxeImageVerificationLib.c
index 8860daeafac4781a5d0ccfcb6bf873d23df333af..4b4d3bf77de53eefdbe0e9221e95434fd1d003aa 100644 (file)
@@ -1,5 +1,5 @@
 /** @file\r
-  Implement image verification services for secure boot service in UEFI2.3.1.\r
+  Implement image verification services for secure boot service\r
 \r
   Caution: This file requires additional review when modified.\r
   This library will have external input - PE/COFF image.\r
@@ -12,7 +12,7 @@
   DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept\r
   untrusted PE/COFF image and validate its data structure within this image buffer before use.\r
 \r
-Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -64,11 +64,11 @@ UINT8 mHashOidValue[] = {
   };\r
 \r
 HASH_TABLE mHash[] = {\r
-  { L"SHA1",   20, &mHashOidValue[0],  5, Sha1GetContextSize,  Sha1Init,   Sha1Update,    Sha1Final  },\r
-  { L"SHA224", 28, &mHashOidValue[5],  9, NULL,                NULL,       NULL,          NULL       },\r
-  { L"SHA256", 32, &mHashOidValue[14], 9, Sha256GetContextSize,Sha256Init, Sha256Update,  Sha256Final},\r
-  { L"SHA384", 48, &mHashOidValue[23], 9, NULL,                NULL,       NULL,          NULL       },\r
-  { L"SHA512", 64, &mHashOidValue[32], 9, NULL,                NULL,       NULL,          NULL       }\r
+  { L"SHA1",   20, &mHashOidValue[0],  5, Sha1GetContextSize,   Sha1Init,   Sha1Update,   Sha1Final  },\r
+  { L"SHA224", 28, &mHashOidValue[5],  9, NULL,                 NULL,       NULL,         NULL       },\r
+  { L"SHA256", 32, &mHashOidValue[14], 9, Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final},\r
+  { L"SHA384", 48, &mHashOidValue[23], 9, Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final},\r
+  { L"SHA512", 64, &mHashOidValue[32], 9, Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final}\r
 };\r
 \r
 /**\r
@@ -99,11 +99,11 @@ SecureBootHook (
 \r
   @param  FileHandle      Pointer to the file handle to read the PE/COFF image.\r
   @param  FileOffset      Offset into the PE/COFF image to begin the read operation.\r
-  @param  ReadSize        On input, the size in bytes of the requested read operation.  \r
+  @param  ReadSize        On input, the size in bytes of the requested read operation.\r
                           On output, the number of bytes actually read.\r
   @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
-  \r
-  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size \r
+\r
+  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -117,7 +117,7 @@ DxeImageVerificationLibImageRead (
   UINTN               EndPosition;\r
 \r
   if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
-    return EFI_INVALID_PARAMETER;    \r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if (MAX_ADDRESS - FileOffset < *ReadSize) {\r
@@ -270,7 +270,7 @@ GetImageType (
 }\r
 \r
 /**\r
-  Caculate hash of Pe/Coff image based on the authenticode image hashing in\r
+  Calculate hash of Pe/Coff image based on the authenticode image hashing in\r
   PE/COFF Specification 8.0 Appendix A\r
 \r
   Caution: This function may receive untrusted input.\r
@@ -306,7 +306,7 @@ HashPeImage (
   SectionHeader = NULL;\r
   Status        = FALSE;\r
 \r
-  if ((HashAlg != HASHALG_SHA1) && (HashAlg != HASHALG_SHA256)) {\r
+  if ((HashAlg >= HASHALG_MAX)) {\r
     return FALSE;\r
   }\r
 \r
@@ -315,13 +315,28 @@ HashPeImage (
   //\r
   ZeroMem (mImageDigest, MAX_DIGEST_SIZE);\r
 \r
-  if (HashAlg == HASHALG_SHA1) {\r
-    mImageDigestSize  = SHA1_DIGEST_SIZE;\r
-    mCertType         = gEfiCertSha1Guid;\r
-  } else if (HashAlg == HASHALG_SHA256) {\r
-    mImageDigestSize  = SHA256_DIGEST_SIZE;\r
-    mCertType         = gEfiCertSha256Guid;\r
-  } else {\r
+  switch (HashAlg) {\r
+  case HASHALG_SHA1:\r
+    mImageDigestSize = SHA1_DIGEST_SIZE;\r
+    mCertType        = gEfiCertSha1Guid;\r
+    break;\r
+\r
+  case HASHALG_SHA256:\r
+    mImageDigestSize = SHA256_DIGEST_SIZE;\r
+    mCertType        = gEfiCertSha256Guid;\r
+    break;\r
+\r
+  case HASHALG_SHA384:\r
+    mImageDigestSize = SHA384_DIGEST_SIZE;\r
+    mCertType        = gEfiCertSha384Guid;\r
+    break;\r
+\r
+  case HASHALG_SHA512:\r
+    mImageDigestSize = SHA512_DIGEST_SIZE;\r
+    mCertType        = gEfiCertSha512Guid;\r
+    break;\r
+\r
+  default:\r
     return FALSE;\r
   }\r
 \r
@@ -347,8 +362,8 @@ HashPeImage (
   //\r
   if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     //\r
-    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r
-    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the \r
+    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
+    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
     //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
     //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
     //\r
@@ -359,7 +374,7 @@ HashPeImage (
     //\r
     Magic =  mNtHeader.Pe32->OptionalHeader.Magic;\r
   }\r
-  \r
+\r
   //\r
   // 3.  Calculate the distance from the base of the image header to the image checksum address.\r
   // 4.  Hash the image header from its base to beginning of the image checksum.\r
@@ -466,7 +481,7 @@ HashPeImage (
       if (!Status) {\r
         goto Done;\r
       }\r
-    }    \r
+    }\r
   }\r
 \r
   //\r
@@ -594,7 +609,7 @@ Done:
 }\r
 \r
 /**\r
-  Recognize the Hash algorithm in PE/COFF Authenticode and caculate hash of\r
+  Recognize the Hash algorithm in PE/COFF Authenticode and calculate hash of\r
   Pe/Coff image based on the authenticode image hashing in PE/COFF Specification\r
   8.0 Appendix A\r
 \r
@@ -604,7 +619,7 @@ Done:
 \r
   @param[in]  AuthData            Pointer to the Authenticode Signature retrieved from signed image.\r
   @param[in]  AuthDataSize        Size of the Authenticode Signature in bytes.\r
-  \r
+\r
   @retval EFI_UNSUPPORTED             Hash algorithm is not supported.\r
   @retval EFI_SUCCESS                 Hash successfully.\r
 \r
@@ -696,6 +711,58 @@ GetImageExeInfoTableSize (
   return TotalSize;\r
 }\r
 \r
+/**\r
+  Create signature list based on input signature data and certificate type GUID. Caller is reposible \r
+  to free new created SignatureList.\r
+\r
+  @param[in]   SignatureData           Signature data in SignatureList.\r
+  @param[in]   SignatureDataSize       Signature data size.\r
+  @param[in]   CertType                Certificate Type.\r
+  @param[out]  SignatureList           Created SignatureList.\r
+  @param[out]  SignatureListSize       Created SignatureListSize.\r
+\r
+  @return EFI_OUT_OF_RESOURCES         The operation is failed due to lack of resources.\r
+  @retval EFI_SUCCESS          Successfully create signature list.\r
+\r
+**/\r
+EFI_STATUS\r
+CreateSignatureList(\r
+  IN UINT8                *SignatureData,\r
+  IN UINTN                SignatureDataSize,\r
+  IN EFI_GUID             *CertType,\r
+  OUT EFI_SIGNATURE_LIST  **SignatureList,\r
+  OUT UINTN               *SignatureListSize\r
+  )\r
+{\r
+  EFI_SIGNATURE_LIST   *SignList;\r
+  UINTN                SignListSize;\r
+  EFI_SIGNATURE_DATA   *Signature;\r
+\r
+  SignList       = NULL;\r
+  *SignatureList = NULL;\r
+\r
+  SignListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + SignatureDataSize;\r
+  SignList     = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignListSize);\r
+  if (SignList == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  SignList->SignatureHeaderSize = 0;\r
+  SignList->SignatureListSize   = (UINT32) SignListSize;\r
+  SignList->SignatureSize       = (UINT32) SignatureDataSize + sizeof (EFI_SIGNATURE_DATA) - 1;\r
+  CopyMem (&SignList->SignatureType, CertType, sizeof (EFI_GUID));\r
+\r
+  DEBUG((EFI_D_INFO, "SignatureDataSize %x\n", SignatureDataSize));\r
+  Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignList + sizeof (EFI_SIGNATURE_LIST));\r
+  CopyMem (Signature->SignatureData, SignatureData, SignatureDataSize);\r
+\r
+  *SignatureList     = SignList;\r
+  *SignatureListSize = SignListSize;\r
+\r
+  return EFI_SUCCESS;\r
+\r
+}\r
+\r
 /**\r
   Create an Image Execution Information Table entry and add it to system configuration table.\r
 \r
@@ -722,11 +789,13 @@ AddImageExeInfo (
   UINTN                           NewImageExeInfoEntrySize;\r
   UINTN                           NameStringLen;\r
   UINTN                           DevicePathSize;\r
+  CHAR16                          *NameStr;\r
 \r
   ImageExeInfoTable     = NULL;\r
   NewImageExeInfoTable  = NULL;\r
   ImageExeInfoEntry     = NULL;\r
   NameStringLen         = 0;\r
+  NameStr               = NULL;\r
 \r
   if (DevicePath == NULL) {\r
     return ;\r
@@ -754,7 +823,12 @@ AddImageExeInfo (
   }\r
 \r
   DevicePathSize            = GetDevicePathSize (DevicePath);\r
-  NewImageExeInfoEntrySize  = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;\r
+\r
+  //\r
+  // Signature size can be odd. Pad after signature to ensure next EXECUTION_INFO entry align\r
+  //\r
+  NewImageExeInfoEntrySize = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;\r
+\r
   NewImageExeInfoTable      = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize + NewImageExeInfoEntrySize);\r
   if (NewImageExeInfoTable == NULL) {\r
     return ;\r
@@ -768,24 +842,26 @@ AddImageExeInfo (
   NewImageExeInfoTable->NumberOfImages++;\r
   ImageExeInfoEntry = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) NewImageExeInfoTable + ImageExeInfoTableSize);\r
   //\r
-  // Update new item's infomation.\r
+  // Update new item's information.\r
   //\r
-  WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->Action, Action);\r
-  WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->InfoSize, (UINT32) NewImageExeInfoEntrySize);\r
+  WriteUnaligned32 ((UINT32 *) ImageExeInfoEntry, Action);\r
+  WriteUnaligned32 ((UINT32 *) ((UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION)), (UINT32) NewImageExeInfoEntrySize);\r
 \r
+  NameStr = (CHAR16 *)(ImageExeInfoEntry + 1);\r
   if (Name != NULL) {\r
-    CopyMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), Name, NameStringLen);\r
+    CopyMem ((UINT8 *) NameStr, Name, NameStringLen);\r
   } else {\r
-    ZeroMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), sizeof (CHAR16));\r
+    ZeroMem ((UINT8 *) NameStr, sizeof (CHAR16));\r
   }\r
+\r
   CopyMem (\r
-    (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen,\r
+    (UINT8 *) NameStr + NameStringLen,\r
     DevicePath,\r
     DevicePathSize\r
     );\r
   if (Signature != NULL) {\r
     CopyMem (\r
-      (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen + DevicePathSize,\r
+      (UINT8 *) NameStr + NameStringLen + DevicePathSize,\r
       Signature,\r
       SignatureSize\r
       );\r
@@ -803,6 +879,135 @@ AddImageExeInfo (
   }\r
 }\r
 \r
+/**\r
+  Check whether the hash of an given X.509 certificate is in forbidden database (DBX).\r
+\r
+  @param[in]  Certificate       Pointer to X.509 Certificate that is searched for.\r
+  @param[in]  CertSize          Size of X.509 Certificate.\r
+  @param[in]  SignatureList     Pointer to the Signature List in forbidden database.\r
+  @param[in]  SignatureListSize Size of Signature List.\r
+  @param[out] RevocationTime    Return the time that the certificate was revoked.\r
+\r
+  @return TRUE   The certificate hash is found in the forbidden database.\r
+  @return FALSE  The certificate hash is not found in the forbidden database.\r
+\r
+**/\r
+BOOLEAN\r
+IsCertHashFoundInDatabase (\r
+  IN  UINT8               *Certificate,\r
+  IN  UINTN               CertSize,\r
+  IN  EFI_SIGNATURE_LIST  *SignatureList,\r
+  IN  UINTN               SignatureListSize,\r
+  OUT EFI_TIME            *RevocationTime\r
+  )\r
+{\r
+  BOOLEAN             IsFound;\r
+  BOOLEAN             Status;\r
+  EFI_SIGNATURE_LIST  *DbxList;\r
+  UINTN               DbxSize;\r
+  EFI_SIGNATURE_DATA  *CertHash;\r
+  UINTN               CertHashCount;\r
+  UINTN               Index;\r
+  UINT32              HashAlg;\r
+  VOID                *HashCtx;\r
+  UINT8               CertDigest[MAX_DIGEST_SIZE];\r
+  UINT8               *DbxCertHash;\r
+  UINTN               SiglistHeaderSize;\r
+  UINT8               *TBSCert;\r
+  UINTN               TBSCertSize;\r
+\r
+  IsFound  = FALSE;\r
+  DbxList  = SignatureList;\r
+  DbxSize  = SignatureListSize;\r
+  HashCtx  = NULL;\r
+  HashAlg  = HASHALG_MAX;\r
+\r
+  if ((RevocationTime == NULL) || (DbxList == NULL)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Retrieve the TBSCertificate from the X.509 Certificate.\r
+  //\r
+  if (!X509GetTBSCert (Certificate, CertSize, &TBSCert, &TBSCertSize)) {\r
+    return FALSE;\r
+  }\r
+\r
+  while ((DbxSize > 0) && (SignatureListSize >= DbxList->SignatureListSize)) {\r
+    //\r
+    // Determine Hash Algorithm of Certificate in the forbidden database.\r
+    //\r
+    if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha256Guid)) {\r
+      HashAlg = HASHALG_SHA256;\r
+    } else if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha384Guid)) {\r
+      HashAlg = HASHALG_SHA384;\r
+    } else if (CompareGuid (&DbxList->SignatureType, &gEfiCertX509Sha512Guid)) {\r
+      HashAlg = HASHALG_SHA512;\r
+    } else {\r
+      DbxSize -= DbxList->SignatureListSize;\r
+      DbxList  = (EFI_SIGNATURE_LIST *) ((UINT8 *) DbxList + DbxList->SignatureListSize);\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Calculate the hash value of current TBSCertificate for comparision.\r
+    //\r
+    if (mHash[HashAlg].GetContextSize == NULL) {\r
+      goto Done;\r
+    }\r
+    ZeroMem (CertDigest, MAX_DIGEST_SIZE);\r
+    HashCtx = AllocatePool (mHash[HashAlg].GetContextSize ());\r
+    if (HashCtx == NULL) {\r
+      goto Done;\r
+    }\r
+    Status = mHash[HashAlg].HashInit (HashCtx);\r
+    if (!Status) {\r
+      goto Done;\r
+    }\r
+    Status = mHash[HashAlg].HashUpdate (HashCtx, TBSCert, TBSCertSize);\r
+    if (!Status) {\r
+      goto Done;\r
+    }\r
+    Status = mHash[HashAlg].HashFinal (HashCtx, CertDigest);\r
+    if (!Status) {\r
+      goto Done;\r
+    }\r
+\r
+    SiglistHeaderSize = sizeof (EFI_SIGNATURE_LIST) + DbxList->SignatureHeaderSize;\r
+    CertHash          = (EFI_SIGNATURE_DATA *) ((UINT8 *) DbxList + SiglistHeaderSize);\r
+    CertHashCount     = (DbxList->SignatureListSize - SiglistHeaderSize) / DbxList->SignatureSize;\r
+    for (Index = 0; Index < CertHashCount; Index++) {\r
+      //\r
+      // Iterate each Signature Data Node within this CertList for verify.\r
+      //\r
+      DbxCertHash = CertHash->SignatureData;\r
+      if (CompareMem (DbxCertHash, CertDigest, mHash[HashAlg].DigestLength) == 0) {\r
+        //\r
+        // Hash of Certificate is found in forbidden database.\r
+        //\r
+        IsFound = TRUE;\r
+\r
+        //\r
+        // Return the revocation time.\r
+        //\r
+        CopyMem (RevocationTime, (EFI_TIME *)(DbxCertHash + mHash[HashAlg].DigestLength), sizeof (EFI_TIME));\r
+        goto Done;\r
+      }\r
+      CertHash = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertHash + DbxList->SignatureSize);\r
+    }\r
+\r
+    DbxSize -= DbxList->SignatureListSize;\r
+    DbxList  = (EFI_SIGNATURE_LIST *) ((UINT8 *) DbxList + DbxList->SignatureListSize);\r
+  }\r
+\r
+Done:\r
+  if (HashCtx != NULL) {\r
+    FreePool (HashCtx);\r
+  }\r
+\r
+  return IsFound;\r
+}\r
+\r
 /**\r
   Check whether signature is in specified database.\r
 \r
@@ -831,6 +1036,7 @@ IsSignatureFoundInDatabase (
   UINTN               Index;\r
   UINTN               CertCount;\r
   BOOLEAN             IsFound;\r
+\r
   //\r
   // Read signature database variable.\r
   //\r
@@ -890,162 +1096,1043 @@ Done:
 }\r
 \r
 /**\r
-  Verify PKCS#7 SignedData using certificate found in Variable which formatted\r
-  as EFI_SIGNATURE_LIST. The Variable may be PK, KEK, DB or DBX.\r
+  Check whether the timestamp is valid by comparing the signing time and the revocation time.\r
 \r
-  @param[in]  AuthData      Pointer to the Authenticode Signature retrieved from signed image.\r
-  @param[in]  AuthDataSize  Size of the Authenticode Signature in bytes.\r
-  @param[in]  VariableName  Name of Variable to search for Certificate.\r
-  @param[in]  VendorGuid    Variable vendor GUID.\r
+  @param SigningTime         A pointer to the signing time.\r
+  @param RevocationTime      A pointer to the revocation time.\r
 \r
-  @retval TRUE         Image pass verification.\r
-  @retval FALSE        Image fail verification.\r
+  @retval  TRUE              The SigningTime is not later than the RevocationTime.\r
+  @retval  FALSE             The SigningTime is later than the RevocationTime.\r
 \r
 **/\r
 BOOLEAN\r
-IsPkcsSignedDataVerifiedBySignatureList (\r
-  IN UINT8              *AuthData,\r
-  IN UINTN              AuthDataSize,\r
-  IN CHAR16             *VariableName,\r
-  IN EFI_GUID           *VendorGuid\r
+IsValidSignatureByTimestamp (\r
+  IN EFI_TIME               *SigningTime,\r
+  IN EFI_TIME               *RevocationTime\r
+  )\r
+{\r
+  if (SigningTime->Year != RevocationTime->Year) {\r
+    return (BOOLEAN) (SigningTime->Year < RevocationTime->Year);\r
+  } else if (SigningTime->Month != RevocationTime->Month) {\r
+    return (BOOLEAN) (SigningTime->Month < RevocationTime->Month);\r
+  } else if (SigningTime->Day != RevocationTime->Day) {\r
+    return (BOOLEAN) (SigningTime->Day < RevocationTime->Day);\r
+  } else if (SigningTime->Hour != RevocationTime->Hour) {\r
+    return (BOOLEAN) (SigningTime->Hour < RevocationTime->Hour);\r
+  } else if (SigningTime->Minute != RevocationTime->Minute) {\r
+    return (BOOLEAN) (SigningTime->Minute < RevocationTime->Minute);\r
+  }\r
+\r
+  return (BOOLEAN) (SigningTime->Second <= RevocationTime->Second);\r
+}\r
+\r
+/**\r
+  Check if the given time value is zero.\r
+\r
+  @param[in]  Time      Pointer of a time value.\r
+\r
+  @retval     TRUE      The Time is Zero.\r
+  @retval     FALSE     The Time is not Zero.\r
+\r
+**/\r
+BOOLEAN\r
+IsTimeZero (\r
+  IN EFI_TIME               *Time\r
+  )\r
+{\r
+  if ((Time->Year == 0) && (Time->Month == 0) &&  (Time->Day == 0) &&\r
+      (Time->Hour == 0) && (Time->Minute == 0) && (Time->Second == 0)) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Record multiple certificate list & verification state of a verified image to \r
+  IMAGE_EXECUTION_TABLE.\r
+\r
+  @param[in]  CertBuf              Certificate list buffer.\r
+  @param[in]  CertBufLength        Certificate list buffer.\r
+  @param[in]  Action               Certificate list action to be record.\r
+  @param[in]  ImageName            Image name.\r
+  @param[in]  ImageDevicePath      Image device path.\r
+\r
+**/\r
+VOID \r
+RecordCertListToImageExeuctionTable(\r
+  IN UINT8                          *CertBuf,\r
+  IN UINTN                           CertBufLength,\r
+  IN EFI_IMAGE_EXECUTION_ACTION      Action,\r
+  IN CHAR16                         *ImageName OPTIONAL,\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath OPTIONAL\r
+  )\r
+{\r
+  UINT8               CertNumber;\r
+  UINT8               *CertPtr;\r
+  UINTN               Index;\r
+  UINT8               *Cert;\r
+  UINTN               CertSize;\r
+  EFI_STATUS          Status;\r
+  EFI_SIGNATURE_LIST  *SignatureList;\r
+  UINTN               SignatureListSize;\r
+\r
+  CertNumber = (UINT8) (*CertBuf);\r
+  CertPtr    = CertBuf + 1;\r
+  for (Index = 0; Index < CertNumber; Index++) {\r
+    CertSize = (UINTN) ReadUnaligned32 ((UINT32 *)CertPtr);\r
+    Cert     = (UINT8 *)CertPtr + sizeof (UINT32);\r
+\r
+    //\r
+    // Record all cert in cert chain to be passed\r
+    //\r
+    Status = CreateSignatureList(Cert, CertSize, &gEfiCertX509Guid, &SignatureList, &SignatureListSize);\r
+    if (!EFI_ERROR(Status)) {\r
+      AddImageExeInfo (Action, ImageName, ImageDevicePath, SignatureList, SignatureListSize);\r
+      FreePool (SignatureList);\r
+    }\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Check whether the timestamp signature is valid and the signing time is also earlier than \r
+  the revocation time.\r
+\r
+  @param[in]  AuthData        Pointer to the Authenticode signature retrieved from signed image.\r
+  @param[in]  AuthDataSize    Size of the Authenticode signature in bytes.\r
+  @param[in]  RevocationTime  The time that the certificate was revoked.\r
+\r
+  @retval TRUE      Timestamp signature is valid and signing time is no later than the \r
+                    revocation time.\r
+  @retval FALSE     Timestamp signature is not valid or the signing time is later than the\r
+                    revocation time.\r
+\r
+**/\r
+BOOLEAN\r
+PassTimestampCheck (\r
+  IN UINT8                  *AuthData,\r
+  IN UINTN                  AuthDataSize,\r
+  IN EFI_TIME               *RevocationTime\r
   )\r
 {\r
   EFI_STATUS                Status;\r
   BOOLEAN                   VerifyStatus;\r
   EFI_SIGNATURE_LIST        *CertList;\r
   EFI_SIGNATURE_DATA        *Cert;\r
-  UINTN                     DataSize;\r
-  UINT8                     *Data;\r
+  UINT8                     *DbtData;\r
+  UINTN                     DbtDataSize;\r
   UINT8                     *RootCert;\r
   UINTN                     RootCertSize;\r
   UINTN                     Index;\r
   UINTN                     CertCount;\r
+  EFI_TIME                  SigningTime;\r
 \r
-  Data         = NULL;\r
-  CertList     = NULL;\r
-  Cert         = NULL;\r
-  RootCert     = NULL;\r
-  RootCertSize = 0;\r
-  VerifyStatus = FALSE;\r
-\r
-  DataSize = 0;\r
-  Status   = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, NULL);\r
-  if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    Data = (UINT8 *) AllocateZeroPool (DataSize);\r
-    if (Data == NULL) {\r
-      return VerifyStatus;\r
-    }\r
+  //\r
+  // Variable Initialization\r
+  //\r
+  VerifyStatus      = FALSE;\r
+  DbtData           = NULL;\r
+  CertList          = NULL;\r
+  Cert              = NULL;\r
+  RootCert          = NULL;\r
+  RootCertSize      = 0;\r
 \r
-    Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, (VOID *) Data);\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
+  //\r
+  // If RevocationTime is zero, the certificate shall be considered to always be revoked.\r
+  //\r
+  if (IsTimeZero (RevocationTime)) {\r
+    return FALSE;\r
+  }\r
 \r
-    //\r
-    // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.\r
-    //\r
-    CertList = (EFI_SIGNATURE_LIST *) Data;\r
-    while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {\r
-      if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
-        Cert          = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
-        CertCount     = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
-        for (Index = 0; Index < CertCount; Index++) {\r
-          //\r
-          // Iterate each Signature Data Node within this CertList for verify.\r
-          //\r
-          RootCert      = Cert->SignatureData;\r
-          RootCertSize  = CertList->SignatureSize - sizeof (EFI_GUID);\r
+  //\r
+  // RevocationTime is non-zero, the certificate should be considered to be revoked from that time and onwards.\r
+  // Using the dbt to get the trusted TSA certificates.\r
+  //\r
+  DbtDataSize = 0;\r
+  Status   = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, NULL);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    goto Done;\r
+  }\r
+  DbtData = (UINT8 *) AllocateZeroPool (DbtDataSize);\r
+  if (DbtData == NULL) {\r
+    goto Done;\r
+  }\r
+  Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, NULL, &DbtDataSize, (VOID *) DbtData);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Done;\r
+  }\r
 \r
+  CertList = (EFI_SIGNATURE_LIST *) DbtData;\r
+  while ((DbtDataSize > 0) && (DbtDataSize >= CertList->SignatureListSize)) {\r
+    if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
+      Cert      = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
+      CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
+      for (Index = 0; Index < CertCount; Index++) {\r
+        //\r
+        // Iterate each Signature Data Node within this CertList for verify.\r
+        //\r
+        RootCert     = Cert->SignatureData;\r
+        RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
+        //\r
+        // Get the signing time if the timestamp signature is valid.\r
+        //\r
+        if (ImageTimestampVerify (AuthData, AuthDataSize, RootCert, RootCertSize, &SigningTime)) {\r
           //\r
-          // Call AuthenticodeVerify library to Verify Authenticode struct.\r
+          // The signer signature is valid only when the signing time is earlier than revocation time.\r
           //\r
-          VerifyStatus = AuthenticodeVerify (\r
-                           AuthData,\r
-                           AuthDataSize,\r
-                           RootCert,\r
-                           RootCertSize,\r
-                           mImageDigest,\r
-                           mImageDigestSize\r
-                           );\r
-          if (VerifyStatus) {\r
-            SecureBootHook (VariableName, VendorGuid, CertList->SignatureSize, Cert);\r
+          if (IsValidSignatureByTimestamp (&SigningTime, RevocationTime)) {\r
+            VerifyStatus = TRUE;\r
             goto Done;\r
           }\r
-          Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
         }\r
+        Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
       }\r
-      DataSize -= CertList->SignatureListSize;\r
-      CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
     }\r
+    DbtDataSize -= CertList->SignatureListSize;\r
+    CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
   }\r
 \r
 Done:\r
-  if (Data != NULL) {\r
-    FreePool (Data);\r
+  if (DbtData != NULL) {\r
+    FreePool (DbtData);\r
   }\r
 \r
   return VerifyStatus;\r
 }\r
 \r
 /**\r
-  Provide verification service for signed images, which include both signature validation\r
-  and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and\r
-  MSFT Authenticode type signatures are supported.\r
-\r
-  In this implementation, only verify external executables when in USER MODE.\r
-  Executables from FV is bypass, so pass in AuthenticationStatus is ignored.\r
-\r
-  The image verification policy is:\r
-    If the image is signed,\r
-      At least one valid signature or at least one hash value of the image must match a record\r
-      in the security database "db", and no valid signature nor any hash value of the image may\r
-      be reflected in the security database "dbx".\r
-    Otherwise, the image is not signed,\r
-      The SHA256 hash value of the image must match a record in the security database "db", and\r
-      not be reflected in the security data base "dbx".\r
-\r
-  Caution: This function may receive untrusted input.\r
-  PE/COFF image is external input, so this function will validate its data structure\r
-  within this image buffer before use.\r
+  Check whether the image signature is forbidden by the forbidden database (dbx).\r
+  The image is forbidden to load if any certificates for signing are revoked before signing time.\r
 \r
-  @param[in]    AuthenticationStatus\r
-                           This is the authentication status returned from the security\r
-                           measurement services for the input file.\r
-  @param[in]    File       This is a pointer to the device path of the file that is\r
-                           being dispatched. This will optionally be used for logging.\r
-  @param[in]    FileBuffer File buffer matches the input file device path.\r
-  @param[in]    FileSize   Size of File buffer matches the input file device path.\r
-  @param[in]    BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
+  @param[in]  AuthData             Pointer to the Authenticode signature retrieved from the signed image.\r
+  @param[in]  AuthDataSize         Size of the Authenticode signature in bytes.\r
+  @param[in]  IsAuditMode          Whether system Secure Boot Mode is in AuditMode.\r
+  @param[in]  ImageName            Name of the image to verify.\r
+  @param[in]  ImageDevicePath      DevicePath of the image to verify.\r
 \r
-  @retval EFI_SUCCESS            The file specified by DevicePath and non-NULL\r
-                                 FileBuffer did authenticate, and the platform policy dictates\r
-                                 that the DXE Foundation may use the file.\r
-  @retval EFI_SUCCESS            The device path specified by NULL device path DevicePath\r
-                                 and non-NULL FileBuffer did authenticate, and the platform\r
-                                 policy dictates that the DXE Foundation may execute the image in\r
-                                 FileBuffer.\r
-  @retval EFI_OUT_RESOURCE       Fail to allocate memory.\r
-  @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
-                                 the platform policy dictates that File should be placed\r
-                                 in the untrusted state. The image has been added to the file\r
-                                 execution table.\r
-  @retval EFI_ACCESS_DENIED      The file specified by File and FileBuffer did not\r
-                                 authenticate, and the platform policy dictates that the DXE\r
-                                 Foundation many not use File.\r
+  @retval TRUE              Image is forbidden by dbx.\r
+  @retval FALSE             Image is not forbidden by dbx.\r
 \r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
-DxeImageVerificationHandler (\r
-  IN  UINT32                           AuthenticationStatus,\r
-  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,\r
-  IN  VOID                             *FileBuffer,\r
-  IN  UINTN                            FileSize,\r
-  IN  BOOLEAN                          BootPolicy\r
+BOOLEAN\r
+IsForbiddenByDbx (\r
+  IN UINT8                          *AuthData,\r
+  IN UINTN                          AuthDataSize,\r
+  IN BOOLEAN                        IsAuditMode,\r
+  IN CHAR16                         *ImageName OPTIONAL,\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS                           Status;\r
-  UINT16                               Magic;\r
+  EFI_STATUS                Status;\r
+  BOOLEAN                   IsForbidden;\r
+  UINT8                     *Data;\r
+  UINTN                     DataSize;\r
+  EFI_SIGNATURE_LIST        *CertList;\r
+  UINTN                     CertListSize;\r
+  EFI_SIGNATURE_DATA        *CertData;\r
+  UINT8                     *RootCert;\r
+  UINTN                     RootCertSize;\r
+  UINTN                     CertCount;\r
+  UINTN                     Index;\r
+  UINT8                     *CertBuffer;\r
+  UINTN                     BufferLength;\r
+  UINT8                     *TrustedCert;\r
+  UINTN                     TrustedCertLength;\r
+  UINT8                     CertNumber;\r
+  UINT8                     *CertPtr;\r
+  UINT8                     *Cert;\r
+  UINTN                     CertSize;\r
+  EFI_TIME                  RevocationTime;\r
+  UINT8                     *SignerCert;\r
+  UINTN                     SignerCertLength;\r
+  UINT8                     *UnchainCert;\r
+  UINTN                     UnchainCertLength;\r
+  //\r
+  // Variable Initialization\r
+  //\r
+  IsForbidden       = FALSE;\r
+  Data              = NULL;\r
+  CertList          = NULL;\r
+  CertData          = NULL;\r
+  RootCert          = NULL;\r
+  RootCertSize      = 0;\r
+  Cert              = NULL;\r
+  CertBuffer        = NULL;\r
+  BufferLength      = 0;\r
+  TrustedCert       = NULL;\r
+  TrustedCertLength = 0;\r
+  SignerCert        = NULL;\r
+  SignerCertLength  = 0;\r
+  UnchainCert       = NULL;\r
+  UnchainCertLength = 0;\r
+\r
+  //\r
+  // The image will not be forbidden if dbx can't be got.\r
+  //\r
+  DataSize = 0;\r
+  Status   = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return IsForbidden;\r
+  }\r
+  Data = (UINT8 *) AllocateZeroPool (DataSize);\r
+  if (Data == NULL) {\r
+    return IsForbidden;\r
+  }\r
+\r
+  Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
+  if (EFI_ERROR (Status)) {\r
+    return IsForbidden;\r
+  }\r
+\r
+  //\r
+  // Verify image signature with RAW X509 certificates in DBX database.\r
+  // If passed, the image will be forbidden.\r
+  //\r
+  CertList     = (EFI_SIGNATURE_LIST *) Data;\r
+  CertListSize = DataSize;\r
+  while ((CertListSize > 0) && (CertListSize >= CertList->SignatureListSize)) {\r
+    if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
+      CertData  = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
+      CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
+\r
+      for (Index = 0; Index < CertCount; Index++) {\r
+        //\r
+        // Iterate each Signature Data Node within this CertList for verify.\r
+        //\r
+        RootCert     = CertData->SignatureData;\r
+        RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
+\r
+        //\r
+        // Call AuthenticodeVerify library to Verify Authenticode struct.\r
+        //\r
+        IsForbidden = AuthenticodeVerify (\r
+                        AuthData,\r
+                        AuthDataSize,\r
+                        RootCert,\r
+                        RootCertSize,\r
+                        mImageDigest,\r
+                        mImageDigestSize\r
+                        );\r
+        if (IsForbidden) {\r
+          SecureBootHook (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, CertData);\r
+          goto Done;\r
+        }\r
+\r
+        CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
+      }\r
+    }\r
+\r
+    CertListSize -= CertList->SignatureListSize;\r
+    CertList      = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
+  }\r
+\r
+  //\r
+  // Check X.509 Certificate Hash & Possible Timestamp.\r
+  //\r
+\r
+  //\r
+  // Retrieve the certificate stack from AuthData\r
+  // The output CertStack format will be:\r
+  //       UINT8  CertNumber;\r
+  //       UINT32 Cert1Length;\r
+  //       UINT8  Cert1[];\r
+  //       UINT32 Cert2Length;\r
+  //       UINT8  Cert2[];\r
+  //       ...\r
+  //       UINT32 CertnLength;\r
+  //       UINT8  Certn[];\r
+  //\r
+  Pkcs7GetSigners (AuthData, AuthDataSize, &CertBuffer, &BufferLength, &TrustedCert, &TrustedCertLength);\r
+  if ((BufferLength == 0) || (CertBuffer == NULL)) {\r
+    IsForbidden = TRUE;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Check if any hash of certificates embedded in AuthData is in the forbidden database.\r
+  //\r
+  CertNumber = (UINT8) (*CertBuffer);\r
+  CertPtr    = CertBuffer + 1;\r
+  for (Index = 0; Index < CertNumber; Index++) {\r
+    CertSize = (UINTN) ReadUnaligned32 ((UINT32 *)CertPtr);\r
+    Cert     = (UINT8 *)CertPtr + sizeof (UINT32);\r
+\r
+    if (IsCertHashFoundInDatabase (Cert, CertSize, (EFI_SIGNATURE_LIST *)Data, DataSize, &RevocationTime)) {\r
+      //\r
+      // Check the timestamp signature and signing time to determine if the image can be trusted.\r
+      //\r
+      IsForbidden = TRUE;\r
+      if (PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime)) {\r
+        IsForbidden = FALSE;\r
+      }\r
+      goto Done;\r
+    }\r
+\r
+    CertPtr = CertPtr + sizeof (UINT32) + CertSize;\r
+  }\r
+\r
+Done:\r
+  if (IsForbidden && IsAuditMode) {\r
+    Pkcs7GetCertificatesList(AuthData, AuthDataSize, &SignerCert, &SignerCertLength, &UnchainCert, &UnchainCertLength);\r
+\r
+    //\r
+    // Record all certs in image to be failed\r
+    //\r
+    if ((SignerCertLength != 0) && (SignerCert != NULL)) {\r
+      RecordCertListToImageExeuctionTable(\r
+        SignerCert,\r
+        SignerCertLength,\r
+        EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+        ImageName,\r
+        ImageDevicePath\r
+        );\r
+    }\r
+\r
+    if ((UnchainCertLength != 0) && (UnchainCert != NULL)) {\r
+      RecordCertListToImageExeuctionTable(\r
+        UnchainCert,\r
+        UnchainCertLength,\r
+        EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+        ImageName,\r
+        ImageDevicePath\r
+        );\r
+    }\r
+  }\r
+\r
+  if (Data != NULL) {\r
+    FreePool (Data);\r
+  }\r
+\r
+  Pkcs7FreeSigners (CertBuffer);\r
+  Pkcs7FreeSigners (TrustedCert);\r
+  Pkcs7FreeSigners (SignerCert);\r
+  Pkcs7FreeSigners (UnchainCert);\r
+\r
+  return IsForbidden;\r
+}\r
+\r
+\r
+/**\r
+  Check whether the image signature can be verified by the trusted certificates in DB database.\r
+\r
+  @param[in]  AuthData              Pointer to the Authenticode signature retrieved from signed image.\r
+  @param[in]  AuthDataSize          Size of the Authenticode signature in bytes.\r
+  @param[in]  IsAuditMode           Whether system Secure Boot Mode is in AuditMode.\r
+  @param[in]  ImageName             Name of the image to verify.\r
+  @param[in]  ImageDevicePath       DevicePath of the image to verify.\r
+\r
+  @retval TRUE         Image passed verification using certificate in db.\r
+  @retval FALSE        Image didn't pass verification using certificate in db.\r
+\r
+**/\r
+BOOLEAN\r
+IsAllowedByDb (\r
+  IN UINT8                          *AuthData,\r
+  IN UINTN                          AuthDataSize,\r
+  IN BOOLEAN                        IsAuditMode,\r
+  IN CHAR16                         *ImageName OPTIONAL,\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  BOOLEAN                   VerifyStatus;\r
+  EFI_SIGNATURE_LIST        *CertList;\r
+  EFI_SIGNATURE_DATA        *CertData;\r
+  UINTN                     DataSize;\r
+  UINT8                     *Data;\r
+  UINT8                     *RootCert;\r
+  UINTN                     RootCertSize;\r
+  UINTN                     Index;\r
+  UINTN                     CertCount;\r
+  UINTN                     DbxDataSize;\r
+  UINT8                     *DbxData;\r
+  EFI_TIME                  RevocationTime;\r
+  UINT8                     *SignerCert;\r
+  UINTN                     SignerCertLength;\r
+  UINT8                     *UnchainCert;\r
+  UINTN                     UnchainCertLength;\r
+\r
+  Data              = NULL;\r
+  CertList          = NULL;\r
+  CertData          = NULL;\r
+  RootCert          = NULL;\r
+  DbxData           = NULL;\r
+  RootCertSize      = 0;\r
+  VerifyStatus      = FALSE;\r
+  SignerCert        = NULL;\r
+  SignerCertLength  = 0;\r
+  UnchainCert       = NULL;\r
+  UnchainCertLength = 0;\r
+\r
+  DataSize = 0;\r
+  Status   = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, NULL);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    Data = (UINT8 *) AllocateZeroPool (DataSize);\r
+    if (Data == NULL) {\r
+      return VerifyStatus;\r
+    }\r
+\r
+    Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, NULL, &DataSize, (VOID *) Data);\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
+\r
+    //\r
+    // Find X509 certificate in Signature List to verify the signature in pkcs7 signed data.\r
+    //\r
+    CertList = (EFI_SIGNATURE_LIST *) Data;\r
+    while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {\r
+      if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
+        CertData  = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
+        CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
+\r
+        for (Index = 0; Index < CertCount; Index++) {\r
+          //\r
+          // Iterate each Signature Data Node within this CertList for verify.\r
+          //\r
+          RootCert     = CertData->SignatureData;\r
+          RootCertSize = CertList->SignatureSize - sizeof (EFI_GUID);\r
+\r
+          //\r
+          // Call AuthenticodeVerify library to Verify Authenticode struct.\r
+          //\r
+          VerifyStatus = AuthenticodeVerify (\r
+                           AuthData,\r
+                           AuthDataSize,\r
+                           RootCert,\r
+                           RootCertSize,\r
+                           mImageDigest,\r
+                           mImageDigestSize\r
+                           );\r
+          if (VerifyStatus) {\r
+            //\r
+            // Here We still need to check if this RootCert's Hash is revoked\r
+            //\r
+            Status   = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, NULL);\r
+            if (Status == EFI_BUFFER_TOO_SMALL) {\r
+              goto Done;\r
+            }\r
+            DbxData = (UINT8 *) AllocateZeroPool (DbxDataSize);\r
+            if (DbxData == NULL) {\r
+              goto Done;\r
+            }\r
+\r
+            Status = gRT->GetVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid, NULL, &DbxDataSize, (VOID *) DbxData);\r
+            if (EFI_ERROR (Status)) {\r
+              goto Done;\r
+            }\r
+\r
+            if (IsCertHashFoundInDatabase (RootCert, RootCertSize, (EFI_SIGNATURE_LIST *)DbxData, DbxDataSize, &RevocationTime)) {\r
+              //\r
+              // Check the timestamp signature and signing time to determine if the image can be trusted.\r
+              //\r
+              VerifyStatus = PassTimestampCheck (AuthData, AuthDataSize, &RevocationTime);\r
+            }\r
+\r
+            goto Done;\r
+          }\r
+\r
+          CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertData + CertList->SignatureSize);\r
+        }\r
+      }\r
+\r
+      DataSize -= CertList->SignatureListSize;\r
+      CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
+    }\r
+  }\r
+\r
+Done:\r
+\r
+  if (VerifyStatus) {\r
+    SecureBootHook (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, CertData);\r
+  }\r
+\r
+  if (IsAuditMode) {\r
+\r
+    Pkcs7GetCertificatesList(AuthData, AuthDataSize, &SignerCert, &SignerCertLength, &UnchainCert, &UnchainCertLength);\r
+    if (VerifyStatus) {\r
+      if ((SignerCertLength != 0) && (SignerCert != NULL)) {\r
+        //\r
+        // Record all cert in signer's cert chain to be passed\r
+        //\r
+        RecordCertListToImageExeuctionTable(\r
+          SignerCert,\r
+          SignerCertLength,\r
+          EFI_IMAGE_EXECUTION_AUTH_SIG_PASSED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+          ImageName,\r
+          ImageDevicePath\r
+          );\r
+      }\r
+\r
+      if ((UnchainCertLength != 0) && (UnchainCert != NULL)) {\r
+        //\r
+        // Record all certs in unchained certificates lists to be failed\r
+        //\r
+        RecordCertListToImageExeuctionTable(\r
+          UnchainCert,\r
+          UnchainCertLength,\r
+          EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+          ImageName,\r
+          ImageDevicePath\r
+          );\r
+      }\r
+    } else {\r
+      //\r
+      // Record all certs in image to be failed\r
+      //\r
+      if ((SignerCertLength != 0) && (SignerCert != NULL)) {\r
+        RecordCertListToImageExeuctionTable(\r
+          SignerCert,\r
+          SignerCertLength,\r
+          EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+          ImageName,\r
+          ImageDevicePath\r
+          );\r
+      }\r
+\r
+      if ((UnchainCertLength != 0) && (UnchainCert != NULL)) {\r
+        RecordCertListToImageExeuctionTable(\r
+          UnchainCert,\r
+          UnchainCertLength,\r
+          EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED,\r
+          ImageName,\r
+          ImageDevicePath\r
+          );\r
+      }\r
+    }\r
+  }\r
+\r
+\r
+  if (Data != NULL) {\r
+    FreePool (Data);\r
+  }\r
+  if (DbxData != NULL) {\r
+    FreePool (DbxData);\r
+  }\r
+\r
+  Pkcs7FreeSigners (SignerCert);\r
+  Pkcs7FreeSigners (UnchainCert);\r
+\r
+  return VerifyStatus;\r
+}\r
+\r
+/**\r
+  Provide verification service for signed images in AuditMode, which include both signature validation\r
+  and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and\r
+  MSFT Authenticode type signatures are supported. \r
+\r
+  In this implementation, only verify external executables when in AuditMode.\r
+  Executables from FV is bypass, so pass in AuthenticationStatus is ignored. Other authentication status\r
+  are record into IMAGE_EXECUTION_TABLE.\r
+\r
+  The image verification policy is:\r
+    If the image is signed,\r
+      At least one valid signature or at least one hash value of the image must match a record\r
+      in the security database "db", and no valid signature nor any hash value of the image may\r
+      be reflected in the security database "dbx".\r
+    Otherwise, the image is not signed,\r
+      The SHA256 hash value of the image must match a record in the security database "db", and\r
+      not be reflected in the security data base "dbx".\r
+\r
+  Caution: This function may receive untrusted input.\r
+  PE/COFF image is external input, so this function will validate its data structure\r
+  within this image buffer before use.\r
+\r
+  @param[in]    AuthenticationStatus\r
+                           This is the authentication status returned from the security\r
+                           measurement services for the input file.\r
+  @param[in]    File       This is a pointer to the device path of the file that is\r
+                           being dispatched. This will optionally be used for logging.\r
+  @param[in]    FileBuffer File buffer matches the input file device path.\r
+  @param[in]    FileSize   Size of File buffer matches the input file device path.\r
+  @param[in]    BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
+\r
+  @retval EFI_SUCCESS            The authenticate info is sucessfully stored for the file \r
+                                 specified by DevicePath and non-NULL FileBuffer \r
+  @retval EFI_ACCESS_DENIED      The file specified by File and FileBuffer did not\r
+                                 authenticate, and the platform policy dictates that the DXE\r
+                                 Foundation many not use File.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ImageVerificationInAuditMode (\r
+  IN  UINT32                           AuthenticationStatus,\r
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,\r
+  IN  VOID                             *FileBuffer,\r
+  IN  UINTN                            FileSize,\r
+  IN  BOOLEAN                          BootPolicy\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT16                               Magic;\r
+  EFI_IMAGE_DOS_HEADER                 *DosHdr;\r
+  EFI_SIGNATURE_LIST                   *SignatureList;\r
+  EFI_IMAGE_EXECUTION_ACTION           Action;\r
+  WIN_CERTIFICATE                      *WinCertificate;\r
+  UINT32                               Policy;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT         ImageContext;\r
+  UINT32                               NumberOfRvaAndSizes;\r
+  WIN_CERTIFICATE_EFI_PKCS             *PkcsCertData;\r
+  WIN_CERTIFICATE_UEFI_GUID            *WinCertUefiGuid;\r
+  UINT8                                *AuthData;\r
+  UINTN                                AuthDataSize;\r
+  EFI_IMAGE_DATA_DIRECTORY             *SecDataDir;\r
+  UINT32                               OffSet;\r
+  CHAR16                               *FilePathStr;\r
+  UINTN                                SignatureListSize;\r
+\r
+  SignatureList     = NULL;\r
+  WinCertificate    = NULL;\r
+  SecDataDir        = NULL;\r
+  PkcsCertData      = NULL;\r
+  FilePathStr       = NULL;\r
+  Action            = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED;\r
+  Status            = EFI_ACCESS_DENIED;\r
+\r
+\r
+  //\r
+  // Check the image type and get policy setting.\r
+  //\r
+  switch (GetImageType (File)) {\r
+\r
+  case IMAGE_FROM_FV:\r
+    Policy = ALWAYS_EXECUTE;\r
+    break;\r
+\r
+  case IMAGE_FROM_OPTION_ROM:\r
+    Policy = PcdGet32 (PcdOptionRomImageVerificationPolicy);\r
+    break;\r
+\r
+  case IMAGE_FROM_REMOVABLE_MEDIA:\r
+    Policy = PcdGet32 (PcdRemovableMediaImageVerificationPolicy);\r
+    break;\r
+\r
+  case IMAGE_FROM_FIXED_MEDIA:\r
+    Policy = PcdGet32 (PcdFixedMediaImageVerificationPolicy);\r
+    break;\r
+\r
+  default:\r
+    Policy = DENY_EXECUTE_ON_SECURITY_VIOLATION;\r
+    break;\r
+  }\r
+\r
+  //\r
+  // If policy is always/never execute, return directly.\r
+  //\r
+  if (Policy == ALWAYS_EXECUTE) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Get Image Device Path Str\r
+  //\r
+  FilePathStr = ConvertDevicePathToText (File, FALSE, TRUE);\r
+\r
+  //\r
+  // Authentication failed because of (unspecified) firmware security policy\r
+  //\r
+  if (Policy == NEVER_EXECUTE) {\r
+    //\r
+    // No signature, record FilePath/FilePathStr only\r
+    //\r
+    AddImageExeInfo (EFI_IMAGE_EXECUTION_POLICY_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED, FilePathStr, File, NULL, 0);\r
+    goto END;\r
+  }\r
+\r
+  //\r
+  // The policy QUERY_USER_ON_SECURITY_VIOLATION and ALLOW_EXECUTE_ON_SECURITY_VIOLATION\r
+  // violates the UEFI spec and has been removed.\r
+  //\r
+  ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION && Policy != ALLOW_EXECUTE_ON_SECURITY_VIOLATION);\r
+  if (Policy == QUERY_USER_ON_SECURITY_VIOLATION || Policy == ALLOW_EXECUTE_ON_SECURITY_VIOLATION) {\r
+    CpuDeadLoop ();\r
+  }\r
+\r
+  //\r
+  // Read the Dos header.\r
+  //\r
+  if (FileBuffer == NULL) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto END;\r
+  }\r
+\r
+  mImageBase  = (UINT8 *) FileBuffer;\r
+  mImageSize  = FileSize;\r
+\r
+  ZeroMem (&ImageContext, sizeof (ImageContext));\r
+  ImageContext.Handle    = (VOID *) FileBuffer;\r
+  ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeImageVerificationLibImageRead;\r
+\r
+  //\r
+  // Get information about the image being loaded\r
+  //\r
+  Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The information can't be got from the invalid PeImage\r
+    //\r
+    goto END;\r
+  }\r
+\r
+\r
+  DosHdr = (EFI_IMAGE_DOS_HEADER *) mImageBase;\r
+  if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
+    //\r
+    // DOS image header is present,\r
+    // so read the PE header after the DOS image header.\r
+    //\r
+    mPeCoffHeaderOffset = DosHdr->e_lfanew;\r
+  } else {\r
+    mPeCoffHeaderOffset = 0;\r
+  }\r
+\r
+  //\r
+  // Check PE/COFF image.\r
+  //\r
+  mNtHeader.Pe32 = (EFI_IMAGE_NT_HEADERS32 *) (mImageBase + mPeCoffHeaderOffset);\r
+  if (mNtHeader.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
+    //\r
+    // It is not a valid Pe/Coff file.\r
+    //\r
+    Status = EFI_ACCESS_DENIED;\r
+    goto END;\r
+  }\r
+\r
+  if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    //\r
+    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
+    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
+    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
+    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
+    //\r
+    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
+  } else {\r
+    //\r
+    // Get the magic value from the PE/COFF Optional Header\r
+    //\r
+    Magic = mNtHeader.Pe32->OptionalHeader.Magic;\r
+  }\r
+\r
+  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    //\r
+    // Use PE32 offset.\r
+    //\r
+    NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
+    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
+      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
+    }\r
+  } else {\r
+    //\r
+    // Use PE32+ offset.\r
+    //\r
+    NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
+    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
+      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
+    }\r
+  }\r
+\r
+  //\r
+  // Start Image Validation.\r
+  //\r
+  if (SecDataDir == NULL || SecDataDir->Size == 0) {\r
+    //\r
+    // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db",\r
+    // and not be reflected in the security data base "dbx".\r
+    //\r
+    if (!HashPeImage (HASHALG_SHA256)) {\r
+      Status = EFI_ACCESS_DENIED;\r
+      goto END;\r
+    }\r
+\r
+    //\r
+    // Image Hash is in forbidden database (DBX).\r
+    //\r
+    if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
+      //\r
+      // Image Hash is in allowed database (DB).\r
+      //\r
+      if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
+        Action = EFI_IMAGE_EXECUTION_AUTH_SIG_PASSED | EFI_IMAGE_EXECUTION_INITIALIZED;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Add HASH digest for image without signature\r
+    //\r
+    Status = CreateSignatureList(mImageDigest, mImageDigestSize, &mCertType, &SignatureList, &SignatureListSize);\r
+    if (!EFI_ERROR(Status)) {\r
+      AddImageExeInfo (Action, FilePathStr, File, SignatureList, SignatureListSize);\r
+      FreePool (SignatureList);\r
+    }\r
+    goto END;\r
+  }\r
+\r
+  //\r
+  // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7\r
+  // "Attribute Certificate Table".\r
+  // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.\r
+  //\r
+  for (OffSet = SecDataDir->VirtualAddress;\r
+       OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);\r
+       OffSet += (WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {\r
+    WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);\r
+    if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||\r
+        (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.\r
+    //\r
+    if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {\r
+      //\r
+      // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the\r
+      // Authenticode specification.\r
+      //\r
+      PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;\r
+      if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {\r
+        break;\r
+      }\r
+      AuthData   = PkcsCertData->CertData;\r
+      AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);\r
+    } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {\r
+      //\r
+      // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.\r
+      //\r
+      WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;\r
+      if (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
+        break;\r
+      }\r
+      if (!CompareGuid (&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid)) {\r
+        continue;\r
+      }\r
+      AuthData = WinCertUefiGuid->CertData;\r
+      AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);\r
+    } else {\r
+      if (WinCertificate->dwLength < sizeof (WIN_CERTIFICATE)) {\r
+        break;\r
+      }\r
+      continue;\r
+    }\r
+\r
+    Status = HashPeImageByType (AuthData, AuthDataSize);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED | EFI_IMAGE_EXECUTION_INITIALIZED;\r
+\r
+    //\r
+    // Check the digital signature against the revoked certificate in forbidden database (dbx).\r
+    // Check the digital signature against the valid certificate in allowed database (db).\r
+    //\r
+    if (!IsForbiddenByDbx (AuthData, AuthDataSize, TRUE, FilePathStr, File)) {\r
+      IsAllowedByDb (AuthData, AuthDataSize, TRUE, FilePathStr, File);\r
+    }\r
+\r
+    //\r
+    // Check the image's hash value.\r
+    //\r
+    if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
+      if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
+        Action = EFI_IMAGE_EXECUTION_AUTH_SIG_PASSED | EFI_IMAGE_EXECUTION_INITIALIZED; \r
+      }\r
+    }\r
+\r
+    //\r
+    // Add HASH digest for image with signature\r
+    //\r
+    Status = CreateSignatureList(mImageDigest, mImageDigestSize, &mCertType, &SignatureList, &SignatureListSize);\r
+\r
+    if (!EFI_ERROR(Status)) {\r
+      AddImageExeInfo (Action, FilePathStr, File, SignatureList, SignatureListSize);\r
+      FreePool (SignatureList);\r
+    } else {\r
+      goto END;\r
+    }\r
+  }\r
+\r
+\r
+  if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {\r
+    //\r
+    // The Size in Certificate Table or the attribute certicate table is corrupted.\r
+    //\r
+    Status = EFI_ACCESS_DENIED;\r
+  } else {\r
+    Status = EFI_SUCCESS;\r
+  }\r
+\r
+END:\r
+\r
+  if (FilePathStr != NULL) {\r
+    FreePool(FilePathStr);\r
+    FilePathStr = NULL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Provide verification service for signed images, which include both signature validation\r
+  and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and\r
+  MSFT Authenticode type signatures are supported.\r
+\r
+  In this implementation, only verify external executables when in USER MODE.\r
+  Executables from FV is bypass, so pass in AuthenticationStatus is ignored.\r
+\r
+  The image verification policy is:\r
+    If the image is signed,\r
+      At least one valid signature or at least one hash value of the image must match a record\r
+      in the security database "db", and no valid signature nor any hash value of the image may\r
+      be reflected in the security database "dbx".\r
+    Otherwise, the image is not signed,\r
+      The SHA256 hash value of the image must match a record in the security database "db", and\r
+      not be reflected in the security data base "dbx".\r
+\r
+  Caution: This function may receive untrusted input.\r
+  PE/COFF image is external input, so this function will validate its data structure\r
+  within this image buffer before use.\r
+\r
+  @param[in]    AuthenticationStatus\r
+                           This is the authentication status returned from the security\r
+                           measurement services for the input file.\r
+  @param[in]    File       This is a pointer to the device path of the file that is\r
+                           being dispatched. This will optionally be used for logging.\r
+  @param[in]    FileBuffer File buffer matches the input file device path.\r
+  @param[in]    FileSize   Size of File buffer matches the input file device path.\r
+  @param[in]    BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
+\r
+  @retval EFI_SUCCESS            The file specified by DevicePath and non-NULL\r
+                                 FileBuffer did authenticate, and the platform policy dictates\r
+                                 that the DXE Foundation may use the file.\r
+  @retval EFI_SUCCESS            The device path specified by NULL device path DevicePath\r
+                                 and non-NULL FileBuffer did authenticate, and the platform\r
+                                 policy dictates that the DXE Foundation may execute the image in\r
+                                 FileBuffer.\r
+  @retval EFI_OUT_RESOURCE       Fail to allocate memory.\r
+  @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
+                                 the platform policy dictates that File should be placed\r
+                                 in the untrusted state. The image has been added to the file\r
+                                 execution table.\r
+  @retval EFI_ACCESS_DENIED      The file specified by File and FileBuffer did not\r
+                                 authenticate, and the platform policy dictates that the DXE\r
+                                 Foundation many not use File.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DxeImageVerificationHandler (\r
+  IN  UINT32                           AuthenticationStatus,\r
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,\r
+  IN  VOID                             *FileBuffer,\r
+  IN  UINTN                            FileSize,\r
+  IN  BOOLEAN                          BootPolicy\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT16                               Magic;\r
   EFI_IMAGE_DOS_HEADER                 *DosHdr;\r
   EFI_STATUS                           VerifyStatus;\r
   EFI_SIGNATURE_LIST                   *SignatureList;\r
@@ -1054,7 +2141,9 @@ DxeImageVerificationHandler (
   EFI_IMAGE_EXECUTION_ACTION           Action;\r
   WIN_CERTIFICATE                      *WinCertificate;\r
   UINT32                               Policy;\r
-  UINT8                                *SecureBoot;\r
+  UINT8                                *VarData;\r
+  UINT8                                SecureBoot;\r
+  UINT8                                AuditMode;\r
   PE_COFF_LOADER_IMAGE_CONTEXT         ImageContext;\r
   UINT32                               NumberOfRvaAndSizes;\r
   WIN_CERTIFICATE_EFI_PKCS             *PkcsCertData;\r
@@ -1063,6 +2152,7 @@ DxeImageVerificationHandler (
   UINTN                                AuthDataSize;\r
   EFI_IMAGE_DATA_DIRECTORY             *SecDataDir;\r
   UINT32                               OffSet;\r
+  CHAR16                               *NameStr;\r
 \r
   SignatureList     = NULL;\r
   SignatureListSize = 0;\r
@@ -1073,6 +2163,20 @@ DxeImageVerificationHandler (
   Status            = EFI_ACCESS_DENIED;\r
   VerifyStatus      = EFI_ACCESS_DENIED;\r
 \r
+  GetEfiGlobalVariable2 (EFI_AUDIT_MODE_NAME, (VOID**)&VarData, NULL);\r
+  //\r
+  // Skip verification if AuditMode variable doesn't exist. AuditMode should always exist\r
+  //\r
+  if (VarData == NULL) {\r
+    return EFI_SUCCESS;\r
+  }\r
+  AuditMode = *VarData;\r
+  FreePool(VarData);\r
+\r
+  if (AuditMode == AUDIT_MODE_ENABLE) {\r
+    return ImageVerificationInAuditMode(AuthenticationStatus, File, FileBuffer, FileSize, BootPolicy);\r
+  }\r
+\r
   //\r
   // Check the image type and get policy setting.\r
   //\r
@@ -1108,29 +2212,30 @@ DxeImageVerificationHandler (
   }\r
 \r
   //\r
-  // The policy QUERY_USER_ON_SECURITY_VIOLATION violates the UEFI spec and has been removed.\r
+  // The policy QUERY_USER_ON_SECURITY_VIOLATION and ALLOW_EXECUTE_ON_SECURITY_VIOLATION\r
+  // violates the UEFI spec and has been removed.\r
   //\r
-  ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION);\r
-  if (Policy == QUERY_USER_ON_SECURITY_VIOLATION) {\r
+  ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION && Policy != ALLOW_EXECUTE_ON_SECURITY_VIOLATION);\r
+  if (Policy == QUERY_USER_ON_SECURITY_VIOLATION || Policy == ALLOW_EXECUTE_ON_SECURITY_VIOLATION) {\r
     CpuDeadLoop ();\r
   }\r
 \r
-  GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);\r
+  GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&VarData, NULL);\r
   //\r
   // Skip verification if SecureBoot variable doesn't exist.\r
   //\r
-  if (SecureBoot == NULL) {\r
+  if (VarData == NULL) {\r
     return EFI_SUCCESS;\r
   }\r
+  SecureBoot = *VarData;\r
+  FreePool(VarData);\r
 \r
   //\r
-  // Skip verification if SecureBoot is disabled.\r
+  // Skip verification if SecureBoot is disabled but not AuditMode\r
   //\r
-  if (*SecureBoot == SECURE_BOOT_MODE_DISABLE) {\r
-    FreePool (SecureBoot);\r
+  if (SecureBoot == SECURE_BOOT_MODE_DISABLE) {\r
     return EFI_SUCCESS;\r
   }\r
-  FreePool (SecureBoot);\r
 \r
   //\r
   // Read the Dos header.\r
@@ -1182,8 +2287,8 @@ DxeImageVerificationHandler (
 \r
   if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     //\r
-    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r
-    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the \r
+    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
+    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
     //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
     //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
     //\r
@@ -1194,7 +2299,7 @@ DxeImageVerificationHandler (
     //\r
     Magic = mNtHeader.Pe32->OptionalHeader.Magic;\r
   }\r
-  \r
+\r
   if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     //\r
     // Use PE32 offset.\r
@@ -1202,7 +2307,7 @@ DxeImageVerificationHandler (
     NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
     if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
       SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];\r
-    }        \r
+    }\r
   } else {\r
     //\r
     // Use PE32+ offset.\r
@@ -1218,7 +2323,7 @@ DxeImageVerificationHandler (
   //\r
   if (SecDataDir == NULL || SecDataDir->Size == 0) {\r
     //\r
-    // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db", \r
+    // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db",\r
     // and not be reflected in the security data base "dbx".\r
     //\r
     if (!HashPeImage (HASHALG_SHA256)) {\r
@@ -1246,25 +2351,25 @@ DxeImageVerificationHandler (
   }\r
 \r
   //\r
-  // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7 \r
+  // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7\r
   // "Attribute Certificate Table".\r
   // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.\r
   //\r
   for (OffSet = SecDataDir->VirtualAddress;\r
        OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);\r
-       OffSet += WinCertificate->dwLength, OffSet += ALIGN_SIZE (OffSet)) {\r
+       OffSet += (WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {\r
     WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);\r
     if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||\r
         (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {\r
       break;\r
     }\r
-    \r
+\r
     //\r
     // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.\r
     //\r
     if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {\r
       //\r
-      // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the \r
+      // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the\r
       // Authenticode specification.\r
       //\r
       PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;\r
@@ -1297,11 +2402,11 @@ DxeImageVerificationHandler (
     if (EFI_ERROR (Status)) {\r
       continue;\r
     }\r
-    \r
+\r
     //\r
     // Check the digital signature against the revoked certificate in forbidden database (dbx).\r
     //\r
-    if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {\r
+    if (IsForbiddenByDbx (AuthData, AuthDataSize, FALSE, NULL, NULL)) {\r
       Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;\r
       VerifyStatus = EFI_ACCESS_DENIED;\r
       break;\r
@@ -1311,7 +2416,7 @@ DxeImageVerificationHandler (
     // Check the digital signature against the valid certificate in allowed database (db).\r
     //\r
     if (EFI_ERROR (VerifyStatus)) {\r
-      if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {\r
+      if (IsAllowedByDb (AuthData, AuthDataSize, FALSE, NULL, NULL)) {\r
         VerifyStatus = EFI_SUCCESS;\r
       }\r
     }\r
@@ -1336,7 +2441,7 @@ DxeImageVerificationHandler (
     //\r
     VerifyStatus = EFI_ACCESS_DENIED;\r
   }\r
-  \r
+\r
   if (!EFI_ERROR (VerifyStatus)) {\r
     return EFI_SUCCESS;\r
   } else {\r
@@ -1353,7 +2458,7 @@ DxeImageVerificationHandler (
       }\r
       SignatureList->SignatureHeaderSize  = 0;\r
       SignatureList->SignatureListSize    = (UINT32) SignatureListSize;\r
-      SignatureList->SignatureSize        = (UINT32) mImageDigestSize;\r
+      SignatureList->SignatureSize        = (UINT32) (sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize);\r
       CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));\r
       Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));\r
       CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);\r
@@ -1365,7 +2470,12 @@ Done:
     //\r
     // Policy decides to defer or reject the image; add its information in image executable information table.\r
     //\r
-    AddImageExeInfo (Action, NULL, File, SignatureList, SignatureListSize);\r
+    NameStr = ConvertDevicePathToText (File, FALSE, TRUE);\r
+    AddImageExeInfo (Action, NameStr, File, SignatureList, SignatureListSize);\r
+    if (NameStr != NULL) {\r
+      DEBUG((EFI_D_INFO, "The image doesn't pass verification: %s\n", NameStr));\r
+      FreePool(NameStr);\r
+    }\r
     Status = EFI_SECURITY_VIOLATION;\r
   }\r
 \r
@@ -1376,6 +2486,41 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+  On Ready To Boot Services Event notification handler.\r
+\r
+  Add the image execution information table if it is not in system configuration table.\r
+\r
+  @param[in]  Event     Event whose notification function is being invoked\r
+  @param[in]  Context   Pointer to the notification function's context\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+OnReadyToBoot (\r
+  IN      EFI_EVENT               Event,\r
+  IN      VOID                    *Context\r
+  )\r
+{\r
+  EFI_IMAGE_EXECUTION_INFO_TABLE  *ImageExeInfoTable;\r
+  UINTN                           ImageExeInfoTableSize;\r
+\r
+  EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);\r
+  if (ImageExeInfoTable != NULL) {\r
+    return;\r
+  }\r
+\r
+  ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);\r
+  ImageExeInfoTable     = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize);\r
+  if (ImageExeInfoTable == NULL) {\r
+    return ;\r
+  }\r
+\r
+  ImageExeInfoTable->NumberOfImages = 0;\r
+  gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) ImageExeInfoTable);\r
+\r
+}\r
+\r
 /**\r
   Register security measurement handler.\r
 \r
@@ -1391,6 +2536,18 @@ DxeImageVerificationLibConstructor (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
+  EFI_EVENT            Event;\r
+\r
+  //\r
+  // Register the event to publish the image execution table.\r
+  //\r
+  EfiCreateEventReadyToBootEx (\r
+    TPL_CALLBACK,\r
+    OnReadyToBoot,\r
+    NULL,\r
+    &Event\r
+    );\r
+\r
   return RegisterSecurity2Handler (\r
           DxeImageVerificationHandler,\r
           EFI_AUTH_OPERATION_VERIFY_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r