Good Afternoon

For a work I have to sign a PDF.
But not with a common way, because I alredy have the signature and the 
certificates (for example "document.p7s") and obviously the original file 
("document.pdf"), and I have to put the signature inside the PDF.

I made it in this way:


byte[] signature = readFile(path + p7sFile);
// Gets the certificates
PKCS7 pkcs7 = new PKCS7(signature);
X509Certificate[] chain2 = pkcs7.getCertificates();

PdfReader reader = new PdfReader(path + pdfSource);
                PdfStamper stp = PdfStamper.createSignature(reader,
                        new FileOutputStream(path + fileToSign), '\0');
                PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setVisibleSignature(new Rectangle(600, 50, 150, 100), 1, null);
sap.setSignDate(new GregorianCalendar());
sap.setCrypto(null, chain2, null,
                PdfSignatureAppearance.WINCER_SIGNED);
sap.setAcro6Layers(true);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
sap.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);

PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS,
                PdfName.ADBE_PKCS7_DETACHED);
           
dic.setDate(new PdfDate(sap.getSignDate()));
dic.setName(PdfPKCS7.getSubjectFields((X509Certificate) chain[1])
                .getField("CN"));
           
sap.setCryptoDictionary(dic);
       
        HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
        exc.put(PdfName.CONTENTS, new Integer(4000 * 2 + 2));
        sap.preClose(exc);

           
byte[] pk = signature;
byte[] outc = new byte[4000];
PdfDictionary dic2 = new PdfDictionary();

System.arraycopy(pk, 0, outc, 0, pk.length);
dic2.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));

sap.close(dic2);

In pratical I've used the same code used for signing with iText + bouncy 
castle, but i tried to insert an existing signature, and not generate a new 
signature).
But when i try to validate this with Adobe Reader or other tools, said me that 
the certificates are ok, but the signature
is invalid and the document could be modified or alterated.
I cannot understand why gives me this problem.

Are two weeks that we try to fix this problem and we didn't found any help on 
web.

Can you help me about this stuff?

Thanks in advance

Niccolò
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
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