2017-02-21 194 views
0

我有一個服務器,我有一個客戶端。我有他們兩個在同一臺機器上運行。我試圖建立客戶端和服務器之間的SSL連接。我使用以下keytool命令爲服務器和客戶端生成了證書。javax.net.ssl.SSLHandshakeException:null證書鏈空證書鏈

對於客戶 的keytool -keystore clientstore -genkey -alias客戶-validity 3650

然後我在客戶端的根證書導出到callled client.cer

服務器A CER文件 的keytool -keystore serverstore -genkey -alias服務器-validity 3650 然後,我將服務器的根證書導出到cer文件,調用server.cer

我現在將客戶端證書「client.cer」導入serverstore密鑰存儲庫中, G指令

的keytool -import -keystore serverstore -file client.cer -alias客戶

而且還導入服務器證書 「server.cer」 到clientstore密鑰庫使用以下命令

的keytool -import -keystore clientstore -file server.cer -alias服務器

完成此操作後,我將server.cer和client.cer都導入到cacerts密鑰存儲區。但是,當我嘗試建立一個ssl連接時,我得到服務器上的此錯誤javax.net.ssl.SSLHandshakeException:null證書鏈和客戶端javax.net.ssl.SSLHandshakeException上的此錯誤:收到致命警報:bad_certificate。

我的服務器代碼。

package serverapplicationssl; 


import java.io.*; 
import java.security.KeyStore; 
import java.security.Security; 
import java.security.PrivilegedActionException; 

import javax.net.ssl.*; 
import com.sun.net.ssl.internal.ssl.Provider; 

import org.bouncycastle.jce.provider.BouncyCastleProvider; 

import java.security.Security; 

import java.io.*; 

public class ServerApplicationSSL { 

public static void main(String[] args) { 
    boolean debug = true; 

    System.out.println("Waiting For Connection"); 

    int intSSLport = 4447; 

    { 
     Security.addProvider(new Provider()); 

    } 
    if (debug) { 
     System.setProperty("javax.net.debug", "all"); 
    } 
    FileWriter file = null; 
    try { 
     file = new FileWriter("C:\\SSLCERT\\Javalog.txt"); 

    } catch (Exception ee) { 
     //message = ee.getMessage(); 

    } 

    try { 

     KeyStore keystore = KeyStore.getInstance("JKS"); 
     keystore.load(new FileInputStream("C:\\SSLCERT\\OntechServerKS"), "server".toCharArray()); 
     file.write("Incoming Connection\r\n"); 

     KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory 
       .getDefaultAlgorithm()); 
     kmf.init(keystore, "server".toCharArray()); 

     SSLContext context = SSLContext.getInstance("TLS"); 
     context.init(kmf.getKeyManagers(), null, null); 

     SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory) context.getServerSocketFactory(); 
     SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(intSSLport); 
     sslServerSocket.setEnabledCipherSuites(sslServerSocket.getSupportedCipherSuites()); 
     sslServerSocket.setNeedClientAuth(true); 
     SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); 
     //SSLServerSocket server_socket = (SSLServerSocket) sslServerSocket; 

     sslSocket.startHandshake(); 

    // Start the session 
     System.out.println("Connection Accepted"); 
     file.write("Connection Accepted\r\n"); 

     while (true) { 
      PrintWriter out = new PrintWriter(sslSocket.getOutputStream(), true); 

      String inputLine; 

      //while ((inputLine = in.readLine()) != null) { 
      out.println("Hello Client....Welcome"); 
      System.out.println("Hello Client....Welcome"); 
      //} 

      out.close(); 
      //in.close(); 
      sslSocket.close(); 
      sslServerSocket.close(); 
      file.flush(); 
      file.close(); 
     } 

    } catch (Exception exp) { 
     try { 
      System.out.println(exp.getMessage() + "\r\n"); 
      exp.printStackTrace(); 
      file.write(exp.getMessage() + "\r\n"); 
      file.flush(); 
      file.close(); 
     } catch (Exception eee) { 
      //message = eee.getMessage(); 
     } 

    } 

} 

} 

這裏是我的客戶代碼

import java.io.*; 
import java.net.*; 
import java.security.*; 
import java.util.Enumeration; 

import javax.net.ssl.*; 

public class SSLConnect { 

public String MakeSSlCall(String meternum) { 
    String message = ""; 
    FileWriter file = null; 
    try { 
     file = new FileWriter("C:\\SSLCERT\\ClientJavalog.txt"); 

    } catch (Exception ee) { 
     message = ee.getMessage(); 

    } 
    //writer = new BufferedWriter(file); 
    try { 
     file.write("KeyStore Generated\r\n"); 
     KeyStore keystore = KeyStore.getInstance("JKS"); 
     keystore.load(new FileInputStream("C:\\SSLCERT\\SkyeClientKS"), "client".toCharArray()); 

     file.write("KeyStore Generated\r\n"); 
     Enumeration enumeration = keystore.aliases(); 
     while (enumeration.hasMoreElements()) { 
      String alias = (String) enumeration.nextElement(); 
      file.write("alias name: " + alias + "\r\n"); 
      keystore.getCertificate(alias); 
      file.write(keystore.getCertificate(alias).toString() + "\r\n"); 
     } 
     TrustManagerFactory tmf =TrustManagerFactory.getInstance("SunX509"); 
     tmf.init(keystore); 
     file.write("KeyStore Stored\r\n"); 
     SSLContext context = SSLContext.getInstance("SSL"); 
     TrustManager[] trustManagers = tmf.getTrustManagers(); 
     context.init(null, trustManagers, null); 

     SSLSocketFactory f = context.getSocketFactory(); 
     file.write("About to Connect to Ontech\r\n"); 
     SSLSocket c = (SSLSocket) f.createSocket("192.168.1.16", 4447); 
     file.write("Connection Established to 196.14.30.33 Port: 8462\r\n"); 
     file.write("About to Start Handshake\r\n"); 
     c.startHandshake(); 
     file.write("Handshake Established\r\n"); 
     file.flush(); 
     file.close(); 
     return "Connection Established"; 
    } catch (Exception e) { 
     try { 
      file.write("An Error Occured\r\n"); 
      file.write(e.getMessage() + "\r\n"); 
      StackTraceElement[] arrmessage = e.getStackTrace(); 
      for (int i = 0; i < arrmessage.length; i++) { 
       file.write(arrmessage[i] + "\r\n"); 
      } 

      file.flush(); 
      file.close(); 
     } catch (Exception eee) { 
      message = eee.getMessage(); 

     } 
     return "Connection Failed"; 
    } 
} 
} 

堆棧跟蹤Execption我的服務器

javax.net.ssl.SSLHandshakeException: null cert chain 
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:292) 
    at sun.security.ssl.ServerHandshaker.clientCertificate(ServerHandshaker.java:1804) 
    at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:222) 
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:957) 
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:892) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375) 
    at serverapplicationssl.ServerApplicationSSL.main(ServerApplicationSSL.java:69) 

堆棧跟蹤Execption我的客戶端上

Received fatal alert: bad_certificate 
sun.security.ssl.Alerts.getSSLException(Unknown Source) 
sun.security.ssl.Alerts.getSSLException(Unknown Source) 
sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source) 
sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) 
sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) 
sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
SSLConnect.MakeSSlCall(SSLConnect.java:96) 
BankCollectSSLCon.main(BankCollectSSLCon.java:13) 

什麼可能會造成這個錯誤?可以的因爲我在同一臺計算機上同時運行服務器和客戶端?...已經在此上了很長時間了。我需要幫助

+0

你發現什麼問題了?我有類似的問題。 – WiteCastle

+0

發表了答案 –

回答

2

@WiteCastle是的,我相信我,我有一個真正不愉快的經歷,找出問題所在。在我粘貼我的代碼片段之前,首先解釋客戶端和服務器之間的SSL Communication.SSL連接。

  1. 客戶說你好
  2. 服務器說你好。
  3. 服務器提供。驗證證書。
  4. 客戶端檢查證書是否在其TrustStore中。
  5. 客戶端證書的服務器請求。
  6. 客戶端提供證書供服務器驗證。
  7. 如果客戶端證書通過驗證通信現在可以發生 。

所以javax.net.ssl.SSLHandshakeException:收到致命警報:當客戶端證書上的服務器和javax.net.ssl.SSLHandshakeException驗證失敗情況發生空證書鏈錯誤bad_certificate發生在服務器無法找到證明客戶在其信任庫中提供的證書。 所以我所做的就是

爲客戶

import java.io.*; 
import java.net.*; 
import java.security.*; 
import java.util.Enumeration; 

import javax.net.ssl.*; 

public class SSLConnect { 

public String MakeSSlCall(String meternum) { 
String message = ""; 
FileWriter file = null; 
try { 
    file = new FileWriter("C:\\SSLCERT\\ClientJavalog.txt"); 

} catch (Exception ee) { 
    message = ee.getMessage(); 

} 
//writer = new BufferedWriter(file); 
try { 
    file.write("KeyStore Generated\r\n"); 
    KeyStore keystore = KeyStore.getInstance("JKS"); 
    keystore.load(new FileInputStream("C:\\SSLCERT\\SkyeClientKS"), 
"client".toCharArray()); 

    file.write("KeyStore Generated\r\n"); 
    Enumeration enumeration = keystore.aliases(); 
    while (enumeration.hasMoreElements()) { 
     String alias = (String) enumeration.nextElement(); 
     file.write("alias name: " + alias + "\r\n"); 
     keystore.getCertificate(alias); 
     file.write(keystore.getCertificate(alias).toString() + "\r\n"); 
    } 
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory 
       .getDefaultAlgorithm()); 
     kmf.init(keystore, KeystorePassword.toCharArray()); 
    TrustManagerFactory tmf =TrustManagerFactory.getInstance("SunX509"); 
    tmf.init(keystore); 
    file.write("KeyStore Stored\r\n"); 
    SSLContext context = SSLContext.getInstance("SSL"); 
    TrustManager[] trustManagers = tmf.getTrustManagers(); 

    context.init(kmf.getKeyManagers(), trustManagers, null); 

    SSLSocketFactory f = context.getSocketFactory(); 
    file.write("About to Connect to Ontech\r\n"); 
    SSLSocket c = (SSLSocket) f.createSocket("192.168.1.16", 4447); 
    file.write("Connection Established to 196.14.30.33 Port: 8462\r\n"); 
    file.write("About to Start Handshake\r\n"); 
    c.startHandshake(); 
    file.write("Handshake Established\r\n"); 
    file.flush(); 
    file.close(); 
    return "Connection Established"; 

} catch (Exception e) { 
    try { 
     file.write("An Error Occured\r\n"); 
     file.write(e.getMessage() + "\r\n"); 
     StackTraceElement[] arrmessage = e.getStackTrace(); 
     for (int i = 0; i < arrmessage.length; i++) { 
      file.write(arrmessage[i] + "\r\n"); 
     } 

     file.flush(); 
     file.close(); 
    } catch (Exception eee) { 
     message = eee.getMessage(); 

    } 
    return "Connection Failed"; 
} 
} 
} 

對於服務器

package serverapplicationssl; 


import java.io.*; 
import java.security.KeyStore; 
import java.security.Security; 
import java.security.PrivilegedActionException; 

import javax.net.ssl.*; 
import com.sun.net.ssl.internal.ssl.Provider; 

import org.bouncycastle.jce.provider.BouncyCastleProvider; 

import java.security.Security; 

import java.io.*; 

public class ServerApplicationSSL { 

public static void main(String[] args) { 
boolean debug = true; 

System.out.println("Waiting For Connection"); 

int intSSLport = 4447; 

{ 
    Security.addProvider(new Provider()); 

} 
if (debug) { 
    System.setProperty("javax.net.debug", "all"); 
} 
FileWriter file = null; 
try { 
    file = new FileWriter("C:\\SSLCERT\\Javalog.txt"); 

} catch (Exception ee) { 
    //message = ee.getMessage(); 

} 

try { 

    KeyStore keystore = KeyStore.getInstance("JKS"); 
    keystore.load(new FileInputStream("C:\\SSLCERT\\OntechServerKS"), 
"server".toCharArray()); 
    file.write("Incoming Connection\r\n"); 

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory 
      .getDefaultAlgorithm()); 
    kmf.init(keystore, "server".toCharArray()); 

    TrustManagerFactory tmf =TrustManagerFactory.getInstance("SunX509"); 
    tmf.init(keystore); 
    file.write("KeyStore Stored\r\n"); 
    TrustManager[] trustManagers = tmf.getTrustManagers(); 

    SSLContext context = SSLContext.getInstance("TLS"); 
    context.init(kmf.getKeyManagers(), trustManagers, null); 

    SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory) context.getServerSocketFactory(); 
    SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(intSSLport); 
    sslServerSocket.setEnabledCipherSuites(sslServerSocket.getSupportedCipherSuites()); 
    sslServerSocket.setNeedClientAuth(true); 
    SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); 
    //SSLServerSocket server_socket = (SSLServerSocket) sslServerSocket; 

    sslSocket.startHandshake(); 

// Start the session 
    System.out.println("Connection Accepted"); 
    file.write("Connection Accepted\r\n"); 

    while (true) { 
     PrintWriter out = new PrintWriter(sslSocket.getOutputStream(), true); 

     String inputLine; 

     //while ((inputLine = in.readLine()) != null) { 
     out.println("Hello Client....Welcome"); 
     System.out.println("Hello Client....Welcome"); 
     //} 

     out.close(); 
     //in.close(); 
     sslSocket.close(); 
     sslServerSocket.close(); 
     file.flush(); 
     file.close(); 
    } 

} catch (Exception exp) { 
    try { 
     System.out.println(exp.getMessage() + "\r\n"); 
     exp.printStackTrace(); 
     file.write(exp.getMessage() + "\r\n"); 
     file.flush(); 
     file.close(); 
    } catch (Exception eee) { 
     //message = eee.getMessage(); 
    } 

    } 

} 

}