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

native_storage issue: Unhandled Exception: UnknownException: Exception in Java code called through JNI: javax.crypto.AEADBadTagException #28

Open
sixtusagbo opened this issue Aug 24, 2024 · 4 comments

Comments

@sixtusagbo
Copy link

sixtusagbo commented Aug 24, 2024

I noticed this error after updating Flutter to the latest version...
IsolatedNativeStorage from storage.secure.isolated is not persisting data as expected:

E/flutter (11172): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: UnknownException: Exception in Java code called through JNI: javax.crypto.AEADBadTagException
E/flutter (11172): 
E/flutter (11172): javax.crypto.AEADBadTagException
E/flutter (11172): 	at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:617)
E/flutter (11172): 	at javax.crypto.Cipher.doFinal(Cipher.java:2114)
E/flutter (11172): 	at com.google.crypto.tink.integration.android.AndroidKeystoreAesGcm.decryptInternal(AndroidKeystoreAesGcm.java:118)
E/flutter (11172): 	at com.google.crypto.tink.integration.android.AndroidKeystoreAesGcm.decrypt(AndroidKeystoreAesGcm.java:101)
E/flutter (11172): 	at com.google.crypto.tink.KeysetHandle.decrypt(KeysetHandle.java:919)
E/flutter (11172): 	at com.google.crypto.tink.KeysetHandle.readWithAssociatedData(KeysetHandle.java:804)
E/flutter (11172): 	at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:785)
E/flutter (11172): 	at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readMasterkeyDecryptAndParseKeyset(AndroidKeysetManager.java:381)
E/flutter (11172): 	at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:297)
E/flutter (11172): 	at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:169)
E/flutter (11172): 	at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:130)
E/flutter (11172): 	at dev.celest.native_storage.NativeSecureStorage$sharedPreferences$2.invoke(NativeSecureStorage.kt:23)
E/flutter (11172): 	at dev.celest.native_storage.NativeSecureStorage$sharedPreferences$2.invoke(NativeSecureStorage.kt:19)
E/flutter (11172): 	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
E/flutter (11172): 	at dev.celest.native_storage.NativeSecureStorage.getSharedPreferences(NativeSecureStorage.kt:19)
E/flutter (11172): 	at dev.celest.native_storage.NativeStorage.getEditor(NativeStorage.kt:28)
E/flutter (11172): 	at dev.celest.native_storage.NativeStorage.write(NativeStorage.kt:36)
E/flutter (11172): Caused by: android.security.KeyStoreException: Signature/MAC verification failed (internal Keystore code: -30 message: In KeystoreOperation::finish
E/flutter (11172): 
E/flutter (11172): Caused by:
E/flutter (11172):     0: In finish: KeyMint::finish failed.
E/flutter (11172):     1: Error::Km(ErrorCode(-30))) (public error code: 10 internal Keystore code: -30)
E/flutter (11172): 	at android.security.KeyStore2.getKeyStoreException(KeyStore2.java:418)
E/flutter (11172): 	at android.security.KeyStoreOperation.handleExceptions(KeyStoreOperation.java:78)
E/flutter (11172): 	at android.security.KeyStoreOperation.finish(KeyStoreOperation.java:128)
E/flutter (11172): 	at android.security.keystore2.KeyStoreCryptoOperationChunkedStreamer$MainDataStream.finish(KeyStoreCryptoOperationChunkedStreamer.java:228)
E/flutter (11172): 	at android.security.keystore2.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:181)
E/flutter (11172): 	at android.security.keystore2.AndroidKeyStoreAuthenticatedAESCipherSpi$BufferAllOutputUntilDoFinalStreamer.doFinal(AndroidKeyStoreAuthenticatedAESCipherSpi.java:396)
E/flutter (11172): 	at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:609)
E/flutter (11172): 	... 16 more
2 E/flutter (11172): 
E/flutter (11172): package:jni/src/accessors.dart 119:5                                          JniAccessorWrappers.throwException
E/flutter (11172): package:jni/src/accessors.dart 11:19                                          _check
E/flutter (11172): package:jni/src/accessors.dart 102:5                                          JThrowableCheckMethod.check
E/flutter (11172): package:native_storage/src/native/android/jni_bindings.ffi.dart 164:10        NativeStorage.write
E/flutter (11172): package:native_storage/src/secure/secure_storage.android.dart 42:14           SecureStorageAndroid.write
E/flutter (11172): package:native_storage/src/isolated/isolated_storage_platform.vm.dart 225:30  _extension#1.handle
E/flutter (11172): package:native_storage/src/isolated/isolated_storage_platform.vm.dart 104:32  IsolatedNativeStoragePlatform._handleRequests
E/flutter (11172): ===== asynchronous gap ===========================
E/flutter (11172): package:native_storage/src/isolated/isolated_storage_request.dart 58:34       IsolatedStorageRequest.unwrap
E/flutter (11172): package:native_storage/src/isolated/isolated_storage_platform.vm.dart 127:23  IsolatedNativeStoragePlatform._send
E/flutter (11172): package:native_storage/src/isolated/isolated_storage_platform.vm.dart 150:26  IsolatedNativeStoragePlatform.write
E/flutter (11172): 

@rubenferreira97
Copy link

Look celest-dev/celest#156 for more context. @dnys1 I think there was no dart-packages when I filled that issue. Maybe move it here?

@sixtusagbo
Copy link
Author

One weird thing though, it works as expected on Android 12 emulator.
I encountered that error why running it on an Android 13 physical device.
I just tested it on both to confirm.

@dnys1
Copy link
Member

dnys1 commented Sep 2, 2024

Seems like it may be due to backing up of the shared preferences file tink-crypto/tink-java#23

I have a TODO for excluding the items from backup. Will look into it 👍

@Judimax
Copy link

Judimax commented Oct 26, 2024

Is this fixed I am interested in using the package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants