mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
WIP KeePassLibPCL - problem with awaitables
This commit is contained in:
@@ -19,18 +19,15 @@
|
||||
|
||||
using System;
|
||||
using System.Security;
|
||||
using Windows.Security.Cryptography;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.DataProtection;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Keys
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Security;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLibPCL.Keys
|
||||
{
|
||||
/// <summary>
|
||||
/// A user key depending on the currently logged on Windows user account.
|
||||
@@ -65,11 +62,8 @@ namespace ModernKeePassLib.Keys
|
||||
// Test if ProtectedData is supported -- throws an exception
|
||||
// when running on an old system (Windows 98 / ME).
|
||||
byte[] pbDummyData = new byte[128];
|
||||
|
||||
DataProtectionProvider provider = new DataProtectionProvider("Local=user");
|
||||
provider.ProtectAsync(pbDummyData.AsBuffer()).GetResults();
|
||||
/*ProtectedData.Protect(pbDummyData, m_pbEntropy,
|
||||
DataProtectionScope.CurrentUser);*/
|
||||
ProtectedData.Protect(pbDummyData, m_pbEntropy,
|
||||
DataProtectionScope.CurrentUser);
|
||||
|
||||
byte[] pbKey = LoadUserKey(false);
|
||||
if(pbKey == null) pbKey = CreateUserKey();
|
||||
@@ -87,19 +81,17 @@ namespace ModernKeePassLib.Keys
|
||||
private static string GetUserKeyFilePath(bool bCreate)
|
||||
{
|
||||
#if KeePassRT
|
||||
string strUserDir = ApplicationData.Current.RoamingFolder.Path;
|
||||
string strUserDir = Windows.Storage.ApplicationData.Current.RoamingFolder.Path;
|
||||
#else
|
||||
/*string strUserDir = Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.ApplicationData);*/
|
||||
var strUserDir = ApplicationData.Current.RoamingFolder.Path;
|
||||
string strUserDir = Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.ApplicationData);
|
||||
#endif
|
||||
|
||||
strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);
|
||||
strUserDir += PwDefs.ShortProductName;
|
||||
|
||||
// Folder is sure to exist
|
||||
/*if(bCreate && !Directory.Exists(strUserDir))
|
||||
Directory.CreateDirectory(strUserDir);*/
|
||||
if(bCreate && !Directory.Exists(strUserDir))
|
||||
Directory.CreateDirectory(strUserDir);
|
||||
|
||||
strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);
|
||||
return strUserDir + UserKeyFileName;
|
||||
@@ -113,27 +105,16 @@ namespace ModernKeePassLib.Keys
|
||||
try
|
||||
{
|
||||
string strFilePath = GetUserKeyFilePath(false);
|
||||
var pbProtectedKeyStream =
|
||||
ApplicationData.Current.RoamingFolder.GetFileAsync(strFilePath).GetResults().
|
||||
OpenAsync(FileAccessMode.Read).GetResults().AsStream();
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
ms.CopyTo(pbProtectedKeyStream);
|
||||
var pbProtectedKey = ms.ToArray();
|
||||
byte[] pbProtectedKey = File.ReadAllBytes(strFilePath);
|
||||
|
||||
//byte[] pbProtectedKey = File.ReadAllBytes(strFilePath);
|
||||
pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy,
|
||||
DataProtectionScope.CurrentUser);
|
||||
|
||||
DataProtectionProvider provider = new DataProtectionProvider("Local=user");
|
||||
pbKey = provider.UnprotectAsync(pbProtectedKey.AsBuffer()).GetResults().ToArray();
|
||||
/*pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy,
|
||||
DataProtectionScope.CurrentUser);*/
|
||||
|
||||
Array.Clear(pbProtectedKey, 0, pbProtectedKey.Length);
|
||||
}
|
||||
Array.Clear(pbProtectedKey, 0, pbProtectedKey.Length);
|
||||
}
|
||||
catch(Exception exLoad)
|
||||
{
|
||||
//if(bShowWarning) MessageService.ShowWarning(exLoad);
|
||||
if(bShowWarning) MessageService.ShowWarning(exLoad);
|
||||
|
||||
pbKey = null;
|
||||
}
|
||||
@@ -152,16 +133,10 @@ namespace ModernKeePassLib.Keys
|
||||
string strFilePath = GetUserKeyFilePath(true);
|
||||
|
||||
byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64);
|
||||
byte[] pbProtectedKey = ProtectedData.Protect(pbRandomKey,
|
||||
m_pbEntropy, DataProtectionScope.CurrentUser);
|
||||
|
||||
DataProtectionProvider provider = new DataProtectionProvider("Local=user");
|
||||
var pbProtectedKey = provider.ProtectAsync(pbRandomKey.AsBuffer()).GetResults().ToArray();
|
||||
/*byte[] pbProtectedKey = ProtectedData.Protect(pbRandomKey,
|
||||
m_pbEntropy, DataProtectionScope.CurrentUser);*/
|
||||
|
||||
var file = ApplicationData.Current.RoamingFolder.CreateFileAsync(strFilePath).GetResults().
|
||||
OpenAsync(FileAccessMode.ReadWrite).GetResults();
|
||||
file.WriteAsync(pbProtectedKey.AsBuffer()).GetResults();
|
||||
//File.WriteAllBytes(strFilePath, pbProtectedKey);
|
||||
File.WriteAllBytes(strFilePath, pbProtectedKey);
|
||||
|
||||
Array.Clear(pbProtectedKey, 0, pbProtectedKey.Length);
|
||||
Array.Clear(pbRandomKey, 0, pbRandomKey.Length);
|
||||
|
Reference in New Issue
Block a user