使用CA证书文件validation远程服务器X509Certificate

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

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

我的问题是,当RemoteCertificateValidationCallbackX509CertificateX509Chain似乎没有给我任何工作时,如何使用CA的公共证书文件而不使用Windows证书存储或WCF来validation我的特定CA是否签署了证书?

由于CA证书不在根证书存储区中,因此您将在RemoteCertificateValidationCallback()中包含SslPolicyErrors.RemoteCertificateChainErrors的错误标志; 一种可能性是针对您自己的X509Certificate2Collection明确validation证书链,因为您没有使用本地存储。

 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标志进行validation,因为您将不受信任的证书添加到链中。

您还可以通过在受信任的根存储中以编程方式添加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来构建证书链并validation信任。 选项很明确,错误很容易理解。 在成功的过程中,它将构建链,否则返回exception。 示例如下:

  // 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(); ... 

当RemoteCertificateValidationCallback,X509Certificate和X509Chain似乎没有给我任何工作时,如何使用CA的公共证书文件而不使用Windows证书存储或WCF来validation我的特定CA是否已签署证书?

以下代码将避免Windows证书存储并validation链。 它与JB的代码略有不同,特别是在使用标志时。 下面的代码不需要AllowUnknownCertificateAuthority (但它确实使用X509RevocationMode.NoCheck因为我没有CRL)。

function的名称无关紧要。 下面, VerifyServerCertificateSslStream类中的RemoteCertificateValidationCallback相同。 您还可以将它用于ServicePointManagerServerCertificateValidationCallback

 static bool VerifyServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { try { String CA_FILE = "ca-cert.der"; X509Certificate2 ca = new X509Certificate2(CA_FILE); X509Chain chain2 = new X509Chain(); chain2.ChainPolicy.ExtraStore.Add(ca); // Check all properties chain2.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag; // This setup does not have revocation information chain2.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; // Build the chain chain2.Build(new X509Certificate2(certificate)); // Are there any failures from building the chain? if (chain2.ChainStatus.Length == 0) return true; // If there is a status, verify the status is NoError bool result = chain2.ChainStatus[0].Status == X509ChainStatusFlags.NoError; Debug.Assert(result == true); return result; } catch (Exception ex) { Console.WriteLine(ex); } return false; } 

没有想出如何使用这个链(下面的链chain2 )默认,这样就不需要回调了。 也就是说,将它安装在ssl套接字上,连接将“正常工作”。 而且我还没弄清楚如何安装它以便将其传递给回调。 也就是说,我必须为每次调用回调构建链。 我认为这些是.Net中的架构缺陷,但我可能会遗漏一些明显的东西。