[Digital Signature] Embed P7S signature inside PDF with iText and BouncyCastle
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.
Ok I made this stuff.
First I load the p7s inside a CMSSignedData object, and I put the signature and certificates inside the PDF.
... ok, the process works correctly, if I open the signed pdf I can see the graphics signature and i can read the certificate, but... If i try to validate the signed pdf (with the iText procedure or with acrobat), it said me that the signature is not valid, because the document was modified or damaged.
PKCS7 pkcs7 = new PKCS7(p7s.toByteArray());
CMSSignedData signedData = new CMSSignedData(p7s.toByteArray());
X509Certificate chain2 = pkcs7.getCertificates();
PdfSignatureAppearance sap = stamper.getSignatureAppearance();
sap.setVisibleSignature(new Rectangle(600, 50, 150, 100), 1, null);
sap.setCrypto(null, chain2, null,
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS,
HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
exc.put(PdfName.CONTENTS, new Integer(4000 * 2 + 2));
byte pk = signedData.getEncoded();
byte outc = new byte;
PdfDictionary dic2 = new PdfDictionary();
System.arraycopy(pk, 0, outc, 0, pk.length);
dic2.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));
How can I fix this? Can you help me?
Thanks in advance
Re: [Digital Signature] Embed P7S signature inside PDF with iText and BouncyCastle
I tried to implement the signature with BC from keystore and works well.
I tried after to change only the signedData, using not the data signed with BC but the p7s loaded as byte array... always the same problem... I'm sure that the pdf and his p7s are not corrupted, so probably the problem is when the p7s it's loaded or probably i use the wronge information when i put the signature inside pdf... please help me... are two weeks that i cannot find a solution for this stuff.
Thanks in advance