improved error handling

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5289 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-07-12 19:20:31 +00:00
parent 749dd01d77
commit f5c276c9b5
11 changed files with 124 additions and 101 deletions

View file

@ -527,15 +527,17 @@ bool PGPHandler::exportGPGKeyPair(const std::string& filename,const PGPIdType& e
return true ;
}
bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& imported_key_id)
bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& imported_key_id,std::string& import_error)
{
import_error = "" ;
// 1 - Test for file existance
//
FILE *ftest = fopen(filename.c_str(),"r") ;
if(ftest == NULL)
{
std::cerr << "Cannot open file " << filename << " for read. Please check access permissions." << std::endl;
import_error = "Cannot open file " + filename + " for read. Please check access permissions." ;
return false ;
}
@ -547,12 +549,14 @@ bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& importe
if(ops_false == ops_keyring_read_from_file(tmp_keyring, ops_true, filename.c_str()))
{
std::cerr << "PGPHandler::readKeyRing(): cannot read key file. File corrupted?" << std::endl ;
import_error = "PGPHandler::readKeyRing(): cannot read key file. File corrupted?" ;
return false ;
}
if(tmp_keyring->nkeys != 2)
{
std::cerr << "PGPHandler::importKeyPair(): file does not contain a valid keypair." << std::endl ;
import_error = "PGPHandler::importKeyPair(): file does not contain a valid keypair." ;
if(tmp_keyring->nkeys > 2)
import_error += "\nMake sure that your key is a RSA key (DSA is not yet supported) and does not contain subkeys (not supported yet).";
return false ;
}
@ -567,6 +571,7 @@ bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& importe
seckey = &tmp_keyring->keys[0] ;
else
{
import_error = "Unrecognised key type in key file for key #0. Giving up." ;
std::cerr << "Unrecognised key type " << tmp_keyring->keys[0].type << " in key file for key #0. Giving up." << std::endl;
return false ;
}
@ -576,18 +581,24 @@ bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& importe
seckey = &tmp_keyring->keys[1] ;
else
{
import_error = "Unrecognised key type in key file for key #1. Giving up." ;
std::cerr << "Unrecognised key type " << tmp_keyring->keys[1].type << " in key file for key #1. Giving up." << std::endl;
return false ;
}
if(pubkey == NULL || seckey == NULL || pubkey == seckey)
{
std::cerr << "File does not contain a public and a private key. Sorry." << std::endl;
import_error = "File does not contain a public and a private key. Sorry." ;
return false ;
}
if(memcmp(pubkey->fingerprint.fingerprint,seckey->fingerprint.fingerprint,KEY_FINGERPRINT_SIZE) != 0)
{
std::cerr << "Public and private keys do nt have the same fingerprint. Sorry!" << std::endl;
import_error = "Public and private keys do nt have the same fingerprint. Sorry!" ;
return false ;
}
if(pubkey->key.pkey.version != 4)
{
import_error = "Public key is not version 4. Rejected!" ;
return false ;
}
@ -602,7 +613,7 @@ bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& importe
if( (!ops_validate_key_signatures(result, const_cast<ops_keydata_t*>(pubkey), &dummy_keyring, cb_get_passphrase)) || result->valid_count != 1 || result->invalid_count > 0)
{
std::cerr << "Cannot validate self signature for the imported key. Sorry." << std::endl;
import_error = "Cannot validate self signature for the imported key. Sorry." ;
return false ;
}
ops_validate_result_free(result);
@ -622,13 +633,13 @@ bool PGPHandler::importGPGKeyPair(const std::string& filename,PGPIdType& importe
if(!ops_write_transferable_secret_key_from_packet_data(seckey,ops_false,cinfo))
{
std::cerr << "(EE) Cannot encode secret key to disk!! Disk full? Out of disk quota?" << std::endl;
import_error = "(EE) Cannot encode secret key to disk!! Disk full? Out of disk quota?" ;
return false ;
}
ops_teardown_file_write(cinfo,fd) ;
}
else
std::cerr << "Private key already exists! Not importing it again." << std::endl;
import_error = "Private key already exists! Not importing it again." ;
if(addOrMergeKey(_pubring,_public_keyring_map,pubkey))
_pubring_changed = true ;

View file

@ -72,7 +72,7 @@ class PGPHandler
bool getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) const ;
bool haveSecretKey(const PGPIdType& id) const ;
bool importGPGKeyPair(const std::string& filename,PGPIdType& imported_id) ;
bool importGPGKeyPair(const std::string& filename,PGPIdType& imported_id,std::string& import_error) ;
bool exportGPGKeyPair(const std::string& filename,const PGPIdType& exported_id) const ;
bool availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids);

View file

@ -304,11 +304,11 @@ bool AuthGPG::exportProfile(const std::string& fname,const std::string& exported
return PGPHandler::exportGPGKeyPair(fname,PGPIdType(exported_id)) ;
}
bool AuthGPG::importProfile(const std::string& fname,std::string& imported_id)
bool AuthGPG::importProfile(const std::string& fname,std::string& imported_id,std::string& import_error)
{
PGPIdType id ;
if(PGPHandler::importGPGKeyPair(fname,id))
if(PGPHandler::importGPGKeyPair(fname,id,import_error))
{
imported_id = id.toStdString() ;
return true ;

View file

@ -172,7 +172,7 @@ class AuthGPG: public p3Config, public RsThread, public PGPHandler
virtual bool getGPGValidList(std::list<std::string> &ids);
virtual bool getGPGAcceptedList(std::list<std::string> &ids);
virtual bool getGPGSignedList(std::list<std::string> &ids);
virtual bool importProfile(const std::string& filename,std::string& gpg_id) ;
virtual bool importProfile(const std::string& filename,std::string& gpg_id,std::string& import_error) ;
virtual bool exportProfile(const std::string& filename,const std::string& gpg_id) ;
/*********************************************************************************/

View file

@ -81,7 +81,7 @@ class RsInit
static bool ValidateCertificate(std::string &userName) ;
static bool exportIdentity(const std::string& fname,const std::string& pgp_id) ;
static bool importIdentity(const std::string& fname,std::string& imported_pgp_id) ;
static bool importIdentity(const std::string& fname,std::string& imported_pgp_id,std::string& import_error) ;
/*!
* Generating GPGme Account

View file

@ -713,9 +713,9 @@ bool RsInit::exportIdentity(const std::string& fname,const std::string& id)
return AuthGPG::getAuthGPG()->exportProfile(fname,id);
}
bool RsInit::importIdentity(const std::string& fname,std::string& id)
bool RsInit::importIdentity(const std::string& fname,std::string& id,std::string& import_error)
{
return AuthGPG::getAuthGPG()->importProfile(fname,id);
return AuthGPG::getAuthGPG()->importProfile(fname,id,import_error);
}
bool RsInit::copyGnuPGKeyrings()