private PdfName GetSignatureHashKey(String signatureName) { PdfDictionary dic = acroFields.GetSignatureDictionary(signatureName); PdfString contents = dic.GetAsString(PdfName.CONTENTS); byte[] bc = contents.GetOriginalBytes(); byte[] bt = null; if (PdfName.ETSI_RFC3161.Equals(dic.GetAsName(PdfName.SUBFILTER))) { Asn1InputStream din = new Asn1InputStream(new MemoryStream(bc)); Asn1Object pkcs = din.ReadObject(); bc = pkcs.GetEncoded(); } bt = HashBytesSha1(bc); return(new PdfName(Utilities.ConvertToHex(bt))); }
static PdfName getSignatureHashKey(PdfDictionary dic, bool encrypted) { PdfString contents = dic.GetAsString(PdfName.CONTENTS); byte[] bc = contents.GetOriginalBytes(); if (PdfName.ETSI_RFC3161.Equals(PdfReader.GetPdfObject(dic.Get(PdfName.SUBFILTER)))) { using (Asn1InputStream din = new Asn1InputStream(bc)) { Asn1Object pkcs = din.ReadObject(); bc = pkcs.GetEncoded(); } } byte[] bt = hashBytesSha1(bc); return(new PdfName(Utilities.ConvertToHex(bt))); }
static void TestPDF(string path) { Console.WriteLine("processing PDF"); AcroFields acroFields = new PdfReader(path).AcroFields; List <string> names = acroFields.GetSignatureNames(); foreach (var name in names) { try { Console.WriteLine(name); PdfDictionary dict = acroFields.GetSignatureDictionary(name); PdfString contents = (PdfString)PdfReader.GetPdfObject(dict.Get(PdfName.CONTENTS)); byte[] PKCS7 = contents.GetOriginalBytes(); TestP7s(PKCS7); } catch (Exception exc) { Console.WriteLine(exc.Message); } } }
static void TestPDF(string path) { Console.WriteLine("processing PDF"); AcroFields acroFields = new PdfReader(path).AcroFields; List <string> names = acroFields.GetSignatureNames(); foreach (var name in names) { try { Console.WriteLine(name); PdfDictionary dict = acroFields.GetSignatureDictionary(name); PdfString contents = (PdfString)PdfReader.GetPdfObject(dict.Get(PdfName.CONTENTS)); byte[] PKCS7 = contents.GetOriginalBytes(); var signedData = new SignedCms(); signedData.Decode(PKCS7); Console.WriteLine(signedData.Certificates.Count); int i = 0; foreach (var certificate in signedData.Certificates) { i++; X509CertificateParser x509CertificateParser = new X509CertificateParser(); X509Certificate x509Certificate = x509CertificateParser.ReadCertificate(certificate.GetRawCertData()); RsaKeyParameters rsaKeyParameters = x509Certificate.GetPublicKey() as RsaKeyParameters; if (RocaTest.IsVulnerable(rsaKeyParameters)) { Console.WriteLine("Cetificate #" + i + " is vulnerable. Cert Hash: " + certificate.GetCertHashString()); } else { Console.WriteLine("Cetificate #" + i + " is NOT vulnerable"); } } } catch (Exception exc) { Console.WriteLine(exc.Message); } } }