小编KaP*_*aPa的帖子

在Android中,如何确保私钥存储在安全硬件中

KeyInfo 的 isInsideSecureHardware-method 的返回值似乎取决于设备型号、操作系统版本和其他一些随机因素。

例如,当使用 Sony xperia z5 compact 和较旧的操作系统版本时,isInsideSecureHardware() 可能会在一段时间内返回 true,然后突然开始为相同的私钥返回 false。使用最新的操作系统版本 (32.2.A.0.224) 似乎只返回 false。华为 Nexus 6P 返回始终为真。

有没有办法确保密钥存储在安全硬件中?

这是我当前的代码:

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
                                    .setUserAuthenticationRequired(true)
                                    .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
                                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                                    .build());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// Check that private key is inside secure hardware
KeyFactory factory = KeyFactory.getInstance(key.getAlgorithm(), "AndroidKeyStore");
KeyInfo keyInfo = factory.getKeySpec(key, KeyInfo.class);
boolean secure = keyInfo.isInsideSecureHardware(); //  this usually returns false
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:在索尼支持论坛上有一个关于同样问题的话题:https : //talk.sonymobile.com/t5/Android-development/hardware-backed-keystore/td-p/1154124

有人提到以下警告会打印到日志中:

W 密钥库:主 keymaster 设备无法生成密钥,回退到 SW。

security android android-keystore private-key

6
推荐指数
1
解决办法
2389
查看次数

标签 统计

android ×1

android-keystore ×1

private-key ×1

security ×1