13#include "InternalCryptLib.h"
14#include <mbedtls/pkcs7.h>
15#include <mbedtls/asn1write.h>
45 UINT8 **FindExtensionData,
46 UINTN *FindExtensionDataLen
54 UINTN FindExtensionLen;
71 Ret = mbedtls_asn1_get_tag (
75 MBEDTLS_ASN1_CONSTRUCTED |
79 HeaderLen = (
UINTN)(Ptr - ExtensionPtr);
80 FindExtensionLen = ObjLen;
82 Ret = mbedtls_asn1_get_tag (
92 if ((Ret == 0) && !
CompareMem (Ptr, Oid, OidSize)) {
95 Ret = mbedtls_asn1_get_tag (
105 Ret = mbedtls_asn1_get_tag (
109 MBEDTLS_ASN1_OCTET_STRING
116 *FindExtensionData = Ptr;
117 *FindExtensionDataLen = ObjLen;
123 Ptr = ExtensionPtr + HeaderLen + FindExtensionLen;
152 CONST mbedtls_x509_crt *Cert,
155 UINT8 *ExtensionData,
156 UINTN *ExtensionDataSize
159 CONST mbedtls_x509_crt *Crt;
170 if ((Cert ==
NULL) || (Oid ==
NULL) || (OidSize == 0) ||
171 (ExtensionDataSize ==
NULL))
181 End = Crt->v3_ext.p + Crt->v3_ext.len;
182 Ret = mbedtls_asn1_get_tag (
186 MBEDTLS_ASN1_CONSTRUCTED |
187 MBEDTLS_ASN1_SEQUENCE
202 if (*ExtensionDataSize < ObjLen) {
203 *ExtensionDataSize = ObjLen;
209 if (ExtensionData ==
NULL) {
213 CopyMem (ExtensionData, Ptr, ObjLen);
216 *ExtensionDataSize = ObjLen;
218 *ExtensionDataSize = 0;
241 IN CONST mbedtls_x509_crt *Cert,
252 if ((Cert ==
NULL) || (EKU ==
NULL)) {
253 Status = EFI_INVALID_PARAMETER;
261 (
CONST UINT8 *)EkuOID,
267 Status = EFI_NOT_FOUND;
272 if (Buffer ==
NULL) {
273 Status = EFI_NOT_FOUND;
279 (
CONST UINT8 *)EkuOID,
285 if ((Len == 0) || (!Ret)) {
286 Status = EFI_NOT_FOUND;
290 Status = EFI_NOT_FOUND;
291 for (Index = 0; Index <= Len - EkuLen; Index++) {
292 if (!
CompareMem (Buffer + Index, EKU, EkuLen)) {
294 if (Index == Len - EkuLen) {
298 }
else if (Buffer[Index + EkuLen] == 0x06) {
308 if (Buffer !=
NULL) {
329 IN OUT UINT8 *CheckOid,
339 EKUsSize = RequiredEKUsSize;
341 CheckOid[0] = (UINT8)((RequiredEKUs[0] -
'0') * 40 + (RequiredEKUs[2] -
'0'));
343 EKUsSize = EKUsSize - 4;
344 Ptr = (UINT8 *)(RequiredEKUs + 4);
352 while ((*Ptr !=
'.') && (*Ptr !=
'\0')) {
359 Data = 10 * Data + (*(Ptr - Index) -
'0');
369 CheckOid[OidIndex] = (UINT8)Data;
372 CheckOid[OidIndex + 1] = (UINT8)(Data & 0xFF);
373 CheckOid[OidIndex] = (UINT8)(((((Data & 0xFF00) << 1) | 0x8000) >> 8) & 0xFF);
374 OidIndex = OidIndex + 2;
400 mbedtls_x509_crt Cert;
403 mbedtls_x509_crt_init (&Cert);
405 ObjLen = End - Start;
407 if (mbedtls_x509_crt_parse_der (&Cert, Start, ObjLen) != 0) {
415 (
CONST UINT8 *)gOidBasicConstraints,
416 sizeof (gOidBasicConstraints),
426 if (Buffer ==
NULL) {
432 (
CONST UINT8 *)gOidBasicConstraints,
433 sizeof (gOidBasicConstraints),
442 }
else if (!Status) {
447 if ((Len ==
sizeof (gBasicConstraintsCase1)) &&
448 (!
CompareMem (Buffer, gBasicConstraintsCase1,
sizeof (gBasicConstraintsCase1))))
454 if ((Len ==
sizeof (gBasicConstraintsCase2)) &&
455 (!
CompareMem (Buffer, gBasicConstraintsCase2,
sizeof (gBasicConstraintsCase2))))
464 mbedtls_x509_crt_free (&Cert);
466 if (Buffer !=
NULL) {
489 IN CONST mbedtls_x509_crt *SignerCert,
490 IN CONST CHAR8 *RequiredEKUs[],
491 IN CONST UINT32 RequiredEKUsSize,
492 IN BOOLEAN RequireAllPresent
506 if ((SignerCert ==
NULL) || (RequiredEKUs ==
NULL) || (RequiredEKUsSize == 0)) {
507 Status = EFI_INVALID_PARAMETER;
511 for (Index = 0; Index < RequiredEKUsSize; Index++) {
515 GetOidFromTxt (RequiredEKUs[Index], strlen (RequiredEKUs[Index]), CheckOid, &OidLen);
523 if (!RequireAllPresent) {
537 if (RequireAllPresent &&
538 (NumEkusFound == RequiredEKUsSize))
582 IN CONST UINT8 *Pkcs7Signature,
584 IN CONST CHAR8 *RequiredEKUs[],
585 IN CONST UINT32 RequiredEKUsSize,
586 IN BOOLEAN RequireAllPresent
590 mbedtls_x509_crt Cert;
600 if ((RequiredEKUs ==
NULL) || (Pkcs7Signature ==
NULL)) {
601 Status = EFI_INVALID_PARAMETER;
605 mbedtls_x509_crt_init (&Cert);
607 Ptr = (UINT8 *)(
UINTN)Pkcs7Signature;
608 Len = (UINT32)SignatureSize;
612 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
617 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
623 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET) != 0) {
629 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
636 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) != 0) {
643 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
649 while ((Ptr != End) && (Ptr < End)) {
655 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
669 ObjLen += Ptr - OldEnd;
672 if (mbedtls_x509_crt_parse_der (&Cert, Ptr, ObjLen) != 0) {
676 Status =
CheckEKUs (&Cert, RequiredEKUs, RequiredEKUsSize, RequireAllPresent);
685 mbedtls_x509_crt_free (&Cert);
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define GLOBAL_REMOVE_IF_UNREFERENCED
STATIC EFI_STATUS CheckEKUs(IN CONST X509 *SignerCert, IN CONST CHAR8 *RequiredEKUs[], IN CONST UINT32 RequiredEKUsSize, IN BOOLEAN RequireAllPresent)
STATIC EFI_STATUS IsEkuInCertificate(IN CONST X509 *Cert, IN ASN1_OBJECT *Asn1ToFind)
EFI_STATUS EFIAPI VerifyEKUsInPkcs7Signature(IN CONST UINT8 *Pkcs7Signature, IN CONST UINT32 SignatureSize, IN CONST CHAR8 *RequiredEKUs[], IN CONST UINT32 RequiredEKUsSize, IN BOOLEAN RequireAllPresent)
VOID EFIAPI Exit(IN EFI_STATUS Status)
STATIC BOOLEAN InternalX509FindExtensionData(UINT8 *Start, UINT8 *End, CONST UINT8 *Oid, UINTN OidSize, UINT8 **FindExtensionData, UINTN *FindExtensionDataLen)
STATIC BOOLEAN IsCertSignerCert(UINT8 *Start, UINT8 *End)
STATIC BOOLEAN GetExtensionData(CONST mbedtls_x509_crt *Cert, CONST UINT8 *Oid, UINTN OidSize, UINT8 *ExtensionData, UINTN *ExtensionDataSize)
VOID GetOidFromTxt(IN CONST CHAR8 *RequiredEKUs, IN UINTN RequiredEKUsSize, IN OUT UINT8 *CheckOid, OUT UINT8 *OidLen)