mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
added caching of GPG information to PGPHandler
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5086 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
b43fb7e8b3
commit
fa429ad8d4
5 changed files with 316 additions and 479 deletions
|
@ -177,7 +177,7 @@ PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring,Pa
|
|||
int i=0 ;
|
||||
while( (keydata = ops_keyring_get_key_by_index(_pubring,i)) != NULL )
|
||||
{
|
||||
_public_keyring_map[ PGPIdType(keydata->key_id).toUInt64() ] = i ;
|
||||
initCertificateInfo(_public_keyring_map[ PGPIdType(keydata->key_id).toStdString() ],keydata,i) ;
|
||||
++i ;
|
||||
}
|
||||
|
||||
|
@ -189,13 +189,39 @@ PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring,Pa
|
|||
i=0 ;
|
||||
while( (keydata = ops_keyring_get_key_by_index(_secring,i)) != NULL )
|
||||
{
|
||||
_secret_keyring_map[ PGPIdType(keydata->key_id).toUInt64() ] = i ;
|
||||
initCertificateInfo(_secret_keyring_map[ PGPIdType(keydata->key_id).toStdString() ],keydata,i) ;
|
||||
++i ;
|
||||
}
|
||||
|
||||
std::cerr << "Secring read successfully." << std::endl;
|
||||
}
|
||||
|
||||
void PGPHandler::initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t index)
|
||||
{
|
||||
// Parse certificate name
|
||||
//
|
||||
std::string namestring( (char *)keydata->uids[0].user_id ) ;
|
||||
|
||||
cert._name = "" ;
|
||||
int i=0;
|
||||
while(i < namestring.length() && namestring[i] != '(' && namestring[i] != '<') { cert._name += namestring[i] ; ++i ;}
|
||||
|
||||
std::string& next = (namestring[i] == '(')?cert._comment:cert._email ;
|
||||
next = "" ;
|
||||
while(i < namestring.length() && namestring[i] != '(' && namestring[i] != '<') { cert._name += namestring[i] ; ++i ;}
|
||||
next += namestring[i] ;
|
||||
|
||||
next = (namestring[i] == '(')?cert._comment:cert._email ;
|
||||
next = "" ;
|
||||
while(i < namestring.length() && namestring[i] != '(' && namestring[i] != '<') { cert._name += namestring[i] ; ++i ;}
|
||||
next += namestring[i] ;
|
||||
|
||||
cert._trustLvl = 1 ; // to be setup accordingly
|
||||
cert._key_index = index ;
|
||||
|
||||
std::cerr << __PRETTY_FUNCTION__ << ": unfinished!!" << std::endl;
|
||||
}
|
||||
|
||||
PGPHandler::~PGPHandler()
|
||||
{
|
||||
std::cerr << "Freeing PGPHandler. Deleting keyrings." << std::endl;
|
||||
|
@ -209,13 +235,42 @@ PGPHandler::~PGPHandler()
|
|||
free(_secring) ;
|
||||
}
|
||||
|
||||
void PGPHandler::printKeys() const
|
||||
bool PGPHandler::printKeys() const
|
||||
{
|
||||
std::cerr << "Public keyring: " << std::endl;
|
||||
ops_keyring_list(_pubring) ;
|
||||
for(std::map<std::string,PGPCertificateInfo>::const_iterator it(_public_keyring_map.begin()); it != _public_keyring_map.end(); it++)
|
||||
{
|
||||
std::cerr << "PGP Key: " << it->first << std::endl;
|
||||
|
||||
std::cerr << "Secret keyring: " << std::endl;
|
||||
ops_keyring_list(_secring) ;
|
||||
std::cerr << "\tName : " << it->second._name << std::endl;
|
||||
std::cerr << "\tEmail : " << it->second._email << std::endl;
|
||||
std::cerr << "\tOwnSign : " << (it->second._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE) << std::endl;
|
||||
std::cerr << "\tAccept Connect: " << (it->second._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE) << std::endl;
|
||||
std::cerr << "\ttrustLvl : " << it->second._trustLvl << std::endl;
|
||||
std::cerr << "\tvalidLvl : " << it->second._validLvl << std::endl;
|
||||
|
||||
std::set<std::string>::const_iterator sit;
|
||||
for(sit = it->second.signers.begin(); sit != it->second.signers.end(); sit++)
|
||||
{
|
||||
std::cerr << "\t\tSigner ID:" << *sit << ", Name: " ;
|
||||
const PGPCertificateInfo *info = PGPHandler::getCertificateInfo(PGPIdType::fromUserId_hex(*sit)) ;
|
||||
|
||||
if(info != NULL)
|
||||
std::cerr << info->_name ;
|
||||
|
||||
std::cerr << std::endl ;
|
||||
}
|
||||
}
|
||||
return true ;
|
||||
}
|
||||
|
||||
const PGPCertificateInfo *PGPHandler::getCertificateInfo(const PGPIdType& id) const
|
||||
{
|
||||
std::map<std::string,PGPCertificateInfo>::const_iterator it( _public_keyring_map.find(id.toStdString()) ) ;
|
||||
|
||||
if(it != _public_keyring_map.end())
|
||||
return &it->second;
|
||||
else
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids)
|
||||
|
@ -305,14 +360,14 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
|
|||
|
||||
pgpId = PGPIdType(tmp_keyring->keys[0].key_id) ;
|
||||
addNewKeyToOPSKeyring(_secring,tmp_keyring->keys[0]) ;
|
||||
_secret_keyring_map[ pgpId.toUInt64() ] = _secring->nkeys-1 ;
|
||||
initCertificateInfo(_secret_keyring_map[ pgpId.toStdString() ],&tmp_keyring->keys[0],_secring->nkeys-1) ;
|
||||
|
||||
std::cerr << "Added new secret key with id " << pgpId.toStdString() << " to secret keyring." << std::endl;
|
||||
|
||||
// 5 - copy the private key to the public keyring
|
||||
|
||||
addNewKeyToOPSKeyring(_pubring,tmp_keyring->keys[0]) ;
|
||||
_public_keyring_map[ pgpId.toUInt64() ] = _pubring->nkeys-1 ;
|
||||
initCertificateInfo(_public_keyring_map[ pgpId.toStdString() ],&tmp_keyring->keys[0],_pubring->nkeys-1) ;
|
||||
|
||||
std::cerr << "Added new public key with id " << pgpId.toStdString() << " to public keyring." << std::endl;
|
||||
|
||||
|
@ -344,21 +399,21 @@ std::string PGPHandler::makeRadixEncodedPGPKey(const ops_keydata_t *key)
|
|||
|
||||
const ops_keydata_t *PGPHandler::getSecretKey(const PGPIdType& id) const
|
||||
{
|
||||
std::map<uint64_t,uint32_t>::const_iterator res = _secret_keyring_map.find(id.toUInt64()) ;
|
||||
std::map<std::string,PGPCertificateInfo>::const_iterator res = _secret_keyring_map.find(id.toStdString()) ;
|
||||
|
||||
if(res == _secret_keyring_map.end())
|
||||
return NULL ;
|
||||
else
|
||||
return ops_keyring_get_key_by_index(_secring,res->second) ;
|
||||
return ops_keyring_get_key_by_index(_secring,res->second._key_index) ;
|
||||
}
|
||||
const ops_keydata_t *PGPHandler::getPublicKey(const PGPIdType& id) const
|
||||
{
|
||||
std::map<uint64_t,uint32_t>::const_iterator res = _public_keyring_map.find(id.toUInt64()) ;
|
||||
std::map<std::string,PGPCertificateInfo>::const_iterator res = _public_keyring_map.find(id.toStdString()) ;
|
||||
|
||||
if(res == _public_keyring_map.end())
|
||||
return NULL ;
|
||||
else
|
||||
return ops_keyring_get_key_by_index(_pubring,res->second) ;
|
||||
return ops_keyring_get_key_by_index(_pubring,res->second._key_index) ;
|
||||
}
|
||||
|
||||
std::string PGPHandler::SaveCertificateToString(const PGPIdType& id,bool include_signatures)
|
||||
|
@ -414,7 +469,7 @@ bool PGPHandler::LoadCertificateFromString(const std::string& pgp_cert,PGPIdType
|
|||
id = PGPIdType(keydata->key_id) ;
|
||||
|
||||
addNewKeyToOPSKeyring(_pubring,*keydata) ;
|
||||
_public_keyring_map[id.toUInt64()] = _pubring->nkeys-1 ;
|
||||
initCertificateInfo(_public_keyring_map[id.toStdString()],keydata,_pubring->nkeys-1) ;
|
||||
}
|
||||
|
||||
std::cerr << "Added the key in the main public keyring." << std::endl;
|
||||
|
@ -628,3 +683,42 @@ bool PGPHandler::VerifySignBin(const void *data, uint32_t data_len, unsigned cha
|
|||
return false ;
|
||||
}
|
||||
|
||||
void PGPHandler::setAcceptConnexion(const PGPIdType& id,bool b)
|
||||
{
|
||||
std::map<std::string,PGPCertificateInfo>::iterator res = _public_keyring_map.find(id.toStdString()) ;
|
||||
|
||||
if(res != _public_keyring_map.end())
|
||||
if(b)
|
||||
res->second._flags |= PGPCertificateInfo::PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION ;
|
||||
else
|
||||
res->second._flags &= ~PGPCertificateInfo::PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION ;
|
||||
}
|
||||
|
||||
bool PGPHandler::getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(const PGPCertificateInfo&)) const
|
||||
{
|
||||
list.clear() ;
|
||||
|
||||
for(std::map<std::string,PGPCertificateInfo>::const_iterator it(_public_keyring_map.begin());it!=_public_keyring_map.end();++it)
|
||||
if( filter == NULL || (*filter)(it->second) )
|
||||
list.push_back(PGPIdType::fromUserId_hex(it->first)) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool PGPHandler::isGPGId(const std::string &id)
|
||||
{
|
||||
return _public_keyring_map.find(id) != _public_keyring_map.end() ;
|
||||
}
|
||||
|
||||
bool PGPHandler::isGPGSigned(const std::string &id)
|
||||
{
|
||||
std::map<std::string,PGPCertificateInfo>::const_iterator res = _public_keyring_map.find(id) ;
|
||||
return res != _public_keyring_map.end() && (res->second._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE) ;
|
||||
}
|
||||
|
||||
bool PGPHandler::isGPGAccepted(const std::string &id)
|
||||
{
|
||||
std::map<std::string,PGPCertificateInfo>::const_iterator res = _public_keyring_map.find(id) ;
|
||||
return res != _public_keyring_map.end() && (res->second._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE) ;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <util/rsthreads.h>
|
||||
|
||||
extern "C" {
|
||||
|
@ -33,6 +34,7 @@ class PGPIdType
|
|||
|
||||
private:
|
||||
unsigned char bytes[KEY_ID_SIZE] ;
|
||||
std::string _string_id ;
|
||||
};
|
||||
class PGPFingerprintType
|
||||
{
|
||||
|
@ -60,6 +62,32 @@ class PGPFingerprintType
|
|||
PGPFingerprintType() {}
|
||||
private:
|
||||
unsigned char bytes[KEY_FINGERPRINT_SIZE] ;
|
||||
std::string _string_id ;
|
||||
};
|
||||
|
||||
class PGPCertificateInfo
|
||||
{
|
||||
public:
|
||||
PGPCertificateInfo() {}
|
||||
|
||||
std::string _name;
|
||||
std::string _email;
|
||||
std::string _comment;
|
||||
|
||||
std::set<std::string> signers;
|
||||
|
||||
uint32_t _trustLvl;
|
||||
uint32_t _validLvl;
|
||||
uint32_t _flags ;
|
||||
|
||||
PGPFingerprintType _fpr; /* fingerprint */
|
||||
PGPIdType _key_id ;
|
||||
|
||||
uint32_t _key_index ; // index to array of keys in the public keyring
|
||||
|
||||
static const uint32_t PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION = 0x0001 ;
|
||||
static const uint32_t PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE = 0x0002 ;
|
||||
static const uint32_t PGP_CERTIFICATE_FLAG_HAS_SIGNED_ME = 0x0004 ;
|
||||
};
|
||||
|
||||
class PGPHandler
|
||||
|
@ -72,6 +100,7 @@ class PGPHandler
|
|||
/**
|
||||
* @param ids list of gpg certificate ids (note, not the actual certificates)
|
||||
*/
|
||||
bool getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) const ;
|
||||
|
||||
bool availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids);
|
||||
bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, PGPIdType& pgpId, std::string& errString) ;
|
||||
|
@ -89,14 +118,24 @@ class PGPHandler
|
|||
|
||||
bool getKeyFingerprint(const PGPIdType& id,PGPFingerprintType& fp) const ;
|
||||
|
||||
void setAcceptConnexion(const PGPIdType&,bool) ;
|
||||
|
||||
// Debug stuff.
|
||||
virtual void printKeys() const ;
|
||||
virtual bool printKeys() const ;
|
||||
|
||||
const PGPCertificateInfo *getCertificateInfo(const PGPIdType& id) const ;
|
||||
|
||||
bool isGPGId(const std::string &id);
|
||||
bool isGPGSigned(const std::string &id);
|
||||
bool isGPGAccepted(const std::string &id);
|
||||
|
||||
private:
|
||||
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ;
|
||||
static ops_keyring_t *allocateOPSKeyring() ;
|
||||
static void addNewKeyToOPSKeyring(ops_keyring_t*, const ops_keydata_t&) ;
|
||||
|
||||
void initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t i) ;
|
||||
|
||||
const ops_keydata_t *getPublicKey(const PGPIdType&) const ;
|
||||
const ops_keydata_t *getSecretKey(const PGPIdType&) const ;
|
||||
|
||||
|
@ -105,8 +144,8 @@ class PGPHandler
|
|||
ops_keyring_t *_pubring ;
|
||||
ops_keyring_t *_secring ;
|
||||
|
||||
std::map<uint64_t,uint32_t> _public_keyring_map ; // used for fast access to keys. Gives the index in the keyring.
|
||||
std::map<uint64_t,uint32_t> _secret_keyring_map ;
|
||||
std::map<std::string,PGPCertificateInfo> _public_keyring_map ; // used for fast access to keys. Gives the index in the keyring.
|
||||
std::map<std::string,PGPCertificateInfo> _secret_keyring_map ;
|
||||
|
||||
const std::string _pubring_path ;
|
||||
const std::string _secring_path ;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue