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

Legend:
Removed from v.25  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26