public final class KeyChain
extends Object
| java.lang.Object | |
| android.security.KeyChain | |
KeyChain类提供对凭证存储中的私钥和其相应证书链的访问。
访问 KeyChain应用程序通常会经历以下步骤:
X509KeyManager that a private key is requested. choosePrivateKeyAlias to 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 callback alias(String), or null if no private key is available or the user cancels the request. getPrivateKey(Context, String) and getCertificateChain(Context, String) to retrieve the credentials to return to the corresponding X509KeyManager callbacks. 应用程序可能会记住所选别名的值,以避免在后续连接中提示用户choosePrivateKeyAlias 。 如果别名不再有效,则使用该值的查找将返回空值
应用程序可以通过Intent提供的createInstallIntent()请求安装私钥和证书。 通过Intent安装的Intent将通过choosePrivateKeyAlias(Activity, KeyChainAliasCallback, String[], Principal[], Uri, String)访问,而证书颁发机构(CA)证书将通过默认的X509TrustManager被所有应用程序信任。
常量(Constants) |
|
|---|---|
String |
ACTION_STORAGE_CHANGED 广播动作:表示可信存储已更改。 |
String |
EXTRA_CERTIFICATE 可选额外指定 |
String |
EXTRA_NAME 可选额外指定 |
String |
EXTRA_PKCS12 可选额外用于由 |
Public constructors |
|
|---|---|
KeyChain() |
|
公共方法(Public methods) |
|
|---|---|
static void |
choosePrivateKeyAlias(Activity activity, KeyChainAliasCallback response, String[] keyTypes, Principal[] issuers, String host, int port, String alias) 为用户启动 |
static void |
choosePrivateKeyAlias(Activity activity, KeyChainAliasCallback response, String[] keyTypes, Principal[] issuers, Uri uri, String alias) 为用户启动 |
static Intent |
createInstallIntent() 返回可用于凭证安装的 |
static X509Certificate[] |
getCertificateChain(Context context, String alias) 返回所请求的别名的 |
static PrivateKey |
getPrivateKey(Context context, String alias) 返回所请求的别名的 |
static boolean |
isBoundKeyAlgorithm(String algorithm) 此方法在API级别23中已弃用。密钥是否已被导入后,是否知道该密钥是否与安全硬件绑定。 要了解,请使用: |
static boolean |
isKeyAlgorithmSupported(String algorithm) 如果当前设备的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
String ACTION_STORAGE_CHANGED
广播动作:表示可信存储已更改。 发生这种情况时发送:
常量值:“android.security.STORAGE_CHANGED”
String EXTRA_CERTIFICATE
可选额外指定Intent返回的createInstallIntent()上安装的X.509证书。 额外的值应该是PEM或ASN.1 DER编码的byte[] 。 一个X509Certificate可以转换为DER编码字节getEncoded() 。
可以使用 EXTRA_NAME为安装的证书提供默认别名。
常量值:“CERT”
String EXTRA_NAME
可选额外指定 String的证书名称 Intent通过返回 createInstallIntent() 。
常量值:“名称”
String EXTRA_PKCS12
可选额外用于由Intent返回的createInstallIntent()指定要安装的PKCS#12密钥库。 额外的值应该是byte[] 。 这些字节可能来自外部来源,也可能在“PKCS12”实例上使用store(OutputStream, char[])生成。
系统将提示用户输入密码以加载密钥存储。
密钥存储将被扫描 KeyStore.PrivateKeyEntry条目,并且私钥和相关证书链将被安装。
EXTRA_NAME可用于为安装的凭证提供默认别名。
常量值:“PKCS12”
void choosePrivateKeyAlias (Activity activity, KeyChainAliasCallback response, String[] keyTypes, Principal[] issuers, String host, int port, String alias)
为用户启动Activity ,以便为私钥和证书对选择别名以进行身份验证。 所选的别名或null将通过KeyChainAliasCallback回调返回。
在显示活动之前,设备或配置文件所有者可以拦截此事件,以选择特定的私钥别名。
可以使用 keyTypes和 issuers向用户强调建议的选择,尽管为了处理有时由服务器提供的错误值,用户可能能够覆盖这些建议。
可以使用 host和 port向用户提供关于请求证书的服务器的更多上下文。
alias允许选择器预先选择一个现有的别名,这个别名仍然需要用户确认。
| 参数(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. |
response |
KeyChainAliasCallback: Callback to invoke when the request completes; must not be null |
keyTypes |
String: The acceptable types of asymmetric keys such as "RSA" or "DSA", or a null array. |
issuers |
Principal: The acceptable certificate issuers for the certificate matching the private key, or null. |
host |
String: The host name of the server requesting the certificate, or null if unavailable. |
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. |
void choosePrivateKeyAlias (Activity activity, KeyChainAliasCallback response, String[] keyTypes, Principal[] issuers, Uri uri, String alias)
为用户启动Activity以选择用于认证的私钥和证书对的别名。 所选的别名或null将通过KeyChainAliasCallback回调返回。
在显示活动之前,设备或配置文件所有者可以拦截此事件,以选择特定的私钥别名。
可以使用 keyTypes和 issuers向用户强调建议的选择,尽管为了处理有时由服务器提供的错误值,用户可能能够覆盖这些建议。
可以使用 host和 port向用户提供关于请求证书的服务器的更多上下文。
alias允许选择器预先选择一个现有的别名,这个别名仍然需要用户确认。
| 参数(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. |
response |
KeyChainAliasCallback: Callback to invoke when the request completes; must not be null |
keyTypes |
String: The acceptable types of asymmetric keys such as "EC" or "RSA", or a null array. |
issuers |
Principal: The acceptable certificate issuers for the certificate matching the private key, or null. |
uri |
Uri: The full URI the server is requesting the certificate for, or null if unavailable. |
alias |
String: The alias to preselect if available, or null if unavailable. |
Intent createInstallIntent ()
返回可用于凭证安装的Intent 。 意图可以在没有任何额外的情况下使用,在这种情况下用户将能够从他们自己的来源安装证书。
或者,也可以使用EXTRA_CERTIFICATE或EXTRA_PKCS12来指定要安装的X.509证书或PKCS#12密钥存储区的字节。 这些额外信息可能与EXTRA_NAME结合为正在安装的凭证提供默认别名。
与 startActivityForResult(Intent, int) RESULT_OK使用时,如果成功安装了凭证,将返回 RESULT_CANCELED ,否则将返回 RESULT_CANCELED 。
| 返回(Returns) | |
|---|---|
Intent |
|
X509Certificate[] getCertificateChain (Context context, String alias)
返回所请求的别名的 X509Certificate链,如果没有结果,则返回null。
注意:如果在安装别名时明确指定了证书链,则此方法将返回该链。 如果在安装时仅指定客户端证书,则此方法将尝试使用所有可用的信任锚(预安装和用户添加)来构建证书链。
此方法可能在等待与另一个进程的连接时阻塞,并且绝不能从主线程调用。
| 参数(Parameters) | |
|---|---|
context |
Context
|
alias |
String: The alias of the desired certificate chain, typically returned via alias(String). |
| 返回(Returns) | |
|---|---|
X509Certificate[] |
|
| 抛出异常(Throws) | |
|---|---|
KeyChainException |
if the alias was valid but there was some problem accessing it. |
IllegalStateException |
if called from the main thread. |
InterruptedException |
|
PrivateKey getPrivateKey (Context context, String alias)
返回所请求的别名的 PrivateKey ,如果没有结果,则返回null。
此方法可能在等待与另一个进程的连接时阻塞,并且绝不能从主线程调用。
| 参数(Parameters) | |
|---|---|
context |
Context
|
alias |
String: The alias of the desired private key, typically returned via alias(String). |
| 返回(Returns) | |
|---|---|
PrivateKey |
|
| 抛出异常(Throws) | |
|---|---|
KeyChainException |
if the alias was valid but there was some problem accessing it. |
IllegalStateException |
if called from the main thread. |
InterruptedException |
|
boolean isBoundKeyAlgorithm (String algorithm)
此方法在API级别23中已弃用。
只有在导入密钥后才知道密钥是否与安全硬件绑定。 要了解,请使用:
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
}
返回true如果当前设备的KeyChain结合任何PrivateKey给定的algorithm一次进口或产生的设备。 这可以用来判断是否有特殊的硬件支持可用于将密钥绑定到设备,使其不可输出。
| 参数(Parameters) | |
|---|---|
algorithm |
String
|
| 返回(Returns) | |
|---|---|
boolean |
|
boolean isKeyAlgorithmSupported (String algorithm)
如果当前设备的 KeyChain支持由 algorithm (例如“RSA”)指定的特定 PrivateKey类型,则返回 true 。
| 参数(Parameters) | |
|---|---|
algorithm |
String
|
| 返回(Returns) | |
|---|---|
boolean |
|