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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26