From 27e0c7421b287133640af604c4b560c28204cd90 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 22 Apr 2017 14:36:31 -0400 Subject: [PATCH] rename CryptoKey to SymmetricCryptoKey --- src/App/Abstractions/Services/IAuthService.cs | 2 +- .../Abstractions/Services/ICryptoService.cs | 24 ++++----- src/App/App.csproj | 2 +- src/App/Models/LoginResult.cs | 2 +- .../{CryptoKey.cs => SymmetricCryptoKey.cs} | 4 +- src/App/Pages/LoginTwoFactorPage.cs | 4 +- src/App/Services/AuthService.cs | 8 +-- src/App/Services/CryptoService.cs | 49 ++++++++++--------- src/App/Services/SyncService.cs | 4 +- 9 files changed, 50 insertions(+), 49 deletions(-) rename src/App/Models/{CryptoKey.cs => SymmetricCryptoKey.cs} (93%) diff --git a/src/App/Abstractions/Services/IAuthService.cs b/src/App/Abstractions/Services/IAuthService.cs index ebaf71c5a..39cb636a6 100644 --- a/src/App/Abstractions/Services/IAuthService.cs +++ b/src/App/Abstractions/Services/IAuthService.cs @@ -15,6 +15,6 @@ namespace Bit.App.Abstractions bool BelongsToOrganization(string orgId); void LogOut(); Task TokenPostAsync(string email, string masterPassword); - Task TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, CryptoKey key); + Task TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, SymmetricCryptoKey key); } } diff --git a/src/App/Abstractions/Services/ICryptoService.cs b/src/App/Abstractions/Services/ICryptoService.cs index 934141674..338e361e5 100644 --- a/src/App/Abstractions/Services/ICryptoService.cs +++ b/src/App/Abstractions/Services/ICryptoService.cs @@ -6,24 +6,24 @@ namespace Bit.App.Abstractions { public interface ICryptoService { - CryptoKey Key { get; set; } - CryptoKey PreviousKey { get; } + SymmetricCryptoKey Key { get; set; } + SymmetricCryptoKey PreviousKey { get; } bool KeyChanged { get; } byte[] PrivateKey { get; } - IDictionary OrgKeys { get; set; } + IDictionary OrgKeys { get; set; } - void SetPrivateKey(CipherString privateKeyEnc, CryptoKey key); - CryptoKey GetOrgKey(string orgId); + void SetPrivateKey(CipherString privateKeyEnc, SymmetricCryptoKey key); + SymmetricCryptoKey GetOrgKey(string orgId); void ClearOrgKey(string orgId); void ClearKeys(); - CryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey); - string Decrypt(CipherString encyptedValue, CryptoKey key = null); - byte[] DecryptToBytes(CipherString encyptedValue, CryptoKey key = null); + SymmetricCryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey); + string Decrypt(CipherString encyptedValue, SymmetricCryptoKey key = null); + byte[] DecryptToBytes(CipherString encyptedValue, SymmetricCryptoKey key = null); byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey); - CipherString Encrypt(string plaintextValue, CryptoKey key = null); - CryptoKey MakeKeyFromPassword(string password, string salt); + CipherString Encrypt(string plaintextValue, SymmetricCryptoKey key = null); + SymmetricCryptoKey MakeKeyFromPassword(string password, string salt); string MakeKeyFromPasswordBase64(string password, string salt); - byte[] HashPassword(CryptoKey key, string password); - string HashPasswordBase64(CryptoKey key, string password); + byte[] HashPassword(SymmetricCryptoKey key, string password); + string HashPasswordBase64(SymmetricCryptoKey key, string password); } } \ No newline at end of file diff --git a/src/App/App.csproj b/src/App/App.csproj index 1dc0c80eb..a84979ddc 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -112,7 +112,7 @@ - + diff --git a/src/App/Models/LoginResult.cs b/src/App/Models/LoginResult.cs index 18f2c7a44..be9209ceb 100644 --- a/src/App/Models/LoginResult.cs +++ b/src/App/Models/LoginResult.cs @@ -9,7 +9,7 @@ public class FullLoginResult : LoginResult { public bool TwoFactorRequired { get; set; } - public CryptoKey Key { get; set; } + public SymmetricCryptoKey Key { get; set; } public string MasterPasswordHash { get; set; } } } diff --git a/src/App/Models/CryptoKey.cs b/src/App/Models/SymmetricCryptoKey.cs similarity index 93% rename from src/App/Models/CryptoKey.cs rename to src/App/Models/SymmetricCryptoKey.cs index f2671d247..f015fd9de 100644 --- a/src/App/Models/CryptoKey.cs +++ b/src/App/Models/SymmetricCryptoKey.cs @@ -4,9 +4,9 @@ using System.Linq; namespace Bit.App.Models { - public class CryptoKey + public class SymmetricCryptoKey { - public CryptoKey(byte[] rawBytes, EncryptionType? encType = null) + public SymmetricCryptoKey(byte[] rawBytes, EncryptionType? encType = null) { if(rawBytes == null || rawBytes.Length == 0) { diff --git a/src/App/Pages/LoginTwoFactorPage.cs b/src/App/Pages/LoginTwoFactorPage.cs index 87947919a..0220d8a7d 100644 --- a/src/App/Pages/LoginTwoFactorPage.cs +++ b/src/App/Pages/LoginTwoFactorPage.cs @@ -20,9 +20,9 @@ namespace Bit.App.Pages private IPushNotification _pushNotification; private readonly string _email; private readonly string _masterPasswordHash; - private readonly CryptoKey _key; + private readonly SymmetricCryptoKey _key; - public LoginTwoFactorPage(string email, string masterPasswordHash, CryptoKey key) + public LoginTwoFactorPage(string email, string masterPasswordHash, SymmetricCryptoKey key) : base(updateActivity: false) { _email = email; diff --git a/src/App/Services/AuthService.cs b/src/App/Services/AuthService.cs index 79176ba7e..2de4bfba7 100644 --- a/src/App/Services/AuthService.cs +++ b/src/App/Services/AuthService.cs @@ -245,7 +245,7 @@ namespace Bit.App.Services } public async Task TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, - CryptoKey key) + SymmetricCryptoKey key) { var result = new LoginResult(); @@ -271,7 +271,7 @@ namespace Bit.App.Services return result; } - private async Task ProcessLoginSuccessAsync(CryptoKey key, TokenResponse response) + private async Task ProcessLoginSuccessAsync(SymmetricCryptoKey key, TokenResponse response) { if(response.PrivateKey != null) { @@ -288,7 +288,7 @@ namespace Bit.App.Services if(response.PrivateKey != null) { var profile = await _accountsApiRepository.GetProfileAsync(); - var orgKeysDict = new Dictionary(); + var orgKeysDict = new Dictionary(); if(profile.Succeeded && (profile.Result.Organizations?.Any() ?? false)) { @@ -297,7 +297,7 @@ namespace Bit.App.Services try { var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null); - orgKeysDict.Add(org.Id, new CryptoKey(decBytes)); + orgKeysDict.Add(org.Id, new SymmetricCryptoKey(decBytes)); } catch { diff --git a/src/App/Services/CryptoService.cs b/src/App/Services/CryptoService.cs index 974663e7c..46d191b29 100644 --- a/src/App/Services/CryptoService.cs +++ b/src/App/Services/CryptoService.cs @@ -21,10 +21,10 @@ namespace Bit.App.Services private readonly ISecureStorageService _secureStorage; private readonly IKeyDerivationService _keyDerivationService; - private CryptoKey _key; - private CryptoKey _legacyEtmKey; - private CryptoKey _previousKey; - private IDictionary _orgKeys; + private SymmetricCryptoKey _key; + private SymmetricCryptoKey _legacyEtmKey; + private SymmetricCryptoKey _previousKey; + private IDictionary _orgKeys; private byte[] _privateKey; public CryptoService( @@ -35,7 +35,7 @@ namespace Bit.App.Services _keyDerivationService = keyDerivationService; } - public CryptoKey Key + public SymmetricCryptoKey Key { get { @@ -44,7 +44,7 @@ namespace Bit.App.Services var keyBytes = _secureStorage.Retrieve(KeyKey); if(keyBytes != null) { - _key = new CryptoKey(keyBytes); + _key = new SymmetricCryptoKey(keyBytes); } } @@ -66,7 +66,7 @@ namespace Bit.App.Services } } - public CryptoKey PreviousKey + public SymmetricCryptoKey PreviousKey { get { @@ -75,7 +75,7 @@ namespace Bit.App.Services var keyBytes = _secureStorage.Retrieve(PreviousKeyKey); if(keyBytes != null) { - _previousKey = new CryptoKey(keyBytes); + _previousKey = new SymmetricCryptoKey(keyBytes); } } @@ -135,7 +135,7 @@ namespace Bit.App.Services } } - public IDictionary OrgKeys + public IDictionary OrgKeys { get { @@ -147,11 +147,11 @@ namespace Bit.App.Services var orgKeysDictJson = Encoding.UTF8.GetString(orgKeysDictBytes, 0, orgKeysDictBytes.Length); if(!string.IsNullOrWhiteSpace(orgKeysDictJson)) { - _orgKeys = new Dictionary(); + _orgKeys = new Dictionary(); var orgKeysDict = JsonConvert.DeserializeObject>(orgKeysDictJson); foreach(var item in orgKeysDict) { - _orgKeys.Add(item.Key, new CryptoKey(item.Value)); + _orgKeys.Add(item.Key, new SymmetricCryptoKey(item.Value)); } } } @@ -182,13 +182,13 @@ namespace Bit.App.Services } } - public void SetPrivateKey(CipherString privateKeyEnc, CryptoKey key) + public void SetPrivateKey(CipherString privateKeyEnc, SymmetricCryptoKey key) { var bytes = DecryptToBytes(privateKeyEnc, key); PrivateKey = bytes; } - public CryptoKey GetOrgKey(string orgId) + public SymmetricCryptoKey GetOrgKey(string orgId) { if(OrgKeys == null || !OrgKeys.ContainsKey(orgId)) { @@ -218,13 +218,13 @@ namespace Bit.App.Services PrivateKey = null; } - public CryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey) + public SymmetricCryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey) { try { var localOrgKeys = OrgKeys; var decBytes = RsaDecryptToBytes(encOrgKey, privateKey); - var key = new CryptoKey(decBytes); + var key = new SymmetricCryptoKey(decBytes); if(localOrgKeys.ContainsKey(orgId)) { localOrgKeys[orgId] = key; @@ -245,7 +245,7 @@ namespace Bit.App.Services } } - public CipherString Encrypt(string plaintextValue, CryptoKey key = null) + public CipherString Encrypt(string plaintextValue, SymmetricCryptoKey key = null) { if(key == null) { @@ -270,10 +270,11 @@ namespace Bit.App.Services var encryptedBytes = WinRTCrypto.CryptographicEngine.Encrypt(cryptoKey, plaintextBytes, iv); var mac = key.MacKey != null ? ComputeMac(encryptedBytes, iv, key.MacKey) : null; - return new CipherString(key.EncryptionType, Convert.ToBase64String(iv), Convert.ToBase64String(encryptedBytes), mac); + return new CipherString(key.EncryptionType, Convert.ToBase64String(iv), + Convert.ToBase64String(encryptedBytes), mac); } - public string Decrypt(CipherString encyptedValue, CryptoKey key = null) + public string Decrypt(CipherString encyptedValue, SymmetricCryptoKey key = null) { try { @@ -287,7 +288,7 @@ namespace Bit.App.Services } } - public byte[] DecryptToBytes(CipherString encyptedValue, CryptoKey key = null) + public byte[] DecryptToBytes(CipherString encyptedValue, SymmetricCryptoKey key = null) { if(key == null) { @@ -310,7 +311,7 @@ namespace Bit.App.Services // Old encrypt-then-mac scheme, swap out the key if(_legacyEtmKey == null) { - _legacyEtmKey = new CryptoKey(key.Key, Enums.EncryptionType.AesCbc128_HmacSha256_B64); + _legacyEtmKey = new SymmetricCryptoKey(key.Key, Enums.EncryptionType.AesCbc128_HmacSha256_B64); } key = _legacyEtmKey; @@ -392,7 +393,7 @@ namespace Bit.App.Services return Convert.ToBase64String(mac); } - public CryptoKey MakeKeyFromPassword(string password, string salt) + public SymmetricCryptoKey MakeKeyFromPassword(string password, string salt) { if(password == null) { @@ -408,7 +409,7 @@ namespace Bit.App.Services var saltBytes = Encoding.UTF8.GetBytes(salt); var keyBytes = _keyDerivationService.DeriveKey(passwordBytes, saltBytes, 5000); - return new CryptoKey(keyBytes); + return new SymmetricCryptoKey(keyBytes); } public string MakeKeyFromPasswordBase64(string password, string salt) @@ -417,7 +418,7 @@ namespace Bit.App.Services return Convert.ToBase64String(key.Key); } - public byte[] HashPassword(CryptoKey key, string password) + public byte[] HashPassword(SymmetricCryptoKey key, string password) { if(key == null) { @@ -434,7 +435,7 @@ namespace Bit.App.Services return hash; } - public string HashPasswordBase64(CryptoKey key, string password) + public string HashPasswordBase64(SymmetricCryptoKey key, string password) { var hash = HashPassword(key, password); return Convert.ToBase64String(hash); diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index d45c24250..d39cf4549 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -391,7 +391,7 @@ namespace Bit.App.Services private void SyncOrgKeys(ProfileResponse profile) { - var orgKeysDict = new Dictionary(); + var orgKeysDict = new Dictionary(); if(profile.Organizations != null) { @@ -400,7 +400,7 @@ namespace Bit.App.Services try { var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null); - orgKeysDict.Add(org.Id, new CryptoKey(decBytes)); + orgKeysDict.Add(org.Id, new SymmetricCryptoKey(decBytes)); } catch {