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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations)
Mon Oct 17 08:49:30 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23365 byte(s)
More bug fixes all over the place.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26