为什么在申请 TSA 时抛出 'Not enough space' 异常?

Why 'Not enough space' exception thrown when applying TSA?

我正在尝试使用下面的代码申请 TSA,为什么我遇到了:"Not enough space" 例外情况

准备签名容器:

        ITSAClient tsaClient = new TSAClientBouncyCastle("https://freetsa.org/tsr", "", "");

        PdfSigner signer = new PdfSigner(new PdfReader("results/example.pdf"), new FileStream("results/prepared1.pdf", FileMode.Create), new StampingProperties().UseAppendMode());
        signer.SetFieldName("Signature1");
        signer.SetCertificationLevel(0);
        signer.Timestamp(tsaClient, "Signature1");   // EXCEPTION THROWN HERE

        PdfSignatureAppearance sigAppearance = signer.GetSignatureAppearance();
        sigAppearance
            .SetPageRect(new Rectangle(144, 144, 200, 100))
            .SetPageNumber(1)
            .SetContact("This is contact1")
            .SetReason("This is reason1")
            .SetLocation("This is location1")
            .SetSignatureCreator("This is signature creator");

        ExternalEmptySignatureContainer container = new ExternalEmptySignatureContainer();
        signer.SignExternalContainer(container, 8192);

        byte[] dataToSign = container.Data;

        return dataToSign;enter code here

您使用 TSAClientBouncyCastle 构造函数而没有估计大小:

ITSAClient tsaClient = new TSAClientBouncyCastle("https://freetsa.org/tsr", "", "");

因此假定默认值为 4 KB。因此,当您应用文档时间戳

signer.Timestamp(tsaClient, "Signature1");

4 KB 时间戳令牌的占位符已保留,然后请求时间戳。您在此处收到的错误消息现在告诉您实际检索到的时间戳显然更大。

因此,您应该使用 TSAClientBouncyCastle 构造函数,它也接受大小参数并将其设置为例如8 KB = 8192.


话虽如此,您似乎误解了 Timestamp 方法:它立即应用文档时间戳,然后关闭文档:

    /// <summary>Signs a document with a PAdES-LTV Timestamp.</summary>
    /// <remarks>
    /// Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
    /// <br /><br />
    /// NOTE: This method closes the underlying pdf document. This means, that current instance
    /// of PdfSigner cannot be used after this method call.
    /// </remarks>
    /// <param name="tsa">the timestamp generator</param>
    /// <param name="signatureName">
    /// the signature name or null to have a name generated
    /// automatically
    /// </param>
    public virtual void Timestamp(ITSAClient tsa, String signatureName)

因此,在修复时间戳令牌大小估计问题后,您将 运行 陷入其他错误!