0

Could somebody please tell me what I am doing wrong in this code? The server side seems to be working. Once I run the code for the server side the server waits for a client to request a connection. Then when I run the client code it throws the following exception on the client side:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at LoginDialog.doConnectToServer(LoginDialog.java:208)
    at LoginDialog.<init>(LoginDialog.java:67)
    at ClientSideMain.main(ClientSideMain.java:5)
Press any key to continue . . .

and the following exception on the server side:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: no cipher suites in common
    at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at Talker.recieve(Talker.java:78)
    at CTC.run(CTC.java:49)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ServerHandshaker.chooseCipherSuite(Unknown Source)
    at sun.security.ssl.ServerHandshaker.clientHello(Unknown Source)
    at sun.security.ssl.ServerHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeBytes(Unknown Source)
    at Talker.send(Talker.java:58)
    at CTC.send(CTC.java:344)
    at CTC.<init>(CTC.java:31)
    at ServerSide.<init>(ServerSide.java:73)
    at ServerSideMain.main(ServerSideMain.java:5)

Server Side Code

public class ServerSide
{
ServerSocket serverSocket;
Socket regSocket;
Hashtable<String,User> userList;
CTC tempCTC;
File f;
DataInputStream in;
BufferedReader br;

SSLContext sslContext;
KeyManagerFactory keyManagerFactory;
KeyStore keyStore;
char[] keyStorePassphrase;

SSLServerSocketFactory sslServerSocketFactory;
SSLServerSocket sslServerSocket;
SSLSocket sslNormalSocket;

ServerSide()
{
    f = new File("userlist.txt");
    userList = new Hashtable<String, User>();
    loadUsers(f);

    try
    {
        sslContext = SSLContext.getInstance("SSL");
        keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyStore = KeyStore.getInstance("JKS");
        keyStorePassphrase = "passphrase".toCharArray();
        keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);
        keyManagerFactory.init(keyStore, keyStorePassphrase);
        sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
        sslServerSocketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();

        sslServerSocket = (SSLServerSocket)sslServerSocketFactory.createServerSocket(12345);

        try{Thread.sleep(1000);}catch(Exception e){}

        while(true)
        {
            sslNormalSocket = (SSLSocket)sslServerSocket.accept();
            tempCTC = new CTC(sslNormalSocket, userList, f);
        }
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
    catch(NoSuchAlgorithmException nsae)
    {
        nsae.printStackTrace();
    }
    catch(KeyStoreException kse)
    {
        kse.printStackTrace();
    }
    catch(CertificateException ce)
    {
        ce.printStackTrace();
    }
    catch(UnrecoverableKeyException uke)
    {
        uke.printStackTrace();
    }
    catch(KeyManagementException kme)
    {
        kme.printStackTrace();
    }
}

Client Side Code (Where the exception occurs)

private void doConnectToServer()
{
    SSLSocketFactory        sslSocketFactory;
    KeyManagerFactory       keyManagerFactory;
    SSLContext              sslContext;
    KeyStore                keyStore;
    char[]                  keyStorePassphrase;
    SSLSocket               sslSocket;

    try
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        System.setProperty("javax.net.ssl.trustStore", "samplecacerts");
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

        sslContext = SSLContext.getInstance("SSL");
        keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyStore = KeyStore.getInstance("JKS");

        keyStorePassphrase = "passphrase".toCharArray();
        keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);

        keyManagerFactory.init(keyStore, keyStorePassphrase);
        sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

        sslSocketFactory = sslContext.getSocketFactory();

        sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();

        sslSocket = (SSLSocket)sslSocketFactory.createSocket("127.0.0.1", 12345);

        sslSocket.startHandshake(); //line that throws the exception

        cts = new CTS(sslSocket, this);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
BCRwar3
  • 45
  • 1
  • 11

1 Answers1

0

Seems like you dont have the same encryption method on both sides (server/client).

Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common

Make sure you are using the same on both sides.

Tiago Luz
  • 129
  • 6