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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations)
Tue Oct 18 07:57:13 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23698 byte(s)
Some bug fixes and adjustments for the latest
GPGME changes.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26