Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Crash on App Launch #20

Open
praveenathuru opened this issue Mar 19, 2024 · 3 comments
Open

Getting Crash on App Launch #20

praveenathuru opened this issue Mar 19, 2024 · 3 comments
Labels
third-party Issue caused by third-party.

Comments

@praveenathuru
Copy link

praveenathuru commented Mar 19, 2024

Caused by: java.security.KeyStoreException: the master key android-keystore://_androidx_security_master_key_ exists but is unusable
	at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewMasterKey(AndroidKeysetManager.java:276)
	at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:237)
	at androidx.security.crypto.EncryptedFile$Builder.build(EncryptedFile.java:172)
	at com.huru.datastore.di.DataStoreModule$getDataStore$1.invoke(DataStoreModule.kt:61)
	at com.huru.datastore.di.DataStoreModule$getDataStore$1.invoke(DataStoreModule.kt:55)
	at io.github.osipxd.security.crypto.EncryptedPreferenceDataStoreFactoryKt.createEncrypted(EncryptedPreferenceDataStoreFactory.kt:61)
	at io.github.osipxd.security.crypto.EncryptedPreferenceDataStoreFactoryKt.createEncrypted$default(EncryptedPreferenceDataStoreFactory.kt:48)
@osipxd
Copy link
Owner

osipxd commented Mar 19, 2024

@praveenathuru, thank you for the report. Is it full stacktrace? Are there more causes?

@praveenathuru
Copy link
Author

praveenathuru commented Mar 19, 2024

Not sure just got this one today as I launched my app after 24 hours on Android 9.
I'm maintaining 2 different Data Stores variants in the application one for User and one for App level configuration.
This is how I'm maintaining data stores.

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AppDataStore

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UserDataStore

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UserDataStoreManager

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class AppDataStoreManager

@Module
@InstallIn(SingletonComponent::class)
object DataStoreModule {
    @Provides
    @Singleton
    @UserDataStore
    fun provideUserDataPreference(@ApplicationContext context: Context): DataStore<Preferences> {
        return context.getDataStore(fileName = "user_data.preferences_pb")
    }

    @Provides
    @Singleton
    @AppDataStore
    fun provideAppDataPreference(@ApplicationContext context: Context): DataStore<Preferences> {
        return context.getDataStore(fileName = "app_data.preferences_pb")
    }

    private fun Context.getDataStore(fileName: String): DataStore<Preferences> {
        return PreferenceDataStoreFactory.createEncrypted {
            EncryptedFile.Builder(
                dataStoreFile(fileName = fileName),
                this,
                MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
                EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
            ).build()
        }
    }

    @Provides
    @Singleton
    @UserDataStoreManager
    fun provideUserDataStoreManager(@UserDataStore dataStore: DataStore<Preferences>): DataStoreManager {
        return DataStoreManagerImpl(dataStore)
    }

    @Provides
    @Singleton
    @AppDataStoreManager
    fun provideAppDataStoreManager(@AppDataStore dataStore: DataStore<Preferences>): DataStoreManager {
        return DataStoreManagerImpl(dataStore)
    }
}

@osipxd
Copy link
Owner

osipxd commented Mar 19, 2024

There are many reasons why the library can't decrypt master key, but firstly I'd suggest updating tink (the cryptographic library used under the hood) to the latest version:

dependencies {
    implementation("com.google.crypto.tink:tink-android:1.12.0")
}

If it does not help, look at this issue: tink-crypto/tink#535 (especially this comment and this one)
Similar issue in Google's issuetracker: b/176215143

@osipxd osipxd added the third-party Issue caused by third-party. label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
third-party Issue caused by third-party.
Projects
None yet
Development

No branches or pull requests

2 participants