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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 21883 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26