Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encode() with private key in "AndroidKeyStore" return null

With Android 4.3, this code return null.

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
keyStore.setKeyEntry(alias, privateKey, null, certificateChain);
PrivateKeyEntry entry=(PrivateKeyEntry)keyStore.getEntry(alias, new PasswordProtection(password));
assert(entry.getPrivateKey().getEncoded()!=null);

How it's possible to get the encoded version of private key ?

Or, is it possible to transmit the private key handler to another application ?

Thank's

like image 664
pprados Avatar asked Oct 01 '22 05:10

pprados


1 Answers

The Android KeyChain API prevents you from being able to get an encoded private key.

See the method at line 158 of OpenSSLRSAPrivateKey.java

@Override
public final BigInteger getPrivateExponent() {
    if (key.isEngineBased()) {
        throw new UnsupportedOperationException("private exponent cannot be extracted");
    }

But the benefit of using the KeyChain API is that it provides system-wide credential storage. Any app should be able to retrieve the key pair and certificate by its alias. Refer to the KeyStore docs.

like image 53
Jason Callaway Avatar answered Oct 05 '22 12:10

Jason Callaway