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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Mon Apr 25 07:15:30 2005 UTC (19 years, 10 months ago) by twoaday
File size: 11682 byte(s)
Commit bug fixes. Release 0.9.92.

1 twoaday 2 /* wptKeysignDlg.cpp - Key signing dialog
2 twoaday 13 * Copyright (C) 2001-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    
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 twoaday 13 for( i = 0; i < strlen (fpr); i += 4 ) {
47 twoaday 2 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 twoaday 13 do_add_local_user (gpgme_ctx_t ctx, HWND dlg)
124 twoaday 2 {
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 twoaday 13 CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
167 twoaday 2 SetForegroundWindow (dlg);
168     center_window (dlg);
169     return TRUE;
170    
171     case WM_COMMAND:
172     switch( LOWORD( wparam ) ) {
173     case IDOK:
174     if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS0 ) )
175     sig_class_choice = 0;
176     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS1 ) )
177     sig_class_choice = 1;
178     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS2 ) )
179     sig_class_choice = 2;
180     else if( IsDlgButtonChecked( dlg, IDC_SIGCLASS_CLASS3 ) )
181     sig_class_choice = 3;
182     else
183     sig_class_choice = 0;
184     EndDialog( dlg, TRUE );
185     break;
186     }
187     break;
188     }
189    
190     return FALSE;
191     } /* sig_class_dlg_proc */
192    
193    
194     BOOL CALLBACK
195     keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
196     {
197     static gpgme_key_t key;
198     static int enable=0;
199     gpgme_editkey_t ke;
200     gpgme_ctx_t ctx;
201     gpgme_error_t err;
202     SYSTEMTIME st;
203     HWND h;
204     char keymsg[4096], pwd[256], *uid = NULL;
205     const char *keyid, *s;
206     u32 created;
207     int type, expires=0;
208    
209    
210     switch ( msg ) {
211     case WM_INITDIALOG:
212     enable = 0;
213     if( lparam == NULL )
214     dlg_fatal_error( dlg, "Could not get dialog param." );
215     #ifndef LANG_DE
216     SetWindowText( dlg, _("Key Signing") );
217     #endif
218     key = (gpgme_key_t) lparam;
219     created = gpgme_key_get_ulong_attr(key, GPGME_ATTR_CREATED, NULL, 0);
220     s = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );
221     if( s )
222     uid = utf8_to_wincp (s, strlen (s));
223     _snprintf( keymsg, sizeof keymsg -1,
224     _("pub %d/%s created: %s\n\n"
225     "Primary key fingerprint: %s\n\n"
226     "\t%s\n\n"
227     "\nAre you really sure that you want to sign this key with YOUR key?\n"),
228     gpgme_key_get_ulong_attr( key, GPGME_ATTR_LEN, NULL, 0 ),
229     gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 )+8,
230     gpgme_key_expand_attr( GPGME_ATTR_CREATED, created ),
231     get_printable_fpr( gpgme_key_get_string_attr( key, GPGME_ATTR_FPR, NULL, 0 ) ),
232     uid );
233     free (uid);
234     s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0);
235     if (do_fill_seckeylist (dlg, s)) {
236     msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );
237     EndDialog( dlg, FALSE );
238     }
239     SetDlgItemText( dlg, IDC_KEYSIGN_INFOS, keymsg );
240     #ifndef LANG_DE
241     SetDlgItemText( dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)") );
242     SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
243     SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
244     #endif
245     CheckDlgButton( dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED );
246     CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
247     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
248     if (reg_prefs.expert == 0)
249     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
250     SetForegroundWindow( dlg );
251     h = GetDlgItem( dlg, IDC_KEYSIGN_PASSPHRASE );
252     SetFocus( h );
253     return FALSE;
254    
255     case WM_SYSCOMMAND:
256     if( LOWORD( wparam ) == SC_CLOSE ) {
257     SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" );
258     EndDialog( dlg, TRUE );
259     }
260     return FALSE;
261    
262     case WM_COMMAND:
263     if( HIWORD( wparam ) == CBN_SELCHANGE ) {
264     do_check_protection( dlg );
265     break;
266     }
267     if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
268     enable ^= 1;
269     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
270     }
271    
272     switch( LOWORD( wparam ) ) {
273     case IDOK:
274     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
275     sig_class_dlg_proc, NULL,
276     _("Choose Signature Class"), IDS_WINPT_SIGCLASS );
277     type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
278     if (type)
279     type = GPGME_EDITKEY_LSIGN;
280     else
281     type = GPGME_EDITKEY_SIGN;
282    
283     if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV))
284     {
285     type = GPGME_EDITKEY_NRSIGN;
286     if (type == GPGME_EDITKEY_LSIGN)
287     type = GPGME_EDITKEY_NRLSIGN;
288     }
289     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
290     expires = 1;
291     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
292     sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
293     }
294    
295     GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, sizeof pwd - 1 );
296     keyid = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );
297     if( !keyid ) {
298     msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );
299     return FALSE;
300     }
301     err = gpgme_new( &ctx );
302     if (err)
303     BUG (0);
304     do_add_local_user (ctx, dlg);
305    
306     err = gpgme_editkey_new (&ke);
307     if (err)
308     BUG (0);
309     err = gpgme_editkey_sign_set (ke, pwd, sig_class_choice, type,
310     expires? keymsg : NULL);
311     if (err) {
312     msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
313     gpgme_release (ctx);
314     gpgme_editkey_release (ke);
315     return FALSE;
316     }
317    
318     gpgme_set_edit_ctx (ctx, ke, type);
319    
320     err = gpgme_op_editkey( ctx, keyid );
321     memset( &pwd, 0, sizeof pwd );
322     if (err == GPGME_Conflict)
323     Sleep (500); /* it seems the process is not really finished */
324     gpgme_release( ctx );
325     gpgme_editkey_release( ke );
326     if( err ) {
327     if( err == GPGME_Conflict )
328     msg_box( dlg, _("Key is already signed by your key."), _("Key Signing"), MB_INFO );
329     else if( err == GPGME_Invalid_Mode )
330     msg_box( dlg, _("Unusable secret key."), _("Key Signing"), MB_ERR );
331     else
332     msg_box( dlg, gpgme_strerror( err ), _("Key Signing"), MB_ERR );
333     }
334     else {
335     status_box (dlg, _("Key successfully signed."), PGM_NAME);
336     keycache_set_reload (1);
337     EndDialog (dlg, TRUE);
338     }
339     return TRUE;
340    
341     case IDCANCEL:
342     EndDialog( dlg, FALSE );
343     return FALSE;
344     }
345     break;
346     }
347    
348     return FALSE;
349     } /* keysign_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26