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