java - can't verify pdf signatures. itext, pdf, GOST3410 -
i'am trying verify signatures in pdf file. there 3 of them. have signed file code i've found in internet , adopted needs, might encorrect too. here signed file pdf file
verifier code here:
package com.mycompany.verifysignature; import java.io.bytearrayoutputstream; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.hashmap; import java.util.map; import org.bouncycastle.crypto.digests.gost3411digest; import ru.cryptopro.cades.cadessignature; import ru.cryptopro.cades.cadestype; public class main { public static void main(string args[]) { try { arraylist<map<string, string>> reslist = new arraylist<map<string, string>>(); inputstream pdfis = new fileinputstream("/home/user1/desktop/321-17.pdf"); com.itextpdf.text.pdf.pdfreader reader = new com.itextpdf.text.pdf.pdfreader(pdfis); bytearrayoutputstream baos = new bytearrayoutputstream(); com.itextpdf.text.pdf.pdfstamper stamper = com.itextpdf.text.pdf.pdfstamper.createsignature(reader, baos, '\0'); com.itextpdf.text.pdf.pdfsignatureappearance sap = stamper.getsignatureappearance(); com.itextpdf.text.pdf.acrofields fields = reader.getacrofields(); (string signame : fields.getsignaturenames()) { hashmap<string, string> m = new hashmap(); m.put("name", signame.tostring()); system.out.println("name:"+signame); com.itextpdf.text.pdf.pdfdictionary sig = fields.getsignaturedictionary(signame); if (sig != null && sig.getasstring(com.itextpdf.text.pdf.pdfname.reason) != null) { m.put("reason", sig.getasstring(com.itextpdf.text.pdf.pdfname.reason).tostring() .replaceall("\"", "\\\"")); system.out.println("reason:"+sig.getasstring(com.itextpdf.text.pdf.pdfname.reason).tostring() .replaceall("\"", "\\\"")); } else { m.put("reason", "undefined"); system.out.println("reason:undefined"); } byte signature[] = null; if (sig != null && sig.getbytes() != null) { signature = sig.getbytes(); } byte hash[] = calchash(sap.getrangestream()); if (hash != null) { cadessignature cadessignature = new cadessignature(signature, hash, cadestype.cades_x_long_type_1); try { cadessignature.verify(null); m.put("valid", "true"); system.out.println("valid:true"); } catch(exception ex) { m.put("valid", "false"); system.out.println("valid:false"); } } else { m.put("valid", "\"undefined\""); system.out.println("valid:undefined"); } // com.itextpdf.text.pdf.security.pdfpkcs7 pk = fields.verifysignature(signame); // // m.put("valid", new boolean(pk.verify()).tostring()); // system.out.println("valid:"+new boolean(pk.verify()).tostring()); reslist.add(m); } } catch (exception ex) { ex.printstacktrace(); } } public static byte[] calchash(inputstream is) { if (is == null) return null; try { gost3411digest digest = new gost3411digest(); byte node[] = readbytesfromstream(is); digest.update(node, 0, node.length); byte[] resbuf = new byte[digest.getdigestsize()]; digest.dofinal(resbuf, 0); return resbuf; } catch (throwable e) { e.printstacktrace(); //throw new exception(e); } return null; } private static byte[] readbytesfromstream(inputstream is) throws exception { arraylist<object[]> c = new arraylist(); int n, size = 0; byte b[] = null; if (is == null) throw new exception("input stream null"); try { while ((n = is.read(b = new byte[1024])) > 0) { c.add(new object[] { n, b }); size += n; } } catch (ioexception e) { e.printstacktrace(); } byte rv[] = new byte[size]; int pos = 0; (object[] bb : c) { (int = 0; < (integer) bb[0]; i++) { rv[pos++] = ((byte[]) bb[1])[i]; } } return rv; } }
i have signed file's digest, made gost3411, test certificate, generated on cryptopro site.
when open file pdf reader, says there 3 signatures. have realy signed 3 times. code above takes out pdf signature names not equal names wrote. signature1, signature2 etc. there should written "cn" in 3 cases. please help. have made wrong?
the file provided op, 321-174.pdf, signed using 1 signature, not three, , prime error contents of signature dictionary content not cms signature instead textually, base64 encoded. thus, decoding in-between in code seems necessary.
that been said cannot find gost3410 in table 257 – subfilter value algorithm support - of specification iso 32000-1- use in context wont accepted.
Comments
Post a Comment