How to prevent choosePrivateKeyAlias dialog in android app?

Viewed 2247

I have an android app that call a secured website in a webview. The webview retrieve the certificate to give it to the website.

I have to use the KeyChain.choosePrivateKeyAlias(this, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS); method, and this displays a dialog like this

certificate select

I'd want to display this window only the first time the user uses the app, but I don't know if it's possible.

I saw about intercepting this with a device/owner profile. Does that mean that it should be on android work ? it's a little blurry to me.

Another solution would be to save certificate and private key retrieve the first time somewhere inaccessible by any other app nor the user. I think about SharedPreferences in private mode.

Am I wrong ?

Thanks for your anwers !

2 Answers

I use the following in order to check whether or not the user has already selected the certificate:

activity?.let { it1 ->
    var selectedPrivateKey = KeyChain.getPrivateKey(it1, "keystore-test")
    if (selectedPrivateKey == null) { // if we can't access the private key then prompt for cert selection
        KeyChain.choosePrivateKeyAlias(it1, {
            Log.d("App", "Popped up KeyChain selection screen")
        }, null, null, null, "keystore-test") // this is the alias that is chosen in the popup by default
    }
}

KeyChain.getPrivateKey(it1, "keystore-test") will return null if you don't have access to the private key, meaning that the user did not select a certificate yet.

This implementation will save you from having to use SharedPreferences and also won't give you a false positive if the user deletes the certificate.

Related