I'm trying to create an Android app that uses 2-way SSL to communicate with a NodeJS application. I have 2 versions of the code to make the request, and neither version is working. This first version of code works if I run it with plain Java, but when I try to pull this into my Android app, the server is not recognizing the client certificate:
Version 1:
System.setProperty("javax.net.ssl.keyStore", "jks-keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "pass1");
System.setProperty("javax.net.ssl.trustStore", "jkstruststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "pass2");
// specify url
String url = "https://example.com/startup";
System.out.println("Startup URL is " + url);
// This block of code keeps self-signed certificates from causing errors.
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
}
);
// initiate the request
try
{
URL hp = new URL(url);
HttpsURLConnection hpCon = (HttpsURLConnection)hp.openConnection();
boolean isProxy = hpCon.usingProxy();
InputStream obj = (InputStream) hpCon.getInputStream();
// print out JSON response
System.out.println(convertStreamToString(obj));
}
catch (Exception ex)
{
ex.printStackTrace();
}
Error 1:
03-17 12:25:18.616: W/System.err(331): javax.net.ssl.SSLHandshakeException:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Version 2:
// load truststore certificate
InputStream clientTruststoreIs = getResources().openRawResource(R.raw.tsserver);
KeyStore trustStore = null;
trustStore = KeyStore.getInstance("BKS");
trustStore.load(clientTruststoreIs, "pass1".toCharArray());
System.out.println("Loaded server certificates: " + trustStore.size());
// initialize trust manager factory with the read truststore
TrustManagerFactory trustManagerFactory = null;
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
// setup client certificate
// load client certificate
InputStream keyStoreStream = getResources().openRawResource(R.raw.tsclient);
KeyStore keyStore = null;
keyStore = KeyStore.getInstance("BKS");
keyStore.load(keyStoreStream, "pass2".toCharArray());
System.out.println("Loaded client certificates: " + keyStore.size());
// initialize key manager factory with the read client certificate
KeyManagerFactory keyManagerFactory = null;
keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "pass2".toCharArray());
// initialize SSLSocketFactory to use the certificates
SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, "pass2", trustStore, null, null);
// Set basic data
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Android app/1.0.0");
// Make pool
ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnManagerParams.setMaxTotalConnections(params, 20);
// Set timeout
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// Some client params
HttpClientParams.setRedirecting(params, false);
// Register http/s schemas!
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", socketFactory, 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
DefaultHttpClient sClient = new DefaultHttpClient(conMgr, params);
try {
String res = executeHttpGet(sClient, "https://example.com/startup");
System.out.println("------- SSL RESULT IS = " + res);
} catch (Exception e) {
System.out.println("---- ex " + e.getMessage());
e.printStackTrace();
}
Error 2: Server does not see client certificate
Both of these code samples result in the server not seeing the client certificate. Any ideas why this is not working? Thank you.