When adding another signature or upgrading the initial signature from PAdES B-T to PAdES B-LT:
byte[] addLTV(byte[] in)
throws IOException, GeneralSecurityException, java.io.IOException {
IOcspClient ocsp = new OcspClientBouncyCastle(new com.itextpdf.signatures.OCSPVerifier(null, null));
ICrlClient crl = new CrlClientOnline();
PdfReader reader = new PdfReader(new ByteArrayInputStream(in));
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(out);
PdfDocument pdfDoc = new PdfDocument(reader, writer, new StampingProperties().useAppendMode());
LtvVerification v = new LtvVerification(pdfDoc);
SignatureUtil signatureUtil = new SignatureUtil(pdfDoc);
List<String> names = signatureUtil.getSignatureNames();
String sigName = names.get(names.size() - 1);
PdfPKCS7 pkcs7 = signatureUtil.readSignatureData(sigName);
if (pkcs7.isTsp()) {
v.addVerification(sigName, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN,
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.YES);
}
}
v.merge();
pdfDoc.close();
return out.toByteArray();
}
the initial signature becomes invalid with an error “the document has been altered or corrupted since the Signature was applied.
Upon inspection, I found that the initial signature becomes invalid as soon as it’s read by PdfDocument or PdfSigner.
String srcFileName = "./BT.pdf";
String outFileName = "./B-LT01.pdf";
PdfDocument document = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(outFileName), new StampingProperties().useAppendMode());
document.close();
The code works fine with other PDF files. How can I resolve this issue?
my pdf file
Vinh Dương is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.