androidx-releases / Security

Apache License 2.0
0 stars 0 forks source link

Caused by java.security.InvalidKeyException: Keystore operation failed #1

Open ameerprpt opened 8 months ago

ameerprpt commented 8 months ago

I am getting InvalidKeyException when using security library.

We are using Koin library to inject EncryptedSharedPreferences. Below is the code to generate the SharedPreferences.

private fun createEncryptedSharedPreferences(context: Context): SharedPreferences {
    return EncryptedSharedPreferences.create(
        context,
        Constants.PREFERENCE_FILE_NAME,
        MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(),
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    )
}

Library version : androidx.security:security-crypto:1.1.0-alpha06

Devices affected: Pixel 5, Pixel 4A Below is the crash log:

Caused by yf.c: Could not create instance for [Singleton:'android.content.SharedPreferences']
       at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:61)
       at org.koin.core.instance.SingleInstanceFactory.create(SingleInstanceFactory.kt:46)
       at org.koin.core.instance.SingleInstanceFactory$get$1.invoke(SingleInstanceFactory.kt:53)
       at org.koin.core.instance.SingleInstanceFactory$get$1.invoke(SingleInstanceFactory.kt:51)
       at org.koin.mp.KoinPlatformTools.synchronized(KoinPlatformTools.kt:20)
       at org.koin.core.instance.SingleInstanceFactory.get(SingleInstanceFactory.kt:51)
       at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:111)

Caused by java.security.InvalidKeyException: Keystore operation failed
       at android.security.keystore2.KeyStoreCryptoOperationUtils.getInvalidKeyException(KeyStoreCryptoOperationUtils.java:128)
       at android.security.keystore2.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:152)
       at android.security.keystore2.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:354)
       at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:185)
       at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2985)
       at javax.crypto.Cipher.tryCombinations(Cipher.java:2892)
       at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2797)

Caused by android.security.KeyStoreException: -22 (internal Keystore code: -22 message: system/security/keystore2/src/security_level.rs:358: Failed to begin operation.

Caused by:
    0: system/security/keystore2/src/security_level.rs:868
    1: system/security/keystore2/src/utils.rs:200: Calling km_op.
    2: Error::Km(r#KEY_EXPORT_OPTIONS_INVALID))
       at android.security.KeyStore2.getKeyStoreException(KeyStore2.java:386)
       at android.security.KeyStoreSecurityLevel.createOperation(KeyStoreSecurityLevel.java:120)
       at android.security.keystore2.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:349)
       at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:185)
       at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2985)
       at javax.crypto.Cipher.tryCombinations(Cipher.java:2892)
       at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAn
dazza5000 commented 4 months ago

We are experiencing this same issue when trying to instantiate dependencies using koin on app init.

ameerprpt commented 4 months ago

This library has been deprecated.