modifications to item encryption so as to match APIs

This commit is contained in:
csoler 2015-10-22 20:24:36 -04:00
parent 1c12178874
commit 21c31fb446
11 changed files with 230 additions and 106 deletions

View file

@ -1,5 +1,6 @@
#include "rsnxsitems.h"
#include "rsbaseserial.h"
#include "util/rsprint.h"
#include <iomanip>
/***
@ -392,7 +393,21 @@ bool RsNxsSessionKeyItem::serialise(void *data, uint32_t& size) const
if(!serialise_header(data,size,tlvsize,offset))
return false ;
ok &= encrypted_key_data.SetTlv(data, size, &offset) ;
if(offset + EVP_MAX_IV_LENGTH >= size)
{
std::cerr << "RsNxsSessionKeyItem::serialize(): error. Not enough room for IV !" << std::endl;
return false ;
}
memcpy(&((uint8_t*)data)[offset],iv,EVP_MAX_IV_LENGTH) ;
offset += EVP_MAX_IV_LENGTH ;
ok &= setRawUInt32(data, size, &offset, encrypted_session_keys.size());
for(std::map<RsGxsId,RsTlvBinaryData>::const_iterator it(encrypted_session_keys.begin());it!=encrypted_session_keys.end();++it)
{
ok &= it->first.serialise(data, size, offset) ;
ok &= it->second.SetTlv(data, size, &offset) ;
}
if(offset != tlvsize)
{
@ -757,7 +772,27 @@ RsNxsSessionKeyItem *RsNxsSerialiser::deserialNxsSessionKeyItem(void* data,
RsNxsSessionKeyItem* item = new RsNxsSessionKeyItem(SERVICE_TYPE);
ok &= item->encrypted_key_data.GetTlv(data,*size,&offset) ;
if(offset + EVP_MAX_IV_LENGTH >= *size)
{
std::cerr << __PRETTY_FUNCTION__ << ": not enough room for IV." << std::endl;
return NULL ;
}
memcpy(item->iv,&((uint8_t*)data)[offset],EVP_MAX_IV_LENGTH) ;
offset += EVP_MAX_IV_LENGTH ;
uint32_t n ;
ok &= getRawUInt32(data, *size, &offset, &n) ;
for(uint32_t i=0;ok && i<n;++i)
{
RsGxsId gxs_id ;
RsTlvBinaryData bdata(0) ;
ok &= gxs_id.deserialise(data,*size,offset) ;
ok &= bdata.GetTlv(data,*size,&offset) ;
item->encrypted_session_keys[gxs_id] = bdata ;
}
if (offset != *size)
{
@ -931,7 +966,11 @@ uint32_t RsNxsSessionKeyItem::serial_size() const
{
uint32_t s = 8; // header size
s += encrypted_key_data.TlvSize() ;
s += EVP_MAX_IV_LENGTH ; // iv
s += 4 ; // encrypted_session_keys.size() ;
for(std::map<RsGxsId,RsTlvBinaryData>::const_iterator it(encrypted_session_keys.begin());it!=encrypted_session_keys.end();++it)
s += it->first.serial_size() + it->second.TlvSize() ;
return s;
}
@ -1002,8 +1041,12 @@ void RsNxsTransacItem::clear(){
void RsNxsEncryptedDataItem::clear(){
aes_encrypted_data.TlvClear() ;
}
void RsNxsSessionKeyItem::clear(){
encrypted_key_data.TlvClear() ;
void RsNxsSessionKeyItem::clear()
{
for(std::map<RsGxsId,RsTlvBinaryData>::iterator it(encrypted_session_keys.begin());it!=encrypted_session_keys.end();++it)
it->second.TlvClear() ;
encrypted_session_keys.clear() ;
}
std::ostream& RsNxsSyncGrpReqItem::print(std::ostream &out, uint16_t indent)
@ -1176,13 +1219,13 @@ std::ostream& RsNxsSessionKeyItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsNxsSessionKeyItem", indent);
out << "encrypted key data: " << std::hex << std::setw(2) << std::setfill('0') ;
out << " iv: " << RsUtil::BinToHex((char*)iv,EVP_MAX_IV_LENGTH) << std::endl;
for(uint32_t i=0;i<std::min(50u,encrypted_key_data.bin_len);++i)
out << (int)((unsigned char*)encrypted_key_data.bin_data)[i] ;
out << " encrypted keys: " << std::endl;
for(std::map<RsGxsId,RsTlvBinaryData>::const_iterator it(encrypted_session_keys.begin());it!=encrypted_session_keys.end();++it)
out << " id=" << it->first << ": ekey=" << RsUtil::BinToHex((char*)it->second.bin_data,it->second.bin_len) << std::endl;
out << std::dec << std::endl;
printRsItemEnd(out ,"RsNxsSessionKeyItem", indent);
return out;
}
@ -1190,13 +1233,13 @@ std::ostream& RsNxsEncryptedDataItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsNxsEncryptedDataItem", indent);
out << "encrypted data: " << std::hex << std::setw(2) << std::setfill('0') ;
out << " encrypted data: " << RsUtil::BinToHex((char*)aes_encrypted_data.bin_data,std::min(50u,aes_encrypted_data.bin_len)) ;
for(uint32_t i=0;i<std::min(50u,aes_encrypted_data.bin_len);++i)
out << (int)((unsigned char *)aes_encrypted_data.bin_data)[i] ;
if(aes_encrypted_data.bin_len > 50u)
out << "..." ;
out << std::endl;
out << std::dec << std::endl;
printRsItemEnd(out ,"RsNxsSessionKeyItem", indent);
return out;
}

View file

@ -28,6 +28,7 @@
#include <map>
#include <openssl/ssl.h>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
@ -228,7 +229,7 @@ class RsNxsSessionKeyItem : public RsNxsItem
public:
RsNxsSessionKeyItem(uint16_t servtype) : RsNxsItem(servtype, RS_PKT_SUBTYPE_NXS_SESSION_KEY_ITEM),encrypted_key_data(servtype) { clear(); }
RsNxsSessionKeyItem(uint16_t servtype) : RsNxsItem(servtype, RS_PKT_SUBTYPE_NXS_SESSION_KEY_ITEM) { clear(); }
virtual ~RsNxsSessionKeyItem() {}
virtual bool serialise(void *data,uint32_t& size) const;
@ -239,8 +240,8 @@ public:
/// Session key encrypted for the whole group
///
RsTlvBinaryData initialisation_vector ;
std::map<RsGxsId, RsTlvBinaryData> encrypted_session_keys;
uint8_t iv[EVP_MAX_IV_LENGTH] ; // initialisation vector
std::map<RsGxsId, RsTlvBinaryData> encrypted_session_keys; // encrypted session keys
};
/*!
* Use to send to peer list of grps

View file

@ -176,16 +176,16 @@ const uint16_t TLV_TYPE_CERT_X509 = 0x0101;
const uint16_t TLV_TYPE_CERT_OPENPGP = 0x0102;
const uint16_t TLV_TYPE_KEY_EVP_PKEY = 0x0110; /* Used (Generic - Distrib) */
const uint16_t TLV_TYPE_KEY_PRIV_RSA = 0x0111; /* not used yet */
const uint16_t TLV_TYPE_KEY_PUB_RSA = 0x0112; /* not used yet */
const uint16_t TLV_TYPE_KEY_PRIV_RSA = 0x0111; /* not used yet */
const uint16_t TLV_TYPE_KEY_PUB_RSA = 0x0112; /* not used yet */
const uint16_t TLV_TYPE_SIGN_RSA_SHA1 = 0x0120; /* Used (Distrib/Forums) */
const uint16_t TLV_TYPE_SIGN_RSA_SHA1 = 0x0120; /* Used (Distrib/Forums) */
const uint16_t TLV_TYPE_BIN_IMAGE = 0x0130; /* Used (Generic - Forums) */
const uint16_t TLV_TYPE_BIN_FILEDATA = 0x0140; /* Used - ACTIVE! */
const uint16_t TLV_TYPE_BIN_IMAGE = 0x0130; /* Used (Generic - Forums) */
const uint16_t TLV_TYPE_BIN_FILEDATA = 0x0140; /* Used - ACTIVE! */
const uint16_t TLV_TYPE_BIN_SERIALISE = 0x0150; /* Used (Generic - Distrib) */
const uint16_t TLV_TYPE_BIN_GENERIC = 0x0160; /* Used (DSDV Data) */
const uint16_t TLV_TYPE_BIN_ENCRYPTED = 0x0170; /* Encrypted data
const uint16_t TLV_TYPE_BIN_GENERIC = 0x0160; /* Used (DSDV Data) */
const uint16_t TLV_TYPE_BIN_ENCRYPTED = 0x0170; /* Encrypted data */
/**** Compound Types ****/

View file

@ -36,10 +36,14 @@
/*!********************************** RsTlvFileBinaryData **********************************/
RsTlvBinaryData::RsTlvBinaryData()
:tlvtype(0), bin_len(0), bin_data(NULL)
{
}
RsTlvBinaryData::RsTlvBinaryData(uint16_t t)
:tlvtype(t), bin_len(0), bin_data(NULL)
{
return;
}
RsTlvBinaryData::RsTlvBinaryData(const RsTlvBinaryData &b)

View file

@ -35,26 +35,29 @@
class RsTlvBinaryData: public RsTlvItem
{
public:
RsTlvBinaryData(uint16_t t);
RsTlvBinaryData(const RsTlvBinaryData& b); // as per rule of three
void operator=(const RsTlvBinaryData& b); // as per rule of three
virtual ~RsTlvBinaryData(); // as per rule of three
virtual uint32_t TlvSize() const;
virtual void TlvClear(); /*! Initialize fields to empty legal values ( "0", "", etc) */
virtual void TlvShallowClear(); /*! Don't delete the binary data */
public:
RsTlvBinaryData();
RsTlvBinaryData(uint16_t t);
RsTlvBinaryData(const RsTlvBinaryData& b); // as per rule of three
void operator=(const RsTlvBinaryData& b); // as per rule of three
virtual ~RsTlvBinaryData(); // as per rule of three
virtual uint32_t TlvSize() const;
virtual void TlvClear(); /*! Initialize fields to empty legal values ( "0", "", etc) */
virtual void TlvShallowClear(); /*! Don't delete the binary data */
/// Serialise.
/*! Serialise Tlv to buffer(*data) of 'size' bytes starting at *offset */
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
/// Serialise.
/*! Serialise Tlv to buffer(*data) of 'size' bytes starting at *offset */
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
/// Deserialise.
/*! Deserialise Tlv buffer(*data) of 'size' bytes starting at *offset */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const; /*! Error/Debug util function */
/// Deserialise.
/*! Deserialise Tlv buffer(*data) of 'size' bytes starting at *offset */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const; /*! Error/Debug util function */
// mallocs the necessary size, and copies data into the allocated buffer in bin_data
bool setBinData(const void *data, uint32_t size);
// mallocs the necessary size, and copies data into the allocated buffer in bin_data
bool setBinData(const void *data, uint32_t size);
uint16_t tlvtype; /// set/checked against TLV input
uint32_t bin_len; /// size of malloc'ed data (not serialised)