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

Annotation of /trunk/Src/wptPassphraseDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 368 - (hide 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 werner 36 /* wptPassphraseDlg.cpp - Dialog to get the passphrase
2 twoaday 328 * Copyright (C) 2001-2006, 2009 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 297 #include "wptRegistry.h"
33 twoaday 328 #include "wptErrors.h"
34 twoaday 278 #include "StringBuffer.h"
35 werner 36
36    
37     struct passphrase_s {
38 twoaday 225 char *title; /* Title of the dialog. */
39 twoaday 328 // FIXME: do not use static buffer
40     char pwd[2048]; /* The actual passphrase. */
41 twoaday 225 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 twoaday 328 static subclass_s passwd_edit_proc;
53 twoaday 225
54 twoaday 328 /* 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 twoaday 225 /* 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 twoaday 368 char *uid = utf8_to_native (key->uids->name);
83    
84     StringBuffer p;
85 twoaday 278 p = p + uid + " (" + get_keyinfo (key) + ")";
86    
87 twoaday 225 SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF,
88 twoaday 278 CB_ADDSTRING, 0, (LPARAM)(char *)p.getBuffer ());
89 twoaday 225 SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF, CB_SETCURSEL, 0, 0);
90 twoaday 278 EnableWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), FALSE);
91 twoaday 225 free_if_alloc (uid);
92     }
93    
94    
95 werner 36 /* 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 twoaday 181 static passphrase_s *pwd;
100 werner 36 int nbytes;
101 twoaday 229 int cancel;
102 werner 36
103     switch (msg) {
104     case WM_INITDIALOG:
105     pwd = (passphrase_s *)lparam;
106 twoaday 225 if (!pwd)
107     BUG (0);
108 werner 36 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 twoaday 328 if (pwd->key) {
117     ShowWindow (GetDlgItem (dlg, IDC_PASSWD_QOPINF), SW_HIDE);
118     ShowWindow (GetDlgItem (dlg, IDC_PASSWD_QOP), SW_HIDE);
119 twoaday 225 set_passphrase_hint (dlg, pwd->key);
120 twoaday 328 }
121 twoaday 225 else
122     ShowWindow (GetDlgItem (dlg, IDC_PASSWD_KEYINF), SW_HIDE);
123 twoaday 78 SetDlgItemText (dlg, IDC_PASSWD_HIDE, _("&Hide Typing"));
124 twoaday 101 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
125 twoaday 225 SetDlgItemText (dlg, IDOK, _("&OK"));
126 twoaday 328 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 twoaday 225 SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD));
144 werner 36 center_window2 (dlg, NULL, HWND_TOPMOST);
145     center_window (dlg, NULL);
146     SetForegroundWindow (dlg);
147     return FALSE;
148 twoaday 181
149     case WM_ACTIVATE:
150 twoaday 297 /* 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 twoaday 181 break;
154    
155     case WM_DESTROY:
156 twoaday 297 if (!reg_prefs.no_safe_pwd_ctrl)
157     safe_edit_control_free (dlg, IDC_PASSWD_PWD);
158 twoaday 328 balloon_msg_disable ();
159 twoaday 181 break;
160 werner 36
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 twoaday 225
167 werner 36 SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
168     SetFocus (hwnd);
169     }
170    
171     switch (LOWORD (wparam)) {
172     case IDOK:
173     pwd->cancel = 0;
174 twoaday 181 nbytes = SafeGetDlgItemText (dlg, IDC_PASSWD_PWD,
175     pwd->pwd, DIM (pwd->pwd)-1);
176     if (!nbytes && pwd->not_empty) {
177 twoaday 328 show_balloon_msg (GetDlgItem (dlg, IDC_PASSWD_PWD),
178     _("Please enter a passphrase."), IDI_ERROR);
179 twoaday 181 return TRUE;
180     }
181    
182 werner 36 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 twoaday 248 "\n\nContinue?"),
188 werner 36 _("Key Generation"), MB_ICONWARNING|MB_YESNO);
189     if (id == IDNO)
190     return TRUE;
191     }
192 twoaday 229 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 twoaday 328 "Make sure that all systems you work on properly support UTF-8 handling.\n"
197 twoaday 248 "Continue?"),
198 twoaday 229 _("Key Generation"), MB_ICONWARNING|MB_YESNO);
199     if (cancel == IDNO) {
200     wipememory (pwd->pwd, sizeof (pwd->pwd));
201     return TRUE;
202     }
203     }
204 werner 36 SetDlgItemText (dlg, IDC_PASSWD_PWD, "");
205 twoaday 225 EndDialog (dlg, 1);
206 werner 36 return TRUE;
207    
208     case IDCANCEL:
209     pwd->cancel = 1;
210 twoaday 225 EndDialog (dlg, 0);
211 werner 36 return TRUE;
212     }
213     break;
214     }
215    
216     return FALSE;
217     }
218    
219    
220 twoaday 225 /* 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 twoaday 340
241     p = m_strdup (pass.pwd);
242 twoaday 225 wipememory (pass.pwd, sizeof (pass.pwd));
243     return p;
244     }
245    
246    
247 werner 36 /* 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 twoaday 170 *ret_cancel = 0; /* reset */
257 werner 36 memset (&pass, 0, sizeof (pass));
258 twoaday 225 if (title && *title)
259 werner 36 pass.title = m_strdup (title);
260     pass.repeat = flags & PASSDLG_INIT? 0 : 1;
261     pass.strict = flags & PASSDLG_STRICT? 1 : 0;
262 twoaday 181 pass.not_empty = flags & PASSDLG_NOTEMPTY? 1: 0;
263 twoaday 229 pass.warn_utf8 = flags & PASSDLG_WARN_UTF8? 1 : 0;
264 werner 36 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd,
265     passwd_dlg_proc, (LPARAM)&pass);
266 twoaday 225 free_if_alloc (pass.title);
267 werner 36 if (pass.cancel == 1) {
268     *ret_cancel = 1;
269     return NULL;
270     }
271 twoaday 340 p = m_strdup (pass.pwd);
272 twoaday 225 wipememory (pass.pwd, sizeof (pass.pwd));
273 werner 36 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 twoaday 229 else
304     equal = 1;
305 werner 36 }
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