温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Add revocation information to signature using iText7
.net c# itext7 pdf

c# - 使用iText7将吊销信息添加到签名

发布于 2020-05-09 13:11:57

我正在制作长期签名。我想将吊销信息(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()方法在响应中返回true。

请从以下链接中找到签名的文档:https : //1drv.ms/b/s!AvIgyv7xAxxoihGn9aFbe9TQSps4?e=eKPdn8

在这方面的任何帮助都将受到高度赞赏。问候

查看更多

提问者
Muddassir Awan
被浏览
54
mkl 2020-02-20 21:24

一些工作代码

您使用了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();
}

检查结果看到:

PDF结构的屏幕截图

特别是,提供的OCSP响应和提供的CRL嵌入在PDF中,因此iText LtvVerification类可以完成其工作。

项目中可能存在的问题

首先你说:

我想将吊销信息(Crls,OCSP响应,证书链)作为未签名的属性添加到签名

这已经表明不匹配:您使用了LtvVerification该类,因此在上面的工作代码中也是如此。此类不会更改嵌入式CMS容器。它并没有吊销信息添加到嵌入式CMS容器的无符号属性,而是以PDF的DSS(文档安全存储)结构。

嵌入撤销数据的无符号嵌入的CMS签名容器的属性,实际上是不可能在一个可互操作的方法:你要么使用签署 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容器,因此在该实现中,您可以向其添加任何信息。