/**
         * @Method calls the Wacom Pad UI for signing and returns the sign data
         * @Return GraphSign if confirmed, null if cancel by the user
         */
        public GraphSign padSigning(Signer signer)
        {
            ILog Log;
            Log = LogManager.GetLogger(Properties.Settings.Default.logName);
            try
            {

                wgssSTU.UsbDevices usbDevices = new wgssSTU.UsbDevices();
                wgssSTU.IUsbDevice usbDevice = usbDevices[0]; // select a device

                WacomPadForm.PresingString = String.Format(Properties.Settings.Default.presingModel, signer.Nombre, signer.Nif);
                WacomPadForm demo = new WacomPadForm(usbDevice,this.minPoints);
                demo.Title = String.Format(Properties.Settings.Default.presingModel, signer.Nombre, signer.Nif);
                //WacomPadUtils.BringToFrontCustom(demo);
                demo.ShowDialog();
                GraphSign result = demo.getSign();
                demo.Dispose();
                return result;

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                return null;
            }
        }
        /**
         * @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;
        }
예제 #3
0
        public static List<Signer> parseSigners(string jsonData)
        {
            List<Signer> result = new List<Signer>();
            dynamic data = JsonConvert.DeserializeObject(jsonData);
            foreach (dynamic obj in data)
            {
                Signer signer = new Signer();
                signer.Nif = obj.nif;
                signer.Nombre = obj.nombre;
                signer.Page = obj.page;
                signer.Telefono = obj.telefono;
                signer.X = obj.x;
                signer.Y = obj.y;
                result.Add(signer);
            }

            return result;
        }
        /**
         *
         *
         */
        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 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);
 }