-
Notifications
You must be signed in to change notification settings - Fork 501
SecureStorage Java.Security.InvalidKeyException: javax.crypto.BadPaddingException #564
Comments
So this is randomly not working correctly? |
I had this error some time ago. I think this is caused if the user has enabled backing up data from the app on Google Drive and restoring it during reinstallation of the app. I solved this problem by specifing that I don't want to backup and restore any data from my app on Google Drive. This can be done easily in Android manifest like this:
Source: https://developer.android.com/guide/topics/data/autobackup However, Google recommends separating data which you want to backup and sensitive data which you don't. Data allowed for backup SHOULD NOT be stored in Secure Storage because the symmetric key is changing after every launch of the app, so it can throw the exception from this issue if data is encrypted with an outdated symmetric key. You can read more about that on Android documentation about Autobackup. I hope it helps :) |
I don't want to say randomly but above is the best guess i have. I've been wrong before :) AES key (randomly generated at runtime) and stored in the shared preferences file under the key SecureStorageKey Yesterday i tried fresh installs (x4) on the same device to attempt a re-create but it didn't happen. Ill be able to to test a brand new install in the coming days (new app) and see if happens again. |
Linked to #602 For this if we see an exception occur we will blow away all secure storage keys and re-generate things. I recommend setting up the selective backup; https://docs.microsoft.com/en-us/xamarin/essentials/secure-storage?tabs=android |
Changes merged. |
Xamarin Essentials 0.11.0-preview
Android api 22
Description
Had a bit of an issue on a fresh installing on a physical device which i did not have access to. (Samsung api 22)
Had them uninstall and reinstall and then it worked.
I had put this down to secure storage and was able to reproduce on a HUAWEI SCL-L01
From reading the documentation and the fact a reinstall worked i believe its the random key
Steps to Reproduce
The documentation states
On older API levels, the Android KeyStore only supports storing RSA keys, which is used with an RSA/ECB/PKCS1Padding cipher to encrypt an AES key (randomly generated at runtime) and stored in the shared preferences file under the key SecureStorageKey, if one has not already been generated.
My workaround
So i believed the system was not happy with the key being generated.
what app starts
1: Test if the SecureStorage.SetAsync is working as expected
If not
2: Set Preferences.Set(SecureStorageKey .... i don't want the random generated one
Expected Behaviour
Actual Behaviour
Error thrown - exception below
Basic Information
Exception:
{Java.Security.InvalidKeyException: javax.crypto.BadPaddingException: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error ---> Javax.Crypto.BadPaddingException: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
--- End of inner exception stack trace ---
at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in :0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0001f] in :0
at Javax.Crypto.Cipher.Unwrap (System.Byte[] wrappedKey, System.String wrappedKeyAlgorithm, Javax.Crypto.KeyType wrappedKeyType) [0x00059] in :0
at Xamarin.Essentials.AndroidKeyStore.UnwrapKey (System.Byte[] wrappedData, Java.Security.IKey withKey) [0x00012] in C:\bld\Essentials-Release\Xamarin.Essentials\SecureStorage\SecureStorage.android.cs:229
at Xamarin.Essentials.AndroidKeyStore.GetKey () [0x0005c] in C:\bld\Essentials-Release\Xamarin.Essentials\SecureStorage\SecureStorage.android.cs:127
at Xamarin.Essentials.AndroidKeyStore.Encrypt (System.String data) [0x00000] in C:\bld\Essentials-Release\Xamarin.Essentials\SecureStorage\SecureStorage.android.cs:235
at Xamarin.Essentials.SecureStorage.PlatformSetAsync (System.String key, System.String data) [0x00014] in C:\bld\Essentials-Release\Xamarin.Essentials\SecureStorage\SecureStorage.android.cs:48
at Xamarin.Essentials.SecureStorage.SetAsync (System.String key, System.String value) [0x00021] in ...........ServiceManager+<SecureStorage_SetAsync>d__45.MoveNext () [0x00039] in ........cs:77
--- End of managed Java.Security.InvalidKeyException stack trace ---
java.security.InvalidKeyException: javax.crypto.BadPaddingException: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
at com.android.org.conscrypt.OpenSSLCipherRSA.engineUnwrap(OpenSSLCipherRSA.java:340)
at javax.crypto.Cipher.unwrap(Cipher.java:1545)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: javax.crypto.BadPaddingException: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
at com.android.org.conscrypt.NativeCrypto.RSA_private_decrypt(Native Method)
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:273)
at com.android.org.conscrypt.OpenSSLCipherRSA.engineUnwrap(OpenSSLCipherRSA.java:325)
... 11 more
The text was updated successfully, but these errors were encountered: