16

I am generating an Asymmetric key pair in the Android key store as below: I have used the public key for symmetric key wrapping and storing the wrapped key to a file. When I try to unwrap symmetric key using the private key, I am able to do so within that instance. Once my application is re-installed, I am unable to get the key store entry with the alias.

KeyPairGenerator kpg = KeyPairGenerator.getInstance(
    KeyProperties.KEY_ALGORITHM_RSA,
    "AndroidKeyStore"
);

kpg.initialize(new KeyGenParameterSpec.Builder(
        Constants.KEY_STORE_ALIAS_NAME,
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
    )
    .setKeySize(Constants.ASYMMETRIC_KEY_LENGTH)
    .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
    .build()
);

keyPair =  kpg.generateKeyPair();

// Code for accessing the key store entry to un wrap the symmetric key
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
KeyStore.Entry entry = ks.getEntry(Constants.KEY_STORE_ALIAS_NAME, null);
PrivateKey privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Hithendra Nath
  • 173
  • 2
  • 10

1 Answers1

8

Keys stored in Android Keystore are non-extractable. It is a security measure

Security Features

Android Keystore system protects key material from unauthorized use. Firstly, Android Keystore mitigates unauthorized use of key material outside of the Android device by preventing extraction of the key material from application processes and from the Android device as a whole. Secondly, Android KeyStore mitigates unauthorized use of key material on the Android device by making apps specify authorized uses of their keys and then enforcing these restrictions outside of the apps' processes

This means that the keys can not be part of the Android backup service in any way. It allows to store application data on the cloud once the application is uninstalled. See HowBackupWorks.

It would be a serious security risk that private keys could be extracted and stored in cloud or even that they remain stored in the device when the application has been uninstalled

If you need to use an encryption key that does not depend on the reinstallation, you could generate a symmetric key from a user passphrase using a key derivation algorithm

Community
  • 1
  • 1
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • Thank you ... Even with passphrase based encryption, we have to maintain passphrase uniquely. Otherwise,the files that were encrypted can not be decrypted at all once the passphrase changed by user. I thought of generating asymmetric key pair once and use it for decrypting the wrap/unwrap of symmetric key. My goal is application should be able to decrypt all the files that were encrypted before un installation aswell. – Hithendra Nath Jan 18 '18 at 17:10
  • The passphrase is requested to user, so the user must remember it.The alternative is to make a backup copy of the encryption key on the server side associated with the user account in some way, and download it after the reinstallation but this solution has security risks. Try that user keys never leave the device – pedrofb Jan 19 '18 at 08:33
  • 1
    Ok .. In general how a private is maintained then? Where is it stored? Because, I can generate the key pair outside the android app so that, I can use the same RSA key pair. In this case how to keep the private key securely? Ideally, key pair should be generated once and used. So, generating it from app will not be the correct way. Keys should be generated outside the app and supplied to app. Is this understanding correct? – Hithendra Nath Jan 19 '18 at 10:20
  • 1
    A use case (there may be others): **The keypair is generated by the android app** using the standard KeyPairGenerator (not AndroidKeyStore) and stored on disk protected with a passphrase. You can use a pkcs#12 file (.p12) or encrypt the private into a PEM file (see [this](https://stackoverflow.com/a/6164414/6371459)). On reinstallation, prompt user for the password and decrypt the key. Then you can import it to AndroidKeyStore or decrypt the key every time the app is started – pedrofb Jan 19 '18 at 11:01
  • Ok, consider accepting the answer by clicking the check-mark or upvote when you have enough reputation. – pedrofb Jan 21 '18 at 19:03
  • I am actually not clear on the way private key has been encrypted. I still need to try how to decrypt the same. But, I was actually looking for this. Thanks. – Hithendra Nath Jan 21 '18 at 23:21