2015-01-02 20 views
5

我有一個C#程序連接到一個網絡服務器並顯示SSL證書的到期日期。如何確定與網絡服務器的連接是否使用完全正向保密?

我想知道的是如何確定連接是否使用完美向前保密[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; 
     } 
    } 
} 

回答

6

前言:我不是密碼學家。

this Information Security答案,你想看看約定的密碼套件,即套件的密鑰交換片。據此,任何基於Diffie-Hellman的產品都能提供完美的前向保密。正如埃裏克森在評論中指出的那樣,這可能是不真實的,並且您會想要了解假設完全向前保密存在的安全性複雜性,但實際上並非如此。

因此,您正在尋找SslStream。這將允許您訪問您所需的密鑰交換屬性。

它不像使用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確實支持Perfect Forward Secrecy。

如果要修改當前的代碼以使所有這些都發生在一個連接中,則可以使用遠程證書sslStream.RemoteCertificate,以便獲得證書到期日期。

[1]有可能並非所有Diffie-Hellman交換都支持完美前向保密。再次考慮這一點的安全性複雜性。

+0

我將不得不閱讀,但我相信有DH模式,*不*提供PFS。我認爲你需要更具體地查看*臨時* DH密鑰協議(EDH_XXX密碼套件)。 – erickson

+1

尋找DHE(EDH; Diffie-Hellman短暫)和ECDHE(EECDH;橢圓曲線Diffie-Hellman短暫)。只有這些密鑰協議算法才提供PFS。 DH,ECDH和RSA不。 – zakjan

+0

Steven,我正確的理解你在說如果使用WebRequest.Create創建一個連接,那麼就沒有辦法確定正在使用什麼類型的加密? – JonnyBoats

相關問題