1 |
/* wptKeysigDlg.cpp - Key signature listing |
2 |
* Copyright (C) 2001-2006 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 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <commctrl.h> |
26 |
#include <time.h> |
27 |
|
28 |
#include "resource.h" |
29 |
#include "wptGPG.h" |
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptContext.h" /* for passphrase_s */ |
32 |
#include "wptDlgs.h" |
33 |
#include "wptW32API.h" |
34 |
#include "wptNLS.h" |
35 |
#include "wptKeylist.h" |
36 |
#include "wptKeyserver.h" |
37 |
#include "wptUTF8.h" |
38 |
#include "wptTypes.h" |
39 |
#include "wptVersion.h" |
40 |
#include "wptErrors.h" |
41 |
#include "wptKeyEdit.h" |
42 |
|
43 |
static subclass_s siglist_proc; |
44 |
|
45 |
|
46 |
static int |
47 |
is_sig (listview_ctrl_t lv, int pos) |
48 |
{ |
49 |
char tmpbuf[200]; |
50 |
|
51 |
if (pos == -1) |
52 |
pos = listview_get_curr_pos (lv); |
53 |
if (pos == -1) |
54 |
return 0; |
55 |
listview_get_item_text (lv, pos, SL_COL_UID, tmpbuf, sizeof (tmpbuf)-1); |
56 |
if (*tmpbuf == ' ') |
57 |
return -1; |
58 |
return 0; |
59 |
} |
60 |
|
61 |
|
62 |
/* Delete the selected signature from list view @lv. The |
63 |
key is given in @key. */ |
64 |
static int |
65 |
do_delsig (HWND dlg, listview_ctrl_t lv, winpt_key_t key) |
66 |
{ |
67 |
gpgme_error_t err; |
68 |
GpgKeyEdit *ke; |
69 |
char keyid[32]; |
70 |
int pos, npos, id, is_selfsig=0; |
71 |
int signo=0, uidno=0; |
72 |
|
73 |
npos = pos = listview_get_curr_pos (lv); |
74 |
if (!is_sig (lv, -1)) |
75 |
return -1; |
76 |
listview_get_item_text (lv, pos, SL_COL_KEYID, keyid, sizeof (keyid)-1); |
77 |
if (!strncmp (key->keyid, keyid+2, 8)) |
78 |
is_selfsig = 1; |
79 |
|
80 |
while (pos > 0 && is_sig (lv, pos)) { |
81 |
signo++; |
82 |
pos--; |
83 |
} |
84 |
pos = npos; |
85 |
while (npos > 0) { |
86 |
if (!is_sig (lv, npos)) |
87 |
uidno++; |
88 |
npos--; |
89 |
} |
90 |
uidno++; |
91 |
|
92 |
/* XXX: do not allow to delete the self signature */ |
93 |
id = log_box (_("Key Manager"), is_selfsig? MB_WARN_ASK : MB_QUEST_ASK, |
94 |
_("Are you really sure you want to delete this %s from\n\n" |
95 |
" \"%s\""), is_selfsig? _("self signature") : _("signature"), |
96 |
key->uid); |
97 |
if (id == IDNO) |
98 |
return 0; |
99 |
|
100 |
ke = new GpgKeyEdit (key->keyid); |
101 |
if (!ke) |
102 |
BUG (NULL); |
103 |
err = ke->delUseridSignature (uidno, signo); |
104 |
if (err) |
105 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
106 |
else |
107 |
listview_del_item (lv, pos); |
108 |
delete ke; |
109 |
if (!err) |
110 |
key->update = 1; |
111 |
return err? -1 : 0; |
112 |
} |
113 |
|
114 |
|
115 |
/* Dialog box procedure to display the signature properties. */ |
116 |
BOOL CALLBACK |
117 |
sigprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
118 |
{ |
119 |
static gpgme_key_sig_t ks; |
120 |
gpgme_key_t issuer; |
121 |
char tmpbuf[256]; |
122 |
struct { |
123 |
unsigned int exportable:1; |
124 |
unsigned int expired:1; |
125 |
unsigned int nrev:1; |
126 |
unsigned int rev:1; |
127 |
const char *alg; |
128 |
int _class; |
129 |
} ctx; |
130 |
const char *fmt_templ = _("%s %s signature"); |
131 |
|
132 |
switch (msg) { |
133 |
case WM_SYSCOMMAND: |
134 |
if (LOWORD (wparam) == SC_CLOSE) |
135 |
EndDialog (dlg, TRUE); |
136 |
return FALSE; |
137 |
|
138 |
case WM_INITDIALOG: |
139 |
SetWindowText (dlg, _("Signature Properties")); |
140 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable")); |
141 |
SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably")); |
142 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired")); |
143 |
SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked")); |
144 |
SetDlgItemText (dlg, IDC_SIGPROPS_CLASSINF, _("Class")); |
145 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXPSTR, _("Expire date")); |
146 |
SetDlgItemText (dlg, IDC_SIGPROPS_KEYINF, _("Issuer key")); |
147 |
SetDlgItemText (dlg, IDC_SIGPROPS_KEYIDINF, _("Issuer key ID")); |
148 |
ks = (gpgme_key_sig_t)lparam; |
149 |
if (!ks) |
150 |
BUG (0); |
151 |
memset (&ctx, 0, sizeof ctx); |
152 |
ctx._class = ks->sig_class; |
153 |
if (ctx._class == 0) |
154 |
ctx._class = 16; |
155 |
ctx._class -= 6; |
156 |
if (ks->pubkey_algo == GPGME_PK_DSA) |
157 |
ctx.alg = "DSA"; |
158 |
else if (ks->pubkey_algo == GPGME_PK_RSA) |
159 |
ctx.alg = "RSA"; |
160 |
else |
161 |
ctx.alg = "ELG"; |
162 |
ctx.exportable = ks->exportable; |
163 |
_snprintf (tmpbuf, DIM (tmpbuf)-1, fmt_templ, |
164 |
ctx.exportable? _("Exportable") : _("Non-exportable"), ctx.alg); |
165 |
SetDlgItemText (dlg, IDC_SIGPROPS_INFO, tmpbuf); |
166 |
|
167 |
SetDlgItemText (dlg, IDC_SIGPROPS_KEYID, ks->keyid+8); |
168 |
SetDlgItemInt (dlg, IDC_SIGPROPS_CLASS, ctx._class, FALSE); |
169 |
if (ctx.exportable) |
170 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXP, BST_CHECKED); |
171 |
if (!get_pubkey (ks->keyid+8, &issuer)) { |
172 |
char *uid = utf8_to_wincp2 (issuer->uids->uid); |
173 |
SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, uid); |
174 |
safe_free (uid); |
175 |
} |
176 |
else |
177 |
SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, _(" user ID not found")); |
178 |
if (ks->expires == 0) { |
179 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPSTR), SW_HIDE); |
180 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), SW_HIDE); |
181 |
} |
182 |
else { |
183 |
SYSTEMTIME st; |
184 |
struct tm *tm; |
185 |
time_t t = time (NULL); |
186 |
time_t tmp = ks->expires; |
187 |
|
188 |
tm = localtime (&tmp); |
189 |
memset (&st, 0, sizeof st); |
190 |
st.wYear = tm->tm_year+1900; |
191 |
st.wMonth = tm->tm_mon; |
192 |
st.wDay = tm->tm_mday; |
193 |
DateTime_SetSystemtime (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), |
194 |
GDT_VALID, &st); |
195 |
|
196 |
tm = localtime (&t); |
197 |
tm->tm_mon++; |
198 |
tm->tm_year += 1900; |
199 |
if (tm->tm_year > st.wYear) |
200 |
ctx.expired = 1; |
201 |
else if (tm->tm_mon > st.wMonth) |
202 |
ctx.expired = 1; |
203 |
if (ctx.expired) |
204 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED, BST_CHECKED); |
205 |
} |
206 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable")); |
207 |
SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably")); |
208 |
SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked")); |
209 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired")); |
210 |
SetWindowText (dlg, _("Signature Properties")); |
211 |
SetForegroundWindow (dlg); |
212 |
center_window (dlg, NULL); |
213 |
return TRUE; |
214 |
|
215 |
case WM_COMMAND: |
216 |
switch (LOWORD (wparam)) { |
217 |
case IDOK: |
218 |
EndDialog (dlg, TRUE); |
219 |
break; |
220 |
|
221 |
case IDCANCEL: |
222 |
EndDialog (dlg, FALSE); |
223 |
break; |
224 |
} |
225 |
} |
226 |
|
227 |
return FALSE; |
228 |
} |
229 |
|
230 |
|
231 |
static BOOL CALLBACK |
232 |
subclass_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
233 |
{ |
234 |
listview_ctrl_t lv; |
235 |
winpt_key_t key; |
236 |
gpgme_key_sig_t ks; |
237 |
|
238 |
switch (msg) { |
239 |
case WM_KEYUP: |
240 |
int virt_key = (int)wparam; |
241 |
key = (winpt_key_t)siglist_proc.opaque; |
242 |
lv = key->callback.ctl; |
243 |
if (virt_key == VK_SPACE) { |
244 |
if (is_sig (lv, -1)) { |
245 |
ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv)); |
246 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
247 |
sigprops_dlg_proc, (LPARAM)ks); |
248 |
} |
249 |
} |
250 |
else if (virt_key == VK_DELETE) |
251 |
do_delsig (dlg, lv, key); |
252 |
break; |
253 |
} |
254 |
return CallWindowProc (siglist_proc.old, dlg, msg, wparam, lparam); |
255 |
} |
256 |
|
257 |
|
258 |
/* Return 1 if the list view @lv contains non-available keys. */ |
259 |
static int |
260 |
check_for_missing_keys (listview_ctrl_t lv) |
261 |
{ |
262 |
int i, n; |
263 |
char id[128]; |
264 |
|
265 |
n = listview_count_items( lv, 0 ); |
266 |
for( i = 0; i < n; i++ ) { |
267 |
listview_get_item_text (lv, i, SL_COL_VALID, id, sizeof (id) - 1); |
268 |
if (!strncmp (id, "NOKEY", 5)) |
269 |
return 1; |
270 |
} |
271 |
|
272 |
return 0; |
273 |
} |
274 |
|
275 |
|
276 |
/* Receive all missing keys marked in the list @lv. */ |
277 |
static int |
278 |
recv_missing_keys (HWND dlg, listview_ctrl_t lv) |
279 |
{ |
280 |
char id[128], keyid[18+1]; |
281 |
int i, n, cnt=0; |
282 |
|
283 |
i = msg_box (dlg, _("Really receive all missing keys?"), |
284 |
_("Key Manager"), MB_YESNO|MB_INFO); |
285 |
if (i == IDNO) |
286 |
return 0; |
287 |
|
288 |
n = listview_count_items (lv, 0); |
289 |
for (i = 0; i < n; i++) { |
290 |
listview_get_item_text (lv, i, SL_COL_VALID, id, sizeof (id) - 1); |
291 |
if (!strncmp (id, "NOKEY", 5)) { |
292 |
listview_get_item_text (lv, i, SL_COL_KEYID, |
293 |
keyid, sizeof (keyid) -1); |
294 |
if (!hkp_recv_key (dlg, default_keyserver, |
295 |
default_keyserver_port, keyid, 0, 0)) { |
296 |
keycache_update (0, keyid); |
297 |
cnt++; |
298 |
} |
299 |
else |
300 |
break; |
301 |
} |
302 |
} |
303 |
return cnt; |
304 |
} |
305 |
|
306 |
|
307 |
/* Create a mini popup with available choices. */ |
308 |
static void |
309 |
do_create_popup (HWND dlg) |
310 |
{ |
311 |
HMENU hm, sm; |
312 |
POINT p; |
313 |
|
314 |
GetCursorPos (&p); |
315 |
hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYSIG_CTX)); |
316 |
sm = GetSubMenu (hm, 0); |
317 |
set_menu_text (sm, ID_SIGCTX_PROPS, _("Signature &Properties")); |
318 |
set_menu_text (sm, ID_SIGCTX_KEYPROPS, _("Signing &Key Properties")); |
319 |
TrackPopupMenu (sm, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL); |
320 |
DestroyMenu (hm); |
321 |
DestroyMenu (sm); |
322 |
} |
323 |
|
324 |
|
325 |
/* Fetch a single key and extract the keyid from the listview |
326 |
entry at the pos @idx. */ |
327 |
static int |
328 |
recv_single_key (HWND dlg, listview_ctrl_t lv, int idx) |
329 |
{ |
330 |
char keyid[32]; |
331 |
int rc; |
332 |
|
333 |
listview_get_item_text (lv, idx, SL_COL_KEYID, keyid, DIM (keyid)-1); |
334 |
rc = hkp_recv_key (dlg, default_keyserver, |
335 |
default_keyserver_port, keyid, 0, 0); |
336 |
|
337 |
if (!rc) |
338 |
keycache_update (0, keyid); |
339 |
return rc; |
340 |
} |
341 |
|
342 |
|
343 |
/* Load the key property dialog with the selected key from @lv. */ |
344 |
static void |
345 |
do_load_keyprops (HWND dlg, listview_ctrl_t lv) |
346 |
{ |
347 |
winpt_key_s k; |
348 |
gpgme_key_t key; |
349 |
char keyid[32] = {0}; |
350 |
char status[64] = {0}, creation[64] = {0}; |
351 |
int n = listview_get_curr_pos (lv); |
352 |
|
353 |
listview_get_item_text (lv, n, SL_COL_VALID, status, DIM (status)-1); |
354 |
listview_get_item_text (lv, n, SL_COL_CREATE, creation, DIM (creation)-1); |
355 |
listview_get_item_text (lv, n, SL_COL_KEYID, keyid, DIM (keyid)-1); |
356 |
if (!strcmp (status, "NOKEY")) { |
357 |
int id = msg_box (dlg, _("Key not found in keyring, do you want to fetch it from the keyserver?"), |
358 |
_("Key Manager"), MB_QUEST_ASK); |
359 |
if (id == IDNO) |
360 |
return; |
361 |
if (recv_single_key (dlg, lv, n)) |
362 |
return; |
363 |
} |
364 |
|
365 |
if ((strlen (keyid) < 3 ||get_pubkey (keyid, &key))) { |
366 |
if (strlen (creation) > 0) |
367 |
msg_box (dlg, _("Key not found in keyring."), _("Key Manager"), MB_INFO); |
368 |
return; |
369 |
} |
370 |
memset (&k, 0, sizeof k); |
371 |
k.keyid = keyid; |
372 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYPROPS, dlg, |
373 |
keyprops_dlg_proc, (LPARAM)&k); |
374 |
} |
375 |
|
376 |
|
377 |
/* Dialog box procedure to list signatures. */ |
378 |
BOOL CALLBACK |
379 |
keysig_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
380 |
{ |
381 |
static listview_ctrl_t lv = NULL; |
382 |
static struct winpt_key_s *k; |
383 |
gpgme_key_sig_t ks; |
384 |
HWND sl; |
385 |
char inf[384]; |
386 |
int idx = 0; |
387 |
|
388 |
switch (msg) { |
389 |
case WM_INITDIALOG: |
390 |
k = (winpt_key_t) lparam; |
391 |
if (!k) |
392 |
BUG (0); |
393 |
if (k->uid) |
394 |
_snprintf (inf, DIM (inf)-1, _("Signature List for \"%s\""), k->uid); |
395 |
SetWindowText (dlg, inf); |
396 |
SetDlgItemText (dlg, IDC_KEYSIG_RECVKEY, _("&Receive Key")); |
397 |
SetDlgItemText (dlg, IDC_KEYSIG_SIGPROPS, _("&Properties")); |
398 |
|
399 |
lv = siglist_load (GetDlgItem (dlg, IDC_KEYSIG_LIST), k->keyid); |
400 |
if (!check_for_missing_keys (lv)) |
401 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), FALSE); |
402 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_SIGPROPS), FALSE); |
403 |
k->callback.ctl = lv; |
404 |
sl = GetDlgItem (dlg, IDC_KEYSIG_LIST); |
405 |
siglist_proc.dlg = dlg; |
406 |
siglist_proc.opaque = k; |
407 |
siglist_proc.current = (WNDPROC)subclass_dlg_proc; |
408 |
siglist_proc.old = (WNDPROC)GetWindowLong (sl, GWL_WNDPROC); |
409 |
if (siglist_proc.old) { |
410 |
if (!SetWindowLong (sl, GWL_WNDPROC, (LONG)siglist_proc.current)) { |
411 |
msg_box (dlg, "Could not set keylist window procedure.", |
412 |
_("Key Manager"), MB_ERR); |
413 |
BUG (0); |
414 |
} |
415 |
} |
416 |
SetForegroundWindow (dlg); |
417 |
center_window (dlg, NULL); |
418 |
return TRUE; |
419 |
|
420 |
case WM_DESTROY: |
421 |
if (lv) { |
422 |
siglist_delete (lv); |
423 |
lv = NULL; |
424 |
} |
425 |
return FALSE; |
426 |
|
427 |
case WM_SYSCOMMAND: |
428 |
if (LOWORD (wparam) == SC_CLOSE) |
429 |
EndDialog (dlg, TRUE); |
430 |
return FALSE; |
431 |
|
432 |
case WM_NOTIFY: |
433 |
NMHDR *notify; |
434 |
|
435 |
notify = (NMHDR *)lparam; |
436 |
if (!notify) |
437 |
return FALSE; |
438 |
if (notify->code == NM_DBLCLK |
439 |
&& notify->idFrom == IDC_KEYSIG_LIST) |
440 |
do_load_keyprops (dlg, lv); |
441 |
if (notify->code == NM_RCLICK && |
442 |
notify->idFrom == IDC_KEYSIG_LIST && |
443 |
is_sig (lv, -1)) |
444 |
do_create_popup (dlg); |
445 |
if (notify->code == LVN_ITEMCHANGED && |
446 |
((LPNMLISTVIEW)lparam)->uNewState) { |
447 |
idx = listview_get_curr_pos (lv); |
448 |
listview_get_item_text (lv, idx, SL_COL_VALID, inf, DIM (inf)-1); |
449 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), |
450 |
strcmp (inf, "NOKEY") == 0? TRUE: FALSE); |
451 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_SIGPROPS), |
452 |
is_sig (lv, -1)? TRUE : FALSE); |
453 |
} |
454 |
if (notify->code == LVN_COLUMNCLICK) { |
455 |
NMLISTVIEW *nft = (LPNMLISTVIEW) lparam; |
456 |
int sortby = 0; |
457 |
switch (nft->iSubItem) { |
458 |
//case 0: sortby = KEY_SORT_USERID; break; |
459 |
case 1: sortby = KEY_SORT_VALIDITY; break; |
460 |
case 2: sortby = SIG_SORT_CLASS; break; |
461 |
case 3: sortby = KEY_SORT_CREATED; break; |
462 |
case 4: sortby = KEY_SORT_KEYID; break; |
463 |
case 5: sortby = SIG_SORT_EXPIRE; break; |
464 |
case 6: sortby = KEY_SORT_ALGO; break; |
465 |
default: return TRUE; |
466 |
} |
467 |
siglist_sort (lv, sortby); |
468 |
} |
469 |
break; |
470 |
|
471 |
case WM_COMMAND: |
472 |
switch (LOWORD (wparam)) { |
473 |
case ID_SIGCTX_KEYPROPS: |
474 |
do_load_keyprops (dlg, lv); |
475 |
break; |
476 |
|
477 |
case ID_SIGCTX_PROPS: |
478 |
if (is_sig (lv, -1)) { |
479 |
ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv)); |
480 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
481 |
sigprops_dlg_proc, (LPARAM)ks); |
482 |
} |
483 |
break; |
484 |
|
485 |
case IDC_KEYSIG_RECVKEY: |
486 |
idx = listview_get_curr_pos (lv); |
487 |
if (idx == -1) |
488 |
recv_missing_keys (dlg, lv); |
489 |
else |
490 |
recv_single_key (dlg, lv, idx); |
491 |
return TRUE; |
492 |
|
493 |
case IDC_KEYSIG_SIGPROPS: |
494 |
if (is_sig (lv, -1)) { |
495 |
ks = ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv)); |
496 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
497 |
sigprops_dlg_proc, (LPARAM)ks); |
498 |
} |
499 |
return TRUE; |
500 |
|
501 |
case IDOK: |
502 |
EndDialog (dlg, TRUE); |
503 |
return TRUE; |
504 |
} |
505 |
break; |
506 |
} |
507 |
return FALSE; |
508 |
} |