moerged with upstream/master

This commit is contained in:
csoler 2016-04-24 21:29:55 -04:00
commit 18e9e1c2db
25 changed files with 239 additions and 127 deletions

View file

@ -264,9 +264,12 @@ bool DistantChatService::initiateDistantChatConnexion(const RsGxsId& to_gxs_id,
RsChatMsgItem *item = new RsChatMsgItem;
item->message = "[Starting distant chat. Please wait for secure tunnel to be established]" ;
item->chatFlags = RS_CHAT_FLAG_PRIVATE ;
item->sendTime = time(NULL) ;
item->PeerId(RsPeerId(tunnel_id)) ;
handleRecvChatMsgItem(item) ;
delete item ; // item is replaced by NULL if partial, but this is not the case here.
return true ;
}

View file

@ -63,7 +63,7 @@ public:
// derived in p3ChatService, so as to pass down some info
virtual void handleIncomingItem(RsItem *) = 0;
virtual bool handleRecvChatMsgItem(RsChatMsgItem *ci)=0 ;
virtual bool handleRecvChatMsgItem(RsChatMsgItem *& ci)=0 ;
bool handleOutgoingItem(RsChatItem *) ;
bool handleRecvItem(RsChatItem *) ;

View file

@ -424,11 +424,6 @@ void DistributedChatService::checkSizeAndSendLobbyMessage(RsChatItem *msg)
sendChatItem(msg) ;
}
bool DistributedChatService::locked_checkAndRebuildPartialLobbyMessage(RsChatLobbyMsgItem *ci)
{
return true ;
}
bool DistributedChatService::handleRecvItem(RsChatItem *item)
{
switch(item->PacketSubType())

View file

@ -90,7 +90,6 @@ class DistributedChatService
void addToSaveList(std::list<RsItem*>& list) const ;
bool processLoadListItem(const RsItem *item) ;
bool locked_checkAndRebuildPartialLobbyMessage(RsChatLobbyMsgItem *) ;
void checkSizeAndSendLobbyMessage(RsChatItem *) ;
bool sendLobbyChat(const ChatLobbyId &lobby_id, const std::string&) ;

View file

@ -428,7 +428,14 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
return true;
}
bool p3ChatService::locked_checkAndRebuildPartialMessage(RsChatMsgItem *ci)
// This method might take control over the memory, or modify it, possibly adding missing parts.
// This function looks weird because it cannot duplicate the message since it does not know
// what type of object it is and the duplicate method of lobby messages is reserved for
// ChatLobby bouncing objects.
//
// Returns false if the item shouldn't be used (and replaced to NULL)
bool p3ChatService::locked_checkAndRebuildPartialMessage(RsChatMsgItem *& ci)
{
// Check is the item is ending an incomplete item.
//
@ -445,13 +452,16 @@ bool p3ChatService::locked_checkAndRebuildPartialMessage(RsChatMsgItem *ci)
ci->message = it->second->message + ci->message ;
ci->chatFlags |= it->second->chatFlags ;
// always remove existing partial. The compound message is in ci now.
delete it->second ;
if(!ci_is_incomplete)
_pendingPartialMessages.erase(it) ;
_pendingPartialMessages.erase(it) ;
}
// now decide what to do: if ci is incomplete, store it and replace the pointer with NULL
// if complete, return it.
if(ci_is_incomplete)
{
#ifdef CHAT_DEBUG
@ -459,7 +469,8 @@ bool p3ChatService::locked_checkAndRebuildPartialMessage(RsChatMsgItem *ci)
#endif
// The item is a partial message. Push it, and wait for the rest.
//
_pendingPartialMessages[ci->PeerId()] = ci ;
_pendingPartialMessages[ci->PeerId()] = ci ; // cannot use duplicate() here
ci = NULL ; // takes memory ownership over ci
return false ;
}
else
@ -503,8 +514,10 @@ void p3ChatService::handleIncomingItem(RsItem *item)
//
RsChatMsgItem *ci = dynamic_cast<RsChatMsgItem*>(item) ;
if(ci != NULL)
{
if(! handleRecvChatMsgItem(ci))
{
handleRecvChatMsgItem(ci);
if(ci)
delete ci ;
return ; // don't delete! It's handled by handleRecvChatMsgItem in some specific cases only.
@ -665,7 +678,7 @@ bool p3ChatService::checkForMessageSecurity(RsChatMsgItem *ci)
return true ;
}
bool p3ChatService::handleRecvChatMsgItem(RsChatMsgItem *ci)
bool p3ChatService::handleRecvChatMsgItem(RsChatMsgItem *& ci)
{
time_t now = time(NULL);
std::string name;
@ -674,15 +687,8 @@ bool p3ChatService::handleRecvChatMsgItem(RsChatMsgItem *ci)
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
// This crap is because chat lobby messages use a different method for chunking messages using an additional
// subpacket ID, and a list of lobbies. We cannot just collapse the two because it would make the normal chat
// (and chat lobbies) not backward compatible.
if(!DistributedChatService::locked_checkAndRebuildPartialLobbyMessage(dynamic_cast<RsChatLobbyMsgItem*>(ci)))
return true ;
if(!locked_checkAndRebuildPartialMessage(ci))
return true ;
if(!locked_checkAndRebuildPartialMessage(ci)) // we make sure this call does not take control over the memory
return true ; // message is a subpart of an existing message. So everything ok, but we need to return.
}
// Check for security. This avoids bombing messages, and so on.

View file

@ -205,7 +205,8 @@ private:
void receiveStateString(const RsPeerId& id,const std::string& s) ;
/// methods for handling various Chat items.
bool handleRecvChatMsgItem(RsChatMsgItem *item) ; // returns false if the item should be deleted.
virtual bool handleRecvChatMsgItem(RsChatMsgItem *&item) ; // NULL-ifies the item if memory ownership is taken
void handleRecvChatStatusItem(RsChatStatusItem *item) ;
void handleRecvChatAvatarItem(RsChatAvatarItem *item) ;
@ -220,7 +221,8 @@ private:
void checkSizeAndSendMessage(RsChatMsgItem *item) ; // keep for compatibility for a few weeks.
/// Called when a RsChatMsgItem is received. The item may be collapsed with any waiting partial chat item from the same peer.
bool locked_checkAndRebuildPartialMessage(RsChatMsgItem *) ;
/// if so, the chat item will be turned to NULL
bool locked_checkAndRebuildPartialMessage(RsChatMsgItem *&) ;
RsChatAvatarItem *makeOwnAvatarItem() ;
RsChatStatusItem *makeOwnCustomStateStringItem() ;

View file

@ -613,7 +613,6 @@ RsGRouterAbstractMsgItem *GRouterDataInfo::addDataChunk(RsGRouterTransactionChun
{
RsItem *data_item = RsGRouterSerialiser().deserialise(incoming_data_buffer->chunk_data,&incoming_data_buffer->chunk_size) ;
incoming_data_buffer->chunk_data = NULL;
delete incoming_data_buffer;
incoming_data_buffer = NULL ;

View file

@ -225,20 +225,13 @@ private:
void handleLowLevelTransactionAckItem(RsGRouterTransactionAcknItem*) ;
static Sha1CheckSum computeDataItemHash(RsGRouterGenericDataItem *data_item);
#ifdef __APPLE__
public:
#endif
class nullstream: public std::ostream {};
std::ostream& grouter_debug() const
{
static nullstream null ;
static std::ostream null(0);
return _debug_enabled?(std::cerr):null;
}
#ifdef __APPLE__
private:
#endif
void routePendingObjects() ;
void handleTunnels() ;
void autoWash() ;
@ -364,5 +357,3 @@ private:
uint64_t _random_salt ;
};
template<typename T> p3GRouter::nullstream& operator<<(p3GRouter::nullstream& ns,const T&) { return ns ; }

View file

@ -425,6 +425,8 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// [--- Encrypted session key length ---|--- Encrypted session key ---|--- IV ---|---- Encrypted data ---]
//
out = NULL ;
RSA *tmpkey = ::extractPublicKey(key) ;
RSA *rsa_publish_pub = RSAPublicKey_dup(tmpkey) ;
RSA_free(tmpkey) ;
@ -509,7 +511,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// move to end
out_offset += out_currOffset;
// make sure offset has not gone passed valid memory bounds
if(out_offset > max_outlen)
{
@ -521,6 +523,8 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// free encrypted key data
free(ek);
EVP_CIPHER_CTX_cleanup(&ctx);
outlen = out_offset;
return true;
}
@ -538,11 +542,12 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
//
out = NULL ;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
std::vector<EVP_PKEY *> public_keys(keys.size(),NULL);
try
{
std::vector<EVP_PKEY *> public_keys(keys.size(),NULL);
for(uint32_t i=0;i<keys.size();++i)
{
RSA *tmpkey = ::extractPublicKey(keys[i]) ;
@ -561,9 +566,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
}
}
EVP_CIPHER_CTX ctx;
unsigned char iv[EVP_MAX_IV_LENGTH];
EVP_CIPHER_CTX_init(&ctx);
std::vector<unsigned char *> ek(keys.size(),NULL) ;
std::vector<int> eklen(keys.size(),0) ;
@ -587,6 +590,11 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
if(!EVP_SealInit(&ctx, EVP_aes_128_cbc(), ek.data(), eklen.data(), iv, public_keys.data(), keys.size()))
return false;
// now we can release the encryption keys
for(uint32_t i=0;i<public_keys.size();++i)
EVP_PKEY_free(public_keys[i]) ;
public_keys.clear() ;
int total_ek_size = MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE * keys.size() ;
int max_outlen = MULTI_ENCRYPTION_FORMAT_v001_HEADER_SIZE + MULTI_ENCRYPTION_FORMAT_v001_NUMBER_OF_KEYS_SIZE + total_ek_size + EVP_MAX_IV_LENGTH + (inlen + cipher_block_size) ;
@ -653,15 +661,23 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
if(ek[i]) free(ek[i]);
outlen = out_offset;
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
catch(std::exception& e)
{
std::cerr << "(EE) Exception caught while encrypting: " << e.what() << std::endl;
EVP_CIPHER_CTX_cleanup(&ctx);
if(out) free(out) ;
out = NULL ;
for(uint32_t i=0;i<public_keys.size();++i)
EVP_PKEY_free(public_keys[i]) ;
public_keys.clear() ;
return false ;
}
}
@ -675,6 +691,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
//
// This method can be used to decrypt multi-encrypted data, if passing he correct encrypted key block (corresponding to the given key)
out = NULL ;
#ifdef GXS_SECURITY_DEBUG
std::cerr << "GxsSecurity::decrypt() " << std::endl;
#endif
@ -767,6 +784,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
outlen += out_currOffset;
free(ek);
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
@ -783,9 +801,13 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
#ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::decrypt() " << std::endl;
#endif
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
try
{
out = NULL ;
// check that the input block has a valid format.
uint32_t offset = 0 ;
@ -826,8 +848,6 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
// decrypt
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
bool succeed = false;
for(uint32_t j=0;j<keys.size() && !succeed;++j)
@ -855,6 +875,9 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
{
succeed = EVP_OpenInit(&ctx, EVP_aes_128_cbc(),in + encrypted_keys_offset + i*MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE , MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE, in+IV_offset, privateKey);
if(!succeed)
EVP_CIPHER_CTX_cleanup(&ctx);
#ifdef GXS_SECURITY_DEBUG
std::cerr << " encrypted key at offset " << encrypted_keys_offset + i*MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE << ": " << succeed << std::endl;
#endif
@ -890,6 +913,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
#ifdef GXS_SECURITY_DEBUG
std::cerr << " successfully decrypted block of size " << outlen << std::endl;
#endif
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
catch(std::exception& e)
@ -905,6 +929,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
out = NULL ;
}
EVP_CIPHER_CTX_cleanup(&ctx);
return false;
}
}

View file

@ -1626,8 +1626,10 @@ void RsGxsNetService::recvNxsItemQueue()
GXSNETDEBUG_P_(item->PeerId()) << " decrypted item " << std::endl;
#endif
}
#ifdef NXS_NET_DEBUG_7
else
std::cerr << "(EE) Could not decrypt incoming encrypted NXS item. Probably a friend subscribed to a circle-restricted group." << std::endl;
GXSNETDEBUG_P_(item->PeerId()) << " (EE) Could not decrypt incoming encrypted NXS item. Probably a friend subscribed to a circle-restricted group." << std::endl;
#endif
}
switch(ni->PacketSubType())
@ -1638,7 +1640,8 @@ void RsGxsNetService::recvNxsItemQueue()
case RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY_ITEM:handleRecvPublishKeys (dynamic_cast<RsNxsGroupPublishKeyItem*>(ni)) ; break ;
default:
std::cerr << "Unhandled item subtype " << (uint32_t) ni->PacketSubType() << " in RsGxsNetService: " << std::endl; break;
if(ni->PacketSubType() != RS_PKT_SUBTYPE_NXS_ENCRYPTED_DATA_ITEM)
std::cerr << "Unhandled item subtype " << (uint32_t) ni->PacketSubType() << " in RsGxsNetService: " << std::endl; break;
}
delete item ;
}
@ -3861,7 +3864,9 @@ bool RsGxsNetService::decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypt
if(!GxsSecurity::decrypt(decrypted_mem,decrypted_len, (uint8_t*)encrypted_item->encrypted_data.bin_data,encrypted_item->encrypted_data.bin_len,private_keys))
{
std::cerr << "Failed! Cannot decrypt this item." << std::endl;
#ifdef NXS_NET_DEBUG_7
GXSNETDEBUG_P_(encrypted_item->PeerId()) << " Failed! Cannot decrypt this item." << std::endl;
#endif
decrypted_mem = NULL ; // for safety
return false ;
}
@ -3876,6 +3881,7 @@ bool RsGxsNetService::decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypt
if(decrypted_mem!=NULL)
{
ditem = RsNxsSerialiser(mServType).deserialise(decrypted_mem,&decrypted_len) ;
free(decrypted_mem) ;
if(ditem != NULL)
{

View file

@ -1074,6 +1074,12 @@ int pqissl::Initiate_SSL_Connection()
"pqissl::Initiate_SSL_Connection() SSL Connection Okay");
#endif
if(ssl_connection != NULL)
{
SSL_shutdown(ssl_connection);
SSL_free(ssl_connection) ;
}
ssl_connection = ssl;
net_internal_SSL_set_fd(ssl, sockfd);

View file

@ -491,7 +491,7 @@ int pqistreamer::handleoutgoing_locked()
// send a out_pkt., else send out_data. unless there is a pending packet. The strategy is to
// - grab as many packets as possible while below the optimal packet size, so as to allow some packing and decrease encryption padding overhead (suposeddly)
// - limit packets size to OPTIMAL_PACKET_SIZE when sending big packets so as to keep as much QoS as possible.
if (!mPkt_wpending)
{
void *dta;

View file

@ -366,11 +366,7 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck
#ifdef LOCALNET_TESTING
>> parameter('R',"restrict-port" ,portRestrictions ,"port1-port2","Apply port restriction" ,false)
#endif
#ifdef __APPLE__
>> help('h',"help","Display this Help") ;
#else
>> help() ;
#endif
as.defaultErrorHandling(true) ;

View file

@ -726,7 +726,7 @@ bool p3turtle::loadList(std::list<RsItem*>& load)
}
}
delete vitem ;
delete *it ;
}
load.clear() ;
return true ;

View file

@ -140,16 +140,11 @@ namespace
protected:
inline OptionHolder(char s,
const char* l,
const char* desc);
#ifdef __APPLE__
const char* desc);
friend OptionHolder help(char s,
const char* l,
const char* desc);
#else
friend OptionHolder help(char s='h',
const char* l="help",
const char* desc="Display this help");
#endif
private:
std::string shortName_;
std::string longName_;