added serialisation for encrypted items

This commit is contained in:
csoler 2015-09-12 16:27:01 -04:00
parent 08d9dcbe22
commit 85e6eca2f0
2 changed files with 213 additions and 287 deletions

View File

@ -1,5 +1,6 @@
#include "rsnxsitems.h" #include "rsnxsitems.h"
#include "rsbaseserial.h" #include "rsbaseserial.h"
#include <iomanip>
/*** /***
* #define RSSERIAL_DEBUG 1 * #define RSSERIAL_DEBUG 1
@ -178,7 +179,7 @@ bool RsNxsMsg::serialise(void *data, uint32_t& size) const
ok &= setRawUInt8(data, size, &offset, pos); ok &= setRawUInt8(data, size, &offset, pos);
ok &= msgId.serialise(data, size, offset); ok &= msgId.serialise(data, size, offset);
ok &= grpId.serialise(data, size, offset); ok &= grpId.serialise(data, size, offset);
ok &= msg.SetTlv(data, tlvsize, &offset); ok &= msg.SetTlv(data, size, &offset);
ok &= meta.SetTlv(data, size, &offset); ok &= meta.SetTlv(data, size, &offset);
@ -212,7 +213,7 @@ bool RsNxsGrp::serialise(void *data, uint32_t& size) const
ok &= setRawUInt32(data, size, &offset, transactionNumber); ok &= setRawUInt32(data, size, &offset, transactionNumber);
ok &= setRawUInt8(data, size, &offset, pos); ok &= setRawUInt8(data, size, &offset, pos);
ok &= grpId.serialise(data, size, offset); ok &= grpId.serialise(data, size, offset);
ok &= grp.SetTlv(data, tlvsize, &offset); ok &= grp.SetTlv(data, size, &offset);
ok &= meta.SetTlv(data, size, &offset); ok &= meta.SetTlv(data, size, &offset);
if(offset != tlvsize){ if(offset != tlvsize){
@ -383,47 +384,61 @@ bool RsNxsGroupPublishKeyItem::serialise(void *data, uint32_t& size) const
return ok; return ok;
} }
bool RsNxsSessionKeyItem::serialise(void *data, uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
ok &= encrypted_key_data.SetTlv(data, size, &offset) ;
if(offset != tlvsize)
{
std::cerr << "RsNxsSerialiser::serialiseGroupPublishKeyItem() FAIL Size Error! " << std::endl;
ok = false;
}
if (!ok)
std::cerr << "RsNxsSerialiser::serialiseGroupPublishKeyItem( NOK" << std::endl;
return ok;
}
bool RsNxsEncryptedDataItem::serialise(void *data, uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
ok &= aes_encrypted_data.SetTlv(data, size, &offset) ;
if(offset != tlvsize)
{
std::cerr << "RsNxsSerialiser::serialiseGroupPublishKeyItem() FAIL Size Error! " << std::endl;
ok = false;
}
if (!ok)
std::cerr << "RsNxsSerialiser::serialiseGroupPublishKeyItem( NOK" << std::endl;
return ok;
}
/*** deserialisation ***/ /*** deserialisation ***/
RsNxsGrp* RsNxsSerialiser::deserialNxsGrpItem(void *data, uint32_t *size) RsNxsGrp* RsNxsSerialiser::deserialNxsGrpItem(void *data, uint32_t *size)
{ {
bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_GRP_ITEM);
#ifdef RSSERIAL_DEBUG uint32_t offset = 8;
std::cerr << "RsNxsSerialiser::deserialNxsGrp()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_GRP_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGrp() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGrp() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsGrp* item = new RsNxsGrp(SERVICE_TYPE); RsNxsGrp* item = new RsNxsGrp(SERVICE_TYPE);
/* skip the header */ /* skip the header */
offset += 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->pos)); ok &= getRawUInt8(data, *size, &offset, &(item->pos));
@ -431,7 +446,7 @@ RsNxsGrp* RsNxsSerialiser::deserialNxsGrpItem(void *data, uint32_t *size)
ok &= item->grp.GetTlv(data, *size, &offset); ok &= item->grp.GetTlv(data, *size, &offset);
ok &= item->meta.GetTlv(data, *size, &offset); ok &= item->meta.GetTlv(data, *size, &offset);
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGrp() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsGrp() FAIL size mismatch" << std::endl;
@ -456,44 +471,12 @@ RsNxsGrp* RsNxsSerialiser::deserialNxsGrpItem(void *data, uint32_t *size)
RsNxsMsg* RsNxsSerialiser::deserialNxsMsgItem(void *data, uint32_t *size){ RsNxsMsg* RsNxsSerialiser::deserialNxsMsgItem(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_MSG_ITEM);
std::cerr << "RsNxsSerialiser::deserialNxsMsg()" << std::endl; uint32_t offset = 8;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0; RsNxsMsg* item = new RsNxsMsg(SERVICE_TYPE);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_MSG_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsMsg() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsMsg() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsMsg* item = new RsNxsMsg(getRsItemService(rstype));
/* skip the header */ /* skip the header */
offset += 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->pos)); ok &= getRawUInt8(data, *size, &offset, &(item->pos));
ok &= item->msgId.deserialise(data, *size, offset); ok &= item->msgId.deserialise(data, *size, offset);
@ -501,7 +484,7 @@ RsNxsMsg* RsNxsSerialiser::deserialNxsMsgItem(void *data, uint32_t *size){
ok &= item->msg.GetTlv(data, *size, &offset); ok &= item->msg.GetTlv(data, *size, &offset);
ok &= item->meta.GetTlv(data, *size, &offset); ok &= item->meta.GetTlv(data, *size, &offset);
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsMsg() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsMsg() FAIL size mismatch" << std::endl;
@ -523,45 +506,45 @@ RsNxsMsg* RsNxsSerialiser::deserialNxsMsgItem(void *data, uint32_t *size){
return item; return item;
} }
bool RsNxsSerialiser::checkItemHeader(void *data,uint32_t *size,uint8_t subservice_type)
RsNxsSyncGrpReqItem* RsNxsSerialiser::deserialNxsSyncGrpReqItem(void *data, uint32_t *size){ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp()" << std::endl; std::cerr << "RsNxsSerialiser::checkItemHeader()" << std::endl;
#endif #endif
/* get the type and size */ /* get the type and size */
uint32_t rstype = getRsItemId(data); uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data); uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (SERVICE_TYPE != getRsItemService(rstype)) || (subservice_type != getRsItemSubType(rstype)))
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_SYNC_GRP_REQ_ITEM != getRsItemSubType(rstype)))
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp() FAIL wrong type" << std::endl; std::cerr << "RsNxsSerialiser::checkItemHeader() FAIL wrong type" << std::endl;
#endif #endif
return NULL; /* wrong type */ return false; /* wrong type */
} }
if (*size < rssize) /* check size */ if (*size < rssize) /* check size */
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp() FAIL wrong size" << std::endl; std::cerr << "RsNxsSerialiser::checkItemHeader() FAIL wrong size" << std::endl;
#endif #endif
return NULL; /* not enough data */ return false; /* not enough data */
} }
/* set the packet length */ /* set the packet length */
*size = rssize; *size = rssize;
bool ok = true; return true ;
}
RsNxsSyncGrpReqItem* item = new RsNxsSyncGrpReqItem(getRsItemService(rstype)); RsNxsSyncGrpReqItem* RsNxsSerialiser::deserialNxsSyncGrpReqItem(void *data, uint32_t *size)
{
bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_SYNC_GRP_REQ_ITEM);
RsNxsSyncGrpReqItem* item = new RsNxsSyncGrpReqItem(SERVICE_TYPE);
/* skip the header */ /* skip the header */
offset += 8; uint32_t offset = 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->flag)); ok &= getRawUInt8(data, *size, &offset, &(item->flag));
@ -569,7 +552,7 @@ RsNxsSyncGrpReqItem* RsNxsSerialiser::deserialNxsSyncGrpReqItem(void *data, uint
ok &= GetTlvString(data, *size, &offset, TLV_TYPE_STR_HASH_SHA1, item->syncHash); ok &= GetTlvString(data, *size, &offset, TLV_TYPE_STR_HASH_SHA1, item->syncHash);
ok &= getRawUInt32(data, *size, &offset, &(item->updateTS)); ok &= getRawUInt32(data, *size, &offset, &(item->updateTS));
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp() FAIL size mismatch" << std::endl;
@ -594,42 +577,11 @@ RsNxsSyncGrpReqItem* RsNxsSerialiser::deserialNxsSyncGrpReqItem(void *data, uint
RsNxsSyncGrpItem* RsNxsSerialiser::deserialNxsSyncGrpItem(void *data, uint32_t *size){ RsNxsSyncGrpItem* RsNxsSerialiser::deserialNxsSyncGrpItem(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM);
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsSyncGrpItem* item = new RsNxsSyncGrpItem(SERVICE_TYPE); RsNxsSyncGrpItem* item = new RsNxsSyncGrpItem(SERVICE_TYPE);
/* skip the header */ /* skip the header */
offset += 8; uint32_t offset = 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->flag)); ok &= getRawUInt8(data, *size, &offset, &(item->flag));
@ -637,7 +589,7 @@ RsNxsSyncGrpItem* RsNxsSerialiser::deserialNxsSyncGrpItem(void *data, uint32_t *
ok &= getRawUInt32(data, *size, &offset, &(item->publishTs)); ok &= getRawUInt32(data, *size, &offset, &(item->publishTs));
ok &= item->authorId.deserialise(data, *size, offset); ok &= item->authorId.deserialise(data, *size, offset);
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpItem() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpItem() FAIL size mismatch" << std::endl;
@ -661,41 +613,8 @@ RsNxsSyncGrpItem* RsNxsSerialiser::deserialNxsSyncGrpItem(void *data, uint32_t *
RsNxsTransacItem* RsNxsSerialiser::deserialNxsTransacItem(void *data, uint32_t *size){ RsNxsTransacItem* RsNxsSerialiser::deserialNxsTransacItem(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_TRANSAC_ITEM);
std::cerr << "RsNxsSerialiser::deserialNxsTrans()" << std::endl; uint32_t offset = 8 ;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_TRANSAC_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem( FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
/* skip the header */
offset += 8;
bool ok = true;
RsNxsTransacItem* item = new RsNxsTransacItem(SERVICE_TYPE); RsNxsTransacItem* item = new RsNxsTransacItem(SERVICE_TYPE);
@ -704,7 +623,7 @@ RsNxsTransacItem* RsNxsSerialiser::deserialNxsTransacItem(void *data, uint32_t *
ok &= getRawUInt32(data, *size, &offset, &(item->nItems)); ok &= getRawUInt32(data, *size, &offset, &(item->nItems));
ok &= getRawUInt32(data, *size, &offset, &(item->updateTS)); ok &= getRawUInt32(data, *size, &offset, &(item->updateTS));
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsTrans() FAIL size mismatch" << std::endl;
@ -730,42 +649,10 @@ RsNxsTransacItem* RsNxsSerialiser::deserialNxsTransacItem(void *data, uint32_t *
RsNxsSyncMsgItem* RsNxsSerialiser::deserialNxsSyncMsgItem(void *data, uint32_t *size){ RsNxsSyncMsgItem* RsNxsSerialiser::deserialNxsSyncMsgItem(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_SYNC_MSG_ITEM);
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem()" << std::endl; uint32_t offset = 8 ;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0; RsNxsSyncMsgItem* item = new RsNxsSyncMsgItem(SERVICE_TYPE);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_SYNC_MSG_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem( FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsSyncMsgItem* item = new RsNxsSyncMsgItem(getRsItemService(rstype));
/* skip the header */
offset += 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->flag)); ok &= getRawUInt8(data, *size, &offset, &(item->flag));
@ -773,7 +660,7 @@ RsNxsSyncMsgItem* RsNxsSerialiser::deserialNxsSyncMsgItem(void *data, uint32_t *
ok &= item->msgId.deserialise(data, *size, offset); ok &= item->msgId.deserialise(data, *size, offset);
ok &= item->authorId.deserialise(data, *size, offset); ok &= item->authorId.deserialise(data, *size, offset);
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsSyncMsgItem() FAIL size mismatch" << std::endl;
@ -798,45 +685,10 @@ RsNxsSyncMsgItem* RsNxsSerialiser::deserialNxsSyncMsgItem(void *data, uint32_t *
RsNxsSyncMsgReqItem* RsNxsSerialiser::deserialNxsSyncMsgReqItem(void *data, uint32_t *size) RsNxsSyncMsgReqItem* RsNxsSerialiser::deserialNxsSyncMsgReqItem(void *data, uint32_t *size)
{ {
bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_SYNC_MSG_REQ_ITEM);
uint32_t offset = 8 ;
RsNxsSyncMsgReqItem* item = new RsNxsSyncMsgReqItem(SERVICE_TYPE);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrp()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_SYNC_MSG_REQ_ITEM != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsg() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsg() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsSyncMsgReqItem* item = new RsNxsSyncMsgReqItem(getRsItemService(rstype));
/* skip the header */
offset += 8;
ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber)); ok &= getRawUInt32(data, *size, &offset, &(item->transactionNumber));
ok &= getRawUInt8(data, *size, &offset, &(item->flag)); ok &= getRawUInt8(data, *size, &offset, &(item->flag));
@ -845,7 +697,7 @@ RsNxsSyncMsgReqItem* RsNxsSerialiser::deserialNxsSyncMsgReqItem(void *data, uint
ok &= item->grpId.deserialise(data, *size, offset); ok &= item->grpId.deserialise(data, *size, offset);
ok &= getRawUInt32(data, *size, &offset, &(item->updateTS)); ok &= getRawUInt32(data, *size, &offset, &(item->updateTS));
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncMsg() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsSyncMsg() FAIL size mismatch" << std::endl;
@ -868,46 +720,15 @@ RsNxsSyncMsgReqItem* RsNxsSerialiser::deserialNxsSyncMsgReqItem(void *data, uint
} }
RsNxsGroupPublishKeyItem* RsNxsSerialiser::deserialNxsGroupPublishKeyItem(void *data, uint32_t *size) RsNxsGroupPublishKeyItem* RsNxsSerialiser::deserialNxsGroupPublishKeyItem(void *data, uint32_t *size)
{ {
bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY_ITEM);
uint32_t offset = 8 ;
RsNxsGroupPublishKeyItem* item = new RsNxsGroupPublishKeyItem(SERVICE_TYPE);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY_ITEM != getRsItemSubType(rstype)))
{
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL wrong type" << std::endl;
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL wrong size" << std::endl;
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsGroupPublishKeyItem* item = new RsNxsGroupPublishKeyItem(getRsItemService(rstype));
/* skip the header */
offset += 8;
ok &= item->grpId.deserialise(data, *size, offset); ok &= item->grpId.deserialise(data, *size, offset);
ok &= item->key.GetTlv(data, *size, &offset) ; ok &= item->key.GetTlv(data, *size, &offset) ;
if (offset != rssize) if (offset != *size)
{ {
#ifdef RSSERIAL_DEBUG #ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL size mismatch" << std::endl; std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL size mismatch" << std::endl;
@ -931,15 +752,65 @@ RsNxsGroupPublishKeyItem* RsNxsSerialiser::deserialNxsGroupPublishKeyItem(void *
RsNxsSessionKeyItem *RsNxsSerialiser::deserialNxsSessionKeyItem(void* data, uint32_t *size) RsNxsSessionKeyItem *RsNxsSerialiser::deserialNxsSessionKeyItem(void* data, uint32_t *size)
{ {
std::cerr << __PRETTY_FUNCTION__ << ": Not implemented!" << std::endl; bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_SESSION_KEY_ITEM);
uint32_t offset = 8 ;
RsNxsSessionKeyItem* item = new RsNxsSessionKeyItem(SERVICE_TYPE);
ok &= item->encrypted_key_data.GetTlv(data,*size,&offset) ;
if (offset != *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL; return NULL;
} }
if (!ok)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
RsNxsEncryptedDataItem *RsNxsSerialiser::deserialNxsEncryptedDataItem(void* data, uint32_t *size) RsNxsEncryptedDataItem *RsNxsSerialiser::deserialNxsEncryptedDataItem(void* data, uint32_t *size)
{ {
std::cerr << __PRETTY_FUNCTION__ << ": Not implemented!" << std::endl; bool ok = checkItemHeader(data,size,RS_PKT_SUBTYPE_NXS_ENCRYPTED_DATA_ITEM);
uint32_t offset = 8 ;
RsNxsEncryptedDataItem* item = new RsNxsEncryptedDataItem(SERVICE_TYPE);
ok &= item->aes_encrypted_data.GetTlv(data,*size,&offset) ;
if (offset != *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL; return NULL;
} }
if (!ok)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsGroupPublishKeyItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*** size functions ***/ /*** size functions ***/
@ -1048,6 +919,24 @@ uint32_t RsNxsTransacItem::serial_size() const
return s; return s;
} }
uint32_t RsNxsEncryptedDataItem::serial_size() const
{
uint32_t s = 8; // header size
s += aes_encrypted_data.TlvSize() ;
return s;
}
uint32_t RsNxsSessionKeyItem::serial_size() const
{
uint32_t s = 8; // header size
s += encrypted_key_data.TlvSize() ;
return s;
}
int RsNxsGrp::refcount = 0; int RsNxsGrp::refcount = 0;
@ -1110,6 +999,13 @@ void RsNxsTransacItem::clear(){
timestamp = 0; timestamp = 0;
transactionNumber = 0; transactionNumber = 0;
} }
void RsNxsEncryptedDataItem::clear(){
aes_encrypted_data.TlvClear() ;
}
void RsNxsSessionKeyItem::clear(){
encrypted_key_data.TlvClear() ;
}
std::ostream& RsNxsSyncGrpReqItem::print(std::ostream &out, uint16_t indent) std::ostream& RsNxsSyncGrpReqItem::print(std::ostream &out, uint16_t indent)
{ {
@ -1276,3 +1172,31 @@ std::ostream& RsNxsTransacItem::print(std::ostream &out, uint16_t indent){
printRsItemEnd(out ,"RsNxsTransac", indent); printRsItemEnd(out ,"RsNxsTransac", indent);
return out; return out;
} }
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') ;
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 << std::dec << std::endl;
printRsItemEnd(out ,"RsNxsSessionKeyItem", indent);
return out;
}
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') ;
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] ;
out << std::dec << std::endl;
printRsItemEnd(out ,"RsNxsSessionKeyItem", indent);
return out;
}

View File

@ -237,7 +237,7 @@ public:
virtual void clear(); virtual void clear();
virtual std::ostream &print(std::ostream &out, uint16_t indent); virtual std::ostream &print(std::ostream &out, uint16_t indent);
/// grpId of grp held by sending peer /// Session key encrypted for the whole group
/// ///
RsTlvBinaryData encrypted_key_data ; RsTlvBinaryData encrypted_key_data ;
}; };
@ -528,16 +528,18 @@ public:
virtual RsItem* deserialise(void *data, uint32_t *size); virtual RsItem* deserialise(void *data, uint32_t *size);
private: private:
virtual RsNxsSyncGrpReqItem *deserialNxsSyncGrpReqItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_GRP */ RsNxsSyncGrpReqItem *deserialNxsSyncGrpReqItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_GRP */
virtual RsNxsSyncGrpItem *deserialNxsSyncGrpItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_GRP_ITEM */ RsNxsSyncGrpItem *deserialNxsSyncGrpItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_GRP_ITEM */
virtual RsNxsSyncMsgReqItem *deserialNxsSyncMsgReqItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_MSG */ RsNxsSyncMsgReqItem *deserialNxsSyncMsgReqItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_MSG */
virtual RsNxsSyncMsgItem *deserialNxsSyncMsgItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_MSG_ITEM */ RsNxsSyncMsgItem *deserialNxsSyncMsgItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_SYNC_MSG_ITEM */
virtual RsNxsGrp *deserialNxsGrpItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_GRP */ RsNxsGrp *deserialNxsGrpItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_GRP */
virtual RsNxsMsg *deserialNxsMsgItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_MSG */ RsNxsMsg *deserialNxsMsgItem(void *data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_MSG */
virtual RsNxsTransacItem *deserialNxsTransacItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_TRANS */ RsNxsTransacItem *deserialNxsTransacItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_TRANS */
virtual RsNxsGroupPublishKeyItem *deserialNxsGroupPublishKeyItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY */ RsNxsGroupPublishKeyItem *deserialNxsGroupPublishKeyItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY */
virtual RsNxsSessionKeyItem *deserialNxsSessionKeyItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_SESSION_KEY_ITEM */ RsNxsSessionKeyItem *deserialNxsSessionKeyItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_SESSION_KEY_ITEM */
virtual RsNxsEncryptedDataItem *deserialNxsEncryptedDataItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_ENCRYPTED_DATA_ITEM */ RsNxsEncryptedDataItem *deserialNxsEncryptedDataItem(void* data, uint32_t *size); /* RS_PKT_SUBTYPE_NXS_ENCRYPTED_DATA_ITEM */
bool checkItemHeader(void *data, uint32_t *size, uint8_t subservice_type);
const uint16_t SERVICE_TYPE; const uint16_t SERVICE_TYPE;
}; };