启用LTV的pdf iText 7

时间:2019-02-13 13:55:19

标签: java itext digital-signature

我已经阅读了许多有关如何使用iText启用pdf ltv的问题/答案。他们俩都不为我工作。我有pdf格式的内容,并设置了一个签名字段,然后使用该字段来调用signDetached方法并对pdf签名。 我使用:

signer.signDetached(new BouncyCastleDigest(), pks, chain,
 Collections.singleton(crlClient), ocspClient, tsc,0, subfilter);

但是什么也没发生。我读到您必须包括除根证书以外的所有证书。我添加了我的私人证书链(我用来签名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;
    }

2 个答案:

答案 0 :(得分:0)

一般来说,LTV无法启用示例签名。

首先,默认情况下,您的签名者证书不是来自Adobe Reader信任的CA,即CA证书既不在AATL上也不在EUTL上。除非它以某种方式信任签名者,否则PDF Reader绝不会将其称为“启用LTV”。

此外,签名者证书没有任何AIA(授权信息访问)扩展名,签名代码可从该扩展名确定要从中检索发行者证书或吊销信息的位置。信息的丢失使得无法自动检索证书和吊销信息。

因此,即使CA被信任,自动启用LTV仍然需要自定义代码。


在这里进行评论(以及与iText支持进行交流)时,发现所提供的示例签名不具有代表性。另一方面,还有其他相关的边界条件,最终您需要在您的用例中创建一个创建支持LTV的签名的例程。

有关此例程的详细信息,请参见您的答案。

答案 1 :(得分:0)

几个小时后,我想出了一个解决方案。需要明确的是,Adobe有自己的信任库,因此您必须使用其证书之一,或者使用Windows的信任库并相应地配置AC Reader,然后在其中添加根证书。大家都提到过,您应该在PDF文档中包括所有认证链。您可以访问我的GitHub project,以查看对PDF文档进行签名,使用私钥对其进行加密并使用ERMIS中的timepstap使用iText 7将其启用ltv的示例。

制作启用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();
相关问题