使用 iText7 将撤销信息添加到签名

Add revocation information to signature using iText7

我正在制作长期签名。我正在尝试将吊销信息(Crls、OCSP 响应、证书链)作为未签名属性添加到签名中,但吊销信息未嵌入最终签名中。 以下是代码片段:

        Stream outputStream = new MemoryStream();

        List<byte[]> ocspCollection = new List<byte[]>();
        List<byte[]> crlCollection = new List<byte[]>();
        List<byte[]> certsCollection = new List<byte[]>();

        Stream readerStream = new MemoryStream(signedDocument);
        PdfReader pdfReader = new PdfReader(readerStream);
        PdfSigner pdfSigner = new PdfSigner(pdfReader, outputStream, new StampingProperties().UseAppendMode());

        LtvVerification ltvVerification = new LtvVerification(pdfSigner.GetDocument());

        X509Chain chain = new X509Chain();
        chain.Build(signerCertificate);

        foreach (X509ChainElement item in chain.ChainElements)
        {
            byte[] certBytes = item.Certificate.Export(X509ContentType.Cert);
            certsCollection.Add(certBytes);
        }

        foreach (byte[] ocsp in revocationInfo.OCSPResponses)
        {
            ocspCollection.Add(ocsp);
        }

        foreach (byte[] crlBytes in revocationInfo.CRLs)
        {
            crlCollection.Add(crlBytes);
        }

        bool revocationInfoAdded = ltvVerification.AddVerification(signingRequest.FieldName, ocspCollection, crlCollection, certsCollection);

ltvVerification.AddVerification() 方法 returns 响应为真。

请从下面找到签名文件link: https://1drv.ms/b/s!AvIgyv7xAxxoihGn9aFbe9TQSps4?e=eKPdn8

非常感谢这方面的任何帮助。 问候

一些工作代码

您使用了 PdfSigner(这仅在同时应用签名或文档时间戳时才有意义,但您只提供了已签名的文件)并且有一些我在这里没有的变量。因此,我基本上只基于 PdfDocument 和您的共享文件编写了一个没有这些额外变量的示例:

using (PdfReader pdfReader = new PdfReader("LTV Doc-Revocation Info Issue.pdf"))
using (PdfWriter pdfWriter = new PdfWriter("LTV Doc-Revocation Info Issue-WithRevocation.pdf"))
using (PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter, new StampingProperties().UseAppendMode()))
{
    List<byte[]> ocspCollection = new List<byte[]>();
    List<byte[]> crlCollection = new List<byte[]>();
    List<byte[]> certsCollection = new List<byte[]>();
    ocspCollection.Add(File.ReadAllBytes(@"Ocsp"));
    crlCollection.Add(File.ReadAllBytes(@"Crl.crl"));

    LtvVerification ltvVerification = new LtvVerification(pdfDocument);
    ltvVerification.AddVerification("SH_SIGNATURE_532546", ocspCollection, crlCollection, certsCollection);
    ltvVerification.Merge();
}

查看结果:

特别是提供的 OCSP 响应和提供的 CRL 嵌入在 PDF 中,因此 iText LtvVerification class 完成了它的工作。

您的项目中可能存在的问题

首先你说:

I am trying to add revocation information (Crls, OCSP Responses, Certificate Chain) to the signature as an unsigned attributes

这已经表明不匹配:您使用 LtvVerification class,我在上面的工作代码中也是如此。 此 class 不会 更改嵌入式 CMS 容器。 不会 添加吊销嵌入式 CMS 容器的未签名属性的信息,而不是 PDF 的 DSS(文档安全存储)结构。

将吊销数据作为嵌入式 CMS 签名容器的 unsigned 属性实际上无法以可互操作的方式嵌入:您要么使用 signed adbe-revocationInfoArchival CMS 容器中的属性或 CMS 容器外的 DSS。

(一些验证器接受在未签名属性中嵌入 CAdES 样式的撤销数据,但严格来说,这在 PAdES 中是被禁止的,并且在 PDF 2.0 中不可互操作。)

所以如果你真的想在 CMS 容器中嵌入吊销数据,将它们提供给你选择的 PdfSigner 签名方法,它们都显式或隐式地接受吊销数据嵌入,

public virtual void SignDetached(IExternalSignature externalSignature, X509Certificate[] chain,
    ICollection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient,
    int estimatedSize, PdfSigner.CryptoStandard sigtype)

public virtual void SignDetached(IExternalSignature externalSignature, X509Certificate[] chain,
    ICollection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient,
    int estimatedSize, PdfSigner.CryptoStandard sigtype, SignaturePolicyInfo signaturePolicy)

public virtual void SignDetached(IExternalSignature externalSignature, X509Certificate[] chain,
    ICollection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient,
    int estimatedSize, PdfSigner.CryptoStandard sigtype, SignaturePolicyIdentifier signaturePolicy)

public virtual void SignExternalContainer(IExternalSignatureContainer externalSignatureContainer,
    int estimatedSize)

前三个显式接受 CRL 和 OCSP 客户端(可以实现以提供预先存在的 CRL 和 OCSP),而后者从给定的 IExternalSignatureContainer 实现中获取完整的 CMS 容器,因此在该实现中您可以向其中添加任何您想要的信息。