/[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 20 by twoaday, Wed Jul 27 11:17:22 2005 UTC revision 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC
# Line 1  Line 1 
1  /* wptKeylist.cpp - Keylist element  /* wptKeylist.cpp - Keylist element
2   *      Copyright (C) 2001-2004 Timo Schulz   *      Copyright (C) 2001-2005 Timo Schulz
3   *      Copyright (C) 2004 Andreas Jobs   *      Copyright (C) 2004 Andreas Jobs
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# Line 18  Line 18 
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>  #include <windows.h>
22  #include <commctrl.h>  #include <commctrl.h>
23  #include <time.h>  #include <time.h>
# Line 33  Line 32 
32  #include "wptErrors.h"  #include "wptErrors.h"
33  #include "wptUTF8.h"  #include "wptUTF8.h"
34  #include "wptRegistry.h"  #include "wptRegistry.h"
35    #include "wptContext.h"
36    
37    
38    #define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled)
39    
40  static struct listview_column_s klist_enc[] = {  static struct listview_column_s klist_enc[] = {
41      {0, 242, (char *)_("User ID")},      {0, 242, (char *)_("User ID")},
# Line 104  key_array_search( key_array_s *ka, size_ Line 107  key_array_search( key_array_s *ka, size_
107  } /* key_array_search */  } /* key_array_search */
108    
109    
110    gpgme_user_id_t
111    get_nth_userid (gpgme_key_t key, int idx)
112    {
113        gpgme_user_id_t t;
114    
115        if (!key->uids)
116            return NULL;
117        t = key->uids;
118        while (idx-- && t->next)
119            t = t->next;
120        return t;
121    }
122    
123    
124    int
125    count_userids (gpgme_key_t key)
126    {
127        gpgme_user_id_t u;
128        int n = 1;
129    
130        u = key->uids;
131        if (!u)
132            return 0;
133        while (u->next) {
134            u = u->next;
135            n++;
136        }
137        return n;
138    }
139    
140    
141    gpgme_subkey_t
142    get_nth_key (gpgme_key_t key, int idx)
143    {
144        gpgme_subkey_t t;
145    
146        if (!key->subkeys)
147            return NULL;
148        t = key->subkeys;
149        while (idx-- && t->next)
150            t = t->next;
151        return t;
152    }
153    
154    int
155    count_subkeys (gpgme_key_t key)
156    {
157        gpgme_subkey_t k;
158        int n = 1;
159    
160        k = key->subkeys;
161        if (!k)
162            return 0;
163        while (k->next) {
164            k = k->next;
165            n++;
166        }
167        return n;
168    }
169    
170    
171    gpgme_key_sig_t
172    get_selfsig (gpgme_user_id_t uid, const char *keyid, int first)
173    {
174        gpgme_key_sig_t s, self_sig=NULL;
175        long timestamp=0;
176    
177        for (s = uid->signatures; s; s = s->next) {
178            if (!strcmp (s->keyid+8, keyid) && s->timestamp > timestamp) {
179                self_sig = s;
180                timestamp = s->timestamp;
181                if (first)
182                    break;
183            }
184        }
185        return self_sig;
186    }
187    
188    
189    
190  const char*  const char*
191  get_key_algo( gpgme_key_t key, int keyidx )  get_key_algo (gpgme_key_t key, int keyidx)
192  {  {
193      static char algo_id[128];      static char algo_id[128];
194      int n, algo_main, algo_sub;      gpgme_subkey_t k;
195        char alg[32];
196        const char *subalg;
197        int n=0;
198            
199      if( keyidx > 0 ) {      if (keyidx > 0) {
200          algo_main = gpgme_key_get_ulong_attr( key, GPGME_ATTR_ALGO, NULL, keyidx-1 );          k = get_nth_key (key, keyidx-1);
201          _snprintf( algo_id, sizeof algo_id-1, "%s",          subalg =  get_key_pubalgo (k->pubkey_algo);
202              gpgme_key_expand_attr( GPGME_ATTR_ALGO, algo_main ) );          _snprintf( algo_id, DIM (algo_id)-1, "%s", subalg);
203          return algo_id;          return algo_id;
204      }      }
205      algo_main = gpgme_key_get_ulong_attr( key, GPGME_ATTR_ALGO, NULL, 0 );      strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo));
206      n = gpgme_key_count_items( key, GPGME_ATTR_KEYID );      n = count_subkeys (key);
207      if( n > 1 ) {      if (n > 1) {
208          algo_sub = gpgme_key_get_ulong_attr( key, GPGME_ATTR_ALGO, NULL, n-1 );          k = get_nth_key (key, n-1);
209          _snprintf( algo_id, sizeof algo_id - 1, "%s/%s",          subalg = get_key_pubalgo (k->pubkey_algo);
210              gpgme_key_expand_attr( GPGME_ATTR_ALGO, algo_main ),          _snprintf (algo_id, DIM (algo_id)-1, "%s/%s", alg, subalg);
             gpgme_key_expand_attr( GPGME_ATTR_ALGO, algo_sub ) );  
211          return algo_id;          return algo_id;
212      }      }
213      return gpgme_key_expand_attr( GPGME_ATTR_ALGO, algo_main );      return get_key_pubalgo (key->subkeys->pubkey_algo);
214  } /* get_key_algo */  } /* get_key_algo */
215    
216    
# Line 135  get_key_created( long timestamp ) Line 220  get_key_created( long timestamp )
220      static char timebuf[128];      static char timebuf[128];
221      struct tm *warp;      struct tm *warp;
222    
223      if( timestamp == 0 || timestamp == -1 )      if (timestamp == 0 || timestamp == -1)
224          return "????-??-??";          return "????-??-??";
225      warp = localtime( &timestamp );      warp = localtime( &timestamp );
226      _snprintf( timebuf, sizeof timebuf - 1, "%04d-%02d-%02d",      _snprintf( timebuf, sizeof timebuf - 1, "%04d-%02d-%02d",
# Line 145  get_key_created( long timestamp ) Line 230  get_key_created( long timestamp )
230    
231    
232  const char*  const char*
233  get_key_expire_date( long timestamp )  get_key_expire_date (long timestamp)
234  {  {
235      static char timebuf[64];      static char timebuf[64];
236      struct tm *warp;      struct tm *warp;
# Line 159  get_key_expire_date( long timestamp ) Line 244  get_key_expire_date( long timestamp )
244  } /* get_key_expire_date */  } /* get_key_expire_date */
245    
246    
247  const char *  const char*
248  get_key_type( gpgme_key_t key )  get_key_type (gpgme_key_t key)
249  {  {
250      int valid = gpgme_key_get_ulong_attr( key, GPGME_ATTR_VALIDITY, NULL, 0 );      if (find_secret_key (key))
     if( find_secret_key( key ) || valid == GPGME_VALIDITY_ULTIMATE )      
251          return _("Key Pair");          return _("Key Pair");
252      return _("Public Key");      return _("Public Key");
253  } /* get_key_type */  } /* get_key_type */
254    
255    
256  const char *  const char*
257  get_key_size( gpgme_key_t key, int keyidx )  get_key_size (gpgme_key_t key, int keyidx)
258  {  {
259      static char size_id[64];      static char size_id[64];
260        gpgme_subkey_t k;
261      int n, size_main, size_sub;      int n, size_main, size_sub;
262            
263      if( keyidx > 0 ) {      if (keyidx > 0) {
264          size_main = gpgme_key_get_ulong_attr( key, GPGME_ATTR_LEN, NULL, keyidx-1 );          k = get_nth_key (key, keyidx-1);
265          _snprintf( size_id, sizeof size_id-1, "%d", size_main );          size_main = k->length;
266            _snprintf (size_id, DIM (size_id)-1, "%d", size_main);
267          return size_id;          return size_id;
268      }      }
269      size_main = gpgme_key_get_ulong_attr( key, GPGME_ATTR_LEN, NULL, 0 );      size_main =  key->subkeys->length;
270      n = gpgme_key_count_items( key, GPGME_ATTR_KEYID );      n = count_subkeys (key);
271      if( n > 1 ) {          if (n > 1) {
272          size_sub = gpgme_key_get_ulong_attr( key, GPGME_ATTR_LEN, NULL, n-1 );          k = get_nth_key (key, n-1);
273            size_sub = k->length;
274          _snprintf( size_id, sizeof (size_id) - 1, "%d/%d", size_main, size_sub );          _snprintf( size_id, sizeof (size_id) - 1, "%d/%d", size_main, size_sub );
275          return size_id;          return size_id;
276      }      }
# Line 192  get_key_size( gpgme_key_t key, int keyid Line 279  get_key_size( gpgme_key_t key, int keyid
279  } /* get_key_size */  } /* get_key_size */
280    
281    
282    const char*
283    get_key_pubalgo (gpgme_pubkey_algo_t alg)
284    {
285        switch (alg) {
286        case GPGME_PK_DSA: return "DSA";
287        case GPGME_PK_ELG:
288        case GPGME_PK_ELG_E: return "ELG";
289        case GPGME_PK_RSA: return "RSA";
290        }
291        return "???";
292    }
293    
294  const char *  const char *
295  get_key_fpr( gpgme_key_t key )  get_key_fpr (gpgme_key_t key)
296  {  {
297      static char fpr_md[64];      static char fpr_md[64];
298      const char * fpr;      const char *fpr;
299      char t[16], tmp[40];      char t[16], tmp[40];
300      size_t i;      size_t i=0;
301            
302      memset (fpr_md, 0, sizeof (fpr_md));      memset (fpr_md, 0, sizeof (fpr_md));
303      fpr = gpgme_key_get_string_attr (key, GPGME_ATTR_FPR, NULL, 0);      fpr = key->subkeys->fpr;
304      if (!fpr || !*fpr) {      if (!fpr || !*fpr) {
305          memset (tmp, '0', 40);          memset (tmp, '0', 40);
306          fpr = tmp;          fpr = tmp;
307      }      }
308      for (i = 0; i < strlen (fpr) / 4; i++) {      if (strlen (fpr) == 32) {
309          sprintf (t, "%c%c%c%c ", fpr[4*i], fpr[4*i+1], fpr[4*i+2], fpr[4*i+3]);          strcat (fpr_md, "        ");
310          strcat (fpr_md, t);          for (i=0; i < strlen (fpr)/2; i++) {
311                sprintf (t, "%c%c ", fpr[2*i], fpr[2*i+1]);
312                strcat (fpr_md, t);
313            }
314        }
315        else {
316            strcat (fpr_md, " ");
317            for (i = 0; i < strlen (fpr) / 4; i++) {
318                sprintf (t, "%c%c%c%c ", fpr[4*i], fpr[4*i+1], fpr[4*i+2], fpr[4*i+3]);
319                strcat (fpr_md, t);
320            }
321      }      }
322      return fpr_md;      return fpr_md;
323  } /* get_key_fpr */  } /* get_key_fpr */
324    
325    
326    const char *
 static const char *  
327  get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode)  get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode)
328  {  {
329      if (key)      if (key)
330          val = gpgme_key_get_ulong_attr (key, GPGME_ATTR_OTRUST, NULL, uididx);          val = key->owner_trust; /* uididx?? */
331      switch (val) {      switch (val) {
332      case GPGME_TRUST_UNKNOWN:      case GPGME_VALIDITY_UNKNOWN:
333      case GPGME_TRUST_DONTKNOW:          case GPGME_VALIDITY_UNDEFINED:    
334          return "None";          return "None";
335      case GPGME_TRUST_NEVER:          case GPGME_VALIDITY_NEVER:    
336          return "Never";          return "Never";
337      case GPGME_TRUST_MARGINAL:      case GPGME_VALIDITY_MARGINAL:
338          return "Marginal";          return "Marginal";
339      case GPGME_TRUST_FULLY:      case GPGME_VALIDITY_FULL:
340      case GPGME_TRUST_ULTIMATE:      case GPGME_VALIDITY_ULTIMATE:
341          return "Full";          return "Full";
342      }      }
343      return "";      return "";
# Line 251  get_key_trust_str (int val) Line 359  get_key_trust_str (int val)
359    
360    
361  char*  char*
362  get_key_status( gpgme_key_t key, int uididx, int listmode )  get_key_status (gpgme_key_t key, int uididx, int listmode)
363  {  {
364      char fmt[128], * p;      gpgme_user_id_t u;
365        char fmt[64], * p;
366      const char * attr;      const char * attr;
367      int i = 0;      int i = 0;
368      u32 key_attr =0;      u32 key_attr =0;
369    
370      if( uididx < 0 || gpgme_key_count_items( key, GPGME_ATTR_USERID ) > uididx )      if (uididx < 0 || count_userids (key) > uididx)
371          uididx = 0;          uididx = 0;
372      memset( fmt, 0, sizeof fmt );      memset (fmt, 0, sizeof (fmt));
373      if( listmode ) {      if (listmode) {
374          fmt[i++] = '[';          if (key->revoked)
375          if( gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_REVOKED, NULL, 0 ) )              sprintf (fmt, "Revoked");
376              fmt[i++] = 'R';              else if (key->expired)
377          if( gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_EXPIRED, NULL, 0 ) )              sprintf (fmt, "Expired");
378              fmt[i++] = 'E';              else if (key->disabled)
379          if( gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_DISABLED, NULL, 0 ) )              sprintf (fmt, "Disabled");
380              fmt[i++] = 'D';          /* if the key has a special status, we don't continue to figure out
381          fmt[i++] = ']';             what any user-id validities. */
382          fmt[i++] = ' ';          if (strlen (fmt) > 0)
383      }              return m_strdup (fmt);
384      key_attr = gpgme_key_get_ulong_attr( key, GPGME_ATTR_VALIDITY, NULL, uididx );      }
385      attr = gpgme_key_expand_attr( GPGME_ATTR_VALIDITY, key_attr );      u = get_nth_userid (key, uididx);
386      p = new char[strlen( fmt ) + 1 + strlen( attr ) + 2];      key_attr = u->validity;
387      sprintf( p, "%s%s", fmt, attr );      attr = get_key_trust2 (NULL, key_attr, 0, 0);
388        p = new char[strlen( attr ) + 2];
389        if (!p)
390            BUG (NULL);
391        sprintf (p, "%s", attr);
392      return p;      return p;
393  } /* get_key_status */  } /* get_key_status */
394    
395    
396    /* Integer comparsion of @a and @b.
397       Return values: same as in strcmp. */
398  static inline int  static inline int
399  int_cmp( int a, int b )  int_cmp (int a, int b)
400  {  {
401      if( a == b ) return 0;            if (a == b) return 0;      
402      else if( a > b ) return 1;      else if (a > b) return 1;
403      else return -1;      else return -1;
404      return 0;      return 0;
405  }  }
406    
407    
408    /* List view sorting callback. */
409  static int CALLBACK  static int CALLBACK
410  keylist_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby)  keylist_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby)
411  {  {
     static char tmpa[128], tmpb[128];      
412      gpgme_key_t a, b;          gpgme_key_t a, b;    
     const char *aa = NULL, *bb = NULL;  
     long ta, tb;  
     int na = 0, nb = 0;  
413      int cmpresult = 0;      int cmpresult = 0;
414            
415      a = (gpgme_key_t)first;      a = (gpgme_key_t)first;
416      b = (gpgme_key_t)second;          b = (gpgme_key_t)second;
417        if (!a || !b)
418            BUG (NULL);
419            
420      switch( sortby & ~KEYLIST_SORT_DESC ) {      switch (sortby & ~KEYLIST_SORT_DESC) {
421      case GPGME_ATTR_USERID:      case KEY_SORT_USERID:
422          aa = gpgme_key_get_string_attr( a, GPGME_ATTR_USERID, NULL, 0 );          cmpresult = strcmpi (a->uids->uid, b->uids->uid);
         bb = gpgme_key_get_string_attr( b, GPGME_ATTR_USERID, NULL, 0 );  
         cmpresult = strcmpi( aa? aa : "", bb? bb : "" );  
423          break;          break;
424                    
425      case GPGME_ATTR_KEYID:      case KEY_SORT_KEYID:
426          aa = gpgme_key_get_string_attr( a, GPGME_ATTR_KEYID, NULL, 0) + 8;          cmpresult = strcmpi (a->subkeys->keyid+8,
427          bb = gpgme_key_get_string_attr( b, GPGME_ATTR_KEYID, NULL, 0) + 8;                               b->subkeys->keyid+8);
         cmpresult = strcmpi( aa? aa : "", bb? bb : "" );  
428          break;          break;
429    
430      case GPGME_ATTR_VALIDITY:      case KEY_SORT_VALIDITY: /* XXX: handle expire, revoked */
431          na = gpgme_key_get_ulong_attr( a, GPGME_ATTR_VALIDITY, NULL, 0 );          cmpresult = int_cmp (a->uids->validity, b->uids->validity);
         nb = gpgme_key_get_ulong_attr( b, GPGME_ATTR_VALIDITY, NULL, 0 );        
         cmpresult = int_cmp( na, nb );  
432          break;          break;
433    
434      case GPGME_ATTR_OTRUST:      case KEY_SORT_OTRUST: /* XXX: handle expire, revoked */
435          na = gpgme_key_get_ulong_attr (a, GPGME_ATTR_OTRUST, NULL, 0);          cmpresult = int_cmp (a->owner_trust, b->owner_trust);
         nb = gpgme_key_get_ulong_attr (b, GPGME_ATTR_OTRUST, NULL, 0);  
         cmpresult = int_cmp (na, nb);  
436          break;          break;
437    
438      case GPGME_ATTR_IS_SECRET:        case KEY_SORT_IS_SECRET:
439          aa = gpgme_key_get_string_attr( a, GPGME_ATTR_KEYID, NULL, 0 );          get_seckey (a->subkeys->keyid, &a);
440          bb = gpgme_key_get_string_attr( b, GPGME_ATTR_KEYID, NULL, 0 );          get_seckey (b->subkeys->keyid, &b);
441          get_seckey( aa, &a );          cmpresult = int_cmp (a? a->secret : 0, b? b->secret : 0);
         get_seckey( bb, &b );  
         if( a )  
             na = gpgme_key_get_ulong_attr( a, GPGME_ATTR_IS_SECRET, NULL, 0 );  
         if( b )  
             nb = gpgme_key_get_ulong_attr( b, GPGME_ATTR_IS_SECRET, NULL, 0 );  
         cmpresult = int_cmp( na, nb );  
442          break;          break;
443    
444      case GPGME_ATTR_LEN:      case KEY_SORT_LEN:
445          na = gpgme_key_get_ulong_attr( a, GPGME_ATTR_LEN, NULL, 0 );          cmpresult = int_cmp (a->subkeys->length,
446          nb = gpgme_key_get_ulong_attr( b, GPGME_ATTR_LEN, NULL, 0 );                               b->subkeys->length);
         cmpresult = int_cmp( na, nb );  
447          break;          break;
448    
449      case GPGME_ATTR_CREATED:      case KEY_SORT_CREATED:
450          ta = gpgme_key_get_ulong_attr( a, GPGME_ATTR_CREATED, NULL, 0 );          cmpresult = int_cmp (a->subkeys->timestamp,
451          tb = gpgme_key_get_ulong_attr( b, GPGME_ATTR_CREATED, NULL, 0 );                               b->subkeys->timestamp);
         strcpy( tmpa, get_key_created( ta ) ); aa = tmpa;  
         strcpy( tmpb, get_key_created( tb ) ); bb = tmpb;  
         cmpresult = strcmpi( aa? aa : "", bb? bb : "" );  
452          break;          break;
453    
454      case GPGME_ATTR_ALGO:      case KEY_SORT_ALGO:
455          aa = gpgme_key_get_string_attr (a, GPGME_ATTR_ALGO, NULL, 0);          cmpresult = int_cmp (a->subkeys->pubkey_algo,
456          bb = gpgme_key_get_string_attr (b, GPGME_ATTR_ALGO, NULL, 0);                               b->subkeys->pubkey_algo);
         cmpresult = strcmpi (aa? aa : "", bb? bb : "");  
457          break;          break;
458                    
459      default:      default:
460          aa = gpgme_key_get_string_attr( a, GPGME_ATTR_USERID, NULL, 0 );          cmpresult = strcmpi (a->uids->uid, b->uids->uid);
         bb = gpgme_key_get_string_attr( b, GPGME_ATTR_USERID, NULL, 0 );  
         cmpresult = strcmpi( aa? aa : "", bb? bb : "" );  
461          break;          break;
462      }      }
463      if (sortby & KEYLIST_SORT_DESC)      if (sortby & KEYLIST_SORT_DESC)
464          return (~cmpresult + 1);          return (~cmpresult + 1);
465      else      else
466          return cmpresult;          return cmpresult;
467  } /* keylist_cmp_cb */  }
468    
469    
470  static const char*  static const char*
471  calc_validity( gpg_group_t grp )  calc_validity (gpg_group_t grp)
472  {  {
473      int level = 0, valid;      int level = 0, valid;
474      gpg_member_t mbr;      gpg_member_t mbr;
# Line 383  calc_validity( gpg_group_t grp ) Line 477  calc_validity( gpg_group_t grp )
477      for( mbr = grp->list; mbr; mbr = mbr->next ) {      for( mbr = grp->list; mbr; mbr = mbr->next ) {
478          if( get_pubkey( mbr->name, &key ) )          if( get_pubkey( mbr->name, &key ) )
479              continue;              continue;
480          valid = gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_VALIDITY, NULL, 0 );          valid = key->uids->validity;
481          switch( valid ) {                switch( valid ) {      
482          case GPGME_VALIDITY_MARGINAL:          case GPGME_VALIDITY_MARGINAL:
483          case GPGME_VALIDITY_NEVER:          case GPGME_VALIDITY_NEVER:
484          case GPGME_VALIDITY_UNDEFINED:          case GPGME_VALIDITY_UNDEFINED:
485              return gpgme_key_expand_attr( GPGME_ATTR_VALIDITY, valid );              return get_key_trust2 (NULL, valid, 0, 0);
486          }          }
487      }      }
488      return gpgme_key_expand_attr( GPGME_ATTR_VALIDITY, GPGME_VALIDITY_FULL );      return _("Full");
489  } /* calc_validity */  } /* calc_validity */
490    
491    
492  int  int
493  keylist_add_groups( listview_ctrl_t lv )  keylist_add_groups( listview_ctrl_t lv )
494  {  {
495    #if 0
496      gpg_optfile_t gh;      gpg_optfile_t gh;
497      gpg_group_t grp;      gpg_group_t grp;
498      const char *valid;      const char *valid;
# Line 415  keylist_add_groups( listview_ctrl_t lv ) Line 510  keylist_add_groups( listview_ctrl_t lv )
510          listview_add_sub_item( lv, 0, 3, "Unknown" );          listview_add_sub_item( lv, 0, 3, "Unknown" );
511          listview_add_sub_item( lv, 0, 4, valid?valid : "Unknown" );          listview_add_sub_item( lv, 0, 4, valid?valid : "Unknown" );
512      }      }
513    #endif
514      return 0;      return 0;
515  } /* keylist_add_groups */  } /* keylist_add_groups */
516    
# Line 456  keylist_build( listview_ctrl_t *r_lv, HW Line 552  keylist_build( listview_ctrl_t *r_lv, HW
552    
553  static void  static void
554  keylist_load_keycache (listview_ctrl_t lv, int mode,  keylist_load_keycache (listview_ctrl_t lv, int mode,
555                         gpgme_keycache_t pubkc, gpgme_keycache_t seckc)                         gpg_keycache_t pubkc, gpg_keycache_t seckc)
556  {  {
557      gpgme_error_t err = GPGME_No_Error;      gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
558      gpgme_key_t key, skey;      gpgme_key_t key, skey;
559      const char * keyid;      const char * keyid;
560    
561      if( pubkc && seckc ) {      if (pubkc && seckc) {
562          gpgme_keycache_rewind( pubkc );          gpg_keycache_rewind (pubkc);
563          while( !gpgme_keycache_next_key( pubkc, 0, &key ) ) {          while (!gpg_keycache_next_key (pubkc, 0, &key)) {
564              keyid = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );              keyid = key->subkeys->keyid;
565              if( keyid && !gpgme_keycache_find_key( seckc, keyid, 0, &skey ) )              if (keyid && !gpg_keycache_find_key (seckc, keyid, 0, &skey))
566                  keylist_add_key (lv, mode, key);                  keylist_add_key (lv, mode, key);
567          }                }      
568      }      }
569      else if (pubkc) {      else if (pubkc) {
570          gpgme_keycache_rewind (pubkc);          gpg_keycache_rewind (pubkc);
571          while (!err) {              while (!err) {    
572              err = gpgme_keycache_next_key (pubkc, 0, &key);              err = gpg_keycache_next_key (pubkc, 0, &key);
573              if (!err)              if (!err)
574                  keylist_add_key (lv, mode, key);                  keylist_add_key (lv, mode, key);
575          }          }
# Line 482  keylist_load_keycache (listview_ctrl_t l Line 578  keylist_load_keycache (listview_ctrl_t l
578    
579    
580  listview_ctrl_t  listview_ctrl_t
581  keylist_load( HWND ctrl, gpgme_keycache_t pubkc, gpgme_keycache_t seckc,  keylist_load (HWND ctrl, gpg_keycache_t pubkc, gpg_keycache_t seckc,
582                int mode, int sortby )                int mode, int sortby)
583  {      {    
584      listview_ctrl_t lv;      listview_ctrl_t lv;
585      int rc = 0;      int rc = 0;    
586    
587      rc = keylist_build( &lv, ctrl, mode );      rc = keylist_build (&lv, ctrl, mode);
588      if( rc )      if (rc)
589          return NULL;                      return NULL;            
590      keylist_load_keycache( lv, mode, pubkc, seckc );      keylist_load_keycache (lv, mode, pubkc, seckc);
591      keylist_sort( lv, sortby );      keylist_sort (lv, sortby);
592      if( (mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN) )      if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN))
593          keylist_add_groups( lv );          keylist_add_groups (lv);
594      return lv;      return lv;
595  } /* keylist_load */  } /* keylist_load */
596    
597    
598  int  int
599  keylist_reload( listview_ctrl_t lv, gpgme_keycache_t pubkc, int mode, int sortby )  keylist_reload( listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby )
600  {  {
601      listview_del_all( lv );      listview_del_all( lv );
602      keylist_load_keycache( lv, mode, pubkc, NULL );      keylist_load_keycache( lv, mode, pubkc, NULL );
# Line 524  find_secret_key( gpgme_key_t key ) Line 620  find_secret_key( gpgme_key_t key )
620      const char * keyid;      const char * keyid;
621      gpgme_key_t skey;        gpgme_key_t skey;  
622    
623      keyid = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );      keyid = key->subkeys->keyid;
624      if( !keyid )      if (!keyid)
625          return 0;          return 0;
626      get_seckey( keyid, &skey );      get_seckey (keyid, &skey);
627      return skey? 1 : 0;      return skey? 1 : 0;
628  } /* find_secret_key */  } /* find_secret_key */
629    
630    
631  static int  static int
632  do_addkey( listview_ctrl_t lv, gpgme_key_t key, int uididx, int keyidx, int list )  do_addkey (listview_ctrl_t lv, gpgme_key_t key, int uididx, int keyidx, int list)
633  {      {    
634      LV_ITEM lvi;      LV_ITEM lvi;
635      gpgme_key_t seckey;      gpgme_user_id_t u;
636        gpgme_subkey_t k;
637      char fmt[128];      char fmt[128];
638      const char *attr;      const char *attr;
639      u32 key_attr;      u32 key_attr;
640      int idx = 0;      int idx = 0;    
641    
642      /* we check the pubkey algorithm here to make sure that no ElGamal      /* we check the pubkey algorithm here to make sure that no ElGamal
643         sign+encrypt key is used in _any_ mode */         sign+encrypt key is used in _any_ mode */
644      if( list != 1 && gpgme_key_get_ulong_attr( key, GPGME_ATTR_ALGO, NULL, 0 )      if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) {
645                   == GPGME_PK_ELG_ES )          log_debug ("ElGamal (E+S) key found: %s (%s)\n",
646                        key->uids->name, key->subkeys->keyid);
647          return 0;          return 0;
648        }    
649    
650                    
651      if( listview_add_item( lv, " " ) )        if (listview_add_item2 (lv, " ", (void *)key))      
652          return WPTERR_GENERAL;          return WPTERR_GENERAL;
653                    
654      attr = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );      attr = key->uids->uid;
655      memset( &lvi, 0, sizeof lvi );      memset( &lvi, 0, sizeof lvi );
656      lvi.mask = LVIF_TEXT | LVIF_PARAM;      lvi.mask = LVIF_TEXT | LVIF_PARAM;
657      lvi.pszText = (char *)attr;      lvi.pszText = (char *)attr;
# Line 559  do_addkey( listview_ctrl_t lv, gpgme_key Line 659  do_addkey( listview_ctrl_t lv, gpgme_key
659      if( ListView_SetItem( lv->ctrl, &lvi ) == FALSE )        if( ListView_SetItem( lv->ctrl, &lvi ) == FALSE )  
660          return WPTERR_GENERAL;          return WPTERR_GENERAL;
661                    
662      if( uididx == -1 ) {      if( uididx == -1 ) { /* request the primary user-id of the key. */
663          /* request the primary user-id of the key. */          attr = key->uids->uid;
         attr = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );  
664          uididx = 0;          uididx = 0;
665      }      }
666      else {      else {
667          if( gpgme_key_get_ulong_attr( key, GPGME_ATTR_UID_REVOKED, NULL, uididx ) || uididx < 0 )          u = get_nth_userid (key, uididx);
668            if (!u || u->revoked || uididx < 0)
669              uididx = 0; /* fixme: this happen sometimes but it's illegal! (<0) */              uididx = 0; /* fixme: this happen sometimes but it's illegal! (<0) */
670          attr = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, uididx );          u = get_nth_userid (key, uididx);
671            attr = key->uids->uid;
672      }      }
673      if( attr == NULL || strlen( attr ) < 5 ) { /* normal userids are >= 5 chars */      if( attr == NULL || strlen( attr ) < 5 ) { /* normal userids are >= 5 chars */
674          attr = _("Invalid User ID");          attr = _("Invalid User ID");
# Line 575  do_addkey( listview_ctrl_t lv, gpgme_key Line 676  do_addkey( listview_ctrl_t lv, gpgme_key
676      }        }  
677      else {      else {
678          char * uid = utf8_to_wincp (attr, strlen (attr));          char * uid = utf8_to_wincp (attr, strlen (attr));
679          if( uid ) {                if (uid) {
680              listview_add_sub_item( lv, 0, idx++, uid );              listview_add_sub_item( lv, 0, idx++, uid );
681              free( uid );              free (uid);
682          }          }
683      }      }
684      attr = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, keyidx );      k = get_nth_key (key, keyidx);
685      if( attr ) {      if( k && k->keyid ) {
686          _snprintf( fmt, sizeof fmt -1, "0x%s", attr + 8 );          _snprintf( fmt, sizeof fmt -1, "0x%s", k->keyid + 8 );
687          listview_add_sub_item( lv, 0, idx++, fmt );          listview_add_sub_item( lv, 0, idx++, fmt );
688      }      }
689                if (list > 0) {
690      if( list > 0 ) {          attr = find_secret_key (key)? "pub/sec" : "pub";
691          attr = find_secret_key( key )? "pub/sec" : "pub";          if (strchr( attr, '/')) {
692          if( strchr( attr, '/' ) ) {              struct winpt_key_s k;
693              const char * kid;              winpt_get_seckey (key->subkeys->keyid, &k);
694              kid = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );              if (k.ext->gloflags.divert_to_card)
             get_seckey( kid, &seckey );  
             if( gpgme_key_get_ulong_attr( seckey, GPGME_ATTR_DIVERT_CARD, NULL, 0 ) )  
695                  attr = "pub/crd";                  attr = "pub/crd";
696          }          }
697          listview_add_sub_item( lv, 0, idx++, attr );          listview_add_sub_item( lv, 0, idx++, attr );
698      }      }
699      if( lv->cols >= 2 ) {      if (lv->cols >= 2) {
700          attr = get_key_size( key, list==-1? keyidx+1 : 0 );          attr = get_key_size (key, list == -1? keyidx+1 : 0);
701          if( attr )          if (attr)
702              listview_add_sub_item( lv, 0, idx++, attr );                      listview_add_sub_item (lv, 0, idx++, attr);
703      }      }
704      if( lv->cols >= 3 ) {      if (lv->cols >= 3) {
705          attr = get_key_algo( key, list==-1? keyidx+1 : 0 );          attr = get_key_algo (key, list == -1? keyidx+1 : 0);
706          if( attr )          if (attr)
707              listview_add_sub_item( lv, 0, idx++, attr );              listview_add_sub_item( lv, 0, idx++, attr);
708      }      }
709      if( lv->cols >= 4 ) {      if( lv->cols >= 4 ) {
710          char * status = get_key_status( key, uididx, list>0? 1 : 0 );          char * status = get_key_status( key, uididx, list > 0? 1 : 0 );
711          if( !status )          if (!status)
712              return WPTERR_GENERAL;              return WPTERR_GENERAL;
713          listview_add_sub_item( lv, 0, idx++, status );          listview_add_sub_item( lv, 0, idx++, status );
714          free_if_alloc( status );          free_if_alloc( status );
715      }      }
716      if (lv->cols >= 5) {      if (lv->cols >= 5) {
717          const char * s = get_key_trust (key, uididx, list>0? 1 : 0);          const char * s = get_key_trust (key, uididx, list > 0? 1 : 0);
718          listview_add_sub_item (lv, 0, idx++, s);          listview_add_sub_item (lv, 0, idx++, s);
719      }      }
720      if( lv->cols >= 6 ) {      if( lv->cols >= 6 ) {
721          key_attr = gpgme_key_get_ulong_attr( key, GPGME_ATTR_CREATED, NULL, keyidx );          k = get_nth_key (key, keyidx);
722            key_attr = k->timestamp;
723          if( key_attr ) {          if( key_attr ) {
724              attr = gpgme_key_expand_attr( GPGME_ATTR_CREATED, key_attr );              attr = get_key_created (key_attr);
725              listview_add_sub_item( lv, 0, idx++, attr );              listview_add_sub_item( lv, 0, idx++, attr );
726          }                }      
727      }      }
# Line 630  do_addkey( listview_ctrl_t lv, gpgme_key Line 730  do_addkey( listview_ctrl_t lv, gpgme_key
730  } /* do_addkey */  } /* do_addkey */
731    
732    
733    void
734    keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key)
735    {
736        const char *s;
737        char tmp[32];
738    
739        listview_set_item2 (lv, pos, (void *)key);
740        /* the only mode we support is KYLIST_LIST in the Key Manager */
741        
742        s = key->uids->uid;
743        if (s)
744            listview_add_sub_item (lv, pos, 0, s);
745    
746        s = key->subkeys->keyid;
747        if (s) {
748            sprintf (tmp, "0x%s", s+8);
749            listview_add_sub_item (lv, pos, 1, tmp);
750        }
751    
752        s = find_secret_key (key)? "pub/sec" : "pub";
753        listview_add_sub_item (lv, pos, 2, s);
754    
755        s = get_key_size (key, 0);
756        if (s)
757            listview_add_sub_item (lv, pos, 3, s);
758    
759        s = get_key_algo (key, 0);
760        if (s)
761            listview_add_sub_item (lv, pos, 4, s);
762    
763        s = get_key_status (key, 0, 1);
764        if (s)
765            listview_add_sub_item (lv, pos, 5, s);
766    
767        s = get_key_trust (key, 0, 1);
768        if (s)
769            listview_add_sub_item (lv, pos, 6, s);
770    
771        long t = key->subkeys->timestamp;
772        s = get_key_created (t);
773        if (s)
774            listview_add_sub_item (lv, pos, 7, s);
775    }
776    
777    
778  int  int
779  keylist_add_key (listview_ctrl_t lv, int mode, gpgme_key_t key)  keylist_add_key (listview_ctrl_t lv, int mode, gpgme_key_t key)
780  {  {
781      int uids, rc = 0, i, n = 0;      int uids, rc = 0, i, n = 0;
782                gpgme_subkey_t k;
783      for (i = 0; i < gpgme_key_count_items (key, GPGME_ATTR_KEYID); i++) {  
784          if (gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_INVALID, NULL, i))      for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) {
785            if (k->invalid) {
786                log_debug ("keylist_add_key: invalid key \"%s\"\n", key->uids->name);
787              continue; /* Don't use invalid keys */              continue; /* Don't use invalid keys */
788                            }
789    
790          if (mode & KEYLIST_ALL) {          if (mode & KEYLIST_ALL) {
791              uids = gpgme_key_count_items (key, GPGME_ATTR_USERID);              uids = count_userids (key);
792              rc = do_addkey (lv, key, uids, i, 0);              rc = do_addkey (lv, key, uids, i, 0);
793              if( rc )              if( rc )
794                  return rc;                  return rc;
# Line 648  keylist_add_key (listview_ctrl_t lv, int Line 796  keylist_add_key (listview_ctrl_t lv, int
796          else if (mode & KEYLIST_LIST)          else if (mode & KEYLIST_LIST)
797              return do_addkey (lv, key, -1, i, 1);              return do_addkey (lv, key, -1, i, 1);
798          else if (mode & KEYLIST_ENCRYPT) {          else if (mode & KEYLIST_ENCRYPT) {
799              if (gpgme_key_get_cability (key, GPGME_ATTR_CAN_ENCRYPT, i)              if (k->can_encrypt && key_is_useable (k)) {
800                  && gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, i))                  if (mode & KEYLIST_FLAG_FILE) {
801              {                      rc = do_addkey (lv, key, -1, i, -1);
802                  if (mode & KEYLIST_FLAG_FILE ) {                      if (rc)
                     rc = do_addkey( lv, key, -1, i, -1 );  
                     if( rc )  
803                          return rc;                          return rc;
804                  }                  }
805                  else {                  else {
806                      for( uids = 0;  uids < gpgme_key_count_items( key, GPGME_ATTR_USERID ); uids++ ) {                      for( uids = 0;  uids < count_userids (key); uids++ ) {
807                          rc = do_addkey( lv, key, uids, i, -1 );                          rc = do_addkey( lv, key, uids, i, -1 );
808                          if( rc )                          if( rc )
809                              return rc;                              return rc;
# Line 666  keylist_add_key (listview_ctrl_t lv, int Line 812  keylist_add_key (listview_ctrl_t lv, int
812              }              }
813          }          }
814          else if (mode & KEYLIST_ENCRYPT_MIN) {          else if (mode & KEYLIST_ENCRYPT_MIN) {
815              if( gpgme_key_get_cability (key, GPGME_ATTR_CAN_ENCRYPT, i)              if( k->can_encrypt && key_is_useable (k))
                 && gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, i))  
816              {              {
817                  rc = do_addkey (lv, key, -1, i, -1);                  rc = do_addkey (lv, key, -1, i, -1);
818                  return rc;                  return rc;
819              }              }
820          }                }      
821          else if (mode & KEYLIST_SIGN) {          else if (mode & KEYLIST_SIGN) {
822              if ( gpgme_key_get_cability( key, GPGME_ATTR_CAN_SIGN, i )              if ( k->can_sign
823                  && find_secret_key( key )                  && find_secret_key( key )
824                  && gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, i))                  && key_is_useable (k))
825              {              {
826                  rc = do_addkey (lv, key, -1, i, -1);                  rc = do_addkey (lv, key, -1, i, -1);
827                  if( rc )                  if( rc )
# Line 692  keylist_add_key (listview_ctrl_t lv, int Line 837  keylist_add_key (listview_ctrl_t lv, int
837  int  int
838  keylist_sort (listview_ctrl_t lv, int sortby)  keylist_sort (listview_ctrl_t lv, int sortby)
839  {        {      
840      return listview_sort_items( lv, sortby, keylist_cmp_cb );      return listview_sort_items (lv, sortby, keylist_cmp_cb);
841  } /* keylist_sort */  }
842    
843    
844    /* Check that the validity @validity is at least >= marginal. */
845  static int  static int
846  key_check_validity (const char *validity)  key_check_validity (const char *validity)
847  {      {    
848      if (strstr (validity, "Unknown")      if (strstr (validity, "Unknown") ||
849          || strstr (validity, "Undefined")          strstr (validity, "Undefined") ||
850          || strstr (validity, "Never"))          strstr (validity, "Never") ||
851            strstr (validity, "None"))
852          return 0;            return 0;  
853      return 1;      return 1;
854  } /* key_check_validity */  }
855    
856    
857  gpgme_recipients_t  /* Extract all selected recipients from the list @lv and return them
858       as a vector. @r_force_trust is >= 1 if one of the recipients is not
859       fully trusted. @r_count returns the number of selected keys.
860       Return value: the key list on success, NULL otherwise. */
861    gpgme_key_t*
862  keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count)  keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count)
863  {  {
864      int count = 0, force_trust = 0;      int count = 0, force_trust = 0;
865      int n, j, ka_pos = 0, rc = 0;      int n, j, ka_pos = 0, rc = 0;
866        int k_pos=0;
867      char keyid[32], valid[32], id[100];      char keyid[32], valid[32], id[100];
868      key_array_s *ka = NULL;      key_array_s *ka = NULL;
869      gpgme_error_t err;      gpgme_key_t *keybuf;
     gpgme_recipients_t rset;  
870    
     err = gpgme_recipients_new( &rset );  
     if( err )  
         BUG( NULL );  
       
871      n = listview_count_items( lv, 0 );      n = listview_count_items( lv, 0 );
872        
873      ka = key_array_new( n );      ka = key_array_new( n );
874      if ( !ka )      if (!ka)
875          BUG( NULL );          BUG (NULL);
876    
877        keybuf = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t));
878        if (!keybuf)
879            BUG (NULL);
880                    
881      for( j = 0; j < n; j++ ) {      for( j = 0; j < n; j++ ) {
882          if( listview_get_item_state( lv, j ) || n == 1 ) {          if( listview_get_item_state (lv, j) || n == 1) {
883              listview_get_item_text( lv, j, 1, keyid, sizeof keyid - 1 );                                      listview_get_item_text (lv, j, 0, id, sizeof id-1);
884              listview_get_item_text( lv, j, 4, valid, sizeof valid -1 );              listview_get_item_text (lv, j, 1, keyid, sizeof keyid - 1);                
885              listview_get_item_text( lv, j, 0, id, sizeof id-1 );              listview_get_item_text (lv, j, 4, valid, sizeof valid -1);
886              if( !strncmp( keyid, "gpg_group_t", 5 ) ) {              if( !key_check_validity (valid)
887                  listview_get_item_text( lv, j, 0, id, sizeof id -1 );                   && !key_array_search( ka, ka_pos, keyid )) {
                 rc = km_groupdb_expand_recipients( id, rset );  
                 if( rc )  
                     force_trust++;  
             }  
             else if( !key_check_validity( valid )  
                     && !key_array_search( ka, ka_pos, keyid ) ) {  
888                  char *warn = new char[512+strlen (id) + 1];                  char *warn = new char[512+strlen (id) + 1];
889                  if (!warn)                  if (!warn)
890                      BUG (0);                      BUG (0);
# Line 753  keylist_get_recipients (listview_ctrl_t Line 899  keylist_get_recipients (listview_ctrl_t
899                  else                  else
900                      rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);                      rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);
901                  if (rc == IDYES) {                  if (rc == IDYES) {
902                      gpgme_recipients_add_name_with_validity (rset, keyid, GPGME_VALIDITY_FULL);                      gpgme_key_t k;
903                        get_pubkey (keyid, &k);
904                        keybuf[k_pos++] = k;
905                      force_trust++;                      force_trust++;
906                      ka[ka_pos].checked = 1;                      ka[ka_pos].checked = 1;
907                      strcpy (ka[ka_pos++].keyid, keyid);                      strcpy (ka[ka_pos++].keyid, keyid);
908                        count++;
909                  }                  }
910                  free_if_alloc (warn);                  free_if_alloc (warn);
911              }              }
912              else {              else {
913                    gpgme_key_t k;
914                  listview_get_item_text( lv, j, 1, keyid, sizeof keyid -1 );                  listview_get_item_text( lv, j, 1, keyid, sizeof keyid -1 );
915                  gpgme_recipients_add_name( rset, keyid );                  get_pubkey (keyid, &k);
916                    keybuf[k_pos++] = k;
917                  count++;                          count++;        
918              }              }
919          }          }
# Line 772  keylist_get_recipients (listview_ctrl_t Line 923  keylist_get_recipients (listview_ctrl_t
923          *r_force_trust = force_trust;          *r_force_trust = force_trust;
924      if (r_count)      if (r_count)
925          *r_count = count;          *r_count = count;
926      return rset;      return keybuf;
927  } /* keylist_get_recipients */  }
928    
929    
930  static int  static int
# Line 798  keylist_get_keyflags (const char *buf, s Line 949  keylist_get_keyflags (const char *buf, s
949  } /* keylist_get_keyflags */  } /* keylist_get_keyflags */
950    
951    
952  gpgme_recipients_t  gpgme_key_t*
953  keylist_enum_recipients (listview_ctrl_t lv,  int listype)  keylist_enum_recipients (listview_ctrl_t lv,  int listype, int *r_count)
954  {  {
955      gpgme_recipients_t rset;      gpgme_key_t* rset;
956      int i, n, id;      gpgme_key_t k;
957        int i, n, id, k_pos=0;
958      char keyid[32], t[128], t2[128];      char keyid[32], t[128], t2[128];
959    
960      if( gpgme_recipients_new( &rset ) )      n = listview_count_items (lv, 0);
961          BUG( NULL );      if (!n)
962            return 0;
963      n = listview_count_items( lv, 0 );      rset = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t));
964        if (!rset)
965            BUG (NULL);
966      for( i = 0; i < n; i++ ) {      for( i = 0; i < n; i++ ) {
967          if( !listview_get_item_state( lv, i ) )          if( !listview_get_item_state( lv, i ) )
968              continue;              continue;
# Line 825  keylist_enum_recipients (listview_ctrl_t Line 979  keylist_enum_recipients (listview_ctrl_t
979              }              }
980              break;              break;
981          }          }
982          gpgme_recipients_add_name( rset, keyid );          get_pubkey (keyid, &k);
983            rset[k_pos++] = k;
984      }      }
985        if (r_count)
986            *r_count = k_pos;
987      return rset;      return rset;
988  } /* keylist_enum_recipients */  } /* keylist_enum_recipients */
989    
# Line 847  seclist_destroy (keylist_t * list) Line 1004  seclist_destroy (keylist_t * list)
1004  void  void
1005  seclist_init (HWND dlg, int ctlid, int flags, keylist_t * ret_list)  seclist_init (HWND dlg, int ctlid, int flags, keylist_t * ret_list)
1006  {      {    
1007      gpgme_keycache_t kc = NULL;      gpg_keycache_t kc = NULL;
1008      gpgme_key_t key = NULL;      gpgme_key_t key = NULL;
1009      HWND kb;      HWND kb;
1010      keylist_t list=NULL, l, l2;      keylist_t list=NULL, l, l2;    
     gpgme_attr_t name_attr = GPGME_ATTR_USERID;  
1011      long pos = 0;      long pos = 0;
1012    
1013      SendDlgItemMessage (dlg, ctlid, CB_RESETCONTENT, 0, 0);      SendDlgItemMessage (dlg, ctlid, CB_RESETCONTENT, 0, 0);
# Line 859  seclist_init (HWND dlg, int ctlid, int f Line 1015  seclist_init (HWND dlg, int ctlid, int f
1015      kc = keycache_get_ctx (0);      kc = keycache_get_ctx (0);
1016      if (!kc)      if (!kc)
1017          BUG (0);          BUG (0);
1018      gpgme_keycache_rewind (kc);      gpg_keycache_rewind (kc);
1019        
1020      if (flags & KEYLIST_FLAG_SHORT)      while (!gpg_keycache_next_key (kc, 1, &key)) {
         name_attr = GPGME_ATTR_NAME;  
     while (!gpgme_keycache_next_key (kc, 1, &key)) {  
1021          char * inf = NULL, * uid = NULL;          char * inf = NULL, * uid = NULL;
1022          const char * id;          const char * id;
1023          const char * keyid;          const char * keyid;
1024          int algo;          int algo;
1025          size_t size = 0;          size_t size = 0;
1026            
1027          id = gpgme_key_get_string_attr (key, name_attr, NULL, 0);          if (flags & KEYLIST_FLAG_SHORT)
1028          keyid = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0);              id = key->uids->name;
1029          algo = gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0);          else
1030                id = key->uids->uid;
1031            keyid = key->subkeys->keyid;
1032            algo = key->subkeys->pubkey_algo;
1033          if (!id || !keyid)          if (!id || !keyid)
1034              continue; /* fixme: error? */                    continue; /* fixme: error? */      
1035          if (!gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, 0))          if (!key_is_useable (key->subkeys))
1036              continue;              continue;
1037    
1038          uid = utf8_to_wincp (id, strlen (id));          uid = utf8_to_wincp (id, strlen (id));
# Line 883  seclist_init (HWND dlg, int ctlid, int f Line 1040  seclist_init (HWND dlg, int ctlid, int f
1040          inf = new char[size+1];          inf = new char[size+1];
1041          if( !inf )          if( !inf )
1042              BUG( NULL );              BUG( NULL );
1043          _snprintf(inf, size, _("%s (%s/0x%s)"), uid,          _snprintf (inf, size, _("%s (%s/0x%s)"), uid,
1044                    gpgme_key_expand_attr (GPGME_ATTR_ALGO, algo), keyid + 8);                     get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8);
               
1045          combox_add_string (kb, inf);          combox_add_string (kb, inf);
1046          free_if_alloc (inf);          free_if_alloc (inf);
1047          free (uid);          free (uid);
# Line 895  seclist_init (HWND dlg, int ctlid, int f Line 1051  seclist_init (HWND dlg, int ctlid, int f
1051          l->key = key;          l->key = key;
1052          if (!list)          if (!list)
1053              list = l;              list = l;
1054          else          else {
         {  
1055              for( l2 = list; l2->next; l2 = l2->next )              for( l2 = list; l2->next; l2 = l2->next )
1056                  ;                  ;
1057              l2->next = l;              l2->next = l;
# Line 906  seclist_init (HWND dlg, int ctlid, int f Line 1061  seclist_init (HWND dlg, int ctlid, int f
1061          SendMessage( kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key );          SendMessage( kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key );
1062      SendMessage( kb, CB_SETCURSEL, 0, 0 );      SendMessage( kb, CB_SETCURSEL, 0, 0 );
1063      *ret_list = list;      *ret_list = list;
1064  } /* seclist_init */  }
1065                    
1066    
1067    /* Select a secret key from the combo box with the ID @ctlid.
1068       Return the code on success in @ret_key. */
1069  int  int
1070  seclist_select_key (HWND dlg, int ctlid, gpgme_key_t * ret_key)  seclist_select_key (HWND dlg, int ctlid, gpgme_key_t *ret_key)
1071  {  {
1072      int pos;      int pos;
1073      DWORD k = 0;      DWORD k = 0;
1074    
1075      pos = SendDlgItemMessage( dlg, ctlid, CB_GETCURSEL, 0, 0 );      pos = SendDlgItemMessage (dlg, ctlid, CB_GETCURSEL, 0, 0);
1076      if( pos == CB_ERR ) {      if (pos == CB_ERR) {
1077          msg_box( dlg, _("No key was selected."), _("Secret Key List"), MB_ERR );          msg_box (dlg, _("No key was selected."), _("Secret Key List"), MB_ERR);
1078          *ret_key = NULL;          *ret_key = NULL;
1079      }      }
1080      else {      else {
1081          k = SendDlgItemMessage( dlg, ctlid, CB_GETITEMDATA, pos, 0 );          k = SendDlgItemMessage (dlg, ctlid, CB_GETITEMDATA, pos, 0);
1082          *ret_key = (gpgme_key_t)k;          *ret_key = (gpgme_key_t)k;
1083      }      }
1084      return k? 0 : -1;      return k? 0 : -1;
1085  } /* seclist_dlg_proc */  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26