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[] 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 STEPTRACE4PLOT(string block) { signatureBlock = new Elements(ExtractBlocks(block), false); }