我需要对客户端证书进行验证,我有点不确定我是否以正确的方式进行操作。我的策略是:
我做了
Issuer
和链长度检查,因为我做了一些单元测试,这确实表明,如果我只是发送了 的 公钥部分
root certificate
或intermediate certificate
链已成功构建,因此我不能单独依赖链构建,这就是为什么我要做一些额外的检查,但现在我不确定这是否是正确的方法。我没有检查撤销,因为我知道
CA
目前没有提供这一点(看起来他们的 CRL 已过期。更多信息这里)。
这是我的代码
public ChainValidatorStatus Validate(X509Certificate2 clientCertificateToBeValidated)
{
var chain = new X509Chain();
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.CustomTrustStore.Add(_options.CA);
chain.ChainPolicy.CustomTrustStore.Add(_options.Intermediate);
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
try
{
var buildValid = chain.Build(clientCertificateToBeValidated);
var status = new ChainValidatorStatus { BuildValid = buildValid };
if (buildValid)
{
var buildClientCert = chain.ChainElements.First();
// there needs to be three elements in the ChainElements. We expect there to be: root, intermediate, client certificate
var isThereThreeElementsInChainElements = chain.ChainElements.Count == 3;
// we expect the first cert in the built chain to have an issuer that is the same as the intermediate
var validIssuerInFirstCertificateInChain = buildClientCert.Certificate.Issuer == _options.Intermediate.Subject;
// we expect the incoming to have the issuer as the intermediate
var validIssuer = clientCertificateToBeValidated.Issuer == _options.Intermediate.Subject;
X509ChainElementCollection elements = chain.ChainElements;
bool isRootCertTheSame = elements[^1].Certificate.RawData.SequenceEqual(_options.CA.GetRawCertData());
status.ValidIssuerInFirstCertificateInChain = validIssuerInFirstCertificateInChain;
status.ValidIssuer = validIssuer;
status.IsThereThreeElementsInChainElements = isThereThreeElementsInChainElements;
status.IsRootCertTheSame = isRootCertTheSame;
}
// if we get here we know: the client cert is trusted, is signed by the intermediate, and is not the CA or the intermediate
return status;
}
catch (Exception exception)
{
return ChainValidatorStatus.NotValid;
}
}
我完全离开这里了吗?或者这是确保以下内容的有效方法:
希望有人能够出力。
X509Chain
执行完整的证书链验证逻辑(如 RFC 5280 §6 中所述以及更多内容),因此 if (buildValid) {}
条件下的逻辑是多余的并且可能存在缺陷。如果您研究 RFC,就会发现链构建和验证是非常复杂的过程,您极不可能以同样的置信度自己复制相同的过程。 X509Chain
为您实现这一点。我同意评论,不要推出自己的加密货币。
您需要在该条件块中执行实际的身份验证和授权。也就是说,您必须检查叶证书,提取身份信息并将其绑定到身份数据库中的帐户。之后,您可以应用授权规则来确定连接的身份可以做什么和不能做什么。