1 |
/* wptKeygenDlg.cpp - Key Generation dialog |
2 |
* Copyright (C) 2000-2004 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 |
|
21 |
#include <windows.h> |
22 |
|
23 |
#include "../resource.h" |
24 |
#include "wptTypes.h" |
25 |
#include "wptNLS.h" |
26 |
#include "wptGPG.h" |
27 |
#include "wptCommonCtl.h" |
28 |
#include "wptContext.h" /* for passphrase_s */ |
29 |
#include "wptDlgs.h" |
30 |
#include "wptW32API.h" |
31 |
#include "wptVersion.h" |
32 |
#include "wptErrors.h" |
33 |
#include "wptUTF8.h" |
34 |
|
35 |
|
36 |
static void |
37 |
clear_dlg_fields( HWND dlg ) |
38 |
{ |
39 |
SetDlgItemText( dlg, IDC_KEYGEN_SUBKEYBITS, "" ); |
40 |
SetDlgItemText( dlg, IDC_KEYGEN_NAME, "" ); |
41 |
SetDlgItemText( dlg, IDC_KEYGEN_EMAIL, "" ); |
42 |
SetDlgItemText( dlg, IDC_KEYGEN_COMMENT, "" ); |
43 |
SetDlgItemText( dlg, IDC_KEYGEN_EXPDATE, "" ); |
44 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
45 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
46 |
} /* clear_dlg_fields */ |
47 |
|
48 |
|
49 |
static void |
50 |
ask_for_backup( HWND dlg ) |
51 |
{ |
52 |
int id; |
53 |
char * path = NULL, * keyring = NULL; |
54 |
const char * name; |
55 |
|
56 |
path = get_gnupg_path( ); |
57 |
if( !path ) |
58 |
BUG( dlg ); |
59 |
id = msg_box( dlg, |
60 |
_("It is STRONGLY recommend that you backup your keyrings because they both " |
61 |
"contain VERY important data.\nRemember that your hard disk can crash or the " |
62 |
"files can be deleted by accident; so it is a good\nidea to store them on " |
63 |
"a different mass stoarge like a floppy or CDR!\n\n" |
64 |
"Backup your keyrings now?"), |
65 |
_("WARNING - Important hint" ), MB_YESNO ); |
66 |
if( id == IDYES ) { |
67 |
name = get_filename_dlg( dlg, 1, _("Destination for Public Keyring"), NULL, "pubring.gpg" ); |
68 |
if( name ) { |
69 |
keyring = make_filename( path, "pubring", "gpg" ); |
70 |
if( !CopyFile( keyring, name, FALSE ) ) |
71 |
log_box( _("Key Generation"), MB_ERR, |
72 |
_("Could not copy %s -> %s"), keyring, name ); |
73 |
free_if_alloc( keyring ); |
74 |
} |
75 |
name = get_filename_dlg( dlg, 1, _("Destination for Secret Keyring"), NULL, "secring.gpg" ); |
76 |
if( name ) { |
77 |
keyring = make_filename( path, "secring", "gpg" ); |
78 |
if( !CopyFile( keyring, name, FALSE ) ) |
79 |
log_box( _("Key Generation"), MB_ERR, |
80 |
_("Could not copy %s -> %s"), keyring, name ); |
81 |
free_if_alloc( keyring ); |
82 |
} |
83 |
} |
84 |
free_if_alloc( path ); |
85 |
} /* ask_for_backup */ |
86 |
|
87 |
|
88 |
static void |
89 |
fill_keytype_box( HWND dlg ) |
90 |
{ |
91 |
HWND cb = GetDlgItem( dlg, IDC_KEYGEN_KEYTYPE ); |
92 |
|
93 |
#define addstr( cb, str ) \ |
94 |
SendMessage( (cb), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)(str) ) |
95 |
addstr( cb, _("DSA and ELG (default)") ); |
96 |
addstr( cb, _("DSA and RSA") ); |
97 |
addstr( cb, _("DSA sign only") ); |
98 |
addstr( cb, _("RSA sign only") ); |
99 |
addstr( cb, _("RSA sign and encrypt") ); |
100 |
addstr( cb, _("RSA and RSA (PGP)") ); |
101 |
SendMessage( cb, CB_SETCURSEL, 0, 0 ); |
102 |
#undef addstr |
103 |
} /* fill_keytype_box */ |
104 |
|
105 |
|
106 |
static inline int |
107 |
get_keytype( HWND dlg ) |
108 |
{ |
109 |
HWND cb = GetDlgItem( dlg, IDC_KEYGEN_KEYTYPE ); |
110 |
return SendMessage( cb, CB_GETCURSEL, 0, 0 ) + 1; |
111 |
} /* get_keytype */ |
112 |
|
113 |
|
114 |
int |
115 |
keygen_check_date( SYSTEMTIME *st ) |
116 |
{ |
117 |
SYSTEMTIME t; |
118 |
|
119 |
GetSystemTime( &t ); |
120 |
if( st->wYear > t.wYear || st->wMonth > t.wMonth ) |
121 |
return 1; |
122 |
else if( st->wYear < t.wYear || st->wMonth < t.wMonth || st->wDay < t.wDay) |
123 |
return 0; |
124 |
return 1; |
125 |
} /* keygen_check_date */ |
126 |
|
127 |
|
128 |
BOOL CALLBACK |
129 |
keygen_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
130 |
{ |
131 |
static int hide = 1; |
132 |
static int act_expdate = 0; |
133 |
SYSTEMTIME st; |
134 |
gpgme_error_t err; |
135 |
char name[128] = {0}, email[128] = {0}, comment[128] = {0}; |
136 |
char pwd[128], pwd2[128]; |
137 |
char t[64], *expire = NULL; |
138 |
int bits, use_comment, keytype = 0; |
139 |
char * p; |
140 |
|
141 |
switch ( msg ) { |
142 |
case WM_INITDIALOG: |
143 |
hide = 1; |
144 |
#ifndef LANG_DE |
145 |
SetWindowText( dlg, _("Key Generation") ); |
146 |
SetDlgItemText( dlg, IDC_KEYGEN_INFO, |
147 |
_("NOTE: Key generation can be a lengthy process! Please wait until " |
148 |
"you get the message that key generation was finished.") ); |
149 |
SetDlgItemText( dlg, IDC_KEYGEN_SUBKEYINF, _("Subkey size in &bits")); |
150 |
SetDlgItemText( dlg, IDC_KEYGEN_NAMEINF, _("&Real name") ); |
151 |
SetDlgItemText( dlg, IDC_KEYGEN_COMMINF, _("&Comment (optional)") ); |
152 |
SetDlgItemText( dlg, IDC_KEYGEN_EMAILINF, _("Email &address") ); |
153 |
SetDlgItemText( dlg, IDC_KEYGEN_EXPINF, _("Key &expiration") ); |
154 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDINF, _("&Passphrase") ); |
155 |
SetDlgItemText( dlg, IDC_KEYGEN_REPWDINF, _("&Repeat passphrase") ); |
156 |
SetDlgItemText( dlg, IDC_KEYGEN_KEYTYPEINF, _("Key &type") ); |
157 |
SetDlgItemText (dlg, IDC_KEYGEN_CLEAR, _("&Never")); |
158 |
SetDlgItemText (dlg, IDC_KEYGEN_HIDEPWD, _("&Hide Typing")); |
159 |
#endif |
160 |
SetDlgItemInt( dlg, IDC_KEYGEN_SUBKEYBITS, 2048, FALSE ); |
161 |
CheckDlgButton (dlg, IDC_KEYGEN_HIDEPWD, BST_CHECKED); |
162 |
CheckDlgButton (dlg, IDC_KEYGEN_EXPNEVER, BST_CHECKED); |
163 |
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), FALSE); |
164 |
fill_keytype_box( dlg ); |
165 |
center_window( dlg ); |
166 |
SetForegroundWindow( dlg ); |
167 |
return TRUE; |
168 |
|
169 |
case WM_SYSCOMMAND: |
170 |
if( LOWORD( wparam ) == SC_CLOSE ) { |
171 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
172 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
173 |
EndDialog( dlg, TRUE ); |
174 |
} |
175 |
return FALSE; |
176 |
|
177 |
case WM_COMMAND: |
178 |
if (HIWORD (wparam) == BN_CLICKED && |
179 |
LOWORD (wparam) == IDC_KEYGEN_EXPNEVER) { |
180 |
act_expdate ^= 1; |
181 |
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), act_expdate); |
182 |
} |
183 |
if( HIWORD( wparam ) == BN_CLICKED |
184 |
&& LOWORD( wparam ) == IDC_KEYGEN_HIDEPWD ) { |
185 |
HWND hwnd_a = GetDlgItem( dlg, IDC_KEYGEN_PASSPHRASE ); |
186 |
HWND hwnd_b = GetDlgItem( dlg, IDC_KEYGEN_PWDCHECK ); |
187 |
hide ^= 1; |
188 |
SendMessage( hwnd_a, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 ); |
189 |
SetFocus( hwnd_a ); |
190 |
SendMessage( hwnd_b, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 ); |
191 |
SetFocus( hwnd_b ); |
192 |
|
193 |
} |
194 |
|
195 |
switch( LOWORD( wparam ) ) { |
196 |
case IDOK: |
197 |
bits = GetDlgItemInt (dlg, IDC_KEYGEN_SUBKEYBITS, NULL, FALSE); |
198 |
if (bits < 1024 || bits > 4096) { |
199 |
msg_box (dlg, _("Invalid value. Allowed values 1024-4096 bits."), |
200 |
_("Key Generation"), MB_ERR); |
201 |
return FALSE; |
202 |
} |
203 |
if (bits > 2048) { |
204 |
int id = msg_box (dlg, _("Do you really need such a large key?"), |
205 |
_("Key Generation"), MB_YESNO); |
206 |
if (id == IDNO) |
207 |
bits = 2048; |
208 |
} |
209 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_NAME, name, sizeof name - 1 ) ) { |
210 |
msg_box( dlg, _("Please enter the name."), _("Key Generation"), MB_ERR ); |
211 |
return FALSE; |
212 |
} |
213 |
if (strchr (name, '@')) { |
214 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
215 |
_("Key Generation"), MB_INFO); |
216 |
return FALSE; |
217 |
} |
218 |
if( !GetDlgItemText(dlg, IDC_KEYGEN_EMAIL, email, sizeof email -1 ) |
219 |
|| !strchr( email, '@') ) { |
220 |
msg_box( dlg, _("Please enter a valid email address."), |
221 |
_("Key Generation"), MB_ERR ); |
222 |
return FALSE; |
223 |
} |
224 |
use_comment = GetDlgItemText( dlg, IDC_KEYGEN_COMMENT, comment, sizeof comment -1 ); |
225 |
if( use_comment > 0 && strchr( comment, '@' ) ) { |
226 |
msg_box( dlg, _("Please do NOT enter the email address in the comment field."), |
227 |
_("Key Generation"), MB_INFO ); |
228 |
return FALSE; |
229 |
} |
230 |
keytype = get_keytype( dlg ); |
231 |
if (IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER)) |
232 |
expire = NULL; |
233 |
else { |
234 |
DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), &st); |
235 |
_snprintf (t, DIM (t)-1, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); |
236 |
expire = t; |
237 |
} |
238 |
|
239 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, pwd, sizeof pwd -1 ) ) { |
240 |
msg_box( dlg, _("Please enter the passphrase."), |
241 |
_("Key Generation"), MB_ERR ); |
242 |
return FALSE; |
243 |
} |
244 |
else if( strlen( pwd ) < 8 ) { |
245 |
int id = msg_box( dlg, _("Your passphrase should be at least 8 characters" |
246 |
" long\nand should contain non-alphabetic characters." |
247 |
"\n\nStill proceed?"), |
248 |
_("Key Generation"), MB_ICONWARNING|MB_YESNO ); |
249 |
if( id == IDNO ) { |
250 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
251 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
252 |
return FALSE; |
253 |
} |
254 |
} |
255 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, pwd2, sizeof pwd2 -1 ) ) { |
256 |
msg_box( dlg, _("Please repeat the passphrase."), |
257 |
_("Key Generation"), MB_ERR ); |
258 |
return FALSE; |
259 |
} |
260 |
if( strcmp( pwd, pwd2 ) ) { |
261 |
msg_box( dlg, _("Passphrases are NOT identical!" ), |
262 |
_("Key Generation"), MB_ERR ); |
263 |
memset( pwd, 0, sizeof pwd ); |
264 |
memset( pwd2, 0, sizeof pwd2 ); |
265 |
return FALSE; |
266 |
} |
267 |
if( is_8bit_string( pwd ) ) { |
268 |
msg_box( dlg, _("The passphrase contains 8-bit characters.\n" |
269 |
"It is not suggested to use charset specific characters."), |
270 |
_("Key Generation"), MB_ERR ); |
271 |
memset( pwd, 0, sizeof pwd ); |
272 |
memset( pwd2, 0, sizeof pwd2 ); |
273 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
274 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
275 |
return FALSE; |
276 |
} |
277 |
|
278 |
if( !use_comment && !strlen( comment ) ) { |
279 |
char *utf8_name; |
280 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
281 |
if( !utf8_name ) |
282 |
BUG( dlg ); |
283 |
p = gpgme_genkey_params( keytype, bits, utf8_name, NULL, email, expire, pwd ); |
284 |
free( utf8_name ); |
285 |
} |
286 |
else { |
287 |
char *utf8_name, *utf8_comment; |
288 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
289 |
utf8_comment = wincp_to_utf8 (comment, strlen (comment)); |
290 |
if( !utf8_name || !utf8_comment ) |
291 |
BUG( dlg ); |
292 |
p = gpgme_genkey_params( keytype, bits, utf8_name, utf8_comment, email, expire, pwd ); |
293 |
free( utf8_name ); |
294 |
free( utf8_comment ); |
295 |
} |
296 |
keygen_cb_dlg_create( ); |
297 |
err = gpgme_op_genkey_auto( p, keygen_cb, NULL ); |
298 |
memset( pwd, 0, sizeof pwd ); |
299 |
memset( pwd2, 0, sizeof pwd2 ); |
300 |
if( p ) { |
301 |
memset( p, 0, strlen( p ) ); /* burn the passphrase! */ |
302 |
free( p ); |
303 |
} |
304 |
keygen_cb_dlg_destroy( ); |
305 |
keygen_cb( NULL, NULL, 0, 0, 0 ); /* flush */ |
306 |
if( err ) { |
307 |
msg_box( dlg, gpgme_strerror( err ), _("Key Generation"), MB_ERR ); |
308 |
return FALSE; |
309 |
} |
310 |
status_box( dlg, _("Key Generation completed"), _("GnuPG Status") ); |
311 |
keycache_set_reload( 1 ); |
312 |
clear_dlg_fields( dlg ); |
313 |
ask_for_backup( dlg ); |
314 |
EndDialog( dlg, TRUE ); |
315 |
return TRUE; |
316 |
|
317 |
case IDCANCEL: |
318 |
SetDlgItemText (dlg, IDC_KEYGEN_PASSPHRASE, ""); |
319 |
SetDlgItemText (dlg, IDC_KEYGEN_PWDCHECK, ""); |
320 |
EndDialog (dlg, FALSE); |
321 |
return FALSE; |
322 |
} |
323 |
break; |
324 |
} |
325 |
|
326 |
return FALSE; |
327 |
} /* keygen_dlg_proc */ |
328 |
|
329 |
|
330 |
BOOL CALLBACK |
331 |
keygen_wizard_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
332 |
{ |
333 |
static key_wizard_s * ctx; |
334 |
static int pubkey_algo = GPGME_KEYGEN_DSA_ELG; |
335 |
gpgme_error_t err; |
336 |
char name[128], email[128]; |
337 |
char * utf8_name, * p; |
338 |
char * pass = NULL; |
339 |
int cancel = 0; |
340 |
|
341 |
|
342 |
switch( msg ) { |
343 |
case WM_INITDIALOG: |
344 |
ctx = (key_wizard_s *)lparam; |
345 |
if (!ctx || (ctx && ctx->interactive == 0)) |
346 |
EnableWindow (GetDlgItem (dlg, IDC_KEYWIZARD_EXPERT), FALSE); |
347 |
SetDlgItemText (dlg, IDC_KEYWIZARD_USERSA, _("&Prefer RSA keys")); |
348 |
SetDlgItemText (dlg, IDC_KEYWIZARD_NAMEINF, _("Real name:")); |
349 |
SetDlgItemText (dlg, IDC_KEYWIZARD_EMAILINF, _("Email address:")); |
350 |
SetDlgItemText (dlg, IDC_KEYWIZARD_TITLEINF, _("Name and E-Mail Assignment")); |
351 |
SetDlgItemText (dlg, IDC_KEYWIZARD_TEXT1INF, _("Every key pair must have a name associated with it. The name and\nemail address let your correspondents that your public key they are\nusing belongs to us.")); |
352 |
SetDlgItemText (dlg, IDC_KEYWIZARD_TEXT2INF, _("By accosiating an email address with your key pair, you will enable WinPT to assist your correspondents in selecting the correct public\nkey when communicating with you.")); |
353 |
SetForegroundWindow (dlg); |
354 |
center_window (dlg); |
355 |
break; |
356 |
|
357 |
case WM_SYSCOMMAND: |
358 |
if( LOWORD( wparam ) == SC_CLOSE ) |
359 |
EndDialog( dlg, FALSE ); |
360 |
|
361 |
case WM_COMMAND: |
362 |
switch( LOWORD( wparam ) ) { |
363 |
case IDC_KEYWIZARD_EXPERT: |
364 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYGEN, dlg, |
365 |
keygen_dlg_proc, NULL); |
366 |
EndDialog (dlg, TRUE); |
367 |
break; |
368 |
|
369 |
case IDOK: |
370 |
if( !GetDlgItemText( dlg, IDC_KEYWIZARD_NAME, name, sizeof name-1 ) ) { |
371 |
msg_box( dlg, _("Please enter the name."), |
372 |
_("Key Generation Wizard"), MB_ERR ); |
373 |
return FALSE; |
374 |
} |
375 |
if (strchr (name, '@')) { |
376 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
377 |
_("Key Generation Wizard"), MB_ERR); |
378 |
return FALSE; |
379 |
} |
380 |
if( !GetDlgItemText( dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 ) |
381 |
|| !strchr( email, '@' ) ) { |
382 |
msg_box( dlg, _("Please enter a valid email address."), |
383 |
_("Key Generation Wizard"), MB_ERR ); |
384 |
return FALSE; |
385 |
} |
386 |
pass = request_passphrase2 (_("Key Generation"), &cancel); |
387 |
if (cancel) |
388 |
return FALSE; |
389 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
390 |
if( !utf8_name ) |
391 |
BUG( NULL ); |
392 |
if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA)) |
393 |
pubkey_algo = GPGME_KEYGEN_DSA_RSA; |
394 |
p = gpgme_genkey_params (pubkey_algo, 2048, utf8_name, |
395 |
NULL, email, NULL, pass); |
396 |
free( utf8_name ); |
397 |
keygen_cb_dlg_create(); |
398 |
err = gpgme_op_genkey_auto( p, keygen_cb, NULL ); |
399 |
keygen_cb_dlg_destroy(); |
400 |
keygen_cb( NULL, NULL, 0, 0, 0 ); |
401 |
if( p ) { |
402 |
memset( p, 0, strlen( p ) ); |
403 |
free( p ); |
404 |
} |
405 |
sfree_if_alloc (pass); |
406 |
if( err ) { |
407 |
msg_box( dlg, gpgme_strerror( err ), _("Key Generation Wizard"), MB_ERR ); |
408 |
return FALSE; |
409 |
} |
410 |
status_box( dlg, _("Key Generation completed"), _("GnuPG Status") ); |
411 |
keycache_set_reload( 1 ); |
412 |
EndDialog( dlg, TRUE ); |
413 |
break; |
414 |
|
415 |
case IDCANCEL: |
416 |
EndDialog( dlg, FALSE ); |
417 |
break; |
418 |
} |
419 |
break; |
420 |
} |
421 |
return FALSE; |
422 |
} |