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

Annotation of /trunk/Src/wptPassphraseDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (hide annotations)
Tue Jun 6 13:37:59 2006 UTC (18 years, 8 months ago) by twoaday
File size: 7422 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26