/[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 73 by twoaday, Tue Nov 8 07:15:13 2005 UTC revision 167 by twoaday, Thu Jan 26 10:17:17 2006 UTC
# Line 1  Line 1 
1  /* wptKeylist.cpp - Keylist element  /* wptKeylist.cpp - Keylist element
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *      Copyright (C) 2004 Andreas Jobs   *      Copyright (C) 2004 Andreas Jobs
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# 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    
22  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
23  #include <config.h>  #include <config.h>
24  #endif  #endif
# Line 37  Line 38 
38  #include "wptUTF8.h"  #include "wptUTF8.h"
39  #include "wptRegistry.h"  #include "wptRegistry.h"
40  #include "wptContext.h"  #include "wptContext.h"
41    #include "wptVersion.h"
42    #include "resource.h"
43    
44  #define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled)  #define key_is_useable(key) (!(key)->revoked && !(key)->expired && !(key)->disabled)
45    
 static struct listview_column_s klist_enc[] = {  
     {0, 242, (char *)_("User ID")},  
     {1, 80, (char *)_("Key ID")},  
     {3, 46, (char *)_("Size")},  
     {4, 50, (char *)_("Cipher")},  
     {5, 70, (char *)_("Validity")},  
     {0, 0, NULL}  
 };  
 #define KLIST_ENC_ITEMS (DIM(klist_enc) -1)  
   
 static struct listview_column_s klist[] = {  
     {0, 242, (char *)_("User ID")},  
     {1, 78, (char *)_("Key ID")},  
     {2, 52, (char *)_("Type")},      
     {3, 68, (char *)_("Size")},  
     {4, 66, (char *)_("Cipher")},  
     {5, 70, (char *)_("Validity")},  
     {6, 40, (char *)_("Trust")},  
     {7, 72, (char *)_("Creation")},  
     {0, 0, NULL}  
 };  
 #define KLIST_ITEMS (DIM(klist) - 1)  
   
46  struct key_array_s {  struct key_array_s {
47      char keyid[32];      char keyid[32];
48      int checked;      int checked;
# Line 73  static int find_secret_key (gpgme_key_t Line 52  static int find_secret_key (gpgme_key_t
52    
53    
54  static key_array_s*  static key_array_s*
55  key_array_new( size_t items )  key_array_new (int items)
56  {  {
57      key_array_s *ka;      key_array_s *ka;
58      size_t j;      int j;
59            
60      if( items == 0 )      if (items == 0)
61          return NULL;          return NULL;
62      ka = new key_array_s[items + 1];      ka = new key_array_s[items + 1];
63      if( ka == NULL )      if (!ka)
64          return NULL;              BUG (NULL);
65      for ( j = 0; j < items; j++ )      for (j = 0; j < items; j++)
66          ka[j].checked = 0;          ka[j].checked = 0;
67      return ka;      return ka;
68  } /* key_array_new */  }
69    
70    
71  static void  static void
72  key_array_release( key_array_s *ka )  key_array_release (key_array_s *ka)
73  {  {
74      free_if_alloc( ka );      free_if_alloc (ka);
75  } /* key_array_release */  }
76    
77    
78    /* Check if the keyid @keyid is in the key array @ka.
79       Return value: 1 if it exists, 0 otherwise. */
80  static int  static int
81  key_array_search( key_array_s *ka, size_t items, const char *keyid )  key_array_search (key_array_s *ka, int items, const char *keyid)
82  {  {
83      size_t j;      int j;
84            
85      /* fixme: we need a faster search method */      for (j = 0; j < items; j++) {
86      for( j = 0; j < items; j++ ) {          if (!strcmp (keyid, ka[j].keyid ))
         if( !strcmp( keyid, ka[j].keyid ) )  
87              return 1;                return 1;  
88      }      }
       
89      return 0;      return 0;
90  } /* key_array_search */  }
91    
92    
93  gpgme_user_id_t  gpgme_user_id_t
# Line 173  count_subkeys (gpgme_key_t key) Line 152  count_subkeys (gpgme_key_t key)
152  }  }
153    
154    
155    /* Return the self signature of the key @keyid.
156       If first is set, the first self sig will be returned. */
157  gpgme_key_sig_t  gpgme_key_sig_t
158  get_selfsig (gpgme_user_id_t uid, const char *keyid, int first)  get_selfsig (gpgme_user_id_t uid, const char *keyid, int first)
159  {  {
160      gpgme_key_sig_t s, self_sig=NULL;      gpgme_key_sig_t s, self_sig=NULL;
161      long timestamp=0;      long timestamp=0;
162        int off = 0;
163    
164        if (strlen (keyid) == 8)
165            off = 8;
166    
167      for (s = uid->signatures; s; s = s->next) {      for (s = uid->signatures; s; s = s->next) {
168          if (!strcmp (s->keyid+8, keyid) && s->timestamp > timestamp) {          if (!strcmp (s->keyid+off, keyid) && s->timestamp > timestamp) {
169              self_sig = s;              self_sig = s;
170              timestamp = s->timestamp;              timestamp = s->timestamp;
171              if (first)              if (first)
# Line 204  get_key_algo (gpgme_key_t key, int keyid Line 189  get_key_algo (gpgme_key_t key, int keyid
189      if (keyidx > 0) {      if (keyidx > 0) {
190          k = get_nth_key (key, keyidx-1);          k = get_nth_key (key, keyidx-1);
191          subalg =  get_key_pubalgo (k->pubkey_algo);          subalg =  get_key_pubalgo (k->pubkey_algo);
192          _snprintf( algo_id, DIM (algo_id)-1, "%s", subalg);          _snprintf (algo_id, DIM (algo_id)-1, "%s", subalg);
193          return algo_id;          return algo_id;
194      }      }
195      strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo));      strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo));
# Line 216  get_key_algo (gpgme_key_t key, int keyid Line 201  get_key_algo (gpgme_key_t key, int keyid
201          return algo_id;          return algo_id;
202      }      }
203      return get_key_pubalgo (key->subkeys->pubkey_algo);      return get_key_pubalgo (key->subkeys->pubkey_algo);
204  } /* get_key_algo */  }
205    
206    
207  const char*  const char*
# Line 224  get_key_created (long timestamp) Line 209  get_key_created (long timestamp)
209  {  {
210      static char timebuf[128];      static char timebuf[128];
211      struct tm *warp;      struct tm *warp;
212        const char *dat;
213    
214      if (timestamp == 0 || timestamp == -1)      if (timestamp < 1)
215          return "????" "-??" "-??";          return "????" "-??" "-??";
216      warp = localtime( &timestamp );      dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1);
217      _snprintf( timebuf, sizeof timebuf - 1, "%04d-%02d-%02d",      if (dat)
218                  warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday );          return dat;
219        warp = localtime (&timestamp);
220        _snprintf (timebuf, sizeof timebuf - 1, "%04d-%02d-%02d",
221                   warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday);
222      return timebuf;      return timebuf;
223  } /* get_key_created */  }
224    
225    
226  /* Return a string presentation of the time @timestamp. */  /* Return a string presentation of the time @timestamp. */
# Line 240  get_key_expire_date (long timestamp) Line 229  get_key_expire_date (long timestamp)
229  {  {
230      static char timebuf[64];      static char timebuf[64];
231      struct tm *warp;      struct tm *warp;
232        const char *dat;
233    
234      if( !timestamp )      if (timestamp == 0)
235          return _("Never");          return _("Never");
236      warp = localtime( &timestamp );      dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1);
237        if (dat)
238            return dat;
239        warp = localtime (&timestamp);
240      _snprintf (timebuf, sizeof timebuf -1, "%04d-%02d-%02d",      _snprintf (timebuf, sizeof timebuf -1, "%04d-%02d-%02d",
241                 warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday);                 warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday);
242      return timebuf;      return timebuf;
# Line 260  get_key_type (gpgme_key_t key) Line 253  get_key_type (gpgme_key_t key)
253      else if (type == 2)      else if (type == 2)
254          return _("Key Pair (Card)");          return _("Key Pair (Card)");
255      return _("Public Key");      return _("Public Key");
256  } /* get_key_type */  }
257    
258    
259  const char*  const char*
# Line 281  get_key_size (gpgme_key_t key, int keyid Line 274  get_key_size (gpgme_key_t key, int keyid
274      if (n > 1) {      if (n > 1) {
275          k = get_nth_key (key, n-1);          k = get_nth_key (key, n-1);
276          size_sub = k->length;          size_sub = k->length;
277          _snprintf( size_id, sizeof (size_id) - 1, "%d/%d", size_main, size_sub );          _snprintf (size_id, sizeof (size_id) - 1, "%d/%d",
278                        size_main, size_sub);
279          return size_id;          return size_id;
280      }      }
281      _snprintf( size_id, sizeof (size_id) - 1, "%d", size_main );      _snprintf( size_id, sizeof (size_id) - 1, "%d", size_main );
282      return size_id;      return size_id;
283  } /* get_key_size */  }
284    
285    
286    const char*
287    get_key_pubalgo2 (gpgme_pubkey_algo_t alg)
288    {
289        switch (alg) {
290        case GPGME_PK_DSA: return "D";
291        case GPGME_PK_RSA: return "R";
292        case GPGME_PK_ELG: return "G";
293        default: return "?";
294        }
295        return "?";
296    }
297    
298    
299  const char*  const char*
# Line 302  get_key_pubalgo (gpgme_pubkey_algo_t alg Line 309  get_key_pubalgo (gpgme_pubkey_algo_t alg
309      return "???";      return "???";
310  }  }
311    
312  const char *  const char*
313  get_key_fpr (gpgme_key_t key)  get_key_fpr (gpgme_key_t key)
314  {  {
315      static char fpr_md[64];      static char fpr_md[64];
# Line 331  get_key_fpr (gpgme_key_t key) Line 338  get_key_fpr (gpgme_key_t key)
338          }          }
339      }      }
340      return fpr_md;      return fpr_md;
341  } /* get_key_fpr */  }
342    
343    
344  const char *  const char*
345  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)
346  {  {
347      if (key)      if (key)
# Line 342  get_key_trust2 (gpgme_key_t key, int val Line 349  get_key_trust2 (gpgme_key_t key, int val
349      switch (val) {      switch (val) {
350      case GPGME_VALIDITY_UNKNOWN:      case GPGME_VALIDITY_UNKNOWN:
351      case GPGME_VALIDITY_UNDEFINED:          case GPGME_VALIDITY_UNDEFINED:    
352          return "None";          return _("None");
353      case GPGME_VALIDITY_NEVER:          case GPGME_VALIDITY_NEVER:    
354          return "Never";          return _("Never");
355      case GPGME_VALIDITY_MARGINAL:      case GPGME_VALIDITY_MARGINAL:
356          return "Marginal";          return _("Marginal");
357      case GPGME_VALIDITY_FULL:      case GPGME_VALIDITY_FULL:
358      case GPGME_VALIDITY_ULTIMATE:      case GPGME_VALIDITY_ULTIMATE:
359          return "Full";          return _("Full");
360      }      }
361      return "";      return "";
362  }  }
363    
364    
365  const char *  const char*
366  get_key_trust (gpgme_key_t key, int uididx, int listmode)  get_key_trust (gpgme_key_t key, int uididx, int listmode)
367  {  {
368      return get_key_trust2 (key, 0, uididx, listmode);      return get_key_trust2 (key, 0, uididx, listmode);
369  }  }
370    
371    
372  const char *  const char*
373  get_key_trust_str (int val)  get_key_trust_str (int val)
374  {  {
375      return get_key_trust2 (NULL, val, 0, 0);      return get_key_trust2 (NULL, val, 0, 0);
# Line 380  get_key_status (gpgme_key_t key, int uid Line 387  get_key_status (gpgme_key_t key, int uid
387      if (uididx < 0 || count_userids (key) > uididx)      if (uididx < 0 || count_userids (key) > uididx)
388          uididx = 0;          uididx = 0;
389      if (listmode) {      if (listmode) {
390          const char *s;          const char *s;
         
391          if (key->revoked)          if (key->revoked)
392              s = _("Revoked");              s = _("Revoked");
393          else if (key->expired)          else if (key->expired)
394              s = _("Expired");              s = _("Expired");
395          else if (key->disabled)          else if (key->disabled)
396              s = _("Disabled");              s = _("Disabled");
397          else          else
398            s = "";              s = "";
           
399          /* if the key has a special status, we don't continue to figure out          /* if the key has a special status, we don't continue to figure out
400             what any user-id validities. */             the user-id validities. */
401          if (*s)          if (*s)
402              return m_strdup (s);              return m_strdup (s);
403      }      }
# Line 403  get_key_status (gpgme_key_t key, int uid Line 408  get_key_status (gpgme_key_t key, int uid
408  }  }
409    
410    
411    /* Return human readable description of the key @key. */
412    char*
413    get_key_desc (gpgme_key_t key)
414    {
415        gpgme_key_t sk;
416        const char *state, *alg, *type;
417        char *p;
418    
419        /* XXX: problems with the German translation. */
420        state = "";
421        if (key->disabled)
422            state = _("Disabled");
423        if (key->expired)
424            state = _("Expired");
425        if (key->revoked)
426            state = _("Revoked");
427        alg = "OpenPGP";
428        if (strlen (key->subkeys->fpr) == 32)
429            alg = "RSA Legacy";
430        type = _("public key");
431        if (!get_seckey (key->subkeys->keyid+8, &sk))
432            type = _("key pair");
433        p = new char[strlen (state) + strlen (alg) + strlen (type) + 4 + 1];
434        if (!p)
435            BUG (0);
436        sprintf (p, "%s %s %s", state, alg, type);
437        return p;
438    }
439    
440    
441  /* Integer comparsion of @a and @b.  /* Integer comparsion of @a and @b.
442     Return values: same as in strcmp. */     Return values: same as in strcmp. */
443  static inline int  static inline int
# Line 424  get_ext_validity (gpgme_key_t k) Line 459  get_ext_validity (gpgme_key_t k)
459          return GPGME_VALIDITY_ULTIMATE+1;          return GPGME_VALIDITY_ULTIMATE+1;
460      else if (k->expired)      else if (k->expired)
461          return GPGME_VALIDITY_ULTIMATE+2;          return GPGME_VALIDITY_ULTIMATE+2;
462        else if (k->disabled)
463            return GPGME_VALIDITY_ULTIMATE+3;
464      return k->uids->validity;      return k->uids->validity;
465  }  }
466    
# Line 491  keylist_cmp_cb (LPARAM first, LPARAM sec Line 528  keylist_cmp_cb (LPARAM first, LPARAM sec
528  }  }
529    
530    
 /* Return the validity of the group @grp. */  
 static const char*  
 calc_validity (gpg_group_t grp)  
 {  
     int valid=0;  
     gpg_member_t mbr;  
     gpgme_key_t key;  
   
     for (mbr = grp->list; mbr; mbr = mbr->next) {  
         if (get_pubkey (mbr->name, &key))  
             continue;  
         valid = key->uids->validity;  
         switch (valid) {  
         case GPGME_VALIDITY_MARGINAL:  
         case GPGME_VALIDITY_NEVER:  
         case GPGME_VALIDITY_UNDEFINED:  
             return get_key_trust2 (NULL, valid, 0, 0);  
         }  
     }  
     return _("Full");  
 }  
   
   
531  int  int
532  keylist_add_groups( listview_ctrl_t lv )  keylist_add_groups (listview_ctrl_t lv)
533  {  {
 #if 0  
     gpg_optfile_t gh;  
     gpg_group_t grp;  
     const char *valid;  
   
     gh = km_groupdb_open( );      
     if( !gh )    
         return WPTERR_FILE_OPEN;  
   
     for( grp = gh->grp; grp; grp = grp->next ) {  
         valid = calc_validity( grp );  
         listview_add_item( lv, " " );    
         listview_add_sub_item( lv, 0, 0, grp->name );    
         listview_add_sub_item( lv, 0, 1, "gpg_group_t" );        
         listview_add_sub_item( lv, 0, 2, "" );    
         listview_add_sub_item( lv, 0, 3, "Unknown" );  
         listview_add_sub_item( lv, 0, 4, valid?valid : "Unknown" );  
     }  
 #endif  
534      return 0;      return 0;
535  } /* keylist_add_groups */  }
536    
537    
538  /* Create a listview for listing keys. Use the mode given in @mode  /* Create a listview for listing keys. Use the mode given in @mode
# Line 545  keylist_add_groups( listview_ctrl_t lv ) Line 540  keylist_add_groups( listview_ctrl_t lv )
540  static int  static int
541  keylist_build (listview_ctrl_t *r_lv, HWND ctrl, int mode)  keylist_build (listview_ctrl_t *r_lv, HWND ctrl, int mode)
542  {  {
543        struct listview_column_s klist_enc[] = {
544        {0, 242, (char *)_("User ID")},
545        {1, 80, (char *)_("Key ID")},
546        {3, 46, (char *)_("Size")},
547        {4, 50, (char *)_("Cipher")},
548        {5, 70, (char *)_("Validity")},
549        {0, 0, NULL}
550        };
551        struct listview_column_s klist[] = {
552        {0, 240, (char *)_("User ID")},
553        {1, 78, (char *)_("Key ID")},
554        {2, 52, (char *)_("Type")},    
555        {3, 66, (char *)_("Size")},
556        {4, 60, (char *)_("Cipher")},
557        {5, 66, (char *)_("Validity")},
558        {6, 58, (char *)_("Trust")},
559        {7, 72, (char *)_("Creation")},
560        {0, 0, NULL}
561        };
562        HICON ico[2];
563      listview_ctrl_t lv;      listview_ctrl_t lv;
564      listview_column_t col;      listview_column_t col;
565      int j, n = 0;      int j, n = 0;
566      int rc = 0;      int rc = 0;
567            
568      rc = listview_new (&lv);      rc = listview_new (&lv);
569      if( rc )      if (rc)
570          return rc;          return rc;
571            
572      lv->ctrl = ctrl;      lv->ctrl = ctrl;
573      if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) {      if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) {
574          col = klist_enc;          col = klist_enc;
575          n = KLIST_ENC_ITEMS;          n = (DIM(klist_enc) -1);
576      }        }  
577      else if ((mode & KEYLIST_SIGN)) {      else if ((mode & KEYLIST_SIGN)) {
578          col = klist_enc;          col = klist_enc;
579          n = KLIST_ENC_ITEMS - 1;          n = (DIM(klist_enc) - 1) - 1;
580      }      }
581      else {      else {
582          col = klist;          col = klist;
583          n = KLIST_ITEMS;          n = (DIM(klist) - 1);
584      }      }
585            
586      for( j = 0; j < n; j++ )          for (j = 0; j < n; j++)
587          listview_add_column( lv, &col[j] );              listview_add_column (lv, &col[j]);
588      listview_set_ext_style( lv );      listview_set_ext_style (lv);
589        ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_PUBKEY);
590        ico[1] = LoadIcon (glob_hinst, (LPCTSTR)IDI_KEYPAIR);
591        listview_set_image_list (lv, ico, 2);
592        listview_del_all_items (lv);
593    
594      *r_lv = lv;      *r_lv = lv;
       
595      return 0;      return 0;
596  }  }
597    
# Line 628  keylist_load (HWND ctrl, gpg_keycache_t Line 647  keylist_load (HWND ctrl, gpg_keycache_t
647  int  int
648  keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby)  keylist_reload (listview_ctrl_t lv, gpg_keycache_t pubkc, int mode, int sortby)
649  {  {
650      listview_del_all (lv);      listview_del_all_items (lv);
651      keylist_load_keycache( lv, mode, pubkc, NULL );      keylist_load_keycache (lv, mode, pubkc, NULL);
652      keylist_sort (lv, sortby);      keylist_sort (lv, sortby);
653      return 0;      return 0;
654  }  }
# Line 677  do_addkey (listview_ctrl_t lv, gpgme_key Line 696  do_addkey (listview_ctrl_t lv, gpgme_key
696      /* we check the pubkey algorithm here to make sure that no ElGamal      /* we check the pubkey algorithm here to make sure that no ElGamal
697         sign+encrypt key is used in _any_ mode */         sign+encrypt key is used in _any_ mode */
698      if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) {      if (list != 1 && key->subkeys->pubkey_algo == GPGME_PK_ELG) {
699          log_debug ("ElGamal (E+S) key found: %s (%s)\n",          log_debug ("ElGamal (E+S) key found: %s (%s)\n",
700                     key->uids->name, key->subkeys->keyid);                     key->uids->name, key->subkeys->keyid);
701          return 0;          return 0;
702      }      }
   
703                    
704      if (listview_add_item2 (lv, " ", (void *)key))            if (listview_add_item2 (lv, " ", (void *)key))
705          return WPTERR_GENERAL;          return WPTERR_GENERAL;
706            
707      attr = key->uids->uid;      attr = key->uids->uid;
708      memset (&lvi, 0, sizeof lvi);      memset (&lvi, 0, sizeof lvi);
709      lvi.mask = LVIF_TEXT | LVIF_PARAM;      lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
710      lvi.pszText = (char *)attr;      lvi.pszText = (char *)attr;
711        lvi.iImage = find_secret_key (key)? 1 : 0;
712      lvi.lParam = (LPARAM )key;      lvi.lParam = (LPARAM )key;
713      if (ListView_SetItem( lv->ctrl, &lvi ) == FALSE)      if (ListView_SetItem (lv->ctrl, &lvi) == FALSE)
714          return WPTERR_GENERAL;          return WPTERR_GENERAL;
715                    
716      if (uididx == -1) { /* request the primary user-id of the key. */      if (uididx == -1) { /* request the primary user-id of the key. */
# Line 701  do_addkey (listview_ctrl_t lv, gpgme_key Line 720  do_addkey (listview_ctrl_t lv, gpgme_key
720      else {      else {
721          u = get_nth_userid (key, uididx);          u = get_nth_userid (key, uididx);
722          if (!u || u->revoked || uididx < 0)          if (!u || u->revoked || uididx < 0)
723              uididx = 0; /* fixme: this happen sometimes but it's illegal! (<0) */              uididx = 0;
724          u = get_nth_userid (key, uididx);          u = get_nth_userid (key, uididx);
         /*attr = key->uids->uid; XXX*/  
725          attr = u->uid;          attr = u->uid;
726      }      }
727      if (attr == NULL || strlen (attr) < 5) { /* normal userids are >= 5 chars */      if (attr == NULL || strlen (attr) < 5) { /* normal userids are > 5 chars */
728          attr = _("Invalid User ID");          attr = _("Invalid User ID");
729          listview_add_sub_item (lv, 0, idx++, attr);          listview_add_sub_item (lv, 0, idx++, attr);
730      }        }  
# Line 740  do_addkey (listview_ctrl_t lv, gpgme_key Line 758  do_addkey (listview_ctrl_t lv, gpgme_key
758          if (attr)          if (attr)
759              listview_add_sub_item( lv, 0, idx++, attr);              listview_add_sub_item( lv, 0, idx++, attr);
760      }      }
761      if( lv->cols >= 4 ) {      if (lv->cols >= 4) {
762          p = get_key_status( key, uididx, list > 0? 1 : 0 );          p = get_key_status( key, uididx, list > 0? 1 : 0 );
763          if (!p)          if (!p)
764              return WPTERR_GENERAL;              return WPTERR_GENERAL;
# Line 764  do_addkey (listview_ctrl_t lv, gpgme_key Line 782  do_addkey (listview_ctrl_t lv, gpgme_key
782  }  }
783    
784    
785    /* Update a single column @col but for each element in the
786       listview @lv. */
787    void
788    keylist_upd_col (listview_ctrl_t lv, int col)
789    {
790        gpgme_key_t key;
791        const char *s;
792        char buf[32], *p;
793        int i;
794    
795        for (i=0; i < listview_count_items (lv, 0); i++) {
796            key = (gpgme_key_t)listview_get_item2 (lv, i);
797            if (!key)
798                continue;
799            switch (col) {
800            case KM_COL_KEYID:
801                _snprintf (buf, sizeof (buf)-1, "0x%s", key->subkeys->keyid+8);
802                listview_add_sub_item (lv, i, col, buf);
803                break;
804    
805            case KM_COL_CIPHER:
806                s = get_key_algo (key, 0);
807                listview_add_sub_item (lv, i, col, s);
808                break;
809    
810            case KM_COL_TYPE:
811                s = find_secret_key (key)? "pub/sec" : "pub";
812                listview_add_sub_item (lv, i, col, s);
813                break;
814    
815            case KM_COL_CREAT:
816                s = get_key_created (key->subkeys->timestamp);
817                listview_add_sub_item (lv, i, col, s);
818                break;
819    
820            case KM_COL_DESC:
821                p = get_key_desc (key);
822                listview_add_sub_item (lv, i, col, p);
823                free_if_alloc (p);
824                break;
825            }
826        }
827    }
828    
829    
830    /* Update the listview item at position @pos with the data from
831       the key @key. */
832  void  void
833  keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key)  keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key)
834  {  {
835      const char *s;      const char *s;
836        char *uid, *p;
837      char tmp[32];      char tmp[32];
838    
839      listview_set_item2 (lv, pos, (void *)key);      listview_set_item2 (lv, pos, (void *)key);
840      /* the only mode we support is KYLIST_LIST in the Key Manager */      /* the only mode we support is KYLIST_LIST in the Key Manager */
841            
842      s = key->uids->uid;      s = key->uids->uid;
843      if (s)      if (s) {
844          listview_add_sub_item (lv, pos, 0, s);          uid = utf8_to_wincp2 (s);
845            listview_add_sub_item (lv, pos, KM_COL_UID, uid);
846            free (uid);
847        }
848    
849      s = key->subkeys->keyid;      s = key->subkeys->keyid;
850      if (s) {      if (s) {
851          sprintf (tmp, "0x%s", s+8);          sprintf (tmp, "0x%s", s+8);
852          listview_add_sub_item (lv, pos, 1, tmp);          listview_add_sub_item (lv, pos, KM_COL_KEYID, tmp);
853      }      }
854    
855      s = find_secret_key (key)? "pub/sec" : "pub";      s = find_secret_key (key)? "pub/sec" : "pub";
856      listview_add_sub_item (lv, pos, 2, s);      listview_add_sub_item (lv, pos, KM_COL_TYPE, s);
857    
858      s = get_key_size (key, 0);      s = get_key_size (key, 0);
859      if (s)      if (s)
860          listview_add_sub_item (lv, pos, 3, s);          listview_add_sub_item (lv, pos, KM_COL_SIZE, s);
861    
862      s = get_key_algo (key, 0);      s = get_key_algo (key, 0);
863      if (s)      if (s)
864          listview_add_sub_item (lv, pos, 4, s);          listview_add_sub_item (lv, pos, KM_COL_CIPHER, s);
865    
866      s = get_key_status (key, 0, 1);      p = get_key_status (key, 0, 1);
867      if (s)      if (p) {
868          listview_add_sub_item (lv, pos, 5, s);          listview_add_sub_item (lv, pos, KM_COL_VALID, p);
869            free_if_alloc (p);
870        }
871    
872      s = get_key_trust (key, 0, 1);      s = get_key_trust (key, 0, 1);
873      if (s)      if (s)
874          listview_add_sub_item (lv, pos, 6, s);          listview_add_sub_item (lv, pos, KM_COL_TRUST, s);
875    
876      long t = key->subkeys->timestamp;      long t = key->subkeys->timestamp;
877      s = get_key_created (t);      s = get_key_created (t);
878      if (s)      if (s)
879          listview_add_sub_item (lv, pos, 7, s);          listview_add_sub_item (lv, pos, KM_COL_CREAT, s);
880  }  }
881    
882    
# Line 816  keylist_add_key (listview_ctrl_t lv, int Line 887  keylist_add_key (listview_ctrl_t lv, int
887      gpgme_subkey_t k;      gpgme_subkey_t k;
888    
889      /* if the entire key is disabled, just return. */      /* if the entire key is disabled, just return. */
890      if (key->disabled)      if (key->disabled && !(mode & KEYLIST_LIST))
891          return 0;          return 0;
892    
893      for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) {      for (k=key->subkeys, i = 0; i < count_subkeys (key); i++, k=k->next) {
# Line 828  keylist_add_key (listview_ctrl_t lv, int Line 899  keylist_add_key (listview_ctrl_t lv, int
899          if (mode & KEYLIST_ALL) {          if (mode & KEYLIST_ALL) {
900              uids = count_userids (key);              uids = count_userids (key);
901              rc = do_addkey (lv, key, uids, i, 0);              rc = do_addkey (lv, key, uids, i, 0);
902              if( rc )              if (rc)
903                  return rc;                  return rc;
904          }          }
905          else if (mode & KEYLIST_LIST)          else if (mode & KEYLIST_LIST)
# Line 841  keylist_add_key (listview_ctrl_t lv, int Line 912  keylist_add_key (listview_ctrl_t lv, int
912                          return rc;                          return rc;
913                  }                  }
914                  else {                  else {
915                      for( uids = 0;  uids < count_userids (key); uids++ ) {                      for (uids = 0;  uids < count_userids (key); uids++) {
916                          rc = do_addkey( lv, key, uids, i, -1 );                          rc = do_addkey (lv, key, uids, i, -1);
917                          if( rc )                          if (rc)
918                              return rc;                              return rc;
919                      }                      }
920                  }                  }
# Line 868  keylist_add_key (listview_ctrl_t lv, int Line 939  keylist_add_key (listview_ctrl_t lv, int
939      }      }
940    
941      return rc;        return rc;  
942  } /* keylist_add_key */  }
943    
944    
945  int  int
946  keylist_sort (listview_ctrl_t lv, int sortby)  keylist_sort (listview_ctrl_t lv, int sortby)
947  {        {
948      return listview_sort_items (lv, sortby, keylist_cmp_cb);      return listview_sort_items (lv, sortby, keylist_cmp_cb);
949  }  }
950    
951    
952  /* Check that the validity @validity is at least >= marginal. */  /* Check that the validity @validity is at least >= marginal. */
953  static int  static int
954  key_check_validity (const char *validity)  key_check_validity (gpgme_key_t key)
955  {      {
956      if (strstr (validity, "Unknown") ||      gpgme_user_id_t u;
957          strstr (validity, "Undefined") ||  
958          strstr (validity, "Never") ||      for (u=key->uids; u; u =u->next) {
959          strstr (validity, "None"))          if (u->validity >= GPGME_VALIDITY_MARGINAL)
960          return 0;                return -1;
961      return 1;      }
962    
963        return 0;
964  }  }
965    
966    
# Line 898  key_check_validity (const char *validity Line 971  key_check_validity (const char *validity
971  gpgme_key_t*  gpgme_key_t*
972  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)
973  {  {
974        key_array_s *ka = NULL;
975        gpgme_key_t *keybuf, key;
976      int count = 0, force_trust = 0;      int count = 0, force_trust = 0;
977      int n, j, ka_pos = 0, rc = 0;      int n, j, ka_pos = 0, rc = 0;
978      int k_pos=0;      int k_pos=0;
     char keyid[32], valid[32], id[100];  
     key_array_s *ka = NULL;  
     gpgme_key_t *keybuf;  
979    
980      n = listview_count_items( lv, 0 );      n = listview_count_items (lv, 0);
981            
982      ka = key_array_new( n );      ka = key_array_new (n);
983      if (!ka)      if (!ka)
984          BUG (NULL);          BUG (NULL);
985    
# Line 915  keylist_get_recipients (listview_ctrl_t Line 987  keylist_get_recipients (listview_ctrl_t
987      if (!keybuf)      if (!keybuf)
988          BUG (NULL);          BUG (NULL);
989                    
990      for( j = 0; j < n; j++ ) {      for (j = 0; j < n; j++) {
991          if( listview_get_item_state (lv, j) || n == 1) {          if (listview_get_item_state (lv, j) || n == 1) {
992              listview_get_item_text (lv, j, 0, id, sizeof id-1);              key = (gpgme_key_t)listview_get_item2 (lv, j);
993              listview_get_item_text (lv, j, 1, keyid, sizeof keyid - 1);                              if (!key)
994              listview_get_item_text (lv, j, 4, valid, sizeof valid -1);                  BUG (0);
995              if( !key_check_validity (valid)              if (!key_check_validity (key) &&
996                   && !key_array_search( ka, ka_pos, keyid )) {                  !key_array_search (ka, ka_pos, key->subkeys->keyid)) {
997                  char *warn = new char[512+strlen (id) + 1];                  char *warn = new char[512+strlen (key->uids->uid) + 1];
998                  if (!warn)                  if (!warn)
999                      BUG (0);                      BUG (0);
1000                  sprintf (warn,                  sprintf (warn,
# Line 930  keylist_get_recipients (listview_ctrl_t Line 1002  keylist_get_recipients (listview_ctrl_t
1002                        "named in the user ID.  If you *really* know what you are\n"                        "named in the user ID.  If you *really* know what you are\n"
1003                        "doing, you may answer the next question with yes\n"                        "doing, you may answer the next question with yes\n"
1004                        "\n"                        "\n"
1005                        "Use \"%s\" anyway?"), id);                        "Use \"%s\" anyway?"), key->uids->uid);
1006                  if (reg_prefs.always_trust)                  if (reg_prefs.always_trust)
1007                      rc = IDYES;                      rc = IDYES;
1008                  else                  else
1009                      rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);                      rc = msg_box (NULL, warn, _("Recipients"), MB_ERR_ASK);
1010                  if (rc == IDYES) {                  if (rc == IDYES) {
1011                      gpgme_key_t k;                      keybuf[k_pos++] = key;
                     get_pubkey (keyid, &k);  
                     keybuf[k_pos++] = k;  
1012                      force_trust++;                      force_trust++;
1013                      ka[ka_pos].checked = 1;                      ka[ka_pos].checked = 1;
1014                      strcpy (ka[ka_pos++].keyid, keyid);                      strcpy (ka[ka_pos++].keyid, key->subkeys->keyid);
1015                      count++;                      count++;
1016                  }                  }
1017                  free_if_alloc (warn);                  free_if_alloc (warn);
1018              }              }
1019              else {              else {
1020                  gpgme_key_t k;                  keybuf[k_pos++] = key;
                 listview_get_item_text( lv, j, 1, keyid, sizeof keyid -1 );  
                 get_pubkey (keyid, &k);  
                 keybuf[k_pos++] = k;  
1021                  count++;                          count++;        
1022              }              }
1023          }          }
# Line 965  keylist_get_recipients (listview_ctrl_t Line 1032  keylist_get_recipients (listview_ctrl_t
1032    
1033    
1034  static int  static int
1035  keylist_get_keyflags (const char *buf, size_t buflen)  keylist_get_keyflags (gpgme_key_t key)
1036  {  {
1037      int c = 0, flags = 0;      int flags = KEYFLAG_NONE;
1038    
1039      if( *buf != '[' )      if (key->revoked)
1040          return KEYFLAG_NONE;          flags |= KEYFLAG_REVOKED;
1041      while (buf && c != ']')      if (key->expired)
1042      {          flags |= KEYFLAG_EXPIRED;
1043          c = *buf++;      if (key->disabled)
1044          if (c == 'R')          flags |= KEYFLAG_DISABLED;
             flags |= KEYFLAG_REVOKED;  
         if (c == 'E')  
             flags |= KEYFLAG_EXPIRED;  
         if (c == 'D')  
             flags |= KEYFLAG_DISABLED;  
     }  
1045    
1046      return flags;      return flags;
1047  } /* keylist_get_keyflags */  }
1048    
1049    
1050  gpgme_key_t*  gpgme_key_t*
1051  keylist_enum_recipients (listview_ctrl_t lv,  int listype, int *r_count)  keylist_enum_recipients (listview_ctrl_t lv,  int listype, int *r_count)
1052  {  {
1053      gpgme_key_t* rset;      gpgme_key_t *rset;
1054      gpgme_key_t k;      gpgme_key_t key;
1055      int i, n, id, k_pos=0;      int i, n, id, k_pos=0;
     char keyid[32], t[128], t2[128];  
1056    
1057      n = listview_count_items (lv, 0);      n = listview_count_items (lv, 0);
1058      if (!n)      if (!n)
# Line 1000  keylist_enum_recipients (listview_ctrl_t Line 1060  keylist_enum_recipients (listview_ctrl_t
1060      rset = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t));      rset = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t));
1061      if (!rset)      if (!rset)
1062          BUG (NULL);          BUG (NULL);
1063      for( i = 0; i < n; i++ ) {      for (i = 0; i < n; i++) {
1064          if( !listview_get_item_state( lv, i ) )          if (!listview_get_item_state (lv, i))
1065              continue;              continue;
1066          listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 );          key = (gpgme_key_t)listview_get_item2 (lv, i);
1067          switch( listype ) {          if (!key)
1068                BUG (0);
1069            switch (listype) {
1070          case KEYLIST_LIST:          case KEYLIST_LIST:
1071              listview_get_item_text( lv, i, 5, t, sizeof t - 1 );              if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) {
1072              if( keylist_get_keyflags( t, strlen( t ) ) & KEYFLAG_REVOKED ) {                  id = printf_box (_("Recipients"), MB_INFO|MB_YESNO,
1073                  _snprintf( t2, sizeof t2 -1,                  _("KeyID %s.\nDo you really want to export a revoked key?"),
1074                              _("KeyID %s.\nDo you really want to export a revoked key?"), keyid );                                   key->uids->uid);
1075                  id = msg_box( lv->ctrl, t2, _("Recipients"), MB_INFO|MB_YESNO );                  if (id == IDNO)
1076                  if( id == IDNO )                      continue;
                     continue;            
1077              }              }
1078              break;              break;
1079          }          }
1080          get_pubkey (keyid, &k);          rset[k_pos++] = key;
         rset[k_pos++] = k;  
1081      }      }
1082      if (r_count)      if (r_count)
1083          *r_count = k_pos;          *r_count = k_pos;
1084      return rset;      return rset;
1085  } /* keylist_enum_recipients */  }
1086    
1087    
1088  void  void
1089  seclist_destroy (keylist_t * list)  seclist_destroy (keylist_t *list)
1090  {  {
1091      keylist_t l2;      keylist_t l2;
1092      while (*list) {      while (*list) {
# Line 1035  seclist_destroy (keylist_t * list) Line 1095  seclist_destroy (keylist_t * list)
1095          *list = l2;              *list = l2;    
1096      }      }
1097      list = NULL;      list = NULL;
1098  } /* seclist_destroy */  }
1099    
1100    
1101  void  void
# Line 1073  seclist_init (HWND dlg, int ctlid, int f Line 1133  seclist_init (HWND dlg, int ctlid, int f
1133              continue;              continue;
1134    
1135          uid = utf8_to_wincp (id, strlen (id));          uid = utf8_to_wincp (id, strlen (id));
1136          size = strlen( uid ) + strlen( keyid ) + 32;          size = strlen (uid) + strlen (keyid) + 32;
1137          inf = new char[size+1];          inf = new char[size+1];
1138          if( !inf )          if (!inf)
1139              BUG( NULL );              BUG (NULL);
1140          _snprintf (inf, size, "%s (%s/0x%s)", uid,          _snprintf (inf, size, "%s (%s/0x%s)", uid,
1141                     get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8);                     get_key_pubalgo (key->subkeys->pubkey_algo), keyid + 8);
1142          combox_add_string (kb, inf);          combox_add_string (kb, inf);
1143          free_if_alloc (inf);          free_if_alloc (inf);
# Line 1094  seclist_init (HWND dlg, int ctlid, int f Line 1154  seclist_init (HWND dlg, int ctlid, int f
1154              l2->next = l;              l2->next = l;
1155          }          }
1156      }      }
1157      for( pos = 0, l2=list; pos < SendMessage( kb, CB_GETCOUNT, 0, 0 ); pos++, l2=l2->next )      for (pos = 0, l2=list; pos < SendMessage (kb, CB_GETCOUNT, 0, 0);
1158          SendMessage( kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key );           pos++, l2=l2->next)
1159      SendMessage( kb, CB_SETCURSEL, 0, 0 );          SendMessage (kb, CB_SETITEMDATA, pos, (LPARAM)(DWORD)l2->key);
1160        SendMessage (kb, CB_SETCURSEL, 0, 0);
1161      *ret_list = list;      *ret_list = list;
1162  }  }
1163                    

Legend:
Removed from v.73  
changed lines
  Added in v.167

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26