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

Legend:
Removed from v.28  
changed lines
  Added in v.167

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26