我在使用受信任的时间戳与Bouncy Castle创建有效的CMS签名时遇到问题。签名创建工作良好(我想将签名包括到PDF文件中),签名有效。但是,当我在签名的未签名属性表中添加可信时间戳后,签名仍然保持有效,但是阅读器报告该签名包括嵌入式时间戳,但是无效。这使我相信,哈希I时间戳不是正确的时间戳,但是我似乎无法弄清楚问题出在哪里。

签名代码:

Store store = new JcaCertStore(Arrays.asList(certContainer.getChain()));

CMSSignedDataGenerator signedDataGenerator = new CMSSignedDataGenerator();
JcaSignerInfoGeneratorBuilder infoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build());
JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder("SHA1withRSA");
signedDataGenerator.addSignerInfoGenerator(
                       infoGeneratorBuilder.build(contentSignerBuilder.build(certContainer.getPrivateKey()), (X509Certificate)certContainer.getSignatureCertificate()));
signedDataGenerator.addCertificates(store);
CMSTypedData cmsData = new CMSProcessableByteArray(data);
signedData = signedDataGenerator.generate(cmsData, false);
Collection<SignerInformation> ss = signedData.getSignerInfos().getSigners();
SignerInformation si = ss.iterator().next(); // get first signer (should be only one)
ASN1EncodableVector timestampVector = new ASN1EncodableVector();
Attribute token = createTSToken(si.getSignature());
timestampVector.add(token);
AttributeTable at = new AttributeTable(timestampVector);
si = SignerInformation.replaceUnsignedAttributes(si, at);
ss.clear();
ss.add(si);
SignerInformationStore newSignerStore = new SignerInformationStore(ss);
CMSSignedData newSignedData = CMSSignedData.replaceSigners(signedData, newSignerStore);
createTSToken代码:
public Attribute createTSToken(byte[] data) throws NoSuchProviderException, NoSuchAlgorithmException, IOException {
    // Generate timestamp
    MessageDigest digest = MessageDigest.getInstance("SHA1", "BC");
    TimeStampResponse response = timestampData(digest.digest(data));
    TimeStampToken timestampToken = response.getTimeStampToken();
    // Create timestamp attribute

    Attribute a = new Attribute(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, new DERSet(ASN1Primitive.fromByteArray(timestampToken.getEncoded())));
    return a;
}
timestampData:
TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, data);
byte request[] = req.getEncoded();

URL url = new URL("http://time.certum.pl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "application/timestamp-query");
con.setRequestProperty("Content-length", String.valueOf(request.length));

OutputStream out = con.getOutputStream();
out.write(request);
out.flush();

if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " +                 con.getResponseMessage());
}
InputStream in = con.getInputStream();
TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
response = new TimeStampResponse(resp);
response.validate(req);
if(response.getStatus() != 0) {
    System.out.println(response.getStatusString());
    return null;
}
return response;

谢谢你的帮助!

示例文件:

Signed PDF

Unsigned PDF

Signed PDF with iText

Signed PDF with LTV-编辑

最佳答案

Signed_lipsum.pdf,第一版
时间戳记 token 引用作为签名者

CN = e-Szigno Test TSA2,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU

由...发行

CN = Microsec e-Szigno测试根CA 2008,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU

序列号为7。
但是,它本身不提供此证书,并且封装签名CMS容器也不在某些与验证有关的信息PDF文档部分中,也不提供此证书。
因此,至少在我的计算机上,没有机会以任何方式验证时间戳记 token ,并且Adobe Reader完全不接受时间戳记是正确的。
您是否以适合您的Adobe Reader的方式在计算机上提供了相关证书?如果有,但仍然无法使用,请提供它以进行进一步测试。如果还没有,请尝试检索并提供它们。
您可能想增强时间戳记 token 本身,以在将该证书包含到签名中之前包含该证书。
Signed_lipsum.pdf,第二版
在更新的文件signed_lipsum.pdf中,签名时间戳包含一个TSA证书,但这是错误的证书!
就像第一个版本中的时间戳一样,时间戳引用签名者证书

  • 主题CN = e-Szigno Test TSA2,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU
  • Issuer CN = Microsec e-Szigno测试根CA 2008,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU
  • 序列号7.

  • 另一方面,包含的证书具有
  • 主题CN = e-Szigno Test TSA2,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU
  • Issuer CN = Microsec e-Szigno测试根CA 2008,OU = e-Szigno CA,O = Microsec Ltd.,L =布达佩斯,C = HU
  • 序列号5。

  • 我认为测试TSA使用带有单独证书的多个签名设备/软 token ,并且OP包含错误的签名设备/软 token 。
    因此,您可能想要包括正确的证书。
    顺便说一句,由iText签名的PDF中的时间戳包含与戳中的引用匹配的证书...
    RFC 3161时间戳请求可以要求TSA自动包括签署者证书。有弹性的城堡允许这样设置此标志:
    TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
    reqgen.setCertReq(true); // <<<<<<<<<<<<<<<<<<<<<<<<<<
    TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, data);
    
    您可以尝试这样做,而不是自己包含证书。
    启用LTV
    从评论:

    出于好奇,刚启用PDF LTV时需要添加什么额外的内容?

    引用Leonard Rosenthol(Adobe的PDF专家):

    启用LTV意味着其中包含验证文件所需的所有信息(减去根证书)。因此,这一说法是正确的。

    PDF正确签名,并且包含所有必要的证书,每个证书的有效CRL或OSCP响应

    (Jan 10, 2013; 7:07pm;Leonard Rosenthol on itext-general)

    10-04 15:39
    查看更多