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

Diff of /trunk/Src/wptKeygenDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.34  
changed lines
  Added in v.115

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26