fixed bug in keypair generation

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5263 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-07-01 14:34:11 +00:00
parent efa5460a52
commit e723c8739d

View File

@ -299,10 +299,12 @@ bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& i
return true ;
}
bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, PGPIdType& pgpId, std::string& errString)
bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passphrase, PGPIdType& pgpId, std::string& errString)
{
static const int KEY_NUMBITS = 2048 ;
// 1 - generate keypair - RSA-2048
//
ops_user_id_t uid ;
char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ;
uid.user_id = (unsigned char *)s ;
@ -315,10 +317,6 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
if(!key)
return false ;
// 1 - get a passphrase for encrypting.
std::string passphrase = _passphrase_callback(NULL,PGPIdType(key->key_id).toStdString().c_str(),"Please enter passwd for encrypting your key : ",false) ;
// 2 - save the private key encrypted to a temporary memory buffer
ops_create_info_t *cinfo = NULL ;
@ -327,12 +325,10 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
ops_write_transferable_secret_key(key,(unsigned char *)passphrase.c_str(),passphrase.length(),ops_false,cinfo);
ops_keydata_free(key) ;
// 3 - read the file into a keyring
ops_keyring_t *tmp_keyring = allocateOPSKeyring() ;
if(! ops_keyring_read_from_mem(tmp_keyring, ops_false, buf))
ops_keyring_t *tmp_secring = allocateOPSKeyring() ;
if(! ops_keyring_read_from_mem(tmp_secring, ops_false, buf))
{
std::cerr << "(EE) Cannot re-read key from memory!!" << std::endl;
return false ;
@ -341,31 +337,40 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
// 4 - copy the private key to the private keyring
pgpId = PGPIdType(tmp_keyring->keys[0].key_id) ;
addNewKeyToOPSKeyring(_secring,tmp_keyring->keys[0]) ;
initCertificateInfo(_secret_keyring_map[ pgpId.toStdString() ],&tmp_keyring->keys[0],_secring->nkeys-1) ;
pgpId = PGPIdType(tmp_secring->keys[0].key_id) ;
addNewKeyToOPSKeyring(_secring,tmp_secring->keys[0]) ;
initCertificateInfo(_secret_keyring_map[ pgpId.toStdString() ],&tmp_secring->keys[0],_secring->nkeys-1) ;
#ifdef DEBUG_PGPHANDLER
std::cerr << "Added new secret key with id " << pgpId.toStdString() << " to secret keyring." << std::endl;
#endif
ops_keyring_free(tmp_secring) ;
free(tmp_secring) ;
// 5 - copy the private key to the public keyring
addNewKeyToOPSKeyring(_pubring,tmp_keyring->keys[0]) ;
initCertificateInfo(_public_keyring_map[ pgpId.toStdString() ],&tmp_keyring->keys[0],_pubring->nkeys-1) ;
ops_setup_memory_write(&cinfo, &buf, 0);
ops_write_transferable_public_key(key, ops_false, cinfo);
ops_keyring_t *tmp_pubring = allocateOPSKeyring() ;
if(! ops_keyring_read_from_mem(tmp_pubring, ops_false, buf))
{
std::cerr << "(EE) Cannot re-read key from memory!!" << std::endl;
return false ;
}
ops_teardown_memory_write(cinfo,buf); // cleanup memory
addNewKeyToOPSKeyring(_pubring,tmp_pubring->keys[0]) ;
initCertificateInfo(_public_keyring_map[ pgpId.toStdString() ],&tmp_pubring->keys[0],_pubring->nkeys-1) ;
#ifdef DEBUG_PGPHANDLER
std::cerr << "Added new public key with id " << pgpId.toStdString() << " to public keyring." << std::endl;
#endif
// 6 - clean
ops_keydata_free(key) ;
ops_keyring_free(tmp_keyring) ;
free(tmp_keyring) ;
// 7 - validate own signature and update certificate.
// validateAndUpdateSignatures(_public_keyring_map[ pgpId.toStdString() ],getPublicKey(pgpId)) ;
// 7 - Update flags.
_pubring_changed = true ;
_secring_changed = true ;
@ -546,6 +551,13 @@ bool PGPHandler::encryptTextToFile(const PGPIdType& key_id,const std::string& te
std::cerr << "Cannot get public key of id " << key_id.toStdString() << std::endl;
return false ;
}
if(public_key->type != OPS_PTAG_CT_PUBLIC_KEY)
{
std::cerr << "PGPHandler::encryptTextToFile(): ERROR: supplied id did not return a public key!" << outfile << std::endl;
return false ;
}
if (fd < 0)
{
std::cerr << "PGPHandler::encryptTextToFile(): ERROR: Cannot write to " << outfile << std::endl;
@ -791,7 +803,7 @@ bool PGPHandler::privateTrustCertificate(const PGPIdType& id,int trustlvl)
return false ;
}
if( it->second._validLvl != (int)trustlvl )
if( (int)it->second._validLvl != trustlvl )
_trustdb_changed = true ;
it->second._validLvl = trustlvl ;
@ -908,12 +920,9 @@ bool PGPHandler::locked_syncPublicKeyring()
librs::util::ConvertUtf8ToUtf16(_pubring_path, wfullname);
if(-1 == _wstati64(wfullname.c_str(), &buf))
#else
if(-1 == stat64(_pubring_path.c_str(), &buf))
if(-1 == stat64(_pubring_path.c_str(), &buf))
#endif
{
std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _pubring_path << ". Can't sync public keyring." << std::endl;
return false;
}
std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _pubring_path << ". Can't sync public keyring." << std::endl;
if(_pubring_last_update_time < buf.st_mtime)
{
@ -946,12 +955,10 @@ bool PGPHandler::locked_syncSecretKeyring()
librs::util::ConvertUtf8ToUtf16(_secring_path, wfullname);
if(-1 == _wstati64(wfullname.c_str(), &buf))
#else
if(-1 == stat64(_secring_path.c_str(), &buf))
if(-1 == stat64(_secring_path.c_str(), &buf))
#endif
{
std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _secring_path << ". Can't sync secret keyring." << std::endl;
return false;
}
std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _secring_path << ". Can't sync secret keyring." << std::endl;
#ifdef TODO
if(_secring_last_update_time < buf.st_mtime)
{