14
votes

I want to create an app use SSLSocket: client send a String to server and server will uppercase that String and send back to client for display.

SSLServer

public class SSLServer {
    public static void main(String args[]) throws Exception
    {
        try{
        //Creaet a SSLServersocket
        SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
        //Tạo 1 đối tượng Socket từ serversocket để lắng nghe và chấp nhận kết nối từ client
        SSLSocket sslsocket=(SSLSocket) sslserversocket.accept();
        //Tao cac luong de nhan va gui du lieu cua client
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());
        PrintStream os=new PrintStream(sslsocket.getOutputStream());
        while(true)  //khi dang ket noi voi client
        {
            //Doc du lieu den
            String input=is.readUTF();
            String ketqua=input.toUpperCase();
            //Du lieu tra ve
            os.println(ketqua);
        }
        }
        catch(IOException e)
        {
           System.out.print(e);
        }
    }
}

SSLClient

public class SSLClient {
    public static void main(String args[])
    {
        try
        {
        //Mo 1 client socket den server voi so cong va dia chi xac dinh
        SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket=(SSLSocket) factory.createSocket("127.0.0.1",1234);

        //Tao luong nhan va gui du lieu len server
        DataOutputStream os=new DataOutputStream(sslsocket.getOutputStream());
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());

        //Gui du lieu len server
        String str="helloworld";
        os.writeBytes(str);

        //Nhan du lieu da qua xu li tu server ve
        String responseStr;
        if((responseStr=is.readUTF())!=null)
        {
            System.out.println(responseStr);
        }

        os.close();
        is.close();
        sslsocket.close();
        }
        catch(UnknownHostException e)
        {
             System.out.println(e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

When run SSLServer. It displays this error:

javax.net.ssl.SSLException: No available certificate or key corresponds 
    to the SSL cipher suites which are enabled

I have search and do some ways but.. Can you help me.

3
server at startup gives you that exception? on which line? have you googled for that error?Nikolay Kuznetsov
These examples will help you. You will need correct SSL certificate in keystore. example 1 example 2Muhammad Imran Tariq
I've seen those: github.com/gpotter2/SSLKeystoreFactories which are 2 classes that ease the creation of SSL sockets. It's quite old but still workingCukic0d

3 Answers

13
votes

This will generate certificate:

keytool -genkey -keystore yourKEYSTORE -keyalg RSA

Enter yourPASSWORD and than start your server with ssl debug information(put yourKEYSTORE into directory with SSLServer.class):

java -Djavax.net.ssl.keyStore=yourKEYSTORE -Djavax.net.ssl.keyStorePassword=yourPASSWORD -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl SSLServer

Than start your client(put yourKEYSTORE into directory with SSLClient.class):

java -Djavax.net.ssl.trustStore=yourKEYSTORE -Djavax.net.ssl.trustStorePassword=yourPASSWORD SSLClient
2
votes

@corVaroxid's answer is right. But if you want to set configurations programmatically to avoid global settings (like me), you can go like below (Kotlin):

val password = "yourPassword".toCharArray()

val keyStore = KeyStore.getInstance(File("yourKeystorePath.jks"), password)

val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(keyStore)

val keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509")
keyManagerFactory.init(keyStore, password)

val context = SSLContext.getInstance("TLS") //"SSL" "TLS"
context.init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, null)

val factory = context.serverSocketFactory

(factory.createServerSocket(LISTENING_PORT) as SSLServerSocket).use { serverSocket ->
    logger.trace("Listening on port: $LISTENING_PORT")

    // ...
}

Or in Java:

final char[] password = "yourPassword".toCharArray();

final KeyStore keyStore = KeyStore.getInstance(new File("yourKeystorePath.jks"), password);

final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);

final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509");
keyManagerFactory.init(keyStore, password);

final SSLContext context = SSLContext.getInstance("TLS");//"SSL" "TLS"
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

final SSLServerSocketFactory factory = context.getServerSocketFactory();

try (SSLServerSocket serverSocket = ((SSLServerSocket) factory.createServerSocket(LISTENING_PORT))) { 
    logger.trace("Listening on port: " + LISTENING_PORT);

    // ...
}
0
votes

Check the certificates that you have installed. Make sure they are supporting the cipher suites that you are negotiating.