Repaired key file creation in lib

This commit is contained in:
BONNEVILLE Geoffroy
2017-11-15 17:56:31 +01:00
parent 7a632c8f80
commit 9313ac1abf
6 changed files with 42 additions and 20 deletions

View File

@@ -13,10 +13,12 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
public class StandardAesEngineTests
{
// Test vector (official ECB test vector #356)
private byte[] pbReferenceCT = new byte[16] {
private byte[] pbReferenceCT = new byte[16]
{
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
};
[TestMethod]
public void TestEncryptStream()
{
@@ -29,7 +31,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var aes = new StandardAesEngine();
var inStream = aes.EncryptStream(outStream, pbTestKey, pbIV);
new BinaryWriter(inStream).Write(pbTestData);
Assert.AreEqual(outStream.Position, 16);
Assert.AreEqual(16, outStream.Position);
outStream.Position = 0;
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, pbReferenceCT));

View File

@@ -36,7 +36,7 @@ namespace ModernKeePassLib.Test.Keys
0x31, 0xAA, 0x14, 0x3D, 0x95, 0xBF, 0x63, 0xFF
};
var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
//var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
var file = ApplicationData.Current.TemporaryFolder.CreateFileAsync(TestCreateFile).GetAwaiter().GetResult();
using (var fs = file.OpenStreamForWriteAsync().GetAwaiter().GetResult())
{
@@ -50,7 +50,7 @@ namespace ModernKeePassLib.Test.Keys
try
{
var keyFile = new KcpKeyFile(fullPath);
var keyFile = new KcpKeyFile(file);
var keyData = keyFile.KeyData.ReadData();
Assert.IsTrue(MemUtil.ArraysEqual(keyData, expectedKeyData));
}
@@ -63,9 +63,9 @@ namespace ModernKeePassLib.Test.Keys
[TestMethod]
public void TestCreate()
{
var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
//var fullPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, TestCreateFile);
var file = ApplicationData.Current.TemporaryFolder.CreateFileAsync(TestCreateFile).GetAwaiter().GetResult();
KcpKeyFile.Create(fullPath, null);
KcpKeyFile.Create(file, null);
try
{
var fileContents = FileIO.ReadTextAsync(file).GetAwaiter().GetResult();

View File

@@ -152,10 +152,6 @@
<HintPath>..\packages\Portable.BouncyCastle.1.8.1.3\lib\netstandard1.0\BouncyCastle.Crypto.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ModernKeePassLib, Version=2.37.0.2000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ModernKeePassLib.2.37.6000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Splat, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Splat.2.0.0\lib\Portable-Win81+Wpa81\Splat.dll</HintPath>
<Private>True</Private>
@@ -183,6 +179,12 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ModernKeePassLib\ModernKeePassLib.csproj">
<Project>{2e710089-9559-4967-846c-e763dd1f3acb}</Project>
<Name>ModernKeePassLib</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>

View File

@@ -118,7 +118,7 @@ namespace ModernKeePassLib.Cryptography
for(i = 0; i < 16; ++i) pbTestData[i] = 0;
pbTestData[0] = 0x04;
#if ModernKeePassLib
#if ModernKeePassLib || KeePassUAP
AesEngine r = new AesEngine();
r.Init(true, new KeyParameter(pbTestKey));
if(r.GetBlockSize() != pbTestData.Length)
@@ -1032,7 +1032,7 @@ namespace ModernKeePassLib.Cryptography
private static void TestUrlUtil()
{
#if DEBUG
#if !ModernKeePassLib
#if !ModernKeePassLib && !KeePassUAP
Debug.Assert(Uri.UriSchemeHttp.Equals("http", StrUtil.CaseIgnoreCmp));
Debug.Assert(Uri.UriSchemeHttps.Equals("https", StrUtil.CaseIgnoreCmp));
#endif

View File

@@ -23,13 +23,13 @@ using System.IO;
using System.Security;
using System.Text;
using System.Xml;
#if ModernKeePassLib
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage;
#else
using System.Security.Cryptography;
#endif
@@ -68,6 +68,13 @@ namespace ModernKeePassLib.Keys
get { return m_pbKeyData; }
}
#if ModernKeePassLib
public KcpKeyFile(StorageFile strKeyFile)
{
Construct(IOConnectionInfo.FromFile(strKeyFile), false);
}
#endif
public KcpKeyFile(string strKeyFile)
{
Construct(IOConnectionInfo.FromPath(strKeyFile), false);
@@ -185,7 +192,11 @@ namespace ModernKeePassLib.Keys
/// the random key. May be <c>null</c> (in this case only the KeePass-internal
/// random number generator is used).</param>
/// <returns>Returns a <c>FileSaveResult</c> error code.</returns>
#if ModernKeePassLib
public static void Create(StorageFile strFilePath, byte[] pbAdditionalEntropy)
#else
public static void Create(string strFilePath, byte[] pbAdditionalEntropy)
#endif
{
byte[] pbKey32 = CryptoRandom.Instance.GetRandomBytes(32);
if(pbKey32 == null) throw new SecurityException();
@@ -293,18 +304,25 @@ namespace ModernKeePassLib.Keys
return pbKeyData;
}
#if ModernKeePassLib
private static void CreateXmlKeyFile(StorageFile strFile, byte[] pbKeyData)
#else
private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData)
#endif
{
Debug.Assert(strFile != null);
if(strFile == null) throw new ArgumentNullException("strFile");
Debug.Assert(pbKeyData != null);
if(pbKeyData == null) throw new ArgumentNullException("pbKeyData");
#if ModernKeePassLib
IOConnectionInfo ioc = IOConnectionInfo.FromFile(strFile);
#else
IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
#endif
Stream sOut = IOConnection.OpenWrite(ioc);
#if ModernKeePassLib
#if ModernKeePassLib || KeePassUAP
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = StrUtil.Utf8;
xws.Indent = false;

View File

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>ModernKeePassLib</id>
<version>2.37.6000</version>
<version>2.37.7000</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>TBD</releaseNotes>
<releaseNotes>Can now create key files</releaseNotes>
<copyright>Copyright © 2017 Geoffroy Bonneville</copyright>
<tags>KeePass KeePassLib Portable PCL NetStandard</tags>
<dependencies>