1 |
/* 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 |
#include "resource.h" |
27 |
#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 |
#ifndef LANG_DE |
60 |
SetWindowText (dlg, _("Passphrase Dialog")); |
61 |
#endif |
62 |
CheckDlgButton (dlg, IDC_PASSWD_HIDE, BST_CHECKED); |
63 |
if (pwd->title) |
64 |
SetWindowText (dlg, pwd->title); |
65 |
if (pwd->repeat) |
66 |
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase")); |
67 |
else |
68 |
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase")); |
69 |
SetDlgItemText (dlg, IDC_PASSWD_HIDE, _("&Hide Typing")); |
70 |
SetFocus (GetDlgItem (dlg, IDC_PASSWD_PWD )); |
71 |
center_window2 (dlg, NULL, HWND_TOPMOST); |
72 |
center_window (dlg, NULL); |
73 |
SetForegroundWindow (dlg); |
74 |
return FALSE; |
75 |
|
76 |
case WM_COMMAND: |
77 |
if (HIWORD (wparam) == BN_CLICKED && |
78 |
LOWORD (wparam) == IDC_PASSWD_HIDE) { |
79 |
HWND hwnd = GetDlgItem (dlg, IDC_PASSWD_PWD); |
80 |
int hide = IsDlgButtonChecked (dlg, IDC_PASSWD_HIDE); |
81 |
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
82 |
SetFocus (hwnd); |
83 |
} |
84 |
|
85 |
switch (LOWORD (wparam)) { |
86 |
case IDOK: |
87 |
pwd->cancel = 0; |
88 |
nbytes = GetDlgItemText( dlg, IDC_PASSWD_PWD, pwd->pwd, DIM (pwd->pwd)-1); |
89 |
if (!nbytes) |
90 |
strcpy (pwd->pwd, ""); |
91 |
else if (pwd->strict && check_passwd_quality (pwd->pwd, 0)) { |
92 |
int id = msg_box (dlg, _("Your passphrase should be at least 8 characters" |
93 |
" long\nand should contain non-alphabetic characters." |
94 |
"\n\nStill proceed?"), |
95 |
_("Key Generation"), MB_ICONWARNING|MB_YESNO); |
96 |
if (id == IDNO) |
97 |
return TRUE; |
98 |
} |
99 |
SetDlgItemText (dlg, IDC_PASSWD_PWD, ""); |
100 |
EndDialog (dlg, TRUE); |
101 |
return TRUE; |
102 |
|
103 |
case IDCANCEL: |
104 |
pwd->cancel = 1; |
105 |
EndDialog (dlg, FALSE); |
106 |
return TRUE; |
107 |
} |
108 |
break; |
109 |
} |
110 |
|
111 |
return FALSE; |
112 |
} |
113 |
|
114 |
|
115 |
/* Request a passphrase from the user. @title is the title of the |
116 |
dialog and @ret_cancel is true if user cancelled the operation. |
117 |
Return value: the passphrase or NUL for an error. */ |
118 |
char* |
119 |
request_passphrase (const char *title, int flags, int *ret_cancel) |
120 |
{ |
121 |
passphrase_s pass; |
122 |
char *p; |
123 |
|
124 |
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 |
} |