How to determine if a connection to a web server uses Perfect Forward Secrecy?

I have a C # program that connects to a web server and displays the expiration date of the SSL certificate.

I would like to know how to determine if a connection uses 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; } } } 
+5
source share
1 answer

Foreword: I am not a cryptographer.

In this information security, answer, you want to look at the agreed set of ciphers, namely at the key part of the packet exchange. According to this, everything based on Diffie-Hellman provides perfect direct secrecy. As Erickson notes in the comments, this may not be true, and you might want to understand security issues, suggesting that Perfect Forward Secrecy is present when it really is not.

With this you are looking for SslStream . This will allow you to access the key exchange properties that you need.

It's not as simple as using WebRequest or even HttpRequest . You will have to write the link yourself. An example looks like this:

 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 } 

In theory, KeyExchangeAlgorithm is an enumeration. And you can do if(sslStream.KeyExchangeAlgorithm == ExchangeAlgorithmType.DiffieHellman) , and you will find out the answer [1] . But according to this Microsoft Forum post , ExchangeAlgorithmType could be 44550, which is equivalent to the Diffie-Hellman elliptic curve. The Diffie-Hellman elliptical curve supports Perfect Forward Secrecy.

If you want to change your current code so that all this happens in one connection, the remote certificate is available in sslStream.RemoteCertificate so that you can get the certificate expiration date.

[1] Perhaps not all Diffie-Hellman exchanges support Perfect Forward Secrecy. Again, consider the security concerns of this.

+6
source

Source: https://habr.com/ru/post/1210251/


All Articles