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

Diff of /trunk/Src/wptKeylist.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26