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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (hide annotations)
Mon Mar 7 13:21:36 2005 UTC (19 years, 11 months ago) by twoaday
File size: 11626 byte(s)
2005-03-03  Timo Schulz  <twoaday@g10code.com>
                                                                                
        * wptCardDlg.cpp (card_changepin_dlg_proc): Add item to re-type the
        new PIN. Suggested by Achim.
        Support to show the unmasked PIN.
        Modified TAB-order.
        * wptPINDlg.cpp (pin_cb_dlg_proc): Show unmasked PIN.
 
        * Fixed wrong GPG --command-fd strings. Thanks to Achim.
 
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.
 


1 twoaday 2 /* wptKeysignDlg.cpp - Key signing dialog
2     * Copyright (C) 2001-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    
24     #include "../resource.h"
25     #include "wptGPG.h"
26     #include "wptNLS.h"
27     #include "wptW32API.h"
28     #include "wptVersion.h"
29     #include "wptTypes.h"
30     #include "wptErrors.h"
31     #include "wptCommonCtl.h"
32     #include "wptContext.h"
33     #include "wptDlgs.h"
34     #include "wptUTF8.h"
35     #include "wptRegistry.h"
36    
37     static int sig_class_choice = 0;
38    
39     static const char *
40     get_printable_fpr (const char * fpr)
41     {
42     static char pfpr[64];
43     int pos = 0;
44     size_t i;
45    
46     for( i = 0; i < strlen( fpr ); i += 4 ) {
47     pfpr[pos++] = fpr[i];
48     pfpr[pos++] = fpr[i+1];
49     pfpr[pos++] = fpr[i+2];
50     pfpr[pos++] = fpr[i+3];
51     pfpr[pos++] = ' ';
52     }
53     return pfpr;
54     } /* get_printable_fpr */
55    
56    
57     static const char *
58     get_keyinfo (gpgme_key_t key)
59     {
60     static char buf[64+16];
61    
62     _snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s",
63     gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0),
64     gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, 0),
65     gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0) + 8 );
66     if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_DIVERT_CARD, NULL, 0))
67     strcat (buf, " (Card)");
68     return buf;
69     } /* get_keyinfo */
70    
71    
72     static int
73     do_fill_seckeylist (HWND dlg, const char * keyid)
74     {
75     gpgme_keycache_t sec;
76     gpgme_key_t pk;
77     const char * s;
78     char * uid, * p;
79     int i = 0, n=0;
80    
81     sec = keycache_get_ctx (0);
82     if (!sec)
83     BUG (0);
84     gpgme_keycache_rewind (sec);
85     while (!gpgme_keycache_next_key (sec, 1, &pk))
86     {
87 twoaday 5 if (!pk)
88     continue;
89 twoaday 2 s = gpgme_key_get_string_attr (pk, GPGME_ATTR_KEYID, NULL, 0);
90     if (!strcmp (s, keyid))
91     continue;
92     /* skip all ElGamal sign+encrypt keys */
93     if( gpgme_key_get_ulong_attr( pk, GPGME_ATTR_ALGO, NULL, 0 )
94     == GPGME_PK_ELG_ES )
95     continue;
96     /* make sure the public key is okay not: revoked, expired or disabled. */
97     if( gpgme_key_get_ulong_attr (pk, GPGME_ATTR_EXPIRE, NULL, 0)
98     || gpgme_key_get_ulong_attr (pk, GPGME_ATTR_KEY_REVOKED, NULL, 0)
99     || gpgme_key_get_ulong_attr (pk, GPGME_ATTR_KEY_DISABLED, NULL, 0))
100     continue;
101     s = gpgme_key_get_string_attr (pk, GPGME_ATTR_NAME, NULL, 0);
102     if( !s )
103     continue;
104     uid = utf8_to_wincp (s, strlen (s));
105     p = new char[strlen( uid ) + 64];
106     if( !p )
107     BUG( NULL );
108     _snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk));
109     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_ADDSTRING, i, (LPARAM)(char *)p);
110     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
111     free_if_alloc (p);
112     free (uid);
113     n++;
114     }
115     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETCURSEL, 0, 0);
116     if (!n)
117     return -1;
118     return 0;
119     } /* do_fill_seckeylist */
120    
121    
122     static void
123     do_add_local_user( gpgme_ctx_t ctx, HWND dlg )
124     {
125     int idx;
126     const char * s;
127     gpgme_key_t key;
128    
129     idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );
130     key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );
131     if( key && (s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 )) )
132     gpgme_set_local_user( ctx, s );
133     } /* do_add_local_user */
134    
135    
136     static void
137     do_check_protection (HWND dlg)
138     {
139     int idx, protec;
140     gpgme_key_t key;
141    
142     idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );
143     key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );
144     if (key)
145     {
146     protec = gpgme_key_get_ulong_attr (key, GPGME_ATTR_IS_PROTECTED, NULL, 0);
147     if (!protec)
148     protec = gpgme_key_get_ulong_attr (key, GPGME_ATTR_DIVERT_CARD, NULL, 0);
149     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
150     }
151     } /* do_check_protection */
152    
153    
154     BOOL CALLBACK
155     sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
156     {
157     switch (msg)
158     {
159     case WM_INITDIALOG:
160     SetWindowText (dlg, _("Choose Signature Class"));
161     SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF, _("How carefully have you verified the key you are about to sign actually belongs to the person? If you don't know what to anwser, use \"0\"."));
162     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0, _("(0) I will not answer (default)"));
163     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1, _("(1) I have not checked at all."));
164     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2, _("(2) I have done causal checking."));
165     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings."));
166     SetForegroundWindow (dlg);
167     center_window (dlg);
168     return TRUE;
169    
170     case WM_COMMAND:
171     switch( LOWORD( wparam ) ) {
172     case IDOK:
173     if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS0 ) )
174     sig_class_choice = 0;
175     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS1 ) )
176     sig_class_choice = 1;
177     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS2 ) )
178     sig_class_choice = 2;
179     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS3 ) )
180     sig_class_choice = 3;
181     else
182     sig_class_choice = 0;
183     EndDialog( dlg, TRUE );
184     break;
185     }
186     break;
187     }
188    
189     return FALSE;
190     } /* sig_class_dlg_proc */
191    
192    
193     BOOL CALLBACK
194     keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
195     {
196     static gpgme_key_t key;
197     static int enable=0;
198     gpgme_editkey_t ke;
199     gpgme_ctx_t ctx;
200     gpgme_error_t err;
201     SYSTEMTIME st;
202     HWND h;
203     char keymsg[4096], pwd[256], *uid = NULL;
204     const char *keyid, *s;
205     u32 created;
206     int type, expires=0;
207    
208    
209     switch ( msg ) {
210     case WM_INITDIALOG:
211     enable = 0;
212     if( lparam == NULL )
213     dlg_fatal_error( dlg, "Could not get dialog param." );
214     #ifndef LANG_DE
215     SetWindowText( dlg, _("Key Signing") );
216     #endif
217     key = (gpgme_key_t) lparam;
218     created = gpgme_key_get_ulong_attr(key, GPGME_ATTR_CREATED, NULL, 0);
219     s = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );
220     if( s )
221     uid = utf8_to_wincp (s, strlen (s));
222     _snprintf( keymsg, sizeof keymsg -1,
223     _("pub %d/%s created: %s\n\n"
224     "Primary key fingerprint: %s\n\n"
225     "\t%s\n\n"
226     "\nAre you really sure that you want to sign this key with YOUR key?\n"),
227     gpgme_key_get_ulong_attr( key, GPGME_ATTR_LEN, NULL, 0 ),
228     gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 )+8,
229     gpgme_key_expand_attr( GPGME_ATTR_CREATED, created ),
230     get_printable_fpr( gpgme_key_get_string_attr( key, GPGME_ATTR_FPR, NULL, 0 ) ),
231     uid );
232     free (uid);
233     s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0);
234     if (do_fill_seckeylist (dlg, s)) {
235     msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );
236     EndDialog( dlg, FALSE );
237     }
238     SetDlgItemText( dlg, IDC_KEYSIGN_INFOS, keymsg );
239     #ifndef LANG_DE
240     SetDlgItemText( dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)") );
241     SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
242     SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
243     #endif
244     CheckDlgButton( dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED );
245     CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
246     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
247     if (reg_prefs.expert == 0)
248     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
249     SetForegroundWindow( dlg );
250     h = GetDlgItem( dlg, IDC_KEYSIGN_PASSPHRASE );
251     SetFocus( h );
252     return FALSE;
253    
254     case WM_SYSCOMMAND:
255     if( LOWORD( wparam ) == SC_CLOSE ) {
256     SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" );
257     EndDialog( dlg, TRUE );
258     }
259     return FALSE;
260    
261     case WM_COMMAND:
262     if( HIWORD( wparam ) == CBN_SELCHANGE ) {
263     do_check_protection( dlg );
264     break;
265     }
266     if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
267     enable ^= 1;
268     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
269     }
270    
271     switch( LOWORD( wparam ) ) {
272     case IDOK:
273     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
274     sig_class_dlg_proc, NULL,
275     _("Choose Signature Class"), IDS_WINPT_SIGCLASS );
276     type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
277     if (type)
278     type = GPGME_EDITKEY_LSIGN;
279     else
280     type = GPGME_EDITKEY_SIGN;
281    
282     if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV))
283     {
284     type = GPGME_EDITKEY_NRSIGN;
285     if (type == GPGME_EDITKEY_LSIGN)
286     type = GPGME_EDITKEY_NRLSIGN;
287     }
288     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
289     expires = 1;
290     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
291     sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
292     }
293    
294     GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, sizeof pwd - 1 );
295     keyid = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );
296     if( !keyid ) {
297     msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );
298     return FALSE;
299     }
300     err = gpgme_new( &ctx );
301     if (err)
302     BUG (0);
303     do_add_local_user (ctx, dlg);
304    
305     err = gpgme_editkey_new (&ke);
306     if (err)
307     BUG (0);
308     err = gpgme_editkey_sign_set (ke, pwd, sig_class_choice, type,
309     expires? keymsg : NULL);
310     if (err) {
311     msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
312     gpgme_release (ctx);
313     gpgme_editkey_release (ke);
314     return FALSE;
315     }
316    
317     gpgme_set_edit_ctx (ctx, ke, type);
318    
319     err = gpgme_op_editkey( ctx, keyid );
320     memset( &pwd, 0, sizeof pwd );
321     if (err == GPGME_Conflict)
322     Sleep (500); /* it seems the process is not really finished */
323     gpgme_release( ctx );
324     gpgme_editkey_release( ke );
325     if( err ) {
326     if( err == GPGME_Conflict )
327     msg_box( dlg, _("Key is already signed by your key."), _("Key Signing"), MB_INFO );
328     else if( err == GPGME_Invalid_Mode )
329     msg_box( dlg, _("Unusable secret key."), _("Key Signing"), MB_ERR );
330     else
331     msg_box( dlg, gpgme_strerror( err ), _("Key Signing"), MB_ERR );
332     }
333     else {
334     status_box (dlg, _("Key successfully signed."), PGM_NAME);
335     keycache_set_reload (1);
336     EndDialog (dlg, TRUE);
337     }
338     return TRUE;
339    
340     case IDCANCEL:
341     EndDialog( dlg, FALSE );
342     return FALSE;
343     }
344     break;
345     }
346    
347     return FALSE;
348     } /* keysign_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26