]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c
UEFI 2.4 X509 Certificate Hash and RFC3161 Timestamp Verification support for Secure...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / AuthService.c
index 566c5e29a7866cc4248f01fc56df13fed3e08816..3f0698e606eb8a81b4962b8d960b4c2313910a7b 100644 (file)
@@ -9,7 +9,7 @@
   Variable attribute should also be checked to avoid authentication bypass.\r
      The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
   which is assumed to be protected by platform.  All variable code and metadata in flash/SMM Memory\r
-  may not be modified without authorization. If platform fails to protect these resources, \r
+  may not be modified without authorization. If platform fails to protect these resources,\r
   the authentication service provided in this driver will be broken, and the behavior is undefined.\r
 \r
   ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do\r
@@ -77,7 +77,10 @@ EFI_SIGNATURE_ITEM mSupportSigItem[] = {
   {EFI_CERT_X509_GUID,            0,               ((UINT32) ~0)},\r
   {EFI_CERT_SHA224_GUID,          0,               28           },\r
   {EFI_CERT_SHA384_GUID,          0,               48           },\r
-  {EFI_CERT_SHA512_GUID,          0,               64           }\r
+  {EFI_CERT_SHA512_GUID,          0,               64           },\r
+  {EFI_CERT_X509_SHA256_GUID,     0,               48           },\r
+  {EFI_CERT_X509_SHA384_GUID,     0,               64           },\r
+  {EFI_CERT_X509_SHA512_GUID,     0,               80           }\r
 };\r
 \r
 /**\r
@@ -88,7 +91,7 @@ EFI_SIGNATURE_ITEM mSupportSigItem[] = {
 \r
   @retval TRUE      This variable is protected, only a physical present user could set this variable.\r
   @retval FALSE     This variable is not protected.\r
-  \r
+\r
 **/\r
 BOOLEAN\r
 NeedPhysicallyPresent(\r
@@ -100,7 +103,7 @@ NeedPhysicallyPresent(
     || (CompareGuid (VendorGuid, &gEfiCustomModeEnableGuid) && (StrCmp (VariableName, EFI_CUSTOM_MODE_NAME) == 0))) {\r
     return TRUE;\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
 \r
@@ -122,7 +125,7 @@ InCustomMode (
   if (Variable.CurrPtr != NULL && *(GetVariableDataPtr (Variable.CurrPtr)) == CUSTOM_SECURE_BOOT_MODE) {\r
     return TRUE;\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
 \r
@@ -256,7 +259,7 @@ AutenticatedVariableServiceInitialize (
     Data      = GetVariableDataPtr (Variable.CurrPtr);\r
     ASSERT ((DataSize != 0) && (Data != NULL));\r
     //\r
-    // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before) \r
+    // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)\r
     //  Therefore, there is no memory overflow in underlying CopyMem.\r
     //\r
     CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
@@ -269,7 +272,7 @@ AutenticatedVariableServiceInitialize (
   } else {\r
     DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));\r
   }\r
-  \r
+\r
   //\r
   // Create "SetupMode" variable with BS+RT attribute set.\r
   //\r
@@ -293,7 +296,7 @@ AutenticatedVariableServiceInitialize (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
   // Create "SignatureSupport" variable with BS+RT attribute set.\r
   //\r
@@ -390,12 +393,12 @@ AutenticatedVariableServiceInitialize (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));\r
 \r
   //\r
   // Check "certdb" variable's existence.\r
-  // If it doesn't exist, then create a new one with \r
+  // If it doesn't exist, then create a new one with\r
   // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
   //\r
   Status = FindVariable (\r
@@ -423,7 +426,7 @@ AutenticatedVariableServiceInitialize (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-  }  \r
+  }\r
 \r
   //\r
   // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.\r
@@ -480,7 +483,7 @@ AutenticatedVariableServiceInitialize (
   Add public key in store and return its index.\r
 \r
   @param[in]  PubKey                  Input pointer to Public Key data\r
-  @param[in]  VariableDataEntry       The variable data entry \r
+  @param[in]  VariableDataEntry       The variable data entry\r
 \r
   @return                             Index of new added item\r
 \r
@@ -543,7 +546,7 @@ AddPubKeyInStore (
         //\r
         return 0;\r
       }\r
-      \r
+\r
       Status = Reclaim (\r
                  mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
                  &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
@@ -573,7 +576,7 @@ AddPubKeyInStore (
       Data      = GetVariableDataPtr (Variable.CurrPtr);\r
       ASSERT ((DataSize != 0) && (Data != NULL));\r
       //\r
-      // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before) \r
+      // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)\r
       //  Therefore, there is no memory overflow in underlying CopyMem.\r
       //\r
       CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
@@ -581,7 +584,7 @@ AddPubKeyInStore (
 \r
       if (mPubKeyNumber == mMaxKeyNumber) {\r
         return 0;\r
-      }     \r
+      }\r
     }\r
 \r
     //\r
@@ -656,7 +659,7 @@ VerifyCounterBasedPayload (
   UINT8                           Digest[SHA256_DIGEST_SIZE];\r
   VOID                            *Rsa;\r
   UINTN                           PayloadSize;\r
-  \r
+\r
   PayloadSize = DataSize - AUTHINFO_SIZE;\r
   Rsa         = NULL;\r
   CertData    = NULL;\r
@@ -885,7 +888,7 @@ UpdatePlatformMode (
 }\r
 \r
 /**\r
-  Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable.\r
+  Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.\r
 \r
   @param[in]  VariableName                Name of Variable to be check.\r
   @param[in]  VendorGuid                  Variable vendor GUID.\r
@@ -894,7 +897,7 @@ UpdatePlatformMode (
 \r
   @return EFI_INVALID_PARAMETER           Invalid signature list format.\r
   @return EFI_SUCCESS                     Passed signature list format check successfully.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 CheckSignatureListFormat(\r
@@ -921,9 +924,10 @@ CheckSignatureListFormat(
 \r
   if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
     IsPk = TRUE;\r
-  } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0) ||\r
-             (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
-              (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0 || StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))){\r
+  } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) ||\r
+             (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
+             ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
+              (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)))) {\r
     IsPk = FALSE;\r
   } else {\r
     return EFI_SUCCESS;\r
@@ -942,10 +946,10 @@ CheckSignatureListFormat(
     for (Index = 0; Index < (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM)); Index++ ) {\r
       if (CompareGuid (&SigList->SignatureType, &mSupportSigItem[Index].SigType)) {\r
         //\r
-        // The value of SignatureSize should always be 16 (size of SignatureOwner \r
+        // The value of SignatureSize should always be 16 (size of SignatureOwner\r
         // component) add the data length according to signature type.\r
         //\r
-        if (mSupportSigItem[Index].SigDataSize != ((UINT32) ~0) && \r
+        if (mSupportSigItem[Index].SigDataSize != ((UINT32) ~0) &&\r
           (SigList->SignatureSize - sizeof (EFI_GUID)) != mSupportSigItem[Index].SigDataSize) {\r
           return EFI_INVALID_PARAMETER;\r
         }\r
@@ -986,7 +990,7 @@ CheckSignatureListFormat(
       return EFI_INVALID_PARAMETER;\r
     }\r
     SigCount += (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize;\r
-    \r
+\r
     SigDataSize -= SigList->SignatureListSize;\r
     SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize);\r
   }\r
@@ -1007,7 +1011,7 @@ CheckSignatureListFormat(
 \r
   @return EFI_SUCCESS           Variable is updated successfully.\r
   @return Others                Failed to update variable.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 VendorKeyIsModified (\r
@@ -1021,7 +1025,7 @@ VendorKeyIsModified (
     return EFI_SUCCESS;\r
   }\r
   mVendorKeyState = VENDOR_KEYS_MODIFIED;\r
-  \r
+\r
   FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
   Status = UpdateVariable (\r
              EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
@@ -1093,10 +1097,10 @@ ProcessVarWithPk (
   UINT8                       *Payload;\r
   UINTN                       PayloadSize;\r
 \r
-  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 || \r
+  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 ||\r
       (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
     //\r
-    // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
+    // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
     // authenticated variable.\r
     //\r
     return EFI_INVALID_PARAMETER;\r
@@ -1221,7 +1225,7 @@ ProcessVarWithKek (
   if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 ||\r
       (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
     //\r
-    // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
+    // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
     // authenticated variable.\r
     //\r
     return EFI_INVALID_PARAMETER;\r
@@ -1253,7 +1257,7 @@ ProcessVarWithKek (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    \r
+\r
     Status = UpdateVariable (\r
                VariableName,\r
                VendorGuid,\r
@@ -1338,23 +1342,23 @@ ProcessVariable (
     //\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
-  \r
+\r
   //\r
   // A time-based authenticated variable and a count-based authenticated variable\r
   // can't be updated by each other.\r
-  // \r
-  if (Variable->CurrPtr != NULL) {    \r
+  //\r
+  if (Variable->CurrPtr != NULL) {\r
     if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
         ((Variable->CurrPtr->Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
-      return EFI_SECURITY_VIOLATION;      \r
+      return EFI_SECURITY_VIOLATION;\r
     }\r
-    \r
-    if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) && \r
+\r
+    if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
         ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
-      return EFI_SECURITY_VIOLATION;      \r
+      return EFI_SECURITY_VIOLATION;\r
     }\r
   }\r
-    \r
+\r
   //\r
   // Process Time-based Authenticated variable.\r
   //\r
@@ -1392,7 +1396,7 @@ ProcessVariable (
       KeyIndex   = Variable->CurrPtr->PubKeyIndex;\r
       IsFirstTime = FALSE;\r
     }\r
-  } else if ((Variable->CurrPtr != NULL) && \r
+  } else if ((Variable->CurrPtr != NULL) &&\r
              ((Variable->CurrPtr->Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0)\r
             ) {\r
     //\r
@@ -1423,7 +1427,7 @@ ProcessVariable (
   if (!IsFirstTime) {\r
     //\r
     // 2 cases need to check here\r
-    //   1. Internal PubKey variable. PubKeyIndex is always 0 \r
+    //   1. Internal PubKey variable. PubKeyIndex is always 0\r
     //   2. Other counter-based AuthVariable. Check input PubKey.\r
     //\r
     if (KeyIndex == 0 || CompareMem (PubKey, mPubKeyStore + (KeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE, EFI_CERT_TYPE_RSA2048_SIZE) != 0) {\r
@@ -1478,7 +1482,7 @@ ProcessVariable (
 \r
   @param[in, out]  Data              Pointer to original EFI_SIGNATURE_LIST.\r
   @param[in]       DataSize          Size of Data buffer.\r
-  @param[in]       FreeBufSize       Size of free data buffer \r
+  @param[in]       FreeBufSize       Size of free data buffer\r
   @param[in]       NewData           Pointer to new EFI_SIGNATURE_LIST to be appended.\r
   @param[in]       NewDataSize       Size of NewData buffer.\r
   @param[out]      MergedBufSize     Size of the merged buffer\r
@@ -1714,7 +1718,7 @@ FindCertsFromDb (
       //\r
       // Check whether VariableName matches.\r
       //\r
-      if ((NameSize == StrLen (VariableName)) && \r
+      if ((NameSize == StrLen (VariableName)) &&\r
           (CompareMem (Data + Offset, VariableName, NameSize * sizeof (CHAR16)) == 0)) {\r
         Offset = Offset + NameSize * sizeof (CHAR16);\r
 \r
@@ -1723,7 +1727,7 @@ FindCertsFromDb (
         }\r
 \r
         if (CertDataSize != NULL) {\r
-          *CertDataSize = CertSize;        \r
+          *CertDataSize = CertSize;\r
         }\r
 \r
         if (CertNodeOffset != NULL) {\r
@@ -1744,7 +1748,7 @@ FindCertsFromDb (
     }\r
   }\r
 \r
-  return EFI_NOT_FOUND;  \r
+  return EFI_NOT_FOUND;\r
 }\r
 \r
 /**\r
@@ -1778,7 +1782,7 @@ GetCertsFromDb (
   if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Get variable "certdb".\r
   //\r
@@ -1788,7 +1792,7 @@ GetCertsFromDb (
              &CertDbVariable,\r
              &mVariableModuleGlobal->VariableGlobal,\r
              FALSE\r
-             );      \r
+             );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -1851,7 +1855,7 @@ DeleteCertsFromDb (
   if ((VariableName == NULL) || (VendorGuid == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Get variable "certdb".\r
   //\r
@@ -1861,7 +1865,7 @@ DeleteCertsFromDb (
              &CertDbVariable,\r
              &mVariableModuleGlobal->VariableGlobal,\r
              FALSE\r
-             );      \r
+             );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -1929,8 +1933,8 @@ DeleteCertsFromDb (
 \r
   //\r
   // Set "certdb".\r
-  // \r
-  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;  \r
+  //\r
+  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
   Status   = UpdateVariable (\r
                EFI_CERT_DB_NAME,\r
                &gEfiCertDbGuid,\r
@@ -1984,7 +1988,7 @@ InsertCertsToDb (
   if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Get variable "certdb".\r
   //\r
@@ -1994,7 +1998,7 @@ InsertCertsToDb (
              &CertDbVariable,\r
              &mVariableModuleGlobal->VariableGlobal,\r
              FALSE\r
-             );      \r
+             );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -2030,7 +2034,7 @@ InsertCertsToDb (
   // Construct new data content of variable "certdb".\r
   //\r
   NameSize      = (UINT32) StrLen (VariableName);\r
-  CertNodeSize  = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16); \r
+  CertNodeSize  = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16);\r
   NewCertDbSize = (UINT32) DataSize + CertNodeSize;\r
   if (NewCertDbSize > mMaxCertDbSize) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -2053,7 +2057,7 @@ InsertCertsToDb (
   CopyMem (&Ptr->CertNodeSize, &CertNodeSize, sizeof (UINT32));\r
   CopyMem (&Ptr->NameSize, &NameSize, sizeof (UINT32));\r
   CopyMem (&Ptr->CertDataSize, &CertDataSize, sizeof (UINT32));\r
-  \r
+\r
   CopyMem (\r
     (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA),\r
     VariableName,\r
@@ -2065,11 +2069,11 @@ InsertCertsToDb (
     CertData,\r
     CertDataSize\r
     );\r
-  \r
+\r
   //\r
   // Set "certdb".\r
-  // \r
-  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;  \r
+  //\r
+  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
   Status   = UpdateVariable (\r
                EFI_CERT_DB_NAME,\r
                &gEfiCertDbGuid,\r
@@ -2349,7 +2353,7 @@ VerifyTimeBasedPayload (
   } else if (AuthVarType == AuthVarTypePriv) {\r
 \r
     //\r
-    // Process common authenticated variable except PK/KEK/DB/DBX.\r
+    // Process common authenticated variable except PK/KEK/DB/DBX/DBT.\r
     // Get signer's certificates from SignedData.\r
     //\r
     VerifyStatus = Pkcs7GetSigners (\r
@@ -2376,7 +2380,7 @@ VerifyTimeBasedPayload (
       if (EFI_ERROR (Status)) {\r
         goto Exit;\r
       }\r
-    \r
+\r
       if ((CertStackSize != CertsSizeinDb) ||\r
           (CompareMem (SignerCerts, CertsInCertDb, CertsSizeinDb) != 0)) {\r
         goto Exit;\r
@@ -2419,7 +2423,7 @@ VerifyTimeBasedPayload (
     Cert     = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
     RootCert      = Cert->SignatureData;\r
     RootCertSize  = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);\r
-    \r
+\r
     // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
     //\r
     VerifyStatus = Pkcs7Verify (\r
@@ -2469,4 +2473,3 @@ Exit:
            &CertData->TimeStamp\r
            );\r
 }\r
-\r