/[winpt]/trunk/Src/wptPassphraseDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptPassphraseDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 368 - (show annotations)
Tue Dec 6 13:40:04 2011 UTC (13 years, 2 months ago) by twoaday
File size: 8967 byte(s)
2011-12-06  Timo Schulz  <twoaday@gmx.net>

        * wptNLS.cpp (get_user_langid): Removed.
        Use gettext_get_langid() instead. Changed all callers.
        (gettext_set_file): Removed.
        (w32_stpcpy): Likewise.
        * StringBuffer.cpp: Overload '=' operator for 'char*'.


1 /* wptPassphraseDlg.cpp - Dialog to get the passphrase
2 * Copyright (C) 2001-2006, 2009 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 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <windows.h>
21
22 #include "resource.h"
23 #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 #include "wptRegistry.h"
33 #include "wptErrors.h"
34 #include "StringBuffer.h"
35
36
37 struct passphrase_s {
38 char *title; /* Title of the dialog. */
39 // FIXME: do not use static buffer
40 char pwd[2048]; /* 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 warn_utf8;
45 int not_empty;
46 gpgme_key_t key;
47 };
48
49
50 const char* get_keyinfo (gpgme_key_t key);
51
52 static subclass_s passwd_edit_proc;
53
54 /* We sub-class the passphrase edit control to monitor
55 the structure of the passphrase. This output is used
56 as a quality INDICATOR(!). */
57 static BOOL CALLBACK
58 edit_subclass_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
59 {
60 HWND dlg = passwd_edit_proc.dlg;
61 switch (msg) {
62 case WM_CHAR:
63 int n, pos = 0;
64 n = GetWindowTextLength (GetDlgItem (dlg, IDC_PASSWD_PWD));
65 if (n > 0)
66 pos = n*6;
67 if (pos > 100)
68 pos = 100;
69 SendDlgItemMessage (dlg, IDC_PASSWD_QOP, PBM_SETPOS, (WPARAM)pos, 0);
70 break;
71 }
72
73 return CallWindowProc (passwd_edit_proc.old, hwnd, msg, wparam, lparam);
74 }
75
76
77 /* Fill in the key information in the combo box,
78 according to the key in @key. */
79 static void
80 set_passphrase_hint (HWND dlg, gpgme_key_t key)
81 {
82 char *uid = utf8_to_native (key->uids->name);
83
84 StringBuffer p;
85 p = p + uid + " (" + get_keyinfo (key) + ")";
86
87 SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF,
88 CB_ADDSTRING, 0, (LPARAM)(char *)p.getBuffer ());
89 SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF, CB_SETCURSEL, 0, 0);
90 EnableWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), FALSE);
91 free_if_alloc (uid);
92 }
93
94
95 /* Dialog procedure to request a passphrase from the user. */
96 static BOOL CALLBACK
97 passwd_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
98 {
99 static passphrase_s *pwd;
100 int nbytes;
101 int cancel;
102
103 switch (msg) {
104 case WM_INITDIALOG:
105 pwd = (passphrase_s *)lparam;
106 if (!pwd)
107 BUG (0);
108 SetWindowText (dlg, _("Passphrase Dialog"));
109 CheckDlgButton (dlg, IDC_PASSWD_HIDE, BST_CHECKED);
110 if (pwd->title)
111 SetWindowText (dlg, pwd->title);
112 if (pwd->repeat)
113 SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase"));
114 else
115 SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase"));
116 if (pwd->key) {
117 ShowWindow (GetDlgItem (dlg, IDC_PASSWD_QOPINF), SW_HIDE);
118 ShowWindow (GetDlgItem (dlg, IDC_PASSWD_QOP), SW_HIDE);
119 set_passphrase_hint (dlg, pwd->key);
120 }
121 else
122 ShowWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), SW_HIDE);
123 SetDlgItemText (dlg, IDC_PASSWD_HIDE, _("&Hide Typing"));
124 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
125 SetDlgItemText (dlg, IDOK, _("&OK"));
126 SetDlgItemText (dlg, IDC_PASSWD_QOPINF, _("Quality indicator:"));
127
128 if (!pwd->key) {
129 HWND hwnd;
130 hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD);
131 passwd_edit_proc.dlg = dlg;
132 passwd_edit_proc.current = (WNDPROC)edit_subclass_proc;
133 passwd_edit_proc.old = (WNDPROC)GetWindowLong(hwnd, GWL_WNDPROC);
134 if (passwd_edit_proc.old) {
135 if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)passwd_edit_proc.current)) {
136 msg_box (dlg, "Could not set keylist window procedure.",
137 _("Key Manager"), MB_ERR);
138 BUG (NULL);
139 }
140 }
141 }
142
143 SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD));
144 center_window2 (dlg, NULL, HWND_TOPMOST);
145 center_window (dlg, NULL);
146 SetForegroundWindow (dlg);
147 return FALSE;
148
149 case WM_ACTIVATE:
150 /* See comment in wptPassphraseCBDlg.cpp for more information. */
151 if (!reg_prefs.no_safe_pwd_ctrl)
152 safe_edit_control_init (dlg, IDC_PASSWD_PWD);
153 break;
154
155 case WM_DESTROY:
156 if (!reg_prefs.no_safe_pwd_ctrl)
157 safe_edit_control_free (dlg, IDC_PASSWD_PWD);
158 balloon_msg_disable ();
159 break;
160
161 case WM_COMMAND:
162 if (HIWORD (wparam) == BN_CLICKED &&
163 LOWORD (wparam) == IDC_PASSWD_HIDE) {
164 HWND hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD);
165 int hide = IsDlgButtonChecked (dlg, IDC_PASSWD_HIDE);
166
167 SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
168 SetFocus (hwnd);
169 }
170
171 switch (LOWORD (wparam)) {
172 case IDOK:
173 pwd->cancel = 0;
174 nbytes = SafeGetDlgItemText (dlg, IDC_PASSWD_PWD,
175 pwd->pwd, DIM (pwd->pwd)-1);
176 if (!nbytes && pwd->not_empty) {
177 show_balloon_msg (GetDlgItem (dlg, IDC_PASSWD_PWD),
178 _("Please enter a passphrase."), IDI_ERROR);
179 return TRUE;
180 }
181
182 if (!nbytes)
183 strcpy (pwd->pwd, "");
184 else if (pwd->strict && check_passwd_quality (pwd->pwd, 0)) {
185 int id = msg_box (dlg, _("Your passphrase should be at least 8 characters"
186 " long\nand should contain non-alphabetic characters."
187 "\n\nContinue?"),
188 _("Key Generation"), MB_ICONWARNING|MB_YESNO);
189 if (id == IDNO)
190 return TRUE;
191 }
192 if (pwd->warn_utf8 && !pwd->repeat &&
193 is_8bit_string (pwd->pwd)) {
194 cancel = msg_box (dlg,
195 _("The passphrase contains 8-bit characters.\n"
196 "Make sure that all systems you work on properly support UTF-8 handling.\n"
197 "Continue?"),
198 _("Key Generation"), MB_ICONWARNING|MB_YESNO);
199 if (cancel == IDNO) {
200 wipememory (pwd->pwd, sizeof (pwd->pwd));
201 return TRUE;
202 }
203 }
204 SetDlgItemText (dlg, IDC_PASSWD_PWD, "");
205 EndDialog (dlg, 1);
206 return TRUE;
207
208 case IDCANCEL:
209 pwd->cancel = 1;
210 EndDialog (dlg, 0);
211 return TRUE;
212 }
213 break;
214 }
215
216 return FALSE;
217 }
218
219
220 /* Same as request_passphrase(), but with an additional hint about the
221 key to unprotect. */
222 char*
223 request_key_passphrase (gpgme_key_t key, const char *title, int *ret_cancel)
224 {
225 passphrase_s pass;
226 char *p;
227
228 *ret_cancel = 0;
229 memset (&pass, 0, sizeof (pass));
230 pass.key = key;
231 pass.title = title? m_strdup (title) : NULL;
232
233 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd,
234 passwd_dlg_proc, (LPARAM)&pass);
235 free_if_alloc (pass.title);
236 if (pass.cancel == 1) {
237 *ret_cancel = 1;
238 return NULL;
239 }
240
241 p = m_strdup (pass.pwd);
242 wipememory (pass.pwd, sizeof (pass.pwd));
243 return p;
244 }
245
246
247 /* Request a passphrase from the user. @title is the title of the
248 dialog and @ret_cancel is true if user cancelled the operation.
249 Return value: the passphrase or NUL for an error. */
250 char*
251 request_passphrase (const char *title, int flags, int *ret_cancel)
252 {
253 passphrase_s pass;
254 char *p;
255
256 *ret_cancel = 0; /* reset */
257 memset (&pass, 0, sizeof (pass));
258 if (title && *title)
259 pass.title = m_strdup (title);
260 pass.repeat = flags & PASSDLG_INIT? 0 : 1;
261 pass.strict = flags & PASSDLG_STRICT? 1 : 0;
262 pass.not_empty = flags & PASSDLG_NOTEMPTY? 1: 0;
263 pass.warn_utf8 = flags & PASSDLG_WARN_UTF8? 1 : 0;
264 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd,
265 passwd_dlg_proc, (LPARAM)&pass);
266 free_if_alloc (pass.title);
267 if (pass.cancel == 1) {
268 *ret_cancel = 1;
269 return NULL;
270 }
271 p = m_strdup (pass.pwd);
272 wipememory (pass.pwd, sizeof (pass.pwd));
273 return p;
274 }
275
276
277 /* Request a passphrase from the user but also confirm the passphrase
278 to make sure there is no typo. Arguments same as in the normal version
279 of the function. */
280 char*
281 request_passphrase2 (const char *title, int flags, int *ret_cancel)
282 {
283 char *pass1, *pass2;
284 int equal = 0, cancel = 0;
285
286 *ret_cancel = 1;
287 while (!equal) {
288 pass1 = request_passphrase (title, PASSDLG_INIT|flags, &cancel);
289 if (cancel)
290 return NULL;
291 pass2 = request_passphrase (title, PASSDLG_REPEAT, &cancel);
292 if (cancel) {
293 sfree_if_alloc (pass1);
294 return NULL;
295 }
296
297 if (strcmp (pass1, pass2)) {
298 msg_box (NULL, _("Passphrases do not match. Please try again."),
299 title, MB_INFO);
300 sfree_if_alloc (pass1);
301 sfree_if_alloc (pass2);
302 }
303 else
304 equal = 1;
305 }
306 sfree_if_alloc (pass2);
307 *ret_cancel = 0;
308 return pass1;
309 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26