mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 16:10:16 -04:00
WIP Lib version 2.39.1
This commit is contained in:
@@ -223,48 +223,43 @@ namespace ModernKeePassLib.Cryptography
|
||||
pb = DiagnosticsExt.GetProcessEntropy();
|
||||
MemUtil.Write(ms, pb);
|
||||
#elif !KeePassLibSD
|
||||
Process p = null;
|
||||
try
|
||||
{
|
||||
p = Process.GetCurrentProcess();
|
||||
using(Process p = Process.GetCurrentProcess())
|
||||
{
|
||||
pb = MemUtil.Int64ToBytes(p.Handle.ToInt64());
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int32ToBytes(p.HandleCount);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int32ToBytes(p.Id);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.NonpagedSystemMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PagedMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PagedSystemMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakPagedMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakVirtualMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakWorkingSet64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PrivateMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.StartTime.ToBinary());
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.VirtualMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.WorkingSet64);
|
||||
MemUtil.Write(ms, pb);
|
||||
|
||||
pb = MemUtil.Int64ToBytes(p.Handle.ToInt64());
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int32ToBytes(p.HandleCount);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int32ToBytes(p.Id);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.NonpagedSystemMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PagedMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PagedSystemMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakPagedMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakVirtualMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PeakWorkingSet64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.PrivateMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.StartTime.ToBinary());
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.VirtualMemorySize64);
|
||||
MemUtil.Write(ms, pb);
|
||||
pb = MemUtil.Int64ToBytes(p.WorkingSet64);
|
||||
MemUtil.Write(ms, pb);
|
||||
|
||||
// Not supported in Mono 1.2.6:
|
||||
// pb = MemUtil.UInt32ToBytes((uint)p.SessionId);
|
||||
// MemUtil.Write(ms, pb);
|
||||
// Not supported in Mono 1.2.6:
|
||||
// pb = MemUtil.UInt32ToBytes((uint)p.SessionId);
|
||||
// MemUtil.Write(ms, pb);
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(NativeLib.IsUnix()); }
|
||||
finally
|
||||
{
|
||||
try { if(p != null) p.Dispose(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@@ -35,6 +35,42 @@ namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
public static class CryptoUtil
|
||||
{
|
||||
private static bool? g_obProtData = null;
|
||||
public static bool IsProtectedDataSupported
|
||||
{
|
||||
get
|
||||
{
|
||||
if(g_obProtData.HasValue) return g_obProtData.Value;
|
||||
|
||||
bool b = false;
|
||||
try
|
||||
{
|
||||
Random r = CryptoRandom.NewWeakRandom();
|
||||
|
||||
byte[] pbData = new byte[137];
|
||||
r.NextBytes(pbData);
|
||||
|
||||
byte[] pbEnt = new byte[41];
|
||||
r.NextBytes(pbEnt);
|
||||
|
||||
byte[] pbEnc = ProtectedData.Protect(pbData, pbEnt,
|
||||
DataProtectionScope.CurrentUser);
|
||||
if((pbEnc != null) && !MemUtil.ArraysEqual(pbEnc, pbData))
|
||||
{
|
||||
byte[] pbDec = ProtectedData.Unprotect(pbEnc, pbEnt,
|
||||
DataProtectionScope.CurrentUser);
|
||||
if((pbDec != null) && MemUtil.ArraysEqual(pbDec, pbData))
|
||||
b = true;
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
Debug.Assert(b); // Should be supported on all systems
|
||||
g_obProtData = b;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] HashSha256(byte[] pbData)
|
||||
{
|
||||
if(pbData == null) throw new ArgumentNullException("pbData");
|
||||
@@ -165,5 +201,37 @@ namespace ModernKeePassLib.Cryptography
|
||||
return null;
|
||||
}
|
||||
#endif
|
||||
public static byte[] ProtectData(byte[] pb, byte[] pbOptEntropy,
|
||||
DataProtectionScope s)
|
||||
{
|
||||
return ProtectDataPriv(pb, true, pbOptEntropy, s);
|
||||
}
|
||||
|
||||
public static byte[] UnprotectData(byte[] pb, byte[] pbOptEntropy,
|
||||
DataProtectionScope s)
|
||||
{
|
||||
return ProtectDataPriv(pb, false, pbOptEntropy, s);
|
||||
}
|
||||
|
||||
private static byte[] ProtectDataPriv(byte[] pb, bool bProtect,
|
||||
byte[] pbOptEntropy, DataProtectionScope s)
|
||||
{
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
|
||||
if((pbOptEntropy != null) && (pbOptEntropy.Length == 0))
|
||||
pbOptEntropy = null;
|
||||
|
||||
if(CryptoUtil.IsProtectedDataSupported)
|
||||
{
|
||||
if(bProtect)
|
||||
return ProtectedData.Protect(pb, pbOptEntropy, s);
|
||||
return ProtectedData.Unprotect(pb, pbOptEntropy, s);
|
||||
}
|
||||
|
||||
Debug.Assert(false);
|
||||
byte[] pbCopy = new byte[pb.Length];
|
||||
Array.Copy(pb, pbCopy, pb.Length);
|
||||
return pbCopy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -71,9 +71,6 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
|
||||
private static bool GCryptInitLib()
|
||||
{
|
||||
Debug.Assert(Marshal.SizeOf(typeof(int)) == 4); // Also on 64-bit systems
|
||||
Debug.Assert(Marshal.SizeOf(typeof(uint)) == 4);
|
||||
|
||||
if(!NativeLib.IsUnix()) return false; // Independent of workaround state
|
||||
if(!MonoWorkarounds.IsRequired(1468)) return false; // Can be turned off
|
||||
|
||||
|
@@ -239,8 +239,7 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
|
||||
PwProfile pp = new PwProfile();
|
||||
Debug.Assert(psPassword != null); if(psPassword == null) return pp;
|
||||
|
||||
byte[] pbUtf8 = psPassword.ReadUtf8();
|
||||
char[] vChars = StrUtil.Utf8.GetChars(pbUtf8);
|
||||
char[] vChars = psPassword.ReadChars();
|
||||
|
||||
pp.GeneratorType = PasswordGeneratorType.CharSet;
|
||||
pp.Length = (uint)vChars.Length;
|
||||
@@ -266,7 +265,6 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
|
||||
}
|
||||
|
||||
MemUtil.ZeroArray<char>(vChars);
|
||||
MemUtil.ZeroByteArray(pbUtf8);
|
||||
return pp;
|
||||
}
|
||||
|
||||
|
@@ -75,7 +75,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
#endif
|
||||
#endif
|
||||
|
||||
try { return IsPopularPasswordPriv(vPassword, out uDictSize); }
|
||||
try { return IsPopularPasswordPriv(vPassword, out uDictSize); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
uDictSize = 0;
|
||||
|
@@ -420,11 +420,12 @@ namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
if(pbUnprotectedUtf8 == null) { Debug.Assert(false); return 0; }
|
||||
|
||||
char[] vChars = StrUtil.Utf8.GetChars(pbUnprotectedUtf8);
|
||||
uint uResult = EstimatePasswordBits(vChars);
|
||||
MemUtil.ZeroArray<char>(vChars);
|
||||
char[] v = StrUtil.Utf8.GetChars(pbUnprotectedUtf8);
|
||||
uint r;
|
||||
try { r = EstimatePasswordBits(v); }
|
||||
finally { MemUtil.ZeroArray<char>(v); }
|
||||
|
||||
return uResult;
|
||||
return r;
|
||||
}
|
||||
|
||||
private static QeCharType GetCharType(char ch)
|
||||
|
@@ -22,6 +22,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
|
||||
@@ -55,6 +56,25 @@ namespace ModernKeePassLib.Cryptography
|
||||
/// </summary>
|
||||
public static void Perform()
|
||||
{
|
||||
#if KeePassUAP
|
||||
Debug.Assert(Marshal.SizeOf<int>() == 4);
|
||||
Debug.Assert(Marshal.SizeOf<uint>() == 4);
|
||||
Debug.Assert(Marshal.SizeOf<long>() == 8);
|
||||
Debug.Assert(Marshal.SizeOf<ulong>() == 8);
|
||||
Debug.Assert(Marshal.SizeOf<IntPtr>() == IntPtr.Size);
|
||||
#else
|
||||
Debug.Assert(Marshal.SizeOf(typeof(int)) == 4);
|
||||
Debug.Assert(Marshal.SizeOf(typeof(uint)) == 4);
|
||||
Debug.Assert(Marshal.SizeOf(typeof(long)) == 8);
|
||||
Debug.Assert(Marshal.SizeOf(typeof(ulong)) == 8);
|
||||
Debug.Assert(Marshal.SizeOf(typeof(IntPtr)) == IntPtr.Size);
|
||||
#endif
|
||||
Debug.Assert((IntPtr.Size == 4) || (IntPtr.Size == 8));
|
||||
|
||||
Debug.Assert((int)PwIcon.World == 1);
|
||||
Debug.Assert((int)PwIcon.Warning == 2);
|
||||
Debug.Assert((int)PwIcon.BlackBerry == 68);
|
||||
|
||||
Random r = CryptoRandom.NewWeakRandom();
|
||||
|
||||
TestFipsComplianceProblems(); // Must be the first test
|
||||
@@ -76,10 +96,6 @@ namespace ModernKeePassLib.Cryptography
|
||||
TestStrUtil();
|
||||
TestUrlUtil();
|
||||
|
||||
Debug.Assert((int)PwIcon.World == 1);
|
||||
Debug.Assert((int)PwIcon.Warning == 2);
|
||||
Debug.Assert((int)PwIcon.BlackBerry == 68);
|
||||
|
||||
#if KeePassUAP
|
||||
SelfTestEx.Perform();
|
||||
#endif
|
||||
@@ -944,6 +960,14 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(ps.ReadString() != str)
|
||||
throw new SecurityException("ProtectedString-14");
|
||||
}
|
||||
|
||||
ps = new ProtectedString(false, "ABCD");
|
||||
ps2 = new ProtectedString(true, "EFG");
|
||||
ps += (ps2 + "HI");
|
||||
if(!ps.Equals(new ProtectedString(true, "ABCDEFGHI"), true))
|
||||
throw new SecurityException("ProtectedString-15");
|
||||
if(!ps.Equals(new ProtectedString(false, "ABCDEFGHI"), false))
|
||||
throw new SecurityException("ProtectedString-16");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1041,6 +1065,30 @@ namespace ModernKeePassLib.Cryptography
|
||||
throw new InvalidOperationException("StrUtil-Case1");
|
||||
if(string.Equals(@"a<b", @"a>b", StrUtil.CaseIgnoreCmp))
|
||||
throw new InvalidOperationException("StrUtil-Case2");
|
||||
|
||||
const string strNL = "\n01\r23\n45\r\n67\r";
|
||||
string strW = StrUtil.NormalizeNewLines(strNL, true);
|
||||
string strL = StrUtil.NormalizeNewLines(strNL, false);
|
||||
if(strW != "\r\n01\r\n23\r\n45\r\n67\r\n")
|
||||
throw new InvalidOperationException("StrUtil-NewLine1");
|
||||
if(strL != "\n01\n23\n45\n67\n")
|
||||
throw new InvalidOperationException("StrUtil-NewLine2");
|
||||
if(StrUtil.IsNewLineNormalized(strNL.ToCharArray(), true))
|
||||
throw new InvalidOperationException("StrUtil-NewLine3");
|
||||
if(StrUtil.IsNewLineNormalized(strNL.ToCharArray(), false))
|
||||
throw new InvalidOperationException("StrUtil-NewLine4");
|
||||
if(!StrUtil.IsNewLineNormalized(strW.ToCharArray(), true))
|
||||
throw new InvalidOperationException("StrUtil-NewLine5");
|
||||
if(StrUtil.IsNewLineNormalized(strW.ToCharArray(), false))
|
||||
throw new InvalidOperationException("StrUtil-NewLine6");
|
||||
if(StrUtil.IsNewLineNormalized(strL.ToCharArray(), true))
|
||||
throw new InvalidOperationException("StrUtil-NewLine7");
|
||||
if(!StrUtil.IsNewLineNormalized(strL.ToCharArray(), false))
|
||||
throw new InvalidOperationException("StrUtil-NewLine8");
|
||||
if(!StrUtil.IsNewLineNormalized(string.Empty.ToCharArray(), true))
|
||||
throw new InvalidOperationException("StrUtil-NewLine9");
|
||||
if(!StrUtil.IsNewLineNormalized(string.Empty.ToCharArray(), false))
|
||||
throw new InvalidOperationException("StrUtil-NewLine10");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user