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

Annotation of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Fri Apr 28 10:28:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 27332 byte(s)
Bug fixes and cleanups.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26