簡體   English   中英

使用 IText SignDeferred 簽署文檔時如何保留 PDF-A

[英]Howto keep PDF-A when signing a document using IText SignDeferred

我確實使用 IText 通過延遲簽名(SignDeferred)將簽名應用於 pdf 文檔。 該過程包含以下步驟:

  • 准備 pdf 文檔進行 sig
    • 為 pdf 文檔中的簽名預留空間
  • 創建 pdf 文檔的 hash 值
  • 根據 hash 值創建簽名
    • 使用自簽名證書
  • 將簽名應用於 pdf 文檔

整個過程有效,我以 pdf 文檔結束,其中簽名已設置且有效。

在此處輸入圖像描述

原來的 pdf 是PDF-A1a ,但生成的 pdf 不再是有效的 PDF-A1a。 我知道有一個關於 IText PDF-A 支持的文檔( https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-jump-start-tutorial-for-java/chapter-7-creating -pdf-ua-and-pdf-a-documents ),但這似乎不適用,因為我沒有更改文檔的內容。

在此處輸入圖像描述

我的問題:如何使用延遲簽名應用簽名並將 PDF-A1a 保留在生成的文檔中?

注意:如果我直接應用簽名(沒有 SignDeferred),則生成的 pdf 仍然是 PDF-A1a,但我必須使用 SignDeferred 注意:我確實使用https://www.pdfen.com/pdf-a-用於檢查 pdf-A 的驗證器

代碼示例

  • 用於簽名的組件:
    • itext.sign 7.1.5.0
    • itext.kernel 7.1.5.0
  • 用於創建 hash 的組件
    • BouncyCastle.Crypto 1.8.1.0

以下是一個完整的代碼示例,其中包含一個文件中所需的所有內容。 它只需要對 itext 和 BouncyCastle 的引用以及自簽名證書的路徑

using iText.Kernel.Pdf;
using iText.Signatures;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;

namespace DeferredSigningTestConsole
{
    class Program
    {
        static string SignatureAttributeName = "DeferredSignature";
        static string CertificatePath = @"C:\temp\PDFA\PdfATestCert.2pfx.pfx";
        static string CertificatePassword = "test";

        static void Main(string[] args)
        {
            var signedPdf = SignPdf(System.IO.File.ReadAllBytes(@"C:\temp\PDFA\PDF_A1a.pdf"));
            System.IO.File.WriteAllBytes(@"C:\temp\PDFA\signed.pdf", signedPdf);
        }

        public static byte[] SignPdf(byte[] pdfToSign)
        {
            byte[] hash = null;
            byte[] tmpPdf = null;
            //Step #1 >> prepare pdf for signing (Allocate space for the signature and calculate hash)
            using (MemoryStream input = new MemoryStream(pdfToSign))
            {
                using (var reader = new PdfReader(input))
                {
                    StampingProperties sp = new StampingProperties();
                    sp.UseAppendMode();
                    using (MemoryStream baos = new MemoryStream())
                    {
                        var signer = new PdfSigner(reader, baos, sp);
                        signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);

                        signer.SetFieldName(SignatureAttributeName);
                        DigestCalcBlankSigner external = new DigestCalcBlankSigner(PdfName.Adobe_PPKLite, PdfName.Adbe_pkcs7_detached);

                        signer.SignExternalContainer(external, 121743);
                        hash = external.GetDocBytesHash();
                        tmpPdf = baos.ToArray();
                    }
                }

                //Step #2 >> Create the signature based on the document hash
                byte[] signature = GetSignatureFromHash(hash);

                //Step #3 >> Apply the signature to the document
                ReadySignatureSigner extSigContainer = new ReadySignatureSigner(signature);
                using (MemoryStream preparedPdfStream = new MemoryStream(tmpPdf))
                {
                    using (var pdfReader = new PdfReader(preparedPdfStream))
                    {
                        using (PdfDocument docToSign = new PdfDocument(pdfReader))
                        {
                            using (MemoryStream outStream = new MemoryStream())
                            {
                                PdfSigner.SignDeferred(docToSign, SignatureAttributeName, outStream, extSigContainer);
                                return outStream.ToArray();
                            }
                        }
                    }
                }

            }
        }

        public static byte[] GetSignatureFromHash(byte[] hash)
        {
            FileStream fs = new FileStream(CertificatePath, FileMode.Open);
            Pkcs12Store store = new Pkcs12Store(fs, CertificatePassword.ToCharArray());
            String alias = "";
            foreach (string al in store.Aliases)
                if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                {
                    alias = al;
                    break;
                }
            AsymmetricKeyEntry pk = store.GetKey(alias);
            X509CertificateEntry[] chain = store.GetCertificateChain(alias);

            List<Org.BouncyCastle.X509.X509Certificate> c = new List<Org.BouncyCastle.X509.X509Certificate>();
            foreach (X509CertificateEntry en in chain)
            {
                c.Add(en.Certificate);
            }
            PrivateKeySignature signature = new PrivateKeySignature(pk.Key, "SHA256");
            String hashAlgorithm = signature.GetHashAlgorithm();
            PdfPKCS7 sgn = new PdfPKCS7(null, c.ToArray(), hashAlgorithm, false);
            DateTime signingTime = DateTime.Now;
            byte[] sh = sgn.GetAuthenticatedAttributeBytes(hash, null, null, PdfSigner.CryptoStandard.CMS);
            byte[] extSignature = signature.Sign(sh);
            sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());
            return sgn.GetEncodedPKCS7(hash, null, null, null, PdfSigner.CryptoStandard.CMS);

        }
    }

    internal class DigestCalcBlankSigner : IExternalSignatureContainer
    {
        private readonly PdfName _filter;

        private readonly PdfName _subFilter;

        private byte[] _docBytesHash;

        internal DigestCalcBlankSigner(PdfName filter, PdfName subFilter)
        {
            _filter = filter;
            _subFilter = subFilter;
        }

        internal virtual byte[] GetDocBytesHash()
        {
            return _docBytesHash;
        }

        public virtual byte[] Sign(Stream docBytes)
        {

            _docBytesHash = CalcDocBytesHash(docBytes);
            //If we retun the signature bytes, GetAuthenticatedAttributeBytes will throw an exception
            //Not clear how this should be done
            return new byte[0];
        }

        public virtual void ModifySigningDictionary(PdfDictionary signDic)
        {
            signDic.Put(PdfName.Filter, _filter);
            signDic.Put(PdfName.SubFilter, _subFilter);
        }

        internal static byte[] CalcDocBytesHash(Stream docBytes)
        {
            byte[] docBytesHash = null;
            docBytesHash = DigestAlgorithms.Digest(docBytes, DigestUtilities.GetDigest(DigestAlgorithms.SHA256));
            return docBytesHash;
        }
    }


    internal class ReadySignatureSigner : IExternalSignatureContainer
    {
        private byte[] cmsSignatureContents;

        internal ReadySignatureSigner(byte[] cmsSignatureContents)
        {
            this.cmsSignatureContents = cmsSignatureContents;
        }

        public virtual byte[] Sign(Stream docBytes)
        {
            return cmsSignatureContents;
        }

        public virtual void ModifySigningDictionary(PdfDictionary signDic)
        {
        }
    }
}

似乎簽名的 pdf 不再是有效的 PDF-A1a 的原因是簽名的估計大小。 我為簽名使用了大約 120kb 的值。

//doesn't work
signer.SignExternalContainer(external, 121743);

//does work
signer.SignExternalContainer(external, 65000);

此概念記錄在來自 iText 的電子書“PDF 文檔的數字簽名”中。

似乎為了獲得有效的 pdf-A1a,最大大小限制為 65kb。

我現在必須測試當我添加視覺表示(簽名圖像)時這是否有效,因為這就是我選擇如此大的估計尺寸的原因。

編輯:我做了一些更多的測試,我現在能夠生成帶有簽名的有效 pdf-A 文檔:pdf 現在是有效的 pdf-A,估計大小已更改:

  • 適用於估計大小 32'000/65'000
    • A1a
    • A1b
  • 對估計大小 32'000 有效
    • A2a
    • A2b
    • A2u
    • A3a
    • A3b
    • A3u

添加視覺表示(圖像)時,pdf-A1a 和 pdf-A1b 不再有效。

存在透明的軟蒙版。 從 PDF 開始,支持 1.4 透明度。 一些基於 PDF 的 ISO 標准禁止使用透明度。

但這是我現在試圖解決的另一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM