1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-12-26 16:57:59 +01:00

prefix keychain key with appid

This commit is contained in:
Kyle Spearrin 2019-07-03 20:04:23 -04:00
parent 64506a7080
commit 6c56e44b61
2 changed files with 17 additions and 12 deletions

View File

@ -12,30 +12,33 @@ namespace Bit.iOS.Core.Services
{
public class KeyChainStorageService : IStorageService
{
private readonly string _keyFormat = "bwKeyChainStorage:{0}";
private readonly string _keyFormat = "bwKeyChainStorage:{0}:{1}";
private readonly string _service;
private readonly string _group;
private readonly Func<Task<string>> _getAppId;
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
public KeyChainStorageService(string service, string group)
public KeyChainStorageService(string service, string group, Func<Task<string>> getAppId)
{
_service = service;
_group = group;
_getAppId = getAppId;
}
public Task<T> GetAsync<T>(string key)
public async Task<T> GetAsync<T>(string key)
{
var formattedKey = string.Format(_keyFormat, key);
var appId = await _getAppId.Invoke();
var formattedKey = string.Format(_keyFormat, appId, key);
byte[] dataBytes = null;
using(var existingRecord = GetKeyRecord(formattedKey))
using(var record = SecKeyChain.QueryAsRecord(existingRecord, out SecStatusCode resultCode))
{
if(resultCode == SecStatusCode.ItemNotFound)
{
return Task.FromResult((T)(object)null);
return (T)(object)null;
}
CheckError(resultCode);
@ -45,11 +48,11 @@ namespace Bit.iOS.Core.Services
var dataString = Encoding.UTF8.GetString(dataBytes);
if(typeof(T) == typeof(string))
{
return Task.FromResult((T)(object)dataString);
return (T)(object)dataString;
}
else
{
return Task.FromResult(JsonConvert.DeserializeObject<T>(dataString, _jsonSettings));
return JsonConvert.DeserializeObject<T>(dataString, _jsonSettings);
}
}
@ -71,7 +74,8 @@ namespace Bit.iOS.Core.Services
dataString = JsonConvert.SerializeObject(obj, _jsonSettings);
}
var formattedKey = string.Format(_keyFormat, key);
var appId = await _getAppId.Invoke();
var formattedKey = string.Format(_keyFormat, appId, key);
var dataBytes = Encoding.UTF8.GetBytes(dataString);
using(var data = NSData.FromArray(dataBytes))
using(var newRecord = GetKeyRecord(formattedKey, data))
@ -81,9 +85,10 @@ namespace Bit.iOS.Core.Services
}
}
public Task RemoveAsync(string key)
public async Task RemoveAsync(string key)
{
var formattedKey = string.Format(_keyFormat, key);
var appId = await _getAppId.Invoke();
var formattedKey = string.Format(_keyFormat, appId, key);
using(var record = GetExistingRecord(formattedKey))
{
if(record != null)
@ -91,7 +96,6 @@ namespace Bit.iOS.Core.Services
CheckError(SecKeyChain.Remove(record));
}
}
return Task.FromResult(0);
}
private SecRecord GetKeyRecord(string key, NSData data = null)

View File

@ -53,7 +53,8 @@ namespace Bit.iOS.Core.Utilities
var broadcasterService = new BroadcasterService();
var messagingService = new MobileBroadcasterMessagingService(broadcasterService);
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
var secureStorageService = new KeyChainStorageService(AppId, AccessGroup);
var secureStorageService = new KeyChainStorageService(AppId, AccessGroup,
() => ServiceContainer.Resolve<IAppIdService>("appIdService").GetAppIdAsync());
var cryptoPrimitiveService = new CryptoPrimitiveService();
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);