예제 #1
0
        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();
        }
예제 #2
0
        public virtual void LtvEnabledTest01()
        {
            String tsaCertFileName = certsSrc + "tsCertRsa.p12";
            String caCertFileName  = certsSrc + "rootRsa.p12";
            String srcFileName     = sourceFolder + "signedDoc.pdf";
            String ltvFileName     = destinationFolder + "ltvEnabledTest01.pdf";
            String ltvTsFileName   = destinationFolder + "ltvEnabledTsTest01.pdf";

            X509Certificate[] tsaChain       = Pkcs12FileHelper.ReadFirstChain(tsaCertFileName, password);
            ICipherParameters tsaPrivateKey  = Pkcs12FileHelper.ReadFirstKey(tsaCertFileName, password, password);
            X509Certificate   caCert         = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(caCertFileName, password)[0];
            ICipherParameters caPrivateKey   = Pkcs12FileHelper.ReadFirstKey(caCertFileName, password, password);
            TestTsaClient     testTsa        = new TestTsaClient(JavaUtil.ArraysAsList(tsaChain), tsaPrivateKey);
            TestOcspClient    testOcspClient = new TestOcspClient().AddBuilderForCertIssuer(caCert, caPrivateKey);
            TestCrlClient     testCrlClient  = new TestCrlClient(caCert, caPrivateKey);
            PdfDocument       document       = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(ltvFileName), new StampingProperties
                                                                   ().UseAppendMode());
            LtvVerification ltvVerification = new LtvVerification(document);

            ltvVerification.AddVerification("Signature1", testOcspClient, testCrlClient, LtvVerification.CertificateOption
                                            .SIGNING_CERTIFICATE, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
            ltvVerification.Merge();
            document.Close();
            PdfSigner signer = new PdfSigner(new PdfReader(ltvFileName), new FileStream(ltvTsFileName, FileMode.Create
                                                                                        ), new StampingProperties().UseAppendMode());

            signer.Timestamp(testTsa, "timestampSig1");
            BasicCheckLtvDoc("ltvEnabledTsTest01.pdf", "timestampSig1");
        }
예제 #3
0
        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();
        }
예제 #4
0
        public void AddLtv(String src, String dest, IOcspClient ocsp, ICrlClient crl, ITSAClient tsa)
        {
            PdfReader       r       = new PdfReader(src);
            FileStream      fos     = new FileStream(dest, FileMode.Create);
            PdfStamper      stp     = PdfStamper.CreateSignature(r, fos, '\0', null, true);
            LtvVerification v       = stp.LtvVerification;
            AcroFields      fields  = stp.AcroFields;
            List <String>   names   = fields.GetSignatureNames();
            String          sigName = names[names.Count - 1];
            PdfPKCS7        pkcs7   = fields.VerifySignature(sigName);

            if (pkcs7.IsTsp)
            {
                v.AddVerification(sigName, ocsp, crl, LtvVerification.CertificateOption.SIGNING_CERTIFICATE, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO);
            }
            else
            {
                foreach (String name in names)
                {
                    v.AddVerification(name, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO);
                }
            }
            PdfSignatureAppearance sap = stp.SignatureAppearance;

            LtvTimestamp.Timestamp(sap, tsa, null);
        }
예제 #5
0
        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);
        }
예제 #6
0
파일: MainForm.cs 프로젝트: random9/SignPDF
        void Button4Click(object sender, EventArgs e)
        {
            if (!tsaCbx.Checked || TSAUrlTextBox.Text == "")
            {
                MessageBox.Show("Marca temporale non selezionata, oppure server non definito");
                return;
            }

            string TSA_URL   = TSAUrlTextBox.Text;
            string TSA_ACCNT = tsaLogin.Text;
            string TSA_PASSW = tsaPwd.Text;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string   foldername = fbd.SelectedPath;
                string[] files      = Directory.GetFiles(foldername, "*.pdf");
                pb.Minimum = 0;
                pb.Maximum = files.Length;
                pb.Visible = true;
                lb2.Items.Clear();
                foreach (string s in files)
                {
                    //just filename
                    try {
                        string ext = s.Substring(1 + s.LastIndexOf(@".")).ToLowerInvariant();
                        if (ext == "pdf" || ext == "PDF")
                        {
                            //ricreo il percorso con il nome del nuovo file
                            string          file      = s.Substring(1 + s.LastIndexOf(@"\"));
                            string          NuovoFile = s.Substring(0, s.LastIndexOf(@"\") + 1) + file.Substring(0, file.LastIndexOf(".")) + "_validato_" + DateTime.Now.ToFileTime() + ".pdf";
                            PdfReader       r         = new PdfReader(s);
                            FileStream      fout      = new FileStream(NuovoFile, FileMode.Create);
                            PdfStamper      stp       = PdfStamper.CreateSignature(r, fout, '\0', null, true);
                            LtvVerification v         = stp.LtvVerification;
                            AcroFields      af        = stp.AcroFields;
                            foreach (string sigName in af.GetSignatureNames())
                            {
                                v.AddVerification(sigName, new OcspClientBouncyCastle(), new CrlClientImp(), LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO);
                            }
                            PdfSignatureAppearance sap = stp.SignatureAppearance;
                            TSAClientBouncyCastle  tsa = new TSAClientBouncyCastle(TSA_URL, TSA_ACCNT, TSA_PASSW, 6500, "sha256");
                            LtvTimestamp.Timestamp(sap, tsa, null);
                            lb2.Items.Add(NuovoFile);
                            lb2.Refresh();
                            pb.Increment(1);
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                        pb.Visible = false;
                        return;
                    }
                }
                MessageBox.Show(pb.Maximum.ToString() + " file firmati correttamente", "Operazione Completata");
                pb.Visible = false;
            }
        }
        private void AddLtvInfo(String src, String dest, String sigName, TestOcspClient testOcspClient, TestCrlClient
                                testCrlClient)
        {
            PdfDocument document = new PdfDocument(new PdfReader(src), new PdfWriter(dest), new StampingProperties().UseAppendMode
                                                       ());
            LtvVerification ltvVerification = new LtvVerification(document);

            ltvVerification.AddVerification(sigName, testOcspClient, testCrlClient, LtvVerification.CertificateOption.
                                            WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
            ltvVerification.Merge();
            document.Close();
        }
예제 #8
0
        /// <summary>
        /// Perform LTV
        /// </summary>
        private void enableLTV()
        {
            LtvVerification v     = this.stamper.LtvVerification;
            PdfPKCS7        pkcs7 = this.fields.VerifySignature(this.signatureName);
            CrlClientOnline crl   = new CrlClientOnline(pkcs7.SignCertificateChain);

            if (pkcs7.IsTsp)
            {
                v.AddVerification(signatureName, null, crl,
                                  LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
                                  LtvVerification.Level.CRL,
                                  LtvVerification.CertificateInclusion.NO);
            }
            else
            {
                v.AddVerification(signatureName, null, crl,
                                  LtvVerification.CertificateOption.WHOLE_CHAIN,
                                  LtvVerification.Level.CRL,
                                  LtvVerification.CertificateInclusion.NO);
            }
        }
예제 #9
0
파일: DigitalSig.cs 프로젝트: page2me/TeDA
        public byte[] LTVEnable(byte[] pdf, ITSAClient tsaClient)
        {
            PdfReader    document      = new PdfReader(pdf);
            MemoryStream stream        = new MemoryStream();
            string       signatureName = "";

            PdfStamper pdfStamper = new PdfStamper(document, stream, '0', true);
            //PdfStamper pdfStamper = PdfStamper.CreateSignature(document, stream, '\0');
            AcroFields    fields      = pdfStamper.AcroFields;
            List <string> _fieldNames = fields.GetSignatureNames();

            foreach (string _fieldName in _fieldNames)
            {
                signatureName = _fieldName;
            }

            LtvVerification v     = pdfStamper.LtvVerification;
            PdfPKCS7        pkcs7 = fields.VerifySignature(signatureName);
            CrlClientOnline crl   = new CrlClientOnline(pkcs7.SignCertificateChain);

            if (pkcs7.IsTsp)
            {
                v.AddVerification(signatureName, null, crl,
                                  LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
                                  LtvVerification.Level.CRL,
                                  LtvVerification.CertificateInclusion.NO);
            }
            else
            {
                v.AddVerification(signatureName, null, crl,
                                  LtvVerification.CertificateOption.WHOLE_CHAIN,
                                  LtvVerification.Level.CRL,
                                  LtvVerification.CertificateInclusion.NO);
            }
            pdfStamper.Close();
            //PdfSignatureAppearance sap = pdfStamper.SignatureAppearance;
            //LtvTimestamp.Timestamp(sap, tsaClient, null);

            return(stream.ToArray());
        }
        public virtual void PadesSignatureLevelLTTest01()
        {
            String outFileName     = destinationFolder + "padesSignatureLevelLTTest01.pdf";
            String srcFileName     = sourceFolder + "signedPAdES-T.pdf";
            String tsaCertFileName = certsSrc + "tsCertRsa.p12";
            String caCertFileName  = certsSrc + "rootRsa.p12";

            X509Certificate[] tsaChain      = Pkcs12FileHelper.ReadFirstChain(tsaCertFileName, password);
            ICipherParameters tsaPrivateKey = Pkcs12FileHelper.ReadFirstKey(tsaCertFileName, password, password);
            X509Certificate   caCert        = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(caCertFileName, password)[0];
            ICipherParameters caPrivateKey  = Pkcs12FileHelper.ReadFirstKey(caCertFileName, password, password);
            ICrlClient        crlClient     = new TestCrlClient(caCert, caPrivateKey);
            TestOcspClient    ocspClient    = new TestOcspClient().AddBuilderForCertIssuer(caCert, caPrivateKey);
            TestTsaClient     testTsa       = new TestTsaClient(JavaUtil.ArraysAsList(tsaChain), tsaPrivateKey);
            PdfDocument       document      = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(outFileName), new StampingProperties
                                                                  ().UseAppendMode());
            LtvVerification ltvVerification = new LtvVerification(document);

            ltvVerification.AddVerification("Signature1", ocspClient, crlClient, LtvVerification.CertificateOption.SIGNING_CERTIFICATE
                                            , LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
            ltvVerification.Merge();
            document.Close();
            BasicCheckDssDict("padesSignatureLevelLTTest01.pdf");
        }
예제 #11
0
 private void addVerificationInfo(IOcspClient ocspClient, LtvVerification verification, CrlClientOnline crl, String name)
 {
     verification.AddVerification(name, ocspClient, crl, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
 }