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 |
twoaday |
22 |
#include "wptKeyList.h" |
37 |
twoaday |
2 |
|
38 |
|
|
static int sig_class_choice = 0; |
39 |
|
|
|
40 |
|
|
static const char * |
41 |
|
|
get_printable_fpr (const char * fpr) |
42 |
|
|
{ |
43 |
|
|
static char pfpr[64]; |
44 |
|
|
int pos = 0; |
45 |
|
|
size_t i; |
46 |
|
|
|
47 |
twoaday |
22 |
for (i = 0; i < strlen (fpr); i += 4) { |
48 |
twoaday |
2 |
pfpr[pos++] = fpr[i]; |
49 |
|
|
pfpr[pos++] = fpr[i+1]; |
50 |
|
|
pfpr[pos++] = fpr[i+2]; |
51 |
|
|
pfpr[pos++] = fpr[i+3]; |
52 |
|
|
pfpr[pos++] = ' '; |
53 |
|
|
} |
54 |
|
|
return pfpr; |
55 |
|
|
} /* get_printable_fpr */ |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
static const char * |
59 |
|
|
get_keyinfo (gpgme_key_t key) |
60 |
|
|
{ |
61 |
|
|
static char buf[64+16]; |
62 |
|
|
|
63 |
|
|
_snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s", |
64 |
|
|
gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0), |
65 |
|
|
gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, 0), |
66 |
|
|
gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0) + 8 ); |
67 |
|
|
if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_DIVERT_CARD, NULL, 0)) |
68 |
|
|
strcat (buf, " (Card)"); |
69 |
|
|
return buf; |
70 |
|
|
} /* get_keyinfo */ |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
static int |
74 |
|
|
do_fill_seckeylist (HWND dlg, const char * keyid) |
75 |
|
|
{ |
76 |
|
|
gpgme_keycache_t sec; |
77 |
|
|
gpgme_key_t pk; |
78 |
|
|
const char * s; |
79 |
|
|
char * uid, * p; |
80 |
|
|
int i = 0, n=0; |
81 |
|
|
|
82 |
|
|
sec = keycache_get_ctx (0); |
83 |
|
|
if (!sec) |
84 |
|
|
BUG (0); |
85 |
|
|
gpgme_keycache_rewind (sec); |
86 |
twoaday |
22 |
while (!gpgme_keycache_next_key (sec, 1, &pk)) { |
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 |
twoaday |
22 |
if (!s) |
103 |
twoaday |
2 |
continue; |
104 |
|
|
uid = utf8_to_wincp (s, strlen (s)); |
105 |
|
|
p = new char[strlen( uid ) + 64]; |
106 |
twoaday |
22 |
if (!p) |
107 |
twoaday |
2 |
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 |
twoaday |
22 |
switch (msg) { |
158 |
twoaday |
2 |
case WM_INITDIALOG: |
159 |
|
|
SetWindowText (dlg, _("Choose Signature Class")); |
160 |
|
|
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\".")); |
161 |
|
|
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0, _("(0) I will not answer (default)")); |
162 |
|
|
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1, _("(1) I have not checked at all.")); |
163 |
|
|
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2, _("(2) I have done causal checking.")); |
164 |
|
|
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings.")); |
165 |
twoaday |
13 |
CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED); |
166 |
twoaday |
2 |
SetForegroundWindow (dlg); |
167 |
|
|
center_window (dlg); |
168 |
|
|
return TRUE; |
169 |
|
|
|
170 |
|
|
case WM_COMMAND: |
171 |
|
|
switch( LOWORD( wparam ) ) { |
172 |
|
|
case IDOK: |
173 |
twoaday |
22 |
if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0)) |
174 |
twoaday |
2 |
sig_class_choice = 0; |
175 |
twoaday |
22 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1)) |
176 |
twoaday |
2 |
sig_class_choice = 1; |
177 |
twoaday |
22 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2)) |
178 |
twoaday |
2 |
sig_class_choice = 2; |
179 |
twoaday |
22 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3)) |
180 |
twoaday |
2 |
sig_class_choice = 3; |
181 |
|
|
else |
182 |
|
|
sig_class_choice = 0; |
183 |
twoaday |
22 |
EndDialog (dlg, TRUE); |
184 |
twoaday |
2 |
break; |
185 |
|
|
} |
186 |
|
|
break; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
return FALSE; |
190 |
|
|
} /* sig_class_dlg_proc */ |
191 |
|
|
|
192 |
|
|
|
193 |
twoaday |
22 |
static const char* |
194 |
|
|
get_expire_date (gpgme_key_t key) |
195 |
|
|
{ |
196 |
|
|
static char tmp[64]; |
197 |
|
|
|
198 |
|
|
u32 u = gpgme_key_get_ulong_attr (key, GPGME_ATTR_EXPIRES, NULL, 0); |
199 |
|
|
if (!u) |
200 |
|
|
return "never"; |
201 |
|
|
return get_key_expire_date (u); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
|
205 |
twoaday |
2 |
BOOL CALLBACK |
206 |
|
|
keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
207 |
|
|
{ |
208 |
twoaday |
22 |
static winpt_key_t key; |
209 |
twoaday |
2 |
gpgme_editkey_t ke; |
210 |
|
|
gpgme_ctx_t ctx; |
211 |
|
|
gpgme_error_t err; |
212 |
|
|
SYSTEMTIME st; |
213 |
|
|
HWND h; |
214 |
|
|
char keymsg[4096], pwd[256], *uid = NULL; |
215 |
|
|
const char *keyid, *s; |
216 |
|
|
u32 created; |
217 |
|
|
int type, expires=0; |
218 |
|
|
|
219 |
|
|
|
220 |
|
|
switch ( msg ) { |
221 |
|
|
case WM_INITDIALOG: |
222 |
twoaday |
22 |
if (lparam == NULL) |
223 |
twoaday |
2 |
dlg_fatal_error( dlg, "Could not get dialog param." ); |
224 |
|
|
#ifndef LANG_DE |
225 |
|
|
SetWindowText( dlg, _("Key Signing") ); |
226 |
|
|
#endif |
227 |
twoaday |
22 |
key = (winpt_key_t) lparam; |
228 |
|
|
created = gpgme_key_get_ulong_attr (key->ctx, GPGME_ATTR_CREATED, NULL, 0); |
229 |
|
|
s = gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_USERID, NULL, 0 ); |
230 |
twoaday |
2 |
if( s ) |
231 |
|
|
uid = utf8_to_wincp (s, strlen (s)); |
232 |
|
|
_snprintf( keymsg, sizeof keymsg -1, |
233 |
twoaday |
22 |
_("pub %d/%s created: %s expires: %s\n\n" |
234 |
twoaday |
2 |
"Primary key fingerprint: %s\n\n" |
235 |
|
|
"\t%s\n\n" |
236 |
|
|
"\nAre you really sure that you want to sign this key with YOUR key?\n"), |
237 |
twoaday |
22 |
gpgme_key_get_ulong_attr( key->ctx, GPGME_ATTR_LEN, NULL, 0 ), |
238 |
|
|
gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_KEYID, NULL, 0 )+8, |
239 |
twoaday |
2 |
gpgme_key_expand_attr( GPGME_ATTR_CREATED, created ), |
240 |
twoaday |
22 |
get_expire_date (key->ctx), |
241 |
|
|
get_printable_fpr( gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_FPR, NULL, 0 ) ), |
242 |
twoaday |
2 |
uid ); |
243 |
|
|
free (uid); |
244 |
twoaday |
22 |
s = gpgme_key_get_string_attr (key->ctx, GPGME_ATTR_KEYID, NULL, 0); |
245 |
twoaday |
2 |
if (do_fill_seckeylist (dlg, s)) { |
246 |
|
|
msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR ); |
247 |
|
|
EndDialog( dlg, FALSE ); |
248 |
|
|
} |
249 |
|
|
SetDlgItemText( dlg, IDC_KEYSIGN_INFOS, keymsg ); |
250 |
|
|
#ifndef LANG_DE |
251 |
twoaday |
22 |
SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)")); |
252 |
twoaday |
2 |
SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on")); |
253 |
|
|
SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably")); |
254 |
|
|
#endif |
255 |
twoaday |
22 |
CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED); |
256 |
twoaday |
2 |
CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED); |
257 |
twoaday |
22 |
CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED); |
258 |
twoaday |
2 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE); |
259 |
|
|
if (reg_prefs.expert == 0) |
260 |
|
|
ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE); |
261 |
|
|
SetForegroundWindow( dlg ); |
262 |
|
|
h = GetDlgItem( dlg, IDC_KEYSIGN_PASSPHRASE ); |
263 |
|
|
SetFocus( h ); |
264 |
|
|
return FALSE; |
265 |
twoaday |
22 |
|
266 |
|
|
case WM_DESTROY: |
267 |
|
|
sig_class_choice = 0; |
268 |
|
|
break; |
269 |
|
|
|
270 |
twoaday |
2 |
case WM_SYSCOMMAND: |
271 |
|
|
if( LOWORD( wparam ) == SC_CLOSE ) { |
272 |
|
|
SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" ); |
273 |
|
|
EndDialog( dlg, TRUE ); |
274 |
|
|
} |
275 |
|
|
return FALSE; |
276 |
|
|
|
277 |
|
|
case WM_COMMAND: |
278 |
|
|
if( HIWORD( wparam ) == CBN_SELCHANGE ) { |
279 |
|
|
do_check_protection( dlg ); |
280 |
|
|
break; |
281 |
|
|
} |
282 |
|
|
if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) { |
283 |
twoaday |
22 |
int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG); |
284 |
twoaday |
2 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
switch( LOWORD( wparam ) ) { |
288 |
|
|
case IDOK: |
289 |
twoaday |
22 |
if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL)) |
290 |
|
|
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg, |
291 |
|
|
sig_class_dlg_proc, (LPARAM)NULL, |
292 |
|
|
_("Choose Signature Class"), IDS_WINPT_SIGCLASS); |
293 |
twoaday |
2 |
type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL); |
294 |
|
|
if (type) |
295 |
|
|
type = GPGME_EDITKEY_LSIGN; |
296 |
|
|
else |
297 |
|
|
type = GPGME_EDITKEY_SIGN; |
298 |
|
|
|
299 |
twoaday |
22 |
if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) { |
300 |
twoaday |
2 |
type = GPGME_EDITKEY_NRSIGN; |
301 |
|
|
if (type == GPGME_EDITKEY_LSIGN) |
302 |
|
|
type = GPGME_EDITKEY_NRLSIGN; |
303 |
|
|
} |
304 |
|
|
if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) { |
305 |
|
|
expires = 1; |
306 |
|
|
DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st); |
307 |
|
|
sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); |
308 |
|
|
} |
309 |
|
|
|
310 |
twoaday |
22 |
GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1); |
311 |
|
|
keyid = gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_KEYID, NULL, 0 ); |
312 |
twoaday |
2 |
if( !keyid ) { |
313 |
|
|
msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR ); |
314 |
|
|
return FALSE; |
315 |
|
|
} |
316 |
|
|
err = gpgme_new( &ctx ); |
317 |
|
|
if (err) |
318 |
|
|
BUG (0); |
319 |
|
|
do_add_local_user (ctx, dlg); |
320 |
|
|
|
321 |
|
|
err = gpgme_editkey_new (&ke); |
322 |
|
|
if (err) |
323 |
|
|
BUG (0); |
324 |
|
|
err = gpgme_editkey_sign_set (ke, pwd, sig_class_choice, type, |
325 |
|
|
expires? keymsg : NULL); |
326 |
|
|
if (err) { |
327 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR); |
328 |
|
|
gpgme_release (ctx); |
329 |
|
|
gpgme_editkey_release (ke); |
330 |
|
|
return FALSE; |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
gpgme_set_edit_ctx (ctx, ke, type); |
334 |
|
|
|
335 |
|
|
err = gpgme_op_editkey( ctx, keyid ); |
336 |
|
|
memset( &pwd, 0, sizeof pwd ); |
337 |
|
|
if (err == GPGME_Conflict) |
338 |
|
|
Sleep (500); /* it seems the process is not really finished */ |
339 |
|
|
gpgme_release( ctx ); |
340 |
|
|
gpgme_editkey_release( ke ); |
341 |
|
|
if( err ) { |
342 |
|
|
if( err == GPGME_Conflict ) |
343 |
|
|
msg_box( dlg, _("Key is already signed by your key."), _("Key Signing"), MB_INFO ); |
344 |
|
|
else if( err == GPGME_Invalid_Mode ) |
345 |
|
|
msg_box( dlg, _("Unusable secret key."), _("Key Signing"), MB_ERR ); |
346 |
|
|
else |
347 |
|
|
msg_box( dlg, gpgme_strerror( err ), _("Key Signing"), MB_ERR ); |
348 |
|
|
} |
349 |
|
|
else { |
350 |
|
|
status_box (dlg, _("Key successfully signed."), PGM_NAME); |
351 |
twoaday |
22 |
key->update = 1; |
352 |
twoaday |
2 |
EndDialog (dlg, TRUE); |
353 |
|
|
} |
354 |
|
|
return TRUE; |
355 |
|
|
|
356 |
|
|
case IDCANCEL: |
357 |
|
|
EndDialog( dlg, FALSE ); |
358 |
|
|
return FALSE; |
359 |
|
|
} |
360 |
|
|
break; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
return FALSE; |
364 |
|
|
} /* keysign_dlg_proc */ |