gpt4 book ai didi

java - 如何将证书信息与已签名的数据关联?

转载 作者:行者123 更新时间:2023-11-30 06:22:34 25 4
gpt4 key购买 nike

我正在尝试使用 pdf-box 库(v2.0.8)向 pdf 文档添加数字签名。我正在从网络服务接收已签名的内容(仅使用私钥签名)。现在我需要将证书信息与此签名数据相关联,以便将其添加到 PDF 文档中。我们如何将证书添加到已签名的内容中,最好使用 bouncy caSTLe api ?

// here content is data which has to be signed
public byte[] sign(InputStream content) throws IOException {
try {
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
List<Certificate> certList = new ArrayList<Certificate>();
certList.add(certificate);
Store certs = new JcaCertStore(certList);
gen.addCertificates(certs);

CMSProcessableInputStream msg = new CMSProcessableInputStream(signPrivate(content));
CMSSignedData signedData = gen.generate(msg, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DEROutputStream dos = new DEROutputStream(baos);
dos.writeObject(signedData.toASN1Structure());
return baos.toByteArray();
} catch (Exception e) {
throw new IOException(e);
}
}

在这里,我能够生成数字签名,但它不包含任何证书信息。我已经检查过thisthis问题,但他们不考虑内容已经使用私钥单独签名并且只需要关联证书的情况。

最佳答案

(您发布的代码涉及 CMS 签名容器,因此我假设我们正在讨论 adbe.pkcs7.detachedETSI.CAdES.detached PDF 签名。)

在 CMS 签名容器中创建签名时,可以选择签名值是真的仅对文档数据(的哈希值)进行签名,还是对所谓的签名属性的集合进行签名。 > (SignerInfo 规范中的 signedAttrs),文档数据的哈希值只是这些属性之一的值。

  SignerInfo ::= SEQUENCE {
version CMSVersion,
sid SignerIdentifier,
digestAlgorithm DigestAlgorithmIdentifier,
signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,
signatureAlgorithm SignatureAlgorithmIdentifier,
signature SignatureValue,
unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL }

( RFC 5652 section 5.3. SignerInfo Type )

但是,要认真对待此处的所有配置文件,要求您使用签名属性,特别是它们要求您使用签名的 ESS 签名证书 ( RFC 2634 section 5.4 ) 或 ESS 签名证书-v2 ( RFC 5035 section 3 )引用签名者证书的属性。

因此,在这些属性中,签名与其签名证书的关联在生成签名值之前是固定的。

因此,您无法自由地将签名证书关联到已生成的签名。

关于java - 如何将证书信息与已签名的数据关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com