4

I my android application am using SSL for client authentication and I am using BKS format keystore and MyHttpClient class looks as below,

    public class MyHttpClient extends DefaultHttpClient {

    final Context context;

    public MyHttpClient(Context context) {
        this.context = context;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // Register for port 443 our SSLSocketFactory with our keystore
        // to the ConnectionManager
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
    }

    private SSLSocketFactory newSslSocketFactory() { 
        try {
            // Get an instance of the Bouncy Castle KeyStore format
            KeyStore trusted = KeyStore.getInstance("BKS");
            // Get the raw resource, which contains the keystore with
            // your trusted certificates (root and any intermediate certs)
            InputStream in = context.getResources().openRawResource(R.raw.mykeys);
            try {
                // Initialize the keystore with the provided trusted certificates
                // Also provide the password of the keystore
                trusted.load(in, "mypassword".toCharArray());
            } finally {
                in.close();
            }
            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

When I run the app, I don't get any run time error on eclipse but am not getting any value from the server. But the same code works for http authentication.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • if i intentionally change he password, am getting the following error 12-04 16:48:43.778: E/AndroidRuntime(5038): FATAL EXCEPTION: main12-04 16:48:43.778: E/AndroidRuntime(5038): java.lang.AssertionError: java.io.IOException: KeyStore integrity check failed. But when password is correct, it wil not show any error and am not getting any value from the server –  Dec 05 '13 at 07:12
  • I'm having the same problem, please share your solution if you found one. I've also asked a similar question here: http://stackoverflow.com/questions/26369837/ssl-pinning-with-loopj-in-android – Lucas Jota Oct 15 '14 at 12:47

0 Answers0