X509Chain.Build() 已成功吊销证书。 C#

X509Chain.Build() succeeding for revoked certificate. C#

我这样创建证书请求:

certreq -new req.inf req-Revoked.req
certreq -submit -attrib "SAN:email=ttesting@Test.Domain&upn=1234567890@Test.Domain" -config Win2K8-64\Test-Win2K8-64-CA req-Revoked.req testerCert-Revoked.cer
certreq -accept testerCert-Revoked.cer
CertUtil -f -p testerCert -exportPFX -user My Testing.Tester.T.1234567890 testerCert-Revoked.pfx
CertUtil -delstore -user My Testing.Tester.T.1234567890

然后我撤销它,通过:

CertUtil -revoke <SerialNumber_from_above_Cert>

然后我执行这段代码:

X509Certificate2 certificate = GetCertificate("testerCert-Revoked.pfx", "password"); // helper method loads the pfx from a file
X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;

if (!chain.Build(certificate))
{
  errorBuffer.Append("Could not build X.509 certificate chain:\r\n");
  foreach (X509ChainStatus status in chain.ChainStatus)
  {
    errorBuffer.AppendFormat("  - {0}\r\n", status.StatusInformation);
  }
  throw new CryptographicException(errorBuffer.ToString());
}

chain.Build() 始终 returns 为真。但是既然证书被吊销了,那应该是False!我仔细检查了证书是否已撤销,并且序列号列在服务器管理器中的已撤销证书下。我已经仔细检查了服务器上的 CURL 分发点 url 是否与证书请求匹配。 (它们是 LDAP 网址)。 CertUtil 将中间 .cer 文件视为已撤销。但是上面的 C# 代码没有。

这一切都在原始 CA 过期之前有效,并且 IT 使用新证书重建了我的测试机器。我已经用新的 CA 重新生成了证书,并且每个单元测试都再次运行,除了那些处理撤销的单元测试。过期的证书按预期工作,但不是吊销的证书。

我不知道下一步该怎么做才能让这段代码再次运行,希望得到一些帮助。谢谢!

链只是一个证书列表,从您提供的证书开始,一直到根证书。它建立在任何可以找到的证书之上——即使它们已过时、无效或被吊销。

您要做的是验证链。为此使用 chain.ChainStatus。

来自MSDN docs: X509Chain 对象有一个名为 ChainStatus 的全局错误状态,应该用于证书验证。管理证书验证的规则很复杂,并且很容易通过忽略所涉及的一个或多个元素的错误状态来过度简化验证逻辑。全局错误状态考虑了链中每个元素的状态。

原来问题出在一个名为 Tumbleweed 的应用程序上。它安装在服务器上,拦截所有吊销请求。禁用 Tumbleweed 服务完全解决了我的问题。

查尔斯.