encryption/decryption of distant messages. Moved initialisation of services after GXS components in rsinit.cc. Slightly changed prototype of GxsSecurity::{en,de}crypt() to be consistent with the other methods of that class

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-IdCleaning@7193 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-03-20 23:01:00 +00:00
parent 9926517df9
commit d3bbd88abc
5 changed files with 391 additions and 323 deletions

View file

@ -204,22 +204,21 @@ std::string GxsSecurity::getBinDataSign(void *data, int len)
bool GxsSecurity::encrypt(void *& out, int & outlen, const void *in, int inlen, EVP_PKEY *privateKey) bool GxsSecurity::encrypt(void *& out, int & outlen, const void *in, int inlen, const RsTlvSecurityKey& key)
{ {
#ifdef DISTRIB_DEBUG #ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::encrypt() " << std::endl; std::cerr << "GxsSecurity::encrypt() " << std::endl;
#endif #endif
RSA *rsa_publish_pub = NULL; RSA *rsa_publish_pub = RSAPublicKey_dup(extractPublicKey(key)) ;
EVP_PKEY *public_key = NULL; EVP_PKEY *public_key = NULL;
RSA* rsa_publish = EVP_PKEY_get1_RSA(privateKey); //RSA* rsa_publish = EVP_PKEY_get1_RSA(privateKey);
rsa_publish_pub = RSAPublicKey_dup(rsa_publish); //rsa_publish_pub = RSAPublicKey_dup(rsa_publish);
if(rsa_publish_pub != NULL){ if(rsa_publish_pub != NULL)
{
public_key = EVP_PKEY_new(); public_key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(public_key, rsa_publish_pub); EVP_PKEY_assign_RSA(public_key, rsa_publish_pub);
}else{ }else{
@ -282,23 +281,35 @@ bool GxsSecurity::encrypt(void *& out, int & outlen, const void *in, int inlen,
outlen = out_offset; outlen = out_offset;
return true; return true;
delete[] ek;
#ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::encrypt() finished with outlen : " << outlen << std::endl;
#endif
return true;
} }
bool GxsSecurity::decrypt(void *& out, int & outlen, const void *in, int inlen, EVP_PKEY *privateKey) bool GxsSecurity::decrypt(void *& out, int & outlen, const void *in, int inlen, const RsTlvSecurityKey& key)
{ {
#ifdef DISTRIB_DEBUG #ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::decrypt() " << std::endl; std::cerr << "GxsSecurity::decrypt() " << std::endl;
#endif #endif
RSA *rsa_publish = RSAPublicKey_dup(extractPrivateKey(key)) ;
EVP_PKEY *privateKey = NULL;
//RSA* rsa_publish = EVP_PKEY_get1_RSA(privateKey);
//rsa_publish_pub = RSAPublicKey_dup(rsa_publish);
if(rsa_publish != NULL)
{
privateKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(privateKey, rsa_publish);
}
else
{
#ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity(): Could not generate publish key " << grpId
<< std::endl;
#endif
return false;
}
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX ctx;
int eklen = 0, net_ekl = 0; int eklen = 0, net_ekl = 0;

View file

@ -98,7 +98,7 @@ public:
*@param in *@param in
*@param inlen *@param inlen
*/ */
static bool encrypt(void *&out, int &outlen, const void *in, int inlen, EVP_PKEY *privateKey); static bool encrypt(void *&out, int &outlen, const void *in, int inlen, const RsTlvSecurityKey& key) ;
/** /**
@ -110,7 +110,7 @@ public:
* @param inlen * @param inlen
* @return false if encryption failed * @return false if encryption failed
*/ */
static bool decrypt(void *&out, int &outlen, const void *in, int inlen, EVP_PKEY *privateKey); static bool decrypt(void *&out, int &outlen, const void *in, int inlen, const RsTlvSecurityKey& key) ;
/*! /*!
* uses grp signature to check if group has been * uses grp signature to check if group has been

View file

@ -1293,63 +1293,6 @@ int RsServer::StartupRetroShare()
// //
mPluginsManager->loadPlugins(programatically_inserted_plugins) ; mPluginsManager->loadPlugins(programatically_inserted_plugins) ;
/* create Services */
mDisc = new p3discovery2(mPeerMgr, mLinkMgr, mNetMgr);
mHeart = new p3heartbeat(mLinkMgr, pqih);
msgSrv = new p3MsgService(mLinkMgr);
chatSrv = new p3ChatService(mLinkMgr, mHistoryMgr);
mStatusSrv = new p3StatusService(mLinkMgr);
#ifdef GROUTER
p3GRouter *gr = new p3GRouter(mLinkMgr) ;
rsGRouter = gr ;
pqih->addService(gr) ;
#endif
p3turtle *tr = new p3turtle(mLinkMgr) ;
rsTurtle = tr ;
pqih -> addService(tr);
pqih -> addService(ftserver);
rsDisc = mDisc;
rsMsgs = new p3Msgs(msgSrv, chatSrv);
// connect components to turtle router.
ftserver->connectToTurtleRouter(tr) ;
chatSrv->connectToTurtleRouter(tr) ;
#ifdef GROUTER
msgSrv->connectToGlobalRouter(gr) ;
#endif
pqih -> addService(mHeart);
pqih -> addService(mDisc);
pqih -> addService(msgSrv);
pqih -> addService(chatSrv);
pqih ->addService(mStatusSrv);
// set interfaces for plugins
//
RsPlugInInterfaces interfaces;
interfaces.mFiles = rsFiles;
interfaces.mPeers = rsPeers;
interfaces.mMsgs = rsMsgs;
interfaces.mTurtle = rsTurtle;
interfaces.mDisc = rsDisc;
interfaces.mDht = rsDht;
// don't exist no more.
//interfaces.mForums = mForums;
interfaces.mNotify = mNotify;
mPluginsManager->setInterfaces(interfaces);
// now add plugin objects inside the loop:
// - client services provided by plugins.
// - cache services provided by plugins.
//
mPluginsManager->registerClientServices(pqih) ;
mPluginsManager->registerCacheServices() ;
#ifdef RS_ENABLE_GXS #ifdef RS_ENABLE_GXS
// The idea is that if priorGxsDir is non // The idea is that if priorGxsDir is non
@ -1537,6 +1480,64 @@ int RsServer::StartupRetroShare()
#endif // RS_ENABLE_GXS. #endif // RS_ENABLE_GXS.
/* create Services */
mDisc = new p3discovery2(mPeerMgr, mLinkMgr, mNetMgr);
mHeart = new p3heartbeat(mLinkMgr, pqih);
msgSrv = new p3MsgService(mLinkMgr,mGxsIdService);
chatSrv = new p3ChatService(mLinkMgr, mHistoryMgr);
mStatusSrv = new p3StatusService(mLinkMgr);
#ifdef GROUTER
p3GRouter *gr = new p3GRouter(mLinkMgr) ;
rsGRouter = gr ;
pqih->addService(gr) ;
#endif
p3turtle *tr = new p3turtle(mLinkMgr) ;
rsTurtle = tr ;
pqih -> addService(tr);
pqih -> addService(ftserver);
rsDisc = mDisc;
rsMsgs = new p3Msgs(msgSrv, chatSrv);
// connect components to turtle router.
ftserver->connectToTurtleRouter(tr) ;
chatSrv->connectToTurtleRouter(tr) ;
#ifdef GROUTER
msgSrv->connectToGlobalRouter(gr) ;
#endif
pqih -> addService(mHeart);
pqih -> addService(mDisc);
pqih -> addService(msgSrv);
pqih -> addService(chatSrv);
pqih ->addService(mStatusSrv);
// set interfaces for plugins
//
RsPlugInInterfaces interfaces;
interfaces.mFiles = rsFiles;
interfaces.mPeers = rsPeers;
interfaces.mMsgs = rsMsgs;
interfaces.mTurtle = rsTurtle;
interfaces.mDisc = rsDisc;
interfaces.mDht = rsDht;
// don't exist no more.
//interfaces.mForums = mForums;
interfaces.mNotify = mNotify;
mPluginsManager->setInterfaces(interfaces);
// now add plugin objects inside the loop:
// - client services provided by plugins.
// - cache services provided by plugins.
//
mPluginsManager->registerClientServices(pqih) ;
mPluginsManager->registerCacheServices() ;
#ifdef RS_RTT #ifdef RS_RTT
p3rtt *mRtt = new p3rtt(mLinkMgr); p3rtt *mRtt = new p3rtt(mLinkMgr);

View file

@ -26,15 +26,20 @@
#include "retroshare/rsiface.h" #include "retroshare/rsiface.h"
#include "retroshare/rspeers.h" #include "retroshare/rspeers.h"
#include "retroshare/rsidentity.h"
#include "pqi/pqibin.h" #include "pqi/pqibin.h"
#include "pqi/pqiarchive.h" #include "pqi/pqiarchive.h"
#include "pqi/p3linkmgr.h" #include "pqi/p3linkmgr.h"
#include "pqi/authgpg.h" #include "pqi/authgpg.h"
#include "services/p3msgservice.h"
#include "pgp/pgpkeyutil.h"
#include "pqi/p3cfgmgr.h" #include "pqi/p3cfgmgr.h"
#include "gxs/gxssecurity.h"
#include "services/p3idservice.h"
#include "services/p3msgservice.h"
#include "pgp/pgpkeyutil.h"
#include "rsserver/p3face.h" #include "rsserver/p3face.h"
#include "serialiser/rsconfigitems.h" #include "serialiser/rsconfigitems.h"
#ifdef GROUTER #ifdef GROUTER
@ -78,9 +83,8 @@ static const uint8_t DISTANT_MSG_PROTOCOL_VERSION_02 = 0x48 ;
*/ */
p3MsgService::p3MsgService(p3LinkMgr *lm) p3MsgService::p3MsgService(p3LinkMgr *lm,p3IdService *id_serv)
:p3Service(RS_SERVICE_TYPE_MSG), p3Config(CONFIG_TYPE_MSGS), :p3Service(RS_SERVICE_TYPE_MSG), p3Config(CONFIG_TYPE_MSGS), mLinkMgr(lm), mIdService(id_serv), mMsgMtx("p3MsgService"), mMsgUniqueId(time(NULL))
mLinkMgr(lm), mMsgMtx("p3MsgService"), mMsgUniqueId(time(NULL))
{ {
_serialiser = new RsMsgSerialiser(); _serialiser = new RsMsgSerialiser();
addSerialType(_serialiser); addSerialType(_serialiser);
@ -1677,19 +1681,49 @@ RsMsgItem *p3MsgService::initMIRsMsg(const MessageInfo &info, const RsPeerId& to
return msg; return msg;
} }
//void p3MsgService::handleResponse(uint32_t token,uint32_t req_type)
//{
//}
//
//bool p3MsgService::hasRequestGxsIdKey(const RsGxsId& key_id)
//{
// // Finite state machinery to get the GXS key. This is horrible.
// //
//
// std::map<RsGxsId,
// uint32_t token=0 ;
// RsTokReqOptions opts;
// opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
//
// std::cerr << "Requesting GXS key to encrypt." << std::endl;
//
// RsGenExchange::getTokenService()->requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts);
//
// uint32_t status ;
// while( (status = RsGenExchange::getTokenService()->requestStatus(token)) != GXS_REQUEST_V2_STATUS_COMPLETE)
// {
// std::cerr << " Waiting for answer..." << std::endl;
// usleep(500000) ;
// }
//}
bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const RsGxsId& source_gxs_id,RsMsgItem *item) bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const RsGxsId& source_gxs_id,RsMsgItem *item)
{ {
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << "Creating distant message for recipient " << item->PeerId() << ", encryption key=" << destination_gxs_id << " in place." << std::endl; std::cerr << "Creating distant message for recipient " << item->PeerId() << ", encryption key=" << destination_gxs_id << " in place." << std::endl;
#endif #endif
unsigned char *data = NULL ;
void *encrypted_data = NULL ;
try
{
// 0 - append own id to the data. // 0 - append own id to the data.
// //
uint32_t conservative_max_signature_size = 1000 ; uint32_t conservative_max_signature_size = 1000 ;
uint32_t message_size = _serialiser->size(item) ; uint32_t message_size = _serialiser->size(item) ;
uint32_t total_data_size = 1+5+source_gxs_id.SIZE_IN_BYTES+5+message_size+conservative_max_signature_size ; uint32_t total_data_size = 1+5+source_gxs_id.SIZE_IN_BYTES+5+message_size+conservative_max_signature_size ;
unsigned char *data = (unsigned char *)malloc(total_data_size) ; data = (unsigned char *)malloc(total_data_size) ;
// -1 - setup protocol version // -1 - setup protocol version
// //
@ -1719,11 +1753,8 @@ bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const
uint32_t remaining_size = total_data_size - offset ; uint32_t remaining_size = total_data_size - offset ;
if(!_serialiser->serialise(item,&data[offset],&remaining_size)) if(!_serialiser->serialise(item,&data[offset],&remaining_size))
{ throw std::runtime_error("Serialization error.") ;
std::cerr << "(EE) p3MsgService::encryptMessage(): Serialization error." << std::endl;
free(data) ;
return false;
}
offset += message_size ; offset += message_size ;
// 2 - now sign the data, if necessary, and put the signature up front // 2 - now sign the data, if necessary, and put the signature up front
@ -1731,7 +1762,7 @@ bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const
uint32_t signature_length = 0 ; uint32_t signature_length = 0 ;
unsigned char *signature_data = NULL ; unsigned char *signature_data = NULL ;
if(item->msgFlags & RS_MSG_FLAGS_SIGNED) if(false)//item->msgFlags & RS_MSG_FLAGS_SIGNED)
{ {
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Signing the message..." << std::endl; std::cerr << " Signing the message..." << std::endl;
@ -1768,22 +1799,25 @@ bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const
// 2 - pgp-encrypt the whole chunk with the user-supplied public key. // 2 - pgp-encrypt the whole chunk with the user-supplied public key.
// //
uint32_t encrypted_size = offset + 1000 ; int encrypted_size = offset + 1000 ;
unsigned char *encrypted_data = new unsigned char[encrypted_size] ; encrypted_data = malloc(encrypted_size) ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Encrypting for Key ID " << pgp_id << std::endl; std::cerr << " Encrypting for Key ID " << pgp_id << std::endl;
#endif #endif
// Do proper encryption here // Do proper encryption here
#warning UNFINISHED CODE!!! #warning UNFINISHED CODE!!!
// if(!mGxsIface->encrypt(data,offset,encrypted_data,encrypted_size,destination_gxs_id)))
// { RsTlvSecurityKey encryption_key ;
if(!mIdService->getKey(destination_gxs_id,encryption_key))
throw std::runtime_error("Cannot get encryption key for id " + destination_gxs_id.toStdString()) ;
if(!GxsSecurity::encrypt(encrypted_data,encrypted_size,data,offset,encryption_key))
throw std::runtime_error("Encryption failed!") ;
free(data) ; free(data) ;
delete[] encrypted_data ; data = NULL ;
std::cerr << "Encryption failed!" << std::endl;
return false;
//}
//free(data) ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Decrypted size = " << offset << std::endl; std::cerr << " Decrypted size = " << offset << std::endl;
@ -1798,7 +1832,9 @@ bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const
#endif #endif
std::string armoured_data ; std::string armoured_data ;
Radix64::encode((char *)encrypted_data,encrypted_size,armoured_data) ; Radix64::encode((char *)encrypted_data,encrypted_size,armoured_data) ;
delete[] encrypted_data ;
free(encrypted_data) ;
encrypted_data = NULL ;
// wipe the item clean and replace the message by the encrypted data. // wipe the item clean and replace the message by the encrypted data.
@ -1818,6 +1854,15 @@ bool p3MsgService::createDistantMessage(const RsGxsId& destination_gxs_id,const
#endif #endif
return true ; return true ;
} }
catch(std::exception& e)
{
std::cerr << __PRETTY_FUNCTION__ << ": Error. reason: " << e.what() << std::endl;
if(encrypted_data) free(encrypted_data) ;
if(data) free(data) ;
return false ;
}
}
std::string printNumber(uint32_t n,bool hex) std::string printNumber(uint32_t n,bool hex)
{ {
@ -1828,13 +1873,14 @@ std::string printNumber(uint32_t n,bool hex)
} }
bool p3MsgService::decryptMessage(const std::string& mId) bool p3MsgService::decryptMessage(const std::string& mId)
{ {
unsigned char *decrypted_data = NULL; void *decrypted_data = NULL;
char *encrypted_data = NULL; char *encrypted_data = NULL;
try try
{ {
uint32_t msgId = atoi(mId.c_str()); uint32_t msgId = atoi(mId.c_str());
std::string encrypted_string ; std::string encrypted_string ;
RsGxsId destination_gxs_id ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << "Decrypting message with Id " << mId << std::endl; std::cerr << "Decrypting message with Id " << mId << std::endl;
@ -1851,6 +1897,7 @@ bool p3MsgService::decryptMessage(const std::string& mId)
} }
encrypted_string = mit->second->message ; encrypted_string = mit->second->message ;
destination_gxs_id = RsGxsId(mit->second->PeerId()) ;
} }
size_t encrypted_size ; size_t encrypted_size ;
@ -1861,11 +1908,9 @@ bool p3MsgService::decryptMessage(const std::string& mId)
std::cerr << " Message has been radix64 decoded." << std::endl; std::cerr << " Message has been radix64 decoded." << std::endl;
#endif #endif
uint32_t decrypted_size = encrypted_size + 500 ;
unsigned char *decrypted_data = new unsigned char[decrypted_size] ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Calling decryption. Encrypted data size = " << encrypted_size << ", Allocated size = " << decrypted_size << std::endl; std::cerr << " Calling decryption. Encrypted data size = " << encrypted_size << ", Allocated size = " << decrypted_size << std::endl;
std::cerr << " Destination GxsId : " << destination_gxs_id << std::endl;
std::cerr << " First bytes of encrypted data: " << std::hex << (int)((unsigned char*)encrypted_data)[0] << " " << (int)((unsigned char*)encrypted_data)[1] << " " << (int)((unsigned char*)encrypted_data)[2] << std::dec << std::endl; std::cerr << " First bytes of encrypted data: " << std::hex << (int)((unsigned char*)encrypted_data)[0] << " " << (int)((unsigned char*)encrypted_data)[1] << " " << (int)((unsigned char*)encrypted_data)[2] << std::dec << std::endl;
std::cerr << " Encrypted data hash = " << RsDirUtil::sha1sum((uint8_t*)encrypted_data,encrypted_size).toStdString() << std::endl; std::cerr << " Encrypted data hash = " << RsDirUtil::sha1sum((uint8_t*)encrypted_data,encrypted_size).toStdString() << std::endl;
#endif #endif
@ -1873,17 +1918,26 @@ bool p3MsgService::decryptMessage(const std::string& mId)
// TODO // TODO
// //
#warning UNFINISHED CODE!!! #warning UNFINISHED CODE!!!
// if(!rsGxs->decrypt(encrypted_data,encrypted_size,decrypted_data,&decrypted_size))
// throw std::runtime_error("decryption failed!") ;
int decrypted_size = 0 ;
decrypted_data = NULL ;
RsTlvSecurityKey encryption_key ;
if(!mIdService->getPrivateKey(destination_gxs_id,encryption_key))
throw std::runtime_error("Cannot get private encryption key for id " + destination_gxs_id.toStdString()) ;
if(!GxsSecurity::decrypt(decrypted_data,decrypted_size,(void*)encrypted_data,encrypted_size,encryption_key))
throw std::runtime_error("Decryption failed!") ;
uint8_t *decr_data = (uint8_t*)decrypted_data ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Message has succesfully decrypted. Decrypted size = " << decrypted_size << std::endl; std::cerr << " Message has succesfully decrypted. Decrypted size = " << decrypted_size << std::endl;
#endif #endif
// 1 - get the sender's id // 1 - get the sender's id
uint32_t offset = 0 ; uint32_t offset = 0 ;
unsigned char protocol_version = decrypted_data[offset++] ; unsigned char protocol_version = decr_data[offset++] ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Read protocol version number " << std::hex << (int)protocol_version << std::dec << std::endl; std::cerr << " Read protocol version number " << std::hex << (int)protocol_version << std::dec << std::endl;
@ -1894,19 +1948,19 @@ bool p3MsgService::decryptMessage(const std::string& mId)
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Reading identity section " << std::endl; std::cerr << " Reading identity section " << std::endl;
#endif #endif
uint8_t ptag = decrypted_data[offset] ; uint8_t ptag = decr_data[offset] ;
if(ptag != DISTANT_MSG_TAG_IDENTITY) if(ptag != DISTANT_MSG_TAG_IDENTITY)
throw std::runtime_error("Bad ptag in encrypted msg packet "+printNumber(ptag,true)+" => packet is dropped.") ; throw std::runtime_error("Bad ptag in encrypted msg packet "+printNumber(ptag,true)+" => packet is dropped.") ;
unsigned char *tmp_data = &decrypted_data[offset] ; unsigned char *tmp_data = &decr_data[offset] ;
uint32_t identity_size = PGPKeyParser::read_125Size(tmp_data) ; uint32_t identity_size = PGPKeyParser::read_125Size(tmp_data) ;
offset += tmp_data - decrypted_data ; offset += tmp_data - decr_data ;
if(identity_size != RsGxsId::SIZE_IN_BYTES) if(identity_size != RsGxsId::SIZE_IN_BYTES)
throw std::runtime_error("Bad size in Identity section " + printNumber(identity_size,false) + " => packet is dropped.") ; throw std::runtime_error("Bad size in Identity section " + printNumber(identity_size,false) + " => packet is dropped.") ;
RsGxsId senders_id(&decrypted_data[offset]) ; RsGxsId senders_id(&decr_data[offset]) ;
offset += identity_size ; offset += identity_size ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
@ -1914,19 +1968,19 @@ bool p3MsgService::decryptMessage(const std::string& mId)
#endif #endif
// 2 - deserialize the item // 2 - deserialize the item
ptag = decrypted_data[offset] ; ptag = decr_data[offset] ;
if(ptag != DISTANT_MSG_TAG_CLEAR_MSG) if(ptag != DISTANT_MSG_TAG_CLEAR_MSG)
throw std::runtime_error("Bad ptag in encrypted msg packet " + printNumber(ptag,true) + " => packet is dropped.") ; throw std::runtime_error("Bad ptag in encrypted msg packet " + printNumber(ptag,true) + " => packet is dropped.") ;
tmp_data = &decrypted_data[offset] ; tmp_data = &decr_data[offset] ;
uint32_t item_size = PGPKeyParser::read_125Size(tmp_data) ; uint32_t item_size = PGPKeyParser::read_125Size(tmp_data) ;
offset += tmp_data - decrypted_data ; offset += tmp_data - decr_data ;
#ifdef DEBUG_DISTANT_MSG #ifdef DEBUG_DISTANT_MSG
std::cerr << " Deserializing..." << std::endl; std::cerr << " Deserializing..." << std::endl;
#endif #endif
RsMsgItem *item = dynamic_cast<RsMsgItem*>(_serialiser->deserialise(&decrypted_data[offset],&item_size)) ; RsMsgItem *item = dynamic_cast<RsMsgItem*>(_serialiser->deserialise(&decr_data[offset],&item_size)) ;
if(item == NULL) if(item == NULL)
throw std::runtime_error("Decrypted message could not be deserialized.") ; throw std::runtime_error("Decrypted message could not be deserialized.") ;
@ -1939,14 +1993,14 @@ bool p3MsgService::decryptMessage(const std::string& mId)
if(offset < decrypted_size) if(offset < decrypted_size)
{ {
uint8_t ptag = decrypted_data[offset++] ; uint8_t ptag = decr_data[offset++] ;
if(ptag != DISTANT_MSG_TAG_SIGNATURE) if(ptag != DISTANT_MSG_TAG_SIGNATURE)
throw std::runtime_error("Bad ptag in signature packet " + printNumber(ptag,true) + " => packet is dropped.") ; throw std::runtime_error("Bad ptag in signature packet " + printNumber(ptag,true) + " => packet is dropped.") ;
unsigned char *tmp_data = &decrypted_data[offset] ; unsigned char *tmp_data = &decr_data[offset] ;
uint32_t signature_size = PGPKeyParser::read_125Size(tmp_data) ; uint32_t signature_size = PGPKeyParser::read_125Size(tmp_data) ;
offset += tmp_data - decrypted_data ; offset += tmp_data - decr_data ;
std::cerr << " Signature is present. Verifying it..." << std::endl; std::cerr << " Signature is present. Verifying it..." << std::endl;
@ -1965,8 +2019,8 @@ bool p3MsgService::decryptMessage(const std::string& mId)
else else
throw std::runtime_error("Structural error in packet: sizes do not match. Dropping the message.") ; throw std::runtime_error("Structural error in packet: sizes do not match. Dropping the message.") ;
delete[] decrypted_data ; free(decr_data) ;
decrypted_data = NULL ; decr_data = NULL ;
// 4 - replace the item with the decrypted data, and update flags // 4 - replace the item with the decrypted data, and update flags
@ -2035,8 +2089,8 @@ bool p3MsgService::decryptMessage(const std::string& mId)
catch(std::exception& e) catch(std::exception& e)
{ {
std::cerr << "Decryption failed: " << e.what() << std::endl; std::cerr << "Decryption failed: " << e.what() << std::endl;
if(encrypted_data != NULL) delete[] encrypted_data ; if(encrypted_data != NULL) free(encrypted_data) ;
if(decrypted_data != NULL) delete[] decrypted_data ; if(decrypted_data != NULL) free(decrypted_data) ;
return false ; return false ;
} }

View file

@ -51,6 +51,7 @@
#include "turtle/turtleclientservice.h" #include "turtle/turtleclientservice.h"
class p3LinkMgr; class p3LinkMgr;
class p3IdService;
// Temp tweak to test grouter // Temp tweak to test grouter
class p3MsgService: public p3Service, public p3Config, public pqiMonitor class p3MsgService: public p3Service, public p3Config, public pqiMonitor
@ -59,7 +60,7 @@ class p3MsgService: public p3Service, public p3Config, public pqiMonitor
#endif #endif
{ {
public: public:
p3MsgService(p3LinkMgr *lm); p3MsgService(p3LinkMgr *lm,p3IdService *id_service);
/* External Interface */ /* External Interface */
bool getMessageSummaries(std::list<MsgInfoSummary> &msgList); bool getMessageSummaries(std::list<MsgInfoSummary> &msgList);
@ -181,6 +182,7 @@ int checkOutgoingMessages();
void initStandardTagTypes(); void initStandardTagTypes();
p3LinkMgr *mLinkMgr; p3LinkMgr *mLinkMgr;
p3IdService *mIdService ;
/* Mutex Required for stuff below */ /* Mutex Required for stuff below */