/[winpt]/trunk/Src/wptKeylist.cpp
ViewVC logotype

Diff of /trunk/Src/wptKeylist.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.32  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26