向 PDF 添加时间戳会损坏文件
Adding a timestamp to a PDF corrupts the file
我正在尝试使用 TSA 服务器向 PDF 文件添加时间戳,但在添加时间戳后,Adobe Reader 说文档自签名后已被更改或损坏。
测试代码:
@SpringBootApplication
public class TestTimestampApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TestTimestampApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
TSAClient tsa = new TSAClientBouncyCastle("http://tsa.buenosaires.gob.ar/TSS/HttpTspServer");
try (OutputStream os = new FileOutputStream("I:/output.pdf")) {
PdfReader reader = new PdfReader("I:/input.pdf");
PdfStamper stamper = PdfStamper.createSignature(reader, os, '[=10=]', null, true);
PdfSignatureAppearance sap = stamper.getSignatureAppearance();
LtvTimestamp.timestamp(sap, tsa, "Atenea");
}
}
}
我正在使用这些框架来做时间戳:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.58</version>
</dependency>
当我打开输出文件时,我得到了这个:
知道问题出在哪里吗??
输出文件示例:https://drive.google.com/file/d/0B5OSF4ESCy5gRU5xTXQxU2NEMmM/view?usp=sharing
谢谢,
朱利安
首先,时间戳TSTInfo
中的MessageImprint
包含正确的时间戳字节范围哈希值。因此,问题一定是不同的。
##扩展密钥用法
TSA 证书的扩展密钥使用扩展的 ASN.1 表示形式如下所示:
951 49: . . . . . . . . SEQUENCE {
953 3: . . . . . . . . . OBJECT IDENTIFIER extKeyUsage (2 5 29 37)
: . . . . . . . . . . (X.509 extension)
958 42: . . . . . . . . . OCTET STRING, encapsulates {
960 40: . . . . . . . . . . SEQUENCE {
962 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . serverAuth (1 3 6 1 5 5 7 3 1)
: . . . . . . . . . . . . (PKIX key purpose)
972 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . codeSigning (1 3 6 1 5 5 7 3 3)
: . . . . . . . . . . . . (PKIX key purpose)
982 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . timeStamping (1 3 6 1 5 5 7 3 8)
: . . . . . . . . . . . . (PKIX key purpose)
992 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . ocspSigning (1 3 6 1 5 5 7 3 9)
: . . . . . . . . . . . . (PKIX key purpose)
: . . . . . . . . . . . }
: . . . . . . . . . . }
: . . . . . . . . . }
这特别意味着证书被标记为用于服务器身份验证、代码签名、时间戳和 OCSP 签名。此外,扩展未标记为关键。
另一方面,RFC 3161 要求:
###2.3. Identification of the TSA
The TSA MUST sign each time-stamp message with a key reserved
specifically for that purpose. A TSA MAY have distinct private keys,
e.g., to accommodate different policies, different algorithms,
different private key sizes or to increase the performance. The
corresponding certificate MUST contain only one instance of the
extended key usage field extension as defined in [RFC2459] Section
4.2.1.13 with KeyPurposeID having value:
id-kp-timeStamping. This extension MUST be critical.
因此,该 TSA 的证书不得用于生成 RFC 3161 时间戳,因此使用它生成的所有时间戳都是无效的。
我正在尝试使用 TSA 服务器向 PDF 文件添加时间戳,但在添加时间戳后,Adobe Reader 说文档自签名后已被更改或损坏。
测试代码:
@SpringBootApplication
public class TestTimestampApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TestTimestampApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
TSAClient tsa = new TSAClientBouncyCastle("http://tsa.buenosaires.gob.ar/TSS/HttpTspServer");
try (OutputStream os = new FileOutputStream("I:/output.pdf")) {
PdfReader reader = new PdfReader("I:/input.pdf");
PdfStamper stamper = PdfStamper.createSignature(reader, os, '[=10=]', null, true);
PdfSignatureAppearance sap = stamper.getSignatureAppearance();
LtvTimestamp.timestamp(sap, tsa, "Atenea");
}
}
}
我正在使用这些框架来做时间戳:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.58</version>
</dependency>
当我打开输出文件时,我得到了这个:
知道问题出在哪里吗??
输出文件示例:https://drive.google.com/file/d/0B5OSF4ESCy5gRU5xTXQxU2NEMmM/view?usp=sharing
谢谢, 朱利安
首先,时间戳TSTInfo
中的MessageImprint
包含正确的时间戳字节范围哈希值。因此,问题一定是不同的。
##扩展密钥用法
TSA 证书的扩展密钥使用扩展的 ASN.1 表示形式如下所示:
951 49: . . . . . . . . SEQUENCE {
953 3: . . . . . . . . . OBJECT IDENTIFIER extKeyUsage (2 5 29 37)
: . . . . . . . . . . (X.509 extension)
958 42: . . . . . . . . . OCTET STRING, encapsulates {
960 40: . . . . . . . . . . SEQUENCE {
962 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . serverAuth (1 3 6 1 5 5 7 3 1)
: . . . . . . . . . . . . (PKIX key purpose)
972 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . codeSigning (1 3 6 1 5 5 7 3 3)
: . . . . . . . . . . . . (PKIX key purpose)
982 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . timeStamping (1 3 6 1 5 5 7 3 8)
: . . . . . . . . . . . . (PKIX key purpose)
992 8: . . . . . . . . . . . OBJECT IDENTIFIER
: . . . . . . . . . . . . ocspSigning (1 3 6 1 5 5 7 3 9)
: . . . . . . . . . . . . (PKIX key purpose)
: . . . . . . . . . . . }
: . . . . . . . . . . }
: . . . . . . . . . }
这特别意味着证书被标记为用于服务器身份验证、代码签名、时间戳和 OCSP 签名。此外,扩展未标记为关键。
另一方面,RFC 3161 要求:
###2.3. Identification of the TSA
The TSA MUST sign each time-stamp message with a key reserved specifically for that purpose. A TSA MAY have distinct private keys, e.g., to accommodate different policies, different algorithms, different private key sizes or to increase the performance. The corresponding certificate MUST contain only one instance of the extended key usage field extension as defined in [RFC2459] Section 4.2.1.13 with KeyPurposeID having value:
id-kp-timeStamping. This extension MUST be critical.
因此,该 TSA 的证书不得用于生成 RFC 3161 时间戳,因此使用它生成的所有时间戳都是无效的。