14#include <mbedtls/ecdh.h>
19#define MAX_SIGNATURE_SIZE 1024
39 mbedtls_md_type_t DigestType
46 Ret = mbedtls_oid_get_oid_by_md (DigestType, (
CONST CHAR8 **)&OidPtr, &OidLen);
48 return mbedtls_asn1_write_oid (Ptr, (
CONST UINT8 *)Start, (
CONST CHAR8 *)OidPtr, OidLen);
71 mbedtls_md_type_t *DigestTypes,
80 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_null (Ptr, Start));
82 for (Idx = 0; Idx < Count; Idx++) {
89 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, (
UINTN)Len));
93 mbedtls_asn1_write_tag (
96 (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
100 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, (
UINTN)Len));
104 mbedtls_asn1_write_tag (
107 (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)
141 if (Content !=
NULL) {
142 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_octet_string (Ptr, Start, Content, ContentLen));
147 mbedtls_asn1_write_oid (
150 MBEDTLS_OID_PKCS7_DATA,
151 sizeof (MBEDTLS_OID_PKCS7_DATA) - 1
155 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
157 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
181 mbedtls_x509_crt *Cert,
182 mbedtls_x509_crt *OtherCerts
187 mbedtls_x509_crt *TmpCert;
192 TmpCert = OtherCerts;
193 while (TmpCert !=
NULL) {
194 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_raw_buffer (Ptr, Start, TmpCert->raw.p, TmpCert->raw.len));
195 TmpCert = TmpCert->next;
199 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_raw_buffer (Ptr, Start, Cert->raw.p, Cert->raw.len));
202 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
203 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
232 Pt = SerialRaw + SerialRawLen;
233 while (Pt > SerialRaw) {
238 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
239 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_INTEGER));
274 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_raw_buffer (Ptr, Start, IssuerRaw, IssuerRawLen));
276 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
277 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
314 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_octet_string (Ptr, Start, SignerInfo->Sig.p, SignerInfo->Sig.len));
316 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_algorithm_identifier (Ptr, Start, (
CONST CHAR8 *)SignerInfo->SigAlgIdentifier.p, SignerInfo->SigAlgIdentifier.len, 0));
318 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_algorithm_identifier (Ptr, Start, (
CONST CHAR8 *)SignerInfo->AlgIdentifier.p, SignerInfo->AlgIdentifier.len, 0));
322 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_int (Ptr, Start, SignerInfo->Version));
324 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
325 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
352 SignerInfo = SignersSet;
355 while (SignerInfo !=
NULL) {
358 SignerInfo = SignerInfo->Next;
361 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
362 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
402 mbedtls_md_type_t DigestAlg[1];
404 DigestAlg[0] = MBEDTLS_MD_SHA256;
409 EDKII_ASN1_CHK_ADD (Len,
MbedTlsPkcs7WriteCertificates (Ptr, Start, &(Pkcs7->SignedData.Certificates), Pkcs7->SignedData.Certificates.next));
415 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_int (Ptr, Start, Pkcs7->SignedData.Version));
417 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_len (Ptr, Start, Len));
418 EDKII_ASN1_CHK_ADD (Len, mbedtls_asn1_write_tag (Ptr, Start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
459 IN UINT8 *OtherCerts OPTIONAL,
460 OUT UINT8 **SignedData,
466 mbedtls_pk_context Pkey;
470 UINT8 *NewPrivateKey;
471 mbedtls_x509_crt *Crt;
483 if ((PrivateKey ==
NULL) || (KeyPassword ==
NULL) || (InData ==
NULL) ||
484 (SignCert ==
NULL) || (SignedData ==
NULL) || (SignedDataSize ==
NULL) || (InDataSize > INT_MAX))
493 Crt = (mbedtls_x509_crt *)SignCert;
495 NewPrivateKey =
NULL;
496 if (PrivateKey[PrivateKeySize - 1] != 0) {
498 if (NewPrivateKey ==
NULL) {
502 CopyMem (NewPrivateKey, PrivateKey, PrivateKeySize);
503 NewPrivateKey[PrivateKeySize] = 0;
507 if (NewPrivateKey ==
NULL) {
511 CopyMem (NewPrivateKey, PrivateKey, PrivateKeySize);
514 mbedtls_pk_init (&Pkey);
515 Ret = mbedtls_pk_parse_key (
538 Ret = mbedtls_pk_sign (
555 Pkcs7.SignedData.Version = 1;
557 Crt->next = (mbedtls_x509_crt *)OtherCerts;
558 Pkcs7.SignedData.Certificates = *Crt;
560 SignerInfo.Next =
NULL;
561 SignerInfo.Sig.p = Signature;
562 SignerInfo.Sig.len = SignatureLen;
563 SignerInfo.Version = 1;
564 SignerInfo.AlgIdentifier.p = MbedtlsOidDigestAlgSha256;
565 SignerInfo.AlgIdentifier.len =
sizeof (MBEDTLS_OID_DIGEST_ALG_SHA256) - 1;
566 if (mbedtls_pk_get_type (&Pkey) == MBEDTLS_PK_RSA) {
567 SignerInfo.SigAlgIdentifier.p = MbedtlsOidPkcs1Rsa;
568 SignerInfo.SigAlgIdentifier.len =
sizeof (MBEDTLS_OID_PKCS1_RSA) - 1;
570 Ret = mbedtls_oid_get_oid_by_sig_alg (MBEDTLS_PK_ECDSA, MBEDTLS_MD_SHA256, (
CONST CHAR8 **)&SignerInfo.SigAlgIdentifier.p, &SignerInfo.SigAlgIdentifier.len);
577 SignerInfo.Serial = ((mbedtls_x509_crt *)SignCert)->serial;
578 SignerInfo.IssuerRaw = ((mbedtls_x509_crt *)SignCert)->issuer_raw;
579 Pkcs7.SignedData.SignerInfos = SignerInfo;
582 if (Buffer ==
NULL) {
587 Ptr = Buffer + BufferSize;
591 while (Len == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL) {
592 BufferSize = BufferSize * 2;
593 Ptr = Buffer + BufferSize;
596 if (Buffer ==
NULL) {
601 Ptr = Buffer + BufferSize;
611 if (*SignedData ==
NULL) {
616 *SignedDataSize = Len;
617 CopyMem (*SignedData, Ptr, Len);
622 mbedtls_pk_free (&Pkey);
625 if (NewPrivateKey !=
NULL) {
626 memset (NewPrivateKey, 0, PrivateKeySize);
630 if (Buffer !=
NULL) {
631 memset (Buffer, 0, BufferSize);
#define SHA256_DIGEST_SIZE
BOOLEAN EFIAPI Sha256HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
INT32 MbedtlsRand(VOID *RngState, UINT8 *Output, UINTN Len)
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
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)
#define GLOBAL_REMOVE_IF_UNREFERENCED
BOOLEAN EFIAPI Pkcs7Sign(IN CONST UINT8 *PrivateKey, IN UINTN PrivateKeySize, IN CONST UINT8 *KeyPassword, IN UINT8 *InData, IN UINTN InDataSize, IN UINT8 *SignCert, IN UINT8 *OtherCerts OPTIONAL, OUT UINT8 **SignedData, OUT UINTN *SignedDataSize)
STATIC INT32 MbedTlsPkcs7WriteDer(UINT8 **Ptr, UINT8 *Start, MbedtlsPkcs7 *Pkcs7)
STATIC INT32 MbedTlsPkcs7WriteInt(UINT8 **Ptr, UINT8 *Start, UINT8 *SerialRaw, INTN SerialRawLen)
STATIC INT32 MbedTlsPkcs7WriteDigestAlgorithmSet(UINT8 **Ptr, UINT8 *Start, mbedtls_md_type_t *DigestTypes, INTN Count)
STATIC INT32 MbedTlsPkcs7WriteSignerInfo(UINT8 **Ptr, UINT8 *Start, MbedtlsPkcs7SignerInfo *SignerInfo)
#define MAX_SIGNATURE_SIZE
STATIC INT32 MbedTlsPkcs7WriteCertificates(UINT8 **Ptr, UINT8 *Start, mbedtls_x509_crt *Cert, mbedtls_x509_crt *OtherCerts)
STATIC INT32 MbedTlsPkcs7WriteIssuerAndSerialNumber(UINT8 **Ptr, UINT8 *Start, UINT8 *Serial, INTN SerialLen, UINT8 *IssuerRaw, INTN IssuerRawLen)
STATIC INT32 MbedTlsPkcs7WriteContentInfo(UINT8 **Ptr, UINT8 *Start, UINT8 *Content, INTN ContentLen)
STATIC INT32 MbedTlsPkcs7WriteDigestAlgorithm(UINT8 **Ptr, UINT8 *Start, mbedtls_md_type_t DigestType)
STATIC INT32 MbedTlsPkcs7WriteSignersInfoSet(UINT8 **Ptr, UINT8 *Start, MbedtlsPkcs7SignerInfo *SignersSet)