/[winpt]/trunk/Src/wptKeylist.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (show annotations)
Mon Jun 12 13:40:21 2006 UTC (18 years, 8 months ago) by twoaday
File size: 27362 byte(s)
Prepare new release.


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 (&timestamp);
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 (&timestamp);
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[2];
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 listview_set_image_list (lv, 22, 14, ico, 2);
626 listview_del_all_items (lv);
627
628 *r_lv = lv;
629 return 0;
630 }
631
632
633 static void
634 keylist_load_keycache (listview_ctrl_t lv, int mode,
635 gpg_keycache_t pubkc, gpg_keycache_t seckc)
636 {
637 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
638 gpgme_key_t key, skey;
639 struct keycache_s *c;
640 const char *keyid;
641
642 if (pubkc && seckc) {
643 gpg_keycache_rewind (pubkc);
644 while (!gpg_keycache_next_key2 (pubkc, 0, &c, &key)) {
645 keyid = key->subkeys->keyid;
646 if (keyid && !gpg_keycache_find_key (seckc, keyid, 0, &skey))
647 keylist_add_key (lv, mode, c, key);
648 }
649 }
650 else if (pubkc) {
651 gpg_keycache_rewind (pubkc);
652 while (!err) {
653 err = gpg_keycache_next_key2 (pubkc, 0, &c, &key);
654 if (!err)
655 keylist_add_key (lv, mode, c, key);
656 }
657 }
658 }
659
660
661 /* Load the list view @ctrl with the keys from the cache.
662 Return value: list view context on success. */
663 listview_ctrl_t
664 keylist_load (HWND ctrl, gpg_keycache_t pubkc, gpg_keycache_t seckc,
665 int mode, int sortby)
666 {
667 listview_ctrl_t lv;
668 int rc = 0;
669
670 rc = keylist_build (&lv, ctrl, mode);
671 if (rc)
672 return NULL;
673 keylist_load_keycache (lv, mode, pubkc, seckc);
674 keylist_sort (lv, sortby);
675 if (mode & KEYLIST_ENCRYPT_MIN)
676 keylist_add_groups (lv);
677 return lv;
678 }
679
680
681 /* Reload the given key list control @lv. */
682 int
683 keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby)
684 {
685 listview_del_all_items (lv);
686 keylist_load_keycache (lv, mode, pubkc, NULL);
687 keylist_sort (lv, sortby);
688 return 0;
689 }
690
691
692 void
693 keylist_delete (listview_ctrl_t lv)
694 {
695 if (lv) {
696 listview_release (lv);
697 }
698 }
699
700
701 /* Return if there is a secret for @key.
702 0 means success. */
703 static int
704 find_secret_key (gpgme_key_t key)
705 {
706 const char *keyid;
707 winpt_key_s skey;
708
709 memset (&skey, 0, sizeof (skey));
710 keyid = key->subkeys->keyid;
711 if (!keyid)
712 return 0;
713 winpt_get_seckey (keyid, &skey);
714 if (skey.ext && skey.ext->gloflags.divert_to_card)
715 return 2;
716 return skey.ctx? 1 : 0;
717 }
718
719
720 static int
721 do_addkey (listview_ctrl_t lv, struct keycache_s *ctx, gpgme_key_t key,
722 int uididx, int keyidx, int list)
723 {
724 LV_ITEM lvi;
725 gpgme_user_id_t u;
726 gpgme_subkey_t k;
727 char fmt[128], *p;
728 const char *attr;
729 u32 key_attr;
730 int idx = 0;
731
732 /* we check the pubkey algorithm here to make sure that no ElGamal
733 sign+encrypt key is used in _any_ mode */
734 if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) {
735 log_debug ("ElGamal (E+S) key found: %s (%s)\n",
736 key->uids->name, key->subkeys->keyid);
737 return 0;
738 }
739
740 if (listview_add_item2 (lv, " ", (void *)ctx))
741 return WPTERR_GENERAL;
742
743 attr = ctx->uids->uid;
744 memset (&lvi, 0, sizeof lvi);
745 lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
746 lvi.pszText = (char *)attr;
747 lvi.iImage = find_secret_key (key)? 1 : 0;
748 lvi.lParam = (LPARAM )ctx;
749 if (ListView_SetItem (lv->ctrl, &lvi) == FALSE)
750 return WPTERR_GENERAL;
751
752 if (uididx == -1) { /* request the primary user-id of the key. */
753 attr = ctx->uids->uid;
754 uididx = 0;
755 }
756 else {
757 u = get_nth_userid (key, uididx);
758 if (!u || u->revoked || uididx < 0)
759 uididx = 0;
760 u = get_nth_userid (key, uididx);
761 attr = u->uid;
762 }
763 if (attr == NULL || strlen (attr) < 5) { /* normal userids are > 5 chars */
764 attr = _("Invalid User ID");
765 listview_add_sub_item (lv, 0, idx++, attr);
766 }
767 else
768 listview_add_sub_item (lv, 0, idx++, attr);
769 k = get_nth_key (key, keyidx);
770 if (k && k->keyid) {
771 _snprintf (fmt, sizeof fmt -1, "0x%s", k->keyid + 8);
772 listview_add_sub_item (lv, 0, idx++, fmt);
773 }
774 if (list > 0) {
775 key_attr = find_secret_key (key);
776 if (!key_attr)
777 attr = "pub";
778 else
779 attr = key_attr == 1? "pub/sec" : "pub/crd";
780 listview_add_sub_item (lv, 0, idx++, attr);
781 }
782 if (lv->cols >= 2) {
783 attr = get_key_size (key, list == -1? keyidx+1 : 0);
784 if (attr)
785 listview_add_sub_item (lv, 0, idx++, attr);
786 }
787 if (lv->cols >= 3) {
788 attr = get_key_algo (key, list == -1? keyidx+1 : 0);
789 if (attr)
790 listview_add_sub_item( lv, 0, idx++, attr);
791 }
792 if (lv->cols >= 4) {
793 p = get_key_status( key, uididx, list > 0? 1 : 0 );
794 if (!p)
795 return WPTERR_GENERAL;
796 listview_add_sub_item (lv, 0, idx++, p);
797 free_if_alloc (p);
798 }
799 if (lv->cols >= 5) {
800 attr = get_key_trust (key, uididx, list > 0? 1 : 0);
801 listview_add_sub_item (lv, 0, idx++, attr);
802 }
803 if( lv->cols >= 6 ) {
804 k = get_nth_key (key, keyidx);
805 key_attr = k->timestamp;
806 if( key_attr ) {
807 attr = get_key_created (key_attr);
808 listview_add_sub_item( lv, 0, idx++, attr );
809 }
810 }
811
812 return 0;
813 }
814
815
816 /* Update a single column @col but for each element in the
817 listview @lv. */
818 void
819 keylist_upd_col (listview_ctrl_t lv, int col)
820 {
821 gpgme_key_t key;
822 const char *s;
823 char buf[32], *p;
824 int i;
825
826 for (i=0; i < listview_count_items (lv, 0); i++) {
827 key = km_get_key_ptr (lv, i, NULL);
828 if (!key)
829 continue;
830 switch (col) {
831 case KM_COL_KEYID:
832 _snprintf (buf, sizeof (buf)-1, "0x%s", key->subkeys->keyid+8);
833 listview_add_sub_item (lv, i, col, buf);
834 break;
835
836 case KM_COL_CIPHER:
837 s = get_key_algo (key, 0);
838 listview_add_sub_item (lv, i, col, s);
839 break;
840
841 case KM_COL_TYPE:
842 s = find_secret_key (key)? "pub/sec" : "pub";
843 listview_add_sub_item (lv, i, col, s);
844 break;
845
846 case KM_COL_CREAT:
847 s = get_key_created (key->subkeys->timestamp);
848 listview_add_sub_item (lv, i, col, s);
849 break;
850
851 case KM_COL_DESC:
852 p = get_key_desc (key);
853 listview_add_sub_item (lv, i, col, p);
854 free_if_alloc (p);
855 break;
856 }
857 }
858 }
859
860
861 /* Update the listview item at position @pos with the data from
862 the key @key. */
863 void
864 keylist_upd_key (listview_ctrl_t lv, int pos,
865 struct keycache_s *ctx, gpgme_key_t key)
866 {
867 const char *s;
868 char *p;
869 char tmp[32];
870
871 listview_set_item2 (lv, pos, (void *)ctx);
872 /* the only mode we support is KEYLIST_LIST in the Key Manager */
873
874 s = ctx->uids->uid;
875 if (s)
876 listview_add_sub_item (lv, pos, KM_COL_UID, s);
877
878 s = key->subkeys->keyid;
879 if (s) {
880 sprintf (tmp, "0x%s", s+8);
881 listview_add_sub_item (lv, pos, KM_COL_KEYID, tmp);
882 }
883
884 s = find_secret_key (key)? "pub/sec" : "pub";
885 listview_add_sub_item (lv, pos, KM_COL_TYPE, s);
886
887 s = get_key_size (key, 0);
888 if (s)
889 listview_add_sub_item (lv, pos, KM_COL_SIZE, s);
890
891 s = get_key_algo (key, 0);
892 if (s)
893 listview_add_sub_item (lv, pos, KM_COL_CIPHER, s);
894
895 p = get_key_status (key, 0, 1);
896 if (p) {
897 listview_add_sub_item (lv, pos, KM_COL_VALID, p);
898 free_if_alloc (p);
899 }
900
901 s = get_key_trust (key, 0, 1);
902 if (s)
903 listview_add_sub_item (lv, pos, KM_COL_TRUST, s);
904
905 long t = key->subkeys->timestamp;
906 s = get_key_created (t);
907 if (s)
908 listview_add_sub_item (lv, pos, KM_COL_CREAT, s);
909 }
910
911
912 int
913 keylist_add_key (listview_ctrl_t lv, int mode,
914 struct keycache_s *ctx, gpgme_key_t key)
915 {
916 int uids, rc = 0, i;
917 gpgme_subkey_t k;
918
919 /* if the entire key is disabled, just return. */
920 if (key->disabled && !(mode & KEYLIST_LIST))
921 return 0;
922
923 for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) {
924 if (k->invalid) {
925 log_debug ("keylist_add_key: invalid key \"%s\"\n", key->uids->name);
926 continue; /* Don't use invalid keys */
927 }
928
929 if (mode & KEYLIST_ALL) {
930 uids = count_userids (key);
931 rc = do_addkey (lv, ctx, key, uids, i, 0);
932 if (rc)
933 return rc;
934 }
935 else if (mode & KEYLIST_LIST)
936 return do_addkey (lv, ctx, key, -1, i, 1);
937 else if (mode & KEYLIST_ENCRYPT) {
938 if (k->can_encrypt && key_is_useable (k)) {
939 if (mode & KEYLIST_FLAG_FILE) {
940 rc = do_addkey (lv, ctx, key, -1, i, -1);
941 if (rc)
942 return rc;
943 }
944 else {
945 for (uids = 0; uids < count_userids (key); uids++) {
946 rc = do_addkey (lv, ctx, key, uids, i, -1);
947 if (rc)
948 return rc;
949 }
950 }
951 }
952 }
953 else if (mode & KEYLIST_ENCRYPT_MIN) {
954 if( k->can_encrypt && key_is_useable (k))
955 {
956 rc = do_addkey (lv, ctx, key, -1, i, -1);
957 return rc;
958 }
959 }
960 else if (mode & KEYLIST_SIGN) {
961 if (k->can_sign
962 && find_secret_key (key)
963 && key_is_useable (k)) {
964 rc = do_addkey (lv, ctx, key, -1, i, -1);
965 if (rc)
966 return rc;
967 }
968 }
969 }
970
971 return rc;
972 }
973
974
975 int
976 keylist_sort (listview_ctrl_t lv, int sortby)
977 {
978 return listview_sort_items (lv, sortby, keylist_cmp_cb);
979 }
980
981
982 /* Check that the validity @validity is at least >= marginal. */
983 static int
984 key_check_validity (gpgme_key_t key)
985 {
986 gpgme_user_id_t u;
987
988 for (u=key->uids; u; u =u->next) {
989 if (u->validity >= GPGME_VALIDITY_MARGINAL)
990 return -1;
991 }
992
993 return 0;
994 }
995
996
997 /* Extract all selected recipients from the list @lv and return them
998 as a vector. @r_force_trust is >= 1 if one of the recipients is not
999 fully trusted. @r_count returns the number of selected keys.
1000 Return value: the key list on success, NULL otherwise. */
1001 gpgme_key_t*
1002 keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count)
1003 {
1004 key_array_s *ka = NULL;
1005 keycache_s *c;
1006 gpgme_key_t *keybuf, key;
1007 int count = 0, force_trust = 0;
1008 int n, j, ka_pos = 0, rc = 0;
1009 int k_pos=0;
1010
1011 n = listview_count_items (lv, 0);
1012
1013 ka = key_array_new (n);
1014 if (!ka)
1015 BUG (NULL);
1016
1017 keybuf = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t));
1018 if (!keybuf)
1019 BUG (NULL);
1020
1021 for (j = 0; j < n; j++) {
1022 if (listview_get_item_state (lv, j) || n == 1) {
1023 key = km_get_key_ptr (lv, j, &c);
1024 if (!key)
1025 BUG (0);
1026 if (!key_check_validity (key) &&
1027 !key_array_search (ka, ka_pos, key->subkeys->keyid)) {
1028 char *warn = new char[512+strlen (c->uids->uid) + 1];
1029 if (!warn)
1030 BUG (0);
1031 sprintf (warn,
1032 _("It is NOT certain that the key belongs to the person\n"
1033 "named in the user ID. If you *really* know what you are\n"
1034 "doing, you may answer the next question with yes\n"
1035 "\n"
1036 "Use \"%s\" anyway?"), c->uids->uid);
1037 if (reg_prefs.always_trust)
1038 rc = IDYES;
1039 else
1040 rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);
1041 if (rc == IDYES) {
1042 keybuf[k_pos++] = key;
1043 force_trust++;
1044 ka[ka_pos].checked = 1;
1045 strcpy (ka[ka_pos++].keyid, key->subkeys->keyid);
1046 count++;
1047 }
1048 free_if_alloc (warn);
1049 }
1050 else {
1051 keybuf[k_pos++] = key;
1052 count++;
1053 }
1054 }
1055 }
1056 key_array_release (ka);
1057 if (r_force_trust)
1058 *r_force_trust = force_trust;
1059 if (r_count)
1060 *r_count = count;
1061 return keybuf;
1062 }
1063
1064
1065 static int
1066 keylist_get_keyflags (gpgme_key_t key)
1067 {
1068 int flags = KEYFLAG_NONE;
1069
1070 if (key->revoked)
1071 flags |= KEYFLAG_REVOKED;
1072 if (key->expired)
1073 flags |= KEYFLAG_EXPIRED;
1074 if (key->disabled)
1075 flags |= KEYFLAG_DISABLED;
1076
1077 return flags;
1078 }
1079
1080
1081 gpgme_key_t*
1082 keylist_enum_recipients (listview_ctrl_t lv, int listype, int *r_count)
1083 {
1084 struct keycache_s *c;
1085 gpgme_key_t *rset;
1086 gpgme_key_t key;
1087 int i, n, id, k_pos=0;
1088
1089 n = listview_count_items (lv, 0);
1090 if (!n)
1091 return 0;
1092 rset = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t));
1093 if (!rset)
1094 BUG (NULL);
1095 for (i = 0; i < n; i++) {
1096 if (!listview_get_item_state (lv, i))
1097 continue;
1098 key = km_get_key_ptr (lv, i, &c);
1099 switch (listype) {
1100 case KEYLIST_LIST:
1101 if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) {
1102 id = printf_box (_("Recipients"), MB_INFO|MB_YESNO,
1103 _("KeyID %s.\nDo you really want to export a revoked key?"),
1104 c->uids->uid);
1105 if (id == IDNO)
1106 continue;
1107 }
1108 break;
1109 }
1110 rset[k_pos++] = key;
1111 }
1112 if (r_count)
1113 *r_count = k_pos;
1114 return rset;
1115 }
1116
1117
1118 void
1119 seclist_destroy (keylist_t *list)
1120 {
1121 keylist_t l2;
1122 while (*list) {
1123 l2 = (*list)->next;
1124 safe_free (*list);
1125 *list = l2;
1126 }
1127 list = NULL;
1128 }
1129
1130
1131 void
1132 seclist_init (HWND dlg, int ctlid, int flags, keylist_t * ret_list)
1133 {
1134 gpg_keycache_t kc = NULL;
1135 gpgme_key_t key = NULL;
1136 HWND kb;
1137 keylist_t list=NULL, l, l2;
1138 long pos = 0;
1139
1140 SendDlgItemMessage (dlg, ctlid, CB_RESETCONTENT, 0, 0);
1141 kb = GetDlgItem (dlg, ctlid);
1142 kc = keycache_get_ctx (0);
1143 if (!kc)
1144 BUG (0);
1145 gpg_keycache_rewind (kc);
1146
1147 while (!gpg_keycache_next_key (kc, 1, &key)) {
1148 char *inf = NULL, *uid = NULL;
1149 const char *id;
1150 const char *keyid;
1151 int algo;
1152 size_t size = 0;
1153
1154 if (flags & KEYLIST_FLAG_SHORT)
1155 id = key->uids->name;
1156 else
1157 id = key->uids->uid;
1158 keyid = key->subkeys->keyid;
1159 algo = key->subkeys->pubkey_algo;
1160 if (!id || !keyid)
1161 continue;
1162 if (key->disabled || !key_is_useable (key->subkeys))
1163 continue;
1164
1165 uid = utf8_to_native (id);
1166 size = strlen (uid) + strlen (keyid) + 32;
1167 inf = new char[size+1];
1168 if (!inf)
1169 BUG (NULL);
1170 _snprintf (inf, size, "%s (%s/0x%s)", uid,
1171 get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8);
1172 combox_add_string (kb, inf);
1173 free_if_alloc (inf);
1174 free (uid);
1175 l = (struct keylist_s *)calloc (1, sizeof * l);
1176 if (!l)
1177 BUG (0);
1178 l->key = key;
1179 if (!list)
1180 list = l;
1181 else {
1182 for( l2 = list; l2->next; l2 = l2->next )
1183 ;
1184 l2->next = l;
1185 }
1186 }
1187 for (pos = 0, l2=list; pos < SendMessage (kb, CB_GETCOUNT, 0, 0);
1188 pos++, l2=l2->next)
1189 SendMessage (kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key);
1190 SendMessage (kb, CB_SETCURSEL, 0, 0);
1191 *ret_list = list;
1192 }
1193
1194
1195 /* Select a secret key from the combo box with the ID @ctlid.
1196 Return the code on success in @ret_key. */
1197 int
1198 seclist_select_key (HWND dlg, int ctlid, gpgme_key_t *ret_key)
1199 {
1200 int pos;
1201 DWORD k = 0;
1202
1203 pos = SendDlgItemMessage (dlg, ctlid, CB_GETCURSEL, 0, 0);
1204 if (pos == CB_ERR) {
1205 msg_box (dlg, _("No key was selected."), _("Secret Key List"), MB_ERR);
1206 *ret_key = NULL;
1207 }
1208 else {
1209 k = SendDlgItemMessage (dlg, ctlid, CB_GETITEMDATA, pos, 0);
1210 *ret_key = (gpgme_key_t)k;
1211 }
1212 return k? 0 : -1;
1213 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26