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

Annotation of /trunk/Src/wptPassphraseDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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