在我们App开发过程中,可能会涉及到一些敏感和安全数据需要加密的情况,比如登录token的存储。我们往往会使用一些加密算法将这些敏感数据加密之后再保存起来,需要取出来的时候再进行解密。
此时就会有一个问题:用于加解密的Key该如何存储?
- 如果把Key和加密后的数据存到一起,那有一定的安全风险。
- 对Key再进行一次加密,这就陷入了死循环。
为了保证安全性,Android提供了KeyStore
系统来保存Key,本文就浅探一下KeyStore
及其使用方法。
一、什么是KeyStore?如何保证安全性?
1、什么是KeyStore?
先来看看官方对他的定义:
This class represents a storage facility for cryptographic keys and certificates.
这个类代表加密密钥和证书的存储设施
定义其实很简单,他就是用来保存加解密的Key和证书的。
2、如何保证安全性?
安全性保护措施在官方文档里有写(Android 密钥库系统 | Android 开发者 | Android Developers (google.cn)), 我就按自己的理解总结一下:
- Key是保存在手机系统里的,应用进程在获取Key的时候是通过系统进程获取到内存里的。也就是说只能在本应用进程里才能拿到Key,想要把Key提取出来是不可能的。
KeyStore
还可以指定密钥的授权使用方式(比如用户安全锁验证),可保证必须在授权的情况下才能获取到Key。
总的来说就是使用者只能在应用程序运行时使用KeyStore
里存放的Key,除非攻击者拿到源码打断点调试,否则无法知道Key到底是什么。这样就保证了存储Key的安全性。
二、KeyStore的使用
KeyStore
支持的加密算法有很多,其中对部分算法有API Level的要求,具体可以查看Android 密钥库系统 | Android 开发者 | Android Developers (google.cn)
本文就以AES加密算法为例,简单说明一下KeyStore
的使用方式。注意,本文涉及到的代码需要minSdk>=23.
先简单实现一下AES算法的加解密
1、数据加密
object AESUtil {
private const val IV_BLOCK_SIZE = 16
fun encryptAES(encryptBytes: ByteArray, encryptKey: SecretKey): ByteArray?{
try {
//创建密码器
val cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING")
//用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, encryptKey)
val final = cipher.doFinal(encryptBytes)
// iv占前16位,加密后的数据占后面
return cipher.iv + final
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
} catch (e: BadPaddingException) {
e.printStackTrace()
} catch (e: IllegalBlockSizeException) {
e.printStackTrace()
}
return null
}
fun decryptAES(decryptBytes: ByteArray, decryptKey: SecretKey): ByteArray? {
try {
// 先取出IV
val iv = decryptBytes.copyOfRange(0, IV_BLOCK_SIZE)
// 取出加密后的数据
val decryptData = decryptBytes.copyOfRange(IV_BLOCK_SIZE, decryptBytes.size)
val cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING")
cipher.init(Cipher.DECRYPT_MODE, decryptKey, IvParameterSpec(iv))
return cipher.doFinal(decryptData)
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
} catch (e: BadPaddingException) {
e.printStackTrace()
} catch (e: IllegalBlockSizeException) {
e.printStackTrace()
}
return null
}
}
然后我们需要为加密生成一个Key,通过KeyGenerator来实现,先生成一个KeyGenerator
private fun getKeyGenerator(alias: String): KeyGenerator {
// 第一个参数指定加密算法,第二个参数指定Provider
val keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore")
val parameterSpec = KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT //用于加密和解密
)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC) // AEC_CBC
.setUserAuthenticationRequired(false) // 是否需要用户认证
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) //AES算法的PADDING, 和前面的AESUtil里保持统一
.build()
keyGenerator.init(parameterSpec)
return keyGenerator
}
这个方法里接受一个alias的参数,是生成Key的别名,这个会在之后从KeyStore
里取的时候用到。
生成KeyGenerator
之后,就可以生成出加解密需要的Key了:
val key: SecretKey = getKeyGenerator("myKey").generateKey()
那紧接着我们就可以对需要保护的数据进行加密然后存储。
val srcData = "hello world"
val encryptData = AESUtil.encryptAES(srcData.toByteArray(), key)
// 存储加密后的数据
...
2、从KeyStore中获取Key解密
前面我们已经把数据加密存储好了,接下来就是拿出数据解密后使用了。
我们从KeyStore
中取出我们解密的Key
fun getKeyFromKeyStore(alias: String): SecretKey? {
// 参数为Provider
val keyStore = KeyStore.getInstance("AndroidKeyStore")
// 一定要先初始化
keyStore.load(null)
// 获取KeyStore中的所有Key的别名
val aliases = keyStore.aliases()
// KeyStore里没有key
if (!aliases.hasMoreElements()) {
return null
}
// Key的保护参数,这里为不需要密码
val protParam: KeyStore.ProtectionParameter =
KeyStore.PasswordProtection(null)
// 通过别名获取Key
val entry = keyStore.getEntry(alias, protParam) as KeyStore.SecretKeyEntry
return entry.secretKey
}
每一步的注释都写在了代码里,这里方法的alias
参数就是我们之前通过KeyGenerator
生成Key时生成的参数。
接着就可以拿到Key去解密了。
val decryptKey = KeyStoreUtil.getKeyFromKeyStore(KEY_ALIAS)
decryptKey?.let {
// 解密数据
val decryptAES = AESUtil.decryptAES(encryptData, decryptKey)
}
到这里,KeyStore
的简单使用就结束了。
三、源码分析
细心的读者可能会发现问题,在前面的使用中,**并没有把Key存入到KeyStore
里的操作,为什么后面就可以直接取出来?**想要搞清楚这个问题,就必须得通过源码去解决了。
先拟定一下分析问题的思路:
KeyStore
是从哪里取的?KeyGenerator
生成Key的时候是怎么存的?
1、KeyStore是从哪里取的
KeyStore
取Key的方法主要是getEntry
,这个方法的源码很清晰简单
public final Entry getEntry(String alias, ProtectionParameter protParam)
throws NoSuchAlgorithmException, UnrecoverableEntryException,
KeyStoreException {
if (alias == null) {
throw new NullPointerException("invalid null input");
}
if (!initialized) {
throw new KeyStoreException("Uninitialized keystore");
}
return keyStoreSpi.engineGetEntry(alias, protParam);
}
首先取的时候alias
不能为空,这是取Key的别名,如果为空自然就不知道你要哪一个Key了。
其次会判断KeyStore
是否初始化。
那核心的代码就是最后一行了。
这里的KeyStoreSpi
是一个abstract
类,里面实现了engineGetEntry
方法。
public KeyStore.Entry engineGetEntry(String alias,
KeyStore.ProtectionParameter protParam)
throws KeyStoreException, NoSuchAlgorithmException,
UnrecoverableEntryException {
...
if ((protParam == null) || protParam instanceof KeyStore.PasswordProtection) {
if (engineIsCertificateEntry(alias)) {
throw new UnsupportedOperationException
("trusted certificate entries are not password-protected");
} else if (engineIsKeyEntry(alias)) {
char[] password = null;
if (protParam != null) {
KeyStore.PasswordProtection pp =
(KeyStore.PasswordProtection)protParam;
password = pp.getPassword();
}
Key key = engineGetKey(alias, password);
....
}
}
....
}
顺着源码走就会发现,真正拿Key的实现是通过engineGetKey()
方法拿的,而这个方法是个abstract方法,也就是要找到具体的实现类。
我们使用的Provider是AndroidKeyStore
,对应是实现类是AndroidKeyStoreSpi
。那就到里面取看一下engineGetKey()
的实现
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException,UnrecoverableKeyException {
try {
return AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(mKeyStore,alias,mNamespace);
}
....
}
里面最核心的代码也就一句话,继续深挖到AndroidKeyStoreProvider
里。
public static AndroidKeyStoreKey loadAndroidKeyStoreKeyFromKeystore(
@NonNull KeyStore2 keyStore, @NonNull String alias, int namespace)
throws UnrecoverableKeyException, KeyPermanentlyInvalidatedException {
....
final AndroidKeyStoreKey key = loadAndroidKeyStoreKeyFromKeystore(keyStore, descriptor);
if (key instanceof AndroidKeyStorePublicKey) {
return ((AndroidKeyStorePublicKey) key).getPrivateKey();
} else {
return key;
}
}
核心代码是loadAndroidKeyStoreKeyFromKeystore()
方法
private static AndroidKeyStoreKey loadAndroidKeyStoreKeyFromKeystore(
@NonNull KeyStore2 keyStore, @NonNull KeyDescriptor descriptor)
throws UnrecoverableKeyException, KeyPermanentlyInvalidatedException {
KeyEntryResponse response = null;
try {
// 核心代码
response = keyStore.getKeyEntry(descriptor);
} catch (android.security.KeyStoreException e) {
....
}
}
...
}
终于快能看到最终拿Key的地方了,不过这里的keyStore
要注意以下,是Android下的KeyStore2
这个类。
/**
* Retrieves a key entry from the keystore backend.
* @see IKeystoreService#getKeyEntry(KeyDescriptor) for more details.
* @param descriptor
* @return
* @throws KeyStoreException
* @hide
*/
public KeyEntryResponse getKeyEntry(@NonNull KeyDescriptor descriptor)
throws KeyStoreException {
return handleRemoteExceptionWithRetry((service) -> service.getKeyEntry(descriptor));
}
从注释里可以看到**,KeyStore
获取Key的方式是通过IKeystoreService
这个服务取获取的,也就是通过系统进程获取的。**这里我们主要是查看从哪里取的,更多如何取的细节读者可以看一下IKeystoreService
。
2、怎么存的?
前面我们弄清楚了是从哪里取的,接下来就要看一看是怎么存进去的。
KeyStore
里存Key的方法是setEntry()
,我们就从这里下手看看。
public final void setEntry(String alias, Entry entry,
ProtectionParameter protParam)
throws KeyStoreException {
if (alias == null || entry == null) {
throw new NullPointerException("invalid null input");
}
if (!initialized) {
throw new KeyStoreException("Uninitialized keystore");
}
keyStoreSpi.engineSetEntry(alias, entry, protParam);
}
可以看到,存的时候KeyStore
还是交给了KeyStoreSpi
。而KeyStoreSpi
的核心方法是engineSetKeyEntry()
,我们直接到AndroidKeyStoreSpi
里去看具体的实现。
@Override
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain)
throws KeyStoreException {
if ((password != null) && (password.length > 0)) {
throw new KeyStoreException("entries cannot be protected with passwords");
}
if (key instanceof PrivateKey) {
setPrivateKeyEntry(alias, (PrivateKey) key, chain, null);
} else if (key instanceof SecretKey) {
setSecretKeyEntry(alias, (SecretKey) key, null);
} else {
throw new KeyStoreException("Only PrivateKey and SecretKey are supported");
}
}
这里简单说一下:
-
PrivateKey
通常是非对称加密算法的私钥,公钥用于加密,私钥用于解密,比如RSA算法。 -
SecretKey
通常是对称加密算法的密钥,加密解密都用他,比如AES算法。
接着看一下setSecretKeyEntry()
方法
private void setSecretKeyEntry(String alias, SecretKey key,
java.security.KeyStore.ProtectionParameter param)
throws KeyStoreException {
...
try {
KeyStoreSecurityLevel securityLevelInterface = mKeyStore.getSecurityLevel(
securityLevel);
KeyDescriptor descriptor = makeKeyDescriptor(alias);
securityLevelInterface.importKey(descriptor, null /* TODO attestationKey */,
importArgs, flags, keyMaterial);
} catch (android.security.KeyStoreException e) {
throw new KeyStoreException("Failed to import secret key.", e);
}
}
方法很长,但是最终存入的方法是最后这里。
可以看到,最终是KeyStoreSecurityLevel
这个类通过importKey()
方法去保存的。
/**
* Imports a key into Keystore.
* @see IKeystoreSecurityLevel#importKey(KeyDescriptor, KeyDescriptor, KeyParameter[], int,
*/
public KeyMetadata importKey(KeyDescriptor descriptor, KeyDescriptor attestationKey,
Collection<KeyParameter> args, int flags, byte[] keyData)
throws KeyStoreException {
return handleExceptions(() -> mSecurityLevel.importKey(descriptor, attestationKey,
args.toArray(new KeyParameter[args.size()]), flags, keyData));
}
从注释里就能看懂了,往KeyStore里导入Key
3、KeyGenerator是不是帮我们存了?
搞清楚了怎么存的,就可以去KeyGenerator的源码看看他是不是确实帮我们直接保存了。
public final SecretKey generateKey() {
if (serviceIterator == null) {
return spi.engineGenerateKey();
}
....
}
KeyGeneratorSpi
也是个abstact
类,我们这里的具体实现类是AndroidKeyStoreKeyGeneratorSpi
@Override
protected SecretKey engineGenerateKey() {
....
KeyStoreSecurityLevel iSecurityLevel = null;
try {
iSecurityLevel = mKeyStore.getSecurityLevel(securityLevel);
metadata = iSecurityLevel.generateKey(
descriptor,
null, /* Attestation key not applicable to symmetric keys. */
params,
flags,
additionalEntropy);
} catch (android.security.KeyStoreException e) {
switch (e.getErrorCode()) {
// TODO replace with ErrorCode.HARDWARE_TYPE_UNAVAILABLE when KeyMint spec
// becomes available.
case KeymasterDefs.KM_ERROR_HARDWARE_TYPE_UNAVAILABLE:
throw new StrongBoxUnavailableException("Failed to generate key");
default:
throw new ProviderException("Keystore key generation failed", e);
}
}
....
SecretKey result = new AndroidKeyStoreSecretKey(descriptor, metadata, keyAlgorithmJCA,
iSecurityLevel);
return result;
}
这个方法也特别长,但是在最后能看到一个老朋友:KeyStoreSecurityLevel
。原来最终生成Key的方法是调用了他的generateKey()
方法。
/**
* Generates a new key in Keystore.
* @see IKeystoreSecurityLevel#generateKey(KeyDescriptor, KeyDescriptor, KeyParameter[], int,
*/
public KeyMetadata generateKey(@NonNull KeyDescriptor descriptor, KeyDescriptor attestationKey,
Collection<KeyParameter> args, int flags, byte[] entropy)
throws KeyStoreException {
return handleExceptions(() -> mSecurityLevel.generateKey(
descriptor, attestationKey, args.toArray(new KeyParameter[args.size()]),
flags, entropy));
}
在KeyStore
里生成一个新的Key,这里就很明显了。
KeyGenerator
最终在生成Key的时候,会直接生成在KeyStore
里,所以我们才可以直接取到。
四、总结
本篇文章简单介绍了什么是KeyStore
,如果使用KeyGenerator
和KeyStore
,并对KeyStore
的存取方式做了源码分析。