diff --git a/ModernKeePass/ModernKeePass.csproj b/ModernKeePass/ModernKeePass.csproj index dfcc270..04de4e3 100644 --- a/ModernKeePass/ModernKeePass.csproj +++ b/ModernKeePass/ModernKeePass.csproj @@ -253,9 +253,9 @@ ..\packages\Microsoft.Toolkit.Uwp.Notifications.2.0.0\lib\dotnet\Microsoft.Toolkit.Uwp.Notifications.dll True - - ..\packages\ModernKeePassLib.2.37.2000\lib\netstandard1.2\ModernKeePassLib.dll - True + + False + ..\ModernKeePassLib\bin\Debug\ModernKeePassLib.dll ..\packages\Splat.2.0.0\lib\Portable-Win81+Wpa81\Splat.dll diff --git a/ModernKeePass/packages.config b/ModernKeePass/packages.config index 86484fc..84861ce 100644 --- a/ModernKeePass/packages.config +++ b/ModernKeePass/packages.config @@ -3,7 +3,7 @@ - + diff --git a/ModernKeePassLib.Test/Keys/CompositeKeyTests.cs b/ModernKeePassLib.Test/Keys/CompositeKeyTests.cs index c740cbe..8afb827 100644 --- a/ModernKeePassLib.Test/Keys/CompositeKeyTests.cs +++ b/ModernKeePassLib.Test/Keys/CompositeKeyTests.cs @@ -1,34 +1,33 @@ using NUnit.Framework; -using System; - -#if KeePassLib -using KeePassLib.Keys; -#else +using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Keys; -#endif namespace ModernKeePassLib.Test.Shared.Keys { [TestFixture ()] public class CompositeKeyTests { - [Test ()] + [Test] public void TestGenerateKey32 () { - var originalKey = new byte[32]; - var expectedKey = new byte[32] { + var originalKey = new byte[32]; + var expectedKey = new byte[32] { 0xF0, 0xED, 0x57, 0xD5, 0xF0, 0xDA, 0xF3, 0x47, 0x90, 0xD0, 0xDB, 0x43, 0x25, 0xC6, 0x81, 0x2C, 0x81, 0x6A, 0x0D, 0x94, 0x96, 0xA9, 0x03, 0xE1, 0x20, 0xD4, 0x3A, 0x3E, 0x45, 0xAD, 0x02, 0x65 - }; - const ulong rounds = 1; + }; + const ulong rounds = 1; - var composite = new CompositeKey (); - var key = composite.GenerateKey32 (originalKey, rounds); - Assert.That (key, Is.Not.Null); - var keyData = key.ReadData (); - Assert.That (keyData, Is.EqualTo (expectedKey)); + var composite = new CompositeKey (); + AesKdf kdf = new AesKdf(); + KdfParameters p = kdf.GetDefaultParameters(); + p.SetUInt64(AesKdf.ParamRounds, rounds); + p.SetByteArray(AesKdf.ParamSeed, originalKey); + var key = composite.GenerateKey32(p); + Assert.That (key, Is.Not.Null); + var keyData = key.ReadData (); + Assert.That (keyData, Is.EqualTo (expectedKey)); } } } diff --git a/ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs b/ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs index 51d6dd7..fec5e68 100644 --- a/ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs +++ b/ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs @@ -25,7 +25,6 @@ using System.Diagnostics; using ModernKeePassLib.Interfaces; using ModernKeePassLib.Security; -using ModernKeePassLib.Utility; #if KeePassLibSD using KeePassLibSD; diff --git a/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs index f2264ce..daa4def 100644 --- a/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs +++ b/ModernKeePassLib/Cryptography/Cipher/StandardAesEngine.cs @@ -122,17 +122,16 @@ namespace ModernKeePassLib.Cryptography.Cipher Array.Copy(pbKey, pbLocalKey, 32); #if ModernKeePassLib - AesEngine aes = new AesEngine(); - CbcBlockCipher cbc = new CbcBlockCipher(aes); - PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(cbc, + var cbc = new CbcBlockCipher(new AesEngine()); + var bc = new PaddedBufferedBlockCipher(cbc, new Pkcs7Padding()); - KeyParameter kp = new KeyParameter(pbLocalKey); - ParametersWithIV prmIV = new ParametersWithIV(kp, pbLocalIV); + var kp = new KeyParameter(pbLocalKey); + var prmIV = new ParametersWithIV(kp, pbLocalIV); bc.Init(bEncrypt, prmIV); - IBufferedCipher cpRead = (bEncrypt ? null : bc); - IBufferedCipher cpWrite = (bEncrypt ? bc : null); - return new CipherStream(s, cpRead, cpWrite); + var cpRead = (bEncrypt ? null : bc); + var cpWrite = (bEncrypt ? bc : null); + return new CipherStream(s, cpRead, cpWrite); #elif KeePassUAP return StandardAesEngineExt.CreateStream(s, bEncrypt, pbLocalKey, pbLocalIV); #else diff --git a/ModernKeePassLib/Cryptography/CryptoRandom.cs b/ModernKeePassLib/Cryptography/CryptoRandom.cs index f430d8a..a5d269f 100644 --- a/ModernKeePassLib/Cryptography/CryptoRandom.cs +++ b/ModernKeePassLib/Cryptography/CryptoRandom.cs @@ -19,18 +19,20 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.IO; + #if ModernKeePassLib using Windows.Security.Cryptography; -using ModernKeePassLib.Utility; using Windows.Security.Cryptography.Core; #else using System.Security.Cryptography; #endif -using System.IO; -using System.Diagnostics; -using System.Globalization; -using ModernKeePassLib.Native; +using ModernKeePassLib.Native; +using ModernKeePassLib.Utility; namespace ModernKeePassLib.Cryptography { @@ -44,7 +46,7 @@ namespace ModernKeePassLib.Cryptography private byte[] m_pbEntropyPool = new byte[64]; private ulong m_uCounter; #if !ModernKeePassLib - private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider(); + private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider(); #endif private ulong m_uGeneratedBytesCount = 0; diff --git a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs index 209fa29..ffca4f2 100644 --- a/ModernKeePassLib/Cryptography/CryptoRandomStream.cs +++ b/ModernKeePassLib/Cryptography/CryptoRandomStream.cs @@ -20,8 +20,13 @@ using System; using System.Diagnostics; +#if ModernKeePassLib using Windows.Security.Cryptography; using Windows.Security.Cryptography.Core; +#elif !KeePassUAP +using System.Security.Cryptography; +#endif + using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Utility; diff --git a/ModernKeePassLib/Cryptography/CryptoUtil.cs b/ModernKeePassLib/Cryptography/CryptoUtil.cs index 02d2415..a67e510 100644 --- a/ModernKeePassLib/Cryptography/CryptoUtil.cs +++ b/ModernKeePassLib/Cryptography/CryptoUtil.cs @@ -22,13 +22,17 @@ using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; - +using ModernKeePassLib.Cryptography.Hash; +#if ModernKeePassLib +using Org.BouncyCastle.Asn1.Pkcs; using Windows.Security.Cryptography; using Windows.Security.Cryptography.Core; -using ModernKeePassLib.Cryptography.Hash; +#elif !KeePassUAP +using System.Security.Cryptography; +#endif + using ModernKeePassLib.Native; using ModernKeePassLib.Utility; -using Org.BouncyCastle.Asn1.Pkcs; namespace ModernKeePassLib.Cryptography { diff --git a/ModernKeePassLib/Cryptography/HashingStreamEx.cs b/ModernKeePassLib/Cryptography/HashingStreamEx.cs index e3f95cf..790958b 100644 --- a/ModernKeePassLib/Cryptography/HashingStreamEx.cs +++ b/ModernKeePassLib/Cryptography/HashingStreamEx.cs @@ -21,17 +21,19 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Text; + #if ModernKeePassLib using Windows.Security.Cryptography; using Windows.Security.Cryptography.Core; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -#else +using Org.BouncyCastle.Crypto.Tls; +#elif !KeePassUAP using System.Security.Cryptography; #endif -using System.Runtime.InteropServices.ComTypes; + using ModernKeePassLib.Utility; -using Org.BouncyCastle.Crypto.Tls; namespace ModernKeePassLib.Cryptography { @@ -39,7 +41,7 @@ namespace ModernKeePassLib.Cryptography { private readonly Stream m_sBaseStream; private readonly bool m_bWriting; - #if ModernKeePassLib +#if ModernKeePassLib //private ICryptoTransform m_hash; //private CryptographicHash m_hash; private IDigest m_hash; @@ -81,20 +83,19 @@ namespace ModernKeePassLib.Cryptography } #if ModernKeePassLib - //public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm? hashAlgorithm) - public HashingStreamEx(Stream sBaseStream, bool bWriting, string hashAlgorithm) + public HashingStreamEx(Stream sBaseStream, bool bWriting, IDigest hashAlgorithm) #else public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm hashAlgorithm) #endif - { - if (sBaseStream == null) throw new ArgumentNullException("sBaseStream"); + { + if(sBaseStream == null) throw new ArgumentNullException("sBaseStream"); m_sBaseStream = sBaseStream; m_bWriting = bWriting; #if ModernKeePassLib //m_hash = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithm.Sha256).CreateHash(); //m_hash = HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithmNames.Sha256).CreateHash(); - m_hash = new Sha256Digest(); + m_hash = hashAlgorithm ?? new Sha256Digest(); #elif !KeePassLibSD m_hash = (hashAlgorithm ?? new SHA256Managed()); #else // KeePassLibSD @@ -108,46 +109,48 @@ namespace ModernKeePassLib.Cryptography if(m_hash == null) { Debug.Assert(false); return; } // Validate hash algorithm - /*if(!m_hash.CanReuseTransform || !m_hash.CanTransformMultipleBlocks) +#if ModernKeePassLib +#else + if(!m_hash.CanReuseTransform || !m_hash.CanTransformMultipleBlocks) { Debug.Assert(false); m_hash = null; - }*/ + } +#endif } -#if ModernKeePassLib || KeePassRT - protected override void Dispose(bool disposing) - { - if (!disposing) return; -#else - public override void Close() + protected override void Dispose(bool disposing) { -#endif - if (m_hash != null) - { - try - { - //m_hash.TransformFinalBlock(new byte[0], 0, 0); + if(disposing) + { + if(m_hash != null) + { + try + { #if ModernKeePassLib - //m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset (); - //CryptographicBuffer.CopyToByteArray(m_hash.GetValueAndReset(), out m_pbFinalHash); - m_pbFinalHash = new byte[32]; - m_hash.DoFinal(m_pbFinalHash, 0); - m_hash.Reset(); + //m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset (); + //CryptographicBuffer.CopyToByteArray(m_hash.GetValueAndReset(), out m_pbFinalHash); + m_pbFinalHash = new byte[32]; + m_hash.DoFinal(m_pbFinalHash, 0); + m_hash.Reset(); #else - m_pbFinalHash = m_hash.Hash; + m_hash.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0); + + m_pbFinalHash = m_hash.Hash; #endif - } - catch (Exception) - { - Debug.Assert(false); - } + } + catch(Exception) { Debug.Assert(false); } - base.Dispose(disposing); - } - } + m_hash = null; + } - public override void Flush() + m_sBaseStream.Dispose(); + } + + base.Dispose(disposing); + } + + public override void Flush() { m_sBaseStream.Flush(); } @@ -181,8 +184,11 @@ namespace ModernKeePassLib.Cryptography #endif if((m_hash != null) && (nRead > 0)) - //m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset); +#if ModernKeePassLib m_hash.BlockUpdate(pbBuffer, nOffset, nRead); +#else + m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset); +#endif #if DEBUG Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg)); @@ -201,8 +207,11 @@ namespace ModernKeePassLib.Cryptography #endif if((m_hash != null) && (nCount > 0)) - //m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset); +#if ModernKeePassLib m_hash.BlockUpdate(pbBuffer, nOffset, nCount); +#else + m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset); +#endif #if DEBUG Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg)); diff --git a/ModernKeePassLib/Cryptography/HmacOtp.cs b/ModernKeePassLib/Cryptography/HmacOtp.cs index f7eab67..e35035c 100644 --- a/ModernKeePassLib/Cryptography/HmacOtp.cs +++ b/ModernKeePassLib/Cryptography/HmacOtp.cs @@ -19,18 +19,18 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text; #if ModernKeePassLib using Windows.Security.Cryptography; -#else +using Windows.Security.Cryptography.Core; +#elif !KeePassUAP using System.Security.Cryptography; #endif -using System.Globalization; using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; -#if (!KeePassLibSD && !KeePassRT) +#if !KeePassLibSD namespace ModernKeePassLib.Cryptography { /// @@ -47,11 +47,15 @@ namespace ModernKeePassLib.Cryptography byte[] pbText = MemUtil.UInt64ToBytes(uFactor); Array.Reverse(pbText); // To big-endian +#if ModernKeePassLib var hsha1 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(CryptographicBuffer.CreateFromByteArray(pbSecret)); hsha1.Append(CryptographicBuffer.CreateFromByteArray(pbText)); byte[] pbHash; CryptographicBuffer.CopyToByteArray(hsha1.GetValueAndReset(), out pbHash); - +#else + HMACSHA1 hsha1 = new HMACSHA1(pbSecret); + byte[] pbHash = hsha1.ComputeHash(pbText); +#endif uint uOffset = (uint)(pbHash[pbHash.Length - 1] & 0xF); if((iTruncationOffset >= 0) && (iTruncationOffset < (pbHash.Length - 4))) uOffset = (uint)iTruncationOffset; diff --git a/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.Core.cs b/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.Core.cs index 11d5531..18692c9 100644 --- a/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.Core.cs +++ b/ModernKeePassLib/Cryptography/KeyDerivation/Argon2Kdf.Core.cs @@ -114,7 +114,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation ctx.LaneLength = ctx.SegmentLength * NbSyncPoints; Debug.Assert(NbBlockSize == (NbBlockSizeInQW * -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP (ulong)Marshal.SizeOf() #else (ulong)Marshal.SizeOf(typeof(ulong)) diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs index 19c056e..1cc54bb 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PwGenerator.cs @@ -21,9 +21,14 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; -using System.Diagnostics; + +#if ModernKeePassLib using Windows.Security.Cryptography; using Windows.Security.Cryptography.Core; +#elif !KeePassUAP +using System.Security.Cryptography; +#endif + using ModernKeePassLib.Security; using ModernKeePassLib.Utility; @@ -88,7 +93,6 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator byte[] pbHash; CryptographicBuffer.CopyToByteArray(h, out pbHash); MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length); - #else using(SHA512Managed h = new SHA512Managed()) { diff --git a/ModernKeePassLib/Cryptography/PasswordGenerator/PwProfile.cs b/ModernKeePassLib/Cryptography/PasswordGenerator/PwProfile.cs index 20f38a2..f58796a 100644 --- a/ModernKeePassLib/Cryptography/PasswordGenerator/PwProfile.cs +++ b/ModernKeePassLib/Cryptography/PasswordGenerator/PwProfile.cs @@ -265,7 +265,7 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator else pcs.Add(ch); } - Array.Clear(vChars, 0, vChars.Length); + MemUtil.ZeroArray(vChars); MemUtil.ZeroByteArray(pbUtf8); return pp; } diff --git a/ModernKeePassLib/Keys/CompositeKey.cs b/ModernKeePassLib/Keys/CompositeKey.cs index a28920e..fdc7134 100644 --- a/ModernKeePassLib/Keys/CompositeKey.cs +++ b/ModernKeePassLib/Keys/CompositeKey.cs @@ -18,32 +18,15 @@ */ using System; -using System.Text; using System.Collections.Generic; using System.Diagnostics; -using System.IO; -using System.Runtime.InteropServices.WindowsRuntime; -#if ModernKeePassLib -using Windows.Security.Cryptography; -#else -using System.Security.Cryptography; -#endif +using System.Text; -#if KeePassRT -using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Parameters; -#endif - -using ModernKeePassLib.Native; +using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; -using Windows.Storage.Streams; -using ModernKeePassLib.Cryptography; -using ModernKeePassLib.Cryptography.KeyDerivation; -using Org.BouncyCastle.Crypto.Engines; -using KdfParameters = Org.BouncyCastle.Crypto.Parameters.KdfParameters; namespace ModernKeePassLib.Keys { @@ -119,7 +102,6 @@ namespace ModernKeePassLib.Keys return m_vUserKeys.Remove(pKey); } -#if !ModernKeePassLib && !KeePassRT /// /// Test whether the composite key contains a specific type of /// user keys (password, key file, ...). If at least one user @@ -137,7 +119,7 @@ namespace ModernKeePassLib.Keys { if(pKey == null) { Debug.Assert(false); continue; } -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP if(pKey.GetType() == tUserKeyType) return true; #else @@ -164,7 +146,7 @@ namespace ModernKeePassLib.Keys { if(pKey == null) { Debug.Assert(false); continue; } -#if KeePassUAP +#if ModernKeePassLib || KeePassUAP if(pKey.GetType() == tUserKeyType) return pKey; #else @@ -175,7 +157,6 @@ namespace ModernKeePassLib.Keys return null; } -#endif /// /// Creates the composite key from the supplied user key sources (password, @@ -236,7 +217,7 @@ namespace ModernKeePassLib.Keys if(pbKeySeed32.Length != 32) throw new ArgumentException("pbKeySeed32"); AesKdf kdf = new AesKdf(); - var p = kdf.GetDefaultParameters(); + KdfParameters p = kdf.GetDefaultParameters(); p.SetUInt64(AesKdf.ParamRounds, uNumRounds); p.SetByteArray(AesKdf.ParamSeed, pbKeySeed32); @@ -246,7 +227,7 @@ namespace ModernKeePassLib.Keys /// /// Generate a 32-byte (256-bit) key from the composite key. /// - public ProtectedBinary GenerateKey32(Cryptography.KeyDerivation.KdfParameters p) + public ProtectedBinary GenerateKey32(KdfParameters p) { if(p == null) { Debug.Assert(false); throw new ArgumentNullException("p"); } diff --git a/ModernKeePassLib/Keys/KcpCustomKey.cs b/ModernKeePassLib/Keys/KcpCustomKey.cs index b018208..6c002ca 100644 --- a/ModernKeePassLib/Keys/KcpCustomKey.cs +++ b/ModernKeePassLib/Keys/KcpCustomKey.cs @@ -19,18 +19,11 @@ using System; using System.Collections.Generic; -using System.Text; using System.Diagnostics; -#if ModernKeePassLib -using Windows.Security.Cryptography; -#else -using System.Security.Cryptography; -#endif +using System.Text; -using ModernKeePassLib.Security; -using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; using ModernKeePassLib.Cryptography; +using ModernKeePassLib.Security; namespace ModernKeePassLib.Keys { diff --git a/ModernKeePassLib/Keys/KcpKeyFile.cs b/ModernKeePassLib/Keys/KcpKeyFile.cs index b09eff2..3961b48 100644 --- a/ModernKeePassLib/Keys/KcpKeyFile.cs +++ b/ModernKeePassLib/Keys/KcpKeyFile.cs @@ -18,26 +18,27 @@ */ using System; -using System.Text; +using System.Diagnostics; using System.IO; -using System.Xml; 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; #else using System.Security.Cryptography; #endif -using System.Diagnostics; using ModernKeePassLib.Cryptography; using ModernKeePassLib.Resources; using ModernKeePassLib.Security; using ModernKeePassLib.Serialization; using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; namespace ModernKeePassLib.Keys { diff --git a/ModernKeePassLib/Keys/KcpPassword.cs b/ModernKeePassLib/Keys/KcpPassword.cs index 19fb048..5a58c61 100644 --- a/ModernKeePassLib/Keys/KcpPassword.cs +++ b/ModernKeePassLib/Keys/KcpPassword.cs @@ -18,17 +18,11 @@ */ using System; -using System.Text; using System.Diagnostics; -#if ModernKeePassLib -using Windows.Security.Cryptography; -#else -using System.Security.Cryptography; -#endif +using System.Text; using ModernKeePassLib.Security; using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; using ModernKeePassLib.Cryptography; namespace ModernKeePassLib.Keys diff --git a/ModernKeePassLib/Keys/KcpUserAccount.cs b/ModernKeePassLib/Keys/KcpUserAccount.cs index d8e64c3..b427dc8 100644 --- a/ModernKeePassLib/Keys/KcpUserAccount.cs +++ b/ModernKeePassLib/Keys/KcpUserAccount.cs @@ -21,8 +21,9 @@ using System; using System.Diagnostics; using System.IO; using System.Security; + +#if ModernKeePassLib using Windows.Storage; -#if !KeePassUAP using Windows.Security.Cryptography; #endif @@ -100,7 +101,7 @@ namespace ModernKeePassLib.Keys strUserDir += PwDefs.ShortProductName; #if !ModernKeePassLib - if (bCreate && !Directory.Exists(strUserDir)) + if(bCreate && !Directory.Exists(strUserDir)) Directory.CreateDirectory(strUserDir); #endif strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); @@ -121,9 +122,9 @@ namespace ModernKeePassLib.Keys fileStream.Read(pbProtectedKey, 0, (int)fileStream.Length); fileStream.Dispose(); #else - byte[] pbProtectedKey = File.ReadAllBytes(strFilePath); + byte[] pbProtectedKey = File.ReadAllBytes(strFilePath); #endif - pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy, + pbKey = ProtectedData.Unprotect(pbProtectedKey, m_pbEntropy, DataProtectionScope.CurrentUser); } catch(Exception) @@ -133,7 +134,7 @@ namespace ModernKeePassLib.Keys } #endif - return pbKey; + return pbKey; } private static byte[] CreateUserKey() @@ -151,10 +152,10 @@ namespace ModernKeePassLib.Keys fileStream.Write(pbProtectedKey, 0, (int)fileStream.Length); fileStream.Dispose(); #else - File.WriteAllBytes(strFilePath, pbProtectedKey); + File.WriteAllBytes(strFilePath, pbProtectedKey); #endif - byte[] pbKey = LoadUserKey(true); + byte[] pbKey = LoadUserKey(true); Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey)); MemUtil.ZeroByteArray(pbRandomKey); diff --git a/ModernKeePassLib/ModernKeePassLib.csproj b/ModernKeePassLib/ModernKeePassLib.csproj index 4137d30..f3b3ce0 100644 --- a/ModernKeePassLib/ModernKeePassLib.csproj +++ b/ModernKeePassLib/ModernKeePassLib.csproj @@ -125,7 +125,8 @@ - + + @@ -173,6 +174,7 @@ False Libs\Windows.winmd + False diff --git a/ModernKeePassLib/Serialization/FileLock.cs b/ModernKeePassLib/Serialization/FileLock.cs index 224609e..53ab92a 100644 --- a/ModernKeePassLib/Serialization/FileLock.cs +++ b/ModernKeePassLib/Serialization/FileLock.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,16 +19,15 @@ using System; using System.Collections.Generic; -using System.Text; -using System.IO; -#if ModernKeePassLib -using System.Threading.Tasks; -#else -using System.Threading; -#endif using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; +#if ModernKeePassLib using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; using Windows.Storage.Streams; +#endif using ModernKeePassLib.Cryptography; using ModernKeePassLib.Resources; using ModernKeePassLib.Utility; diff --git a/ModernKeePassLib/Serialization/FileTransactionEx.cs b/ModernKeePassLib/Serialization/FileTransactionEx.cs index bdb1440..7f54e08 100644 --- a/ModernKeePassLib/Serialization/FileTransactionEx.cs +++ b/ModernKeePassLib/Serialization/FileTransactionEx.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,9 +19,9 @@ using System; using System.Collections.Generic; -using System.Text; -using System.IO; using System.Diagnostics; +using System.IO; +using System.Text; #if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) using System.Security.AccessControl; diff --git a/ModernKeePassLib/Serialization/HmacBlockStream.cs b/ModernKeePassLib/Serialization/HmacBlockStream.cs index ff80b41..70f80d6 100644 --- a/ModernKeePassLib/Serialization/HmacBlockStream.cs +++ b/ModernKeePassLib/Serialization/HmacBlockStream.cs @@ -29,6 +29,7 @@ using ModernKeePassLib.Resources; using ModernKeePassLib.Utility; using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Macs; +using Org.BouncyCastle.Crypto.Parameters; namespace ModernKeePassLib.Serialization { @@ -244,17 +245,18 @@ namespace ModernKeePassLib.Serialization byte[] pbCmpHmac; byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex); -/*#if ModernKeePassLib +#if ModernKeePassLib var h = new HMac(new Sha256Digest()); + h.Init(new KeyParameter(pbBlockKey)); h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length); h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length); if (m_pbBuffer.Length > 0) h.BlockUpdate(m_pbBuffer, 0, m_pbBuffer.Length); - + pbCmpHmac = MemUtil.EmptyByteArray; h.DoFinal(pbCmpHmac, 0); h.Reset(); -#else*/ - using(HMACSHA256 h = new HMACSHA256(pbBlockKey)) +#else + using(HMACSHA256 h = new HMACSHA256(pbBlockKey)) { h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length, pbBlockIndex, 0); @@ -269,7 +271,7 @@ namespace ModernKeePassLib.Serialization pbCmpHmac = h.Hash; } -//#endif +#endif MemUtil.ZeroByteArray(pbBlockKey); if(!MemUtil.ArraysEqual(pbCmpHmac, pbStoredHmac)) @@ -317,16 +319,18 @@ namespace ModernKeePassLib.Serialization byte[] pbBlockHmac; byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex); -/*#if ModernKeePassLib +#if ModernKeePassLib var h = new HMac(new Sha256Digest()); - h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length); + h.Init(new KeyParameter(pbBlockKey)); + h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length); h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length); - if (m_pbBuffer.Length > 0) - h.BlockUpdate(m_pbBuffer, 0, m_pbBuffer.Length); + if (cbBlockSize > 0) + h.BlockUpdate(m_pbBuffer, 0, cbBlockSize); - h.DoFinal(pbBlockHmac, 0); + pbBlockHmac = MemUtil.EmptyByteArray; + h.DoFinal(pbBlockHmac, 0); h.Reset(); -#else*/ +#else using(HMACSHA256 h = new HMACSHA256(pbBlockKey)) { h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length, @@ -341,7 +345,7 @@ namespace ModernKeePassLib.Serialization pbBlockHmac = h.Hash; } -//#endif +#endif MemUtil.ZeroByteArray(pbBlockKey); MemUtil.Write(m_sBase, pbBlockHmac); diff --git a/ModernKeePassLib/Serialization/IOConnection.cs b/ModernKeePassLib/Serialization/IOConnection.cs index 560f808..f0a4132 100644 --- a/ModernKeePassLib/Serialization/IOConnection.cs +++ b/ModernKeePassLib/Serialization/IOConnection.cs @@ -1,13 +1,12 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,24 +18,27 @@ */ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Net; -using System.Diagnostics; -using Windows.Storage.Streams; -using System.Threading.Tasks; -using ModernKeePassLib.Native; -#if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) +using System.Reflection; +using System.Text; + +#if (!ModernKeePassLib && !KeePassLibSD && !KeePassUAP) using System.Net.Cache; using System.Net.Security; #endif -#if !ModernKeePassLib && !KeePassRT +#if !ModernKeePassLib && !KeePassUAP 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 @@ -44,10 +46,17 @@ namespace ModernKeePassLib.Serialization #if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) internal sealed class IOWebClient : WebClient { + private IOConnectionInfo m_ioc; + + public IOWebClient(IOConnectionInfo ioc) : base() + { + m_ioc = ioc; + } + protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); - IOConnection.ConfigureWebRequest(request); + IOConnection.ConfigureWebRequest(request, m_ioc); return request; } } @@ -581,7 +590,7 @@ namespace ModernKeePassLib.Serialization new Uri(ioc.Path))); } #else - public static Stream OpenRead(IOConnectionInfo ioc) + public static Stream OpenRead(IOConnectionInfo ioc) { RaiseIOAccessPreEvent(ioc, IOAccessType.Read); @@ -700,7 +709,7 @@ namespace ModernKeePassLib.Serialization catch(Exception) { Debug.Assert(false); } } #endif - public static byte[] ReadFile(IOConnectionInfo ioc) + public static byte[] ReadFile(IOConnectionInfo ioc) { Stream sIn = null; MemoryStream ms = null; diff --git a/ModernKeePassLib/Serialization/IOConnectionInfo.cs b/ModernKeePassLib/Serialization/IOConnectionInfo.cs index 0bf6de3..224fada 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-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,12 +19,11 @@ using System; using System.Collections.Generic; -using System.Text; -using System.IO; -using System.Net; using System.ComponentModel; -using System.Xml.Serialization; using System.Diagnostics; +using System.IO; +using System.Text; +using System.Xml.Serialization; #if ModernKeePassLib using Windows.Storage; //using PCLStorage; @@ -338,13 +337,13 @@ namespace ModernKeePassLib.Serialization public StorageFile StorageFile { get; set; } - public async Task CanProbablyAccess() + public bool CanProbablyAccess() { #if ModernKeePassLib if (IsLocalFile()) { //return (FileSystem.Current.GetFileFromPathAsync(m_strUrl).Result != null); - var file = await StorageFile.GetFileFromPathAsync(m_strUrl); + var file = StorageFile.GetFileFromPathAsync(m_strUrl).GetAwaiter().GetResult(); return file != null; } #else diff --git a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs index 0e48d17..6da3604 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Read.Streamed.cs @@ -25,8 +25,6 @@ using System.Text; using System.Security; using System.Drawing; using System.Xml; -using System.IO; -using System.Diagnostics; using ModernKeePassLib; using ModernKeePassLib.Collections; diff --git a/ModernKeePassLib/Serialization/KdbxFile.Read.cs b/ModernKeePassLib/Serialization/KdbxFile.Read.cs index 3900cab..76dde29 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Read.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Read.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -17,18 +17,22 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +// #define KDBX_BENCHMARK + using System; using System.Collections.Generic; -using System.Text; -using System.IO; using System.Diagnostics; +using System.IO; using System.Security; +using System.Text; +using System.Xml; #if ModernKeePassLib using Windows.Security.Cryptography; +using Windows.Security.Cryptography.Core; +using Windows.Storage.Streams; #else using System.Security.Cryptography; #endif -using System.Xml; #if !KeePassLibSD using System.IO.Compression; @@ -42,8 +46,6 @@ using ModernKeePassLib.Interfaces; using ModernKeePassLib.Keys; using ModernKeePassLib.Resources; using ModernKeePassLib.Utility; -using Windows.Security.Cryptography.Core; -using Windows.Storage.Streams; using ModernKeePassLib.Collections; using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Security; diff --git a/ModernKeePassLib/Serialization/KdbxFile.Write.cs b/ModernKeePassLib/Serialization/KdbxFile.Write.cs index 5cf0bd8..6cc7024 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.Write.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.Write.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,23 +19,23 @@ using System; using System.Collections.Generic; -using System.Text; +using System.Diagnostics; +using System.Globalization; using System.IO; -using System.Xml; using System.Security; +using System.Text; +using System.Xml; #if ModernKeePassLib using Windows.Security.Cryptography; #else +using System.Drawing; using System.Security.Cryptography; #endif -using System.Drawing; -using System.Globalization; -using System.Diagnostics; -#if !KeePassLibSD -using System.IO.Compression; -#else +#if KeePassLibSD using KeePassLibSD; +#else +using System.IO.Compression; #endif using ModernKeePassLib.Collections; diff --git a/ModernKeePassLib/Serialization/KdbxFile.cs b/ModernKeePassLib/Serialization/KdbxFile.cs index 0690e5b..6233932 100644 --- a/ModernKeePassLib/Serialization/KdbxFile.cs +++ b/ModernKeePassLib/Serialization/KdbxFile.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,20 +19,17 @@ using System; using System.Collections.Generic; -using System.Xml; -using System.Text; +using System.Diagnostics; using System.Globalization; using System.IO; -using System.Diagnostics; using System.Security; -using Windows.Security.Cryptography; -using Windows.Security.Cryptography.Core; -#if !KeePassLibSD -using System.IO.Compression; -#endif +using System.Text; +using System.Xml; #if ModernKeePassLib using Windows.Storage; +using Windows.Security.Cryptography; +using Windows.Security.Cryptography.Core; #endif using ModernKeePassLib.Collections; @@ -207,11 +204,7 @@ namespace ModernKeePassLib.Serialization private PwDatabase m_pwDatabase; // Not null, see constructor private bool m_bUsedOnce = false; -#if ModernKeePassLib private XmlWriter m_xmlWriter = null; -#else - private XmlTextWriter m_xmlWriter = null; -#endif private CryptoRandomStream m_randomStream = null; private KdbxFormat m_format = KdbxFormat.Default; private IStatusLogger m_slLogger = null; @@ -403,12 +396,12 @@ namespace ModernKeePassLib.Serialization .HashData(CryptographicBuffer.CreateFromByteArray(pbCmp)); CryptographicBuffer.CopyToByteArray(h, out pbHmacKey64); #else - using(SHA512Managed h = new SHA512Managed()) + using(SHA512Managed h = new SHA512Managed()) { pbHmacKey64 = h.ComputeHash(pbCmp); } #endif - } + } finally { MemUtil.ZeroByteArray(pbCmp); } } @@ -459,11 +452,11 @@ namespace ModernKeePassLib.Serialization byte[] pbHeaderHmac; byte[] pbBlockKey = HmacBlockStream.GetHmacKey64( pbKey, ulong.MaxValue); - using (HMACSHA256 h = new HMACSHA256(pbBlockKey)) + using(HMACSHA256 h = new HMACSHA256(pbBlockKey)) { pbHeaderHmac = h.ComputeHash(pbHeader); } - MemUtil.ZeroByteArray(pbBlockKey); + MemUtil.ZeroByteArray(pbBlockKey); return pbHeaderHmac; } @@ -522,10 +515,9 @@ namespace ModernKeePassLib.Serialization if(!string.IsNullOrEmpty(strExt)) strPath += "." + strExt; - ++iTry; - } + ++iTry; + } #if ModernKeePassLib - //while(FileSystem.Current.GetFileFromPathAsync(strPath).Result != null); while (StorageFile.GetFileFromPathAsync(strPath).GetResults() != null); #else while(File.Exists(strPath)); diff --git a/ModernKeePassLib/Utility/GfxUtil.PCL.cs b/ModernKeePassLib/Utility/GfxUtil.PCL.cs new file mode 100644 index 0000000..16a8069 --- /dev/null +++ b/ModernKeePassLib/Utility/GfxUtil.PCL.cs @@ -0,0 +1,22 @@ +using System.IO; +using System.Threading.Tasks; +using Splat; + +namespace ModernKeePassLib.Utility +{ + public class GfxUtil + { + public static async Task LoadImage(byte[] pb) + { + return await ScaleImage(pb, null, null); + } + + public static async Task ScaleImage(byte[] pb, int? w, int? h) + { + using (var ms = new MemoryStream(pb, false)) + { + return await BitmapLoader.Current.Load(ms, w, h); + } + } + } +} diff --git a/ModernKeePassLib/Utility/GfxUtil.cs b/ModernKeePassLib/Utility/GfxUtil.cs index 976e790..9752628 100644 --- a/ModernKeePassLib/Utility/GfxUtil.cs +++ b/ModernKeePassLib/Utility/GfxUtil.cs @@ -1,6 +1,6 @@ /* KeePass Password Safe - The Open-Source Password Manager - Copyright (C) 2003-2014 Dominik Reichl + Copyright (C) 2003-2017 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 @@ -19,16 +19,15 @@ using System; using System.Collections.Generic; -using System.Text; +using System.Diagnostics; using System.IO; -#if ModernKeePassLib -using Splat; -#else +using System.Text; + +#if !KeePassUAP using System.Drawing; +using System.Drawing.Drawing2D; using System.Drawing.Imaging; #endif -using System.Diagnostics; -using System.Threading.Tasks; namespace ModernKeePassLib.Utility { @@ -68,19 +67,6 @@ namespace ModernKeePassLib.Utility try { return Image.FromStream(ms); } finally { ms.Close(); } } -#elif ModernKeePassLib - public static async Task LoadImage(byte[] pb) - { - return await ScaleImage(pb, null, null); - } - - public static async Task ScaleImage(byte[] pb, int? w, int? h) - { - using (var ms = new MemoryStream(pb, false)) - { - return await BitmapLoader.Current.Load(ms, w, h); - } - } #else public static Image LoadImage(byte[] pb) { @@ -437,9 +423,8 @@ namespace ModernKeePassLib.Utility #endif // DEBUG #endif // !KeePassLibSD #endif // KeePassUAP -#if ModernKeePassLib -#else - internal static string ImageToDataUri(Image img) + + internal static string ImageToDataUri(Image img) { if(img == null) { Debug.Assert(false); return string.Empty; } @@ -452,6 +437,5 @@ namespace ModernKeePassLib.Utility return StrUtil.DataToDataUri(pb, "image/png"); } -#endif - } + } }