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

Contents of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations)
Sat Oct 22 10:17:59 2005 UTC (19 years, 4 months ago) by twoaday
File size: 23744 byte(s)
Sync with old SVN repository.

1 /* wptCardDlg.cpp - Smart Card support
2 * Copyright (C) 2003, 2004, 2005 Timo Schulz
3 * 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 * 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 #include "gpgme.h"
28 #include "wptTypes.h"
29 #include "wptW32API.h"
30 #include "wptErrors.h"
31 #include "wptRegistry.h"
32 #include "wptVersion.h"
33 #include "wptCommonCtl.h"
34 #include "wptDlgs.h"
35 #include "wptGPG.h"
36 #include "wptUTF8.h"
37 #include "wptCardEdit.h"
38 #include "wptCard.h"
39 #include "wptContext.h"
40
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 {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 {IDC_CEDIT_LOGIN, "No Login name"},
62 {0},
63 };
64
65
66
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 /* 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
87 *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 p[2] = get_printable_version (card->version);
94 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
102
103 static int
104 idx_from_lang (const char * _lang)
105 {
106 const char * s;
107 int i;
108
109 if (!_lang)
110 return 0;
111 for (i=0; (s = lang[i]); i++) {
112 if (!strcmp (_lang, s ))
113 return i;
114 }
115 return 0;
116 }
117
118
119 #if 0 /* @unused@ */
120 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 #endif
138
139 /* 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 gpg_card_load (void)
144 {
145 gpgme_error_t err;
146 GpgCardEdit *ce;
147 gpg_card_t card = NULL;
148 struct card_cb_s cb = {0};
149
150 ce = new GpgCardEdit ();
151 if (!ce)
152 BUG (0);
153 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 goto leave;
159 }
160
161 if (!card->aid || strncmp (card->aid, "D276000124", 10)) {
162 msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);
163 gpg_card_release (card);
164 card = NULL;
165 }
166 else {
167 struct winpt_key_s key;
168 memset (&key, 0, sizeof (key));
169 winpt_get_pubkey (card->fpr[1]+32, &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
178 leave:
179 delete ce;
180 return card;
181 }
182
183
184 /* Print human friendly fingerprint to control @id in the
185 dialog @dlg. @fpr contains the raw fingerprint. */
186 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 strcpy (buf, _("No Fingerprint"));
194 else {
195 memset (buf, 0, sizeof (buf));
196 for( i=0, c=0; i < strlen (fpr); i++) {
197 dig[0] = fpr[i]; dig[1] = 0;
198 strcat (buf, dig);
199 if (++c == 4) {
200 strcat (buf, " ");
201 c=0;
202 }
203 }
204 }
205 SetDlgItemText (dlg, id, buf);
206 }
207
208
209 /* 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 static int
213 card_status (HWND dlg, gpg_card_t card)
214 {
215 static int fprbuf[] = {IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};
216 static int fprtime[] = {IDC_CEDIT_SIG_FPRTIME, IDC_CEDIT_DEC_FPRTIME, IDC_CEDIT_AUTH_FPRTIME, 0};
217 const char *s;
218 char **attrs;
219 char cardinf[128];
220 int idx=0, n=0;
221
222 if (!card->aid) {
223 msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR );
224 return -1;
225 }
226 SetDlgItemText (dlg, IDC_CEDIT_AID, card->aid);
227 SetDlgItemInt (dlg, IDC_CEDIT_SIGCOUNT, card->sig_count, TRUE);
228
229 for (idx=0; fprbuf[idx]; idx++) {
230 print_fpr (dlg, fprbuf[idx], card->fpr[idx]);
231 SetDlgItemText (dlg, fprtime[idx], card->fpr_created_str[idx]);
232 }
233
234 attrs = card_get_items (card, &n);
235 for (idx=1; attr_tab[idx].ctlid; idx++) {
236 s = attrs[idx];
237 SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err);
238 }
239 free (attrs);
240
241 idx = idx_from_lang (card->lang);
242 SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0);
243
244 switch (card->sex) {
245 case 'm': idx=0; break;
246 case 'f': idx=1; break;
247 default :
248 case 'u': idx=2; break;
249 }
250 SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0);
251
252 s = card->serial;
253 while (s && *s == '0') s++;
254 _snprintf (cardinf, sizeof (cardinf)-1,
255 "Card Edit - %s serial no. %s version %s",
256 card->card_type, s, get_printable_version (card->version));
257 SetWindowText (dlg, cardinf);
258
259 return 0;
260 }
261
262
263 /* Initialize the enum combox boxes in dialog @dlg. */
264 static void
265 prepare_dialog (HWND dlg)
266 {
267 const char * s;
268 int i;
269
270 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 }
277
278
279 /* Return 0 if the given string @str has the proper format. */
280 static int
281 check_string (const char *str, int flags)
282 {
283 size_t i;
284 for (i=0; i < strlen (str); i++) {
285 if (flags & 0x02 && !isalpha (str[i]))
286 return -1;
287 }
288 return 0;
289 }
290
291
292 static int
293 do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s *pincb, gpg_card_t card)
294 {
295 static struct {
296 int id;
297 int cmd;
298 int us_ascii;
299 int changed;
300 } idctl[] = {
301 {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 {IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1|4,0},
305 {IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1, 0},
306 {0}
307 };
308 gpgme_error_t err;
309 GpgCardEdit *ce;
310 char buf[256], tmp[128];
311 int errc=0, use_arg2 = 0;
312 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 if (!pincb || !card) /* just return the changed elements */
338 return n;
339 if (!n)
340 return 0;
341 if (!pincb->apin) {
342 msg_box (dlg, _("No PINs found."), _("Card Edit"), MB_ERR);
343 return 0;
344 }
345
346 ce = new GpgCardEdit ();
347 if (!ce)
348 BUG (NULL);
349 ce->setAdminPIN (pincb->apin);
350 /*ce->setPIN (pincb->upin);*/
351 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 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 if( idctl[i].cmd == GPG_EDITCARD_NAME ) {
371 /* The "name" command actually needs two fields */
372 GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
373 use_arg2 = 1;
374 }
375 else
376 use_arg2 = 0;
377 err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL);
378 if (err) {
379 log_box (_("Card Edit"), MB_ERR,
380 _("Could not modify card attribute: %s"),
381 gpgme_strerror (err));
382 errc--;
383 /* If no card is inserted, we leave the loop. */
384 if (gpgme_err_code (err) == GPG_ERR_CARD_NOT_PRESENT)
385 break;
386 }
387 }
388 }
389 if (!errc) {
390 /* 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 delete ce;
398 return errc;
399 } /* do_proc_card_cmds */
400
401
402 /* Cleanup pin callback @ctx. */
403 void
404 free_pincb (struct pin_cb_ctx_s *ctx)
405 {
406 if (!ctx)
407 return;
408 free_if_alloc (ctx->info_text);
409 sfree_if_alloc (ctx->upin);
410 sfree_if_alloc (ctx->apin);
411 }
412
413
414 /* 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 static int
420 do_askpin (HWND dlg, int which, gpg_card_t card,
421 struct pin_cb_ctx_s *pincb)
422 {
423 const char * s, * fmt;
424 const char * n1, * n2, * serial;
425 char * p;
426 size_t n;
427
428 if( (which == CARD_ADMIN_PIN && pincb->apin) ||
429 (which == CARD_USER_PIN && pincb->upin) )
430 return 0;
431
432 if (which == CARD_ADMIN_PIN)
433 s = _("Please enter the 'Admin PIN'");
434 else if (which == CARD_USER_PIN)
435 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 n1 = card->givenname;
443 n2 = card->surname;
444 if( !n1 || !n2 ) {
445 n1 = "No"; n2 = "Name";
446 }
447 serial = card->serial;
448 if (!serial)
449 serial = "No Serial ID";
450 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 pin_cb_dlg_proc, (LPARAM)pincb);
463 if (!pincb->apin && !pincb->upin) {
464 safe_free (pincb->info_text);
465 return -1;
466 }
467 return 0;
468 }
469
470
471 /* Dialog box procedure for card edit. */
472 BOOL CALLBACK
473 card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
474 {
475 static gpg_card_t card;
476 char tmp[128];
477 size_t n=0;
478
479 switch (msg) {
480 case WM_INITDIALOG:
481 card = (gpg_card_t)lparam;
482 if (!card)
483 BUG (0);
484 prepare_dialog (dlg);
485 if (card_status (dlg, card ))
486 EndDialog (dlg, TRUE);
487 center_window (dlg, NULL);
488 SetForegroundWindow (dlg);
489 return TRUE;
490
491 case WM_DESTROY:
492 free_pincb (&pincb);
493 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 GetDlgItemText (dlg, ctlid, tmp, sizeof (tmp)-1);
509 SetDlgItemText (dlg, dstid, tmp);
510 break;
511 }
512 switch (LOWORD (wparam)) {
513 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 return TRUE;
525 }
526 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
527 glob_hwnd, card_keygen_dlg_proc, NULL);
528 break;
529
530 case IDOK:
531 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 }
536 do_proc_card_cmds (dlg, &pincb, card);
537 free_pincb (&pincb);
538 if (!n)
539 EndDialog (dlg, TRUE);
540 break;
541
542 case IDCANCEL:
543 EndDialog( dlg, FALSE );
544 break;
545 }
546 break;
547 }
548
549 return FALSE;
550 }
551
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 /* Dialog box procedure for the key generation on cards. */
573 BOOL CALLBACK
574 card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
575 {
576 gpgme_error_t err;
577 GpgCardEdit *ce;
578 char name[128], email[128], comment[128];
579 char pass[128];
580 int card_flags = GPG_CARDFLAG_NONE;
581 int expires=0, valid;
582 size_t n;
583
584 switch (msg) {
585 case WM_INITDIALOG:
586 center_window (dlg, NULL);
587 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 SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0,
592 (LPARAM)(const char*)"RSA");
593 SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
594 SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
595 SetForegroundWindow (dlg);
596 return FALSE;
597
598 case WM_SYSCOMMAND:
599 if (LOWORD (wparam) == SC_CLOSE)
600 EndDialog (dlg, TRUE);
601 return FALSE;
602
603 case WM_COMMAND:
604 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
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 return TRUE;
620 }
621 if (n < 5) {
622 msg_box (dlg, _("Name must be at least 5 characters long."),
623 _("Card Edit"), MB_INFO);
624 return TRUE;
625 }
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 return TRUE;
631 }
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 return TRUE;
638 }
639 n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
640 if (!n && IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) {
641 msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);
642 return TRUE;
643 }
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 if (is_8bit_string (name) || n > 0 && is_8bit_string (comment)) {
648 msg_box (dlg, _("Please use plain ASCII charset for the fields."),
649 _("Card Edit"), MB_INFO);
650 return TRUE;
651 }
652 memset (&pincb, 0, sizeof (pincb));
653 if (do_askpin (dlg, CARD_ADMIN_PIN, NULL, &pincb)) {
654 free_pincb (&pincb);
655 return TRUE;
656 }
657 if (do_askpin (dlg, CARD_USER_PIN, NULL, &pincb)) {
658 free_pincb (&pincb);
659 return TRUE;
660 }
661 ce = new GpgCardEdit ();
662 if (!ce)
663 BUG (0);
664
665 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 free_pincb (&pincb);
673 delete ce;
674 return TRUE;
675 }
676 GetSystemTime (&ct);
677 /* XXX this is not very precise */
678 valid = calc_days (st.wYear, st.wMonth, st.wDay,
679 ct.wYear, ct.wMonth, ct.wDay);
680 }
681 if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))
682 card_flags |= GPG_CARDFLAG_REPLACE;
683 if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))
684 card_flags |= GPG_CARDFLAG_BAKENC;
685 ce->setKeygenPassphrase (pass);
686 ce->setPIN (pincb.upin);
687 ce->setAdminPIN (pincb.apin);
688
689 SetCursor (LoadCursor (NULL, IDC_WAIT));
690 err = ce->genKey (card_flags, name, email, n? comment: NULL,
691 expires? valid : 0, NULL);
692
693 SetCursor (LoadCursor (NULL, IDC_ARROW));
694
695 if (gpgme_err_code (err) == GPG_ERR_CANCELED)
696 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 else
700 if (err)
701 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 wipememory (pass, sizeof (pass));
708 free_pincb (&pincb);
709 delete ce;
710 return TRUE;
711
712 case IDCANCEL:
713 EndDialog (dlg, FALSE);
714 return FALSE;
715 }
716 break;
717 }
718 return FALSE;
719 }
720
721
722 /* 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 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 if (which == CARD_ADMIN_PIN
737 && pinlen < 8) {
738 msg_box (NULL, _("Admin PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);
739 return -1;
740 }
741 if (which == CARD_USER_PIN
742 && 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 /* Dialog box procedure to change the PIN. */
750 BOOL CALLBACK
751 card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
752 {
753 static int hide = 1;
754 gpgme_error_t err;
755 GpgCardEdit *ce;
756 char pold[128], pnew[128], pnew2[128];
757 int which;
758 size_t n;
759
760 switch( msg ) {
761 case WM_INITDIALOG:
762 hide = 1;
763 CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);
764 center_window (dlg, NULL);
765 CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);
766 SetForegroundWindow (dlg);
767 break;
768
769 case WM_COMMAND:
770 if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) {
771 HWND hwnd;
772 hide ^= 1;
773 hwnd = GetDlgItem (dlg, IDC_CHPIN_OLDPIN);
774 SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
775 SetFocus (hwnd);
776 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 switch( LOWORD( wparam ) ) {
784 case IDOK:
785 if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))
786 which = CARD_ADMIN_PIN;
787 else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))
788 which = CARD_USER_PIN;
789
790 n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);
791 if (check_pin_len (which, 1, n))
792 return TRUE;
793 n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);
794 if (check_pin_len (which, 0, n))
795 return TRUE;
796 n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);
797 if (check_pin_len (which, 0, n))
798 return TRUE;
799 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 msg_box (dlg, _("Passphrases do not match. Please try again."),
804 _("Card Edit"), MB_ERR);
805 return TRUE;
806 }
807
808 ce = new GpgCardEdit ();
809 if (!ce)
810 BUG (0);
811 if (which == CARD_ADMIN_PIN)
812 ce->setAdminPIN (pold);
813 else
814 ce->setPIN (pold);
815 ce->setNewPIN (pnew);
816 err = ce->changePIN (which == CARD_ADMIN_PIN? GPG_EDITCARD_CHAPIN :
817 GPG_EDITCARD_CHUPIN);
818 if (err)
819 msg_box (dlg, gpgme_strerror (err), _("Card Edit"), MB_ERR);
820 else {
821 msg_box (dlg, _("PIN successfully changed."),
822 _("Card Edit"), MB_OK);
823 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
824 SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
825 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
826 }
827 wipememory (pold, sizeof (pold));
828 wipememory (pnew, sizeof (pnew));
829 wipememory (pnew2, sizeof (pnew2));
830 delete ce;
831 break;
832
833 case IDCANCEL:
834 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
835 SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
836 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
837 EndDialog (dlg, FALSE);
838 break;
839 }
840 break;
841 }
842
843 return FALSE;
844 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26