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

Diff of /trunk/Src/wptCardDlg.cpp

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

revision 6 by twoaday, Mon Apr 4 06:59:24 2005 UTC revision 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC
# Line 32  Line 32 
32  #include "wptDlgs.h"  #include "wptDlgs.h"
33  #include "wptGPG.h"  #include "wptGPG.h"
34  #include "wptUTF8.h"  #include "wptUTF8.h"
35    #include "wptCardEdit.h"
36    #include "w32gpgme.h"
37  #include "wptCard.h"  #include "wptCard.h"
38    
39    
40  int keygen_check_date (SYSTEMTIME * st);  int keygen_check_date (SYSTEMTIME * st);
41    
42  static const char * sex[] = {"Male", "Female", "Undefined", NULL};  static const char * sex[] = {"Male", "Female", "Undefined", NULL};
# Line 45  static pin_cb_ctx_s pincb; Line 48  static pin_cb_ctx_s pincb;
48    
49  struct {  struct {
50      int ctlid;      int ctlid;
     gpgme_attr_t attr;  
51      const char * err;      const char * err;
52  } attr_tab[] = {  } attr_tab[] = {
53      {IDC_CEDIT_AID,    GPGME_ATTR_CARD_AID,    ""},      {IDC_CEDIT_AID,    ""},
54      {IDC_CEDIT_VENDOR, GPGME_ATTR_CARD_VENDOR, "No Vendor"},      {IDC_CEDIT_VENDOR, "No Vendor"},
55      {IDC_CEDIT_VERSION,GPGME_ATTR_CARD_VER,    "No Version"},      {IDC_CEDIT_VERSION,"No Version"},
56      {IDC_CEDIT_SERIAL, GPGME_ATTR_CARD_SERIAL, "No Serial-No"},      {IDC_CEDIT_SERIAL, "No Serial-No"},
57      {IDC_CEDIT_NAME,   GPGME_ATTR_CARD_NAME,   "No Name"},      {IDC_CEDIT_NAME,   "No Name"},
58      {IDC_CEDIT_NAME2,  GPGME_ATTR_CARD_NAME2,  "No Surname"},      {IDC_CEDIT_NAME2,  "No Surname"},
59      {IDC_CEDIT_KEYURL, GPGME_ATTR_CARD_URL,    "No Key-URL"},      {IDC_CEDIT_KEYURL, "No Key-URL"},
60      {IDC_CEDIT_LOGIN,  GPGME_ATTR_CARD_LOGIN,  "No Login name"},      {IDC_CEDIT_LOGIN,  "No Login name"},
61      {0},      {0},
62  };  };
63    
64    
65  #define card_get_string(card, what) \  /* Return all card attributes from @card. @n contains
66      gpgme_card_get_string_attr ((card), (what), NULL, 1)     the number of items which were returned. */
67  #define card_get_ulong(card, what)  \  char**
68      gpgme_card_get_ulong_attr ((card), (what), NULL, 0)  card_get_items (gpg_card_t card, int *n)
69  #define card_get_fpr(card, idx)     \  {
70      gpgme_card_get_string_attr ((card), GPGME_ATTR_CARD_FPR, NULL, (idx)-1)      char **p;
71    
72        *n= 8;
73        p = (char **)calloc (*n+1, sizeof (char*));
74        if (!p)
75            BUG (0);
76        p[0] = card->aid;
77        p[1] = card->vendor;
78        p[2] = card->ver;
79        p[3] = card->serial;
80        p[4] = card->givenname;
81        p[5] = card->surname;
82        p[6] = card->url;
83        p[7] = card->login;
84        return p;
85    }
86    
87    
88  static int  static int
89  idx_from_lang( const char * _lang )  idx_from_lang (const char * _lang)
90  {  {
91      const char * s;      const char * s;
92      int i;      int i;
# Line 77  idx_from_lang( const char * _lang ) Line 94  idx_from_lang( const char * _lang )
94      if (!_lang)      if (!_lang)
95          return 0;          return 0;
96      for (i=0; (s = lang[i]); i++) {      for (i=0; (s = lang[i]); i++) {
97          if( !strcmp( _lang, s ) )          if (!strcmp( _lang, s ))
98              return i;              return i;
99      }      }
100      return 0;      return 0;
101  } /* idx_from_lang */  }
102    
103    
104    #if 0 /* @unused@ */
105  int  int
106  show_card_status (void)  show_card_status (void)
107  {  {
# Line 101  show_card_status (void) Line 119  show_card_status (void)
119      }      }
120      return 0;      return 0;
121  } /* show_card_status */  } /* show_card_status */
122    #endif
123    
124    /* Check if there is a card in the reader and analyze the
125  gpgme_card_t     returned information.
126       Return value: card context or NULL on error. */
127    gpg_card_t
128  gpg_load_scard (void)  gpg_load_scard (void)
129  {  {
130      gpgme_error_t rc;      gpgme_error_t err;
131      gpgme_card_t card = NULL;      GpgCardEdit *ce;
132      gpgme_editcard_t ec;      gpg_card_t card = NULL;
     gpgme_ctx_t ctx;  
     const char * s;  
133      struct card_cb_s cb = {0};      struct card_cb_s cb = {0};
134        
135      rc = gpgme_new (&ctx);      ce = new GpgCardEdit ();
136      if (!rc)      if (!ce)
         rc = gpgme_editcard_new (&ec);  
     if (rc)  
137          BUG (0);          BUG (0);
138      gpgme_enable_logging (ctx);      memset (&cb, 0, sizeof (cb));
139      gpgme_editcard_set_callback (ec, card_callback, &cb);      ce->setCallback (card_callback, &cb);
140      gpgme_set_edit_ctx (ctx, ec, 0);      err = ce->getCardStatus (&card);
141      rc = gpgme_op_statuscard (ctx, &card);      if (err) {
142      if (rc) {          msg_box (NULL, gpgme_strerror (err), _("Card Manager"), MB_ERR);
         gpgme_show_error (NULL, rc, ctx, _("Card Manager"), MB_ERR);  
143          goto leave;          goto leave;
144      }      }
145      s = gpgme_card_get_string_attr (card, GPGME_ATTR_CARD_AID, NULL, 0);  
146      if (!s || strncmp (s, "D276000124", 10)) {      if (!card->aid || strncmp (card->aid, "D276000124", 10)) {
147          msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);          msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);
148          gpgme_card_release (card);          gpg_card_release (card);
149          card = NULL;          card = NULL;
150      }      }
151    
152  leave:  leave:
153      gpgme_editcard_release (ec);      delete ce;
     gpgme_release (ctx);  
154      return card;      return card;
155  } /* gpg_load_scard */  }
156    
157    
158    /* Print human friendly fingerprint to control @id in the
159       dialog @dlg. @fpr contains the raw fingerprint. */
160  static void  static void
161  print_fpr (HWND dlg, int id, const char * fpr)  print_fpr (HWND dlg, int id, const char * fpr)
162  {  {
# Line 147  print_fpr (HWND dlg, int id, const char Line 164  print_fpr (HWND dlg, int id, const char
164      size_t i, c;      size_t i, c;
165    
166      if (!fpr)      if (!fpr)
167          strcpy( buf, _("No Fingerprint") );          strcpy (buf, _("No Fingerprint"));
168      else {      else {
169          memset( buf, 0, sizeof (buf) );          memset (buf, 0, sizeof (buf));
170          for( i=0, c=0; i < strlen( fpr ); i++ ) {          for( i=0, c=0; i < strlen (fpr); i++) {
171              dig[0] = fpr[i]; dig[1] = 0;              dig[0] = fpr[i]; dig[1] = 0;
172              strcat( buf, dig );              strcat (buf, dig);
173              if( ++c == 4 ) {              if (++c == 4) {
174                  strcat( buf, " " );                  strcat (buf, " ");
175                  c=0;                  c=0;
176              }              }
177          }          }
178      }      }
179      SetDlgItemText( dlg, id, buf );      SetDlgItemText (dlg, id, buf);
180  } /* print_fpr */  }
181    
182    
183    /* Fill in all card information from @card. into the corresponding
184       dialog item fields in the dialog @dlg.
185       Return value: 0 on success. */
186  static int  static int
187  card_status( HWND dlg, gpgme_card_t card )  card_status (HWND dlg, gpg_card_t card)
188  {  {
189      static int fprbuf[] = {0, IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};      static int fprbuf[] = {IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};
190      const char * s;      const char * s;
191      int idx=0;      char **attrs;
192      u32 t;      int idx=0, n=0;
193    
194      s = card_get_string( card, GPGME_ATTR_CARD_AID );      if (!card->aid) {
     if( !s ) {  
195          msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR );          msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR );
196          return -1;          return -1;
197      }      }
198      SetDlgItemText( dlg, IDC_CEDIT_AID, s );      SetDlgItemText (dlg, IDC_CEDIT_AID, card->aid);
199        SetDlgItemInt (dlg, IDC_CEDIT_SIGCOUNT, card->sig_count, TRUE);
200    
201      t = card_get_ulong( card, GPGME_ATTR_CARD_SIGCOUNT );      for( idx=0; fprbuf[idx]; idx++ )
202      SetDlgItemInt( dlg, IDC_CEDIT_SIGCOUNT, t, TRUE );          print_fpr (dlg, fprbuf[idx], card->fpr[idx]);
   
     for( idx=1; fprbuf[idx]; idx++ ) {  
         s = card_get_fpr( card, idx );  
         print_fpr( dlg, fprbuf[idx], s );  
     }  
203    
204        attrs = card_get_items (card, &n);
205      /* XXX: show version as 1.1 not 0101 */      /* XXX: show version as 1.1 not 0101 */
206      for (idx=1; attr_tab[idx].attr; idx++) {      for (idx=1; attr_tab[idx].ctlid; idx++) {
207          s = card_get_string (card, attr_tab[idx].attr);          s = attrs[idx];
208          SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err);          SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err);
209      }      }
210        free (attrs);
211    
212      s = card_get_string( card, GPGME_ATTR_CARD_LANG );      idx = idx_from_lang (card->lang);    
213      idx = idx_from_lang( s );          SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0);
     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0 );  
214    
215      t = card_get_ulong( card, GPGME_ATTR_CARD_SEX );      switch (card->sex) {
     switch( t ) {  
216      case 'm': idx=0; break;      case 'm': idx=0; break;
217      case 'f': idx=1; break;      case 'f': idx=1; break;
218      default :      default :
219      case 'u': idx=2; break;      case 'u': idx=2; break;
220      }      }
221      SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0 );      SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0);
222    
223      return 0;      return 0;
224  } /* card_status */  }
225    
226    
227    /* Initialize the enum combox boxes in dialog @dlg. */
228  static void  static void
229  prepare_dialog( HWND dlg )  prepare_dialog (HWND dlg)
230  {  {
231      const char * s;      const char * s;
232      int i;      int i;
# Line 221  prepare_dialog( HWND dlg ) Line 237  prepare_dialog( HWND dlg )
237      for( i=0; (s = lang[i]); i++ )      for( i=0; (s = lang[i]); i++ )
238          SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s );          SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s );
239      SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0 );      SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0 );
240  } /* prepare_dialog */  }
241    
242    
243    
244    /* Return 0 if the given string @str has the proper format. */
245  static int  static int
246  check_string( const char * str, int flags )  check_string (const char *str, int flags)
247  {  {
248      size_t i;      size_t i;
249      for( i=0; i < strlen( str ); i++ ) {      for( i=0; i < strlen( str ); i++ ) {
# Line 237  check_string( const char * str, int flag Line 255  check_string( const char * str, int flag
255    
256    
257  static int  static int
258  do_proc_card_cmds( HWND dlg, struct pin_cb_ctx_s * pincb, gpgme_card_t card )  do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s * pincb, gpg_card_t card)
259  {  {
260      static struct {      static struct {
261          int id;          int id;
# Line 245  do_proc_card_cmds( HWND dlg, struct pin_ Line 263  do_proc_card_cmds( HWND dlg, struct pin_
263          int us_ascii;          int us_ascii;
264          int changed;          int changed;
265      } idctl[] = {      } idctl[] = {
266          {IDC_CEDIT_NAME,  GPGME_EDITCARD_NAME,  1,  0},          {IDC_CEDIT_NAME,  GPG_EDITCARD_NAME,  1,  0},
267          {IDC_CEDIT_LANG2, GPGME_EDITCARD_LANG,  1,  0},          {IDC_CEDIT_LANG2, GPG_EDITCARD_LANG,  1,  0},
268          {IDC_CEDIT_SEX2,  GPGME_EDITCARD_SEX,   1|1,0},          {IDC_CEDIT_SEX2,  GPG_EDITCARD_SEX,   1|1,0},
269          {IDC_CEDIT_KEYURL,GPGME_EDITCARD_KEYURL,1,  0},          {IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1,  0},
270          {IDC_CEDIT_LOGIN, GPGME_EDITCARD_LOGIN, 1,  0},          {IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1,  0},
271          {0}          {0}
272      };      };    
273      gpgme_editcard_t ec;      gpgme_error_t err;
274      gpgme_ctx_t ctx;      GpgCardEdit *ce;
     gpgme_error_t rc;  
275      char buf[256], tmp[128];      char buf[256], tmp[128];
276      int errc=0;      int errc=0, use_arg2 = 0;
277      int i, id, n=0;      int i, id, n=0;
278    
279      /* XXX rewrite the entire function */      /* XXX rewrite the entire function */
# Line 287  do_proc_card_cmds( HWND dlg, struct pin_ Line 304  do_proc_card_cmds( HWND dlg, struct pin_
304      if( !n )      if( !n )
305          return 0;          return 0;
306            
307      rc = gpgme_editcard_new( &ec );      ce = new GpgCardEdit ();
308      if( !rc )      if (!ce)
         rc = gpgme_new( &ctx );  
     if( rc )  
309          BUG( NULL );          BUG( NULL );
310      gpgme_editcard_control( ec, GPGME_EDITCARD_APIN, pincb->apin );      ce->setAdminPIN (pincb->apin);
311      gpgme_editcard_control( ec, GPGME_EDITCARD_UPIN, pincb->upin );      ce->setPIN (pincb->upin);
312      for( i=0; idctl[i].id; i++ ) {      for( i=0; idctl[i].id; i++ ) {
313          if( idctl[i].changed ) {          if( idctl[i].changed ) {
314              GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );              GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );
# Line 307  do_proc_card_cmds( HWND dlg, struct pin_ Line 322  do_proc_card_cmds( HWND dlg, struct pin_
322                           _("Card Edit"), MB_ERR );                           _("Card Edit"), MB_ERR );
323                  errc--; continue;                  errc--; continue;
324              }              }
325              if( idctl[i].cmd == GPGME_EDITCARD_NAME ) {              if( idctl[i].cmd == GPG_EDITCARD_NAME ) {
326                  /* The "name" command actually needs two fields */                  /* The "name" command actually needs two fields */
327                  GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );                  GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
328                  gpgme_editcard_control( ec, GPGME_EDITCARD_NAME2, tmp );                  use_arg2 = 1;
329              }              }
330              gpgme_editcard_control( ec, idctl[i].cmd, buf );              else
331              gpgme_set_edit_ctx( ctx, ec, idctl[i].cmd );                  use_arg2 = 0;
332              rc = gpgme_op_editcard( ctx );              err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL);
333              if( rc ) {              if (err) {
334                  msg_box( dlg, _("Could not modify card attribute."),                  msg_box( dlg, _("Could not modify card attribute."),
335                           _("Card Edit"), MB_ERR );                           _("Card Edit"), MB_ERR );
336                  errc--;                  errc--;
# Line 330  do_proc_card_cmds( HWND dlg, struct pin_ Line 345  do_proc_card_cmds( HWND dlg, struct pin_
345          SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );          SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );
346          SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );          SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );
347      }      }
348      gpgme_editcard_release( ec );      delete ce;
     gpgme_release( ctx );  
349      return errc;      return errc;
350  } /* do_proc_card_cmds */  } /* do_proc_card_cmds */
351    
352    
353    /* Cleanup pin callback @ctx. */
354  void  void
355  free_pincb (struct pin_cb_ctx_s * ctx)  free_pincb (struct pin_cb_ctx_s *ctx)
356  {  {
357      if (!ctx)      if (!ctx)
358          return;          return;
359      free_if_alloc (ctx->info_text);      free_if_alloc (ctx->info_text);
360      free_if_alloc (ctx->upin);      sfree_if_alloc (ctx->upin);
361      free_if_alloc (ctx->apin);      sfree_if_alloc (ctx->apin);
362  } /* free_pincb */  }
363    
364    
365    /* Request a PIN from the user. @which decided if the
366       normal PIN or the admin PIN will be requested.
367       @card is used to show some information to the user.
368       @pincb is the actuall callback context.
369       Return value: 0 on success. */
370  static int  static int
371  do_askpin( HWND dlg, gpgme_edit_card_t which, gpgme_card_t card,  do_askpin (HWND dlg, int which, gpg_card_t card,
372             struct pin_cb_ctx_s * pincb )             struct pin_cb_ctx_s *pincb)
373  {  {
374      const char * s, * fmt;      const char * s, * fmt;
375      const char * n1, * n2, * serial;      const char * n1, * n2, * serial;
376      char * p;      char * p;
377      size_t n;      size_t n;
378    
379      if( (which == GPGME_EDITCARD_CHAPIN && pincb->apin) ||      if( (which == GPG_EDITCARD_CHAPIN && pincb->apin) ||
380          (which == GPGME_EDITCARD_CHUPIN && pincb->upin) )          (which == GPG_EDITCARD_CHUPIN && pincb->upin) )
381          return 0;          return 0;
382            
383      if( which == GPGME_EDITCARD_CHAPIN )      if( which == GPG_EDITCARD_CHAPIN )
384          s = _("Please enter the 'Admin PIN'");          s = _("Please enter the 'Admin PIN'");
385      else if( which == GPGME_EDITCARD_CHUPIN )      else if( which == GPG_EDITCARD_CHUPIN )
386          s = _("Please enter the 'User PIN'");          s = _("Please enter the 'User PIN'");
387      else      else
388          s = _("Please enter the PIN");          s = _("Please enter the PIN");
# Line 370  do_askpin( HWND dlg, gpgme_edit_card_t w Line 390  do_askpin( HWND dlg, gpgme_edit_card_t w
390      free_if_alloc( pincb->info_text );      free_if_alloc( pincb->info_text );
391      if( card ) {      if( card ) {
392          fmt = _("%s\nName: %s %s\nSerial-No: %s\n");          fmt = _("%s\nName: %s %s\nSerial-No: %s\n");
393          n1 = card_get_string( card, GPGME_ATTR_CARD_NAME );          n1 = card->givenname;
394          n2 = card_get_string( card, GPGME_ATTR_CARD_NAME2 );          n2 = card->surname;
395          if( !n1 || !n2 ) {          if( !n1 || !n2 ) {
396              n1 = "No"; n2 = "Name";              n1 = "No"; n2 = "Name";
397          }              }    
398          serial = card_get_string( card, GPGME_ATTR_CARD_SERIAL );          serial = card->serial;
399          if( !serial )          if (!serial)
400              serial = "No Serial";              serial = "No Serial ID";
401          n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;          n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;
402          p = pincb->info_text = new char[strlen( s )+n+1 ];          p = pincb->info_text = new char[strlen( s )+n+1 ];
403          if( !p )          if( !p )
# Line 391  do_askpin( HWND dlg, gpgme_edit_card_t w Line 411  do_askpin( HWND dlg, gpgme_edit_card_t w
411      }      }
412      DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,      DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,
413                      pin_cb_dlg_proc, (LPARAM)pincb);                      pin_cb_dlg_proc, (LPARAM)pincb);
414      if( !pincb->apin && !pincb->upin) {      if (!pincb->apin && !pincb->upin) {
415          safe_free (pincb->info_text);          safe_free (pincb->info_text);
416          return -1;          return -1;
417      }      }
418      return 0;      return 0;
419  } /* do_askpin */  }
420    
421    
422    /* Dialog box procedure for card edit. */
423  BOOL CALLBACK  BOOL CALLBACK
424  card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
425  {  {
426      static gpgme_card_t card;          static gpg_card_t card;    
427      char tmp[128];      char tmp[128];
428      size_t n=0;      size_t n=0;
429    
430      switch (msg) {      switch (msg) {
431      case WM_INITDIALOG:      case WM_INITDIALOG:
432          card = (gpgme_card_t)lparam;          card = (gpg_card_t)lparam;
433          if (!card)          if (!card)
434              BUG (0);              BUG (0);
435          prepare_dialog (dlg);          prepare_dialog (dlg);
436          if (card_status (dlg, card ))          if (card_status (dlg, card ))
437              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
438          center_window (dlg);          center_window (dlg, NULL);
439          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
440          return TRUE;          return TRUE;
441    
442      case WM_DESTROY:      case WM_DESTROY:
443          free_if_alloc (pincb.info_text);          free_if_alloc (pincb.info_text);
444          free_if_alloc (pincb.apin);          sfree_if_alloc (pincb.apin);
445          free_if_alloc (pincb.upin);          sfree_if_alloc (pincb.upin);
446          memset (&pincb, 0, sizeof pincb);          memset (&pincb, 0, sizeof pincb);
447          break;          break;
448    
# Line 453  card_edit_dlg_proc (HWND dlg, UINT msg, Line 474  card_edit_dlg_proc (HWND dlg, UINT msg,
474                      _("This operation will override the keys on the card.\n"                      _("This operation will override the keys on the card.\n"
475                        "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);                        "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);
476                  if (id == IDNO)                  if (id == IDNO)
477                      return FALSE;                      return TRUE;
478              }              }
479              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
480                              glob_hwnd, card_keygen_dlg_proc, NULL);                              glob_hwnd, card_keygen_dlg_proc, NULL);
# Line 462  card_edit_dlg_proc (HWND dlg, UINT msg, Line 483  card_edit_dlg_proc (HWND dlg, UINT msg,
483          case IDOK:          case IDOK:
484              n = do_proc_card_cmds( dlg, NULL, NULL );              n = do_proc_card_cmds( dlg, NULL, NULL );
485              if( n ) {              if( n ) {
486                  if( do_askpin( dlg, GPGME_EDITCARD_CHAPIN, card, &pincb ) )                  if( do_askpin( dlg, GPG_EDITCARD_CHAPIN, card, &pincb ) )
487                      EndDialog( dlg, FALSE );                      EndDialog( dlg, FALSE );
488                  if( do_askpin( dlg, GPGME_EDITCARD_CHUPIN, card, &pincb ) )                  if( do_askpin( dlg, GPG_EDITCARD_CHUPIN, card, &pincb ) )
489                      EndDialog( dlg, FALSE );                      EndDialog( dlg, FALSE );
490              }              }
491              do_proc_card_cmds( dlg, &pincb, card );              do_proc_card_cmds( dlg, &pincb, card );
# Line 481  card_edit_dlg_proc (HWND dlg, UINT msg, Line 502  card_edit_dlg_proc (HWND dlg, UINT msg,
502      }      }
503    
504      return FALSE;      return FALSE;
505  } /* card_edit_dlg_proc */  }
   
506    
507    
508  static int /* fixme: works only roughly */  static int /* fixme: works only roughly */
# Line 504  calc_days (int y2, int m2, int d2, Line 524  calc_days (int y2, int m2, int d2,
524  }  }
525    
526    
527  static void  /* Dialog box procedure for the key generation on cards. */
 keygen_fill_algbox (HWND dlg)  
 {  
     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, (LPARAM)(const char*)"RSA");  
     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);  
 }  
   
   
528  BOOL CALLBACK  BOOL CALLBACK
529  card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
530  {  {
531      static int state = 0;      static int state = 0;
532      static int pwd_state = 0;      static int pwd_state = 0;
     gpgme_ctx_t ctx;  
     gpgme_editcard_t crd;  
533      gpgme_error_t err;      gpgme_error_t err;
534      char name[128], email[128], comment[128], expdate[64];      GpgCardEdit *ce;
535        char name[128], email[128], comment[128];
536      char pass[128];      char pass[128];
537      int card_flags = GPGME_CARDFLAG_NONE;      int card_flags = GPG_CARDFLAG_NONE;
538      int expires=0;      int expires=0, valid;
539      size_t n;      size_t n;
540    
541      switch (msg) {      switch (msg) {
542      case WM_INITDIALOG:      case WM_INITDIALOG:
543          state = 0;          state = 0;
544          pwd_state = 1;          pwd_state = 1;
545          center_window (dlg);          center_window (dlg, NULL);
546          CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);          CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);
547          CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);          CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);
548          CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);          CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);
549          EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);          EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);
550          keygen_fill_algbox (dlg);          SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, (LPARAM)(const char*)"RSA");
551            SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
552          SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));          SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
553          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
554          return FALSE;          return FALSE;
# Line 560  card_keygen_dlg_proc (HWND dlg, UINT msg Line 573  card_keygen_dlg_proc (HWND dlg, UINT msg
573              n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);              n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);
574              if (!n) {              if (!n) {
575                  msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);                  msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);
576                  return FALSE;                  return TRUE;
577              }              }
578              if (n < 5) {              if (n < 5) {
579                  msg_box (dlg, _("Name must be at least 5 characters long."),                  msg_box (dlg, _("Name must be at least 5 characters long."),
580                           _("Card Edit"), MB_INFO);                           _("Card Edit"), MB_INFO);
581                  return FALSE;                  return TRUE;
582              }              }
583              n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);              n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);
584              if (!n) {              if (!n) {
585                  msg_box (dlg, _("Please enter your e-mail address."),                  msg_box (dlg, _("Please enter your e-mail address."),
586                           _("Card Edit"), MB_ERR);                           _("Card Edit"), MB_ERR);
587                  return FALSE;                  return TRUE;
588              }              }
589              GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);              GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);
590              GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);              GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);
591              if (!strchr (email, '@') || n < 3) {              if (!strchr (email, '@') || n < 3) {
592                  msg_box (dlg, _("Please enter a valid e-mail address."),                  msg_box (dlg, _("Please enter a valid e-mail address."),
593                           _("Card Edit"), MB_ERR);                           _("Card Edit"), MB_ERR);
594                  return FALSE;                  return TRUE;
595              }              }
596              n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);              n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
597              if (!n && pwd_state == 1) {              if (!n && pwd_state == 1) {
598                  msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);                  msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);
599                  return FALSE;                  return TRUE;
600              }              }
601              n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);              n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);
602              if (n > 0)              if (n > 0)
# Line 591  card_keygen_dlg_proc (HWND dlg, UINT msg Line 604  card_keygen_dlg_proc (HWND dlg, UINT msg
604              if (is_8bit_string (name) ||is_8bit_string (comment)) {              if (is_8bit_string (name) ||is_8bit_string (comment)) {
605                  msg_box (dlg, _("Please use plain ASCII charset for the fields."),                  msg_box (dlg, _("Please use plain ASCII charset for the fields."),
606                           _("Card Edit"), MB_INFO);                           _("Card Edit"), MB_INFO);
607                  return FALSE;                  return TRUE;
608              }              }
609              memset (&pincb, 0, sizeof (pincb));              memset (&pincb, 0, sizeof (pincb));
610              if (do_askpin (dlg, GPGME_EDITCARD_CHAPIN, NULL, &pincb)) {              if (do_askpin (dlg, GPG_EDITCARD_CHAPIN, NULL, &pincb)) {
611                  free_pincb (&pincb);                  free_pincb (&pincb);
612                  return FALSE;                  return TRUE;
613              }              }
614              if (do_askpin (dlg, GPGME_EDITCARD_CHUPIN, NULL, &pincb)) {              if (do_askpin (dlg, GPG_EDITCARD_CHUPIN, NULL, &pincb)) {
615                  free_pincb (&pincb);                  free_pincb (&pincb);
616                  return FALSE;                  return TRUE;
617              }              }
618              err = gpgme_new (&ctx);              ce = new GpgCardEdit ();
619              if (!err)              if (!ce)
                 err = gpgme_editcard_new (&crd);  
             if (err)  
620                  BUG (0);                  BUG (0);
621                
622              expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);              expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);
623              if (expires) {              if (expires) {
624                  SYSTEMTIME st, ct;                  SYSTEMTIME st, ct;
# Line 614  card_keygen_dlg_proc (HWND dlg, UINT msg Line 626  card_keygen_dlg_proc (HWND dlg, UINT msg
626                  if (!keygen_check_date (&st)) {                  if (!keygen_check_date (&st)) {
627                      msg_box (dlg, _("The date you have chosen lies in the past."),                      msg_box (dlg, _("The date you have chosen lies in the past."),
628                               _("Card Edit"), MB_ERR);                               _("Card Edit"), MB_ERR);
629                      gpgme_release (ctx);                      delete ce;
630                      gpgme_editcard_release (crd);                      return TRUE;
                     return FALSE;  
631                  }                  }
632                  GetSystemTime (&ct);                  GetSystemTime (&ct);
633                  /* XXX this is not very precise */                  /* XXX this is not very precise */
634                  sprintf (expdate, "%d", calc_days (st.wYear, st.wMonth, st.wDay,                  valid = calc_days (st.wYear, st.wMonth, st.wDay,
635                                                     ct.wYear, ct.wMonth, ct.wDay));                                     ct.wYear, ct.wMonth, ct.wDay);
636              }              }
637              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))
638                  card_flags |= GPGME_CARDFLAG_REPLACE;                  card_flags |= GPG_CARDFLAG_REPLACE;
639              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))
640                  card_flags |= GPGME_CARDFLAG_BAKENC;                  card_flags |= GPG_CARDFLAG_BAKENC;
641              gpgme_editcard_set_keygen_params (crd, card_flags, name, email,              ce->setKeygenPassphrase (pass);
642                                                n? comment : NULL,              ce->setPIN (pincb.upin);
643                                                expires? expdate : NULL);              ce->setAdminPIN (pincb.apin);
644              gpgme_editcard_set_passwd (crd, pass);              
             gpgme_editcard_control (crd, GPGME_EDITCARD_APIN, pincb.apin);  
             gpgme_editcard_control (crd, GPGME_EDITCARD_UPIN, pincb.upin);  
             gpgme_set_edit_ctx (ctx, crd, GPGME_EDITCARD_GENKEY);  
645              SetCursor( LoadCursor (NULL, IDC_WAIT));              SetCursor( LoadCursor (NULL, IDC_WAIT));
646              err = gpgme_op_editcard (ctx);              err = ce->genKey (card_flags, name, email, n? comment: NULL,
647                                  expires? valid : 0);
648                
649              SetCursor (LoadCursor (NULL, IDC_ARROW));              SetCursor (LoadCursor (NULL, IDC_ARROW));
650              if (err == GPGME_Canceled)              /*
651                if (err == gpg_error (GPG_ERR_CANCELLED)
652                  msg_box (dlg, _("Operation was canceled. It seems that there are "                  msg_box (dlg, _("Operation was canceled. It seems that there are "
653                                  "existing\nkeys on the cards. You need to mark the "                                  "existing\nkeys on the cards. You need to mark the "
654                                  "'Overwrite' flag."), _("Card Edit"), MB_INFO);                                  "'Overwrite' flag."), _("Card Edit"), MB_INFO);
655              else if (err)              else */
656                if (err)
657                  msg_box (dlg, "The operation does not succeed.\n"                  msg_box (dlg, "The operation does not succeed.\n"
658                                "Please make sure you entered the right PIN's."                                "Please make sure you entered the right PIN's."
659                                , _("Card Edit"), MB_ERR);                                , _("Card Edit"), MB_ERR);
# Line 650  card_keygen_dlg_proc (HWND dlg, UINT msg Line 662  card_keygen_dlg_proc (HWND dlg, UINT msg
662                           _("Card Edit"), MB_OK);                           _("Card Edit"), MB_OK);
663              memset (pass, 0, sizeof (pass));              memset (pass, 0, sizeof (pass));
664              free_pincb (&pincb);              free_pincb (&pincb);
665              gpgme_release (ctx);              delete ce;
             gpgme_editcard_release (crd);  
666              break;              break;
667    
668          case IDCANCEL:          case IDCANCEL:
# Line 661  card_keygen_dlg_proc (HWND dlg, UINT msg Line 672  card_keygen_dlg_proc (HWND dlg, UINT msg
672          break;          break;
673      }      }
674      return FALSE;      return FALSE;
675  } /* card_keygen_dlg_proc */  }
676    
677    
678    /* Check if the given pinlen is valid.
679       @which decided what PIN will be used.
680       @pinlen is the pin length entered by the user.
681       Return value: 0 on success. */
682  static int  static int
683  check_pin_len (int which, int flag, int pinlen)  check_pin_len (int which, int flag, int pinlen)
684  {  {
# Line 674  check_pin_len (int which, int flag, int Line 689  check_pin_len (int which, int flag, int
689              msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);              msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);
690          return -1;          return -1;
691      }      }
692      if (which == GPGME_EDITCARD_CHAPIN      if (which == GPG_EDITCARD_CHAPIN
693          && pinlen < 8) {          && pinlen < 8) {
694          msg_box (NULL, _("PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);          msg_box (NULL, _("Admin PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);
695          return -1;          return -1;
696      }      }
697      if (which == GPGME_EDITCARD_CHUPIN      if (which == GPG_EDITCARD_CHUPIN
698          && pinlen < 6) {          && pinlen < 6) {
699          msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR);          msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR);
700          return -1;          return -1;
# Line 687  check_pin_len (int which, int flag, int Line 702  check_pin_len (int which, int flag, int
702      return 0;      return 0;
703  }  }
704    
705    /* Dialog box procedure to change the PIN. */
706  BOOL CALLBACK  BOOL CALLBACK
707  card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
708  {      {    
709      static int hide = 1;      static int hide = 1;
710      gpgme_ctx_t ctx;      gpgme_error_t err;
711      gpgme_editcard_t chpin;      GpgCardEdit *ce;    
     gpgme_error_t rc;  
     gpgme_edit_card_t which;  
712      char pold[128], pnew[128], pnew2[128];      char pold[128], pnew[128], pnew2[128];
713        int which;
714      size_t n;      size_t n;
715    
716      switch( msg ) {      switch( msg ) {
717      case WM_INITDIALOG:      case WM_INITDIALOG:
718          hide = 1;          hide = 1;
719          CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);          CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);
720          center_window (dlg);          center_window (dlg, NULL);
721          CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);          CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);
722          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
723          break;          break;
# Line 722  card_changepin_dlg_proc (HWND dlg, UINT Line 736  card_changepin_dlg_proc (HWND dlg, UINT
736          switch( LOWORD( wparam ) ) {          switch( LOWORD( wparam ) ) {
737          case IDOK:          case IDOK:
738              if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))              if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))
739                  which = GPGME_EDITCARD_CHAPIN;                  which = GPG_EDITCARD_CHAPIN;
740              else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))              else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))
741                  which = GPGME_EDITCARD_CHUPIN;                  which = GPG_EDITCARD_CHUPIN;
742              else              else
743                  BUG (0);                  BUG (0);
744    
745              n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);              n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);
746              if (check_pin_len (which, 1, n))              if (check_pin_len (which, 1, n))
747                  return FALSE;                  return TRUE;
748              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);
749              if (check_pin_len (which, 0, n))              if (check_pin_len (which, 0, n))
750                  return FALSE;                  return TRUE;
751              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);
752              if (check_pin_len (which, 0, n))              if (check_pin_len (which, 0, n))
753                  return FALSE;                  return TRUE;
754              GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1);              GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1);
755              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1);              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1);
756              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1);              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1);
757              if (strcmp (pnew, pnew2)) {              if (strcmp (pnew, pnew2)) {
758                  msg_box (dlg, _("Passphrases do not match. Please try again."), _("Card Edit"), MB_ERR);                  msg_box (dlg, _("Passphrases do not match. Please try again."), _("Card Edit"), MB_ERR);
759                  return FALSE;                  return TRUE;
760              }              }
761              rc = gpgme_new (&ctx);  
762              if (!rc)              ce = new GpgCardEdit ();
763                  rc = gpgme_editcard_new (&chpin);              if (!ce)
             if (rc)  
764                  BUG (0);                  BUG (0);
765              gpgme_changepin_set( chpin, which, pold, pnew );              ce->setPIN (pold);
766              gpgme_set_edit_ctx( ctx, chpin, which );              ce->setNewPIN (pnew);
767              rc = gpgme_op_changepin( ctx );              ce->setType (which);
768              if( rc )  
769                  msg_box( dlg, gpgme_strerror( rc ), _("Card Edit"), MB_ERR );              err = ce->changePIN ();
770                if (err)
771                    msg_box (dlg, gpgme_strerror (err), _("Card Edit"), MB_ERR);
772              else {              else {
773                  msg_box( dlg, _("PIN successfully changed."), _("Card Edit"), MB_OK );                  msg_box( dlg, _("PIN successfully changed."), _("Card Edit"), MB_OK );
774                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
775                  SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");                  SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
776                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
777              }              }
778              gpgme_release( ctx );              delete ce;
             gpgme_editcard_release( chpin );  
779              break;              break;
780    
781          case IDCANCEL:          case IDCANCEL:
# Line 775  card_changepin_dlg_proc (HWND dlg, UINT Line 789  card_changepin_dlg_proc (HWND dlg, UINT
789      }      }
790    
791      return FALSE;      return FALSE;
 } /* card_changepin_dlg_proc */  
792    }

Legend:
Removed from v.6  
changed lines
  Added in v.23

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26