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

Annotation of /trunk/Src/wptKeygenDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23162 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26