celest-dev / dart-packages

A collection of useful Dart packages
Other
5 stars 0 forks source link

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

Open sixtusagbo opened 3 months ago

sixtusagbo commented 3 months ago

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 commented 2 months ago

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

sixtusagbo commented 2 months ago

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 commented 2 months ago

Seems like it may be due to backing up of the shared preferences file https://github.com/tink-crypto/tink-java/issues/23

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

Judimax commented 1 month ago

Is this fixed I am interested in using the package