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

Legend:
Removed from v.24  
changed lines
  Added in v.80

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26