X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptX509.c;h=bf7c4ccd42feb8bb0ca283394d3be9018c5d0ebc;hp=5abe970cceac130f02b295855258a5088a68d246;hb=5b7c22450591a9e20ff54b970c11087ccfff563d;hpb=02ee8d3b4cebb319ff1747f9bdc3f6b473d63f3e diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c index 5abe970cce..bf7c4ccd42 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 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2017, 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,7 +14,7 @@ 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. @@ -38,7 +38,8 @@ X509ConstructCertificate ( OUT UINT8 **SingleX509Cert ) { - X509 *X509Cert; + X509 *X509Cert; + CONST UINT8 *Temp; // // Check input parameters. @@ -50,7 +51,8 @@ X509ConstructCertificate ( // // Read DER-encoded X509 Certificate and Construct X509 object. // - X509Cert = d2i_X509 (NULL, &Cert, (long) CertSize); + Temp = Cert; + X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize); if (X509Cert == NULL) { return FALSE; } @@ -65,13 +67,13 @@ X509ConstructCertificate ( 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 by certificate size. A NULL terminates the list. The pairs are the arguments to X509ConstructCertificate(). - + @retval TRUE The X509 stack construction succeeded. @retval FALSE The construction operation failed. @@ -80,7 +82,7 @@ BOOLEAN EFIAPI X509ConstructCertificateStack ( IN OUT UINT8 **X509Stack, - ... + ... ) { UINT8 *Cert; @@ -123,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; } @@ -167,14 +175,14 @@ EFIAPI X509Free ( IN VOID *X509Cert ) -{ +{ // // Check input parameters. // if (X509Cert == NULL) { return; } - + // // Free OpenSSL X509 object. // @@ -201,7 +209,7 @@ X509StackFree ( if (X509Stack == NULL) { return; } - + // // Free OpenSSL X509 stack object. // @@ -237,6 +245,7 @@ X509GetSubjectName ( BOOLEAN Status; X509 *X509Cert; X509_NAME *X509Name; + UINTN X509NameSize; // // Check input parameters. @@ -266,13 +275,14 @@ X509GetSubjectName ( goto _Exit; } - if (*SubjectSize < (UINTN) X509Name->bytes->length) { - *SubjectSize = (UINTN) X509Name->bytes->length; + X509NameSize = i2d_X509_NAME(X509Name, NULL); + if (*SubjectSize < X509NameSize) { + *SubjectSize = X509NameSize; goto _Exit; } - *SubjectSize = (UINTN) X509Name->bytes->length; + *SubjectSize = X509NameSize; if (CertSubject != NULL) { - CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize); + i2d_X509_NAME(X509Name, &CertSubject); Status = TRUE; } @@ -287,6 +297,115 @@ _Exit: return Status; } +/** + Retrieve the common name (CN) string from one X.509 certificate. + + @param[in] Cert Pointer to the DER-encoded X509 certificate. + @param[in] CertSize Size of the X509 certificate in bytes. + @param[out] CommonName Buffer to contain the retrieved certificate common + name string. At most CommonNameSize bytes will be + written and the string will be null terminated. May be + NULL in order to determine the size buffer needed. + @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input, + and the size of buffer returned CommonName on output. + If CommonName is NULL then the amount of space needed + in buffer (including the final null) is returned. + + @retval RETURN_SUCCESS The certificate CommonName retrieved successfully. + @retval RETURN_INVALID_PARAMETER If Cert is NULL. + If CommonNameSize is NULL. + If CommonName is not NULL and *CommonNameSize is 0. + If Certificate is invalid. + @retval RETURN_NOT_FOUND If no CommonName entry exists. + @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size + (including the final null) is returned in the + CommonNameSize parameter. + @retval RETURN_UNSUPPORTED The operation is not supported. + +**/ +RETURN_STATUS +EFIAPI +X509GetCommonName ( + IN CONST UINT8 *Cert, + IN UINTN CertSize, + OUT CHAR8 *CommonName, OPTIONAL + IN OUT UINTN *CommonNameSize + ) +{ + RETURN_STATUS ReturnStatus; + BOOLEAN Status; + X509 *X509Cert; + X509_NAME *X509Name; + INTN Length; + + ReturnStatus = RETURN_INVALID_PARAMETER; + + // + // Check input parameters. + // + if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) { + return ReturnStatus; + } + if ((CommonName != NULL) && (*CommonNameSize == 0)) { + return ReturnStatus; + } + + X509Cert = NULL; + // + // Read DER-encoded X509 Certificate and Construct X509 object. + // + Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert); + if ((X509Cert == NULL) || (!Status)) { + // + // Invalid X.509 Certificate + // + goto _Exit; + } + + Status = FALSE; + + // + // Retrieve subject name from certificate object. + // + X509Name = X509_get_subject_name (X509Cert); + if (X509Name == NULL) { + // + // Fail to retrieve subject name content + // + goto _Exit; + } + + // + // Retrieve the CommonName information from X.509 Subject + // + Length = (INTN) X509_NAME_get_text_by_NID (X509Name, NID_commonName, CommonName, (int)(*CommonNameSize)); + if (Length < 0) { + // + // No CommonName entry exists in X509_NAME object + // + *CommonNameSize = 0; + ReturnStatus = RETURN_NOT_FOUND; + goto _Exit; + } + + *CommonNameSize = (UINTN)(Length + 1); + if (CommonName == NULL) { + ReturnStatus = RETURN_BUFFER_TOO_SMALL; + } else { + ReturnStatus = RETURN_SUCCESS; + } + +_Exit: + // + // Release Resources. + // + if (X509Cert != NULL) { + X509_free (X509Cert); + } + + return ReturnStatus; +} + /** Retrieve the RSA Public Key from one DER-encoded X509 certificate. @@ -314,7 +433,7 @@ RsaGetPublicKeyFromX509 ( BOOLEAN Status; EVP_PKEY *Pkey; X509 *X509Cert; - + // // Check input parameters. // @@ -340,14 +459,14 @@ RsaGetPublicKeyFromX509 ( // Retrieve and check EVP_PKEY data from X509 Certificate. // Pkey = X509_get_pubkey (X509Cert); - if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) { + if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) { goto _Exit; } // // Duplicate RSA Context from the retrieved EVP_PKEY. // - if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) { + if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) { Status = TRUE; } @@ -361,7 +480,7 @@ _Exit: if (Pkey != NULL) { EVP_PKEY_free (Pkey); - } + } return Status; } @@ -395,8 +514,8 @@ X509VerifyCert ( X509 *X509Cert; X509 *X509CACert; X509_STORE *CertStore; - X509_STORE_CTX CertCtx; - + X509_STORE_CTX *CertCtx; + // // Check input parameters. // @@ -408,6 +527,7 @@ X509VerifyCert ( X509Cert = NULL; X509CACert = NULL; CertStore = NULL; + CertCtx = NULL; // // Register & Initialize necessary digest algorithms for certificate verification. @@ -453,18 +573,29 @@ X509VerifyCert ( goto _Exit; } + // + // Allow partial certificate chains, terminated by a non-self-signed but + // still trusted intermediate certificate. Also disable time checks. + // + X509_STORE_set_flags (CertStore, + X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME); + // // Set up X509_STORE_CTX for the subsequent verification operation. // - if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) { + CertCtx = X509_STORE_CTX_new (); + if (CertCtx == NULL) { + goto _Exit; + } + if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) { goto _Exit; } // // X509 Certificate Verification. // - Status = (BOOLEAN) X509_verify_cert (&CertCtx); - X509_STORE_CTX_cleanup (&CertCtx); + Status = (BOOLEAN) X509_verify_cert (CertCtx); + X509_STORE_CTX_cleanup (CertCtx); _Exit: // @@ -481,6 +612,85 @@ _Exit: if (CertStore != NULL) { X509_STORE_free (CertStore); } - + + X509_STORE_CTX_free (CertCtx); + 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; +}