Android KeyStore的使用

在我们App开发过程中,可能会涉及到一些敏感和安全数据需要加密的情况,比如登录token的存储。我们往往会使用一些加密算法将这些敏感数据加密之后再保存起来,需要取出来的时候再进行解密。

此时就会有一个问题:用于加解密的Key该如何存储?

  • 如果把Key和加密后的数据存到一起,那有一定的安全风险。
  • 对Key再进行一次加密,这就陷入了死循环。

为了保证安全性,Android提供了KeyStore系统来保存Key,本文就浅探一下KeyStore及其使用方法。

一、什么是KeyStore?如何保证安全性?

1、什么是KeyStore?

先来看看官方对他的定义:

Android Keystore系统可让您将加密密钥存储在容器中,以使其更难从设备中提取。一旦密钥进入密钥库,就可以将其用于加密操作,而密钥材料仍不可导出。而且,它提供了限制何时和如何使用密钥的功能,例如要求用户进行身份验证才能使用密钥,或者限制仅在某些加密模式下使用密钥。

定义其实很简单,他就是用来保存加解密的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里的操作,为什么后

面就可以直接取出来?想要搞清楚这个问题,就必须得通过源码去解决了。

先拟定一下分析问题的思路

  1. KeyStore是从哪里取的?
  2. 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的存取方式做了源码分析。
————————————————
版权声明:本文为CSDN博主「电光法拉利」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43478882/article/details/127392947

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Keystore 2是Android系统中的一个加密机制,用于存储和保护应用程序的机密信息,例如密钥、证书和数字签名等。它提供了一个安全的方式来存储和管理应用程序的密钥,确保这些机密信息不会被恶意攻击者窃取。 在Android Keystore 2中,密钥是使用硬件支持加密的,这意味着即使应用程序被攻击者窃取,也无法轻易地获得和使用这些密钥。此外,Android Keystore 2还提供了对密钥和签名的校验和验证,以确保数据完整性和身份验证。 使用Android Keystore 2,开发人员可以安全地存储会话密钥、API密钥、数字证书和其他重要信息。它还提供了一套API,以简化密钥生成、存储和使用的过程。开发人员可以使用这些API,使其应用程序具有更高的安全性和数据保护性能。 总之,Android Keystore 2是一个强大而安全的加密机制,可以保护应用程序中的关键信息。它提供了一种安全的方式来存储和管理密钥,使得攻击者难以窃取敏感信息。开发人员可以使用它,确保其应用程序具有更高的安全性和性能。 ### 回答2: Android Keystore2是一个用于安全地存储密钥和证书的框架,它是Android系统提供的加密服务。它可以保护敏感数据,防止数据被未经授权访问和使用Android Keystore2的优点: 1. 强加密:使用先进的标准加密算法来保护密钥和证书。系统会使用硬件密钥保护加密密钥,使攻击者无法轻易获取到密钥和证书。 2. 可扩展性:支持多个存储提供者,可以通过不同的存储提供者来添加新的密钥类型和证书类型。 3. 认证安全:支持安全缓存的证书和密钥。它可以在设备锁定后,确保应用程序可以安全地访问受保护的密钥和证书。 4. 非破坏性操作:支持可重复使用的密钥和证书的多次使用,同时保持其完整性和安全性。 5. 异地备份和恢复:支持将密钥和证书导出为单独的备份文件,支持将其复制到另一个设备或平台上恢复。 总的来说,Android Keystore2是一个非常强大的加密框架,可以为用户和应用程序提供有效的安全保障。它可以支持大多数应用程序需要的各种证书和密钥,并在处理敏感数据时提供了安全性和隐私性。无论是作为个人用户还是企业用户,安全保障都是非常重要的,Android Keystore2的出现为Android平台上的数据安全管理提供了稳定而强大的支持。 ### 回答3: Android Keystore2是Android系统的一个组件,用于管理和保护应用程序中的机密信息,如私钥、证书、密码等。它的主要作用是提供一个安全的存储空间,可以对敏感信息进行加密和解密,以保护应用程序的安全性和数据的完整性。 在之前的版本中,Android使用了一个叫做“Android Keystore”的组件来实现应用程序的加密和解密。然而,由于它的一些不足,例如不支持硬件密钥存储和在使用上存在一定的复杂性等问题,所以在Android 7.0(Nougat)版本中,Google引入了Android Keystore2,来能够更好地满足应用程序的安全需求。 具体来说,Android Keystore2与之前的Android Keystore相比,它的最大优势之一是支持硬件密钥存储,这使得应用程序的机密信息更加安全可靠。此外,新版本的Keystore还提供了更多的加密算法和安全功能,包括更灵活的密钥管理、更严格的访问控制、更安全的随机数生成器、更可靠的密钥导出和非对称密钥等。 总之,Android Keystore2是一个非常重要的安全组件,它可以提供可靠的保护机制来保护应用程序的敏感信息,有助于提高用户的安全性和隐私保护。同时,作为开发者,我们也要认真学习和使用Android Keystore2,以更好地保护我们的应用程序安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值