forked from sirAndros/KeePassWinHello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinHelloProvider.cs
225 lines (194 loc) · 6.97 KB
/
WinHelloProvider.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using KeePassLib.Keys;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePassWinHello
{
public class WinHelloProvider : KeyProvider
{
public const string CfgAutoPrompt = KeePassWinHelloExt.ShortProductName + "_AutoPrompt";
public const string CfgValidPeriod = KeePassWinHelloExt.ShortProductName + "_ValidPeriod";
public const ulong VALID_UNLIMITED = 0;
public const ulong VALID_1MINUTE = 60;
public const ulong VALID_5MINUTES = VALID_1MINUTE * 5;
public const ulong VALID_10MINUTES = VALID_5MINUTES * 2;
public const ulong VALID_15MINUTES = VALID_5MINUTES * 3;
public const ulong VALID_30MINUTES = VALID_15MINUTES * 2;
public const ulong VALID_1HOUR = VALID_30MINUTES * 2;
public const ulong VALID_2HOURS = VALID_1HOUR * 2;
public const ulong VALID_6HOURS = VALID_2HOURS * 3;
public const ulong VALID_12HOURS = VALID_6HOURS * 2;
public const ulong VALID_1DAY = VALID_12HOURS * 2;
public const ulong VALID_7DAYS = VALID_1DAY * 7;
public const ulong VALID_MONTH = VALID_1DAY * 30;
public const ulong VALID_DEFAULT = VALID_1DAY;
private class WinHelloData
{
public DateTime ValidUntil;
public ProtectedBinary ComposedKey;
public bool IsValid()
{
return ValidUntil >= DateTime.Now;
}
}
/// <summary>Maps database paths to cached keys</summary>
private readonly Dictionary<string, WinHelloData> _unlockCache;
public override string Name
{
get { return KeePassWinHelloExt.ShortProductName; }
}
public override bool DirectKey
{
get { return true; }
}
public override bool SecureDesktopCompatible
{
get { return false; }
}
public WinHelloProvider()
{
_unlockCache = new Dictionary<string, WinHelloData>();
}
public void CacheKeyForDB(string databasePath, CompositeKey keys)
{
Contract.Requires(!string.IsNullOrEmpty(databasePath));
Contract.Requires(keys != null);
Contract.Requires(WinHello.IsAvailable());
var validPeriod = KeePassWinHelloExt.Host.CustomConfig.GetULong(CfgValidPeriod, VALID_DEFAULT);
lock (_unlockCache)
{
_unlockCache[databasePath] = new WinHelloData
{
ValidUntil = validPeriod == VALID_UNLIMITED ? DateTime.MaxValue : DateTime.Now.AddSeconds(validPeriod),
ComposedKey = Encrypt(keys),
};
}
}
public void RemoveCachedKey(string databasePath)
{
lock (_unlockCache)
_unlockCache.Remove(databasePath);
}
private bool TryGetCachedKey(string databasePath, out WinHelloData data)
{
lock (_unlockCache)
return _unlockCache.TryGetValue(databasePath, out data);
}
public bool IsCachedKey(string databasePath)
{
lock (_unlockCache)
return _unlockCache.ContainsKey(databasePath);
}
public void ClearExpiredKeys()
{
lock (_unlockCache)
foreach (var key in _unlockCache.Where(kv => !kv.Value.IsValid()).Select(kv => kv.Key).ToList())
_unlockCache.Remove(key);
}
public void ClearCache()
{
lock (_unlockCache)
_unlockCache.Clear();
}
public override byte[] GetKey(KeyProviderQueryContext ctx)
{
if (ctx.CreatingNewKey)
{
MessageService.ShowWarning("Can't use this plugin to create new keys.");
return null;
}
if (ctx.IsOnSecureDesktop)
{
MessageService.ShowWarning("Can't use WinHello on secure desktop.");
return null;
}
WinHelloData data;
if (!TryGetCachedKey(ctx.DatabasePath, out data) || !data.IsValid())
{
MessageService.ShowWarning("WinHello is not available for this database.");
return null;
}
try
{
RemoveCachedKey(ctx.DatabasePath);
var result = Decrypt(data.ComposedKey);
return result.ReadData();
}
catch (Exception ex)
{
Debug.Fail(ex.ToString());
return null;
}
}
private ProtectedBinary CombineKeys(CompositeKey key)
{
var dataList = new List<byte[]>();
int dataLength = 0;
foreach (var pKey in key.UserKeys)
{
var b = pKey.KeyData;
if (b != null)
{
var keyData = b.ReadData();
dataList.Add(keyData);
dataLength += keyData.Length;
}
}
var allData = new byte[dataLength];
int p = 0;
foreach (var pbData in dataList)
{
Array.Copy(pbData, 0, allData, p, pbData.Length);
p += pbData.Length;
MemUtil.ZeroByteArray(pbData);
}
var pb = new ProtectedBinary(true, allData);
MemUtil.ZeroByteArray(allData);
return pb;
}
private ProtectedBinary Encrypt(CompositeKey composedKey)
{
return Encrypt(CombineKeys(composedKey));
}
private ProtectedBinary Encrypt(ProtectedBinary composedKey)
{
WinHello winHello = new WinHello();
byte[] data = composedKey.ReadData();
byte[] encryptedData;
try
{
encryptedData = winHello.Encrypt(data);
}
finally
{
MemUtil.ZeroByteArray(data);
}
var result = new ProtectedBinary(true, encryptedData);
MemUtil.ZeroByteArray(encryptedData);
return result;
}
private ProtectedBinary Decrypt(ProtectedBinary encryptedKey)
{
WinHello winHello = new WinHello();
winHello.Message = "Authentication to access KeePass database";
winHello.ParentHandle = KeePassWinHelloExt.KeyPromptForm.Handle;
byte[] encryptedData = encryptedKey.ReadData();
byte[] data;
try
{
data = winHello.PromptToDecrypt(encryptedData);
}
finally
{
MemUtil.ZeroByteArray(encryptedData);
}
var result = new ProtectedBinary(true, data);
MemUtil.ZeroByteArray(data);
return result;
}
}
}