iTextSharp读取时间戳证书



>同一个人介意帮助我使用iTextSharp编写代码吗?我已经签名了pdf文件,我需要检索有关签名和时间戳的信息。我在获取签名证书的信息方面没有问题。但我无法从 TSA 证书中获取信息。我只获取有关发行人的信息,我需要获取有关日期的信息,而不是之前和之后以及其他。

这是我的代码:

PdfReader reader = new PdfReader(file);
AcroFields af = reader.AcroFields;
List<string> names = af.GetSignatureNames();
for (int i = 0; i < names.Count; ++i)
{
    // it is working fine
    string name = (string)names[i];
    iTextSharp.text.pdf.security.PdfPKCS7 pk = af.VerifySignature(name);
    Console.WriteLine();
    Console.WriteLine(String.Format("Podepsal: {0}", pk.SignName));
    Console.WriteLine(String.Format("Datum: {0}", pk.SignDate));
    Console.WriteLine(String.Format("Platnost od: {0}", pk.SigningCertificate.NotBefore));
    Console.WriteLine(String.Format("Platnost do: {0}", pk.SigningCertificate.NotAfter));
    // here I need to help 
    Org.BouncyCastle.Tsp.TimeStampToken tts = pk.TimeStampToken;
    string s = tts.TimeStampInfo.Tsa.Name.ToString();
    // this line returns null
    DateTime dt  = tts.SignerID.Certificate.NotAfter;
}

这是 pdf 样本 http://www.filedropper.com/sample

谢谢!

iText 在af.VerifySignature通话期间已经验证的基本签名相比,尚未分析签名时间戳。特别是尚未寻找实际的TSA证书。

因此,您首先必须确定有问题的证书。通常它包含在时间戳带来的证书集合中,因此在下文中,我假设它在那里(在您的示例文件的情况下(。我们按颁发者和序列号查找它,然后用它验证时间戳,以确保它是正确的证书,而不是假的。此后,您可以根据需要检查证书。

// Define a selector matching issuer and serial number
X509CertStoreSelector selector = new X509CertStoreSelector();
selector.Issuer = tts.SignerID.Issuer;
selector.SerialNumber = tts.SignerID.SerialNumber;
// Retrieve the matching certificates from the time stamp certificate collection
System.Collections.ICollection certs = tts.GetCertificates("COLLECTION").GetMatches(selector);
// Assuming at most one match, retrieve this matching certificate
IEnumerator enumCerts = certs.GetEnumerator();
if (enumCerts.MoveNext())
{
    X509Certificate cert = (X509Certificate)enumCerts.Current;
    // Verify that this is the correct certificate by verifying the time stamp token
    tts.Validate(cert);
    // Extracting information from the now verified tsa certificate
    Console.WriteLine(String.Format("Not before: {0}", cert.NotBefore));
    Console.WriteLine(String.Format("Not after: {0}", cert.NotAfter));
}

最新更新