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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26