Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions) Summary: [Constants](https://developer.android.com/reference/android/security/KeyChainManager#constants) \| [Methods](https://developer.android.com/reference/android/security/KeyChainManager#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/security/KeyChainManager#inhmethods)

# KeyChainManager

*** ** * ** ***


`
public

final

class
KeyChainManager
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.security.KeyChainManager |

<br />

*** ** * ** ***

Manages KeyChain certificates and grants.

## Summary

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE` Scope indicating the key pair is installed device-wide and is visible to all affiliated users on the device. |
| `int` | `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` Scope indicating the key pair is installed for the current user only. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/java/security/cert/X509Certificate>` | ` https://developer.android.com/reference/android/security/KeyChainManager#getCertificateChain(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String alias, int scope) ` Returns the certificate chain of the given scoped certificate. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/security/GrantedKeyInfo>` | ` https://developer.android.com/reference/android/security/KeyChainManager#getGrantedKeyPairs(int)(int scope) ` Returns a list of granted keys that the calling app has access to for the specified scope. |
| ` https://developer.android.com/reference/java/security/PrivateKey` | ` https://developer.android.com/reference/android/security/KeyChainManager#getPrivateKey(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String alias, int scope) ` Returns the private key for the given alias and scope. |

### Inherited methods

From class `

https://developer.android.com/reference/java/lang/Object

` `


https://developer.android.com/reference/java/lang/Object` `
https://developer.android.com/reference/java/lang/Object#clone()()
`

Creates and returns a copy of this object.


`


boolean` `
https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj)
`

Indicates whether some other object is "equal to" this one.


`


void` `
https://developer.android.com/reference/java/lang/Object#finalize()()
`

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.


`


final

https://developer.android.com/reference/java/lang/Class<?>` `
https://developer.android.com/reference/java/lang/Object#getClass()()
`

Returns the runtime class of this `Object`.


`


int` `
https://developer.android.com/reference/java/lang/Object#hashCode()()
`

Returns a hash code value for the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notify()()
`

Wakes up a single thread that is waiting on this object's
monitor.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notifyAll()()
`

Wakes up all threads that are waiting on this object's monitor.


`


https://developer.android.com/reference/java/lang/String` `
https://developer.android.com/reference/java/lang/Object#toString()()
`

Returns a string representation of the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait()()
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*.


## Constants

### KEYPAIR_SCOPE_DEVICE

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public static final int KEYPAIR_SCOPE_DEVICE
```

Scope indicating the key pair is installed device-wide and is visible
to all affiliated users on the device.

<br />

Constant Value:

1
(0x00000001)


### KEYPAIR_SCOPE_USER

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public static final int KEYPAIR_SCOPE_USER
```

Scope indicating the key pair is installed for the current user only.
This is the default scope for existing KeyChain credentials.

<br />

Constant Value:

2
(0x00000002)


## Public methods

### getCertificateChain

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public List<X509Certificate> getCertificateChain (String alias, 
                int scope)
```

Returns the certificate chain of the given scoped certificate. Callable by the application
that installed the certificate, any app that has been granted access to the given alias,
as well as DPCs and their delegates.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: The alias of the desired certificate chain. This value cannot be `null`. <br /> |
| `scope` | `int`: The scope to search (`https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` or `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE`). Value is one of the following: - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/java/security/cert/X509Certificate>` | The list of `https://developer.android.com/reference/java/security/cert/X509Certificate`s representing the chain, or an empty list if no certificate exists for the given alias/scope or if access to the key is not granted. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `scope` is invalid. |

### getGrantedKeyPairs

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public List<GrantedKeyInfo> getGrantedKeyPairs (int scope)
```

Returns a list of granted keys that the calling app has access to for the specified scope.

<br />

| Parameters ||
|---|---|
| `scope` | `int`: The scope of the key pair: `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` or `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE`. Value is one of the following: - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/security/GrantedKeyInfo>` | A non-null list of `https://developer.android.com/reference/android/security/GrantedKeyInfo` objects. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `scope` is invalid. |

### getPrivateKey

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public PrivateKey getPrivateKey (String alias, 
                int scope)
```

Returns the private key for the given alias and scope.


This method may take several seconds to complete, so it should
only be called from a worker thread.

<br />

| Parameters ||
|---|---|
| `alias` | `String`: The alias of the desired key pair. This value cannot be `null`. <br /> |
| `scope` | `int`: The scope to search (`https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` or `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE`). Value is one of the following: - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_USER` - `https://developer.android.com/reference/android/security/KeyChainManager#KEYPAIR_SCOPE_DEVICE` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/PrivateKey` | The `https://developer.android.com/reference/java/security/PrivateKey`, or `null` if no key exists for the given alias/scope or if access to the key is not granted. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `scope` is invalid. |