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

Annotation of /trunk/Src/wptKeygenDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations)
Wed Oct 26 11:20:09 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23165 byte(s)
2005-10-25  Timo Schulz  <twoaday@g10code.com>
                                                                                
        * wptGPGUtil.cpp (create_process): Hide window.
        * wptKeyPropsDlg.cpp (get_photo_tmpname): New.
        * wptClipSignEncDlg.cpp (clip_signenc_dlg_proc): Remove
        static var 'enable'.
        * wptKeygenDlg.cpp (keygen_dlg_proc): Likewise.
        (gpg_genkey_params): Make sure all primary keys are capable
        for signing and certification.
        * wptKeySigDlg.cpp (is_sig): If no item is selected, return 0.
        * wptGPG.cpp (gnupg_access_keyring): Check return value for
        NULL. Noted by Ralf.
        (get_gnupg_prog): Simplified.
        (check_homedir): Fixed. Return 0 when the dir is successfully created.
        * wptKeyManagerDlg.cpp (km_file_import): Use the hourglass to
        indicate a pending GPG process.
        * wptFileManager.cpp (op_begin, op_end): New. Indicate an start
        and and of an operation. For now just the cursor changes.
        (fm_parse_command_line): Remove debug output. Thanks to Ralf again.
        * WinPT.cpp (WinMain): Check if there is already an instance and
        set a variable early as possible.
        (load_gettext): If a previous instance was found, do not output
        any errors. Kudos to Ralf.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26