File: SecureTextBoxEx.cs

package info (click to toggle)
keepass2 2.60%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,892 kB
  • sloc: cs: 119,878; xml: 6,087; ansic: 2,033; cpp: 738; sh: 50; makefile: 42
file content (444 lines) | stat: -rw-r--r-- 12,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2025 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.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using KeePass.Native;

using KeePassLib.Cryptography;
using KeePassLib.Cryptography.Cipher;
using KeePassLib.Cryptography.PasswordGenerator;
using KeePassLib.Delegates;
using KeePassLib.Security;
using KeePassLib.Utility;

using NativeLib = KeePassLib.Native.NativeLib;

namespace KeePass.UI
{
	// Non-sealed for plugins
	public class SecureTextBoxEx : TextBox
	{
		private uint m_uBlockTextChanged = 0;
		private bool m_bFirstGotFocus = true;

#if DEBUG
		private bool m_bInitExCalled = false;
#endif

		// With ProtectedString.Empty no incremental protection (RemoveInsert)
		[NonSerialized]
		private ProtectedString m_psText = ProtectedString.EmptyEx;
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public virtual ProtectedString TextEx
		{
			get { return m_psText; }
			set
			{
				if(value == null) { Debug.Assert(false); value = ProtectedString.EmptyEx; }
				m_psText = value.WithProtection(true); // For incremental editing

				ShowCurrentText(0, 0);
			}
		}
		// https://msdn.microsoft.com/en-us/library/53b8022e.aspx
		public virtual void ResetTextEx() { this.TextEx = ProtectedString.EmptyEx; }
		public virtual bool ShouldSerializeTextEx() { return false; }

		private static char? m_ochPasswordChar = null;
		public static char PasswordCharEx
		{
			get
			{
				if(!m_ochPasswordChar.HasValue)
				{
					// On Windows 98/ME, an ANSI character must be used as
					// password char
					m_ochPasswordChar = ((Environment.OSVersion.Platform ==
						PlatformID.Win32Windows) ? '\u00D7' : '\u25CF');
				}

				return m_ochPasswordChar.Value;
			}
		}

		private bool UseWindowsApi
		{
			get
			{
				if(NativeLib.IsUnix() || !this.IsHandleCreated) return false;
				if(GetStyle(ControlStyles.CacheText)) { Debug.Assert(false); return false; }
				return true;
			}
		}

		public SecureTextBoxEx()
		{
			if(Program.DesignMode) return;

			try
			{
				bool bSTA = (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA);
				this.AllowDrop = bSTA;
			}
			catch(Exception) { Debug.Assert(false); this.AllowDrop = false; }

			this.UseSystemPasswordChar = true;
		}

#if DEBUG
		~SecureTextBoxEx()
		{
			if(!Program.DesignMode) { Debug.Assert(m_bInitExCalled); }
		}
#endif

		public static void InitEx(ref SecureTextBoxEx s)
		{
			if(Program.DesignMode) return;

#if DEBUG
			if(s != null) s.m_bInitExCalled = true; // Org. object
#endif
			UIUtil.PerformOverride(ref s);
#if DEBUG
			if(s != null) s.m_bInitExCalled = true; // New object
#endif
		}

		private static Dictionary<int, string> g_dPwCharStrings = null;
		internal static string GetPasswordCharString(int cch)
		{
			if(cch <= 0) { Debug.Assert(cch == 0); return string.Empty; }

			Dictionary<int, string> d = g_dPwCharStrings;
			if(d == null)
			{
				d = new Dictionary<int, string>();

				for(int i = 1; i < 40; ++i)
					d[i] = new string(SecureTextBoxEx.PasswordCharEx, i);

				g_dPwCharStrings = d;
			}

			string str;
			if(d.TryGetValue(cch, out str)) return str;

			str = new string(SecureTextBoxEx.PasswordCharEx, cch);
			d[cch] = str;
			return str;
		}

		public virtual void EnableProtection(bool bEnable)
		{
			if(bEnable == this.UseSystemPasswordChar) return;

			if(!MonoWorkarounds.IsRequired(5795))
			{
				FontUtil.SetDefaultFont(this);

				if(bEnable) FontUtil.AssignDefault(this);
				else FontUtil.AssignDefaultMono(this, true);
			}

			this.UseSystemPasswordChar = bEnable;
			ShowCurrentText(-1, -1);
		}

		private void ShowCurrentText(int nSelStart, int nSelLength)
		{
			if(nSelStart < 0) nSelStart = this.SelectionStart;
			if(nSelLength < 0) nSelLength = this.SelectionLength;

			++m_uBlockTextChanged;
			if(!this.UseSystemPasswordChar)
				this.Text = m_psText.ReadString();
			else
			{
				string str = GetPasswordCharString(m_psText.Length);
				if(!this.UseWindowsApi) this.Text = str;
				else if(NativeMethods.SetWindowText(this.Handle, str)) { Debug.Assert(this.Text == str); }
				else { Debug.Assert(false); }
			}
			--m_uBlockTextChanged;

			int nNewTextLen = this.TextLength;
			if(nSelStart < 0) { Debug.Assert(false); nSelStart = 0; }
			if(nSelStart > nNewTextLen) nSelStart = nNewTextLen; // Behind last char
			if(nSelLength < 0) { Debug.Assert(false); nSelLength = 0; }
			if((nSelStart + nSelLength) > nNewTextLen)
				nSelLength = nNewTextLen - nSelStart;
			Select(nSelStart, nSelLength);

			ClearUndo(); // Would need special undo buffer

			base.OnTextChanged(EventArgs.Empty); // Text has been set above
		}

		protected override void OnTextChanged(EventArgs e)
		{
			if(m_uBlockTextChanged != 0) return;

			if(!this.UseSystemPasswordChar)
			{
				m_psText = new ProtectedString(true, this.Text);
				base.OnTextChanged(e);
				return;
			}

			int nSelPos = this.SelectionStart;
			int nSelLen = this.SelectionLength;

			char[] vText = (!this.UseWindowsApi ? this.Text.ToCharArray() :
				NativeMethods.GetWindowTextV(this.Handle, true));
			Debug.Assert(MemUtil.ArrayHelperExOfChar.Equals(vText, this.Text.ToCharArray()));

			char chPasswordChar = SecureTextBoxEx.PasswordCharEx;
			int iLeft = -1, iRight = 0;
			for(int i = 0; i < vText.Length; ++i)
			{
				if(vText[i] != chPasswordChar)
				{
					if(iLeft < 0) iLeft = i;
					iRight = i;
				}
			}

			if(iLeft < 0)
				RemoveInsert(nSelPos, vText.Length - nSelPos, vText, 0, 0);
			else
				RemoveInsert(iLeft, vText.Length - iRight - 1,
					vText, iLeft, iRight - iLeft + 1);

			MemUtil.ZeroArray<char>(vText);

			ShowCurrentText(nSelPos, nSelLen);

			// ThreadPool.QueueUserWorkItem(new WaitCallback(this.CreateDummyStrings));
			CreateDummyStrings(); // Other thread => less intertwining in memory
		}

		private void RemoveInsert(int nLeftRem, int nRightRem, char[] vInsert,
			int iInsert, int cchInsert)
		{
			Debug.Assert(nLeftRem >= 0);

			try
			{
				int cr = m_psText.Length - (nLeftRem + nRightRem);
				if(cr >= 0) m_psText = m_psText.Remove(nLeftRem, cr);
				else { Debug.Assert(false); }
				Debug.Assert(m_psText.Length == (nLeftRem + nRightRem));
			}
			catch(Exception) { Debug.Assert(false); }

			try { m_psText = m_psText.Insert(nLeftRem, vInsert, iInsert, cchInsert); }
			catch(Exception) { Debug.Assert(false); }
		}

		private readonly string[] m_vDummyStrings = new string[64];
		private readonly string m_strAlphMain = PwCharSet.UpperCase +
			PwCharSet.LowerCase + PwCharSet.Digits + GetPasswordCharString(1);
		private void CreateDummyStrings()
		{
			try
			{
#if DEBUG
				// Stopwatch sw = Stopwatch.StartNew();
#endif

				int cch = m_psText.Length;
				if(cch == 0) return;

				int cRefs = m_vDummyStrings.Length;
				string[] vRefs = new string[cRefs];
				// lock(m_vDummyStrings) {
				Array.Copy(m_vDummyStrings, vRefs, cRefs); // Read access

				bool bUnix = NativeLib.IsUnix();
				char[] v0 = GetPasswordCharString(cch).ToCharArray();
				char[] v1 = GetPasswordCharString(cch + 1).ToCharArray();
				char[] v2 = (bUnix ? GetPasswordCharString(cch + 2).ToCharArray() : null);
				char chPasswordChar = v0[0];

				byte[] pbKey = CryptoRandom.Instance.GetRandomBytes(32);
				ChaCha20Cipher c = new ChaCha20Cipher(pbKey, new byte[12], true);
				byte[] pbBuf = new byte[4];
				GFunc<uint> fR = delegate()
				{
					c.Encrypt(pbBuf, 0, 4);
					return (uint)BitConverter.ToInt32(pbBuf, 0);
				};
				GFunc<uint, uint> fRM = delegate(uint uMaxExcl)
				{
					uint uGen, uRem;
					do { uGen = fR(); uRem = uGen % uMaxExcl; }
					while((uGen - uRem) > (uint.MaxValue - (uMaxExcl - 1U)));
					return uRem;
				};
				GFunc<uint, uint, uint> fRLow = delegate(uint uRandom, uint uMaxExcl)
				{
					const uint m = 0x000FFFFF;
					Debug.Assert(uMaxExcl <= (m + 1U));
					uint uGen = uRandom & m;
					uint uRem = uGen % uMaxExcl;
					if((uGen - uRem) <= (m - (uMaxExcl - 1U))) return uRem;
					return fRM(uMaxExcl);
				};
				GFunc<uint, string, char> fRLowChar = delegate(uint uRandom, string strCS)
				{
					return strCS[(int)fRLow(uRandom, (uint)strCS.Length)];
				};

				uint cIt = (uint)m_strAlphMain.Length * 3;
				if(bUnix) cIt <<= 2;
				cIt += fRM(cIt);

				for(uint uIt = cIt; uIt != 0; --uIt)
				{
					uint r = fR();

					char[] v;
					if(v2 != null)
					{
						while(r < 0x10000000U) { r = fR(); }
						if(r < 0x60000000U) v = v0;
						else if(r < 0xB0000000U) v = v1;
						else v = v2;
					}
					else v = (((r & 0x10000000) == 0) ? v0 : v1);

					int iPos;
					if((r & 0x07000000) == 0) iPos = (int)fRM((uint)v.Length);
					else iPos = v.Length - 1;

					char ch;
					if((r & 0x00300000) != 0)
						ch = fRLowChar(r, m_strAlphMain);
					else if((r & 0x00400000) == 0)
						ch = fRLowChar(r, PwCharSet.PrintableAsciiSpecial);
					else if((r & 0x00800000) == 0)
						ch = fRLowChar(r, PwCharSet.Latin1S);
					else
						ch = (char)(fRLow(r, 0xD7FFU) + 1);

					int cRep = 1;
					if(bUnix) cRep = (((r & 0x08000000) == 0) ? 1 : 9);

					Debug.Assert(v[iPos] == chPasswordChar);
					v[iPos] = ch;

					int iRef0 = (int)r & int.MaxValue;
					for(int i = cRep; i != 0; --i)
						vRefs[(iRef0 ^ i) % cRefs] = new string(v);

					v[iPos] = chPasswordChar;
				}

				c.Dispose();
				MemUtil.ZeroByteArray(pbKey);
				MemUtil.ZeroArray<char>(v0);
				MemUtil.ZeroArray<char>(v1);
				if(v2 != null) MemUtil.ZeroArray<char>(v2);

				// lock(m_vDummyStrings) {
				Array.Copy(vRefs, m_vDummyStrings, cRefs);

#if DEBUG
				// sw.Stop();
				// Trace.WriteLine(string.Format("CreateDummyStrings: {0} ms.", sw.ElapsedMilliseconds));
				// Console.WriteLine("CreateDummyStrings: {0} ms.", sw.ElapsedMilliseconds);
#endif
			}
			catch(Exception) { Debug.Assert(false); }
		}

		protected override void OnGotFocus(EventArgs e)
		{
			base.OnGotFocus(e);

			if(m_bFirstGotFocus)
			{
				m_bFirstGotFocus = false;

				// OnGotFocus is not called when the box initially has the
				// focus; the user can select characters without triggering
				// OnGotFocus, thus we select all characters only if the
				// selection is in its original state (0, 0), otherwise
				// e.g. the selection restoration when hiding/unhiding does
				// not work the first time (because after restoring the
				// selection, we would override it here by selecting all)
				if((this.SelectionStart <= 0) && (this.SelectionLength <= 0))
					SelectAll();
			}
		}

		private static bool DragCheck(DragEventArgs e)
		{
			if(e.Data.GetDataPresent(typeof(string)))
			{
				DragDropEffects eA = e.AllowedEffect;
				if((eA & DragDropEffects.Copy) != DragDropEffects.None)
				{
					e.Effect = DragDropEffects.Copy;
					return true;
				}
				if((eA & DragDropEffects.Move) != DragDropEffects.None)
				{
					e.Effect = DragDropEffects.Move;
					return true;
				}
			}

			e.Effect = DragDropEffects.None;
			return false;
		}

		protected override void OnDragEnter(DragEventArgs drgevent)
		{
			if(!DragCheck(drgevent)) base.OnDragEnter(drgevent);
		}

		protected override void OnDragOver(DragEventArgs drgevent)
		{
			if(!DragCheck(drgevent)) base.OnDragOver(drgevent);
		}

		protected override void OnDragDrop(DragEventArgs drgevent)
		{
			if(drgevent.Data.GetDataPresent(typeof(string)))
			{
				string str = (drgevent.Data.GetData(typeof(string)) as string);
				if(str == null) { Debug.Assert(false); return; }
				if(str.Length == 0) return;

				Paste(str);
			}
			else base.OnDragDrop(drgevent);
		}
	}
}