gpt4 book ai didi

.net - 如何在不导入根证书的情况下验证X509证书?

转载 作者:行者123 更新时间:2023-12-02 03:55:19 25 4
gpt4 key购买 nike

我的程序包含 2 个我了解并信任的根证书。我必须验证信任中心的证书和信任中心颁发的“用户”证书,它们都源自这两个根证书。

我使用 X509Chain 类进行验证,但仅当根证书位于 Windows 证书存储中时才有效。

我正在寻找一种在不导入这些根证书的情况下验证证书的方法 - 以某种方式告诉 X509Chain 类我确实信任此根证书,并且它应该只检查链中的证书而不检查其他内容。

实际代码:

        X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.ExtraStore.Add(root); // i do trust this
chain.ChainPolicy.ExtraStore.Add(trust);
chain.Build(cert);

编辑:这是一个 .NET 2.0 Winforms 应用程序。

最佳答案

我打开了Issue在 dotnet/corefx 上,他们回复如下:

If AllowUnknownCertificateAuthority is the only flag set then chain.Build() will return true if

  • The chain correctly terminated in a self-signed certificate (via ExtraStore, or searched persisted stores)

  • None of the certificates are invalid per the requested revocation policy

  • All of the certificates are valid under the (optional) ApplicationPolicy or CertificatePolicy values

  • All of the certificates' NotBefore values are at-or-before VerificationTime and all of the certificates' NotAfter values are (at-or-)after VerificationTime.

If that flag is not specified then an additional constraint is added:

The self-signed certificate must be registered as trusted on the system (e.g. in the LM\Root store).

So, Build() returns true, you know that a time-valid non-revoked chain is present. The thing to do at that point is read chain.ChainElements[chain.ChainElements.Count - 1].Certificate and determine if it is a certificate that you trust. I recommend comparing chainRoot.RawData to a byte[] representing a certificate that you trust as a root in context (that is, byte-for-byte compare rather than using a thumbprint value).

(If other flags are set then other constraints are also relaxed)

所以你应该这样做:

X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.ExtraStore.Add(root);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
var isValid = chain.Build(cert);

var chainRoot = chain.ChainElements[chain.ChainElements.Count - 1].Certificate;
isValid = isValid && chainRoot.RawData.SequenceEqual(root.RawData);

关于.net - 如何在不导入根证书的情况下验证X509证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6097671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com