1 |
/* wptKeygenDlg.cpp - Key Generation dialog |
2 |
* Copyright (C) 2000-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 "wptTypes.h" |
28 |
#include "wptNLS.h" |
29 |
#include "wptGPG.h" |
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptContext.h" /* for passphrase_s */ |
32 |
#include "wptDlgs.h" |
33 |
#include "wptW32API.h" |
34 |
#include "wptVersion.h" |
35 |
#include "wptErrors.h" |
36 |
#include "wptUTF8.h" |
37 |
|
38 |
/* All valid key generation combination. */ |
39 |
enum gpg_keytype_t { |
40 |
GPG_KEYGEN_NONE = 0, |
41 |
GPG_KEYGEN_DSA_ELG = 1, |
42 |
GPG_KEYGEN_DSA_RSA = 2, |
43 |
GPG_KEYGEN_DSA_SIG = 3, |
44 |
GPG_KEYGEN_RSA_SIG = 4, |
45 |
GPG_KEYGEN_RSA = 5, |
46 |
GPG_KEYGEN_RSA_RSA = 6 /*PGP*/ |
47 |
}; |
48 |
|
49 |
|
50 |
static const char key_params[] = |
51 |
"<GnupgKeyParms format=\"internal\">\n" |
52 |
"Key-Type: %s\n" |
53 |
"Key-Usage: sign\n" |
54 |
"Key-Length: %d\n" |
55 |
"Subkey-Type: %s\n" |
56 |
"Subkey-Length: %d\n" |
57 |
"Name-Real: %s\n" |
58 |
"Name-Email: %s\n" |
59 |
"Expire-Date: %s\n" |
60 |
"Passphrase: %s\n" |
61 |
"</GnupgKeyParms>\n"; |
62 |
|
63 |
static const char key_params_with_comment[] = |
64 |
"<GnupgKeyParms format=\"internal\">\n" |
65 |
"Key-Type: %s\n" |
66 |
"Key-Usage: sign\n" |
67 |
"Key-Length: %d\n" |
68 |
"Subkey-Type: %s\n" |
69 |
"Subkey-Length: %d\n" |
70 |
"Name-Real: %s\n" |
71 |
"Name-Comment: %s\n" |
72 |
"Name-Email: %s\n" |
73 |
"Expire-Date: %s\n" |
74 |
"Passphrase: %s\n" |
75 |
"</GnupgKeyParms>\n"; |
76 |
|
77 |
static const char key_params_one[] = |
78 |
"<GnupgKeyParms format=\"internal\">\n" |
79 |
"Key-Type: %s\n" |
80 |
"Key-Length: %d\n" |
81 |
"Key-Usage: %s\n" |
82 |
"Name-Real: %s\n" |
83 |
"Name-Email: %s\n" |
84 |
"Expire-Date: %s\n" |
85 |
"Passphrase: %s\n" |
86 |
"</GnupgKeyParms>\n"; |
87 |
|
88 |
static const char key_params_one_with_comment[] = |
89 |
"<GnupgKeyParms format=\"internal\">\n" |
90 |
"Key-Type: %s\n" |
91 |
"Key-Length: %d\n" |
92 |
"Key-Usage: %s\n" |
93 |
"Name-Real: %s\n" |
94 |
"Name-Email: %s\n" |
95 |
"Name-Comment: %s\n" |
96 |
"Expire-Date: %s\n" |
97 |
"Passphrase: %s\n" |
98 |
"</GnupgKeyParms>\n"; |
99 |
|
100 |
|
101 |
/* Generate the GPG specific genkey params with the given information. |
102 |
@keytype: See valid combinations. |
103 |
@bits: Length in bits. |
104 |
@user: user-ID name |
105 |
@comment: comment for the user-ID. |
106 |
@email: email address. |
107 |
@expdata: date of expiration or NULL. |
108 |
@passphrase: the actual passphrase. |
109 |
Return value: the gen. params. */ |
110 |
static char* |
111 |
gpg_genkey_params (int keytype, int bits, |
112 |
const char *user, const char *comment, const char *email, |
113 |
const char *expdate, const char *passphrase) |
114 |
{ |
115 |
char *p = NULL; |
116 |
int addsize = strlen ("sign encrypt"); |
117 |
int size = 0; |
118 |
|
119 |
if (keytype == GPG_KEYGEN_NONE) |
120 |
return NULL; |
121 |
|
122 |
if( comment && *comment ) { |
123 |
size = strlen (key_params_with_comment) |
124 |
+ 16 |
125 |
+ strlen( user ) |
126 |
+ addsize |
127 |
+ strlen( comment ) |
128 |
+ strlen( email ) |
129 |
+ strlen( passphrase ) + 32; |
130 |
} |
131 |
else { |
132 |
size = strlen( key_params ) |
133 |
+ 16 |
134 |
+ strlen( user ) |
135 |
+ strlen( email ) |
136 |
+ strlen( passphrase ) |
137 |
+ addsize |
138 |
+ 32; |
139 |
} |
140 |
if( expdate ) |
141 |
size += strlen( expdate ); |
142 |
p = (char *)malloc( size+1 ); |
143 |
if( !p ) |
144 |
return NULL; |
145 |
if( comment && *comment ) { |
146 |
switch( keytype ) { |
147 |
case GPG_KEYGEN_DSA_ELG: |
148 |
sprintf (p, key_params_with_comment, |
149 |
"DSA", 1024, "ELG-E", bits, user, comment, email, |
150 |
expdate ? expdate : "0", passphrase); |
151 |
break; |
152 |
|
153 |
case GPG_KEYGEN_DSA_RSA: |
154 |
sprintf( p, key_params_with_comment, |
155 |
"DSA", 1024, "RSA", bits, user, comment, email, |
156 |
expdate ? expdate : "0", passphrase ); |
157 |
break; |
158 |
|
159 |
case GPG_KEYGEN_DSA_SIG: |
160 |
sprintf( p, key_params_one_with_comment, |
161 |
"DSA", 1024, "sign", |
162 |
user, comment, email, |
163 |
expdate ? expdate : "0", passphrase ); |
164 |
break; |
165 |
|
166 |
case GPG_KEYGEN_RSA_SIG: |
167 |
sprintf( p, key_params_one_with_comment, |
168 |
"RSA", bits, "sign", |
169 |
user, comment, email, |
170 |
expdate ? expdate : "0", passphrase ); |
171 |
break; |
172 |
|
173 |
case GPG_KEYGEN_RSA: |
174 |
sprintf( p, key_params_one_with_comment, |
175 |
"RSA", bits, "sign encrypt", |
176 |
user, comment, email, |
177 |
expdate ? expdate : "0", passphrase ); |
178 |
break; |
179 |
|
180 |
case GPG_KEYGEN_RSA_RSA: |
181 |
sprintf( p, key_params_with_comment, |
182 |
"RSA", bits, "RSA", bits, user, comment, email, |
183 |
expdate? expdate : "0", passphrase ); |
184 |
break; |
185 |
|
186 |
default: |
187 |
if (p) |
188 |
free (p); |
189 |
p = NULL; |
190 |
break; |
191 |
} |
192 |
} |
193 |
else { |
194 |
switch ( keytype ) { |
195 |
case GPG_KEYGEN_DSA_ELG: |
196 |
sprintf( p, key_params, |
197 |
"DSA", 1024, "ELG-E", bits, user, email, |
198 |
expdate ? expdate : "0", passphrase ); |
199 |
break; |
200 |
|
201 |
case GPG_KEYGEN_DSA_RSA: |
202 |
sprintf( p, key_params, |
203 |
"DSA", 1024, "RSA", bits, user, email, |
204 |
expdate ? expdate : "0", passphrase ); |
205 |
break; |
206 |
|
207 |
case GPG_KEYGEN_DSA_SIG: |
208 |
sprintf( p, key_params_one, |
209 |
"DSA", 1024, "sign", |
210 |
user, email, |
211 |
expdate ? expdate : "0", passphrase ); |
212 |
break; |
213 |
|
214 |
case GPG_KEYGEN_RSA_SIG: |
215 |
sprintf( p, key_params_one, |
216 |
"RSA", bits, "sign", |
217 |
user, email, |
218 |
expdate ? expdate : "0", passphrase ); |
219 |
break; |
220 |
|
221 |
case GPG_KEYGEN_RSA: |
222 |
sprintf( p, key_params_one, |
223 |
"RSA", bits, "sign encrypt", |
224 |
user, email, |
225 |
expdate ? expdate : "0", passphrase ); |
226 |
break; |
227 |
|
228 |
case GPG_KEYGEN_RSA_RSA: |
229 |
sprintf( p, key_params, |
230 |
"RSA", bits, "RSA", bits, user, email, |
231 |
expdate? expdate : "0", passphrase ); |
232 |
break; |
233 |
|
234 |
default: |
235 |
if (p) |
236 |
free (p); |
237 |
p = NULL; |
238 |
break; |
239 |
} |
240 |
} |
241 |
return p; |
242 |
} |
243 |
|
244 |
|
245 |
/* Generate a key with the given params @params. @prog_cb is a user defined |
246 |
progress callback which is called during the generation. |
247 |
@fpr will store the fingerprint of the generated key. |
248 |
Return value: 0 on success. */ |
249 |
gpgme_error_t |
250 |
gpg_genkey (const char *params, gpgme_progress_cb_t prog_cb, char **fpr) |
251 |
{ |
252 |
gpgme_error_t err = 0; |
253 |
gpgme_ctx_t ctx; |
254 |
gpgme_genkey_result_t res; |
255 |
|
256 |
err = gpgme_new(&ctx); |
257 |
if (err) |
258 |
return err; |
259 |
if (prog_cb) |
260 |
gpgme_set_progress_cb (ctx, prog_cb, NULL); |
261 |
err = gpgme_op_genkey (ctx, params, NULL, NULL); |
262 |
if (!err) { |
263 |
res = gpgme_op_genkey_result (ctx); |
264 |
*fpr = res->fpr? strdup (res->fpr) : NULL; |
265 |
} |
266 |
gpgme_release (ctx); |
267 |
return err; |
268 |
} |
269 |
|
270 |
|
271 |
|
272 |
/* Clear all dialog fields. */ |
273 |
static void |
274 |
clear_dlg_fields (HWND dlg) |
275 |
{ |
276 |
SetDlgItemText (dlg, IDC_KEYGEN_SUBKEYBITS, ""); |
277 |
SetDlgItemText (dlg, IDC_KEYGEN_NAME, ""); |
278 |
SetDlgItemText (dlg, IDC_KEYGEN_EMAIL, ""); |
279 |
SetDlgItemText (dlg, IDC_KEYGEN_COMMENT, ""); |
280 |
SetDlgItemText (dlg, IDC_KEYGEN_EXPDATE, ""); |
281 |
SetDlgItemText (dlg, IDC_KEYGEN_PASSPHRASE, ""); |
282 |
SetDlgItemText (dlg, IDC_KEYGEN_PWDCHECK, ""); |
283 |
} |
284 |
|
285 |
|
286 |
/* Ask the user if a keyring backup is wanted and if so, |
287 |
backup both keyrings to the selected folder. @dlg is |
288 |
the handle of the parent window.*/ |
289 |
static void |
290 |
backup_keyrings (HWND dlg) |
291 |
{ |
292 |
int id; |
293 |
char *path = NULL, *keyring = NULL; |
294 |
const char * name; |
295 |
|
296 |
path = get_gnupg_path (); |
297 |
if (!path) |
298 |
BUG (dlg); |
299 |
id = msg_box (dlg, |
300 |
_("It is STRONGLY recommend that you backup your keyrings because they both " |
301 |
"contain VERY important data.\nRemember that your hard disk can crash or the " |
302 |
"files can be deleted by accident; so it is a good\nidea to store them on " |
303 |
"a different mass stoarge like a floppy or CDR!\n\n" |
304 |
"Backup your keyrings now?"), |
305 |
_("WARNING - Important hint" ), MB_YESNO); |
306 |
if (id == IDYES) { |
307 |
name = get_filename_dlg( dlg, 1, _("Destination for Public Keyring"), NULL, "pubring.gpg" ); |
308 |
if( name ) { |
309 |
keyring = make_filename( path, "pubring", "gpg" ); |
310 |
if( !CopyFile( keyring, name, FALSE ) ) |
311 |
log_box( _("Key Generation"), MB_ERR, |
312 |
_("Could not copy %s -> %s"), keyring, name ); |
313 |
free_if_alloc( keyring ); |
314 |
} |
315 |
name = get_filename_dlg( dlg, 1, _("Destination for Secret Keyring"), NULL, "secring.gpg" ); |
316 |
if( name ) { |
317 |
keyring = make_filename( path, "secring", "gpg" ); |
318 |
if( !CopyFile( keyring, name, FALSE ) ) |
319 |
log_box( _("Key Generation"), MB_ERR, |
320 |
_("Could not copy %s -> %s"), keyring, name ); |
321 |
free_if_alloc( keyring ); |
322 |
} |
323 |
} |
324 |
free_if_alloc( path ); |
325 |
} |
326 |
|
327 |
|
328 |
/* Fill in all valid GPG algorithms. */ |
329 |
static void |
330 |
fill_keytype_box (HWND dlg) |
331 |
{ |
332 |
HWND cb = GetDlgItem (dlg, IDC_KEYGEN_KEYTYPE ); |
333 |
|
334 |
#define addstr( cb, str ) \ |
335 |
SendMessage( (cb), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)(str) ) |
336 |
addstr( cb, _("DSA and ELG (default)") ); |
337 |
addstr( cb, _("DSA and RSA") ); |
338 |
addstr( cb, _("DSA sign only") ); |
339 |
addstr( cb, _("RSA sign only") ); |
340 |
addstr( cb, _("RSA sign and encrypt") ); |
341 |
addstr( cb, _("RSA and RSA (PGP)") ); |
342 |
SendMessage( cb, CB_SETCURSEL, 0, 0 ); |
343 |
#undef addstr |
344 |
} |
345 |
|
346 |
|
347 |
/* Check that the given date lies not in the past. |
348 |
Return value: 1 on success. */ |
349 |
int |
350 |
keygen_check_date (SYSTEMTIME *st) |
351 |
{ |
352 |
SYSTEMTIME t; |
353 |
|
354 |
GetSystemTime (&t); |
355 |
if (st->wYear > t.wYear || st->wMonth > t.wMonth) |
356 |
return 1; |
357 |
else if (st->wYear < t.wYear || st->wMonth < t.wMonth || st->wDay < t.wDay) |
358 |
return 0; |
359 |
return 1; |
360 |
} |
361 |
|
362 |
|
363 |
/* Dialog box procedure for key generation. */ |
364 |
BOOL CALLBACK |
365 |
keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
366 |
{ |
367 |
static genkey_s *ctx; |
368 |
SYSTEMTIME st; |
369 |
gpgme_error_t err; |
370 |
char name[128] = {0}, email[128] = {0}, comment[128] = {0}; |
371 |
char pwd[128], pwd2[128]; |
372 |
char t[64], *expire = NULL, *fpr=NULL; |
373 |
int bits, use_comment, keytype = 0; |
374 |
char * p; |
375 |
|
376 |
switch ( msg ) { |
377 |
case WM_INITDIALOG: |
378 |
if (lparam != NULL) |
379 |
ctx = (genkey_s *)lparam; |
380 |
SetWindowText( dlg, _("Key Generation") ); |
381 |
SetDlgItemText( dlg, IDC_KEYGEN_INFO, |
382 |
_("NOTE: Key generation can be a lengthy process! Please wait until " |
383 |
"you get the message that key generation was finished.") ); |
384 |
SetDlgItemText( dlg, IDC_KEYGEN_SUBKEYINF, _("Subkey size in &bits")); |
385 |
SetDlgItemText( dlg, IDC_KEYGEN_NAMEINF, _("&Real name") ); |
386 |
SetDlgItemText( dlg, IDC_KEYGEN_COMMINF, _("&Comment (optional)") ); |
387 |
SetDlgItemText( dlg, IDC_KEYGEN_EMAILINF, _("Email &address") ); |
388 |
SetDlgItemText( dlg, IDC_KEYGEN_EXPINF, _("&Expire date")); |
389 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDINF, _("&Passphrase") ); |
390 |
SetDlgItemText( dlg, IDC_KEYGEN_REPWDINF, _("&Repeat passphrase") ); |
391 |
SetDlgItemText( dlg, IDC_KEYGEN_KEYTYPEINF, _("Key &type") ); |
392 |
SetDlgItemText (dlg, IDC_KEYGEN_EXPNEVER, _("&Never")); |
393 |
SetDlgItemText (dlg, IDC_KEYGEN_HIDEPWD, _("&Hide Typing")); |
394 |
|
395 |
SetDlgItemInt (dlg, IDC_KEYGEN_SUBKEYBITS, 2048, FALSE); |
396 |
CheckDlgButton (dlg, IDC_KEYGEN_HIDEPWD, BST_CHECKED); |
397 |
CheckDlgButton (dlg, IDC_KEYGEN_EXPNEVER, BST_CHECKED); |
398 |
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), FALSE); |
399 |
fill_keytype_box (dlg); |
400 |
center_window (dlg, NULL); |
401 |
SetForegroundWindow (dlg); |
402 |
return TRUE; |
403 |
|
404 |
case WM_SYSCOMMAND: |
405 |
if (LOWORD (wparam) == SC_CLOSE) { |
406 |
SetDlgItemText (dlg, IDC_KEYGEN_PASSPHRASE, ""); |
407 |
SetDlgItemText (dlg, IDC_KEYGEN_PWDCHECK, ""); |
408 |
EndDialog (dlg, TRUE); |
409 |
} |
410 |
return FALSE; |
411 |
|
412 |
case WM_COMMAND: |
413 |
if (HIWORD (wparam) == BN_CLICKED && |
414 |
LOWORD (wparam) == IDC_KEYGEN_EXPNEVER) { |
415 |
int never = IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER); |
416 |
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), !never); |
417 |
} |
418 |
if (HIWORD (wparam) == BN_CLICKED |
419 |
&& LOWORD (wparam) == IDC_KEYGEN_HIDEPWD) { |
420 |
HWND hwnd_a = GetDlgItem (dlg, IDC_KEYGEN_PASSPHRASE); |
421 |
HWND hwnd_b = GetDlgItem (dlg, IDC_KEYGEN_PWDCHECK); |
422 |
int hide = IsDlgButtonChecked (dlg, IDC_KEYGEN_HIDEPWD); |
423 |
SendMessage (hwnd_a, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
424 |
SetFocus (hwnd_a); |
425 |
SendMessage (hwnd_b, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
426 |
SetFocus (hwnd_b); |
427 |
} |
428 |
|
429 |
switch( LOWORD( wparam ) ) { |
430 |
case IDOK: |
431 |
bits = GetDlgItemInt (dlg, IDC_KEYGEN_SUBKEYBITS, NULL, FALSE); |
432 |
if (bits < 1024 || bits > 4096) { |
433 |
msg_box (dlg, _("Invalid value. Allowed values 1024-4096 bits."), |
434 |
_("Key Generation"), MB_ERR); |
435 |
return FALSE; |
436 |
} |
437 |
if (bits > DFAULT_KEYSIZE) { |
438 |
int id = msg_box (dlg, _("Do you really need such a large key?"), |
439 |
_("Key Generation"), MB_YESNO); |
440 |
if (id == IDNO) |
441 |
bits = DFAULT_KEYSIZE; |
442 |
} |
443 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_NAME, name, sizeof name - 1 ) ) { |
444 |
msg_box( dlg, _("Please enter the name."), _("Key Generation"), MB_ERR ); |
445 |
return FALSE; |
446 |
} |
447 |
if (strchr (name, '@')) { |
448 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
449 |
_("Key Generation"), MB_INFO); |
450 |
return FALSE; |
451 |
} |
452 |
if( !GetDlgItemText(dlg, IDC_KEYGEN_EMAIL, email, sizeof email -1 ) |
453 |
|| !strchr( email, '@') ) { |
454 |
msg_box( dlg, _("Please enter a valid email address."), |
455 |
_("Key Generation"), MB_ERR ); |
456 |
return FALSE; |
457 |
} |
458 |
use_comment = GetDlgItemText( dlg, IDC_KEYGEN_COMMENT, comment, sizeof comment -1 ); |
459 |
if( use_comment > 0 && strchr( comment, '@' ) ) { |
460 |
msg_box( dlg, _("Please do NOT enter the email address in the comment field."), |
461 |
_("Key Generation"), MB_INFO ); |
462 |
return FALSE; |
463 |
} |
464 |
keytype = SendDlgItemMessage (dlg, IDC_KEYGEN_KEYTYPE, CB_GETCURSEL, 0, 0) + 1; |
465 |
if (IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER)) |
466 |
expire = NULL; |
467 |
else { |
468 |
DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), &st); |
469 |
_snprintf (t, DIM (t)-1, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); |
470 |
expire = t; |
471 |
} |
472 |
|
473 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, pwd, sizeof pwd -1 ) ) { |
474 |
msg_box( dlg, _("Please enter the passphrase."), |
475 |
_("Key Generation"), MB_ERR ); |
476 |
return FALSE; |
477 |
} |
478 |
else if (check_passwd_quality (pwd, 0)) { |
479 |
int id = msg_box( dlg, _("Your passphrase should be at least 8 characters" |
480 |
" long\nand should contain non-alphabetic characters." |
481 |
"\n\nStill proceed?"), |
482 |
_("Key Generation"), MB_ICONWARNING|MB_YESNO ); |
483 |
if( id == IDNO ) { |
484 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
485 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
486 |
return FALSE; |
487 |
} |
488 |
} |
489 |
if( !GetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, pwd2, sizeof pwd2 -1 ) ) { |
490 |
msg_box( dlg, _("Please repeat the passphrase."), |
491 |
_("Key Generation"), MB_ERR ); |
492 |
return FALSE; |
493 |
} |
494 |
if( strcmp( pwd, pwd2 ) ) { |
495 |
msg_box( dlg, _("Passphrases are NOT identical!" ), |
496 |
_("Key Generation"), MB_ERR ); |
497 |
wipememory (pwd, sizeof (pwd)); |
498 |
wipememory (pwd2, sizeof (pwd2)); |
499 |
return FALSE; |
500 |
} |
501 |
if( is_8bit_string( pwd ) ) { |
502 |
msg_box( dlg, _("The passphrase contains 8-bit characters.\n" |
503 |
"It is not suggested to use charset specific characters."), |
504 |
_("Key Generation"), MB_ERR ); |
505 |
wipememory (pwd, sizeof (pwd)); |
506 |
wipememory (pwd2, sizeof (pwd2)); |
507 |
SetDlgItemText( dlg, IDC_KEYGEN_PASSPHRASE, "" ); |
508 |
SetDlgItemText( dlg, IDC_KEYGEN_PWDCHECK, "" ); |
509 |
return FALSE; |
510 |
} |
511 |
|
512 |
if( !use_comment && !strlen( comment ) ) { |
513 |
char *utf8_name; |
514 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
515 |
if( !utf8_name ) |
516 |
BUG( dlg ); |
517 |
p = gpg_genkey_params( keytype, bits, utf8_name, NULL, email, expire, pwd ); |
518 |
free( utf8_name ); |
519 |
} |
520 |
else { |
521 |
char *utf8_name, *utf8_comment; |
522 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
523 |
utf8_comment = wincp_to_utf8 (comment, strlen (comment)); |
524 |
if( !utf8_name || !utf8_comment ) |
525 |
BUG( dlg ); |
526 |
p = gpg_genkey_params( keytype, bits, utf8_name, utf8_comment, email, expire, pwd ); |
527 |
free( utf8_name ); |
528 |
free( utf8_comment ); |
529 |
} |
530 |
keygen_cb_dlg_create( ); |
531 |
err = gpg_genkey (p, keygen_cb, &fpr); |
532 |
wipememory (pwd, sizeof (pwd)); |
533 |
wipememory (pwd2, sizeof (pwd2)); |
534 |
if( p ) { |
535 |
wipememory (p, strlen (p)); /* burn the passphrase! */ |
536 |
free (p); |
537 |
} |
538 |
keygen_cb_dlg_destroy (); |
539 |
keygen_cb (NULL, NULL, 0, 0, 0); /* flush */ |
540 |
if( err ) { |
541 |
if (fpr) |
542 |
free (fpr); |
543 |
msg_box (dlg, gpgme_strerror( err ), _("Key Generation"), MB_ERR); |
544 |
return FALSE; |
545 |
} |
546 |
status_box( dlg, _("Key Generation completed"), _("GnuPG Status") ); |
547 |
|
548 |
keycache_update (0, fpr); |
549 |
keycache_update (1, fpr); |
550 |
if (ctx->first_start == 0 && ctx != NULL) |
551 |
get_pubkey (fpr, &ctx->newkey); |
552 |
if (fpr) |
553 |
free (fpr); |
554 |
|
555 |
clear_dlg_fields (dlg); |
556 |
backup_keyrings (dlg); |
557 |
EndDialog (dlg, TRUE); |
558 |
return TRUE; |
559 |
|
560 |
case IDCANCEL: |
561 |
SetDlgItemText (dlg, IDC_KEYGEN_PASSPHRASE, ""); |
562 |
SetDlgItemText (dlg, IDC_KEYGEN_PWDCHECK, ""); |
563 |
EndDialog (dlg, FALSE); |
564 |
return FALSE; |
565 |
} |
566 |
break; |
567 |
} |
568 |
|
569 |
return FALSE; |
570 |
} |
571 |
|
572 |
|
573 |
BOOL CALLBACK |
574 |
keygen_wizard_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
575 |
{ |
576 |
static genkey_s *ctx; |
577 |
static int pubkey_algo = GPG_KEYGEN_DSA_ELG; |
578 |
gpgme_error_t err; |
579 |
char name[128], email[128]; |
580 |
char * utf8_name, * p, *fpr=NULL; |
581 |
char * pass = NULL; |
582 |
int cancel = 0; |
583 |
|
584 |
|
585 |
switch( msg ) { |
586 |
case WM_INITDIALOG: |
587 |
ctx = (genkey_s *)lparam; |
588 |
if (!ctx || (ctx && ctx->interactive == 0)) |
589 |
EnableWindow (GetDlgItem (dlg, IDC_KEYWIZARD_EXPERT), FALSE); |
590 |
SetDlgItemText (dlg, IDC_KEYWIZARD_USERSA, _("&Prefer RSA keys")); |
591 |
SetDlgItemText (dlg, IDC_KEYWIZARD_NAMEINF, _("Real name:")); |
592 |
SetDlgItemText (dlg, IDC_KEYWIZARD_EMAILINF, _("Email address:")); |
593 |
SetDlgItemText (dlg, IDC_KEYWIZARD_TITLEINF, _("Name and E-Mail Assignment")); |
594 |
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.")); |
595 |
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.")); |
596 |
SetWindowText (dlg, _("Key Generation Wizard")); |
597 |
SetForegroundWindow (dlg); |
598 |
center_window (dlg, NULL); |
599 |
break; |
600 |
|
601 |
case WM_SYSCOMMAND: |
602 |
if( LOWORD( wparam ) == SC_CLOSE ) |
603 |
EndDialog( dlg, FALSE ); |
604 |
|
605 |
case WM_COMMAND: |
606 |
switch( LOWORD( wparam ) ) { |
607 |
case IDC_KEYWIZARD_EXPERT: |
608 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYGEN, dlg, |
609 |
keygen_dlg_proc, NULL); |
610 |
EndDialog (dlg, TRUE); |
611 |
break; |
612 |
|
613 |
case IDOK: |
614 |
if( !GetDlgItemText( dlg, IDC_KEYWIZARD_NAME, name, sizeof name-1 ) ) { |
615 |
msg_box( dlg, _("Please enter the name."), |
616 |
_("Key Generation Wizard"), MB_ERR ); |
617 |
return FALSE; |
618 |
} |
619 |
if (strchr (name, '@')) { |
620 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
621 |
_("Key Generation Wizard"), MB_WARN); |
622 |
return FALSE; |
623 |
} |
624 |
if( !GetDlgItemText( dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 ) |
625 |
|| !strchr( email, '@' ) ) { |
626 |
msg_box( dlg, _("Please enter a valid email address."), |
627 |
_("Key Generation Wizard"), MB_ERR ); |
628 |
return FALSE; |
629 |
} |
630 |
if (strchr (email, '<') || strchr (email, '>')) { |
631 |
msg_box (dlg, _("Please do not add '<' or '>' to the email address."), |
632 |
_("Key Generation Wizard"), MB_WARN); |
633 |
return FALSE; |
634 |
} |
635 |
pass = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel); |
636 |
if (cancel) |
637 |
return FALSE; |
638 |
utf8_name = wincp_to_utf8 (name, strlen (name)); |
639 |
if( !utf8_name ) |
640 |
BUG( NULL ); |
641 |
if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA)) |
642 |
pubkey_algo = GPG_KEYGEN_DSA_RSA; |
643 |
p = gpg_genkey_params (pubkey_algo, DFAULT_KEYSIZE, utf8_name, |
644 |
NULL, email, NULL, pass); |
645 |
free( utf8_name ); |
646 |
keygen_cb_dlg_create(); |
647 |
err = gpg_genkey (p, keygen_cb, &fpr); |
648 |
keygen_cb_dlg_destroy(); |
649 |
keygen_cb( NULL, NULL, 0, 0, 0 ); |
650 |
if( p ) { |
651 |
memset( p, 0, strlen( p ) ); |
652 |
free( p ); |
653 |
} |
654 |
sfree_if_alloc (pass); |
655 |
if( err ) { |
656 |
msg_box( dlg, gpgme_strerror( err ), _("Key Generation Wizard"), MB_ERR ); |
657 |
if (fpr) |
658 |
free (fpr); |
659 |
return FALSE; |
660 |
} |
661 |
status_box( dlg, _("Key Generation completed"), _("GnuPG Status") ); |
662 |
|
663 |
keycache_update (0, fpr); |
664 |
keycache_update (1, fpr); |
665 |
if (ctx->first_start == 0 && ctx != NULL) |
666 |
get_pubkey (fpr, &ctx->newkey); |
667 |
if (fpr) |
668 |
free (fpr); |
669 |
backup_keyrings (dlg); |
670 |
EndDialog (dlg, TRUE); |
671 |
break; |
672 |
|
673 |
case IDCANCEL: |
674 |
EndDialog( dlg, FALSE ); |
675 |
break; |
676 |
} |
677 |
break; |
678 |
} |
679 |
return FALSE; |
680 |
} |