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

Diff of /trunk/Src/wptKeyManager.cpp

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

revision 21 by twoaday, Wed Jul 27 11:17:22 2005 UTC revision 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC
# Line 64  km_get_clip_info (const char *uid, char Line 64  km_get_clip_info (const char *uid, char
64    
65    
66  char*  char*
67  km_quote_uid (const char * uid)  km_quote_uid (const char *uid)
68  {      {    
69      char * q = new char[strlen (uid) + 4];      char * q = new char[strlen (uid) + 4];
70      if (!q)      if (!q)
# Line 75  km_quote_uid (const char * uid) Line 75  km_quote_uid (const char * uid)
75    
76    
77  int  int
78  km_check_for_seckey (listview_ctrl_t lv, int pos, int * utrust)  km_check_for_seckey (listview_ctrl_t lv, int pos, int *utrust)
79  {  {
80      char t[32], t2[64];      char t[32], t2[64];
81      int type = 0;      int type = 0;
82            
83      listview_get_item_text (lv, pos, 5, t, sizeof (t) - 1);      if (utrust)
84      listview_get_item_text (lv, pos, 2, t2, sizeof (t2) - 1);          *utrust = 0;
85        listview_get_item_text (lv, pos, 5, t, DIM (t)-1);
86        listview_get_item_text (lv, pos, 2, t2, DIM (t2)-1);
87      if (!strcmp (t2, "pub/sec"))      if (!strcmp (t2, "pub/sec"))
88          type = 1;          type = 1;
89      else if (!strcmp (t2, "pub/crd"))      else if (!strcmp (t2, "pub/crd"))
90          type = 2;          type = 2;
91      if (stristr (t, "ultimate") && utrust)      if ((strstr (t, "Expired") || strstr (t, "Revoked")) && utrust)
92            *utrust = -1;
93        else if (stristr (t, "Ultimate") && utrust)
94          *utrust = 1;          *utrust = 1;
95      return type;      return type;
96  } /* km_check_for_seckey */  } /* km_check_for_seckey */
97    
98    
99  int  int
100  km_check_if_protected( listview_ctrl_t lv, int pos )  km_check_if_protected (listview_ctrl_t lv, int pos)
101  {  {
102      gpgme_key_t key;      gpgme_key_t key;
103      char keyid[32];      
104            key = (gpgme_key_t)listview_get_item2 (lv, pos);
105      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );      if (key == NULL)
106      if( get_pubkey( keyid, &key ) )          return 1; /* assume yes */
107          BUG( NULL );          return gpgme_key_get_ulong_attr (key, GPGME_ATTR_IS_PROTECTED, NULL, 0);
     return gpgme_key_get_ulong_attr( key, GPGME_ATTR_IS_PROTECTED, NULL, 0 );  
108  } /* km_check_if_protected */  } /* km_check_if_protected */
109    
110    
111  int  int
112  km_check_key_status (listview_ctrl_t lv, int pos)  km_check_key_status (listview_ctrl_t lv, int pos)
113  {  {
114      char t[128];      int flags = km_get_key_status (lv, pos);
     int i = 1;  
115            
116      listview_get_item_text( lv, pos, 5, t, sizeof t - 1 );      if (flags & KM_FLAG_EXPIRED) {
117      if( t[0] == '[' && t[1] == ']' )          msg_box (lv->ctrl, _("This key has expired!\n"  
118          return 1;                               "Key check failed."), _("Key Manager"), MB_ERR);
119      for( i = 0; t[i] != ']'; i++ ) {          return -1;
120          if( t[i] == 'E' )      }
121              msg_box(lv->ctrl, _("This key has expired!\n"      else if (flags & KM_FLAG_REVOKED) {
122                                  "Key check failed."), _("Key Manager"), MB_ERR );          msg_box (lv->ctrl, _("This key has been revoked by its owner!\n"
123          else if( t[i] == 'R' )                               "Key check failed."), _("Key Manager"), MB_ERR);
124              msg_box(lv->ctrl, _("This key has been revoked by its owner!\n"          return -1;
                                 "Key check failed."), _("Key Manager"), MB_ERR );  
125      }      }
126    
127      return 0;      return 0;
# Line 128  km_check_key_status (listview_ctrl_t lv, Line 129  km_check_key_status (listview_ctrl_t lv,
129    
130    
131  int  int
132  km_get_key_status( listview_ctrl_t lv, int pos )  km_get_key_status (listview_ctrl_t lv, int pos)
133  {  {
134      char t[128];      gpgme_key_t key;
135      int i, flags = 0;      int flags = 0;
136    
137      if( pos == -1 )      if (pos == -1)
138          return 0;          return 0;
139      listview_get_item_text( lv, pos, 5, t, sizeof t-1 );      key = (gpgme_key_t)listview_get_item2 (lv, pos);
140      for( i = 0; t[i] != ']'; i++ ) {      if (key == NULL)
141          if( t[i] == 'E' )          return 0;
142              flags |= KM_FLAG_EXPIRED;      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_EXPIRED, NULL, 0))
143          if( t[i] == 'R' )          flags |= KM_FLAG_EXPIRED;
144              flags |= KM_FLAG_REVOKED;      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_REVOKED, NULL, 0))
145          if( t[i] == 'D' )          flags |= KM_FLAG_REVOKED;
146              flags |= KM_FLAG_DISABLED;      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_DISABLED, NULL, 0))
147      }          flags |= KM_FLAG_DISABLED;
148      return flags;      return flags;
149  } /* km_get_key_status */  } /* km_get_key_status */
150    
# Line 157  km_enable_disable_key (listview_ctrl_t l Line 158  km_enable_disable_key (listview_ctrl_t l
158      int edit_id;      int edit_id;
159      char keyid[32];      char keyid[32];
160    
161      listview_get_item_text( lv, pos, 1, keyid, DIM (keyid)-1 );      listview_get_item_text (lv, pos, 1, keyid, DIM (keyid)-1);
162      err = gpgme_editkey_new( &ek );      err = gpgme_editkey_new (&ek);
163      if( err )      if (err)
164          BUG( NULL );          BUG (NULL);
165      if( enable ) {      if (enable) {
166          gpgme_editkey_enable_set (ek);          gpgme_editkey_enable_set (ek);
167          edit_id = GPGME_EDITKEY_ENABLE;          edit_id = GPGME_EDITKEY_ENABLE;
168      }      }
# Line 225  leave: Line 226  leave:
226    
227    
228  int  int
229  km_privkey_export( HWND dlg, listview_ctrl_t lv, const char *fname )  km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname)
230  {  {
231      gpgme_recipients_t rset;      gpgme_recipients_t rset;
232      gpgme_data_t keydata;      gpgme_data_t keydata;
# Line 233  km_privkey_export( HWND dlg, listview_ct Line 234  km_privkey_export( HWND dlg, listview_ct
234      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
235      size_t n = 0;      size_t n = 0;
236    
237      rset = keylist_enum_recipients( lv, KEYLIST_LIST );      rset = keylist_enum_recipients (lv, KEYLIST_LIST);
238      n = gpgme_recipients_count( rset );      n = gpgme_recipients_count (rset);
239      if( !n ) {      if (!n) {
240          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );
241          return WPTERR_GENERAL;          return WPTERR_GENERAL;
242      }      }
243      if( n > 1 ) {      if (n > 1) {
244          msg_box( dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR );
245          return 0; /* we checked this before, so we just quit */          return 0; /* we checked this before, so we just quit */
246      }      }
# Line 249  km_privkey_export( HWND dlg, listview_ct Line 250  km_privkey_export( HWND dlg, listview_ct
250      err = gpgme_new( &ctx );      err = gpgme_new( &ctx );
251      if( err )      if( err )
252          BUG( dlg );          BUG( dlg );
253      gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);
254      gpgme_control( ctx, GPGME_CTRL_WITH_SECRET_KEY, 1 );      gpgme_control (ctx, GPGME_CTRL_WITH_SECRET_KEY, 1);
255        gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);
256    
257      err = gpgme_op_export( ctx, rset, keydata );      err = gpgme_op_export( ctx, rset, keydata );
258      if( err ) {      if( err ) {
# Line 292  km_file_export (HWND dlg, listview_ctrl_ Line 294  km_file_export (HWND dlg, listview_ctrl_
294      err = gpgme_new( &ctx );      err = gpgme_new( &ctx );
295      if( err )      if( err )
296          BUG( dlg );          BUG( dlg );
297      gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);
298      gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);      gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);
299            
300      err = gpgme_op_export( ctx, rset, keydata );      err = gpgme_op_export( ctx, rset, keydata );
# Line 361  km_clip_import( HWND dlg ) Line 363  km_clip_import( HWND dlg )
363          id = msg_box( dlg, _("The key you want to import is dash escacped.\n"          id = msg_box( dlg, _("The key you want to import is dash escacped.\n"
364                               "Do you want to extract the key?"),                               "Do you want to extract the key?"),
365                        _("Key Manager"), MB_YESNO );                        _("Key Manager"), MB_YESNO );
366          if( id == IDYES )          if (id == IDYES)
367              extract_dash_escaped_key( );              extract_dash_escaped_key ();
368          else          else
369              msg_box( dlg, _("Cannot import dash escaped OpenPGP keys."), _("Key Manager"), MB_INFO );              msg_box( dlg, _("Cannot import dash escaped OpenPGP keys."), _("Key Manager"), MB_INFO );
370      }      }
# Line 390  km_http_import (HWND dlg, const char * u Line 392  km_http_import (HWND dlg, const char * u
392          return WPTERR_GENERAL;          return WPTERR_GENERAL;
393      }      }
394    
395      GetTempPath (499, tmpdir);      GetTempPath (sizeof (tmpdir)-1, tmpdir);
396      p = make_filename (tmpdir, "file_http", "tmp");      p = make_filename (tmpdir, "winpt_file_http", "tmp");
397      if (!p)      if (!p)
398          BUG (0);          BUG (0);
399      fp = fopen (p, "wb");      fp = fopen (p, "wb");
# Line 410  km_http_import (HWND dlg, const char * u Line 412  km_http_import (HWND dlg, const char * u
412      fclose (fp);      fclose (fp);
413      if (rc) {      if (rc) {
414          free_if_alloc (p);          free_if_alloc (p);
415          msg_box (dlg, http_strerror (rc), _("Key Import HTTP"), MB_ERR);          msg_box (dlg, winpt_strerror (rc), _("Key Import HTTP"), MB_ERR);
416          return WPTERR_GENERAL;          return WPTERR_GENERAL;
417      }      }
418      km_file_import (dlg, p);      km_file_import (dlg, p);
419        // XXX: delete file
420      free_if_alloc (p);      free_if_alloc (p);
421      return 0;      return 0;
422  }  }
423    
424    
425  int  int
426  km_file_import( HWND dlg, const char * fname )  km_file_import (HWND dlg, const char * fname)
427  {  {
428      gpgme_data_t keydata = NULL;      gpgme_data_t keydata = NULL;
429      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
430      gpgme_error_t err;      gpgme_error_t err;    
431        gpgme_recipients_t keys = NULL;
432      fm_state_s fm_stat;      fm_state_s fm_stat;
433      int import_res[14];      int import_res[14];
434            
435      memset( &fm_stat, 0, sizeof fm_stat );      memset (&fm_stat, 0, sizeof (fm_stat));
436      fm_stat.opaque = m_strdup( fname );      fm_stat.opaque = m_strdup (fname);
437            
438      dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,      dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
439                        file_import_dlg_proc, (LPARAM)&fm_stat,                        file_import_dlg_proc, (LPARAM)&fm_stat,
440                        _("File Import"), IDS_WINPT_IMPORT );                        _("File Import"), IDS_WINPT_IMPORT);
441      if( fm_stat.cancel == 1 )      if (fm_stat.cancel == 1 ) {
442            free_if_alloc (fm_stat.opaque);
443          return WPTERR_GENERAL;          return WPTERR_GENERAL;
444        }
445            
446      err = gpgme_new( &ctx );      err = gpgme_new( &ctx );
447      if( err )      if( err )
# Line 451  km_file_import( HWND dlg, const char * f Line 458  km_file_import( HWND dlg, const char * f
458          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );
459          goto leave;          goto leave;
460      }      }
461        
462      gpgme_get_import_status( ctx, import_res, NULL );      gpgme_get_import_status (ctx, import_res, &keys);
463      print_import_status( import_res, fm_stat.implist_revcert );      print_import_status (import_res, fm_stat.implist_revcert);
464      if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0  ) {      if (import_res[GPGME_IMPSTAT_NOSELFSIG] > 0) {
465          msg_box( dlg, _("Key without a self signature was dectected!\n"          msg_box (dlg, _("Key without a self signature was dectected!\n"
466                          "(This key is NOT usable for encryption, etc)\n"                          "(This key is NOT usable for encryption, etc)\n"
467                          "\n"                              "\n"    
468                          "Cannot import these key(s)!"), _("Import"), MB_INFO );                          "Cannot import these key(s)!"), _("Import"), MB_INFO);
469      }      }
470        
471  leave:          leave:
472      gpgme_data_release( keydata );      gpgme_recipients_release (keys);
473      gpgme_release( ctx );      gpgme_data_release (keydata);
474      free_if_alloc( fm_stat.opaque );      gpgme_release (ctx);
475        free_if_alloc (fm_stat.opaque);
476      return (int)err;      return (int)err;
477  } /* km_file_import */  } /* km_file_import */
478    
# Line 473  static void Line 481  static void
481  delete_keys_from_cache (gpgme_recipients_t rset)  delete_keys_from_cache (gpgme_recipients_t rset)
482  {  {
483      gpgme_keycache_t pub = keycache_get_ctx (1);      gpgme_keycache_t pub = keycache_get_ctx (1);
484      void * ctx =NULL;      void *ctx =NULL;
485      const char * s;      const char *s;
486    
487      gpgme_recipients_enum_open (rset, &ctx);      gpgme_recipients_enum_open (rset, &ctx);
488      while ((s = gpgme_recipients_enum_read (rset, &ctx)))      while ((s = gpgme_recipients_enum_read (rset, &ctx)))
# Line 489  km_delete_keys (listview_ctrl_t lv, HWND Line 497  km_delete_keys (listview_ctrl_t lv, HWND
497      gpgme_error_t err;      gpgme_error_t err;
498      gpgme_recipients_t rset;      gpgme_recipients_t rset;
499      char keyid[32], uid[256], date[64], keylen[64];          char keyid[32], uid[256], date[64], keylen[64];    
500      int with_seckey, seckey_type=0;      int with_seckey=0, seckey_type=0, confirm=0;
501      int i, rc, n;      int i, rc, n;
502            
503      if( listview_get_curr_pos( lv ) == -1 ) {      if (listview_get_curr_pos (lv) == -1) {
504          msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
505          return WPTERR_GENERAL;          return WPTERR_GENERAL;
506      }      }
507                
508        if (listview_count_items (lv, 1) > 8) {
509            i = msg_box (NULL, _("Do you really want to confirm each key?"), _("Delete Confirmation"), MB_YESNOCANCEL|MB_ICONQUESTION);
510            if (i == IDCANCEL)
511                return 0;
512            if (i != IDNO)
513                confirm = 1;
514        }
515        else
516            confirm = 1;
517      err = gpgme_recipients_new (&rset);      err = gpgme_recipients_new (&rset);
518      if (err)      if (err)
519          BUG (0);          BUG (0);
# Line 507  km_delete_keys (listview_ctrl_t lv, HWND Line 524  km_delete_keys (listview_ctrl_t lv, HWND
524              listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 );              listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 );
525              listview_get_item_text( lv, i, 3, keylen, sizeof keylen - 1 );              listview_get_item_text( lv, i, 3, keylen, sizeof keylen - 1 );
526              listview_get_item_text( lv, i, 7, date, sizeof date - 1 );              listview_get_item_text( lv, i, 7, date, sizeof date - 1 );
527              seckey_type = km_check_for_seckey( lv, i, NULL );              seckey_type = km_check_for_seckey (lv, i, NULL);
528              if( !seckey_type ) {              if (confirm && !seckey_type) {
529                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,
530                                _("Do you really want to delete this key?\n\n"                                _("Do you really want to delete this key?\n\n"
531                                  "pub %s %s %s\n"                                  "pub %s %s %s\n"
532                                  "  \"%s\""), keylen, keyid, date, uid );                                  "  \"%s\""), keylen, keyid, date, uid );
533                  if( rc == IDYES )                  if (rc == IDYES)
534                      gpgme_recipients_add_name( rset, keyid );                      gpgme_recipients_add_name(rset, keyid);
535                  with_seckey = 0;                  with_seckey = 0;
536              }              }
537              else {              else if (confirm) {
538                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,                                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,                
539                                _("Do you really want to delete this KEY PAIR?\n\n"                                _("Do you really want to delete this KEY PAIR?\n\n"
540                                  "Please remember that you are not able to decrypt\n"                                  "Please remember that you are not able to decrypt\n"
# Line 534  km_delete_keys (listview_ctrl_t lv, HWND Line 551  km_delete_keys (listview_ctrl_t lv, HWND
551                  }                  }
552                  with_seckey = 1;                  with_seckey = 1;
553              }              }
554                else {
555                    with_seckey = 1;
556                    gpgme_recipients_add_name (rset, keyid);
557                }
558          }          }
559      }      }
560            
# Line 554  km_delete_keys (listview_ctrl_t lv, HWND Line 575  km_delete_keys (listview_ctrl_t lv, HWND
575      }      }
576      show_msg (dlg, 1500, _("GnuPG Status: Finished"));      show_msg (dlg, 1500, _("GnuPG Status: Finished"));
577      listview_del_items (lv);      listview_del_items (lv);
     if (keyring_check_last_access ())  
         keycache_set_reload (1);  
578      delete_keys_from_cache (rset);      delete_keys_from_cache (rset);
579      gpgme_recipients_release (rset);      gpgme_recipients_release (rset);
580    
# Line 754  km_complete_status_bar( HWND sb, listvie Line 773  km_complete_status_bar( HWND sb, listvie
773  } /* km_complete_status_bar */  } /* km_complete_status_bar */
774    
775    
776  void  int
777  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)
778  {  {
779      gpgme_error_t err;      gpgme_error_t err;
# Line 773  km_set_implicit_trust (HWND dlg, listvie Line 792  km_set_implicit_trust (HWND dlg, listvie
792      gpgme_editkey_trust_set (ek, 5);      gpgme_editkey_trust_set (ek, 5);
793    
794      err = gpgme_op_editkey (ctx, keyid);      err = gpgme_op_editkey (ctx, keyid);
795    
796      if (err)      if (err)
797          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
798      else {      else
799          show_msg (dlg, 1500, _("GnuPG Status: Finished"));          show_msg (dlg, 1500, _("GnuPG Status: Finished"));
         keycache_set_reload (1);  
     }  
800    
801      gpgme_release (ctx);      gpgme_release (ctx);
802      gpgme_editkey_release (ek);          gpgme_editkey_release (ek);
803        return (int)err;
804    }
805    
806    
807    void
808    km_find_key (HWND dlg, listview_ctrl_t lv)
809    {
810        int oldpos = listview_get_curr_pos (lv);
811        int n;
812        char *name = get_input_dialog (dlg, "Search", "Search for:");
813        if (name == NULL)
814            return;
815        if (oldpos < 0)
816            oldpos = 0;
817        n = listview_find (lv, name);
818        if (n != -1) {
819            listview_select_one (lv, n);
820            listview_scroll (lv, oldpos, n);
821        }
822        else {
823            const char *s = _("String pattern \"%s\" not found.");
824            char *p = new char[strlen (s) + strlen (name) + 2];
825            if (!p)
826                BUG (0);
827            sprintf (p, s, name);
828            msg_box (dlg, p, _("Key Manager"), MB_INFO);
829            free_if_alloc (p);
830        }
831        free_if_alloc (name);
832    }
833    
834    
835    
836    void
837    km_dump_key (gpgme_key_t key)
838    {
839    #if _DEBUG
840        log_box ("DEBUG", MB_OK,
841                 "%d %d %s %d\n%s",
842                 gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0),
843                 gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0),
844                 gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0),
845                 gpgme_key_get_ulong_attr (key, GPGME_ATTR_CREATED, NULL, 0),
846                 gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, 0));
847    #endif
848  }  }
849    
850    

Legend:
Removed from v.21  
changed lines
  Added in v.22

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26