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

Contents of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 441 - (show annotations)
Sat Apr 14 14:11:03 2012 UTC (12 years, 10 months ago) by twoaday
File size: 22845 byte(s)
2012-04-14  Timo Schulz  <twoaday@gmx.net>

        * wptBalloonPop.cpp (window_proc): Removed unused variable.
        * wptCardDlg.cpp (do_askpin): Corrected generation of PIN
        info text. Thanks to Grzesiek.
			

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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26