page.title=Android Keystore System @jd:body

In this document

  1. Security Features
  2. Choosing Between a Keychain or the Android Keystore Provider
  3. Using Android Keystore Provider
    1. Generating a New Private Key
    2. Working with Keystore Entries
    3. Listing Entries
    4. Signing and Verifying Data

Blog articles

  1. Unifying Key Store Access in ICS

The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device. Once keys are in the keystore, they can be used for cryptographic operations with the key material remaining non-exportable. Moreover, it offers facilities to restrict when and how keys can be used, such as requiring user authentication for key use or restricting encryption keys to be used only in certain block modes. See Security Features section for more information.

The Keystore system is used by the {@link android.security.KeyChain} API as well as the Android Keystore provider feature that was introduced in Android 4.3 (API level 18). This document goes over when and how to use the Android Keystore provider.

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.

Extraction Prevention

Key material of Android Keystore keys is protected from extraction using two security measures:

Key Use Authorizations

To mitigate unauthorized use of keys on the Android device, Android Keystore lets apps specify authorized uses of their keys when generating or importing the keys. Once a key is generated or imported, its authorizations can not be changed. Authorizations are then enforced by the Android Keystore whenever the key is used.

Supported key use authorizations fall into the following categories:

As an additional security measure, for keys whose key material is inside secure hardware (see {@link android.security.keystore.KeyInfo#isInsideSecureHardware() KeyInfo.isInsideSecurityHardware()}) some key use authorizations may be enforced by secure hardware, depending on the Android device. Cryptographic and user authentication authorizations are likely to be enforced by secure hardware. Temporal validity interval authorizations are unlikely to be enforced by the secure hardware because it normally does not have an independent secure real-time clock.

Whether a key's user authentication authorization is enforced by the secure hardware can be queried using {@link android.security.keystore.KeyInfo#isUserAuthenticationRequirementEnforcedBySecureHardware() KeyInfo.isUserAuthenticationRequirementEnforcedBySecureHardware()}.

Choosing Between a Keychain or the Android Keystore Provider

Use the {@link android.security.KeyChain} API when you want system-wide credentials. When an app requests the use of any credential through the {@link android.security.KeyChain} API, users get to choose, through a system-provided UI, which of the installed credentials an app can access. This allows several apps to use the same set of credentials with user consent.

Use the Android Keystore provider to let an individual app store its own credentials that only the app itself can access. This provides a way for apps to manage credentials that are usable only by itself while providing the same security benefits that the {@link android.security.KeyChain} API provides for system-wide credentials. This method requires no user interaction to select the credentials.

Using Android Keystore Provider

To use this feature, you use the standard {@link java.security.KeyStore} and {@link java.security.KeyPairGenerator} or {@link javax.crypto.KeyGenerator} classes along with the {@code AndroidKeyStore} provider introduced in Android 4.3 (API level 18).

{@code AndroidKeyStore} is registered as a {@link java.security.KeyStore} type for use with the {@link java.security.KeyStore#getInstance(String) KeyStore.getInstance(type)} method and as a provider for use with the {@link java.security.KeyPairGenerator#getInstance(String, String) KeyPairGenerator.getInstance(algorithm, provider)} and {@link javax.crypto.KeyGenerator#getInstance(String, String) KeyGenerator.getInstance(algorithm, provider)} methods.

Generating a New Private Key

Generating a new {@link java.security.PrivateKey} requires that you also specify the initial X.509 attributes that the self-signed certificate will have. You can replace the certificate at a later time with a certificate signed by a Certificate Authority.

To generate the key, use a {@link java.security.KeyPairGenerator} with {@link android.security.KeyPairGeneratorSpec}:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java generate}

Generating a New Secret Key

To generate the key, use a {@link javax.crypto.KeyGenerator} with {@link android.security.keystore.KeyGenParameterSpec}.

Working with Keystore Entries

Using the {@code AndroidKeyStore} provider takes place through all the standard {@link java.security.KeyStore} APIs.

Listing Entries

List entries in the keystore by calling the {@link java.security.KeyStore#aliases()} method:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java list}

Signing and Verifying Data

Sign data by fetching the {@link java.security.KeyStore.Entry} from the keystore and using the {@link java.security.Signature} APIs, such as {@link java.security.Signature#sign()}:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java sign}

Similarly, verify data with the {@link java.security.Signature#verify(byte[])} method:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java verify}

Requiring User Authentication For Key Use

When generating or importing a key into the {@code AndroidKeyStore} you can specify that the key can only be used if user has been authenticated. The user is authenticated using a subset of their secure lock screen credentials. This is a security measure which makes it possible to generate cryptographic assertions about the user having been authenticated.

When a key is configured to require user authentication, it is also configured to operate in one of the two modes: