1 |
/* wptKeylist.cpp - Keylist element |
2 |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* Copyright (C) 2004 Andreas Jobs |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <commctrl.h> |
28 |
#include <time.h> |
29 |
|
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptTypes.h" |
32 |
#include "wptGPG.h" |
33 |
#include "wptKeylist.h" |
34 |
#include "wptKeyManager.h" |
35 |
#include "wptW32API.h" |
36 |
#include "wptNLS.h" |
37 |
#include "wptErrors.h" |
38 |
#include "wptUTF8.h" |
39 |
#include "wptRegistry.h" |
40 |
#include "wptContext.h" |
41 |
#include "wptVersion.h" |
42 |
#include "resource.h" |
43 |
|
44 |
#define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled) |
45 |
|
46 |
struct key_array_s { |
47 |
char keyid[32]; |
48 |
int checked; |
49 |
}; |
50 |
|
51 |
static int find_secret_key (gpgme_key_t key); |
52 |
|
53 |
|
54 |
static key_array_s* |
55 |
key_array_new (int items) |
56 |
{ |
57 |
key_array_s *ka; |
58 |
int j; |
59 |
|
60 |
if (items == 0) |
61 |
return NULL; |
62 |
ka = new key_array_s[items + 1]; |
63 |
if (!ka) |
64 |
BUG (NULL); |
65 |
for (j = 0; j < items; j++) |
66 |
ka[j].checked = 0; |
67 |
return ka; |
68 |
} |
69 |
|
70 |
|
71 |
static void |
72 |
key_array_release (key_array_s *ka) |
73 |
{ |
74 |
free_if_alloc (ka); |
75 |
} |
76 |
|
77 |
|
78 |
/* Check if the keyid @keyid is in the key array @ka. |
79 |
Return value: 1 if it exists, 0 otherwise. */ |
80 |
static int |
81 |
key_array_search (key_array_s *ka, int items, const char *keyid) |
82 |
{ |
83 |
int j; |
84 |
|
85 |
for (j = 0; j < items; j++) { |
86 |
if (!strcmp (keyid, ka[j].keyid)) |
87 |
return 1; |
88 |
} |
89 |
return 0; |
90 |
} |
91 |
|
92 |
|
93 |
gpgme_user_id_t |
94 |
get_nth_userid (gpgme_key_t key, int idx) |
95 |
{ |
96 |
gpgme_user_id_t t; |
97 |
|
98 |
if (!key->uids) |
99 |
return NULL; |
100 |
t = key->uids; |
101 |
while (idx-- && t->next) |
102 |
t = t->next; |
103 |
return t; |
104 |
} |
105 |
|
106 |
|
107 |
int |
108 |
count_userids (gpgme_key_t key) |
109 |
{ |
110 |
gpgme_user_id_t u; |
111 |
int n = 1; |
112 |
|
113 |
u = key->uids; |
114 |
if (!u) |
115 |
return 0; |
116 |
while (u->next) { |
117 |
u = u->next; |
118 |
n++; |
119 |
} |
120 |
return n; |
121 |
} |
122 |
|
123 |
|
124 |
gpgme_subkey_t |
125 |
get_nth_key (gpgme_key_t key, int idx) |
126 |
{ |
127 |
gpgme_subkey_t t; |
128 |
|
129 |
if (!key->subkeys) |
130 |
return NULL; |
131 |
t = key->subkeys; |
132 |
while (idx-- && t->next) |
133 |
t = t->next; |
134 |
return t; |
135 |
} |
136 |
|
137 |
|
138 |
int |
139 |
count_subkeys (gpgme_key_t key) |
140 |
{ |
141 |
gpgme_subkey_t k; |
142 |
int n = 1; |
143 |
|
144 |
k = key->subkeys; |
145 |
if (!k) |
146 |
return 0; |
147 |
while (k->next) { |
148 |
k = k->next; |
149 |
n++; |
150 |
} |
151 |
return n; |
152 |
} |
153 |
|
154 |
|
155 |
/* Return the self signature of the key @keyid. |
156 |
If first is set, the first self sig will be returned. */ |
157 |
gpgme_key_sig_t |
158 |
get_selfsig (gpgme_key_sig_t sigs, const char *keyid, int first) |
159 |
{ |
160 |
gpgme_key_sig_t s, self_sig=NULL; |
161 |
long timestamp=0; |
162 |
int off = 0; |
163 |
|
164 |
if (strlen (keyid) == 8) |
165 |
off = 8; |
166 |
|
167 |
for (s = sigs; s; s = s->next) { |
168 |
if (!strcmp (s->keyid+off, keyid) && s->timestamp > timestamp) { |
169 |
self_sig = s; |
170 |
timestamp = s->timestamp; |
171 |
if (first) |
172 |
break; |
173 |
} |
174 |
} |
175 |
return self_sig; |
176 |
} |
177 |
|
178 |
|
179 |
const char* |
180 |
get_key_algo (gpgme_key_t key, int keyidx) |
181 |
{ |
182 |
static char algo_id[128]; |
183 |
gpgme_subkey_t k; |
184 |
char alg[32]; |
185 |
const char *subalg; |
186 |
int n=0; |
187 |
|
188 |
if (keyidx > 0) { |
189 |
k = get_nth_key (key, keyidx-1); |
190 |
subalg = get_key_pubalgo (k->pubkey_algo); |
191 |
_snprintf (algo_id, DIM (algo_id)-1, "%s", subalg); |
192 |
return algo_id; |
193 |
} |
194 |
strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo)); |
195 |
n = count_subkeys (key); |
196 |
if (n > 1) { |
197 |
do { |
198 |
k = get_nth_key (key, --n); |
199 |
if (k->revoked || k->expired) |
200 |
continue; |
201 |
else |
202 |
break; |
203 |
} while (n > 0); |
204 |
subalg = get_key_pubalgo (k->pubkey_algo); |
205 |
if (k == key->subkeys) |
206 |
_snprintf (algo_id, DIM (algo_id)-1, "%s", subalg); |
207 |
else |
208 |
_snprintf (algo_id, DIM (algo_id)-1, "%s/%s", alg, subalg); |
209 |
return algo_id; |
210 |
} |
211 |
return get_key_pubalgo (key->subkeys->pubkey_algo); |
212 |
} |
213 |
|
214 |
|
215 |
const char* |
216 |
get_key_created (long timestamp) |
217 |
{ |
218 |
static char timebuf[128]; |
219 |
struct tm *warp; |
220 |
const char *dat; |
221 |
|
222 |
if (timestamp < 1) |
223 |
return "????" "-??" "-??"; |
224 |
dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); |
225 |
if (dat) |
226 |
return dat; |
227 |
/* Fallback if locate date conversion failed. */ |
228 |
warp = localtime (×tamp); |
229 |
_snprintf (timebuf, sizeof timebuf - 1, "%04d-%02d-%02d", |
230 |
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); |
231 |
return timebuf; |
232 |
} |
233 |
|
234 |
|
235 |
/* Return a string presentation of the time @timestamp. */ |
236 |
const char* |
237 |
get_key_expire_date (long timestamp) |
238 |
{ |
239 |
static char timebuf[64]; |
240 |
struct tm *warp; |
241 |
const char *dat; |
242 |
|
243 |
if (timestamp == 0) |
244 |
return _("Never"); |
245 |
dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); |
246 |
if (dat) |
247 |
return dat; |
248 |
warp = localtime (×tamp); |
249 |
_snprintf (timebuf, sizeof timebuf -1, "%04d-%02d-%02d", |
250 |
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); |
251 |
return timebuf; |
252 |
} |
253 |
|
254 |
|
255 |
const char* |
256 |
get_key_type (gpgme_key_t key) |
257 |
{ |
258 |
int type = find_secret_key (key); |
259 |
|
260 |
if (type == 1) |
261 |
return _("Key Pair"); |
262 |
else if (type == 2) |
263 |
return _("Key Pair (Card)"); |
264 |
return _("Public Key"); |
265 |
} |
266 |
|
267 |
|
268 |
const char* |
269 |
get_key_size (gpgme_key_t key, int keyidx) |
270 |
{ |
271 |
static char size_id[64]; |
272 |
gpgme_subkey_t k; |
273 |
int n, size_main, size_sub; |
274 |
|
275 |
if (keyidx > 0) { |
276 |
k = get_nth_key (key, keyidx-1); |
277 |
size_main = k->length; |
278 |
_snprintf (size_id, DIM (size_id)-1, "%d", size_main); |
279 |
return size_id; |
280 |
} |
281 |
size_main = key->subkeys->length; |
282 |
n = count_subkeys (key); |
283 |
if (n > 1) { |
284 |
k = get_nth_key (key, n-1); |
285 |
size_sub = k->length; |
286 |
_snprintf (size_id, sizeof (size_id) - 1, "%d/%d", |
287 |
size_main, size_sub); |
288 |
return size_id; |
289 |
} |
290 |
_snprintf( size_id, sizeof (size_id) - 1, "%d", size_main ); |
291 |
return size_id; |
292 |
} |
293 |
|
294 |
|
295 |
const char* |
296 |
get_key_pubalgo2 (gpgme_pubkey_algo_t alg) |
297 |
{ |
298 |
switch (alg) { |
299 |
case GPGME_PK_DSA: return "D"; |
300 |
case GPGME_PK_RSA: return "R"; |
301 |
case GPGME_PK_ELG: return "G"; |
302 |
default: return "?"; |
303 |
} |
304 |
return "?"; |
305 |
} |
306 |
|
307 |
|
308 |
const char* |
309 |
get_key_pubalgo (gpgme_pubkey_algo_t alg) |
310 |
{ |
311 |
switch (alg) { |
312 |
case GPGME_PK_DSA: return "DSA"; |
313 |
case GPGME_PK_ELG: |
314 |
case GPGME_PK_ELG_E: return "ELG"; |
315 |
case 0: /* XXX: do we still need this?? */ |
316 |
case GPGME_PK_RSA: |
317 |
case GPGME_PK_RSA_S: |
318 |
case GPGME_PK_RSA_E: return "RSA"; |
319 |
default: return "???"; |
320 |
} |
321 |
return "???"; |
322 |
} |
323 |
|
324 |
const char* |
325 |
get_key_fpr (gpgme_key_t key) |
326 |
{ |
327 |
static char fpr_md[64]; |
328 |
const char *fpr; |
329 |
char t[16], tmp[40]; |
330 |
size_t i=0; |
331 |
|
332 |
memset (fpr_md, 0, sizeof (fpr_md)); |
333 |
fpr = key->subkeys->fpr; |
334 |
if (!fpr || !*fpr) { |
335 |
memset (tmp, '0', 40); |
336 |
fpr = tmp; |
337 |
} |
338 |
if (strlen (fpr) == 32) { |
339 |
strcat (fpr_md, " "); |
340 |
for (i=0; i < strlen (fpr)/2; i++) { |
341 |
sprintf (t, "%c%c ", fpr[2*i], fpr[2*i+1]); |
342 |
strcat (fpr_md, t); |
343 |
} |
344 |
} |
345 |
else { |
346 |
strcat (fpr_md, " "); |
347 |
for (i = 0; i < strlen (fpr) / 4; i++) { |
348 |
sprintf (t, "%c%c%c%c ", fpr[4*i], fpr[4*i+1], fpr[4*i+2], fpr[4*i+3]); |
349 |
strcat (fpr_md, t); |
350 |
} |
351 |
} |
352 |
return fpr_md; |
353 |
} |
354 |
|
355 |
|
356 |
/* Extract the key ID from the fingerprint. |
357 |
A long ID will be converted into a short ID. */ |
358 |
const char* |
359 |
get_keyid_from_fpr (const char *fpr) |
360 |
{ |
361 |
if (!fpr) |
362 |
return "????????"; |
363 |
if (strlen (fpr) == 40) |
364 |
fpr += 32; |
365 |
else if (strlen (fpr) == 32) |
366 |
fpr += 24; |
367 |
else if (strlen (fpr) == 16) |
368 |
fpr += 8; |
369 |
else |
370 |
return "????????"; |
371 |
return fpr; |
372 |
} |
373 |
|
374 |
|
375 |
const char* |
376 |
get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode) |
377 |
{ |
378 |
if (key) |
379 |
val = key->owner_trust; /* uididx?? */ |
380 |
switch (val) { |
381 |
case GPGME_VALIDITY_UNKNOWN: |
382 |
case GPGME_VALIDITY_UNDEFINED: |
383 |
return _("None"); |
384 |
case GPGME_VALIDITY_NEVER: |
385 |
return _("Never"); |
386 |
case GPGME_VALIDITY_MARGINAL: |
387 |
return _("Marginal"); |
388 |
case GPGME_VALIDITY_FULL: |
389 |
return _("Full"); |
390 |
case GPGME_VALIDITY_ULTIMATE: |
391 |
return _("Ultimate"); |
392 |
} |
393 |
return ""; |
394 |
} |
395 |
|
396 |
|
397 |
const char* |
398 |
get_key_trust (gpgme_key_t key, int uididx, int listmode) |
399 |
{ |
400 |
return get_key_trust2 (key, 0, uididx, listmode); |
401 |
} |
402 |
|
403 |
|
404 |
const char* |
405 |
get_key_trust_str (int val) |
406 |
{ |
407 |
return get_key_trust2 (NULL, val, 0, 0); |
408 |
} |
409 |
|
410 |
|
411 |
/* Return the status of the key @key. */ |
412 |
char* |
413 |
get_key_status (gpgme_key_t key, int uididx, int listmode) |
414 |
{ |
415 |
gpgme_user_id_t u; |
416 |
const char *attr; |
417 |
u32 key_attr =0; |
418 |
|
419 |
if (uididx < 0 || count_userids (key) > uididx) |
420 |
uididx = 0; |
421 |
if (listmode) { |
422 |
const char *s; |
423 |
if (key->revoked) |
424 |
s = _("Revoked"); |
425 |
else if (key->expired) |
426 |
s = _("Expired"); |
427 |
else if (key->disabled) |
428 |
s = _("Disabled"); |
429 |
else |
430 |
s = ""; |
431 |
/* if the key has a special status, we don't continue to figure out |
432 |
the user-id validities. */ |
433 |
if (*s) |
434 |
return m_strdup (s); |
435 |
} |
436 |
u = get_nth_userid (key, uididx); |
437 |
key_attr = u->validity; |
438 |
attr = get_key_trust2 (NULL, key_attr, 0, 0); |
439 |
return m_strdup (attr); |
440 |
} |
441 |
|
442 |
|
443 |
/* Return human readable description of the key @key. */ |
444 |
char* |
445 |
get_key_desc (gpgme_key_t key) |
446 |
{ |
447 |
gpgme_key_t sk; |
448 |
const char *state, *alg, *type; |
449 |
char *p; |
450 |
|
451 |
/* XXX: problems with the German translation. */ |
452 |
state = ""; |
453 |
if (key->disabled) |
454 |
state = _("Disabled"); |
455 |
if (key->expired) |
456 |
state = _("Expired"); |
457 |
if (key->revoked) |
458 |
state = _("Revoked"); |
459 |
alg = "OpenPGP"; |
460 |
if (strlen (key->subkeys->fpr) == 32) |
461 |
alg = "RSA Legacy"; |
462 |
type = _("public key"); |
463 |
if (!get_seckey (key->subkeys->keyid+8, &sk)) |
464 |
type = _("key pair"); |
465 |
p = new char[strlen (state) + strlen (alg) + strlen (type) + 4 + 1]; |
466 |
if (!p) |
467 |
BUG (0); |
468 |
sprintf (p, "%s %s %s", state, alg, type); |
469 |
return p; |
470 |
} |
471 |
|
472 |
|
473 |
/* Integer comparsion of @a and @b. |
474 |
Return values: same as in strcmp. */ |
475 |
static inline int |
476 |
int_cmp (int a, int b) |
477 |
{ |
478 |
if (a == b) return 0; |
479 |
else if (a > b) return 1; |
480 |
else return -1; |
481 |
return 0; |
482 |
} |
483 |
|
484 |
|
485 |
/* To allow to sort the keys, we need to take care of |
486 |
the expired/revoke status also. */ |
487 |
static int |
488 |
get_ext_validity (gpgme_key_t k) |
489 |
{ |
490 |
if (k->revoked) |
491 |
return GPGME_VALIDITY_ULTIMATE+1; |
492 |
else if (k->expired) |
493 |
return GPGME_VALIDITY_ULTIMATE+2; |
494 |
else if (k->disabled) |
495 |
return GPGME_VALIDITY_ULTIMATE+3; |
496 |
return k->uids->validity; |
497 |
} |
498 |
|
499 |
|
500 |
/* List view sorting callback. */ |
501 |
static int CALLBACK |
502 |
keylist_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby) |
503 |
{ |
504 |
struct keycache_s *aa, *bb; |
505 |
gpgme_key_t a, b; |
506 |
int cmpresult = 0; |
507 |
|
508 |
aa = (struct keycache_s *)first; |
509 |
bb = (struct keycache_s *)second; |
510 |
if (!aa || !bb) |
511 |
BUG (NULL); |
512 |
a = aa->key; |
513 |
b = bb->key; |
514 |
|
515 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
516 |
case KEY_SORT_USERID: |
517 |
cmpresult = strcmpi (a->uids->uid, b->uids->uid); |
518 |
break; |
519 |
|
520 |
case KEY_SORT_KEYID: |
521 |
cmpresult = strcmpi (a->subkeys->keyid+8, |
522 |
b->subkeys->keyid+8); |
523 |
break; |
524 |
|
525 |
case KEY_SORT_VALIDITY: |
526 |
cmpresult = int_cmp (get_ext_validity (a), |
527 |
get_ext_validity (b)); |
528 |
break; |
529 |
|
530 |
case KEY_SORT_OTRUST: |
531 |
cmpresult = int_cmp (a->owner_trust, b->owner_trust); |
532 |
break; |
533 |
|
534 |
case KEY_SORT_IS_SECRET: |
535 |
get_seckey (a->subkeys->keyid, &a); |
536 |
get_seckey (b->subkeys->keyid, &b); |
537 |
cmpresult = int_cmp (a? a->secret : 0, b? b->secret : 0); |
538 |
break; |
539 |
|
540 |
case KEY_SORT_LEN: |
541 |
cmpresult = int_cmp (a->subkeys->length, |
542 |
b->subkeys->length); |
543 |
break; |
544 |
|
545 |
case KEY_SORT_CREATED: |
546 |
cmpresult = int_cmp (a->subkeys->timestamp, |
547 |
b->subkeys->timestamp); |
548 |
break; |
549 |
|
550 |
case KEY_SORT_ALGO: |
551 |
cmpresult = int_cmp (a->subkeys->pubkey_algo, |
552 |
b->subkeys->pubkey_algo); |
553 |
break; |
554 |
|
555 |
default: |
556 |
cmpresult = strcmpi (a->uids->uid, b->uids->uid); |
557 |
break; |
558 |
} |
559 |
if (sortby & KEYLIST_SORT_DESC) |
560 |
return (~cmpresult + 1); |
561 |
else |
562 |
return cmpresult; |
563 |
} |
564 |
|
565 |
|
566 |
int |
567 |
keylist_add_groups (listview_ctrl_t lv) |
568 |
{ |
569 |
return 0; |
570 |
} |
571 |
|
572 |
|
573 |
/* Create a listview for listing keys. Use the mode given in @mode |
574 |
and the control is given in @ctrl. */ |
575 |
static int |
576 |
keylist_build (listview_ctrl_t *r_lv, HWND ctrl, int mode) |
577 |
{ |
578 |
struct listview_column_s klist_enc[] = { |
579 |
{0, 242, (char *)_("User ID")}, |
580 |
{1, 80, (char *)_("Key ID")}, |
581 |
{3, 46, (char *)_("Size")}, |
582 |
{4, 50, (char *)_("Cipher")}, |
583 |
{5, 70, (char *)_("Validity")}, |
584 |
{0, 0, NULL} |
585 |
}; |
586 |
struct listview_column_s klist[] = { |
587 |
{0, 240, (char *)_("User ID")}, |
588 |
{1, 78, (char *)_("Key ID")}, |
589 |
{2, 52, (char *)_("Type")}, |
590 |
{3, 66, (char *)_("Size")}, |
591 |
{4, 60, (char *)_("Cipher")}, |
592 |
{5, 66, (char *)_("Validity")}, |
593 |
{6, 58, (char *)_("Trust")}, |
594 |
{7, 72, (char *)_("Creation")}, |
595 |
{0, 0, NULL} |
596 |
}; |
597 |
HICON ico[4]; |
598 |
listview_ctrl_t lv; |
599 |
listview_column_t col; |
600 |
int j, n = 0, ext_chk = 0; |
601 |
|
602 |
listview_new (&lv, ctrl); |
603 |
if (mode & KEYLIST_ENCRYPT_MIN) { |
604 |
col = klist_enc; |
605 |
n = (DIM (klist_enc) -1); |
606 |
ext_chk = 1; |
607 |
} |
608 |
else if ((mode & KEYLIST_SIGN)) { |
609 |
col = klist_enc; |
610 |
n = (DIM (klist_enc) - 1) - 1; |
611 |
ext_chk = 1; |
612 |
} |
613 |
else { |
614 |
col = klist; |
615 |
n = (DIM (klist) - 1); |
616 |
} |
617 |
|
618 |
for (j = 0; j < n; j++) |
619 |
listview_add_column (lv, &col[j]); |
620 |
listview_set_ext_style (lv); |
621 |
if (ext_chk) |
622 |
listview_set_chkbox_style (lv); |
623 |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_PUBKEY); |
624 |
ico[1] = LoadIcon (glob_hinst, (LPCTSTR)IDI_KEYPAIR); |
625 |
ico[2] = LoadIcon (glob_hinst, (LPCTSTR)IDI_SORT_DOWNARROW); |
626 |
ico[3] = LoadIcon (glob_hinst, (LPCTSTR)IDI_SORT_UPARROW); |
627 |
listview_set_image_list (lv, 22, 14, ico, 4); |
628 |
listview_del_all_items (lv); |
629 |
|
630 |
*r_lv = lv; |
631 |
return 0; |
632 |
} |
633 |
|
634 |
|
635 |
static void |
636 |
keylist_load_keycache (listview_ctrl_t lv, int mode, |
637 |
gpg_keycache_t pubkc, gpg_keycache_t seckc) |
638 |
{ |
639 |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
640 |
gpgme_key_t key, skey; |
641 |
struct keycache_s *c; |
642 |
const char *keyid; |
643 |
|
644 |
if (pubkc && seckc) { |
645 |
gpg_keycache_rewind (pubkc); |
646 |
while (!gpg_keycache_next_key2 (pubkc, 0, &c, &key)) { |
647 |
keyid = key->subkeys->keyid; |
648 |
if (keyid && !gpg_keycache_find_key (seckc, keyid, 0, &skey)) |
649 |
keylist_add_key (lv, mode, c, key); |
650 |
} |
651 |
} |
652 |
else if (pubkc) { |
653 |
gpg_keycache_rewind (pubkc); |
654 |
while (!err) { |
655 |
err = gpg_keycache_next_key2 (pubkc, 0, &c, &key); |
656 |
if (!err) |
657 |
keylist_add_key (lv, mode, c, key); |
658 |
} |
659 |
} |
660 |
} |
661 |
|
662 |
|
663 |
/* Load the list view @ctrl with the keys from the cache. |
664 |
Return value: list view context on success. */ |
665 |
listview_ctrl_t |
666 |
keylist_load (HWND ctrl, gpg_keycache_t pubkc, gpg_keycache_t seckc, |
667 |
int mode, int sortby) |
668 |
{ |
669 |
listview_ctrl_t lv; |
670 |
int rc = 0; |
671 |
|
672 |
rc = keylist_build (&lv, ctrl, mode); |
673 |
if (rc) |
674 |
return NULL; |
675 |
keylist_load_keycache (lv, mode, pubkc, seckc); |
676 |
keylist_sort (lv, sortby); |
677 |
if (mode & KEYLIST_ENCRYPT_MIN) |
678 |
keylist_add_groups (lv); |
679 |
return lv; |
680 |
} |
681 |
|
682 |
|
683 |
/* Reload the given key list control @lv. */ |
684 |
int |
685 |
keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby) |
686 |
{ |
687 |
listview_del_all_items (lv); |
688 |
keylist_load_keycache (lv, mode, pubkc, NULL); |
689 |
keylist_sort (lv, sortby); |
690 |
return 0; |
691 |
} |
692 |
|
693 |
|
694 |
void |
695 |
keylist_delete (listview_ctrl_t lv) |
696 |
{ |
697 |
if (lv) { |
698 |
listview_release (lv); |
699 |
} |
700 |
} |
701 |
|
702 |
|
703 |
/* Return if there is a secret for @key. |
704 |
0 means success. */ |
705 |
static int |
706 |
find_secret_key (gpgme_key_t key) |
707 |
{ |
708 |
const char *keyid; |
709 |
winpt_key_s skey; |
710 |
|
711 |
memset (&skey, 0, sizeof (skey)); |
712 |
keyid = key->subkeys->keyid; |
713 |
if (!keyid) |
714 |
return 0; |
715 |
winpt_get_seckey (keyid, &skey); |
716 |
if (skey.ext && skey.ext->gloflags.divert_to_card) |
717 |
return 2; |
718 |
return skey.ctx? 1 : 0; |
719 |
} |
720 |
|
721 |
|
722 |
static int |
723 |
do_addkey (listview_ctrl_t lv, struct keycache_s *ctx, gpgme_key_t key, |
724 |
int uididx, int keyidx, int list) |
725 |
{ |
726 |
LV_ITEM lvi; |
727 |
gpgme_user_id_t u; |
728 |
gpgme_subkey_t k; |
729 |
char fmt[128], *p; |
730 |
const char *attr; |
731 |
u32 key_attr; |
732 |
int idx = 0; |
733 |
|
734 |
/* we check the pubkey algorithm here to make sure that no ElGamal |
735 |
sign+encrypt key is used in _any_ mode */ |
736 |
if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) { |
737 |
log_debug ("ElGamal (E+S) key found: %s (%s)\n", |
738 |
key->uids->name, key->subkeys->keyid); |
739 |
return 0; |
740 |
} |
741 |
|
742 |
if (listview_add_item2 (lv, " ", (void *)ctx)) |
743 |
return WPTERR_GENERAL; |
744 |
|
745 |
attr = ctx->uids->uid; |
746 |
memset (&lvi, 0, sizeof lvi); |
747 |
lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE; |
748 |
lvi.pszText = (char *)attr; |
749 |
lvi.iImage = find_secret_key (key)? 1 : 0; |
750 |
lvi.lParam = (LPARAM )ctx; |
751 |
if (ListView_SetItem (lv->ctrl, &lvi) == FALSE) |
752 |
return WPTERR_GENERAL; |
753 |
|
754 |
if (uididx == -1) { /* request the primary user-id of the key. */ |
755 |
attr = ctx->uids->uid; |
756 |
uididx = 0; |
757 |
} |
758 |
else { |
759 |
u = get_nth_userid (key, uididx); |
760 |
if (!u || u->revoked || uididx < 0) |
761 |
uididx = 0; |
762 |
u = get_nth_userid (key, uididx); |
763 |
attr = u->uid; |
764 |
} |
765 |
if (attr == NULL || strlen (attr) < 5) { /* normal userids are > 5 chars */ |
766 |
attr = _("Invalid User ID"); |
767 |
listview_add_sub_item (lv, 0, idx++, attr); |
768 |
} |
769 |
else |
770 |
listview_add_sub_item (lv, 0, idx++, attr); |
771 |
k = get_nth_key (key, keyidx); |
772 |
if (k && k->keyid) { |
773 |
_snprintf (fmt, sizeof fmt -1, "0x%s", k->keyid + 8); |
774 |
listview_add_sub_item (lv, 0, idx++, fmt); |
775 |
} |
776 |
if (list > 0) { |
777 |
key_attr = find_secret_key (key); |
778 |
if (!key_attr) |
779 |
attr = "pub"; |
780 |
else |
781 |
attr = key_attr == 1? "pub/sec" : "pub/crd"; |
782 |
listview_add_sub_item (lv, 0, idx++, attr); |
783 |
} |
784 |
if (lv->cols >= 2) { |
785 |
attr = get_key_size (key, list == -1? keyidx+1 : 0); |
786 |
if (attr) |
787 |
listview_add_sub_item (lv, 0, idx++, attr); |
788 |
} |
789 |
if (lv->cols >= 3) { |
790 |
attr = get_key_algo (key, list == -1? keyidx+1 : 0); |
791 |
if (attr) |
792 |
listview_add_sub_item( lv, 0, idx++, attr); |
793 |
} |
794 |
if (lv->cols >= 4) { |
795 |
p = get_key_status( key, uididx, list > 0? 1 : 0 ); |
796 |
if (!p) |
797 |
return WPTERR_GENERAL; |
798 |
listview_add_sub_item (lv, 0, idx++, p); |
799 |
free_if_alloc (p); |
800 |
} |
801 |
if (lv->cols >= 5) { |
802 |
attr = get_key_trust (key, uididx, list > 0? 1 : 0); |
803 |
listview_add_sub_item (lv, 0, idx++, attr); |
804 |
} |
805 |
if( lv->cols >= 6 ) { |
806 |
k = get_nth_key (key, keyidx); |
807 |
key_attr = k->timestamp; |
808 |
if( key_attr ) { |
809 |
attr = get_key_created (key_attr); |
810 |
listview_add_sub_item( lv, 0, idx++, attr ); |
811 |
} |
812 |
} |
813 |
|
814 |
return 0; |
815 |
} |
816 |
|
817 |
|
818 |
/* Update a single column @col but for each element in the |
819 |
listview @lv. */ |
820 |
void |
821 |
keylist_upd_col (listview_ctrl_t lv, int col) |
822 |
{ |
823 |
gpgme_key_t key; |
824 |
const char *s; |
825 |
char buf[32], *p; |
826 |
int i; |
827 |
|
828 |
for (i=0; i < listview_count_items (lv, 0); i++) { |
829 |
key = km_get_key_ptr (lv, i, NULL); |
830 |
if (!key) |
831 |
continue; |
832 |
switch (col) { |
833 |
case KM_COL_KEYID: |
834 |
_snprintf (buf, sizeof (buf)-1, "0x%s", key->subkeys->keyid+8); |
835 |
listview_add_sub_item (lv, i, col, buf); |
836 |
break; |
837 |
|
838 |
case KM_COL_CIPHER: |
839 |
s = get_key_algo (key, 0); |
840 |
listview_add_sub_item (lv, i, col, s); |
841 |
break; |
842 |
|
843 |
case KM_COL_TYPE: |
844 |
s = find_secret_key (key)? "pub/sec" : "pub"; |
845 |
listview_add_sub_item (lv, i, col, s); |
846 |
break; |
847 |
|
848 |
case KM_COL_CREAT: |
849 |
s = get_key_created (key->subkeys->timestamp); |
850 |
listview_add_sub_item (lv, i, col, s); |
851 |
break; |
852 |
|
853 |
case KM_COL_DESC: |
854 |
p = get_key_desc (key); |
855 |
listview_add_sub_item (lv, i, col, p); |
856 |
free_if_alloc (p); |
857 |
break; |
858 |
} |
859 |
} |
860 |
} |
861 |
|
862 |
|
863 |
/* Update the listview item at position @pos with the data from |
864 |
the key @key. */ |
865 |
void |
866 |
keylist_upd_key (listview_ctrl_t lv, int pos, |
867 |
struct keycache_s *ctx, gpgme_key_t key) |
868 |
{ |
869 |
const char *s; |
870 |
char *p; |
871 |
char tmp[32]; |
872 |
|
873 |
listview_set_item2 (lv, pos, (void *)ctx); |
874 |
/* the only mode we support is KEYLIST_LIST in the Key Manager */ |
875 |
|
876 |
s = ctx->uids->uid; |
877 |
if (s) |
878 |
listview_add_sub_item (lv, pos, KM_COL_UID, s); |
879 |
|
880 |
s = key->subkeys->keyid; |
881 |
if (s) { |
882 |
sprintf (tmp, "0x%s", s+8); |
883 |
listview_add_sub_item (lv, pos, KM_COL_KEYID, tmp); |
884 |
} |
885 |
|
886 |
s = find_secret_key (key)? "pub/sec" : "pub"; |
887 |
listview_add_sub_item (lv, pos, KM_COL_TYPE, s); |
888 |
|
889 |
s = get_key_size (key, 0); |
890 |
if (s) |
891 |
listview_add_sub_item (lv, pos, KM_COL_SIZE, s); |
892 |
|
893 |
s = get_key_algo (key, 0); |
894 |
if (s) |
895 |
listview_add_sub_item (lv, pos, KM_COL_CIPHER, s); |
896 |
|
897 |
p = get_key_status (key, 0, 1); |
898 |
if (p) { |
899 |
listview_add_sub_item (lv, pos, KM_COL_VALID, p); |
900 |
free_if_alloc (p); |
901 |
} |
902 |
|
903 |
s = get_key_trust (key, 0, 1); |
904 |
if (s) |
905 |
listview_add_sub_item (lv, pos, KM_COL_TRUST, s); |
906 |
|
907 |
long t = key->subkeys->timestamp; |
908 |
s = get_key_created (t); |
909 |
if (s) |
910 |
listview_add_sub_item (lv, pos, KM_COL_CREAT, s); |
911 |
} |
912 |
|
913 |
|
914 |
int |
915 |
keylist_add_key (listview_ctrl_t lv, int mode, |
916 |
struct keycache_s *ctx, gpgme_key_t key) |
917 |
{ |
918 |
int uids, rc = 0, i; |
919 |
gpgme_subkey_t k; |
920 |
|
921 |
/* if the entire key is disabled, just return. */ |
922 |
if (key->disabled && !(mode & KEYLIST_LIST)) |
923 |
return 0; |
924 |
|
925 |
for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) { |
926 |
if (k->invalid) { |
927 |
log_debug ("keylist_add_key: invalid key \"%s\"\n", key->uids->name); |
928 |
continue; /* Don't use invalid keys */ |
929 |
} |
930 |
|
931 |
if (mode & KEYLIST_ALL) { |
932 |
uids = count_userids (key); |
933 |
rc = do_addkey (lv, ctx, key, uids, i, 0); |
934 |
if (rc) |
935 |
return rc; |
936 |
} |
937 |
else if (mode & KEYLIST_LIST) |
938 |
return do_addkey (lv, ctx, key, -1, i, 1); |
939 |
else if (mode & KEYLIST_ENCRYPT) { |
940 |
if (k->can_encrypt && key_is_useable (k)) { |
941 |
if (mode & KEYLIST_FLAG_FILE) { |
942 |
rc = do_addkey (lv, ctx, key, -1, i, -1); |
943 |
if (rc) |
944 |
return rc; |
945 |
} |
946 |
else { |
947 |
for (uids = 0; uids < count_userids (key); uids++) { |
948 |
rc = do_addkey (lv, ctx, key, uids, i, -1); |
949 |
if (rc) |
950 |
return rc; |
951 |
} |
952 |
} |
953 |
} |
954 |
} |
955 |
else if (mode & KEYLIST_ENCRYPT_MIN) { |
956 |
if( k->can_encrypt && key_is_useable (k)) |
957 |
{ |
958 |
rc = do_addkey (lv, ctx, key, -1, i, -1); |
959 |
return rc; |
960 |
} |
961 |
} |
962 |
else if (mode & KEYLIST_SIGN) { |
963 |
if (k->can_sign |
964 |
&& find_secret_key (key) |
965 |
&& key_is_useable (k)) { |
966 |
rc = do_addkey (lv, ctx, key, -1, i, -1); |
967 |
if (rc) |
968 |
return rc; |
969 |
} |
970 |
} |
971 |
} |
972 |
|
973 |
return rc; |
974 |
} |
975 |
|
976 |
|
977 |
int |
978 |
keylist_sort (listview_ctrl_t lv, int sortby) |
979 |
{ |
980 |
return listview_sort_items (lv, sortby, keylist_cmp_cb); |
981 |
} |
982 |
|
983 |
|
984 |
/* Check that the validity @validity is at least >= marginal. */ |
985 |
static int |
986 |
key_check_validity (gpgme_key_t key) |
987 |
{ |
988 |
gpgme_user_id_t u; |
989 |
|
990 |
for (u=key->uids; u; u =u->next) { |
991 |
if (u->validity >= GPGME_VALIDITY_MARGINAL) |
992 |
return -1; |
993 |
} |
994 |
|
995 |
return 0; |
996 |
} |
997 |
|
998 |
|
999 |
/* Extract all selected recipients from the list @lv and return them |
1000 |
as a vector. @r_force_trust is >= 1 if one of the recipients is not |
1001 |
fully trusted. @r_count returns the number of selected keys. |
1002 |
Return value: the key list on success, NULL otherwise. */ |
1003 |
gpgme_key_t* |
1004 |
keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, size_t *r_count) |
1005 |
{ |
1006 |
key_array_s *ka = NULL; |
1007 |
keycache_s *c; |
1008 |
gpgme_key_t *keybuf, key; |
1009 |
size_t count = 0; |
1010 |
int force_trust = 0; |
1011 |
int n, j, ka_pos = 0, rc = 0; |
1012 |
int k_pos=0; |
1013 |
|
1014 |
n = listview_count_items (lv, 0); |
1015 |
|
1016 |
ka = key_array_new (n); |
1017 |
if (!ka) |
1018 |
BUG (NULL); |
1019 |
|
1020 |
keybuf = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); |
1021 |
if (!keybuf) |
1022 |
BUG (NULL); |
1023 |
|
1024 |
for (j = 0; j < n; j++) { |
1025 |
if (listview_get_item_state (lv, j) || n == 1) { |
1026 |
key = km_get_key_ptr (lv, j, &c); |
1027 |
if (!key) |
1028 |
BUG (0); |
1029 |
if (!key_check_validity (key) && |
1030 |
!key_array_search (ka, ka_pos, key->subkeys->keyid)) { |
1031 |
char *warn = new char[512+strlen (c->uids->uid) + 1]; |
1032 |
if (!warn) |
1033 |
BUG (0); |
1034 |
sprintf (warn, |
1035 |
_("It is NOT certain that the key belongs to the person\n" |
1036 |
"named in the user ID. If you *really* know what you are\n" |
1037 |
"doing, you may answer the next question with yes\n" |
1038 |
"\n" |
1039 |
"Use \"%s\" anyway?"), c->uids->uid); |
1040 |
if (reg_prefs.always_trust) |
1041 |
rc = IDYES; |
1042 |
else |
1043 |
rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK); |
1044 |
if (rc == IDYES) { |
1045 |
keybuf[k_pos++] = key; |
1046 |
force_trust++; |
1047 |
ka[ka_pos].checked = 1; |
1048 |
strcpy (ka[ka_pos++].keyid, key->subkeys->keyid); |
1049 |
count++; |
1050 |
} |
1051 |
free_if_alloc (warn); |
1052 |
} |
1053 |
else { |
1054 |
keybuf[k_pos++] = key; |
1055 |
count++; |
1056 |
} |
1057 |
} |
1058 |
} |
1059 |
key_array_release (ka); |
1060 |
if (r_force_trust) |
1061 |
*r_force_trust = force_trust; |
1062 |
if (r_count) |
1063 |
*r_count = count; |
1064 |
return keybuf; |
1065 |
} |
1066 |
|
1067 |
|
1068 |
static int |
1069 |
keylist_get_keyflags (gpgme_key_t key) |
1070 |
{ |
1071 |
int flags = KEYFLAG_NONE; |
1072 |
|
1073 |
if (key->revoked) |
1074 |
flags |= KEYFLAG_REVOKED; |
1075 |
if (key->expired) |
1076 |
flags |= KEYFLAG_EXPIRED; |
1077 |
if (key->disabled) |
1078 |
flags |= KEYFLAG_DISABLED; |
1079 |
|
1080 |
return flags; |
1081 |
} |
1082 |
|
1083 |
|
1084 |
gpgme_key_t* |
1085 |
keylist_enum_recipients (listview_ctrl_t lv, int listype, size_t *r_count) |
1086 |
{ |
1087 |
struct keycache_s *c; |
1088 |
gpgme_key_t *rset; |
1089 |
gpgme_key_t key; |
1090 |
size_t k_pos = 0; |
1091 |
int i, n, id; |
1092 |
|
1093 |
n = listview_count_items (lv, 0); |
1094 |
if (!n) |
1095 |
return 0; |
1096 |
rset = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); |
1097 |
if (!rset) |
1098 |
BUG (NULL); |
1099 |
for (i = 0; i < n; i++) { |
1100 |
if (!listview_get_item_state (lv, i)) |
1101 |
continue; |
1102 |
key = km_get_key_ptr (lv, i, &c); |
1103 |
switch (listype) { |
1104 |
case KEYLIST_LIST: |
1105 |
if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) { |
1106 |
id = printf_box (_("Recipients"), MB_INFO|MB_YESNO, |
1107 |
_("KeyID %s.\nDo you really want to export a revoked key?"), |
1108 |
c->uids->uid); |
1109 |
if (id == IDNO) |
1110 |
continue; |
1111 |
} |
1112 |
break; |
1113 |
} |
1114 |
rset[k_pos++] = key; |
1115 |
} |
1116 |
if (r_count) |
1117 |
*r_count = k_pos; |
1118 |
return rset; |
1119 |
} |
1120 |
|
1121 |
|
1122 |
void |
1123 |
seclist_destroy (keylist_t *list) |
1124 |
{ |
1125 |
keylist_t l2; |
1126 |
while (*list) { |
1127 |
l2 = (*list)->next; |
1128 |
safe_free (*list); |
1129 |
*list = l2; |
1130 |
} |
1131 |
list = NULL; |
1132 |
} |
1133 |
|
1134 |
|
1135 |
void |
1136 |
seclist_init (HWND dlg, int ctlid, int flags, keylist_t * ret_list) |
1137 |
{ |
1138 |
gpg_keycache_t kc = NULL; |
1139 |
gpgme_key_t key = NULL; |
1140 |
HWND kb; |
1141 |
keylist_t list=NULL, l, l2; |
1142 |
long pos = 0; |
1143 |
|
1144 |
SendDlgItemMessage (dlg, ctlid, CB_RESETCONTENT, 0, 0); |
1145 |
kb = GetDlgItem (dlg, ctlid); |
1146 |
kc = keycache_get_ctx (0); |
1147 |
if (!kc) |
1148 |
BUG (0); |
1149 |
gpg_keycache_rewind (kc); |
1150 |
|
1151 |
while (!gpg_keycache_next_key (kc, 1, &key)) { |
1152 |
char *inf = NULL, *uid = NULL; |
1153 |
const char *id; |
1154 |
const char *keyid; |
1155 |
int algo; |
1156 |
size_t size = 0; |
1157 |
|
1158 |
if (flags & KEYLIST_FLAG_SHORT) |
1159 |
id = key->uids->name; |
1160 |
else |
1161 |
id = key->uids->uid; |
1162 |
keyid = key->subkeys->keyid; |
1163 |
algo = key->subkeys->pubkey_algo; |
1164 |
if (!id || !keyid) |
1165 |
continue; |
1166 |
if (key->disabled || !key_is_useable (key->subkeys)) |
1167 |
continue; |
1168 |
|
1169 |
uid = utf8_to_native (id); |
1170 |
size = strlen (uid) + strlen (keyid) + 32; |
1171 |
inf = new char[size+1]; |
1172 |
if (!inf) |
1173 |
BUG (NULL); |
1174 |
_snprintf (inf, size, "%s (%s/0x%s)", uid, |
1175 |
get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8); |
1176 |
combox_add_string (kb, inf); |
1177 |
free_if_alloc (inf); |
1178 |
free (uid); |
1179 |
l = (struct keylist_s *)calloc (1, sizeof * l); |
1180 |
if (!l) |
1181 |
BUG (0); |
1182 |
l->key = key; |
1183 |
if (!list) |
1184 |
list = l; |
1185 |
else { |
1186 |
for( l2 = list; l2->next; l2 = l2->next ) |
1187 |
; |
1188 |
l2->next = l; |
1189 |
} |
1190 |
} |
1191 |
for (pos = 0, l2=list; pos < SendMessage (kb, CB_GETCOUNT, 0, 0); |
1192 |
pos++, l2=l2->next) |
1193 |
SendMessage (kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key); |
1194 |
SendMessage (kb, CB_SETCURSEL, 0, 0); |
1195 |
*ret_list = list; |
1196 |
} |
1197 |
|
1198 |
|
1199 |
/* Select a secret key from the combo box with the ID @ctlid. |
1200 |
Return the code on success in @ret_key. */ |
1201 |
int |
1202 |
seclist_select_key (HWND dlg, int ctlid, gpgme_key_t *ret_key) |
1203 |
{ |
1204 |
int pos; |
1205 |
DWORD k = 0; |
1206 |
|
1207 |
pos = SendDlgItemMessage (dlg, ctlid, CB_GETCURSEL, 0, 0); |
1208 |
if (pos == CB_ERR) { |
1209 |
msg_box (dlg, _("No key was selected."), _("Secret Key List"), MB_ERR); |
1210 |
*ret_key = NULL; |
1211 |
} |
1212 |
else { |
1213 |
k = SendDlgItemMessage (dlg, ctlid, CB_GETITEMDATA, pos, 0); |
1214 |
*ret_key = (gpgme_key_t)k; |
1215 |
} |
1216 |
return k? 0 : -1; |
1217 |
} |