使用 iText 向 PDF 添加单独的签名和单独的 ocsp 字节

Add separate signature and separate ocsp bytes to PDF with iText

我有一个带有签名字节的数组和一个带有 ocsp 字节的数组,用于将 ltv 信息添加到我的 pdf 中。签名是时间戳签名。现在,我尝试将此信息添加到与

一起使用的 pdf 文件中
//add signature
PdfLiteral pdfLiteral = (PdfLiteral) pdfSignature.get( PdfName.CONTENTS );
byte[] outc = new byte[ ( pdfLiteral.getPosLength( ) - 2 ) / 2 ];
Arrays.fill( outc, (byte) 0 );
System.arraycopy( externalSignature, 0, outc, 0, externalSignature.length );
PdfDictionary dic2 = new PdfDictionary( );
PdfString pdfString = new PdfString( outc ).setHexWriting( true );
dic2.put( PdfName.CONTENTS, pdfString );

pdfSignatureAppearance.close( dic2 );

...

//add LTV
for ( String sigName : stamper.getAcroFields( ).getSignatureNames( ) ) {
        addVerification = validation.addVerification(
                sigName, // Signature Name
                ocspColl, // OCSP
                crlColl, // CRL
                null // certs
        );
    }

validation.merge( );
stamper.close( );

好的,这行得通。但是在签名中我必须设置标志认证级别 1(不允许更改)并且我的应用程序将无法运行。因为添加 LTV 我更改了文档。我尝试创建自己的 DSS 词典并将其添加到我的文档中,但失败了。 请问有谁可以帮忙吗?提前致谢

作为 ISO-32000-2 ISO 委员会的成员,我可以访问该规范,所以请允许我解释几件事。

There can be 3 types of signatures (*) in a PDF document:

  • At most one certification signature (aka author signature),
  • One or more approval signatures (aka recipient signatures),
  • Any number of document time stamp signatures (of which the SubFilter value is ETSI.RFC3161).

(*) There is a fourth type, called a usage rights signature, but that will be deprecated in PDF 2.0.

Source: section 12.8.1 entitled "General" in section 12.8 entitled "Digital Signatures" of ISO 32000-2 (Draft)

您在谈论文档时间戳签名(为 LTV 原因添加),但您也在谈论 DocMDP(MDP 代表修改检测和预防)。

MDP 仅适用于认证签名:

The DocMDP transform method shall be used to detect modifications relative to a signature field that is signed by the author of a document (the person applying a certification signature). A document can contain only one signature field that contains a DocMDP transform method. It enables the author to specify what changes shall be permitted to be made to the document and what changes invalidate the author’s signature.

Source: section 12.8.2.2.1 entitled "General" in section 12.8.2.2 entitled "DocMDP" of ISO 32000-2 (Draft)

当然,您可以向包含 "An array of signature reference dictionaries".

的签名词典添加 Reference 条目

签名参考字典可以有一个 TransformMethod 条目,其值为:

  • DocMDP: Used to detect modifications to a document relative to a signature field that is signed by the originator of a document.
  • FieldMDP: Used to detect modifications to a list of form fields specified in TransformParams.

Source: Table 259 "Entries in a signature reference dictionary" in section 12.8.1 of ISO 32000-1

如果需要MDP,认证签名可以使用DocMDP,其他签名可以使用FieldMDP。但是,如果我们查看 Reference 条目 Table 255: Entries in a signature dictionary,我们会看到 "If SubFilter is ETSI.RFC3161, this entry shall not be used."

换句话说,您不能为文档时间戳使用 DocMDP 或 FieldMDP。这很正常,因为正如我们在第 12.8.5.1 节 "General" 和第 12.8.5 "Document time-stamp (DTS) dictionary":

节中所读到的

A document time-stamp dictionary establishes the exact contents of the complete PDF file at the time indicated in the time-stamp token.

它没有说明将标志设置为 "certification level 1 (no changes are allowed)" 的能力。这不是文档时间戳的用途。代码失败是正常的。

设置认证级别只能通过第一个签名来完成,以防第一个签名是认证签名。告诉您使用文档时间戳签名 (ETSI.RFC3161) 设置认证级别的人是在要求您做一些根据即将发布的 PDF 2.0 ISO 标准不可能完成的事情。

如果您的后续问题是:ISO 32000-1 中是如何完成的,那么答案很简单:ISO 32000-1 中不存在文档时间戳签名。