]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Correct the Hash Calculation for Revoked X.509 Certificate to align with RFC3280...
authorLong, Qin <qin.long@intel.com>
Thu, 25 Dec 2014 08:37:08 +0000 (08:37 +0000)
committerqlong <qlong@Edk2>
Thu, 25 Dec 2014 08:37:08 +0000 (08:37 +0000)
This patch added one new X509GetTBSCert() interface in BaseCryptLib to retrieve the TBSCertificate,
and also corrected the hash calculation for revoked certificate to aligned the RFC3280 and UEFI 2.4 spec.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: "Long, Qin" <qin.long@intel.com>
Reviewed-by: "Dong, Guo" <guo.dong@initel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16559 6f19259b-4bc3-4df7-8a09-765794883524

CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
CryptoPkg/Library/BaseCryptLib/Pk/CryptX509Null.c
CryptoPkg/Library/BaseCryptLibRuntimeCryptProtocol/Pk/CryptX509Null.c
SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigImpl.c

index e36041bf4f782c3c60e14c73f770b682fc299e8e..bc36ac7fd1810636e9461ad7bbb1fc4a0a3f1822 100644 (file)
@@ -1906,6 +1906,32 @@ X509StackFree (
   IN  VOID  *X509Stack\r
   );\r
 \r
+/**\r
+  Retrieve the TBSCertificate from one given X.509 certificate.\r
+\r
+  @param[in]      Cert         Pointer to the given DER-encoded X509 certificate.\r
+  @param[in]      CertSize     Size of the X509 certificate in bytes.\r
+  @param[out]     TBSCert      DER-Encoded To-Be-Signed certificate.\r
+  @param[out]     TBSCertSize  Size of the TBS certificate in bytes.\r
+\r
+  If Cert is NULL, then return FALSE.\r
+  If TBSCert is NULL, then return FALSE.\r
+  If TBSCertSize is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @retval  TRUE   The TBSCertificate was retrieved successfully.\r
+  @retval  FALSE  Invalid X.509 certificate.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+X509GetTBSCert (\r
+  IN  CONST UINT8  *Cert,\r
+  IN  UINTN        CertSize,\r
+  OUT UINT8        **TBSCert,\r
+  OUT UINTN        *TBSCertSize\r
+  );\r
+\r
 /**\r
   Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:\r
   Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
@@ -2067,6 +2093,7 @@ AuthenticodeVerify (
   signature.\r
 \r
   If AuthData is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
 \r
   @param[in]  AuthData     Pointer to the Authenticode Signature retrieved from signed\r
                            PE/COFF image to be verified.\r
index 5abe970cceac130f02b295855258a5088a68d246..29efc42b021a60782f54dfd53572f0ef977241fc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2014, 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
@@ -484,3 +484,79 @@ _Exit:
   \r
   return Status;\r
 }\r
+\r
+/**\r
+  Retrieve the TBSCertificate from one given X.509 certificate.\r
+\r
+  @param[in]      Cert         Pointer to the given DER-encoded X509 certificate.\r
+  @param[in]      CertSize     Size of the X509 certificate in bytes.\r
+  @param[out]     TBSCert      DER-Encoded To-Be-Signed certificate.\r
+  @param[out]     TBSCertSize  Size of the TBS certificate in bytes.\r
+\r
+  If Cert is NULL, then return FALSE.\r
+  If TBSCert is NULL, then return FALSE.\r
+  If TBSCertSize is NULL, then return FALSE.\r
+\r
+  @retval  TRUE   The TBSCertificate was retrieved successfully.\r
+  @retval  FALSE  Invalid X.509 certificate.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+X509GetTBSCert (\r
+  IN  CONST UINT8  *Cert,\r
+  IN  UINTN        CertSize,\r
+  OUT UINT8        **TBSCert,\r
+  OUT UINTN        *TBSCertSize\r
+  )\r
+{\r
+  CONST UINT8  *Temp;\r
+  INTN         Asn1Tag;\r
+  INTN         ObjClass;\r
+  UINTN        Length;\r
+\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if ((Cert == NULL) || (TBSCert == NULL) || (TBSCertSize == NULL)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // An X.509 Certificate is: (defined in RFC3280)\r
+  //   Certificate  ::=  SEQUENCE  {\r
+  //     tbsCertificate       TBSCertificate,\r
+  //     signatureAlgorithm   AlgorithmIdentifier,\r
+  //     signature            BIT STRING }\r
+  //\r
+  // and\r
+  //\r
+  //  TBSCertificate  ::=  SEQUENCE  {\r
+  //    version         [0]  Version DEFAULT v1,\r
+  //    ...\r
+  //    }\r
+  //\r
+  // So we can just ASN1-parse the x.509 DER-encoded data. If we strip\r
+  // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.\r
+  //\r
+  Temp = Cert;\r
+  ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);\r
+\r
+  if (Asn1Tag != V_ASN1_SEQUENCE) {\r
+    return FALSE;\r
+  }\r
+\r
+  *TBSCert = (UINT8 *)Temp;\r
+\r
+  ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);\r
+  //\r
+  // Verify the parsed TBSCertificate is one correct SEQUENCE data.\r
+  //\r
+  if (Asn1Tag != V_ASN1_SEQUENCE) {\r
+    return FALSE;\r
+  }\r
+\r
+  *TBSCertSize = Length + (Temp - *TBSCert);\r
+  \r
+  return TRUE;\r
+}\r
index 5dbddca42342284a3544169c24b62fec3f0eb4c0..e1eb84d99b77da3d702d4f46b4d11004dd42fb66 100644 (file)
@@ -2,7 +2,7 @@
   X.509 Certificate Handler Wrapper Implementation which does not provide\r
   real capabilities.\r
 \r
-Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2012 - 2014, 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
@@ -178,3 +178,29 @@ X509VerifyCert (
   ASSERT (FALSE);\r
   return FALSE;\r
 }\r
+\r
+/**\r
+  Retrieve the TBSCertificate from one given X.509 certificate.\r
+\r
+  Return FALSE to indicate this interface is not supported.\r
+\r
+  @param[in]      Cert         Pointer to the given DER-encoded X509 certificate.\r
+  @param[in]      CertSize     Size of the X509 certificate in bytes.\r
+  @param[out]     TBSCert      DER-Encoded To-Be-Signed certificate.\r
+  @param[out]     TBSCertSize  Size of the TBS certificate in bytes.\r
+\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+X509GetTBSCert (\r
+  IN  CONST UINT8  *Cert,\r
+  IN  UINTN        CertSize,\r
+  OUT UINT8        **TBSCert,\r
+  OUT UINTN        *TBSCertSize\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
index 5dbddca42342284a3544169c24b62fec3f0eb4c0..c43ca075839037aed65e9837f387ce87b978d24e 100644 (file)
@@ -2,7 +2,7 @@
   X.509 Certificate Handler Wrapper Implementation which does not provide\r
   real capabilities.\r
 \r
-Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2012 - 2014, 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
@@ -178,3 +178,29 @@ X509VerifyCert (
   ASSERT (FALSE);\r
   return FALSE;\r
 }\r
+\r
+/**\r
+  Retrieve the TBSCertificate from one given X.509 certificate.\r
+\r
+  Return FALSE to indicate this interface is not supported.\r
+\r
+  @param[in]      Cert         Pointer to the given DER-encoded X509 certificate.\r
+  @param[in]      CertSize     Size of the X509 certificate in bytes.\r
+  @param[out]     TBSCert      DER-Encoded To-Be-Signed certificate.\r
+  @param[out]     TBSCertSize  Size of the TBS certificate in bytes.\r
+\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+X509GetTBSCert (\r
+  IN  CONST UINT8  *Cert,\r
+  IN  UINTN        CertSize,\r
+  OUT UINT8        **TBSCert,\r
+  OUT UINTN        *TBSCertSize\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}
\ No newline at end of file
index 2475f35c85f544342d3b7610dc4abd07dcb3ef83..959a9b062db3714fa13b244452a93a43cf3b821a 100644 (file)
@@ -852,6 +852,8 @@ IsCertHashFoundInDatabase (
   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
@@ -859,8 +861,16 @@ IsCertHashFoundInDatabase (
   HashCtx  = NULL;\r
   HashAlg  = HASHALG_MAX;\r
 \r
-  ASSERT (RevocationTime != NULL);\r
-  ASSERT (DbxList != NULL);\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
@@ -879,7 +889,7 @@ IsCertHashFoundInDatabase (
     }\r
 \r
     //\r
-    // Calculate the hash value of current db certificate for comparision.\r
+    // Calculate the hash value of current TBSCertificate for comparision.\r
     //\r
     if (mHash[HashAlg].GetContextSize == NULL) {\r
       goto Done;\r
@@ -893,7 +903,7 @@ IsCertHashFoundInDatabase (
     if (!Status) {\r
       goto Done;\r
     }\r
-    Status = mHash[HashAlg].HashUpdate (HashCtx, Certificate, CertSize);\r
+    Status = mHash[HashAlg].HashUpdate (HashCtx, TBSCert, TBSCertSize);\r
     if (!Status) {\r
       goto Done;\r
     }\r
index 517d9d9904c48e09f44551a23fd89e862808c916..5b8ae7e8d8e2ff4933ca299ab4aceedbffaeb393 100644 (file)
@@ -1073,6 +1073,8 @@ CalculateCertHash (
   BOOLEAN                   Status;\r
   VOID                      *HashCtx;\r
   UINTN                     CtxSize;\r
+  UINT8                     *TBSCert;\r
+  UINTN                     TBSCertSize;\r
 \r
   HashCtx = NULL;\r
   Status  = FALSE;\r
@@ -1081,6 +1083,13 @@ CalculateCertHash (
     return FALSE;\r
   }\r
 \r
+  //\r
+  // Retrieve the TBSCertificate for Hash Calculation.\r
+  //\r
+  if (!X509GetTBSCert (CertData, CertSize, &TBSCert, &TBSCertSize)) {\r
+    return FALSE;\r
+  }\r
+\r
   //\r
   // 1. Initialize context of hash.\r
   //\r
@@ -1099,7 +1108,7 @@ CalculateCertHash (
   //\r
   // 3. Calculate the hash.\r
   //\r
-  Status  = mHash[HashAlg].HashUpdate (HashCtx, CertData, CertSize);\r
+  Status  = mHash[HashAlg].HashUpdate (HashCtx, TBSCert, TBSCertSize);\r
   if (!Status) {\r
     goto Done;\r
   }\r