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

Diff of /trunk/Src/wptKeylist.cpp

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26