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

Diff of /trunk/Src/wptKeylist.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26