ModernKeePassLib custom PCL version

This commit is contained in:
2017-09-22 15:40:24 +02:00
parent baba70e56d
commit a43bc20eb3
98 changed files with 6049 additions and 3038 deletions

View File

@@ -0,0 +1,70 @@
using System;
using PlatformID = System.UInt32;
namespace KeePass2PCL.Native
{
internal static class NativeLib
{
public static ulong MonoVersion {
get { throw new NotImplementedException(); }
}
public static bool IsUnix()
{
return true;
}
}
internal static class NativeMethods
{
public static bool SupportsStrCmpNaturally {
get { throw new NotImplementedException(); }
}
public static int StrCmpNaturally (string s1, string s2)
{
throw new NotImplementedException();
}
}
internal enum DataProtectionScope
{
CurrentUser,
LocalMachine
}
internal static class ProtectedData
{
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
throw new NotImplementedException();
}
public static byte[] Unprotect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
throw new NotImplementedException();
}
}
internal enum MemoryProtectionScope
{
CrossProcess,
SameLogon,
SameProcess
}
internal static class ProtectedMemory
{
public static byte[] Protect(byte[] userData, MemoryProtectionScope scope)
{
throw new NotImplementedException();
}
public static byte[] Unprotect(byte[] userData, MemoryProtectionScope scope)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,6 +1,6 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
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,7 +19,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using ModernKeePassLib.Utility;
@@ -44,6 +50,43 @@ namespace ModernKeePassLib.Native
set { m_bAllowNative = value; }
}
private static ulong? m_ouMonoVersion = null;
public static ulong MonoVersion
{
get
{
if(m_ouMonoVersion.HasValue) return m_ouMonoVersion.Value;
ulong uVersion = 0;
try
{
Type t = Type.GetType("Mono.Runtime");
if(t != null)
{
MethodInfo mi = t.GetMethod("GetDisplayName",
BindingFlags.NonPublic | BindingFlags.Static);
if(mi != null)
{
string strName = (mi.Invoke(null, null) as string);
if(!string.IsNullOrEmpty(strName))
{
Match m = Regex.Match(strName, "\\d+(\\.\\d+)+");
if(m.Success)
uVersion = StrUtil.ParseVersion(m.Value);
else { Debug.Assert(false); }
}
else { Debug.Assert(false); }
}
else { Debug.Assert(false); }
}
}
catch(Exception) { Debug.Assert(false); }
m_ouMonoVersion = uVersion;
return uVersion;
}
}
/// <summary>
/// Determine if the native library is installed.
/// </summary>
@@ -70,10 +113,6 @@ namespace ModernKeePassLib.Native
{
if(m_bIsUnix.HasValue) return m_bIsUnix.Value;
#if KeePassWinRT
return false;
#else
PlatformID p = GetPlatformID();
// Mono defines Unix as 128 in early .NET versions
@@ -84,19 +123,20 @@ namespace ModernKeePassLib.Native
m_bIsUnix = (((int)p == 4) || ((int)p == 6) || ((int)p == 128));
#endif
return m_bIsUnix.Value;
#endif //KeePassWinRT
}
}
// TODO Bert : Not supported for the time being.
#if !KeePassWinRT
private static PlatformID? m_platID = null;
public static PlatformID GetPlatformID()
{
if(m_platID.HasValue) return m_platID.Value;
#if KeePassRT
m_platID = PlatformID.Win32NT;
#else
m_platID = Environment.OSVersion.Platform;
#endif
#if !KeePassLibSD
#if (!KeePassLibSD && !KeePassRT)
// Mono returns PlatformID.Unix on Mac OS X, workaround this
if(m_platID.Value == PlatformID.Unix)
{
@@ -108,11 +148,8 @@ namespace ModernKeePassLib.Native
return m_platID.Value;
}
#endif
// BERT Todo: Not supported for the moment.
#if !KeePassLibSD && TODO
#if (!KeePassLibSD && !KeePassRT)
public static string RunConsoleApp(string strAppPath, string strParams)
{
return RunConsoleApp(strAppPath, strParams, null);
@@ -120,40 +157,121 @@ namespace ModernKeePassLib.Native
public static string RunConsoleApp(string strAppPath, string strParams,
string strStdInput)
{
return RunConsoleApp(strAppPath, strParams, strStdInput,
(AppRunFlags.GetStdOutput | AppRunFlags.WaitForExit));
}
private delegate string RunProcessDelegate();
public static string RunConsoleApp(string strAppPath, string strParams,
string strStdInput, AppRunFlags f)
{
if(strAppPath == null) throw new ArgumentNullException("strAppPath");
if(strAppPath.Length == 0) throw new ArgumentException("strAppPath");
try
bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);
RunProcessDelegate fnRun = delegate()
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = strAppPath;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
if(strStdInput != null) psi.RedirectStandardInput = true;
if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;
Process p = Process.Start(psi);
if(strStdInput != null)
try
{
p.StandardInput.Write(strStdInput);
p.StandardInput.Close();
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = strAppPath;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = bStdOut;
if(strStdInput != null) psi.RedirectStandardInput = true;
if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;
Process p = Process.Start(psi);
if(strStdInput != null)
{
// Workaround for Mono Process StdIn BOM bug;
// https://sourceforge.net/p/keepass/bugs/1219/
EnsureNoBom(p.StandardInput);
p.StandardInput.Write(strStdInput);
p.StandardInput.Close();
}
string strOutput = string.Empty;
if(bStdOut) strOutput = p.StandardOutput.ReadToEnd();
if((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
p.WaitForExit();
else if((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
{
Thread th = new Thread(delegate()
{
try { p.WaitForExit(); }
catch(Exception) { Debug.Assert(false); }
});
th.Start();
}
return strOutput;
}
catch(Exception) { Debug.Assert(false); }
return null;
};
if((f & AppRunFlags.DoEvents) != AppRunFlags.None)
{
List<Form> lDisabledForms = new List<Form>();
if((f & AppRunFlags.DisableForms) != AppRunFlags.None)
{
foreach(Form form in Application.OpenForms)
{
if(!form.Enabled) continue;
lDisabledForms.Add(form);
form.Enabled = false;
}
}
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
IAsyncResult ar = fnRun.BeginInvoke(null, null);
return strOutput;
while(!ar.AsyncWaitHandle.WaitOne(0))
{
Application.DoEvents();
Thread.Sleep(2);
}
string strRet = fnRun.EndInvoke(ar);
for(int i = lDisabledForms.Count - 1; i >= 0; --i)
lDisabledForms[i].Enabled = true;
return strRet;
}
return fnRun();
}
private static void EnsureNoBom(StreamWriter sw)
{
if(sw == null) { Debug.Assert(false); return; }
if(!NativeLib.IsUnix()) return;
try
{
Encoding enc = sw.Encoding;
if(enc == null) { Debug.Assert(false); return; }
byte[] pbBom = enc.GetPreamble();
if((pbBom == null) || (pbBom.Length == 0)) return;
FieldInfo fi = typeof(StreamWriter).GetField("preamble_done",
BindingFlags.Instance | BindingFlags.NonPublic);
if(fi != null) fi.SetValue(sw, true);
}
catch(Exception) { Debug.Assert(false); }
return null;
}
#endif
@@ -172,12 +290,12 @@ namespace ModernKeePassLib.Native
KeyValuePair<IntPtr, IntPtr> kvp = PrepareArrays256(pBuf256, pKey256);
bool bResult = false;
/*try
try
{
bResult = NativeMethods.TransformKey(kvp.Key, kvp.Value, uRounds);
}
catch(Exception) { bResult = false; }
*/
if(bResult) GetBuffers256(kvp, pBuf256, pKey256);
NativeLib.FreeArrays(kvp);
@@ -196,8 +314,8 @@ namespace ModernKeePassLib.Native
if(m_bAllowNative == false) return false;
/*try { puRounds = NativeMethods.TransformKeyBenchmark(uTimeMs); }
catch(Exception) { return false; }*/
try { puRounds = NativeMethods.TransformKeyBenchmark(uTimeMs); }
catch(Exception) { return false; }
return true;
}

View File

@@ -0,0 +1,112 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
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;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
namespace KeePass2PCL.Native
{
internal static partial class NativeMethods
{
#if (!KeePassLibSD && !KeePassRT)
[StructLayout(LayoutKind.Sequential)]
private struct XClassHint
{
public IntPtr res_name;
public IntPtr res_class;
}
[DllImport("libX11")]
private static extern int XSetClassHint(IntPtr display, IntPtr window, IntPtr class_hints);
private static Type m_tXplatUIX11 = null;
private static Type GetXplatUIX11Type(bool bThrowOnError)
{
if(m_tXplatUIX11 == null)
{
// CheckState is in System.Windows.Forms
string strTypeCS = typeof(CheckState).AssemblyQualifiedName;
string strTypeX11 = strTypeCS.Replace("CheckState", "XplatUIX11");
m_tXplatUIX11 = Type.GetType(strTypeX11, bThrowOnError, true);
}
return m_tXplatUIX11;
}
private static Type m_tHwnd = null;
private static Type GetHwndType(bool bThrowOnError)
{
if(m_tHwnd == null)
{
// CheckState is in System.Windows.Forms
string strTypeCS = typeof(CheckState).AssemblyQualifiedName;
string strTypeHwnd = strTypeCS.Replace("CheckState", "Hwnd");
m_tHwnd = Type.GetType(strTypeHwnd, bThrowOnError, true);
}
return m_tHwnd;
}
internal static void SetWmClass(Form f, string strName, string strClass)
{
if(f == null) { Debug.Assert(false); return; }
// The following crashes under Mac OS X (SIGSEGV in native code,
// not just an exception), thus skip it when we're on Mac OS X;
// https://sourceforge.net/projects/keepass/forums/forum/329221/topic/5860588
if(NativeLib.GetPlatformID() == PlatformID.MacOSX) return;
try
{
Type tXplatUIX11 = GetXplatUIX11Type(true);
FieldInfo fiDisplayHandle = tXplatUIX11.GetField("DisplayHandle",
BindingFlags.NonPublic | BindingFlags.Static);
IntPtr hDisplay = (IntPtr)fiDisplayHandle.GetValue(null);
Type tHwnd = GetHwndType(true);
MethodInfo miObjectFromHandle = tHwnd.GetMethod("ObjectFromHandle",
BindingFlags.Public | BindingFlags.Static);
object oHwnd = miObjectFromHandle.Invoke(null, new object[] { f.Handle });
FieldInfo fiWholeWindow = tHwnd.GetField("whole_window",
BindingFlags.NonPublic | BindingFlags.Instance);
IntPtr hWindow = (IntPtr)fiWholeWindow.GetValue(oHwnd);
XClassHint xch = new XClassHint();
xch.res_name = Marshal.StringToCoTaskMemAnsi(strName ?? string.Empty);
xch.res_class = Marshal.StringToCoTaskMemAnsi(strClass ?? string.Empty);
IntPtr pXch = Marshal.AllocCoTaskMem(Marshal.SizeOf(xch));
Marshal.StructureToPtr(xch, pXch, false);
XSetClassHint(hDisplay, hWindow, pXch);
Marshal.FreeCoTaskMem(pXch);
Marshal.FreeCoTaskMem(xch.res_name);
Marshal.FreeCoTaskMem(xch.res_class);
}
catch(Exception) { Debug.Assert(false); }
}
#endif
}
}

View File

@@ -1,6 +1,6 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
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
@@ -21,13 +21,20 @@ using System;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Native
{
internal static class NativeMethods
internal static partial class NativeMethods
{
internal const int MAX_PATH = 260;
// internal const uint TF_SFT_SHOWNORMAL = 0x00000001;
// internal const uint TF_SFT_HIDDEN = 0x00000008;
/* [DllImport("KeePassNtv32.dll", EntryPoint = "TransformKey")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TransformKey32(IntPtr pBuf256,
@@ -65,7 +72,7 @@ namespace ModernKeePassLib.Native
else
return TransformKeyTimed32(pBuf256, pKey256, ref puRounds, uSeconds);
} */
/*
[DllImport("KeePassLibC32.dll", EntryPoint = "TransformKey256")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TransformKey32(IntPtr pBuf256,
@@ -75,7 +82,7 @@ namespace ModernKeePassLib.Native
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TransformKey64(IntPtr pBuf256,
IntPtr pKey256, UInt64 uRounds);
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
UInt64 uRounds)
{
@@ -95,11 +102,25 @@ namespace ModernKeePassLib.Native
{
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
return TransformKeyBenchmark64(uTimeMs);
else
return TransformKeyBenchmark32(uTimeMs);
return TransformKeyBenchmark32(uTimeMs);
}
*/
#if !KeePassLibSD && TODO
/* [DllImport("KeePassLibC32.dll", EntryPoint = "TF_ShowLangBar")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TF_ShowLangBar32(UInt32 dwFlags);
[DllImport("KeePassLibC64.dll", EntryPoint = "TF_ShowLangBar")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TF_ShowLangBar64(UInt32 dwFlags);
internal static bool TfShowLangBar(uint dwFlags)
{
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
return TF_ShowLangBar64(dwFlags);
return TF_ShowLangBar32(dwFlags);
} */
#if (!KeePassLibSD && !KeePassRT)
[DllImport("ShlWApi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int StrCmpLogicalW(string x, string y);
@@ -114,7 +135,7 @@ namespace ModernKeePassLib.Native
private static void TestNaturalComparisonsSupport()
{
#if KeePassLibSD || !TODO
#if (KeePassLibSD || KeePassRT)
#warning No native natural comparisons supported.
m_bSupportsLogicalCmp = false;
#else
@@ -143,11 +164,36 @@ namespace ModernKeePassLib.Native
if(m_bSupportsLogicalCmp.HasValue == false) TestNaturalComparisonsSupport();
if(m_bSupportsLogicalCmp.Value == false) return 0;
#if KeePassLibSD || !TODO
#if (KeePassLibSD || KeePassRT)
#warning No native natural comparisons supported.
return x.CompareTo(y);
#else
return StrCmpLogicalW(x, y);
#endif
}
internal static string GetUserRuntimeDir()
{
#if !KeePassLibSD
#if KeePassRT
string strRtDir = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#else
string strRtDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
if(string.IsNullOrEmpty(strRtDir))
strRtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if(string.IsNullOrEmpty(strRtDir))
{
Debug.Assert(false);
return Path.GetTempPath(); // Not UrlUtil (otherwise cyclic)
}
#endif
strRtDir = UrlUtil.EnsureTerminatingSeparator(strRtDir, false);
strRtDir += PwDefs.ShortProductName;
return strRtDir;
#else
return Path.GetTempPath();
#endif
}
}