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

Annotation of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (hide annotations)
Sun Nov 5 08:57:45 2006 UTC (18 years, 3 months ago) by twoaday
File size: 27500 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26