title: Using client/server certificates for two way authentication SSL socket on Android
body: <p>I'm working on an Android app that requires both client and server certificate authentication. I have an SSLClient class that I created that works beautifully on regular desktop Java SE 6. I've moved it into my Android project and I'm getting the following error: "KeyStore JKS implementation not found".</p>
  
  <p>I've looked online a bit and it looks like there's a possibility that Java Keystores are not supported on Android (awesome!) but I have a feeling there's more to it than that because none of the sample code I've found resembles what I'm trying to do at all. Everything I found talks about using an http client rather than raw SSL sockets. I need SSL sockets for this application.</p>
  
  <p>Below is the code in my SSLClient.java file. It reads the keystore and truststore, creates an SSL socket connection to the server, then runs a loop while waiting for input lines from the server then handles them as they come in by calling a method in a different class. I'm very interested to hear from anyone with any experience doing SSL sockets on the Android platform.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.security.AccessControlException;
  import java.security.KeyManagementException;
  import java.security.KeyStore;
  import java.security.KeyStoreException;
  import java.security.NoSuchAlgorithmException;
  import java.security.UnrecoverableKeyException;
  import java.security.cert.CertificateException;
  
  import javax.net.ssl.KeyManagerFactory;
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSocket;
  import javax.net.ssl.SSLSocketFactory;
  import javax.net.ssl.TrustManagerFactory;
  import otherpackege.OtherClass;
  
  import android.content.Context;
  import android.util.Log;
  
  public class SSLClient 
  {
      static SSLContext ssl_ctx;
  
      public SSLClient(Context context)
      {
          try
          {
              // Setup truststore
              KeyStore trustStore = KeyStore.getInstance("BKS");
              TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              InputStream trustStoreStream = context.getResources().openRawResource(R.raw.mysrvtruststore);
              trustStore.load(trustStoreStream, "testtest".toCharArray());
              trustManagerFactory.init(trustStore);
  
              // Setup keystore
              KeyStore keyStore = KeyStore.getInstance("BKS");
              KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
              InputStream keyStoreStream = context.getResources().openRawResource(R.raw.clientkeystore);
  keyStore.load(keyStoreStream, "testtest".toCharArray());
              keyManagerFactory.init(keyStore, "testtest".toCharArray());
  
              Log.d("SSL", "Key " + keyStore.size());
              Log.d("SSL", "Trust " + trustStore.size());
  
              // Setup the SSL context to use the truststore and keystore
              ssl_ctx = SSLContext.getInstance("TLS");
              ssl_ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
  
              Log.d("SSL", "keyManagerFactory " + keyManagerFactory.getKeyManagers().length);
              Log.d("SSL", "trustManagerFactory " + trustManagerFactory.getTrustManagers().length);
          }
          catch (NoSuchAlgorithmException nsae)
          {
              Log.d("SSL", nsae.getMessage());
          }
          catch (KeyStoreException kse)
          {
              Log.d("SSL", kse.getMessage());
          }
          catch (IOException ioe)
          {
              Log.d("SSL", ioe.getMessage());
          }
          catch (CertificateException ce)
          {
              Log.d("SSL", ce.getMessage());
          }
          catch (KeyManagementException kme)
          {
              Log.d("SSL", kme.getMessage());
          }
          catch(AccessControlException ace)
          {
              Log.d("SSL", ace.getMessage());
          }
          catch(UnrecoverableKeyException uke)
          {
              Log.d("SSL", uke.getMessage());
          }
  
          try
          {
              Handler handler = new Handler();
              handler.start();
          }
          catch (IOException ioException) 
          {
              ioException.printStackTrace();
          }
       }  
  }
  
  //class Handler implements Runnable 
  class Handler extends Thread
  {
      private SSLSocket socket;
      private BufferedReader input;
      static public PrintWriter output;
  
      private String serverUrl = "174.61.103.206";
      private String serverPort = "6000";
  
      Handler(SSLSocket socket) throws IOException
      {
  
      }
      Handler() throws IOException
      {
  
      }
  
      public void sendMessagameInfoge(String message)
      {
          Handler.output.println(message);
      }
  
      @Override
      public void run() 
      {
          String line;
  
          try 
          {
              SSLSocketFactory socketFactory = (SSLSocketFactory) SSLClient.ssl_ctx.getSocketFactory();
              socket = (SSLSocket) socketFactory.createSocket(serverUrl, Integer.parseInt(serverPort));
              this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              Handler.output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
              Log.d("SSL", "Created the socket, input, and output!!");
  
              do
              {
                  line = input.readLine();
                  while (line == null)
                  {
                      line = input.readLine();
                  }
  
                  // Parse the message and do something with it
                  // Done in a different class
                  OtherClass.parseMessageString(line);
              }
              while ( !line.equals("exit|") );
          }
          catch (IOException ioe)
          {
              System.out.println(ioe);
          }
          finally 
          {
              try 
              {
                  input.close();
                  output.close();
                  socket.close();
              } 
              catch(IOException ioe) 
              {
              } 
              finally 
              {
  
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Update:</strong><br>
  Making some good progress on this problem. Found out that JKS is indeed not supported, neither is directly choosing the SunX509 type. I've updated my code above to reflect these changes. I'm still having an issue with it apparently not loading the keystore and truststore. I'll update as I figure out more.</p>
  
  <hr>
  
  <p><strong>Update2:</strong><br>
  I was doing my keystore and truststore file loading in a desktop Java way rather than the correct Android way. The files must be put in the res/raw folder and loaded using getResources(). I'm now getting a count of 1 and 1 for the keystore and truststore size which means they're loading. I'm still crashing on an exception, but getting closer! I'll update when I get this working.</p>
  
  <hr>
  
  <p><strong>Update3:</strong><br>
  Looks like everything is working now with the exception of my keystore being set up incorrectly. If I disable client side authentication on the server, it connects without issue. When I leave it enabled, I get a <code>handling exception: javax.net.ssl.SSLHandshakeException: null cert chain</code> error. So it looks like I'm not setting up the certificate chain correctly. I've posted another question asking how to create a client keystore in the BKS format with the proper certificate chain: <a href="https://stackoverflow.com/questions/4065379/how-to-create-a-bks-bouncycastle-format-java-keystore-that-contains-a-client-ce">How to create a BKS (BouncyCastle) format Java Keystore that contains a client certificate chain</a></p>