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