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

[Android] SecureStorage: Rework logic to delete shared prefs when key is corrupt #23850

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions src/Essentials/src/SecureStorage/SecureStorage.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Android.Content;
using AndroidX.Security.Crypto;
using Java.Security;
using Javax.Crypto;
using Xamarin.Google.Crypto.Tink.Shaded.Protobuf;

namespace Microsoft.Maui.Storage
Expand Down Expand Up @@ -80,31 +81,62 @@ void PlatformRemoveAll()
editor?.Clear()?.Apply();
}

static void DeleteSharedPreferences()
{
// Open an editor to the preferences we can clear, using the alias for storing encrypted values
var editPreferences = Application.Context.GetSharedPreferences(Alias, FileCreationMode.Private).Edit();
// Commit is synchronous here so we can be sure it's done before trying to create the encrypted preferences again
editPreferences?.Clear()?.Commit();
}

ISharedPreferences GetEncryptedSharedPreferences()
{
try
{
var context = Application.Context;

var prefsMainKey = new MasterKey.Builder(context, Alias)
.SetKeyScheme(MasterKey.KeyScheme.Aes256Gcm)
.Build();

return EncryptedSharedPreferences.Create(
context,
Alias,
prefsMainKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.Aes256Siv,
EncryptedSharedPreferences.PrefValueEncryptionScheme.Aes256Gcm);
return CreateEncryptedSharedPreferences();
}
catch (InvalidProtocolBufferException)
catch (System.Exception ex)
when (ex is InvalidProtocolBufferException or Android.Security.KeyStoreException or KeyStoreException or BadPaddingException)
{
// TODO: Use Logger here?
System.Diagnostics.Debug.WriteLine(
"Unable get encrypted shared preferences, which is likely due to an app uninstall. Removing all keys and returning null.");
PlatformRemoveAll();
// If we encounter any of these exceptions, it's likely due to a corrupt key or bad migration between devices
// There isn't much to do at this point except try to delete the shared preferences so we can recreate them
try
{
System.Diagnostics.Debug.WriteLine(
"Unable get encrypted shared preferences, which is likely due to corrupt encryption key or bad app cache backup/restore. Removing all keys and returning null.");
System.Diagnostics.Debug.WriteLine(ex);

// Delete the shared preferences
DeleteSharedPreferences();

// Try to return a new instance now that we've deleted the old
return CreateEncryptedSharedPreferences();
}
catch (System.Exception ex2)
{
// If we still can't create things, we'll have to give up and return null
// TODO: Use Logger here?
System.Diagnostics.Debug.WriteLine("Still unable to create encrypted shared preferences after attempting to deleting them. Returning null.");
System.Diagnostics.Debug.WriteLine(ex2);
}
return null;
}
}

ISharedPreferences CreateEncryptedSharedPreferences()
{
var context = Application.Context;

var prefsMainKey = new MasterKey.Builder(context, Alias)
.SetKeyScheme(MasterKey.KeyScheme.Aes256Gcm)
.Build();

return EncryptedSharedPreferences.Create(
context,
Alias,
prefsMainKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.Aes256Siv,
EncryptedSharedPreferences.PrefValueEncryptionScheme.Aes256Gcm);
}
}
}
Loading