KeyChain
class KeyChain
| kotlin.Any | |
| ↳ | android.security.KeyChain |
The KeyChain class provides access to private keys and their corresponding certificate chains in credential storage.
Applications accessing the KeyChain normally go through these steps:
- Receive a callback from an
X509KeyManagerthat a private key is requested. - Call
choosePrivateKeyAliasto allow the user to select from a list of currently available private keys and corresponding certificate chains. The chosen alias will be returned by the callbackKeyChainAliasCallback#alias, or null if no private key is available or the user cancels the request. - Call
getPrivateKeyandgetCertificateChainto retrieve the credentials to return to the corresponding callbacks.
An application may remember the value of a selected alias to avoid prompting the user with choosePrivateKeyAlias on subsequent connections. If the alias is no longer valid, null will be returned on lookups using that value
An application can request the installation of private keys and certificates via the Intent provided by . Private keys installed via this Intent will be accessible via choosePrivateKeyAlias while Certificate Authority (CA) certificates will be trusted by all applications through the default X509TrustManager.
Summary
Constants |
|
|---|---|
| static String |
Broadcast Action: Indicates the contents of the keychain has changed. |
| static String |
Broadcast Action: Indicates that the access permissions for a private key have changed. |
| static String |
Broadcast Action: Indicates the trusted storage has changed. |
| static String |
Broadcast Action: Indicates the contents of the trusted certificate store has changed. |
| static String |
Optional extra to specify an X. |
| static String |
Used as a boolean extra field in |
| static String |
Used as a String extra field in |
| static String |
Optional extra to specify a |
| static String |
Optional extra for use with the |
| static String |
Used by DPC or delegated app in |
Public constructors |
|
|---|---|
<init>() |
|
Public methods |
|
|---|---|
| static Unit |
choosePrivateKeyAlias(activity: Activity, response: KeyChainAliasCallback, keyTypes: Array<String!>?, issuers: Array<Principal!>?, host: String?, port: Int, alias: String?)Launches an |
| static Unit |
choosePrivateKeyAlias(activity: Activity, response: KeyChainAliasCallback, keyTypes: Array<String!>?, issuers: Array<Principal!>?, uri: Uri?, alias: String?)Launches an |
| static Intent |
Returns an |
| static Array<X509Certificate!>? |
getCertificateChain(context: Context, alias: String)Returns the |
| static PrivateKey? |
getPrivateKey(context: Context, alias: String)Returns the |
| static Boolean |
isBoundKeyAlgorithm(algorithm: String)Returns |
| static Boolean |
isKeyAlgorithmSupported(algorithm: String)Returns |
Constants
ACTION_KEYCHAIN_CHANGED
static val ACTION_KEYCHAIN_CHANGED: String
Broadcast Action: Indicates the contents of the keychain has changed. Sent when a KeyChain entry is added, modified or removed.
Value: "android.security.action.KEYCHAIN_CHANGED"
ACTION_KEY_ACCESS_CHANGED
static val ACTION_KEY_ACCESS_CHANGED: String
Broadcast Action: Indicates that the access permissions for a private key have changed.
Value: "android.security.action.KEY_ACCESS_CHANGED"
ACTION_STORAGE_CHANGED
static valACTION_STORAGE_CHANGED: String
Deprecated: Use ACTION_KEYCHAIN_CHANGED, ACTION_TRUST_STORE_CHANGED or ACTION_KEY_ACCESS_CHANGED. Apps that target a version higher than android.os.Build.VERSION_CODES#N_MR1 will only receive this broadcast if they register for it at runtime.
Broadcast Action: Indicates the trusted storage has changed. Sent when one of this happens:
- a new CA is added,
- an existing CA is removed or disabled,
- a disabled CA is enabled,
- trusted storage is reset (all user certs are cleared),
- when permission to access a private key is changed.
Value: "android.security.STORAGE_CHANGED"
ACTION_TRUST_STORE_CHANGED
static val ACTION_TRUST_STORE_CHANGED: String
Broadcast Action: Indicates the contents of the trusted certificate store has changed. Sent when one the following occurs:
- A pre-installed CA is disabled or re-enabled
- A CA is added or removed from the trust store
Value: "android.security.action.TRUST_STORE_CHANGED"
EXTRA_CERTIFICATE
static val EXTRA_CERTIFICATE: String
Optional extra to specify an X.509 certificate to install on the Intent returned by createInstallIntent. The extra value should be a PEM or ASN.1 DER encoded byte[]. An X509Certificate can be converted to DER encoded bytes with X509Certificate#getEncoded.
EXTRA_NAME may be used to provide a default alias name for the installed certificate.
Value: "CERT"
EXTRA_KEY_ACCESSIBLE
static val EXTRA_KEY_ACCESSIBLE: String
Used as a boolean extra field in ACTION_KEY_ACCESS_CHANGED to supply if the key is accessible to the application.
Value: "android.security.extra.KEY_ACCESSIBLE"
EXTRA_KEY_ALIAS
static val EXTRA_KEY_ALIAS: String
Used as a String extra field in ACTION_KEY_ACCESS_CHANGED to supply the alias of the key.
Value: "android.security.extra.KEY_ALIAS"
EXTRA_NAME
static val EXTRA_NAME: String
Optional extra to specify a String credential name on the Intent returned by createInstallIntent.
Value: "name"
EXTRA_PKCS12
static val EXTRA_PKCS12: String
Optional extra for use with the Intent returned by createInstallIntent to specify a PKCS#12 key store to install. The extra value should be a byte[]. The bytes may come from an external source or be generated with on a "PKCS12" instance.
The user will be prompted for the password to load the key store.
The key store will be scanned for entries and both the private key and associated certificate chain will be installed.
EXTRA_NAME may be used to provide a default alias name for the installed credentials.
Value: "PKCS12"
KEY_ALIAS_SELECTION_DENIED
static val KEY_ALIAS_SELECTION_DENIED: String
Used by DPC or delegated app in android.app.admin.DeviceAdminReceiver#onChoosePrivateKeyAlias or android.app.admin.DelegatedAdminReceiver#onChoosePrivateKeyAlias to identify that the requesting app is not granted access to any key, and nor will the user be able to grant access manually.
Value: "android:alias-selection-denied"
Public constructors
<init>
KeyChain()
Public methods
choosePrivateKeyAlias
static fun choosePrivateKeyAlias(
activity: Activity,
response: KeyChainAliasCallback,
keyTypes: Array<String!>?,
issuers: Array<Principal!>?,
host: String?,
port: Int,
alias: String?
): Unit
Launches an Activity for the user to select the alias for a private key and certificate pair for authentication. The selected alias or null will be returned via the KeyChainAliasCallback callback.
A device policy controller (as a device or profile owner) can intercept the request before the activity is shown, to pick a specific private key alias by implementing onChoosePrivateKeyAlias.
keyTypes and issuers may be used to narrow down suggested choices to the user. If either keyTypes or issuers is specified and non-empty, and there are no matching certificates in the KeyChain, then the certificate selection prompt would be suppressed entirely.
host and port may be used to give the user more context about the server requesting the credentials.
alias allows the caller to preselect an existing alias which will still be subject to user confirmation.
| Parameters | |
|---|---|
activity |
Activity: The Activity context to use for launching the new sub-Activity to prompt the user to select a private key; used only to call startActivity(); must not be null. This value cannot be null. |
response |
KeyChainAliasCallback: Callback to invoke when the request completes; must not be null. This value cannot be null. |
keyTypes |
Array<String!>?: The acceptable types of asymmetric keys such as "RSA", "EC" or null. This value may be null. Value is android.security.keystore.KeyProperties#KEY_ALGORITHM_RSA, android.security.keystore.KeyProperties#KEY_ALGORITHM_EC, android.security.keystore.KeyProperties#KEY_ALGORITHM_AES, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA1, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA224, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA256, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA384, or android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA512 |
issuers |
Array<Principal!>?: The acceptable certificate issuers for the certificate matching the private key, or null. This value may be null. |
host |
String?: The host name of the server requesting the certificate, or null if unavailable. This value may be null. |
port |
Int: The port number of the server requesting the certificate, or -1 if unavailable. |
alias |
String?: The alias to preselect if available, or null if unavailable. This value may be null. |
choosePrivateKeyAlias
static fun choosePrivateKeyAlias(
activity: Activity,
response: KeyChainAliasCallback,
keyTypes: Array<String!>?,
issuers: Array<Principal!>?,
uri: Uri?,
alias: String?
): Unit
Launches an Activity for the user to select the alias for a private key and certificate pair for authentication. The selected alias or null will be returned via the KeyChainAliasCallback callback.
A device policy controller (as a device or profile owner) can intercept the request before the activity is shown, to pick a specific private key alias by implementing onChoosePrivateKeyAlias.
keyTypes and issuers may be used to narrow down suggested choices to the user. If either keyTypes or issuers is specified and non-empty, and there are no matching certificates in the KeyChain, then the certificate selection prompt would be suppressed entirely.
uri may be used to give the user more context about the server requesting the credentials.
alias allows the caller to preselect an existing alias which will still be subject to user confirmation.
| Parameters | |
|---|---|
activity |
Activity: The Activity context to use for launching the new sub-Activity to prompt the user to select a private key; used only to call startActivity(); must not be null. This value cannot be null. |
response |
KeyChainAliasCallback: Callback to invoke when the request completes; must not be null. This value cannot be null. |
keyTypes |
Array<String!>?: The acceptable types of asymmetric keys such as "RSA", "EC" or null. This value may be null. Value is android.security.keystore.KeyProperties#KEY_ALGORITHM_RSA, android.security.keystore.KeyProperties#KEY_ALGORITHM_EC, android.security.keystore.KeyProperties#KEY_ALGORITHM_AES, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA1, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA224, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA256, android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA384, or android.security.keystore.KeyProperties#KEY_ALGORITHM_HMAC_SHA512 |
issuers |
Array<Principal!>?: The acceptable certificate issuers for the certificate matching the private key, or null. This value may be null. |
uri |
Uri?: The full URI the server is requesting the certificate for, or null if unavailable. This value may be null. |
alias |
String?: The alias to preselect if available, or null if unavailable. This value may be null. |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if the specified issuers are not of type X500Principal. |
createInstallIntent
static fun createInstallIntent(): Intent
Returns an Intent that can be used for credential installation. The intent may be used without any extras, in which case the user will be able to install credentials from their own source.
Alternatively, EXTRA_CERTIFICATE or maybe used to specify the bytes of an X.509 certificate or a PKCS#12 key store for installation. These extras may be combined with EXTRA_NAME to provide a default alias name for credentials being installed.
When used with Activity#startActivityForResult, Activity#RESULT_OK will be returned if a credential was successfully installed, otherwise Activity#RESULT_CANCELED will be returned.
| Return | |
|---|---|
Intent |
This value cannot be null. |
getCertificateChain
static fun getCertificateChain(
context: Context,
alias: String
): Array<X509Certificate!>?
Returns the X509Certificate chain for the requested alias, or null if the alias does not exist or the caller has no permission to access it (see note on exceptions in getPrivateKey).
Note: If a certificate chain was explicitly specified when the alias was installed, this method will return that chain. If only the client certificate was specified at the installation time, this method will try to build a certificate chain using all available trust anchors (preinstalled and user-added).
This method may block while waiting for a connection to another process, and must never be called from the main thread.
As Activity and Service contexts are short-lived and can be destroyed at any time from the main thread, it is safer to rely on a long-lived context such as one returned from Context#getApplicationContext().
In case the caller specifies an alias for which it lacks a grant, it must call choosePrivateKeyAlias again. See getPrivateKey for more details on coping with this scenario.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
alias |
String: The alias of the desired certificate chain, typically returned via KeyChainAliasCallback#alias. This value cannot be null. |
context |
Context: This value cannot be null. |
| Exceptions | |
|---|---|
android.security.KeyChainException |
if the alias was valid but there was some problem accessing it. |
java.lang.IllegalStateException |
if called from the main thread. |
getPrivateKey
static fun getPrivateKey(
context: Context,
alias: String
): PrivateKey?
Returns the PrivateKey for the requested alias, or null if the alias does not exist or the caller has no permission to access it (see note on exceptions below).
This method may block while waiting for a connection to another process, and must never be called from the main thread.
As Activity and Service contexts are short-lived and can be destroyed at any time from the main thread, it is safer to rely on a long-lived context such as one returned from Context#getApplicationContext().
If the caller provides a valid alias to which it was not granted access, then the caller must invoke choosePrivateKeyAlias again to get another valid alias or a grant to access the same alias.
On Android versions prior to Q, when a key associated with the specified alias is unavailable, the method will throw a KeyChainException rather than return null. If the exception's cause (as obtained by calling KeyChainException.getCause()) is a throwable of type IllegalStateException then the caller lacks a grant to access the key and certificates associated with this alias.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
alias |
String: The alias of the desired private key, typically returned via KeyChainAliasCallback#alias. This value cannot be null. |
context |
Context: This value cannot be null. |
| Exceptions | |
|---|---|
android.security.KeyChainException |
if the alias was valid but there was some problem accessing it. |
java.lang.IllegalStateException |
if called from the main thread. |
isBoundKeyAlgorithm
static funisBoundKeyAlgorithm(algorithm: String): Boolean
Deprecated: Whether the key is bound to the secure hardware is known only once the key has been imported. To find out, use:
<code>PrivateKey key = ...; // private key from KeyChain
KeyFactory keyFactory =
KeyFactory.getInstance(key.getAlgorithm(), "AndroidKeyStore");
KeyInfo keyInfo = keyFactory.getKeySpec(key, KeyInfo.class);
if (keyInfo.isInsideSecureHardware()) {
// The key is bound to the secure hardware of this Android
}</code>
Returns true if the current device's KeyChain binds any PrivateKey of the given algorithm to the device once imported or generated. This can be used to tell if there is special hardware support that can be used to bind keys to the device in a way that makes it non-exportable.
isKeyAlgorithmSupported
static fun isKeyAlgorithmSupported(algorithm: String): Boolean
Returns true if the current device's KeyChain supports a specific PrivateKey type indicated by algorithm (e.g., "RSA").