WIP Windows User Accounts Composite Key integration

This commit is contained in:
BONNEVILLE Geoffroy
2017-12-20 18:49:11 +01:00
parent dfa3a21e6b
commit acb196d9c2
15 changed files with 112 additions and 35 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.DataProtection;
using Windows.Storage.Streams;
using ModernKeePassLib.Native;
namespace ModernKeePassLib.Cryptography
{
public static class ProtectedData
{
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
var provider =
new DataProtectionProvider(scope == DataProtectionScope.CurrentUser ? "LOCAL=user" : "LOCAL=machine");
// Encode the plaintext input message to a buffer.
var buffMsg = userData.AsBuffer();
// Encrypt the message.
IBuffer buffProtected;
try
{
buffProtected = provider.ProtectAsync(buffMsg).GetAwaiter().GetResult();
}
catch (Exception e)
{
throw;
}
return buffProtected.ToArray();
}
public static byte[] Unprotect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
var provider =
new DataProtectionProvider(scope == DataProtectionScope.CurrentUser ? "LOCAL=user" : "LOCAL=machine");
// Decode the encrypted input message to a buffer.
var buffMsg = userData.AsBuffer();
// Decrypt the message.
IBuffer buffUnprotected;
try
{
buffUnprotected = provider.UnprotectAsync(buffMsg).GetAwaiter().GetResult();
}
catch (Exception e)
{
throw;
}
return buffUnprotected.ToArray();
}
}
}

View File

@@ -98,8 +98,9 @@ namespace ModernKeePassLib.Keys
#endif
strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);
strUserDir += PwDefs.ShortProductName;
#if !ModernKeePassLib
strUserDir += PwDefs.ShortProductName;
if(bCreate && !Directory.Exists(strUserDir))
Directory.CreateDirectory(strUserDir);
@@ -117,10 +118,13 @@ namespace ModernKeePassLib.Keys
{
string strFilePath = GetUserKeyFilePath(false);
#if ModernKeePassLib
var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult().OpenStreamForReadAsync().GetAwaiter().GetResult();
var pbProtectedKey = new byte[(int)fileStream.Length];
fileStream.Read(pbProtectedKey, 0, (int)fileStream.Length);
fileStream.Dispose();
byte[] pbProtectedKey;
using (var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult()
.OpenStreamForReadAsync().GetAwaiter().GetResult())
{
pbProtectedKey = new byte[(int) fileStream.Length];
fileStream.Read(pbProtectedKey, 0, (int) fileStream.Length);
}
#else
byte[] pbProtectedKey = File.ReadAllBytes(strFilePath);
#endif
@@ -148,9 +152,11 @@ namespace ModernKeePassLib.Keys
byte[] pbProtectedKey = ProtectedData.Protect(pbRandomKey,
m_pbEntropy, DataProtectionScope.CurrentUser);
#if ModernKeePassLib
var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult().OpenStreamForWriteAsync().GetAwaiter().GetResult();
fileStream.Write(pbProtectedKey, 0, (int)fileStream.Length);
fileStream.Dispose();
using (var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult()
.OpenStreamForWriteAsync().GetAwaiter().GetResult())
{
fileStream.Write(pbProtectedKey, 0, (int) fileStream.Length);
}
#else
File.WriteAllBytes(strFilePath, pbProtectedKey);
#endif

View File

@@ -81,6 +81,7 @@
<Compile Include="Cryptography\PasswordGenerator\PwCharSet.cs" />
<Compile Include="Cryptography\PasswordGenerator\PwProfile.cs" />
<Compile Include="Cryptography\PopularPasswords.cs" />
<Compile Include="Cryptography\ProtectedData.cs" />
<Compile Include="Cryptography\QualityEstimation.cs" />
<Compile Include="Cryptography\SelfTest.cs" />
<Compile Include="Interfaces\IStructureItem.cs" />

View File

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>ModernKeePassLib</id>
<version>2.37.8000</version>
<version>2.37.9000</version>
<title>ModernKeePassLib</title>
<authors>Geoffroy Bonneville</authors>
<owners>Geoffroy Bonneville</owners>
@@ -10,7 +10,7 @@
<projectUrl>https://github.com/wismna/ModernKeePass</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Portable KeePass Password Management Library that targets .Net Standard and WinRT. Allows reading, editing and writing to KeePass 2.x databases.</description>
<releaseNotes>Code cleanup</releaseNotes>
<releaseNotes>Implements Windows User Accounts</releaseNotes>
<copyright>Copyright © 2017 Geoffroy Bonneville</copyright>
<tags>KeePass KeePassLib Portable PCL NetStandard</tags>
<dependencies>

View File

@@ -54,25 +54,12 @@ namespace ModernKeePassLib.Native
}
}
internal enum DataProtectionScope
public enum DataProtectionScope
{
CurrentUser,
LocalMachine
}
internal static class ProtectedData
{
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
throw new NotImplementedException();
}
public static byte[] Unprotect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
throw new NotImplementedException();
}
}
internal enum MemoryProtectionScope
{
CrossProcess,

View File

@@ -34,6 +34,7 @@ using System.Security.Cryptography;
#endif
using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.PasswordGenerator;
using ModernKeePassLib.Native;
using ModernKeePassLib.Security;