Ludo Ludo - 3 months ago 18
Android Question

How to prevent choosePrivateKeyAlias dialog in android app?

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 !

Answer Source

I not sure about the good way to resolve this problem, but here is what I did that worked fine for me.

I checked a boolean variable in the preferences, and if false returned, I display the choosPrivateKeyAlias window. If true returned, I know that I have the permission to retrieve certificate directly, so no need to display popup.

boolean isGranted = prefs.getBoolean("MY_CERT", false);
if(!isGranted) {
        //Get cert and private key from internal android store
        KeyChainAliasCallback keyChainAliasCallback = new KeyChainAliasCallback() {
            @Override
            public void alias(@Nullable String s) {
                Log.d(TAG, "selected alias = " + s);
                SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                editor.putBoolean("MY_CERT", true);
                editor.commit();
                retriveCertsTask.execute();
            }
        };
        KeyChain.choosePrivateKeyAlias(mActivity, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS);
    } else {
        // Retrieve certs an private key
        retriveCertsTask.execute();
    }

Hope it helps...