]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
Fix several issues in BaseCryptLib:
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
index 88c21369b6de0d5481bfbf0300cd167c1a9ffcfe..f0a5d0ac7fbd931b0fc5a8a35f325e2c511894a2 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 - 2012, 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
@@ -19,8 +19,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 /**\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
@@ -43,12 +43,9 @@ X509ConstructCertificate (
   BOOLEAN  Status;\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
@@ -82,7 +79,7 @@ _Exit:
 /**\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
                               On output, pointer to the X509 stack object with new\r
@@ -111,9 +108,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
@@ -174,7 +173,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 +183,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 +200,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,14 +211,104 @@ 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
   sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);\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
+  int             Result;\r
+  int             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
+  Length = ((BUF_MEM *) CertBio->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
   Retrieve the subject bytes from one X.509 certificate.\r
 \r
@@ -224,8 +318,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 +340,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 +353,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 +381,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 +397,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 +415,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 +431,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 +456,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 +475,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 +497,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 +513,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 +537,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 +571,17 @@ _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