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

Legend:
Removed from v.25  
changed lines
  Added in v.207

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26