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

Annotation of /trunk/Src/wptPassphraseDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 5634 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 twoaday 2 /* wptPassphraseDlg.cpp - Dialog to get the passphrase
2 twoaday 4 * Copyright (C) 2001-2005 Timo Schulz
3 twoaday 2 *
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     #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    
33    
34     struct passphrase_s {
35 twoaday 23 char *title; /* title of the dialog. */
36     char pwd[256]; /* the actual passphrase. */
37     int strict; /* do a simple check how good the passphrase is. */
38     int repeat; /* Indicate last try was wrong. */
39     int cancel; /* 1 if user cancelled operation. */
40 twoaday 2 };
41    
42    
43 twoaday 23 /* Dialog procedure to request a passphrase from the user. */
44 twoaday 2 static BOOL CALLBACK
45 twoaday 4 passwd_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
46 twoaday 2 {
47     static passphrase_s * pwd;
48     int nbytes;
49    
50 twoaday 4 switch (msg) {
51 twoaday 2 case WM_INITDIALOG:
52     pwd = (passphrase_s *)lparam;
53 twoaday 4 if (pwd == NULL)
54     dlg_fatal_error (dlg, "Could not get dialog param!");
55 twoaday 23 #ifndef LANG_DE
56 twoaday 4 SetWindowText (dlg, _("Passphrase Dialog"));
57 twoaday 23 #endif
58 twoaday 4 CheckDlgButton (dlg, IDC_PASSWD_HIDE, BST_CHECKED);
59     if (pwd->title)
60     SetWindowText (dlg, pwd->title);
61 twoaday 2 if (pwd->repeat)
62     SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase"));
63     else
64     SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase"));
65 twoaday 4 SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD ));
66 twoaday 23 center_window2 (dlg, NULL, HWND_TOPMOST);
67     center_window (dlg, NULL);
68 twoaday 4 SetForegroundWindow (dlg);
69 twoaday 2 return FALSE;
70    
71     case WM_COMMAND:
72 twoaday 22 if (HIWORD (wparam) == BN_CLICKED &&
73     LOWORD (wparam) == IDC_PASSWD_HIDE) {
74     HWND hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD);
75     int hide = IsDlgButtonChecked (dlg, IDC_PASSWD_HIDE);
76     SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
77     SetFocus (hwnd);
78 twoaday 2 }
79    
80 twoaday 22 switch (LOWORD (wparam)) {
81 twoaday 2 case IDOK:
82     pwd->cancel = 0;
83 twoaday 22 nbytes = GetDlgItemText( dlg, IDC_PASSWD_PWD, pwd->pwd, DIM (pwd->pwd)-1);
84     if (!nbytes)
85     strcpy (pwd->pwd, "");
86     else if (pwd->strict && check_passwd_quality (pwd->pwd, 0)) {
87     int id = msg_box (dlg, _("Your passphrase should be at least 8 characters"
88     " long\nand should contain non-alphabetic characters."
89     "\n\nStill proceed?"),
90     _("Key Generation"), MB_ICONWARNING|MB_YESNO);
91     if (id == IDNO)
92 twoaday 23 return TRUE;
93 twoaday 22 }
94     SetDlgItemText (dlg, IDC_PASSWD_PWD, "");
95     EndDialog (dlg, TRUE);
96 twoaday 2 return TRUE;
97    
98     case IDCANCEL:
99     pwd->cancel = 1;
100 twoaday 23 EndDialog (dlg, FALSE);
101     return TRUE;
102 twoaday 2 }
103     break;
104     }
105    
106     return FALSE;
107 twoaday 23 }
108 twoaday 2
109    
110 twoaday 23 /* Request a passphrase from the user. @title is the title of the
111     dialog and @ret_cancel is true if user cancelled the operation.
112     Return value: the passphrase or NUL for an error. */
113 twoaday 22 char*
114     request_passphrase (const char *title, int flags, int *ret_cancel)
115 twoaday 2 {
116     passphrase_s pass;
117 twoaday 22 char *p;
118 twoaday 2
119 twoaday 22 memset (&pass, 0, sizeof (pass));
120 twoaday 2 if (title && *title) {
121     pass.title = m_strdup (title);
122     if (!pass.title)
123     BUG (0);
124     }
125 twoaday 22 pass.repeat = flags & PASSDLG_INIT? 0 : 1;
126     pass.strict = flags & PASSDLG_STRICT? 1 : 0;
127 twoaday 2 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd,
128     passwd_dlg_proc, (LPARAM)&pass);
129     if (pass.cancel == 1) {
130     *ret_cancel = 1;
131     return NULL;
132     }
133     p = m_strdup (pass.pwd);
134     if (!p)
135     BUG (0);
136     free_if_alloc (pass.title);
137 twoaday 22 memset (&pass, 0, sizeof (pass));
138 twoaday 2 return p;
139 twoaday 23 }
140 twoaday 2
141    
142 twoaday 23 /* Request a passphrase from the user but also confirm the passphrase
143     to make sure there is no typo. Arguments same as in the normal version
144     of the function. */
145     char*
146 twoaday 22 request_passphrase2 (const char *title, int flags, int *ret_cancel)
147 twoaday 2 {
148 twoaday 22 char *pass1, *pass2;
149 twoaday 2 int equal = 0, cancel = 0;
150    
151     *ret_cancel = 1;
152     while (!equal) {
153 twoaday 22 pass1 = request_passphrase (title, PASSDLG_INIT|flags, &cancel);
154 twoaday 2 if (cancel)
155     return NULL;
156 twoaday 22 pass2 = request_passphrase (title, PASSDLG_REPEAT, &cancel);
157 twoaday 2 if (cancel) {
158     sfree_if_alloc (pass1);
159     return NULL;
160     }
161    
162     if (strcmp (pass1, pass2)) {
163     msg_box (NULL, _("Passphrases do not match. Please try again."),
164     title, MB_INFO);
165     sfree_if_alloc (pass1);
166     sfree_if_alloc (pass2);
167     }
168     else {
169     if (is_8bit_string (pass1)) {
170     msg_box (NULL, _("The passphrase contains 8-bit characters.\n"
171     "It is not suggested to use charset specific characters."),
172     title, MB_ERR);
173     equal = 0;
174     sfree_if_alloc (pass1);
175     sfree_if_alloc (pass2);
176     }
177     else
178     equal = 1;
179     }
180     }
181     sfree_if_alloc (pass2);
182     *ret_cancel = 0;
183     return pass1;
184     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26