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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (hide annotations)
Thu Oct 20 12:35:59 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23740 byte(s)
Minor cleanups and prepare the translation.

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 twoaday 28 winpt_get_pubkey (card->fpr[1]+32, &key);
170 twoaday 27 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 twoaday 28 "Card Edit - %s serial no. %s version %s",
256     card->card_type, s, get_printable_version (card->version));
257 twoaday 26 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 /* Return 0 if the given string @str has the proper format. */
280 twoaday 2 static int
281 twoaday 23 check_string (const char *str, int flags)
282 twoaday 2 {
283     size_t i;
284 twoaday 26 for (i=0; i < strlen (str); i++) {
285     if (flags & 0x02 && !isalpha (str[i]))
286 twoaday 2 return -1;
287     }
288     return 0;
289     }
290    
291    
292     static int
293 twoaday 26 do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s *pincb, gpg_card_t card)
294 twoaday 2 {
295     static struct {
296     int id;
297     int cmd;
298     int us_ascii;
299     int changed;
300     } idctl[] = {
301 twoaday 23 {IDC_CEDIT_NAME, GPG_EDITCARD_NAME, 1, 0},
302     {IDC_CEDIT_LANG2, GPG_EDITCARD_LANG, 1, 0},
303     {IDC_CEDIT_SEX2, GPG_EDITCARD_SEX, 1|1,0},
304 twoaday 26 {IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1|4,0},
305 twoaday 23 {IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1, 0},
306 twoaday 2 {0}
307 twoaday 23 };
308     gpgme_error_t err;
309     GpgCardEdit *ce;
310 twoaday 2 char buf[256], tmp[128];
311 twoaday 23 int errc=0, use_arg2 = 0;
312 twoaday 2 int i, id, n=0;
313    
314     /* XXX rewrite the entire function */
315     for( i=0; idctl[i].id; i++ ) /* reset */
316     idctl[i].changed = 0;
317    
318     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_LANG2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
319     idctl[1].changed = 1;
320     n++;
321     }
322     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_SEX2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
323     idctl[2].changed = 1;
324     n++;
325     }
326    
327     if( SendDlgItemMessage( dlg, IDC_CEDIT_NAME2, EM_GETMODIFY, 0, 0 ) ) {
328     idctl[0].changed = 1;
329     n++;
330     }
331     for( i=0; (id = idctl[i].id); i++ ) {
332     if( SendDlgItemMessage( dlg, id, EM_GETMODIFY, 0, 0 ) ) {
333     idctl[i].changed = 1;
334     n++;
335     }
336     }
337 twoaday 26 if (!pincb || !card) /* just return the changed elements */
338 twoaday 2 return n;
339 twoaday 26 if (!n)
340 twoaday 2 return 0;
341 twoaday 26 if (!pincb->apin) {
342     msg_box (dlg, _("No PINs found."), _("Card Edit"), MB_ERR);
343     return 0;
344     }
345 twoaday 2
346 twoaday 23 ce = new GpgCardEdit ();
347     if (!ce)
348 twoaday 26 BUG (NULL);
349 twoaday 23 ce->setAdminPIN (pincb->apin);
350 twoaday 26 /*ce->setPIN (pincb->upin);*/
351 twoaday 2 for( i=0; idctl[i].id; i++ ) {
352     if( idctl[i].changed ) {
353     GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );
354     if (idctl[i].us_ascii && is_8bit_string (buf)) {
355     msg_box (dlg, _("Only plain ASCII is currently allowed."),
356     _("Card Edit"), MB_ERR);
357     errc--; continue;
358     }
359     if( (idctl[i].us_ascii & 2) && check_string( buf, 2 ) ) {
360     msg_box( dlg, _("Only alphabetic characters are allowed."),
361     _("Card Edit"), MB_ERR );
362     errc--; continue;
363     }
364 twoaday 26 if ((idctl[i].us_ascii & 4) &&
365     (!strchr (buf, ':') || !strstr (buf, "//"))) {
366     /* XXX: better URL check. */
367     msg_box (dlg, _("Invalid URL."), _("Card Edit"), MB_ERR);
368     errc--; continue;
369     }
370 twoaday 23 if( idctl[i].cmd == GPG_EDITCARD_NAME ) {
371 twoaday 2 /* The "name" command actually needs two fields */
372     GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
373 twoaday 23 use_arg2 = 1;
374 twoaday 2 }
375 twoaday 23 else
376     use_arg2 = 0;
377     err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL);
378     if (err) {
379 twoaday 26 log_box (_("Card Edit"), MB_ERR,
380     _("Could not modify card attribute: %s"),
381     gpgme_strerror (err));
382 twoaday 2 errc--;
383 twoaday 26 /* If no card is inserted, we leave the loop. */
384 twoaday 27 if (gpgme_err_code (err) == GPG_ERR_CARD_NOT_PRESENT)
385 twoaday 26 break;
386 twoaday 2 }
387     }
388     }
389 twoaday 26 if (!errc) {
390 twoaday 2 /* if the operation(s) succeeded, reset the modify flag for each control */
391     for( i = 0; idctl[i].id; i++ )
392     SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 );
393     msg_box( dlg, _("Card attribute changed."), _("Card Edit"), MB_OK );
394     SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );
395     SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );
396     }
397 twoaday 23 delete ce;
398 twoaday 2 return errc;
399     } /* do_proc_card_cmds */
400    
401    
402 twoaday 23 /* Cleanup pin callback @ctx. */
403 twoaday 2 void
404 twoaday 23 free_pincb (struct pin_cb_ctx_s *ctx)
405 twoaday 2 {
406     if (!ctx)
407     return;
408     free_if_alloc (ctx->info_text);
409 twoaday 23 sfree_if_alloc (ctx->upin);
410     sfree_if_alloc (ctx->apin);
411     }
412 twoaday 2
413    
414 twoaday 23 /* Request a PIN from the user. @which decided if the
415     normal PIN or the admin PIN will be requested.
416     @card is used to show some information to the user.
417     @pincb is the actuall callback context.
418     Return value: 0 on success. */
419 twoaday 2 static int
420 twoaday 23 do_askpin (HWND dlg, int which, gpg_card_t card,
421     struct pin_cb_ctx_s *pincb)
422 twoaday 2 {
423     const char * s, * fmt;
424     const char * n1, * n2, * serial;
425     char * p;
426     size_t n;
427    
428 twoaday 26 if( (which == CARD_ADMIN_PIN && pincb->apin) ||
429     (which == CARD_USER_PIN && pincb->upin) )
430 twoaday 2 return 0;
431    
432 twoaday 26 if (which == CARD_ADMIN_PIN)
433 twoaday 2 s = _("Please enter the 'Admin PIN'");
434 twoaday 26 else if (which == CARD_USER_PIN)
435 twoaday 2 s = _("Please enter the 'User PIN'");
436     else
437     s = _("Please enter the PIN");
438     pincb->which = which;
439     free_if_alloc( pincb->info_text );
440     if( card ) {
441     fmt = _("%s\nName: %s %s\nSerial-No: %s\n");
442 twoaday 23 n1 = card->givenname;
443     n2 = card->surname;
444 twoaday 2 if( !n1 || !n2 ) {
445     n1 = "No"; n2 = "Name";
446     }
447 twoaday 23 serial = card->serial;
448     if (!serial)
449     serial = "No Serial ID";
450 twoaday 2 n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;
451     p = pincb->info_text = new char[strlen( s )+n+1 ];
452     if( !p )
453     BUG (0);
454     sprintf( p, fmt, s, n1, n2, serial );
455     }
456     else {
457     p = pincb->info_text = m_strdup (s);
458     if (!p)
459     BUG (0);
460     }
461     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,
462 twoaday 26 pin_cb_dlg_proc, (LPARAM)pincb);
463 twoaday 23 if (!pincb->apin && !pincb->upin) {
464 twoaday 2 safe_free (pincb->info_text);
465     return -1;
466     }
467     return 0;
468 twoaday 23 }
469 twoaday 2
470    
471 twoaday 23 /* Dialog box procedure for card edit. */
472 twoaday 2 BOOL CALLBACK
473     card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
474     {
475 twoaday 23 static gpg_card_t card;
476 twoaday 2 char tmp[128];
477     size_t n=0;
478    
479     switch (msg) {
480     case WM_INITDIALOG:
481 twoaday 23 card = (gpg_card_t)lparam;
482 twoaday 2 if (!card)
483     BUG (0);
484     prepare_dialog (dlg);
485     if (card_status (dlg, card ))
486     EndDialog (dlg, TRUE);
487 twoaday 23 center_window (dlg, NULL);
488 twoaday 2 SetForegroundWindow (dlg);
489     return TRUE;
490    
491     case WM_DESTROY:
492 twoaday 26 free_pincb (&pincb);
493 twoaday 2 memset (&pincb, 0, sizeof pincb);
494     break;
495    
496     case WM_COMMAND:
497     switch( HIWORD( wparam ) ) {
498     case CBN_KILLFOCUS:
499     case CBN_EDITCHANGE:
500     case CBN_EDITUPDATE:
501     int ctlid = GetDlgCtrlID( (HWND)lparam );
502     int dstid = 0;
503    
504     switch (ctlid) {
505     case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break;
506     case IDC_CEDIT_SEX: dstid = IDC_CEDIT_SEX2; break;
507     }
508 twoaday 26 GetDlgItemText (dlg, ctlid, tmp, sizeof (tmp)-1);
509 twoaday 2 SetDlgItemText (dlg, dstid, tmp);
510     break;
511     }
512 twoaday 28 switch (LOWORD (wparam)) {
513 twoaday 2 case IDC_CEDIT_CHPIN:
514     DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg,
515     card_changepin_dlg_proc, NULL );
516     break;
517    
518     case IDC_CEDIT_NEWKEYS:
519     if (item_get_text_length (dlg, IDC_CEDIT_FPR1) > 0) {
520     int id = msg_box (dlg,
521     _("This operation will override the keys on the card.\n"
522     "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);
523     if (id == IDNO)
524 twoaday 23 return TRUE;
525 twoaday 2 }
526     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
527     glob_hwnd, card_keygen_dlg_proc, NULL);
528     break;
529    
530     case IDOK:
531 twoaday 26 n = do_proc_card_cmds (dlg, NULL, NULL);
532     if (n) {
533     if (do_askpin (dlg, CARD_ADMIN_PIN, card, &pincb))
534     EndDialog (dlg, FALSE);
535 twoaday 2 }
536 twoaday 26 do_proc_card_cmds (dlg, &pincb, card);
537     free_pincb (&pincb);
538 twoaday 28 if (!n)
539     EndDialog (dlg, TRUE);
540 twoaday 2 break;
541    
542     case IDCANCEL:
543     EndDialog( dlg, FALSE );
544     break;
545     }
546     break;
547     }
548    
549     return FALSE;
550 twoaday 23 }
551 twoaday 2
552    
553     static int /* fixme: works only roughly */
554     calc_days (int y2, int m2, int d2,
555     int y1, int m1, int d1)
556    
557     {
558     int n=0;
559    
560     if ((y2-y1) > 0)
561     n += (y2-y1)*365;
562     if ((m2-m1) > 0)
563     n += (m2-m1)*30;
564     if ((d2-d1) > 0)
565     n += (d2-d1);
566     else if ((d2-d1) < 0)
567     n -= (d1-d2);
568     return n;
569     }
570    
571    
572 twoaday 23 /* Dialog box procedure for the key generation on cards. */
573 twoaday 2 BOOL CALLBACK
574     card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
575     {
576     gpgme_error_t err;
577 twoaday 23 GpgCardEdit *ce;
578     char name[128], email[128], comment[128];
579 twoaday 2 char pass[128];
580 twoaday 23 int card_flags = GPG_CARDFLAG_NONE;
581     int expires=0, valid;
582 twoaday 2 size_t n;
583    
584     switch (msg) {
585     case WM_INITDIALOG:
586 twoaday 23 center_window (dlg, NULL);
587 twoaday 2 CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);
588     CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);
589     CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);
590     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);
591 twoaday 26 SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0,
592     (LPARAM)(const char*)"RSA");
593 twoaday 23 SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
594 twoaday 2 SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
595     SetForegroundWindow (dlg);
596     return FALSE;
597    
598 twoaday 26 case WM_SYSCOMMAND:
599     if (LOWORD (wparam) == SC_CLOSE)
600     EndDialog (dlg, TRUE);
601     return FALSE;
602    
603 twoaday 2 case WM_COMMAND:
604 twoaday 26 if (HIWORD (wparam) == BN_CLICKED &&
605     (LOWORD (wparam) == IDC_CKEYGEN_BACKUP) ||
606     LOWORD (wparam) == IDC_CKEYGEN_NEVER) {
607     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID),
608     IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER)? 0: 1);
609     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS),
610     IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)? 1 : 0);
611     return TRUE;
612     }
613 twoaday 2
614     switch (LOWORD (wparam)) {
615     case IDOK:
616     n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);
617     if (!n) {
618     msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);
619 twoaday 23 return TRUE;
620 twoaday 2 }
621     if (n < 5) {
622     msg_box (dlg, _("Name must be at least 5 characters long."),
623     _("Card Edit"), MB_INFO);
624 twoaday 23 return TRUE;
625 twoaday 2 }
626     n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);
627     if (!n) {
628     msg_box (dlg, _("Please enter your e-mail address."),
629     _("Card Edit"), MB_ERR);
630 twoaday 23 return TRUE;
631 twoaday 2 }
632     GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);
633     GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);
634     if (!strchr (email, '@') || n < 3) {
635     msg_box (dlg, _("Please enter a valid e-mail address."),
636     _("Card Edit"), MB_ERR);
637 twoaday 23 return TRUE;
638 twoaday 2 }
639     n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
640 twoaday 26 if (!n && IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) {
641 twoaday 2 msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);
642 twoaday 23 return TRUE;
643 twoaday 2 }
644     n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);
645     if (n > 0)
646     GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1);
647 twoaday 28 if (is_8bit_string (name) || n > 0 && is_8bit_string (comment)) {
648 twoaday 2 msg_box (dlg, _("Please use plain ASCII charset for the fields."),
649     _("Card Edit"), MB_INFO);
650 twoaday 23 return TRUE;
651 twoaday 2 }
652     memset (&pincb, 0, sizeof (pincb));
653 twoaday 26 if (do_askpin (dlg, CARD_ADMIN_PIN, NULL, &pincb)) {
654 twoaday 2 free_pincb (&pincb);
655 twoaday 23 return TRUE;
656 twoaday 2 }
657 twoaday 26 if (do_askpin (dlg, CARD_USER_PIN, NULL, &pincb)) {
658 twoaday 2 free_pincb (&pincb);
659 twoaday 23 return TRUE;
660 twoaday 2 }
661 twoaday 23 ce = new GpgCardEdit ();
662     if (!ce)
663 twoaday 2 BUG (0);
664 twoaday 23
665 twoaday 2 expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);
666     if (expires) {
667     SYSTEMTIME st, ct;
668     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_CKEYGEN_VALID), &st);
669     if (!keygen_check_date (&st)) {
670     msg_box (dlg, _("The date you have chosen lies in the past."),
671     _("Card Edit"), MB_ERR);
672 twoaday 28 free_pincb (&pincb);
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 28
695     if (gpgme_err_code (err) == GPG_ERR_CANCELED)
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 28 else
700 twoaday 23 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