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

Annotation of /trunk/Src/wptCardDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations)
Mon Apr 4 06:59:24 2005 UTC (19 years, 10 months ago) by twoaday
File size: 22417 byte(s)
2005-03-04  Timo Schulz  <twoaday@g10code.com>
 
        * GPG asks twice for the new PIN. Thanks to Achim.
        * wptCardDlg.cpp (card_changepin_dlg_proc): Reset the 'safety' pin also.        Only check the passphrase if the backup flag is enabled. Again thanks to        Achim.
 
2005-03-06  Timo Schulz  <twoaday@freakmail.de>
 
        * wptKeySignDlg.cpp (do_fill_seckeylist): Skip secret keys without
        a public key. Noted by Kurt Fitzner.
 
2005-03-22  Timo Schulz  <twoaday@freakmail.de>
 
        * WinPT.cpp (WinMain): --debug as an alias for --enable-debug.
        (enable_mobile_mode): New.
        * wptKeyEditDlg.cpp (keyedit_addsubkey_dlg_proc): Use new
        ID's for adding subkeys.
 


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     #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 twoaday 6 gpgme_card_get_string_attr ((card), (what), NULL, 1)
65 twoaday 2 #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 twoaday 6 /* XXX: show version as 1.1 not 0101 */
190     for (idx=1; attr_tab[idx].attr; idx++) {
191     s = card_get_string (card, attr_tab[idx].attr);
192     SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err);
193 twoaday 2 }
194    
195     s = card_get_string( card, GPGME_ATTR_CARD_LANG );
196     idx = idx_from_lang( s );
197     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0 );
198    
199     t = card_get_ulong( card, GPGME_ATTR_CARD_SEX );
200     switch( t ) {
201     case 'm': idx=0; break;
202     case 'f': idx=1; break;
203     default :
204     case 'u': idx=2; break;
205     }
206     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0 );
207    
208     return 0;
209     } /* card_status */
210    
211    
212     static void
213     prepare_dialog( HWND dlg )
214     {
215     const char * s;
216     int i;
217    
218     for( i=0; (s = sex[i]); i++ )
219     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s );
220     SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0 );
221     for( i=0; (s = lang[i]); i++ )
222     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s );
223     SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0 );
224     } /* prepare_dialog */
225    
226    
227     static int
228     check_string( const char * str, int flags )
229     {
230     size_t i;
231     for( i=0; i < strlen( str ); i++ ) {
232     if( flags & 0x02 && !isalpha( str[i] ) )
233     return -1;
234     }
235     return 0;
236     }
237    
238    
239     static int
240     do_proc_card_cmds( HWND dlg, struct pin_cb_ctx_s * pincb, gpgme_card_t card )
241     {
242     static struct {
243     int id;
244     int cmd;
245     int us_ascii;
246     int changed;
247     } idctl[] = {
248     {IDC_CEDIT_NAME, GPGME_EDITCARD_NAME, 1, 0},
249     {IDC_CEDIT_LANG2, GPGME_EDITCARD_LANG, 1, 0},
250     {IDC_CEDIT_SEX2, GPGME_EDITCARD_SEX, 1|1,0},
251     {IDC_CEDIT_KEYURL,GPGME_EDITCARD_KEYURL,1, 0},
252     {IDC_CEDIT_LOGIN, GPGME_EDITCARD_LOGIN, 1, 0},
253     {0}
254     };
255     gpgme_editcard_t ec;
256     gpgme_ctx_t ctx;
257     gpgme_error_t rc;
258     char buf[256], tmp[128];
259     int errc=0;
260     int i, id, n=0;
261    
262     /* XXX rewrite the entire function */
263     for( i=0; idctl[i].id; i++ ) /* reset */
264     idctl[i].changed = 0;
265    
266     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_LANG2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
267     idctl[1].changed = 1;
268     n++;
269     }
270     if( SendMessage( GetDlgItem( dlg, IDC_CEDIT_SEX2 ), WM_GETTEXTLENGTH, 0, 0 ) ) {
271     idctl[2].changed = 1;
272     n++;
273     }
274    
275     if( SendDlgItemMessage( dlg, IDC_CEDIT_NAME2, EM_GETMODIFY, 0, 0 ) ) {
276     idctl[0].changed = 1;
277     n++;
278     }
279     for( i=0; (id = idctl[i].id); i++ ) {
280     if( SendDlgItemMessage( dlg, id, EM_GETMODIFY, 0, 0 ) ) {
281     idctl[i].changed = 1;
282     n++;
283     }
284     }
285     if( !pincb || !card ) /* just return the changed elements */
286     return n;
287     if( !n )
288     return 0;
289    
290     rc = gpgme_editcard_new( &ec );
291     if( !rc )
292     rc = gpgme_new( &ctx );
293     if( rc )
294     BUG( NULL );
295     gpgme_editcard_control( ec, GPGME_EDITCARD_APIN, pincb->apin );
296     gpgme_editcard_control( ec, GPGME_EDITCARD_UPIN, pincb->upin );
297     for( i=0; idctl[i].id; i++ ) {
298     if( idctl[i].changed ) {
299     GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 );
300     if (idctl[i].us_ascii && is_8bit_string (buf)) {
301     msg_box (dlg, _("Only plain ASCII is currently allowed."),
302     _("Card Edit"), MB_ERR);
303     errc--; continue;
304     }
305     if( (idctl[i].us_ascii & 2) && check_string( buf, 2 ) ) {
306     msg_box( dlg, _("Only alphabetic characters are allowed."),
307     _("Card Edit"), MB_ERR );
308     errc--; continue;
309     }
310     if( idctl[i].cmd == GPGME_EDITCARD_NAME ) {
311     /* The "name" command actually needs two fields */
312     GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 );
313     gpgme_editcard_control( ec, GPGME_EDITCARD_NAME2, tmp );
314     }
315     gpgme_editcard_control( ec, idctl[i].cmd, buf );
316     gpgme_set_edit_ctx( ctx, ec, idctl[i].cmd );
317     rc = gpgme_op_editcard( ctx );
318     if( rc ) {
319     msg_box( dlg, _("Could not modify card attribute."),
320     _("Card Edit"), MB_ERR );
321     errc--;
322     }
323     }
324     }
325     if( !errc ) {
326     /* if the operation(s) succeeded, reset the modify flag for each control */
327     for( i = 0; idctl[i].id; i++ )
328     SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 );
329     msg_box( dlg, _("Card attribute changed."), _("Card Edit"), MB_OK );
330     SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" );
331     SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" );
332     }
333     gpgme_editcard_release( ec );
334     gpgme_release( ctx );
335     return errc;
336     } /* do_proc_card_cmds */
337    
338    
339     void
340     free_pincb (struct pin_cb_ctx_s * ctx)
341     {
342     if (!ctx)
343     return;
344     free_if_alloc (ctx->info_text);
345     free_if_alloc (ctx->upin);
346     free_if_alloc (ctx->apin);
347     } /* free_pincb */
348    
349    
350     static int
351     do_askpin( HWND dlg, gpgme_edit_card_t which, gpgme_card_t card,
352     struct pin_cb_ctx_s * pincb )
353     {
354     const char * s, * fmt;
355     const char * n1, * n2, * serial;
356     char * p;
357     size_t n;
358    
359     if( (which == GPGME_EDITCARD_CHAPIN && pincb->apin) ||
360     (which == GPGME_EDITCARD_CHUPIN && pincb->upin) )
361     return 0;
362    
363     if( which == GPGME_EDITCARD_CHAPIN )
364     s = _("Please enter the 'Admin PIN'");
365     else if( which == GPGME_EDITCARD_CHUPIN )
366     s = _("Please enter the 'User PIN'");
367     else
368     s = _("Please enter the PIN");
369     pincb->which = which;
370     free_if_alloc( pincb->info_text );
371     if( card ) {
372     fmt = _("%s\nName: %s %s\nSerial-No: %s\n");
373     n1 = card_get_string( card, GPGME_ATTR_CARD_NAME );
374     n2 = card_get_string( card, GPGME_ATTR_CARD_NAME2 );
375     if( !n1 || !n2 ) {
376     n1 = "No"; n2 = "Name";
377     }
378     serial = card_get_string( card, GPGME_ATTR_CARD_SERIAL );
379     if( !serial )
380     serial = "No Serial";
381     n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3;
382     p = pincb->info_text = new char[strlen( s )+n+1 ];
383     if( !p )
384     BUG (0);
385     sprintf( p, fmt, s, n1, n2, serial );
386     }
387     else {
388     p = pincb->info_text = m_strdup (s);
389     if (!p)
390     BUG (0);
391     }
392     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg,
393     pin_cb_dlg_proc, (LPARAM)pincb);
394     if( !pincb->apin && !pincb->upin) {
395     safe_free (pincb->info_text);
396     return -1;
397     }
398     return 0;
399     } /* do_askpin */
400    
401    
402     BOOL CALLBACK
403     card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
404     {
405     static gpgme_card_t card;
406     char tmp[128];
407     size_t n=0;
408    
409     switch (msg) {
410     case WM_INITDIALOG:
411     card = (gpgme_card_t)lparam;
412     if (!card)
413     BUG (0);
414     prepare_dialog (dlg);
415     if (card_status (dlg, card ))
416     EndDialog (dlg, TRUE);
417     center_window (dlg);
418     SetForegroundWindow (dlg);
419     return TRUE;
420    
421     case WM_DESTROY:
422     free_if_alloc (pincb.info_text);
423     free_if_alloc (pincb.apin);
424     free_if_alloc (pincb.upin);
425     memset (&pincb, 0, sizeof pincb);
426     break;
427    
428     case WM_COMMAND:
429     switch( HIWORD( wparam ) ) {
430     case CBN_KILLFOCUS:
431     case CBN_EDITCHANGE:
432     case CBN_EDITUPDATE:
433     int ctlid = GetDlgCtrlID( (HWND)lparam );
434     int dstid = 0;
435    
436     switch (ctlid) {
437     case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break;
438     case IDC_CEDIT_SEX: dstid = IDC_CEDIT_SEX2; break;
439     }
440     GetDlgItemText (dlg, ctlid, tmp, 127);
441     SetDlgItemText (dlg, dstid, tmp);
442     break;
443     }
444     switch( LOWORD( wparam ) ) {
445     case IDC_CEDIT_CHPIN:
446     DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg,
447     card_changepin_dlg_proc, NULL );
448     break;
449    
450     case IDC_CEDIT_NEWKEYS:
451     if (item_get_text_length (dlg, IDC_CEDIT_FPR1) > 0) {
452     int id = msg_box (dlg,
453     _("This operation will override the keys on the card.\n"
454     "Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO);
455     if (id == IDNO)
456     return FALSE;
457     }
458     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN,
459     glob_hwnd, card_keygen_dlg_proc, NULL);
460     break;
461    
462     case IDOK:
463     n = do_proc_card_cmds( dlg, NULL, NULL );
464     if( n ) {
465     if( do_askpin( dlg, GPGME_EDITCARD_CHAPIN, card, &pincb ) )
466     EndDialog( dlg, FALSE );
467     if( do_askpin( dlg, GPGME_EDITCARD_CHUPIN, card, &pincb ) )
468     EndDialog( dlg, FALSE );
469     }
470     do_proc_card_cmds( dlg, &pincb, card );
471     free_pincb( &pincb );
472     if( !n )
473     EndDialog( dlg, TRUE );
474     break;
475    
476     case IDCANCEL:
477     EndDialog( dlg, FALSE );
478     break;
479     }
480     break;
481     }
482    
483     return FALSE;
484     } /* card_edit_dlg_proc */
485    
486    
487    
488     static int /* fixme: works only roughly */
489     calc_days (int y2, int m2, int d2,
490     int y1, int m1, int d1)
491    
492     {
493     int n=0;
494    
495     if ((y2-y1) > 0)
496     n += (y2-y1)*365;
497     if ((m2-m1) > 0)
498     n += (m2-m1)*30;
499     if ((d2-d1) > 0)
500     n += (d2-d1);
501     else if ((d2-d1) < 0)
502     n -= (d1-d2);
503     return n;
504     }
505    
506    
507     static void
508     keygen_fill_algbox (HWND dlg)
509     {
510     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, (LPARAM)(const char*)"RSA");
511     SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0);
512     }
513    
514    
515     BOOL CALLBACK
516     card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
517     {
518     static int state = 0;
519     static int pwd_state = 0;
520     gpgme_ctx_t ctx;
521     gpgme_editcard_t crd;
522     gpgme_error_t err;
523     char name[128], email[128], comment[128], expdate[64];
524     char pass[128];
525     int card_flags = GPGME_CARDFLAG_NONE;
526     int expires=0;
527     size_t n;
528    
529     switch (msg) {
530     case WM_INITDIALOG:
531     state = 0;
532     pwd_state = 1;
533     center_window (dlg);
534     CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED);
535     CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED);
536     CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED);
537     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE);
538     keygen_fill_algbox (dlg);
539     SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME));
540     SetForegroundWindow (dlg);
541     return FALSE;
542    
543     case WM_COMMAND:
544     if (HIWORD (wparam) == BN_CLICKED) {
545     switch (LOWORD (wparam)) {
546     case IDC_CKEYGEN_NEVER:
547     state ^= 1;
548     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), state);
549     break;
550    
551     case IDC_CKEYGEN_BACKUP:
552     pwd_state ^= 1;
553     EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS), pwd_state);
554     break;
555     }
556     }
557    
558     switch (LOWORD (wparam)) {
559     case IDOK:
560     n = item_get_text_length (dlg, IDC_CKEYGEN_NAME);
561     if (!n) {
562     msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR);
563     return FALSE;
564     }
565     if (n < 5) {
566     msg_box (dlg, _("Name must be at least 5 characters long."),
567     _("Card Edit"), MB_INFO);
568     return FALSE;
569     }
570     n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL);
571     if (!n) {
572     msg_box (dlg, _("Please enter your e-mail address."),
573     _("Card Edit"), MB_ERR);
574     return FALSE;
575     }
576     GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1);
577     GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1);
578     if (!strchr (email, '@') || n < 3) {
579     msg_box (dlg, _("Please enter a valid e-mail address."),
580     _("Card Edit"), MB_ERR);
581     return FALSE;
582     }
583     n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1);
584 twoaday 5 if (!n && pwd_state == 1) {
585 twoaday 2 msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR);
586     return FALSE;
587     }
588     n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT);
589     if (n > 0)
590     GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1);
591     if (is_8bit_string (name) ||is_8bit_string (comment)) {
592     msg_box (dlg, _("Please use plain ASCII charset for the fields."),
593     _("Card Edit"), MB_INFO);
594     return FALSE;
595     }
596     memset (&pincb, 0, sizeof (pincb));
597     if (do_askpin (dlg, GPGME_EDITCARD_CHAPIN, NULL, &pincb)) {
598     free_pincb (&pincb);
599     return FALSE;
600     }
601     if (do_askpin (dlg, GPGME_EDITCARD_CHUPIN, NULL, &pincb)) {
602     free_pincb (&pincb);
603     return FALSE;
604     }
605     err = gpgme_new (&ctx);
606     if (!err)
607     err = gpgme_editcard_new (&crd);
608     if (err)
609     BUG (0);
610     expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER);
611     if (expires) {
612     SYSTEMTIME st, ct;
613     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_CKEYGEN_VALID), &st);
614     if (!keygen_check_date (&st)) {
615     msg_box (dlg, _("The date you have chosen lies in the past."),
616     _("Card Edit"), MB_ERR);
617     gpgme_release (ctx);
618     gpgme_editcard_release (crd);
619     return FALSE;
620     }
621     GetSystemTime (&ct);
622     /* XXX this is not very precise */
623     sprintf (expdate, "%d", calc_days (st.wYear, st.wMonth, st.wDay,
624     ct.wYear, ct.wMonth, ct.wDay));
625     }
626     if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE))
627     card_flags |= GPGME_CARDFLAG_REPLACE;
628     if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP))
629     card_flags |= GPGME_CARDFLAG_BAKENC;
630     gpgme_editcard_set_keygen_params (crd, card_flags, name, email,
631     n? comment : NULL,
632     expires? expdate : NULL);
633     gpgme_editcard_set_passwd (crd, pass);
634     gpgme_editcard_control (crd, GPGME_EDITCARD_APIN, pincb.apin);
635     gpgme_editcard_control (crd, GPGME_EDITCARD_UPIN, pincb.upin);
636     gpgme_set_edit_ctx (ctx, crd, GPGME_EDITCARD_GENKEY);
637     SetCursor( LoadCursor (NULL, IDC_WAIT));
638     err = gpgme_op_editcard (ctx);
639     SetCursor (LoadCursor (NULL, IDC_ARROW));
640     if (err == GPGME_Canceled)
641     msg_box (dlg, _("Operation was canceled. It seems that there are "
642     "existing\nkeys on the cards. You need to mark the "
643     "'Overwrite' flag."), _("Card Edit"), MB_INFO);
644     else if (err)
645     msg_box (dlg, "The operation does not succeed.\n"
646     "Please make sure you entered the right PIN's."
647     , _("Card Edit"), MB_ERR);
648     else
649     msg_box (dlg, _("Keys successfully created."),
650     _("Card Edit"), MB_OK);
651     memset (pass, 0, sizeof (pass));
652     free_pincb (&pincb);
653     gpgme_release (ctx);
654     gpgme_editcard_release (crd);
655     break;
656    
657     case IDCANCEL:
658     EndDialog (dlg, FALSE);
659     return FALSE;
660     }
661     break;
662     }
663     return FALSE;
664     } /* card_keygen_dlg_proc */
665    
666    
667     static int
668     check_pin_len (int which, int flag, int pinlen)
669     {
670     if (!pinlen) {
671     if (flag)
672     msg_box (NULL, _("Please enter the old card PIN."), _("Card Edit"), MB_ERR);
673     else
674     msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR);
675     return -1;
676     }
677     if (which == GPGME_EDITCARD_CHAPIN
678     && pinlen < 8) {
679     msg_box (NULL, _("PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR);
680     return -1;
681     }
682     if (which == GPGME_EDITCARD_CHUPIN
683     && pinlen < 6) {
684     msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR);
685     return -1;
686     }
687     return 0;
688     }
689    
690    
691     BOOL CALLBACK
692 twoaday 5 card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
693 twoaday 2 {
694 twoaday 5 static int hide = 1;
695 twoaday 2 gpgme_ctx_t ctx;
696     gpgme_editcard_t chpin;
697     gpgme_error_t rc;
698     gpgme_edit_card_t which;
699 twoaday 5 char pold[128], pnew[128], pnew2[128];
700 twoaday 2 size_t n;
701    
702     switch( msg ) {
703     case WM_INITDIALOG:
704 twoaday 5 hide = 1;
705     CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED);
706     center_window (dlg);
707     CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED);
708     SetForegroundWindow (dlg);
709 twoaday 2 break;
710    
711     case WM_COMMAND:
712 twoaday 5 if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) {
713     HWND hwnd;
714     hide ^= 1;
715     hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN);
716     SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
717     SetFocus (hwnd);
718     hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN2);
719     SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
720     SetFocus (hwnd);
721     }
722 twoaday 2 switch( LOWORD( wparam ) ) {
723     case IDOK:
724 twoaday 5 if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN))
725 twoaday 2 which = GPGME_EDITCARD_CHAPIN;
726 twoaday 5 else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK))
727 twoaday 2 which = GPGME_EDITCARD_CHUPIN;
728     else
729     BUG (0);
730    
731 twoaday 5 n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN);
732 twoaday 2 if (check_pin_len (which, 1, n))
733     return FALSE;
734 twoaday 5 n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN);
735 twoaday 2 if (check_pin_len (which, 0, n))
736     return FALSE;
737 twoaday 5 n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2);
738     if (check_pin_len (which, 0, n))
739     return FALSE;
740     GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1);
741     GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1);
742     GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1);
743     if (strcmp (pnew, pnew2)) {
744     msg_box (dlg, _("Passphrases do not match. Please try again."), _("Card Edit"), MB_ERR);
745     return FALSE;
746     }
747     rc = gpgme_new (&ctx);
748     if (!rc)
749     rc = gpgme_editcard_new (&chpin);
750 twoaday 2 if (rc)
751     BUG (0);
752     gpgme_changepin_set( chpin, which, pold, pnew );
753     gpgme_set_edit_ctx( ctx, chpin, which );
754     rc = gpgme_op_changepin( ctx );
755     if( rc )
756     msg_box( dlg, gpgme_strerror( rc ), _("Card Edit"), MB_ERR );
757     else {
758     msg_box( dlg, _("PIN successfully changed."), _("Card Edit"), MB_OK );
759 twoaday 5 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
760     SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
761     SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
762 twoaday 2 }
763     gpgme_release( ctx );
764     gpgme_editcard_release( chpin );
765     break;
766    
767     case IDCANCEL:
768 twoaday 5 SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, "");
769     SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, "");
770     SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, "");
771 twoaday 2 EndDialog( dlg, FALSE );
772     break;
773     }
774     break;
775     }
776    
777     return FALSE;
778     } /* card_changepin_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26