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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 306 - (hide annotations)
Fri Mar 23 14:07:24 2007 UTC (17 years, 11 months ago) by twoaday
File size: 22809 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26