migrate and cleanup old key store

This commit is contained in:
Kyle Spearrin 2017-05-27 11:42:22 -04:00
parent a028172cf6
commit cb2a25ad46
2 changed files with 30 additions and 4 deletions

View file

@ -189,7 +189,7 @@ namespace Bit.Android
// Services
.RegisterType<IDatabaseService, DatabaseService>(new ContainerControlledLifetimeManager())
.RegisterType<ISqlService, SqlService>(new ContainerControlledLifetimeManager())
.RegisterType<ISecureStorageService, KeyStoreStorageService>(new ContainerControlledLifetimeManager())
.RegisterType<ISecureStorageService, KeyStoreBackedStorageService>(new ContainerControlledLifetimeManager())
.RegisterType<ICryptoService, CryptoService>(new ContainerControlledLifetimeManager())
.RegisterType<IKeyDerivationService, BouncyCastleKeyDerivationService>(new ContainerControlledLifetimeManager())
.RegisterType<IAuthService, AuthService>(new ContainerControlledLifetimeManager())

View file

@ -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>(string.Format(SettingsFormat, key));
if(cipherString == null)
if(!_settings.Contains(key))
{
return null;
return TryGetAndMigrateFromOldKeyStore(key);
}
var cipherString = _settings.GetValueOrDefault<string>(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);
}
}
}
}