From 6c56e44b6159e8e2ea9d0390320db2d377ffb43b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 3 Jul 2019 20:04:23 -0400 Subject: [PATCH] prefix keychain key with appid --- .../Services/KeyChainStorageService.cs | 26 +++++++++++-------- src/iOS.Core/Utilities/iOSCoreHelpers.cs | 3 ++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/iOS.Core/Services/KeyChainStorageService.cs b/src/iOS.Core/Services/KeyChainStorageService.cs index dbf7ecab6..3f4c50b31 100644 --- a/src/iOS.Core/Services/KeyChainStorageService.cs +++ b/src/iOS.Core/Services/KeyChainStorageService.cs @@ -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> _getAppId; private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; - public KeyChainStorageService(string service, string group) + public KeyChainStorageService(string service, string group, Func> getAppId) { _service = service; _group = group; + _getAppId = getAppId; } - public Task GetAsync(string key) + public async Task GetAsync(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(dataString, _jsonSettings)); + return JsonConvert.DeserializeObject(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) diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index ce63d5a4b..0a939ee53 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -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("appIdService").GetAppIdAsync()); var cryptoPrimitiveService = new CryptoPrimitiveService(); var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage); var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);