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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 22856 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26