【问题标题】:How to associate certificate information to already signed data?如何将证书信息与已签名的数据相关联?
【发布时间】:2017-12-15 10:06:29
【问题描述】:

我正在尝试使用 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 签名配置文件中,您需要相关证书创建签名作为对相关证书的引用是签名数据的一部分。
  • @mkl,你的意思是说我们以后不能关联证书,只能在用私钥签署内容的时候完成?

标签: java digital-signature x509certificate bouncycastle pdfbox


【解决方案1】:

(您发布的代码指的是 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) 签名引用签名者证书的属性。

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

因此,您不能随意将签名证书与已生成的签名相关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多