1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-05 23:53:33 +00:00

rename CryptoKey to SymmetricCryptoKey

This commit is contained in:
Kyle Spearrin
2017-04-22 14:36:31 -04:00
parent b26c3d050c
commit 27e0c7421b
9 changed files with 50 additions and 49 deletions

View File

@@ -15,6 +15,6 @@ namespace Bit.App.Abstractions
bool BelongsToOrganization(string orgId); bool BelongsToOrganization(string orgId);
void LogOut(); void LogOut();
Task<FullLoginResult> TokenPostAsync(string email, string masterPassword); Task<FullLoginResult> TokenPostAsync(string email, string masterPassword);
Task<LoginResult> TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, CryptoKey key); Task<LoginResult> TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, SymmetricCryptoKey key);
} }
} }

View File

@@ -6,24 +6,24 @@ namespace Bit.App.Abstractions
{ {
public interface ICryptoService public interface ICryptoService
{ {
CryptoKey Key { get; set; } SymmetricCryptoKey Key { get; set; }
CryptoKey PreviousKey { get; } SymmetricCryptoKey PreviousKey { get; }
bool KeyChanged { get; } bool KeyChanged { get; }
byte[] PrivateKey { get; } byte[] PrivateKey { get; }
IDictionary<string, CryptoKey> OrgKeys { get; set; } IDictionary<string, SymmetricCryptoKey> OrgKeys { get; set; }
void SetPrivateKey(CipherString privateKeyEnc, CryptoKey key); void SetPrivateKey(CipherString privateKeyEnc, SymmetricCryptoKey key);
CryptoKey GetOrgKey(string orgId); SymmetricCryptoKey GetOrgKey(string orgId);
void ClearOrgKey(string orgId); void ClearOrgKey(string orgId);
void ClearKeys(); void ClearKeys();
CryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey); SymmetricCryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey);
string Decrypt(CipherString encyptedValue, CryptoKey key = null); string Decrypt(CipherString encyptedValue, SymmetricCryptoKey key = null);
byte[] DecryptToBytes(CipherString encyptedValue, CryptoKey key = null); byte[] DecryptToBytes(CipherString encyptedValue, SymmetricCryptoKey key = null);
byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey); byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey);
CipherString Encrypt(string plaintextValue, CryptoKey key = null); CipherString Encrypt(string plaintextValue, SymmetricCryptoKey key = null);
CryptoKey MakeKeyFromPassword(string password, string salt); SymmetricCryptoKey MakeKeyFromPassword(string password, string salt);
string MakeKeyFromPasswordBase64(string password, string salt); string MakeKeyFromPasswordBase64(string password, string salt);
byte[] HashPassword(CryptoKey key, string password); byte[] HashPassword(SymmetricCryptoKey key, string password);
string HashPasswordBase64(CryptoKey key, string password); string HashPasswordBase64(SymmetricCryptoKey key, string password);
} }
} }

View File

@@ -112,7 +112,7 @@
<Compile Include="Models\Api\LoginDataModel.cs" /> <Compile Include="Models\Api\LoginDataModel.cs" />
<Compile Include="Models\Cipher.cs" /> <Compile Include="Models\Cipher.cs" />
<Compile Include="Models\CipherString.cs" /> <Compile Include="Models\CipherString.cs" />
<Compile Include="Models\CryptoKey.cs" /> <Compile Include="Models\SymmetricCryptoKey.cs" />
<Compile Include="Models\Data\SettingsData.cs" /> <Compile Include="Models\Data\SettingsData.cs" />
<Compile Include="Models\Data\FolderData.cs" /> <Compile Include="Models\Data\FolderData.cs" />
<Compile Include="Abstractions\IDataObject.cs" /> <Compile Include="Abstractions\IDataObject.cs" />

View File

@@ -9,7 +9,7 @@
public class FullLoginResult : LoginResult public class FullLoginResult : LoginResult
{ {
public bool TwoFactorRequired { get; set; } public bool TwoFactorRequired { get; set; }
public CryptoKey Key { get; set; } public SymmetricCryptoKey Key { get; set; }
public string MasterPasswordHash { get; set; } public string MasterPasswordHash { get; set; }
} }
} }

View File

@@ -4,9 +4,9 @@ using System.Linq;
namespace Bit.App.Models 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) if(rawBytes == null || rawBytes.Length == 0)
{ {

View File

@@ -20,9 +20,9 @@ namespace Bit.App.Pages
private IPushNotification _pushNotification; private IPushNotification _pushNotification;
private readonly string _email; private readonly string _email;
private readonly string _masterPasswordHash; 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) : base(updateActivity: false)
{ {
_email = email; _email = email;

View File

@@ -245,7 +245,7 @@ namespace Bit.App.Services
} }
public async Task<LoginResult> TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash, public async Task<LoginResult> TokenPostTwoFactorAsync(string token, string email, string masterPasswordHash,
CryptoKey key) SymmetricCryptoKey key)
{ {
var result = new LoginResult(); var result = new LoginResult();
@@ -271,7 +271,7 @@ namespace Bit.App.Services
return result; return result;
} }
private async Task ProcessLoginSuccessAsync(CryptoKey key, TokenResponse response) private async Task ProcessLoginSuccessAsync(SymmetricCryptoKey key, TokenResponse response)
{ {
if(response.PrivateKey != null) if(response.PrivateKey != null)
{ {
@@ -288,7 +288,7 @@ namespace Bit.App.Services
if(response.PrivateKey != null) if(response.PrivateKey != null)
{ {
var profile = await _accountsApiRepository.GetProfileAsync(); var profile = await _accountsApiRepository.GetProfileAsync();
var orgKeysDict = new Dictionary<string, CryptoKey>(); var orgKeysDict = new Dictionary<string, SymmetricCryptoKey>();
if(profile.Succeeded && (profile.Result.Organizations?.Any() ?? false)) if(profile.Succeeded && (profile.Result.Organizations?.Any() ?? false))
{ {
@@ -297,7 +297,7 @@ namespace Bit.App.Services
try try
{ {
var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null); var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null);
orgKeysDict.Add(org.Id, new CryptoKey(decBytes)); orgKeysDict.Add(org.Id, new SymmetricCryptoKey(decBytes));
} }
catch catch
{ {

View File

@@ -21,10 +21,10 @@ namespace Bit.App.Services
private readonly ISecureStorageService _secureStorage; private readonly ISecureStorageService _secureStorage;
private readonly IKeyDerivationService _keyDerivationService; private readonly IKeyDerivationService _keyDerivationService;
private CryptoKey _key; private SymmetricCryptoKey _key;
private CryptoKey _legacyEtmKey; private SymmetricCryptoKey _legacyEtmKey;
private CryptoKey _previousKey; private SymmetricCryptoKey _previousKey;
private IDictionary<string, CryptoKey> _orgKeys; private IDictionary<string, SymmetricCryptoKey> _orgKeys;
private byte[] _privateKey; private byte[] _privateKey;
public CryptoService( public CryptoService(
@@ -35,7 +35,7 @@ namespace Bit.App.Services
_keyDerivationService = keyDerivationService; _keyDerivationService = keyDerivationService;
} }
public CryptoKey Key public SymmetricCryptoKey Key
{ {
get get
{ {
@@ -44,7 +44,7 @@ namespace Bit.App.Services
var keyBytes = _secureStorage.Retrieve(KeyKey); var keyBytes = _secureStorage.Retrieve(KeyKey);
if(keyBytes != null) 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 get
{ {
@@ -75,7 +75,7 @@ namespace Bit.App.Services
var keyBytes = _secureStorage.Retrieve(PreviousKeyKey); var keyBytes = _secureStorage.Retrieve(PreviousKeyKey);
if(keyBytes != null) if(keyBytes != null)
{ {
_previousKey = new CryptoKey(keyBytes); _previousKey = new SymmetricCryptoKey(keyBytes);
} }
} }
@@ -135,7 +135,7 @@ namespace Bit.App.Services
} }
} }
public IDictionary<string, CryptoKey> OrgKeys public IDictionary<string, SymmetricCryptoKey> OrgKeys
{ {
get get
{ {
@@ -147,11 +147,11 @@ namespace Bit.App.Services
var orgKeysDictJson = Encoding.UTF8.GetString(orgKeysDictBytes, 0, orgKeysDictBytes.Length); var orgKeysDictJson = Encoding.UTF8.GetString(orgKeysDictBytes, 0, orgKeysDictBytes.Length);
if(!string.IsNullOrWhiteSpace(orgKeysDictJson)) if(!string.IsNullOrWhiteSpace(orgKeysDictJson))
{ {
_orgKeys = new Dictionary<string, CryptoKey>(); _orgKeys = new Dictionary<string, SymmetricCryptoKey>();
var orgKeysDict = JsonConvert.DeserializeObject<IDictionary<string, byte[]>>(orgKeysDictJson); var orgKeysDict = JsonConvert.DeserializeObject<IDictionary<string, byte[]>>(orgKeysDictJson);
foreach(var item in orgKeysDict) 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); var bytes = DecryptToBytes(privateKeyEnc, key);
PrivateKey = bytes; PrivateKey = bytes;
} }
public CryptoKey GetOrgKey(string orgId) public SymmetricCryptoKey GetOrgKey(string orgId)
{ {
if(OrgKeys == null || !OrgKeys.ContainsKey(orgId)) if(OrgKeys == null || !OrgKeys.ContainsKey(orgId))
{ {
@@ -218,13 +218,13 @@ namespace Bit.App.Services
PrivateKey = null; PrivateKey = null;
} }
public CryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey) public SymmetricCryptoKey AddOrgKey(string orgId, CipherString encOrgKey, byte[] privateKey)
{ {
try try
{ {
var localOrgKeys = OrgKeys; var localOrgKeys = OrgKeys;
var decBytes = RsaDecryptToBytes(encOrgKey, privateKey); var decBytes = RsaDecryptToBytes(encOrgKey, privateKey);
var key = new CryptoKey(decBytes); var key = new SymmetricCryptoKey(decBytes);
if(localOrgKeys.ContainsKey(orgId)) if(localOrgKeys.ContainsKey(orgId))
{ {
localOrgKeys[orgId] = key; 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) if(key == null)
{ {
@@ -270,10 +270,11 @@ namespace Bit.App.Services
var encryptedBytes = WinRTCrypto.CryptographicEngine.Encrypt(cryptoKey, plaintextBytes, iv); var encryptedBytes = WinRTCrypto.CryptographicEngine.Encrypt(cryptoKey, plaintextBytes, iv);
var mac = key.MacKey != null ? ComputeMac(encryptedBytes, iv, key.MacKey) : null; 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 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) if(key == null)
{ {
@@ -310,7 +311,7 @@ namespace Bit.App.Services
// Old encrypt-then-mac scheme, swap out the key // Old encrypt-then-mac scheme, swap out the key
if(_legacyEtmKey == null) 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; key = _legacyEtmKey;
@@ -392,7 +393,7 @@ namespace Bit.App.Services
return Convert.ToBase64String(mac); return Convert.ToBase64String(mac);
} }
public CryptoKey MakeKeyFromPassword(string password, string salt) public SymmetricCryptoKey MakeKeyFromPassword(string password, string salt)
{ {
if(password == null) if(password == null)
{ {
@@ -408,7 +409,7 @@ namespace Bit.App.Services
var saltBytes = Encoding.UTF8.GetBytes(salt); var saltBytes = Encoding.UTF8.GetBytes(salt);
var keyBytes = _keyDerivationService.DeriveKey(passwordBytes, saltBytes, 5000); var keyBytes = _keyDerivationService.DeriveKey(passwordBytes, saltBytes, 5000);
return new CryptoKey(keyBytes); return new SymmetricCryptoKey(keyBytes);
} }
public string MakeKeyFromPasswordBase64(string password, string salt) public string MakeKeyFromPasswordBase64(string password, string salt)
@@ -417,7 +418,7 @@ namespace Bit.App.Services
return Convert.ToBase64String(key.Key); return Convert.ToBase64String(key.Key);
} }
public byte[] HashPassword(CryptoKey key, string password) public byte[] HashPassword(SymmetricCryptoKey key, string password)
{ {
if(key == null) if(key == null)
{ {
@@ -434,7 +435,7 @@ namespace Bit.App.Services
return hash; return hash;
} }
public string HashPasswordBase64(CryptoKey key, string password) public string HashPasswordBase64(SymmetricCryptoKey key, string password)
{ {
var hash = HashPassword(key, password); var hash = HashPassword(key, password);
return Convert.ToBase64String(hash); return Convert.ToBase64String(hash);

View File

@@ -391,7 +391,7 @@ namespace Bit.App.Services
private void SyncOrgKeys(ProfileResponse profile) private void SyncOrgKeys(ProfileResponse profile)
{ {
var orgKeysDict = new Dictionary<string, CryptoKey>(); var orgKeysDict = new Dictionary<string, SymmetricCryptoKey>();
if(profile.Organizations != null) if(profile.Organizations != null)
{ {
@@ -400,7 +400,7 @@ namespace Bit.App.Services
try try
{ {
var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null); var decBytes = _cryptoService.RsaDecryptToBytes(new CipherString(org.Key), null);
orgKeysDict.Add(org.Id, new CryptoKey(decBytes)); orgKeysDict.Add(org.Id, new SymmetricCryptoKey(decBytes));
} }
catch catch
{ {