I am trying to get two-way SSL authentication working between a Python server and an Android client application. I have access to the server and client, and I would like to implement client authentication using my own certificate. So far, I have been able to verify the server certificate and connect without client authentication.
What certificate does the client need and how to get it so that it automatically sends it to the server during the handshake process? Here is the client and server code that I still have. Is my approach wrong?
Server code
while True: # Keep listening for clients c, fromaddr = sock.accept() ssl_sock = ssl.wrap_socket(c, keyfile = "serverPrivateKey.pem", certfile = "servercert.pem", server_side = True, # Require the client to provide a certificate cert_reqs = ssl.CERT_REQUIRED, ssl_version = ssl.PROTOCOL_TLSv1, ca_certs = "clientcert.pem", #TODO must point to a file of CA certificates?? do_handshake_on_connect = True, ciphers="!NULL:!EXPORT:AES256-SHA") print ssl_sock.cipher() thrd = sock_thread(ssl_sock) thrd.daemon = True thrd.start()
I suspect that I can use the wrong file for ca_certs ...?
Client code
private boolean connect() { try { KeyStore keystore = KeyStore.getInstance("BKS");
I created a client private key, client certificate, server private key and server certificate using openssl. Then I added the client certificate to keystore.bks (which I stored in /res/raw/keystore.bks ). Then I added the server certificate to truststore.bks
So, now that the client is trying to connect, I get this side of the error server:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140890C7:SSL routines:SSL3_GET_CLIENT_CERTIFICATE:peer did not return a certificate
And when I try to do this in android client
SSLSession s = mSSLSocket.getSession(); s.getPeerCertificates();
I get this error:
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Thus, it is obvious that the keystore that I use does not seem to have the correct peer certificate in it, and therefore does not send it to the server.
What should be stored in the keystore to prevent this exception?
Also, is this two-way SSL authentication method safe and effective?