~ubuntu-branches/ubuntu/wily/keepass2/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-webdav-storage-with-mono-2.11.patch/KeePassLib/Native/NativeLib.cs

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2014-08-17 15:55:03 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20140817155503-3jcbkd107zug4clp
Tags: 2.27+dfsg-1
* New upstream release (Closes: #754575)
* remove upstream applied fix-webdav-storage-with-mono-2.11.patch
* add fix-autotype-paste.patch to fix pasting with autotype
* mention the need for the mono-mcs package to be installed for plugins to
  work in README.Debian (Closes: #749346)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  KeePass Password Safe - The Open-Source Password Manager
3
 
  Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
4
 
 
5
 
  This program is free software; you can redistribute it and/or modify
6
 
  it under the terms of the GNU General Public License as published by
7
 
  the Free Software Foundation; either version 2 of the License, or
8
 
  (at your option) any later version.
9
 
 
10
 
  This program is distributed in the hope that it will be useful,
11
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
  GNU General Public License for more details.
14
 
 
15
 
  You should have received a copy of the GNU General Public License
16
 
  along with this program; if not, write to the Free Software
17
 
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
*/
19
 
 
20
 
using System;
21
 
using System.Collections.Generic;
22
 
using System.Text;
23
 
using System.Runtime.InteropServices;
24
 
using System.Windows.Forms;
25
 
using System.Threading;
26
 
using System.IO;
27
 
using System.Reflection;
28
 
using System.Diagnostics;
29
 
 
30
 
using KeePassLib.Utility;
31
 
 
32
 
namespace KeePassLib.Native
33
 
{
34
 
        /// <summary>
35
 
        /// Interface to native library (library containing fast versions of
36
 
        /// several cryptographic functions).
37
 
        /// </summary>
38
 
        public static class NativeLib
39
 
        {
40
 
                private static bool m_bAllowNative = true;
41
 
 
42
 
                /// <summary>
43
 
                /// If this property is set to <c>true</c>, the native library is used.
44
 
                /// If it is <c>false</c>, all calls to functions in this class will fail.
45
 
                /// </summary>
46
 
                public static bool AllowNative
47
 
                {
48
 
                        get { return m_bAllowNative; }
49
 
                        set { m_bAllowNative = value; }
50
 
                }
51
 
 
52
 
                /// <summary>
53
 
                /// Determine if the native library is installed.
54
 
                /// </summary>
55
 
                /// <returns>Returns <c>true</c>, if the native library is installed.</returns>
56
 
                public static bool IsLibraryInstalled()
57
 
                {
58
 
                        byte[] pDummy0 = new byte[32];
59
 
                        byte[] pDummy1 = new byte[32];
60
 
 
61
 
                        // Save the native state
62
 
                        bool bCachedNativeState = m_bAllowNative;
63
 
 
64
 
                        // Temporarily allow native functions and try to load the library
65
 
                        m_bAllowNative = true;
66
 
                        bool bResult = TransformKey256(pDummy0, pDummy1, 16);
67
 
 
68
 
                        // Pop native state and return result
69
 
                        m_bAllowNative = bCachedNativeState;
70
 
                        return bResult;
71
 
                }
72
 
 
73
 
                private static bool? m_bIsUnix = null;
74
 
                public static bool IsUnix()
75
 
                {
76
 
                        if(m_bIsUnix.HasValue) return m_bIsUnix.Value;
77
 
 
78
 
                        PlatformID p = GetPlatformID();
79
 
 
80
 
                        // Mono defines Unix as 128 in early .NET versions
81
 
#if !KeePassLibSD
82
 
                        m_bIsUnix = ((p == PlatformID.Unix) || (p == PlatformID.MacOSX) ||
83
 
                                ((int)p == 128));
84
 
#else
85
 
                        m_bIsUnix = (((int)p == 4) || ((int)p == 6) || ((int)p == 128));
86
 
#endif
87
 
                        return m_bIsUnix.Value;
88
 
                }
89
 
 
90
 
                private static PlatformID? m_platID = null;
91
 
                public static PlatformID GetPlatformID()
92
 
                {
93
 
                        if(m_platID.HasValue) return m_platID.Value;
94
 
 
95
 
#if KeePassRT
96
 
                        m_platID = PlatformID.Win32NT;
97
 
#else
98
 
                        m_platID = Environment.OSVersion.Platform;
99
 
#endif
100
 
 
101
 
#if (!KeePassLibSD && !KeePassRT)
102
 
                        // Mono returns PlatformID.Unix on Mac OS X, workaround this
103
 
                        if(m_platID.Value == PlatformID.Unix)
104
 
                        {
105
 
                                if((RunConsoleApp("uname", null) ?? string.Empty).Trim().Equals(
106
 
                                        "Darwin", StrUtil.CaseIgnoreCmp))
107
 
                                        m_platID = PlatformID.MacOSX;
108
 
                        }
109
 
#endif
110
 
 
111
 
                        return m_platID.Value;
112
 
                }
113
 
 
114
 
#if (!KeePassLibSD && !KeePassRT)
115
 
                public static string RunConsoleApp(string strAppPath, string strParams)
116
 
                {
117
 
                        return RunConsoleApp(strAppPath, strParams, null);
118
 
                }
119
 
 
120
 
                public static string RunConsoleApp(string strAppPath, string strParams,
121
 
                        string strStdInput)
122
 
                {
123
 
                        return RunConsoleApp(strAppPath, strParams, strStdInput,
124
 
                                (AppRunFlags.GetStdOutput | AppRunFlags.WaitForExit));
125
 
                }
126
 
 
127
 
                private delegate string RunProcessDelegate();
128
 
 
129
 
                public static string RunConsoleApp(string strAppPath, string strParams,
130
 
                        string strStdInput, AppRunFlags f)
131
 
                {
132
 
                        if(strAppPath == null) throw new ArgumentNullException("strAppPath");
133
 
                        if(strAppPath.Length == 0) throw new ArgumentException("strAppPath");
134
 
 
135
 
                        bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);
136
 
 
137
 
                        RunProcessDelegate fnRun = delegate()
138
 
                        {
139
 
                                try
140
 
                                {
141
 
                                        ProcessStartInfo psi = new ProcessStartInfo();
142
 
 
143
 
                                        psi.CreateNoWindow = true;
144
 
                                        psi.FileName = strAppPath;
145
 
                                        psi.WindowStyle = ProcessWindowStyle.Hidden;
146
 
                                        psi.UseShellExecute = false;
147
 
                                        psi.RedirectStandardOutput = bStdOut;
148
 
 
149
 
                                        if(strStdInput != null) psi.RedirectStandardInput = true;
150
 
 
151
 
                                        if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;
152
 
 
153
 
                                        Process p = Process.Start(psi);
154
 
 
155
 
                                        if(strStdInput != null)
156
 
                                        {
157
 
                                                // Workaround for Mono Process StdIn BOM bug;
158
 
                                                // https://sourceforge.net/p/keepass/bugs/1219/
159
 
                                                EnsureNoBom(p.StandardInput);
160
 
 
161
 
                                                p.StandardInput.Write(strStdInput);
162
 
                                                p.StandardInput.Close();
163
 
                                        }
164
 
 
165
 
                                        string strOutput = string.Empty;
166
 
                                        if(bStdOut) strOutput = p.StandardOutput.ReadToEnd();
167
 
 
168
 
                                        if((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
169
 
                                                p.WaitForExit();
170
 
                                        else if((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
171
 
                                        {
172
 
                                                Thread th = new Thread(delegate()
173
 
                                                {
174
 
                                                        try { p.WaitForExit(); }
175
 
                                                        catch(Exception) { Debug.Assert(false); }
176
 
                                                });
177
 
                                                th.Start();
178
 
                                        }
179
 
 
180
 
                                        return strOutput;
181
 
                                }
182
 
                                catch(Exception) { Debug.Assert(false); }
183
 
 
184
 
                                return null;
185
 
                        };
186
 
 
187
 
                        if((f & AppRunFlags.DoEvents) != AppRunFlags.None)
188
 
                        {
189
 
                                List<Form> lDisabledForms = new List<Form>();
190
 
                                if((f & AppRunFlags.DisableForms) != AppRunFlags.None)
191
 
                                {
192
 
                                        foreach(Form form in Application.OpenForms)
193
 
                                        {
194
 
                                                if(!form.Enabled) continue;
195
 
 
196
 
                                                lDisabledForms.Add(form);
197
 
                                                form.Enabled = false;
198
 
                                        }
199
 
                                }
200
 
 
201
 
                                IAsyncResult ar = fnRun.BeginInvoke(null, null);
202
 
 
203
 
                                while(!ar.AsyncWaitHandle.WaitOne(0))
204
 
                                {
205
 
                                        Application.DoEvents();
206
 
                                        Thread.Sleep(2);
207
 
                                }
208
 
 
209
 
                                string strRet = fnRun.EndInvoke(ar);
210
 
 
211
 
                                for(int i = lDisabledForms.Count - 1; i >= 0; --i)
212
 
                                        lDisabledForms[i].Enabled = true;
213
 
 
214
 
                                return strRet;
215
 
                        }
216
 
 
217
 
                        return fnRun();
218
 
                }
219
 
 
220
 
                private static void EnsureNoBom(StreamWriter sw)
221
 
                {
222
 
                        if(sw == null) { Debug.Assert(false); return; }
223
 
                        if(!NativeLib.IsUnix()) return;
224
 
 
225
 
                        try
226
 
                        {
227
 
                                Encoding enc = sw.Encoding;
228
 
                                if(enc == null) { Debug.Assert(false); return; }
229
 
                                byte[] pbBom = enc.GetPreamble();
230
 
                                if((pbBom == null) || (pbBom.Length == 0)) return;
231
 
 
232
 
                                FieldInfo fi = typeof(StreamWriter).GetField("preamble_done",
233
 
                                        BindingFlags.Instance | BindingFlags.NonPublic);
234
 
                                if(fi != null) fi.SetValue(sw, true);
235
 
                        }
236
 
                        catch(Exception) { Debug.Assert(false); }
237
 
                }
238
 
#endif
239
 
 
240
 
                /// <summary>
241
 
                /// Transform a key.
242
 
                /// </summary>
243
 
                /// <param name="pBuf256">Source and destination buffer.</param>
244
 
                /// <param name="pKey256">Key to use in the transformation.</param>
245
 
                /// <param name="uRounds">Number of transformation rounds.</param>
246
 
                /// <returns>Returns <c>true</c>, if the key was transformed successfully.</returns>
247
 
                public static bool TransformKey256(byte[] pBuf256, byte[] pKey256,
248
 
                        ulong uRounds)
249
 
                {
250
 
                        if(m_bAllowNative == false) return false;
251
 
 
252
 
                        KeyValuePair<IntPtr, IntPtr> kvp = PrepareArrays256(pBuf256, pKey256);
253
 
                        bool bResult = false;
254
 
 
255
 
                        try
256
 
                        {
257
 
                                bResult = NativeMethods.TransformKey(kvp.Key, kvp.Value, uRounds);
258
 
                        }
259
 
                        catch(Exception) { bResult = false; }
260
 
 
261
 
                        if(bResult) GetBuffers256(kvp, pBuf256, pKey256);
262
 
 
263
 
                        NativeLib.FreeArrays(kvp);
264
 
                        return bResult;
265
 
                }
266
 
 
267
 
                /// <summary>
268
 
                /// Benchmark key transformation.
269
 
                /// </summary>
270
 
                /// <param name="uTimeMs">Number of seconds to perform the benchmark.</param>
271
 
                /// <param name="puRounds">Number of transformations done.</param>
272
 
                /// <returns>Returns <c>true</c>, if the benchmark was successful.</returns>
273
 
                public static bool TransformKeyBenchmark256(uint uTimeMs, out ulong puRounds)
274
 
                {
275
 
                        puRounds = 0;
276
 
 
277
 
                        if(m_bAllowNative == false) return false;
278
 
 
279
 
                        try { puRounds = NativeMethods.TransformKeyBenchmark(uTimeMs); }
280
 
                        catch(Exception) { return false; }
281
 
 
282
 
                        return true;
283
 
                }
284
 
 
285
 
                private static KeyValuePair<IntPtr, IntPtr> PrepareArrays256(byte[] pBuf256,
286
 
                        byte[] pKey256)
287
 
                {
288
 
                        Debug.Assert((pBuf256 != null) && (pBuf256.Length == 32));
289
 
                        if(pBuf256 == null) throw new ArgumentNullException("pBuf256");
290
 
                        if(pBuf256.Length != 32) throw new ArgumentException();
291
 
 
292
 
                        Debug.Assert((pKey256 != null) && (pKey256.Length == 32));
293
 
                        if(pKey256 == null) throw new ArgumentNullException("pKey256");
294
 
                        if(pKey256.Length != 32) throw new ArgumentException();
295
 
 
296
 
                        IntPtr hBuf = Marshal.AllocHGlobal(pBuf256.Length);
297
 
                        Marshal.Copy(pBuf256, 0, hBuf, pBuf256.Length);
298
 
 
299
 
                        IntPtr hKey = Marshal.AllocHGlobal(pKey256.Length);
300
 
                        Marshal.Copy(pKey256, 0, hKey, pKey256.Length);
301
 
 
302
 
                        return new KeyValuePair<IntPtr, IntPtr>(hBuf, hKey);
303
 
                }
304
 
 
305
 
                private static void GetBuffers256(KeyValuePair<IntPtr, IntPtr> kvpSource,
306
 
                        byte[] pbDestBuf, byte[] pbDestKey)
307
 
                {
308
 
                        if(kvpSource.Key != IntPtr.Zero)
309
 
                                Marshal.Copy(kvpSource.Key, pbDestBuf, 0, pbDestBuf.Length);
310
 
 
311
 
                        if(kvpSource.Value != IntPtr.Zero)
312
 
                                Marshal.Copy(kvpSource.Value, pbDestKey, 0, pbDestKey.Length);
313
 
                }
314
 
 
315
 
                private static void FreeArrays(KeyValuePair<IntPtr, IntPtr> kvpPointers)
316
 
                {
317
 
                        if(kvpPointers.Key != IntPtr.Zero)
318
 
                                Marshal.FreeHGlobal(kvpPointers.Key);
319
 
 
320
 
                        if(kvpPointers.Value != IntPtr.Zero)
321
 
                                Marshal.FreeHGlobal(kvpPointers.Value);
322
 
                }
323
 
        }
324
 
}