]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
CryptoPkg/BaseCryptLib: Add missing OpenSSL includes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
index 88c21369b6de0d5481bfbf0300cd167c1a9ffcfe..fba356f8e75860aef4ae5e9ccd8bc715a92af5f5 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2015, 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
@@ -14,13 +14,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "InternalCryptLib.h"\r
 #include <openssl/x509.h>\r
-\r
+#include <openssl/rsa.h>\r
 \r
 /**\r
   Construct a X509 object from DER-encoded certificate data.\r
 \r
-  If Cert is NULL, then ASSERT().\r
-  If SingleX509Cert is NULL, then ASSERT().\r
+  If Cert is NULL, then return FALSE.\r
+  If SingleX509Cert is NULL, then return FALSE.\r
 \r
   @param[in]  Cert            Pointer to the DER-encoded certificate data.\r
   @param[in]  CertSize        The size of certificate data in bytes.\r
@@ -38,53 +38,36 @@ X509ConstructCertificate (
   OUT  UINT8        **SingleX509Cert\r
   )\r
 {\r
-  BIO      *CertBio;\r
-  X509     *X509Cert;\r
-  BOOLEAN  Status;\r
+  X509         *X509Cert;\r
+  CONST UINT8  *Temp;\r
 \r
   //\r
-  // ASSERT if Cert is NULL or SingleX509Cert is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (Cert != NULL);\r
-  ASSERT (SingleX509Cert != NULL);\r
-\r
-  if (CertSize > INT_MAX) {\r
+  if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {\r
     return FALSE;\r
   }\r
 \r
-  Status = FALSE;\r
-\r
   //\r
   // Read DER-encoded X509 Certificate and Construct X509 object.\r
   //\r
-  CertBio = BIO_new (BIO_s_mem ());\r
-  BIO_write (CertBio, Cert, (int) CertSize);\r
-  if (CertBio == NULL) {\r
-    goto _Exit;\r
-  }\r
-  X509Cert = d2i_X509_bio (CertBio, NULL);\r
+  Temp     = Cert;\r
+  X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);\r
   if (X509Cert == NULL) {\r
-    goto _Exit;\r
+    return FALSE;\r
   }\r
 \r
   *SingleX509Cert = (UINT8 *) X509Cert;\r
-  Status = TRUE;\r
-\r
-_Exit:\r
-  //\r
-  // Release Resources.\r
-  //\r
-  BIO_free (CertBio);\r
 \r
-  return Status;\r
+  return TRUE;\r
 }\r
 \r
 /**\r
   Construct a X509 stack object from a list of DER-encoded certificate data.\r
 \r
-  If X509Stack is NULL, then ASSERT().\r
+  If X509Stack is NULL, then return FALSE.\r
 \r
-  @param[in, out]  X509Stack  On input, pointer to an existing X509 stack object.\r
+  @param[in, out]  X509Stack  On input, pointer to an existing or NULL X509 stack object.\r
                               On output, pointer to the X509 stack object with new\r
                               inserted X509 certificate.\r
   @param           ...        A list of DER-encoded single certificate data followed\r
@@ -111,9 +94,11 @@ X509ConstructCertificateStack (
   UINTN           Index;\r
 \r
   //\r
-  // ASSERT if input X509Stack is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (X509Stack != NULL);\r
+  if (X509Stack == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   Status = FALSE;\r
 \r
@@ -140,17 +125,23 @@ X509ConstructCertificateStack (
     }\r
 \r
     CertSize = VA_ARG (Args, UINTN);\r
+    if (CertSize == 0) {\r
+      break;\r
+    }\r
 \r
     //\r
     // Construct X509 Object from the given DER-encoded certificate data.\r
     //\r
+    X509Cert = NULL;\r
     Status = X509ConstructCertificate (\r
                (CONST UINT8 *) Cert,\r
                CertSize,\r
                (UINT8 **) &X509Cert\r
                );\r
     if (!Status) {\r
-      X509_free (X509Cert);\r
+      if (X509Cert != NULL) {\r
+        X509_free (X509Cert);\r
+      }\r
       break;\r
     }\r
 \r
@@ -174,7 +165,7 @@ X509ConstructCertificateStack (
 /**\r
   Release the specified X509 object.\r
 \r
-  If X509Cert is NULL, then ASSERT().\r
+  If X509Cert is NULL, then return FALSE.\r
 \r
   @param[in]  X509Cert  Pointer to the X509 object to be released.\r
 \r
@@ -184,9 +175,14 @@ EFIAPI
 X509Free (\r
   IN  VOID  *X509Cert\r
   )\r
-{\r
-  ASSERT (X509Cert != NULL);\r
-\r
+{ \r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (X509Cert == NULL) {\r
+    return;\r
+  }\r
+  \r
   //\r
   // Free OpenSSL X509 object.\r
   //\r
@@ -196,7 +192,7 @@ X509Free (
 /**\r
   Release the specified X509 stack object.\r
 \r
-  If X509Stack is NULL, then ASSERT().\r
+  If X509Stack is NULL, then return FALSE.\r
 \r
   @param[in]  X509Stack  Pointer to the X509 stack object to be released.\r
 \r
@@ -207,8 +203,13 @@ X509StackFree (
   IN  VOID  *X509Stack\r
   )\r
 {\r
-  ASSERT (X509Stack != NULL);\r
-\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (X509Stack == NULL) {\r
+    return;\r
+  }\r
+  \r
   //\r
   // Free OpenSSL X509 stack object.\r
   //\r
@@ -224,8 +225,8 @@ X509StackFree (
   @param[in, out] SubjectSize  The size in bytes of the CertSubject buffer on input,\r
                                and the size of buffer returned CertSubject on output.\r
 \r
-  If Cert is NULL, then ASSERT().\r
-  If SubjectSize is NULL, then ASSERT().\r
+  If Cert is NULL, then return FALSE.\r
+  If SubjectSize is NULL, then return FALSE.\r
 \r
   @retval  TRUE   The certificate subject retrieved successfully.\r
   @retval  FALSE  Invalid certificate, or the SubjectSize is too small for the result.\r
@@ -246,12 +247,12 @@ X509GetSubjectName (
   X509_NAME  *X509Name;\r
 \r
   //\r
-  // ASSERT if Cert is NULL or SubjectSize is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (Cert        != NULL);\r
-  ASSERT (SubjectSize != NULL);\r
+  if (Cert == NULL || SubjectSize == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
-  Status   = FALSE;\r
   X509Cert = NULL;\r
 \r
   //\r
@@ -259,20 +260,27 @@ X509GetSubjectName (
   //\r
   Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
   if ((X509Cert == NULL) || (!Status)) {\r
+    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
+  Status = FALSE;\r
+\r
   //\r
   // Retrieve subject name from certificate object.\r
   //\r
   X509Name = X509_get_subject_name (X509Cert);\r
+  if (X509Name == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
   if (*SubjectSize < (UINTN) X509Name->bytes->length) {\r
     *SubjectSize = (UINTN) X509Name->bytes->length;\r
     goto _Exit;\r
   }\r
   *SubjectSize = (UINTN) X509Name->bytes->length;\r
   if (CertSubject != NULL) {\r
-    CopyMem (CertSubject, (UINT8 *)X509Name->bytes->data, *SubjectSize);\r
+    CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize);\r
     Status = TRUE;\r
   }\r
 \r
@@ -280,7 +288,9 @@ _Exit:
   //\r
   // Release Resources.\r
   //\r
-  X509_free (X509Cert);\r
+  if (X509Cert != NULL) {\r
+    X509_free (X509Cert);\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -294,8 +304,8 @@ _Exit:
                            RSA public key component. Use RsaFree() function to free the\r
                            resource.\r
 \r
-  If Cert is NULL, then ASSERT().\r
-  If RsaContext is NULL, then ASSERT().\r
+  If Cert is NULL, then return FALSE.\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @retval  TRUE   RSA Public Key was retrieved successfully.\r
   @retval  FALSE  Fail to retrieve RSA public key from X509 certificate.\r
@@ -312,14 +322,14 @@ RsaGetPublicKeyFromX509 (
   BOOLEAN   Status;\r
   EVP_PKEY  *Pkey;\r
   X509      *X509Cert;\r
-\r
+  \r
   //\r
-  // ASSERT if Cert is NULL or RsaContext is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (Cert       != NULL);\r
-  ASSERT (RsaContext != NULL);\r
+  if (Cert == NULL || RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
-  Status   = FALSE;\r
   Pkey     = NULL;\r
   X509Cert = NULL;\r
 \r
@@ -328,9 +338,12 @@ RsaGetPublicKeyFromX509 (
   //\r
   Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
   if ((X509Cert == NULL) || (!Status)) {\r
+    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
+  Status = FALSE;\r
+\r
   //\r
   // Retrieve and check EVP_PKEY data from X509 Certificate.\r
   //\r
@@ -350,8 +363,13 @@ _Exit:
   //\r
   // Release Resources.\r
   //\r
-  X509_free (X509Cert);\r
-  EVP_PKEY_free (Pkey);\r
+  if (X509Cert != NULL) {\r
+    X509_free (X509Cert);\r
+  }\r
+\r
+  if (Pkey != NULL) {\r
+    EVP_PKEY_free (Pkey);\r
+  }  \r
 \r
   return Status;\r
 }\r
@@ -364,8 +382,8 @@ _Exit:
   @param[in]      CACert       Pointer to the DER-encoded trusted CA certificate.\r
   @param[in]      CACertSize   Size of the CA Certificate in bytes.\r
 \r
-  If Cert is NULL, then ASSERT().\r
-  If CACert is NULL, then ASSERT().\r
+  If Cert is NULL, then return FALSE.\r
+  If CACert is NULL, then return FALSE.\r
 \r
   @retval  TRUE   The certificate was issued by the trusted CA.\r
   @retval  FALSE  Invalid certificate or the certificate was not issued by the given\r
@@ -386,12 +404,13 @@ X509VerifyCert (
   X509            *X509CACert;\r
   X509_STORE      *CertStore;\r
   X509_STORE_CTX  CertCtx;\r
-\r
+  \r
   //\r
-  // ASSERT if Cert is NULL or CACert is NULL.\r
+  // Check input parameters.\r
   //\r
-  ASSERT (Cert   != NULL);\r
-  ASSERT (CACert != NULL);\r
+  if (Cert == NULL || CACert == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   Status     = FALSE;\r
   X509Cert   = NULL;\r
@@ -401,15 +420,22 @@ X509VerifyCert (
   //\r
   // Register & Initialize necessary digest algorithms for certificate verification.\r
   //\r
-  EVP_add_digest (EVP_md5());\r
-  EVP_add_digest (EVP_sha1());\r
-  EVP_add_digest (EVP_sha256());\r
+  if (EVP_add_digest (EVP_md5 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
+  if (EVP_add_digest (EVP_sha1 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
+  if (EVP_add_digest (EVP_sha256 ()) == 0) {\r
+    goto _Exit;\r
+  }\r
 \r
   //\r
   // Read DER-encoded certificate to be verified and Construct X509 object.\r
   //\r
   Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
   if ((X509Cert == NULL) || (!Status)) {\r
+    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
@@ -418,9 +444,12 @@ X509VerifyCert (
   //\r
   Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);\r
   if ((X509CACert == NULL) || (!Status)) {\r
+    Status = FALSE;\r
     goto _Exit;\r
   }\r
 \r
+  Status = FALSE;\r
+\r
   //\r
   // Set up X509 Store for trusted certificate.\r
   //\r
@@ -449,9 +478,94 @@ _Exit:
   //\r
   // Release Resources.\r
   //\r
-  X509_free (X509Cert);\r
-  X509_free (X509CACert);\r
-  X509_STORE_free (CertStore);\r
+  if (X509Cert != NULL) {\r
+    X509_free (X509Cert);\r
+  }\r
+\r
+  if (X509CACert != NULL) {\r
+    X509_free (X509CACert);\r
+  }\r
 \r
+  if (CertStore != NULL) {\r
+    X509_STORE_free (CertStore);\r
+  }\r
+  \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) ||\r
+      (TBSCertSize == NULL) || (CertSize > INT_MAX)) {\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