1 |
/* wptKeylist.cpp - Keylist element |
/* wptKeylist.cpp - Keylist element |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* Copyright (C) 2004 Andreas Jobs |
* Copyright (C) 2004 Andreas Jobs |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
#include <config.h> |
24 |
#endif |
#endif |
38 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
39 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
40 |
#include "wptContext.h" |
#include "wptContext.h" |
41 |
|
#include "wptVersion.h" |
42 |
|
#include "resource.h" |
43 |
|
|
44 |
#define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled) |
#define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled) |
45 |
|
|
|
static struct listview_column_s klist_enc[] = { |
|
|
{0, 242, (char *)_("User ID")}, |
|
|
{1, 80, (char *)_("Key ID")}, |
|
|
{3, 46, (char *)_("Size")}, |
|
|
{4, 50, (char *)_("Cipher")}, |
|
|
{5, 70, (char *)_("Validity")}, |
|
|
{0, 0, NULL} |
|
|
}; |
|
|
#define KLIST_ENC_ITEMS (DIM(klist_enc) -1) |
|
|
|
|
|
static struct listview_column_s klist[] = { |
|
|
{0, 242, (char *)_("User ID")}, |
|
|
{1, 78, (char *)_("Key ID")}, |
|
|
{2, 52, (char *)_("Type")}, |
|
|
{3, 68, (char *)_("Size")}, |
|
|
{4, 66, (char *)_("Cipher")}, |
|
|
{5, 70, (char *)_("Validity")}, |
|
|
{6, 40, (char *)_("Trust")}, |
|
|
{7, 72, (char *)_("Creation")}, |
|
|
{0, 0, NULL} |
|
|
}; |
|
|
#define KLIST_ITEMS (DIM(klist) - 1) |
|
|
|
|
46 |
struct key_array_s { |
struct key_array_s { |
47 |
char keyid[32]; |
char keyid[32]; |
48 |
int checked; |
int checked; |
52 |
|
|
53 |
|
|
54 |
static key_array_s* |
static key_array_s* |
55 |
key_array_new( size_t items ) |
key_array_new (int items) |
56 |
{ |
{ |
57 |
key_array_s *ka; |
key_array_s *ka; |
58 |
size_t j; |
int j; |
59 |
|
|
60 |
if( items == 0 ) |
if (items == 0) |
61 |
return NULL; |
return NULL; |
62 |
ka = new key_array_s[items + 1]; |
ka = new key_array_s[items + 1]; |
63 |
if( ka == NULL ) |
if (!ka) |
64 |
return NULL; |
BUG (NULL); |
65 |
for ( j = 0; j < items; j++ ) |
for (j = 0; j < items; j++) |
66 |
ka[j].checked = 0; |
ka[j].checked = 0; |
67 |
return ka; |
return ka; |
68 |
} /* key_array_new */ |
} |
69 |
|
|
70 |
|
|
71 |
static void |
static void |
72 |
key_array_release( key_array_s *ka ) |
key_array_release (key_array_s *ka) |
73 |
{ |
{ |
74 |
free_if_alloc( ka ); |
free_if_alloc (ka); |
75 |
} /* key_array_release */ |
} |
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 |
static int |
81 |
key_array_search( key_array_s *ka, size_t items, const char *keyid ) |
key_array_search (key_array_s *ka, int items, const char *keyid) |
82 |
{ |
{ |
83 |
size_t j; |
int j; |
84 |
|
|
85 |
/* fixme: we need a faster search method */ |
for (j = 0; j < items; j++) { |
86 |
for( j = 0; j < items; j++ ) { |
if (!strcmp (keyid, ka[j].keyid )) |
|
if( !strcmp( keyid, ka[j].keyid ) ) |
|
87 |
return 1; |
return 1; |
88 |
} |
} |
|
|
|
89 |
return 0; |
return 0; |
90 |
} /* key_array_search */ |
} |
91 |
|
|
92 |
|
|
93 |
gpgme_user_id_t |
gpgme_user_id_t |
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 |
gpgme_key_sig_t |
158 |
get_selfsig (gpgme_user_id_t uid, const char *keyid, int first) |
get_selfsig (gpgme_user_id_t uid, const char *keyid, int first) |
159 |
{ |
{ |
160 |
gpgme_key_sig_t s, self_sig=NULL; |
gpgme_key_sig_t s, self_sig=NULL; |
161 |
long timestamp=0; |
long timestamp=0; |
162 |
|
int off = 0; |
163 |
|
|
164 |
|
if (strlen (keyid) == 8) |
165 |
|
off = 8; |
166 |
|
|
167 |
for (s = uid->signatures; s; s = s->next) { |
for (s = uid->signatures; s; s = s->next) { |
168 |
if (!strcmp (s->keyid+8, keyid) && s->timestamp > timestamp) { |
if (!strcmp (s->keyid+off, keyid) && s->timestamp > timestamp) { |
169 |
self_sig = s; |
self_sig = s; |
170 |
timestamp = s->timestamp; |
timestamp = s->timestamp; |
171 |
if (first) |
if (first) |
189 |
if (keyidx > 0) { |
if (keyidx > 0) { |
190 |
k = get_nth_key (key, keyidx-1); |
k = get_nth_key (key, keyidx-1); |
191 |
subalg = get_key_pubalgo (k->pubkey_algo); |
subalg = get_key_pubalgo (k->pubkey_algo); |
192 |
_snprintf( algo_id, DIM (algo_id)-1, "%s", subalg); |
_snprintf (algo_id, DIM (algo_id)-1, "%s", subalg); |
193 |
return algo_id; |
return algo_id; |
194 |
} |
} |
195 |
strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo)); |
strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo)); |
201 |
return algo_id; |
return algo_id; |
202 |
} |
} |
203 |
return get_key_pubalgo (key->subkeys->pubkey_algo); |
return get_key_pubalgo (key->subkeys->pubkey_algo); |
204 |
} /* get_key_algo */ |
} |
205 |
|
|
206 |
|
|
207 |
const char* |
const char* |
209 |
{ |
{ |
210 |
static char timebuf[128]; |
static char timebuf[128]; |
211 |
struct tm *warp; |
struct tm *warp; |
212 |
|
const char *dat; |
213 |
|
|
214 |
if (timestamp == 0 || timestamp == -1) |
if (timestamp < 1) |
215 |
return "????" "-??" "-??"; |
return "????" "-??" "-??"; |
216 |
warp = localtime( ×tamp ); |
dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); |
217 |
_snprintf( timebuf, sizeof timebuf - 1, "%04d-%02d-%02d", |
if (dat) |
218 |
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday ); |
return dat; |
219 |
|
warp = localtime (×tamp); |
220 |
|
_snprintf (timebuf, sizeof timebuf - 1, "%04d-%02d-%02d", |
221 |
|
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); |
222 |
return timebuf; |
return timebuf; |
223 |
} /* get_key_created */ |
} |
224 |
|
|
225 |
|
|
226 |
/* Return a string presentation of the time @timestamp. */ |
/* Return a string presentation of the time @timestamp. */ |
229 |
{ |
{ |
230 |
static char timebuf[64]; |
static char timebuf[64]; |
231 |
struct tm *warp; |
struct tm *warp; |
232 |
|
const char *dat; |
233 |
|
|
234 |
if( !timestamp ) |
if (timestamp == 0) |
235 |
return _("Never"); |
return _("Never"); |
236 |
warp = localtime( ×tamp ); |
dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); |
237 |
|
if (dat) |
238 |
|
return dat; |
239 |
|
warp = localtime (×tamp); |
240 |
_snprintf (timebuf, sizeof timebuf -1, "%04d-%02d-%02d", |
_snprintf (timebuf, sizeof timebuf -1, "%04d-%02d-%02d", |
241 |
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); |
warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); |
242 |
return timebuf; |
return timebuf; |
253 |
else if (type == 2) |
else if (type == 2) |
254 |
return _("Key Pair (Card)"); |
return _("Key Pair (Card)"); |
255 |
return _("Public Key"); |
return _("Public Key"); |
256 |
} /* get_key_type */ |
} |
257 |
|
|
258 |
|
|
259 |
const char* |
const char* |
274 |
if (n > 1) { |
if (n > 1) { |
275 |
k = get_nth_key (key, n-1); |
k = get_nth_key (key, n-1); |
276 |
size_sub = k->length; |
size_sub = k->length; |
277 |
_snprintf( size_id, sizeof (size_id) - 1, "%d/%d", size_main, size_sub ); |
_snprintf (size_id, sizeof (size_id) - 1, "%d/%d", |
278 |
|
size_main, size_sub); |
279 |
return size_id; |
return size_id; |
280 |
} |
} |
281 |
_snprintf( size_id, sizeof (size_id) - 1, "%d", size_main ); |
_snprintf( size_id, sizeof (size_id) - 1, "%d", size_main ); |
282 |
return size_id; |
return size_id; |
283 |
} /* get_key_size */ |
} |
284 |
|
|
285 |
|
|
286 |
|
const char* |
287 |
|
get_key_pubalgo2 (gpgme_pubkey_algo_t alg) |
288 |
|
{ |
289 |
|
switch (alg) { |
290 |
|
case GPGME_PK_DSA: return "D"; |
291 |
|
case GPGME_PK_RSA: return "R"; |
292 |
|
case GPGME_PK_ELG: return "G"; |
293 |
|
default: return "?"; |
294 |
|
} |
295 |
|
return "?"; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
const char* |
const char* |
303 |
case GPGME_PK_DSA: return "DSA"; |
case GPGME_PK_DSA: return "DSA"; |
304 |
case GPGME_PK_ELG: |
case GPGME_PK_ELG: |
305 |
case GPGME_PK_ELG_E: return "ELG"; |
case GPGME_PK_ELG_E: return "ELG"; |
306 |
|
case 0: /* XXX: gpgme returned pubkey algo 0 for a RSA made sig. */ |
307 |
case GPGME_PK_RSA: return "RSA"; |
case GPGME_PK_RSA: return "RSA"; |
308 |
default: return "???"; |
default: return "???"; |
309 |
} |
} |
310 |
return "???"; |
return "???"; |
311 |
} |
} |
312 |
|
|
313 |
const char * |
const char* |
314 |
get_key_fpr (gpgme_key_t key) |
get_key_fpr (gpgme_key_t key) |
315 |
{ |
{ |
316 |
static char fpr_md[64]; |
static char fpr_md[64]; |
339 |
} |
} |
340 |
} |
} |
341 |
return fpr_md; |
return fpr_md; |
342 |
} /* get_key_fpr */ |
} |
343 |
|
|
344 |
|
|
345 |
const char * |
const char* |
346 |
get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode) |
get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode) |
347 |
{ |
{ |
348 |
if (key) |
if (key) |
350 |
switch (val) { |
switch (val) { |
351 |
case GPGME_VALIDITY_UNKNOWN: |
case GPGME_VALIDITY_UNKNOWN: |
352 |
case GPGME_VALIDITY_UNDEFINED: |
case GPGME_VALIDITY_UNDEFINED: |
353 |
return "None"; |
return _("None"); |
354 |
case GPGME_VALIDITY_NEVER: |
case GPGME_VALIDITY_NEVER: |
355 |
return "Never"; |
return _("Never"); |
356 |
case GPGME_VALIDITY_MARGINAL: |
case GPGME_VALIDITY_MARGINAL: |
357 |
return "Marginal"; |
return _("Marginal"); |
358 |
case GPGME_VALIDITY_FULL: |
case GPGME_VALIDITY_FULL: |
359 |
case GPGME_VALIDITY_ULTIMATE: |
case GPGME_VALIDITY_ULTIMATE: |
360 |
return "Full"; |
return _("Full"); |
361 |
} |
} |
362 |
return ""; |
return ""; |
363 |
} |
} |
364 |
|
|
365 |
|
|
366 |
const char * |
const char* |
367 |
get_key_trust (gpgme_key_t key, int uididx, int listmode) |
get_key_trust (gpgme_key_t key, int uididx, int listmode) |
368 |
{ |
{ |
369 |
return get_key_trust2 (key, 0, uididx, listmode); |
return get_key_trust2 (key, 0, uididx, listmode); |
370 |
} |
} |
371 |
|
|
372 |
|
|
373 |
const char * |
const char* |
374 |
get_key_trust_str (int val) |
get_key_trust_str (int val) |
375 |
{ |
{ |
376 |
return get_key_trust2 (NULL, val, 0, 0); |
return get_key_trust2 (NULL, val, 0, 0); |
388 |
if (uididx < 0 || count_userids (key) > uididx) |
if (uididx < 0 || count_userids (key) > uididx) |
389 |
uididx = 0; |
uididx = 0; |
390 |
if (listmode) { |
if (listmode) { |
391 |
const char *s; |
const char *s; |
|
|
|
392 |
if (key->revoked) |
if (key->revoked) |
393 |
s = _("Revoked"); |
s = _("Revoked"); |
394 |
else if (key->expired) |
else if (key->expired) |
395 |
s = _("Expired"); |
s = _("Expired"); |
396 |
else if (key->disabled) |
else if (key->disabled) |
397 |
s = _("Disabled"); |
s = _("Disabled"); |
398 |
else |
else |
399 |
s = ""; |
s = ""; |
|
|
|
400 |
/* if the key has a special status, we don't continue to figure out |
/* if the key has a special status, we don't continue to figure out |
401 |
what any user-id validities. */ |
the user-id validities. */ |
402 |
if (*s) |
if (*s) |
403 |
return m_strdup (s); |
return m_strdup (s); |
404 |
} |
} |
409 |
} |
} |
410 |
|
|
411 |
|
|
412 |
|
/* Return human readable description of the key @key. */ |
413 |
|
char* |
414 |
|
get_key_desc (gpgme_key_t key) |
415 |
|
{ |
416 |
|
gpgme_key_t sk; |
417 |
|
const char *state, *alg, *type; |
418 |
|
char *p; |
419 |
|
|
420 |
|
/* XXX: problems with the German translation. */ |
421 |
|
state = ""; |
422 |
|
if (key->disabled) |
423 |
|
state = _("Disabled"); |
424 |
|
if (key->expired) |
425 |
|
state = _("Expired"); |
426 |
|
if (key->revoked) |
427 |
|
state = _("Revoked"); |
428 |
|
alg = "OpenPGP"; |
429 |
|
if (strlen (key->subkeys->fpr) == 32) |
430 |
|
alg = "RSA Legacy"; |
431 |
|
type = _("public key"); |
432 |
|
if (!get_seckey (key->subkeys->keyid+8, &sk)) |
433 |
|
type = _("key pair"); |
434 |
|
p = new char[strlen (state) + strlen (alg) + strlen (type) + 4 + 1]; |
435 |
|
if (!p) |
436 |
|
BUG (0); |
437 |
|
sprintf (p, "%s %s %s", state, alg, type); |
438 |
|
return p; |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
/* Integer comparsion of @a and @b. |
/* Integer comparsion of @a and @b. |
443 |
Return values: same as in strcmp. */ |
Return values: same as in strcmp. */ |
444 |
static inline int |
static inline int |
460 |
return GPGME_VALIDITY_ULTIMATE+1; |
return GPGME_VALIDITY_ULTIMATE+1; |
461 |
else if (k->expired) |
else if (k->expired) |
462 |
return GPGME_VALIDITY_ULTIMATE+2; |
return GPGME_VALIDITY_ULTIMATE+2; |
463 |
|
else if (k->disabled) |
464 |
|
return GPGME_VALIDITY_ULTIMATE+3; |
465 |
return k->uids->validity; |
return k->uids->validity; |
466 |
} |
} |
467 |
|
|
529 |
} |
} |
530 |
|
|
531 |
|
|
|
/* Return the validity of the group @grp. */ |
|
|
static const char* |
|
|
calc_validity (gpg_group_t grp) |
|
|
{ |
|
|
int valid=0; |
|
|
gpg_member_t mbr; |
|
|
gpgme_key_t key; |
|
|
|
|
|
for (mbr = grp->list; mbr; mbr = mbr->next) { |
|
|
if (get_pubkey (mbr->name, &key)) |
|
|
continue; |
|
|
valid = key->uids->validity; |
|
|
switch (valid) { |
|
|
case GPGME_VALIDITY_MARGINAL: |
|
|
case GPGME_VALIDITY_NEVER: |
|
|
case GPGME_VALIDITY_UNDEFINED: |
|
|
return get_key_trust2 (NULL, valid, 0, 0); |
|
|
} |
|
|
} |
|
|
return _("Full"); |
|
|
} |
|
|
|
|
|
|
|
532 |
int |
int |
533 |
keylist_add_groups( listview_ctrl_t lv ) |
keylist_add_groups (listview_ctrl_t lv) |
534 |
{ |
{ |
|
#if 0 |
|
|
gpg_optfile_t gh; |
|
|
gpg_group_t grp; |
|
|
const char *valid; |
|
|
|
|
|
gh = km_groupdb_open( ); |
|
|
if( !gh ) |
|
|
return WPTERR_FILE_OPEN; |
|
|
|
|
|
for( grp = gh->grp; grp; grp = grp->next ) { |
|
|
valid = calc_validity( grp ); |
|
|
listview_add_item( lv, " " ); |
|
|
listview_add_sub_item( lv, 0, 0, grp->name ); |
|
|
listview_add_sub_item( lv, 0, 1, "gpg_group_t" ); |
|
|
listview_add_sub_item( lv, 0, 2, "" ); |
|
|
listview_add_sub_item( lv, 0, 3, "Unknown" ); |
|
|
listview_add_sub_item( lv, 0, 4, valid?valid : "Unknown" ); |
|
|
} |
|
|
#endif |
|
535 |
return 0; |
return 0; |
536 |
} /* keylist_add_groups */ |
} |
537 |
|
|
538 |
|
|
539 |
/* Create a listview for listing keys. Use the mode given in @mode |
/* Create a listview for listing keys. Use the mode given in @mode |
541 |
static int |
static int |
542 |
keylist_build (listview_ctrl_t *r_lv, HWND ctrl, int mode) |
keylist_build (listview_ctrl_t *r_lv, HWND ctrl, int mode) |
543 |
{ |
{ |
544 |
|
struct listview_column_s klist_enc[] = { |
545 |
|
{0, 242, (char *)_("User ID")}, |
546 |
|
{1, 80, (char *)_("Key ID")}, |
547 |
|
{3, 46, (char *)_("Size")}, |
548 |
|
{4, 50, (char *)_("Cipher")}, |
549 |
|
{5, 70, (char *)_("Validity")}, |
550 |
|
{0, 0, NULL} |
551 |
|
}; |
552 |
|
struct listview_column_s klist[] = { |
553 |
|
{0, 240, (char *)_("User ID")}, |
554 |
|
{1, 78, (char *)_("Key ID")}, |
555 |
|
{2, 52, (char *)_("Type")}, |
556 |
|
{3, 66, (char *)_("Size")}, |
557 |
|
{4, 60, (char *)_("Cipher")}, |
558 |
|
{5, 66, (char *)_("Validity")}, |
559 |
|
{6, 58, (char *)_("Trust")}, |
560 |
|
{7, 72, (char *)_("Creation")}, |
561 |
|
{0, 0, NULL} |
562 |
|
}; |
563 |
|
HICON ico[2]; |
564 |
listview_ctrl_t lv; |
listview_ctrl_t lv; |
565 |
listview_column_t col; |
listview_column_t col; |
566 |
int j, n = 0; |
int j, n = 0, ext_chk = 0; |
567 |
int rc = 0; |
int rc = 0; |
568 |
|
|
569 |
rc = listview_new (&lv); |
rc = listview_new (&lv); |
570 |
if( rc ) |
if (rc) |
571 |
return rc; |
return rc; |
572 |
|
|
573 |
lv->ctrl = ctrl; |
lv->ctrl = ctrl; |
574 |
if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) { |
if (mode & KEYLIST_ENCRYPT_MIN) { |
575 |
col = klist_enc; |
col = klist_enc; |
576 |
n = KLIST_ENC_ITEMS; |
n = (DIM(klist_enc) -1); |
577 |
|
ext_chk = 1; |
578 |
} |
} |
579 |
else if ((mode & KEYLIST_SIGN)) { |
else if ((mode & KEYLIST_SIGN)) { |
580 |
col = klist_enc; |
col = klist_enc; |
581 |
n = KLIST_ENC_ITEMS - 1; |
n = (DIM(klist_enc) - 1) - 1; |
582 |
|
ext_chk = 1; |
583 |
} |
} |
584 |
else { |
else { |
585 |
col = klist; |
col = klist; |
586 |
n = KLIST_ITEMS; |
n = (DIM(klist) - 1); |
587 |
} |
} |
588 |
|
|
589 |
for( j = 0; j < n; j++ ) |
for (j = 0; j < n; j++) |
590 |
listview_add_column( lv, &col[j] ); |
listview_add_column (lv, &col[j]); |
591 |
listview_set_ext_style( lv ); |
listview_set_ext_style (lv); |
592 |
|
if (ext_chk) |
593 |
|
listview_set_chkbox_style (lv); |
594 |
|
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_PUBKEY); |
595 |
|
ico[1] = LoadIcon (glob_hinst, (LPCTSTR)IDI_KEYPAIR); |
596 |
|
listview_set_image_list (lv, 22, 14, ico, 2); |
597 |
|
listview_del_all_items (lv); |
598 |
|
|
599 |
*r_lv = lv; |
*r_lv = lv; |
|
|
|
600 |
return 0; |
return 0; |
601 |
} |
} |
602 |
|
|
642 |
return NULL; |
return NULL; |
643 |
keylist_load_keycache (lv, mode, pubkc, seckc); |
keylist_load_keycache (lv, mode, pubkc, seckc); |
644 |
keylist_sort (lv, sortby); |
keylist_sort (lv, sortby); |
645 |
if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) |
if (mode & KEYLIST_ENCRYPT_MIN) |
646 |
keylist_add_groups (lv); |
keylist_add_groups (lv); |
647 |
return lv; |
return lv; |
648 |
} |
} |
652 |
int |
int |
653 |
keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby) |
keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby) |
654 |
{ |
{ |
655 |
listview_del_all (lv); |
listview_del_all_items (lv); |
656 |
keylist_load_keycache( lv, mode, pubkc, NULL ); |
keylist_load_keycache (lv, mode, pubkc, NULL); |
657 |
keylist_sort (lv, sortby); |
keylist_sort (lv, sortby); |
658 |
return 0; |
return 0; |
659 |
} |
} |
701 |
/* we check the pubkey algorithm here to make sure that no ElGamal |
/* we check the pubkey algorithm here to make sure that no ElGamal |
702 |
sign+encrypt key is used in _any_ mode */ |
sign+encrypt key is used in _any_ mode */ |
703 |
if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) { |
if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) { |
704 |
log_debug ("ElGamal (E+S) key found: %s (%s)\n", |
log_debug ("ElGamal (E+S) key found: %s (%s)\n", |
705 |
key->uids->name, key->subkeys->keyid); |
key->uids->name, key->subkeys->keyid); |
706 |
return 0; |
return 0; |
707 |
} |
} |
|
|
|
708 |
|
|
709 |
if (listview_add_item2 (lv, " ", (void *)key)) |
if (listview_add_item2 (lv, " ", (void *)key)) |
710 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
711 |
|
|
712 |
attr = key->uids->uid; |
attr = key->uids->uid; |
713 |
memset (&lvi, 0, sizeof lvi); |
memset (&lvi, 0, sizeof lvi); |
714 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE; |
715 |
lvi.pszText = (char *)attr; |
lvi.pszText = (char *)attr; |
716 |
|
lvi.iImage = find_secret_key (key)? 1 : 0; |
717 |
lvi.lParam = (LPARAM )key; |
lvi.lParam = (LPARAM )key; |
718 |
if (ListView_SetItem( lv->ctrl, &lvi ) == FALSE) |
if (ListView_SetItem (lv->ctrl, &lvi) == FALSE) |
719 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
720 |
|
|
721 |
if (uididx == -1) { /* request the primary user-id of the key. */ |
if (uididx == -1) { /* request the primary user-id of the key. */ |
725 |
else { |
else { |
726 |
u = get_nth_userid (key, uididx); |
u = get_nth_userid (key, uididx); |
727 |
if (!u || u->revoked || uididx < 0) |
if (!u || u->revoked || uididx < 0) |
728 |
uididx = 0; /* fixme: this happen sometimes but it's illegal! (<0) */ |
uididx = 0; |
729 |
u = get_nth_userid (key, uididx); |
u = get_nth_userid (key, uididx); |
|
/*attr = key->uids->uid; XXX*/ |
|
730 |
attr = u->uid; |
attr = u->uid; |
731 |
} |
} |
732 |
if (attr == NULL || strlen (attr) < 5) { /* normal userids are >= 5 chars */ |
if (attr == NULL || strlen (attr) < 5) { /* normal userids are > 5 chars */ |
733 |
attr = _("Invalid User ID"); |
attr = _("Invalid User ID"); |
734 |
listview_add_sub_item (lv, 0, idx++, attr); |
listview_add_sub_item (lv, 0, idx++, attr); |
735 |
} |
} |
736 |
else { |
else { |
737 |
char *uid = utf8_to_wincp (attr, strlen (attr)); |
char *uid = utf8_to_native (attr); |
738 |
if (uid) { |
if (uid) { |
739 |
listview_add_sub_item (lv, 0, idx++, uid); |
listview_add_sub_item (lv, 0, idx++, uid); |
740 |
free (uid); |
free (uid); |
763 |
if (attr) |
if (attr) |
764 |
listview_add_sub_item( lv, 0, idx++, attr); |
listview_add_sub_item( lv, 0, idx++, attr); |
765 |
} |
} |
766 |
if( lv->cols >= 4 ) { |
if (lv->cols >= 4) { |
767 |
p = get_key_status( key, uididx, list > 0? 1 : 0 ); |
p = get_key_status( key, uididx, list > 0? 1 : 0 ); |
768 |
if (!p) |
if (!p) |
769 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
787 |
} |
} |
788 |
|
|
789 |
|
|
790 |
|
/* Update a single column @col but for each element in the |
791 |
|
listview @lv. */ |
792 |
|
void |
793 |
|
keylist_upd_col (listview_ctrl_t lv, int col) |
794 |
|
{ |
795 |
|
gpgme_key_t key; |
796 |
|
const char *s; |
797 |
|
char buf[32], *p; |
798 |
|
int i; |
799 |
|
|
800 |
|
for (i=0; i < listview_count_items (lv, 0); i++) { |
801 |
|
key = (gpgme_key_t)listview_get_item2 (lv, i); |
802 |
|
if (!key) |
803 |
|
continue; |
804 |
|
switch (col) { |
805 |
|
case KM_COL_KEYID: |
806 |
|
_snprintf (buf, sizeof (buf)-1, "0x%s", key->subkeys->keyid+8); |
807 |
|
listview_add_sub_item (lv, i, col, buf); |
808 |
|
break; |
809 |
|
|
810 |
|
case KM_COL_CIPHER: |
811 |
|
s = get_key_algo (key, 0); |
812 |
|
listview_add_sub_item (lv, i, col, s); |
813 |
|
break; |
814 |
|
|
815 |
|
case KM_COL_TYPE: |
816 |
|
s = find_secret_key (key)? "pub/sec" : "pub"; |
817 |
|
listview_add_sub_item (lv, i, col, s); |
818 |
|
break; |
819 |
|
|
820 |
|
case KM_COL_CREAT: |
821 |
|
s = get_key_created (key->subkeys->timestamp); |
822 |
|
listview_add_sub_item (lv, i, col, s); |
823 |
|
break; |
824 |
|
|
825 |
|
case KM_COL_DESC: |
826 |
|
p = get_key_desc (key); |
827 |
|
listview_add_sub_item (lv, i, col, p); |
828 |
|
free_if_alloc (p); |
829 |
|
break; |
830 |
|
} |
831 |
|
} |
832 |
|
} |
833 |
|
|
834 |
|
|
835 |
|
/* Update the listview item at position @pos with the data from |
836 |
|
the key @key. */ |
837 |
void |
void |
838 |
keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key) |
keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key) |
839 |
{ |
{ |
840 |
const char *s; |
const char *s; |
841 |
|
char *uid, *p; |
842 |
char tmp[32]; |
char tmp[32]; |
843 |
|
|
844 |
listview_set_item2 (lv, pos, (void *)key); |
listview_set_item2 (lv, pos, (void *)key); |
845 |
/* the only mode we support is KYLIST_LIST in the Key Manager */ |
/* the only mode we support is KYLIST_LIST in the Key Manager */ |
846 |
|
|
847 |
s = key->uids->uid; |
s = key->uids->uid; |
848 |
if (s) |
if (s) { |
849 |
listview_add_sub_item (lv, pos, 0, s); |
uid = utf8_to_native (s); |
850 |
|
listview_add_sub_item (lv, pos, KM_COL_UID, uid); |
851 |
|
free (uid); |
852 |
|
} |
853 |
|
|
854 |
s = key->subkeys->keyid; |
s = key->subkeys->keyid; |
855 |
if (s) { |
if (s) { |
856 |
sprintf (tmp, "0x%s", s+8); |
sprintf (tmp, "0x%s", s+8); |
857 |
listview_add_sub_item (lv, pos, 1, tmp); |
listview_add_sub_item (lv, pos, KM_COL_KEYID, tmp); |
858 |
} |
} |
859 |
|
|
860 |
s = find_secret_key (key)? "pub/sec" : "pub"; |
s = find_secret_key (key)? "pub/sec" : "pub"; |
861 |
listview_add_sub_item (lv, pos, 2, s); |
listview_add_sub_item (lv, pos, KM_COL_TYPE, s); |
862 |
|
|
863 |
s = get_key_size (key, 0); |
s = get_key_size (key, 0); |
864 |
if (s) |
if (s) |
865 |
listview_add_sub_item (lv, pos, 3, s); |
listview_add_sub_item (lv, pos, KM_COL_SIZE, s); |
866 |
|
|
867 |
s = get_key_algo (key, 0); |
s = get_key_algo (key, 0); |
868 |
if (s) |
if (s) |
869 |
listview_add_sub_item (lv, pos, 4, s); |
listview_add_sub_item (lv, pos, KM_COL_CIPHER, s); |
870 |
|
|
871 |
s = get_key_status (key, 0, 1); |
p = get_key_status (key, 0, 1); |
872 |
if (s) |
if (p) { |
873 |
listview_add_sub_item (lv, pos, 5, s); |
listview_add_sub_item (lv, pos, KM_COL_VALID, p); |
874 |
|
free_if_alloc (p); |
875 |
|
} |
876 |
|
|
877 |
s = get_key_trust (key, 0, 1); |
s = get_key_trust (key, 0, 1); |
878 |
if (s) |
if (s) |
879 |
listview_add_sub_item (lv, pos, 6, s); |
listview_add_sub_item (lv, pos, KM_COL_TRUST, s); |
880 |
|
|
881 |
long t = key->subkeys->timestamp; |
long t = key->subkeys->timestamp; |
882 |
s = get_key_created (t); |
s = get_key_created (t); |
883 |
if (s) |
if (s) |
884 |
listview_add_sub_item (lv, pos, 7, s); |
listview_add_sub_item (lv, pos, KM_COL_CREAT, s); |
885 |
} |
} |
886 |
|
|
887 |
|
|
892 |
gpgme_subkey_t k; |
gpgme_subkey_t k; |
893 |
|
|
894 |
/* if the entire key is disabled, just return. */ |
/* if the entire key is disabled, just return. */ |
895 |
if (key->disabled) |
if (key->disabled && !(mode & KEYLIST_LIST)) |
896 |
return 0; |
return 0; |
897 |
|
|
898 |
for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) { |
for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) { |
904 |
if (mode & KEYLIST_ALL) { |
if (mode & KEYLIST_ALL) { |
905 |
uids = count_userids (key); |
uids = count_userids (key); |
906 |
rc = do_addkey (lv, key, uids, i, 0); |
rc = do_addkey (lv, key, uids, i, 0); |
907 |
if( rc ) |
if (rc) |
908 |
return rc; |
return rc; |
909 |
} |
} |
910 |
else if (mode & KEYLIST_LIST) |
else if (mode & KEYLIST_LIST) |
917 |
return rc; |
return rc; |
918 |
} |
} |
919 |
else { |
else { |
920 |
for( uids = 0; uids < count_userids (key); uids++ ) { |
for (uids = 0; uids < count_userids (key); uids++) { |
921 |
rc = do_addkey( lv, key, uids, i, -1 ); |
rc = do_addkey (lv, key, uids, i, -1); |
922 |
if( rc ) |
if (rc) |
923 |
return rc; |
return rc; |
924 |
} |
} |
925 |
} |
} |
944 |
} |
} |
945 |
|
|
946 |
return rc; |
return rc; |
947 |
} /* keylist_add_key */ |
} |
948 |
|
|
949 |
|
|
950 |
int |
int |
951 |
keylist_sort (listview_ctrl_t lv, int sortby) |
keylist_sort (listview_ctrl_t lv, int sortby) |
952 |
{ |
{ |
953 |
return listview_sort_items (lv, sortby, keylist_cmp_cb); |
return listview_sort_items (lv, sortby, keylist_cmp_cb); |
954 |
} |
} |
955 |
|
|
956 |
|
|
957 |
/* Check that the validity @validity is at least >= marginal. */ |
/* Check that the validity @validity is at least >= marginal. */ |
958 |
static int |
static int |
959 |
key_check_validity (const char *validity) |
key_check_validity (gpgme_key_t key) |
960 |
{ |
{ |
961 |
if (strstr (validity, "Unknown") || |
gpgme_user_id_t u; |
962 |
strstr (validity, "Undefined") || |
|
963 |
strstr (validity, "Never") || |
for (u=key->uids; u; u =u->next) { |
964 |
strstr (validity, "None")) |
if (u->validity >= GPGME_VALIDITY_MARGINAL) |
965 |
return 0; |
return -1; |
966 |
return 1; |
} |
967 |
|
|
968 |
|
return 0; |
969 |
} |
} |
970 |
|
|
971 |
|
|
976 |
gpgme_key_t* |
gpgme_key_t* |
977 |
keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count) |
keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count) |
978 |
{ |
{ |
979 |
|
key_array_s *ka = NULL; |
980 |
|
gpgme_key_t *keybuf, key; |
981 |
int count = 0, force_trust = 0; |
int count = 0, force_trust = 0; |
982 |
int n, j, ka_pos = 0, rc = 0; |
int n, j, ka_pos = 0, rc = 0; |
983 |
int k_pos=0; |
int k_pos=0; |
|
char keyid[32], valid[32], id[100]; |
|
|
key_array_s *ka = NULL; |
|
|
gpgme_key_t *keybuf; |
|
984 |
|
|
985 |
n = listview_count_items( lv, 0 ); |
n = listview_count_items (lv, 0); |
986 |
|
|
987 |
ka = key_array_new( n ); |
ka = key_array_new (n); |
988 |
if (!ka) |
if (!ka) |
989 |
BUG (NULL); |
BUG (NULL); |
990 |
|
|
991 |
keybuf = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t)); |
keybuf = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); |
992 |
if (!keybuf) |
if (!keybuf) |
993 |
BUG (NULL); |
BUG (NULL); |
994 |
|
|
995 |
for( j = 0; j < n; j++ ) { |
for (j = 0; j < n; j++) { |
996 |
if( listview_get_item_state (lv, j) || n == 1) { |
if (listview_get_item_state (lv, j) || n == 1) { |
997 |
listview_get_item_text (lv, j, 0, id, sizeof id-1); |
key = (gpgme_key_t)listview_get_item2 (lv, j); |
998 |
listview_get_item_text (lv, j, 1, keyid, sizeof keyid - 1); |
if (!key) |
999 |
listview_get_item_text (lv, j, 4, valid, sizeof valid -1); |
BUG (0); |
1000 |
if( !key_check_validity (valid) |
if (!key_check_validity (key) && |
1001 |
&& !key_array_search( ka, ka_pos, keyid )) { |
!key_array_search (ka, ka_pos, key->subkeys->keyid)) { |
1002 |
char *warn = new char[512+strlen (id) + 1]; |
char *utf8_uid = utf8_to_native (key->uids->uid); |
1003 |
|
char *warn = new char[512+strlen (utf8_uid) + 1]; |
1004 |
if (!warn) |
if (!warn) |
1005 |
BUG (0); |
BUG (0); |
1006 |
sprintf (warn, |
sprintf (warn, |
1008 |
"named in the user ID. If you *really* know what you are\n" |
"named in the user ID. If you *really* know what you are\n" |
1009 |
"doing, you may answer the next question with yes\n" |
"doing, you may answer the next question with yes\n" |
1010 |
"\n" |
"\n" |
1011 |
"Use \"%s\" anyway?"), id); |
"Use \"%s\" anyway?"), utf8_uid); |
1012 |
if (reg_prefs.always_trust) |
if (reg_prefs.always_trust) |
1013 |
rc = IDYES; |
rc = IDYES; |
1014 |
else |
else |
1015 |
rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK); |
rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK); |
1016 |
if (rc == IDYES) { |
if (rc == IDYES) { |
1017 |
gpgme_key_t k; |
keybuf[k_pos++] = key; |
|
get_pubkey (keyid, &k); |
|
|
keybuf[k_pos++] = k; |
|
1018 |
force_trust++; |
force_trust++; |
1019 |
ka[ka_pos].checked = 1; |
ka[ka_pos].checked = 1; |
1020 |
strcpy (ka[ka_pos++].keyid, keyid); |
strcpy (ka[ka_pos++].keyid, key->subkeys->keyid); |
1021 |
count++; |
count++; |
1022 |
} |
} |
1023 |
|
safe_free (utf8_uid); |
1024 |
free_if_alloc (warn); |
free_if_alloc (warn); |
1025 |
} |
} |
1026 |
else { |
else { |
1027 |
gpgme_key_t k; |
keybuf[k_pos++] = key; |
|
listview_get_item_text( lv, j, 1, keyid, sizeof keyid -1 ); |
|
|
get_pubkey (keyid, &k); |
|
|
keybuf[k_pos++] = k; |
|
1028 |
count++; |
count++; |
1029 |
} |
} |
1030 |
} |
} |
1039 |
|
|
1040 |
|
|
1041 |
static int |
static int |
1042 |
keylist_get_keyflags (const char *buf, size_t buflen) |
keylist_get_keyflags (gpgme_key_t key) |
1043 |
{ |
{ |
1044 |
int c = 0, flags = 0; |
int flags = KEYFLAG_NONE; |
1045 |
|
|
1046 |
if( *buf != '[' ) |
if (key->revoked) |
1047 |
return KEYFLAG_NONE; |
flags |= KEYFLAG_REVOKED; |
1048 |
while (buf && c != ']') |
if (key->expired) |
1049 |
{ |
flags |= KEYFLAG_EXPIRED; |
1050 |
c = *buf++; |
if (key->disabled) |
1051 |
if (c == 'R') |
flags |= KEYFLAG_DISABLED; |
|
flags |= KEYFLAG_REVOKED; |
|
|
if (c == 'E') |
|
|
flags |= KEYFLAG_EXPIRED; |
|
|
if (c == 'D') |
|
|
flags |= KEYFLAG_DISABLED; |
|
|
} |
|
1052 |
|
|
1053 |
return flags; |
return flags; |
1054 |
} /* keylist_get_keyflags */ |
} |
1055 |
|
|
1056 |
|
|
1057 |
gpgme_key_t* |
gpgme_key_t* |
1058 |
keylist_enum_recipients (listview_ctrl_t lv, int listype, int *r_count) |
keylist_enum_recipients (listview_ctrl_t lv, int listype, int *r_count) |
1059 |
{ |
{ |
1060 |
gpgme_key_t* rset; |
gpgme_key_t *rset; |
1061 |
gpgme_key_t k; |
gpgme_key_t key; |
1062 |
int i, n, id, k_pos=0; |
int i, n, id, k_pos=0; |
|
char keyid[32], t[128], t2[128]; |
|
1063 |
|
|
1064 |
n = listview_count_items (lv, 0); |
n = listview_count_items (lv, 0); |
1065 |
if (!n) |
if (!n) |
1066 |
return 0; |
return 0; |
1067 |
rset = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t)); |
rset = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); |
1068 |
if (!rset) |
if (!rset) |
1069 |
BUG (NULL); |
BUG (NULL); |
1070 |
for( i = 0; i < n; i++ ) { |
for (i = 0; i < n; i++) { |
1071 |
if( !listview_get_item_state( lv, i ) ) |
if (!listview_get_item_state (lv, i)) |
1072 |
continue; |
continue; |
1073 |
listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 ); |
key = (gpgme_key_t)listview_get_item2 (lv, i); |
1074 |
switch( listype ) { |
if (!key) |
1075 |
|
BUG (0); |
1076 |
|
switch (listype) { |
1077 |
case KEYLIST_LIST: |
case KEYLIST_LIST: |
1078 |
listview_get_item_text( lv, i, 5, t, sizeof t - 1 ); |
if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) { |
1079 |
if( keylist_get_keyflags( t, strlen( t ) ) & KEYFLAG_REVOKED ) { |
id = printf_box (_("Recipients"), MB_INFO|MB_YESNO, |
1080 |
_snprintf( t2, sizeof t2 -1, |
_("KeyID %s.\nDo you really want to export a revoked key?"), |
1081 |
_("KeyID %s.\nDo you really want to export a revoked key?"), keyid ); |
key->uids->uid); |
1082 |
id = msg_box( lv->ctrl, t2, _("Recipients"), MB_INFO|MB_YESNO ); |
if (id == IDNO) |
1083 |
if( id == IDNO ) |
continue; |
|
continue; |
|
1084 |
} |
} |
1085 |
break; |
break; |
1086 |
} |
} |
1087 |
get_pubkey (keyid, &k); |
rset[k_pos++] = key; |
|
rset[k_pos++] = k; |
|
1088 |
} |
} |
1089 |
if (r_count) |
if (r_count) |
1090 |
*r_count = k_pos; |
*r_count = k_pos; |
1091 |
return rset; |
return rset; |
1092 |
} /* keylist_enum_recipients */ |
} |
1093 |
|
|
1094 |
|
|
1095 |
void |
void |
1096 |
seclist_destroy (keylist_t * list) |
seclist_destroy (keylist_t *list) |
1097 |
{ |
{ |
1098 |
keylist_t l2; |
keylist_t l2; |
1099 |
while (*list) { |
while (*list) { |
1102 |
*list = l2; |
*list = l2; |
1103 |
} |
} |
1104 |
list = NULL; |
list = NULL; |
1105 |
} /* seclist_destroy */ |
} |
1106 |
|
|
1107 |
|
|
1108 |
void |
void |
1139 |
if (key->disabled || !key_is_useable (key->subkeys)) |
if (key->disabled || !key_is_useable (key->subkeys)) |
1140 |
continue; |
continue; |
1141 |
|
|
1142 |
uid = utf8_to_wincp (id, strlen (id)); |
uid = utf8_to_native (id); |
1143 |
size = strlen( uid ) + strlen( keyid ) + 32; |
size = strlen (uid) + strlen (keyid) + 32; |
1144 |
inf = new char[size+1]; |
inf = new char[size+1]; |
1145 |
if( !inf ) |
if (!inf) |
1146 |
BUG( NULL ); |
BUG (NULL); |
1147 |
_snprintf (inf, size, "%s (%s/0x%s)", uid, |
_snprintf (inf, size, "%s (%s/0x%s)", uid, |
1148 |
get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8); |
get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8); |
1149 |
combox_add_string (kb, inf); |
combox_add_string (kb, inf); |
1150 |
free_if_alloc (inf); |
free_if_alloc (inf); |
1161 |
l2->next = l; |
l2->next = l; |
1162 |
} |
} |
1163 |
} |
} |
1164 |
for( pos = 0, l2=list; pos < SendMessage( kb, CB_GETCOUNT, 0, 0 ); pos++, l2=l2->next ) |
for (pos = 0, l2=list; pos < SendMessage (kb, CB_GETCOUNT, 0, 0); |
1165 |
SendMessage( kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key ); |
pos++, l2=l2->next) |
1166 |
SendMessage( kb, CB_SETCURSEL, 0, 0 ); |
SendMessage (kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key); |
1167 |
|
SendMessage (kb, CB_SETCURSEL, 0, 0); |
1168 |
*ret_list = list; |
*ret_list = list; |
1169 |
} |
} |
1170 |
|
|