/[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 150 by twoaday, Wed Jan 18 11:52:45 2006 UTC revision 225 by twoaday, Tue Jun 6 13:37:59 2006 UTC
# Line 22  Line 22 
22  #endif  #endif
23    
24  #include <windows.h>  #include <windows.h>
25    #include <time.h>
26    
27  #include "resource.h"  #include "resource.h"
28  #include "wptTypes.h"  #include "wptTypes.h"
# Line 33  Line 34 
34  #include "wptW32API.h"  #include "wptW32API.h"
35  #include "wptVersion.h"  #include "wptVersion.h"
36  #include "wptErrors.h"  #include "wptErrors.h"
37  #include "wptUTF8.h"  
38    
39  /* All valid key generation combination. */  /* All valid key generation combination. */
40  enum gpg_keytype_t {  enum gpg_keytype_t {
# Line 91  static const char key_params_one_with_co Line 92  static const char key_params_one_with_co
92          "Key-Length: %d\n"          "Key-Length: %d\n"
93          "Key-Usage: %s\n"                "Key-Usage: %s\n"      
94          "Name-Real: %s\n"                "Name-Real: %s\n"      
         "Name-Email: %s\n"  
95          "Name-Comment: %s\n"          "Name-Comment: %s\n"
96            "Name-Email: %s\n"
97          "Expire-Date: %s\n"          "Expire-Date: %s\n"
98          "Passphrase: %s\n"          "Passphrase: %s\n"
99          "</GnupgKeyParms>\n";          "</GnupgKeyParms>\n";
# Line 126  gpg_genkey_params (int keytype, int bits Line 127  gpg_genkey_params (int keytype, int bits
127          size += strlen (key_params);          size += strlen (key_params);
128      if (expdate)      if (expdate)
129          size += strlen (expdate) + 1;          size += strlen (expdate) + 1;
130      p = (char *)malloc (size+1);      p = new char[size+1];
131      if (!p)      if (!p)
132          BUG (0);          BUG (0);
133      /* XXX: simply the whole switch-case code. */      /* XXX: simply the whole switch-case code. */
# Line 172  gpg_genkey_params (int keytype, int bits Line 173  gpg_genkey_params (int keytype, int bits
173              break;              break;
174                            
175          default:          default:
176              if (p)              free_if_alloc (p);
                 free (p);  
             p = NULL;  
177              break;              break;
178          }          }
179      }      }
# Line 220  gpg_genkey_params (int keytype, int bits Line 219  gpg_genkey_params (int keytype, int bits
219              break;              break;
220                            
221          default:          default:
222              if (p)              free_if_alloc (p);
                 free (p);  
             p = NULL;  
223              break;              break;
224          }          }
225      }      }
# Line 249  gpg_genkey (const char *params, gpgme_pr Line 246  gpg_genkey (const char *params, gpgme_pr
246      err = gpgme_op_genkey (ctx, params, NULL, NULL);      err = gpgme_op_genkey (ctx, params, NULL, NULL);
247      if (!err) {      if (!err) {
248          res = gpgme_op_genkey_result (ctx);          res = gpgme_op_genkey_result (ctx);
249          *fpr = res->fpr? strdup (res->fpr) : NULL;          *fpr = res && res->fpr? m_strdup (res->fpr) : NULL;
250      }      }
251      gpgme_release (ctx);      gpgme_release (ctx);
252      return err;      return err;
# Line 304  backup_keyrings (HWND dlg) Line 301  backup_keyrings (HWND dlg)
301          }          }
302          name = get_filesave_dlg (dlg, _("Destination for Secret Keyring"),          name = get_filesave_dlg (dlg, _("Destination for Secret Keyring"),
303                                   NULL, "secring.gpg");                                   NULL, "secring.gpg");
304          if( name ) {          if (name) {
305              keyring = make_filename (path, "secring", "gpg");              keyring = make_filename (path, "secring", "gpg");
306              if (!CopyFile (keyring, name, FALSE))              if (!CopyFile (keyring, name, FALSE))
307                  log_box (_("Key Generation"), MB_ERR,                  log_box (_("Key Generation"), MB_ERR,
# Line 335  fill_keytype_box (HWND dlg) Line 332  fill_keytype_box (HWND dlg)
332  }  }
333    
334    
335    time_t w32_mktime (SYSTEMTIME *st);
336    
337  /* Check that the given date lies not in the past.  /* Check that the given date lies not in the past.
338     Return value: 1 on success. */     Return value: 1 on success. */
339  int  int
340  keygen_check_date (SYSTEMTIME *st)  keygen_check_date (SYSTEMTIME *st)
341  {        {      
342      SYSTEMTIME t;      time_t dat, now;
343                    
344      GetSystemTime (&t);      dat = w32_mktime (st);
345      if (st->wYear > t.wYear || st->wMonth > t.wMonth)      now = time (NULL);
346        if (dat >= now)
347          return 1;          return 1;
348      else if (st->wYear < t.wYear || st->wMonth < t.wMonth || st->wDay < t.wDay)      return 0;
         return 0;  
     return 1;  
349  }  }
350    
351    
# Line 358  keygen_dlg_proc (HWND dlg, UINT msg, WPA Line 356  keygen_dlg_proc (HWND dlg, UINT msg, WPA
356      static genkey_s *ctx;      static genkey_s *ctx;
357      SYSTEMTIME st;      SYSTEMTIME st;
358      gpgme_error_t err;      gpgme_error_t err;
359      char name[128] = {0}, email[128] = {0}, comment[128] = {0};      char *utf8_name =NULL, *utf8_comment = NULL;
360        char email[128];
361      char *pwd;      char *pwd;
362      char t[64], *expire = NULL, *fpr=NULL;      char t[64], *expire = NULL, *fpr=NULL;
363      int bits, use_comment, keytype = 0;      int bits, use_comment, keytype = 0;
364      int cancel = 0;      int cancel = 0;
365      char *p;      char *p;
366            
367      switch ( msg ) {      switch (msg) {
368      case WM_INITDIALOG:      case WM_INITDIALOG:
369          if (lparam != 0)          if (lparam != 0)
370              ctx = (genkey_s *)lparam;              ctx = (genkey_s *)lparam;
# Line 417  keygen_dlg_proc (HWND dlg, UINT msg, WPA Line 416  keygen_dlg_proc (HWND dlg, UINT msg, WPA
416                  if (id == IDNO)                  if (id == IDNO)
417                      bits = DFAULT_KEYSIZE;                      bits = DFAULT_KEYSIZE;
418              }              }
419              if (!GetDlgItemText (dlg, IDC_KEYGEN_NAME, name, sizeof (name) - 1)) {              if (!GetDlgItemText_utf8 (dlg, IDC_KEYGEN_NAME, &utf8_name)) {
420                  msg_box( dlg, _("Please enter the name."), _("Key Generation"), MB_ERR);                  msg_box (dlg, _("Please enter the name."), _("Key Generation"), MB_ERR);
421                  return FALSE;                  return FALSE;
422              }              }
423              if (strchr (name, '@')) {              if (strchr (utf8_name, '@')) {
424                  msg_box (dlg, _("Please do not enter the email address in the name field."),                  msg_box (dlg, _("Please do not enter the email address in the name field."),
425                           _("Key Generation"), MB_INFO);                           _("Key Generation"), MB_INFO);
426                    free_if_alloc (utf8_name);
427                  return FALSE;                  return FALSE;
428              }              }
429              if (!GetDlgItemText (dlg, IDC_KEYGEN_EMAIL, email, sizeof (email) -1)              if (!GetDlgItemText (dlg, IDC_KEYGEN_EMAIL, email, sizeof (email) -1)
430                  || !strchr (email, '@')) {                  || check_email_address (email)) {
431                  msg_box (dlg, _("Please enter a valid email address."),                  msg_box (dlg, _("Please enter a valid email address."),
432                           _("Key Generation"), MB_ERR);                           _("Key Generation"), MB_ERR);
433                    free_if_alloc (utf8_name);
434                  return FALSE;                  return FALSE;
435              }              }
436              use_comment = GetDlgItemText(dlg, IDC_KEYGEN_COMMENT,              use_comment = GetDlgItemText_utf8 (dlg, IDC_KEYGEN_COMMENT,
437                                           comment, sizeof (comment) -1);                                                 &utf8_comment);
438              if (use_comment > 0 && strchr (comment, '@')) {              if (use_comment > 0 && strchr (utf8_comment, '@')) {
439                  msg_box (dlg, _("Please do NOT enter the email address in the comment field."),                  msg_box (dlg, _("Please do NOT enter the email address in the comment field."),
440                           _("Key Generation"), MB_INFO);                           _("Key Generation"), MB_INFO);
441                  return FALSE;                  free_if_alloc (utf8_name);
442                    free_if_alloc (utf8_comment);
443                    return TRUE;
444              }              }
445              keytype = SendDlgItemMessage (dlg, IDC_KEYGEN_KEYTYPE, CB_GETCURSEL, 0, 0) + 1;              keytype = SendDlgItemMessage (dlg, IDC_KEYGEN_KEYTYPE, CB_GETCURSEL, 0, 0) + 1;
446              if (IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER))              if (IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER))
447                  expire = NULL;                  expire = NULL;
448              else {              else {
449                  DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), &st);                  DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), &st);
450                    if (!keygen_check_date (&st)) {
451                        free_if_alloc (utf8_name);
452                        free_if_alloc (utf8_comment);
453                        msg_box (dlg, _("The date you have chosen lies in the past."),
454                                 _("Key Generation"), MB_ERR);
455                        return TRUE;
456                    }
457                  _snprintf (t, DIM (t)-1, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);                  _snprintf (t, DIM (t)-1, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
458                  expire = t;                  expire = t;
459              }              }
# Line 451  keygen_dlg_proc (HWND dlg, UINT msg, WPA Line 461  keygen_dlg_proc (HWND dlg, UINT msg, WPA
461              pwd = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel);              pwd = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel);
462              if (cancel) {              if (cancel) {
463                  sfree_if_alloc (pwd);                  sfree_if_alloc (pwd);
464                    free_if_alloc (utf8_name);
465                    free_if_alloc (utf8_comment);
466                  return FALSE;                  return FALSE;
467              }              }
468              if (!pwd) {              if (!pwd) {
469                  msg_box (dlg, _("Please enter the passphrase."),                  msg_box (dlg, _("Please enter the passphrase."),
470                           _("Key Generation"), MB_ERR);                           _("Key Generation"), MB_ERR);
471                    free_if_alloc (utf8_name);
472                    free_if_alloc (utf8_comment);
473                  return FALSE;                  return FALSE;
474              }              }
475    
476              if (!use_comment && !strlen (comment)) {              if (!use_comment && !utf8_comment)
                 char *utf8_name;  
                 utf8_name = wincp_to_utf8 (name, strlen (name));  
                 if (!utf8_name)  
                     BUG (0);  
477                  p = gpg_genkey_params (keytype, bits, utf8_name, NULL,                  p = gpg_genkey_params (keytype, bits, utf8_name, NULL,
478                                         email, expire, pwd);                                         email, expire, pwd);
479                  safe_free (utf8_name);              else
             }  
             else {  
                 char *utf8_name, *utf8_comment;  
                 utf8_name = wincp_to_utf8 (name, strlen (name));  
                 utf8_comment = wincp_to_utf8 (comment, strlen (comment));                
                 if( !utf8_name || !utf8_comment )  
                     BUG (0);  
480                  p = gpg_genkey_params (keytype, bits, utf8_name, utf8_comment,                  p = gpg_genkey_params (keytype, bits, utf8_name, utf8_comment,
481                                         email, expire, pwd);                                         email, expire, pwd);
482                  safe_free (utf8_name);              free_if_alloc (utf8_name);
483                  safe_free (utf8_comment);              free_if_alloc (utf8_comment);
             }  
484              keygen_cb_dlg_create ();              keygen_cb_dlg_create ();
485              err = gpg_genkey (p, keygen_cb, &fpr);              err = gpg_genkey (p, keygen_cb, &fpr);
486              sfree_if_alloc (pwd);              sfree_if_alloc (pwd);
487              if (p) {              sfree_if_alloc (p); /* burn the passphrase! */
488                  wipememory (p, strlen (p));  /* burn the passphrase! */              keygen_cb_dlg_destroy (1);
                 free (p);  
             }  
             keygen_cb_dlg_destroy ();  
             keygen_cb (NULL, NULL, 0, 0, 0); /* flush */  
489              if (err) {              if (err) {
490                  safe_free (fpr);                  free_if_alloc (fpr);
491                  msg_box (dlg, gpgme_strerror (err), _("Key Generation"), MB_ERR);                  msg_box (dlg, gpgme_strerror (err), _("Key Generation"), MB_ERR);
492                  return FALSE;                  return FALSE;
493              }              }
# Line 497  keygen_dlg_proc (HWND dlg, UINT msg, WPA Line 495  keygen_dlg_proc (HWND dlg, UINT msg, WPA
495                            
496              keycache_update (0, fpr);              keycache_update (0, fpr);
497              keycache_update (1, fpr);              keycache_update (1, fpr);
498              safe_free (fpr);                      free_if_alloc (fpr);            
499    
500              clear_dlg_fields (dlg);              clear_dlg_fields (dlg);
501              backup_keyrings (dlg);              backup_keyrings (dlg);
# Line 525  keygen_wizard_dlg_proc (HWND dlg, UINT m Line 523  keygen_wizard_dlg_proc (HWND dlg, UINT m
523      static genkey_s *ctx;      static genkey_s *ctx;
524      static int pubkey_algo = GPG_KEYGEN_DSA_ELG;      static int pubkey_algo = GPG_KEYGEN_DSA_ELG;
525      gpgme_error_t err;      gpgme_error_t err;
526      char name[128], email[128];      char email[128];
527      char * utf8_name, * p, *fpr=NULL;      char *utf8_name=NULL, * p, *fpr=NULL;
528      char * pass = NULL;      char * pass = NULL;
529      int cancel = 0;      int cancel = 0;
530            
# Line 549  keygen_wizard_dlg_proc (HWND dlg, UINT m Line 547  keygen_wizard_dlg_proc (HWND dlg, UINT m
547          center_window (dlg, NULL);          center_window (dlg, NULL);
548          break;          break;
549    
     case WM_SYSCOMMAND:  
         if( LOWORD( wparam ) == SC_CLOSE )  
             EndDialog( dlg, FALSE );  
   
550      case WM_COMMAND:      case WM_COMMAND:
551          switch( LOWORD( wparam ) ) {          switch (LOWORD( wparam)) {
552          case IDC_KEYWIZARD_EXPERT:          case IDC_KEYWIZARD_EXPERT:
553              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYGEN, dlg,              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYGEN, dlg,
554                              keygen_dlg_proc, 0);                              keygen_dlg_proc, 0);
# Line 562  keygen_wizard_dlg_proc (HWND dlg, UINT m Line 556  keygen_wizard_dlg_proc (HWND dlg, UINT m
556              break;              break;
557    
558          case IDOK:          case IDOK:
559              if( !GetDlgItemText( dlg, IDC_KEYWIZARD_NAME, name, sizeof name-1 ) ) {              if (!GetDlgItemText_utf8 (dlg, IDC_KEYWIZARD_NAME, &utf8_name)) {
560                  msg_box( dlg, _("Please enter the name."),                  msg_box (dlg, _("Please enter the name."),
561                           _("Key Generation Wizard"), MB_ERR );                           _("Key Generation Wizard"), MB_ERR);
562                  return FALSE;                  return FALSE;
563              }              }
564              if (strchr (name, '@')) {              if (strchr (utf8_name, '@')) {
565                  msg_box (dlg, _("Please do not enter the email address in the name field."),                  msg_box (dlg, _("Please do not enter the email address in the name field."),
566                           _("Key Generation Wizard"), MB_WARN);                           _("Key Generation Wizard"), MB_WARN);
567                    free_if_alloc (utf8_name);
568                  return FALSE;                  return FALSE;
569              }              }
570              if( !GetDlgItemText( dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 )              if (!GetDlgItemText(dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 )
571                  || !strchr( email, '@' ) ) {                  || check_email_address (email)) {
572                  msg_box( dlg, _("Please enter a valid email address."),                  msg_box (dlg, _("Please enter a valid email address."),
573                           _("Key Generation Wizard"), MB_ERR );                           _("Key Generation Wizard"), MB_ERR);
574                    free_if_alloc (utf8_name);
575                  return FALSE;                  return FALSE;
576              }              }
577              if (strchr (email, '<') || strchr (email, '>')) {              if (strchr (email, '<') || strchr (email, '>')) {
578                  msg_box (dlg, _("Please do not add '<' or '>' to the email address."),                  msg_box (dlg, _("Please do not add '<' or '>' to the email address."),
579                           _("Key Generation Wizard"), MB_WARN);                           _("Key Generation Wizard"), MB_WARN);
580                    free_if_alloc (utf8_name);
581                  return FALSE;                  return FALSE;
582              }              }
583              pass = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel);              pass = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel);
584              if (cancel)              if (cancel) {
585                    free_if_alloc (utf8_name);
586                  return FALSE;                  return FALSE;
587              utf8_name = wincp_to_utf8 (name, strlen (name));              }
             if( !utf8_name )  
                 BUG( NULL );  
588              if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA))              if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA))
589                  pubkey_algo = GPG_KEYGEN_DSA_RSA;                  pubkey_algo = GPG_KEYGEN_DSA_RSA;
590              p = gpg_genkey_params (pubkey_algo, DFAULT_KEYSIZE, utf8_name,              p = gpg_genkey_params (pubkey_algo, DFAULT_KEYSIZE, utf8_name,
591                                       NULL, email, NULL, pass);                                       NULL, email, NULL, pass);
592              free( utf8_name );              free_if_alloc (utf8_name);
593              keygen_cb_dlg_create();              keygen_cb_dlg_create();
594              err = gpg_genkey (p, keygen_cb, &fpr);              err = gpg_genkey (p, keygen_cb, &fpr);
595              keygen_cb_dlg_destroy();              keygen_cb_dlg_destroy (1);
596              keygen_cb( NULL, NULL, 0, 0, 0 );              sfree_if_alloc (p);
             if( p ) {  
                 memset( p, 0, strlen( p ) );  
                 free( p );  
             }  
597              sfree_if_alloc (pass);              sfree_if_alloc (pass);
598              if( err ) {              if (err) {
599                  msg_box( dlg, gpgme_strerror( err ), _("Key Generation Wizard"), MB_ERR );                  msg_box (dlg, gpgme_strerror( err ), _("Key Generation Wizard"), MB_ERR);
600                  if (fpr)                  free_if_alloc (fpr);
                     free (fpr);  
601                  return FALSE;                  return FALSE;
602              }              }
603              status_box( dlg, _("Key Generation completed"), _("GnuPG Status") );              status_box (dlg, _("Key Generation completed"), _("GnuPG Status"));
               
604              keycache_update (0, fpr);              keycache_update (0, fpr);
605              keycache_update (1, fpr);              keycache_update (1, fpr);
606              safe_free (fpr);              free_if_alloc (fpr);
607    
608              backup_keyrings (dlg);              backup_keyrings (dlg);
609              if (ctx)              if (ctx)

Legend:
Removed from v.150  
changed lines
  Added in v.225

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26