我最近在使用 Android 密钥库系统。
根据此链接提供的GrapheneOS/AttestationSamples,使用小米9手机和华为P20(更新至Android 9)进行测试,可能不支持StrongBox 。
此外,以下代码返回 false 表示该设备没有 StrongBox 功能。
private boolean hasStrongBox(Context context){
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_STRONGBOX_KEYSTORE);
}
Run Code Online (Sandbox Code Playgroud)
但是,在测试过程中,当我使用下面的代码生成 AES 加密密钥时,两部手机都不会抛出 StrongBoxUnavailableException:
protected int createAndroidKeyStoreSymmetricKey() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(ALIAS_MASTER_KEY, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
.setIsStrongBoxBacked(true);
keyGenerator.init(builder.build());
secretKey = keyGenerator.generateKey();
if(secretKey != null ){
return 0;
} else {
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
在生成 RSA 加密密钥时,它们都抛出了 StrongBoxUnavailableException:
private KeyPair genKeyPair(String alias, boolean isStrongBoxBacked) throws Exception {
KeyPairGenerator kpg =
KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
KeyGenParameterSpec.Builder keyBuilder = new KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.setBlockModes(KeyProperties.BLOCK_MODE_ECB)
.setKeySize(CRYPTO_BITS)
.setIsStrongBoxBacked(true);
kpg.initialize(keyBuilder.build());
return kpg.generateKeyPair();
}
Run Code Online (Sandbox Code Playgroud)
android.security.keystore.StrongBoxUnavailableException: Failed to generate key pair
Run Code Online (Sandbox Code Playgroud)
密钥生成期间是否有任何规范配置错误?由于预期结果是即使在 AES 生成期间也会抛出 StrongBoxUnavailableException。
| 归档时间: |
|
| 查看次数: |
667 次 |
| 最近记录: |