added message encryption for distant msgs. Still missing decryption.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-GenericTunneling@6351 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-05-03 21:02:44 +00:00
parent 7708e6cd93
commit 6f769b3b54
9 changed files with 186 additions and 33 deletions

View File

@ -917,7 +917,37 @@ bool PGPHandler::addOrMergeKey(ops_keyring_t *keyring,std::map<std::string,PGPCe
return ret ;
}
// bool PGPHandler::encryptTextToString(const PGPIdType& key_id,const std::string& text,std::string& outstring)
// {
// RsStackMutex mtx(pgphandlerMtx) ; // lock access to PGP memory structures.
//
// const ops_keydata_t *public_key = getPublicKey(key_id) ;
//
// if(public_key == NULL)
// {
// 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!" << std::endl;
// return false ;
// }
//
// ops_create_info_t *info;
// ops_memory_t *buf = NULL ;
// ops_setup_memory_write(&info, &buf, 0);
//
// ops_encrypt_stream(info, public_key, NULL, ops_false, ops_true);
// ops_write(text.c_str(), text.length(), info);
// ops_writer_close(info);
//
// outstring = std::string((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) ;
// ops_create_info_delete(info);
//
// return true ;
// }
bool PGPHandler::encryptTextToFile(const PGPIdType& key_id,const std::string& text,const std::string& outfile)
{
RsStackMutex mtx(pgphandlerMtx) ; // lock access to PGP memory structures.

View File

@ -98,7 +98,9 @@ class PGPHandler
bool decryptDataBin(const PGPIdType& key_id,const void *data, const uint32_t len, unsigned char *decrypted_data, unsigned int *decrypted_data_len) ;
bool encryptTextToFile(const PGPIdType& key_id,const std::string& text,const std::string& outfile) ;
bool decryptTextFromFile(const PGPIdType& key_id,std::string& text,const std::string& inputfile) ;
bool decryptTextFromFile(const PGPIdType& key_id,std::string& text,const std::string& encrypted_inputfile) ;
//bool encryptTextToString(const PGPIdType& key_id,const std::string& text,std::string& outstring) ;
//bool decryptTextFromString(const PGPIdType& key_id,const std::string& encrypted_text,std::string& outstring) ;
bool getKeyFingerprint(const PGPIdType& id,PGPFingerprintType& fp) const ;
void setAcceptConnexion(const PGPIdType&,bool) ;

View File

@ -60,11 +60,21 @@ bool AuthGPG::decryptTextFromFile(std::string& text,const std::string& inputfile
return PGPHandler::decryptTextFromFile(mOwnGpgId,text,inputfile) ;
}
// bool AuthGPG::decryptTextFromString(std::string& encrypted_text,std::string& output)
// {
// return PGPHandler::decryptTextFromString(mOwnGpgId,encrypted_text,output) ;
// }
bool AuthGPG::encryptTextToFile(const std::string& text,const std::string& outfile)
{
return PGPHandler::encryptTextToFile(mOwnGpgId,text,outfile) ;
}
// bool AuthGPG::encryptTextToString(const std::string& pgp_id,const std::string& text,std::string& outstr)
// {
// return PGPHandler::encryptTextToString(PGPIdType(pgp_id),text,outstr) ;
// }
std::string pgp_pwd_callback(void * /*hook*/, const char *uid_hint, const char * /*passphrase_info*/, int prev_was_bad)
{
#define GPG_DEBUG2

View File

@ -221,6 +221,9 @@ class AuthGPG: public p3Config, public RsThread, public PGPHandler
virtual bool decryptTextFromFile( std::string& text,const std::string& filename);
virtual bool encryptTextToFile (const std::string& text,const std::string& filename);
// virtual bool decryptTextFromString( std::string& encrypted_text,std::string& clear_string);
// virtual bool encryptTextToString (const std::string& pgp_id,const std::string& clear_text,std::string& encrypted_string);
bool getGPGFilteredList(std::list<std::string>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) ;
//END of PGP public functions

View File

@ -99,6 +99,7 @@ class MessageInfo
std::wstring attach_title;
std::wstring attach_comment;
std::list<FileInfo> files;
std::map<std::string,std::string> encryption_keys ; // for concerned ids only the public pgp key id to encrypt the message with.
int size; /* total of files */
int count; /* file count */

View File

@ -71,6 +71,7 @@ const uint8_t RS_PKT_SUBTYPE_MSG_TAG_TYPE = 0x03;
const uint8_t RS_PKT_SUBTYPE_MSG_TAGS = 0x04;
const uint8_t RS_PKT_SUBTYPE_MSG_SRC_TAG = 0x05;
const uint8_t RS_PKT_SUBTYPE_MSG_PARENT_TAG = 0x06;
const uint8_t RS_PKT_SUBTYPE_MSG_ENCRYPTED = 0x07;
typedef uint64_t ChatLobbyId ;
typedef uint64_t ChatLobbyMsgId ;
@ -443,6 +444,36 @@ std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvFileSet attachment;
};
// // Encrypted messages encapsulate a whole RsMsgItem into the binary data. The signature is optional.
// // There is no To or CC peers since the whole communiation addresses are obfuscated in the encrypted
// // layer, and because of public key encryption, the email can only have one destination peer.
// //
// class RsEncryptedMsgItem: public RsItem
// {
// public:
// RsEncryptedMsgItem()
// :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG, RS_PKT_SUBTYPE_MSG_ENCRYPTED)
// {
// setPriorityLevel(QOS_PRIORITY_RS_MSG_ITEM) ;
// }
//
// // RsEncryptedMsgItem(uint16_t type)
// // :RsItem(RS_PKT_VERSION_SERVICE, type, RS_PKT_SUBTYPE_DEFAULT)
// // {
// // setPriorityLevel(QOS_PRIORITY_RS_MSG_ITEM) ;
// // }
//
// virtual ~RsEncryptedMsgItem();
// virtual void clear();
// std::ostream &print(std::ostream &out, uint16_t indent = 0);
//
// uint32_t msgId;
// unsigned char *encrypted_bin_data ;
// uint32_t encrypted_bin_size ;
// unsigned char *signature_bin_data ;
// uint32_t signature_bin_size ;
// };
class RsMsgTagType : public RsItem
{
public:
@ -523,45 +554,48 @@ public:
class RsMsgSerialiser: public RsSerialType
{
public:
RsMsgSerialiser(bool bConfiguration = false)
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG), m_bConfiguration (bConfiguration)
{ return; }
RsMsgSerialiser(uint16_t type)
:RsSerialType(RS_PKT_VERSION_SERVICE, type), m_bConfiguration (false)
{ return; }
virtual ~RsMsgSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
virtual RsItem * deserialise(void *data, uint32_t *size);
RsMsgSerialiser(bool bConfiguration = false)
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG), m_bConfiguration (bConfiguration)
{ return; }
RsMsgSerialiser(uint16_t type)
:RsSerialType(RS_PKT_VERSION_SERVICE, type), m_bConfiguration (false)
{ return; }
virtual ~RsMsgSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeMsgItem(RsMsgItem *);
virtual bool serialiseMsgItem (RsMsgItem *item, void *data, uint32_t *size);
virtual RsMsgItem *deserialiseMsgItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgItem(RsMsgItem *);
virtual bool serialiseMsgItem (RsMsgItem *item, void *data, uint32_t *size);
virtual RsMsgItem *deserialiseMsgItem(void *data, uint32_t *size);
virtual uint32_t sizeTagItem(RsMsgTagType *);
virtual bool serialiseTagItem (RsMsgTagType *item, void *data, uint32_t *size);
virtual RsMsgTagType *deserialiseTagItem(void *data, uint32_t *size);
virtual uint32_t sizeTagItem(RsMsgTagType *);
virtual bool serialiseTagItem (RsMsgTagType *item, void *data, uint32_t *size);
virtual RsMsgTagType *deserialiseTagItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgTagItem(RsMsgTags *);
virtual bool serialiseMsgTagItem (RsMsgTags *item, void *data, uint32_t *size);
virtual RsMsgTags *deserialiseMsgTagItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgTagItem(RsMsgTags *);
virtual bool serialiseMsgTagItem (RsMsgTags *item, void *data, uint32_t *size);
virtual RsMsgTags *deserialiseMsgTagItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgSrcIdItem(RsMsgSrcId *);
virtual bool serialiseMsgSrcIdItem (RsMsgSrcId *item, void *data, uint32_t *size);
virtual RsMsgSrcId *deserialiseMsgSrcIdItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgSrcIdItem(RsMsgSrcId *);
virtual bool serialiseMsgSrcIdItem (RsMsgSrcId *item, void *data, uint32_t *size);
virtual RsMsgSrcId *deserialiseMsgSrcIdItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgParentIdItem(RsMsgParentId *);
virtual bool serialiseMsgParentIdItem (RsMsgParentId *item, void *data, uint32_t *size);
virtual RsMsgParentId *deserialiseMsgParentIdItem(void *data, uint32_t *size);
virtual uint32_t sizeMsgParentIdItem(RsMsgParentId *);
virtual bool serialiseMsgParentIdItem (RsMsgParentId *item, void *data, uint32_t *size);
virtual RsMsgParentId *deserialiseMsgParentIdItem(void *data, uint32_t *size);
// virtual uint32_t sizeEncryptedMsgItem(RsEncryptedMsgItem *) ;
// virtual bool serialiseEncryptedMsgItem(RsEncryptedMsgItem *item, void *data, uint32_t *size);
// virtual RsEncryptedMsgItem *deserialiseEncryptedMsgItem(void *data, uint32_t *size);
bool m_bConfiguration; // is set to true for saving configuration (enables serialising msgId)
bool m_bConfiguration; // is set to true for saving configuration (enables serialising msgId)
};
/**************************************************************************/

View File

@ -33,6 +33,7 @@
#include "pqi/authgpg.h"
#include "services/p3msgservice.h"
#include "pgp/pgpkeyutil.h"
#include "pqi/pqinotify.h"
#include "util/rsdebug.h"
@ -1537,7 +1538,8 @@ RsMsgItem *p3MsgService::initMIRsMsg(MessageInfo &info, const std::string &to)
msg -> recvTime = 0;
msg -> subject = info.title;
msg -> message = info.msg;
msg -> message = info.msg;
std::list<std::string>::iterator pit;
for(pit = info.msgto.begin(); pit != info.msgto.end(); pit++)
@ -1583,11 +1585,78 @@ RsMsgItem *p3MsgService::initMIRsMsg(MessageInfo &info, const std::string &to)
if (info.msgflags & RS_MSG_FRIEND_RECOMMENDATION)
msg->msgFlags |= RS_MSG_FLAGS_FRIEND_RECOMMENDATION;
//std::cerr << "p3MsgService::initMIRsMsg()" << std::endl;
// See if we need to encrypt this message. If so, we replace the msg text
// by the whole message serialized and binary encrypted, so as to obfuscate
// all its content.
//
bool enc_ok = false ;
if(info.encryption_keys.find(to) != info.encryption_keys.end())
encryptMessage(info.encryption_keys[to],msg) ;
//std::cerr << "p3MsgService::initMIRsMsg()" << std::endl;
//msg->print(std::cerr);
return msg;
}
bool p3MsgService::encryptMessage(const std::string& pgp_id,RsMsgItem *item)
{
std::cerr << "Encrypting message with public key " << pgp_id << " in place." << std::endl;
// 1 - serialise the whole message item into a binary chunk.
//
uint32_t rssize = _serialiser->size(item) ;
unsigned char *data = new unsigned char[rssize] ;
if(!_serialiser->serialise(item,data,&rssize))
{
std::cerr << "(EE) p3MsgService::sendTurtleData(): Serialization error." << std::endl;
delete[] data ;
return false;
}
// 2 - pgp-encrypt the chunk with the user-supplied public key.
//
uint32_t encrypted_size = rssize + 1000 ;
unsigned char *encrypted_data = new unsigned char[encrypted_size] ;
if(!AuthGPG::getAuthGPG()->encryptDataBin(pgp_id,data,rssize,encrypted_data,&encrypted_size))
{
delete[] data ;
delete[] encrypted_data ;
std::cerr << "Encryption failed!" << std::endl;
return false;
}
delete[] data ;
// Now turn the binary encrypted chunk into a readable radix string.
//
std::string armoured_data = PGPKeyManagement::makeArmouredKey(encrypted_data,encrypted_size,"Retroshare encrypted message") ;
delete[] encrypted_data ;
std::wstring encrypted_msg ;
if(!librs::util::ConvertUtf8ToUtf16(armoured_data,encrypted_msg))
return false;
// wipe the item clean and replace the message by the encrypted data.
item->message = encrypted_msg ;
item->subject = L"" ;
item->msgcc.ids.clear() ;
item->msgbcc.ids.clear() ;
item->msgto.ids.clear() ;
item->msgFlags |= RS_MSG_FLAGS_ENCRYPTED ;
return true ;
}
bool p3MsgService::decryptMessage(RsMsgItem *item)
{
std::cerr << "Not implemented!!" << std::endl;
return true ;
}
void p3MsgService::connectToTurtleRouter(p3turtle *pt)
{
mTurtle = pt ;

View File

@ -137,6 +137,9 @@ int checkOutgoingMessages();
// Utility functions
bool encryptMessage(const std::string& pgp_id,RsMsgItem *msg) ;
bool decryptMessage(RsMsgItem *msg) ;
void manageDistantPeers() ;
void sendTurtleData(const std::string& hash,RsMsgItem *) ;
void handleIncomingItem(RsMsgItem *) ;

View File

@ -1285,6 +1285,7 @@ bool MessageComposer::sendMessage_internal(bool bDraftbox)
QMessageBox::warning(this, tr("RetroShare"), tr("Please insert at least one recipient."), QMessageBox::Ok);
return false; // Don't send with no recipient
}
mi.encryption_keys = _distant_peers ;
if (rsMsgs->MessageSend(mi) == false) {
return false;