KeePassLib bumped to 2.37, compilation ok.

WIP tests
This commit is contained in:
bg45
2017-10-21 13:25:54 -04:00
committed by BONNEVILLE Geoffroy
parent d5b7845242
commit c82d6d001d
15 changed files with 414 additions and 91 deletions

View File

@@ -22,11 +22,39 @@ namespace ModernKeePassLib.Native
get { throw new NotImplementedException(); }
}
public static int StrCmpNaturally (string s1, string s2)
internal const int GCRY_CIPHER_AES256 = 9;
internal const int GCRY_CIPHER_MODE_ECB = 1;
public static int StrCmpNaturally (string s1, string s2)
{
throw new NotImplementedException();
}
}
internal static void gcry_check_version(IntPtr zero)
{
throw new NotImplementedException();
}
public static void gcry_cipher_open(ref IntPtr intPtr, object gcryCipherAes256, object gcryCipherModeEcb, int i)
{
throw new NotImplementedException();
}
internal static int gcry_cipher_setkey(IntPtr h, IntPtr pSeed32, IntPtr n32)
{
throw new NotImplementedException();
}
internal static void gcry_cipher_close(IntPtr h)
{
throw new NotImplementedException();
}
internal static int gcry_cipher_encrypt(IntPtr h, IntPtr pData32, IntPtr n32, IntPtr zero1, IntPtr zero2)
{
throw new NotImplementedException();
}
}
internal enum DataProtectionScope
{

View File

@@ -50,6 +50,24 @@ namespace ModernKeePassLib.Native
set { m_bAllowNative = value; }
}
private static int? g_oiPointerSize = null;
/// <summary>
/// Size of a native pointer (in bytes).
/// </summary>
public static int PointerSize
{
get
{
if(!g_oiPointerSize.HasValue)
#if KeePassUAP
g_oiPointerSize = Marshal.SizeOf<IntPtr>();
#else
g_oiPointerSize = Marshal.SizeOf(typeof(IntPtr));
#endif
return g_oiPointerSize.Value;
}
}
private static ulong? m_ouMonoVersion = null;
public static ulong MonoVersion
{
@@ -130,13 +148,13 @@ namespace ModernKeePassLib.Native
{
if(m_platID.HasValue) return m_platID.Value;
#if KeePassRT
m_platID = PlatformID.Win32NT;
#if KeePassUAP
m_platID = EnvironmentExt.OSVersion.Platform;
#else
m_platID = Environment.OSVersion.Platform;
#endif
#if (!KeePassLibSD && !KeePassRT)
#if (!KeePassLibSD && !KeePassUAP)
// Mono returns PlatformID.Unix on Mac OS X, workaround this
if(m_platID.Value == PlatformID.Unix)
{
@@ -149,7 +167,55 @@ namespace ModernKeePassLib.Native
return m_platID.Value;
}
#if (!KeePassLibSD && !KeePassRT)
private static DesktopType? m_tDesktop = null;
public static DesktopType GetDesktopType()
{
if(!m_tDesktop.HasValue)
{
DesktopType t = DesktopType.None;
if(!IsUnix()) t = DesktopType.Windows;
else
{
try
{
string strXdg = (Environment.GetEnvironmentVariable(
"XDG_CURRENT_DESKTOP") ?? string.Empty).Trim();
string strGdm = (Environment.GetEnvironmentVariable(
"GDMSESSION") ?? string.Empty).Trim();
StringComparison sc = StrUtil.CaseIgnoreCmp;
if(strXdg.Equals("Unity", sc))
t = DesktopType.Unity;
else if(strXdg.Equals("LXDE", sc))
t = DesktopType.Lxde;
else if(strXdg.Equals("XFCE", sc))
t = DesktopType.Xfce;
else if(strXdg.Equals("MATE", sc))
t = DesktopType.Mate;
else if(strXdg.Equals("X-Cinnamon", sc))
t = DesktopType.Cinnamon;
else if(strXdg.Equals("Pantheon", sc)) // Elementary OS
t = DesktopType.Pantheon;
else if(strXdg.Equals("KDE", sc) || // Mint 16
strGdm.Equals("kde-plasma", sc)) // Ubuntu 12.04
t = DesktopType.Kde;
else if(strXdg.Equals("GNOME", sc))
{
if(strGdm.Equals("cinnamon", sc)) // Mint 13
t = DesktopType.Cinnamon;
else t = DesktopType.Gnome;
}
}
catch(Exception) { Debug.Assert(false); }
}
m_tDesktop = t;
}
return m_tDesktop.Value;
}
#if (!KeePassLibSD && !KeePassUAP)
public static string RunConsoleApp(string strAppPath, string strParams)
{
return RunConsoleApp(strAppPath, strParams, null);
@@ -192,8 +258,6 @@ namespace ModernKeePassLib.Native
if(strStdInput != null)
{
// Workaround for Mono Process StdIn BOM bug;
// https://sourceforge.net/p/keepass/bugs/1219/
EnsureNoBom(p.StandardInput);
p.StandardInput.Write(strStdInput);
@@ -217,7 +281,11 @@ namespace ModernKeePassLib.Native
return strOutput;
}
catch(Exception) { Debug.Assert(false); }
#if DEBUG
catch(Exception ex) { Debug.Assert(ex is ThreadAbortException); }
#else
catch(Exception) { }
#endif
return null;
};
@@ -258,7 +326,7 @@ namespace ModernKeePassLib.Native
private static void EnsureNoBom(StreamWriter sw)
{
if(sw == null) { Debug.Assert(false); return; }
if(!NativeLib.IsUnix()) return;
if(!MonoWorkarounds.IsRequired(1219)) return;
try
{
@@ -267,9 +335,24 @@ namespace ModernKeePassLib.Native
byte[] pbBom = enc.GetPreamble();
if((pbBom == null) || (pbBom.Length == 0)) return;
FieldInfo fi = typeof(StreamWriter).GetField("preamble_done",
// For Mono >= 4.0 (using Microsoft's reference source)
try
{
FieldInfo fi = typeof(StreamWriter).GetField("haveWrittenPreamble",
BindingFlags.Instance | BindingFlags.NonPublic);
if(fi != null)
{
fi.SetValue(sw, true);
return;
}
}
catch(Exception) { Debug.Assert(false); }
// For Mono < 4.0
FieldInfo fiPD = typeof(StreamWriter).GetField("preamble_done",
BindingFlags.Instance | BindingFlags.NonPublic);
if(fi != null) fi.SetValue(sw, true);
if(fiPD != null) fiPD.SetValue(sw, true);
else { Debug.Assert(false); }
}
catch(Exception) { Debug.Assert(false); }
}
@@ -285,7 +368,10 @@ namespace ModernKeePassLib.Native
public static bool TransformKey256(byte[] pBuf256, byte[] pKey256,
ulong uRounds)
{
if(m_bAllowNative == false) return false;
#if KeePassUAP
return false;
#else
if(!m_bAllowNative) return false;
KeyValuePair<IntPtr, IntPtr> kvp = PrepareArrays256(pBuf256, pKey256);
bool bResult = false;
@@ -298,26 +384,31 @@ namespace ModernKeePassLib.Native
if(bResult) GetBuffers256(kvp, pBuf256, pKey256);
NativeLib.FreeArrays(kvp);
FreeArrays(kvp);
return bResult;
#endif
}
/// <summary>
/// Benchmark key transformation.
/// </summary>
/// <param name="uTimeMs">Number of seconds to perform the benchmark.</param>
/// <param name="uTimeMs">Number of milliseconds to perform the benchmark.</param>
/// <param name="puRounds">Number of transformations done.</param>
/// <returns>Returns <c>true</c>, if the benchmark was successful.</returns>
public static bool TransformKeyBenchmark256(uint uTimeMs, out ulong puRounds)
{
puRounds = 0;
if(m_bAllowNative == false) return false;
#if KeePassUAP
return false;
#else
if(!m_bAllowNative) return false;
try { puRounds = NativeMethods.TransformKeyBenchmark(uTimeMs); }
catch(Exception) { return false; }
return true;
#endif
}
private static KeyValuePair<IntPtr, IntPtr> PrepareArrays256(byte[] pBuf256,

View File

@@ -29,7 +29,7 @@ namespace ModernKeePassLib.Native
{
internal static partial class NativeMethods
{
#if (!KeePassLibSD && !KeePassRT)
#if (!KeePassLibSD && !KeePassUAP)
[StructLayout(LayoutKind.Sequential)]
private struct XClassHint
{
@@ -108,5 +108,106 @@ namespace ModernKeePassLib.Native
catch(Exception) { Debug.Assert(false); }
}
#endif
// =============================================================
// LibGCrypt 1.8.1
private const string LibGCrypt = "libgcrypt.so.20";
internal const int GCRY_CIPHER_AES256 = 9;
internal const int GCRY_CIPHER_MODE_ECB = 1;
[DllImport(LibGCrypt)]
internal static extern IntPtr gcry_check_version(IntPtr lpReqVersion);
[DllImport(LibGCrypt)]
internal static extern uint gcry_cipher_open(ref IntPtr ph, int nAlgo,
int nMode, uint uFlags);
[DllImport(LibGCrypt)]
internal static extern void gcry_cipher_close(IntPtr h);
[DllImport(LibGCrypt)]
internal static extern uint gcry_cipher_setkey(IntPtr h, IntPtr pbKey,
IntPtr cbKey); // cbKey is size_t
[DllImport(LibGCrypt)]
internal static extern uint gcry_cipher_encrypt(IntPtr h, IntPtr pbOut,
IntPtr cbOut, IntPtr pbIn, IntPtr cbIn); // cb* are size_t
/* internal static IntPtr Utf8ZFromString(string str)
{
byte[] pb = StrUtil.Utf8.GetBytes(str ?? string.Empty);
IntPtr p = Marshal.AllocCoTaskMem(pb.Length + 1);
if(p != IntPtr.Zero)
{
Marshal.Copy(pb, 0, p, pb.Length);
Marshal.WriteByte(p, pb.Length, 0);
}
else { Debug.Assert(false); }
return p;
}
internal static string Utf8ZToString(IntPtr p)
{
if(p == IntPtr.Zero) { Debug.Assert(false); return null; }
List<byte> l = new List<byte>();
for(int i = 0; i < int.MaxValue; ++i)
{
byte bt = Marshal.ReadByte(p, i);
if(bt == 0) break;
l.Add(bt);
}
return StrUtil.Utf8.GetString(l.ToArray());
}
internal static void Utf8ZFree(IntPtr p)
{
if(p != IntPtr.Zero) Marshal.FreeCoTaskMem(p);
} */
/* // =============================================================
// LibGLib 2
private const string LibGLib = "libglib-2.0.so.0";
internal const int G_FALSE = 0;
// https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
[DllImport(LibGLib)]
internal static extern void g_free(IntPtr pMem); // pMem may be null
// =============================================================
// LibGTK 3 (3.22.11 / 3.22.24)
private const string LibGtk = "libgtk-3.so.0";
internal static readonly IntPtr GDK_SELECTION_PRIMARY = new IntPtr(1);
internal static readonly IntPtr GDK_SELECTION_CLIPBOARD = new IntPtr(69);
[DllImport(LibGtk)]
internal static extern int gtk_init_check(IntPtr pArgc, IntPtr pArgv);
[DllImport(LibGtk)]
// The returned handle is owned by GTK and must not be freed
internal static extern IntPtr gtk_clipboard_get(IntPtr pSelection);
[DllImport(LibGtk)]
internal static extern void gtk_clipboard_clear(IntPtr hClipboard);
[DllImport(LibGtk)]
internal static extern IntPtr gtk_clipboard_wait_for_text(IntPtr hClipboard);
[DllImport(LibGtk)]
internal static extern void gtk_clipboard_set_text(IntPtr hClipboard,
IntPtr lpText, int cbLen);
[DllImport(LibGtk)]
internal static extern void gtk_clipboard_store(IntPtr hClipboard); */
}
}

View File

@@ -73,6 +73,7 @@ namespace ModernKeePassLib.Native
return TransformKeyTimed32(pBuf256, pKey256, ref puRounds, uSeconds);
} */
#if !KeePassUAP
[DllImport("KeePassLibC32.dll", EntryPoint = "TransformKey256")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TransformKey32(IntPtr pBuf256,
@@ -86,7 +87,7 @@ namespace ModernKeePassLib.Native
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
UInt64 uRounds)
{
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
if(NativeLib.PointerSize == 8)
return TransformKey64(pBuf256, pKey256, uRounds);
else
return TransformKey32(pBuf256, pKey256, uRounds);
@@ -100,10 +101,11 @@ namespace ModernKeePassLib.Native
internal static UInt64 TransformKeyBenchmark(UInt32 uTimeMs)
{
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
if(NativeLib.PointerSize == 8)
return TransformKeyBenchmark64(uTimeMs);
return TransformKeyBenchmark32(uTimeMs);
}
#endif
/* [DllImport("KeePassLibC32.dll", EntryPoint = "TF_ShowLangBar")]
[return: MarshalAs(UnmanagedType.Bool)]
@@ -120,63 +122,66 @@ namespace ModernKeePassLib.Native
return TF_ShowLangBar32(dwFlags);
} */
#if (!KeePassLibSD && !KeePassRT)
[DllImport("ShlWApi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int StrCmpLogicalW(string x, string y);
#if (!KeePassLibSD && !KeePassUAP)
[DllImport("ShlWApi.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PathRelativePathTo([Out] StringBuilder pszPath,
[In] string pszFrom, [In] uint dwAttrFrom, [In] string pszTo,
[In] uint dwAttrTo);
#endif
[In] string pszFrom, uint dwAttrFrom, [In] string pszTo, uint dwAttrTo);
private static bool? m_bSupportsLogicalCmp = null;
[DllImport("ShlWApi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int StrCmpLogicalW(string x, string y);
private static bool? m_obSupportsLogicalCmp = null;
private static void TestNaturalComparisonsSupport()
{
#if (KeePassLibSD || KeePassRT)
#warning No native natural comparisons supported.
m_bSupportsLogicalCmp = false;
#else
try
{
StrCmpLogicalW("0", "0"); // Throws exception if unsupported
m_bSupportsLogicalCmp = true;
m_obSupportsLogicalCmp = true;
}
catch(Exception) { m_bSupportsLogicalCmp = false; }
#endif
catch(Exception) { m_obSupportsLogicalCmp = false; }
}
#endif
internal static bool SupportsStrCmpNaturally
{
get
{
if(m_bSupportsLogicalCmp.HasValue == false)
#if (!KeePassLibSD && !KeePassUAP)
if(!m_obSupportsLogicalCmp.HasValue)
TestNaturalComparisonsSupport();
return m_bSupportsLogicalCmp.Value;
return m_obSupportsLogicalCmp.Value;
#else
return false;
#endif
}
}
internal static int StrCmpNaturally(string x, string y)
{
if(m_bSupportsLogicalCmp.HasValue == false) TestNaturalComparisonsSupport();
if(m_bSupportsLogicalCmp.Value == false) return 0;
#if (!KeePassLibSD && !KeePassUAP)
if(!NativeMethods.SupportsStrCmpNaturally)
{
Debug.Assert(false);
return string.Compare(x, y, true);
}
#if (KeePassLibSD || KeePassRT)
#warning No native natural comparisons supported.
return x.CompareTo(y);
#else
return StrCmpLogicalW(x, y);
#else
Debug.Assert(false);
return string.Compare(x, y, true);
#endif
}
internal static string GetUserRuntimeDir()
{
#if !KeePassLibSD
#if KeePassRT
string strRtDir = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#if KeePassLibSD
return Path.GetTempPath();
#else
#if KeePassUAP
string strRtDir = EnvironmentExt.AppDataLocalFolderPath;
#else
string strRtDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
if(string.IsNullOrEmpty(strRtDir))
@@ -192,8 +197,6 @@ namespace ModernKeePassLib.Native
strRtDir += PwDefs.ShortProductName;
return strRtDir;
#else
return Path.GetTempPath();
#endif
}
}