我正在尝试验证pdf文件中的签名。一共有三个。我已经使用在Internet上找到并已满足我需要的代码对文件进行了签名,因此它可能也是正确的。这是签名文件pdf file
验证程序代码在这里:
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();
for (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 is 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;
for (Object[] bb : c) {
for (int i = 0; i < (Integer) bb[0]; i++) {
rv[pos++] = ((byte[]) bb[1])[i];
}
}
return rv;
}
}
我已经用cryptocert网站上生成的带有测试证书的GOST3411进行了文件摘要签名。
当我使用pdf阅读器打开此文件时,它说有3个签名。我确实签署了三遍。但是上面的代码从pdf签名名中删除了,这些签名名与我写的名字不相等。它们看起来像Signature1,Signature2等。在所有三种情况下都应写为“ CN”。请帮忙。我做错了什么?
最佳答案
OP提供的文件321-174.pdf仅使用一个签名而不是三个签名进行签名,主要的错误是签名字典内容的内容不是CMS签名,而是文本形式的内容,可能是base64编码的。因此,您的代码之间需要进行一些解码。
话虽如此,我在规范ISO 32000-1的表257(SubFilter值算法支持)中找不到GOST3410,因此很可能不会接受在这种情况下使用它。