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

时间:2011-10-08 07:35:31

标签: c# ssl openssl x509certificate sslstream

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

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

我的问题是,如何在RemoteCertificateValidationCallback {X509Certificate {{{}} {{}} {{}} {{}} {{}} {{}} {{{} 下使用CA的公共证书文件来验证证书是否已由我的特定CA签署{1}}和X509Chain似乎没有给我任何合作?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

  

如果RemoteCertificateValidationCallback,X509Certificate和X509Chain似乎没有使用任何东西可以使用CA的公共证书文件而不使用Windows证书存储区或WCF,我如何验证我的特定CA已签署证书?< / p>

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

该功能的名称无关紧要。下面,VerifyServerCertificateRemoteCertificateValidationCallback类中的SslStream回调相同。您也可以将其用于ServerCertificateValidationCallback中的ServicePointManager

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中的架构缺陷,但我可能会遗漏一些明显的东西。