Java TLS communication between Server-Client

Viewed 73

I am trying to make a Client-Server application which implements TLS for a secure communication. I have this code for the Server:

 private static SSLContext sslContext;
    private static final String fullPathToTrustStore = ".\\lib\\truststore";
    private static final String fullPathToKeyStore = ".\\lib\\keystore";
    private final String serverIP = "127.0.0.1";
    private static final String password = "password";
    private static KeyManagerFactory kmf;
    private static TrustManagerFactory tmf;


boolean status;
        status = CreateKeyTrustManagerFactory();
        if (status) {
            CreateSSLContext();
        }

        try {
            SSLServerSocketFactory ssf = sslContext.getServerSocketFactory();
            SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket(5000);

            while (true) {
                Socket s = null;
                try {
                    // socket object to receive incoming client requests
                    s = serverSocket.accept();
                    System.out.println("A new client is connected : " + s);
                    // obtaining input and out streams
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    DataOutputStream dos = new DataOutputStream(s.getOutputStream());

                    System.out.println("Assigning new thread for this client");

                    Thread t = new ClientHandler(s, dis, dos);

                    t.start();


                } catch (Exception e) {
                    s.close();
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private static boolean CreateKeyTrustManagerFactory() throws CertificateException, IOException, NoSuchAlgorithmException {
        boolean retval = true;
        char[] passPhrase = password.toCharArray();
        try {

            KeyStore ksKeys = KeyStore.getInstance("PKCS12");
            ksKeys.load(new FileInputStream(fullPathToKeyStore), passPhrase);
            KeyStore ksTrust = KeyStore.getInstance("PKCS12");
            ksTrust.load(new FileInputStream(fullPathToTrustStore), passPhrase);

            kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ksKeys, passPhrase);
            tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(ksKeys);

        } catch (KeyStoreException | FileNotFoundException | UnrecoverableKeyException e) {
            retval = false;
        }
        System.out.println("CreateKeyTrustManagerFactory returned: " + retval);
        return retval;
    }

private static boolean CreateSSLContext() {
    boolean retval = true;
    try {
        sslContext = SSLContext.getInstance("TLSv1.3");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    } catch (Exception ex) {
        retval = false;
    }
    System.out.println("CreateSSLContext returned: " + retval);
    return retval;
}

And some very similar code for the Client Side. For the truststore and keystore I used keytool from jdk. But my problem is that on Wireshark when i look at loopback traffic capture, i only see TCP traffic, but never TLS. What could be the problem?

Edit: The communication works, but it appear as being simple TCP traffic

Edit 2: Here is the code for Client:

private Client() throws CertificateException, IOException, NoSuchAlgorithmException {
        this.connection=true;

        boolean status;
        status = CreateKeyTrustManagerFactory();
        if (status) {
            CreateSSLContext();
        }

        SSLSocketFactory ssf = sslContext.getSocketFactory();

        try {
            SSLSocket sslClientSocket = (SSLSocket) ssf.createSocket();
            InetSocketAddress connectAddress = new InetSocketAddress(serverIP, serverPort);
            sslClientSocket.connect(connectAddress);
            sslClientSocket.startHandshake();
            SSLSession session = sslClientSocket.getSession();
            System.out.println("Session: " + session);
            InStream = new DataInputStream(sslClientSocket.getInputStream());
            OutStream = new DataOutputStream(sslClientSocket.getOutputStream());
        } catch (IOException e) {
            this.connection=false;
          //  e.printStackTrace();
        }
    }

The methods CreateKeyTrustManagerFactory and CreateSSLContext are the same from the server.

0 Answers
Related