c# - SslStream 身份验证失败

标签 c# ssl

各位,我正在尝试写一些关于 SSL 的文章,这里是问题:

我在下面构建了一些东西:

  • CA 证书(自制 CA)
  • Server pfx, Server cert, Server key(由自制的CA签发到“localhost”)

现在我正在使用 .Net SslStream 来测试连接:(客户端和服务器在不同的线程中,并且已经建立了 TCP 连接)

客户:

sslStream.AuthenticateAsClient("localhost");

服务器:

sslStream.AuthenticateAsServer(serverCert); 
//serverCert is X509Certificate2 built from "server.pfx"

客户端的 AuthenticateAsClient 方法会抛出异常

“根据验证程序,远程证书无效。”

我猜是因为服务器的证书是由不受信任的CA签发的,所以认证失败,那我怎么把CA证书添加到我的信任列表呢?

我试图在客户端代码中添加下面的代码,但它不起作用

        X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadWrite);
        store.Add(new X509Certificate2(Resources.CACertPath));
        store.Close();
        sslStream.AuthenticateAsClient("localhost");

最佳答案

以下代码将避免 Windows 证书存储并验证链。

我认为没有理由在证书存储中添加验证链所需的 CA 证书到数百个。这意味着 Windows 将尝试使用“数百 + 1”证书而不是真正需要的一个证书来验证链。

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

函数的名称无关紧要。下面,VerifyServerCertificateSslStream 类中的 RemoteCertificateValidationCallback 是相同的回调。您还可以将它用于 ServicePointManager 中的 ServerCertificateValidationCallback

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;
}

关于c# - SslStream 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206528/

相关文章:

ssl - SWF(使用 URLLoader)可以访问 HTTPS 网络服务吗?

ssl - 如何在加特林中关闭 TLSv1.3?

c# - MVC 模型绑定(bind) - 验证每个属性?

c# - 如何将应用程序栏颜色转换为主题资源颜色

C# 对 XElement 进行排序,带注释

java - 将对象流与 SSL 套接字一起使用时出错

php - PDO 与数据库的连接在证书验证时失败

c# - NHibernate 和多个数据库

c# - 正则表达式删除 C# 中的转义字符(特定字符)

tomcat - SSL 握手 - 客户端身份验证