Reputation: 125
I need to do Public key /Certificate pinning for Webview. I see there is an api has been introduced in API21 As per Android documentation, http://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedClientCertRequest(android.webkit.WebView, android.webkit.ClientCertRequest)
onReceivedClientCertRequest()
is added in api 21, but I am not getting callback when I load any url. Could anyone please help????
@Override
public void onReceivedClientCertRequest(WebView view, final ClientCertRequest request) {
Log.e("ClientCertRequest", "===> certificate required!");
KeyChain.choosePrivateKeyAlias(WebViewActivity.this, new KeyChainAliasCallback(){
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void alias(String alias) {
Log.e(getClass().getSimpleName(), "===>Key alias is: " + alias);
try {
PrivateKey changPrivateKey = KeyChain.getPrivateKey(WebViewActivity.this, alias);
X509Certificate[] certificates = KeyChain.getCertificateChain(WebViewActivity.this, alias);
Log.v(getClass().getSimpleName(), "===>Getting Private Key Success!" );
request.proceed(changPrivateKey, certificates);
} catch (KeyChainException e) {
Log.e(getClass().getSimpleName(), Util.printException(e));
} catch (InterruptedException e) {
Log.e(getClass().getSimpleName(), Util.printException(e));
}
}
},new String[]{"RSA"}, null, null, -1, null);
super.onReceivedClientCertRequest(view,request);
}
Upvotes: 1
Views: 1572
Reputation: 46
Client Certificate Authentication can fail in a number of ways in Android:
WebViewClient.onPageStarted()
handler.proceed()
in WebViewClient.onReceivedSslError(view, handler, error)
SSLVerifyClient require
along with the required parameters SSLVerifyDepth
and SSLCACertificateFile
in the configUpvotes: 3