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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 278 - (hide annotations)
Mon Jan 15 22:02:04 2007 UTC (18 years, 1 month ago) by twoaday
File size: 23169 byte(s)
See ChangeLog.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26