iTextSharp读取时间戳证书

时间:2017-03-21 09:56:27

标签: c# timestamp itext

使用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

谢谢!

1 个答案:

答案 0 :(得分:1)

与您在af.VerifySignature通话期间已通过iText验证的基本签名相比,尚未分析签名时间戳。特别是尚未查找实际的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));
}