/[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 25 by twoaday, Wed Oct 12 10:04:26 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 (!get_seckey (r->keyid, &key))
1112                return true;
1113        }
1114        return false;
1115    }
1116    
1117    
1118  /* Decrypt the file @name. */  /* Decrypt the file @name. */
1119  int  int
1120  fm_decrypt (fm_state_t c, const char *name)  fm_decrypt (fm_state_t c, const char *name)
1121  {  {
1122      gpgme_error_t err;      gpgme_error_t err;
1123      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;    
     gpgme_signature_t sig = NULL;  
1124      gpgme_decrypt_result_t res;      gpgme_decrypt_result_t res;
1125      gpgme_verify_result_t sigres;      gpgme_verify_result_t sigres;
1126      file_data_t in =NULL, out=NULL;      file_data_t in =NULL, out=NULL;
1127      char keyid[17];      int is_signed = 0;
     int is_signed = 0, sigok = 0;  
1128      int rc = 0;      int rc = 0;
1129            
1130      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 1139  fm_decrypt (fm_state_t c, const char *na
1139      if (is_openpgp_ext (c->output))      if (is_openpgp_ext (c->output))
1140          c->output[strlen (c->output)-4] = '\0';          c->output[strlen (c->output)-4] = '\0';
1141      else {      else {
1142          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"),  
1143                                            NULL, NULL);                                            NULL, NULL);
1144          if( s ) {          if (s) {
1145              free_if_alloc (c->output);              free_if_alloc (c->output);
1146              c->output = m_strdup (s);              c->output = m_strdup (s);
1147              if (!c->output)              if (!c->output)
# Line 1124  fm_decrypt (fm_state_t c, const char *na Line 1173  fm_decrypt (fm_state_t c, const char *na
1173      }      }
1174    
1175      res = gpgme_op_decrypt_result (ctx);      res = gpgme_op_decrypt_result (ctx);
1176      if (res && gpgme_err_code (err) == GPG_ERR_NO_SECKEY) {      if (res && res->recipients && !secret_key_available (res->recipients)) {
1177            const char *keyid = res->recipients->keyid;
1178          char *p = get_key_userid (keyid+8);          char *p = get_key_userid (keyid+8);
1179          gpgme_pubkey_algo_t pkalgo = (gpgme_pubkey_algo_t)0;          gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;
1180            
         if (res->recipients)  
             pkalgo = res->recipients->pubkey_algo;  
1181          log_box( _("Decryption"), MB_ERR,          log_box( _("Decryption"), MB_ERR,
1182                   _("Encrypted with %s key, ID %s.%s\n"                   _("Encrypted with %s key, ID %s.%s\n"
1183                     "Decryption failed: secret key not available."),                     "Decryption failed: secret key not available."),
# Line 1144  fm_decrypt (fm_state_t c, const char *na Line 1192  fm_decrypt (fm_state_t c, const char *na
1192          goto leave;          goto leave;
1193      }      }
1194      if (file_exist_check (c->output)) {      if (file_exist_check (c->output)) {
         //msg_box( c->dlg, _("Decryption failed"), _("Decrypt"), MB_ERR );  
1195          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);
1196          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1197      }      }
1198            
1199      sigres = gpgme_op_verify_result (ctx);      sigres = gpgme_op_verify_result (ctx);
1200      if (sigres && sigres->signatures) {      if (sigres && sigres->signatures)
1201          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;  
1202    
         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"));  
         }  
     }  
1203            
1204  leave:  leave:
1205      if (in)      if (in)
# Line 1188  fm_sign (fm_state_t c, const char * name Line 1219  fm_sign (fm_state_t c, const char * name
1219      file_data_t in=NULL, out=NULL;      file_data_t in=NULL, out=NULL;
1220      char ext[5];      char ext[5];
1221    
1222      if( !c->init_cb || !c->cache_cb ) {      if (!c->init_cb || !c->cache_cb) {
1223          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") );
1224          c->init_cb = 1;          c->init_cb = 1;
1225      }      }
1226            
1227      free_if_alloc (c->output);      free_if_alloc (c->output);
1228      c->output = new char[strlen( name ) + 5 + 1];      c->output = new char[strlen (name) + 5 + 1];
1229      if( !c->output)      if( !c->output)
1230          BUG( NULL );          BUG( NULL );
1231      strcpy( ext, file_get_extension( ctx, c->sigmode ) );      strcpy (ext, file_get_extension (ctx, c->sigmode));
1232      strcpy( c->output, name );      strcpy (c->output, name);
1233      strcat( c->output, ext );      strcat (c->output, ext);
1234            
1235      if (!overwrite_file (c->output)) {      if (!overwrite_file (c->output)) {
1236          rc = ask_filename (c, _("Enter filename for signed file"), NULL);          rc = ask_filename (c, _("Enter filename for signed file"), NULL);
1237          if (rc)          if (rc)
1238              goto leave;              goto leave;
1239      }      }
1240      remove_crit_file_attrs( c->output, 0 );      remove_crit_file_attrs (c->output, 0);
1241    
1242      err = gpg_file_data_new (name, 1, &in);      err = gpg_file_data_new (name, 1, &in);
1243      if (err)      if (err)
# Line 1234  leave: Line 1265  leave:
1265      if (out)      if (out)
1266          gpg_file_data_release (out);          gpg_file_data_release (out);
1267      return rc;      return rc;
1268  } /* fm_sign */  }
1269    
1270    
1271  static int  static int
1272  fm_add_sig_stat( siglog_context_t log )  fm_add_sig_stat (file_sig_ctx_t log)
1273  {  {
1274      gpgme_key_t key;          gpgme_key_t key;    
1275      const char *kid;      const char *kid;
# Line 1246  fm_add_sig_stat( siglog_context_t log ) Line 1277  fm_add_sig_stat( siglog_context_t log )
1277    
1278      kid = log->sig->fpr;      kid = log->sig->fpr;
1279      if (!kid)      if (!kid)
1280          kid = "????????????????";          BUG (NULL);
1281      if (strlen (kid) == 40)      if (strlen (kid) == 40)
1282          kid += 32;          kid += 32;      
1283      else      else if (strlen (kid) == 32)
1284          kid += 24;          kid += 24;
1285      if (get_pubkey (kid, &key))      if (get_pubkey (kid, &key))
1286          not_found = 1;          log->use_uid = 0;
1287      log->use_uid = 0;      else {
     if (!not_found) {  
1288          log->user_id = key->uids->uid;          log->user_id = key->uids->uid;
1289          log->use_uid = 1;                log->use_uid = 1;
1290      }      }
1291      file_verify_add_state (log);      file_verify_add_state (log);
   
1292      return 0;      return 0;
1293  } /* fm_add_sig_stat */  }
1294    
1295    
1296  static int  static int
# Line 1332  fm_verify_pasted_detsig (listview_ctrl_t Line 1361  fm_verify_pasted_detsig (listview_ctrl_t
1361  }  }
1362    
1363    
1364  int  /* Extract automatically the output file name from @name.
1365  fm_verify (fm_state_t c, int detached, const char *name)     If @detached is 1, a detached sig is assumed. */
1366    static int
1367    get_output_file (fm_state_t c, const char *name, int detached)
1368  {  {
1369      gpgme_ctx_t ctx = c->ctx;      const char *file = NULL;
1370      gpgme_error_t err;      const char *title;
1371      gpgme_signature_t s;      char fname[384];
1372      gpgme_verify_result_t res;      
1373      struct siglog_context_s log;      if (detached)
1374      file_data_t in=NULL, out=NULL;          title = _("Select Data File");
1375      int rc = 0;      else
1376                    title = _("Selected Output File");
1377      if (detached) {  
1378          const char *file = NULL;      if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) {
1379          if (strstr (name, ".sig") || strstr (name, ".asc")) {          _snprintf (fname, sizeof (fname) - 1, "%s", name);
1380              char fname[512];          fname[strlen (fname) - 4] = '\0';
1381              _snprintf (fname, sizeof (fname) - 1, "%s", name);          if (file_exist_check (fname) == 0 && detached)  
1382              fname[strlen (fname) - 4] = '\0';              file = fname;
1383              if (file_exist_check (fname) == 0)          else if (!detached) {
1384                /* If the signature is clear or normal, make sure we do not
1385                   overwrite the original file if it exists. */
1386                if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {
1387                    file = get_filesave_dlg (c->dlg, title, NULL, NULL);
1388                    if (!file)
1389                        return WPTERR_GENERAL;
1390                }
1391                else
1392                  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);  
1393          }          }
1394          else {      }
1395              msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);      if (!file)
1396              return WPTERR_GENERAL;          file = get_fileopen_dlg (c->dlg, title, NULL, NULL);
1397          }      if (file) {    
1398          c->sigmode = GPGME_SIG_MODE_DETACH;          free_if_alloc (c->output);    
1399            c->output = m_strdup (file);
1400            if (!c->output)
1401                BUG (NULL);
1402      }      }
1403      else {      else {
1404            msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1405            return WPTERR_GENERAL;  
1406        }
1407        if (detached)
1408            c->sigmode = GPGME_SIG_MODE_DETACH;
1409        else {
1410          if (strstr (name, ".asc"))          if (strstr (name, ".asc"))
1411              c->sigmode = GPGME_SIG_MODE_CLEAR;              c->sigmode = GPGME_SIG_MODE_CLEAR;
1412          else          else
1413              c->sigmode = GPGME_SIG_MODE_NORMAL;              c->sigmode = GPGME_SIG_MODE_NORMAL;
1414      }      }
1415        return 0;
1416    }
1417    
1418    
1419    /* Verify the signature from the file @name. If @detached 1,
1420       it is assumed that a detached signature should be checked. */
1421    int
1422    fm_verify (fm_state_t c, int detached, const char *name)
1423    {
1424        gpgme_ctx_t ctx = c->ctx;
1425        gpgme_error_t err;
1426        gpgme_signature_t s;
1427        gpgme_verify_result_t res;
1428        struct file_sig_ctx_s log;
1429        file_data_t in=NULL, out=NULL;
1430        int rc = 0;
1431    
1432        if (strstr (name, ".sig"))
1433            detached = 1;
1434    
1435        if (get_output_file (c, name, detached))
1436            return WPTERR_GENERAL;
1437    
1438      memset (&log, 0, sizeof (log));      memset (&log, 0, sizeof (log));
1439      strcpy (log.file, name); /* XXX: check bounds */      log.file = m_strdup (name);
1440        if (!log.file)
1441            BUG (NULL);
1442      file_verify_create_dlg ();      file_verify_create_dlg ();
1443    
1444      err = gpg_file_data_new (name, 1, &in);      err = gpg_file_data_new (name, 1, &in);
1445      if (err)      if (err)
1446          goto leave;          goto leave;
1447      err = gpg_file_data_new (c->output, 0, &out);      err = gpg_file_data_new (c->output, detached? 1 : 0, &out);
1448      if (err)      if (err)
1449          goto leave;          goto leave;
1450    
1451      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  
1452          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1453            else
1454      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;  
     }  
1455      if (err) {      if (err) {
1456          msg_box (c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR);          msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1457          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1458          goto leave;          goto leave;
1459      }          }    
1460    
1461            res = gpgme_op_verify_result (ctx);
1462      for (s=res->signatures; s; s=s->next) {      for (s=res->signatures; s; s=s->next) {
1463          log.sig = s;          log.sig = s;
1464          fm_add_sig_stat (&log);          fm_add_sig_stat (&log);
1465      }      }
1466      if (!c->output)      if (!c->output)
1467          c->output = m_strdup( name ); /* for later use */          c->output = m_strdup (name); /* for later use */
1468    
1469  leave:  leave:
1470      if (in)      if (in)
1471          gpg_file_data_release (in);          gpg_file_data_release (in);
1472      if (out)      if (out)
1473          gpg_file_data_release (out);          gpg_file_data_release (out);
1474        if (log.file)
1475            delete []log.file;
1476      return rc;      return rc;
1477  } /* fm_verify */  }
1478    
1479    
1480  int  int
# Line 1492  fm_export (fm_state_t c) Line 1552  fm_export (fm_state_t c)
1552      if (!name)      if (!name)
1553          name = "keys.gpg";          name = "keys.gpg";
1554    
1555      patt = gpg_keylist_to_pattern (rset);      patt = gpg_keylist_to_pattern (rset, c->n_recp);
1556    
1557      err = gpg_file_data_new (name, 0, &keydata);      err = gpg_file_data_new (name, 0, &keydata);
1558      if (err)      if (err)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26