0

I'm trying to create a secure connection in Java. For that I created following server and client

Server:

    public static void main(String[] args){
    try {
        // relative keystorepath
        String certificateChain = "keystore";
        String password = "***";
        System.setProperty("javax.net.ssl.keyStore", certificateChain);
        System.setProperty("javax.net.ssl.keyStorePassword", password);
        SSLServerSocketFactory sslserversocketfactory =
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        SSLServerSocket sslserversocket =
                (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
        SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();

        InputStream inputstream = sslsocket.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        String string = null;
        while ((string = bufferedreader.readLine()) != null) {
            System.out.println(string);
            System.out.flush();
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }

}

Client:

    public static void main(String[] arstring) {
    try {
        // Pfad zum Truststore
        String certificateChain = "/usr/lib64/jvm/java-1.7.0-openjdk-1.7.0/jre/lib/security/cacerts";
        String password = "***";
        System.setProperty("javax.net.ssl.trustStore", certificateChain);
        System.setProperty("javax.net.ssl.trustStorePassword", password);

        SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);

        OutputStream outputstream = sslsocket.getOutputStream();
        OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
        BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

        String string = "testmessage";
            bufferedwriter.write(string + '\n');
            bufferedwriter.flush();

    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

After that I tried adding my certificate to the Truststore at the specific path. The generated key was added to the keystore in the working directory.

If tried to follow all the tutorials and instructions on the first 5 pages of google using many different keywords, without luck. I'm always getting the handshake_failure Exception:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:702)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:122)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:254)
at client.SSLClientMain.main(SSLClientMain.java:151)

Since the code is just copy-pasted I think it's not a codeproblem, but a certificate problem. So my question is: what am I doing wrong, what do I need to do more?

Poehli
  • 307
  • 4
  • 16
  • The server you are connecting to, have you added it's public key to your truststore your client is using? – user3465651 Jul 01 '14 at 11:09
  • I added the .csr file which contains - as far as I know - the certificate und pubkey, am I wrong? – Poehli Jul 01 '14 at 11:49
  • When you say added, does that mean you have imported it into the truststore on the client? – user3465651 Jul 01 '14 at 11:57
  • yes, exactly. I used `keytool -import` command to do so. And I tried using Portecle as well, but that didn't change anything. – Poehli Jul 01 '14 at 12:15
  • This **sounds** similar to this posting: http://stackoverflow.com/questions/11113338/javax-net-ssl-sslhandshakeexception-received-fatal-alert-handshake-failure-err – user3465651 Jul 01 '14 at 12:20
  • CSRs are certificate requests, not certificates (nor private keys). – Bruno Jul 01 '14 at 14:13
  • Many thanks! With that I found out, that it wasn't imported correctly! I followed this example: http://www.agentbob.info/agentbob/79-AB.html and it worked perfectly – Poehli Jul 01 '14 at 14:34

1 Answers1

0

You may be using a certificate in Server side which may not be in Client side and vice versa. Try it out with same certificate for server and client.

Rohan
  • 1
  • 3