【问题标题】:UnrecoverableKeyException Failed to obtain information about private key, KeyStoreException: Invalid key blobUnrecoverableKeyException 获取私钥信息失败,KeyStoreException: Invalid key blob
【发布时间】:2018-06-25 16:46:58
【问题描述】:

在我们的应用中,我们遇到了 Android Keystore 中的数据突然变得无法访问的问题。我们看到的具体异常在这里:

java.security.UnrecoverableKeyException: Failed to obtain information about private key
 at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:223)
 at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:259)
 at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePrivateKeyFromKeystore(AndroidKeyStoreProvider.java:269)
 at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:94)
 at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:474)
 at java.security.KeyStore.getEntry(KeyStore.java:1560)
 at <PACKAGE_NAME>.EncryptionInteractor.generateKeys(EncryptionInteractor.java:104)
 at <PACKAGE_NAME>.EncryptionInteractor.generateKeys(EncryptionInteractor.java:100)
 at <PACKAGE_NAME>.EncryptionInteractor.init(EncryptionInteractor.java:93)
 at <PACKAGE_NAME>.EncryptionInteractor.<init>(EncryptionInteractor.java:80)
 at <PACKAGE_NAME>.EncryptionInteractor.init(EncryptionInteractor.java:65)
 at <PACKAGE_NAME>.<APPLICATION_CLASS>.onCreate(APPLICATION_CLASS.java:17)
 at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1118)
 at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5791)
 at android.app.ActivityThread.-wrap1(Unknown Source:0)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
 at android.os.Handler.dispatchMessage(Handler.java:105)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6541)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.security.KeyStoreException: Invalid key blob
 at android.security.KeyStore.getKeyStoreException(KeyStore.java:695)
 at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:224)
  ... 21 more

我们无法找到重现问题的可靠方法。有几篇文章提到了可能导致密钥库“忘记”密钥或被锁定的可能状态,例如here。然而,据我所知,我们还没有陷入任何这些极端情况。在第一次设置密钥后让设备静置一段时间后似乎会发生这种情况。我们已经看到这种情况发生在多个模拟器和设备上,范围从 21 到 26。此外,这些设备使用滑动解锁或 PIN。更改 PIN 或安全方法似乎不会导致问题。同样,这个问题似乎是在设备闲置几天后出现的。

我找到了另外两个 SO herehere 以及一个 Google issue。如果我理解正确,两者中的答案似乎依赖于调用者在创建密钥时调用setUserAuthenticationValidityDurationSeconds 的前提,而我们没有这样做。此外,给定的解决方案似乎仅依赖于删除密钥并生成新密钥。

下面是我们为版本 >= API 23 设置的密钥。我省略了为 23 之前的版本生成密钥,因为我们主要在 API >= 23 上看到了这一点。

private static final int RSA_KEY_SIZE = 2048;
private static final String CERT_SUBJECT_STRING = "CN=<COMPANY_NAME> Android App O=<COMPANY_NAME>";
private static final String ANDROID_KEY_STORE = "AndroidKeyStore";

try {
        String alias = KEY_NAME;
        KeyPairGenerator generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_KEY_STORE);

        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        end.add(Calendar.YEAR, 1);
        KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(RSA_KEY_SIZE, RSAKeyGenParameterSpec.F4))
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
            .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
            .setCertificateNotAfter(end.getTime())
            .setCertificateNotBefore(start.getTime())
            .setCertificateSerialNumber(BigInteger.ONE)
            .setCertificateSubject(new X500Principal(CERT_SUBJECT_STRING))
            .build();
        generator.initialize(spec);
        generator.generateKeyPair();
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }

我们稍后会尝试通过keyStore.getEntry(KEY_NAME, null) 访问密钥。同样,这会工作一段时间,但随后会开始抛出上述异常。

【问题讨论】:

  • 你找到解决办法了吗?

标签: android private-key android-keystore android-security java-security


【解决方案1】:

我还遇到了 KeyStore 的稳定性问题。

解决方案是使用private key

PrivateKey privKey = ks.getKey(alias, password)

这是公钥

PublicKey pubKey = ks.getCertificate(alias).getPublicKey();

而不是getEntry

ks.getEntry(alias, password)

问题不在于您创建密钥的方式,而在于您阅读密钥的方式。

一年多以来再也没有遇到过这个问题。

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2020-02-07
    • 2019-01-27
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多