Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.
Merged
Changes from 3 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
91 changes: 32 additions & 59 deletions Xamarin.Essentials/SecureStorage/SecureStorage.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ static Task<string> PlatformGetAsync(string key)
{
var context = Platform.AppContext;

string encStr;
using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
encStr = prefs.GetString(Utils.Md5Hash(key), null);
string defaultEncStr = null;
var encStr = Preferences.Get(Utils.Md5Hash(key), defaultEncStr, Alias);

string decryptedData = null;
if (!string.IsNullOrEmpty(encStr))
Expand All @@ -40,13 +39,8 @@ static Task PlatformSetAsync(string key, string data)
var ks = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
var encryptedData = ks.Encrypt(data);

using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
using (var prefsEditor = prefs.Edit())
{
var encStr = Convert.ToBase64String(encryptedData);
prefsEditor.PutString(Utils.Md5Hash(key), encStr);
prefsEditor.Commit();
}
var encStr = Convert.ToBase64String(encryptedData);
Preferences.Set(Utils.Md5Hash(key), encStr, Alias);

return Task.CompletedTask;
}
Expand All @@ -56,36 +50,13 @@ static bool PlatformRemove(string key)
var context = Platform.AppContext;

key = Utils.Md5Hash(key);

using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
{
if (prefs.Contains(key))
{
using (var prefsEditor = prefs.Edit())
{
prefsEditor.Remove(key);
prefsEditor.Commit();
return true;
}
}
}
Preferences.Remove(key, Alias);

return false;
}

static void PlatformRemoveAll()
{
var context = Platform.AppContext;

using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
using (var prefsEditor = prefs.Edit())
{
foreach (var key in prefs.All.Keys)
prefsEditor.Remove(key);

prefsEditor.Commit();
}
}
static void PlatformRemoveAll() =>
Preferences.Clear(Alias);

internal static bool AlwaysUseAsymmetricKeyStorage { get; set; } = false;
}
Expand Down Expand Up @@ -114,10 +85,16 @@ internal AndroidKeyStore(Context context, string keystoreAlias, bool alwaysUseAs
KeyStore keyStore;
bool alwaysUseAsymmetricKey;

bool isPreM = false;
string isPreMKey = "is_pre_m";

ISecretKey GetKey()
{
// check to see if we need to get our key from past-versions or newer versions.
isPreM = Preferences.Get(isPreMKey, !Platform.HasApiLevel(BuildVersionCodes.M), SecureStorage.Alias);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch with the ! there


// If >= API 23 we can use the KeyStore's symmetric key
if (Platform.HasApiLevel(BuildVersionCodes.M) && !alwaysUseAsymmetricKey)
if (!isPreM && !alwaysUseAsymmetricKey)
return GetSymmetricKey();

// NOTE: KeyStore in < API 23 can only store asymmetric keys
Expand All @@ -131,34 +108,27 @@ ISecretKey GetKey()
// Get the asymmetric key pair
var keyPair = GetAsymmetricKeyPair();

using (var prefs = appContext.GetSharedPreferences(alias, FileCreationMode.Private))
{
var existingKeyStr = prefs.GetString(prefsMasterKey, null);
var existingKeyStr = Preferences.Get(prefsMasterKey, null, alias);

if (!string.IsNullOrEmpty(existingKeyStr))
{
var wrappedKey = Convert.FromBase64String(existingKeyStr);
if (!string.IsNullOrEmpty(existingKeyStr))
{
var wrappedKey = Convert.FromBase64String(existingKeyStr);

var unwrappedKey = UnwrapKey(wrappedKey, keyPair.Private);
var kp = unwrappedKey.JavaCast<ISecretKey>();
var unwrappedKey = UnwrapKey(wrappedKey, keyPair.Private);
var kp = unwrappedKey.JavaCast<ISecretKey>();

return kp;
}
else
{
var keyGenerator = KeyGenerator.GetInstance(aesAlgorithm);
var defSymmetricKey = keyGenerator.GenerateKey();
return kp;
}
else
{
var keyGenerator = KeyGenerator.GetInstance(aesAlgorithm);
var defSymmetricKey = keyGenerator.GenerateKey();

var wrappedKey = WrapKey(defSymmetricKey, keyPair.Public);
var wrappedKey = WrapKey(defSymmetricKey, keyPair.Public);

using (var prefsEditor = prefs.Edit())
{
prefsEditor.PutString(prefsMasterKey, Convert.ToBase64String(wrappedKey));
prefsEditor.Commit();
}
Preferences.Set(prefsMasterKey, Convert.ToBase64String(wrappedKey), alias);

return defSymmetricKey;
}
return defSymmetricKey;
}
}

Expand Down Expand Up @@ -213,6 +183,9 @@ KeyPair GetAsymmetricKeyPair()
generator.Initialize(builder.Build());
#pragma warning restore CS0618

// set that we generated keys on pre-m device.
Preferences.Set(isPreMKey, true, SecureStorage.Alias);

return generator.GenerateKeyPair();
}

Expand Down