【发布时间】:2021-07-16 18:14:05
【问题描述】:
我使用 iText 7 创建了代码,该代码能够使用使用 ECDSA 密钥对的 X509 证书对给定 PDF 进行数字签名。当我在 Acrobat Reader DC 中打开这个签名的 PDF 时,它会正确读取它,并验证它是有效的(因为签名等原因,文档没有被修改过)。
但是,当我尝试使用 iText 7 验证同一文档时,完整性和真实性检查返回 false。
这是一个示例代码:
// ...
PdfDocument pdfDoc = new(new PdfReader(stream));
SignatureUtil signUtil = new(pdfDoc);
IList<string> names = signUtil.GetSignatureNames();
foreach (string name in names) {
PdfPKCS7 pkcs7 = signUtil.ReadSignatureData(name);
bool wholeDocument = signUtil.SignatureCoversWholeDocument(name);
bool signatureIntegrityAndAuthenticity = pkcs7.VerifySignatureIntegrityAndAuthenticity(); // this returns false, even though Adobe has no problem verifying the signature.
// more code to read values and put them in a json
}
// ...
以及我从签名中提取的示例输出:
{
"$id": "1",
"signatures": [
{
"$id": "2",
"integrityAndAuthenticity": false, // <---- should be true in my opinion.
"revisionNumber": 1,
"coversWholeDocument": true,
"invisibleSignature": true,
"filterSubType": "ETSI.CAdES.detached",
"encryptionAlgorithm": "ECDSA",
"hashAlgorithm": "SHA512",
"nameOfSigner": "C=HU, CN=Teszt Elek, GIVENNAME=Elek, L=Budapest, O=Teszt ECC Szervezet, SN=202010260807, SURNAME=Teszt",
"alternateNameOfSigner": null,
"signDate": "2021-04-22T12:50:33Z",
"timestamp": {
"$id": "3",
"signDate": "2021-04-22T12:50:33Z",
"service": "C=HU,L=Budapest,O=Microsec Ltd.,2.5.4.97=VATHU-23584497,CN=Test e-Szigno TSA 2017 01",
"verified": true,
"hashAlgorithmOid": "2.16.840.1.101.3.4.2.3"
},
"location": " Hungary",
"reason": "Approval",
"contactInfo": "",
"name": "GUID_97e1669d-0fbe-409a-a8fc-8518a1bae460",
"signatureType": "approval",
"fillInAllowed": true,
"annotationsAllowed": true,
"fieldLocks": []
}
],
"revisions": 1,
"valid": false // is an aggregate of all the signatures integrity in the array above
}
截至发稿时,我使用的是最新的 iText 7 版本,我的平台是 ASP.NET 5 (.Net 5)。示例代码对应于 iText 自己的示例代码,它们为他们的学习书籍提供(但更新为 7,因为这些书籍是为 iText 5 编写的)。
我在this google drive 中添加了一个示例 pdf,以及一些签名版本的组合。它包含一个样本 pdf,它是无符号的和纯的。然后使用 ECDSA 和 RSA 密钥分别对该 pdf 进行签名。然后,它们都使用相反类型的密钥进行签名。以及他们所有的验证结果。注意:在 json 文件中,integrityAndAuthenticity 为简洁起见只是命名为valid,但它保存的值是pkcs7.VerifySignatureIntegrityAndAuthenticity() 的结果。所有签名均由我的应用程序完成(使用 iText 7)。
编辑#1: 我正在提供执行签名的代码:
using System;
using System.Security.Cryptography;
using iText.Signatures;
public class EcdsaSignature : IExternalSignature
{
private readonly string _encryptionAlgorithm;
private readonly string _hashAlgorithm;
private readonly ECDsa _pk;
public EcdsaSignature(ECDsa pk, string hashAlgorithm)
{
_pk = pk;
_hashAlgorithm = DigestAlgorithms.GetDigest(DigestAlgorithms.GetAllowedDigest(hashAlgorithm));
_encryptionAlgorithm = "ECDSA";
}
public virtual string GetEncryptionAlgorithm()
{
return _encryptionAlgorithm;
}
public virtual string GetHashAlgorithm()
{
return _hashAlgorithm;
}
public virtual byte[] Sign(byte[] message)
{
return _pk.SignData(message, new HashAlgorithmName(_hashAlgorithm), DSASignatureFormat.Rfc3279DerSequence); // <---- I have solved the iText 7 issue by providing this enum to the SignData() method.
}
}
然后:
using (var key = myCertificate.GetECDsaPrivateKey()) {
/*PdfSigner*/ signer.SignDetached(new EcdsaSignature(key, DigestAlgorithms.SHA512), chainArray, crlList, ocspClient, tsaClient, 0, subfilter);
}
感谢@mkl 的回复,它消除了一些关于签名格式的困惑,还好微软支持SignData() 方法中的 TLV 序列格式,所以我不必对签名过程进行逆向工程来实现我想要的.虽然我只假设这个枚举是答案中描述的 TLV 序列,因为它使用不同的 RFC 或 IEEE 规范来引用它。尽管如此,它解决了我的问题。 (我还在驱动器sample_signed_ecdsa_Rfc3279DerSequence.pdf 中添加了一个新的pdf 和相应的响应JSON。)可能默认情况下它使用DSASignatureFormat.IeeeP1363FixedFieldConcatenation,因为指定该参数并没有改变签名的有效性,但指定另一个就可以了在 iText 7 中也有效。
至于互操作性,我不确定如何更改我的代码以使用IExternalSignatureContainer。我是这个数字签名的新手,我只关注了他们网站上的 iText 5 书和更新的 iText 7 示例,不幸的是,除了 API 参考之外,我找不到关于它的示例或文档。
【问题讨论】:
-
请分享有问题的 PDF。没有它,这只是猜测。
-
@mkl 我添加了一些 PDF 示例。
-
我只是 d/l'ed 文件。我今天晚些时候再看看。
-
好的,您的签名值格式不正确。 Adobe Acrobat 似乎忽略了这个细节,但 BouncyCastle 没有。下周我会检查更多并写一个答案。
标签: c# pdf digital-signature itext7 ecdsa