c# - 使用 CA 证书文件验证远程服务器 X509Certificate

标签 c# ssl openssl x509certificate sslstream

我已经使用 OpenSSL 生成了一个 CA 和多个证书(由 CA 签名),我有一个 .NET/C# 客户端和服务器都使用 SslStream,每个都有自己的证书/ key ,相互启用身份验证并禁用吊销。

我正在为 SslStream 使用 RemoteCertificateValidationCallback 来验证远程服务器的证书,我希望我可以在程序中加载 CA 的公共(public)证书(作为文件)并使用它来验证远程证书,而不是在 Windows 证书存储中实际安装 CA。问题是 X509Chain 不会显示任何其他内容,除非我将 CA 安装到商店中,当我打开其中一个证书的 PEM 版本时,Windows CryptoAPI shell 也会显示。

我的问题是,当 RemoteCertificateValidationCallbackX509CertificateX509Chain 似乎没有给我任何可以使用的东西?

最佳答案

因为 CA 证书不在根证书存储中,您将在 RemoteCertificateValidationCallback() 中有一个错误标志 SslPolicyErrors.RemoteCertificateChainErrors ;一种可能性是针对您自己的 X509Certificate2Collection 显式验证证书链,因为您没有使用本地商店。

if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
    X509Chain chain0 = new X509Chain();
    chain0.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
    // add all your extra certificate chain
    chain0.ChainPolicy.ExtraStore.Add(new X509Certificate2(PublicResource.my_ca));
    chain0.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
    isValid = chain0.Build((X509Certificate2)certificate);
}

您还可以重新使用回调中传递的链,将额外的证书添加到 ExtraStore 集合中,并使用 AllowUnknownCertificateAuthority 标志进行验证需要,因为您将不受信任的证书添加到链中。

您还可以通过在受信任的根存储中以编程方式添加 CA 证书来防止原始错误(当然它会打开一个弹出窗口,因为全局添加新的受信任的 CA 根是一个主要的安全问题):

var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2 ca_cert = new X509Certificate2(PublicResource.my_ca);
store.Add(ca_cert);
store.Close();

编辑:对于那些想要通过您的 CA 清楚地测试链的人:

另一种可能性是使用库 BouncyCaSTLe 来构建证书链并验证信任。选项很清楚,错误也很容易理解。如果成功,它将构建链,否则返回异常。以下示例:

// rootCerts : collection of CA
// currentCertificate : the one you want to test
var builderParams = new PkixBuilderParameters(rootCerts, 
                        new X509CertStoreSelector { Certificate = currentCertificate });
// crls : The certificate revocation list
builderParams.IsRevocationEnabled = crls.Count != 0;
// validationDate : probably "now"
builderParams.Date = new DateTimeObject(validationDate);

// The indermediate certs are items necessary to create the certificate chain
builderParams.AddStore(X509StoreFactory.Create("Certificate/Collection", new X509CollectionStoreParameters(intermediateCerts)));
builderParams.AddStore(X509StoreFactory.Create("CRL/Collection", new X509CollectionStoreParameters(crls)));

try
{
    PkixCertPathBuilderResult result = builder.Build(builderParams);
    return result.CertPath.Certificates.Cast<X509Certificate>();
    ...

关于c# - 使用 CA 证书文件验证远程服务器 X509Certificate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695438/

相关文章:

c# - 在 asp.net(C#) 的 DataGrid 中通过超链接传递动态参数

c# - 以编程方式重置 VisualStudio 快捷方式

Docker 更新旧的 pem 文件

IOS 相互认证

c# - 如何从文件中加载 c# 中的 .pem 证书?

c# - .NET 验证 POST 请求结果

c# - 问题: ASP. NET无法识别外部脚本

c# - SSL/TLS 中的相互认证

linux - Docker 错误 : Could not connect to deb. debian.org(111:连接被拒绝)

c - 访问 C 库的最 pythonic 方式是什么——例如,OpenSSL?