Hi, I have created a simple pdf signer application in iText. I wanted to add external pkcs7 signature to a pdf file. First I created a hash from pdf data, then I created a pkcs7 signature or timestamp file with this hash, after that I added the created hash to the pdf file. After that, when I wanted to open the pdf file with Adobe acrobat 7, then I always get "Signature contains incorrect, unrecognized, corrupt or suspicious data. Support Information: SigDict /Contents illegal data." When the signature is invalid (wrong hash), then I get different error. The same happens when I copies a pkcs7 data from a signed pdf with valid signature. Below you can see my code, maybe someone can spot the bug. Unfortunatelly I could not find! :(
First function makes the hash and second function makes the signature.
I have attached my output pdf file.
public static void hash_pdf (String pdfname) throws Exception
{
PdfReader reader = new PdfReader(pdfname);
PdfStamper stp = PdfStamper.createSignature(reader, null,
'\0', new File ("d:\\temp\\"));
PdfSignatureAppearance sap = stp.getSignatureAppearance();
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
sap.setLayer2Text("Alairas.");
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.FT, PdfName.SIG);
//dic.put(PdfName.FILTER, new PdfName("Adobe.PPKMS"));
dic.put(PdfName.FILTER, new PdfName("Adobe.PPKLite"));
dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached"));
sap.setCryptoDictionary(dic);
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(0x3502));
sap.preClose(exc);
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte buf[] = new byte[8192];
int n;
InputStream inp = sap.getRangeStream();
while ((n = inp.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
String outhash = convertToHexString (hash);
System.out.println(outhash);
}
public static void create_pdf (String pdfname, String
pkcs7signature, String outputfilename) throws Exception
{
PdfReader reader = new PdfReader(pdfname);
FileOutputStream fout = new FileOutputStream(outputfilename);
PdfStamper stp = PdfStamper.createSignature(reader, fout,
'\0',new File ("d:\\temp\\"));
PdfSignatureAppearance sap = stp.getSignatureAppearance();
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
sap.setLayer2Text("Alairas.");
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.FT, PdfName.SIG);
//dic.put(PdfName.FILTER, new PdfName("Adobe.PPKMS"));
dic.put(PdfName.FILTER, new PdfName("Adobe.PPKLite"));
dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached"));
sap.setCryptoDictionary(dic);
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(0x3502));
//exc.put(PdfName.M, new Integer(0x19));
//exc.put(PdfName.NAME, new Integer(0x1f));
sap.preClose(exc);
PdfDictionary dic2 = new PdfDictionary();
byte[] pdfsignature = convertHexString (pkcs7signature);
dic2.put(PdfName.CONTENTS, new
PdfString(pdfsignature).setHexWriting(true));
sap.close(dic2);
}
Thanks your help in advance!
Best regards!
Tamas
hello-timestamped.pdf
Description: Adobe PDF document
