WIP user accounts - not working at all

This commit is contained in:
BONNEVILLE Geoffroy
2017-12-21 18:24:01 +01:00
parent acb196d9c2
commit fba668860b
2 changed files with 61 additions and 46 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Security.Cryptography.DataProtection; using Windows.Security.Cryptography.DataProtection;
using Windows.Storage.Streams; using Windows.Storage.Streams;
using ModernKeePassLib.Native; using ModernKeePassLib.Native;
@@ -8,47 +9,62 @@ namespace ModernKeePassLib.Cryptography
{ {
public static class ProtectedData public static class ProtectedData
{ {
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope) public static async Task ProtectStream(byte[] buffer, IOutputStream stream)
{ {
var provider = //instantiate a DataProtectionProvider for decryption
new DataProtectionProvider(scope == DataProtectionScope.CurrentUser ? "LOCAL=user" : "LOCAL=machine"); var dpp = new DataProtectionProvider("LOCAL=user");
// Encode the plaintext input message to a buffer.
var buffMsg = userData.AsBuffer();
// Encrypt the message. //Open a stream to load data in
IBuffer buffProtected; using (var inputStream = new InMemoryRandomAccessStream())
try
{ {
buffProtected = provider.ProtectAsync(buffMsg).GetAwaiter().GetResult(); //cteate data writer to write data to the input stream
using (var dw = new DataWriter(inputStream))
{
//write data to the stream
dw.WriteBuffer(buffer.AsBuffer());
await dw.StoreAsync();
//encrypt the intput stream into the file stream
await dpp.ProtectStreamAsync(inputStream.GetInputStreamAt(0),
stream);
}
} }
catch (Exception e)
{
throw;
}
return buffProtected.ToArray();
} }
public static async Task<byte[]> UnprotectStream(IInputStream stream)
public static byte[] Unprotect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{ {
var provider = //instantiate a DataProtectionProvider for decryption
new DataProtectionProvider(scope == DataProtectionScope.CurrentUser ? "LOCAL=user" : "LOCAL=machine"); var dpp = new DataProtectionProvider();
// Decode the encrypted input message to a buffer.
var buffMsg = userData.AsBuffer();
// Decrypt the message. //create a stream to decrypte the data to
IBuffer buffUnprotected; using (var outputStream = new InMemoryRandomAccessStream())
try
{ {
buffUnprotected = provider.UnprotectAsync(buffMsg).GetAwaiter().GetResult(); //decrypt the data
} await dpp.UnprotectStreamAsync(stream, outputStream);
catch (Exception e)
{
throw;
}
return buffUnprotected.ToArray(); //fill the data reader with the content of the outputStream,
//but from position 0
using (var dr = new DataReader(outputStream.GetInputStreamAt(0)))
{
//load data from the stream to the dataReader
await dr.LoadAsync((uint)outputStream.Size);
//load the data from the datareader into a buffer
IBuffer data = dr.ReadBuffer((uint)outputStream.Size);
return data.ToArray();
}
}
}
public static byte[] Unprotect(byte[] pbEnc, byte[] mPbOptEnt, DataProtectionScope currentUser)
{
throw new NotImplementedException();
}
public static byte[] Protect(byte[] pbPlain, byte[] mPbOptEnt, DataProtectionScope currentUser)
{
throw new NotImplementedException();
} }
} }
} }

View File

@@ -20,6 +20,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security; using System.Security;
#if ModernKeePassLib #if ModernKeePassLib
@@ -67,9 +68,9 @@ namespace ModernKeePassLib.Keys
{ {
// Test if ProtectedData is supported -- throws an exception // Test if ProtectedData is supported -- throws an exception
// when running on an old system (Windows 98 / ME). // when running on an old system (Windows 98 / ME).
byte[] pbDummyData = new byte[128]; /*byte[] pbDummyData = new byte[128];
ProtectedData.Protect(pbDummyData, m_pbEntropy, ProtectedData.Protect(pbDummyData, m_pbEntropy,
DataProtectionScope.CurrentUser); DataProtectionScope.CurrentUser);*/
byte[] pbKey = LoadUserKey(false); byte[] pbKey = LoadUserKey(false);
if(pbKey == null) pbKey = CreateUserKey(); if(pbKey == null) pbKey = CreateUserKey();
@@ -118,18 +119,16 @@ namespace ModernKeePassLib.Keys
{ {
string strFilePath = GetUserKeyFilePath(false); string strFilePath = GetUserKeyFilePath(false);
#if ModernKeePassLib #if ModernKeePassLib
byte[] pbProtectedKey; var file = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult();
using (var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult() using (var fileStream = file.OpenReadAsync().GetAwaiter().GetResult())
.OpenStreamForReadAsync().GetAwaiter().GetResult())
{ {
pbProtectedKey = new byte[(int) fileStream.Length]; pbKey = ProtectedData.UnprotectStream(fileStream).GetAwaiter().GetResult();
fileStream.Read(pbProtectedKey, 0, (int) fileStream.Length);
} }
#else #else
byte[] pbProtectedKey = File.ReadAllBytes(strFilePath); byte[] pbProtectedKey = File.ReadAllBytes(strFilePath);
#endif pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy,
pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy,
DataProtectionScope.CurrentUser); DataProtectionScope.CurrentUser);
#endif
} }
catch(Exception) catch(Exception)
{ {
@@ -149,19 +148,19 @@ namespace ModernKeePassLib.Keys
string strFilePath = GetUserKeyFilePath(true); string strFilePath = GetUserKeyFilePath(true);
byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64); byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64);
byte[] pbProtectedKey = ProtectedData.Protect(pbRandomKey,
m_pbEntropy, DataProtectionScope.CurrentUser);
#if ModernKeePassLib #if ModernKeePassLib
using (var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult() var file = ApplicationData.Current.RoamingFolder.CreateFileAsync(UserKeyFileName, CreationCollisionOption.ReplaceExisting).GetAwaiter().GetResult();
.OpenStreamForWriteAsync().GetAwaiter().GetResult()) using (var fileStream = file.OpenAsync(FileAccessMode.ReadWrite).GetAwaiter().GetResult())
{ {
fileStream.Write(pbProtectedKey, 0, (int) fileStream.Length); ProtectedData.ProtectStream(pbRandomKey, fileStream).GetAwaiter().GetResult();
} }
#else #else
byte[] pbProtectedKey = ProtectedData.Protect(pbRandomKey,
m_pbEntropy, DataProtectionScope.CurrentUser);
File.WriteAllBytes(strFilePath, pbProtectedKey); File.WriteAllBytes(strFilePath, pbProtectedKey);
#endif #endif
byte[] pbKey = LoadUserKey(true); byte[] pbKey = LoadUserKey(true);
Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey)); Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey));
MemUtil.ZeroByteArray(pbRandomKey); MemUtil.ZeroByteArray(pbRandomKey);