File: PwGeneratorMenu.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 (263 lines) | stat: -rw-r--r-- 7,814 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
/*
  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.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using KeePass.App.Configuration;
using KeePass.Forms;
using KeePass.Resources;
using KeePass.Util;
using KeePass.Util.MultipleValues;

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

namespace KeePass.UI
{
	internal sealed class PwGeneratorMenu : IDisposable
	{
		private Button m_btnHost = null;
		private readonly object m_oTarget;
		private readonly GFunc<PwEntry> m_fGetContextEntry;
		private readonly PwDatabase m_pdContext;
		private readonly bool m_bMultipleValues;

		private Image m_imgHost = null;
		private CustomContextMenuStripEx m_ctx = null;

		private readonly string GenDeriveFromPrevious = "(" +
			KPRes.GenPwBasedOnPrevious + ")";
		private readonly string GenAuto = "(" +
			KPRes.AutoGeneratedPasswordSettings + ")";

		public PwGeneratorMenu(Button btnHost, ToolTip tt, object oTarget,
			GFunc<PwEntry> fGetContextEntry, PwDatabase pdContext, bool bMultipleValues)
		{
			if(btnHost == null) { Debug.Assert(false); return; }
			Debug.Assert(btnHost.Image == null);
			Debug.Assert(btnHost.Text == string.Empty);
			Debug.Assert(btnHost.TextImageRelation == TextImageRelation.Overlay);
			if(oTarget == null) { Debug.Assert(false); return; }
			Debug.Assert((oTarget is PwInputControlGroup) || (oTarget is TextBoxBase));

			m_imgHost = UIUtil.CreateDropDownImage(Properties.Resources.B16x16_Key_New);
			Image imgSc = UIUtil.SetButtonImage(btnHost, m_imgHost, true);
			UIUtil.OverwriteIfNotEqual(ref m_imgHost, imgSc);

			UIUtil.SetToolTip(tt, btnHost, KPRes.GeneratePassword, true);

			btnHost.Click += this.OnHostBtnClick;

			m_btnHost = btnHost;
			m_oTarget = oTarget;
			m_fGetContextEntry = fGetContextEntry;
			m_pdContext = pdContext;
			m_bMultipleValues = bMultipleValues;
		}

		public void Dispose()
		{
			DisposeContextMenu();

			if(m_btnHost != null)
			{
				UIUtil.DisposeButtonImage(m_btnHost, ref m_imgHost);

				m_btnHost.Click -= this.OnHostBtnClick;
				m_btnHost = null;
			}
			else { Debug.Assert(false); }
		}

		private bool IsMultipleValues(ProtectedString ps)
		{
			if(!m_bMultipleValues) return false;

			if(ps == null) { Debug.Assert(false); return false; }
			return ps.Equals(MultipleValuesEx.CueProtectedString, false);
		}

		private List<ToolStripItem> ConstructMenuItems()
		{
			List<ToolStripItem> l = new List<ToolStripItem>();
			AccessKeyManagerEx ak = new AccessKeyManagerEx();
			ProtectedString ps = GetPassword();
			bool bProfiles = Program.Config.PasswordGenerator.ProfilesEnabled;

			GAction<string, Image, EventHandler, object, bool> fAdd =
				delegate(string strText, Image img, EventHandler ehClick,
				object oTag, bool bEnabled)
			{
				string str = ak.CreateText(strText, true);

				ToolStripMenuItem tsmi = new ToolStripMenuItem(str);
				if(img != null) tsmi.Image = img;
				if(ehClick != null) tsmi.Click += ehClick;
				if(oTag != null) tsmi.Tag = oTag;
				if(!bEnabled) tsmi.Enabled = false;

				l.Add(tsmi);
			};

			fAdd(KPRes.PwGenOpen, Properties.Resources.B16x16_Key_New,
				this.OnGenOpen, null, true);

			l.Add(new ToolStripSeparator());

			fAdd(GenDeriveFromPrevious, Properties.Resources.B16x16_CompFile,
				this.OnGenDeriveFromPrevious, null, !IsMultipleValues(ps));
			fAdd(GenAuto, Properties.Resources.B16x16_FileNew,
				this.OnGenAuto, null, bProfiles);

			bool bHideBuiltIn = ((Program.Config.UI.UIFlags &
				(ulong)AceUIFlags.HideBuiltInPwGenPrfInEntryDlg) != 0);
			bool bFirst = true;

			foreach(PwProfile prf in PwGeneratorUtil.GetAllProfiles(true))
			{
				if(prf == null) { Debug.Assert(false); continue; }

				if(bHideBuiltIn && PwGeneratorUtil.IsBuiltInProfile(prf.Name))
					continue;

				if(bFirst)
				{
					l.Add(new ToolStripSeparator());
					bFirst = false;
				}

				fAdd(prf.Name, Properties.Resources.B16x16_KOrganizer,
					this.OnGenProfile, prf, bProfiles);
			}

			return l;
		}

		private void ConstructContextMenu()
		{
			DisposeContextMenu();

			m_ctx = new CustomContextMenuStripEx();
			m_ctx.SuspendLayout();
			m_ctx.Items.AddRange(ConstructMenuItems().ToArray());
			m_ctx.ResumeLayout(true);
		}

		private void DisposeContextMenu()
		{
			if(m_ctx != null) { m_ctx.Dispose(); m_ctx = null; }
		}

		private void OnHostBtnClick(object sender, EventArgs e)
		{
			if(m_btnHost == null) { Debug.Assert(false); return; }

			ConstructContextMenu();
			m_ctx.ShowEx(m_btnHost);
		}

		private ProtectedString GetPassword()
		{
			PwInputControlGroup icg = (m_oTarget as PwInputControlGroup);
			if(icg != null) return icg.GetPasswordEx();

			TextBoxBase tb = (m_oTarget as TextBoxBase);
			if(tb != null) return new ProtectedString(false, tb.Text);

			Debug.Assert(false); // Unknown target type
			return null;
		}

		private void SetPassword(ProtectedString ps)
		{
			if(ps == null) { Debug.Assert(false); return; }

			PwInputControlGroup icg = (m_oTarget as PwInputControlGroup);
			if(icg != null)
			{
				icg.SetPassword(ps, true);
				return;
			}

			TextBoxBase tb = (m_oTarget as TextBoxBase);
			if(tb != null)
			{
				tb.Text = ps.ReadString();
				return;
			}

			Debug.Assert(false); // Unknown target type
		}

		private void GenerateAndSetPassword(PwProfile prf)
		{
			if(prf == null) { Debug.Assert(false); return; }

			byte[] pbEntropy = EntropyForm.CollectEntropyIfEnabled(prf);
			PwEntry pe = ((m_fGetContextEntry != null) ? m_fGetContextEntry() : null);

			SetPassword(PwGeneratorUtil.GenerateAcceptable(prf,
				pbEntropy, pe, m_pdContext, true));
		}

		private void OnGenOpen(object sender, EventArgs e)
		{
			PwProfile prf = null;
			ProtectedString ps = (GetPassword() ?? ProtectedString.Empty);
			if(!ps.IsEmpty && !IsMultipleValues(ps))
				prf = PwProfile.DeriveFromPassword(ps);

			PwGeneratorForm pgf = new PwGeneratorForm();
			pgf.InitEx(prf, true, false);

			if(pgf.ShowDialog() == DialogResult.OK)
				GenerateAndSetPassword(pgf.SelectedProfile);

			UIUtil.DestroyForm(pgf);
		}

		private void OnGenDeriveFromPrevious(object sender, EventArgs e)
		{
			ProtectedString ps = GetPassword();
			if(ps == null) { Debug.Assert(false); return; }

			GenerateAndSetPassword(PwProfile.DeriveFromPassword(ps));
		}

		private void OnGenAuto(object sender, EventArgs e)
		{
			GenerateAndSetPassword(Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile);
		}

		private void OnGenProfile(object sender, EventArgs e)
		{
			ToolStripItem tsi = (sender as ToolStripItem);
			if(tsi == null) { Debug.Assert(false); return; }

			GenerateAndSetPassword(tsi.Tag as PwProfile);
		}
	}
}