]> 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 6a84137632b7ef472cabf802f684cd3ba54bf009..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 - 2015, 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
@@ -163,6 +163,7 @@ X509PopCertificate (
   STACK_OF(X509)  *CertStack;\r
   BOOLEAN         Status;\r
   INT32           Result;\r
+  BUF_MEM         *Ptr;\r
   INT32           Length;\r
   VOID            *Buffer;\r
 \r
@@ -192,7 +193,8 @@ X509PopCertificate (
     goto _Exit;\r
   }\r
 \r
-  Length = (INT32)(((BUF_MEM *) CertBio->ptr)->length);\r
+  BIO_get_mem_ptr (CertBio, &Ptr);\r
+  Length = (INT32)(Ptr->length);\r
   if (Length <= 0) {\r
     goto _Exit;\r
   }\r
@@ -229,7 +231,7 @@ _Exit:
   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
@@ -238,10 +240,10 @@ _Exit:
   @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
@@ -435,11 +437,11 @@ Pkcs7FreeSigners (
 \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] SingerChainCerts  Pointer to the certificates list chained to signer's\r
-                                certificate. It's caller's responsiblity to free the buffer.\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
-                                responsiblity to free the buffer.\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
@@ -463,12 +465,15 @@ Pkcs7GetCertificatesList (
   BOOLEAN          Wrapped;\r
   UINT8            Index;\r
   PKCS7            *Pkcs7;\r
-  X509_STORE_CTX   CertCtx;\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             *TempCert;\r
   X509             *Issuer;\r
+  X509_NAME        *IssuerName;\r
   UINT8            *CertBuf;\r
   UINT8            *OldBuf;\r
   UINTN            BufferSize;\r
@@ -482,13 +487,18 @@ Pkcs7GetCertificatesList (
   Status         = FALSE;\r
   NewP7Data      = NULL;\r
   Pkcs7          = NULL;\r
+  CertCtx        = NULL;\r
+  CtxChain       = NULL;\r
+  CtxCert        = NULL;\r
+  CtxUntrusted   = NULL;\r
   Cert           = NULL;\r
-  TempCert       = 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
@@ -521,7 +531,7 @@ Pkcs7GetCertificatesList (
   //\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 singer's certificate.\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
@@ -529,19 +539,28 @@ Pkcs7GetCertificatesList (
   }\r
   Signer = sk_X509_value (Signers, 0);\r
 \r
-  if (!X509_STORE_CTX_init (&CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {\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
-  if (CertCtx.chain == NULL) {\r
-    if (((CertCtx.chain = sk_X509_new_null ()) == NULL) ||\r
-        (!sk_X509_push (CertCtx.chain, CertCtx.cert))) {\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
-  (VOID)sk_X509_delete_ptr (CertCtx.untrusted, Signer);\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
@@ -551,27 +570,25 @@ Pkcs7GetCertificatesList (
     //\r
     // Self-Issue checking\r
     //\r
-    if (CertCtx.check_issued (&CertCtx, Cert, Cert)) {\r
-      break;\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 (CertCtx.untrusted != NULL) {\r
+    if (CtxUntrusted != NULL) {\r
       Issuer = NULL;\r
-      for (Index = 0; Index < sk_X509_num (CertCtx.untrusted); Index++) {\r
-        TempCert = sk_X509_value (CertCtx.untrusted, Index);\r
-        if (CertCtx.check_issued (&CertCtx, Cert, TempCert)) {\r
-          Issuer = TempCert;\r
-          break;\r
-        }\r
-      }\r
+      IssuerName = X509_get_issuer_name (Cert);\r
+      Issuer     = X509_find_by_subject (CtxUntrusted, IssuerName);\r
       if (Issuer != NULL) {\r
-        if (!sk_X509_push (CertCtx.chain, Issuer)) {\r
+        if (!sk_X509_push (CtxChain, Issuer)) {\r
           goto _Error;\r
         }\r
-        (VOID)sk_X509_delete_ptr (CertCtx.untrusted, Issuer);\r
+        (VOID)sk_X509_delete_ptr (CtxUntrusted, Issuer);\r
 \r
         Cert = Issuer;\r
         continue;\r
@@ -593,13 +610,13 @@ Pkcs7GetCertificatesList (
   //      UINT8  Certn[];\r
   //\r
 \r
-  if (CertCtx.chain != NULL) {\r
+  if (CtxChain != NULL) {\r
     BufferSize = sizeof (UINT8);\r
     OldSize    = BufferSize;\r
     CertBuf    = NULL;\r
 \r
     for (Index = 0; ; Index++) {\r
-      Status = X509PopCertificate (CertCtx.chain, &SingleCert, &CertSize);\r
+      Status = X509PopCertificate (CtxChain, &SingleCert, &CertSize);\r
       if (!Status) {\r
         break;\r
       }\r
@@ -637,13 +654,13 @@ Pkcs7GetCertificatesList (
     }\r
   }\r
 \r
-  if (CertCtx.untrusted != NULL) {\r
+  if (CtxUntrusted != NULL) {\r
     BufferSize = sizeof (UINT8);\r
     OldSize    = BufferSize;\r
     CertBuf    = NULL;\r
 \r
     for (Index = 0; ; Index++) {\r
-      Status = X509PopCertificate (CertCtx.untrusted, &SingleCert, &CertSize);\r
+      Status = X509PopCertificate (CtxUntrusted, &SingleCert, &CertSize);\r
       if (!Status) {\r
         break;\r
       }\r
@@ -696,7 +713,10 @@ _Error:
   }\r
   sk_X509_free (Signers);\r
 \r
-  X509_STORE_CTX_cleanup (&CertCtx);\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
@@ -716,12 +736,12 @@ _Error:
 }\r
 \r
 /**\r
-  Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\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
@@ -895,7 +915,7 @@ _Exit:
   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
+  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
@@ -903,13 +923,13 @@ _Exit:
   @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 responsiblity to free the buffer.\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
+**/\r
 BOOLEAN\r
 EFIAPI\r
 Pkcs7GetAttachedContent (\r