public override bool VerifyFile(string filePath, ref List <KeyValuePair <X509Certificate2, bool> > verifiedCMS) { byte[] DataDigest = new byte[0]; byte[] BlockDigest = new byte[0]; signatureBlock = new Elements(ExtractBlocks(filePath), false); //digest of the data without the signature(s) DataDigest = Hash(filePath); //signatures found in the file Dictionary <string, string> Signatures = ExtractAllSignatures(filePath); if (Signatures.Count < 1) { throw new NoSignatureFoundException(filePath); } List <KeyValuePair <X509Certificate2, bool> > UsedCertificates = new List <KeyValuePair <X509Certificate2, bool> >(); bool Validation = true; foreach (String Signature in Signatures.Keys) { BlockDigest = HashFunction.ComputeHash(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(Signatures[Signature]))); byte[] merkleHash = new byte[DataDigest.Length + BlockDigest.Length]; Array.Copy(DataDigest, merkleHash, DataDigest.Length); Array.Copy(BlockDigest, 0, merkleHash, DataDigest.Length, BlockDigest.Length); //Content information created from the data digest ContentInfo StepContent = new ContentInfo(merkleHash); SignedCms SignedCMS = new SignedCms(StepContent, true); SignedCMS.Decode(Convert.FromBase64String(Signature)); SignerInfoEnumerator Enumerator = SignedCMS.SignerInfos.GetEnumerator(); if (!Enumerator.MoveNext()) { throw new InvalidSignerInformationException(Signature); } try { //after decoding the signed cms, we check the signature SignedCMS.CheckSignature(true); UsedCertificates.Add(new KeyValuePair <X509Certificate2, bool>(Enumerator.Current.Certificate, true)); } catch (System.Security.Cryptography.CryptographicException e) { //signature can't be verified UsedCertificates.Add(new KeyValuePair <X509Certificate2, bool>(Enumerator.Current.Certificate, false)); Validation = false; } } verifiedCMS = UsedCertificates; return(Validation); }
public override bool VerifyFile(string filePath, ref List <KeyValuePair <X509Certificate2, bool> > verifiedCMS) { byte[] DataDigest = new byte[0]; byte[] EncodedCMS = new byte[0]; //digest of the data without the signature(s) DataDigest = Hash(filePath); //signatures found in the file List <String> Signatures = ExtractAllSignatures(filePath); if (Signatures.Count < 1) { throw new NoSignatureFoundException(filePath); } //Content information created from the data digest ContentInfo StepContent = new ContentInfo(DataDigest); SignedCms SignedCMS = new SignedCms(StepContent, true); List <KeyValuePair <X509Certificate2, bool> > UsedCertificates = new List <KeyValuePair <X509Certificate2, bool> >(); bool Validation = true; foreach (String Signature in Signatures) { SignedCMS.Decode(Convert.FromBase64String(Signature)); SignerInfoEnumerator Enumerator = SignedCMS.SignerInfos.GetEnumerator(); if (!Enumerator.MoveNext()) { throw new InvalidSignerInformationException(Signature); } try { //after decoding the signed cms, we check the signature SignedCMS.CheckSignature(true); UsedCertificates.Add(new KeyValuePair <X509Certificate2, bool>(Enumerator.Current.Certificate, true)); } catch (System.Security.Cryptography.CryptographicException e) { //signature can't be verified UsedCertificates.Add(new KeyValuePair <X509Certificate2, bool>(Enumerator.Current.Certificate, false)); Validation = false; } } verifiedCMS = UsedCertificates; return(Validation); }