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

Contents of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (show annotations)
Wed Oct 12 10:04:26 2005 UTC (19 years, 4 months ago) by twoaday
File size: 26175 byte(s)
First testing phase finished.
Provide bug fixes for a lot of (minor) problems.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26