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

Annotation of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (hide annotations)
Wed Nov 2 08:32:55 2005 UTC (19 years, 4 months ago) by twoaday
File size: 25661 byte(s)
Simply code and use standard GNU file names
s/README.txt/README

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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26