1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-28 22:23:35 +00:00

start writing crypto service

This commit is contained in:
Kyle Spearrin
2019-04-03 14:21:54 -04:00
parent 56b8bc1730
commit 2b2342bcad
9 changed files with 453 additions and 1 deletions

View File

@@ -0,0 +1,109 @@
using Bit.Core.Enums;
using System;
namespace Bit.Core.Models.Domain
{
public class CipherString
{
private string _decryptedValue;
public CipherString(EncryptionType encryptionType, string data, string iv, string mac)
{
if(string.IsNullOrWhiteSpace(data))
{
throw new ArgumentNullException(nameof(data));
}
if(!string.IsNullOrWhiteSpace(iv))
{
EncryptedString = string.Format("{0}.{1}|{2}", (byte)encryptionType, iv, data);
}
else
{
EncryptedString = string.Format("{0}.{1}", (byte)encryptionType, data);
}
if(!string.IsNullOrWhiteSpace(mac))
{
EncryptedString = string.Format("{0}|{1}", EncryptedString, mac);
}
EncryptionType = encryptionType;
Data = data;
Iv = iv;
Mac = mac;
}
public CipherString(string encryptedString)
{
if(string.IsNullOrWhiteSpace(encryptedString))
{
throw new ArgumentException(nameof(encryptedString));
}
EncryptedString = encryptedString;
var headerPieces = EncryptedString.Split('.');
string[] encPieces;
if(headerPieces.Length == 2 && Enum.TryParse(headerPieces[0], out EncryptionType encType))
{
EncryptionType = encType;
encPieces = headerPieces[1].Split('|');
}
else
{
encPieces = EncryptedString.Split('|');
EncryptionType = encPieces.Length == 3 ? EncryptionType.AesCbc128_HmacSha256_B64 :
EncryptionType.AesCbc256_B64;
}
switch(EncryptionType)
{
case EncryptionType.AesCbc128_HmacSha256_B64:
case EncryptionType.AesCbc256_HmacSha256_B64:
if(encPieces.Length != 3)
{
return;
}
Iv = encPieces[0];
Data = encPieces[1];
Mac = encPieces[2];
break;
case EncryptionType.AesCbc256_B64:
if(encPieces.Length != 2)
{
return;
}
Iv = encPieces[0];
Data = encPieces[1];
break;
case EncryptionType.Rsa2048_OaepSha256_B64:
case EncryptionType.Rsa2048_OaepSha1_B64:
if(encPieces.Length != 1)
{
return;
}
Data = encPieces[0];
break;
default:
return;
}
}
public EncryptionType EncryptionType { get; private set; }
public string EncryptedString { get; private set; }
public string Iv { get; private set; }
public string Data { get; private set; }
public string Mac { get; private set; }
public string Decrypt(string orgId = null)
{
if(_decryptedValue == null)
{
// TODO
}
return _decryptedValue;
}
}
}

View File

@@ -0,0 +1,77 @@
using Bit.Core.Enums;
using System;
using System.Linq;
namespace Bit.Core.Models.Domain
{
public class SymmetricCryptoKey
{
public SymmetricCryptoKey(byte[] key, EncryptionType? encType = null)
{
if(key == null)
{
throw new Exception("Must provide key.");
}
if(encType == null)
{
if(key.Length == 32)
{
encType = EncryptionType.AesCbc256_B64;
}
else if(key.Length == 64)
{
encType = EncryptionType.AesCbc256_HmacSha256_B64;
}
else
{
throw new Exception("Unable to determine encType.");
}
}
Key = key;
EncType = encType.Value;
if(EncType == EncryptionType.AesCbc256_B64 && Key.Length == 32)
{
EncKey = Key;
MacKey = null;
}
else if(EncType == EncryptionType.AesCbc128_HmacSha256_B64 && Key.Length == 32)
{
EncKey = new ArraySegment<byte>(Key, 0, 16).ToArray();
MacKey = new ArraySegment<byte>(Key, 16, 16).ToArray();
}
else if(EncType == EncryptionType.AesCbc256_HmacSha256_B64 && Key.Length == 34)
{
EncKey = new ArraySegment<byte>(Key, 0, 32).ToArray();
MacKey = new ArraySegment<byte>(Key, 32, 32).ToArray();
}
else
{
throw new Exception("Unsupported encType/key length.");
}
if(Key != null)
{
KeyB64 = Convert.ToBase64String(Key);
}
if(EncKey != null)
{
EncKeyB64 = Convert.ToBase64String(EncKey);
}
if(MacKey != null)
{
MacKeyB64 = Convert.ToBase64String(MacKey);
}
}
public byte[] Key { get; set; }
public byte[] EncKey { get; set; }
public byte[] MacKey { get; set; }
public EncryptionType EncType { get; set; }
public string KeyB64 { get; set; }
public string EncKeyB64 { get; set; }
public string MacKeyB64 { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using Bit.Core.Enums;
namespace Bit.Core.Models.Response
{
public class ProfileOrganizationResponse
{
public string Id { get; set; }
public string Name { get; set; }
public bool UseGroups { get; set; }
public bool UseDirectory { get; set; }
public bool UseEvents { get; set; }
public bool UseTotp { get; set; }
public bool Use2fa { get; set; }
public bool UseApi { get; set; }
public bool UsersGetPremium { get; set; }
public bool SelfHost { get; set; }
public int Seats { get; set; }
public int MaxCollections { get; set; }
public short? MaxStorageGb { get; set; }
public string Key { get; set; }
public OrganizationUserStatusType Status { get; set; }
public OrganizationUserType Type { get; set; }
public bool Enabled { get; set; }
}
}