java - Obtaining the hash/digest from a PCKS7 signed PDF file with iText -
i'm writing java web service signs pdf documents itext clients in network. documents being signed correctly, , can verified external tools. however, due legal restrictions in order store document in official documentary repository have provide hash/digest message signature.
i have tried hash, closest can obtain whole signature (cert+hash/digest+timestamp) string code snippet (forgive strings , [1] since i'm testing how it):
pdfreader reader = new pdfreader(path); file temp = tempfilemanager.createtempfile("aasd2sd", "asdasda222cff"); pdfstamper stamper = new pdfstamper(reader, new fileoutputstream(temp)); stamper.setrotatecontents(false); pdfstring firma = (pdfstring) stamper.getacrofields().getsignaturedictionary("signature1").get((pdfname)stamper.getacrofields().getsignaturedictionary("signature1").getkeys().toarray()[1]);
with der-enconded pkcs7 signature, far know. but, don't know how decode/read info in order hast.
any idea?
thanks, cris.
first of all, there not the hash/digest message signature, in case of pkcs#7 / cms signatures multiple hashes involved, cf. this answer message digest of pdf in digital signature.
considering need digest fulfill legal restrictions, though, assume after value of signed attribute messagedigest
(if present) etsi.cades.detached or adbe.pkcs7.detached type pdf signatures digest of signed byte ranges.
if want using itext classes (i.e. not security provider classes), have overcome small issue value after stored in private member (pdfpkcs7.digestattr
). reflection allows access it, though:
void extracthashes(pdfreader reader) throws exception { acrofields acrofields = reader.getacrofields(); list<string> names = acrofields.getsignaturenames(); (string name: names) { pdfpkcs7 pdfpkcs7 = acrofields.verifysignature(name); pdfpkcs7.verify(); field digestattrfield = pdfpkcs7.class.getdeclaredfield("digestattr"); digestattrfield.setaccessible(true); byte[] digestattr = (byte[]) digestattrfield.get(pdfpkcs7); // process digest value in digestattr } }
you can find method used in more complete example extracthash.java outputs gigest algorithm , digest value of signature fields in pdf document, e.g.:
firstpage11p0022ad_20150202164018_307494.pdf signature1 digest algorithm: sha1 hash: 4ac0ed7c2ec611d491f37b5ca74598237b85dbab
Comments
Post a Comment