Recover unreadable legacy encrypted preferences and migrate to AGP 9 - #7
Open
minh-rakuten wants to merge 6 commits into
Open
Recover unreadable legacy encrypted preferences and migrate to AGP 9#7minh-rakuten wants to merge 6 commits into
minh-rakuten wants to merge 6 commits into
Conversation
EncryptedSharedPreferences.create() was called straight from a lazy with no error handling. When the AndroidKeyStore master key no longer matches the Tink keyset stored in private_pref -- a device restore, a key invalidation -- Tink throws AEADBadTagException and every encrypt and decrypt through this class fails from then on, permanently. Open the store through a three step recovery instead: retry after clearing the keyset, then after replacing the master key. Only failures that positively identify lost key material trigger it, so a locked device or an unavailable keystore daemon still surfaces as an error rather than discarding readable data. Values written with VERSION_AES_KEY_ENCRYPTED_PREFERENCE before a reset are gone, so report them as UnrecoverableCiphertextException and expose wasEncryptedPreferenceReset() for callers that re-derive from a backup. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
Legacy version-4 values keep their AES data keys in
EncryptedSharedPreferences. After a device restore or OEM KeyStore failure, the Android KeyStore key may no longer match that preference keyset, causingAEADBadTagExceptionand repeated crashes while reading old values.Fix
LocalEncryptionKeyLostExceptionwhen a legacy data key is gone.Result
The host app can catch one specific key-loss error and reuse its existing backup repair mechanism instead of crashing or retrying an unrecoverable decryption. Applications using
VERSION_UNENCRYPTEDremain unchanged.