summaryrefslogtreecommitdiff
path: root/Cryptlib/Pk/CryptPkcs7Verify.c
diff options
context:
space:
mode:
Diffstat (limited to 'Cryptlib/Pk/CryptPkcs7Verify.c')
-rw-r--r--Cryptlib/Pk/CryptPkcs7Verify.c125
1 files changed, 89 insertions, 36 deletions
diff --git a/Cryptlib/Pk/CryptPkcs7Verify.c b/Cryptlib/Pk/CryptPkcs7Verify.c
index efa3796a..cbd9669a 100644
--- a/Cryptlib/Pk/CryptPkcs7Verify.c
+++ b/Cryptlib/Pk/CryptPkcs7Verify.c
@@ -10,7 +10,7 @@
WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated
Variable and will do basic check for data structure.
-Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
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
@@ -30,6 +30,43 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
+BOOLEAN ca_warning;
+
+void
+clear_ca_warning()
+{
+ ca_warning = FALSE;
+}
+
+BOOLEAN
+get_ca_warning()
+{
+ return ca_warning;
+}
+
+int
+X509VerifyCb (
+ IN int Status,
+ IN X509_STORE_CTX *Context
+ )
+{
+ INTN Error;
+
+ Error = (INTN) X509_STORE_CTX_get_error (Context);
+
+ if (Error == X509_V_ERR_INVALID_CA) {
+ /* Due to the historical reason, we have to relax the the x509 v3 extension
+ * check to allow the CA certificates without the CA flag in the basic
+ * constraints or KeyCertSign in the key usage to be loaded. In the future,
+ * this callback should be removed to enforce the proper check. */
+ ca_warning = TRUE;
+
+ return 1;
+ }
+
+ return Status;
+}
+
/**
Check input P7Data is a wrapped ContentInfo structure or not. If not construct
a new structure to wrap P7Data.
@@ -163,6 +200,7 @@ X509PopCertificate (
STACK_OF(X509) *CertStack;
BOOLEAN Status;
INT32 Result;
+ BUF_MEM *Ptr;
INT32 Length;
VOID *Buffer;
@@ -192,7 +230,8 @@ X509PopCertificate (
goto _Exit;
}
- Length = (INT32)(((BUF_MEM *) CertBio->ptr)->length);
+ BIO_get_mem_ptr (CertBio, &Ptr);
+ Length = (INT32)(Ptr->length);
if (Length <= 0) {
goto _Exit;
}
@@ -229,7 +268,7 @@ _Exit:
in a ContentInfo structure.
If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
- return FALSE. If P7Length overflow, then return FAlSE.
+ return FALSE. If P7Length overflow, then return FALSE.
Caution: This function may receive untrusted input.
UEFI Authenticated Variable is external input, so this function will do basic
@@ -238,10 +277,10 @@ _Exit:
@param[in] P7Data Pointer to the PKCS#7 message to verify.
@param[in] P7Length Length of the PKCS#7 message in bytes.
@param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
- It's caller's responsiblity to free the buffer.
+ It's caller's responsibility to free the buffer.
@param[out] StackLength Length of signer's certificates in bytes.
@param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
- It's caller's responsiblity to free the buffer.
+ It's caller's responsibility to free the buffer.
@param[out] CertLength Length of the trusted certificate in bytes.
@retval TRUE The operation is finished successfully.
@@ -436,10 +475,10 @@ Pkcs7FreeSigners (
@param[in] P7Data Pointer to the PKCS#7 message.
@param[in] P7Length Length of the PKCS#7 message in bytes.
@param[out] SignerChainCerts Pointer to the certificates list chained to signer's
- certificate. It's caller's responsiblity to free the buffer.
+ certificate. It's caller's responsibility to free the buffer.
@param[out] ChainLength Length of the chained certificates list buffer in bytes.
@param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
- responsiblity to free the buffer.
+ responsibility to free the buffer.
@param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
@retval TRUE The operation is finished successfully.
@@ -463,12 +502,15 @@ Pkcs7GetCertificatesList (
BOOLEAN Wrapped;
UINT8 Index;
PKCS7 *Pkcs7;
- X509_STORE_CTX CertCtx;
+ X509_STORE_CTX *CertCtx;
+ STACK_OF(X509) *CtxChain;
+ STACK_OF(X509) *CtxUntrusted;
+ X509 *CtxCert;
STACK_OF(X509) *Signers;
X509 *Signer;
X509 *Cert;
- X509 *TempCert;
X509 *Issuer;
+ X509_NAME *IssuerName;
UINT8 *CertBuf;
UINT8 *OldBuf;
UINTN BufferSize;
@@ -482,8 +524,11 @@ Pkcs7GetCertificatesList (
Status = FALSE;
NewP7Data = NULL;
Pkcs7 = NULL;
+ CertCtx = NULL;
+ CtxChain = NULL;
+ CtxCert = NULL;
+ CtxUntrusted = NULL;
Cert = NULL;
- TempCert = NULL;
SingleCert = NULL;
CertBuf = NULL;
OldBuf = NULL;
@@ -531,19 +576,26 @@ Pkcs7GetCertificatesList (
}
Signer = sk_X509_value (Signers, 0);
- if (!X509_STORE_CTX_init (&CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {
+ CertCtx = X509_STORE_CTX_new ();
+ if (CertCtx == NULL) {
+ goto _Error;
+ }
+ if (!X509_STORE_CTX_init (CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {
goto _Error;
}
//
// Initialize Chained & Untrusted stack
//
- if (CertCtx.chain == NULL) {
- if (((CertCtx.chain = sk_X509_new_null ()) == NULL) ||
- (!sk_X509_push (CertCtx.chain, CertCtx.cert))) {
+ CtxChain = X509_STORE_CTX_get0_chain (CertCtx);
+ CtxCert = X509_STORE_CTX_get0_cert (CertCtx);
+ if (CtxChain == NULL) {
+ if (((CtxChain = sk_X509_new_null ()) == NULL) ||
+ (!sk_X509_push (CtxChain, CtxCert))) {
goto _Error;
}
}
- (VOID)sk_X509_delete_ptr (CertCtx.untrusted, Signer);
+ CtxUntrusted = X509_STORE_CTX_get0_untrusted (CertCtx);
+ (VOID)sk_X509_delete_ptr (CtxUntrusted, Signer);
//
// Build certificates stack chained from Signer's certificate.
@@ -553,27 +605,25 @@ Pkcs7GetCertificatesList (
//
// Self-Issue checking
//
- if (CertCtx.check_issued (&CertCtx, Cert, Cert)) {
- break;
+ Issuer = NULL;
+ if (X509_STORE_CTX_get1_issuer (&Issuer, CertCtx, Cert) == 1) {
+ if (X509_cmp (Issuer, Cert) == 0) {
+ break;
+ }
}
//
// Found the issuer of the current certificate
//
- if (CertCtx.untrusted != NULL) {
+ if (CtxUntrusted != NULL) {
Issuer = NULL;
- for (Index = 0; Index < sk_X509_num (CertCtx.untrusted); Index++) {
- TempCert = sk_X509_value (CertCtx.untrusted, Index);
- if (CertCtx.check_issued (&CertCtx, Cert, TempCert)) {
- Issuer = TempCert;
- break;
- }
- }
+ IssuerName = X509_get_issuer_name (Cert);
+ Issuer = X509_find_by_subject (CtxUntrusted, IssuerName);
if (Issuer != NULL) {
- if (!sk_X509_push (CertCtx.chain, Issuer)) {
+ if (!sk_X509_push (CtxChain, Issuer)) {
goto _Error;
}
- (VOID)sk_X509_delete_ptr (CertCtx.untrusted, Issuer);
+ (VOID)sk_X509_delete_ptr (CtxUntrusted, Issuer);
Cert = Issuer;
continue;
@@ -595,13 +645,13 @@ Pkcs7GetCertificatesList (
// UINT8 Certn[];
//
- if (CertCtx.chain != NULL) {
+ if (CtxChain != NULL) {
BufferSize = sizeof (UINT8);
OldSize = BufferSize;
CertBuf = NULL;
for (Index = 0; ; Index++) {
- Status = X509PopCertificate (CertCtx.chain, &SingleCert, &CertSize);
+ Status = X509PopCertificate (CtxChain, &SingleCert, &CertSize);
if (!Status) {
break;
}
@@ -639,13 +689,13 @@ Pkcs7GetCertificatesList (
}
}
- if (CertCtx.untrusted != NULL) {
+ if (CtxUntrusted != NULL) {
BufferSize = sizeof (UINT8);
OldSize = BufferSize;
CertBuf = NULL;
for (Index = 0; ; Index++) {
- Status = X509PopCertificate (CertCtx.untrusted, &SingleCert, &CertSize);
+ Status = X509PopCertificate (CtxUntrusted, &SingleCert, &CertSize);
if (!Status) {
break;
}
@@ -698,7 +748,8 @@ _Error:
}
sk_X509_free (Signers);
- X509_STORE_CTX_cleanup (&CertCtx);
+ X509_STORE_CTX_cleanup (CertCtx);
+ X509_STORE_CTX_free (CertCtx);
if (SingleCert != NULL) {
free (SingleCert);
@@ -718,12 +769,12 @@ _Error:
}
/**
- Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:
+ Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
Cryptographic Message Syntax Standard". The input signed data could be wrapped
in a ContentInfo structure.
If P7Data, TrustedCert or InData is NULL, then return FALSE.
- If P7Length, CertLength or DataLength overflow, then return FAlSE.
+ If P7Length, CertLength or DataLength overflow, then return FALSE.
Caution: This function may receive untrusted input.
UEFI Authenticated Variable is external input, so this function will do basic
@@ -844,6 +895,8 @@ Pkcs7Verify (
goto _Exit;
}
+ X509_STORE_set_verify_cb (CertStore, X509VerifyCb);
+
//
// For generic PKCS#7 handling, InData may be NULL if the content is present
// in PKCS#7 structure. So ignore NULL checking here.
@@ -897,7 +950,7 @@ _Exit:
data could be wrapped in a ContentInfo structure.
If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
- then return FAlSE. If the P7Data is not correctly formatted, then return FALSE.
+ then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
Caution: This function may receive untrusted input. So this function will do
basic check for PKCS#7 data structure.
@@ -905,7 +958,7 @@ _Exit:
@param[in] P7Data Pointer to the PKCS#7 signed data to process.
@param[in] P7Length Length of the PKCS#7 signed data in bytes.
@param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
- It's caller's responsiblity to free the buffer.
+ It's caller's responsibility to free the buffer.
@param[out] ContentSize The size of the extracted content in bytes.
@retval TRUE The P7Data was correctly formatted for processing.