Android KeyStore的使用

2023-05-16

在我们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里的操作,为什么后面就可以直接取出来?**想要搞清楚这个问题,就必须得通过源码去解决了。

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

  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,如果使用KeyGeneratorKeyStore,并对KeyStore的存取方式做了源码分析。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android KeyStore的使用 的相关文章

随机推荐

  • 构建Spring项目的一些配置文件

    pom xml基础配置 lt dependencies gt lt https mvnrepository com artifact log4j log4j gt lt dependency gt lt groupId gt log4j l
  • Api Generator Plus & Copy as curl 自动上传YApi接口

    Api Generator Plus 插件能够一键自动上传YApi接口 xff1b 一键生成 curl 命令 fetch方法 axios方法 快速开始 1 打开插件管理 xff0c 搜索api generator plus xff0c 安装
  • centos6升级到7.2-----2023年

    CentOS 6 升级到 CentOS 7 准确的说 xff0c 是从 CentOS 6 5 先升级到 CentOS 7 2 只有 6 5 以上版本才能这么升级 xff0c 而且最高只能升级到 7 2 现在的问题是官网已经不再维护 Cent
  • 怎么在IDEA中配置power shell

    第一步 xff1a 接下来 xff0c 在步骤3那里找到黄色方框里面的powershell exe文件 在点击OK之后 xff0c 你就能看到以下 说明 xff0c 已经配置成功了
  • 【数据结构】——二叉树的创建详解

    之前有篇文章概要的叙述了一下关于二叉树的创建 xff0c 参考博文二叉树的c语言创建但是很不全面 xff0c 这篇文章就让我们来好好探讨一下关于二叉树的创建吧 首先 xff0c 我们要做一些前期准备 xff0c 有关于本课二叉树的结点信息和
  • Fortify--安装与使用

    Fortify是Micro Focus旗下AST xff08 应用程序安全测试 xff09 产品 xff0c 其产品组合包括 xff1a Fortify Static Code Analyzer提供静态代码分析器 xff08 SAST xf
  • [web安全]burpsuite抓取微信公众号、小程序数据包

    最近在接触微信公众号和微信小程序的渗透 xff0c 用过模拟器 xff0c 也直接在手机上设置代理 xff0c 都遇到各种问题 xff0c 用起来很不舒心 记录遇到的一些问题 xff0c 帮助和我一样踩过坑的 xff0c 亲测好用 IE浏览
  • 安全-开源项目安全检查规范

    有些公司为了提高在IT圈的技术知名度 xff0c 增加行业影响力 xff0c 一些技术会定期将非核心业务源代码以公司级名义上传到源码开源平台 xff0c 如GitHub 特此输出相关的安全检查规范 第一条 开源的代码项目可以为通用的解决方案
  • 安全-系统上线安全检查规范

    现在各个公司都开始重视安全 xff0c 不仅仅是因为国家开始重视安全 xff0c 而是安全漏洞一旦暴露 xff0c 被有心之人发现进行破坏 xff0c 损失将无法估量 xff1b 比如 xff1a 前端时间拼多多优惠券事件 安全测试是一项比
  • 应用安全测试技术DAST、SAST、IAST对比分析-持续更新

    应用安全测试技术DAST SAST IAST对比分析 持续更新 版权来源 xff1a 安全牛首发文章 xff0c 本文仅补充完善 一 全球面临软件安全危机 我们即将处于一个软件定义一切的时代 xff0c 这是 一个最好的时代 xff0c 也
  • Python中函数和方法的区别

    简单总结 xff1a 与类和实例无绑定关系的function都属于函数 xff08 function xff09 xff1b 与类和实例有绑定关系的function都属于方法 xff08 method xff09 首先摒弃错误认知 并不是类
  • 九宫格,二十五宫格,甚至八十一宫格 技巧

    九宫格 二十五宫格 甚至八十一宫格 只要是奇数的平方宫格者能做到横格相加 坚格相加 斜格相加得数相同 而偶数的宫格只有十六宫格有些规律 下面是三宫格 五宫格 七宫格 九宫格图 填写技巧 第一行中间填1 xff0c 右上没有的 xff0c 就
  • python 编写输出到csv

    def test write self fields 61 fields append orderCode with open r 39 test001 csv 39 39 a 39 newline 61 34 34 as f writer
  • Vagrant 共享目录出现 mount:unknown filesystem type ‘vboxsf‘

    环境 xff1a Windows 10 VirtualBox 7 0 6 Vagrant 2 3 4 错误如下 xff1a 61 61 gt default Attempting graceful shutdown of VM 61 61
  • 利用python进行企业微信机器人自动发送消息

    def test 004 robot self headers 61 34 Content Type 34 34 text plain 34 s 61 34 卖品 xff0c 打印码 xff1a 验证码 34 format str prin
  • js修改页面hidden属性

    想获取这个value的值 xff0c 但是看其是个input标签 xff0c 他的type属性是hidden 也就是只能定位 xff0c 不能对其操作 xff0c 想要通过元素的 get attribute 34 value 34 是不可能
  • mybatis的多表查询(一对一,一对多,多对多)

    mybatis多表查询 表之间的关系有几种 xff1a 一对多 多对一 一对一 多对多 举例 xff1a 用户和订单就是一对多 订单和用户就是多对一 一个用户可以下多个订单 多个订单属于同一个用户 人和身份证号就是一对一 一个人只能有一个身
  • 8款纯CSS3搜索框

    效果如下 xff1a 代码实现如下 xff1a span class token doctype lt DOCTYPE html gt span span class token tag span class token tag span
  • ViewBinding和DataBinding的理解和区别

    之前一直把ViewBinding当成了DataBinding xff0c 直到最近的学习中才发现他们不是一个东西 于是写下这篇笔记帮助理解和区分他们俩 一 ViewBinding 1 什么是ViewBinding 先来看看官方是怎么说的 通
  • Android KeyStore的使用

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