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

Annotation of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (hide annotations)
Wed Apr 26 06:37:23 2006 UTC (18 years, 10 months ago) by twoaday
File size: 26643 byte(s)
2006-04-25  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptFileManagerDlg.cpp (file_manager_dlg_proc): Handle IDCANCEL.
        * wptMDSumDlg.cpp (hash_selected_files): New.
        (mdsum_dlg_proc): Cleanups.
        * wptKeysignDlg.cpp (keysign_dlg_proc): Fix segv.
                                                                                
2006-04-23  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptW32API.cpp (SetDlgItemText_utf8): New.
        (SetWindowText_utf8): New.
        (GetDlgItemText_utf8): New.
        * wptListView.cpp (listview_add_sub_item): Internal
        utf8 support.


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 twoaday 204 else
737     listview_add_sub_item (lv, 0, idx++, attr);
738 werner 36 k = get_nth_key (key, keyidx);
739     if (k && k->keyid) {
740     _snprintf (fmt, sizeof fmt -1, "0x%s", k->keyid + 8);
741     listview_add_sub_item( lv, 0, idx++, fmt );
742     }
743     if (list > 0) {
744     key_attr = find_secret_key (key);
745     if (!key_attr)
746     attr = "pub";
747     else
748     attr = key_attr == 1? "pub/sec" : "pub/crd";
749     listview_add_sub_item (lv, 0, idx++, attr);
750     }
751     if (lv->cols >= 2) {
752     attr = get_key_size (key, list == -1? keyidx+1 : 0);
753     if (attr)
754     listview_add_sub_item (lv, 0, idx++, attr);
755     }
756     if (lv->cols >= 3) {
757     attr = get_key_algo (key, list == -1? keyidx+1 : 0);
758     if (attr)
759     listview_add_sub_item( lv, 0, idx++, attr);
760     }
761 twoaday 167 if (lv->cols >= 4) {
762 werner 36 p = get_key_status( key, uididx, list > 0? 1 : 0 );
763     if (!p)
764     return WPTERR_GENERAL;
765     listview_add_sub_item (lv, 0, idx++, p);
766     free_if_alloc (p);
767     }
768     if (lv->cols >= 5) {
769     attr = get_key_trust (key, uididx, list > 0? 1 : 0);
770     listview_add_sub_item (lv, 0, idx++, attr);
771     }
772     if( lv->cols >= 6 ) {
773     k = get_nth_key (key, keyidx);
774     key_attr = k->timestamp;
775     if( key_attr ) {
776     attr = get_key_created (key_attr);
777     listview_add_sub_item( lv, 0, idx++, attr );
778     }
779     }
780    
781     return 0;
782     }
783    
784    
785 twoaday 129 /* Update a single column @col but for each element in the
786     listview @lv. */
787 werner 36 void
788 twoaday 129 keylist_upd_col (listview_ctrl_t lv, int col)
789     {
790     gpgme_key_t key;
791     const char *s;
792 twoaday 167 char buf[32], *p;
793 twoaday 129 int i;
794    
795     for (i=0; i < listview_count_items (lv, 0); i++) {
796     key = (gpgme_key_t)listview_get_item2 (lv, i);
797     if (!key)
798     continue;
799     switch (col) {
800     case KM_COL_KEYID:
801     _snprintf (buf, sizeof (buf)-1, "0x%s", key->subkeys->keyid+8);
802     listview_add_sub_item (lv, i, col, buf);
803     break;
804    
805     case KM_COL_CIPHER:
806     s = get_key_algo (key, 0);
807     listview_add_sub_item (lv, i, col, s);
808     break;
809    
810     case KM_COL_TYPE:
811     s = find_secret_key (key)? "pub/sec" : "pub";
812     listview_add_sub_item (lv, i, col, s);
813     break;
814    
815     case KM_COL_CREAT:
816     s = get_key_created (key->subkeys->timestamp);
817     listview_add_sub_item (lv, i, col, s);
818     break;
819 twoaday 167
820     case KM_COL_DESC:
821     p = get_key_desc (key);
822     listview_add_sub_item (lv, i, col, p);
823     free_if_alloc (p);
824     break;
825 twoaday 129 }
826     }
827     }
828    
829 twoaday 133
830 twoaday 129 /* Update the listview item at position @pos with the data from
831     the key @key. */
832     void
833 werner 36 keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key)
834     {
835     const char *s;
836 twoaday 204 char *p;
837 werner 36 char tmp[32];
838    
839     listview_set_item2 (lv, pos, (void *)key);
840     /* the only mode we support is KYLIST_LIST in the Key Manager */
841    
842     s = key->uids->uid;
843 twoaday 204 if (s)
844     listview_add_sub_item (lv, pos, KM_COL_UID, s);
845 werner 36
846     s = key->subkeys->keyid;
847     if (s) {
848     sprintf (tmp, "0x%s", s+8);
849 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_KEYID, tmp);
850 werner 36 }
851    
852     s = find_secret_key (key)? "pub/sec" : "pub";
853 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_TYPE, s);
854 werner 36
855     s = get_key_size (key, 0);
856     if (s)
857 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_SIZE, s);
858 werner 36
859     s = get_key_algo (key, 0);
860     if (s)
861 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_CIPHER, s);
862 werner 36
863 twoaday 167 p = get_key_status (key, 0, 1);
864     if (p) {
865     listview_add_sub_item (lv, pos, KM_COL_VALID, p);
866     free_if_alloc (p);
867     }
868 werner 36
869     s = get_key_trust (key, 0, 1);
870     if (s)
871 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_TRUST, s);
872 werner 36
873     long t = key->subkeys->timestamp;
874     s = get_key_created (t);
875     if (s)
876 twoaday 167 listview_add_sub_item (lv, pos, KM_COL_CREAT, s);
877 werner 36 }
878    
879    
880     int
881     keylist_add_key (listview_ctrl_t lv, int mode, gpgme_key_t key)
882     {
883 twoaday 73 int uids, rc = 0, i;
884 werner 36 gpgme_subkey_t k;
885    
886 twoaday 41 /* if the entire key is disabled, just return. */
887 twoaday 80 if (key->disabled && !(mode & KEYLIST_LIST))
888 twoaday 41 return 0;
889    
890 werner 36 for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) {
891     if (k->invalid) {
892     log_debug ("keylist_add_key: invalid key \"%s\"\n", key->uids->name);
893     continue; /* Don't use invalid keys */
894     }
895    
896     if (mode & KEYLIST_ALL) {
897     uids = count_userids (key);
898     rc = do_addkey (lv, key, uids, i, 0);
899 twoaday 128 if (rc)
900 werner 36 return rc;
901     }
902     else if (mode & KEYLIST_LIST)
903     return do_addkey (lv, key, -1, i, 1);
904     else if (mode & KEYLIST_ENCRYPT) {
905     if (k->can_encrypt && key_is_useable (k)) {
906     if (mode & KEYLIST_FLAG_FILE) {
907     rc = do_addkey (lv, key, -1, i, -1);
908     if (rc)
909     return rc;
910     }
911     else {
912 twoaday 128 for (uids = 0; uids < count_userids (key); uids++) {
913     rc = do_addkey (lv, key, uids, i, -1);
914     if (rc)
915 werner 36 return rc;
916     }
917     }
918     }
919     }
920     else if (mode & KEYLIST_ENCRYPT_MIN) {
921     if( k->can_encrypt && key_is_useable (k))
922     {
923     rc = do_addkey (lv, key, -1, i, -1);
924     return rc;
925     }
926     }
927     else if (mode & KEYLIST_SIGN) {
928 twoaday 41 if (k->can_sign
929     && find_secret_key (key)
930     && key_is_useable (k)) {
931 werner 36 rc = do_addkey (lv, key, -1, i, -1);
932 twoaday 41 if (rc)
933 werner 36 return rc;
934     }
935     }
936     }
937    
938     return rc;
939 twoaday 133 }
940 werner 36
941    
942     int
943     keylist_sort (listview_ctrl_t lv, int sortby)
944 twoaday 150 {
945 werner 36 return listview_sort_items (lv, sortby, keylist_cmp_cb);
946     }
947    
948    
949     /* Check that the validity @validity is at least >= marginal. */
950     static int
951 twoaday 133 key_check_validity (gpgme_key_t key)
952     {
953     gpgme_user_id_t u;
954    
955     for (u=key->uids; u; u =u->next) {
956     if (u->validity >= GPGME_VALIDITY_MARGINAL)
957     return -1;
958     }
959    
960     return 0;
961 werner 36 }
962    
963    
964     /* Extract all selected recipients from the list @lv and return them
965     as a vector. @r_force_trust is >= 1 if one of the recipients is not
966     fully trusted. @r_count returns the number of selected keys.
967     Return value: the key list on success, NULL otherwise. */
968     gpgme_key_t*
969     keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count)
970     {
971 twoaday 133 key_array_s *ka = NULL;
972     gpgme_key_t *keybuf, key;
973 werner 36 int count = 0, force_trust = 0;
974     int n, j, ka_pos = 0, rc = 0;
975     int k_pos=0;
976    
977 twoaday 133 n = listview_count_items (lv, 0);
978 werner 36
979 twoaday 133 ka = key_array_new (n);
980 werner 36 if (!ka)
981     BUG (NULL);
982    
983 twoaday 197 keybuf = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t));
984 werner 36 if (!keybuf)
985     BUG (NULL);
986    
987 twoaday 133 for (j = 0; j < n; j++) {
988     if (listview_get_item_state (lv, j) || n == 1) {
989     key = (gpgme_key_t)listview_get_item2 (lv, j);
990     if (!key)
991     BUG (0);
992     if (!key_check_validity (key) &&
993     !key_array_search (ka, ka_pos, key->subkeys->keyid)) {
994 twoaday 197 char *utf8_uid = utf8_to_native (key->uids->uid);
995     char *warn = new char[512+strlen (utf8_uid) + 1];
996 werner 36 if (!warn)
997     BUG (0);
998     sprintf (warn,
999     _("It is NOT certain that the key belongs to the person\n"
1000     "named in the user ID. If you *really* know what you are\n"
1001     "doing, you may answer the next question with yes\n"
1002     "\n"
1003 twoaday 197 "Use \"%s\" anyway?"), utf8_uid);
1004 werner 36 if (reg_prefs.always_trust)
1005     rc = IDYES;
1006     else
1007     rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);
1008     if (rc == IDYES) {
1009 twoaday 133 keybuf[k_pos++] = key;
1010 werner 36 force_trust++;
1011     ka[ka_pos].checked = 1;
1012 twoaday 133 strcpy (ka[ka_pos++].keyid, key->subkeys->keyid);
1013 werner 36 count++;
1014     }
1015 twoaday 197 safe_free (utf8_uid);
1016 werner 36 free_if_alloc (warn);
1017     }
1018     else {
1019 twoaday 133 keybuf[k_pos++] = key;
1020 werner 36 count++;
1021     }
1022     }
1023     }
1024     key_array_release (ka);
1025     if (r_force_trust)
1026     *r_force_trust = force_trust;
1027     if (r_count)
1028     *r_count = count;
1029     return keybuf;
1030     }
1031    
1032    
1033     static int
1034 twoaday 133 keylist_get_keyflags (gpgme_key_t key)
1035 werner 36 {
1036 twoaday 133 int flags = KEYFLAG_NONE;
1037 werner 36
1038 twoaday 133 if (key->revoked)
1039     flags |= KEYFLAG_REVOKED;
1040     if (key->expired)
1041     flags |= KEYFLAG_EXPIRED;
1042     if (key->disabled)
1043     flags |= KEYFLAG_DISABLED;
1044 werner 36
1045     return flags;
1046 twoaday 133 }
1047 werner 36
1048    
1049     gpgme_key_t*
1050     keylist_enum_recipients (listview_ctrl_t lv, int listype, int *r_count)
1051     {
1052 twoaday 133 gpgme_key_t *rset;
1053     gpgme_key_t key;
1054 werner 36 int i, n, id, k_pos=0;
1055    
1056     n = listview_count_items (lv, 0);
1057     if (!n)
1058     return 0;
1059 twoaday 197 rset = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t));
1060 werner 36 if (!rset)
1061     BUG (NULL);
1062 twoaday 133 for (i = 0; i < n; i++) {
1063     if (!listview_get_item_state (lv, i))
1064 werner 36 continue;
1065 twoaday 133 key = (gpgme_key_t)listview_get_item2 (lv, i);
1066     if (!key)
1067     BUG (0);
1068     switch (listype) {
1069 werner 36 case KEYLIST_LIST:
1070 twoaday 133 if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) {
1071     id = printf_box (_("Recipients"), MB_INFO|MB_YESNO,
1072     _("KeyID %s.\nDo you really want to export a revoked key?"),
1073     key->uids->uid);
1074     if (id == IDNO)
1075     continue;
1076 werner 36 }
1077     break;
1078     }
1079 twoaday 133 rset[k_pos++] = key;
1080 werner 36 }
1081     if (r_count)
1082     *r_count = k_pos;
1083     return rset;
1084 twoaday 133 }
1085 werner 36
1086    
1087     void
1088 twoaday 133 seclist_destroy (keylist_t *list)
1089 werner 36 {
1090     keylist_t l2;
1091     while (*list) {
1092     l2 = (*list)->next;
1093     safe_free (*list);
1094     *list = l2;
1095     }
1096     list = NULL;
1097 twoaday 133 }
1098 werner 36
1099    
1100     void
1101     seclist_init (HWND dlg, int ctlid, int flags, keylist_t * ret_list)
1102     {
1103     gpg_keycache_t kc = NULL;
1104     gpgme_key_t key = NULL;
1105     HWND kb;
1106     keylist_t list=NULL, l, l2;
1107     long pos = 0;
1108    
1109     SendDlgItemMessage (dlg, ctlid, CB_RESETCONTENT, 0, 0);
1110     kb = GetDlgItem (dlg, ctlid);
1111     kc = keycache_get_ctx (0);
1112     if (!kc)
1113     BUG (0);
1114     gpg_keycache_rewind (kc);
1115    
1116     while (!gpg_keycache_next_key (kc, 1, &key)) {
1117 twoaday 41 char *inf = NULL, *uid = NULL;
1118     const char *id;
1119     const char *keyid;
1120 werner 36 int algo;
1121     size_t size = 0;
1122    
1123     if (flags & KEYLIST_FLAG_SHORT)
1124     id = key->uids->name;
1125     else
1126     id = key->uids->uid;
1127     keyid = key->subkeys->keyid;
1128     algo = key->subkeys->pubkey_algo;
1129     if (!id || !keyid)
1130     continue;
1131 twoaday 41 if (key->disabled || !key_is_useable (key->subkeys))
1132     continue;
1133 werner 36
1134 twoaday 187 uid = utf8_to_native (id);
1135 twoaday 133 size = strlen (uid) + strlen (keyid) + 32;
1136 werner 36 inf = new char[size+1];
1137 twoaday 133 if (!inf)
1138     BUG (NULL);
1139     _snprintf (inf, size, "%s (%s/0x%s)", uid,
1140 werner 36 get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8);
1141     combox_add_string (kb, inf);
1142     free_if_alloc (inf);
1143     free (uid);
1144     l = (struct keylist_s *)calloc (1, sizeof * l);
1145     if (!l)
1146     BUG (0);
1147     l->key = key;
1148     if (!list)
1149     list = l;
1150     else {
1151     for( l2 = list; l2->next; l2 = l2->next )
1152     ;
1153     l2->next = l;
1154     }
1155     }
1156 twoaday 133 for (pos = 0, l2=list; pos < SendMessage (kb, CB_GETCOUNT, 0, 0);
1157     pos++, l2=l2->next)
1158     SendMessage (kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key);
1159     SendMessage (kb, CB_SETCURSEL, 0, 0);
1160 werner 36 *ret_list = list;
1161     }
1162    
1163    
1164     /* Select a secret key from the combo box with the ID @ctlid.
1165     Return the code on success in @ret_key. */
1166     int
1167     seclist_select_key (HWND dlg, int ctlid, gpgme_key_t *ret_key)
1168     {
1169     int pos;
1170     DWORD k = 0;
1171    
1172     pos = SendDlgItemMessage (dlg, ctlid, CB_GETCURSEL, 0, 0);
1173     if (pos == CB_ERR) {
1174     msg_box (dlg, _("No key was selected."), _("Secret Key List"), MB_ERR);
1175     *ret_key = NULL;
1176     }
1177     else {
1178     k = SendDlgItemMessage (dlg, ctlid, CB_GETITEMDATA, pos, 0);
1179     *ret_key = (gpgme_key_t)k;
1180     }
1181     return k? 0 : -1;
1182     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26