2012-10-06 19 views
1

我有一個Java代碼,試圖與網站創建ssl握手。當網站的SSL證書是自簽名的或使用不安全的選項時,我會收到錯誤,並且代碼無法提取證書。如何讓我的java代碼接受完成握手與自簽名或弱sslv2協議

SSLSocket socket=null; 
SSLSocketFactory factory=null; 
factory = HttpsURLConnection.getDefaultSSLSocketFactory(); 

try{ 
    socket = (SSLSocket) factory.createSocket(host, port);   
    System.out.println("listen on port "+port+" for host "+host); 

    } //end try 

    catch (IOException ex) 
     { 
      //remote host is not listening on port 443 
      System.out.println("Can not listen on port "+port+" of host"+host); 
     } //end catch 

     System.out.println("Creating a SSL Socket For "+host+" on port "+port); 
     SSLSession ss = socket.getSession(); 
     socket.startHandshake(); // start the handshake 
     System.out.println("Handshake Done"); 

舉個例子,我得到以下錯誤:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Insecure renegotiation is not allowed 

我怎樣才能讓我的Java代碼接受完成與自簽名和弱SSL配置握手?

+0

可能的重複[如何接受與Java HttpsURLConnection的自簽名證書?](http://stackoverflow.com/questions/859111/how-do-i-accept-a-self-signed-certificate -with-a-java-httpsurlconnection) –

+0

在問題中沒有證據表明您需要支持SSLv2。 – EJP

回答

1

這裏至少有3個可能的問題。弱化你的SSL/TLS配置幾乎總是一個壞主意。

  • 首先,您可以啓用一些默認情況下未啓用的密碼套件。那些支持的和默認啓用的列在Sun Provider documentation中。您可以在SSLSocket/SSLEngine上啓用使用setEnabledCipherSuites的特定密碼套件。值得一讀關於特定cipher suites in the TLS specification的腳註和註釋(特別是匿名密碼套件缺乏安全性)。

  • 重新協商問題稍有不同。您可以使用system property introduced in the updatesun.security.ssl.allowUnsafeRenegotiation。正如名稱所示,這是不安全的。

  • 接受任何證書是另一個問題。一般來說,您應該輸入您想要的特定證書。但是,您可以從X509TrustManager生成SSLContext,在檢查服務器證書時從不會引發任何異常。這裏或其他網站上有很多不安全代碼的例子。

JSSE默認行爲往往是相當明智的。引入不安全的行爲應謹慎。學習/實驗可以沒問題,但是在生產軟件(甚至原型)中這樣做是有問題的。