Daniel Garza
Daniel Garza

Reputation: 35

SSL Server in Java - javax.net.ssl.SSLException

I am trying to create a server using SSL but I keep getting the following error:

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

I am not sure if I am creating the certificates correctly. Here is my code. I am converting an old TCP Server to an SSL Server

// SSL Server

import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;

public class SSL_Server {


public static void main(String[] args) {
    int port = 2018;
    ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
    ServerSocket ssocket = null;
    System.out.println("SSL_Server started");

    System.setProperty("javax.net.ssl.keyStore","mySrvKeystore");
    System.setProperty("javax.net.ssl.keyStorePassword","123456");

    final ExecutorService threadPool = Executors.newCachedThreadPool();

    try {
        ssocket = ssocketFactory.createServerSocket(port);
        InetAddress myIP =InetAddress.getLocalHost();
        System.out.println(myIP.getHostAddress());

        while(true){
            Socket aClient = ssocket.accept();
            //create a new thread for every client
            threadPool.submit(new SSL_ClientHandler(aClient));
        } 
    } 
    catch(Exception e) {
        System.err.println("Server aborted:" + e);
    } finally {
        try{
            ssocket.close();
        } catch (Exception e){
            System.err.println("could not close connection properly" + e);
        }
    }
    System.out.println("connection was closed successfully");
}

}

Upvotes: 1

Views: 4298

Answers (2)

Leslie Esquierdo
Leslie Esquierdo

Reputation: 21

System.setProperty("javax.net.ssl. keyStore","mySrvKeystore"); System.setProperty("javax.net.ssl. keyStorePassword","123456");

ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); 
ServerSocket ssocket = null; 
System.out.println("SSL_Server started");

Upvotes: 2

Bruno
Bruno

Reputation: 122599

You should set the properties that configure the default SSLContext (and thus the default SSLServerSocketFactory) before getting it, since it will configure it then.

System.setProperty("javax.net.ssl.keyStore","mySrvKeystore");
System.setProperty("javax.net.ssl.keyStorePassword","123456");

ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ServerSocket ssocket = null;
System.out.println("SSL_Server started");

Upvotes: 1

Related Questions