X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptX509.c;h=f0a5d0ac7fbd931b0fc5a8a35f325e2c511894a2;hp=3a5485e002c7c4e60d1a8539359d2cd87fe458c8;hb=dda39f3a5850458391aaab330971d46bc9c2b690;hpb=d3945da6446cac381620340eced7c22b50d8ef44 diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c index 3a5485e002..f0a5d0ac7f 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 - 2012, 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 @@ -19,8 +19,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. /** 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. @@ -43,11 +43,11 @@ X509ConstructCertificate ( BOOLEAN Status; // - // ASSERT if Cert is NULL or SingleX509Cert is NULL. + // Check input parameters. // - ASSERT (Cert != NULL); - ASSERT (SingleX509Cert != NULL); - ASSERT (CertSize <= INT_MAX); + if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) { + return FALSE; + } Status = FALSE; @@ -79,7 +79,7 @@ _Exit: /** 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. On output, pointer to the X509 stack object with new @@ -108,9 +108,11 @@ X509ConstructCertificateStack ( UINTN Index; // - // ASSERT if input X509Stack is NULL. + // Check input parameters. // - ASSERT (X509Stack != NULL); + if (X509Stack == NULL) { + return FALSE; + } Status = FALSE; @@ -171,7 +173,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. @@ -181,9 +183,14 @@ EFIAPI X509Free ( IN VOID *X509Cert ) -{ - ASSERT (X509Cert != NULL); - +{ + // + // Check input parameters. + // + if (X509Cert == NULL) { + return; + } + // // Free OpenSSL X509 object. // @@ -193,7 +200,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. @@ -204,14 +211,104 @@ X509StackFree ( IN VOID *X509Stack ) { - ASSERT (X509Stack != NULL); - + // + // Check input parameters. + // + if (X509Stack == NULL) { + return; + } + // // Free OpenSSL X509 stack object. // sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free); } +/** + Pop single certificate from STACK_OF(X509). + + If X509Stack, Cert, or CertSize is NULL, then return FALSE. + + @param[in] X509Stack Pointer to a X509 stack object. + @param[out] Cert Pointer to a X509 certificate. + @param[out] CertSize Length of output X509 certificate in bytes. + + @retval TRUE The X509 stack pop succeeded. + @retval FALSE The pop operation failed. + +**/ +BOOLEAN +X509PopCertificate ( + IN VOID *X509Stack, + OUT UINT8 **Cert, + OUT UINTN *CertSize + ) +{ + BIO *CertBio; + X509 *X509Cert; + STACK_OF(X509) *CertStack; + BOOLEAN Status; + int Result; + int Length; + VOID *Buffer; + + Status = FALSE; + + if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) { + return Status; + } + + CertStack = (STACK_OF(X509) *) X509Stack; + + X509Cert = sk_X509_pop (CertStack); + + if (X509Cert == NULL) { + return Status; + } + + Buffer = NULL; + + CertBio = BIO_new (BIO_s_mem ()); + if (CertBio == NULL) { + return Status; + } + + Result = i2d_X509_bio (CertBio, X509Cert); + if (Result == 0) { + goto _Exit; + } + + Length = ((BUF_MEM *) CertBio->ptr)->length; + if (Length <= 0) { + goto _Exit; + } + + Buffer = malloc (Length); + if (Buffer == NULL) { + goto _Exit; + } + + Result = BIO_read (CertBio, Buffer, Length); + if (Result != Length) { + goto _Exit; + } + + *Cert = Buffer; + *CertSize = Length; + + Status = TRUE; + +_Exit: + + BIO_free (CertBio); + + if (!Status && (Buffer != NULL)) { + free (Buffer); + } + + return Status; +} + /** Retrieve the subject bytes from one X.509 certificate. @@ -221,8 +318,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. @@ -243,12 +340,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; // @@ -256,20 +353,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; } @@ -277,7 +381,9 @@ _Exit: // // Release Resources. // - X509_free (X509Cert); + if (X509Cert != NULL) { + X509_free (X509Cert); + } return Status; } @@ -291,8 +397,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. @@ -309,14 +415,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; @@ -325,9 +431,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. // @@ -347,8 +456,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; } @@ -361,8 +475,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 @@ -383,12 +497,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; @@ -398,15 +513,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; } @@ -415,9 +537,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. // @@ -446,9 +571,17 @@ _Exit: // // Release Resources. // - X509_free (X509Cert); - X509_free (X509CACert); - X509_STORE_free (CertStore); + if (X509Cert != NULL) { + X509_free (X509Cert); + } + if (X509CACert != NULL) { + X509_free (X509CACert); + } + + if (CertStore != NULL) { + X509_STORE_free (CertStore); + } + return Status; }