From 5067f811893933ac40187f4f3b3376b1b252ad84 Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Mon, 23 Mar 2020 18:35:00 +0100 Subject: [PATCH] Downgrade to net standard 1.2 --- .../Cryptography/Cipher/AesTests.cs | 19 +- ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs | 1 - .../ModernKeePassLib.Test.csproj | 7 +- .../Serialization/KdbxFileTests.cs | 4 +- ModernKeePassLib.sln | 64 ++---- .../Cryptography/Cipher/CryptoStreamMode.cs | 8 + .../Cryptography/Cipher/StandardAesEngine.cs | 10 +- .../Cipher/StandardAesEngineExt.cs | 26 +++ ModernKeePassLib/Cryptography/CryptoRandom.cs | 24 ++- .../Cryptography/CryptoRandomStream.cs | 4 +- .../Cryptography/CryptoStreamEx.cs | 2 +- ModernKeePassLib/Cryptography/CryptoUtil.cs | 12 +- ModernKeePassLib/Cryptography/Hash/Blake2b.cs | 172 +++++++++++++++- ModernKeePassLib/Cryptography/Hash/HMAC.cs | 62 ++++++ .../Cryptography/Hash/HMACSHA1.cs | 10 + .../Cryptography/Hash/HMACSHA256.cs | 10 + .../Cryptography/Hash/HashAlgorithm.cs | 105 ++++++++++ .../Cryptography/Hash/SHA256Managed.cs | 9 + .../Cryptography/Hash/SHA512Managed.cs | 10 + .../Cryptography/HashingStreamEx.cs | 5 +- ModernKeePassLib/Cryptography/HmacOtp.cs | 5 +- .../Cryptography/KeyDerivation/AesKdf.cs | 15 +- .../PasswordGenerator/PwGenerator.cs | 59 +++++- .../Cryptography/PopularPasswords.cs | 7 + .../Cryptography/ProtectedData.cs | 1 - .../Cryptography/RNGCryptoServiceProvider.cs | 17 ++ ModernKeePassLib/Cryptography/SelfTest.cs | 30 +-- ModernKeePassLib/Keys/CompositeKey.cs | 14 +- ModernKeePassLib/Keys/KcpKeyFile.cs | 28 +++ ModernKeePassLib/Keys/KcpPassword.cs | 5 + ModernKeePassLib/Keys/KcpUserAccount.cs | 24 ++- ModernKeePassLib/ModernKeePass.Lib.csproj | 194 ++++++++++++++++++ ModernKeePassLib/ModernKeePassLib.csproj | 62 ------ ModernKeePassLib/ModernKeePassLib.nuspec | 36 ++++ ModernKeePassLib/Native/ClipboardU.cs | 2 +- .../{Native.PCL.cs => Native.Standard.cs} | 42 ++-- ModernKeePassLib/Native/NativeLib.cs | 9 +- .../Native/SimpleStat.Standard.cs | 7 + ModernKeePassLib/Properties/AssemblyInfo.cs | 44 ++++ ModernKeePassLib/PwDatabase.cs | 2 - ModernKeePassLib/PwDefs.cs | 8 +- ModernKeePassLib/PwEntry.cs | 4 +- ModernKeePassLib/Security/ProtectedBinary.cs | 4 - ModernKeePassLib/Security/ProtectedString.cs | 4 +- .../Serialization/FileTransactionEx.cs | 14 +- .../Serialization/HmacBlockStream.cs | 6 +- .../Serialization/IOConnection.cs | 17 +- .../Serialization/IOConnectionInfo.cs | 2 +- .../Serialization/KdbxFile.Read.Streamed.cs | 13 +- .../Serialization/KdbxFile.Write.cs | 4 +- ModernKeePassLib/Serialization/KdbxFile.cs | 27 ++- ModernKeePassLib/Translation/KPTranslation.cs | 2 +- ModernKeePassLib/Utility/MessageService.cs | 4 +- ModernKeePassLib/Utility/MonoWorkarounds.cs | 6 +- ModernKeePassLib/Utility/StrUtil.cs | 19 +- ModernKeePassLib/Utility/StreamExtensions.cs | 17 ++ ModernKeePassLib/Utility/UrlUtil.cs | 36 +++- ModernKeePassLib/Utility/XmlUtilEx.cs | 25 ++- ModernKeePassLib/app.config | 139 +++++++++++++ ModernKeePassLib/project.json | 17 ++ 60 files changed, 1250 insertions(+), 285 deletions(-) create mode 100644 ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs create mode 100644 ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/HMAC.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs create mode 100644 ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs create mode 100644 ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs create mode 100644 ModernKeePassLib/ModernKeePass.Lib.csproj delete mode 100644 ModernKeePassLib/ModernKeePassLib.csproj create mode 100644 ModernKeePassLib/ModernKeePassLib.nuspec rename ModernKeePassLib/Native/{Native.PCL.cs => Native.Standard.cs} (77%) create mode 100644 ModernKeePassLib/Native/SimpleStat.Standard.cs create mode 100644 ModernKeePassLib/Properties/AssemblyInfo.cs create mode 100644 ModernKeePassLib/Utility/StreamExtensions.cs create mode 100644 ModernKeePassLib/app.config create mode 100644 ModernKeePassLib/project.json diff --git a/ModernKeePassLib.Test/Cryptography/Cipher/AesTests.cs b/ModernKeePassLib.Test/Cryptography/Cipher/AesTests.cs index 7a52db1..727f3bf 100644 --- a/ModernKeePassLib.Test/Cryptography/Cipher/AesTests.cs +++ b/ModernKeePassLib.Test/Cryptography/Cipher/AesTests.cs @@ -29,7 +29,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher [Test] public void TestEncryptStream() { - var a = CryptoUtil.CreateAes(); + /*var a = CryptoUtil.CreateAes(); if (a.BlockSize != 128) // AES block size { //Debug.Assert(false); @@ -44,7 +44,20 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher iCrypt.TransformBlock(_pbTestData, 0, 16, _pbTestData, 0); - Assert.That(MemUtil.ArraysEqual(_pbTestData, _pbReferenceCt), Is.True); + Assert.That(MemUtil.ArraysEqual(_pbTestData, _pbReferenceCt), Is.True);*/ + + using var outStream = new MemoryStream(); + var aes = new StandardAesEngine(); + + using var inStream = aes.EncryptStream(outStream, _pbTestKey, _pbIv); + new BinaryWriter(inStream, Encoding.UTF8).Write(_pbTestData); + + //Assert.That(outStream.Position, Is.EqualTo(16)); + + //outStream.Position = 0; + var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16); + + Assert.That(MemUtil.ArraysEqual(outBytes, _pbReferenceCt), Is.True); } [Test] @@ -55,8 +68,10 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher inStream.Write(_pbReferenceCt, 0, _pbReferenceCt.Length); inStream.Position = 0; var aes = new StandardAesEngine(); + using var outStream = aes.DecryptStream(inStream, _pbTestKey, _pbIv); var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16); + Assert.That(MemUtil.ArraysEqual(outBytes, _pbTestData), Is.True); } } diff --git a/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs b/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs index 665671c..2551a76 100644 --- a/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs +++ b/ModernKeePassLib.Test/Keys/KcpKeyFileTests.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using ModernKeePassLib.Keys; using ModernKeePassLib.Utility; using Windows.Storage; -using Windows.Storage.Streams; using NUnit.Framework; namespace ModernKeePassLib.Test.Keys diff --git a/ModernKeePassLib.Test/ModernKeePassLib.Test.csproj b/ModernKeePassLib.Test/ModernKeePassLib.Test.csproj index 71eb005..0da699a 100644 --- a/ModernKeePassLib.Test/ModernKeePassLib.Test.csproj +++ b/ModernKeePassLib.Test/ModernKeePassLib.Test.csproj @@ -6,6 +6,10 @@ false + + TRACE;ModernKeePassLib + + @@ -13,11 +17,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/ModernKeePassLib.Test/Serialization/KdbxFileTests.cs b/ModernKeePassLib.Test/Serialization/KdbxFileTests.cs index 9391443..5d7141b 100644 --- a/ModernKeePassLib.Test/Serialization/KdbxFileTests.cs +++ b/ModernKeePassLib.Test/Serialization/KdbxFileTests.cs @@ -95,7 +95,7 @@ namespace ModernKeePassLib.Test.Serialization var file = new KdbxFile(database); file.Load(ms, KdbxFormat.PlainXml, null); } - Assert.That(database.Color.ToArgb(), Is.EqualTo(Color.Red.ToArgb())); + //Assert.That(database.Color.ToArgb(), Is.EqualTo(Color.Red.ToArgb())); Assert.That(database.Compression, Is.EqualTo(PwCompressionAlgorithm.GZip)); //Assert.That (database.CustomData, Is.EqualTo ()); Assert.True(database.CustomIcons.Count == 0); @@ -118,7 +118,7 @@ namespace ModernKeePassLib.Test.Serialization database.DescriptionChanged = date; database.DefaultUserName = TestDefaultUserName; database.DefaultUserNameChanged = date; - database.Color = Color.Red; + //database.Color = Color.Red; database.MasterKeyChanged = date; database.RecycleBinChanged = date; database.EntryTemplatesGroupChanged = date; diff --git a/ModernKeePassLib.sln b/ModernKeePassLib.sln index 05b52e1..72e80cf 100644 --- a/ModernKeePassLib.sln +++ b/ModernKeePassLib.sln @@ -1,71 +1,31 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 -VisualStudioVersion = 16.0.29009.5 +VisualStudioVersion = 16.0.29911.84 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModernKeePassLib", "ModernKeePassLib\ModernKeePassLib.csproj", "{15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernKeePass.Lib", "ModernKeePassLib\ModernKeePass.Lib.csproj", "{2E710089-9559-4967-846C-E763DD1F3ACB}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModernKeePassLib.Test", "ModernKeePassLib.Test\ModernKeePassLib.Test.csproj", "{4797E768-279A-493A-B21B-CD96AB1F8C20}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModernKeePassLib.Test", "ModernKeePassLib.Test\ModernKeePassLib.Test.csproj", "{25A15ECA-3648-47B2-97DF-491D69126231}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - Debug|ARM = Debug|ARM - Debug|ARM64 = Debug|ARM64 - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU - Release|ARM = Release|ARM - Release|ARM64 = Release|ARM64 - Release|x64 = Release|x64 - Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM.ActiveCfg = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM.Build.0 = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM64.ActiveCfg = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|ARM64.Build.0 = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x64.ActiveCfg = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x64.Build.0 = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x86.ActiveCfg = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Debug|x86.Build.0 = Debug|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|Any CPU.Build.0 = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM.ActiveCfg = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM.Build.0 = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM64.ActiveCfg = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|ARM64.Build.0 = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x64.ActiveCfg = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x64.Build.0 = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x86.ActiveCfg = Release|Any CPU - {15A7E2E5-2B46-4975-AFBD-2898E47E5BA1}.Release|x86.Build.0 = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM.ActiveCfg = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM.Build.0 = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM64.ActiveCfg = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|ARM64.Build.0 = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x64.ActiveCfg = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x64.Build.0 = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x86.ActiveCfg = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Debug|x86.Build.0 = Debug|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|Any CPU.Build.0 = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM.ActiveCfg = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM.Build.0 = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM64.ActiveCfg = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|ARM64.Build.0 = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x64.ActiveCfg = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x64.Build.0 = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x86.ActiveCfg = Release|Any CPU - {4797E768-279A-493A-B21B-CD96AB1F8C20}.Release|x86.Build.0 = Release|Any CPU + {2E710089-9559-4967-846C-E763DD1F3ACB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2E710089-9559-4967-846C-E763DD1F3ACB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2E710089-9559-4967-846C-E763DD1F3ACB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2E710089-9559-4967-846C-E763DD1F3ACB}.Release|Any CPU.Build.0 = Release|Any CPU + {25A15ECA-3648-47B2-97DF-491D69126231}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25A15ECA-3648-47B2-97DF-491D69126231}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25A15ECA-3648-47B2-97DF-491D69126231}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25A15ECA-3648-47B2-97DF-491D69126231}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {57DC80FC-1822-4F0A-9BA0-7BC823DA9521} + SolutionGuid = {5BB1D946-0C93-4219-997F-C2532F355FAE} EndGlobalSection EndGlobal diff --git a/ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs b/ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs new file mode 100644 index 0000000..db97009 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs @@ -0,0 +1,8 @@ +namespace ModernKeePassLib.Cryptography.Cipher +{ + public enum CryptoStreamMode + { + Write, + Read + } +} \ No newline at end of file diff --git a/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs index 866374b..a99a353 100644 --- a/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs +++ b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs @@ -24,17 +24,17 @@ using System.IO; using System.Security; using System.Text; -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Resources; +#else using System.Security.Cryptography; #endif -using ModernKeePassLib.Resources; - namespace ModernKeePassLib.Cryptography.Cipher { public sealed class StandardAesEngine : ICipherEngine { -#if !KeePassUAP +#if !ModernKeePassLib && !KeePassUAP private const CipherMode SaeCipherMode = CipherMode.CBC; private const PaddingMode SaePaddingMode = PaddingMode.PKCS7; #endif @@ -97,7 +97,7 @@ namespace ModernKeePassLib.Cryptography.Cipher { StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV); -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP return StandardAesEngineExt.CreateStream(s, bEncrypt, pbKey, pbIV); #else SymmetricAlgorithm a = CryptoUtil.CreateAes(); diff --git a/ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs new file mode 100644 index 0000000..5f0db6f --- /dev/null +++ b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs @@ -0,0 +1,26 @@ +using System.IO; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.IO; +using Org.BouncyCastle.Crypto.Modes; +using Org.BouncyCastle.Crypto.Paddings; +using Org.BouncyCastle.Crypto.Parameters; + +namespace ModernKeePassLib.Cryptography.Cipher +{ + public class StandardAesEngineExt + { + internal static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV) + { + var cbc = new CbcBlockCipher(new AesEngine()); + //var cbc = new CbcBlockCipher(new RijndaelEngine()); + var bc = new PaddedBufferedBlockCipher(cbc, new Pkcs7Padding()); + var kp = new KeyParameter(pbKey); + var prmIV = new ParametersWithIV(kp, pbIV); + bc.Init(bEncrypt, prmIV); + + var cpRead = bEncrypt ? null : bc; + var cpWrite = bEncrypt ? bc : null; + return new CipherStream(s, cpRead, cpWrite); + } + } +} \ No newline at end of file diff --git a/ModernKeePassLib/Cryptography/CryptoRandom.cs b/ModernKeePassLib/Cryptography/CryptoRandom.cs index 0696b2d..dc46722 100644 --- a/ModernKeePassLib/Cryptography/CryptoRandom.cs +++ b/ModernKeePassLib/Cryptography/CryptoRandom.cs @@ -23,12 +23,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; - -#if !KeePassUAP -using System.Drawing; +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +using Windows.Security.Cryptography; +#else using System.Security.Cryptography; -#endif -#if !ModernKeePassLib using System.Windows.Forms; #endif @@ -190,7 +189,7 @@ namespace ModernKeePassLib.Cryptography fI32(Environment.TickCount); fI64(DateTime.UtcNow.ToBinary()); -#if !KeePassLibSD && !ModernKeePassLib +#if (!ModernKeePassLib && !KeePassLibSD) // In try-catch for systems without GUI; // https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/ try @@ -207,20 +206,21 @@ namespace ModernKeePassLib.Cryptography fI32((int)NativeLib.GetPlatformID()); #if KeePassUAP fStr(EnvironmentExt.OSVersion.VersionString); -#else +#elif !ModernKeePassLib fStr(Environment.OSVersion.VersionString); #endif fI32(Environment.ProcessorCount); -#if !KeePassUAP +#if !ModernKeePassLib && !KeePassUAP fStr(Environment.CommandLine); fI64(Environment.WorkingSet); #endif } catch(Exception) { Debug.Assert(false); } - try +#if !ModernKeePassLib + try { foreach(DictionaryEntry de in Environment.GetEnvironmentVariables()) { @@ -229,12 +229,13 @@ namespace ModernKeePassLib.Cryptography } } catch(Exception) { Debug.Assert(false); } +#endif try { #if KeePassUAP - f(DiagnosticsExt.GetProcessEntropy(), true); -#elif !KeePassLibSD + f(DiagnosticsExt.GetProcessEntropy(), true); +#elif !KeePassLibSD && !ModernKeePassLib using(Process p = Process.GetCurrentProcess()) { fI64(p.Handle.ToInt64()); @@ -281,6 +282,7 @@ namespace ModernKeePassLib.Cryptography { byte[] pb = new byte[32]; + try { m_rng.GetBytes(pb); } catch(Exception) { diff --git a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs index 25d5202..f2bf597 100644 --- a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs +++ b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs @@ -20,7 +20,9 @@ using System; using System.Diagnostics; -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif diff --git a/ModernKeePassLib/Cryptography/CryptoStreamEx.cs b/ModernKeePassLib/Cryptography/CryptoStreamEx.cs index 26bb873..809112b 100644 --- a/ModernKeePassLib/Cryptography/CryptoStreamEx.cs +++ b/ModernKeePassLib/Cryptography/CryptoStreamEx.cs @@ -23,7 +23,7 @@ using System.Diagnostics; using System.IO; using System.Text; -#if !KeePassUAP +#if !ModernKeePassLib && !KeePassUAP using System.Security.Cryptography; namespace ModernKeePassLib.Cryptography diff --git a/ModernKeePassLib/Cryptography/CryptoUtil.cs b/ModernKeePassLib/Cryptography/CryptoUtil.cs index 8f0da22..e875054 100644 --- a/ModernKeePassLib/Cryptography/CryptoUtil.cs +++ b/ModernKeePassLib/Cryptography/CryptoUtil.cs @@ -23,8 +23,10 @@ using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; - -#if !KeePassUAP +using ModernKeePassLib.Cryptography.Cipher; +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif @@ -105,7 +107,8 @@ namespace ModernKeePassLib.Cryptography return pbHash; } - internal static byte[] HashSha256(string strFilePath) +#if !ModernKeePassLib + internal static byte[] HashSha256(string strFilePath) { byte[] pbHash = null; @@ -120,6 +123,7 @@ namespace ModernKeePassLib.Cryptography return pbHash; } +#endif /// /// Create a cryptographic key of length @@ -181,7 +185,7 @@ namespace ModernKeePassLib.Cryptography return pbRet; } -#if !KeePassUAP +#if !ModernKeePassLib private static bool? g_obAesCsp = null; public static SymmetricAlgorithm CreateAes() { diff --git a/ModernKeePassLib/Cryptography/Hash/Blake2b.cs b/ModernKeePassLib/Cryptography/Hash/Blake2b.cs index 3c42fbd..93a4fe2 100644 --- a/ModernKeePassLib/Cryptography/Hash/Blake2b.cs +++ b/ModernKeePassLib/Cryptography/Hash/Blake2b.cs @@ -23,9 +23,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Text; -#if !KeePassUAP +#if ModernKeePassLib +#elif !KeePassUAP using System.Security.Cryptography; #endif @@ -33,8 +35,14 @@ using ModernKeePassLib.Utility; namespace ModernKeePassLib.Cryptography.Hash { - public sealed class Blake2b : HashAlgorithm + public sealed class Blake2b : IDisposable { + protected int HashSizeValue; + protected internal byte[] HashValue; + protected int State = 0; + + private bool m_bDisposed = false; + private const int NbRounds = 12; private const int NbBlockBytes = 128; private const int NbMaxOutBytes = 64; @@ -72,6 +80,23 @@ namespace ModernKeePassLib.Cryptography.Hash private ulong[] m_m = new ulong[16]; private ulong[] m_v = new ulong[16]; + public int HashSize + { + get { return HashSizeValue; } + } + + public byte[] Hash + { + get + { + if (m_bDisposed) + throw new ObjectDisposedException(null); + if (State != 0) + throw new InvalidOperationException("Blak2B Cryptography Hash Not Yet Finalized"); + return (byte[])HashValue.Clone(); + } + } + public Blake2b() { m_cbHashLength = NbMaxOutBytes; @@ -91,7 +116,7 @@ namespace ModernKeePassLib.Cryptography.Hash Initialize(); } - public override void Initialize() + public void Initialize() { Debug.Assert(m_h.Length == g_vIV.Length); Array.Copy(g_vIV, m_h, m_h.Length); @@ -166,7 +191,7 @@ namespace ModernKeePassLib.Cryptography.Hash if(m_t[0] < cb) ++m_t[1]; } - protected override void HashCore(byte[] array, int ibStart, int cbSize) + private void HashCore(byte[] array, int ibStart, int cbSize) { Debug.Assert(m_f[0] == 0); @@ -201,7 +226,7 @@ namespace ModernKeePassLib.Cryptography.Hash } } - protected override byte[] HashFinal() + private byte[] HashFinal() { if(m_f[0] != 0) { Debug.Assert(false); throw new InvalidOperationException(); } Debug.Assert(((m_t[1] == 0) && (m_t[0] == 0)) || @@ -228,5 +253,142 @@ namespace ModernKeePassLib.Cryptography.Hash MemUtil.ZeroByteArray(pbHash); return pbShort; } + + public byte[] ComputeHash(Stream inputStream) + { + if (m_bDisposed) + throw new ObjectDisposedException(null); + + // Default the buffer size to 4K. + byte[] buffer = new byte[4096]; + int bytesRead; + do + { + bytesRead = inputStream.Read(buffer, 0, 4096); + if (bytesRead > 0) + { + HashCore(buffer, 0, bytesRead); + } + } while (bytesRead > 0); + + HashValue = HashFinal(); + byte[] Tmp = (byte[])HashValue.Clone(); + Initialize(); + return (Tmp); + } + + public byte[] ComputeHash(byte[] buffer) + { + if (m_bDisposed) + throw new ObjectDisposedException(null); + + // Do some validation + if (buffer == null) throw new ArgumentNullException("buffer"); + + HashCore(buffer, 0, buffer.Length); + HashValue = HashFinal(); + byte[] Tmp = (byte[])HashValue.Clone(); + Initialize(); + return (Tmp); + } + + public byte[] ComputeHash(byte[] buffer, int offset, int count) + { + // Do some validation + if (buffer == null) + throw new ArgumentNullException("buffer"); + if (offset < 0) + throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum"); + if (count < 0 || (count > buffer.Length)) + throw new ArgumentException("Argument_InvalidValue"); + if ((buffer.Length - count) < offset) + throw new ArgumentException("Argument_InvalidOffLen"); + + if (m_bDisposed) + throw new ObjectDisposedException(null); + + HashCore(buffer, offset, count); + HashValue = HashFinal(); + byte[] Tmp = (byte[])HashValue.Clone(); + Initialize(); + return (Tmp); + } + + public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) + { + // Do some validation, we let BlockCopy do the destination array validation + if (inputBuffer == null) + throw new ArgumentNullException("inputBuffer"); + if (inputOffset < 0) + throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum"); + if (inputCount < 0 || (inputCount > inputBuffer.Length)) + throw new ArgumentException("Argument_InvalidValue"); + if ((inputBuffer.Length - inputCount) < inputOffset) + throw new ArgumentException("Argument_InvalidOffLen"); + + if (m_bDisposed) + throw new ObjectDisposedException(null); + + // Change the State value + State = 1; + HashCore(inputBuffer, inputOffset, inputCount); + if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))) + Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount); + return inputCount; + } + + public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) + { + // Do some validation + if (inputBuffer == null) + throw new ArgumentNullException("inputBuffer"); + if (inputOffset < 0) + throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum"); + if (inputCount < 0 || (inputCount > inputBuffer.Length)) + throw new ArgumentException("Argument_InvalidValue"); + if ((inputBuffer.Length - inputCount) < inputOffset) + throw new ArgumentException("Argument_InvalidOffLen"); + + if (m_bDisposed) + throw new ObjectDisposedException(null); + + HashCore(inputBuffer, inputOffset, inputCount); + HashValue = HashFinal(); + byte[] outputBytes; + if (inputCount != 0) + { + outputBytes = new byte[inputCount]; + Array.Copy(inputBuffer, inputOffset, outputBytes, 0, inputCount); + } + else + { + outputBytes = MemUtil.EmptyByteArray; + } + // reset the State value + State = 0; + return outputBytes; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public void Clear() + { + (this as IDisposable).Dispose(); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + if (HashValue != null) + Array.Clear(HashValue, 0, HashValue.Length); + HashValue = null; + m_bDisposed = true; + } + } } } diff --git a/ModernKeePassLib/Cryptography/Hash/HMAC.cs b/ModernKeePassLib/Cryptography/Hash/HMAC.cs new file mode 100644 index 0000000..281baa9 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/HMAC.cs @@ -0,0 +1,62 @@ +using System; +using Org.BouncyCastle.Crypto.Macs; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public class HMAC : IDisposable + { + protected HMac Hmac; + + public byte[] Hash + { + get + { + var result = new byte[Hmac.GetMacSize()]; + Hmac.DoFinal(result, 0); + return result; + } + } + + public byte[] ComputeHash(byte[] value) + { + return ComputeHash(value, 0, value.Length); + } + + public byte[] ComputeHash(byte[] value, int offset, int length) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + + byte[] resBuf = new byte[Hmac.GetMacSize()]; + Hmac.BlockUpdate(value, offset, length); + Hmac.DoFinal(resBuf, offset); + + return resBuf; + } + + public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) + { + Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount); + if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))) + Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount); + } + + public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) + { + Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount); + byte[] outputBytes = new byte[inputCount]; + if (inputCount != 0) + Buffer.BlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount); + return outputBytes; + } + + public void Initialize() + { + Hmac.Reset(); + } + + public void Dispose() + { + Hmac.Reset(); + } + } +} diff --git a/ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs b/ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs new file mode 100644 index 0000000..1653a82 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs @@ -0,0 +1,10 @@ +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Security.Cryptography.Core; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public class HMACSHA1: HashAlgorithm + { + public HMACSHA1(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(key.AsBuffer())) {} + } +} diff --git a/ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs b/ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs new file mode 100644 index 0000000..49280d8 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs @@ -0,0 +1,10 @@ +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Security.Cryptography.Core; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public class HMACSHA256: HashAlgorithm + { + public HMACSHA256(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(key.AsBuffer())) {} + } +} diff --git a/ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs b/ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs new file mode 100644 index 0000000..093de1f --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs @@ -0,0 +1,105 @@ +using System; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Security.Cryptography.Core; +using Validation; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public abstract class HashAlgorithm: IDisposable + { + /// + /// The platform-specific hash object. + /// + private readonly CryptographicHash _hash; + + /// + /// Initializes a new instance of the class. + /// + /// The platform hash. + internal HashAlgorithm(CryptographicHash hash) + { + Requires.NotNull(hash, "Hash"); + _hash = hash; + } + + public bool CanReuseTransform => true; + public bool CanTransformMultipleBlocks => true; + + public byte[] Hash => _hash.GetValueAndReset().ToArray(); + + public void Append(byte[] data) + { + _hash.Append(data.AsBuffer()); + } + + public byte[] GetValueAndReset() + { + return _hash.GetValueAndReset().ToArray(); + } + + #region ICryptoTransform methods + + public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) + { + byte[] buffer; + if (inputCount < inputBuffer.Length) + { + buffer = new byte[inputCount]; + Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount); + } + else + { + buffer = inputBuffer; + } + + Append(buffer); + if (outputBuffer != null) + { + Array.Copy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount); + } + + return inputCount; + } + + public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) + { + this.TransformBlock(inputBuffer, inputOffset, inputCount, null, 0); + if (inputCount == inputBuffer.Length) + { + return inputBuffer; + } + var buffer = new byte[inputCount]; + Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount); + return buffer; + } + + public byte[] ComputeHash(byte[] value) + { + return ComputeHash(value, 0, value.Length); + } + + public byte[] ComputeHash(byte[] value, int offset, int length) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + + TransformFinalBlock(value, offset, length); + var resBuf = GetValueAndReset(); + + return resBuf; + } + public void Initialize() + { + } + + public void Dispose() + { + } + + public void Clear() + { + _hash.GetValueAndReset(); + } + + #endregion + } +} diff --git a/ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs b/ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs new file mode 100644 index 0000000..1996667 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs @@ -0,0 +1,9 @@ +using Windows.Security.Cryptography.Core; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public class SHA256Managed : HashAlgorithm + { + public SHA256Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash()) {} + } +} diff --git a/ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs b/ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs new file mode 100644 index 0000000..da1f8f5 --- /dev/null +++ b/ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs @@ -0,0 +1,10 @@ +using System; +using Windows.Security.Cryptography.Core; + +namespace ModernKeePassLib.Cryptography.Hash +{ + public class SHA512Managed: HashAlgorithm + { + public SHA512Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash()) {} + } +} diff --git a/ModernKeePassLib/Cryptography/HashingStreamEx.cs b/ModernKeePassLib/Cryptography/HashingStreamEx.cs index 9010225..8ab086d 100644 --- a/ModernKeePassLib/Cryptography/HashingStreamEx.cs +++ b/ModernKeePassLib/Cryptography/HashingStreamEx.cs @@ -22,8 +22,9 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; - -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif diff --git a/ModernKeePassLib/Cryptography/HmacOtp.cs b/ModernKeePassLib/Cryptography/HmacOtp.cs index d0aa2b1..8ca1d19 100644 --- a/ModernKeePassLib/Cryptography/HmacOtp.cs +++ b/ModernKeePassLib/Cryptography/HmacOtp.cs @@ -21,8 +21,9 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Text; - -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif diff --git a/ModernKeePassLib/Cryptography/KeyDerivation/AesKdf.cs b/ModernKeePassLib/Cryptography/KeyDerivation/AesKdf.cs index e323311..a3a6106 100644 --- a/ModernKeePassLib/Cryptography/KeyDerivation/AesKdf.cs +++ b/ModernKeePassLib/Cryptography/KeyDerivation/AesKdf.cs @@ -22,7 +22,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Text; -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; @@ -122,6 +122,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation try { #if !ModernKeePassLib + // Try to use the native library first if(NativeLib.TransformKey256(pbNewKey, pbKeySeed32, uNumRounds)) return CryptoUtil.HashSha256(pbNewKey); #endif @@ -140,18 +141,16 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation public static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32, ulong uNumRounds) { -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP KeyParameter kp = new KeyParameter(pbKeySeed32); AesEngine aes = new AesEngine(); aes.Init(true, kp); - for(ulong u = 0; u < uNumRounds; ++u) + for(ulong i = 0; i < uNumRounds; ++i) { aes.ProcessBlock(pbNewKey32, 0, pbNewKey32, 0); aes.ProcessBlock(pbNewKey32, 16, pbNewKey32, 16); } - - aes.Reset(); #else byte[] pbIV = new byte[16]; @@ -213,7 +212,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation pbNewKey[i] = (byte)i; } -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP KeyParameter kp = new KeyParameter(pbKey); AesEngine aes = new AesEngine(); aes.Init(true, kp); @@ -248,7 +247,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation { for(ulong j = 0; j < BenchStep; ++j) { -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP aes.ProcessBlock(pbNewKey, 0, pbNewKey, 0); aes.ProcessBlock(pbNewKey, 16, pbNewKey, 16); #else @@ -269,7 +268,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation } p.SetUInt64(ParamRounds, uRounds); -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP aes.Reset(); #else } diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs index f10d331..8239346 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs @@ -21,11 +21,13 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; - -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif +using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; @@ -163,5 +165,58 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator psOut = pwd; return PwgError.Success; } + + internal static string ErrorToString(PwgError e, bool bHeader) + { + if(e == PwgError.Success) { Debug.Assert(false); return string.Empty; } + if((e == PwgError.Unknown) && bHeader) return KLRes.PwGenFailed; + + string str = KLRes.UnknownError; + switch(e) + { + // case PwgError.Success: + // break; + + case PwgError.Unknown: + break; + + case PwgError.TooFewCharacters: + str = KLRes.CharSetTooFewChars; + break; + + case PwgError.UnknownAlgorithm: + str = KLRes.AlgorithmUnknown; + break; + + case PwgError.InvalidCharSet: + str = KLRes.CharSetInvalid; + break; + + case PwgError.InvalidPattern: + str = KLRes.PatternInvalid; + break; + + default: + Debug.Assert(false); + break; + } + + if(bHeader) + str = KLRes.PwGenFailed + MessageService.NewParagraph + str; + + return str; + } + + internal static string ErrorToString(Exception ex, bool bHeader) + { + string str = KLRes.UnknownError; + if((ex != null) && !string.IsNullOrEmpty(ex.Message)) + str = ex.Message; + + if(bHeader) + str = KLRes.PwGenFailed + MessageService.NewParagraph + str; + + return str; + } } } diff --git a/ModernKeePassLib/Cryptography/PopularPasswords.cs b/ModernKeePassLib/Cryptography/PopularPasswords.cs index 0a43d1b..39d5c3a 100644 --- a/ModernKeePassLib/Cryptography/PopularPasswords.cs +++ b/ModernKeePassLib/Cryptography/PopularPasswords.cs @@ -65,7 +65,14 @@ namespace ModernKeePassLib.Cryptography if(vPassword.Length == 0) { uDictSize = 0; return false; } #if DEBUG +#if ModernKeePassLib + foreach (var ch in vPassword) + { + Debug.Assert(ch == char.ToLower(ch)); + } +#else Array.ForEach(vPassword, ch => Debug.Assert(ch == char.ToLower(ch))); +#endif #endif try { return IsPopularPasswordPriv(vPassword, out uDictSize); } diff --git a/ModernKeePassLib/Cryptography/ProtectedData.cs b/ModernKeePassLib/Cryptography/ProtectedData.cs index c82a7d6..c25562d 100644 --- a/ModernKeePassLib/Cryptography/ProtectedData.cs +++ b/ModernKeePassLib/Cryptography/ProtectedData.cs @@ -1,5 +1,4 @@ using System; -using System.Security.Cryptography; using ModernKeePassLib.Native; namespace ModernKeePassLib.Cryptography diff --git a/ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs b/ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs new file mode 100644 index 0000000..86fba19 --- /dev/null +++ b/ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs @@ -0,0 +1,17 @@ +using System; + +namespace ModernKeePassLib.Cryptography +{ + public class RNGCryptoServiceProvider: IDisposable + { + public void GetBytes(byte[] pb) + { + var random = new Random(); + random.NextBytes(pb); + } + + public void Dispose() + { + } + } +} \ No newline at end of file diff --git a/ModernKeePassLib/Cryptography/SelfTest.cs b/ModernKeePassLib/Cryptography/SelfTest.cs index 36480c3..b6a928c 100644 --- a/ModernKeePassLib/Cryptography/SelfTest.cs +++ b/ModernKeePassLib/Cryptography/SelfTest.cs @@ -26,7 +26,7 @@ using System.Runtime.InteropServices; using System.Security; using System.Text; -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; @@ -105,7 +105,7 @@ namespace ModernKeePassLib.Cryptography internal static void TestFipsComplianceProblems() { -#if !KeePassUAP +#if !ModernKeePassLib try { using(RijndaelManaged r = new RijndaelManaged()) { } } catch(Exception exAes) { @@ -131,7 +131,7 @@ namespace ModernKeePassLib.Cryptography 0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22, 0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7 }; -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP AesEngine aes = new AesEngine(); aes.Init(true, new KeyParameter(pbKey)); if(aes.GetBlockSize() != pbData.Length) @@ -550,7 +550,7 @@ namespace ModernKeePassLib.Cryptography // (test vector for Argon2d 1.3); also on // https://tools.ietf.org/html/draft-irtf-cfrg-argon2-00 - KdfParameters p = kdf.GetDefaultParameters(); + var p = kdf.GetDefaultParameters(); kdf.Randomize(p); Debug.Assert(p.GetUInt32(Argon2Kdf.ParamVersion, 0) == 0x13U); @@ -766,7 +766,7 @@ namespace ModernKeePassLib.Cryptography pbMan = CryptoUtil.HashSha256(pbMan); AesKdf kdf = new AesKdf(); - KdfParameters p = kdf.GetDefaultParameters(); + var p = kdf.GetDefaultParameters(); p.SetUInt64(AesKdf.ParamRounds, uRounds); p.SetByteArray(AesKdf.ParamSeed, pbSeed); byte[] pbKdf = kdf.Transform(pbKey, p); @@ -778,7 +778,7 @@ namespace ModernKeePassLib.Cryptography private static void TestNativeKeyTransform(Random r) { -#if DEBUG +#if !ModernKeePassLib && DEBUG byte[] pbOrgKey = CryptoRandom.Instance.GetRandomBytes(32); byte[] pbSeed = CryptoRandom.Instance.GetRandomBytes(32); ulong uRounds = (ulong)r.Next(1, 0x3FFF); @@ -915,7 +915,7 @@ namespace ModernKeePassLib.Cryptography private static void TestNativeLib() { -#if DEBUG && !ModernKeePassLib +#if DEBUG if(NativeLib.IsUnix()) { if(NativeLib.EncodeDataToArgs("A\"B C\\D") != @@ -973,31 +973,31 @@ namespace ModernKeePassLib.Cryptography throw new InvalidOperationException("MemUtil-7"); byte[] pbRes = MemUtil.ParseBase32("MY======"); - byte[] pbExp = Encoding.ASCII.GetBytes("f"); + byte[] pbExp = Encoding.UTF8.GetBytes("f"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-1"); pbRes = MemUtil.ParseBase32("MZXQ===="); - pbExp = Encoding.ASCII.GetBytes("fo"); + pbExp = Encoding.UTF8.GetBytes("fo"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-2"); pbRes = MemUtil.ParseBase32("MZXW6==="); - pbExp = Encoding.ASCII.GetBytes("foo"); + pbExp = Encoding.UTF8.GetBytes("foo"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-3"); pbRes = MemUtil.ParseBase32("MZXW6YQ="); - pbExp = Encoding.ASCII.GetBytes("foob"); + pbExp = Encoding.UTF8.GetBytes("foob"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-4"); pbRes = MemUtil.ParseBase32("MZXW6YTB"); - pbExp = Encoding.ASCII.GetBytes("fooba"); + pbExp = Encoding.UTF8.GetBytes("fooba"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-5"); pbRes = MemUtil.ParseBase32("MZXW6YTBOI======"); - pbExp = Encoding.ASCII.GetBytes("foobar"); + pbExp = Encoding.UTF8.GetBytes("foobar"); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-6"); pbRes = MemUtil.ParseBase32("JNSXSIDQOJXXM2LEMVZCAYTBONSWIIDPNYQG63TFFV2GS3LFEBYGC43TO5XXEZDTFY======"); - pbExp = Encoding.ASCII.GetBytes("Key provider based on one-time passwords."); + pbExp = Encoding.UTF8.GetBytes("Key provider based on one-time passwords."); if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-7"); int i = 0 - 0x10203040; @@ -1150,7 +1150,7 @@ namespace ModernKeePassLib.Cryptography private static void TestUrlUtil() { #if DEBUG -#if !KeePassUAP +#if !ModernKeePassLib && !KeePassUAP Debug.Assert(Uri.UriSchemeHttp.Equals("http", StrUtil.CaseIgnoreCmp)); Debug.Assert(Uri.UriSchemeHttps.Equals("https", StrUtil.CaseIgnoreCmp)); #endif diff --git a/ModernKeePassLib/Keys/CompositeKey.cs b/ModernKeePassLib/Keys/CompositeKey.cs index f35991b..8663cf4 100644 --- a/ModernKeePassLib/Keys/CompositeKey.cs +++ b/ModernKeePassLib/Keys/CompositeKey.cs @@ -26,7 +26,6 @@ using System.Threading; using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Interfaces; -using ModernKeePassLib.Native; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; @@ -122,7 +121,7 @@ namespace ModernKeePassLib.Keys { if(pKey == null) { Debug.Assert(false); continue; } -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP if(pKey.GetType() == tUserKeyType) return true; #else @@ -149,7 +148,7 @@ namespace ModernKeePassLib.Keys { if(pKey == null) { Debug.Assert(false); continue; } -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP if(pKey.GetType() == tUserKeyType) return pKey; #else @@ -279,9 +278,11 @@ namespace ModernKeePassLib.Keys internal ProtectedBinary GenerateKey32Ex(KdfParameters p, IStatusLogger sl) { - if(sl == null) return GenerateKey32(p); - - CkGkTaskInfo ti = new CkGkTaskInfo(); +#if ModernKeePassLib + return GenerateKey32(p); +#else + if (sl == null) return GenerateKey32(p); + CkGkTaskInfo ti = new CkGkTaskInfo(); ThreadStart f = delegate() { @@ -319,6 +320,7 @@ namespace ModernKeePassLib.Keys Debug.Assert(ti.Key != null); return ti.Key; +#endif } private void ValidateUserKeys() diff --git a/ModernKeePassLib/Keys/KcpKeyFile.cs b/ModernKeePassLib/Keys/KcpKeyFile.cs index fb5ae22..e50e687 100644 --- a/ModernKeePassLib/Keys/KcpKeyFile.cs +++ b/ModernKeePassLib/Keys/KcpKeyFile.cs @@ -248,6 +248,33 @@ namespace ModernKeePassLib.Keys try { +#if ModernKeePassLib + + var doc = XDocument.Load(ms); + + var el = doc.Root; + + if((el == null) || !el.Name.LocalName.Equals(RootElementName)) + return null; + if(el.DescendantNodes().Count() < 2) + return null; + + foreach(var xmlChild in el.Descendants()) + { + if(xmlChild.Name == MetaElementName) { } // Ignore Meta + else if(xmlChild.Name == KeyElementName) + { + foreach(var xmlKeyChild in xmlChild.Descendants()) + { + if(xmlKeyChild.Name == KeyDataElementName) + { + if(pbKeyData == null) + pbKeyData = Convert.FromBase64String(xmlKeyChild.Value); + } + } + } + } +#else XmlDocument doc = XmlUtilEx.CreateXmlDocument(); doc.Load(ms); @@ -270,6 +297,7 @@ namespace ModernKeePassLib.Keys } } } +#endif } catch(Exception) { pbKeyData = null; } finally { ms.Dispose(); } diff --git a/ModernKeePassLib/Keys/KcpPassword.cs b/ModernKeePassLib/Keys/KcpPassword.cs index 38f6e63..ec4e361 100644 --- a/ModernKeePassLib/Keys/KcpPassword.cs +++ b/ModernKeePassLib/Keys/KcpPassword.cs @@ -99,8 +99,13 @@ namespace ModernKeePassLib.Keys { try { +#if ModernKeePassLib + // TODO: find a way to implement this + return true; +#else string str = StrUtil.Utf8.GetString(pb); return str.IsNormalized(NormalizationForm.FormC); +#endif } catch(Exception) { Debug.Assert(false); } diff --git a/ModernKeePassLib/Keys/KcpUserAccount.cs b/ModernKeePassLib/Keys/KcpUserAccount.cs index baae05a..3685494 100644 --- a/ModernKeePassLib/Keys/KcpUserAccount.cs +++ b/ModernKeePassLib/Keys/KcpUserAccount.cs @@ -23,10 +23,12 @@ using System.IO; using System.Security; #if ModernKeePassLib -using System.Security.Cryptography; +using Windows.Storage; +using Windows.Security.Cryptography; #endif using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Native; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; @@ -87,6 +89,8 @@ namespace ModernKeePassLib.Keys { #if KeePassUAP string strUserDir = EnvironmentExt.AppDataRoamingFolderPath; +#elif ModernKeePassLib + string strUserDir = Windows.Storage.ApplicationData.Current.RoamingFolder.Path; #else string strUserDir = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData); @@ -95,8 +99,11 @@ namespace ModernKeePassLib.Keys strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); strUserDir += PwDefs.ShortProductName; +#if !ModernKeePassLib + if(bCreate && !Directory.Exists(strUserDir)) Directory.CreateDirectory(strUserDir); +#endif strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); return (strUserDir + UserKeyFileName); @@ -110,7 +117,15 @@ namespace ModernKeePassLib.Keys try { 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(); +#else byte[] pbProtectedKey = File.ReadAllBytes(strFilePath); +#endif pbKey = CryptoUtil.UnprotectData(pbProtectedKey, m_pbEntropy, DataProtectionScope.CurrentUser); @@ -135,8 +150,13 @@ namespace ModernKeePassLib.Keys byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64); byte[] pbProtectedKey = CryptoUtil.ProtectData(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(); +#else File.WriteAllBytes(strFilePath, pbProtectedKey); +#endif byte[] pbKey = LoadUserKey(true); Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey)); diff --git a/ModernKeePassLib/ModernKeePass.Lib.csproj b/ModernKeePassLib/ModernKeePass.Lib.csproj new file mode 100644 index 0000000..c0b0e38 --- /dev/null +++ b/ModernKeePassLib/ModernKeePass.Lib.csproj @@ -0,0 +1,194 @@ + + + + Debug + AnyCPU + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + {2E710089-9559-4967-846C-E763DD1F3ACB} + Library + Properties + ModernKeePassLib + ModernKeePassLib + false + + + 2.0 + + + + + v5.0 + KeePassLib.pfx + + + 14.0 + + + true + full + false + bin\Debug + DEBUG;ModernKeePassLib + prompt + 4 + AnyCPU + + + true + bin\Release + prompt + 4 + ModernKeePassLib + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + Libs\Windows.winmd + False + + + + \ No newline at end of file diff --git a/ModernKeePassLib/ModernKeePassLib.csproj b/ModernKeePassLib/ModernKeePassLib.csproj deleted file mode 100644 index 4f9b027..0000000 --- a/ModernKeePassLib/ModernKeePassLib.csproj +++ /dev/null @@ -1,62 +0,0 @@ - - - - netstandard2.1 - true - 2.44.0 - Geoffroy Bonneville - https://www.gnu.org/licenses/gpl-3.0.en.html - https://github.com/wismna/ModernKeePassLib - Portable KeePass Password Management Library that targets .Net Standard. Allows reading, editing and writing to KeePass 2.x databases. - wismna - ModernKeePassLib - Save corrections - KeePass KeePassLib Portable PCL NetStandard ModernKeePass - Copyright © 2020 Geoffroy Bonneville - - - - TRACE;ModernKeePassLib - - - - TRACE;ModernKeePassLib - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ModernKeePassLib/ModernKeePassLib.nuspec b/ModernKeePassLib/ModernKeePassLib.nuspec new file mode 100644 index 0000000..e6e594b --- /dev/null +++ b/ModernKeePassLib/ModernKeePassLib.nuspec @@ -0,0 +1,36 @@ + + + + ModernKeePassLib + 2.39.1 + ModernKeePassLib + Geoffroy Bonneville + Geoffroy Bonneville + https://www.gnu.org/licenses/gpl-3.0.en.html + https://github.com/wismna/ModernKeePass + false + Portable KeePass Password Management Library that targets .Net Standard and WinRT. Allows reading, editing and writing to KeePass 2.x databases. + Implementation of KeePass library version 2.39.1 + Copyright © 2018 Geoffroy Bonneville + KeePass KeePassLib Portable PCL NetStandard + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ModernKeePassLib/Native/ClipboardU.cs b/ModernKeePassLib/Native/ClipboardU.cs index f284236..87760b4 100644 --- a/ModernKeePassLib/Native/ClipboardU.cs +++ b/ModernKeePassLib/Native/ClipboardU.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2020 Dominik Reichl + Copyright (C) 2003-2018 Dominik Reichl This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/ModernKeePassLib/Native/Native.PCL.cs b/ModernKeePassLib/Native/Native.Standard.cs similarity index 77% rename from ModernKeePassLib/Native/Native.PCL.cs rename to ModernKeePassLib/Native/Native.Standard.cs index b08cf18..ba13b9e 100644 --- a/ModernKeePassLib/Native/Native.PCL.cs +++ b/ModernKeePassLib/Native/Native.Standard.cs @@ -13,31 +13,20 @@ namespace ModernKeePassLib.Native return true; } - public static bool TransformKey256(byte[] pbNative, byte[] pbSeed, ulong uRounds) - { - return false; - } - - public static System.PlatformID GetPlatformID() - { - return Environment.OSVersion.Platform; - } - - internal static string DecodeArgsToPath(string strApp) - { - if (!string.IsNullOrEmpty(strApp)) return strApp; - return string.Empty; - } - - internal static string EncodeDataToArgs(string v) - { - throw new NotImplementedException(); - } - - public static string DecodeArgsToData(string strApp) + public static string EncodeDataToArgs(string data) { throw new NotImplementedException(); } + public static string DecodeArgsToData(string strArgs) + { + throw new NotImplementedException(); + } + + internal static int GetPlatformID() + { + return Environment.CurrentManagedThreadId; + } + } internal static class NativeMethods @@ -82,6 +71,12 @@ namespace ModernKeePassLib.Native throw new NotImplementedException(); } } + + public enum DataProtectionScope + { + CurrentUser, + LocalMachine + } internal enum MemoryProtectionScope { @@ -102,5 +97,4 @@ namespace ModernKeePassLib.Native throw new NotImplementedException(); } } -} - +} \ No newline at end of file diff --git a/ModernKeePassLib/Native/NativeLib.cs b/ModernKeePassLib/Native/NativeLib.cs index 02b631f..92fc17e 100644 --- a/ModernKeePassLib/Native/NativeLib.cs +++ b/ModernKeePassLib/Native/NativeLib.cs @@ -28,10 +28,8 @@ using System.Text.RegularExpressions; #if !KeePassUAP using System.IO; using System.Threading; -#if !ModernKeePassLib using System.Windows.Forms; #endif -#endif using ModernKeePassLib.Utility; @@ -288,7 +286,6 @@ namespace ModernKeePassLib.Native return null; }; -#if !ModernKeePassLib if((f & AppRunFlags.DoEvents) != AppRunFlags.None) { List
lDisabledForms = new List(); @@ -318,7 +315,7 @@ namespace ModernKeePassLib.Native return strRet; } -#endif + return fnRun(); } @@ -367,7 +364,7 @@ namespace ModernKeePassLib.Native public static bool TransformKey256(byte[] pBuf256, byte[] pKey256, ulong uRounds) { -#if KeePassUAP || ModernKeePassLib +#if KeePassUAP return false; #else if(!m_bAllowNative) return false; @@ -398,7 +395,7 @@ namespace ModernKeePassLib.Native { puRounds = 0; -#if KeePassUAP || ModernKeePassLib +#if KeePassUAP return false; #else if(!m_bAllowNative) return false; diff --git a/ModernKeePassLib/Native/SimpleStat.Standard.cs b/ModernKeePassLib/Native/SimpleStat.Standard.cs new file mode 100644 index 0000000..0433b00 --- /dev/null +++ b/ModernKeePassLib/Native/SimpleStat.Standard.cs @@ -0,0 +1,7 @@ +namespace ModernKeePassLib.Native +{ + internal sealed class SimpleStat + { + + } +} \ No newline at end of file diff --git a/ModernKeePassLib/Properties/AssemblyInfo.cs b/ModernKeePassLib/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..411f4d2 --- /dev/null +++ b/ModernKeePassLib/Properties/AssemblyInfo.cs @@ -0,0 +1,44 @@ +/* + KeePass Password Safe - The Open-Source Password Manager + Copyright (C) 2003-2020 Dominik Reichl + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General assembly properties +[assembly: AssemblyTitle("ModernKeePassLib")] +[assembly: AssemblyDescription("KeePass Password Management Library for .Net Standard")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("wismna")] +[assembly: AssemblyProduct("ModernKeePassLib")] +[assembly: AssemblyCopyright("Copyright © 2020 Geoffroy Bonneville")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// COM settings +[assembly: ComVisible(false)] + +#if !ModernKeePassLib +// Assembly GUID +[assembly: Guid("395f6eec-a1e0-4438-aa82-b75099348134")] +#endif + +// Assembly version information +[assembly: AssemblyVersion("2.44.1.*")] +[assembly: AssemblyFileVersion("2.44.1.0")] diff --git a/ModernKeePassLib/PwDatabase.cs b/ModernKeePassLib/PwDatabase.cs index 671d166..35bd1ec 100644 --- a/ModernKeePassLib/PwDatabase.cs +++ b/ModernKeePassLib/PwDatabase.cs @@ -49,8 +49,6 @@ namespace ModernKeePassLib internal const int DefaultHistoryMaxItems = 10; // -1 = unlimited internal const long DefaultHistoryMaxSize = 6 * 1024 * 1024; // -1 = unlimited - private static bool m_bPrimaryCreated = false; - // Initializations: see Clear() private PwGroup m_pgRootGroup = null; private PwObjectList m_vDeletedObjects = new PwObjectList(); diff --git a/ModernKeePassLib/PwDefs.cs b/ModernKeePassLib/PwDefs.cs index 58b4f67..66174cf 100644 --- a/ModernKeePassLib/PwDefs.cs +++ b/ModernKeePassLib/PwDefs.cs @@ -55,20 +55,20 @@ namespace ModernKeePassLib /// e.g. 2.19 = 0x02130000. /// It is highly recommended to use FileVersion64 instead. ///
- public static readonly uint Version32 = 0x022A0100; + public static readonly uint Version32 = 0x022C0000; /// /// Version, encoded as 64-bit unsigned integer /// (component-wise, 16 bits per component). /// - public static readonly ulong FileVersion64 = 0x0002002A00010000UL; + public static readonly ulong FileVersion64 = 0x0002002C00000000UL; /// /// Version, encoded as string. /// - public static readonly string VersionString = "2.42.1"; + public static readonly string VersionString = "2.44"; - public static readonly string Copyright = @"Copyright © 2003-2019 Dominik Reichl"; + public static readonly string Copyright = @"Copyright © 2003-2020 Dominik Reichl"; /// /// Product website URL. Terminated by a forward slash. diff --git a/ModernKeePassLib/PwEntry.cs b/ModernKeePassLib/PwEntry.cs index 8b5b19c..d379430 100644 --- a/ModernKeePassLib/PwEntry.cs +++ b/ModernKeePassLib/PwEntry.cs @@ -21,7 +21,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; +#if !KeePassUAP using System.Drawing; +#endif using ModernKeePassLib.Collections; using ModernKeePassLib.Interfaces; @@ -936,7 +938,7 @@ namespace ModernKeePassLib if(m_bCompareNaturally) return StrUtil.CompareNaturally(strA, strB); -#if ModernKeePassLib || KeePassRT +#if ModernKeePassLib return string.Compare(strA, strB, m_bCaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture); #else diff --git a/ModernKeePassLib/Security/ProtectedBinary.cs b/ModernKeePassLib/Security/ProtectedBinary.cs index 8ec5841..04ba869 100644 --- a/ModernKeePassLib/Security/ProtectedBinary.cs +++ b/ModernKeePassLib/Security/ProtectedBinary.cs @@ -21,10 +21,6 @@ using System; using System.Diagnostics; using System.Threading; -#if !KeePassUAP -using System.Security.Cryptography; -#endif - using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Native; diff --git a/ModernKeePassLib/Security/ProtectedString.cs b/ModernKeePassLib/Security/ProtectedString.cs index 01dd879..1eec195 100644 --- a/ModernKeePassLib/Security/ProtectedString.cs +++ b/ModernKeePassLib/Security/ProtectedString.cs @@ -394,8 +394,8 @@ namespace ModernKeePassLib.Security if(a == null) throw new ArgumentNullException("a"); if(b == null) throw new ArgumentNullException("b"); - if(b.IsEmpty) return a; - if(a.IsEmpty) return b; + if(b.IsEmpty) return a.WithProtection(a.IsProtected || b.IsProtected); + if(a.IsEmpty) return b.WithProtection(a.IsProtected || b.IsProtected); if(!a.IsProtected && !b.IsProtected) return new ProtectedString(false, a.ReadString() + b.ReadString()); diff --git a/ModernKeePassLib/Serialization/FileTransactionEx.cs b/ModernKeePassLib/Serialization/FileTransactionEx.cs index c40822b..85ffb25 100644 --- a/ModernKeePassLib/Serialization/FileTransactionEx.cs +++ b/ModernKeePassLib/Serialization/FileTransactionEx.cs @@ -23,7 +23,8 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Text; -#if (!ModernKeePassLib && !KeePassUAP) + +#if !ModernKeePassLib && !KeePassLibSD using System.Security.AccessControl; #endif @@ -220,15 +221,15 @@ namespace ModernKeePassLib.Serialization // FileAttributes faBase = FileAttributes.Normal; try { -#if !ModernKeePassLib +#if !(ModernKeePassLib || KeePassUAP) FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); try { if(bEfsEncrypted) File.Decrypt(m_iocBase.Path); } // For TxF catch(Exception) { Debug.Assert(false); } -#endif + otCreation = File.GetCreationTimeUtc(m_iocBase.Path); sStat = SimpleStat.Get(m_iocBase.Path); -#if !ModernKeePassLib + // May throw with Mono FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs); if(sec != null) pbSec = sec.GetSecurityDescriptorBinaryForm(); @@ -522,8 +523,8 @@ namespace ModernKeePassLib.Serialization #endif return false; } - - internal static void ClearOld() +#if !ModernKeePassLib + internal static void ClearOld() { try { @@ -545,5 +546,6 @@ namespace ModernKeePassLib.Serialization } catch(Exception) { Debug.Assert(false); } } +#endif } } diff --git a/ModernKeePassLib/Serialization/HmacBlockStream.cs b/ModernKeePassLib/Serialization/HmacBlockStream.cs index 329e6ec..6706332 100644 --- a/ModernKeePassLib/Serialization/HmacBlockStream.cs +++ b/ModernKeePassLib/Serialization/HmacBlockStream.cs @@ -23,7 +23,9 @@ using System.Diagnostics; using System.IO; using System.Text; -#if !KeePassUAP +#if ModernKeePassLib +using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP using System.Security.Cryptography; #endif @@ -39,7 +41,7 @@ namespace ModernKeePassLib.Serialization private Stream m_sBase; private readonly bool m_bWriting; private readonly bool m_bVerify; - private byte[] m_pbKey; + private readonly byte[] m_pbKey; private bool m_bEos = false; private byte[] m_pbBuffer; diff --git a/ModernKeePassLib/Serialization/IOConnection.cs b/ModernKeePassLib/Serialization/IOConnection.cs index 57dac75..9ccfc55 100644 --- a/ModernKeePassLib/Serialization/IOConnection.cs +++ b/ModernKeePassLib/Serialization/IOConnection.cs @@ -24,6 +24,7 @@ using System.IO; using System.Net; using System.Reflection; using System.Text; + #if (!ModernKeePassLib && !KeePassLibSD && !KeePassUAP) using System.Net.Cache; using System.Net.Security; @@ -33,12 +34,16 @@ using System.Net.Security; using System.Security.Cryptography.X509Certificates; #endif +#if ModernKeePassLib +using Windows.Storage; +using Windows.Storage.Streams; +#endif using ModernKeePassLib.Native; using ModernKeePassLib.Utility; namespace ModernKeePassLib.Serialization { -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD internal sealed class IOWebClient : WebClient { private IOConnectionInfo m_ioc; @@ -240,7 +245,7 @@ namespace ModernKeePassLib.Serialization public static class IOConnection { -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD private static ProxyServerType m_pstProxyType = ProxyServerType.System; private static string m_strProxyAddr = string.Empty; private static string m_strProxyPort = string.Empty; @@ -269,7 +274,7 @@ namespace ModernKeePassLib.Serialization public static event EventHandler IOAccessPre; -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD // Allow self-signed certificates, expired certificates, etc. private static bool AcceptCertificate(object sender, X509Certificate certificate, X509Chain chain, @@ -603,7 +608,7 @@ namespace ModernKeePassLib.Serialization #endif } -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD public static Stream OpenWrite(IOConnectionInfo ioc) { if(ioc == null) { Debug.Assert(false); return null; } @@ -802,7 +807,7 @@ namespace ModernKeePassLib.Serialization #endif } -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD private static bool SendCommand(IOConnectionInfo ioc, string strMethod) { try @@ -910,6 +915,6 @@ namespace ModernKeePassLib.Serialization return (wr is FileWebRequest); #endif } -#endif // ModernKeePass +#endif // ModernKeePassLib } } diff --git a/ModernKeePassLib/Serialization/IOConnectionInfo.cs b/ModernKeePassLib/Serialization/IOConnectionInfo.cs index a2cec31..2ceb78c 100644 --- a/ModernKeePassLib/Serialization/IOConnectionInfo.cs +++ b/ModernKeePassLib/Serialization/IOConnectionInfo.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2020 Dominik Reichl + Copyright (C) 2003-2018 Dominik Reichl This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs index 668b965..a762ec4 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs @@ -30,14 +30,13 @@ using System.Drawing; using ModernKeePassLib; using ModernKeePassLib.Collections; +using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Interfaces; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; -using ModernKeePassLib.Cryptography; -using ModernKeePassLib.Cryptography.Cipher; - namespace ModernKeePassLib.Serialization { /// @@ -758,13 +757,21 @@ namespace ModernKeePassLib.Serialization } m_bReadNextNode = false; // ReadElementString skips end tag +#if ModernKeePassLib + return xr.ReadElementContentAsString(); +#else return xr.ReadElementString(); +#endif } private string ReadStringRaw(XmlReader xr) { m_bReadNextNode = false; // ReadElementString skips end tag +#if ModernKeePassLib + return xr.ReadElementContentAsString(); +#else return xr.ReadElementString(); +#endif } private byte[] ReadBase64(XmlReader xr, bool bRaw) diff --git a/ModernKeePassLib/Serialization/KdbxFile.Write.cs b/ModernKeePassLib/Serialization/KdbxFile.Write.cs index 59de6c4..095d198 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Write.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Write.cs @@ -26,7 +26,7 @@ using System.Security; using System.Text; using System.Xml; -#if !KeePassUAP +#if !ModernKeePassLib && !KeePassUAP using System.Drawing; using System.Security.Cryptography; #endif @@ -204,7 +204,7 @@ namespace ModernKeePassLib.Serialization throw new ArgumentOutOfRangeException("fmt"); } - m_xmlWriter = XmlUtilEx.CreateXmlWriter(sXml); + m_xmlWriter = XmlUtilEx.CreateXmlWriter(sXml, m_uFileVersion >= FileVersion32_4); WriteDocument(pgRoot); diff --git a/ModernKeePassLib/Serialization/KdbxFile.cs b/ModernKeePassLib/Serialization/KdbxFile.cs index 5ac3ad0..6abcc85 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.cs @@ -26,8 +26,9 @@ using System.Security; using System.Text; using System.Xml; -#if !KeePassUAP -using System.Security.Cryptography; +#if ModernKeePassLib +using Windows.Storage; +using ModernKeePassLib.Cryptography.Hash; #endif using ModernKeePassLib.Collections; @@ -511,11 +512,33 @@ namespace ModernKeePassLib.Serialization ++iTry; } +#if ModernKeePassLib + while (StorageFile.GetFileFromPathAsync(strPath).GetResults() != null); +#else while(File.Exists(strPath)); +#endif +#if ModernKeePassLib + byte[] pbData = pb.ReadData(); + /*var file = FileSystem.Current.GetFileFromPathAsync(strPath).Result; + using (var stream = file.OpenAsync(FileAccess.ReadAndWrite).Result) {*/ + var file = StorageFile.GetFileFromPathAsync(strPath).GetAwaiter().GetResult(); + using (var stream = file.OpenAsync(FileAccessMode.ReadWrite).GetAwaiter().GetResult().AsStream()) + { + stream.Write (pbData, 0, pbData.Length); + } + MemUtil.ZeroByteArray(pbData); +#elif !KeePassLibSD + byte[] pbData = pb.ReadData(); + File.WriteAllBytes(strPath, pbData); + MemUtil.ZeroByteArray(pbData); +#else + FileStream fs = new FileStream(strPath, FileMode.Create, + FileAccess.Write, FileShare.None); byte[] pbData = pb.ReadData(); try { File.WriteAllBytes(strPath, pbData); } finally { if(pb.IsProtected) MemUtil.ZeroByteArray(pbData); } +#endif } } } diff --git a/ModernKeePassLib/Translation/KPTranslation.cs b/ModernKeePassLib/Translation/KPTranslation.cs index 072b43b..57f9281 100644 --- a/ModernKeePassLib/Translation/KPTranslation.cs +++ b/ModernKeePassLib/Translation/KPTranslation.cs @@ -174,7 +174,7 @@ namespace ModernKeePassLib.Translation return new Dictionary(); } -#if (!KeePassLibSD && !KeePassUAP) +#if (!KeePassLibSD && !KeePassUAP !&&ModernKeePassLib) public void ApplyTo(Form form) { if(form == null) throw new ArgumentNullException("form"); diff --git a/ModernKeePassLib/Utility/MessageService.cs b/ModernKeePassLib/Utility/MessageService.cs index 88596eb..4632f61 100644 --- a/ModernKeePassLib/Utility/MessageService.cs +++ b/ModernKeePassLib/Utility/MessageService.cs @@ -180,7 +180,7 @@ namespace ModernKeePassLib.Utility internal static DialogResult SafeShowMessageBox(string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi, MessageBoxDefaultButton mdb) { -#if (KeePassLibSD || KeePassRT) +#if KeePassLibSD return MessageBox.Show(strText, strTitle, mb, mi, mdb); #else IWin32Window wnd = null; @@ -215,7 +215,7 @@ namespace ModernKeePassLib.Utility #endif } -#if (!KeePassLibSD && !KeePassRT) +#if !KeePassLibSD internal delegate DialogResult SafeShowMessageBoxInternalDelegate(IWin32Window iParent, string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi, MessageBoxDefaultButton mdb); diff --git a/ModernKeePassLib/Utility/MonoWorkarounds.cs b/ModernKeePassLib/Utility/MonoWorkarounds.cs index 72ac395..6859a00 100644 --- a/ModernKeePassLib/Utility/MonoWorkarounds.cs +++ b/ModernKeePassLib/Utility/MonoWorkarounds.cs @@ -340,7 +340,7 @@ namespace ModernKeePassLib.Utility if(!MonoWorkarounds.IsRequired()) return; if(f == null) { Debug.Assert(false); return; } -#if (!KeePassLibSD && !KeePassRT) +#if !KeePassLibSD f.HandleCreated += MonoWorkarounds.OnFormHandleCreated; SetWmClass(f); @@ -353,14 +353,14 @@ namespace ModernKeePassLib.Utility if(!MonoWorkarounds.IsRequired()) return; if(f == null) { Debug.Assert(false); return; } -#if (!KeePassLibSD && !KeePassRT) +#if !KeePassLibSD f.HandleCreated -= MonoWorkarounds.OnFormHandleCreated; ApplyToControlsRec(f.Controls, f, MonoWorkarounds.ReleaseControl); #endif } -#if (!KeePassLibSD && !KeePassRT) +#if !KeePassLibSD private delegate void MwaControlHandler(Control c, Form fContext); private static void ApplyToControlsRec(Control.ControlCollection cc, diff --git a/ModernKeePassLib/Utility/StrUtil.cs b/ModernKeePassLib/Utility/StrUtil.cs index c4d52fe..6169ee3 100644 --- a/ModernKeePassLib/Utility/StrUtil.cs +++ b/ModernKeePassLib/Utility/StrUtil.cs @@ -25,13 +25,14 @@ using System.Globalization; using System.IO; using System.Text; -#if !KeePassUAP using System.Drawing; +#if ModernKeePassLib +using ModernKeePassLib.Cryptography; +#else using System.Security.Cryptography; #endif using ModernKeePassLib.Collections; -using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography.PasswordGenerator; using ModernKeePassLib.Native; using ModernKeePassLib.Security; @@ -224,7 +225,7 @@ namespace ModernKeePassLib.Utility Encoding.Default, (uint)Encoding.Default.GetBytes("a").Length, null)); #endif -#if !ModernKeePassLib && !KeePassRT +#if !ModernKeePassLib l.Add(new StrEncodingInfo(StrEncodingType.Ascii, "ASCII", Encoding.ASCII, 1, null)); l.Add(new StrEncodingInfo(StrEncodingType.Utf7, @@ -238,7 +239,7 @@ namespace ModernKeePassLib.Utility l.Add(new StrEncodingInfo(StrEncodingType.Utf16BE, "Unicode (UTF-16 BE)", new UnicodeEncoding(true, false), 2, new byte[] { 0xFE, 0xFF })); -#if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) +#if !ModernKeePassLib && !KeePassLibSD l.Add(new StrEncodingInfo(StrEncodingType.Utf32LE, "Unicode (UTF-32 LE)", new UTF32Encoding(false, false), 4, new byte[] { 0xFF, 0xFE, 0x0, 0x0 })); @@ -849,10 +850,8 @@ namespace ModernKeePassLib.Utility Debug.Assert(strY != null); if(strY == null) throw new ArgumentNullException("strY"); -#if !ModernKeePassLib if(NativeMethods.SupportsStrCmpNaturally) return NativeMethods.StrCmpNaturally(strX, strY); -#endif int cX = strX.Length; int cY = strY.Length; @@ -1376,7 +1375,7 @@ namespace ModernKeePassLib.Utility byte[] pbEnc = CryptoUtil.ProtectData(pbPlain, m_pbOptEnt, DataProtectionScope.CurrentUser); -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD return Convert.ToBase64String(pbEnc, Base64FormattingOptions.None); #else return Convert.ToBase64String(pbEnc); @@ -1489,7 +1488,7 @@ namespace ModernKeePassLib.Utility Array.Reverse(pb); for(int i = 0; i < pb.Length; ++i) pb[i] = (byte)(pb[i] ^ 0x65); -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD return Convert.ToBase64String(pb, Base64FormattingOptions.None); #else return Convert.ToBase64String(pb); @@ -1649,7 +1648,7 @@ namespace ModernKeePassLib.Utility if(strMediaType == null) strMediaType = "application/octet-stream"; -#if (!ModernKeePassLib && !KeePassLibSD) +#if !ModernKeePassLib && !KeePassLibSD return ("data:" + strMediaType + ";base64," + Convert.ToBase64String( pbData, Base64FormattingOptions.None)); #else @@ -1680,7 +1679,7 @@ namespace ModernKeePassLib.Utility MemoryStream ms = new MemoryStream(); -#if ModernKeePassLib || KeePassRT +#if ModernKeePassLib Encoding enc = StrUtil.Utf8; #else Encoding enc = Encoding.ASCII; diff --git a/ModernKeePassLib/Utility/StreamExtensions.cs b/ModernKeePassLib/Utility/StreamExtensions.cs new file mode 100644 index 0000000..499a4c2 --- /dev/null +++ b/ModernKeePassLib/Utility/StreamExtensions.cs @@ -0,0 +1,17 @@ +using System.IO; +using Windows.Storage.Streams; + +namespace ModernKeePassLibPCL.Utility +{ + public static class StreamExtensions + { + public static Stream AsStream(this IRandomAccessStream inputStream) + { + var reader = new DataReader(inputStream.GetInputStreamAt(0)); + var bytes = new byte[inputStream.Size]; + reader.LoadAsync((uint)inputStream.Size).GetResults(); + reader.ReadBytes(bytes); + return new MemoryStream(bytes); + } + } +} diff --git a/ModernKeePassLib/Utility/UrlUtil.cs b/ModernKeePassLib/Utility/UrlUtil.cs index 9f5d0e5..1231dec 100644 --- a/ModernKeePassLib/Utility/UrlUtil.cs +++ b/ModernKeePassLib/Utility/UrlUtil.cs @@ -25,6 +25,10 @@ using System.IO; using System.Text; using System.Text.RegularExpressions; +#if ModernKeePassLib +using Windows.Storage; +#endif + using ModernKeePassLib.Native; namespace ModernKeePassLib.Utility @@ -40,7 +44,11 @@ namespace ModernKeePassLib.Utility public static char LocalDirSepChar { +#if ModernKeePassLib + get { return '\\'; } +#else get { return Path.DirectorySeparatorChar; } +#endif } private static char[] g_vDirSepChars = null; @@ -58,7 +66,7 @@ namespace ModernKeePassLib.Utility if(!l.Contains(UrlUtil.LocalDirSepChar)) { - //Debug.Assert(false); + Debug.Assert(false); l.Add(UrlUtil.LocalDirSepChar); } @@ -278,13 +286,14 @@ namespace ModernKeePassLib.Utility if(strUrl == null) { Debug.Assert(false); throw new ArgumentNullException("strUrl"); } if(strUrl.Length == 0) { Debug.Assert(false); return string.Empty; } - if(!strUrl.StartsWith(Uri.UriSchemeFile + ":", StrUtil.CaseIgnoreCmp)) +#if !ModernKeePassLib + if(!strUrl.StartsWith(Uri.UriSchemeFile + ":", StrUtil.CaseIgnoreCmp)) { Debug.Assert(false); return strUrl; } - - try +#endif + try { Uri uri = new Uri(strUrl); string str = uri.LocalPath; @@ -298,7 +307,7 @@ namespace ModernKeePassLib.Utility public static bool UnhideFile(string strFile) { -#if (ModernKeePassLib || KeePassLibSD) +#if ModernKeePassLib || KeePassLibSD return false; #else if(strFile == null) throw new ArgumentNullException("strFile"); @@ -318,7 +327,7 @@ namespace ModernKeePassLib.Utility public static bool HideFile(string strFile, bool bHide) { -#if (ModernKeePassLib || KeePassLibSD) +#if ModernKeePassLib || KeePassLibSD return false; #else if(strFile == null) throw new ArgumentNullException("strFile"); @@ -484,7 +493,16 @@ namespace ModernKeePassLib.Utility } string str; + try + { +#if ModernKeePassLib + var dirT = StorageFolder.GetFolderFromPathAsync( + strPath).GetResults(); + str = dirT.Path; +#else try { str = Path.GetFullPath(strPath); } +#endif + } catch(Exception) { Debug.Assert(false); return strPath; } Debug.Assert((str.IndexOf("\\..\\") < 0) || NativeLib.IsUnix()); @@ -692,11 +710,10 @@ namespace ModernKeePassLib.Utility string strDir; if(NativeLib.IsUnix()) strDir = NativeMethods.GetUserRuntimeDir(); -#if KeePassUAP +#if KeePassUAP || ModernKeePassLib else strDir = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path; #else else strDir = Path.GetTempPath(); -#endif try { @@ -704,10 +721,11 @@ namespace ModernKeePassLib.Utility } catch(Exception) { Debug.Assert(false); } +#endif return strDir; } -#if !KeePassLibSD +#if !ModernKeePassLib && !KeePassLibSD // Structurally mostly equivalent to UrlUtil.GetFileInfos public static List GetFilePaths(string strDir, string strPattern, SearchOption opt) diff --git a/ModernKeePassLib/Utility/XmlUtilEx.cs b/ModernKeePassLib/Utility/XmlUtilEx.cs index 6c36c4e..88689e6 100644 --- a/ModernKeePassLib/Utility/XmlUtilEx.cs +++ b/ModernKeePassLib/Utility/XmlUtilEx.cs @@ -24,6 +24,7 @@ using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; +using Windows.Data.Xml.Dom; namespace ModernKeePassLib.Utility { @@ -33,10 +34,12 @@ namespace ModernKeePassLib.Utility { XmlDocument d = new XmlDocument(); - // .NET 4.5.2 and newer do not resolve external XML resources - // by default; for older .NET versions, we explicitly - // prevent resolving - d.XmlResolver = null; // Default in old .NET: XmlUrlResolver object + // .NET 4.5.2 and newer do not resolve external XML resources + // by default; for older .NET versions, we explicitly + // prevent resolving +#if !ModernKeePassLib + d.XmlResolver = null; // Default in old .NET: XmlUrlResolver object +#endif return d; } @@ -56,11 +59,11 @@ namespace ModernKeePassLib.Utility // Also see PrepMonoDev.sh script xrs.ProhibitDtd = true; // Obsolete in .NET 4, but still there // xrs.DtdProcessing = DtdProcessing.Prohibit; // .NET 4 only -#endif xrs.ValidationType = ValidationType.None; xrs.XmlResolver = null; +#endif return xrs; } @@ -71,24 +74,28 @@ namespace ModernKeePassLib.Utility return XmlReader.Create(s, CreateXmlReaderSettings()); } - public static XmlWriterSettings CreateXmlWriterSettings() + public static XmlWriterSettings CreateXmlWriterSettings(bool isVersionGreaterThan4 = false) { XmlWriterSettings xws = new XmlWriterSettings(); - xws.CloseOutput = false; + xws.CloseOutput = isVersionGreaterThan4; xws.Encoding = StrUtil.Utf8; xws.Indent = true; xws.IndentChars = "\t"; xws.NewLineOnAttributes = false; +#if ModernKeePassLib + // This is needed for Argon2Kdf write + xws.Async = true; +#endif return xws; } - public static XmlWriter CreateXmlWriter(Stream s) + public static XmlWriter CreateXmlWriter(Stream s, bool isVersionGreaterThan4 = false) { if(s == null) { Debug.Assert(false); throw new ArgumentNullException("s"); } - return XmlWriter.Create(s, CreateXmlWriterSettings()); + return XmlWriter.Create(s, CreateXmlWriterSettings(isVersionGreaterThan4)); } public static void Serialize(Stream s, T t) diff --git a/ModernKeePassLib/app.config b/ModernKeePassLib/app.config new file mode 100644 index 0000000..9cf3b82 --- /dev/null +++ b/ModernKeePassLib/app.config @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ModernKeePassLib/project.json b/ModernKeePassLib/project.json new file mode 100644 index 0000000..e8621de --- /dev/null +++ b/ModernKeePassLib/project.json @@ -0,0 +1,17 @@ +{ + "supports": {}, + "dependencies": { + "Microsoft.NETCore.Portable.Compatibility": "1.0.2", + "NETStandard.Library": "2.0.3", + "Portable.BouncyCastle": "1.8.2", + "SixLabors.ImageSharp": "1.0.0-beta0005", + "Splat": "3.0.0", + "System.Runtime.WindowsRuntime": "4.3.0", + "System.Xml.ReaderWriter": "4.3.1", + "System.Xml.XmlSerializer": "4.3.0", + "Validation": "2.4.18" + }, + "frameworks": { + "netstandard1.2": {} + } +} \ No newline at end of file