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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Mon Jan 31 11:02:21 2005 UTC (20 years, 1 month ago) by twoaday
File size: 21440 byte(s)
WinPT initial checkin.


1 twoaday 2 /* wptCardDlg.cpp - Smart Card support
2     * Copyright (C) 2003, 2004 Timo Schulz
3     *
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     #include "wptCard.h"
36    
37     int keygen_check_date (SYSTEMTIME * st);
38    
39     static const char * sex[] = {"Male", "Female", "Undefined", NULL};
40     static const char * lang[] = {"Undefined", "cs", "de", "en", "es", "fr", "hu",
41     "it", "nl", "pt", "ro", "ru", "zh", "at",
42     NULL};
43    
44     static pin_cb_ctx_s pincb;
45    
46     struct {
47     int ctlid;
48     gpgme_attr_t attr;
49     const char * err;
50     } attr_tab[] = {
51     {IDC_CEDIT_AID, GPGME_ATTR_CARD_AID, ""},
52     {IDC_CEDIT_VENDOR, GPGME_ATTR_CARD_VENDOR, "No Vendor"},
53     {IDC_CEDIT_VERSION,GPGME_ATTR_CARD_VER, "No Version"},
54     {IDC_CEDIT_SERIAL, GPGME_ATTR_CARD_SERIAL, "No Serial-No"},
55     {IDC_CEDIT_NAME, GPGME_ATTR_CARD_NAME, "No Name"},
56     {IDC_CEDIT_NAME2, GPGME_ATTR_CARD_NAME2, "No Surname"},
57     {IDC_CEDIT_KEYURL, GPGME_ATTR_CARD_URL, "No Key-URL"},
58     {IDC_CEDIT_LOGIN, GPGME_ATTR_CARD_LOGIN, "No Login name"},
59     {0},
60     };
61    
62    
63     #define card_get_string(card, what) \
64     gpgme_card_get_string_attr ((card), (what), NULL, 0)
65     #define card_get_ulong(card, what) \
66     gpgme_card_get_ulong_attr ((card), (what), NULL, 0)
67     #define card_get_fpr(card, idx) \
68     gpgme_card_get_string_attr ((card), GPGME_ATTR_CARD_FPR, NULL, (idx)-1)
69    
70    
71     static int
72     idx_from_lang( const char * _lang )
73     {
74     const char * s;
75     int i;
76    
77     if (!_lang)
78     return 0;
79     for (i=0; (s = lang[i]); i++) {
80     if( !strcmp( _lang, s ) )
81     return i;
82     }
83     return 0;
84     } /* idx_from_lang */
85    
86    
87     int
88     show_card_status (void)
89     {
90     int rc = 0;
91     int cardstat;
92    
93     cardstat = pcsc_get_card_status ();
94     if ((cardstat & CARD_STATE_UNAWARE) || (cardstat & CARD_STATE_UNAVAIL))
95     rc = WPTERR_NOREADER;
96     else if (cardstat & CARD_STATE_EMPTY)
97     rc = WPTERR_NOCARD;
98     if (rc) {
99     msg_box (NULL, winpt_strerror (rc), _("Card Manager"), MB_ERR);
100     return -1;
101     }
102     return 0;
103     } /* show_card_status */
104    
105    
106     gpgme_card_t
107     gpg_load_scard (void)
108     {
109     gpgme_error_t rc;
110     gpgme_card_t card = NULL;
111     gpgme_editcard_t ec;
112     gpgme_ctx_t ctx;
113     const char * s;
114     struct card_cb_s cb = {0};
115    
116     rc = gpgme_new (&ctx);
117     if (!rc)
118     rc = gpgme_editcard_new (&ec);
119     if (rc)
120     BUG (0);
121     gpgme_enable_logging (ctx);
122     gpgme_editcard_set_callback (ec, card_callback, &cb);
123     gpgme_set_edit_ctx (ctx, ec, 0);
124     rc = gpgme_op_statuscard (ctx, &card);
125     if (rc) {
126     gpgme_show_error (NULL, rc, ctx, _("Card Manager"), MB_ERR);
127     goto leave;
128     }
129     s = gpgme_card_get_string_attr (card, GPGME_ATTR_CARD_AID, NULL, 0);
130     if (!s || strncmp (s, "D276000124", 10)) {
131     msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR);
132     gpgme_card_release (card);
133     card = NULL;
134     }
135    
136     leave:
137     gpgme_editcard_release (ec);
138     gpgme_release (ctx);
139     return card;
140     } /* gpg_load_scard */
141    
142    
143     static void
144     print_fpr (HWND dlg, int id, const char * fpr)
145     {
146     char buf[128], dig[2];
147     size_t i, c;
148    
149     if (!fpr)
150     strcpy( buf, _("No Fingerprint") );
151     else {
152     memset( buf, 0, sizeof (buf) );
153     for( i=0, c=0; i < strlen( fpr ); i++ ) {
154     dig[0] = fpr[i]; dig[1] = 0;
155     strcat( buf, dig );
156     if( ++c == 4 ) {
157     strcat( buf, " " );
158     c=0;
159     }
160     }
161     }
162     SetDlgItemText( dlg, id, buf );
163     } /* print_fpr */
164    
165    
166     static int
167     card_status( HWND dlg, gpgme_card_t card )
168     {
169     static int fprbuf[] = {0, IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0};
170     const char * s;
171     int idx=0;
172     u32 t;
173    
174     s = card_get_string( card, GPGME_ATTR_CARD_AID );
175     if( !s ) {
176     msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR );
177     return -1;
178     }
179     SetDlgItemText( dlg, IDC_CEDIT_AID, s );
180    
181     t = card_get_ulong( card, GPGME_ATTR_CARD_SIGCOUNT );
182     SetDlgItemInt( dlg, IDC_CEDIT_SIGCOUNT, t, TRUE );
183    
184     for( idx=1; fprbuf[idx]; idx++ ) {
185     s = card_get_fpr( card, idx );
186     print_fpr( dlg, fprbuf[idx], s );
187     }
188    
189     for( idx=1; attr_tab[idx].attr; idx++ ) {
190     s = card_get_string( card, attr_tab[idx].attr );
191     SetDlgItemText( dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err );
192     }
193    
194     s = card_get_string( card, GPGME_ATTR_CARD_LANG );
195     idx = idx_from_lang( s );
196     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0 );
197    
198     t = card_get_ulong( card, GPGME_ATTR_CARD_SEX );
199     switch( t ) {
200     case 'm': idx=0; break;
201     case 'f': idx=1; break;
202     default :
203     case 'u': idx=2; break;
204     }
205     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0 );
206    
207     return 0;
208     } /* card_status */
209    
210    
211     static void
212     prepare_dialog( HWND dlg )
213     {
214     const char * s;
215     int i;
216    
217     for( i=0; (s = sex[i]); i++ )
218     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s );
219     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0 );
220     for( i=0; (s = lang[i]); i++ )
221     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s );
222     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0 );
223     } /* prepare_dialog */
224    
225    
226     static int
227     check_string( const char * str, int flags )
228     {
229     size_t i;
230     for( i=0; i < strlen( str ); i++ ) {
231     if( flags & 0x02 && !isalpha( str[i] ) )
232     return -1;
233     }
234     return 0;
235     }
236    
237    
238     static int
239     do_proc_card_cmds( HWND dlg, struct pin_cb_ctx_s * pincb, gpgme_card_t card )
240     {
241     static struct {
242     int id;
243     int cmd;
244     int us_ascii;
245     int changed;
246     } idctl[] = {
247     {IDC_CEDIT_NAME, GPGME_EDITCARD_NAME, 1, 0},
248     {IDC_CEDIT_LANG2, GPGME_EDITCARD_LANG, 1, 0},
249     {IDC_CEDIT_SEX2, GPGME_EDITCARD_SEX, 1|1,0},
250     {IDC_CEDIT_KEYURL,GPGME_EDITCARD_KEYURL,1, 0},
251     {IDC_CEDIT_LOGIN, GPGME_EDITCARD_LOGIN, 1, 0},
252     {0}
253     };
254     gpgme_editcard_t ec;
255     gpgme_ctx_t ctx;
256     gpgme_error_t rc;
257     char buf[256], tmp[128];
258     int errc=0;
259     int i, id, n=0;
260    
261     /* XXX rewrite the entire function */
262     for( i=0; idctl[i].id; i++ ) /* reset */
263     idctl[i].changed = 0;
264    
265     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_LANG2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
266     idctl[1].changed = 1;
267     n++;
268     }
269     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_SEX2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
270     idctl[2].changed = 1;
271     n++;
272     }
273    
274     if( SendDlgItemMessage( dlg, IDC_CEDIT_NAME2, EM_GETMODIFY, 0, 0 ) ) {
275     idctl[0].changed = 1;
276     n++;
277     }
278     for( i=0; (id = idctl[i].id); i++ ) {
279     if( SendDlgItemMessage( dlg, id, EM_GETMODIFY, 0, 0 ) ) {
280     idctl[i].changed = 1;
281     n++;
282     }
283     }
284     if( !pincb || !card ) /* just return the changed elements */
285     return n;
286     if( !n )
287     return 0;
288    
289     rc = gpgme_editcard_new( &ec );
290     if( !rc )
291     rc = gpgme_new( &ctx );
292     if( rc )
293     BUG( NULL );
294     gpgme_editcard_control( ec, GPGME_EDITCARD_APIN, pincb->apin );
295     gpgme_editcard_control( ec, GPGME_EDITCARD_UPIN, pincb->upin );
296     for( i=0; idctl[i].id; i++ ) {
297     if( idctl[i].changed ) {
298     GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );
299     if (idctl[i].us_ascii && is_8bit_string (buf)) {
300     msg_box (dlg, _("Only plain ASCII is currently allowed."),
301     _("Card Edit"), MB_ERR);
302     errc--; continue;
303     }
304     if( (idctl[i].us_ascii & 2) && check_string( buf, 2 ) ) {
305     msg_box( dlg, _("Only alphabetic characters are allowed."),
306     _("Card Edit"), MB_ERR );
307     errc--; continue;
308     }
309     if( idctl[i].cmd == GPGME_EDITCARD_NAME ) {
310     /* The "name" command actually needs two fields */
311     GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
312     gpgme_editcard_control( ec, GPGME_EDITCARD_NAME2, tmp );
313     }
314     gpgme_editcard_control( ec, idctl[i].cmd, buf );
315     gpgme_set_edit_ctx( ctx, ec, idctl[i].cmd );
316     rc = gpgme_op_editcard( ctx );
317     if( rc ) {
318     msg_box( dlg, _("Could not modify card attribute."),
319     _("Card Edit"), MB_ERR );
320     errc--;
321     }
322     }
323     }
324     if( !errc ) {
325     /* if the operation(s) succeeded, reset the modify flag for each control */
326     for( i = 0; idctl[i].id; i++ )
327     SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 );
328     msg_box( dlg, _("Card attribute changed."), _("Card Edit"), MB_OK );
329     SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );
330     SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );
331     }
332     gpgme_editcard_release( ec );
333     gpgme_release( ctx );
334     return errc;
335     } /* do_proc_card_cmds */
336    
337    
338     void
339     free_pincb (struct pin_cb_ctx_s * ctx)
340     {
341     if (!ctx)
342     return;
343     free_if_alloc (ctx->info_text);
344     free_if_alloc (ctx->upin);
345     free_if_alloc (ctx->apin);
346     } /* free_pincb */
347    
348    
349     static int
350     do_askpin( HWND dlg, gpgme_edit_card_t which, gpgme_card_t card,
351     struct pin_cb_ctx_s * pincb )
352     {
353     const char * s, * fmt;
354     const char * n1, * n2, * serial;
355     char * p;
356     size_t n;
357    
358     if( (which == GPGME_EDITCARD_CHAPIN && pincb->apin) ||
359     (which == GPGME_EDITCARD_CHUPIN && pincb->upin) )
360     return 0;
361    
362     if( which == GPGME_EDITCARD_CHAPIN )
363     s = _("Please enter the 'Admin PIN'");
364     else if( which == GPGME_EDITCARD_CHUPIN )
365     s = _("Please enter the 'User PIN'");
366     else
367     s = _("Please enter the PIN");
368     pincb->which = which;
369     free_if_alloc( pincb->info_text );
370     if( card ) {
371     fmt = _("%s\nName: %s %s\nSerial-No: %s\n");
372     n1 = card_get_string( card, GPGME_ATTR_CARD_NAME );
373     n2 = card_get_string( card, GPGME_ATTR_CARD_NAME2 );
374     if( !n1 || !n2 ) {
375     n1 = "No"; n2 = "Name";
376     }
377     serial = card_get_string( card, GPGME_ATTR_CARD_SERIAL );
378     if( !serial )
379     serial = "No Serial";
380     n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;
381     p = pincb->info_text = new char[strlen( s )+n+1 ];
382     if( !p )
383     BUG (0);
384     sprintf( p, fmt, s, n1, n2, serial );
385     }
386     else {
387     p = pincb->info_text = m_strdup (s);
388     if (!p)
389     BUG (0);
390     }
391     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,
392     pin_cb_dlg_proc, (LPARAM)pincb);
393     if( !pincb->apin && !pincb->upin) {
394     safe_free (pincb->info_text);
395     return -1;
396     }
397     return 0;
398     } /* do_askpin */
399    
400    
401     BOOL CALLBACK
402     card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
403     {
404     static gpgme_card_t card;
405     char tmp[128];
406     size_t n=0;
407    
408     switch (msg) {
409     case WM_INITDIALOG:
410     card = (gpgme_card_t)lparam;
411     if (!card)
412     BUG (0);
413     prepare_dialog (dlg);
414     if (card_status (dlg, card ))
415     EndDialog (dlg, TRUE);
416     center_window (dlg);
417     SetForegroundWindow (dlg);
418     return TRUE;
419    
420     case WM_DESTROY:
421     free_if_alloc (pincb.info_text);
422     free_if_alloc (pincb.apin);
423     free_if_alloc (pincb.upin);
424     memset (&pincb, 0, sizeof pincb);
425     break;
426    
427     case WM_COMMAND:
428     switch( HIWORD( wparam ) ) {
429     case CBN_KILLFOCUS:
430     case CBN_EDITCHANGE:
431     case CBN_EDITUPDATE:
432     int ctlid = GetDlgCtrlID( (HWND)lparam );
433     int dstid = 0;
434    
435     switch (ctlid) {
436     case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break;
437     case IDC_CEDIT_SEX: dstid = IDC_CEDIT_SEX2; break;
438     }
439     GetDlgItemText (dlg, ctlid, tmp, 127);
440     SetDlgItemText (dlg, dstid, tmp);
441     break;
442     }
443     switch( LOWORD( wparam ) ) {
444     case IDC_CEDIT_CHPIN:
445     DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg,
446     card_changepin_dlg_proc, NULL );
447     break;
448    
449     case IDC_CEDIT_NEWKEYS:
450     if (item_get_text_length (dlg, IDC_CEDIT_FPR1) > 0) {
451     int id = msg_box (dlg,
452     _("This operation will override the keys on the card.\n"
453     "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);
454     if (id == IDNO)
455     return FALSE;
456     }
457     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
458     glob_hwnd, card_keygen_dlg_proc, NULL);
459     break;
460    
461     case IDOK:
462     n = do_proc_card_cmds( dlg, NULL, NULL );
463     if( n ) {
464     if( do_askpin( dlg, GPGME_EDITCARD_CHAPIN, card, &pincb ) )
465     EndDialog( dlg, FALSE );
466     if( do_askpin( dlg, GPGME_EDITCARD_CHUPIN, card, &pincb ) )
467     EndDialog( dlg, FALSE );
468     }
469     do_proc_card_cmds( dlg, &pincb, card );
470     free_pincb( &pincb );
471     if( !n )
472     EndDialog( dlg, TRUE );
473     break;
474    
475     case IDCANCEL:
476     EndDialog( dlg, FALSE );
477     break;
478     }
479     break;
480     }
481    
482     return FALSE;
483     } /* card_edit_dlg_proc */
484    
485    
486    
487     static int /* fixme: works only roughly */
488     calc_days (int y2, int m2, int d2,
489     int y1, int m1, int d1)
490    
491     {
492     int n=0;
493    
494     if ((y2-y1) > 0)
495     n += (y2-y1)*365;
496     if ((m2-m1) > 0)
497     n += (m2-m1)*30;
498     if ((d2-d1) > 0)
499     n += (d2-d1);
500     else if ((d2-d1) < 0)
501     n -= (d1-d2);
502     return n;
503     }
504    
505    
506     static void
507     keygen_fill_algbox (HWND dlg)
508     {
509     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, (LPARAM)(const char*)"RSA");
510     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
511     }
512    
513    
514     BOOL CALLBACK
515     card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
516     {
517     static int state = 0;
518     static int pwd_state = 0;
519     gpgme_ctx_t ctx;
520     gpgme_editcard_t crd;
521     gpgme_error_t err;
522     char name[128], email[128], comment[128], expdate[64];
523     char pass[128];
524     int card_flags = GPGME_CARDFLAG_NONE;
525     int expires=0;
526     size_t n;
527    
528     switch (msg) {
529     case WM_INITDIALOG:
530     state = 0;
531     pwd_state = 1;
532     center_window (dlg);
533     CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);
534     CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);
535     CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);
536     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);
537     keygen_fill_algbox (dlg);
538     SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
539     SetForegroundWindow (dlg);
540     return FALSE;
541    
542     case WM_COMMAND:
543     if (HIWORD (wparam) == BN_CLICKED) {
544     switch (LOWORD (wparam)) {
545     case IDC_CKEYGEN_NEVER:
546     state ^= 1;
547     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), state);
548     break;
549    
550     case IDC_CKEYGEN_BACKUP:
551     pwd_state ^= 1;
552     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS), pwd_state);
553     break;
554     }
555     }
556    
557     switch (LOWORD (wparam)) {
558     case IDOK:
559     n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);
560     if (!n) {
561     msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);
562     return FALSE;
563     }
564     if (n < 5) {
565     msg_box (dlg, _("Name must be at least 5 characters long."),
566     _("Card Edit"), MB_INFO);
567     return FALSE;
568     }
569     n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);
570     if (!n) {
571     msg_box (dlg, _("Please enter your e-mail address."),
572     _("Card Edit"), MB_ERR);
573     return FALSE;
574     }
575     GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);
576     GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);
577     if (!strchr (email, '@') || n < 3) {
578     msg_box (dlg, _("Please enter a valid e-mail address."),
579     _("Card Edit"), MB_ERR);
580     return FALSE;
581     }
582     n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
583     if (!n) {
584     msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);
585     return FALSE;
586     }
587     n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);
588     if (n > 0)
589     GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1);
590     if (is_8bit_string (name) ||is_8bit_string (comment)) {
591     msg_box (dlg, _("Please use plain ASCII charset for the fields."),
592     _("Card Edit"), MB_INFO);
593     return FALSE;
594     }
595     memset (&pincb, 0, sizeof (pincb));
596     if (do_askpin (dlg, GPGME_EDITCARD_CHAPIN, NULL, &pincb)) {
597     free_pincb (&pincb);
598     return FALSE;
599     }
600     if (do_askpin (dlg, GPGME_EDITCARD_CHUPIN, NULL, &pincb)) {
601     free_pincb (&pincb);
602     return FALSE;
603     }
604     err = gpgme_new (&ctx);
605     if (!err)
606     err = gpgme_editcard_new (&crd);
607     if (err)
608     BUG (0);
609     expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);
610     if (expires) {
611     SYSTEMTIME st, ct;
612     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_CKEYGEN_VALID), &st);
613     if (!keygen_check_date (&st)) {
614     msg_box (dlg, _("The date you have chosen lies in the past."),
615     _("Card Edit"), MB_ERR);
616     gpgme_release (ctx);
617     gpgme_editcard_release (crd);
618     return FALSE;
619     }
620     GetSystemTime (&ct);
621     /* XXX this is not very precise */
622     sprintf (expdate, "%d", calc_days (st.wYear, st.wMonth, st.wDay,
623     ct.wYear, ct.wMonth, ct.wDay));
624     }
625     if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))
626     card_flags |= GPGME_CARDFLAG_REPLACE;
627     if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))
628     card_flags |= GPGME_CARDFLAG_BAKENC;
629     gpgme_editcard_set_keygen_params (crd, card_flags, name, email,
630     n? comment : NULL,
631     expires? expdate : NULL);
632     gpgme_editcard_set_passwd (crd, pass);
633     gpgme_editcard_control (crd, GPGME_EDITCARD_APIN, pincb.apin);
634     gpgme_editcard_control (crd, GPGME_EDITCARD_UPIN, pincb.upin);
635     gpgme_set_edit_ctx (ctx, crd, GPGME_EDITCARD_GENKEY);
636     SetCursor( LoadCursor (NULL, IDC_WAIT));
637     err = gpgme_op_editcard (ctx);
638     SetCursor (LoadCursor (NULL, IDC_ARROW));
639     if (err == GPGME_Canceled)
640     msg_box (dlg, _("Operation was canceled. It seems that there are "
641     "existing\nkeys on the cards. You need to mark the "
642     "'Overwrite' flag."), _("Card Edit"), MB_INFO);
643     else if (err)
644     msg_box (dlg, "The operation does not succeed.\n"
645     "Please make sure you entered the right PIN's."
646     , _("Card Edit"), MB_ERR);
647     else
648     msg_box (dlg, _("Keys successfully created."),
649     _("Card Edit"), MB_OK);
650     memset (pass, 0, sizeof (pass));
651     free_pincb (&pincb);
652     gpgme_release (ctx);
653     gpgme_editcard_release (crd);
654     break;
655    
656     case IDCANCEL:
657     EndDialog (dlg, FALSE);
658     return FALSE;
659     }
660     break;
661     }
662     return FALSE;
663     } /* card_keygen_dlg_proc */
664    
665    
666     static int
667     check_pin_len (int which, int flag, int pinlen)
668     {
669     if (!pinlen) {
670     if (flag)
671     msg_box (NULL, _("Please enter the old card PIN."), _("Card Edit"), MB_ERR);
672     else
673     msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);
674     return -1;
675     }
676     if (which == GPGME_EDITCARD_CHAPIN
677     && pinlen < 8) {
678     msg_box (NULL, _("PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);
679     return -1;
680     }
681     if (which == GPGME_EDITCARD_CHUPIN
682     && pinlen < 6) {
683     msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR);
684     return -1;
685     }
686     return 0;
687     }
688    
689    
690     BOOL CALLBACK
691     card_changepin_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )
692     {
693     gpgme_ctx_t ctx;
694     gpgme_editcard_t chpin;
695     gpgme_error_t rc;
696     gpgme_edit_card_t which;
697     char pold[128], pnew[128];
698     size_t n;
699    
700     switch( msg ) {
701     case WM_INITDIALOG:
702     center_window( dlg );
703     CheckDlgButton( dlg, IDC_CHPIN_ISWORK, BST_CHECKED );
704     SetForegroundWindow( dlg );
705     break;
706    
707     case WM_COMMAND:
708     switch( LOWORD( wparam ) ) {
709     case IDOK:
710     if( IsDlgButtonChecked( dlg, IDC_CHPIN_ISADMIN ) )
711     which = GPGME_EDITCARD_CHAPIN;
712     else if( IsDlgButtonChecked( dlg, IDC_CHPIN_ISWORK ) )
713     which = GPGME_EDITCARD_CHUPIN;
714     else
715     BUG (0);
716    
717     n = item_get_text_length( dlg, IDC_CHPIN_OLDPIN );
718     if (check_pin_len (which, 1, n))
719     return FALSE;
720     n = item_get_text_length( dlg, IDC_CHPIN_NEWPIN );
721     if (check_pin_len (which, 0, n))
722     return FALSE;
723     GetDlgItemText( dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1 );
724     GetDlgItemText( dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1 );
725     rc = gpgme_new( &ctx );
726     if( !rc )
727     rc = gpgme_editcard_new( &chpin );
728     if (rc)
729     BUG (0);
730     gpgme_changepin_set( chpin, which, pold, pnew );
731     gpgme_set_edit_ctx( ctx, chpin, which );
732     rc = gpgme_op_changepin( ctx );
733     if( rc )
734     msg_box( dlg, gpgme_strerror( rc ), _("Card Edit"), MB_ERR );
735     else {
736     msg_box( dlg, _("PIN successfully changed."), _("Card Edit"), MB_OK );
737     SetDlgItemText( dlg, IDC_CHPIN_NEWPIN, "" );
738     SetDlgItemText( dlg, IDC_CHPIN_OLDPIN, "" );
739     }
740     gpgme_release( ctx );
741     gpgme_editcard_release( chpin );
742     break;
743    
744     case IDCANCEL:
745     SetDlgItemText( dlg, IDC_CHPIN_NEWPIN, "" );
746     SetDlgItemText( dlg, IDC_CHPIN_OLDPIN, "" );
747     EndDialog( dlg, FALSE );
748     break;
749     }
750     break;
751     }
752    
753     return FALSE;
754     } /* card_changepin_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26