Java 启用LTV后,签名字节范围无效

Java 启用LTV后,签名字节范围无效,java,itext,amazon-cloudhsm,Java,Itext,Amazon Cloudhsm,我正在使用aws cloudHSM和itext7签署pdf。在我不启用LTV之前,一切都很好 但在启用LTV后,出现错误“至少一个签名有问题”,并显示签名字节范围无效的原因 下面是代码 private void ltvEnable(PdfSigner signer, OutputStream baos, String name11, OcspClientBouncyCastle ocspClient, CrlClientOnline crlClient, CustomTSACli

我正在使用aws cloudHSM和itext7签署pdf。在我不启用LTV之前,一切都很好

但在启用LTV后,出现错误“至少一个签名有问题”,并显示签名字节范围无效的原因

下面是代码

private void ltvEnable(PdfSigner signer, OutputStream baos, String name11,
        OcspClientBouncyCastle ocspClient, CrlClientOnline crlClient, CustomTSAClient tsc) {
    ByteArrayInputStream signedPdfInput = new ByteArrayInputStream(((ByteArrayOutputStream)baos).toByteArray());
    try {
        
        PdfReader pdfReader = new PdfReader(signedPdfInput);
        PdfDocument document = new PdfDocument(pdfReader.setUnethicalReading(true), new PdfWriter(baos),
                new StampingProperties().useAppendMode());
        LtvVerification ltvVerification = new LtvVerification(document);
        SignatureUtil signatureUtil = new SignatureUtil(document);
        List<String> names = signatureUtil.getSignatureNames();
        String sigName = names.get(names.size() - 1);
        PdfPKCS7 pkcs7 = signatureUtil.readSignatureData(sigName);
        if (pkcs7.isTsp()) { 
            ltvVerification.addVerification(sigName, ocspClient, crlClient, LtvVerification.CertificateOption.WHOLE_CHAIN,
                    LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
        } else {
            for (String name : names) {
                ltvVerification.addVerification(name, ocspClient, crlClient, LtvVerification.CertificateOption.WHOLE_CHAIN,
                        LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
            }
        }
        
        ltvVerification.merge();
        //signer.timestamp(tsc, null);
        document.close();
        pdfReader.close();

    } catch (IOException | GeneralSecurityException e) {
        logger.error("Error while making signature ltv enabled");
    }
}
private void ltvEnable(PdfSigner签名者,OutputStream-baos,字符串名称11,
OcspClientBouncyCastle ocspClient、CrlClientOnline crlClient、CustomTSA客户端tsc){
ByteArrayInputStream signedPdfInput=新的ByteArrayInputStream(((ByteArrayOutputStream)baos).toByteArray();
试一试{
PdfReader PdfReader=新PdfReader(签名PDFINPUT);
PdfDocument document=新PdfDocument,
新冲压属性().UseAmpendMode());
LTV验证LTV验证=新的LTV验证(文件);
SignatureUtil SignatureUtil=新的SignatureUtil(文件);
列表名称=signatureUtil.getSignatureNames();
String sigName=names.get(names.size()-1);
PdfPKCS7 pkcs7=signatureUtil.readSignatureData(SignName);
如果(pkcs7.isTsp()){
ltvVerification.addVerification(签名名、ocspClient、crlClient、ltvVerification.CertificateOption.WHOLE_链、,
LtvVerification.Level.OCSP_CRL,LtvVerification.CertificateInclusion.YES);
}否则{
for(字符串名称:名称){
ltvVerification.addVerification(名称、ocspClient、crlClient、ltvVerification.CertificateOption.WHOLE_链、,
LtvVerification.Level.OCSP_CRL,LtvVerification.CertificateInclusion.YES);
}
}
ltvVerification.merge();
//签名者时间戳(tsc,空);
document.close();
pdfReader.close();
}捕获(IOException |一般安全性异常e){
logger.error(“启用签名ltv时出错”);
}
}
在启用ltv-之前:

之后-:


在您的体系结构中,您有一个
ByteArrayOutputStream
参数,在该参数中,您可以将pdf检索到LTV enable,并最终返回LTV enabled结果pdf

在这种体系结构中,必须在从中检索原始内容和向其中添加新内容之间清除
ByteArrayOutputStream

因此,在您的情况下,您必须在

ByteArrayInputStream signedPdfInput=newbytearrayinputstream(((ByteArrayOutputStream)baos.toByteArray());

PdfDocument document=新的PdfDocument(pdfReader.setUnethicalReading(true)、新的PdfWriter(baos),
新冲压属性().UseAmpendMode());

如何启用LTV?请分享关键代码。谢谢@mkl。。。我用代码更新了我的问题。Hhmmm,好的,你能另外提供示例文件吗,一个在应用LTV之前,一个在应用LTV之后?乍一看,代码看起来是正确的。唯一有趣的细节是您使用了
setUnethicalReading(true)
,这可能表明涉及到加密,我还没有测试过加密文档的LTV启用。如果您确实使用加密文件,您还应该尝试激活
StampingProperties.preserveEncryption()
。谢谢@mkl,我解决了这个问题,现在工作很好…非常感谢你的帮助