mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-24 23:19:29 -05:00
added new individual config saving system
global signature file format will be converted git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3514 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
831c73dd9d
commit
52e0d2a095
@ -1039,13 +1039,19 @@ bool AuthSSLimpl::decrypt(void *&out, int &outlen, const void *in, int inlen)
|
||||
int in_offset = 0, out_currOffset = 0;
|
||||
int size_net_ekl = sizeof(net_ekl);
|
||||
|
||||
if(size_net_ekl > inlen) return false;
|
||||
|
||||
memcpy(&net_ekl, (unsigned char*)in, size_net_ekl);
|
||||
eklen = ntohl(net_ekl);
|
||||
in_offset += size_net_ekl;
|
||||
|
||||
if(eklen > (inlen-in_offset)) return false;
|
||||
|
||||
memcpy(ek, (unsigned char*)in + in_offset, eklen);
|
||||
in_offset += eklen;
|
||||
|
||||
if(EVP_MAX_IV_LENGTH > (inlen-in_offset)) return false;
|
||||
|
||||
memcpy(iv, (unsigned char*)in + in_offset, EVP_MAX_IV_LENGTH);
|
||||
in_offset += EVP_MAX_IV_LENGTH;
|
||||
|
||||
@ -1064,7 +1070,8 @@ bool AuthSSLimpl::decrypt(void *&out, int &outlen, const void *in, int inlen)
|
||||
|
||||
outlen += out_currOffset;
|
||||
|
||||
free(ek);
|
||||
if(ek != NULL)
|
||||
free(ek);
|
||||
|
||||
#ifdef AUTHSSL_DEBUG
|
||||
std::cerr << "AuthSSLimpl::decrypt() finished with outlen : " << outlen << std::endl;
|
||||
|
@ -45,6 +45,11 @@ p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame
|
||||
:basedir(dir), metafname(fname), metasigfname(signame),
|
||||
mConfigSaveActive(true)
|
||||
{
|
||||
oldConfigType = checkForGlobalSigConfig();
|
||||
|
||||
// configuration to load correct global types
|
||||
pqiConfig::globalConfigType = oldConfigType;
|
||||
|
||||
}
|
||||
|
||||
void p3ConfigMgr::tick()
|
||||
@ -90,10 +95,59 @@ void p3ConfigMgr::saveConfiguration()
|
||||
if(!RsDiscSpace::checkForDiscSpace(RS_CONFIG_DIRECTORY))
|
||||
return ;
|
||||
|
||||
saveConfig();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void p3ConfigMgr::saveConfig()
|
||||
{
|
||||
|
||||
bool ok= true;
|
||||
|
||||
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
||||
|
||||
std::map<uint32_t, pqiConfig *>::iterator it;
|
||||
for(it = configs.begin(); it != configs.end(); it++)
|
||||
{
|
||||
if (it->second->HasConfigChanged(1))
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig() Saving Element: ";
|
||||
std::cerr << it->first;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
ok &= it->second->saveConfiguration();
|
||||
}
|
||||
/* save metaconfig */
|
||||
}
|
||||
|
||||
if(ok && oldConfigType)
|
||||
removeOldConfigType();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void p3ConfigMgr::removeOldConfigType()
|
||||
{
|
||||
std::string fName = basedir + "/" + metafname;
|
||||
std::string sigfName = basedir + "/" + metasigfname;
|
||||
|
||||
remove(fName.c_str());
|
||||
remove(sigfName.c_str());
|
||||
|
||||
//now set globalconfig type to false so mgr saves
|
||||
oldConfigType = false;
|
||||
pqiConfig::globalConfigType = oldConfigType;
|
||||
|
||||
}
|
||||
void p3ConfigMgr::globalSaveConfig()
|
||||
{
|
||||
|
||||
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::saveConfiguration()";
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig()";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
@ -105,7 +159,7 @@ void p3ConfigMgr::saveConfiguration()
|
||||
if (it->second->HasConfigChanged(1))
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::saveConfiguration() Saving Element: ";
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig() Saving Element: ";
|
||||
std::cerr << it->first;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
@ -114,7 +168,7 @@ void p3ConfigMgr::saveConfiguration()
|
||||
/* save metaconfig */
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::saveConfiguration() Element: ";
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig() Element: ";
|
||||
std::cerr << it->first << " Hash: " << it->second->Hash();
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
@ -135,7 +189,7 @@ void p3ConfigMgr::saveConfiguration()
|
||||
}
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::saveConfiguration() Complete MetaConfigItem: ";
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig() Complete MetaConfigItem: ";
|
||||
std::cerr << std::endl;
|
||||
item->print(std::cerr, 20);
|
||||
|
||||
@ -174,7 +228,7 @@ void p3ConfigMgr::saveConfiguration()
|
||||
signature.length(), BIN_FLAGS_READABLE);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::saveConfiguration() MetaFile Signature:";
|
||||
std::cerr << "p3ConfigMgr::globalSaveConfig() MetaFile Signature:";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << signature;
|
||||
std::cerr << std::endl;
|
||||
@ -184,6 +238,7 @@ void p3ConfigMgr::saveConfiguration()
|
||||
backedUpFileSave(fname, fname_backup, sign_fname, sign_fname_backup, configbio, signbio);
|
||||
|
||||
delete signbio;
|
||||
|
||||
}
|
||||
|
||||
bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string& fname_backup, const std::string& sign_fname,
|
||||
@ -292,7 +347,39 @@ bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string&
|
||||
return true;
|
||||
}
|
||||
|
||||
void p3ConfigMgr::loadConfiguration()
|
||||
void p3ConfigMgr::loadConfiguration()
|
||||
{
|
||||
if(oldConfigType)
|
||||
globalLoadConfig();
|
||||
else
|
||||
loadConfig();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void p3ConfigMgr::loadConfig()
|
||||
{
|
||||
std::map<uint32_t, pqiConfig *>::iterator cit;
|
||||
std::string dummyHash = "dummyHash";
|
||||
for (cit = configs.begin(); cit != configs.end(); cit++)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3ConfigMgr::loadConfig() Element: ";
|
||||
std::cerr << cit->first <<"Dummy Hash: " << dummyHash;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
cit->second->loadConfiguration(dummyHash);
|
||||
|
||||
/* force config to NOT CHANGED */
|
||||
cit->second->HasConfigChanged(0);
|
||||
cit->second->HasConfigChanged(1);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void p3ConfigMgr::globalLoadConfig()
|
||||
{
|
||||
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
||||
|
||||
@ -395,9 +482,11 @@ void p3ConfigMgr::loadConfiguration()
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
(cit->second)->loadConfiguration(hashin);
|
||||
/* force config to NOT CHANGED */
|
||||
cit->second->HasConfigChanged(0);
|
||||
cit->second->HasConfigChanged(1);
|
||||
|
||||
/* force config to CHANGED to force saving into new non-global sig format */
|
||||
cit->second->IndicateConfigChanged();
|
||||
// cit->second->HasConfigChanged(0);
|
||||
// cit->second->HasConfigChanged(1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -532,6 +621,33 @@ void p3ConfigMgr::completeConfiguration()
|
||||
mConfigSaveActive = false;
|
||||
}
|
||||
|
||||
bool p3ConfigMgr::checkForGlobalSigConfig()
|
||||
{
|
||||
bool oldTypeExists;
|
||||
FILE *metaFile = NULL, *metaSig = NULL;
|
||||
std::string fName = basedir + "/" + metafname;
|
||||
std::string sigName = basedir + "/" + metasigfname;
|
||||
|
||||
metaFile = fopen(fName.c_str(), "r");
|
||||
metaSig = fopen(sigName.c_str(), "r");
|
||||
|
||||
// check if files exist
|
||||
if((metaFile != NULL) && (metaSig != NULL))
|
||||
{
|
||||
oldTypeExists = true;
|
||||
fclose(metaFile);
|
||||
fclose(metaSig);
|
||||
}
|
||||
else
|
||||
oldTypeExists = false;
|
||||
|
||||
|
||||
|
||||
return oldTypeExists;
|
||||
|
||||
|
||||
}
|
||||
|
||||
p3Config::p3Config(uint32_t t)
|
||||
:pqiConfig(t)
|
||||
{
|
||||
@ -539,7 +655,121 @@ p3Config::p3Config(uint32_t t)
|
||||
}
|
||||
|
||||
|
||||
bool p3Config::loadConfiguration(std::string &loadHash)
|
||||
bool p3Config::loadConfiguration(std::string &loadHash)
|
||||
{
|
||||
if(globalConfigType)
|
||||
return loadGlobalConfig(loadHash);
|
||||
else
|
||||
return loadConfig();
|
||||
}
|
||||
|
||||
bool p3Config::loadConfig()
|
||||
{
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::loadConfig() loading Configuration\n File: " << Filename() << std::endl;
|
||||
#endif
|
||||
|
||||
bool pass = true;
|
||||
std::string cfgFname = Filename();
|
||||
std::string cfgFnameBackup = cfgFname + ".tmp";
|
||||
|
||||
std::string signFname = Filename() +".sgn";
|
||||
std::string signFnameBackup = signFname + ".tmp";
|
||||
|
||||
std::list<RsItem *> load;
|
||||
std::list<RsItem *>::iterator it;
|
||||
|
||||
// try 1st attempt
|
||||
if(!loadAttempt(cfgFname, signFname, load))
|
||||
{
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::loadConfig() Failed to Load" << std::endl;
|
||||
#endif
|
||||
|
||||
/* bad load */
|
||||
for(it = load.begin(); it != load.end(); it++)
|
||||
{
|
||||
delete (*it);
|
||||
}
|
||||
pass = false;
|
||||
}
|
||||
|
||||
// try 2nd attempt with backup files if first failed
|
||||
if(!pass)
|
||||
{
|
||||
if(!loadAttempt(cfgFnameBackup, signFnameBackup, load))
|
||||
{
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::loadConfig() Failed on 2nd Pass" << std::endl;
|
||||
#endif
|
||||
|
||||
/* bad load */
|
||||
for(it = load.begin(); it != load.end(); it++)
|
||||
{
|
||||
delete (*it);
|
||||
}
|
||||
pass = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(pass)
|
||||
loadList(load);
|
||||
else
|
||||
return false;
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
bool p3Config::loadAttempt(const std::string& cfgFname,const std::string& signFname, std::list<RsItem *>& load)
|
||||
{
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::loadAttempt() \nFilename: " << cfgFname << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
||||
uint32_t stream_flags = BIN_FLAGS_READABLE;
|
||||
|
||||
BinEncryptedFileInterface *bio = new BinEncryptedFileInterface(cfgFname.c_str(), bioflags);
|
||||
pqiSSLstore stream(setupSerialiser(), "CONFIG", bio, stream_flags);
|
||||
|
||||
if(!stream.getEncryptedItems(load))
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::loadAttempt() Error occurred trying to load Item" << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/* set hash */
|
||||
setHash(bio->gethash());
|
||||
|
||||
BinMemInterface *signbio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
|
||||
|
||||
if(!signbio->readfromfile(signFname.c_str()))
|
||||
return false;
|
||||
|
||||
std::string signatureStored((char *) signbio->memptr(), signbio->memsize());
|
||||
|
||||
std::string signatureRead;
|
||||
std::string strHash(Hash());
|
||||
AuthSSL::getAuthSSL()->SignData(strHash.c_str(), strHash.length(), signatureRead);
|
||||
|
||||
if(signatureRead != signatureStored)
|
||||
return false;
|
||||
|
||||
delete signbio;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Config::loadGlobalConfig(std::string &loadHash)
|
||||
{
|
||||
bool pass = false;
|
||||
std::string cfg_fname = Filename();
|
||||
@ -637,7 +867,94 @@ bool p3Config::getHashAttempt(const std::string& loadHash, std::string& hashstr,
|
||||
}
|
||||
|
||||
|
||||
bool p3Config::saveConfiguration()
|
||||
bool p3Config::saveConfiguration()
|
||||
{
|
||||
return saveConfig();
|
||||
}
|
||||
|
||||
bool p3Config::saveConfig()
|
||||
{
|
||||
|
||||
bool cleanup = true;
|
||||
std::list<RsItem *> toSave = saveList(cleanup);
|
||||
|
||||
// temporarily append new to files as these will replace current configuration
|
||||
std::string newCfgFname = Filename() + "_new";
|
||||
std::string newSignFname = Filename() + ".sgn" + "_new";
|
||||
|
||||
std::string tmpCfgFname = Filename() + ".tmp";
|
||||
std::string tmpSignFname = Filename() + ".sgn" + ".tmp";
|
||||
|
||||
std::string cfgFname = Filename();
|
||||
std::string signFname = Filename() + ".sgn";
|
||||
|
||||
|
||||
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
|
||||
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
|
||||
bool written = true;
|
||||
|
||||
if (!cleanup)
|
||||
stream_flags |= BIN_FLAGS_NO_DELETE;
|
||||
|
||||
BinEncryptedFileInterface *cfg_bio = new BinEncryptedFileInterface(newCfgFname.c_str(), bioflags);
|
||||
pqiSSLstore *stream = new pqiSSLstore(setupSerialiser(), "CONFIG", cfg_bio, stream_flags);
|
||||
|
||||
written = written && stream->encryptedSendItems(toSave);
|
||||
|
||||
/* store the hash */
|
||||
setHash(cfg_bio->gethash());
|
||||
|
||||
// bio is taken care of in stream's destructor, also forces file to close
|
||||
delete stream;
|
||||
|
||||
/* sign data */
|
||||
std::string signature;
|
||||
std::string strHash(Hash());
|
||||
AuthSSL::getAuthSSL()->SignData(strHash.c_str(),strHash.length(), signature);
|
||||
|
||||
/* write signature to configuration */
|
||||
BinMemInterface *signbio = new BinMemInterface(signature.c_str(),
|
||||
signature.length(), BIN_FLAGS_READABLE);
|
||||
|
||||
signbio->writetofile(newSignFname.c_str());
|
||||
|
||||
delete signbio;
|
||||
|
||||
// now rewrite current files to temp files
|
||||
// rename back-up to current file
|
||||
if(!RsDirUtil::renameFile(cfgFname, tmpCfgFname) || !RsDirUtil::renameFile(signFname, tmpSignFname)){
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::backedUpFileSave() Failed to rename backup meta files: " << std::endl
|
||||
<< cfgFname << " to " << tmpCfgFname << std::endl
|
||||
<< signFname << " to " << tmpSignFname << std::endl;
|
||||
#endif
|
||||
written = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// now rewrite current files to temp files
|
||||
// rename back-up to current file
|
||||
if(!RsDirUtil::renameFile(newCfgFname, cfgFname) || !RsDirUtil::renameFile(newSignFname, signFname)){
|
||||
#ifdef CONFIG_DEBUG
|
||||
std::cerr << "p3Config::() Failed to rename meta files: " << std::endl
|
||||
<< newCfgFname << " to " << cfgFname << std::endl
|
||||
<< newSignFname << " to " << signFname << std::endl;
|
||||
#endif
|
||||
|
||||
written = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
saveDone(); // callback to inherited class to unlock any Mutexes protecting saveList() data
|
||||
|
||||
return written;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool p3Config::saveGlobalConfig()
|
||||
{
|
||||
|
||||
bool cleanup = true;
|
||||
@ -945,6 +1262,8 @@ bool p3GeneralConfig::loadList(std::list<RsItem *> load)
|
||||
* only the Indication and hash really need it
|
||||
*/
|
||||
|
||||
bool pqiConfig::globalConfigType = false;
|
||||
|
||||
pqiConfig::pqiConfig(uint32_t t)
|
||||
:ConfInd(2), type(t)
|
||||
{
|
||||
|
@ -150,6 +150,8 @@ void setHash(std::string h);
|
||||
|
||||
RsMutex cfgMtx;
|
||||
|
||||
static bool globalConfigType;
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
@ -168,10 +170,13 @@ void setHash(std::string h);
|
||||
std::string filename;
|
||||
std::string hash;
|
||||
|
||||
|
||||
friend class p3ConfigMgr;
|
||||
/* so it can access:
|
||||
* setFilename() and HasConfigChanged()
|
||||
*/
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -223,8 +228,45 @@ class p3ConfigMgr
|
||||
*/
|
||||
void completeConfiguration();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* this checks for the global config file and signature and determines class's mode of operation
|
||||
* @return global file rs-v0.#.cfg and rs-v0.#.sgn are present
|
||||
* @deprecated
|
||||
*/
|
||||
bool checkForGlobalSigConfig();
|
||||
|
||||
/**
|
||||
* removes theoldconfiguration type
|
||||
* @deprecated
|
||||
*/
|
||||
void removeOldConfigType();
|
||||
|
||||
/**
|
||||
* to save old style global-signature configuration files
|
||||
* @deprecated
|
||||
*/
|
||||
void globalSaveConfig();
|
||||
|
||||
/**
|
||||
* to load up old style global-signature config files
|
||||
* @deprecated
|
||||
*/
|
||||
void globalLoadConfig();
|
||||
|
||||
/**
|
||||
* saves configuration of pqiconfigs in object configs
|
||||
*/
|
||||
void saveConfig();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void loadConfig();
|
||||
|
||||
/**
|
||||
* checks if signature and configuration file's signature matches
|
||||
* @return false if signature file does not match configuration file's signature
|
||||
@ -251,10 +293,13 @@ const std::string metasigfname;
|
||||
|
||||
RsMutex cfgMtx; /* below is protected */
|
||||
|
||||
bool oldConfigType;
|
||||
bool mConfigSaveActive;
|
||||
std::map<uint32_t, pqiConfig *> configs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/***************************************************************************************************/
|
||||
|
||||
|
||||
@ -272,6 +317,7 @@ class p3Config: public pqiConfig
|
||||
virtual bool loadConfiguration(std::string &loadHash);
|
||||
virtual bool saveConfiguration();
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
/* Key Functions to be overloaded for Full Configuration */
|
||||
@ -311,6 +357,13 @@ bool backedUpFileSave(const std::string& fname, const std::string& fname_backup,
|
||||
*/
|
||||
bool getHashAttempt(const std::string& loadHash, std::string& hashstr, const std::string& fname, std::list<RsItem *>& load);
|
||||
|
||||
bool loadConfig();
|
||||
bool saveConfig();
|
||||
|
||||
bool loadGlobalConfig(std::string& hash);
|
||||
bool saveGlobalConfig();
|
||||
|
||||
bool loadAttempt(const std::string&,const std::string&, std::list<RsItem *>& load);
|
||||
|
||||
}; /* end of p3Config */
|
||||
|
||||
|
@ -114,7 +114,14 @@ public:
|
||||
PQInterface(std::string id) :peerId(id) { return; }
|
||||
virtual ~PQInterface() { return; }
|
||||
|
||||
/*!
|
||||
* allows user to send RsItems to a particular facility (file, network)
|
||||
*/
|
||||
virtual int SendItem(RsItem *) = 0;
|
||||
|
||||
/*!
|
||||
* Retrieve RsItem from a facility
|
||||
*/
|
||||
virtual RsItem *GetItem() = 0;
|
||||
|
||||
/**
|
||||
|
@ -26,7 +26,9 @@
|
||||
|
||||
|
||||
|
||||
|
||||
#include "pqi/pqibin.h"
|
||||
#include "pqi/authssl.h"
|
||||
#include "util/rsnet.h"
|
||||
|
||||
|
||||
@ -159,7 +161,142 @@ uint64_t BinFileInterface::bytecount()
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int BinFileInterface::getFileSize()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
BinEncryptedFileInterface::BinEncryptedFileInterface(const char* fname, int flags)
|
||||
: BinFileInterface(fname, flags), data(NULL), haveData(false), sizeData(0), cpyCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
BinEncryptedFileInterface::~BinEncryptedFileInterface()
|
||||
{
|
||||
if((sizeData > 0) && data != NULL)
|
||||
{
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int BinEncryptedFileInterface::senddata(void* data, int len)
|
||||
{
|
||||
|
||||
char* encrytedData = NULL;
|
||||
int encDataLen = 0;
|
||||
|
||||
// encrypt using own ssl public key
|
||||
if(len > 0)
|
||||
AuthSSL::getAuthSSL()->encrypt((void*&)encrytedData, encDataLen, data, len, AuthSSL::getAuthSSL()->OwnId());
|
||||
else
|
||||
return -1;
|
||||
|
||||
|
||||
if((encDataLen > 0) && (encrytedData != NULL))
|
||||
{
|
||||
BinFileInterface::senddata(encrytedData, encDataLen);
|
||||
delete[] encrytedData;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int BinEncryptedFileInterface::readdata(void* data, int len)
|
||||
{
|
||||
// to respect the inherited behavior of BinInterface
|
||||
// the whole file is read and decryped and store to be read by subsequent calls
|
||||
char* encryptedData = NULL;
|
||||
int encrypDataLen = 0;
|
||||
|
||||
|
||||
if(!haveData) // read whole data for first call, or first call after close()
|
||||
{
|
||||
|
||||
encrypDataLen = BinFileInterface::getFileSize();
|
||||
encryptedData = new char[encrypDataLen];
|
||||
|
||||
// make sure assign was successful
|
||||
if(encryptedData == NULL)
|
||||
return -1;
|
||||
|
||||
|
||||
if(-1 == BinFileInterface::readdata(encryptedData, encrypDataLen))
|
||||
return -1;
|
||||
|
||||
if((encrypDataLen > 0) && (encryptedData != NULL))
|
||||
{
|
||||
|
||||
if(!AuthSSL::getAuthSSL()->decrypt((void*&)(this->data), sizeData, encryptedData, encrypDataLen))
|
||||
return -1;
|
||||
|
||||
haveData = true;
|
||||
delete[] encryptedData;
|
||||
}
|
||||
|
||||
|
||||
if(len <= sizeData)
|
||||
{
|
||||
memcpy(data, this->data, len);
|
||||
cpyCount += len;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "BinEncryptedFileInterface::readData(): Error, Asking for more data than present" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if((cpyCount + len) <= sizeData)
|
||||
{
|
||||
memcpy(data, (void *) ((this->data) + cpyCount), len);
|
||||
cpyCount += len;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "BinEncryptedFileInterface::readData(): Error, Asking for more data than present" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int BinEncryptedFileInterface::close()
|
||||
{
|
||||
if(data != NULL)
|
||||
{
|
||||
delete[] data;
|
||||
sizeData = 0;
|
||||
haveData = false;
|
||||
cpyCount = 0;
|
||||
}
|
||||
|
||||
return BinFileInterface::close();
|
||||
}
|
||||
|
||||
uint64_t BinEncryptedFileInterface::bytecount()
|
||||
{
|
||||
return cpyCount;
|
||||
}
|
||||
|
||||
bool BinEncryptedFileInterface::moretoread()
|
||||
{
|
||||
if(haveData)
|
||||
return (cpyCount < sizeData);
|
||||
else
|
||||
return cpyCount < getFileSize();
|
||||
}
|
||||
|
||||
BinMemInterface::BinMemInterface(int defsize, int flags)
|
||||
:bin_flags(flags), buf(NULL), size(defsize),
|
||||
|
@ -28,8 +28,10 @@
|
||||
#ifndef PQI_BIN_INTERFACE_HEADER
|
||||
#define PQI_BIN_INTERFACE_HEADER
|
||||
|
||||
|
||||
#include "pqi/pqi_base.h"
|
||||
#include "pqi/pqihash.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@ -70,6 +72,9 @@ virtual bool bandwidthLimited() { return false; }
|
||||
virtual std::string gethash();
|
||||
virtual uint64_t bytecount();
|
||||
|
||||
protected:
|
||||
virtual int getFileSize();
|
||||
|
||||
private:
|
||||
int bin_flags;
|
||||
FILE *buf;
|
||||
@ -78,6 +83,52 @@ private:
|
||||
uint64_t bcount;
|
||||
};
|
||||
|
||||
/*!
|
||||
* use this for writing encrypted data to file using user's (ownid) ssl key
|
||||
* hash for encrypted data is calculated, not the unencrypted data
|
||||
*/
|
||||
class BinEncryptedFileInterface : public BinFileInterface
|
||||
{
|
||||
public:
|
||||
|
||||
BinEncryptedFileInterface(const char *fname, int flags);
|
||||
virtual ~BinEncryptedFileInterface();
|
||||
|
||||
/*!
|
||||
* pls note if hashing is on, it is the hash of the encrypted data that is calculated
|
||||
* also note, sif data is sent more than once, in a single instance of this object, then you will need to store the
|
||||
* encrypted file size for each send externally as they are encrypted with different random keys.
|
||||
* @param data data to be sent
|
||||
* @param len length of data in bytes
|
||||
* @return -1 if failed to write data, if not number bytes sent to file is returned
|
||||
*/
|
||||
int senddata(void *data, int len);
|
||||
|
||||
/*!
|
||||
* uses the hash of the encrypted data
|
||||
* @param location to place data
|
||||
* @param the length of data to be read
|
||||
* @return -1 if failed to write data, if not number of bytes read is returned
|
||||
*/
|
||||
int readdata(void *data, int len);
|
||||
|
||||
/*!
|
||||
* this releases resources held by an instance
|
||||
*/
|
||||
int close();
|
||||
|
||||
uint64_t bytecount();
|
||||
bool moretoread();
|
||||
|
||||
private:
|
||||
|
||||
char* data;
|
||||
bool haveData;
|
||||
int sizeData;
|
||||
uint64_t cpyCount;
|
||||
|
||||
};
|
||||
|
||||
//! handles writing to reading/writing to memory
|
||||
/*!
|
||||
* This provide a memory interface for storing/retrieving information in memory
|
||||
|
@ -181,6 +181,9 @@ RsItem *pqistore::GetItem()
|
||||
return outPkt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// // PQInterface
|
||||
int pqistore::tick()
|
||||
{
|
||||
@ -386,5 +389,190 @@ std::string pqistore::gethash()
|
||||
return bio->gethash();
|
||||
}
|
||||
|
||||
pqiSSLstore::pqiSSLstore(RsSerialiser *rss, std::string srcId, BinEncryptedFileInterface* bio_in, int bio_flagsin)
|
||||
: pqistore(rss, srcId, bio_in, bio_flagsin), enc_bio(bio_in)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pqiSSLstore::~pqiSSLstore()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool pqiSSLstore::encryptedSendItems(const std::list<RsItem*>& rsItemList)
|
||||
{
|
||||
|
||||
std::list<RsItem*>::const_iterator it;
|
||||
uint32_t sizeItems = 0, sizeItem = 0;
|
||||
uint32_t offset = 0;
|
||||
char* data = NULL;
|
||||
|
||||
for(it = rsItemList.begin(); it != rsItemList.end(); it++)
|
||||
sizeItems += rsSerialiser->size(*it);
|
||||
|
||||
data = new char[sizeItems];
|
||||
|
||||
for(it = rsItemList.begin(); it != rsItemList.end(); it++)
|
||||
{
|
||||
sizeItem = rsSerialiser->size(*it);
|
||||
if(!rsSerialiser->serialise(*it, (data+offset),&sizeItem))
|
||||
return false;
|
||||
offset += sizeItem;
|
||||
|
||||
}
|
||||
|
||||
if(sizeItems == offset)
|
||||
enc_bio->senddata(data, sizeItems);
|
||||
else
|
||||
return false;
|
||||
|
||||
if(data != NULL)
|
||||
delete[] data;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool pqiSSLstore::getEncryptedItems(std::list<RsItem* >& rsItemList)
|
||||
{
|
||||
RsItem* item;
|
||||
|
||||
while(NULL != (item = GetItem()))
|
||||
{
|
||||
rsItemList.push_back(item);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
RsItem *pqiSSLstore::GetItem()
|
||||
{
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "pqistore::GetItem()";
|
||||
pqioutput(PQL_DEBUG_ALL, pqistorezone, out.str());
|
||||
}
|
||||
|
||||
// check if this is a reading bio.
|
||||
if (!(bio_flags & BIN_FLAGS_READABLE))
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "pqistore::GetItem()";
|
||||
out << "Error Not Readable" << std::endl;
|
||||
pqioutput(PQL_DEBUG_BASIC, pqistorezone, out.str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// load if we dont have a packet.
|
||||
if (!nextPkt)
|
||||
{
|
||||
if (!readPkt(&nextPkt))
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "pqistore::GetItem()";
|
||||
out << "Failed to ReadPkt" << std::endl;
|
||||
pqioutput(PQL_DEBUG_BASIC, pqistorezone, out.str());
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!nextPkt) return NULL;
|
||||
|
||||
RsItem *outPkt = nextPkt;
|
||||
nextPkt = NULL;
|
||||
|
||||
if (outPkt != NULL)
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "pqistore::GetItem() Returning:" << std::endl;
|
||||
outPkt -> print(out);
|
||||
pqioutput(PQL_DEBUG_BASIC, pqistorezone, out.str());
|
||||
}
|
||||
return outPkt;
|
||||
}
|
||||
|
||||
|
||||
int pqiSSLstore::readPkt(RsItem **item_out)
|
||||
{
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "pqistore::readPkt()";
|
||||
pqioutput(PQL_DEBUG_ALL, pqistorezone, out.str());
|
||||
}
|
||||
|
||||
if ((!(enc_bio->isactive())) || (!(enc_bio->moretoread())))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// enough space to read any packet.
|
||||
int maxlen = getRsPktMaxSize();
|
||||
void *block = malloc(maxlen);
|
||||
|
||||
// initial read size: basic packet.
|
||||
int blen = getRsPktBaseSize();
|
||||
|
||||
int tmplen;
|
||||
/* we have the header */
|
||||
|
||||
// read the basic block (minimum packet size)
|
||||
if (blen != (tmplen = enc_bio->readdata(block, blen)))
|
||||
{
|
||||
pqioutput(PQL_WARNING, pqistorezone,
|
||||
"pqistore::readPkt() bad read(2)");
|
||||
|
||||
free(block);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// workout how much more to read.
|
||||
int extralen = getRsItemSize(block) - blen;
|
||||
|
||||
if(extralen+blen > maxlen)
|
||||
std::cerr << "***** ERROR: trying to read a packet of length " << extralen+blen << ", while the maximum length is " << maxlen << std::endl ;
|
||||
|
||||
if (extralen > 0)
|
||||
{
|
||||
if(extralen > blen + maxlen)
|
||||
{
|
||||
std::cerr << "pqistore: ERROR: Inconsistency in packet format (extralen=" << extralen << ", maxlen=" << maxlen << "). Wasting the whole file." << std::endl ;
|
||||
free(block) ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
void *extradata = (void *) (((char *) block) + blen);
|
||||
|
||||
if (extralen != (tmplen = enc_bio->readdata(extradata, extralen)))
|
||||
{
|
||||
|
||||
std::ostringstream out;
|
||||
out << "pqistore::readPkt() ";
|
||||
out << "Error Completing Read (read ";
|
||||
out << tmplen << "/" << extralen << ")" << std::endl;
|
||||
pqioutput(PQL_ALERT, pqistorezone, out.str());
|
||||
|
||||
free(block);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// create packet, based on header.
|
||||
//std::cerr << "Read Data Block -> Incoming Pkt(";
|
||||
//std::cerr << blen + extralen << ")" << std::endl;
|
||||
uint32_t readbytes = extralen + blen;
|
||||
|
||||
RsItem *item = rsSerialiser->deserialise(block, &readbytes);
|
||||
free(block);
|
||||
|
||||
if (item == NULL)
|
||||
{
|
||||
pqioutput(PQL_ALERT, pqistorezone,
|
||||
"pqistore::readPkt() Failed to create Item from store!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
item->PeerId(mSrcId);
|
||||
*item_out = item;
|
||||
return 1;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@
|
||||
#ifndef MRK_PQI_STORE_STREAMER_HEADER
|
||||
#define MRK_PQI_STORE_STREAMER_HEADER
|
||||
|
||||
#include "pqi/pqi.h"
|
||||
#include "pqi/pqibin.h"
|
||||
|
||||
#include <list>
|
||||
|
||||
@ -53,19 +53,59 @@ virtual int status();
|
||||
|
||||
std::string gethash();
|
||||
|
||||
private:
|
||||
int writePkt(RsItem *item);
|
||||
int readPkt(RsItem **item_out);
|
||||
protected:
|
||||
|
||||
// Serialiser
|
||||
RsSerialiser *rsSerialiser;
|
||||
unsigned int bio_flags; // only BIN_NO_CLOSE at the moment.
|
||||
|
||||
// Temp Storage for transient data.....
|
||||
RsItem *nextPkt;
|
||||
std::string mSrcId;
|
||||
|
||||
private:
|
||||
|
||||
int writePkt(RsItem *item);
|
||||
int readPkt(RsItem **item_out);
|
||||
|
||||
|
||||
|
||||
// Serialiser
|
||||
RsSerialiser *rsSerialiser;
|
||||
// Binary Interface for IO, initialisated at startup.
|
||||
BinInterface *bio;
|
||||
unsigned int bio_flags; // only BIN_NO_CLOSE at the moment.
|
||||
|
||||
// Temp Storage for transient data.....
|
||||
RsItem *nextPkt;
|
||||
std::string mSrcId;
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
* provdes an ssl encrypted stream to file storage
|
||||
*/
|
||||
class pqiSSLstore: public pqistore
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
pqiSSLstore(RsSerialiser *rss, std::string srcId, BinEncryptedFileInterface *bio_in, int bio_flagsin);
|
||||
|
||||
virtual ~pqiSSLstore();
|
||||
|
||||
/*!
|
||||
* send items encrypted to file using client's ssl key
|
||||
*/
|
||||
bool encryptedSendItems(const std::list<RsItem* >&);
|
||||
|
||||
/*!
|
||||
* retrieve encrypted file using client's ssl key
|
||||
*/
|
||||
bool getEncryptedItems(std::list<RsItem*>&);
|
||||
|
||||
private:
|
||||
|
||||
RsItem *GetItem();
|
||||
int readPkt(RsItem **item_out);
|
||||
|
||||
BinEncryptedFileInterface* enc_bio;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -1784,9 +1784,13 @@ bool RsInit::RsTryAutoLogin()
|
||||
|
||||
fclose(helpFile);
|
||||
|
||||
|
||||
delete[] indata;
|
||||
delete[] outdata;
|
||||
delete key;
|
||||
|
||||
if(key != NULL)
|
||||
delete key;
|
||||
|
||||
return true;
|
||||
#else
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user