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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26