From cb2a25ad469ff25729d61ebbb27f5b02c729a5d6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 27 May 2017 11:42:22 -0400 Subject: [PATCH] migrate and cleanup old key store --- src/Android/MainApplication.cs | 2 +- .../Services/KeyStoreBackedStorageService.cs | 32 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index c95bbb701..0f5c96c82 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -189,7 +189,7 @@ namespace Bit.Android // Services .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) - .RegisterType(new ContainerControlledLifetimeManager()) + .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) diff --git a/src/Android/Services/KeyStoreBackedStorageService.cs b/src/Android/Services/KeyStoreBackedStorageService.cs index 004fd39ef..e62145365 100644 --- a/src/Android/Services/KeyStoreBackedStorageService.cs +++ b/src/Android/Services/KeyStoreBackedStorageService.cs @@ -29,9 +29,12 @@ namespace Bit.Android.Services private readonly ISettings _settings; private readonly KeyStore _keyStore; private readonly bool _oldAndroid = Build.VERSION.SdkInt < BuildVersionCodes.M; + private readonly KeyStoreStorageService _oldKeyStorageService; public KeyStoreBackedStorageService(ISettings settings) { + _oldKeyStorageService = new KeyStoreStorageService(); + _settings = settings; _keyStore = KeyStore.GetInstance(AndroidKeyStore); @@ -48,22 +51,24 @@ namespace Bit.Android.Services public void Delete(string key) { + CleanupOldKeyStore(key); _settings.Remove(string.Format(SettingsFormat, key)); } public byte[] Retrieve(string key) { - var cipherString = _settings.GetValueOrDefault(string.Format(SettingsFormat, key)); - if(cipherString == null) + if(!_settings.Contains(key)) { - return null; + return TryGetAndMigrateFromOldKeyStore(key); } + var cipherString = _settings.GetValueOrDefault(string.Format(SettingsFormat, key)); return AesDecrypt(cipherString); } public void Store(string key, byte[] dataBytes) { + CleanupOldKeyStore(key); if(dataBytes == null) { _settings.Remove(key); @@ -218,5 +223,26 @@ namespace Bit.Android.Services return bytes; } + + private byte[] TryGetAndMigrateFromOldKeyStore(string key) + { + if(_oldKeyStorageService.Contains(key)) + { + var value = _oldKeyStorageService.Retrieve(key); + Store(key, value); + _oldKeyStorageService.Delete(key); + return value; + } + + return null; + } + + private void CleanupOldKeyStore(string key) + { + if(_oldKeyStorageService.Contains(key)) + { + _oldKeyStorageService.Delete(key); + } + } } } \ No newline at end of file