1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-04 08:50:18 +01:00

keystore fixes

This commit is contained in:
Kyle Spearrin 2017-05-27 12:23:35 -04:00
parent 72f9951cb1
commit f7f4289614

View File

@ -33,8 +33,7 @@ namespace Bit.Android.Services
public KeyStoreBackedStorageService(ISettings settings) public KeyStoreBackedStorageService(ISettings settings)
{ {
_oldKeyStorageService = new KeyStoreStorageService(); _oldKeyStorageService = new KeyStoreStorageService(new char[] { });
_settings = settings; _settings = settings;
_keyStore = KeyStore.GetInstance(AndroidKeyStore); _keyStore = KeyStore.GetInstance(AndroidKeyStore);
@ -46,7 +45,7 @@ namespace Bit.Android.Services
public bool Contains(string key) public bool Contains(string key)
{ {
return _settings.Contains(string.Format(SettingsFormat, key)); return _settings.Contains(string.Format(SettingsFormat, key)) || _oldKeyStorageService.Contains(key);
} }
public void Delete(string key) public void Delete(string key)
@ -57,26 +56,28 @@ namespace Bit.Android.Services
public byte[] Retrieve(string key) public byte[] Retrieve(string key)
{ {
if(!_settings.Contains(key)) var formattedKey = string.Format(SettingsFormat, key);
if(!_settings.Contains(formattedKey))
{ {
return TryGetAndMigrateFromOldKeyStore(key); return TryGetAndMigrateFromOldKeyStore(key);
} }
var cipherString = _settings.GetValueOrDefault<string>(string.Format(SettingsFormat, key)); var cipherString = _settings.GetValueOrDefault<string>(formattedKey);
return AesDecrypt(cipherString); return AesDecrypt(cipherString);
} }
public void Store(string key, byte[] dataBytes) public void Store(string key, byte[] dataBytes)
{ {
var formattedKey = string.Format(SettingsFormat, key);
CleanupOldKeyStore(key); CleanupOldKeyStore(key);
if(dataBytes == null) if(dataBytes == null)
{ {
_settings.Remove(key); _settings.Remove(formattedKey);
return; return;
} }
var cipherString = AesEncrypt(dataBytes); var cipherString = AesEncrypt(dataBytes);
_settings.AddOrUpdateValue(key, cipherString); _settings.AddOrUpdateValue(formattedKey, cipherString);
} }
private byte[] RandomBytes(int length) private byte[] RandomBytes(int length)
@ -160,10 +161,11 @@ namespace Bit.Android.Services
private string AesEncrypt(byte[] input) private string AesEncrypt(byte[] input)
{ {
var cipher = Cipher.GetInstance(AesMode); var cipher = Cipher.GetInstance(AesMode);
var ivBytes = RandomBytes(12); //var ivBytes = RandomBytes(12);
var spec = new GCMParameterSpec(128, ivBytes); //var spec = new GCMParameterSpec(128, ivBytes);
cipher.Init(CipherMode.EncryptMode, GetAesKey(), spec); cipher.Init(CipherMode.EncryptMode, GetAesKey());
var encBytes = cipher.DoFinal(input); var encBytes = cipher.DoFinal(input);
var ivBytes = cipher.GetIV();
return $"{Convert.ToBase64String(ivBytes)}|{Convert.ToBase64String(encBytes)}"; return $"{Convert.ToBase64String(ivBytes)}|{Convert.ToBase64String(encBytes)}";
} }