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

Annotation of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (hide annotations)
Sat Apr 22 18:30:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 26777 byte(s)
See ChangeLog.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26