I am trying to build a standalone WebSocket client in Java that connects to a WebSocket Server in C#. I am using a Self-Signed Certificate that was generated for the WebSocket server. I'm able to connect to the WebSocket server in my Chrome browser using WSS without any issues. When trying to use the TooTallNate/Java-WebSocket library, it does not work. The OnClose method is called immediately on connection.
I have a Self-Signed certificate in a *.pfx file generated that I use for the C# server and is imported to my Trusted Root store. I created a *.jks from this file using keytool (I think I've done this correctly). I am using keystore.jks which was generated by keytool in my code.
Here is my main method:
public static void main(String[] args) throws Exception {
ChatClient chatclient = new ChatClient("wss://thomas.localnetwork.com:65120/wsserver?userId=Thomas", "Thomas", "1", "1", "L");
// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = "C:\\Users\\thomas\\Desktop\\keystore.jks";
String STOREPASSWORD = "test12345";
String KEYPASSWORD = "test";
KeyStore ks = KeyStore.getInstance( STORETYPE );
File kf = new File( KEYSTORE );
ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() );
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( ks, KEYPASSWORD.toCharArray() );
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( ks );
SSLContext sslContext = null;
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
// sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
chatclient.setSocketFactory( factory );
chatclient.connectBlocking();
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
while ( true ) {
String line = reader.readLine();
if( line.equals( "close" ) ) {
chatclient.closeBlocking();
} else if ( line.equals( "open" ) ) {
chatclient.reconnect();
} else {
ucm.send( line );
}
}
}
Here is the exception I am receiving:
Closed connection on wss://laptop-thomas.compuflex1.com:65120/wsucm_deviceagent?userId=Thomas
javax.net.ssl.SSLException: java.net.SocketException: Connection reset
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1906)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1870)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1815)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:116)
at java.io.InputStream.read(InputStream.java:101)
at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:424)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:928)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
... 3 more
Is there any way I could get more details about the handshake or what the actual problem is? The exception stack trace does not seem to be helpful at all.