1 |
werner |
36 |
/* wptPassphraseDlg.cpp - Dialog to get the passphrase |
2 |
twoaday |
225 |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
werner |
36 |
* |
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 |
werner |
47 |
#include "resource.h" |
27 |
werner |
36 |
#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 |
twoaday |
225 |
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 |
werner |
36 |
int repeat; /* Indicate last try was wrong. */ |
43 |
|
|
int cancel; /* 1 if user cancelled operation. */ |
44 |
twoaday |
229 |
int warn_utf8; |
45 |
twoaday |
181 |
int not_empty; |
46 |
twoaday |
225 |
gpgme_key_t key; |
47 |
werner |
36 |
}; |
48 |
|
|
|
49 |
|
|
|
50 |
twoaday |
225 |
const char* get_keyinfo (gpgme_key_t key); |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
/* Fill in the key information in the combo box, |
54 |
|
|
according to the key in @key. */ |
55 |
|
|
static void |
56 |
|
|
set_passphrase_hint (HWND dlg, gpgme_key_t key) |
57 |
|
|
{ |
58 |
|
|
const char *inf; |
59 |
|
|
char *uid, *p; |
60 |
|
|
size_t len; |
61 |
|
|
|
62 |
|
|
uid = utf8_to_native (key->uids->name); |
63 |
|
|
inf = get_keyinfo (key); |
64 |
|
|
len = strlen (uid) + strlen (inf) + 8; |
65 |
|
|
p = new char[len+1]; |
66 |
|
|
if (!p) |
67 |
|
|
BUG (NULL); |
68 |
|
|
_snprintf (p, len, "%s (%s)", uid, inf); |
69 |
|
|
SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF, |
70 |
|
|
CB_ADDSTRING, 0, (LPARAM)(char *)p); |
71 |
|
|
SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF, CB_SETCURSEL, 0, 0); |
72 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), FALSE); |
73 |
|
|
free_if_alloc (p); |
74 |
|
|
free_if_alloc (uid); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
|
78 |
werner |
36 |
/* Dialog procedure to request a passphrase from the user. */ |
79 |
|
|
static BOOL CALLBACK |
80 |
|
|
passwd_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
81 |
|
|
{ |
82 |
twoaday |
181 |
static passphrase_s *pwd; |
83 |
werner |
36 |
int nbytes; |
84 |
twoaday |
229 |
int cancel; |
85 |
werner |
36 |
|
86 |
|
|
switch (msg) { |
87 |
|
|
case WM_INITDIALOG: |
88 |
|
|
pwd = (passphrase_s *)lparam; |
89 |
twoaday |
225 |
if (!pwd) |
90 |
|
|
BUG (0); |
91 |
werner |
36 |
SetWindowText (dlg, _("Passphrase Dialog")); |
92 |
|
|
CheckDlgButton (dlg, IDC_PASSWD_HIDE, BST_CHECKED); |
93 |
|
|
if (pwd->title) |
94 |
|
|
SetWindowText (dlg, pwd->title); |
95 |
|
|
if (pwd->repeat) |
96 |
|
|
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase")); |
97 |
|
|
else |
98 |
|
|
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase")); |
99 |
twoaday |
225 |
if (pwd->key) |
100 |
|
|
set_passphrase_hint (dlg, pwd->key); |
101 |
|
|
else |
102 |
|
|
ShowWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), SW_HIDE); |
103 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_PASSWD_HIDE, _("&Hide Typing")); |
104 |
twoaday |
101 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
105 |
twoaday |
225 |
SetDlgItemText (dlg, IDOK, _("&OK")); |
106 |
|
|
SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD)); |
107 |
werner |
36 |
center_window2 (dlg, NULL, HWND_TOPMOST); |
108 |
|
|
center_window (dlg, NULL); |
109 |
|
|
SetForegroundWindow (dlg); |
110 |
|
|
return FALSE; |
111 |
twoaday |
181 |
|
112 |
|
|
case WM_ACTIVATE: |
113 |
|
|
safe_edit_control_init (dlg, IDC_PASSWD_PWD); |
114 |
|
|
break; |
115 |
|
|
|
116 |
|
|
case WM_DESTROY: |
117 |
|
|
safe_edit_control_free (dlg, IDC_PASSWD_PWD); |
118 |
|
|
break; |
119 |
werner |
36 |
|
120 |
|
|
case WM_COMMAND: |
121 |
|
|
if (HIWORD (wparam) == BN_CLICKED && |
122 |
|
|
LOWORD (wparam) == IDC_PASSWD_HIDE) { |
123 |
|
|
HWND hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD); |
124 |
|
|
int hide = IsDlgButtonChecked (dlg, IDC_PASSWD_HIDE); |
125 |
twoaday |
225 |
|
126 |
werner |
36 |
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
127 |
|
|
SetFocus (hwnd); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
switch (LOWORD (wparam)) { |
131 |
|
|
case IDOK: |
132 |
|
|
pwd->cancel = 0; |
133 |
twoaday |
181 |
nbytes = SafeGetDlgItemText (dlg, IDC_PASSWD_PWD, |
134 |
|
|
pwd->pwd, DIM (pwd->pwd)-1); |
135 |
|
|
if (!nbytes && pwd->not_empty) { |
136 |
twoaday |
225 |
msg_box (dlg, _("Please enter a passphrase."), |
137 |
|
|
_("Passphrase Dialog"), MB_ERR); |
138 |
twoaday |
181 |
return TRUE; |
139 |
|
|
} |
140 |
|
|
|
141 |
werner |
36 |
if (!nbytes) |
142 |
|
|
strcpy (pwd->pwd, ""); |
143 |
|
|
else if (pwd->strict && check_passwd_quality (pwd->pwd, 0)) { |
144 |
|
|
int id = msg_box (dlg, _("Your passphrase should be at least 8 characters" |
145 |
|
|
" long\nand should contain non-alphabetic characters." |
146 |
|
|
"\n\nStill proceed?"), |
147 |
|
|
_("Key Generation"), MB_ICONWARNING|MB_YESNO); |
148 |
|
|
if (id == IDNO) |
149 |
|
|
return TRUE; |
150 |
|
|
} |
151 |
twoaday |
229 |
if (pwd->warn_utf8 && !pwd->repeat && |
152 |
|
|
is_8bit_string (pwd->pwd)) { |
153 |
|
|
cancel = msg_box (dlg, |
154 |
|
|
_("The passphrase contains 8-bit characters.\n" |
155 |
|
|
"It is not suggested to use charset specific characters.\n" |
156 |
|
|
"Still proceed?"), |
157 |
|
|
_("Key Generation"), MB_ICONWARNING|MB_YESNO); |
158 |
|
|
if (cancel == IDNO) { |
159 |
|
|
wipememory (pwd->pwd, sizeof (pwd->pwd)); |
160 |
|
|
return TRUE; |
161 |
|
|
} |
162 |
|
|
} |
163 |
werner |
36 |
SetDlgItemText (dlg, IDC_PASSWD_PWD, ""); |
164 |
twoaday |
225 |
EndDialog (dlg, 1); |
165 |
werner |
36 |
return TRUE; |
166 |
|
|
|
167 |
|
|
case IDCANCEL: |
168 |
|
|
pwd->cancel = 1; |
169 |
twoaday |
225 |
EndDialog (dlg, 0); |
170 |
werner |
36 |
return TRUE; |
171 |
|
|
} |
172 |
|
|
break; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
return FALSE; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
twoaday |
225 |
/* Same as request_passphrase(), but with an additional hint about the |
180 |
|
|
key to unprotect. */ |
181 |
|
|
char* |
182 |
|
|
request_key_passphrase (gpgme_key_t key, const char *title, int *ret_cancel) |
183 |
|
|
{ |
184 |
|
|
passphrase_s pass; |
185 |
|
|
char *p; |
186 |
|
|
|
187 |
|
|
*ret_cancel = 0; |
188 |
|
|
memset (&pass, 0, sizeof (pass)); |
189 |
|
|
pass.key = key; |
190 |
|
|
pass.title = title? m_strdup (title) : NULL; |
191 |
|
|
|
192 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd, |
193 |
|
|
passwd_dlg_proc, (LPARAM)&pass); |
194 |
|
|
free_if_alloc (pass.title); |
195 |
|
|
if (pass.cancel == 1) { |
196 |
|
|
*ret_cancel = 1; |
197 |
|
|
return NULL; |
198 |
|
|
} |
199 |
twoaday |
233 |
p = native_to_utf8 (pass.pwd); |
200 |
twoaday |
225 |
wipememory (pass.pwd, sizeof (pass.pwd)); |
201 |
|
|
return p; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
|
205 |
werner |
36 |
/* Request a passphrase from the user. @title is the title of the |
206 |
|
|
dialog and @ret_cancel is true if user cancelled the operation. |
207 |
|
|
Return value: the passphrase or NUL for an error. */ |
208 |
|
|
char* |
209 |
|
|
request_passphrase (const char *title, int flags, int *ret_cancel) |
210 |
|
|
{ |
211 |
|
|
passphrase_s pass; |
212 |
|
|
char *p; |
213 |
|
|
|
214 |
twoaday |
170 |
*ret_cancel = 0; /* reset */ |
215 |
werner |
36 |
memset (&pass, 0, sizeof (pass)); |
216 |
twoaday |
225 |
if (title && *title) |
217 |
werner |
36 |
pass.title = m_strdup (title); |
218 |
|
|
pass.repeat = flags & PASSDLG_INIT? 0 : 1; |
219 |
|
|
pass.strict = flags & PASSDLG_STRICT? 1 : 0; |
220 |
twoaday |
181 |
pass.not_empty = flags & PASSDLG_NOTEMPTY? 1: 0; |
221 |
twoaday |
229 |
pass.warn_utf8 = flags & PASSDLG_WARN_UTF8? 1 : 0; |
222 |
werner |
36 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd, |
223 |
|
|
passwd_dlg_proc, (LPARAM)&pass); |
224 |
twoaday |
225 |
free_if_alloc (pass.title); |
225 |
werner |
36 |
if (pass.cancel == 1) { |
226 |
|
|
*ret_cancel = 1; |
227 |
|
|
return NULL; |
228 |
|
|
} |
229 |
twoaday |
229 |
p = native_to_utf8 (pass.pwd); |
230 |
twoaday |
225 |
wipememory (pass.pwd, sizeof (pass.pwd)); |
231 |
werner |
36 |
return p; |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
|
235 |
|
|
/* Request a passphrase from the user but also confirm the passphrase |
236 |
|
|
to make sure there is no typo. Arguments same as in the normal version |
237 |
|
|
of the function. */ |
238 |
|
|
char* |
239 |
|
|
request_passphrase2 (const char *title, int flags, int *ret_cancel) |
240 |
|
|
{ |
241 |
|
|
char *pass1, *pass2; |
242 |
|
|
int equal = 0, cancel = 0; |
243 |
|
|
|
244 |
|
|
*ret_cancel = 1; |
245 |
|
|
while (!equal) { |
246 |
|
|
pass1 = request_passphrase (title, PASSDLG_INIT|flags, &cancel); |
247 |
|
|
if (cancel) |
248 |
|
|
return NULL; |
249 |
|
|
pass2 = request_passphrase (title, PASSDLG_REPEAT, &cancel); |
250 |
|
|
if (cancel) { |
251 |
|
|
sfree_if_alloc (pass1); |
252 |
|
|
return NULL; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
if (strcmp (pass1, pass2)) { |
256 |
|
|
msg_box (NULL, _("Passphrases do not match. Please try again."), |
257 |
|
|
title, MB_INFO); |
258 |
|
|
sfree_if_alloc (pass1); |
259 |
|
|
sfree_if_alloc (pass2); |
260 |
|
|
} |
261 |
twoaday |
229 |
else |
262 |
|
|
equal = 1; |
263 |
werner |
36 |
} |
264 |
|
|
sfree_if_alloc (pass2); |
265 |
|
|
*ret_cancel = 0; |
266 |
|
|
return pass1; |
267 |
|
|
} |