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

Contents of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations)
Thu Oct 20 12:35:59 2005 UTC (19 years, 4 months ago) by twoaday
File size: 26282 byte(s)
Minor cleanups and prepare the translation.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26