1 |
/* wptPassphraseDlg.cpp - Dialog to get the passphrase |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
|
26 |
#include "resource.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptCommonCtl.h" |
29 |
#include "wptContext.h" |
30 |
#include "wptDlgs.h" |
31 |
#include "wptNLS.h" |
32 |
#include "wptW32API.h" |
33 |
#include "wptUTF8.h" |
34 |
#include "wptVersion.h" |
35 |
#include "wptTypes.h" |
36 |
|
37 |
|
38 |
struct passphrase_s { |
39 |
char *title; /* title of the dialog. */ |
40 |
char pwd[256]; /* the actual passphrase. */ |
41 |
int strict; /* do a simple check how good the passphrase is. */ |
42 |
int repeat; /* Indicate last try was wrong. */ |
43 |
int cancel; /* 1 if user cancelled operation. */ |
44 |
int not_empty; |
45 |
}; |
46 |
|
47 |
|
48 |
/* Dialog procedure to request a passphrase from the user. */ |
49 |
static BOOL CALLBACK |
50 |
passwd_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
51 |
{ |
52 |
static passphrase_s *pwd; |
53 |
int nbytes; |
54 |
|
55 |
switch (msg) { |
56 |
case WM_INITDIALOG: |
57 |
pwd = (passphrase_s *)lparam; |
58 |
if (pwd == NULL) |
59 |
dlg_fatal_error (dlg, "Could not get dialog param!"); |
60 |
SetWindowText (dlg, _("Passphrase Dialog")); |
61 |
CheckDlgButton (dlg, IDC_PASSWD_HIDE, BST_CHECKED); |
62 |
if (pwd->title) |
63 |
SetWindowText (dlg, pwd->title); |
64 |
if (pwd->repeat) |
65 |
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase")); |
66 |
else |
67 |
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase")); |
68 |
SetDlgItemText (dlg, IDC_PASSWD_HIDE, _("&Hide Typing")); |
69 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
70 |
SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD )); |
71 |
center_window2 (dlg, NULL, HWND_TOPMOST); |
72 |
center_window (dlg, NULL); |
73 |
SetForegroundWindow (dlg); |
74 |
return FALSE; |
75 |
|
76 |
case WM_ACTIVATE: |
77 |
safe_edit_control_init (dlg, IDC_PASSWD_PWD); |
78 |
break; |
79 |
|
80 |
case WM_DESTROY: |
81 |
safe_edit_control_free (dlg, IDC_PASSWD_PWD); |
82 |
break; |
83 |
|
84 |
case WM_COMMAND: |
85 |
if (HIWORD (wparam) == BN_CLICKED && |
86 |
LOWORD (wparam) == IDC_PASSWD_HIDE) { |
87 |
HWND hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD); |
88 |
int hide = IsDlgButtonChecked (dlg, IDC_PASSWD_HIDE); |
89 |
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
90 |
SetFocus (hwnd); |
91 |
} |
92 |
|
93 |
switch (LOWORD (wparam)) { |
94 |
case IDOK: |
95 |
pwd->cancel = 0; |
96 |
nbytes = SafeGetDlgItemText (dlg, IDC_PASSWD_PWD, |
97 |
pwd->pwd, DIM (pwd->pwd)-1); |
98 |
if (!nbytes && pwd->not_empty) { |
99 |
msg_box (dlg, _("Please enter a passphrase."), _("Passphrase Dialog"), MB_ERR); |
100 |
return TRUE; |
101 |
} |
102 |
|
103 |
if (!nbytes) |
104 |
strcpy (pwd->pwd, ""); |
105 |
else if (pwd->strict && check_passwd_quality (pwd->pwd, 0)) { |
106 |
int id = msg_box (dlg, _("Your passphrase should be at least 8 characters" |
107 |
" long\nand should contain non-alphabetic characters." |
108 |
"\n\nStill proceed?"), |
109 |
_("Key Generation"), MB_ICONWARNING|MB_YESNO); |
110 |
if (id == IDNO) |
111 |
return TRUE; |
112 |
} |
113 |
SetDlgItemText (dlg, IDC_PASSWD_PWD, ""); |
114 |
EndDialog (dlg, TRUE); |
115 |
return TRUE; |
116 |
|
117 |
case IDCANCEL: |
118 |
pwd->cancel = 1; |
119 |
EndDialog (dlg, FALSE); |
120 |
return TRUE; |
121 |
} |
122 |
break; |
123 |
} |
124 |
|
125 |
return FALSE; |
126 |
} |
127 |
|
128 |
|
129 |
/* Request a passphrase from the user. @title is the title of the |
130 |
dialog and @ret_cancel is true if user cancelled the operation. |
131 |
Return value: the passphrase or NUL for an error. */ |
132 |
char* |
133 |
request_passphrase (const char *title, int flags, int *ret_cancel) |
134 |
{ |
135 |
passphrase_s pass; |
136 |
char *p; |
137 |
|
138 |
*ret_cancel = 0; /* reset */ |
139 |
memset (&pass, 0, sizeof (pass)); |
140 |
if (title && *title) { |
141 |
pass.title = m_strdup (title); |
142 |
if (!pass.title) |
143 |
BUG (0); |
144 |
} |
145 |
pass.repeat = flags & PASSDLG_INIT? 0 : 1; |
146 |
pass.strict = flags & PASSDLG_STRICT? 1 : 0; |
147 |
pass.not_empty = flags & PASSDLG_NOTEMPTY? 1: 0; |
148 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd, |
149 |
passwd_dlg_proc, (LPARAM)&pass); |
150 |
if (pass.cancel == 1) { |
151 |
*ret_cancel = 1; |
152 |
return NULL; |
153 |
} |
154 |
p = m_strdup (pass.pwd); |
155 |
if (!p) |
156 |
BUG (0); |
157 |
free_if_alloc (pass.title); |
158 |
memset (&pass, 0, sizeof (pass)); |
159 |
return p; |
160 |
} |
161 |
|
162 |
|
163 |
/* Request a passphrase from the user but also confirm the passphrase |
164 |
to make sure there is no typo. Arguments same as in the normal version |
165 |
of the function. */ |
166 |
char* |
167 |
request_passphrase2 (const char *title, int flags, int *ret_cancel) |
168 |
{ |
169 |
char *pass1, *pass2; |
170 |
int equal = 0, cancel = 0; |
171 |
|
172 |
*ret_cancel = 1; |
173 |
while (!equal) { |
174 |
pass1 = request_passphrase (title, PASSDLG_INIT|flags, &cancel); |
175 |
if (cancel) |
176 |
return NULL; |
177 |
pass2 = request_passphrase (title, PASSDLG_REPEAT, &cancel); |
178 |
if (cancel) { |
179 |
sfree_if_alloc (pass1); |
180 |
return NULL; |
181 |
} |
182 |
|
183 |
if (strcmp (pass1, pass2)) { |
184 |
msg_box (NULL, _("Passphrases do not match. Please try again."), |
185 |
title, MB_INFO); |
186 |
sfree_if_alloc (pass1); |
187 |
sfree_if_alloc (pass2); |
188 |
} |
189 |
else { |
190 |
if (is_8bit_string (pass1)) { |
191 |
msg_box (NULL, _("The passphrase contains 8-bit characters.\n" |
192 |
"It is not suggested to use charset specific characters."), |
193 |
title, MB_ERR); |
194 |
equal = 0; |
195 |
sfree_if_alloc (pass1); |
196 |
sfree_if_alloc (pass2); |
197 |
} |
198 |
else |
199 |
equal = 1; |
200 |
} |
201 |
} |
202 |
sfree_if_alloc (pass2); |
203 |
*ret_cancel = 0; |
204 |
return pass1; |
205 |
} |