无法将ltv添加到PDF文档。错误

时间:2013-06-17 13:43:22

标签: java pdf itext digital-signature digital-certificate

我是使用Itext的签名文件。我有这个方法:

public static void sign(String src, String dest, Certificate[] chain,PrivateKey pk, String digestAlgorithm, String provider,CryptoStandard subfilter, TSAClient tsa )
{

        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');

        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setVisibleSignature(new Rectangle(10, 20, 100, 200), 1, "sig");

        // Creating the signature
        ExternalDigest digest = new BouncyCastleDigest();
        ExternalSignature signature = new PrivateKeySignature(pk,
        digestAlgorithm, provider);
        MakeSignature.signDetached(appearance, digest, signature, chain, null,null, tsa, 0, subfilter);


        // ALREADY SIGNED. ADD LTB NOW.
        CrlClient crlClient = new CrlClientOnline("http://crl.mycrl.com/mycrl.crl");
        OcspClient  ocspClient=new OcspClientBouncyCastle();              
    addLtv(DEST, DEST2, ocspClient, crlClient, tsa);

    }

我签了名:

sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "For Testing", " location", tsa);

的所有内容。 PDF 签名好

然后,我无法添加ltv。我使用itext文档中的代码:

public static void addLtv(String src, String dest, OcspClient ocsp, CrlClient crl,
        TSAClient tsa) throws IOException, DocumentException,
        GeneralSecurityException {

    PdfReader r = new PdfReader(src);
    FileOutputStream fos = new FileOutputStream(dest);

    PdfStamper stp = PdfStamper.createSignature(r, fos, '\0', null, true);
    LtvVerification v = stp.getLtvVerification();

    AcroFields fields = stp.getAcroFields();

    List<String> names = fields.getSignatureNames();
    String sigName = names.get(names.size() - 1);

    PdfPKCS7 pkcs7 = fields.verifySignature(sigName);

    if (pkcs7.isTsp()) {
            v.addVerification(sigName, ocsp, crl,
                LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
                LtvVerification.Level.OCSP_CRL,
                LtvVerification.CertificateInclusion.NO);
    }

    else {
        for (String name : names) {
            v.addVerification(name, ocsp, crl,
                    LtvVerification.CertificateOption.WHOLE_CHAIN,
                    LtvVerification.Level.OCSP_CRL,
                    LtvVerification.CertificateInclusion.NO);
        }
    }
    PdfSignatureAppearance sap = stp.getSignatureAppearance();
    LtvTimestamp.timestamp(sap, tsa, null);
}

EDITED: 一切正常,但在线         LtvTimestamp.timestamp(sap,tsa,null); 我有这个错误:     线程“main”中的异常java.io.IOException:空间不足

那是我的pdf: https://www.dropbox.com/s/o05rw6ubiuslm4j/DOC_SIGNED.pdf

Exception in thread "main" java.io.IOException: Not enough space
at com.itextpdf.text.pdf.security.LtvTimestamp.timestamp(LtvTimestamp.java:103)
at ge.digital.signature.DocumentSigner.DigitalSignature.addLtv(MySignature.java:132)
at ge.digital.signature.DocumentSigner.DigitalSignature.main(MySignature.java:163)

2 个答案:

答案 0 :(得分:3)

当PDF中用于集成时间戳的空间不足时,会发生IOException。因此,您必须更改调用getTokenSizeEstimate方法的TSAClient实例tsa的方法sign,以返回更大的时间戳大小估计值。

如果TSAClientBouncyCastle实现TSAClient,例如,如果使用带有四个参数的构造函数,则可以使其返回任意估计值而不是默认值4096

public TSAClientBouncyCastle(String url, String username, String password, int tokSzEstimate)

某些背景信息:在将签名或文档时间戳集成到PDF中时,首先要准备具有给定大小的间隙的PDF,然后计算除了该间隙之外的所有内容的哈希值,然后哈希的符号或时间戳,最后将生成的签名或时间戳集成到该间隙中,例如

enter image description here

/ ByteRange 条目是签名内容的一部分。因此,之后不能扩大差距。)

因此,您必须在生成签名或时间戳之前使用一些估计值。

如果是文档时间戳,此估算值由TSAClient方法getTokenSizeEstimate.提供

PS:更多细节参见this answer,Adobe文档Digital Signatures in a PDF(我从上面复制了上图),PDF规范ISO 32000-1:2008可用here by Adobe

答案 1 :(得分:0)

如果您没有使用 TSAClientBouncyCastle 而您创建了自己的 TSAClient,则必须设置签名的首选大小,创建一个 org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions 类,然后设置类似 signatureOptions.setPreferredSignatureSize(8192*2)