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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26