14#include <mbedtls/pkcs7.h>
18STATIC mbedtls_x509_crt_profile gCompatProfile =
23 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
24 MBEDTLS_X509_ID_FLAG (MBEDTLS_MD_SHA1) |
26 MBEDTLS_X509_ID_FLAG (MBEDTLS_MD_SHA256) |
27 MBEDTLS_X509_ID_FLAG (MBEDTLS_MD_SHA384) |
28 MBEDTLS_X509_ID_FLAG (MBEDTLS_MD_SHA512),
33 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_SECP256R1) |
34 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_SECP384R1) |
35 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_SECP521R1) |
36 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_BP256R1) |
37 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_BP384R1) |
38 MBEDTLS_X509_ID_FLAG (MBEDTLS_ECP_DP_BP512R1) |
77 Ret = mbedtls_asn1_get_tag (Ptr, End, Len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC);
101 Ret = mbedtls_asn1_get_int (Ptr, End, Ver);
123 mbedtls_asn1_buf *Pkcs7
130 Ret = mbedtls_asn1_get_tag (
134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
138 Ret = mbedtls_asn1_get_tag (Ptr, End, &Len, MBEDTLS_ASN1_OID);
142 Pkcs7->tag = MBEDTLS_ASN1_OID;
165 mbedtls_x509_buf *Alg
170 Ret = mbedtls_asn1_get_alg_null (Ptr, End, Alg);
189 mbedtls_x509_buf *Alg
196 Ret = mbedtls_asn1_get_tag (
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET
206 Ret = mbedtls_asn1_get_alg_null (Ptr, End, Alg);
230 mbedtls_x509_crt *Certs
235 Ret = mbedtls_x509_crt_parse (Certs, *Ptr, Plen);
254 mbedtls_asn1_buf *Signature
261 Ret = mbedtls_asn1_get_tag (Ptr, End, &Len, MBEDTLS_ASN1_OCTET_STRING);
263 Signature->tag = MBEDTLS_ASN1_OCTET_STRING;
264 Signature->len = Len;
305 Ret = mbedtls_asn1_get_tag (
309 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET
315 Ret = mbedtls_asn1_get_tag (
319 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
324 Ret = mbedtls_asn1_get_int (Ptr, EndSet, &SignersSet->Version);
328 Ret = mbedtls_asn1_get_tag (
332 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
337 SignersSet->IssuerRaw.p = *Ptr;
338 Ret = mbedtls_asn1_get_tag (
342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
347 Ret = mbedtls_x509_get_name (Ptr, *Ptr + Len, &SignersSet->Issuer);
351 SignersSet->IssuerRaw.len = *Ptr - SignersSet->IssuerRaw.p;
353 Ret = mbedtls_x509_get_serial (Ptr, EndSet, &SignersSet->Serial);
363 if (mbedtls_asn1_get_tag (&TempP, EndSet, &Len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) == 0) {
364 SignersSet->AuthAttr.len = Len + (TempP - *Ptr);
365 SignersSet->AuthAttr.p = *Ptr;
368 SignersSet->AuthAttr.p =
NULL;
381 SignersSet->Next =
NULL;
422 mbedtls_x509_crt *MoreCert;
424 mbedtls_x509_crt *LastCert;
425 mbedtls_x509_crt *TempCrt;
429 End = Buffer + BufferLen;
432 Ret = mbedtls_asn1_get_tag (
436 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
444 if ((Ret == 0) && (SignedData->Version != 1)) {
453 &SignedData->DigestAlgorithms
459 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
460 ((SignedData->DigestAlgorithms.len == sizeof (MBEDTLS_OID_DIGEST_ALG_SHA1) - 1) &&
462 SignedData->DigestAlgorithms.p,
463 MBEDTLS_OID_DIGEST_ALG_SHA1,
464 SignedData->DigestAlgorithms.len
467 ((SignedData->DigestAlgorithms.len == sizeof (MBEDTLS_OID_DIGEST_ALG_SHA256) - 1) &&
469 SignedData->DigestAlgorithms.p,
470 MBEDTLS_OID_DIGEST_ALG_SHA256,
471 SignedData->DigestAlgorithms.len
473 ((SignedData->DigestAlgorithms.len == sizeof (MBEDTLS_OID_DIGEST_ALG_SHA384) - 1) &&
475 SignedData->DigestAlgorithms.p,
476 MBEDTLS_OID_DIGEST_ALG_SHA384,
477 SignedData->DigestAlgorithms.len
479 ((SignedData->DigestAlgorithms.len == sizeof (MBEDTLS_OID_DIGEST_ALG_SHA512) - 1) &&
481 SignedData->DigestAlgorithms.p,
482 MBEDTLS_OID_DIGEST_ALG_SHA512,
483 SignedData->DigestAlgorithms.len
498 Ptr = Ptr + SignedData->ContentInfo.Oid.len;
514 MoreCert = &SignedData->Certificates;
517 while (TotalCertLen < Len) {
520 Ret = mbedtls_asn1_get_tag (&CertP, End, &CertLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
526 CertLen = CertLen + (CertP - OldCertP);
529 CertP = OldCertP + CertLen;
532 TotalCertLen += CertLen;
534 mbedtls_x509_crt_init (MoreCert);
541 MoreCert->next = mbedtls_calloc (1,
sizeof (mbedtls_x509_crt));
542 MoreCert = MoreCert->next;
545 if (TotalCertLen != Len) {
550 LastCert = &(SignedData->Certificates);
554 LastCert->next =
NULL;
557 LastCert = LastCert->next;
569 if (MoreCert !=
NULL) {
570 mbedtls_x509_crt_free (MoreCert);
574 if (SignedData->Certificates.next !=
NULL) {
575 TempCrt = SignedData->Certificates.next;
576 mbedtls_x509_crt_free (TempCrt);
610 Ptr = (UINT8 *)Buffer;
611 End = Ptr + BufferLen;
618 if ((
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_DATA, Pkcs7->ContentTypeOid.len) == 0) ||
619 (
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, Pkcs7->ContentTypeOid.len) == 0) ||
620 (
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_ENVELOPED_DATA, Pkcs7->ContentTypeOid.len) == 0) ||
621 (
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_SIGNED_AND_ENVELOPED_DATA, Pkcs7->ContentTypeOid.len) == 0) ||
622 (
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_DIGESTED_DATA, Pkcs7->ContentTypeOid.len) == 0))
629 if (
CompareMem (Pkcs7->ContentTypeOid.p, MBEDTLS_OID_PKCS7_SIGNED_DATA, Pkcs7->ContentTypeOid.len) != 0) {
636 Ptr = Ptr + Pkcs7->ContentTypeOid.len;
666 mbedtls_x509_crt *Cert,
672 UINT8 Hash[MBEDTLS_MD_MAX_SIZE];
673 mbedtls_pk_context Pk;
674 CONST mbedtls_md_info_t *MdInfo;
679 ZeroMem (Hash, MBEDTLS_MD_MAX_SIZE);
682 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
683 MdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA1);
684 HashLen = mbedtls_md_get_size (MdInfo);
685 mbedtls_md (MdInfo, Data, DataLen, Hash);
686 if (SignerInfo->AuthAttr.p !=
NULL) {
687 TempAuthAttr = *(SignerInfo->AuthAttr.p);
688 *(SignerInfo->AuthAttr.p) = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET;
689 mbedtls_md (MdInfo, SignerInfo->AuthAttr.p, SignerInfo->AuthAttr.len, Hash);
691 *(SignerInfo->AuthAttr.p) = TempAuthAttr;
694 Ret = mbedtls_pk_verify (&Pk, MBEDTLS_MD_SHA1, Hash, HashLen, SignerInfo->Sig.p, SignerInfo->Sig.len);
702 MdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA256);
703 HashLen = mbedtls_md_get_size (MdInfo);
704 ZeroMem (Hash, MBEDTLS_MD_MAX_SIZE);
705 mbedtls_md (MdInfo, Data, DataLen, Hash);
706 if (SignerInfo->AuthAttr.p !=
NULL) {
707 TempAuthAttr = *(SignerInfo->AuthAttr.p);
708 *(SignerInfo->AuthAttr.p) = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET;
709 mbedtls_md (MdInfo, SignerInfo->AuthAttr.p, SignerInfo->AuthAttr.len, Hash);
711 *(SignerInfo->AuthAttr.p) = TempAuthAttr;
714 Ret = mbedtls_pk_verify (&Pk, MBEDTLS_MD_SHA256, Hash, HashLen, SignerInfo->Sig.p, SignerInfo->Sig.len);
719 MdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA384);
720 HashLen = mbedtls_md_get_size (MdInfo);
721 ZeroMem (Hash, MBEDTLS_MD_MAX_SIZE);
722 mbedtls_md (MdInfo, Data, DataLen, Hash);
723 if (SignerInfo->AuthAttr.p !=
NULL) {
724 TempAuthAttr = *(SignerInfo->AuthAttr.p);
725 *(SignerInfo->AuthAttr.p) = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET;
726 mbedtls_md (MdInfo, SignerInfo->AuthAttr.p, SignerInfo->AuthAttr.len, Hash);
728 *(SignerInfo->AuthAttr.p) = TempAuthAttr;
731 Ret = mbedtls_pk_verify (&Pk, MBEDTLS_MD_SHA384, Hash, HashLen, SignerInfo->Sig.p, SignerInfo->Sig.len);
736 MdInfo = mbedtls_md_info_from_type (MBEDTLS_MD_SHA512);
737 HashLen = mbedtls_md_get_size (MdInfo);
738 ZeroMem (Hash, MBEDTLS_MD_MAX_SIZE);
739 mbedtls_md (MdInfo, Data, DataLen, Hash);
740 if (SignerInfo->AuthAttr.p !=
NULL) {
741 TempAuthAttr = *(SignerInfo->AuthAttr.p);
742 *(SignerInfo->AuthAttr.p) = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET;
743 mbedtls_md (MdInfo, SignerInfo->AuthAttr.p, SignerInfo->AuthAttr.len, Hash);
745 *(SignerInfo->AuthAttr.p) = TempAuthAttr;
748 Ret = mbedtls_pk_verify (&Pk, MBEDTLS_MD_SHA512, Hash, HashLen, SignerInfo->Sig.p, SignerInfo->Sig.len);
768 mbedtls_x509_crt *Certs
771 mbedtls_x509_crt *Cert;
774 while (Cert !=
NULL) {
775 if ((Cert->serial.p ==
NULL) || (Cert->issuer_raw.p ==
NULL)) {
779 if ((Cert->issuer_raw.len == SignerInfo->IssuerRaw.len) &&
780 (
CompareMem (Cert->issuer_raw.p, SignerInfo->IssuerRaw.p, Cert->issuer_raw.len) == 0) &&
781 (Cert->serial.len == SignerInfo->Serial.len) &&
782 (
CompareMem (Cert->serial.p, SignerInfo->Serial.p, Cert->serial.len) == 0))
806 mbedtls_x509_crt *Ca,
807 mbedtls_x509_crl *CaCrl,
808 mbedtls_x509_crt *End
813 mbedtls_x509_crt_profile Profile;
816 CopyMem (&Profile, &gCompatProfile,
sizeof (mbedtls_x509_crt_profile));
818 Ret = mbedtls_x509_crt_verify_with_profile (End, Ca, CaCrl, &Profile,
NULL, &VFlag,
NULL,
NULL);
837 mbedtls_x509_crt *Ca,
838 mbedtls_x509_crt *End
841 mbedtls_x509_crt *AllCert;
842 mbedtls_x509_crt *InterCert;
844 AllCert = &(Pkcs7->SignedData.Certificates);
847 while (AllCert !=
NULL) {
853 AllCert = AllCert->next;
856 if (InterCert ==
NULL) {
882 mbedtls_x509_crt *TrustCert,
888 mbedtls_x509_crt *Cert;
889 mbedtls_x509_crt *AllCert;
892 SignerInfo = &(Pkcs7->SignedData.SignerInfos);
898 while (SignerInfo !=
NULL) {
915 if (Result ==
TRUE) {
917 AllCert = &(Pkcs7->SignedData.Certificates);
918 while (AllCert !=
NULL) {
923 AllCert = AllCert->next;
931 SignerInfo = SignerInfo->Next;
964 OUT BOOLEAN *WrapFlag,
965 OUT UINT8 **WrapData,
976 if ((P7Data[4] == MBEDTLS_ASN1_OID) && (P7Data[5] ==
sizeof (MBEDTLS_OID_PKCS7_SIGNED_DATA) - 1)) {
977 if (
CompareMem (P7Data + 6, MBEDTLS_OID_PKCS7_SIGNED_DATA,
sizeof (MBEDTLS_OID_PKCS7_SIGNED_DATA) - 1) == 0) {
978 if ((P7Data[15] == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) && (P7Data[16] == 0x82)) {
985 *WrapData = (UINT8 *)P7Data;
986 *WrapDataSize = P7Length;
991 *WrapDataSize = P7Length + 19;
993 if (*WrapData ==
NULL) {
998 SignedData = *WrapData;
1003 SignedData[0] = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
1004 SignedData[1] = 0x82;
1009 SignedData[2] = (UINT8)(((UINT16)(*WrapDataSize - 4)) >> 8);
1010 SignedData[3] = (UINT8)(((UINT16)(*WrapDataSize - 4)) & 0xff);
1015 SignedData[4] = MBEDTLS_ASN1_OID;
1016 SignedData[5] =
sizeof (MBEDTLS_OID_PKCS7_SIGNED_DATA) - 1;
1021 CopyMem (SignedData + 6, MBEDTLS_OID_PKCS7_SIGNED_DATA,
sizeof (MBEDTLS_OID_PKCS7_SIGNED_DATA) - 1);
1026 SignedData[15] = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC;
1027 SignedData[16] = 0x82;
1032 SignedData[17] = (UINT8)(((UINT16)P7Length) >> 8);
1033 SignedData[18] = (UINT8)(((UINT16)P7Length) & 0xff);
1038 CopyMem (SignedData + 19, P7Data, P7Length);
1041 *WrapFlag = Wrapped;
1084 mbedtls_x509_crt Crt;
1085 mbedtls_x509_crt *TempCrt;
1090 if ((P7Data ==
NULL) || (TrustedCert ==
NULL) || (InData ==
NULL) ||
1091 (P7Length > INT_MAX) || (CertLength > INT_MAX) || (DataLength > INT_MAX))
1096 Status =
WrapPkcs7Data (P7Data, P7Length, &Wrapped, &WrapData, &WrapDataSize);
1104 mbedtls_x509_crt_init (&Crt);
1111 Ret = mbedtls_x509_crt_parse_der (&Crt, TrustedCert, CertLength);
1120 mbedtls_x509_crt_free (&Crt);
1123 if (Pkcs7.SignedData.Certificates.next !=
NULL) {
1124 TempCrt = Pkcs7.SignedData.Certificates.next;
1125 mbedtls_x509_crt_free (TempCrt);
1143 if (Certs ==
NULL) {
1183 OUT UINT8 **CertStack,
1185 OUT UINT8 **TrustedCert,
1190 mbedtls_x509_crt *Cert;
1196 mbedtls_x509_crt *TempCrt;
1205 if ((P7Data ==
NULL) || (CertStack ==
NULL) || (StackLength ==
NULL) ||
1206 (TrustedCert ==
NULL) || (CertLength ==
NULL) || (P7Length > INT_MAX))
1211 Status =
WrapPkcs7Data (P7Data, P7Length, &Wrapped, &WrapData, &WrapDataSize);
1227 SignerInfo = &(Pkcs7.SignedData.SignerInfos);
1242 BufferSize =
sizeof (UINT8);
1243 OldSize = BufferSize;
1246 while (SignerInfo !=
NULL) {
1253 CertSize = Cert->raw.len;
1254 OldSize = BufferSize;
1256 BufferSize = OldSize + CertSize +
sizeof (UINT32);
1259 if (CertBuf ==
NULL) {
1263 if (OldBuf !=
NULL) {
1264 CopyMem (CertBuf, OldBuf, OldSize);
1270 CopyMem (CertBuf + OldSize +
sizeof (UINT32), Cert->raw.p, CertSize);
1275 SignerInfo = SignerInfo->Next;
1278 if (CertBuf !=
NULL) {
1284 *CertLength = BufferSize - OldSize -
sizeof (UINT32);
1286 if (*TrustedCert ==
NULL) {
1290 CopyMem (*TrustedCert, CertBuf + OldSize +
sizeof (UINT32), *CertLength);
1291 *CertStack = CertBuf;
1292 *StackLength = BufferSize;
1300 if (!Status && (CertBuf !=
NULL)) {
1306 if (Pkcs7.SignedData.Certificates.next !=
NULL) {
1307 TempCrt = Pkcs7.SignedData.Certificates.next;
1308 mbedtls_x509_crt_free (TempCrt);
1312 if (OldBuf !=
NULL) {
1346 OUT UINT8 **SignerChainCerts,
1348 OUT UINT8 **UnchainCerts,
UINT32 EFIAPI WriteUnaligned32(OUT UINT32 *Buffer, IN UINT32 Value)
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 ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
BOOLEAN EFIAPI Pkcs7GetSigners(IN CONST UINT8 *P7Data, IN UINTN P7Length, OUT UINT8 **CertStack, OUT UINTN *StackLength, OUT UINT8 **TrustedCert, OUT UINTN *CertLength)
BOOLEAN WrapPkcs7Data(IN CONST UINT8 *P7Data, IN UINTN P7Length, OUT BOOLEAN *WrapFlag, OUT UINT8 **WrapData, OUT UINTN *WrapDataSize)
BOOLEAN EFIAPI Pkcs7Verify(IN CONST UINT8 *P7Data, IN UINTN P7Length, IN CONST UINT8 *TrustedCert, IN UINTN CertLength, IN CONST UINT8 *InData, IN UINTN DataLength)
VOID EFIAPI Pkcs7FreeSigners(IN UINT8 *Certs)
BOOLEAN EFIAPI Pkcs7GetCertificatesList(IN CONST UINT8 *P7Data, IN UINTN P7Length, OUT UINT8 **SignerChainCerts, OUT UINTN *ChainLength, OUT UINT8 **UnchainCerts, OUT UINTN *UnchainLength)
STATIC INT32 MbedTlsPkcs7GetDigestAlgorithm(UINT8 **Ptr, UINT8 *End, mbedtls_x509_buf *Alg)
STATIC INT32 MbedTlsPkcs7GetDigestAlgorithmSet(UINT8 **Ptr, UINT8 *End, mbedtls_x509_buf *Alg)
STATIC VOID MbedTlsPkcs7Init(MbedtlsPkcs7 *Pkcs7)
STATIC INT32 MbedTlsPkcs7GetCertificates(UINT8 **Ptr, INTN Plen, mbedtls_x509_crt *Certs)
STATIC BOOLEAN MbedTlsPkcs7SignedDataVerify(MbedtlsPkcs7 *Pkcs7, mbedtls_x509_crt *TrustCert, CONST UINT8 *Data, INTN DataLen)
STATIC INT32 Pkcs7GetSignature(UINT8 **Ptr, UINT8 *End, mbedtls_asn1_buf *Signature)
STATIC INT32 MbedtlsPkcs7ParseDer(CONST UINT8 *Buffer, INTN BufferLen, MbedtlsPkcs7 *Pkcs7)
STATIC INT32 MbedTlsPkcs7GetNextContentLen(UINT8 **Ptr, UINT8 *End, UINTN *Len)
STATIC mbedtls_x509_crt * MbedTlsPkcs7FindSignerCert(MbedtlsPkcs7SignerInfo *SignerInfo, mbedtls_x509_crt *Certs)
STATIC INT32 MbedTlsPkcs7GetVersion(UINT8 **Ptr, UINT8 *End, INT32 *Ver)
STATIC INT32 Pkcs7GetContentInfoType(UINT8 **Ptr, UINT8 *End, mbedtls_asn1_buf *Pkcs7)
STATIC BOOLEAN MbedTlsPkcs7VerifyCertChain(MbedtlsPkcs7 *Pkcs7, mbedtls_x509_crt *Ca, mbedtls_x509_crt *End)
STATIC BOOLEAN MbedTlsPkcs7VerifyCert(mbedtls_x509_crt *Ca, mbedtls_x509_crl *CaCrl, mbedtls_x509_crt *End)
STATIC INT32 Pkcs7GetSignedData(UINT8 *Buffer, INTN BufferLen, MbedtlsPkcs7SignedData *SignedData)
STATIC INT32 MbedTlsPkcs7GetSignersInfoSet(UINT8 **Ptr, UINT8 *End, MbedtlsPkcs7SignerInfo *SignersSet)
STATIC INT32 MbedtlsPkcs7SignedDataVerifySigners(MbedtlsPkcs7SignerInfo *SignerInfo, mbedtls_x509_crt *Cert, CONST UINT8 *Data, INTN DataLen)