I'm developing an hybrid cordova app which might connect to different servers. Some of them do require a client certificate.
On an Android mobile the corresponding root cert + client certificate is installed.
On Chrome browser I get the following dialog to choose the corresponding client certificate for the Web connection.
With the cordova plugin cordova-client-cert-authentication the same dialog pops up for Http(s) requests within the WebView.
My question is how to achieve a automatic certificate selection on Http(s) requests on the native Android platform without explicitly declaring the corresponding client certificate. Or is there something similiar to the user selection of certificate like implemented on Chrome?
This is the current implementation, which throws a handshake exception:
try {
URL url = new URL( versionUrl );
HttpsURLConnection urlConnection = ( HttpsURLConnection ) url.openConnection();
urlConnection.setConnectTimeout( 10000 );
InputStream in = urlConnection.getInputStream();
}
catch(Exception e)
{
//javax.net.ssl.SSLHandshakeException: Handshake failed
}
You can use a certificate previously installed in Android KeyChain (the system key store) extending
X509ExtendedKeyManager
to configure theSSLContext
used byURLConnection
The certificate is referenced by an alias that you need. To prompt user for selection with a dialog similar to chrome use:
This is the code to configure the SSL connection using a custom
KeyManager
. It uses the defaultTrustManager
andHostnameVerifier
. You will need to configure them if the server is using a self signed certificate not present in Android default truststore (trusting all certificates is not recommended)Finally here you have and a full implementation of the custom
X509ExtendedKeyManager
extracted from here and here that is in charge of selecting the client certificate. I have extracted the required code.I did not test it. Report any error!
If your URLs are still in development stage (not production version), you can skip those SSL/NON-SSL certificates installing to access the URLs.
Here is how to skip SSL validation : Call when activity onCreate() or where your need before accessing URL.
Note : If your HTTPS URLs are valid, you will no require to use server-generated certificates. You should using this method for testing/development only. For release/production you don't have to use this method.