mirror of
https://github.com/bitwarden/mobile
synced 2025-12-26 21:23:46 +00:00
ios core lib
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
using Foundation;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Bit.iOS.Services
|
||||
{
|
||||
public class CryptoPrimitiveService : ICryptoPrimitiveService
|
||||
{
|
||||
private const uint PBKDFAlgorithm = 2; // kCCPBKDF2
|
||||
|
||||
public byte[] Pbkdf2(byte[] password, byte[] salt, CryptoHashAlgorithm algorithm, int iterations)
|
||||
{
|
||||
uint keySize = 32;
|
||||
uint pseudoRandomAlgorithm = 3; // kCCPRFHmacAlgSHA256
|
||||
if(algorithm == CryptoHashAlgorithm.Sha512)
|
||||
{
|
||||
keySize = 64;
|
||||
pseudoRandomAlgorithm = 5; // kCCPRFHmacAlgSHA512
|
||||
}
|
||||
else if(algorithm != CryptoHashAlgorithm.Sha256)
|
||||
{
|
||||
throw new ArgumentException("Unsupported PBKDF2 algorithm.");
|
||||
}
|
||||
|
||||
var keyData = new NSMutableData();
|
||||
keyData.Length = keySize;
|
||||
|
||||
var passwordData = NSData.FromArray(password);
|
||||
var saltData = NSData.FromArray(salt);
|
||||
|
||||
var result = CCKeyCerivationPBKDF(PBKDFAlgorithm, passwordData.Bytes, passwordData.Length, saltData.Bytes,
|
||||
saltData.Length, pseudoRandomAlgorithm, Convert.ToUInt32(iterations), keyData.MutableBytes,
|
||||
keyData.Length);
|
||||
|
||||
byte[] keyBytes = new byte[keyData.Length];
|
||||
Marshal.Copy(keyData.Bytes, keyBytes, 0, Convert.ToInt32(keyData.Length));
|
||||
return keyBytes;
|
||||
}
|
||||
|
||||
// ref: http://opensource.apple.com/source/CommonCrypto/CommonCrypto-55010/CommonCrypto/CommonKeyDerivation.h
|
||||
[DllImport(ObjCRuntime.Constants.libSystemLibrary, EntryPoint = "CCKeyDerivationPBKDF")]
|
||||
private extern static int CCKeyCerivationPBKDF(uint algorithm, IntPtr password, nuint passwordLen,
|
||||
IntPtr salt, nuint saltLen, uint prf, nuint rounds, IntPtr derivedKey, nuint derivedKeyLength);
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Foundation;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Security;
|
||||
|
||||
namespace Bit.iOS.Services
|
||||
{
|
||||
public class KeyChainStorageService : IStorageService
|
||||
{
|
||||
private readonly string _keyFormat = "bwKeyChainStorage:{0}";
|
||||
private readonly string _service;
|
||||
private readonly string _group;
|
||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public KeyChainStorageService(string service, string group)
|
||||
{
|
||||
_service = service;
|
||||
_group = group;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, 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);
|
||||
}
|
||||
|
||||
CheckError(resultCode);
|
||||
dataBytes = record.Generic.ToArray();
|
||||
}
|
||||
|
||||
var dataString = Encoding.UTF8.GetString(dataBytes);
|
||||
if(typeof(T) == typeof(string))
|
||||
{
|
||||
return Task.FromResult((T)(object)dataString);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(dataString, _jsonSettings));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if(obj == null)
|
||||
{
|
||||
await RemoveAsync(key);
|
||||
return;
|
||||
}
|
||||
|
||||
string dataString = null;
|
||||
if(typeof(T) == typeof(string))
|
||||
{
|
||||
dataString = obj as string;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataString = JsonConvert.SerializeObject(obj, _jsonSettings);
|
||||
}
|
||||
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
var dataBytes = Encoding.UTF8.GetBytes(dataString);
|
||||
using(var data = NSData.FromArray(dataBytes))
|
||||
using(var newRecord = GetKeyRecord(formattedKey, data))
|
||||
{
|
||||
await RemoveAsync(formattedKey);
|
||||
CheckError(SecKeyChain.Add(newRecord));
|
||||
}
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
using(var record = GetExistingRecord(formattedKey))
|
||||
{
|
||||
if(record != null)
|
||||
{
|
||||
CheckError(SecKeyChain.Remove(record));
|
||||
}
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
private SecRecord GetKeyRecord(string key, NSData data = null)
|
||||
{
|
||||
var record = new SecRecord(SecKind.GenericPassword)
|
||||
{
|
||||
Service = _service,
|
||||
Account = key,
|
||||
AccessGroup = _group
|
||||
};
|
||||
if(data != null)
|
||||
{
|
||||
record.Generic = data;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
private SecRecord GetExistingRecord(string key)
|
||||
{
|
||||
var existingRecord = GetKeyRecord(key);
|
||||
SecKeyChain.QueryAsRecord(existingRecord, out SecStatusCode resultCode);
|
||||
return resultCode == SecStatusCode.Success ? existingRecord : null;
|
||||
}
|
||||
|
||||
private void CheckError(SecStatusCode resultCode, [CallerMemberName] string caller = null)
|
||||
{
|
||||
if(resultCode != SecStatusCode.Success)
|
||||
{
|
||||
throw new Exception(string.Format("Failed to execute {0}. Result code: {1}", caller, resultCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,8 +92,6 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="AppDelegate.cs" />
|
||||
<Compile Include="Services\CryptoPrimitiveService.cs" />
|
||||
<Compile Include="Services\KeyChainStorageService.cs" />
|
||||
<None Include="Entitlements.plist" />
|
||||
<None Include="Info.plist" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -166,5 +164,14 @@
|
||||
<Project>{4b8a8c41-9820-4341-974c-41e65b7f4366}</Project>
|
||||
<Name>Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\iOS.Core\iOS.Core.csproj">
|
||||
<Project>{e71f3053-056c-4381-9638-048ed73bdff6}</Project>
|
||||
<Name>iOS.Core</Name>
|
||||
<IsAppExtension>false</IsAppExtension>
|
||||
<IsWatchApp>false</IsWatchApp>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user