How do I create a secure SSL connection between the Android client and the Java server?
I’m trying to create a secure TCP/IP connection between my android client and the java server. So far, cryptography has not been my strong point. I knew I had to use the Bouncy CaSTLe keystore, but I got stuck.
I need to create a self-signed certificate that can be used by both the server and the client. That’s where I’m trapped.
InputStream clientTruststoreIs = getResources().openRawResource(R.raw.bks); KeyStore trustStore = null; trustStore = KeyStore.getInstance("BKS"); trustStore.load(clientTruststoreIs, "123456".toCharArray()); InetAddress serverAddr = InetAddress.getByName(serverIpAddress); SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); sslSocket = (SSLSocket) sslsocketfactory.createSocket(serverAddr, 2222); inputStream = sslSocket.getInputStream(); Log.d("TEST", "\n1 input");
It connects fine, it shows the server-side connection, but
the inputStream = sslSocket.getInputStream() line is completely locked/not returned. I’m not sure if I’m setting up any bouncy castles correctly, or if the server is set up correctly. Please help, there’s nothing I can do ….
I modified the server so that it sends data when connected but still doesn’t send data, I change the order in which I get input and output streams, and then it locks where I get output streams:
sslSocket = (SSLSocket) sslsocketfactory.createSocket(serverAddr, 9999); outputStream = sslSocket.getOutputStream(); Log.d("test", "--**--- created socket 2.0 outputsreams"); does not reach here outputStreamWriter = new OutputStreamWriter(outputStream); bufferedWriter = new BufferedWriter(outputStreamWriter); inputStream = sslSocket.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); ... String line = bufferedReader.readLine(); where I'd expect it to lock...
So still no luck, no timeout, no exception, it’s just waiting there….
I think your question is similar to The same workflow applies to SSL socket connections:
However, the basics are much the same as they are in this program:
Open a socket.
Open an input stream and output stream to the socket.
Read from and write to the stream according to the server’s protocol, call flush() on the stream when you are done with a message.
Close the streams.
Close the socket.
So you should
- A request is first sent to your server through OutputStream that writes from the client to the server, and
then refreshedwhen finished.
- The server will then receive the message through its InputStream and can write to its OutputStream as a return, refreshing again when finished.
- Use InputStream to read the response from the client, starting at 1 and ending with completion.
Finally, you should close all streams.
Refreshing and sending a request before the client attempts to read the response should stop the “hang behavior.”
EDIT: Are you sure you are using
SSLServerSocketFactory on your server? It looks like you’re trying to use
SSLSocketFactory for both the client and the server. That won’t work. I googled this tutorial, which basically resembles what your code should look like.