]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Update the DxeImageVerificationLib to support for Authenticode-signed UEFI images...
authorsfu5 <sfu5@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 21 Feb 2013 05:00:21 +0000 (05:00 +0000)
committersfu5 <sfu5@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 21 Feb 2013 05:00:21 +0000 (05:00 +0000)
Signed-off-by: Fu Siyuan <siyuan.fu@intel.com>
Reviewed-by: Ye Ting <ting.ye@intel.com>
Reviewed-by: Dong Guo <guo.dong@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14141 6f19259b-4bc3-4df7-8a09-765794883524

SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.h

index 3b4fc57cb2ed6e1c72ff95894f422831834f1ba5..2e522c9330ee5ec63001cfbe38b27659f7721e0e 100644 (file)
@@ -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 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2013, 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
@@ -966,43 +966,6 @@ Done:
   return VerifyStatus;\r
 }\r
 \r
-/**\r
-  Verify certificate in WIN_CERT_TYPE_PKCS_SIGNED_DATA format.\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
-\r
-  @retval EFI_SUCCESS                 Image pass verification.\r
-  @retval EFI_SECURITY_VIOLATION      Image fail verification.\r
-\r
-**/\r
-EFI_STATUS\r
-VerifyCertPkcsSignedData (\r
-  IN UINT8              *AuthData,\r
-  IN UINTN              AuthDataSize\r
-  )\r
-{\r
-  //\r
-  // 1: Find certificate from DBX forbidden database for revoked certificate.\r
-  //\r
-  if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {\r
-    //\r
-    // DBX is forbidden database, if Authenticode verification pass with\r
-    // one of the certificate in DBX, this image should be rejected.\r
-    //\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-  //\r
-  // 2: Find certificate from DB database and try to verify authenticode struct.\r
-  //\r
-  if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\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
@@ -1011,25 +974,14 @@ VerifyCertPkcsSignedData (
   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 process is:\r
+  The image verification policy is:\r
     If the image is signed,\r
-      If the image's certificate verifies against a certificate (root or intermediate) in the allowed \r
-      database (DB) and not in the forbidden database (DBX), the certificate verification is passed.\r
-        If the image's hash digest is in DBX,\r
-          deny execution.\r
-        If not,\r
-          run it.\r
-      If the Image's certificate verification failed.\r
-        If the Image's Hash is in DB and not in DBX,\r
-          run it.\r
-        Otherwise,\r
-          deny execution.\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
-      Is the Image's Hash in DBX?\r
-        If yes, deny execution.\r
-        If not, is the Image's Hash in DB?\r
-          If yes, run it.\r
-          If not, deny execution.\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
@@ -1084,12 +1036,12 @@ DxeImageVerificationHandler (
   UINT8                                *SecureBoot;\r
   PE_COFF_LOADER_IMAGE_CONTEXT         ImageContext;\r
   UINT32                               NumberOfRvaAndSizes;\r
-  UINT32                               CertSize;\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
 \r
   SignatureList     = NULL;\r
   SignatureListSize = 0;\r
@@ -1098,6 +1050,8 @@ DxeImageVerificationHandler (
   PkcsCertData      = NULL;\r
   Action            = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;\r
   Status            = EFI_ACCESS_DENIED;\r
+  VerifyStatus      = EFI_ACCESS_DENIED;\r
+\r
   //\r
   // Check the image type and get policy setting.\r
   //\r
@@ -1230,9 +1184,13 @@ DxeImageVerificationHandler (
     }\r
   }\r
 \r
-  if ((SecDataDir == NULL) || ((SecDataDir != NULL) && (SecDataDir->Size == 0))) {\r
+  //\r
+  // Start Image Validation.\r
+  //\r
+  if (SecDataDir == NULL || SecDataDir->Size == 0) {\r
     //\r
-    // This image is not signed.\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
       goto Done;\r
@@ -1259,109 +1217,118 @@ DxeImageVerificationHandler (
   }\r
 \r
   //\r
-  // Verify signature of executables.\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
-  WinCertificate = (WIN_CERTIFICATE *) (mImageBase + SecDataDir->VirtualAddress);\r
-\r
-  CertSize = sizeof (WIN_CERTIFICATE);\r
-\r
-  if ((SecDataDir->Size <= CertSize) || (SecDataDir->Size < WinCertificate->dwLength)) {\r
-    goto Done;\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
+  for (OffSet = SecDataDir->VirtualAddress;\r
+       OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);\r
+       OffSet += WinCertificate->dwLength, OffSet += ALIGN_SIZE (OffSet)) {\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
-    // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the \r
-    // Authenticode specification.\r
+    // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.\r
     //\r
-    PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;\r
-    if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {\r
-      goto Done;\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
-    AuthData   = PkcsCertData->CertData;\r
-    AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);\r
-    \r
+\r
     Status = HashPeImageByType (AuthData, AuthDataSize);\r
     if (EFI_ERROR (Status)) {\r
-      goto Done;\r
+      continue;\r
     }\r
-\r
-    VerifyStatus = VerifyCertPkcsSignedData (AuthData, AuthDataSize);\r
-  } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {\r
+    \r
     //\r
-    // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.\r
+    // Check the digital signature against the revoked certificate in forbidden database (dbx).\r
     //\r
-    WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;\r
-    if (!CompareGuid(&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid) ||\r
-        (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData))) {\r
-      goto Done;\r
-    }\r
-    AuthData = WinCertUefiGuid->CertData;\r
-    AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);\r
-    \r
-    Status = HashPeImageByType (AuthData, AuthDataSize);\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
+    if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {\r
+      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;\r
+      VerifyStatus = EFI_ACCESS_DENIED;\r
+      break;\r
     }\r
-    VerifyStatus = VerifyCertPkcsSignedData (AuthData, AuthDataSize);\r
-  } else {\r
-    goto Done;\r
-  }\r
 \r
-  if (!EFI_ERROR (VerifyStatus)) {\r
     //\r
-    // Verification is passed.\r
-    // Continue to check the image digest in signature database.\r
+    // Check the digital signature against the valid certificate in allowed database (db).\r
     //\r
-    if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
-      //\r
-      // Executable signature verification passes, but is found in forbidden signature database.\r
-      //\r
-      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;\r
-      Status = EFI_ACCESS_DENIED;\r
-    } else {\r
-      //\r
-      // For image verification against enrolled X.509 certificate(root or intermediate),\r
-      // no need to check image's hash in the allowed database.\r
-      //\r
-      return EFI_SUCCESS;\r
+    if (EFI_ERROR (VerifyStatus)) {\r
+      if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {\r
+        VerifyStatus = EFI_SUCCESS;\r
+      }\r
     }\r
-  } else {\r
+\r
     //\r
-    // Verification failure.\r
+    // Check the image's hash value.\r
     //\r
-    if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize) &&\r
-        IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
-      //\r
-      // Verification fail, Image Hash is not in forbidden database (DBX),\r
-      // and Image Hash is in allowed database (DB).\r
-      //\r
-      Status = EFI_SUCCESS;\r
-    } else {\r
-      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;\r
-      Status = EFI_ACCESS_DENIED;\r
+    if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {\r
+      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;\r
+      VerifyStatus = EFI_ACCESS_DENIED;\r
+      break;\r
+    } else if (EFI_ERROR (VerifyStatus)) {\r
+      if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {\r
+        VerifyStatus = EFI_SUCCESS;\r
+      }\r
     }\r
   }\r
 \r
-  if (EFI_ERROR (Status)) {\r
+  if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {\r
     //\r
-    // Get image hash value as executable's signature.\r
+    // The Size in Certificate Table or the attribute certicate table is corrupted.\r
     //\r
-    SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;\r
-    SignatureList     = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);\r
-    if (SignatureList == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto Done;\r
+    VerifyStatus = EFI_ACCESS_DENIED;\r
+  }\r
+  \r
+  if (!EFI_ERROR (VerifyStatus)) {\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    Status = EFI_ACCESS_DENIED;\r
+    if (Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED || Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {\r
+      //\r
+      // Get image hash value as executable's signature.\r
+      //\r
+      SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;\r
+      SignatureList     = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);\r
+      if (SignatureList == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
+      }\r
+      SignatureList->SignatureHeaderSize  = 0;\r
+      SignatureList->SignatureListSize    = (UINT32) SignatureListSize;\r
+      SignatureList->SignatureSize        = (UINT32) 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
     }\r
-    SignatureList->SignatureHeaderSize  = 0;\r
-    SignatureList->SignatureListSize    = (UINT32) SignatureListSize;\r
-    SignatureList->SignatureSize        = (UINT32) 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
   }\r
 \r
 Done:\r
index 55371e90bfc1773521ddb27d8cf31c5f6f1e5280..9aef601cee46115cf439b379ce35b761436fe658 100644 (file)
@@ -2,7 +2,7 @@
   The internal header file includes the common header files, defines\r
   internal structure and functions used by ImageVerificationLib.\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2013, 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
@@ -43,6 +43,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define MAX_NOTIFY_STRING_LEN             64\r
 #define TWO_BYTE_ENCODE                   0x82\r
 \r
+#define ALIGNMENT_SIZE                    8\r
+#define ALIGN_SIZE(a) (((a) % ALIGNMENT_SIZE) ? ALIGNMENT_SIZE - ((a) % ALIGNMENT_SIZE) : 0)\r
+\r
 //\r
 // Image type definitions\r
 //\r