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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26