-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathKeePassQuickUnlockExt.cs
260 lines (218 loc) · 7.06 KB
/
KeePassQuickUnlockExt.cs
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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;
using STimers = System.Timers;
namespace KeePassQuickUnlock
{
public class KeePassQuickUnlockExt : Plugin
{
private static IPluginHost host = null;
private static QuickUnlockProvider provider = null;
private STimers.Timer timer;
public const string ShortProductName = "QuickUnlock";
public static IPluginHost Host
{
get { return host; }
}
public override Image SmallIcon
{
get { return Properties.Resources.B16x16_TimeLock; }
}
public override string UpdateUrl
{
get { return "https://github.com/JanisEst/KeePassQuickUnlock/raw/master/keepass.version"; }
}
public static QuickUnlockProvider Provider
{
get { return provider; }
}
public override bool Initialize(IPluginHost _host)
{
if (host != null) { Debug.Assert(false); Terminate(); }
if (_host == null) { return false; }
//Debugger.Launch();
host = _host;
provider = new QuickUnlockProvider();
host.KeyProviderPool.Add(provider);
host.MainWindow.FileClosingPre += FileClosingPreHandler;
GlobalWindowManager.WindowAdded += WindowAddedHandler;
timer = new STimers.Timer(1000);
timer.Elapsed += ElapsedHandler;
timer.Start();
return true;
}
public override void Terminate()
{
if (host == null) { return; }
timer.Stop();
timer.Elapsed -= ElapsedHandler;
timer = null;
GlobalWindowManager.WindowAdded -= WindowAddedHandler;
host.MainWindow.FileClosingPre -= FileClosingPreHandler;
host.KeyProviderPool.Remove(provider);
host = null;
}
/// <summary>
/// If the timer elapsed clear the expiered keys.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ElapsedHandler(object sender, STimers.ElapsedEventArgs e)
{
provider.ClearExpieredKeys();
}
/// <summary>
/// Gets the masterkey before the database is closed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileClosingPreHandler(object sender, FileClosingEventArgs e)
{
if (e == null) { Debug.Assert(false); return; }
if (e.Cancel) { return; }
if (e.Database != null && e.Database.MasterKey != null)
{
var rootGroup = e.Database.RootGroup;
if (rootGroup != null)
{
// Check if a QuickUnlock entry is available.
var entry = rootGroup.GetEntries(true).Where(en => en.Strings.GetSafe(PwDefs.TitleField).ReadString() == ShortProductName).FirstOrDefault();
if (entry != null)
{
var password = entry.Strings.GetSafe(PwDefs.PasswordField);
if (password.IsEmpty == false)
{
provider.AddCachedKey(e.Database.IOConnectionInfo.Path, password, e.Database.MasterKey);
return;
}
}
}
// Check if PartOfPassword is enabled...
if (Host.CustomConfig.GetEnum(QuickUnlockProvider.CfgMode, Mode.Entry) == Mode.EntryOrPartOf)
{
// ...and there is a password available.
var passwordKey = e.Database.MasterKey.UserKeys.Where(k => k is KcpPassword).FirstOrDefault() as KcpPassword;
if (passwordKey != null)
{
var length = (int)Host.CustomConfig.GetLong(QuickUnlockProvider.CfgPartOfLength, QuickUnlockProvider.MinimumPartOfLength);
if (passwordKey.Password.Length >= length)
{
var origin = Host.CustomConfig.GetEnum(QuickUnlockProvider.CfgPartOfOrigin, PartOfOrigin.Default);
var chars = passwordKey.Password.GetChars();
var startIndex = 0;
if (origin == PartOfOrigin.End)
{
startIndex = chars.Length - length;
}
provider.AddCachedKey(e.Database.IOConnectionInfo.Path, chars.ToProtectedString(startIndex, length), e.Database.MasterKey);
CharUtils.ZeroCharArray(chars);
return;
}
}
}
// If no key is set, remove possible cached key.
provider.RemoveCachedKey(e.Database.IOConnectionInfo.Path);
}
}
/// <summary>
/// Used to modify other form when they load.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WindowAddedHandler(object sender, GwmWindowEventArgs e)
{
var keyPromptForm = e.Form as KeyPromptForm;
if (keyPromptForm != null)
{
keyPromptForm.Shown += delegate (object sender2, EventArgs e2)
{
// Warning: If one of the private fields get renamed this method will fail!
var m_cmbKeyFile = keyPromptForm.Controls.Find("m_cmbKeyFile", false).FirstOrDefault() as ComboBox;
if (m_cmbKeyFile != null)
{
var fieldInfo = keyPromptForm.GetType().GetField("m_ioInfo", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo != null)
{
var ioInfo = fieldInfo.GetValue(keyPromptForm) as IOConnectionInfo;
if (ioInfo != null)
{
if (provider.IsCachedKey(ioInfo.Path))
{
var index = m_cmbKeyFile.Items.IndexOf(ShortProductName);
if (index != -1)
{
m_cmbKeyFile.SelectedIndex = index;
var m_cbPassword = keyPromptForm.Controls.Find("m_cbPassword", false).FirstOrDefault() as CheckBox;
if (m_cbPassword != null)
{
UIUtil.SetChecked(m_cbPassword, false);
}
// If AutoPrompt is enabled click the Ok button.
if (Host.CustomConfig.GetBool(QuickUnlockProvider.CfgAutoPrompt, true))
{
var m_btnOK = keyPromptForm.Controls.Find("m_btnOK", false).FirstOrDefault() as Button;
if (m_btnOK != null)
{
m_btnOK.PerformClick();
}
}
return;
}
}
}
}
// If KeePass autoselected QuickUnlock but there isn't a key available just unselect it.
if (m_cmbKeyFile.Text == ShortProductName)
{
var m_cbKeyFile = keyPromptForm.Controls.Find("m_cbKeyFile", false).FirstOrDefault() as CheckBox;
if (m_cbKeyFile != null)
{
UIUtil.SetChecked(m_cbKeyFile, false);
}
}
}
};
}
var optionsForm = e.Form as OptionsForm;
if (optionsForm != null)
{
optionsForm.Shown += delegate (object sender2, EventArgs e2)
{
try
{
// Add the QuickUnlock options tab.
var m_tabMain = optionsForm.Controls.Find("m_tabMain", true).FirstOrDefault() as TabControl;
if (m_tabMain != null)
{
if (m_tabMain.ImageList == null)
{
m_tabMain.ImageList = new ImageList();
}
var imageIndex = m_tabMain.ImageList.Images.Add(Properties.Resources.B16x16_TimeLock, Color.Transparent);
var newTab = new TabPage(ShortProductName);
newTab.UseVisualStyleBackColor = true;
newTab.ImageIndex = imageIndex;
var optionsPanel = new OptionsPanel(this);
newTab.Controls.Add(optionsPanel);
optionsPanel.Dock = DockStyle.Fill;
m_tabMain.TabPages.Add(newTab);
}
}
catch (Exception ex)
{
Debug.Fail(ex.ToString());
}
};
}
}
}
}