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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 260 - (hide annotations)
Wed Aug 16 10:01:30 2006 UTC (18 years, 6 months ago) by twoaday
File size: 23574 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26