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