WIP KeePassLibPCL

This commit is contained in:
bg45
2017-09-23 09:42:48 -04:00
parent 668afbe817
commit 9d78d59a15
108 changed files with 3283 additions and 181 deletions

View File

@@ -21,7 +21,7 @@ using System;
using System.IO;
#if ModernKeePassLibPCL
using System.Linq;
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -30,6 +30,7 @@ using System.Text;
using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
#if KeePassLibSD
using KeePassLibSD;
@@ -251,13 +252,17 @@ namespace ModernKeePassLibPCL.Serialization
if(m_bVerify)
{
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbComputedHash = sha256.HashData(m_pbBuffer);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbComputedHash = sha256.HashData(m_pbBuffer);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(m_pbBuffer));
byte[] pbComputedHash;
CryptographicBuffer.CopyToByteArray(buffer, out pbComputedHash);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbComputedHash = sha256.ComputeHash(m_pbBuffer);
#endif
if((pbComputedHash == null) || (pbComputedHash.Length != 32))
if ((pbComputedHash == null) || (pbComputedHash.Length != 32))
throw new InvalidOperationException();
for(int iHashPos = 0; iHashPos < 32; ++iHashPos)
@@ -298,8 +303,12 @@ namespace ModernKeePassLibPCL.Serialization
if(m_nBufferPos > 0)
{
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbHash = sha256.HashData(m_pbBuffer.Where((x, i) => i < m_nBufferPos).ToArray());
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbHash = sha256.HashData(m_pbBuffer.Where((x, i) => i < m_nBufferPos).ToArray());*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(m_pbBuffer.Where((x, i) => i < m_nBufferPos).ToArray()));
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(buffer, out pbHash);
#else
SHA256Managed sha256 = new SHA256Managed();
@@ -320,7 +329,7 @@ namespace ModernKeePassLibPCL.Serialization
#endif
m_bwOutput.Write(pbHash);
m_bwOutput.Write(pbHash);
}
else
{

View File

@@ -34,7 +34,7 @@ using System.Security.Cryptography.X509Certificates;
#if ModernKeePassLibPCL
using Windows.Storage;
using PCLStorage;
//using PCLStorage;
#endif
using ModernKeePassLibPCL.Utility;
@@ -432,12 +432,12 @@ namespace ModernKeePassLibPCL.Serialization
private static Stream OpenReadLocal(IOConnectionInfo ioc)
{
#if ModernKeePassLibPCL
if (ioc.StorageFile != null)
{
/*if (ioc.StorageFile != null)
{*/
return ioc.StorageFile.OpenAsync(FileAccessMode.Read).GetResults().AsStream();
}
/*}
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
return file.OpenAsync(PCLStorage.FileAccess.Read).Result;
return file.OpenAsync(PCLStorage.FileAccess.Read).Result;*/
#else
return new FileStream(ioc.Path, FileMode.Open, FileAccess.Read,
FileShare.Read);
@@ -478,12 +478,12 @@ namespace ModernKeePassLibPCL.Serialization
private static Stream OpenWriteLocal(IOConnectionInfo ioc)
{
#if ModernKeePassLibPCL
if (ioc.StorageFile != null)
{
/*if (ioc.StorageFile != null)
{*/
return ioc.StorageFile.OpenAsync(FileAccessMode.ReadWrite).GetResults().AsStream();
}
/*}
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
return file.OpenAsync(FileAccess.ReadAndWrite).Result;
return file.OpenAsync(FileAccess.ReadAndWrite).Result;*/
#else
return new FileStream(ioc.Path, FileMode.Create, FileAccess.Write,
FileShare.None);
@@ -502,8 +502,9 @@ namespace ModernKeePassLibPCL.Serialization
RaiseIOAccessPreEvent(ioc, IOAccessType.Exists);
#if ModernKeePassLibPCL
if(ioc.IsLocalFile())
return (FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result != null);
/*if(ioc.IsLocalFile())
return (FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result != null);*/
return ioc.StorageFile.IsAvailable;
#else
if(ioc.IsLocalFile()) return File.Exists(ioc.Path);
#endif
@@ -546,8 +547,8 @@ namespace ModernKeePassLibPCL.Serialization
#if ModernKeePassLibPCL
if (!ioc.IsLocalFile()) return;
ioc.StorageFile?.DeleteAsync().GetResults();
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
file.DeleteAsync().RunSynchronously();
/*var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
file.DeleteAsync().RunSynchronously();*/
#else
if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; }
#endif
@@ -587,8 +588,8 @@ namespace ModernKeePassLibPCL.Serialization
#if ModernKeePassLibPCL
if (!iocFrom.IsLocalFile()) return;
iocFrom.StorageFile?.RenameAsync(iocTo.Path).GetResults();
var file = FileSystem.Current.GetFileFromPathAsync(iocFrom.Path).Result;
file.MoveAsync(iocTo.Path).RunSynchronously();
/*var file = FileSystem.Current.GetFileFromPathAsync(iocFrom.Path).Result;
file.MoveAsync(iocTo.Path).RunSynchronously();*/
#else
if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; }
#endif
@@ -629,18 +630,18 @@ namespace ModernKeePassLibPCL.Serialization
}
#endif
// using(Stream sIn = IOConnection.OpenRead(iocFrom))
// {
// using(Stream sOut = IOConnection.OpenWrite(iocTo))
// {
// MemUtil.CopyStream(sIn, sOut);
// sOut.Close();
// }
//
// sIn.Close();
// }
// DeleteFile(iocFrom);
}
// using(Stream sIn = IOConnection.OpenRead(iocFrom))
// {
// using(Stream sOut = IOConnection.OpenWrite(iocTo))
// {
// MemUtil.CopyStream(sIn, sOut);
// sOut.Close();
// }
//
// sIn.Close();
// }
// DeleteFile(iocFrom);
}
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
private static bool SendCommand(IOConnectionInfo ioc, string strMethod)
@@ -656,8 +657,8 @@ namespace ModernKeePassLibPCL.Serialization
return true;
}
#endif
internal static void DisposeResponse(WebResponse wr, bool bGetStream)
#if !ModernKeePassLibPCL
internal static void DisposeResponse(WebResponse wr, bool bGetStream)
{
if(wr == null) return;
@@ -674,8 +675,8 @@ namespace ModernKeePassLibPCL.Serialization
try { wr.Dispose(); }
catch(Exception) { Debug.Assert(false); }
}
public static byte[] ReadFile(IOConnectionInfo ioc)
#endif
public static byte[] ReadFile(IOConnectionInfo ioc)
{
Stream sIn = null;
MemoryStream ms = null;

View File

@@ -25,9 +25,9 @@ using System.Net;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Diagnostics;
using Windows.Storage;
#if ModernKeePassLibPCL
using PCLStorage;
using Windows.Storage;
//using PCLStorage;
#endif
using ModernKeePassLibPCL.Interfaces;

View File

@@ -24,7 +24,7 @@ using System.IO;
using System.Diagnostics;
using System.Security;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -42,6 +42,7 @@ using ModernKeePassLibPCL.Interfaces;
using ModernKeePassLibPCL.Keys;
using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Serialization
{
@@ -221,13 +222,16 @@ namespace ModernKeePassLibPCL.Serialization
msHeader.Dispose();
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
m_pbHashOfHeader = sha256.HashData(pbHeader);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
m_pbHashOfHeader = sha256.HashData(pbHeader);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbHeader));
CryptographicBuffer.CopyToByteArray(buffer, out m_pbHashOfHeader);
#else
SHA256Managed sha256 = new SHA256Managed();
m_pbHashOfHeader = sha256.ComputeHash(pbHeader);
#endif
}
}
private bool ReadHeaderField(BinaryReaderEx brSource)
{
@@ -348,14 +352,18 @@ namespace ModernKeePassLibPCL.Serialization
ms.Write(pKey32, 0, 32);
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var aesKey = sha256.HashData(ms.ToArray());
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var aesKey = sha256.HashData(ms.ToArray());*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(ms.ToArray()));
byte[] aesKey;
CryptographicBuffer.CopyToByteArray(buffer, out aesKey);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] aesKey = sha256.ComputeHash(ms.ToArray());
#endif
ms.Dispose();
ms.Dispose();
Array.Clear(pKey32, 0, 32);
if((aesKey == null) || (aesKey.Length != 32))

View File

@@ -24,7 +24,7 @@ using System.IO;
using System.Xml;
using System.Security;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -47,6 +47,7 @@ using ModernKeePassLibPCL.Keys;
using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Serialization
{
@@ -189,14 +190,17 @@ namespace ModernKeePassLibPCL.Serialization
ms.Dispose();
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
m_pbHashOfHeader = sha256.HashData(pbHeader);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
m_pbHashOfHeader = sha256.HashData(pbHeader);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbHeader));
CryptographicBuffer.CopyToByteArray(buffer, out m_pbHashOfHeader);
#else
SHA256Managed sha256 = new SHA256Managed();
m_pbHashOfHeader = sha256.ComputeHash(pbHeader);
#endif
s.Write(pbHeader, 0, pbHeader.Length);
s.Write(pbHeader, 0, pbHeader.Length);
s.Flush();
}
@@ -236,14 +240,18 @@ namespace ModernKeePassLibPCL.Serialization
ms.Write(pKey32, 0, 32);
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var aesKey = sha256.HashData(ms.ToArray());
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var aesKey = sha256.HashData(ms.ToArray());*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(ms.ToArray()));
byte[] aesKey;
CryptographicBuffer.CopyToByteArray(buffer, out aesKey);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] aesKey = sha256.ComputeHash(ms.ToArray());
#endif
ms.Dispose();
ms.Dispose();
Array.Clear(pKey32, 0, 32);
Debug.Assert(CipherPool.GlobalPool != null);

View File

@@ -30,7 +30,8 @@ using System.IO.Compression;
#endif
#if ModernKeePassLibPCL
using PCLStorage;
//using PCLStorage;
using Windows.Storage;
#endif
using ModernKeePassLibPCL.Collections;