/[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 27 by twoaday, Tue Oct 18 07:57:13 2005 UTC revision 278 by twoaday, Mon Jan 15 22:02:04 2007 UTC
# Line 1  Line 1 
1  /* wptCardDlg.cpp - Smart Card support  /* wptCardDlg.cpp - Smart Card support
2   *      Copyright (C) 2003, 2004, 2005 Timo Schulz   *      Copyright (C) 2003-2006 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * 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
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   */
17   * You should have received a copy of the GNU General Public License  
18   * along with WinPT; if not, write to the Free Software Foundation,  #ifdef HAVE_CONFIG_H
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #include <config.h>
20   */  #endif
21    
22  #include <windows.h>  #include <windows.h>
23  #include <commctrl.h>  #include <commctrl.h>
24  #include <ctype.h>  #include <ctype.h>
25    
26  #include "../resource.h"  #include "resource.h"
27  #include "gpgme.h"  #include "gpgme.h"
28  #include "wptTypes.h"  #include "wptTypes.h"
29  #include "wptW32API.h"  #include "wptW32API.h"
30  #include "wptErrors.h"  #include "wptErrors.h"
31  #include "wptRegistry.h"  #include "wptRegistry.h"
32  #include "wptVersion.h"  #include "wptVersion.h"
33  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
34  #include "wptDlgs.h"  #include "wptDlgs.h"
35  #include "wptGPG.h"  #include "wptGPG.h"
36  #include "wptUTF8.h"  #include "wptUTF8.h"
37  #include "wptCardEdit.h"  #include "wptCardEdit.h"
38  #include "wptCard.h"  #include "wptCard.h"
39  #include "wptContext.h"  #include "wptContext.h"
40    #include "StringBuffer.h"
41  int keygen_check_date (SYSTEMTIME * st);  
42    int keygen_check_date (SYSTEMTIME * st);
43  static const char * sex[] = {"Male", "Female", "Undefined", NULL};  
44  static const char * lang[] = {"Undefined", "cs", "de", "en", "es", "fr", "hu",  
45                                "it", "nl", "pt", "ro", "ru", "zh", "at",  #define OPENPGP_APPID "D276000124"
46                                NULL};  
47    /* Possible values for the sex field. */
48  static pin_cb_ctx_s pincb;  static const char *sex[] = {"Male", "Female", "Undefined", NULL};
49    
50  struct {  /* Predefined languages. */
51      int ctlid;  static const char *lang[] = {"Undefined", "cs", "de", "en", "es", "fr", "hu",
52      const char * err;                                "it", "nl", "pt", "ro", "ru", "zh", "at",
53  } attr_tab[] = {                                NULL};
54      {IDC_CEDIT_AID,    ""},  
55      {IDC_CEDIT_VENDOR, "No Vendor"},  /* PIN callback context. */
56      {IDC_CEDIT_VERSION,"No Version"},  static pin_cb_ctx_s pincb;
57      {IDC_CEDIT_SERIAL, "No Serial-No"},  
58      {IDC_CEDIT_NAME,   "No Name"},  /* Attribute table. */
59      {IDC_CEDIT_NAME2,  "No Surname"},  struct {
60      {IDC_CEDIT_KEYURL, "No Key-URL"},      int ctlid;
61      {IDC_CEDIT_LOGIN,  "No Login name"},          const char *err;
62      {0},  } attr_tab[] = {
63  };      {IDC_CEDIT_AID,    ""},
64        {IDC_CEDIT_VENDOR, "No Vendor"},
65        {IDC_CEDIT_VERSION,"No Version"},
66        {IDC_CEDIT_SERIAL, "No Serial-No"},
67  /* XXX: simplify code. */      {IDC_CEDIT_NAME,   "No Name"},
68  char*      {IDC_CEDIT_NAME2,  "No Surname"},
69  get_printable_version (const char *version)      {IDC_CEDIT_KEYURL, "No Key-URL"},
70  {      {IDC_CEDIT_LOGIN,  "No Login name"},    
71      static char buf[16];      {0},
72      char tmp_maj[3]={0}, tmp_min[3]={0};  };
73    
74      strncpy (tmp_maj, version, 2);  
75      strncpy (tmp_min, version+2, 2);  
76      sprintf (buf, "%d.%d", atoi (tmp_maj), atoi (tmp_min));  /* Return all card attributes from @card. @n contains
77      return buf;     the number of items which were returned. */
78  }  char**
79    card_get_items (gpg_card_t card, int *n)
80  /* Return all card attributes from @card. @n contains  {
81     the number of items which were returned. */      static char printver[16];
82  char**      char **p;
83  card_get_items (gpg_card_t card, int *n)  
84  {      *n= 8;
85      char **p;      p = (char **)calloc (*n+1, sizeof (char*));
86        if (!p)
87      *n= 8;          BUG (0);
88      p = (char **)calloc (*n+1, sizeof (char*));      _snprintf (printver, sizeof (printver)-1, "%d.%d",
89      if (!p)                  card->ver[0], card->ver[1]);
90          BUG (0);      p[0] = card->aid;
91      p[0] = card->aid;      p[1] = card->vendor;
92      p[1] = card->vendor;      p[2] = printver;
93      p[2] = get_printable_version (card->version);      p[3] = card->serial;
94      p[3] = card->serial;      p[4] = card->givenname;
95      p[4] = card->givenname;      p[5] = card->surname;
96      p[5] = card->surname;      p[6] = card->url;
97      p[6] = card->url;      p[7] = card->login;
98      p[7] = card->login;      return p;
99      return p;  }
100  }  
101    
102    static int
103  static int  idx_from_lang (const char *_lang)
104  idx_from_lang (const char * _lang)  {
105  {      const char *s;
106      const char * s;      int i;
107      int i;  
108        if (!_lang)
109      if (!_lang)          return 0;
110          return 0;      for (i=0; (s = lang[i]); i++) {
111      for (i=0; (s = lang[i]); i++) {          if (!strcmp (_lang, s))
112          if (!strcmp (_lang, s ))              return i;
113              return i;      }
114      }      return 0;
115      return 0;  }
116  }  
117    
118    static GpgCardEdit*
119  #if 0 /* @unused@ */  create_GpgCardEdit (void)
120  int  {
121  show_card_status (void)      GpgCardEdit *ce;
122  {  
123      int rc = 0;      ce = new GpgCardEdit ();
124      int cardstat;      if (!ce)
125            BUG (0);
126      cardstat = pcsc_get_card_status ();      return ce;
127      if ((cardstat & CARD_STATE_UNAWARE) || (cardstat & CARD_STATE_UNAVAIL))  }
128          rc = WPTERR_NOREADER;  
129      else if (cardstat & CARD_STATE_EMPTY)  
130          rc = WPTERR_NOCARD;      /* Check if there is a card in the reader and analyze the
131      if (rc) {     returned information.
132          msg_box (NULL, winpt_strerror (rc), _("Card Manager"), MB_ERR);     Return value: card context or NULL on error. */
133          return -1;  gpg_card_t
134      }  gpg_card_load (void)
135      return 0;  {
136  } /* show_card_status */      gpgme_error_t err;
137  #endif      GpgCardEdit *ce;
138        gpg_card_t card = NULL;
139  /* Check if there is a card in the reader and analyze the      struct card_cb_s cb = {0};
140     returned information.      
141     Return value: card context or NULL on error. */      ce = create_GpgCardEdit ();
142  gpg_card_t      memset (&cb, 0, sizeof (cb));
143  gpg_card_load (void)      ce->setCallback (card_callback, &cb);
144  {      err = ce->getCardStatus (&card);
145      gpgme_error_t err;      if (err) {
146      GpgCardEdit *ce;          msg_box (NULL, gpgme_strerror (err), _("Card Manager"), MB_ERR);
147      gpg_card_t card = NULL;          goto leave;
148      struct card_cb_s cb = {0};      }
149        
150      ce = new GpgCardEdit ();      if (!card->aid ||
151      if (!ce)          strncmp (card->aid, OPENPGP_APPID, strlen (OPENPGP_APPID))) {
152          BUG (0);          msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);
153      memset (&cb, 0, sizeof (cb));          gpg_card_release (card);
154      ce->setCallback (card_callback, &cb);          card = NULL;
155      err = ce->getCardStatus (&card);      }
156      if (err) {      else {
157          msg_box (NULL, gpgme_strerror (err), _("Card Manager"), MB_ERR);          struct winpt_key_s key;
158          goto leave;  
159      }          memset (&key, 0, sizeof (key));
160            winpt_get_pubkey (card->fpr[1]+32, &key);
161      if (!card->aid || strncmp (card->aid, "D276000124", 10)) {          if (key.ext) {
162          msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);              key.ext->card_type = m_strdup (card->card_type);
163          gpg_card_release (card);              /* memory will be released in gpg_keycache_release (). */
164          card = NULL;          }
165      }      }
166      else {  
167          struct winpt_key_s key;  leave:
168          memset (&key, 0, sizeof (key));      delete ce;
169          winpt_get_pubkey (card->fpr[0], &key);      return card;
170          if (key.ext) {  }
171              key.ext->card_type = strdup (card->card_type);  
172              if (!key.ext->card_type)  
173                  BUG (NULL);  /* Print human friendly fingerprint to control @id in the
174              /* memory will be released in gpg_keycache_release (). */     dialog @dlg. @fpr contains the raw fingerprint. */
175          }  static void
176      }  print_fpr (HWND dlg, int id, const char * fpr)
177    {
178  leave:      char buf[128], dig[2];    
179      delete ce;      size_t i, c;
180      return card;  
181  }      if (!fpr)
182            strcpy (buf, _("No Fingerprint"));
183        else {
184  /* Print human friendly fingerprint to control @id in the          memset (buf, 0, sizeof (buf));
185     dialog @dlg. @fpr contains the raw fingerprint. */          for (i=0, c=0; i < strlen (fpr); i++) {
186  static void              dig[0] = fpr[i]; dig[1] = 0;
187  print_fpr (HWND dlg, int id, const char * fpr)              strcat (buf, dig);
188  {              if (++c == 4) {
189      char buf[128], dig[2];                      strcat (buf, " ");
190      size_t i, c;                  c=0;
191                }
192      if (!fpr)          }
193          strcpy (buf, _("No Fingerprint"));      }
194      else {      SetDlgItemText (dlg, id, buf);
195          memset (buf, 0, sizeof (buf));  }
196          for( i=0, c=0; i < strlen (fpr); i++) {  
197              dig[0] = fpr[i]; dig[1] = 0;  
198              strcat (buf, dig);  /* Fill in all card information from @card. into the corresponding
199              if (++c == 4) {     dialog item fields in the dialog @dlg.
200                  strcat (buf, " ");     Return value: 0 on success. */
201                  c=0;  static int
202              }  card_status (HWND dlg, gpg_card_t card)
203          }  {
204      }      static int fprbuf[] = {IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};
205      SetDlgItemText (dlg, id, buf);      static int fprtime[] = {IDC_CEDIT_SIG_FPRTIME, IDC_CEDIT_DEC_FPRTIME,
206  }                              IDC_CEDIT_AUTH_FPRTIME, 0};
207        const char *s;
208        char **attrs;
209  /* Fill in all card information from @card. into the corresponding      char cardinf[128];
210     dialog item fields in the dialog @dlg.      int idx=0, n=0;
211     Return value: 0 on success. */  
212  static int      if (!card->aid) {
213  card_status (HWND dlg, gpg_card_t card)          msg_box (dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR);
214  {          return -1;
215      static int fprbuf[] = {IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};      }
216      static int fprtime[] = {IDC_CEDIT_SIG_FPRTIME, IDC_CEDIT_DEC_FPRTIME, IDC_CEDIT_AUTH_FPRTIME, 0};      SetDlgItemText (dlg, IDC_CEDIT_AID, card->aid);
217      const char *s;      SetDlgItemInt (dlg, IDC_CEDIT_SIGCOUNT, card->sig_count, TRUE);
218      char **attrs;  
219      char cardinf[128];      for (idx=0; fprbuf[idx]; idx++) {
220      int idx=0, n=0;          print_fpr (dlg, fprbuf[idx], card->fpr[idx]);
221            s = card->fpr_created_str[idx];
222      if (!card->aid) {          if (!s) /* no keys on the card. */
223          msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR );              s = "";
224          return -1;          SetDlgItemText (dlg, fprtime[idx], s);
225      }      }
226      SetDlgItemText (dlg, IDC_CEDIT_AID, card->aid);  
227      SetDlgItemInt (dlg, IDC_CEDIT_SIGCOUNT, card->sig_count, TRUE);      attrs = card_get_items (card, &n);
228        for (idx=1; attr_tab[idx].ctlid; idx++) {
229      for (idx=0; fprbuf[idx]; idx++) {          s = attrs[idx];
230          print_fpr (dlg, fprbuf[idx], card->fpr[idx]);          SetDlgItemText (dlg, attr_tab[idx].ctlid,
231          SetDlgItemText (dlg, fprtime[idx], card->fpr_created_str[idx]);                          s && *s? s : attr_tab[idx].err);
232      }      }
233        safe_free (attrs);
234      attrs = card_get_items (card, &n);  
235      for (idx=1; attr_tab[idx].ctlid; idx++) {      idx = idx_from_lang (card->lang);    
236          s = attrs[idx];      SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0);
237          SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err);  
238      }      switch (card->sex) {
239      free (attrs);      case 'm': idx=0; break;
240        case 'f': idx=1; break;
241      idx = idx_from_lang (card->lang);          default :
242      SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0);      case 'u': idx=2; break;
243        }
244      switch (card->sex) {      SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0);
245      case 'm': idx=0; break;  
246      case 'f': idx=1; break;      s = card->serial;
247      default :      while (s && *s == '0') s++;
248      case 'u': idx=2; break;      _snprintf (cardinf, sizeof (cardinf)-1,
249      }                 "Card Edit - %s serial no. %s version %d.%d",
250      SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0);                 card->card_type, s, card->ver[0], card->ver[1]);
251        SetWindowText (dlg, cardinf);
252      s = card->serial;  
253      while (s && *s == '0') s++;      return 0;
254      _snprintf (cardinf, sizeof (cardinf)-1,  }
255                 "Card Edit - OpenPGP card no. %s version %s",  
256                 s, get_printable_version (card->version));        
257      SetWindowText (dlg, cardinf);  /* Initialize the enum combox boxes in dialog @dlg. */
258    static void
259      return 0;  prepare_dialog (HWND dlg)
260  }  {
261        const char * s;
262        int i;
263  /* Initialize the enum combox boxes in dialog @dlg. */  
264  static void      for (i=0; (s = sex[i]); i++)
265  prepare_dialog (HWND dlg)          SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s);
266  {      SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0);
267      const char * s;      for (i=0; (s = lang[i]); i++)
268      int i;          SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s);
269        SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0);
270      for (i=0; (s = sex[i]); i++)  }
271          SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s);  
272      SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0);  
273      for (i=0; (s = lang[i]); i++)  /* Return 0 if the given string @str has the proper format. */
274          SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s);  static int
275      SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0);  check_string (const char *str, int flags)
276  }  {
277        size_t i;
278    
279        for (i=0; i < strlen (str); i++) {
280  /* Return 0 if the given string @str has the proper format. */          if (flags & 0x02 && !isalpha (str[i]))
281  static int              return -1;
282  check_string (const char *str, int flags)      }
283  {      return 0;
284      size_t i;  }
285      for (i=0; i < strlen (str); i++) {  
286          if (flags & 0x02 && !isalpha (str[i]))  
287              return -1;  static int
288      }  do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s *cb, gpg_card_t card)
289      return 0;  {
290  }      static struct {
291            int id;
292            int cmd;
293  static int          int us_ascii;
294  do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s *pincb, gpg_card_t card)          int changed;
295  {      } idctl[] = {
296      static struct {          {IDC_CEDIT_NAME,  GPG_EDITCARD_NAME,  1,  0},
297          int id;          {IDC_CEDIT_LANG2, GPG_EDITCARD_LANG,  1,  0},
298          int cmd;          {IDC_CEDIT_SEX2,  GPG_EDITCARD_SEX,   1|1,0},
299          int us_ascii;          {IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1|4,0},
300          int changed;          {IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1,  0},
301      } idctl[] = {          {0}
302          {IDC_CEDIT_NAME,  GPG_EDITCARD_NAME,  1,  0},      };    
303          {IDC_CEDIT_LANG2, GPG_EDITCARD_LANG,  1,  0},      gpgme_error_t err;
304          {IDC_CEDIT_SEX2,  GPG_EDITCARD_SEX,   1|1,0},      GpgCardEdit *ce;
305          {IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1|4,0},      char buf[256], tmp[128];
306          {IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1,  0},      int errc=0, use_arg2 = 0;
307          {0}      int i, id, n=0;
308      };      
309      gpgme_error_t err;      /* XXX rewrite the entire function */
310      GpgCardEdit *ce;      for( i=0; idctl[i].id; i++ ) /* reset */
311      char buf[256], tmp[128];          idctl[i].changed = 0;
312      int errc=0, use_arg2 = 0;      
313      int i, id, n=0;      if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_LANG2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
314            idctl[1].changed = 1;
315      /* XXX rewrite the entire function */          n++;
316      for( i=0; idctl[i].id; i++ ) /* reset */      }
317          idctl[i].changed = 0;      if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_SEX2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
318                idctl[2].changed = 1;
319      if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_LANG2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {          n++;
320          idctl[1].changed = 1;      }
321          n++;      
322      }      if( SendDlgItemMessage( dlg, IDC_CEDIT_NAME2, EM_GETMODIFY, 0, 0 ) ) {
323      if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_SEX2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {          idctl[0].changed = 1;
324          idctl[2].changed = 1;          n++;
325          n++;      }
326      }      for( i=0; (id = idctl[i].id); i++ ) {
327                if( SendDlgItemMessage( dlg, id, EM_GETMODIFY, 0, 0 ) ) {
328      if( SendDlgItemMessage( dlg, IDC_CEDIT_NAME2, EM_GETMODIFY, 0, 0 ) ) {              idctl[i].changed = 1;
329          idctl[0].changed = 1;              n++;
330          n++;          }
331      }      }
332      for( i=0; (id = idctl[i].id); i++ ) {      if (!cb || !card) /* just return the changed elements */
333          if( SendDlgItemMessage( dlg, id, EM_GETMODIFY, 0, 0 ) ) {          return n;
334              idctl[i].changed = 1;      if (!n)
335              n++;          return 0;
336          }      if (!cb->apin) {
337      }          msg_box (dlg, _("No PINs found."), _("Card Edit"), MB_ERR);
338      if (!pincb || !card) /* just return the changed elements */          return 0;
339          return n;      }
340      if (!n)      
341          return 0;      ce = create_GpgCardEdit ();
342      if (!pincb->apin) {      ce->setAdminPIN (cb->apin);
343          msg_box (dlg, _("No PINs found."), _("Card Edit"), MB_ERR);      for( i=0; idctl[i].id; i++ ) {
344          return 0;          if( idctl[i].changed ) {
345      }              GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );
346                    if (idctl[i].us_ascii && is_8bit_string (buf)) {
347      ce = new GpgCardEdit ();                  msg_box (dlg, _("Only plain ASCII is currently allowed."),
348      if (!ce)                           _("Card Edit"), MB_ERR);
349          BUG (NULL);                  errc--; continue;
350      ce->setAdminPIN (pincb->apin);              }
351      /*ce->setPIN (pincb->upin);*/              if( (idctl[i].us_ascii & 2) && check_string( buf, 2 ) ) {
352      for( i=0; idctl[i].id; i++ ) {                  msg_box( dlg, _("Only alphabetic characters are allowed."),
353          if( idctl[i].changed ) {                           _("Card Edit"), MB_ERR );
354              GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );                  errc--; continue;
355              if (idctl[i].us_ascii && is_8bit_string (buf)) {              }
356                  msg_box (dlg, _("Only plain ASCII is currently allowed."),              if ((idctl[i].us_ascii & 4) &&
357                           _("Card Edit"), MB_ERR);                  (!strchr (buf, ':') || !strstr (buf, "//"))) {
358                  errc--; continue;                  /* XXX: better URL check. */
359              }                  msg_box (dlg, _("Invalid URL."), _("Card Edit"), MB_ERR);
360              if( (idctl[i].us_ascii & 2) && check_string( buf, 2 ) ) {                  errc--; continue;
361                  msg_box( dlg, _("Only alphabetic characters are allowed."),              }
362                           _("Card Edit"), MB_ERR );              if( idctl[i].cmd == GPG_EDITCARD_NAME ) {
363                  errc--; continue;                  /* The "name" command actually needs two fields */
364              }                  GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
365              if ((idctl[i].us_ascii & 4) &&                  use_arg2 = 1;
366                  (!strchr (buf, ':') || !strstr (buf, "//"))) {              }
367                  /* XXX: better URL check. */              else
368                  msg_box (dlg, _("Invalid URL."), _("Card Edit"), MB_ERR);                  use_arg2 = 0;
369                  errc--; continue;              err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL);
370              }              if (err) {
371              if( idctl[i].cmd == GPG_EDITCARD_NAME ) {                  log_box (_("Card Edit"), MB_ERR,
372                  /* The "name" command actually needs two fields */                           _("Could not modify card attribute: %s"),
373                  GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );                           gpgme_strerror (err));
374                  use_arg2 = 1;                  errc--;
375              }                  /* If no card is inserted, we leave the loop. */
376              else                  if (gpgme_err_code (err) == GPG_ERR_CARD_NOT_PRESENT)
377                  use_arg2 = 0;                      break;
378              err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL);              }
379              if (err) {          }
380                  log_box (_("Card Edit"), MB_ERR,      }
381                           _("Could not modify card attribute: %s"),      if (!errc) {
382                           gpgme_strerror (err));          /* if the operation(s) succeeded, reset the modify flag for each control */
383                  errc--;          for( i = 0; idctl[i].id; i++ )
384                  /* If no card is inserted, we leave the loop. */              SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 );
385                  if (gpgme_err_code (err) == GPG_ERR_CARD_NOT_PRESENT)          msg_box( dlg, _("Card attribute changed."), _("Card Edit"), MB_OK );
386                      break;          SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );
387              }          SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );
388          }      }
389      }      delete ce;
390      if (!errc) {      return errc;
391          /* if the operation(s) succeeded, reset the modify flag for each control */  }
392          for( i = 0; idctl[i].id; i++ )  
393              SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 );  
394          msg_box( dlg, _("Card attribute changed."), _("Card Edit"), MB_OK );  /* Cleanup pin callback @ctx. */
395          SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );  void
396          SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );  free_pincb (struct pin_cb_ctx_s *ctx)
397      }  {
398      delete ce;      if (!ctx)
399      return errc;          return;
400  } /* do_proc_card_cmds */      free_if_alloc (ctx->info_text);
401        sfree_if_alloc (ctx->upin);
402        sfree_if_alloc (ctx->apin);
403  /* Cleanup pin callback @ctx. */  }
404  void  
405  free_pincb (struct pin_cb_ctx_s *ctx)  
406  {  /* Request a PIN from the user. @which decided if the
407      if (!ctx)     normal PIN or the admin PIN will be requested.
408          return;     @card is used to show some information to the user.
409      free_if_alloc (ctx->info_text);     @pincb is the actuall callback context.
410      sfree_if_alloc (ctx->upin);     Return value: 0 on success. */
411      sfree_if_alloc (ctx->apin);  static int
412  }  do_askpin (HWND dlg, int which, gpg_card_t card,
413               struct pin_cb_ctx_s *cb)
414    {
415  /* Request a PIN from the user. @which decided if the      const char *s;
416     normal PIN or the admin PIN will be requested.      char *p;
417     @card is used to show some information to the user.  
418     @pincb is the actuall callback context.      if( (which == CARD_ADMIN_PIN && cb->apin) ||
419     Return value: 0 on success. */          (which == CARD_USER_PIN && cb->upin) )
420  static int          return 0;
421  do_askpin (HWND dlg, int which, gpg_card_t card,      
422             struct pin_cb_ctx_s *pincb)      if (which == CARD_ADMIN_PIN)
423  {          s = _("Please enter the 'Admin PIN'");
424      const char * s, * fmt;      else if (which == CARD_USER_PIN)
425      const char * n1, * n2, * serial;          s = _("Please enter the 'User PIN'");
426      char * p;      else
427      size_t n;          s = _("Please enter the PIN");
428        cb->which = which;
429      if( (which == CARD_ADMIN_PIN && pincb->apin) ||      free_if_alloc (cb->info_text);
430          (which == CARD_USER_PIN && pincb->upin) )      if (card) {
431          return 0;          StringBuffer buf;
432        
433      if (which == CARD_ADMIN_PIN)          buf = s;
434          s = _("Please enter the 'Admin PIN'");          buf = buf + "\nName: " + (card->givenname?card->givenname: "No");
435      else if (which == CARD_USER_PIN)          buf = buf + " " + (card->surname?card->surname : "Name");
436          s = _("Please enter the 'User PIN'");          buf = buf + "\nSerial-No:" + card->serial;
437      else          p = buf.getBufferCopy ();
438          s = _("Please enter the PIN");      }
439      pincb->which = which;      else
440      free_if_alloc( pincb->info_text );          p = cb->info_text = m_strdup (s);
441      if( card ) {      DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,
442          fmt = _("%s\nName: %s %s\nSerial-No: %s\n");                      pin_cb_dlg_proc, (LPARAM)cb);
443          n1 = card->givenname;      if (!cb->apin && !cb->upin) {
444          n2 = card->surname;          safe_free (cb->info_text);
445          if( !n1 || !n2 ) {          return -1;
446              n1 = "No"; n2 = "Name";      }
447          }          return 0;
448          serial = card->serial;  }
449          if (!serial)  
450              serial = "No Serial ID";  
451          n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;  /* Dialog box procedure for card edit. */
452          p = pincb->info_text = new char[strlen( s )+n+1 ];  BOOL CALLBACK
453          if( !p )  card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
454              BUG (0);  {
455          sprintf( p, fmt, s, n1, n2, serial );      static gpg_card_t card;    
456      }      char tmp[128];
457      else {      size_t n=0;
458          p = pincb->info_text = m_strdup (s);  
459          if (!p)      switch (msg) {
460              BUG (0);      case WM_INITDIALOG:
461      }          card = (gpg_card_t)lparam;
462      DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,          if (!card)
463                      pin_cb_dlg_proc, (LPARAM)pincb);              BUG (0);
464      if (!pincb->apin && !pincb->upin) {          prepare_dialog (dlg);
465          safe_free (pincb->info_text);          if (card_status (dlg, card ))
466          return -1;              EndDialog (dlg, TRUE);
467      }          SetDlgItemText (dlg, IDC_CEDIT_NAMEINF, _("&Name"));
468      return 0;          SetDlgItemText (dlg, IDC_CEDIT_LANGINF, _("&Language"));
469  }          SetDlgItemText (dlg, IDC_CEDIT_KURLINF, _("&Key-URL"));
470            SetDlgItemText (dlg, IDC_CEDIT_LOGINF, _("&Login"));
471            SetDlgItemText (dlg, IDC_CEDIT_SEXINF, _("&Sex"));
472  /* Dialog box procedure for card edit. */          SetDlgItemText (dlg, IDOK, _("&OK"));
473  BOOL CALLBACK          SetDlgItemText (dlg, IDCANCEL, _("&Exit"));
474  card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)          SetDlgItemText (dlg, IDC_CEDIT_NEWKEYS, _("&New keys"));
475  {          SetDlgItemText (dlg, IDC_CEDIT_CHPIN, _("Change &PIN"));
476      static gpg_card_t card;              center_window (dlg, NULL);
477      char tmp[128];          SetForegroundWindow (dlg);
478      size_t n=0;          return TRUE;
479    
480      switch (msg) {      case WM_DESTROY:
481      case WM_INITDIALOG:          free_pincb (&pincb);
482          card = (gpg_card_t)lparam;          memset (&pincb, 0, sizeof pincb);
483          if (!card)          break;
484              BUG (0);  
485          prepare_dialog (dlg);      case WM_COMMAND:
486          if (card_status (dlg, card ))          switch (HIWORD (wparam)) {
487              EndDialog (dlg, TRUE);          case CBN_KILLFOCUS:
488          center_window (dlg, NULL);          case CBN_EDITCHANGE:
489          SetForegroundWindow (dlg);          case CBN_EDITUPDATE:
490          return TRUE;              int ctlid = GetDlgCtrlID ((HWND)lparam);
491                int dstid = 0;
492      case WM_DESTROY:  
493          free_pincb (&pincb);              switch (ctlid) {
494          memset (&pincb, 0, sizeof pincb);              case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break;
495          break;              case IDC_CEDIT_SEX:  dstid = IDC_CEDIT_SEX2; break;
496                }      
497      case WM_COMMAND:              GetDlgItemText (dlg, ctlid, tmp, sizeof (tmp)-1);
498          switch( HIWORD( wparam ) ) {              SetDlgItemText (dlg, dstid, tmp);
499          case CBN_KILLFOCUS:              break;
500          case CBN_EDITCHANGE:          }
501          case CBN_EDITUPDATE:          switch (LOWORD (wparam)) {
502              int ctlid = GetDlgCtrlID( (HWND)lparam );          case IDC_CEDIT_CHPIN:
503              int dstid = 0;              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg,
504                                card_changepin_dlg_proc, 0);
505              switch (ctlid) {              break;
506              case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break;  
507              case IDC_CEDIT_SEX:  dstid = IDC_CEDIT_SEX2; break;          case IDC_CEDIT_NEWKEYS:
508              }                    if (item_get_text_length (dlg, IDC_CEDIT_FPR1) > 0) {
509              GetDlgItemText (dlg, ctlid, tmp, sizeof (tmp)-1);                  int id = msg_box (dlg,
510              SetDlgItemText (dlg, dstid, tmp);                      _("This operation will override the keys on the card.\n"
511              break;                        "Continue?"), _("Card Edit"), MB_WARN|MB_YESNO);
512          }                  if (id == IDNO)
513          switch( LOWORD( wparam ) ) {                      return TRUE;
514          case IDC_CEDIT_CHPIN:              }
515              DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg,              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
516                              card_changepin_dlg_proc, NULL );                              dlg, card_keygen_dlg_proc, 0);
517              break;              break;
518    
519          case IDC_CEDIT_NEWKEYS:          case IDOK:
520              if (item_get_text_length (dlg, IDC_CEDIT_FPR1) > 0) {              n = do_proc_card_cmds (dlg, NULL, NULL);
521                  int id = msg_box (dlg,              if (n) {
522                      _("This operation will override the keys on the card.\n"                  if (do_askpin (dlg, CARD_ADMIN_PIN, card, &pincb))
523                        "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);                      EndDialog (dlg, FALSE);
524                  if (id == IDNO)              }
525                      return TRUE;              do_proc_card_cmds (dlg, &pincb, card);
526              }              free_pincb (&pincb);
527              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,              if (!n)
528                              glob_hwnd, card_keygen_dlg_proc, NULL);                  EndDialog (dlg, TRUE);
529              break;              break;
530    
531          case IDOK:          case IDCANCEL:
532              n = do_proc_card_cmds (dlg, NULL, NULL);              EndDialog (dlg, FALSE);
533              if (n) {              break;
534                  if (do_askpin (dlg, CARD_ADMIN_PIN, card, &pincb))          }
535                      EndDialog (dlg, FALSE);          break;
536              }      }
537              do_proc_card_cmds (dlg, &pincb, card);  
538              free_pincb (&pincb);      return FALSE;
539              if( !n )  }
540                  EndDialog( dlg, TRUE );  
541              break;  
542    static int /* fixme: works only roughly */
543          case IDCANCEL:  calc_days (int y2, int m2, int d2,
544              EndDialog( dlg, FALSE );             int y1, int m1, int d1)
545              break;  
546          }  {
547          break;      int n=0;
548      }      
549        if ((y2-y1) > 0)
550      return FALSE;          n += (y2-y1)*365;
551  }      if ((m2-m1) > 0)
552            n += (m2-m1)*30;
553        if ((d2-d1) > 0)
554  static int /* fixme: works only roughly */          n += (d2-d1);
555  calc_days (int y2, int m2, int d2,      else if ((d2-d1) < 0)
556             int y1, int m1, int d1)          n -= (d1-d2);
557        return n;
558  {  }
559      int n=0;  
560        
561      if ((y2-y1) > 0)  /* Dialog box procedure for the key generation on cards. */
562          n += (y2-y1)*365;  BOOL CALLBACK
563      if ((m2-m1) > 0)  card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
564          n += (m2-m1)*30;  {
565      if ((d2-d1) > 0)      gpgme_error_t err;
566          n += (d2-d1);      GpgCardEdit *ce;
567      else if ((d2-d1) < 0)      char name[128], email[128], comment[128];
568          n -= (d1-d2);      char pass[128];
569      return n;      int card_flags = GPG_CARDFLAG_NONE;
570  }      int expires=0, valid=0;
571        DWORD n;
572    
573  /* Dialog box procedure for the key generation on cards. */      switch (msg) {
574  BOOL CALLBACK      case WM_INITDIALOG:
575  card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)          CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);
576  {          CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);
577      gpgme_error_t err;          CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);
578      GpgCardEdit *ce;          EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);
579      char name[128], email[128], comment[128];          SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0,
580      char pass[128];                              (LPARAM)(const char*)"RSA");
581      int card_flags = GPG_CARDFLAG_NONE;          SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
582      int expires=0, valid;          SetDlgItemText (dlg, IDC_CKEYGEN_PKINF, _("Pubkey algorithm"));
583      size_t n;          SetDlgItemText (dlg, IDC_CKEYGEN_NAMEINF, _("&Name"));
584            SetDlgItemText (dlg, IDC_CKEYGEN_CMTINF, _("&Comment (optional)"));
585      switch (msg) {          SetDlgItemText (dlg, IDC_CKEYGEN_EXPDATEINF, _("&Expire date"));
586      case WM_INITDIALOG:          SetDlgItemText (dlg, IDC_CKEYGEN_PWDINF, _("Off-card passphrase"));
587          center_window (dlg, NULL);          SetDlgItemText (dlg, IDC_CKEYGEN_NEVER, _("&Never"));
588          CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);          SetDlgItemText (dlg, IDC_CKEYGEN_MAILINF, _("Email &address"));
589          CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);          SetDlgItemText (dlg, IDC_CKEYGEN_REPLACE, _("Overwrite old keys on the card"));
590          CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);          SetDlgItemText (dlg, IDC_CKEYGEN_BACKUP, _("Make off-card backup of encryption key"));
591          EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);          SetWindowText (dlg, _("Card Key Generation"));
592          SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0,          center_window (dlg, NULL);
593                              (LPARAM)(const char*)"RSA");          SetForegroundWindow (dlg);
594          SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);          SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
595          SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));          return FALSE;
596          SetForegroundWindow (dlg);  
597          return FALSE;      case WM_COMMAND:
598            if (HIWORD (wparam) == BN_CLICKED &&
599      case WM_SYSCOMMAND:              (LOWORD (wparam) == IDC_CKEYGEN_BACKUP) ||
600          if (LOWORD (wparam) == SC_CLOSE)               LOWORD (wparam) == IDC_CKEYGEN_NEVER) {
601              EndDialog (dlg, TRUE);              EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID),
602          return FALSE;                            IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER)? 0: 1);
603                EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS),
604      case WM_COMMAND:                            IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)? 1 : 0);
605          if (HIWORD (wparam) == BN_CLICKED &&              return TRUE;
606              (LOWORD (wparam) == IDC_CKEYGEN_BACKUP) ||          }      
607               LOWORD (wparam) == IDC_CKEYGEN_NEVER) {  
608              EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID),          switch (LOWORD (wparam)) {
609                            IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER)? 0: 1);          case IDOK:
610              EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS),              n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);
611                            IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)? 1 : 0);              if (!n) {
612              return TRUE;                  msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);
613          }                        return TRUE;
614                }
615          switch (LOWORD (wparam)) {              if (n < 5) {
616          case IDOK:                  msg_box (dlg, _("Name must be at least 5 characters long."),
617              n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);                           _("Card Edit"), MB_INFO);
618              if (!n) {                  return TRUE;
619                  msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);              }
620                  return TRUE;              n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);
621              }              if (!n) {
622              if (n < 5) {                  msg_box (dlg, _("Please enter your e-mail address."),
623                  msg_box (dlg, _("Name must be at least 5 characters long."),                           _("Card Edit"), MB_ERR);
624                           _("Card Edit"), MB_INFO);                  return TRUE;
625                  return TRUE;              }
626              }              GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);
627              n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);              GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);
628              if (!n) {              if (check_email_address (email) || n < 3) {
629                  msg_box (dlg, _("Please enter your e-mail address."),                  msg_box (dlg, _("Please enter a valid e-mail address."),
630                           _("Card Edit"), MB_ERR);                           _("Card Edit"), MB_ERR);
631                  return TRUE;                  return TRUE;
632              }              }
633              GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);              n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
634              GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);              if (!n && IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) {
635              if (!strchr (email, '@') || n < 3) {                  msg_box (dlg, _("Please enter an off-card passphrase."),
636                  msg_box (dlg, _("Please enter a valid e-mail address."),                           _("Card Edit"), MB_ERR);
637                           _("Card Edit"), MB_ERR);                  return TRUE;
638                  return TRUE;              }
639              }              n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);
640              n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);              if (n > 0)
641              if (!n && IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) {                  GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1);
642                  msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);              if (is_8bit_string (name) || n > 0 && is_8bit_string (comment)) {
643                  return TRUE;                  msg_box (dlg, _("Please use plain ASCII charset for the fields."),
644              }                           _("Card Edit"), MB_INFO);
645              n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);                  return TRUE;
646              if (n > 0)              }
647                  GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1);              memset (&pincb, 0, sizeof (pincb));
648              if (is_8bit_string (name) ||is_8bit_string (comment)) {              if (do_askpin (dlg, CARD_ADMIN_PIN, NULL, &pincb)) {
649                  msg_box (dlg, _("Please use plain ASCII charset for the fields."),                  free_pincb (&pincb);
650                           _("Card Edit"), MB_INFO);                  return TRUE;
651                  return TRUE;              }
652              }              if (do_askpin (dlg, CARD_USER_PIN, NULL, &pincb)) {
653              memset (&pincb, 0, sizeof (pincb));                  free_pincb (&pincb);
654              if (do_askpin (dlg, CARD_ADMIN_PIN, NULL, &pincb)) {                  return TRUE;
655                  free_pincb (&pincb);              }
656                  return TRUE;              ce = create_GpgCardEdit ();
657              }  
658              if (do_askpin (dlg, CARD_USER_PIN, NULL, &pincb)) {              expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);
659                  free_pincb (&pincb);              if (expires) {
660                  return TRUE;                  SYSTEMTIME st, ct;
661              }  
662              ce = new GpgCardEdit ();                  DateTime_GetSystemtime (GetDlgItem (dlg, IDC_CKEYGEN_VALID), &st);
663              if (!ce)                  if (!keygen_check_date (&st)) {
664                  BUG (0);                      msg_box (dlg, _("The date you have chosen has already passed."),
665                                             _("Card Edit"), MB_ERR);
666              expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);                      free_pincb (&pincb);
667              if (expires) {                      delete ce;
668                  SYSTEMTIME st, ct;                      return TRUE;
669                  DateTime_GetSystemtime (GetDlgItem (dlg, IDC_CKEYGEN_VALID), &st);                  }
670                  if (!keygen_check_date (&st)) {                  GetSystemTime (&ct);
671                      msg_box (dlg, _("The date you have chosen lies in the past."),                  /* XXX this is not very precise */
672                               _("Card Edit"), MB_ERR);                  valid = calc_days (st.wYear, st.wMonth, st.wDay,
673                      delete ce;                                     ct.wYear, ct.wMonth, ct.wDay);
674                      return TRUE;              }
675                  }              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))
676                  GetSystemTime (&ct);                  card_flags |= GPG_CARDFLAG_REPLACE;
677                  /* XXX this is not very precise */              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))
678                  valid = calc_days (st.wYear, st.wMonth, st.wDay,                  card_flags |= GPG_CARDFLAG_BAKENC;
679                                     ct.wYear, ct.wMonth, ct.wDay);              ce->setKeygenPassphrase (pass);
680              }              ce->setPIN (pincb.upin);
681              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))              ce->setAdminPIN (pincb.apin);
682                  card_flags |= GPG_CARDFLAG_REPLACE;              
683              if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))              SetCursor (LoadCursor (NULL, IDC_WAIT));
684                  card_flags |= GPG_CARDFLAG_BAKENC;              err = ce->genKey (card_flags, name, email, n? comment: NULL,
685              ce->setKeygenPassphrase (pass);                                expires? valid : 0, NULL);
686              ce->setPIN (pincb.upin);              SetCursor (LoadCursor (NULL, IDC_ARROW));
687              ce->setAdminPIN (pincb.apin);              
688                            if (gpgme_err_code (err) == GPG_ERR_CANCELED)
689              SetCursor (LoadCursor (NULL, IDC_WAIT));                  msg_box (dlg, _("Operation was canceled. It seems that there are "
690              err = ce->genKey (card_flags, name, email, n? comment: NULL,                                  "existing\nkeys on the cards. You need to mark the "
691                                expires? valid : 0, NULL);                                  "'Overwrite' flag."), _("Card Edit"), MB_INFO);
692                            else
693              SetCursor (LoadCursor (NULL, IDC_ARROW));              if (err)
694              /*                  msg_box (dlg, "The operation does not succeed.\n"
695              if (gpgme_err_code (err) == GPG_ERR_CANCELLED)                                "Please make sure you entered the right PIN's."
696                  msg_box (dlg, _("Operation was canceled. It seems that there are "                                , _("Card Edit"), MB_ERR);
697                                  "existing\nkeys on the cards. You need to mark the "              else
698                                  "'Overwrite' flag."), _("Card Edit"), MB_INFO);                  msg_box (dlg, _("Keys successfully created."),
699              else */                           _("Card Edit"), MB_OK);
700              if (err)              wipememory (pass, sizeof (pass));
701                  msg_box (dlg, "The operation does not succeed.\n"              free_pincb (&pincb);
702                                "Please make sure you entered the right PIN's."              delete ce;
703                                , _("Card Edit"), MB_ERR);              EndDialog (dlg, TRUE);
704              else              return TRUE;
705                  msg_box (dlg, _("Keys successfully created."),  
706                           _("Card Edit"), MB_OK);          case IDCANCEL:
707              memset (pass, 0, sizeof (pass));              EndDialog (dlg, FALSE);
708              free_pincb (&pincb);              return TRUE;
709              delete ce;          }
710              return TRUE;          break;
711        }
712          case IDCANCEL:      return FALSE;
713              EndDialog (dlg, FALSE);  }
714              return FALSE;  
715          }  
716          break;  /* Check if the given pinlen is valid.
717      }     @which decided what PIN will be used.
718      return FALSE;     @pinlen is the pin length entered by the user.
719  }     Return value: 0 on success. */
720    static int
721    check_pin_len (int which, int flag, int pinlen)
722  /* Check if the given pinlen is valid.  {
723     @which decided what PIN will be used.      if (!pinlen) {
724     @pinlen is the pin length entered by the user.          if (flag)
725     Return value: 0 on success. */              msg_box (NULL, _("Please enter the old card PIN."), _("Card Edit"), MB_ERR);
726  static int          else
727  check_pin_len (int which, int flag, int pinlen)              msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);
728  {          return -1;
729      if (!pinlen) {      }
730          if (flag)      if (which == CARD_ADMIN_PIN
731              msg_box (NULL, _("Please enter the old card PIN."), _("Card Edit"), MB_ERR);          && pinlen < 8) {
732          else          msg_box (NULL, _("Admin PIN must be at least 8 characters."), _("Card Edit"), MB_ERR);
733              msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);          return -1;
734          return -1;      }
735      }      if (which == CARD_USER_PIN
736      if (which == CARD_ADMIN_PIN          && pinlen < 6) {
737          && pinlen < 8) {          msg_box (NULL, _("PIN must be at least 6 characters."), _("Card Edit"), MB_ERR);
738          msg_box (NULL, _("Admin PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);          return -1;
739          return -1;      }
740      }      return 0;
741      if (which == CARD_USER_PIN  }
742          && pinlen < 6) {  
743          msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR);  /* Dialog box procedure to change the PIN. */
744          return -1;  BOOL CALLBACK
745      }  card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
746      return 0;  {    
747  }      static int hide = 1;
748        gpgme_error_t err;
749  /* Dialog box procedure to change the PIN. */      GpgCardEdit *ce;    
750  BOOL CALLBACK      char pold[128], pnew[128], pnew2[128];
751  card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      int which = 0;
752  {          DWORD n;
753      static int hide = 1;  
754      gpgme_error_t err;      switch( msg ) {
755      GpgCardEdit *ce;          case WM_INITDIALOG:
756      char pold[128], pnew[128], pnew2[128];          hide = 1;
757      int which;          CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);
758      size_t n;          center_window (dlg, NULL);
759            CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);
760      switch( msg ) {          SetWindowText (dlg, _("Change Card PIN"));
761      case WM_INITDIALOG:          SetForegroundWindow (dlg);
762          hide = 1;          break;
763          CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);  
764          center_window (dlg, NULL);      case WM_COMMAND:
765          CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);          if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) {          
766          SetForegroundWindow (dlg);              HWND hwnd;
767          break;              hide ^= 1;
768                hwnd = GetDlgItem (dlg, IDC_CHPIN_OLDPIN);
769      case WM_COMMAND:              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
770          if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) {                        SetFocus (hwnd);
771              HWND hwnd;              hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN);
772              hide ^= 1;              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
773              hwnd = GetDlgItem (dlg, IDC_CHPIN_OLDPIN);              SetFocus (hwnd);
774              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);              hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN2);
775              SetFocus (hwnd);              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
776              hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN);              SetFocus (hwnd);
777              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);          }
778              SetFocus (hwnd);          switch (LOWORD (wparam)) {
779              hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN2);          case IDOK:
780              SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);              if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))
781              SetFocus (hwnd);                  which = CARD_ADMIN_PIN;
782          }              else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))
783          switch( LOWORD( wparam ) ) {                  which = CARD_USER_PIN;
784          case IDOK:  
785              if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))              n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);
786                  which = CARD_ADMIN_PIN;              if (check_pin_len (which, 1, n))
787              else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))                  return TRUE;
788                  which = CARD_USER_PIN;              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);
789                if (check_pin_len (which, 0, n))
790              n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);                  return TRUE;
791              if (check_pin_len (which, 1, n))              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);
792                  return TRUE;              if (check_pin_len (which, 0, n))
793              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);                  return TRUE;
794              if (check_pin_len (which, 0, n))              GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1);
795                  return TRUE;              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1);
796              n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1);
797              if (check_pin_len (which, 0, n))              if (strcmp (pnew, pnew2)) {
798                  return TRUE;                  wipememory (pnew2, sizeof (pnew2));
799              GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1);                  wipememory (pnew, sizeof (pnew));
800              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1);                  msg_box (dlg, _("Passphrases do not match. Please try again."),
801              GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1);                           _("Card Edit"), MB_ERR);
802              if (strcmp (pnew, pnew2)) {                  return TRUE;
803                  msg_box (dlg, _("Passphrases do not match. Please try again."),              }
804                           _("Card Edit"), MB_ERR);  
805                  return TRUE;              ce = create_GpgCardEdit ();
806              }              if (which == CARD_ADMIN_PIN)
807                    ce->setAdminPIN (pold);
808              ce = new GpgCardEdit ();              else
809              if (!ce)                  ce->setPIN (pold);
810                  BUG (0);              ce->setNewPIN (pnew);
811              if (which == CARD_ADMIN_PIN)              err = ce->changePIN (which == CARD_ADMIN_PIN? GPG_EDITCARD_CHAPIN :
812                  ce->setAdminPIN (pold);                                   GPG_EDITCARD_CHUPIN);
813              else              if (err)
814                  ce->setPIN (pold);                  msg_box (dlg, gpgme_strerror (err), _("Card Edit"), MB_ERR);
815              ce->setNewPIN (pnew);              else {
816              err = ce->changePIN (which == CARD_ADMIN_PIN? GPG_EDITCARD_CHAPIN :                  msg_box (dlg, _("PIN successfully changed."),
817                                   GPG_EDITCARD_CHUPIN);                           _("Card Edit"), MB_OK);
818              if (err)                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
819                  msg_box (dlg, gpgme_strerror (err), _("Card Edit"), MB_ERR);                  SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
820              else {                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
821                  msg_box (dlg, _("PIN successfully changed."),              }
822                           _("Card Edit"), MB_OK);              wipememory (pold, sizeof (pold));
823                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");              wipememory (pnew, sizeof (pnew));
824                  SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");              wipememory (pnew2, sizeof (pnew2));
825                  SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");              delete ce;
826              }              break;
827              memset (pold, 0, sizeof (pold));  
828              memset (pnew, 0, sizeof (pnew));          case IDCANCEL:
829              memset (pnew2, 0, sizeof (pnew2));              SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
830              delete ce;              SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
831              break;              SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
832                EndDialog (dlg, FALSE);
833          case IDCANCEL:              break;
834              SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");          }
835              SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");          break;
836              SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");      }
837              EndDialog (dlg, FALSE);  
838              break;      return FALSE;
839          }  }
         break;  
     }  
   
     return FALSE;  
 }  

Legend:
Removed from v.27  
changed lines
  Added in v.278

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26