我已经阅读了很多有关如何使用iText启用pdf ltv的问题/答案。他们都没有为我工作。我有一个pdf的蒸汽,我设置了一个签名字段,然后我用它来调用signDetached方法并签署pdf。我用:
signer.signDetached(new BouncyCastleDigest(), pks, chain,
Collections.singleton(crlClient), ocspClient, tsc,0, subfilter);
但什么都没发生。我已经读过你必须包括除root以外的所有证书。我添加了我的私人证书链(我用它来签署pdf),但我没有找到可能的方式包括TSA的证书。
我使用iText版本7.X.
KeyStore ks = getKeyStore();
Certificate[] chain = null;
Enumeration<String> al = ks.aliases();
for (Enumeration<String> l = al; l.hasMoreElements();) {
String alias = (String) l.nextElement();
chain = ks.getCertificateChain(alias);
}
PrivateKey pk = (PrivateKey) ks.getKey(ks.aliases().nextElement(), "******".toCharArray());
IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, BouncyCastleProvider.PROVIDER_NAME);
OCSPVerifier ocspVerifier = new OCSPVerifier(null, null);
OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle(ocspVerifier);
String url = CertificateUtil.getCRLURL((X509Certificate) chain[0]);
CrlClientOnline crlClient = new CrlClientOnline(url);
try {
signer.signDetached(new BouncyCastleDigest(), pks, chain, Collections.singleton(crlClient), ocspClient, tsc,
0, subfilter);
} catch (Exception ex) {
System.out.println("Tzizzzzzzzzzzzzzzz" + ex.getCause());
}
private KeyStore getKeyStore()
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("tsaPath"), "****".toCharArray());
ks.load(new FileInputStream("p12Path"), "*******".toCharArray());
return ks;
}
几个小时后,我想出了一个解决方案。需要明确的是,Adobe拥有自己的信任库,因此您必须使用其中一个证书或使用Windows的信任库并相应地配置AC Reader并在那里添加根证书。众所周知,您应该在PDF文档中包含所有认证链。您可以访问我的GitHub project,以查看签署PDF文档的工作示例,使用私钥对其进行加密,并使用来自ERMIS的timepstap使用iText 7进行启用。
使ltv启用示例:
private void ltvEnable(PdfSigner signer, ByteArrayOutputStream baos, OutputStream os, String name,
OcspClientBouncyCastle ocspClient, CrlClientOnline crlClient, CustomTSAClient tsc) {
ByteArrayInputStream signedPdfInput = new ByteArrayInputStream(baos.toByteArray());
try {
PdfReader pdfReader = new PdfReader(signedPdfInput);
PdfDocument document = new PdfDocument(pdfReader.setUnethicalReading(true), new PdfWriter(os),
new StampingProperties().useAppendMode());
LtvVerification ltvVerification = new LtvVerification(document);
ltvVerification.addVerification(name, ocspClient, crlClient, LtvVerification.CertificateOption.WHOLE_CHAIN,
LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
ltvVerification.merge();
document.getCatalog().getPdfObject().getAsDictionary(PdfName.DSS).getAsArray(PdfName.Certs)
.add(new PdfStream(
IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("HPARCA_CA.cer"))));
document.close();
pdfReader.close();
} catch (IOException | GeneralSecurityException e) {
LOG.error("Error while making signature ltv enabled");
}
}
棘手的部分参数:
OCSPVerifier ocspVerifier = new OCSPVerifier(null, null);
OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle(ocspVerifier);
CrlClientOnline crlClient = new CrlClientOnline();
一般情况下,LTV无法启用示例签名。
首先,默认情况下,您的签名者证书不是来自Adobe Reader所信任的CA,即CA证书既不在AATL上也不在EUTL上。 PDF阅读器永远不会调用签名“LTV启用”,除非它以某种方式信任签名者。
此外,签名者证书没有任何AIA(授权信息访问)扩展,签名代码可以从中确定从中检索颁发者证书或撤销信息的位置。丢失的信息使得证书和撤销信息的自动检索变得不可能。
因此,即使CA是可信的,自动LTV启用仍然需要自定义代码。
在这里的评论过程中(以及与iText支持人员的沟通),结果证明所提供的示例签名不具代表性;另一方面,还有其他相关的边界条件,你最终得到了一个例程,在你的用例中创建了支持LTV的签名。
有关此例程的详细信息,请参阅答案。