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

Diff of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.32  
changed lines
  Added in v.205

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26