forked from winauth/winauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangePasswordForm.cs
More file actions
512 lines (465 loc) · 15.3 KB
/
Copy pathChangePasswordForm.cs
File metadata and controls
512 lines (465 loc) · 15.3 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* Copyright (C) 2013 Colin Mackie.
* This software is distributed under the terms of the GNU General Public License.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
#if NETFX_4
using System.Threading.Tasks;
#endif
using System.Windows.Forms;
using MetroFramework.Controls;
using WinAuth.Resources;
namespace WinAuth
{
/// <summary>
/// Form for setting the password and encryption for the current authenticators
/// </summary>
public partial class ChangePasswordForm : WinAuth.ResourceForm
{
/// <summary>
/// Used to show a filled password box
/// </summary>
private const string EXISTING_PASSWORD = "******";
/// <summary>
/// Number of iterations of PBKDF2 for our yubikey phrase
/// </summary>
private const int YUBIKEY_PBKDF2_ITERATIONS = 2048;
/// <summary>
/// Size of Yubikey secret key
/// </summary>
private const int YUBIKEY_PBKDF2_KEYSIZE = 20;
/// <summary>
/// Create the form
/// </summary>
public ChangePasswordForm()
{
InitializeComponent();
}
/// <summary>
/// Current and new password type
/// </summary>
public Authenticator.PasswordTypes PasswordType { get; set; }
/// <summary>
/// Current and new password
/// </summary>
public string Password { get; set; }
/// <summary>
/// If have a current password
/// </summary>
public bool HasPassword { get; set; }
/// <summary>
/// List of seedwords
/// </summary>
private List<string> _seedWords = new List<string>();
/// <summary>
/// Chosen slot for yubikey
/// </summary>
private int YubikeySlot { get; set; }
/// <summary>
/// Current YubiKey info
/// </summary>
public YubiKey Yubikey {get; set;}
/// <summary>
/// Load the form and pretick checkboxes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChangePasswordForm_Load(object sender, EventArgs e)
{
if ((PasswordType & Authenticator.PasswordTypes.Machine) != 0 || (PasswordType & Authenticator.PasswordTypes.User) != 0)
{
machineCheckbox.Checked = true;
}
if ((PasswordType & Authenticator.PasswordTypes.User) != 0)
{
userCheckbox.Checked = true;
}
userCheckbox.Enabled = machineCheckbox.Checked;
if ((PasswordType & Authenticator.PasswordTypes.Explicit) != 0)
{
passwordCheckbox.Checked = true;
if (HasPassword == true)
{
passwordField.Text = EXISTING_PASSWORD;
verifyField.Text = EXISTING_PASSWORD;
}
}
yubiPanelConfigure.Visible = false;
yubiConfigureIntroLabel.Visible = false;
yubiPanelIntro.Visible = true;
yubiPanelExists.Visible = false;
if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot1) != 0 || (PasswordType & Authenticator.PasswordTypes.YubiKeySlot2) != 0)
{
yubiPanelIntro.Visible = false;
yubiPanelExists.Visible = true;
yubiPanelExists.Size = yubiPanelIntro.Size;
yubiPanelExists.Location = yubiPanelIntro.Location;
yubikeyBox.Checked = true;
if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot1) != 0)
{
YubikeySlot = 1;
}
else if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot2) != 0)
{
YubikeySlot = 2;
}
}
}
/// <summary>
/// Form has been shown
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChangePasswordForm_Shown(object sender, EventArgs e)
{
// Buf in MetroFrame where focus is not set correcty during Load, so we do it here
if (passwordField.Enabled == true)
{
passwordField.Focus();
}
}
/// <summary>
/// Machine encryption is ticked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void machineCheckbox_CheckedChanged(object sender, EventArgs e)
{
if (machineCheckbox.Checked == false)
{
userCheckbox.Checked = false;
}
userCheckbox.Enabled = machineCheckbox.Checked;
}
/// <summary>
/// Toggle the Yubikey
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void yubikeyBox_CheckedChanged(object sender, EventArgs e)
{
if (yubikeyBox.Checked == true)
{
if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot1) != 0 || (PasswordType & Authenticator.PasswordTypes.YubiKeySlot2) != 0)
{
yubiPanelIntro.Enabled = false;
yubiPanelIntro.Visible = false;
yubiPanelConfigure.Visible = false;
yubiConfigureIntroLabel.Visible = false;
yubiPanelExists.Visible = true;
yubiPanelExists.Location = yubiPanelIntro.Location;
yubiPanelExists.Size = yubiPanelIntro.Size;
return;
}
yubikeyStatusLabel.Text = "Initialising YubiKey...";
yubikeyStatusLabel.Visible = true;
#if NETFX_4
Task.Factory.StartNew(() =>
{
#endif
if (this.Yubikey == null)
{
this.Yubikey = YubiKey.CreateInstance();
}
#if NETFX_4
}).ContinueWith((task) =>
{
#endif
if (string.IsNullOrEmpty(this.Yubikey.Info.Error) == false)
{
yubikeyStatusLabel.Text = this.Yubikey.Info.Error;
yubikeyBox.Checked = false;
this.Yubikey = null;
}
else if (this.Yubikey.Info.Status.VersionMajor == 0)
{
yubikeyStatusLabel.Text = "Please insert your YubiKey";
yubikeyBox.Checked = false;
this.Yubikey = null;
}
else
{
yubikeyStatusLabel.Text = string.Format("YubiKey {0}.{1}.{2}{3}",
this.Yubikey.Info.Status.VersionMajor,
this.Yubikey.Info.Status.VersionMinor,
this.Yubikey.Info.Status.VersionBuild,
(this.Yubikey.Info.Serial != 0 ? " (Serial " + this.Yubikey.Info.Serial + ")" : string.Empty));
yubiPanelIntro.Enabled = true;
}
#if NETFX_4
}, TaskScheduler.FromCurrentSynchronizationContext());
#endif
}
else
{
this.Yubikey = null;
yubiPanelIntro.Enabled = false;
yubiPanelIntro.Visible = true;
yubiPanelConfigure.Visible = false;
yubiConfigureIntroLabel.Visible = false;
PasswordType &= ~(Authenticator.PasswordTypes.YubiKeySlot1 | Authenticator.PasswordTypes.YubiKeySlot2);
}
}
/// <summary>
/// Password encrpytion is ticked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void passwordCheckbox_CheckedChanged(object sender, EventArgs e)
{
passwordField.Enabled = (passwordCheckbox.Checked);
verifyField.Enabled = (passwordCheckbox.Checked);
if (passwordCheckbox.Checked == true)
{
passwordField.Focus();
}
}
/// <summary>
/// OK button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void okButton_Click(object sender, EventArgs e)
{
// check password is set if requried
if (passwordCheckbox.Checked == true && passwordField.Text.Trim().Length == 0)
{
WinAuthForm.ErrorDialog(this, strings.EnterPassword);
this.DialogResult = System.Windows.Forms.DialogResult.None;
return;
}
if (passwordCheckbox.Checked == true && string.Compare(passwordField.Text.Trim(), verifyField.Text.Trim()) != 0)
{
WinAuthForm.ErrorDialog(this, strings.PasswordsDontMatch);
this.DialogResult = System.Windows.Forms.DialogResult.None;
return;
}
if (yubikeyBox.Checked == true && YubikeySlot == 0 && (PasswordType & (Authenticator.PasswordTypes.YubiKeySlot1 | Authenticator.PasswordTypes.YubiKeySlot2)) == 0)
{
WinAuthForm.ErrorDialog(this, "Please verify your YubiKey using the Use Slot or Configure Slot buttons.");
this.DialogResult = System.Windows.Forms.DialogResult.None;
return;
}
// set the valid password type property
PasswordType = Authenticator.PasswordTypes.None;
Password = null;
if (userCheckbox.Checked == true)
{
PasswordType |= Authenticator.PasswordTypes.User;
}
else if (machineCheckbox.Checked == true)
{
PasswordType |= Authenticator.PasswordTypes.Machine;
}
if (passwordCheckbox.Checked == true)
{
PasswordType |= Authenticator.PasswordTypes.Explicit;
if (this.passwordField.Text != EXISTING_PASSWORD)
{
Password = this.passwordField.Text.Trim();
}
}
if (yubikeyBox.Checked == true)
{
if (YubikeySlot == 1)
{
PasswordType |= Authenticator.PasswordTypes.YubiKeySlot1;
}
else if (YubikeySlot == 2)
{
PasswordType |= Authenticator.PasswordTypes.YubiKeySlot2;
}
}
}
/// <summary>
/// Get a new random seed
/// </summary>
/// <param name="wordCount">number of words in seed</param>
/// <returns></returns>
private string GetSeed(int wordCount)
{
if (_seedWords.Count == 0)
{
using (var s = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("WinAuth.Resources.SeedWords.txt")))
{
string line;
while ((line = s.ReadLine()) != null)
{
if (line.Length != 0)
{
_seedWords.Add(line);
}
}
}
}
List<string> words = new List<string>();
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
byte[] buffer = new byte[4];
for (var i = 0; i < wordCount; i++)
{
random.GetBytes(buffer);
int pos = (int)(BitConverter.ToUInt32(buffer, 0) % (uint)_seedWords.Count());
// check for duplicates
var word = _seedWords[pos].ToLower();
if (words.IndexOf(word) != -1)
{
i--;
continue;
}
words.Add(word);
}
return string.Join(" ", words.ToArray());
}
/// <summary>
/// Get a random seed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void yubiCreateSecretButton_Click(object sender, EventArgs e)
{
yubiSecretField.Text = GetSeed(12);
}
/// <summary>
/// Set up new yubikey
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void yubikeyCreateButton_Click(object sender, EventArgs e)
{
yubiPanelConfigure.Visible = true;
yubiConfigureIntroLabel.Visible = true;
}
/// <summary>
/// Configure the Yubikey
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void yubiSecretUpdateButton_Click(object sender, EventArgs e)
{
if (yubiSecretField.Text.Trim().Length == 0)
{
WinAuthForm.ErrorDialog(this, "Please enter a secret phase or password");
return;
}
if (WinAuthForm.ConfirmDialog(this,
"This will overwrite any existing data on your YubiKey.\n\nAre you sure you want to continue?",
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) != System.Windows.Forms.DialogResult.Yes)
{
return;
}
int slot = (yubiSlotToggle.Checked == true ? 2 : 1);
bool press = yubiPressToggle.Checked;
// bug in YubiKey 3.2.x (and below?) where using keypress doesn't always work
// see http://forum.yubico.com/viewtopic.php?f=26&t=1571
if (press == true
&& (this.Yubikey.Info.Status.VersionMajor < 3
|| (this.Yubikey.Info.Status.VersionMajor == 3 && this.Yubikey.Info.Status.VersionMinor <= 3)))
{
if (WinAuthForm.ConfirmDialog(this,
"This is a known issue using \"Require button press\" with YubiKeys that have firmware version 3.3 and below. It can cause intermittent problems when reading the Challenge-Response. You can contact Yubico and may be able to get a free replacement.\n\nDo you want to continue anyway?",
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) != System.Windows.Forms.DialogResult.Yes)
{
return;
}
}
// calculate the actual key. This is a byte version of the string, salt="mnemonic"(+user password TBD), PBKDF 2048 times, return 20byte/160bit key
byte[] bytes = Encoding.UTF8.GetBytes(yubiSecretField.Text.Trim());
string salt = "mnemonic";
byte[] saltbytes = Encoding.UTF8.GetBytes(salt);
Rfc2898DeriveBytes kg = new Rfc2898DeriveBytes(bytes, saltbytes, YUBIKEY_PBKDF2_ITERATIONS);
byte[] key = kg.GetBytes(YUBIKEY_PBKDF2_KEYSIZE);
try
{
this.Yubikey.SetChallengeResponse(slot, key, key.Length, press);
}
catch (YubKeyException ex)
{
WinAuthForm.ErrorDialog(this, ex.Message, ex);
return;
}
if (press == true)
{
if (WinAuthForm.ConfirmDialog(this,
"Your YubiKey slot will now be verified. Please click its button when it flashes." + Environment.NewLine + Environment.NewLine + "Continue?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
{
WinAuthForm.ErrorDialog(this, "Your YubiKey has been updated. Please verify it before continuing.");
return;
}
}
// perform the test encryption/decryption using the yubi
try
{
string challenge = "WinAuth";
string plain = Authenticator.ByteArrayToString(Encoding.ASCII.GetBytes(challenge));
Authenticator.PasswordTypes passwordType = (slot == 1 ? Authenticator.PasswordTypes.YubiKeySlot1 : Authenticator.PasswordTypes.YubiKeySlot2);
string encrypted = Authenticator.EncryptSequence(plain, passwordType, null, this.Yubikey);
plain = Authenticator.DecryptSequence(encrypted, passwordType, null, this.Yubikey);
string response = Encoding.ASCII.GetString(Authenticator.StringToByteArray(plain));
if (challenge != response)
{
throw new ApplicationException("verification failed");
}
}
catch (ApplicationException ex)
{
WinAuthForm.ErrorDialog(this, "The YubiKey test failed. Please try configuring it again or doing it manually. (" + ex.Message + ")");
return;
}
YubikeySlot = slot;
WinAuthForm.ErrorDialog(this, "Your YubiKey has been successfully updated.");
}
/// <summary>
/// Check the configuration with the current yubikey
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void yubikeyCheckButton_Click(object sender, EventArgs e)
{
if (WinAuthForm.ConfirmDialog(this,
"Your YubiKey slot will now be verified. If it requires you to press the button, please press when the light starts flashing." + Environment.NewLine + Environment.NewLine + "Continue?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
{
return;
}
int slot = (yubiSlotToggle.Checked == true ? 2 : 1);
try
{
byte[] challenge = Encoding.ASCII.GetBytes("WinAuth");
// get the hash from the key
byte[] hash = this.Yubikey.ChallengeResponse(slot, challenge);
}
catch (ApplicationException ex)
{
WinAuthForm.ErrorDialog(this, "The YubiKey test failed. Please check the configuration is for Challenge-Response in HMAC-SHA1 mode with Variable Input. (" + ex.Message + ")");
return;
}
YubikeySlot = slot;
WinAuthForm.ErrorDialog(this, "Your YubiKey has been successfully tested.");
}
}
}