]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7Verify.c
CryptoPkg/BaseCryptLib: Add NULL pointer checks in DH and P7Verify
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7Verify.c
index 2e72f892cb144669859ecce0097d6ef76c6db9d8..d564591cb7f942d309323d8fe28f723b4de7846c 100644 (file)
@@ -10,7 +10,7 @@
   WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated\r
   Variable and will do basic check for data structure.\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2017, 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
@@ -25,91 +25,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <openssl/objects.h>\r
 #include <openssl/x509.h>\r
+#include <openssl/x509v3.h>\r
 #include <openssl/pkcs7.h>\r
 \r
 UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };\r
 \r
-/**\r
-  Verification callback function to override any existing callbacks in OpenSSL\r
-  for intermediate certificate supports.\r
-\r
-  @param[in]  Status   Original status before calling this callback.\r
-  @param[in]  Context  X509 store context.\r
-\r
-  @retval     1        Current X509 certificate is verified successfully.\r
-  @retval     0        Verification failed.\r
-\r
-**/\r
-int\r
-X509VerifyCb (\r
-  IN int            Status,\r
-  IN X509_STORE_CTX *Context\r
-  )\r
-{\r
-  X509_OBJECT  *Obj;\r
-  INTN         Error;\r
-  INTN         Index;\r
-  INTN         Count;\r
-\r
-  Obj   = NULL;\r
-  Error = (INTN) X509_STORE_CTX_get_error (Context);\r
-\r
-  //\r
-  // X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT and X509_V_ERR_UNABLE_TO_GET_ISSUER_\r
-  // CERT_LOCALLY mean a X509 certificate is not self signed and its issuer\r
-  // can not be found in X509_verify_cert of X509_vfy.c.\r
-  // In order to support intermediate certificate node, we override the\r
-  // errors if the certification is obtained from X509 store, i.e. it is\r
-  // a trusted ceritifcate node that is enrolled by user.\r
-  // Besides,X509_V_ERR_CERT_UNTRUSTED and X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE\r
-  // are also ignored to enable such feature.\r
-  //\r
-  if ((Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT) ||\r
-      (Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)) {\r
-    Obj = (X509_OBJECT *) malloc (sizeof (X509_OBJECT));\r
-    if (Obj == NULL) {\r
-      return 0;\r
-    }\r
-\r
-    Obj->type      = X509_LU_X509;\r
-    Obj->data.x509 = Context->current_cert;\r
-\r
-    CRYPTO_w_lock (CRYPTO_LOCK_X509_STORE);\r
-\r
-    if (X509_OBJECT_retrieve_match (Context->ctx->objs, Obj)) {\r
-      Status = 1;\r
-    } else {\r
-      //\r
-      // If any certificate in the chain is enrolled as trusted certificate,\r
-      // pass the certificate verification.\r
-      //\r
-      if (Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) {\r
-        Count = (INTN) sk_X509_num (Context->chain);\r
-        for (Index = 0; Index < Count; Index++) {\r
-          Obj->data.x509 = sk_X509_value (Context->chain, (int) Index);\r
-          if (X509_OBJECT_retrieve_match (Context->ctx->objs, Obj)) {\r
-            Status = 1;\r
-            break;\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    CRYPTO_w_unlock (CRYPTO_LOCK_X509_STORE);\r
-  }\r
-\r
-  if ((Error == X509_V_ERR_CERT_UNTRUSTED) ||\r
-      (Error == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE)) {\r
-    Status = 1;\r
-  }\r
-\r
-  if (Obj != NULL) {\r
-    OPENSSL_free (Obj);\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
 /**\r
   Check input P7Data is a wrapped ContentInfo structure or not. If not construct\r
   a new structure to wrap P7Data.\r
@@ -122,7 +42,7 @@ X509VerifyCb (
   @param[in]  P7Length     Length of the PKCS#7 message in bytes.\r
   @param[out] WrapFlag     If TRUE P7Data is a ContentInfo structure, otherwise\r
                            return FALSE.\r
-  @param[out] WrapData     If return status of this function is TRUE: \r
+  @param[out] WrapData     If return status of this function is TRUE:\r
                            1) when WrapFlag is TRUE, pointer to P7Data.\r
                            2) when WrapFlag is FALSE, pointer to a new ContentInfo\r
                            structure. It's caller's responsibility to free this\r
@@ -218,13 +138,100 @@ WrapPkcs7Data (
   return TRUE;\r
 }\r
 \r
+/**\r
+  Pop single certificate from STACK_OF(X509).\r
+\r
+  If X509Stack, Cert, or CertSize is NULL, then return FALSE.\r
+\r
+  @param[in]  X509Stack       Pointer to a X509 stack object.\r
+  @param[out] Cert            Pointer to a X509 certificate.\r
+  @param[out] CertSize        Length of output X509 certificate in bytes.\r
+\r
+  @retval     TRUE            The X509 stack pop succeeded.\r
+  @retval     FALSE           The pop operation failed.\r
+\r
+**/\r
+BOOLEAN\r
+X509PopCertificate (\r
+  IN  VOID  *X509Stack,\r
+  OUT UINT8 **Cert,\r
+  OUT UINTN *CertSize\r
+  )\r
+{\r
+  BIO             *CertBio;\r
+  X509            *X509Cert;\r
+  STACK_OF(X509)  *CertStack;\r
+  BOOLEAN         Status;\r
+  INT32           Result;\r
+  BUF_MEM         *Ptr;\r
+  INT32           Length;\r
+  VOID            *Buffer;\r
+\r
+  Status = FALSE;\r
+\r
+  if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) {\r
+    return Status;\r
+  }\r
+\r
+  CertStack = (STACK_OF(X509) *) X509Stack;\r
+\r
+  X509Cert = sk_X509_pop (CertStack);\r
+\r
+  if (X509Cert == NULL) {\r
+    return Status;\r
+  }\r
+\r
+  Buffer = NULL;\r
+\r
+  CertBio = BIO_new (BIO_s_mem ());\r
+  if (CertBio == NULL) {\r
+    return Status;\r
+  }\r
+\r
+  Result = i2d_X509_bio (CertBio, X509Cert);\r
+  if (Result == 0) {\r
+    goto _Exit;\r
+  }\r
+\r
+  BIO_get_mem_ptr (CertBio, &Ptr);\r
+  Length = (INT32)(Ptr->length);\r
+  if (Length <= 0) {\r
+    goto _Exit;\r
+  }\r
+\r
+  Buffer = malloc (Length);\r
+  if (Buffer == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  Result = BIO_read (CertBio, Buffer, Length);\r
+  if (Result != Length) {\r
+    goto _Exit;\r
+  }\r
+\r
+  *Cert     = Buffer;\r
+  *CertSize = Length;\r
+\r
+  Status = TRUE;\r
+\r
+_Exit:\r
+\r
+  BIO_free (CertBio);\r
+\r
+  if (!Status && (Buffer != NULL)) {\r
+    free (Buffer);\r
+  }\r
+\r
+  return Status;\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
   in a ContentInfo structure.\r
 \r
   If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then\r
-  return FALSE. If P7Length overflow, then return FAlSE.\r
+  return FALSE. If P7Length overflow, then return FALSE.\r
 \r
   Caution: This function may receive untrusted input.\r
   UEFI Authenticated Variable is external input, so this function will do basic\r
@@ -233,10 +240,10 @@ WrapPkcs7Data (
   @param[in]  P7Data       Pointer to the PKCS#7 message to verify.\r
   @param[in]  P7Length     Length of the PKCS#7 message in bytes.\r
   @param[out] CertStack    Pointer to Signer's certificates retrieved from P7Data.\r
-                           It's caller's responsiblity to free the buffer.\r
+                           It's caller's responsibility to free the buffer.\r
   @param[out] StackLength  Length of signer's certificates in bytes.\r
   @param[out] TrustedCert  Pointer to a trusted certificate from Signer's certificates.\r
-                           It's caller's responsiblity to free the buffer.\r
+                           It's caller's responsibility to free the buffer.\r
   @param[out] CertLength   Length of the trusted certificate in bytes.\r
 \r
   @retval  TRUE            The operation is finished successfully.\r
@@ -257,7 +264,7 @@ Pkcs7GetSigners (
   PKCS7            *Pkcs7;\r
   BOOLEAN          Status;\r
   UINT8            *SignedData;\r
-  UINT8            *Temp;\r
+  CONST UINT8      *Temp;\r
   UINTN            SignedDataSize;\r
   BOOLEAN          Wrapped;\r
   STACK_OF(X509)   *Stack;\r
@@ -273,7 +280,7 @@ Pkcs7GetSigners (
       (TrustedCert == NULL) || (CertLength == NULL) || (P7Length > INT_MAX)) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r
   if (!Status) {\r
     return Status;\r
@@ -324,7 +331,7 @@ Pkcs7GetSigners (
   //\r
   BufferSize = sizeof (UINT8);\r
   OldSize    = BufferSize;\r
-  \r
+\r
   for (Index = 0; ; Index++) {\r
     Status = X509PopCertificate (Stack, &SingleCert, &SingleCertSize);\r
     if (!Status) {\r
@@ -369,7 +376,7 @@ Pkcs7GetSigners (
     *CertStack   = CertBuf;\r
     *StackLength = BufferSize;\r
     Status = TRUE;\r
-  } \r
+  }\r
 \r
 _Exit:\r
   //\r
@@ -399,7 +406,7 @@ _Exit:
   if (OldBuf != NULL) {\r
     free (OldBuf);\r
   }\r
-  \r
+\r
   return Status;\r
 }\r
 \r
@@ -423,12 +430,318 @@ Pkcs7FreeSigners (
 }\r
 \r
 /**\r
-  Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\r
+  Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:\r
+  Cryptographic Message Syntax Standard", and outputs two certificate lists chained and\r
+  unchained to the signer's certificates.\r
+  The input signed data could be wrapped in a ContentInfo structure.\r
+\r
+  @param[in]  P7Data            Pointer to the PKCS#7 message.\r
+  @param[in]  P7Length          Length of the PKCS#7 message in bytes.\r
+  @param[out] SignerChainCerts  Pointer to the certificates list chained to signer's\r
+                                certificate. It's caller's responsibility to free the buffer.\r
+  @param[out] ChainLength       Length of the chained certificates list buffer in bytes.\r
+  @param[out] UnchainCerts      Pointer to the unchained certificates lists. It's caller's\r
+                                responsibility to free the buffer.\r
+  @param[out] UnchainLength     Length of the unchained certificates list buffer in bytes.\r
+\r
+  @retval  TRUE         The operation is finished successfully.\r
+  @retval  FALSE        Error occurs during the operation.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Pkcs7GetCertificatesList (\r
+  IN  CONST UINT8  *P7Data,\r
+  IN  UINTN        P7Length,\r
+  OUT UINT8        **SignerChainCerts,\r
+  OUT UINTN        *ChainLength,\r
+  OUT UINT8        **UnchainCerts,\r
+  OUT UINTN        *UnchainLength\r
+  )\r
+{\r
+  BOOLEAN          Status;\r
+  UINT8            *NewP7Data;\r
+  UINTN            NewP7Length;\r
+  BOOLEAN          Wrapped;\r
+  UINT8            Index;\r
+  PKCS7            *Pkcs7;\r
+  X509_STORE_CTX   *CertCtx;\r
+  STACK_OF(X509)   *CtxChain;\r
+  STACK_OF(X509)   *CtxUntrusted;\r
+  X509             *CtxCert;\r
+  STACK_OF(X509)   *Signers;\r
+  X509             *Signer;\r
+  X509             *Cert;\r
+  X509             *Issuer;\r
+  X509_NAME        *IssuerName;\r
+  UINT8            *CertBuf;\r
+  UINT8            *OldBuf;\r
+  UINTN            BufferSize;\r
+  UINTN            OldSize;\r
+  UINT8            *SingleCert;\r
+  UINTN            CertSize;\r
+\r
+  //\r
+  // Initializations\r
+  //\r
+  Status         = FALSE;\r
+  NewP7Data      = NULL;\r
+  Pkcs7          = NULL;\r
+  CertCtx        = NULL;\r
+  CtxChain       = NULL;\r
+  CtxCert        = NULL;\r
+  CtxUntrusted   = NULL;\r
+  Cert           = NULL;\r
+  SingleCert     = NULL;\r
+  CertBuf        = NULL;\r
+  OldBuf         = NULL;\r
+  Signers        = NULL;\r
+\r
+  ZeroMem (&CertCtx, sizeof (CertCtx));\r
+\r
+  //\r
+  // Parameter Checking\r
+  //\r
+  if ((P7Data == NULL) || (SignerChainCerts == NULL) || (ChainLength == NULL) ||\r
+      (UnchainCerts == NULL) || (UnchainLength == NULL) || (P7Length > INT_MAX)) {\r
+    return Status;\r
+  }\r
+\r
+  *SignerChainCerts = NULL;\r
+  *ChainLength      = 0;\r
+  *UnchainCerts     = NULL;\r
+  *UnchainLength    = 0;\r
+\r
+  //\r
+  // Construct a new PKCS#7 data wrapping with ContentInfo structure if needed.\r
+  //\r
+  Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &NewP7Data, &NewP7Length);\r
+  if (!Status || (NewP7Length > INT_MAX)) {\r
+    goto _Error;\r
+  }\r
+\r
+  //\r
+  // Decodes PKCS#7 SignedData\r
+  //\r
+  Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &NewP7Data, (int) NewP7Length);\r
+  if ((Pkcs7 == NULL) || (!PKCS7_type_is_signed (Pkcs7))) {\r
+    goto _Error;\r
+  }\r
+\r
+  //\r
+  // Obtains Signer's Certificate from PKCS#7 data\r
+  // NOTE: Only one signer case will be handled in this function, which means SignerInfos\r
+  //       should include only one signer's certificate.\r
+  //\r
+  Signers = PKCS7_get0_signers (Pkcs7, NULL, PKCS7_BINARY);\r
+  if ((Signers == NULL) || (sk_X509_num (Signers) != 1)) {\r
+    goto _Error;\r
+  }\r
+  Signer = sk_X509_value (Signers, 0);\r
+\r
+  CertCtx = X509_STORE_CTX_new ();\r
+  if (CertCtx == NULL) {\r
+    goto _Error;\r
+  }\r
+  if (!X509_STORE_CTX_init (CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {\r
+    goto _Error;\r
+  }\r
+  //\r
+  // Initialize Chained & Untrusted stack\r
+  //\r
+  CtxChain = X509_STORE_CTX_get0_chain (CertCtx);\r
+  CtxCert  = X509_STORE_CTX_get0_cert (CertCtx);\r
+  if (CtxChain == NULL) {\r
+    if (((CtxChain = sk_X509_new_null ()) == NULL) ||\r
+        (!sk_X509_push (CtxChain, CtxCert))) {\r
+      goto _Error;\r
+    }\r
+  }\r
+  CtxUntrusted = X509_STORE_CTX_get0_untrusted (CertCtx);\r
+  if (CtxUntrusted != NULL) {\r
+    (VOID)sk_X509_delete_ptr (CtxUntrusted, Signer);\r
+  }\r
+\r
+  //\r
+  // Build certificates stack chained from Signer's certificate.\r
+  //\r
+  Cert = Signer;\r
+  for (; ;) {\r
+    //\r
+    // Self-Issue checking\r
+    //\r
+    Issuer = NULL;\r
+    if (X509_STORE_CTX_get1_issuer (&Issuer, CertCtx, Cert) == 1) {\r
+      if (X509_cmp (Issuer, Cert) == 0) {\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Found the issuer of the current certificate\r
+    //\r
+    if (CtxUntrusted != NULL) {\r
+      Issuer = NULL;\r
+      IssuerName = X509_get_issuer_name (Cert);\r
+      Issuer     = X509_find_by_subject (CtxUntrusted, IssuerName);\r
+      if (Issuer != NULL) {\r
+        if (!sk_X509_push (CtxChain, Issuer)) {\r
+          goto _Error;\r
+        }\r
+        (VOID)sk_X509_delete_ptr (CtxUntrusted, Issuer);\r
+\r
+        Cert = Issuer;\r
+        continue;\r
+      }\r
+    }\r
+\r
+    break;\r
+  }\r
+\r
+  //\r
+  // Converts Chained and Untrusted Certificate to Certificate Buffer in following format:\r
+  //      UINT8  CertNumber;\r
+  //      UINT32 Cert1Length;\r
+  //      UINT8  Cert1[];\r
+  //      UINT32 Cert2Length;\r
+  //      UINT8  Cert2[];\r
+  //      ...\r
+  //      UINT32 CertnLength;\r
+  //      UINT8  Certn[];\r
+  //\r
+\r
+  if (CtxChain != NULL) {\r
+    BufferSize = sizeof (UINT8);\r
+    OldSize    = BufferSize;\r
+    CertBuf    = NULL;\r
+\r
+    for (Index = 0; ; Index++) {\r
+      Status = X509PopCertificate (CtxChain, &SingleCert, &CertSize);\r
+      if (!Status) {\r
+        break;\r
+      }\r
+\r
+      OldSize    = BufferSize;\r
+      OldBuf     = CertBuf;\r
+      BufferSize = OldSize + CertSize + sizeof (UINT32);\r
+      CertBuf    = malloc (BufferSize);\r
+\r
+      if (CertBuf == NULL) {\r
+        Status = FALSE;\r
+        goto _Error;\r
+      }\r
+      if (OldBuf != NULL) {\r
+        CopyMem (CertBuf, OldBuf, OldSize);\r
+        free (OldBuf);\r
+        OldBuf = NULL;\r
+      }\r
+\r
+      WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);\r
+      CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);\r
+\r
+      free (SingleCert);\r
+      SingleCert = NULL;\r
+    }\r
+\r
+    if (CertBuf != NULL) {\r
+      //\r
+      // Update CertNumber.\r
+      //\r
+      CertBuf[0] = Index;\r
+\r
+      *SignerChainCerts = CertBuf;\r
+      *ChainLength      = BufferSize;\r
+    }\r
+  }\r
+\r
+  if (CtxUntrusted != NULL) {\r
+    BufferSize = sizeof (UINT8);\r
+    OldSize    = BufferSize;\r
+    CertBuf    = NULL;\r
+\r
+    for (Index = 0; ; Index++) {\r
+      Status = X509PopCertificate (CtxUntrusted, &SingleCert, &CertSize);\r
+      if (!Status) {\r
+        break;\r
+      }\r
+\r
+      OldSize    = BufferSize;\r
+      OldBuf     = CertBuf;\r
+      BufferSize = OldSize + CertSize + sizeof (UINT32);\r
+      CertBuf    = malloc (BufferSize);\r
+\r
+      if (CertBuf == NULL) {\r
+        Status = FALSE;\r
+        goto _Error;\r
+      }\r
+      if (OldBuf != NULL) {\r
+        CopyMem (CertBuf, OldBuf, OldSize);\r
+        free (OldBuf);\r
+        OldBuf = NULL;\r
+      }\r
+\r
+      WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);\r
+      CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);\r
+\r
+      free (SingleCert);\r
+      SingleCert = NULL;\r
+    }\r
+\r
+    if (CertBuf != NULL) {\r
+      //\r
+      // Update CertNumber.\r
+      //\r
+      CertBuf[0] = Index;\r
+\r
+      *UnchainCerts  = CertBuf;\r
+      *UnchainLength = BufferSize;\r
+    }\r
+  }\r
+\r
+  Status = TRUE;\r
+\r
+_Error:\r
+  //\r
+  // Release Resources.\r
+  //\r
+  if (!Wrapped && (NewP7Data != NULL)) {\r
+    free (NewP7Data);\r
+  }\r
+\r
+  if (Pkcs7 != NULL) {\r
+    PKCS7_free (Pkcs7);\r
+  }\r
+  sk_X509_free (Signers);\r
+\r
+  if (CertCtx != NULL) {\r
+    X509_STORE_CTX_cleanup (CertCtx);\r
+    X509_STORE_CTX_free (CertCtx);\r
+  }\r
+\r
+  if (SingleCert != NULL) {\r
+    free (SingleCert);\r
+  }\r
+\r
+  if (OldBuf != NULL) {\r
+    free (OldBuf);\r
+  }\r
+\r
+  if (!Status && (CertBuf != NULL)) {\r
+    free (CertBuf);\r
+    *SignerChainCerts = NULL;\r
+    *UnchainCerts     = NULL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:\r
   Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
   in a ContentInfo structure.\r
 \r
   If P7Data, TrustedCert or InData is NULL, then return FALSE.\r
-  If P7Length, CertLength or DataLength overflow, then return FAlSE.\r
+  If P7Length, CertLength or DataLength overflow, then return FALSE.\r
 \r
   Caution: This function may receive untrusted input.\r
   UEFI Authenticated Variable is external input, so this function will do basic\r
@@ -458,26 +771,24 @@ Pkcs7Verify (
   )\r
 {\r
   PKCS7       *Pkcs7;\r
-  BIO         *CertBio;\r
   BIO         *DataBio;\r
   BOOLEAN     Status;\r
   X509        *Cert;\r
   X509_STORE  *CertStore;\r
   UINT8       *SignedData;\r
-  UINT8       *Temp;\r
+  CONST UINT8 *Temp;\r
   UINTN       SignedDataSize;\r
   BOOLEAN     Wrapped;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (P7Data == NULL || TrustedCert == NULL || InData == NULL || \r
+  if (P7Data == NULL || TrustedCert == NULL || InData == NULL ||\r
     P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   Pkcs7     = NULL;\r
-  CertBio   = NULL;\r
   DataBio   = NULL;\r
   Cert      = NULL;\r
   CertStore = NULL;\r
@@ -494,18 +805,23 @@ Pkcs7Verify (
   if (EVP_add_digest (EVP_sha256 ()) == 0) {\r
     return FALSE;\r
   }\r
+  if (EVP_add_digest (EVP_sha384 ()) == 0) {\r
+    return FALSE;\r
+  }\r
+  if (EVP_add_digest (EVP_sha512 ()) == 0) {\r
+    return FALSE;\r
+  }\r
   if (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA) == 0) {\r
     return FALSE;\r
   }\r
 \r
-\r
   Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r
   if (!Status) {\r
     return Status;\r
   }\r
 \r
   Status = FALSE;\r
-  \r
+\r
   //\r
   // Retrieve PKCS#7 Data (DER encoding)\r
   //\r
@@ -529,12 +845,8 @@ Pkcs7Verify (
   //\r
   // Read DER-encoded root certificate and Construct X509 Certificate\r
   //\r
-  CertBio = BIO_new (BIO_s_mem ());\r
-  BIO_write (CertBio, TrustedCert, (int)CertLength);\r
-  if (CertBio == NULL) {\r
-    goto _Exit;\r
-  }\r
-  Cert = d2i_X509_bio (CertBio, NULL);\r
+  Temp = TrustedCert;\r
+  Cert = d2i_X509 (NULL, &Temp, (long) CertLength);\r
   if (Cert == NULL) {\r
     goto _Exit;\r
   }\r
@@ -550,18 +862,32 @@ Pkcs7Verify (
     goto _Exit;\r
   }\r
 \r
-  //\r
-  // Register customized X509 verification callback function to support\r
-  // trusted intermediate certificate anchor.\r
-  //\r
-  CertStore->verify_cb = X509VerifyCb;\r
-\r
   //\r
   // For generic PKCS#7 handling, InData may be NULL if the content is present\r
   // in PKCS#7 structure. So ignore NULL checking here.\r
   //\r
   DataBio = BIO_new (BIO_s_mem ());\r
-  BIO_write (DataBio, InData, (int)DataLength);\r
+  if (DataBio == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  if (BIO_write (DataBio, InData, (int) DataLength) <= 0) {\r
+    goto _Exit;\r
+  }\r
+\r
+  //\r
+  // Allow partial certificate chains, terminated by a non-self-signed but\r
+  // still trusted intermediate certificate. Also disable time checks.\r
+  //\r
+  X509_STORE_set_flags (CertStore,\r
+                        X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);\r
+\r
+  //\r
+  // OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and\r
+  // doesn't support the extended key usage for Authenticode Code Signing.\r
+  // Bypass the certificate purpose checking by enabling any purposes setting.\r
+  //\r
+  X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY);\r
 \r
   //\r
   // Verifies the PKCS#7 signedData structure\r
@@ -573,7 +899,6 @@ _Exit:
   // Release Resources\r
   //\r
   BIO_free (DataBio);\r
-  BIO_free (CertBio);\r
   X509_free (Cert);\r
   X509_STORE_free (CertStore);\r
   PKCS7_free (Pkcs7);\r
@@ -584,3 +909,114 @@ _Exit:
 \r
   return Status;\r
 }\r
+\r
+/**\r
+  Extracts the attached content from a PKCS#7 signed data if existed. The input signed\r
+  data could be wrapped in a ContentInfo structure.\r
+\r
+  If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,\r
+  then return FALSE. If the P7Data is not correctly formatted, then return FALSE.\r
+\r
+  Caution: This function may receive untrusted input. So this function will do\r
+           basic check for PKCS#7 data structure.\r
+\r
+  @param[in]   P7Data       Pointer to the PKCS#7 signed data to process.\r
+  @param[in]   P7Length     Length of the PKCS#7 signed data in bytes.\r
+  @param[out]  Content      Pointer to the extracted content from the PKCS#7 signedData.\r
+                            It's caller's responsibility to free the buffer.\r
+  @param[out]  ContentSize  The size of the extracted content in bytes.\r
+\r
+  @retval     TRUE          The P7Data was correctly formatted for processing.\r
+  @retval     FALSE         The P7Data was not correctly formatted for processing.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Pkcs7GetAttachedContent (\r
+  IN  CONST UINT8  *P7Data,\r
+  IN  UINTN        P7Length,\r
+  OUT VOID         **Content,\r
+  OUT UINTN        *ContentSize\r
+  )\r
+{\r
+  BOOLEAN            Status;\r
+  PKCS7              *Pkcs7;\r
+  UINT8              *SignedData;\r
+  UINTN              SignedDataSize;\r
+  BOOLEAN            Wrapped;\r
+  CONST UINT8        *Temp;\r
+  ASN1_OCTET_STRING  *OctStr;\r
+\r
+  //\r
+  // Check input parameter.\r
+  //\r
+  if ((P7Data == NULL) || (P7Length > INT_MAX) || (Content == NULL) || (ContentSize == NULL)) {\r
+    return FALSE;\r
+  }\r
+\r
+  *Content   = NULL;\r
+  Pkcs7      = NULL;\r
+  SignedData = NULL;\r
+  OctStr     = NULL;\r
+\r
+  Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r
+  if (!Status || (SignedDataSize > INT_MAX)) {\r
+    goto _Exit;\r
+  }\r
+\r
+  Status = FALSE;\r
+\r
+  //\r
+  // Decoding PKCS#7 SignedData\r
+  //\r
+  Temp  = SignedData;\r
+  Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (int)SignedDataSize);\r
+  if (Pkcs7 == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  //\r
+  // The type of Pkcs7 must be signedData\r
+  //\r
+  if (!PKCS7_type_is_signed (Pkcs7)) {\r
+    goto _Exit;\r
+  }\r
+\r
+  //\r
+  // Check for detached or attached content\r
+  //\r
+  if (PKCS7_get_detached (Pkcs7)) {\r
+    //\r
+    // No Content supplied for PKCS7 detached signedData\r
+    //\r
+    *Content     = NULL;\r
+    *ContentSize = 0;\r
+  } else {\r
+    //\r
+    // Retrieve the attached content in PKCS7 signedData\r
+    //\r
+    OctStr = Pkcs7->d.sign->contents->d.data;\r
+    if ((OctStr->length > 0) && (OctStr->data != NULL)) {\r
+      *ContentSize = OctStr->length;\r
+      *Content     = malloc (*ContentSize);\r
+      if (*Content == NULL) {\r
+        *ContentSize = 0;\r
+        goto _Exit;\r
+      }\r
+      CopyMem (*Content, OctStr->data, *ContentSize);\r
+    }\r
+  }\r
+  Status = TRUE;\r
+\r
+_Exit:\r
+  //\r
+  // Release Resources\r
+  //\r
+  PKCS7_free (Pkcs7);\r
+\r
+  if (!Wrapped) {\r
+    OPENSSL_free (SignedData);\r
+  }\r
+\r
+  return Status;\r
+}\r