我正在尝试使用
HttpClientHandler.ServerCertificateCustomValidationCallback
验证客户端证书。我已经用我的 x509chain
参数构建了我的 ChainPolicy
。
我在本地有 CRL (.pem) 文件,我想将其添加到吊销流程中。
我正在考虑做类似 CRL 验证 的事情,将带有 distributionPoint oid 的
X509Certificate
导入到我的 X509Extension
中,但我很难理解它。
这是我的一段回调代码
private static Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool>
ServerCertificateCustomValidationCallback()
{
return (sender, cert, chain, sslPolicyErrors) =>
{
X509Certificate2 ca = new X509Certificate2(@"pathToCa\\ca.crt");
X509Chain chai = new X509Chain();
chai.ChainPolicy.ExtraStore.Add(ca);
chai.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chai.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chai.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
chai.ChainPolicy.VerificationTime = DateTime.Now;
try
{
if (!chai.Build(cert))
{
return false;
}
foreach (X509ChainStatus status in chai.ChainStatus)
{
if (status.Status == X509ChainStatusFlags.UntrustedRoot) continue;
if (status.Status == X509ChainStatusFlags.OfflineRevocation) continue;
if (status.Status == X509ChainStatusFlags.RevocationStatusUnknown) continue;
return false;
}
}
catch (Exception e)
{
throw e;
}
return true;
};
}
感谢您的帮助和澄清
如果您想托管自己的 CRL,则需要在某处设置服务器,以便它可以像 html 页面一样托管您的 CRL。
因此,例如,如果您使用 openSSL 或最好使用 LibreSSL 创建自己的证书,则在配置文件中,您将添加以下内容:
crlDistributionPoints = URI:http://myserver.com/mycert.crl
nsCaRevocationUrl = http://myserver.com/mycert.crl
(您可以尝试使用上面键的绝对路径,但我不确定这是否有效,如果它确实让我知道的话)
有几个教程可以帮助您进行此设置,https://jamielinux.com/docs/openssl-certificate-authority/非常好且详细。
在我自己执行此操作时,我发现当您调用构建链时,.Net 框架会检查链,因此您不需要添加额外的代码来确保它检查 CRL,因为这是自动发生的。 (您可以通过检查服务器日志文件来检查是否正在获取 crl)
您可以通过将您自己的证书添加到 CRL 来测试这一点,您应该会发现您得到了
X509ChainStatusFlags.Revoked
希望有帮助
我需要根据 CRL 验证证书。在使用它进行进一步处理之前。 我提取了证书的 CRLDP url 并下载了 .crl 文件。现在我必须读取 .crl 文件的撤销列表并验证序列号 下载的 crl 吊销列表中的证书。
enter code here
公共静态无效 getCRL()
{
string strLocalPath = "C:\\EnjoyCoding"+ "\\.crl";
var certificate_List = CertificateService.FindCertificateInStores("Alstom Solutions Root CA - PREPROD");
if (certificate_List.Count > 0)
{
crlDP_URL = X509Certificate2Extensions.GetCrlDistributionPoints(certificate_List[0]);
}
using (System.Net.WebClient Client = new System.Net.WebClient())
{
Client.DownloadFile(
// Param1 = Link of file
new System.Uri(crlDP_URL[0]),
// Param2 = Path to save
strLocalPath
);
}
}
}