File: KeyCreationSimpleForm.cs

package info (click to toggle)
keepass2-plugin-keepassrpc 2.0.2%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: cs: 29,001; makefile: 14
file content (291 lines) | stat: -rw-r--r-- 7,608 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
/*
  Modified version of KeyCreationForm.cs from...

  KeePass Password Safe - The Open-Source Password Manager
  KeePass is Copyright (C) 2003-2009 Dominik Reichl <dominik.reichl@t-online.de>
*/

using System;
using System.Diagnostics;
using System.Windows.Forms;
using KeePass;
using KeePass.App;
using KeePass.Forms;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib.Cryptography;
using KeePassLib.Keys;
using KeePassLib.Serialization;
using KeePassLib.Utility;

namespace KeePassRPC.Forms
{
	public partial class KeyCreationSimpleForm : Form
	{
		private CompositeKey m_pKey;
		private bool m_bCreatingNew;
		private IOConnectionInfo m_ioInfo = new IOConnectionInfo();
        private string _databaseName;

		private SecureEdit m_secPassword = new SecureEdit();
		private SecureEdit m_secRepeat = new SecureEdit();

		public CompositeKey CompositeKey
		{
			get
			{
				Debug.Assert(m_pKey != null);
				return m_pKey;
			}
		}

        public string DatabaseName
        {
            get
            {
                return _databaseName;
            }
        }

		public KeyCreationSimpleForm()
		{
			InitializeComponent();
			//Program.Translation.ApplyTo(this);
		}

		public void InitEx(IOConnectionInfo ioInfo, bool bCreatingNew)
		{
			if(ioInfo != null) m_ioInfo = ioInfo;

			m_bCreatingNew = bCreatingNew;
		}

		private void OnFormLoad(object sender, EventArgs e)
		{
			GlobalWindowManager.AddWindow(this);

			//this.Icon = Properties.Resources.KeePass;
			Text = KPRes.CreateMasterKey;

			//m_ttRect.SetToolTip(m_cbHidePassword, KPRes.TogglePasswordAsterisks);

			if(!m_bCreatingNew)
				m_lblIntro.Text = KPRes.ChangeMasterKeyIntroShort;

			m_secPassword.Attach(m_tbPassword, ProcessTextChangedPassword, true);
			m_secRepeat.Attach(m_tbRepeatPassword, null, true);
			m_cbHidePassword.Checked = true;

			ProcessTextChangedPassword(sender, e); // Update quality estimation

			CustomizeForScreenReader();
			EnableUserControls();
		}

		private void CustomizeForScreenReader()
		{
			if(!Program.Config.UI.OptimizeForScreenReader) return;

			m_cbHidePassword.Text = KPRes.HideUsingAsterisks;
		}

		private void CleanUpEx()
		{
			m_secPassword.Detach();
			m_secRepeat.Detach();
		}

		private bool CreateCompositeKey()
		{
			m_pKey = new CompositeKey();

			if(m_secPassword.ContentsEqualTo(m_secRepeat) == false)
			{
				MessageService.ShowWarning(KPRes.PasswordRepeatFailed);
				return false;
			}

			if(m_secPassword.TextLength == 0)
			{
				if(!MessageService.AskYesNo(KPRes.EmptyMasterPw +
					MessageService.NewParagraph + KPRes.EmptyMasterPwHint +
					MessageService.NewParagraph + KPRes.EmptyMasterPwQuestion,
					null, false))
				{
					return false;
				}
			}

			uint uMinLen = Program.Config.Security.MasterPassword.MinimumLength;
			if(m_secPassword.TextLength < uMinLen)
			{
				string strML = KPRes.MasterPasswordMinLengthFailed;
				strML = strML.Replace(@"{PARAM}", uMinLen.ToString());
				MessageService.ShowWarning(strML);
				return false;
			}

			byte[] pb = m_secPassword.ToUtf8();

			uint uMinQual = Program.Config.Security.MasterPassword.MinimumQuality;
			if(QualityEstimation.EstimatePasswordBits(pb) < uMinQual)
			{
				string strMQ = KPRes.MasterPasswordMinQualityFailed;
				strMQ = strMQ.Replace(@"{PARAM}", uMinQual.ToString());
				MessageService.ShowWarning(strMQ);
				Array.Clear(pb, 0, pb.Length);
				return false;
			}

			string strValRes = Program.KeyValidatorPool.Validate(pb,
				KeyValidationType.MasterPassword);
			if(strValRes != null)
			{
				MessageService.ShowWarning(strValRes);
				Array.Clear(pb, 0, pb.Length);
				return false;
			}

			m_pKey.AddUserKey(new KcpPassword(pb));
			Array.Clear(pb, 0, pb.Length);

			return true;
		}

		private void EnableUserControls()
		{
			m_tbPassword.Enabled = m_tbRepeatPassword.Enabled = m_cbHidePassword.Enabled =
				m_lblRepeatPassword.Enabled = m_lblQualityBits.Enabled =
				m_lblEstimatedQuality.Enabled = true;


			SetHidePassword(m_cbHidePassword.Checked, false);

		}

		private void SetHidePassword(bool bHide, bool bUpdateCheckBox)
		{
			if(bUpdateCheckBox) m_cbHidePassword.Checked = bHide;

			m_secPassword.EnableProtection(bHide);
			m_secRepeat.EnableProtection(bHide);
		}

		private void OnCheckedPassword(object sender, EventArgs e)
		{
			EnableUserControls();

			m_tbPassword.Focus();
		}

		private void OnCheckedKeyFile(object sender, EventArgs e)
		{
			EnableUserControls();
		}

		private void OnCheckedHidePassword(object sender, EventArgs e)
		{
			SetHidePassword(m_cbHidePassword.Checked, false);
			m_tbPassword.Focus();
		}

		private void OnBtnOK(object sender, EventArgs e)
		{
			if(!CreateCompositeKey()) DialogResult = DialogResult.None;
            if (!string.IsNullOrEmpty(dbNameTextBox.Text))
                _databaseName = dbNameTextBox.Text;
		}

		private void OnBtnCancel(object sender, EventArgs e)
		{
			m_pKey = null;
		}

		private void ProcessTextChangedPassword(object sender, EventArgs e)
		{
			byte[] pbUTF8 = m_secPassword.ToUtf8();
			uint uBits = QualityEstimation.EstimatePasswordBits(pbUTF8);
			MemUtil.ZeroByteArray(pbUTF8);

			m_lblQualityBits.Text = uBits + " " + KPRes.Bits;
			int iPos = (int)((100 * uBits) / (256 / 2));
			if(iPos < 0) iPos = 0; else if(iPos > 100) iPos = 100;
			m_pbPasswordQuality.Value = iPos;
		}

		private void OnClickKeyFileCreate(object sender, EventArgs e)
		{
            SaveFileDialogEx sfd = UIUtil.CreateSaveFileDialog(KPRes.KeyFileCreate,
                UrlUtil.StripExtension(UrlUtil.GetFileName(m_ioInfo.Path)) + "." +
                AppDefs.FileExtension.KeyFile, UIUtil.CreateFileTypeFilter("key",
                    KPRes.KeyFiles, true), 1, "key", null);

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                using (EntropyForm dlg = new EntropyForm())
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        byte[] pbAdditionalEntropy = dlg.GeneratedEntropy;

                        try
                        {
                            KcpKeyFile.Create(sfd.FileName, pbAdditionalEntropy);
                        }
                        catch (Exception exKC)
                        {
                            MessageService.ShowWarning(exKC);
                        }
                    }
                }

                EnableUserControls();
            }
		}

		private void OnClickKeyFileBrowse(object sender, EventArgs e)
		{
            OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog(KPRes.KeyFileUseExisting,
                UIUtil.CreateFileTypeFilter("key", KPRes.KeyFiles, true), 2, null,
                false, null);

		    if (ofd.ShowDialog() == DialogResult.OK)
		    {
		        string str = ofd.FileName;
		    }

		    EnableUserControls();
		}

		private void OnWinUserCheckedChanged(object sender, EventArgs e)
		{
			EnableUserControls();
		}

		private void OnFormClosed(object sender, FormClosedEventArgs e)
		{
			GlobalWindowManager.RemoveWindow(this);
		}

		private void OnBtnHelp(object sender, EventArgs e)
		{
			AppHelp.ShowHelp(AppDefs.HelpTopics.KeySources, null);
		}

		private void OnKeyFileSelectedIndexChanged(object sender, EventArgs e)
		{
			EnableUserControls();
		}

		private void OnFormClosing(object sender, FormClosingEventArgs e)
		{
			CleanUpEx();
		}

        private void button1_Click(object sender, EventArgs e)
        {
            //TODO2: can we securly pass the user's existing master password / CompositeKey to the advanced key creation form?
        }

	}
}