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

Diff of /trunk/Src/wptFileManager.cpp

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

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 28 by twoaday, Thu Oct 20 12:35:59 2005 UTC
# Line 47  Line 47 
47    
48  #include "openpgp.h"  #include "openpgp.h"
49    
50  void progress_cleanup (progress_filter_s * pfx);  void progress_cleanup (progress_filter_s *pfx);
51    
52  char* gpg_keylist_to_pattern (gpgme_key_t *rset);  char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n);
53  gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,  gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
54                                   int prev_was_bad, int fd);                                   int prev_was_bad, int fd);
55    
56  /*-- wptFileVerifyDlg.cpp --*/  /*-- wptFileVerifyDlg.cpp --*/
57  int  file_verify_add_state (siglog_context_t c);  int  file_verify_add_state (file_sig_ctx_t c);
58  void file_verify_use_event (void);  void file_verify_use_event (void);
59  void file_verify_wait (void);  void file_verify_wait (void);
60    
# Line 734  fm_parse_files (listview_ctrl_t lv, HWND Line 734  fm_parse_files (listview_ctrl_t lv, HWND
734      /* we use it here to make sure that pfx_cleanup will not use      /* we use it here to make sure that pfx_cleanup will not use
735         any weird values. */         any weird values. */
736      memset (&pfx, 0, sizeof (pfx));      memset (&pfx, 0, sizeof (pfx));
737      if (cmd != FM_VERIFY && cmd != FM_SIGN && reg_prefs.fm.progress > 0) {      ctx->prog_cb = NULL;
738        if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) {
739          pfx.hwnd = dlg;          pfx.hwnd = dlg;
740          gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);          /*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/
741            /*ctx->prog_cb = &pfx;*/
742      }      }
743            
744      /* Commands we need before we can perform the main command */      /* Commands we need before we can perform the main command */
# Line 814  fm_parse_files (listview_ctrl_t lv, HWND Line 816  fm_parse_files (listview_ctrl_t lv, HWND
816          case FM_DECRYPT:     rc = fm_decrypt( ctx, fname );    break;          case FM_DECRYPT:     rc = fm_decrypt( ctx, fname );    break;
817          case FM_SIGN:        rc = fm_sign( ctx, fname );       break;          case FM_SIGN:        rc = fm_sign( ctx, fname );       break;
818          case FM_SYMENC:      rc = fm_sym_encrypt( ctx, fname );break;          case FM_SYMENC:      rc = fm_sym_encrypt( ctx, fname );break;
819          case FM_VERIFY:      rc = fm_verify( ctx, sig_detached, fname );break;          case FM_VERIFY:      rc = fm_verify (ctx, sig_detached, fname);break;
820          case FM_IMPORT:          case FM_IMPORT:
821              free_if_alloc (ctx->opaque);              free_if_alloc (ctx->opaque);
822              ctx->opaque = m_strdup (fname);              ctx->opaque = m_strdup (fname);
# Line 906  ask_filename (fm_state_t c, const char * Line 908  ask_filename (fm_state_t c, const char *
908    
909    
910  int  int
911  fm_encrypt (fm_state_t c, const char * name, int sign)  fm_encrypt (fm_state_t c, const char *name, int sign)
912  {  {
913      gpgme_error_t err;      gpgme_error_t err;
914      gpgme_key_t key = NULL;      gpgme_key_t key = NULL;
915      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
916      file_data_t in=NULL, out=NULL;      file_data_t in=NULL, out=NULL;
917      char * keyid = NULL, ext[5];          char *keyid = NULL, ext[5];
918      int no_compr = 0;      int no_compr = 0;
919      int rc = 0;      int rc = 0;
920    
# Line 935  fm_encrypt (fm_state_t c, const char * n Line 937  fm_encrypt (fm_state_t c, const char * n
937      err = gpg_file_data_new (c->output, 0, &out);      err = gpg_file_data_new (c->output, 0, &out);
938      if (err)      if (err)
939          goto leave;          goto leave;
940    
941        /*
942        if (c->prog_cb) {
943            c->prog_cb->what = name;
944            gpg_file_data_set_cb (in, c->prog_cb);
945        }
946        */
947            
948      /* XXX      /* XXX
949      no_compr = is_multi_media (name);      no_compr = is_multi_media (name);
# Line 977  fm_encrypt (fm_state_t c, const char * n Line 986  fm_encrypt (fm_state_t c, const char * n
986          }          }
987          if (err) {          if (err) {
988              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
989              if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE))              if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
990                  agent_del_cache (keyid);                  agent_del_cache (keyid);
991              rc = WPTERR_GENERAL;              rc = WPTERR_GENERAL;
992              goto leave;              goto leave;
993          }          }
         gpgme_key_release (key);  
994      }      }
995      else {      else {
996          err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,          err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
997                                  in->dat, out->dat);                                  in->dat, out->dat);
998          if (err) {          if (err) {
999              msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);              msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);
# Line 1063  leave: Line 1071  leave:
1071  } /* fm_sym_encrypt */  } /* fm_sym_encrypt */
1072    
1073    
1074    /* Show the human readable verify result from @sigres. */
1075    static void
1076    show_verify_result (gpgme_verify_result_t sigres)
1077    {
1078        gpgme_key_t key=NULL;
1079        gpgme_signature_t sig=sigres->signatures;
1080        const char *s, *keyid;
1081        int sigok = 0;
1082        int type;
1083        char buf[384];
1084    
1085        sig = sigres->signatures;
1086        sigok = sig->summary & GPGME_SIGSUM_GREEN;
1087        s = sigok? _("Good signature") : _("BAD signature");
1088        type = sigok? MB_OK: MB_ICONWARNING|MB_OK;
1089        keyid = sig->fpr;  
1090        if (!keyid)
1091            return;
1092    
1093        keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;
1094        get_pubkey (sig->fpr, &key);
1095        _snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n"
1096                                        "%s from \"%s\"",
1097                    strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo),
1098                    keyid, s, key? key->uids->uid : _("user ID not found"));
1099        msg_box (NULL, buf, _("Decrypt Verify"), type);
1100    }
1101    
1102    
1103    /* Check the recipients if we have at least one secret key. */
1104    bool
1105    secret_key_available (gpgme_recipient_t rset)
1106    {
1107        gpgme_recipient_t r;
1108        gpgme_key_t key;
1109    
1110        for (r=rset; r; r = r->next) {      
1111            if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY)
1112                continue;
1113            else {
1114                /* extra check to make sure the key is available right now. */
1115                if (!get_seckey (r->keyid, &key))
1116                    return true;
1117            }
1118        }
1119        return false;
1120    }
1121    
1122    
1123  /* Decrypt the file @name. */  /* Decrypt the file @name. */
1124  int  int
1125  fm_decrypt (fm_state_t c, const char *name)  fm_decrypt (fm_state_t c, const char *name)
1126  {  {
1127      gpgme_error_t err;      gpgme_error_t err;
1128      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;    
     gpgme_signature_t sig = NULL;  
1129      gpgme_decrypt_result_t res;      gpgme_decrypt_result_t res;
1130      gpgme_verify_result_t sigres;      gpgme_verify_result_t sigres;
1131      file_data_t in =NULL, out=NULL;      file_data_t in =NULL, out=NULL;
1132      char keyid[17];      int is_signed = 0;
     int is_signed = 0, sigok = 0;  
1133      int rc = 0;      int rc = 0;
1134            
1135      if (!c->init_cb || !c->cache_cb) {      if (!c->init_cb || !c->cache_cb) {
# Line 1089  fm_decrypt (fm_state_t c, const char *na Line 1144  fm_decrypt (fm_state_t c, const char *na
1144      if (is_openpgp_ext (c->output))      if (is_openpgp_ext (c->output))
1145          c->output[strlen (c->output)-4] = '\0';          c->output[strlen (c->output)-4] = '\0';
1146      else {      else {
1147          const char *s = get_filename_dlg (c->dlg, FILE_SAVE,          const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"),
                                           _("Choose Filename for Output"),  
1148                                            NULL, NULL);                                            NULL, NULL);
1149          if( s ) {          if (s) {
1150              free_if_alloc (c->output);              free_if_alloc (c->output);
1151              c->output = m_strdup (s);              c->output = m_strdup (s);
1152              if (!c->output)              if (!c->output)
# Line 1124  fm_decrypt (fm_state_t c, const char *na Line 1178  fm_decrypt (fm_state_t c, const char *na
1178      }      }
1179    
1180      res = gpgme_op_decrypt_result (ctx);      res = gpgme_op_decrypt_result (ctx);
1181      if (res && gpgme_err_code (err) == GPG_ERR_NO_SECKEY) {      if (res && res->recipients && !secret_key_available (res->recipients)) {
1182            const char *keyid = res->recipients->keyid;
1183          char *p = get_key_userid (keyid+8);          char *p = get_key_userid (keyid+8);
1184          gpgme_pubkey_algo_t pkalgo = (gpgme_pubkey_algo_t)0;          gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;
1185            
         if (res->recipients)  
             pkalgo = res->recipients->pubkey_algo;  
1186          log_box( _("Decryption"), MB_ERR,          log_box( _("Decryption"), MB_ERR,
1187                   _("Encrypted with %s key, ID %s.%s\n"                   _("Encrypted with %s key, ID %s.%s\n"
1188                     "Decryption failed: secret key not available."),                     "Decryption failed: secret key not available."),
# Line 1144  fm_decrypt (fm_state_t c, const char *na Line 1197  fm_decrypt (fm_state_t c, const char *na
1197          goto leave;          goto leave;
1198      }      }
1199      if (file_exist_check (c->output)) {      if (file_exist_check (c->output)) {
         //msg_box( c->dlg, _("Decryption failed"), _("Decrypt"), MB_ERR );  
1200          log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output);          log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output);
1201          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1202      }      }
1203            
1204      sigres = gpgme_op_verify_result (ctx);      sigres = gpgme_op_verify_result (ctx);
1205      if (sigres && sigres->signatures) {      if (sigres && sigres->signatures)
1206          sig = sigres->signatures;          show_verify_result (sigres);
         sigok = sig->summary & GPGME_SIGSUM_GREEN;        
         const char *s = sigok? _("Good signature") : _("BAD signature");  
         int type = sigok? MB_OK: MB_ICONWARNING|MB_OK;  
         gpgme_key_t key;  
         const char *keyid = sig->fpr;  
         if (!keyid)  
             keyid = "????????????????";  
         else {  
             keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;  
1207    
         if (get_pubkey (keyid+8, &key))  
             log_box (_("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8);  
         else  
             log_box (_("Verify"), type, "%s using keyID 0x%08X from %s",  
                                 s, keyid, _("no user ID"));  
         }  
     }  
1208            
1209  leave:  leave:
1210      if (in)      if (in)
# Line 1188  fm_sign (fm_state_t c, const char * name Line 1224  fm_sign (fm_state_t c, const char * name
1224      file_data_t in=NULL, out=NULL;      file_data_t in=NULL, out=NULL;
1225      char ext[5];      char ext[5];
1226    
1227      if( !c->init_cb || !c->cache_cb ) {      if (!c->init_cb || !c->cache_cb) {
1228          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") );          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") );
1229          c->init_cb = 1;          c->init_cb = 1;
1230      }      }
1231            
1232      free_if_alloc (c->output);      free_if_alloc (c->output);
1233      c->output = new char[strlen( name ) + 5 + 1];      c->output = new char[strlen (name) + 5 + 1];
1234      if( !c->output)      if( !c->output)
1235          BUG( NULL );          BUG( NULL );
1236      strcpy( ext, file_get_extension( ctx, c->sigmode ) );      strcpy (ext, file_get_extension (ctx, c->sigmode));
1237      strcpy( c->output, name );      strcpy (c->output, name);
1238      strcat( c->output, ext );      strcat (c->output, ext);
1239            
1240      if (!overwrite_file (c->output)) {      if (!overwrite_file (c->output)) {
1241          rc = ask_filename (c, _("Enter filename for signed file"), NULL);          rc = ask_filename (c, _("Enter filename for signed file"), NULL);
1242          if (rc)          if (rc)
1243              goto leave;              goto leave;
1244      }      }
1245      remove_crit_file_attrs( c->output, 0 );      remove_crit_file_attrs (c->output, 0);
1246    
1247      err = gpg_file_data_new (name, 1, &in);      err = gpg_file_data_new (name, 1, &in);
1248      if (err)      if (err)
# Line 1234  leave: Line 1270  leave:
1270      if (out)      if (out)
1271          gpg_file_data_release (out);          gpg_file_data_release (out);
1272      return rc;      return rc;
1273  } /* fm_sign */  }
1274    
1275    
1276  static int  static int
1277  fm_add_sig_stat( siglog_context_t log )  fm_add_sig_stat (file_sig_ctx_t log)
1278  {  {
1279      gpgme_key_t key;          gpgme_key_t key;    
1280      const char *kid;      const char *kid;
# Line 1246  fm_add_sig_stat( siglog_context_t log ) Line 1282  fm_add_sig_stat( siglog_context_t log )
1282    
1283      kid = log->sig->fpr;      kid = log->sig->fpr;
1284      if (!kid)      if (!kid)
1285          kid = "????????????????";          BUG (NULL);
1286      if (strlen (kid) == 40)      if (strlen (kid) == 40)
1287          kid += 32;          kid += 32;      
1288      else      else if (strlen (kid) == 32)
1289          kid += 24;          kid += 24;
1290      if (get_pubkey (kid, &key))      if (get_pubkey (kid, &key))
1291          not_found = 1;          log->use_uid = 0;
1292      log->use_uid = 0;      else {
     if (!not_found) {  
1293          log->user_id = key->uids->uid;          log->user_id = key->uids->uid;
1294          log->use_uid = 1;                log->use_uid = 1;
1295      }      }
1296      file_verify_add_state (log);      file_verify_add_state (log);
   
1297      return 0;      return 0;
1298  } /* fm_add_sig_stat */  }
1299    
1300    
1301  static int  static int
# Line 1332  fm_verify_pasted_detsig (listview_ctrl_t Line 1366  fm_verify_pasted_detsig (listview_ctrl_t
1366  }  }
1367    
1368    
1369  int  /* Extract automatically the output file name from @name.
1370  fm_verify (fm_state_t c, int detached, const char *name)     If @detached is 1, a detached sig is assumed. */
1371    static int
1372    get_output_file (fm_state_t c, const char *name, int detached)
1373  {  {
1374      gpgme_ctx_t ctx = c->ctx;      const char *file = NULL;
1375      gpgme_error_t err;      const char *title;
1376      gpgme_signature_t s;      char fname[384];
1377      gpgme_verify_result_t res;      
1378      struct siglog_context_s log;      if (detached)
1379      file_data_t in=NULL, out=NULL;          title = _("Select Data File");
1380      int rc = 0;      else
1381                    title = _("Selected Output File");
1382      if (detached) {  
1383          const char *file = NULL;      if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) {
1384          if (strstr (name, ".sig") || strstr (name, ".asc")) {          _snprintf (fname, sizeof (fname) - 1, "%s", name);
1385              char fname[512];          fname[strlen (fname) - 4] = '\0';
1386              _snprintf (fname, sizeof (fname) - 1, "%s", name);          if (file_exist_check (fname) == 0 && detached)  
1387              fname[strlen (fname) - 4] = '\0';              file = fname;
1388              if (file_exist_check (fname) == 0)          else if (!detached) {
1389                /* If the signature is clear or normal, make sure we do not
1390                   overwrite the original file if it exists. */
1391                if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {
1392                    file = get_filesave_dlg (c->dlg, title, NULL, NULL);
1393                    if (!file)
1394                        return WPTERR_GENERAL;
1395                }
1396                else
1397                  file = fname;                  file = fname;
         }        
         if (!file)  
             file = get_filename_dlg (c->dlg, FILE_OPEN, _("Select Data File"), NULL, NULL);  
         if (file) {  
             free_if_alloc (c->output);  
             c->output = m_strdup (file);  
         }  
         else {  
             msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);  
             return WPTERR_GENERAL;  
1398          }          }
         c->sigmode = GPGME_SIG_MODE_DETACH;  
1399      }      }
1400        if (!file)
1401            file = get_fileopen_dlg (c->dlg, title, NULL, NULL);
1402        if (file) {    
1403            free_if_alloc (c->output);    
1404            c->output = m_strdup (file);
1405            if (!c->output)
1406                BUG (NULL);
1407        }
1408        else {
1409            msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1410            return WPTERR_GENERAL;  
1411        }
1412        if (detached)
1413            c->sigmode = GPGME_SIG_MODE_DETACH;
1414      else {      else {
1415          if (strstr (name, ".asc"))          if (strstr (name, ".asc"))
1416              c->sigmode = GPGME_SIG_MODE_CLEAR;              c->sigmode = GPGME_SIG_MODE_CLEAR;
1417          else          else
1418              c->sigmode = GPGME_SIG_MODE_NORMAL;              c->sigmode = GPGME_SIG_MODE_NORMAL;
1419      }      }
1420        return 0;
1421    }
1422    
1423    
1424    /* Verify the signature from the file @name. If @detached 1,
1425       it is assumed that a detached signature should be checked. */
1426    int
1427    fm_verify (fm_state_t c, int detached, const char *name)
1428    {
1429        gpgme_ctx_t ctx = c->ctx;
1430        gpgme_error_t err;
1431        gpgme_signature_t s;
1432        gpgme_verify_result_t res;
1433        struct file_sig_ctx_s log;
1434        file_data_t in=NULL, out=NULL;
1435        int rc = 0;
1436    
1437        if (strstr (name, ".sig"))
1438            detached = 1;
1439    
1440        if (get_output_file (c, name, detached))
1441            return WPTERR_GENERAL;
1442    
1443      memset (&log, 0, sizeof (log));      memset (&log, 0, sizeof (log));
1444      strcpy (log.file, name); /* XXX: check bounds */      log.file = m_strdup (name);
1445        if (!log.file)
1446            BUG (NULL);
1447      file_verify_create_dlg ();      file_verify_create_dlg ();
1448    
1449      err = gpg_file_data_new (name, 1, &in);      err = gpg_file_data_new (name, 1, &in);
1450      if (err)      if (err)
1451          goto leave;          goto leave;
1452      err = gpg_file_data_new (c->output, 0, &out);      err = gpg_file_data_new (c->output, detached? 1 : 0, &out);
1453      if (err)      if (err)
1454          goto leave;          goto leave;
1455    
1456      if (c->sigmode == GPGME_SIG_MODE_DETACH)      if (c->sigmode == GPGME_SIG_MODE_DETACH)
         err = gpgme_op_verify (ctx, in->dat, NULL, out->dat);  
     else  
1457          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1458            else
1459      res = gpgme_op_verify_result (ctx);          err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat);
     if (err == gpg_error (GPG_ERR_BAD_SIGNATURE)) {  
         log.sig = res->signatures;  
         fm_add_sig_stat (&log);  
         rc = WPTERR_GENERAL;  
         goto leave;  
     }  
1460      if (err) {      if (err) {
1461          msg_box (c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR);          msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1462          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1463          goto leave;          goto leave;
1464      }          }    
1465    
1466            res = gpgme_op_verify_result (ctx);
1467      for (s=res->signatures; s; s=s->next) {      for (s=res->signatures; s; s=s->next) {
1468          log.sig = s;          log.sig = s;
1469          fm_add_sig_stat (&log);          fm_add_sig_stat (&log);
1470      }      }
1471      if (!c->output)      if (!c->output)
1472          c->output = m_strdup( name ); /* for later use */          c->output = m_strdup (name); /* for later use */
1473    
1474  leave:  leave:
1475      if (in)      if (in)
1476          gpg_file_data_release (in);          gpg_file_data_release (in);
1477      if (out)      if (out)
1478          gpg_file_data_release (out);          gpg_file_data_release (out);
1479        if (log.file)
1480            delete []log.file;
1481      return rc;      return rc;
1482  } /* fm_verify */  }
1483    
1484    
1485  int  int
# Line 1492  fm_export (fm_state_t c) Line 1557  fm_export (fm_state_t c)
1557      if (!name)      if (!name)
1558          name = "keys.gpg";          name = "keys.gpg";
1559    
1560      patt = gpg_keylist_to_pattern (rset);      patt = gpg_keylist_to_pattern (rset, c->n_recp);
1561    
1562      err = gpg_file_data_new (name, 0, &keydata);      err = gpg_file_data_new (name, 0, &keydata);
1563      if (err)      if (err)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26