[23e682]: src / ui / wxWidgets / safecombinationchange.cpp Maximize Restore History

Download this file

safecombinationchange.cpp    420 lines (343 with data), 14.2 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
/*
* Copyright (c) 2003-2013 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/** \file safecombinationchange.cpp
*
*/
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
////@begin includes
#include "SafeCombinationCtrl.h"
////@end includes
#include "safecombinationchange.h"
#include "core/PWCharPool.h" // for CheckPassword()
#include "./wxutils.h" // for ApplyPasswordFont
#include "./ExternalKeyboardButton.h"
#ifdef __WXMSW__
#include <wx/msw/msvcrt.h>
#endif
#ifndef NO_YUBI
////@begin XPM images
#include "graphics/Yubikey-button.xpm"
////@end XPM images
#endif
/*!
* CSafeCombinationChange type definition
*/
IMPLEMENT_CLASS( CSafeCombinationChange, wxDialog )
/*!
* CSafeCombinationChange event table definition
*/
BEGIN_EVENT_TABLE( CSafeCombinationChange, wxDialog )
////@begin CSafeCombinationChange event table entries
#ifndef NO_YUBI
EVT_BUTTON( ID_YUBIBTN, CSafeCombinationChange::OnYubibtnClick )
EVT_BUTTON( ID_YUBIBTN2, CSafeCombinationChange::OnYubibtn2Click )
EVT_TIMER(CYubiMixin::POLLING_TIMER_ID, CSafeCombinationChange::OnPollingTimer)
#endif
EVT_BUTTON( wxID_OK, CSafeCombinationChange::OnOkClick )
EVT_BUTTON( wxID_CANCEL, CSafeCombinationChange::OnCancelClick )
////@end CSafeCombinationChange event table entries
END_EVENT_TABLE()
/*!
* CSafeCombinationChange constructors
*/
CSafeCombinationChange::CSafeCombinationChange(wxWindow* parent, PWScore &core,
wxWindowID id, const wxString& caption,
const wxPoint& pos,
const wxSize& size, long style)
: m_core(core)
{
Init();
Create(parent, id, caption, pos, size, style);
}
/*!
* CSafeCombinationChange creator
*/
bool CSafeCombinationChange::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin CSafeCombinationChange creation
SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
wxDialog::Create( parent, id, caption, pos, size, style );
CreateControls();
if (GetSizer())
{
GetSizer()->SetSizeHints(this);
}
Centre();
////@end CSafeCombinationChange creation
#ifndef NO_YUBI
m_yubiMixin1.SetupMixin(FindWindow(ID_YUBIBTN), FindWindow(ID_YUBISTATUS));
m_yubiMixin1.SetPrompt1(_("Enter old safe combination (if any) and click on top Yubikey button"));
m_yubiMixin2.SetupMixin(FindWindow(ID_YUBIBTN2), FindWindow(ID_YUBISTATUS));
m_yubiMixin2.SetPrompt1(_("Enter old safe combination (if any) and click on top Yubikey button"));
m_pollingTimer = new wxTimer(this, CYubiMixin::POLLING_TIMER_ID);
m_pollingTimer->Start(250); // check for Yubikey every 250ms.
#endif
return true;
}
/*!
* CSafeCombinationChange destructor
*/
CSafeCombinationChange::~CSafeCombinationChange()
{
////@begin CSafeCombinationChange destruction
////@end CSafeCombinationChange destruction
#ifndef NO_YUBI
delete m_pollingTimer;
#endif
}
/*!
* Member initialisation
*/
void CSafeCombinationChange::Init()
{
////@begin CSafeCombinationChange member initialisation
m_oldPasswdEntry = NULL;
m_newPasswdEntry = NULL;
m_confirmEntry = NULL;
#ifndef NO_YUBI
m_YubiBtn = NULL;
m_YubiBtn2 = NULL;
m_yubiStatusCtrl = NULL;
#endif
////@end CSafeCombinationChange member initialisation
}
/*!
* Control creation for CSafeCombinationChange
*/
void CSafeCombinationChange::CreateControls()
{
////@begin CSafeCombinationChange content construction
CSafeCombinationChange* itemDialog1 = this;
wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
itemDialog1->SetSizer(itemBoxSizer2);
wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("Please enter the current combination, followed by a new combination.\nType the new combination once again to confirm it."), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer2->Add(itemStaticText3, 0, wxALIGN_LEFT|wxALL, 5);
#ifndef NO_YUBI
enum { DLGITEM_COLS = 3 };
#else
enum { DLGITEM_COLS = 2 };
#endif
wxFlexGridSizer* itemFlexGridSizer4 = new wxFlexGridSizer(DLGITEM_COLS, 0, 0);
itemBoxSizer2->Add(itemFlexGridSizer4, 0, wxALIGN_LEFT|wxALL, 5);
wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Old safe combination:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
itemFlexGridSizer4->Add(itemStaticText5, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_oldPasswdEntry = new CSafeCombinationCtrl( itemDialog1, ID_OLDPASSWD, &m_oldpasswd, wxDefaultPosition, wxSize(itemDialog1->ConvertDialogToPixels(wxSize(150, -1)).x, -1) );
itemFlexGridSizer4->Add(m_oldPasswdEntry, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
#ifndef NO_YUBI
m_YubiBtn = new wxBitmapButton( itemDialog1, ID_YUBIBTN, itemDialog1->GetBitmapResource(wxT("graphics/Yubikey-button.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(40, 15)), wxBU_AUTODRAW );
itemFlexGridSizer4->Add(m_YubiBtn, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM|wxSHAPED, 5);
#endif
wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("New safe combination:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
itemFlexGridSizer4->Add(itemStaticText8, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_newPasswdEntry = new CSafeCombinationCtrl( itemDialog1, ID_NEWPASSWD, &m_newpasswd, wxDefaultPosition, wxSize(itemDialog1->ConvertDialogToPixels(wxSize(150, -1)).x, -1) );
itemFlexGridSizer4->Add(m_newPasswdEntry, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
#ifndef NO_YUBI
m_YubiBtn2 = new wxBitmapButton( itemDialog1, ID_YUBIBTN2, itemDialog1->GetBitmapResource(wxT("graphics/Yubikey-button.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(40, 15)), wxBU_AUTODRAW );
itemFlexGridSizer4->Add(m_YubiBtn2, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM|wxSHAPED, 5);
#endif
wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("Confirmation:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
itemFlexGridSizer4->Add(itemStaticText11, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_confirmEntry = new CSafeCombinationCtrl( itemDialog1, ID_CONFIRM, &m_confirm, wxDefaultPosition, wxSize(itemDialog1->ConvertDialogToPixels(wxSize(150, -1)).x, -1) );
itemFlexGridSizer4->Add(m_confirmEntry, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
itemFlexGridSizer4->Add(10, 10, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
#ifndef NO_YUBI
m_yubiStatusCtrl = new wxStaticText( itemDialog1, ID_YUBISTATUS, _("Please insert your YubiKey"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer2->Add(m_yubiStatusCtrl, 0, wxGROW|wxALL, 5);
#endif
wxStdDialogButtonSizer* itemStdDialogButtonSizer15 = new wxStdDialogButtonSizer;
itemBoxSizer2->Add(itemStdDialogButtonSizer15, 0, wxGROW|wxALL, 5);
wxButton* itemButton16 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
itemButton16->SetDefault();
itemStdDialogButtonSizer15->AddButton(itemButton16);
wxButton* itemButton17 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer15->AddButton(itemButton17);
wxButton* itemButton18 = new wxButton( itemDialog1, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer15->AddButton(itemButton18);
itemStdDialogButtonSizer15->Realize();
////@end CSafeCombinationChange content construction
}
/*!
* Should we show tooltips?
*/
bool CSafeCombinationChange::ShowToolTips()
{
return true;
}
/*!
* Get bitmap resources
*/
#ifndef NO_YUBI
wxBitmap CSafeCombinationChange::GetBitmapResource( const wxString& name )
{
// Bitmap retrieval
////@begin CSafeCombinationChange bitmap retrieval
wxUnusedVar(name);
if (name == _T("graphics/Yubikey-button.xpm"))
{
wxBitmap bitmap(Yubikey_button_xpm);
return bitmap;
}
return wxNullBitmap;
////@end CSafeCombinationChange bitmap retrieval
}
#endif
/*!
* Get icon resources
*/
wxIcon CSafeCombinationChange::GetIconResource( const wxString& name )
{
// Icon retrieval
////@begin CSafeCombinationChange icon retrieval
wxUnusedVar(name);
return wxNullIcon;
////@end CSafeCombinationChange icon retrieval
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
*/
void CSafeCombinationChange::OnOkClick( wxCommandEvent& /* evt */ )
{
if (Validate() && TransferDataFromWindow()) {
StringX errmess;
int rc = m_core.CheckPasskey(m_core.GetCurFile(), m_oldpasswd);
if (rc == PWScore::WRONG_PASSWORD) {
wxMessageDialog err(this, _("The old safe combination is not correct"),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
} else if (rc == PWScore::CANT_OPEN_FILE) {
wxMessageDialog err(this, _("Cannot verify old safe combination - file gone?"),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
} else if (m_confirm != m_newpasswd) {
wxMessageDialog err(this, _("New safe combination and confirmation do not match"),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
// Vox populi vox dei - folks want the ability to use a weak
// passphrase, best we can do is warn them...
// If someone want to build a version that insists on proper
// passphrases, then just define the preprocessor macro
// PWS_FORCE_STRONG_PASSPHRASE in the build properties/Makefile
// (also used in CPasskeySetup)
} else if (!CPasswordCharPool::CheckPassword(m_newpasswd, errmess)) {
wxString msg = _("Weak passphrase:");
msg += wxT("\n\n");
msg += errmess.c_str();
#ifndef PWS_FORCE_STRONG_PASSPHRASE
msg += wxT("\n");
msg += _("Use it anyway?");
wxMessageDialog err(this, msg,
_("Error"), wxYES_NO | wxICON_HAND);
int rc1 = err.ShowModal();
if (rc1 == wxID_YES)
EndModal(wxID_OK);
#else
wxMessageDialog err(this, msg,
_("Error"), wxOK | wxICON_HAND);
err.ShowModal();
#endif // PWS_FORCE_STRONG_PASSPHRASE
} else { // password checks out OK.
EndModal(wxID_OK);
}
}
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
*/
void CSafeCombinationChange::OnCancelClick( wxCommandEvent& /* evt */ )
{
////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationChange.
// Before editing this code, remove the block markers.
EndModal(wxID_CANCEL);
////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationChange.
}
#ifndef NO_YUBI
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_YUBIBTN
*/
void CSafeCombinationChange::OnYubibtnClick( wxCommandEvent& /* event */ )
{
// Here we just need to get the existing c/r. We verify it as a curtesy to the user,
// that is, to indicate asap that it's incorrect.
m_oldresponse.clear();
// Allow blank password when Yubi's used
m_oldPasswdEntry->AllowEmptyCombinationOnce();
m_newPasswdEntry->AllowEmptyCombinationOnce();
m_confirmEntry->AllowEmptyCombinationOnce();
if (Validate() && TransferDataFromWindow()) {
if (m_yubiMixin1.PerformChallengeResponse(m_oldpasswd, m_oldresponse)) {
// Verify the response - a convenience, as we double check in OnYubibtn2Click().
int rc = m_core.CheckPasskey(m_core.GetCurFile(), m_oldresponse);
if (rc == PWScore::WRONG_PASSWORD) {
m_oldresponse.clear();
m_yubiStatusCtrl->SetForegroundColour(*wxRED);
m_yubiStatusCtrl->SetLabel(_("YubiKey safe combination incorrect"));
} else {
m_yubiMixin2.SetPrompt1(_("Enter new safe combination (if any) and click on bottom Yubikey button"));
m_yubiMixin2.UpdateStatus();
}
}
}
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_YUBIBTN2
*/
void CSafeCombinationChange::OnYubibtn2Click( wxCommandEvent& /* event */ )
{
// Allow blank password when Yubi's used:
m_oldPasswdEntry->AllowEmptyCombinationOnce();
m_newPasswdEntry->AllowEmptyCombinationOnce();
m_confirmEntry->AllowEmptyCombinationOnce();
if (Validate() && TransferDataFromWindow()) {
int rc;
// First check existing password/response:
// 1. Both old password and old response can't be blank
if (m_oldpasswd.empty() && m_oldresponse.empty()) {
m_yubiStatusCtrl->SetForegroundColour(*wxRED);
m_yubiStatusCtrl->SetLabel(_("Please confirm existing combination"));
return;
}
// 2. If there's an old response, it should already have been checked, but JIC:
if (!m_oldresponse.empty()) {
rc = m_core.CheckPasskey(m_core.GetCurFile(), m_oldresponse);
if (rc == PWScore::WRONG_PASSWORD) {
m_oldresponse.clear();
m_yubiStatusCtrl->SetForegroundColour(*wxRED);
m_yubiStatusCtrl->SetLabel(_("YubiKey safe combination incorrect"));
return;
}
} else {
// 3. No old response, we can only check the old password
rc = m_core.CheckPasskey(m_core.GetCurFile(), m_oldpasswd);
if (rc == PWScore::WRONG_PASSWORD) {
m_yubiStatusCtrl->SetForegroundColour(*wxRED);
m_yubiStatusCtrl->SetLabel(_("Current safe combination incorrect"));
return;
}
}
StringX response;
if (m_yubiMixin2.PerformChallengeResponse(m_newpasswd, response)) {
m_newpasswd = response;
EndModal(wxID_OK);
}
}
}
void CSafeCombinationChange::OnPollingTimer(wxTimerEvent &evt)
{
if (evt.GetId() == CYubiMixin::POLLING_TIMER_ID) {
m_yubiMixin1.HandlePollingTimer();
m_yubiMixin2.HandlePollingTimer();
}
}
#endif