TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptPkcs7VerifyEku.c File Reference
#include <Base.h>
#include "InternalCryptLib.h"
#include <mbedtls/pkcs7.h>
#include <mbedtls/asn1write.h>

Go to the source code of this file.

Functions

STATIC BOOLEAN InternalX509FindExtensionData (UINT8 *Start, UINT8 *End, CONST UINT8 *Oid, UINTN OidSize, UINT8 **FindExtensionData, UINTN *FindExtensionDataLen)
 
STATIC BOOLEAN GetExtensionData (CONST mbedtls_x509_crt *Cert, CONST UINT8 *Oid, UINTN OidSize, UINT8 *ExtensionData, UINTN *ExtensionDataSize)
 
STATIC EFI_STATUS IsEkuInCertificate (IN CONST mbedtls_x509_crt *Cert, IN UINT8 *EKU, IN UINTN EkuLen)
 
VOID GetOidFromTxt (IN CONST CHAR8 *RequiredEKUs, IN UINTN RequiredEKUsSize, IN OUT UINT8 *CheckOid, OUT UINT8 *OidLen)
 
STATIC BOOLEAN IsCertSignerCert (UINT8 *Start, UINT8 *End)
 
STATIC EFI_STATUS CheckEKUs (IN CONST mbedtls_x509_crt *SignerCert, IN CONST CHAR8 *RequiredEKUs[], IN CONST UINT32 RequiredEKUsSize, IN BOOLEAN RequireAllPresent)
 
EFI_STATUS EFIAPI VerifyEKUsInPkcs7Signature (IN CONST UINT8 *Pkcs7Signature, IN CONST UINT32 SignatureSize, IN CONST CHAR8 *RequiredEKUs[], IN CONST UINT32 RequiredEKUsSize, IN BOOLEAN RequireAllPresent)
 

Variables

GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 EkuOID [] = { 0x55, 0x1D, 0x25 }
 
GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gBasicConstraintsCase1 [] = { 0x30, 0x00 }
 
GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gBasicConstraintsCase2 [] = { 0x30, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x00 }
 
GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gOidBasicConstraints [] = { 0x55, 0x1D, 0x13 }
 

Detailed Description

This module verifies that Enhanced Key Usages (EKU's) are present within a PKCS7 signature blob using MbedTLS.

Copyright (C) Microsoft Corporation. All Rights Reserved. Copyright (c) 2024, Intel Corporation. All rights reserved.

SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file CryptPkcs7VerifyEku.c.

Function Documentation

◆ CheckEKUs()

STATIC EFI_STATUS CheckEKUs ( IN CONST mbedtls_x509_crt *  SignerCert,
IN CONST CHAR8 *  RequiredEKUs[],
IN CONST UINT32  RequiredEKUsSize,
IN BOOLEAN  RequireAllPresent 
)

Determines if the specified EKUs are present in a signing certificate.

Parameters
[in]SignerCertThe certificate to check.
[in]RequiredEKUsThe EKUs to look for.
[in]RequiredEKUsSizeThe number of EKUs
[in]RequireAllPresentIf TRUE, then all the specified EKUs must be present in the certificate.
Return values
EFI_SUCCESSWe successfully identified the signing type.
EFI_INVALID_PARAMETERA parameter was invalid.
EFI_NOT_FOUNDOne or more EKU's were not found in the signature.

Definition at line 488 of file CryptPkcs7VerifyEku.c.

◆ GetExtensionData()

STATIC BOOLEAN GetExtensionData ( CONST mbedtls_x509_crt *  Cert,
CONST UINT8 *  Oid,
UINTN  OidSize,
UINT8 *  ExtensionData,
UINTN ExtensionDataSize 
)

Retrieve Extension data from one X.509 certificate.

Parameters
[in]CertPointer to the X509 certificate.
[in]OidObject identifier buffer
[in]OidSizeObject identifier buffer size
[out]ExtensionDataExtension bytes.
[in,out]ExtensionDataSizeExtension bytes size.
Return values
RETURN_SUCCESSThe certificate Extension data retrieved successfully.
RETURN_INVALID_PARAMETERIf Cert is NULL. If ExtensionDataSize is NULL. If ExtensionData is not NULL and *ExtensionDataSize is 0. If Certificate is invalid.
RETURN_NOT_FOUNDIf no Extension entry match Oid.
RETURN_BUFFER_TOO_SMALLIf the ExtensionData is NULL. The required buffer size is returned in the ExtensionDataSize parameter.
RETURN_UNSUPPORTEDThe operation is not supported.

Definition at line 151 of file CryptPkcs7VerifyEku.c.

◆ GetOidFromTxt()

VOID GetOidFromTxt ( IN CONST CHAR8 *  RequiredEKUs,
IN UINTN  RequiredEKUsSize,
IN OUT UINT8 *  CheckOid,
OUT UINT8 *  OidLen 
)

Get OID from txt.

Parameters
[in]RequiredEKUsArray of null-terminated strings listing OIDs of required EKUs that must be present in the signature.
[in]RequiredEKUsSizeNumber of elements in the RequiredEKUs string array.
[in,out]CheckOidOID.
[out]OidLenThe size of OID.

Definition at line 326 of file CryptPkcs7VerifyEku.c.

◆ InternalX509FindExtensionData()

STATIC BOOLEAN InternalX509FindExtensionData ( UINT8 *  Start,
UINT8 *  End,
CONST UINT8 *  Oid,
UINTN  OidSize,
UINT8 **  FindExtensionData,
UINTN FindExtensionDataLen 
)

Find first Extension data match with given OID

Parameters
[in]StartPointer to the DER-encoded extensions data
[in]Endextensions data size in bytes
[in]OidOID for match
[in]OidSizeOID size in bytes
[out]FindExtensionDataoutput matched extension data.
[out]FindExtensionDataLenmatched extension data size.

Definition at line 40 of file CryptPkcs7VerifyEku.c.

◆ IsCertSignerCert()

STATIC BOOLEAN IsCertSignerCert ( UINT8 *  Start,
UINT8 *  End 
)

Verify the Cert is signer cert

Parameters
[in]StartPointer to the DER-encoded certificate data Start.
[in]EndPointer to the DER-encoded certificate data End.
Return values
trueverify pass
falseverify fail

Definition at line 392 of file CryptPkcs7VerifyEku.c.

◆ IsEkuInCertificate()

STATIC EFI_STATUS IsEkuInCertificate ( IN CONST mbedtls_x509_crt *  Cert,
IN UINT8 *  EKU,
IN UINTN  EkuLen 
)

Determines if the specified EKU represented in ASN1 form is present in a given certificate.

Parameters
[in]CertThe certificate to check.
[in]EKUThe EKU to look for.
[in]EkuLenThe size of EKU.
Return values
EFI_SUCCESSWe successfully identified the signing type.
EFI_INVALID_PARAMETERA parameter was invalid.
EFI_NOT_FOUNDOne or more EKU's were not found in the signature.

Definition at line 240 of file CryptPkcs7VerifyEku.c.

◆ VerifyEKUsInPkcs7Signature()

EFI_STATUS EFIAPI VerifyEKUsInPkcs7Signature ( IN CONST UINT8 *  Pkcs7Signature,
IN CONST UINT32  SignatureSize,
IN CONST CHAR8 *  RequiredEKUs[],
IN CONST UINT32  RequiredEKUsSize,
IN BOOLEAN  RequireAllPresent 
)

This function receives a PKCS#7 formatted signature blob, looks for the EKU SEQUENCE blob, and if found then looks for all the required EKUs. This function was created so that the Surface team can cut down on the number of Certificate Authorities (CA's) by checking EKU's on leaf signers for a specific product. This prevents one product's certificate from signing another product's firmware or unlock blobs.

Note that this function does not validate the certificate chain. That needs to be done before using this function.

Parameters
[in]Pkcs7SignatureThe PKCS#7 signed information content block. An array containing the content block with both the signature, the signer's certificate, and any necessary intermediate certificates.
[in]Pkcs7SignatureSizeNumber of bytes in Pkcs7Signature.
[in]RequiredEKUsArray of null-terminated strings listing OIDs of required EKUs that must be present in the signature.
[in]RequiredEKUsSizeNumber of elements in the RequiredEKUs string array.
[in]RequireAllPresentIf this is TRUE, then all of the specified EKU's must be present in the leaf signer. If it is FALSE, then we will succeed if we find any of the specified EKU's.
Return values
EFI_SUCCESSThe required EKUs were found in the signature.
EFI_INVALID_PARAMETERA parameter was invalid.
EFI_NOT_FOUNDOne or more EKU's were not found in the signature.

Definition at line 581 of file CryptPkcs7VerifyEku.c.

Variable Documentation

◆ EkuOID

GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 EkuOID[] = { 0x55, 0x1D, 0x25 }

Definition at line 17 of file CryptPkcs7VerifyEku.c.

◆ gBasicConstraintsCase1

GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gBasicConstraintsCase1[] = { 0x30, 0x00 }

Definition at line 20 of file CryptPkcs7VerifyEku.c.

◆ gBasicConstraintsCase2

GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gBasicConstraintsCase2[] = { 0x30, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x00 }

Definition at line 23 of file CryptPkcs7VerifyEku.c.

◆ gOidBasicConstraints

GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 gOidBasicConstraints[] = { 0x55, 0x1D, 0x13 }

Definition at line 25 of file CryptPkcs7VerifyEku.c.