1 |
/* wptKeysigDlg.cpp - Key signature listing |
2 |
* Copyright (C) 2001-2005 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 |
#include <time.h> |
24 |
|
25 |
#include "../resource.h" |
26 |
#include "wptGPG.h" |
27 |
#include "wptCommonCtl.h" |
28 |
#include "wptContext.h" /* for passphrase_s */ |
29 |
#include "wptDlgs.h" |
30 |
#include "wptW32API.h" |
31 |
#include "wptNLS.h" |
32 |
#include "wptKeyList.h" |
33 |
#include "wptKeyserver.h" |
34 |
#include "wptUTF8.h" |
35 |
#include "wptTypes.h" |
36 |
#include "wptVersion.h" |
37 |
#include "wptErrors.h" |
38 |
|
39 |
static subclass_s siglist_proc; |
40 |
|
41 |
|
42 |
static int inline |
43 |
is_sig (listview_ctrl_t lv, int pos) |
44 |
{ |
45 |
char tmpbuf[256]; |
46 |
|
47 |
if (pos == -1) |
48 |
pos = listview_get_curr_pos (lv); |
49 |
listview_get_item_text (lv, pos, 0, tmpbuf, 200); |
50 |
if (*tmpbuf == ' ') |
51 |
return -1; |
52 |
return 0; |
53 |
} |
54 |
|
55 |
|
56 |
static int |
57 |
do_delsig (HWND dlg, listview_ctrl_t lv, winpt_key_t key) |
58 |
{ |
59 |
int pos, npos, id; |
60 |
int signo=0, uidno=0; |
61 |
gpgme_ctx_t ctx; |
62 |
gpgme_editkey_t ek; |
63 |
gpgme_error_t rc; |
64 |
|
65 |
npos = pos = listview_get_curr_pos (lv); |
66 |
if (!is_sig (lv, -1)) |
67 |
return -1; |
68 |
while (pos > 0 && is_sig (lv, pos)) |
69 |
{ |
70 |
signo++; |
71 |
pos--; |
72 |
} |
73 |
pos = npos; |
74 |
while (npos > 0) |
75 |
{ |
76 |
if (!is_sig (lv, npos)) |
77 |
uidno++; |
78 |
npos--; |
79 |
} |
80 |
uidno++; |
81 |
id = log_box(_("Key Manager"), MB_YESNO, |
82 |
_("Are you really sure you want to delete this signature from\n" |
83 |
" \"%s\""), key->uid); |
84 |
if (id == IDNO) |
85 |
return 0; |
86 |
rc = gpgme_new (&ctx); |
87 |
if (rc) |
88 |
BUG (0); |
89 |
rc = gpgme_editkey_new (&ek); |
90 |
if (rc) |
91 |
BUG (0); |
92 |
gpgme_editkey_delsig_set (ek, uidno, signo); |
93 |
gpgme_set_edit_ctx (ctx, ek, GPGME_EDITKEY_DELSIG); |
94 |
rc = gpgme_op_editkey (ctx, key->keyid); |
95 |
if (rc) |
96 |
msg_box (dlg, gpgme_strerror (rc), _("Key Manager"), MB_ERR); |
97 |
else |
98 |
listview_del_item (lv, pos); |
99 |
gpgme_release (ctx); |
100 |
gpgme_editkey_release (ek); |
101 |
return 0; |
102 |
} |
103 |
|
104 |
|
105 |
static BOOL CALLBACK |
106 |
sigprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
107 |
{ |
108 |
static listview_ctrl_t lv; |
109 |
char tmpbuf[256]; |
110 |
int n; |
111 |
struct { |
112 |
unsigned int exportable:1; |
113 |
unsigned int expired:1; |
114 |
unsigned int nrev:1; |
115 |
unsigned int rev:1; |
116 |
const char * alg; |
117 |
int _class; |
118 |
} ctx; |
119 |
const char * fmt_templ = "%s %s signature"; |
120 |
|
121 |
switch (msg) |
122 |
{ |
123 |
case WM_SYSCOMMAND: |
124 |
if (LOWORD (wparam) == SC_CLOSE) |
125 |
EndDialog (dlg, TRUE); |
126 |
return FALSE; |
127 |
|
128 |
case WM_INITDIALOG: |
129 |
lv = (listview_ctrl_t)lparam; |
130 |
if (!lv) |
131 |
dlg_fatal_error (dlg, "could not get dialog param"); |
132 |
memset (&ctx, 0, sizeof ctx); |
133 |
n = listview_get_curr_pos (lv); |
134 |
listview_get_item_text (lv, n, 2, tmpbuf, DIM (tmpbuf)-1); |
135 |
if (!strstr (tmpbuf, "L")) |
136 |
ctx.exportable = 1; |
137 |
ctx._class = atoi (tmpbuf); |
138 |
if (ctx._class == 0) |
139 |
ctx._class = 10; |
140 |
else if (ctx._class < 10) |
141 |
ctx._class += 10; |
142 |
listview_get_item_text (lv, n, 6, tmpbuf, DIM (tmpbuf)-1); |
143 |
if (strstr (tmpbuf, "DSA")) |
144 |
ctx.alg = "DSA"; |
145 |
else if (strstr (tmpbuf, "RSA")) |
146 |
ctx.alg = "RSA"; |
147 |
else |
148 |
ctx.alg = "ELG"; |
149 |
_snprintf (tmpbuf, DIM (tmpbuf)-1, fmt_templ, |
150 |
ctx.exportable? "Exportable" : "Non-exportable", ctx.alg); |
151 |
SetDlgItemText (dlg, IDC_SIGPROPS_INFO, tmpbuf); |
152 |
listview_get_item_text (lv, n, 4, tmpbuf, DIM (tmpbuf)-1); |
153 |
SetDlgItemText (dlg, IDC_SIGPROPS_KEYID, tmpbuf); |
154 |
SetDlgItemInt (dlg, IDC_SIGPROPS_CLASS, ctx._class, FALSE); |
155 |
if (ctx.exportable) |
156 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXP, BST_CHECKED); |
157 |
listview_get_item_text (lv, n, 0, tmpbuf, DIM (tmpbuf)-1); |
158 |
SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, tmpbuf+1); |
159 |
tmpbuf[0] = 0; |
160 |
listview_get_item_text (lv, n, 5, tmpbuf, DIM (tmpbuf)-1); |
161 |
if (strlen (tmpbuf) == 0) { |
162 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPSTR), SW_HIDE); |
163 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), SW_HIDE); |
164 |
} |
165 |
else { |
166 |
SYSTEMTIME st; |
167 |
struct tm * tm; |
168 |
time_t t = time (NULL); |
169 |
|
170 |
memset (&st, 0, sizeof st); |
171 |
st.wYear = atoi (tmpbuf); |
172 |
st.wMonth = atoi (tmpbuf+5); |
173 |
st.wDay = atoi (tmpbuf+8); |
174 |
DateTime_SetSystemtime (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), |
175 |
GDT_VALID, &st); |
176 |
|
177 |
tm = localtime (&t); |
178 |
tm->tm_mon++; |
179 |
tm->tm_year += 1900; |
180 |
if (tm->tm_year > st.wYear) |
181 |
ctx.expired = 1; |
182 |
else if (tm->tm_mon > st.wMonth) |
183 |
ctx.expired = 1; |
184 |
if (ctx.expired) |
185 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED, BST_CHECKED); |
186 |
} |
187 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable")); |
188 |
SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably")); |
189 |
SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked")); |
190 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired")); |
191 |
SetForegroundWindow (dlg); |
192 |
center_window (dlg); |
193 |
return TRUE; |
194 |
|
195 |
case WM_COMMAND: |
196 |
switch (LOWORD (wparam)) { |
197 |
case IDOK: |
198 |
EndDialog (dlg, TRUE); |
199 |
break; |
200 |
} |
201 |
} |
202 |
|
203 |
return FALSE; |
204 |
} |
205 |
|
206 |
|
207 |
static BOOL CALLBACK |
208 |
subclass_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
209 |
{ |
210 |
listview_ctrl_t lv; |
211 |
winpt_key_t key; |
212 |
|
213 |
switch (msg) |
214 |
{ |
215 |
case WM_KEYUP: |
216 |
int virt_key = (int)wparam; |
217 |
key = (winpt_key_t)siglist_proc.opaque; |
218 |
lv = key->callback.ctl; |
219 |
if (virt_key == VK_SPACE) |
220 |
{ |
221 |
if (is_sig (lv, -1)) |
222 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
223 |
sigprops_dlg_proc, (LPARAM)lv); |
224 |
} |
225 |
else if (virt_key == VK_DELETE) |
226 |
do_delsig (dlg, lv, key); |
227 |
break; |
228 |
} |
229 |
return CallWindowProc (siglist_proc.old, dlg, msg, wparam, lparam); |
230 |
} |
231 |
|
232 |
|
233 |
static int |
234 |
check_for_missing_keys (listview_ctrl_t lv) |
235 |
{ |
236 |
int i, n; |
237 |
char id[128]; |
238 |
|
239 |
n = listview_count_items( lv, 0 ); |
240 |
for( i = 0; i < n; i++ ) { |
241 |
listview_get_item_text( lv, i, 1, id, sizeof id - 1 ); |
242 |
if( !strncmp( id, "NOKEY", 5 ) ) |
243 |
return 1; |
244 |
} |
245 |
|
246 |
return 0; |
247 |
} /* check_for_missing_keys */ |
248 |
|
249 |
|
250 |
static int |
251 |
recv_missing_keys (HWND dlg, listview_ctrl_t lv) |
252 |
{ |
253 |
int i, n, rc; |
254 |
char id[128], keyid[18+1]; |
255 |
|
256 |
n = listview_count_items (lv, 0); |
257 |
for( i = 0; i < n; i++ ) { |
258 |
listview_get_item_text( lv, i, 1, id, sizeof id - 1 ); |
259 |
if( !strncmp( id, "NOKEY", 5 ) ) { |
260 |
listview_get_item_text( lv, i, 4, keyid, sizeof keyid -1 ); |
261 |
rc = hkp_recv_key( dlg, default_keyserver, default_keyserver_port, keyid, 0, 0 ); |
262 |
if( rc ) |
263 |
break; |
264 |
} |
265 |
} |
266 |
|
267 |
return rc; |
268 |
} /* recv_missing_keys */ |
269 |
|
270 |
|
271 |
static void |
272 |
do_create_popup (HWND dlg) |
273 |
{ |
274 |
HMENU hm, sm; |
275 |
POINT p; |
276 |
|
277 |
GetCursorPos (&p); |
278 |
hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYSIG_CTX)); |
279 |
sm = GetSubMenu (hm, 0); |
280 |
|
281 |
TrackPopupMenu (sm, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL); |
282 |
|
283 |
DestroyMenu (hm); |
284 |
DestroyMenu (sm); |
285 |
} |
286 |
|
287 |
|
288 |
static void |
289 |
do_load_keyprops (HWND dlg, listview_ctrl_t lv) |
290 |
{ |
291 |
winpt_key_s k; |
292 |
gpgme_key_t key; |
293 |
char keyid[32] = {0}; |
294 |
char status[64] = {0}, creation[64] = {0}; |
295 |
int n = listview_get_curr_pos (lv); |
296 |
|
297 |
listview_get_item_text (lv, n, 1, status, DIM (status)-1); |
298 |
listview_get_item_text (lv, n, 3, creation, DIM (creation)-1); |
299 |
listview_get_item_text (lv, n, 4, keyid, DIM (keyid)-1); |
300 |
if (!strcmp (status, "NOKEY")) { |
301 |
msg_box (dlg, _("Key not found in keyring, please get it from the keyserver first."), |
302 |
_("Key Manager"), MB_INFO); |
303 |
return; |
304 |
} |
305 |
|
306 |
if ((strlen (keyid) < 3 ||get_pubkey (keyid, &key))) { |
307 |
if (strlen (creation) > 0) |
308 |
msg_box (dlg, _("Key not found in keyring."), _("Key Manager"), MB_INFO); |
309 |
return; |
310 |
} |
311 |
memset (&k, 0, sizeof k); |
312 |
k.keyid = keyid; |
313 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYPROPS, dlg, |
314 |
keyprops_dlg_proc, (LPARAM)&k); |
315 |
} |
316 |
|
317 |
|
318 |
BOOL CALLBACK |
319 |
keysig_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
320 |
{ |
321 |
static listview_ctrl_t lv = NULL; |
322 |
struct winpt_key_s *k; |
323 |
char inf[384], keyid[18+1]; |
324 |
int idx = 0, id, rc; |
325 |
HWND sl; |
326 |
|
327 |
switch( msg ) { |
328 |
case WM_INITDIALOG: |
329 |
k = (winpt_key_t) lparam; |
330 |
if (!k) |
331 |
BUG (0); |
332 |
if( k->uid ) |
333 |
_snprintf( inf, sizeof inf - 1, _("Signature List for \"%s\""), k->uid ); |
334 |
SetWindowText( dlg, inf ); |
335 |
#ifndef LANG_DE |
336 |
SetDlgItemText( dlg, IDC_KEYSIG_RECVKEY, _("&Receive Key") ); |
337 |
SetDlgItemText (dlg, IDC_KEYSIG_SIGPROPS, _("&Properties")); |
338 |
#endif |
339 |
lv = siglist_load( GetDlgItem( dlg, IDC_KEYSIG_LIST ), k->keyid ); |
340 |
if( !check_for_missing_keys( lv ) ) |
341 |
EnableWindow( GetDlgItem( dlg, IDC_KEYSIG_RECVKEY ), FALSE ); |
342 |
k->callback.ctl = lv; |
343 |
sl = GetDlgItem (dlg, IDC_KEYSIG_LIST); |
344 |
siglist_proc.dlg = dlg; |
345 |
siglist_proc.opaque = k; |
346 |
siglist_proc.current = (WNDPROC)subclass_dlg_proc; |
347 |
siglist_proc.old = (WNDPROC)GetWindowLong (sl, GWL_WNDPROC); |
348 |
if (siglist_proc.old) |
349 |
{ |
350 |
if (!SetWindowLong (sl, GWL_WNDPROC, (LONG)siglist_proc.current)) |
351 |
{ |
352 |
msg_box (dlg, _("Could not set keylist window procedure."), _("Key Manager"), MB_ERR); |
353 |
BUG (0); |
354 |
} |
355 |
} |
356 |
SetForegroundWindow (dlg); |
357 |
center_window (dlg); |
358 |
return TRUE; |
359 |
|
360 |
case WM_DESTROY: |
361 |
if ( lv ) { |
362 |
siglist_delete( lv ); |
363 |
lv = NULL; |
364 |
} |
365 |
return FALSE; |
366 |
|
367 |
case WM_SYSCOMMAND: |
368 |
if( LOWORD ( wparam ) == SC_CLOSE ) |
369 |
EndDialog( dlg, TRUE ); |
370 |
return FALSE; |
371 |
|
372 |
case WM_NOTIFY: |
373 |
NMHDR * notify; |
374 |
|
375 |
notify = (NMHDR *)lparam; |
376 |
if( notify && notify->code == NM_DBLCLK |
377 |
&& notify->idFrom == IDC_KEYSIG_LIST ) |
378 |
do_load_keyprops (dlg, lv); |
379 |
if (notify && notify->code == NM_RCLICK && |
380 |
notify->idFrom == IDC_KEYSIG_LIST) |
381 |
do_create_popup (dlg); |
382 |
break; |
383 |
|
384 |
case WM_COMMAND: |
385 |
switch ( LOWORD( wparam ) ) { |
386 |
|
387 |
case ID_SIGCTX_KEYPROPS: |
388 |
do_load_keyprops (dlg, lv); |
389 |
break; |
390 |
|
391 |
case ID_SIGCTX_PROPS: |
392 |
if (is_sig (lv, -1)) |
393 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
394 |
sigprops_dlg_proc, (LPARAM)lv); |
395 |
break; |
396 |
|
397 |
case IDC_KEYSIG_RECVKEY: |
398 |
idx = listview_get_curr_pos( lv ); |
399 |
if( idx == -1 ) { |
400 |
id = msg_box( dlg, _("Really receive all missing keys?"), |
401 |
_("Key Manager"), MB_YESNO|MB_INFO ); |
402 |
if( id == IDYES ) { |
403 |
rc = recv_missing_keys( dlg, lv ); |
404 |
if( !rc ) |
405 |
keycache_set_reload( 1 ); |
406 |
return TRUE; |
407 |
} |
408 |
return FALSE; |
409 |
} |
410 |
listview_get_item_text (lv, idx, 4, keyid, sizeof keyid - 1); |
411 |
rc = hkp_recv_key (dlg, default_keyserver, default_keyserver_port, keyid, 0, 0); |
412 |
if (!rc) |
413 |
keycache_set_reload (1); |
414 |
return TRUE; |
415 |
|
416 |
case IDC_KEYSIG_SIGPROPS: |
417 |
if (is_sig (lv, -1)) |
418 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
419 |
sigprops_dlg_proc, (LPARAM)lv); |
420 |
return TRUE; |
421 |
|
422 |
case IDOK: |
423 |
EndDialog (dlg, TRUE); |
424 |
return TRUE; |
425 |
} |
426 |
break; |
427 |
} |
428 |
return FALSE; |
429 |
} /* keysig_dlg_proc */ |