X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptX509.c;h=fba356f8e75860aef4ae5e9ccd8bc715a92af5f5;hp=7557399aa4e7a6a0e7ca4c9572c951522af68aab;hb=1cae0c83bbd88822076542e2715077ca2a8bcadb;hpb=b7d320f8117ed2fffe001b1a0b7bfcd4f40fafc4 diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c index 7557399aa4..fba356f8e7 100644 --- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c +++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c @@ -1,7 +1,7 @@ /** @file X.509 Certificate Handler Wrapper Implementation over OpenSSL. -Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -14,13 +14,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "InternalCryptLib.h" #include - +#include /** Construct a X509 object from DER-encoded certificate data. - If Cert is NULL, then ASSERT(). - If SingleX509Cert is NULL, then ASSERT(). + If Cert is NULL, then return FALSE. + If SingleX509Cert is NULL, then return FALSE. @param[in] Cert Pointer to the DER-encoded certificate data. @param[in] CertSize The size of certificate data in bytes. @@ -38,49 +38,36 @@ X509ConstructCertificate ( OUT UINT8 **SingleX509Cert ) { - BIO *CertBio; - X509 *X509Cert; - BOOLEAN Status; + X509 *X509Cert; + CONST UINT8 *Temp; // - // ASSERT if Cert is NULL or SingleX509Cert is NULL. + // Check input parameters. // - ASSERT (Cert != NULL); - ASSERT (SingleX509Cert != NULL); - - Status = FALSE; + if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) { + return FALSE; + } // // Read DER-encoded X509 Certificate and Construct X509 object. // - CertBio = BIO_new (BIO_s_mem ()); - BIO_write (CertBio, Cert, (int) CertSize); - if (CertBio == NULL) { - goto _Exit; - } - X509Cert = d2i_X509_bio (CertBio, NULL); + Temp = Cert; + X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize); if (X509Cert == NULL) { - goto _Exit; + return FALSE; } *SingleX509Cert = (UINT8 *) X509Cert; - Status = TRUE; - -_Exit: - // - // Release Resources. - // - BIO_free (CertBio); - return Status; + return TRUE; } /** Construct a X509 stack object from a list of DER-encoded certificate data. - If X509Stack is NULL, then ASSERT(). + If X509Stack is NULL, then return FALSE. - @param[in, out] X509Stack On input, pointer to an existing X509 stack object. + @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object. On output, pointer to the X509 stack object with new inserted X509 certificate. @param ... A list of DER-encoded single certificate data followed @@ -107,9 +94,11 @@ X509ConstructCertificateStack ( UINTN Index; // - // ASSERT if input X509Stack is NULL. + // Check input parameters. // - ASSERT (X509Stack != NULL); + if (X509Stack == NULL) { + return FALSE; + } Status = FALSE; @@ -136,17 +125,23 @@ X509ConstructCertificateStack ( } CertSize = VA_ARG (Args, UINTN); + if (CertSize == 0) { + break; + } // // Construct X509 Object from the given DER-encoded certificate data. // + X509Cert = NULL; Status = X509ConstructCertificate ( (CONST UINT8 *) Cert, CertSize, (UINT8 **) &X509Cert ); if (!Status) { - X509_free (X509Cert); + if (X509Cert != NULL) { + X509_free (X509Cert); + } break; } @@ -170,7 +165,7 @@ X509ConstructCertificateStack ( /** Release the specified X509 object. - If X509Cert is NULL, then ASSERT(). + If X509Cert is NULL, then return FALSE. @param[in] X509Cert Pointer to the X509 object to be released. @@ -180,9 +175,14 @@ EFIAPI X509Free ( IN VOID *X509Cert ) -{ - ASSERT (X509Cert != NULL); - +{ + // + // Check input parameters. + // + if (X509Cert == NULL) { + return; + } + // // Free OpenSSL X509 object. // @@ -192,7 +192,7 @@ X509Free ( /** Release the specified X509 stack object. - If X509Stack is NULL, then ASSERT(). + If X509Stack is NULL, then return FALSE. @param[in] X509Stack Pointer to the X509 stack object to be released. @@ -203,8 +203,13 @@ X509StackFree ( IN VOID *X509Stack ) { - ASSERT (X509Stack != NULL); - + // + // Check input parameters. + // + if (X509Stack == NULL) { + return; + } + // // Free OpenSSL X509 stack object. // @@ -220,8 +225,8 @@ X509StackFree ( @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input, and the size of buffer returned CertSubject on output. - If Cert is NULL, then ASSERT(). - If SubjectSize is NULL, then ASSERT(). + If Cert is NULL, then return FALSE. + If SubjectSize is NULL, then return FALSE. @retval TRUE The certificate subject retrieved successfully. @retval FALSE Invalid certificate, or the SubjectSize is too small for the result. @@ -242,12 +247,12 @@ X509GetSubjectName ( X509_NAME *X509Name; // - // ASSERT if Cert is NULL or SubjectSize is NULL. + // Check input parameters. // - ASSERT (Cert != NULL); - ASSERT (SubjectSize != NULL); + if (Cert == NULL || SubjectSize == NULL) { + return FALSE; + } - Status = FALSE; X509Cert = NULL; // @@ -255,20 +260,27 @@ X509GetSubjectName ( // Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert); if ((X509Cert == NULL) || (!Status)) { + Status = FALSE; goto _Exit; } + Status = FALSE; + // // Retrieve subject name from certificate object. // X509Name = X509_get_subject_name (X509Cert); + if (X509Name == NULL) { + goto _Exit; + } + if (*SubjectSize < (UINTN) X509Name->bytes->length) { *SubjectSize = (UINTN) X509Name->bytes->length; goto _Exit; } *SubjectSize = (UINTN) X509Name->bytes->length; if (CertSubject != NULL) { - CopyMem (CertSubject, (UINT8 *)X509Name->bytes->data, *SubjectSize); + CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize); Status = TRUE; } @@ -276,7 +288,9 @@ _Exit: // // Release Resources. // - X509_free (X509Cert); + if (X509Cert != NULL) { + X509_free (X509Cert); + } return Status; } @@ -290,8 +304,8 @@ _Exit: RSA public key component. Use RsaFree() function to free the resource. - If Cert is NULL, then ASSERT(). - If RsaContext is NULL, then ASSERT(). + If Cert is NULL, then return FALSE. + If RsaContext is NULL, then return FALSE. @retval TRUE RSA Public Key was retrieved successfully. @retval FALSE Fail to retrieve RSA public key from X509 certificate. @@ -308,14 +322,14 @@ RsaGetPublicKeyFromX509 ( BOOLEAN Status; EVP_PKEY *Pkey; X509 *X509Cert; - + // - // ASSERT if Cert is NULL or RsaContext is NULL. + // Check input parameters. // - ASSERT (Cert != NULL); - ASSERT (RsaContext != NULL); + if (Cert == NULL || RsaContext == NULL) { + return FALSE; + } - Status = FALSE; Pkey = NULL; X509Cert = NULL; @@ -324,9 +338,12 @@ RsaGetPublicKeyFromX509 ( // Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert); if ((X509Cert == NULL) || (!Status)) { + Status = FALSE; goto _Exit; } + Status = FALSE; + // // Retrieve and check EVP_PKEY data from X509 Certificate. // @@ -346,8 +363,13 @@ _Exit: // // Release Resources. // - X509_free (X509Cert); - EVP_PKEY_free (Pkey); + if (X509Cert != NULL) { + X509_free (X509Cert); + } + + if (Pkey != NULL) { + EVP_PKEY_free (Pkey); + } return Status; } @@ -360,8 +382,8 @@ _Exit: @param[in] CACert Pointer to the DER-encoded trusted CA certificate. @param[in] CACertSize Size of the CA Certificate in bytes. - If Cert is NULL, then ASSERT(). - If CACert is NULL, then ASSERT(). + If Cert is NULL, then return FALSE. + If CACert is NULL, then return FALSE. @retval TRUE The certificate was issued by the trusted CA. @retval FALSE Invalid certificate or the certificate was not issued by the given @@ -382,12 +404,13 @@ X509VerifyCert ( X509 *X509CACert; X509_STORE *CertStore; X509_STORE_CTX CertCtx; - + // - // ASSERT if Cert is NULL or CACert is NULL. + // Check input parameters. // - ASSERT (Cert != NULL); - ASSERT (CACert != NULL); + if (Cert == NULL || CACert == NULL) { + return FALSE; + } Status = FALSE; X509Cert = NULL; @@ -397,15 +420,22 @@ X509VerifyCert ( // // Register & Initialize necessary digest algorithms for certificate verification. // - EVP_add_digest (EVP_md5()); - EVP_add_digest (EVP_sha1()); - EVP_add_digest (EVP_sha256()); + if (EVP_add_digest (EVP_md5 ()) == 0) { + goto _Exit; + } + if (EVP_add_digest (EVP_sha1 ()) == 0) { + goto _Exit; + } + if (EVP_add_digest (EVP_sha256 ()) == 0) { + goto _Exit; + } // // Read DER-encoded certificate to be verified and Construct X509 object. // Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert); if ((X509Cert == NULL) || (!Status)) { + Status = FALSE; goto _Exit; } @@ -414,9 +444,12 @@ X509VerifyCert ( // Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert); if ((X509CACert == NULL) || (!Status)) { + Status = FALSE; goto _Exit; } + Status = FALSE; + // // Set up X509 Store for trusted certificate. // @@ -439,15 +472,100 @@ X509VerifyCert ( // X509 Certificate Verification. // Status = (BOOLEAN) X509_verify_cert (&CertCtx); + X509_STORE_CTX_cleanup (&CertCtx); _Exit: // // Release Resources. // - X509_free (X509Cert); - X509_free (X509CACert); - X509_STORE_free (CertStore); - X509_STORE_CTX_cleanup (&CertCtx); + if (X509Cert != NULL) { + X509_free (X509Cert); + } + if (X509CACert != NULL) { + X509_free (X509CACert); + } + + if (CertStore != NULL) { + X509_STORE_free (CertStore); + } + return Status; } + +/** + Retrieve the TBSCertificate from one given X.509 certificate. + + @param[in] Cert Pointer to the given DER-encoded X509 certificate. + @param[in] CertSize Size of the X509 certificate in bytes. + @param[out] TBSCert DER-Encoded To-Be-Signed certificate. + @param[out] TBSCertSize Size of the TBS certificate in bytes. + + If Cert is NULL, then return FALSE. + If TBSCert is NULL, then return FALSE. + If TBSCertSize is NULL, then return FALSE. + + @retval TRUE The TBSCertificate was retrieved successfully. + @retval FALSE Invalid X.509 certificate. + +**/ +BOOLEAN +EFIAPI +X509GetTBSCert ( + IN CONST UINT8 *Cert, + IN UINTN CertSize, + OUT UINT8 **TBSCert, + OUT UINTN *TBSCertSize + ) +{ + CONST UINT8 *Temp; + INTN Asn1Tag; + INTN ObjClass; + UINTN Length; + + // + // Check input parameters. + // + if ((Cert == NULL) || (TBSCert == NULL) || + (TBSCertSize == NULL) || (CertSize > INT_MAX)) { + return FALSE; + } + + // + // An X.509 Certificate is: (defined in RFC3280) + // Certificate ::= SEQUENCE { + // tbsCertificate TBSCertificate, + // signatureAlgorithm AlgorithmIdentifier, + // signature BIT STRING } + // + // and + // + // TBSCertificate ::= SEQUENCE { + // version [0] Version DEFAULT v1, + // ... + // } + // + // So we can just ASN1-parse the x.509 DER-encoded data. If we strip + // the first SEQUENCE, the second SEQUENCE is the TBSCertificate. + // + Temp = Cert; + ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize); + + if (Asn1Tag != V_ASN1_SEQUENCE) { + return FALSE; + } + + *TBSCert = (UINT8 *)Temp; + + ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length); + // + // Verify the parsed TBSCertificate is one correct SEQUENCE data. + // + if (Asn1Tag != V_ASN1_SEQUENCE) { + return FALSE; + } + + *TBSCertSize = Length + (Temp - *TBSCert); + + return TRUE; +}