c# - 我如何确定与网络服务器的连接是否使用完美前向保密?

标签 c# ssl https

我有一个连接到网络服务器并显示 SSL 证书到期日期的 C# 程序。

我想知道的是如何确定连接是否使用完美前向保密 [PFS]?

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
            ServicePointManager.CheckCertificateRevocationList = true;

            var request = WebRequest.Create("https://www.microsoft.com/");

            var response = request.GetResponse();

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
        private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            Console.WriteLine("Certificate expires on " + certificate.GetExpirationDateString());

            return true;
        }
    }
}

最佳答案

前言:我不是密码学家。

根据 this Information Security答,你要看约定的密码组,即组中的 key 交换片。据此,任何基于 Diffie-Hellman 的算法都可以提供完美的前向保密性。正如埃里克森在评论中指出的那样,这可能是不真实的,您需要了解假设存在完美前向保密而实际上并不存在的安全复杂性。

有了它,您正在寻找 SslStream .这将使您能够访问所需的 key 交换属性。

它不像使用 WebRequest 甚至 HttpRequest 那样容易。你将不得不自己写出连接。一个例子看起来像:

string host = "www.microsoft.com";

using (var client = new TcpClient(host, 443))
using (var stream = client.GetStream())
using (var sslStream = new SslStream(stream))
{
    sslStream.AuthenticateAsClient(host);

    // figure out if sslStream.KeyExchangeAlgorithm support PFS here
}

理论上,KeyExchangeAlgorithm 是一个枚举。您可以执行 if(sslStream.KeyExchangeAlgorithm == ExchangeAlgorithmType.DiffieHellman),您就会知道答案[1]。但是根据this Microsoft Forum post , ExchangeAlgorithmType 可能是 44550 等同于椭圆曲线 Diffie-Hellman。椭圆曲线 Diffie-Hellman 确实支持完美前向保密。

如果您想修改当前代码以在一个连接中实现所有这些,可在 sslStream.RemoteCertificate 中获取远程证书,这样您就可以获得证书到期日期。

[1] 可能并非所有 Diffie-Hellman 交换都支持完美前向保密。同样,请考虑这样做的安全问题。

关于c# - 我如何确定与网络服务器的连接是否使用完美前向保密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741449/

相关文章:

c# - 如何在gridview中连续显示图片

c# - 通过特定的 where 子句在 C# 中创建通用方法

c# - 必须声明标量变量

amazon-web-services - Amazon Web Service ECS (SSL/HTTPS) 问题

java - 如何使用默认的 snakeoil 证书从 Java 应用程序服务器向 Apache 服务器发送 HTTPS 请求?

python - pip 是否可以在不检查 SSL 证书的情况下访问 pypi,或者它甚至可以使用 http 而不是 https?

c# - 类型可以解析字符串的通用类

iphone - 加密密码

ios - 验证服务器是否符合 Apple 的 ATS/TLS 1.2 要求的最佳方式

c# - SslStream:处理证书时发生未知错误