Search code examples
javasslhttpskeystorehandshake

HttpsServer Handlers were not getting called


I am using the following HttpsServer to implement a simple Secure server. First of all I have the keystore created and I have a key pair generated and also I have a self signed certificate in the keystore. I am using the following code to setup the code.

server = HttpsServer.create(new InetSocketAddress( SERVER_PORT), conversationCount);
SSLContext sslContext = SSLContext.getInstance("TLS");
char[] password= "XXXXX".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("server_keystore");
ks.load(fis, password);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, password);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

And my server code is as follows.

server.setHttpsConfigurator( getConfigurator(sslContext));
server.createContext("/", new RootRequestHandler());
server.setExecutor(threadPool);
server.start();
threadPool= Executors.newFixedThreadPool(conversationCount);
threadPool.execute(new Runnable() {
    public void run() {
        System.out.println("Asynchronous task");
    }
});

My Configurator looks like follows.

private HttpsConfigurator getConfigurator( SSLContext sslContext) {
    return new HttpsConfigurator(sslContext) {
        @Override
        public void configure (HttpsParameters params) {
            SSLEngine m_engine = sslContext.createSSLEngine();
            final SSLContext context = getSSLContext();
            final SSLParameters sslParams = context.getDefaultSSLParameters();
            params.setNeedClientAuth(false);
            params.setSSLParameters(sslParams);
            params.setCipherSuites(m_engine.getEnabledCipherSuites());
            params.setProtocols(m_engine.getEnabledProtocols());
        }
    };
}

When ever I open the https://localhost:8001/ "This web page is not available" "ERR_CONNECTION_CLOSED" error is displayed on the browser.

I also tested the code with a unit test and the test fails with following error.

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

I know that when ever I call the server from the browser I ve seen the configuration code is always called but not the handler. Some how the request is not directed to even though the server is called. I am not able to figure out what I am doing wrong here. Hope someone can help me.


Solution

  • After some trial and error in the code I figure that there is nothing wrong with the code so I changed the types of the keys that I have been using. So then I realized that the error was with the keys and for some reason EC keys were not usable and RSA keys were usable.