/** * @Method Method that inserts the GraphSign in the pdf document and call for signing it * @Params DocumentData source document to be signed * @Params GraphSign sign Image and metadata about the Graphical Sign * @Params signer data * @Return true if the document is correctly signed, false if something wrong */ public bool doSignature(DocumentData source,GraphSign sign, string metadata, Signer signer) { bool result = false; bool insertedSign = false; /* if (this.open(source)) { Dictionary<String, String> hMap = this.reader.Info; String keywords = ""; hMap.TryGetValue("Keywords",out keywords); keywords += metadata+Properties.Settings.Default.stringSeparator; this.Log.Debug("Keywords length " + keywords.Length); hMap.Remove("Keywords"); hMap.Add("Keywords", keywords); //Copy PDF this.Log.Debug("Starting PDF copy"); for (int i = 1; i <= reader.NumberOfPages; i++) { doc.SetPageSize(reader.GetPageSize(i)); doc.NewPage(); PdfContentByte cb = writer.DirectContent; PdfImportedPage importedPage = writer.GetImportedPage(reader, i); int rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) cb.AddTemplate(importedPage, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height); else cb.AddTemplate(importedPage, 1.0F, 0, 0, 1.0F, 0, 0); //Insert Graph image on coordenates if (i == signer.Page) { this.Log.Debug(String.Format("Trying to insert graph sign in Page {0}, x={1}, y={2}", signer.Page, signer.X, signer.Y)); insertedSign = this.insertGraphSign(sign, cb, signer.X, signer.Y); } } * */ insertedSign = true; if (insertedSign) { // this.Log.Debug("Graph Sign inserted correctly, starting PAdES process"); //Do PAdES // this.close(); DigitalSignUtils.signPDF(source, metadata, sign,signer); this.Log.Debug("Moving files"); if(File.Exists(source.Docpath+"-signed.pdf")) File.Delete(source.Docpath+"-signed.pdf"); File.Copy(source.Docsignedpath, source.Docpath + "-signed.pdf"); result = true; } // } // this.close(); return result; }
/** * @Method transforms a DocumentData into DatosCaptura reading the signed PDF File and encoding it in Base64 * @Parameters document ready to send * @Return Encoded signed PDF File */ public static DatosCaptura toDatosCaptura(DocumentData doc) { DatosCaptura result = new DatosCaptura(); result.docname = doc.Docname; result.uuid = doc.Uuid; result.idoperation = doc.Idoperation; if (File.Exists(doc.Docsignedpath)) { result.value = fileToBase64(doc.Docsignedpath); } else result.value = ""; return result; }
public static List<DocumentData> parseDocumentData(string jsonData) { List<DocumentData> result = new List<DocumentData>(); dynamic data = JsonConvert.DeserializeObject(jsonData); foreach (dynamic obj in data) { DocumentData doc = new DocumentData(); doc.Docname = obj.docname; doc.Docmetadata2 = obj.docmetadata2.ToString(); doc.Idoperation = obj.idoperation.ToString(); doc.Uuid = (int)obj.uuid; doc.Docmetadata = parseSigners(obj.docmetadata.ToString()); result.Add(doc); } return result; }
/** * @Method get the Document Signers that have not yet signed * @Params Document to check Signers * @Return List of Signers */ private List<Signer> getRemainingSigners(DocumentData doc) { List<Signer> result = new List<Signer>(); foreach (Signer s in doc.Docmetadata) if (!s.Signed) result.Add(s); return result; }
/** * @Method Check if the Wacom Pad is connected, if so calls the pad controller for signing * and after calls PDF controller for digital signing * @Params DocumentData doc document to be signed */ internal bool signPdf(DocumentData doc, int signer) { doc.Docmetadata[signer].Signed = false; if (this.padController.checkPadConnected()) { this.Log.Debug(String.Format("The document {0} from operation {1} is going to be signed by {2}", doc.Docname, doc.Idoperation, doc.Docmetadata[signer].Nombre)); this.Log.Debug("Calling Wacom Pad Controller"); GraphSign sign = this.padController.padSigning(doc.Docmetadata[signer]); if (sign != null) { this.Log.Debug("Graph Sign retrieved correctly"); String jsonSign = Parser.serializeObject(sign.Points); //this.Log.Debug("JSON string for sign "+jsonSign); String[] signArray = this.getSignString(sign); PDF.DigitalSignUtils.index = signer; doc.Docmetadata[signer].Signed = this.pdfController.doSignature(doc, sign, signArray, doc.Docmetadata[signer]); } } else throw new Exception("pad not connected"); return doc.Docmetadata[signer].Signed; }
public static void signPDF_old(DocumentData doc, Dictionary<String, String> metadata) { ILog Log; Log = LogManager.GetLogger(Properties.Settings.Default.logName); try { PdfReader reader = new PdfReader(doc.Docsignedpath); if (File.Exists(doc.Docsignedpath + "-signed.pdf")) File.Delete(doc.Docsignedpath + "-signed.pdf"); FileStream fos = new FileStream(doc.Docsignedpath + "-signed.pdf", FileMode.CreateNew, FileAccess.Write); doc.Docsignedpath = doc.Docsignedpath + "-signed.pdf"; Log.Debug(String.Format("Creating Stamper for doc {0}", doc.Docname)); PdfStamper stp = PdfStamper.CreateSignature(reader, fos, '\x002', null, true); Log.Debug(String.Format("Creating Certificate for doc {0}", doc.Docname)); Org.BouncyCastle.X509.X509Certificate[] chain = crearCertificado(); Log.Debug(String.Format("Reading private key for doc {0}", doc.Docname)); AsymmetricKeyParameter pk = readPrivateKey(); stp.Writer.CloseStream = false; LtvVerification v = stp.LtvVerification; AcroFields af = stp.AcroFields; Log.Debug(String.Format("Adding metadata for doc {0}", doc.Docname)); stp.MoreInfo = metadata; foreach (String sigName in af.GetSignatureNames()) { v.AddVerification(sigName, new OcspClientBouncyCastle(), new CrlClientOffline(null), LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); } PdfSignatureAppearance sap = stp.SignatureAppearance; sap.Reason = ""; sap.Location = ""; //Preserve some space for the contents int contentEstimated = 15000; Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>(); exc.Add(PdfName.CONTENTS, (contentEstimated * 2 + 2)); //Add timestamp Log.Debug(String.Format("Adding timestamp for doc {0}", doc.Docname)); TSAClientBouncyCastle tsc = new TSAClientBouncyCastle(Properties.Settings.Default.tsaUrl, Properties.Settings.Default.tsaUser, Properties.Settings.Default.tsaPass, contentEstimated, DigestAlgorithms.SHA512); // Creating the signature //LtvTimestamp.Timestamp(sap, tsc, null); //Org.BouncyCastle.Crypto.BouncyCastleDigest messageDigest = MessageDigest.getInstance("SHA1"); //IExternalDigest digest = new Org.BouncyCastle.Crypto.BouncyCastleDigest(); //RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey; Log.Debug(String.Format("Dreating signature for doc {0}", doc.Docname)); IExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA512); MakeSignature.SignDetached(sap, signature, chain, null, null, tsc, 0, CryptoStandard.CMS); Log.Debug(String.Format("Closing file for doc {0}", doc.Docname)); stp.Close(); fos.Close(); reader.Close(); } catch (IOException ex) { Log.Error("IOException", ex); } catch (DocumentException dex) { Log.Error("DocumentException", dex); } }
/** * * */ public static void signPDF(DocumentData doc, String metadata, GraphSign sign, Signer signer) { ILog Log; Log = LogManager.GetLogger(Properties.Settings.Default.logName); if (!File.Exists(doc.Docsignedpath /*+ "-signed.pdf"*/)) { File.Copy(doc.Docpath, doc.Docsignedpath); } try { PdfReader reader = new PdfReader(doc.Docsignedpath); if (File.Exists(doc.Docsignedpath + "-signed.pdf")) File.Delete(doc.Docsignedpath + "-signed.pdf"); FileStream fos = new FileStream(doc.Docsignedpath + "-signed.pdf", FileMode.CreateNew, FileAccess.Write); doc.Docsignedpath = doc.Docsignedpath + "-signed.pdf"; Log.Debug(String.Format("Creating Stamper for doc {0}",doc.Docname)); PdfStamper stp = PdfStamper.CreateSignature(reader, fos, '\0', null, true); Log.Debug(String.Format("Creating Certificate for doc {0}", doc.Docname)); Org.BouncyCastle.X509.X509Certificate[] chain = crearCertificado(); Log.Debug(String.Format("Reading private key for doc {0}", doc.Docname)); AsymmetricKeyParameter pk = readPrivateKey(); stp.Writer.CloseStream = false; LtvVerification v = stp.LtvVerification; //AcroFields af = stp.AcroFields; Log.Debug(String.Format("Adding metadata for doc {0}", doc.Docname)); //stp.MoreInfo = metadata; // foreach (String sigName in af.GetSignatureNames()) // { // v.AddVerification(sigName, new OcspClientBouncyCastle(), new CrlClientOffline(null), LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); // } PdfSignatureAppearance sap = stp.SignatureAppearance; //sap.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; sap.Reason = metadata; sap.Location = ""; sign.Image.MakeTransparent(); sap.Image = iTextSharp.text.Image.GetInstance(sign.Image, System.Drawing.Imaging.ImageFormat.Png); sap.Layer2Text = ""; //Preserve some space for the contents int contentEstimated = 15000; Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>(); exc.Add(PdfName.CONTENTS, (contentEstimated * 2 + 2)); //Add timestamp Log.Debug(String.Format("Adding timestamp for doc {0}", doc.Docname)); TSAClientBouncyCastle tsc = new TSAClientBouncyCastle(Properties.Settings.Default.tsaUrl, Properties.Settings.Default.tsaUser, Properties.Settings.Default.tsaPass, contentEstimated, DigestAlgorithms.SHA512); // Creating the signature //LtvTimestamp.Timestamp(sap, tsc, null); //Org.BouncyCastle.Crypto.BouncyCastleDigest messageDigest = MessageDigest.getInstance("SHA1"); //IExternalDigest digest = new Org.BouncyCastle.Crypto.BouncyCastleDigest(); //RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey; Log.Debug(String.Format("Dreating signature for doc {0}", doc.Docname)); IExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA512); sap.Layer2Text = "Huella: "+signature.GetHashCode(); sap.SetVisibleSignature(new Rectangle(signer.X, signer.Y, 200, 200), signer.Page, signer.Nombre+index); MakeSignature.SignDetached(sap, signature, chain, null, null, tsc, 0, CryptoStandard.CMS); Log.Debug(String.Format("Closing file for doc {0}", doc.Docname)); stp.Close(); fos.Close(); reader.Close(); } catch (IOException ex) { Log.Error("IOException", ex); } catch (DocumentException dex) { Log.Error("DocumentException", dex); } }
/** * @Method open a existing pdf file * @Param path path of pdfFile * @Return returns true if the file was opened or false if file couldn't be opened */ private bool open(DocumentData pdfDoc) { String path; pdfDoc.Docsignedpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+ "\\"+ pdfDoc.Idoperation + "\\signed\\" + pdfDoc.Docname; if (pdfDoc.documentHasBeenSigned()) { path = pdfDoc.Docpath + "-signed.pdf"; //signedpath = } else { path = pdfDoc.Docpath; } if (File.Exists(pdfDoc.Docpath)) { doc = new Document(); reader = new PdfReader(path); fos = new FileStream(pdfDoc.Docsignedpath, FileMode.Create, FileAccess.Write); writer = PdfWriter.GetInstance(doc, fos); doc.Open(); doc.AddDocListener(writer); return true; } return false; }
/** * @Method Method that inserts the GraphSign in the pdf document and call for signing it * @Params DocumentData source document to be signed * @Params GraphSign sign Image and metadata about the Graphical Sign * @Params jsonSign Graphometric info JSON serialized * @Params signer data * @Return true if the document is correctly signed, false if something wrong */ internal bool doSignature(DocumentData doc, GraphSign sign, String jsonSign, Signer signer, bool b) { String encrypted = jsonSign; return this.doSignature(doc, sign, encrypted,signer); }
/** * @Method Method that inserts the GraphSign in the pdf document and call for signing it * @Params DocumentData source document to be signed * @Params GraphSign sign Image and metadata about the Graphical Sign * @Params signArray String Array with Graphometric info * @Params signer data * @Return true if the document is correctly signed, false if something wrong */ internal bool doSignature(DocumentData doc, GraphSign sign, string[] signArray, Signer signer) { String encrypted =""; foreach (String s in signArray) encrypted += s; //this.Log.Debug("Sign String to encrypt " + encrypted); encrypted = DigitalSignUtils.encrypt(encrypted); //this.Log.Debug("Sign String encrypted " + encrypted); return this.doSignature(doc, sign, encrypted,signer); }