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