我知道这个论点有很多问题,但我已经陷入这个问题好几天了,所以我在这里。
我有根证书和客户端证书。我需要在 C# Web API 项目中复制命令 openssl verify -CAfile ca.pem client.pem
的作用。
这就是我现在所知道的(希望这是真的):
Verify()
方法实际上验证证书是否由权威机构签名。它就像一个格式控件。哪个权威机构签署了证书并不重要。X509 Chain
是正确的选择。将您的 ca 证书添加到额外的存储中,因为我不会将该证书安装到 Windows 中。然后构建通过客户端证书。让魔法发生吧!不幸的是我在配置上遇到了一些问题。
让我举个例子更清楚
private bool VerifyCertificate(X509Certificate2 client)
{
X509Chain chain = new X509Chain();
var stringCert = WebConfigurationManager.AppSettings["CACertificate"];
var byteCert = Encoding.ASCII.GetBytes(stringCert);
var authority = new X509Certificate2(byteCert);
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreWrongUsage;
chain.ChainPolicy.ExtraStore.Add(authority);
// Do the preliminary validation.
if (!chain.Build(client))
return false;
return true;
}
在本例中,程序返回false
。构建未通过。我确定问题出在 ChainPolicy 属性
,因此我尝试了不同的配置
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
但是这个不会验证任何东西,事实上,使用我的 ca 证书,该方法返回 true
并使用另一个 ca 证书(我没有使用它)签署了我的客户端证书)该方法还返回 true
。
我搜索了 C# 的 OpenSSL 包装器,并找到了它,但不幸的是它基于旧库,并且不再维护该存储库。另外,如果可能的话,我会仅使用 .net 框架来实现我的目标。
所以,伙计们,快速回顾一下。我想检查只有由我的 ca 证书确认的证书才能通过验证,所有其他证书都必须停止。
预先感谢您的帮助
最佳答案
ExtraStore
没有限制,它提供额外的证书来帮助完成链。它不提供信任数据。
为了确定证书是否由您想要的 CA 颁发,您需要执行以下操作:
private static readonly X509Certificate2 s_trustedRoot = ObtainTheRoot();
private static readonly byte[] s_normalizedRoot = s_trustedRoot.RawData;
private bool VerifyCertificate(X509Certificate2 candidate)
{
X509Chain chain = new X509Chain();
// set all the things you need to set to make it build
if (!chain.Build(candidate))
return false;
// Check that the root certificate was the expected one.
X509ChainElementCollection elements = chain.ChainElements;
return elements[elements.Count - 1].Certificate.RawData.SequenceEqual(s_normalizedRoot);
}
我将其证书和规范化字节形式提升为静态,假设进程启动后它们不会改变。如果证书可以动态更改,那么您应该进行相应调整。
关于C# - 验证由正确的根签名的客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52890852/