hi to all,
sorry for the post, i've searched the forum but none of the solution has
encountered my problem.

I'va an external Signature Service that give me the signed hash in SHA256.
Now i've to insert this hash into a pdf file.
The code is:

//Richiesta Certificato Utente
            UserCertificateRequest certificateRequest = new
UserCertificateRequest();
            certificateRequest.applUserId = "tihrcoll";
            certificateRequest.applPwd = "tihrcoll1!";
            certificateRequest.codTitolare = "TEITHR02A02A002A";
            UserCertificateRequestOUT certificateResponse =
proxy.UserCertificateRequest(certificateRequest);

            System.Text.UTF8Encoding encoding = new
System.Text.UTF8Encoding();
            byte[] byteArray =
encoding.GetBytes(certificateResponse.certArray[0].certPEM);
            MemoryStream stream = new MemoryStream(byteArray);
            X509CertificateParser certParser = new X509CertificateParser();
            X509Certificate cert = certParser.ReadCertificate(stream);
            Org.BouncyCastle.X509.X509Certificate[] chain = new
Org.BouncyCastle.X509.X509Certificate[] { cert };

            //Istanza Variabili per gestione PDF
            PdfReader reader = new PdfReader(FILE_ORIGINARIO);
            FileStream fout = new FileStream(FILE_FIRMATO, FileMode.Create,
FileAccess.Write);
            PdfStamper stp = PdfStamper.CreateSignature(reader, fout, '\0');
            PdfSignatureAppearance sap = stp.SignatureAppearance;
            sap.SetCrypto(null, chain, null,
PdfSignatureAppearance.WINCER_SIGNED);

            sap.Contact = "ITS Lab";
            sap.Reason = "Test Firma Digitale";
            sap.Location = "Torre Annunziata (Na)";
            sap.SignDate = DateTime.Now;
            sap.SetVisibleSignature(new Rectangle(100, 100, 200, 200), 1,
"Signature");
            sap.SetExternalDigest(new byte[512], new byte[20], "RSA");
            sap.PreClose();

            //Calcolo hash del file da firmare
            IDigest messageDigest = DigestUtilities.GetDigest("SHA256");
            Stream data = sap.RangeStream;
            byte[] hash = new byte[messageDigest.GetDigestSize()];
            messageDigest.DoFinal(hash, 0);

            //Invocazione WebService e calcolo hashSigned

                PdfSigGenericPKCS tSig = sap.SigStandard;
                PdfLiteral tPDFLiteral =
(PdfLiteral)tSig.Get(PdfName.CONTENTS);
                byte[] tOutBytes = new byte[(tPDFLiteral.PosLength - 2) /
2];
                //PdfPKCS7 tSigner = tSig.Signer;
                PdfPKCS7 tSigner = new PdfPKCS7(null, chain, null, "SHA256",
false);
                tSigner.SetExternalDigest(hash,
encoding.GetBytes(hashResponse.hashSignedArray[0]), "RSA");
                PdfDictionary tPDFDic = new PdfDictionary();

                byte[] tSsig = tSigner.GetEncodedPKCS7();
                System.Array.Copy(tSsig, 0, tOutBytes, 0, tSsig.Length);
                tPDFDic.Put(PdfName.CONTENTS, new
PdfString(tOutBytes).SetHexWriting(true));
                sap.Close(tPDFDic);

And the signature is ok.
Now i've to timestamping the signature field.
I've used the code:

PdfReader reader = new PdfReader(FILE_FIRMATO);
            FileStream fout = new FileStream(FILE_TIMESTAMPED,
FileMode.Create, FileAccess.Write);
            using (PdfStamper stamper = PdfStamper.CreateSignature(reader,
fout, '\0', null, true))
            {
            LtvVerification v = stamper.LtvVerification;
            AcroFields af = stamper.AcroFields;
            foreach (string sigName in af.GetSignatureNames())
            {
                v.AddVerification(sigName, new OcspClientBouncyCastle(), new
CrlClientImp(), LtvVerification.CertificateOption.WHOLE_CHAIN,
LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
            }
            PdfSignatureAppearance sap = stamper.SignatureAppearance;
            sap.CertificationLevel =
PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED;
            // Telecom TSA
            TSAClientBouncyCastle tsa = new TSAClientBouncyCastle(TSA_URL,
TSA_ACCNT, TSA_PASSW, 6500, "sha256");
            LtvTimestamp.Timestamp(sap, tsa, null);

but it append another sing to the document.

What's wrong??? 
Uhm ... any help is appreciated!!!

Thanks!!!
            

--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Insert-TimeStamping-with-external-digest-tp4344945p4344945.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to