如何确定与Web服务器的连接是否正在使用Perfect Forward Secrecy?

Jon*_*ats 6 c# ssl https

我有一个连接到Web服务器的C#程序,并显示SSL证书的到期日期.

我想知道的是如何确定连接是否正在使用Perfect Forward Secrecy [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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*n V 7

前言:我不是密码学家.

根据此信息安全性答案,您希望查看商定的密码套件,即套件的密钥交换部分.据此,基于Diffie-Hellman的任何东西都提供了完美的前向保密.正如埃里克森在评论中指出的那样,这可能是不真实的,你会想要理解假设完美前向保密存在的安全复杂性,而实际上并非如此.

有了它,你正在寻找SslStream.这样您就可以访问所需的密钥交换属性.

它不像使用WebRequest甚至是a 那么容易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
}
Run Code Online (Sandbox Code Playgroud)

理论上,KeyExchangeAlgorithm是一个枚举.你可以这样做if(sslStream.KeyExchangeAlgorithm == ExchangeAlgorithmType.DiffieHellman),你会知道答案[1].但根据微软论坛帖子,ExchangeAlgorithmType可能是44550,相当于Elliptic曲线Diffie-Hellman.椭圆曲线Diffie-Hellman确实支持Perfect Forward Secrecy.

如果要修改当前代码以在一个连接中实现所有这些,则可以使用远程证书,sslStream.RemoteCertificate以便获得证书到期日期.

[1]有可能并非所有Diffie-Hellman交换都支持Perfect Forward Secrecy.再次,考虑一下这种安全问题.