public static void addLTV(String src, String dest, IOcspClient ocsp, ICrlClient crl, ITSAClient itsaClient) { PdfReader reader = new PdfReader(src); PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode()); LtvVerification v = new LtvVerification(pdfDoc); SignatureUtil signatureUtil = new SignatureUtil(pdfDoc); IList <string> names = signatureUtil.GetSignatureNames(); String sigName = names[names.Count - 1]; PdfPKCS7 pkcs7 = signatureUtil.ReadSignatureData(sigName); if (pkcs7.IsTsp()) { v.AddVerification(sigName, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); } else { foreach (var name in names) { v.AddVerification(name, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); } } v.Merge(); pdfDoc.Close(); }
static void addLTVToStream(Stream source, Stream destination, IOcspClient ocsp, ICrlClient crl, LtvVerification.Level timestampLevel, LtvVerification.Level signatureLevel) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(destination), new StampingProperties().UseAppendMode()); LtvVerification v = new LtvVerification(pdfDoc); SignatureUtil signatureUtil = new SignatureUtil(pdfDoc); IList <string> names = signatureUtil.GetSignatureNames(); String sigName = names[(names.Count - 1)]; PdfPKCS7 pkcs7 = signatureUtil.ReadSignatureData(sigName); if (pkcs7.IsTsp()) { v.AddVerification(sigName, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, timestampLevel, LtvVerification.CertificateInclusion.YES); } else { foreach (String name in names) { v.AddVerification(name, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, signatureLevel, LtvVerification.CertificateInclusion.YES); } } v.Merge(); pdfDoc.Close(); }
private void AdicionarLtv(string pdfPrimeiraAssinatura, SingInfo singInfo) { var inicioCarimbo = DateTime.Now; var stamping = new StampingProperties(); stamping.UseAppendMode(); PdfDocument pdfDoc = new PdfDocument( new PdfReader(pdfPrimeiraAssinatura), new PdfWriter(singInfo.TargetPdf)); LtvVerification v = new LtvVerification(pdfDoc); SignatureUtil signatureUtil = new SignatureUtil(pdfDoc); var names = signatureUtil.GetSignatureNames(); var sigName = names[names.Count - 1]; var pkcs7 = signatureUtil.VerifySignature(sigName); if (pkcs7.IsTsp()) { v.AddVerification( sigName, this.ocspClient, new CrlClientOnline(this.cadeiaTempo.ToArray()), LtvVerification.CertificateOption.SIGNING_CERTIFICATE, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES); } else { foreach (var name in names) { v.AddVerification( name, this.ocspClient, new CrlClientOnline(this.cadeiaTempo.ToArray()), LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); } } pdfDoc.Close(); Console.WriteLine("Aplicando timestamp........"); PdfReader r = new PdfReader(pdfPrimeiraAssinatura); PdfSigner pdfSigner = new PdfSigner(r, new FileStream(singInfo.TargetPdf, FileMode.Create), stamping); pdfSigner.Timestamp(this.tsaClient, null); var fimCarimbo = DateTime.Now; var tempoCarimbo = fimCarimbo.Subtract(inicioCarimbo); Console.WriteLine("Tempo Carimbo: {0}ms", tempoCarimbo.TotalMilliseconds); }
private ICollection <string> ObterSignatureFieldName(byte[] fileBytes) { using PdfReader pdfReader = new PdfReader(new MemoryStream(fileBytes)); using PdfDocument pdfDocument = new PdfDocument(pdfReader); SignatureUtil signUtil = new SignatureUtil(pdfDocument); var assinaturas = signUtil.GetSignatureNames(); return(assinaturas); }
private void VerifySignaturesForDocument(String documentPath) { PdfReader reader = new PdfReader(documentPath); PdfDocument pdfDoc = new PdfDocument(new PdfReader(documentPath)); SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); VerifySignatures(signUtil, names); reader.Close(); }
public void VerifySignatures(String path) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(path)); SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); OUT_STREAM.WriteLine(path); foreach (String name in names) { OUT_STREAM.WriteLine("===== " + name + " ====="); VerifySignature(signUtil, name); } }
public virtual void InspectSignatures(String path) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(path)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignaturePermissions perms = null; SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); Console.WriteLine(path); foreach (String name in names) { Console.Out.WriteLine("===== " + name + " ====="); perms = InspectSignature(pdfDoc, signUtil, form, name, perms); } }
public static List <FileDetailsModel> InspectSignatures(String path) { List <FileDetailsModel> fileModelList = new List <FileDetailsModel>(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(path)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignaturePermissions perms = null; SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); foreach (String name in names) { FileDetailsModel fileModel = new FileDetailsModel(); logger.Error("===== " + name + " ====="); fileModel.Signature = name; (perms, fileModel) = InspectSignature(fileModel, pdfDoc, signUtil, form, name, perms); fileModelList.Add(fileModel); } return(fileModelList); }
public virtual void InspectSignatures(String path) { Console.WriteLine("\n\n"); PdfDocument pdfDoc = new PdfDocument(new PdfReader(path)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignaturePermissions perms = null; SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); Console.WriteLine(path); foreach (String name in names) { Console.Out.WriteLine("===== " + name + " ====="); perms = InspectSignature(pdfDoc, signUtil, form, name, perms); } PdfAConformanceLevel conformanceLevel = pdfDoc.GetReader().GetPdfAConformanceLevel(); string conformance = conformanceLevel == null ? string.Empty : conformanceLevel.GetConformance(); string part = conformanceLevel == null ? string.Empty : conformanceLevel.GetPart(); Console.Out.WriteLine($"Document conformance: {conformance}/{part}"); GetMetaInfo(pdfDoc); }
public bool HasDigitalSignature(MemoryStream memoryStream) { memoryStream.Seek(0, SeekOrigin.Begin); using (PdfReader pdfReader = new PdfReader(memoryStream)) using (PdfDocument pdfDocument = new PdfDocument(pdfReader)) { SignatureUtil signUtil = new SignatureUtil(pdfDocument); var assinaturas = signUtil.GetSignatureNames(); pdfDocument.Close(); pdfReader.Close(); if (assinaturas?.Count >= 1) { return(true); } else { return(false); } } }
static void Main(string[] args) { try { //File.WriteAllText("D:\\pipInput.txt", args[0]); MemoryStream ms = null; byte[] password = null; for (int i = 0; i < args.Length; i++) { int splitIndex = args[i].IndexOf(':'); string prefix = null; string value = null; if (splitIndex == -1) { prefix = args[i]; } else { prefix = args[i].Substring(0, splitIndex); value = args[i].Substring(splitIndex + 1); } switch (prefix.ToUpper()) { case "-N": case "/N": if (ms != null) { throw new ArgumentException("incorrect input arguments: filedata already initialized"); } if (string.IsNullOrWhiteSpace(value) || !File.Exists(value)) { throw new ArgumentException("incorrect input arguments: filename not exists"); } if (File.Exists(value)) { ms = new MemoryStream(File.ReadAllBytes(value)); } break; case "-D": case "/D": if (ms != null) { throw new ArgumentException("incorrect input arguments: filedata already initialized"); } if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentException("incorrect input arguments: filedata is empty"); } ms = new MemoryStream(Convert.FromBase64String(value)); break; case "-P": case "/P": if (value == null) { throw new ArgumentException("incorrect input arguments: password is empty"); } password = Encoding.ASCII.GetBytes(value); break; case "-HELP": case "/HELP": case "-?": case "/?": Console.Write("Retrieves PAdES signatures and returns informations structured into XML" + Environment.NewLine + "document." + Environment.NewLine + Environment.NewLine + "PadesInfoProcessor [/N:[path][filename]] [/D:[filedata]] [/P:[password]]" + Environment.NewLine + " /N:[path][filename]" + Environment.NewLine + " Specifies PDF document file path to retrieve informations." + Environment.NewLine + " /D:[filedata]" + Environment.NewLine + " Specifies PDF document data encoded with Base64." + Environment.NewLine + " /P:[password]" + Environment.NewLine + " Specifies password if PDF document is encrypted." + Environment.NewLine + Environment.NewLine); break; default: break; } } string output = string.Empty; if (ms != null) { iText.Kernel.Pdf.PdfReader pdfReader; if (password == null) { pdfReader = new iText.Kernel.Pdf.PdfReader(ms); } else { pdfReader = new iText.Kernel.Pdf.PdfReader(ms, (new iText.Kernel.Pdf.ReaderProperties()).SetPassword(password)); } output += "<PdfSignatures>"; PdfDocument document = new PdfDocument(pdfReader); if (iText.Forms.PdfAcroForm.GetAcroForm(document, false) != null) { SignatureUtil su = new SignatureUtil(document); IList <string> sigNames = su.GetSignatureNames(); foreach (string sigName in sigNames) { PdfSignature sig = su.GetSignature(sigName); //string cert = sig.GetCert().GetValue(); string coversWholeDoc = su.SignatureCoversWholeDocument(sigName).ToString(); string signingTime = getDate(sig.GetDate()); string contentType = sig.GetSubFilter().ToString().Replace("/", ""); string reason = sig.GetReason(); string location = sig.GetLocation(); output += "<PdfSignature>"; output += "<SignatureName>" + sigName + "</SignatureName>"; output += "<PdfSigningTimeUtc>" + signingTime + "</PdfSigningTimeUtc>"; output += "<Reason>" + reason + "</Reason>"; output += "<Location>" + location + "</Location>"; output += "<CoversWholeDocument>" + coversWholeDoc + "</CoversWholeDocument>"; output += "<ContentType>" + contentType + "</ContentType>"; output += processByPdfPKCS7(new PdfDocument(pdfReader), sig, contentType); output += "</PdfSignature>"; } } output += "</PdfSignatures>"; } //File.WriteAllText(@"D:\PadesInfoProcessorOutput.xml", output); Console.Write(output); } catch (ArgumentException argEx) { Console.Error.Write("PadesInfoProcessor 1: " + argEx.Message); } catch (Exception ex) { //File.WriteAllText("D:\\pipException.txt", ex.ToString()); Console.Error.Write("PadesInfoProcessor 1: PDF document unexpected exception: " + ex.ToString()); } }
private SignedDocumentInfo CollectInfo(String documentPath) { SignedDocumentInfo docInfo = new SignedDocumentInfo(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(documentPath)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); docInfo.SetNumberOfTotalRevisions(signUtil.GetTotalRevisions()); SignaturePermissions perms = null; IList <SignatureInfo> signInfos = new List <SignatureInfo>(); foreach (String name in names) { SignatureInfo sigInfo = new SignatureInfo(); sigInfo.SetSignatureName(name); sigInfo.SetRevisionNumber(signUtil.GetRevision(name)); sigInfo.SetSignatureCoversWholeDocument(signUtil.SignatureCoversWholeDocument(name)); IList <PdfWidgetAnnotation> widgetAnnotationsList = form.GetField(name).GetWidgets(); if (widgetAnnotationsList != null && widgetAnnotationsList.Count > 0) { sigInfo.SetSignaturePosition(widgetAnnotationsList[0].GetRectangle().ToRectangle()); } PdfPKCS7 pkcs7 = signUtil.ReadSignatureData(name); sigInfo.SetDigestAlgorithm(pkcs7.GetHashAlgorithm()); sigInfo.SetEncryptionAlgorithm(pkcs7.GetEncryptionAlgorithm()); PdfName filterSubtype = pkcs7.GetFilterSubtype(); if (filterSubtype != null) { sigInfo.SetFilterSubtype(filterSubtype.ToString()); } X509Certificate signCert = pkcs7.GetSigningCertificate(); sigInfo.SetSignerName(iText.Signatures.CertificateInfo.GetSubjectFields(signCert).GetField("CN")); sigInfo.SetAlternativeSignerName(pkcs7.GetSignName()); sigInfo.SetSignDate(pkcs7.GetSignDate().ToUniversalTime()); if (TimestampConstants.UNDEFINED_TIMESTAMP_DATE != pkcs7.GetTimeStampDate()) { sigInfo.SetTimeStamp(pkcs7.GetTimeStampDate().ToUniversalTime()); TimeStampToken ts = pkcs7.GetTimeStampToken(); sigInfo.SetTimeStampService(ts.TimeStampInfo.Tsa.ToString()); } sigInfo.SetLocation(pkcs7.GetLocation()); sigInfo.SetReason(pkcs7.GetReason()); PdfDictionary sigDict = signUtil.GetSignatureDictionary(name); PdfString contactInfo = sigDict.GetAsString(PdfName.ContactInfo); if (contactInfo != null) { sigInfo.SetContactInfo(contactInfo.ToString()); } perms = new SignaturePermissions(sigDict, perms); sigInfo.SetIsCertifiaction(perms.IsCertification()); sigInfo.SetIsFieldsFillAllowed(perms.IsFillInAllowed()); sigInfo.SetIsAddingAnnotationsAllowed(perms.IsAnnotationsAllowed()); IList <String> fieldLocks = new List <String>(); foreach (SignaturePermissions.FieldLock Lock in perms.GetFieldLocks()) { fieldLocks.Add(Lock.ToString()); } sigInfo.SetFieldsLocks(fieldLocks); X509Certificate[] certs = pkcs7.GetSignCertificateChain(); IList <CertificateInfo> certInfos = new List <CertificateInfo>(); for (int i = 0; i < certs.Length; i++) { X509Certificate cert = (X509Certificate)certs[i]; CertificateInfo certInfo = new CertificateInfo(); certInfo.SetIssuer(cert.IssuerDN); certInfo.SetSubject(cert.SubjectDN); certInfo.SetValidFrom(cert.NotBefore); certInfo.SetValidTo(cert.NotAfter); certInfos.Add(certInfo); } sigInfo.SetCertificateInfos(certInfos); signInfos.Add(sigInfo); } docInfo.SetSignatureInfos(signInfos); return(docInfo); }