我尝试使用 X509Certificate2.Verify() 函数来检查证书链是否有效。验证函数返回 false,ChainElementStatus 返回“RevocationStatusUnknown”。
有没有办法在不检查 RevocationStatus 的情况下使用验证功能?没有互联网连接就无法检查 RevocationStatus?是否有其他功能可以在没有 RevocationStatus 的情况下检查链和证书?
一个肮脏的解决方案是检查 RevocationStatus 是否是 element.ChainElementStatus 中的唯一元素。
我已经使用了 X509RevocationMode.Offline 和 IgnoreCertificateAuthorityRevocationUnknown。
来自:X509Certificate2.Verify() 方法的代码始终为有效证书返回 false
X509Chain ch = new X509Chain();
ch.Build(certificate);
ch.ChainPolicy.RevocationMode = X509RevocationMode.Offline;
ch.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
Console.WriteLine("Chain Information");
Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length);
Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);
//Output chain element information.
Console.WriteLine("Chain Element Information");
Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count);
Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);
foreach (X509ChainElement element in ch.ChainElements)
{
Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
Console.WriteLine("Element information: {0}", element.Information);
Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);
if (ch.ChainStatus.Length >= 1)
{
for (int index = 0; index < element.ChainElementStatus.Length; index++)
{
Console.WriteLine(element.ChainElementStatus[index].Status);
Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
}
}
}
结果:
链信息 链撤销标志:ExcludeRoot 链撤销模式:离线 链验证标志:IgnoreCertificateAuthorityRevocationUnknown 链验证时间:19.11.2018 07:53:31 链状态长度:1 链应用策略计数:0 链证书策略计数:0
链元信息 链元数:2 链元同步?错误的
元素颁发者名称:CN=TestRootCA 元素证书有效期至:01.01.2019 00:00:00 元素证书有效:False 元素错误状态长度:1 元素信息:元素扩展数:5
RevocationStatusUnknown Die Sperrfunktion konnte keine Sperrprüfung für das Zertifikat durchführen。
元素颁发者名称:CN=TestRootCA 元素证书有效期至:01.01.2019 00:00:00 元素证书有效:True 元素错误状态长度:0 元素信息:元素扩展数:2