diff --git a/libretroshare/src/serialiser/Makefile b/libretroshare/src/serialiser/Makefile index 487534bfd..94f21e631 100644 --- a/libretroshare/src/serialiser/Makefile +++ b/libretroshare/src/serialiser/Makefile @@ -8,11 +8,16 @@ include $(RS_TOP_DIR)/scripts/config.mk ############################################################### RSOBJ = rsserial.o rsbaseserial.o rstlvbase.o rstlvtypes.o -RSOBJ += rstlvfileitem.o rstlvutil.o # TLV Objs +RSOBJ += rstlvfileitem.o # TLV Objs +RSOBJ += rstlvkeys.o # TLV Objs +RSOBJ += rstlvimage.o # TLV Objs +RSOBJ += rstlvutil.o # TLV Objs RSOBJ += rsbaseitems.o rsconfigitems.o rsdiscitems.o # RsItems RSOBJ += rsmsgitems.o rsrankitems.o # RsItems RSOBJ += rsphotoitems.o # RsItems RSOBJ += rsgameitems.o # RsItems +RSOBJ += rsdistribitems.o # RsItems +RSOBJ += rsforumitems.o # RsItems RSOBJ += rsqblogitems.o # RsItems RSOBJ += rsstatusitems.o # RsItems diff --git a/libretroshare/src/serialiser/rsdistribitems.cc b/libretroshare/src/serialiser/rsdistribitems.cc new file mode 100644 index 000000000..1af365969 --- /dev/null +++ b/libretroshare/src/serialiser/rsdistribitems.cc @@ -0,0 +1,652 @@ + +/* + * libretroshare/src/serialiser: rsforumitems.cc + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include "serialiser/rsdistribitems.h" + +#include "serialiser/rsbaseserial.h" +#include "serialiser/rstlvbase.h" + +#define RSSERIAL_DEBUG 1 +#include + +/*************************************************************************/ + +void RsDistribMsg::clear() +{ + grpId.clear(); + parentId.clear(); + threadId.clear(); + timestamp = 0; + + + msgId.clear(); + publishSignature.TlvClear(); + personalSignature.TlvClear(); +} + +std::ostream &RsDistribMsg::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsDistribMsg", indent); + uint16_t int_Indent = indent + 2; + printIndent(out, int_Indent); + out << "grpId: " << grpId << std::endl; + printIndent(out, int_Indent); + out << "parentId: " << parentId << std::endl; + printIndent(out, int_Indent); + out << "threadId: " << threadId << std::endl; + + printIndent(out, int_Indent); + out << "timestamp: " << timestamp << std::endl; + + printIndent(out, int_Indent); + out << "<<<<<<<< Not Serialised >>>>>>>>" << std::endl; + + printIndent(out, int_Indent); + out << "msgId: " << msgId << std::endl; + publishSignature.print(out, int_Indent); + personalSignature.print(out, int_Indent); + + out << "<<<<<<<< Not Serialised >>>>>>>>" << std::endl; + + printRsItemEnd(out, "RsDistribMsg", indent); + return out; +} + + +void RsDistribSignedMsg::clear() +{ + grpId.clear(); + msgId.clear(); + flags = 0; + timestamp = 0; + packet.TlvClear(); + publishSignature.TlvClear(); + personalSignature.TlvClear(); + + return; +} + +std::ostream &RsDistribSignedMsg::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsDistribSignedMsg", indent); + uint16_t int_Indent = indent + 2; + + printIndent(out, int_Indent); + out << "grpId: " << grpId << std::endl; + printIndent(out, int_Indent); + out << "msgId: " << msgId << std::endl; + printIndent(out, int_Indent); + out << "flags: " << flags << std::endl; + printIndent(out, int_Indent); + out << "timestamp: " << timestamp << std::endl; + packet.print(out, 10); + publishSignature.print(out, 10); + personalSignature.print(out, 10); + + printRsItemEnd(out, "RsDistribSignedMsg", indent); + return out; +} + +/*************************************************************************/ + +void RsDistribGrp::clear() +{ + grpId.clear(); + timestamp = 0; + grpFlags = 0; + grpName.clear(); + grpDesc.clear(); + grpCategory.clear(); + + grpControlFlags = 0; + grpControlList.TlvClear(); + + grpPixmap.TlvClear(); + + adminKey.TlvClear(); + publishKeys.TlvClear(); + + adminSignature.TlvClear(); +} + +std::ostream &RsDistribGrp::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsDistribGrp", indent); + uint16_t int_Indent = indent + 2; + printIndent(out, int_Indent); + out << "grpId: " << grpId << std::endl; + printIndent(out, int_Indent); + out << "timestamp: " << timestamp << std::endl; + printIndent(out, int_Indent); + out << "grpFlags: " << grpFlags << std::endl; + + printIndent(out, int_Indent); + + std::string cnv_name(grpName.begin(), grpName.end()); + out << "grpName: " << cnv_name << std::endl; + + printIndent(out, int_Indent); + + std::string cnv_desc(grpDesc.begin(), grpDesc.end()); + out << "grpDesc: " << cnv_desc << std::endl; + + printIndent(out, int_Indent); + std::string cnv_category(grpCategory.begin(), grpCategory.end()); + out << "grpCategory: " << cnv_category << std::endl; + + printIndent(out, int_Indent); + out << "grpControlFlags: " << grpControlFlags << std::endl; + + grpControlList.print(out, int_Indent); + + grpPixmap.print(out, int_Indent); + + adminKey.print(out, int_Indent); + publishKeys.print(out, int_Indent); + adminSignature.print(out, int_Indent); + + printRsItemEnd(out, "RsDistribGrp", indent); + return out; +} + +/*************************************************************************/ + +void RsDistribGrpKey::clear() +{ + grpId.clear(); + key.TlvClear(); +} + +std::ostream &RsDistribGrpKey::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsDistribGrpKey", indent); + uint16_t int_Indent = indent + 2; + printIndent(out, int_Indent); + out << "grpId: " << grpId << std::endl; + + key.print(out, int_Indent); + + printRsItemEnd(out, "RsDistribGrpKey", indent); + return out; +} + +/*************************************************************************/ +/*************************************************************************/ + +uint32_t RsDistribSerialiser::sizeGrp(RsDistribGrp *item) +{ + uint32_t s = 8; /* header */ + /* RsDistribMsg stuff */ + s += GetTlvStringSize(item->grpId); + s += 4; /* timestamp */ + s += 4; /* grpFlags */ + s += GetTlvWideStringSize(item->grpName); + s += GetTlvWideStringSize(item->grpDesc); + s += GetTlvWideStringSize(item->grpCategory); + + s += 4; /* grpControlFlags */ + s += item->grpControlList.TlvSize(); + + s += item->grpPixmap.TlvSize(); + + s += item->adminKey.TlvSize(); + s += item->publishKeys.TlvSize(); + s += item->adminSignature.TlvSize(); + + return s; +} + +/* serialise the data to the buffer */ +bool RsDistribSerialiser::serialiseGrp(RsDistribGrp *item, void *data, uint32_t *pktsize) +{ + uint32_t tlvsize = sizeGrp(item); + uint32_t offset = 0; + + if (*pktsize < tlvsize) + return false; /* not enough space */ + + *pktsize = tlvsize; + + bool ok = true; + + ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); + + std::cerr << "RsDistribSerialiser::serialiseGrp() Header: " << ok << std::endl; + std::cerr << "RsDistribSerialiser::serialiseGrp() Size: " << tlvsize << std::endl; + + /* skip the header */ + offset += 8; + + /* RsDistribGrp */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpId: " << ok << std::endl; + ok &= setRawUInt32(data, tlvsize, &offset, item->timestamp); + std::cerr << "RsDistribSerialiser::serialiseGrp() timestamp: " << ok << std::endl; + ok &= setRawUInt32(data, tlvsize, &offset, item->grpFlags); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpFlags: " << ok << std::endl; + + ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_NAME, item->grpName); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpName: " << ok << std::endl; + ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_COMMENT, item->grpDesc); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpDesc: " << ok << std::endl; + ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_CATEGORY, item->grpCategory); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpCategory: " << ok << std::endl; + + ok &= setRawUInt32(data, tlvsize, &offset, item->grpControlFlags); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpControlFlags: " << ok << std::endl; + ok &= item->grpControlList.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpControlList: " << ok << std::endl; + + ok &= item->grpPixmap.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrp() grpPixmap: " << ok << std::endl; + + ok &= item->adminKey.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrp() adminKey: " << ok << std::endl; + ok &= item->publishKeys.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrp() publishKeys: " << ok << std::endl; + ok &= item->adminSignature.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrp() adminSignature: " << ok << std::endl; + + if (offset != tlvsize) + { + ok = false; + std::cerr << "RsDistribSerialiser::serialiseGrp() Size Error! " << std::endl; + } + + return ok; +} + + +RsDistribGrp *RsDistribSerialiser::deserialiseGrp(void *data, uint32_t *pktsize) +{ + /* 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)) || + (RS_SERVICE_TYPE_DISTRIB != getRsItemService(rstype)) || + (RS_PKT_SUBTYPE_DISTRIB_GRP != getRsItemSubType(rstype))) + { + return NULL; /* wrong type */ + } + + if (*pktsize < rssize) /* check size */ + return NULL; /* not enough data */ + + /* set the packet length */ + *pktsize = rssize; + + bool ok = true; + + /* ready to load */ + RsDistribGrp *item = new RsDistribGrp(); + item->clear(); + + /* skip the header */ + offset += 8; + + /* RsDistribGrp */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + ok &= getRawUInt32(data, rssize, &offset, &(item->timestamp)); + ok &= getRawUInt32(data, rssize, &offset, &(item->grpFlags)); + + ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_NAME, item->grpName); + ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_COMMENT, item->grpDesc); + ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_CATEGORY, item->grpCategory); + + ok &= getRawUInt32(data, rssize, &offset, &(item->grpControlFlags)); + ok &= item->grpControlList.GetTlv(data, rssize, &offset); + + ok &= item->grpPixmap.GetTlv(data, rssize, &offset); + + ok &= item->adminKey.GetTlv(data, rssize, &offset); + ok &= item->publishKeys.GetTlv(data, rssize, &offset); + ok &= item->adminSignature.GetTlv(data, rssize, &offset); + + if (offset != rssize) + { + /* error */ + delete item; + return NULL; + } + + if (!ok) + { + delete item; + return NULL; + } + + return item; +} + +/*************************************************************************/ + +uint32_t RsDistribSerialiser::sizeGrpKey(RsDistribGrpKey *item) +{ + uint32_t s = 8; /* header */ + s += GetTlvStringSize(item->grpId); + s += item->key.TlvSize(); + + return s; +} + +/* serialise the data to the buffer */ +bool RsDistribSerialiser::serialiseGrpKey(RsDistribGrpKey *item, void *data, uint32_t *pktsize) +{ + uint32_t tlvsize = sizeGrpKey(item); + uint32_t offset = 0; + + if (*pktsize < tlvsize) + return false; /* not enough space */ + + *pktsize = tlvsize; + + bool ok = true; + + ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); + + std::cerr << "RsDistribSerialiser::serialiseGrpKey() Header: " << ok << std::endl; + std::cerr << "RsDistribSerialiser::serialiseGrpKey() Size: " << tlvsize << std::endl; + + /* skip the header */ + offset += 8; + + /* RsDistribGrp */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + std::cerr << "RsDistribSerialiser::serialiseGrpKey() grpId: " << ok << std::endl; + + ok &= item->key.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseGrpKey() key: " << ok << std::endl; + + if (offset != tlvsize) + { + ok = false; + std::cerr << "RsDistribSerialiser::serialiseGrpKey() Size Error! " << std::endl; + } + + return ok; +} + + +RsDistribGrpKey *RsDistribSerialiser::deserialiseGrpKey(void *data, uint32_t *pktsize) +{ + /* 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)) || + (RS_SERVICE_TYPE_DISTRIB != getRsItemService(rstype)) || + (RS_PKT_SUBTYPE_DISTRIB_GRP_KEY != getRsItemSubType(rstype))) + { + return NULL; /* wrong type */ + } + + if (*pktsize < rssize) /* check size */ + return NULL; /* not enough data */ + + /* set the packet length */ + *pktsize = rssize; + + bool ok = true; + + /* ready to load */ + RsDistribGrpKey *item = new RsDistribGrpKey(); + item->clear(); + + /* skip the header */ + offset += 8; + + /* RsDistribGrp */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + ok &= item->key.GetTlv(data, rssize, &offset); + + if (offset != rssize) + { + /* error */ + delete item; + return NULL; + } + + if (!ok) + { + delete item; + return NULL; + } + + return item; +} + +/*************************************************************************/ + +uint32_t RsDistribSerialiser::sizeSignedMsg(RsDistribSignedMsg *item) +{ + uint32_t s = 8; /* header */ + /* RsDistribSignedMsg stuff */ + s += GetTlvStringSize(item->grpId); + s += GetTlvStringSize(item->msgId); + s += 4; /* flags */ + s += 4; /* timestamp */ + s += item->packet.TlvSize(); + s += item->publishSignature.TlvSize(); + s += item->personalSignature.TlvSize(); + + return s; +} + +/* serialise the data to the buffer */ +bool RsDistribSerialiser::serialiseSignedMsg(RsDistribSignedMsg *item, void *data, uint32_t *pktsize) +{ + uint32_t tlvsize = sizeSignedMsg(item); + uint32_t offset = 0; + + if (*pktsize < tlvsize) + return false; /* not enough space */ + + *pktsize = tlvsize; + + bool ok = true; + + ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); + + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() Header: " << ok << std::endl; + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() Size: " << tlvsize << std::endl; + + /* skip the header */ + offset += 8; + + /* RsDistribSignedMsg */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() grpId: " << ok << std::endl; + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSGID, item->msgId); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() msgId: " << ok << std::endl; + + ok &= setRawUInt32(data, tlvsize, &offset, item->flags); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() flags: " << ok << std::endl; + ok &= setRawUInt32(data, tlvsize, &offset, item->timestamp); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() timestamp: " << ok << std::endl; + + ok &= item->packet.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() packet: " << ok << std::endl; + + ok &= item->publishSignature.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() publishSignature: " << ok << std::endl; + ok &= item->personalSignature.SetTlv(data, tlvsize, &offset); + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() personalSignature: " << ok << std::endl; + + if (offset != tlvsize) + { + ok = false; + std::cerr << "RsDistribSerialiser::serialiseSignedMsg() Size Error! " << std::endl; + } + + return ok; +} + + +RsDistribSignedMsg *RsDistribSerialiser::deserialiseSignedMsg(void *data, uint32_t *pktsize) +{ + /* 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)) || + (RS_SERVICE_TYPE_DISTRIB != getRsItemService(rstype)) || + (RS_PKT_SUBTYPE_DISTRIB_SIGNED_MSG != getRsItemSubType(rstype))) + { + return NULL; /* wrong type */ + } + + if (*pktsize < rssize) /* check size */ + return NULL; /* not enough data */ + + /* set the packet length */ + *pktsize = rssize; + + bool ok = true; + + /* ready to load */ + RsDistribSignedMsg *item = new RsDistribSignedMsg(); + item->clear(); + + /* skip the header */ + offset += 8; + + /* RsDistribGrp */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSGID, item->msgId); + ok &= getRawUInt32(data, rssize, &offset, &(item->flags)); + ok &= getRawUInt32(data, rssize, &offset, &(item->timestamp)); + + ok &= item->packet.GetTlv(data, rssize, &offset); + + ok &= item->publishSignature.GetTlv(data, rssize, &offset); + ok &= item->personalSignature.GetTlv(data, rssize, &offset); + + if (offset != rssize) + { + /* error */ + delete item; + return NULL; + } + + if (!ok) + { + delete item; + return NULL; + } + + return item; +} + + + + + +uint32_t RsDistribSerialiser::size(RsItem *i) +{ + RsDistribGrp *dg; + RsDistribGrpKey *dgk; + RsDistribSignedMsg *dsm; + + /* in order of frequency */ + if (NULL != (dsm = dynamic_cast(i))) + { + return sizeSignedMsg(dsm); + } + else if (NULL != (dg = dynamic_cast(i))) + { + return sizeGrp(dg); + } + else if (NULL != (dgk = dynamic_cast(i))) + { + return sizeGrpKey(dgk); + } + + return 0; +} + +bool RsDistribSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize) +{ + RsDistribGrp *dg; + RsDistribGrpKey *dgk; + RsDistribSignedMsg *dsm; + + if (NULL != (dsm = dynamic_cast(i))) + { + return serialiseSignedMsg(dsm, data, pktsize); + } + else if (NULL != (dg = dynamic_cast(i))) + { + return serialiseGrp(dg, data, pktsize); + } + else if (NULL != (dgk = dynamic_cast(i))) + { + return serialiseGrpKey(dgk, data, pktsize); + } + return false; +} + +RsItem *RsDistribSerialiser::deserialise(void *data, uint32_t *pktsize) +{ + /* get the type and size */ + uint32_t rstype = getRsItemId(data); + + if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || + (RS_SERVICE_TYPE_DISTRIB != getRsItemService(rstype))) + { + return NULL; /* wrong type */ + } + + switch(getRsItemSubType(rstype)) + { + case RS_PKT_SUBTYPE_DISTRIB_GRP: + return deserialiseGrp(data, pktsize); + break; + case RS_PKT_SUBTYPE_DISTRIB_GRP_KEY: + return deserialiseGrpKey(data, pktsize); + break; + case RS_PKT_SUBTYPE_DISTRIB_SIGNED_MSG: + return deserialiseSignedMsg(data, pktsize); + break; + default: + return NULL; + break; + } + return NULL; +} + +/*************************************************************************/ +/*************************************************************************/ + diff --git a/libretroshare/src/serialiser/rsdistribitems.h b/libretroshare/src/serialiser/rsdistribitems.h new file mode 100644 index 000000000..118b8270f --- /dev/null +++ b/libretroshare/src/serialiser/rsdistribitems.h @@ -0,0 +1,179 @@ +#ifndef RS_DISTRIB_ITEMS_H +#define RS_DISTRIB_ITEMS_H + +/* + * libretroshare/src/serialiser: rsdistribitems.h + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include + +#include "serialiser/rsserviceids.h" +#include "serialiser/rsserial.h" +#include "serialiser/rstlvbase.h" +#include "serialiser/rstlvtypes.h" +#include "serialiser/rstlvkeys.h" + +const uint8_t RS_PKT_SUBTYPE_DISTRIB_GRP = 0x01; +const uint8_t RS_PKT_SUBTYPE_DISTRIB_GRP_KEY = 0x02; +const uint8_t RS_PKT_SUBTYPE_DISTRIB_SIGNED_MSG = 0x03; + +/**************************************************************************/ + + +class RsDistribMsg: public RsItem +{ + public: + RsDistribMsg(uint16_t servtype, uint8_t subtype) + :RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype) { return; } + +virtual ~RsDistribMsg() { return; } + +virtual void clear(); +virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + std::string grpId; /* Grp Id */ + std::string parentId; /* Parent Msg Id */ + std::string threadId; /* Thread Msg Id */ + uint32_t timestamp; + + /* Not Serialised */ + + std::string msgId; /* Msg Id */ + RsTlvKeySignature publishSignature; + RsTlvKeySignature personalSignature; +}; + +class RsDistribSignedMsg: public RsItem +{ + public: + RsDistribSignedMsg() + :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DISTRIB, RS_PKT_SUBTYPE_DISTRIB_SIGNED_MSG), + packet(TLV_TYPE_BIN_SERIALISE) + { return; } + +virtual ~RsDistribSignedMsg() { return; } + +virtual void clear(); +virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + std::string grpId; + std::string msgId; /* from publishSignature */ + uint32_t flags; + uint32_t timestamp; + + RsTlvBinaryData packet; + RsTlvKeySignature publishSignature; + RsTlvKeySignature personalSignature; +}; + + +class RsDistribGrp: public RsItem +{ + public: +// RsDistribGrp(uint16_t servtype, uint8_t subtype) +// :RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype) { return; } + + RsDistribGrp() + :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DISTRIB, RS_PKT_SUBTYPE_DISTRIB_GRP) + { return; } + +virtual ~RsDistribGrp() { return; } + +virtual void clear(); +virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + std::string grpId; /* Grp Id */ + uint32_t timestamp; + uint32_t grpFlags; + std::wstring grpName; + std::wstring grpDesc; + std::wstring grpCategory; + + RsTlvImage grpPixmap; + + uint32_t grpControlFlags; + RsTlvPeerIdSet grpControlList; + + RsTlvSecurityKey adminKey; + RsTlvSecurityKeySet publishKeys; + + RsTlvKeySignature adminSignature; +}; + + +class RsDistribGrpKey: public RsItem +{ + public: + + RsDistribGrpKey() + :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DISTRIB, RS_PKT_SUBTYPE_DISTRIB_GRP_KEY) + { return; } + +virtual ~RsDistribGrpKey() { return; } + +virtual void clear(); +virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + std::string grpId; /* Grp Id */ + RsTlvSecurityKey key; +}; + + +class RsDistribSerialiser: public RsSerialType +{ + public: + RsDistribSerialiser() + :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DISTRIB) + { return; } +virtual ~RsDistribSerialiser() + { 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: + + /* For RS_PKT_SUBTYPE_DISTRIB_GRP */ +virtual uint32_t sizeGrp(RsDistribGrp *); +virtual bool serialiseGrp (RsDistribGrp *item, void *data, uint32_t *size); +virtual RsDistribGrp *deserialiseGrp(void *data, uint32_t *size); + + /* For RS_PKT_SUBTYPE_DISTRIB_GRP_KEY */ +virtual uint32_t sizeGrpKey(RsDistribGrpKey *); +virtual bool serialiseGrpKey (RsDistribGrpKey *item, void *data, uint32_t *size); +virtual RsDistribGrpKey *deserialiseGrpKey(void *data, uint32_t *size); + + /* For RS_PKT_SUBTYPE_DISTRIB_SIGNED_MSG */ +virtual uint32_t sizeSignedMsg(RsDistribSignedMsg *); +virtual bool serialiseSignedMsg (RsDistribSignedMsg *item, void *data, uint32_t *size); +virtual RsDistribSignedMsg *deserialiseSignedMsg(void *data, uint32_t *size); + +}; + +/**************************************************************************/ + +#endif /* RS_FORUM_ITEMS_H */ + + diff --git a/libretroshare/src/serialiser/rsforumitems.cc b/libretroshare/src/serialiser/rsforumitems.cc new file mode 100644 index 000000000..66e61cc3d --- /dev/null +++ b/libretroshare/src/serialiser/rsforumitems.cc @@ -0,0 +1,220 @@ + +/* + * libretroshare/src/serialiser: rsforumitems.cc + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include "serialiser/rsforumitems.h" + +#include "serialiser/rsbaseserial.h" +#include "serialiser/rstlvbase.h" + +#define RSSERIAL_DEBUG 1 +#include + +/*************************************************************************/ + +void RsForumMsg::clear() +{ + RsDistribMsg::clear(); + + srcId.clear(); + title.clear(); + msg.clear(); +} + +std::ostream &RsForumMsg::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsForumMsg", indent); + uint16_t int_Indent = indent + 2; + + RsDistribMsg::print(out, int_Indent); + + printIndent(out, int_Indent); + out << "srcId: " << srcId << std::endl; + + printIndent(out, int_Indent); + + std::string cnv_title(title.begin(), title.end()); + out << "title: " << cnv_title << std::endl; + + printIndent(out, int_Indent); + + std::string cnv_msg(msg.begin(), msg.end()); + out << "msg: " << cnv_msg << std::endl; + + printRsItemEnd(out, "RsForumMsg", indent); + return out; +} + + +/*************************************************************************/ +/*************************************************************************/ + +uint32_t RsForumSerialiser::sizeMsg(RsForumMsg *item) +{ + uint32_t s = 8; /* header */ + /* RsDistribMsg stuff */ + s += GetTlvStringSize(item->grpId); + s += GetTlvStringSize(item->parentId); + s += GetTlvStringSize(item->threadId); + s += 4; /* timestamp */ + + /* RsForumMsg stuff */ + s += GetTlvStringSize(item->srcId); + s += GetTlvWideStringSize(item->title); + s += GetTlvWideStringSize(item->msg); + + return s; +} + +/* serialise the data to the buffer */ +bool RsForumSerialiser::serialiseMsg(RsForumMsg *item, void *data, uint32_t *pktsize) +{ + uint32_t tlvsize = sizeMsg(item); + uint32_t offset = 0; + + if (*pktsize < tlvsize) + return false; /* not enough space */ + + *pktsize = tlvsize; + + bool ok = true; + + ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); + + std::cerr << "RsForumSerialiser::serialiseMsg() Header: " << ok << std::endl; + std::cerr << "RsForumSerialiser::serialiseMsg() Size: " << tlvsize << std::endl; + + /* skip the header */ + offset += 8; + + /* RsDistribMsg first */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + std::cerr << "RsForumSerialiser::serialiseMsg() grpId: " << ok << std::endl; + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PARENTID, item->parentId); + std::cerr << "RsForumSerialiser::serialiseMsg() parentId: " << ok << std::endl; + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_THREADID, item->threadId); + std::cerr << "RsForumSerialiser::serialiseMsg() threadId: " << ok << std::endl; + + ok &= setRawUInt32(data, tlvsize, &offset, item->timestamp); + std::cerr << "RsForumSerialiser::serialiseMsg() timestamp: " << ok << std::endl; + + /* RsForumMsg */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->srcId); + std::cerr << "RsForumSerialiser::serialiseMsg() srcId: " << ok << std::endl; + + ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_TITLE, item->title); + std::cerr << "RsForumSerialiser::serialiseMsg() Title: " << ok << std::endl; + ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_MSG, item->msg); + std::cerr << "RsForumSerialiser::serialiseMsg() Msg: " << ok << std::endl; + + if (offset != tlvsize) + { + ok = false; + std::cerr << "RsForumSerialiser::serialiseMsg() Size Error! " << std::endl; + } + + return ok; +} + + + +RsForumMsg *RsForumSerialiser::deserialiseMsg(void *data, uint32_t *pktsize) +{ + /* 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)) || + (RS_SERVICE_TYPE_FORUM != getRsItemService(rstype)) || + (RS_PKT_SUBTYPE_FORUM_MSG != getRsItemSubType(rstype))) + { + return NULL; /* wrong type */ + } + + if (*pktsize < rssize) /* check size */ + return NULL; /* not enough data */ + + /* set the packet length */ + *pktsize = rssize; + + bool ok = true; + + /* ready to load */ + RsForumMsg *item = new RsForumMsg(); + item->clear(); + + /* skip the header */ + offset += 8; + + /* RsDistribMsg first */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GROUPID, item->grpId); + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PARENTID, item->parentId); + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_THREADID, item->threadId); + ok &= getRawUInt32(data, rssize, &offset, &(item->timestamp)); + + /* RsForumMsg */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->srcId); + ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_TITLE, item->title); + ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_MSG, item->msg); + + if (offset != rssize) + { + /* error */ + delete item; + return NULL; + } + + if (!ok) + { + delete item; + return NULL; + } + + return item; +} + + +uint32_t RsForumSerialiser::size(RsItem *item) +{ + return sizeMsg((RsForumMsg *) item); +} + +bool RsForumSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize) +{ + return serialiseMsg((RsForumMsg *) item, data, pktsize); +} + +RsItem *RsForumSerialiser::deserialise(void *data, uint32_t *pktsize) +{ + return deserialiseMsg(data, pktsize); +} + + + +/*************************************************************************/ + diff --git a/libretroshare/src/serialiser/rsforumitems.h b/libretroshare/src/serialiser/rsforumitems.h new file mode 100644 index 000000000..c682ab793 --- /dev/null +++ b/libretroshare/src/serialiser/rsforumitems.h @@ -0,0 +1,96 @@ +#ifndef RS_FORUM_ITEMS_H +#define RS_FORUM_ITEMS_H + +/* + * libretroshare/src/serialiser: rsforumitems.h + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include + +#include "serialiser/rsserviceids.h" +#include "serialiser/rsserial.h" +#include "serialiser/rstlvtypes.h" +#include "serialiser/rstlvkeys.h" + +#include "serialiser/rsdistribitems.h" + +const uint8_t RS_PKT_SUBTYPE_FORUM_GRP = 0x01; +const uint8_t RS_PKT_SUBTYPE_FORUM_MSG = 0x02; + +/**************************************************************************/ + +class RsForumMsg: public RsDistribMsg +{ + public: + RsForumMsg() + :RsDistribMsg(RS_SERVICE_TYPE_FORUM, RS_PKT_SUBTYPE_FORUM_MSG) { return; } +virtual ~RsForumMsg() { return; } + +virtual void clear(); +virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + /* + * RsDistribMsg has: + * grpId, parentId, threadId & timestamp. + */ + + std::string srcId; + + std::wstring title; + std::wstring msg; + +}; + +class RsForumSerialiser: public RsSerialType +{ + public: + RsForumSerialiser() + :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_FORUM) + { return; } +virtual ~RsForumSerialiser() + { 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: + + /* For RS_PKT_SUBTYPE_FORUM_GRP */ +//virtual uint32_t sizeGrp(RsForumGrp *); +//virtual bool serialiseGrp (RsForumGrp *item, void *data, uint32_t *size); +//virtual RsForumGrp *deserialiseGrp(void *data, uint32_t *size); + + /* For RS_PKT_SUBTYPE_FORUM_MSG */ +virtual uint32_t sizeMsg(RsForumMsg *); +virtual bool serialiseMsg(RsForumMsg *item, void *data, uint32_t *size); +virtual RsForumMsg *deserialiseMsg(void *data, uint32_t *size); + +}; + +/**************************************************************************/ + +#endif /* RS_FORUM_ITEMS_H */ + + diff --git a/libretroshare/src/serialiser/rstlvbase.h b/libretroshare/src/serialiser/rstlvbase.h index 1cd3386df..ca2501db5 100644 --- a/libretroshare/src/serialiser/rstlvbase.h +++ b/libretroshare/src/serialiser/rstlvbase.h @@ -56,6 +56,20 @@ * * *in / *out - the data to (un)pack. * + * + * BIG TLV Fields have now been added. + * If the Tlv Type & TLV_BIGLEN_BIT + * --------------------------------------------------------- + * | TLV TYPE (2 bytes)| TLV LEN (4 bytes) | + * --------------------------------------------------------- + * | | + * | Data .... | + * | | + * --------------------------------------------------------- + * + * + * + * ******************************************************************/ #include @@ -83,6 +97,17 @@ /* 0b 0001 0000 0010 XXXX PEERS */ /* 0b 0001 0000 0011 XXXX SERVICES */ +/******* BIG LEN TYPES ***************/ + +/* 0b 1000 XXXX XXXX XXXX BIG LEN */ +/* 0b 1000 0000 0001 XXXX STRINGS */ +/* 0b 1000 0000 0010 XXXX WSTRINGS */ +/* 0b 1000 0000 0011 XXXX BINARY */ + +/* 0b 1001 XXXX XXXX XXXX Compound */ + + + const uint16_t TLV_TYPE_UINT8_SERID = 0x0010; const uint16_t TLV_TYPE_UINT16_SERID = 0x0020; @@ -129,19 +154,33 @@ const uint16_t TLV_TYPE_IPV4_LOCAL = 0x0080; const uint16_t TLV_TYPE_IPV4_REMOTE = 0x0081; const uint16_t TLV_TYPE_IPV4_LAST = 0x0082; +/*** MORE STRING IDS ****/ +const uint16_t TLV_TYPE_STR_GROUPID = 0x00a0; +const uint16_t TLV_TYPE_STR_MSGID = 0x00a1; +const uint16_t TLV_TYPE_STR_PARENTID = 0x00a2; +const uint16_t TLV_TYPE_STR_THREADID = 0x00a3; +const uint16_t TLV_TYPE_STR_KEYID = 0x00a4; + +const uint16_t TLV_TYPE_WSTR_CATEGORY = 0x00b0; + /**** Binary Types ****/ const uint16_t TLV_TYPE_CERT_XPGP_DER = 0x0100; const uint16_t TLV_TYPE_CERT_X509 = 0x0101; const uint16_t TLV_TYPE_CERT_OPENPGP = 0x0102; -const uint16_t TLV_TYPE_PRIV_KEY_RSA = 0x0110; +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_PUB_KEY_RSA = 0x0120; +const uint16_t TLV_TYPE_SIGN_RSA_SHA1 = 0x0120; /* Used (Distrib/Forums) */ -const uint16_t TLV_TYPE_SIGN_RSA_SHA1 = 0x0130; +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_FILEDATA = 0x0140; /**** Compound Types ****/ const uint16_t TLV_TYPE_FILEITEM = 0x1000; @@ -152,7 +191,14 @@ const uint16_t TLV_TYPE_KEYVALUE = 0x1010; const uint16_t TLV_TYPE_KEYVALUESET = 0x1011; const uint16_t TLV_TYPE_PEERSET = 0x1020; -const uint16_t TLV_TYPE_SERVICESET = 0x1030; +const uint16_t TLV_TYPE_SERVICESET = 0x1030; + +const uint16_t TLV_TYPE_SECURITYKEY = 0x1040; +const uint16_t TLV_TYPE_SECURITYKEYSET= 0x1041; + +const uint16_t TLV_TYPE_KEYSIGNATURE = 0x1050; + +const uint16_t TLV_TYPE_IMAGE = 0x1060; /**** Basic TLV Functions ****/ diff --git a/libretroshare/src/serialiser/rstlvkeys.cc b/libretroshare/src/serialiser/rstlvkeys.cc new file mode 100644 index 000000000..d77e38779 --- /dev/null +++ b/libretroshare/src/serialiser/rstlvkeys.cc @@ -0,0 +1,408 @@ + +/* + * libretroshare/src/serialiser: rstlvtypes.cc + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Robert Fernie, Chris Parker + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include "rstlvkeys.h" + +#include "rstlvbase.h" +#include "rstlvtypes.h" +#include "rsbaseserial.h" +#include "util/rsprint.h" +#include +#include +#include +#include + + +/************************************* RsTlvSecurityKey ************************************/ + +RsTlvSecurityKey::RsTlvSecurityKey() + :RsTlvItem(), keyFlags(0), startTS(0), endTS(0), keyData(TLV_TYPE_KEY_EVP_PKEY) +{ + return; +} + +void RsTlvSecurityKey::TlvClear() +{ + keyId.clear(); + keyFlags = 0; + startTS = 0; + endTS = 0; + keyData.TlvClear(); +} + +/* clears keyData - but doesn't delete */ +void RsTlvSecurityKey::ShallowClear() +{ + keyId.clear(); + keyFlags = 0; + startTS = 0; + endTS = 0; + keyData.bin_data = 0; + keyData.bin_len = 0; +} + + +uint16_t RsTlvSecurityKey::TlvSize() +{ + uint32_t s = 4; /* header + 4 for size */ + + /* now add comment and title length of this tlv object */ + + s += GetTlvStringSize(keyId); + s += 4; + s += 4; + s += 4; + s += keyData.TlvSize(); + + return s; + +} + +bool RsTlvSecurityKey::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + /* must check sizes */ + uint16_t tlvsize = TlvSize(); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) + return false; /* not enough space */ + + bool ok = true; + + /* start at data[offset] */ + /* add mandatory parts first */ + + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEY, tlvsize); + + ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId); + ok &= setRawUInt32(data, tlvend, offset, keyFlags); + ok &= setRawUInt32(data, tlvend, offset, startTS); + ok &= setRawUInt32(data, tlvend, offset, endTS); + ok &= keyData.SetTlv(data, tlvend, offset); + + return ok; + +} + + +bool RsTlvSecurityKey::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + if (size < *offset + 4) + return false; + + uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + uint16_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) /* check size */ + return false; /* not enough space */ + + if (tlvtype != TLV_TYPE_SECURITYKEY) /* check type */ + return false; + + bool ok = true; + + /* ready to load */ + TlvClear(); + + /* skip the header */ + (*offset) += 4; + + ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId); + ok &= getRawUInt32(data, tlvend, offset, &(keyFlags)); + ok &= getRawUInt32(data, tlvend, offset, &(startTS)); + ok &= getRawUInt32(data, tlvend, offset, &(endTS)); + ok &= keyData.GetTlv(data, tlvend, offset); + + return ok; + +} + + +std::ostream &RsTlvSecurityKey::print(std::ostream &out, uint16_t indent) +{ + printBase(out, "RsTlvSecurityKey", indent); + uint16_t int_Indent = indent + 2; + + printIndent(out, int_Indent); + out << "KeyId:" << keyId; + out << std::endl; + + printIndent(out, int_Indent); + out << "KeyFlags:" << keyFlags; + out << std::endl; + + printIndent(out, int_Indent); + out << "StartTS:" << startTS; + out << std::endl; + + printIndent(out, int_Indent); + out << "EndTS:" << endTS; + out << std::endl; + + keyData.print(out, int_Indent); + + out << std::endl; + + printEnd(out, "RsTlvSecurityKey", indent); + return out; +} + + + + +/************************************* RsTlvSecurityKeySet ************************************/ + +void RsTlvSecurityKeySet::TlvClear() +{ + groupId.clear(); + keys.clear(); //empty list +} + +uint16_t RsTlvSecurityKeySet::TlvSize() +{ + + uint32_t s = 4; /* header + 4 for size */ + + std::map::iterator it; + + s += GetTlvStringSize(groupId); + + if(!keys.empty()) + { + + for(it = keys.begin(); it != keys.end() ; ++it) + s += (it->second).TlvSize(); + + } + + return s; +} + +bool RsTlvSecurityKeySet::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + /* must check sizes */ + uint16_t tlvsize = TlvSize(); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) + return false; /* not enough space */ + + bool ok = true; + + /* start at data[offset] */ + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEYSET , tlvsize); + + /* groupId */ + ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_GROUPID, groupId); + + if(!keys.empty()) + { + std::map::iterator it; + + for(it = keys.begin(); it != keys.end() ; ++it) + ok &= (it->second).SetTlv(data, size, offset); + } + + +return ok; + +} + + +bool RsTlvSecurityKeySet::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + if (size < *offset + 4) + return false; + + uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + uint16_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) /* check size */ + return false; /* not enough space */ + + if (tlvtype != TLV_TYPE_SECURITYKEYSET) /* check type */ + return false; + + bool ok = true; + + /* ready to load */ + TlvClear(); + + /* skip the header */ + (*offset) += 4; + + /* groupId */ + ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_GROUPID, groupId); + + /* while there is TLV */ + while((*offset) + 2 < tlvend) + { + /* get the next type */ + uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + + RsTlvSecurityKey key; + ok &= key.GetTlv(data, size, offset); + if (ok) + { + keys[key.keyId] = key; + key.ShallowClear(); /* so that the Map can get control - should be ref counted*/ + } + + if (!ok) + { + return false; + } + } + + return ok; +} + +// prints out contents of RsTlvSecurityKeySet +std::ostream &RsTlvSecurityKeySet::print(std::ostream &out, uint16_t indent) +{ + printBase(out, "RsTlvSecurityKeySet", indent); + uint16_t int_Indent = indent + 2; + + printIndent(out, int_Indent); + out << "GroupId: " << groupId; + out << std::endl; + + std::map::iterator it; + for(it = keys.begin(); it != keys.end() ; ++it) + (it->second).print(out, int_Indent); + + printEnd(out, "RsTlvSecurityKeySet", indent); + return out; +} + + +/************************************* RsTlvSecurityKey ************************************/ + +RsTlvKeySignature::RsTlvKeySignature() + :RsTlvItem(), signData(TLV_TYPE_SIGN_RSA_SHA1) +{ + return; +} + +void RsTlvKeySignature::TlvClear() +{ + keyId.clear(); + signData.TlvClear(); +} + +/* clears signData - but doesn't delete */ +void RsTlvKeySignature::ShallowClear() +{ + keyId.clear(); + signData.bin_data = 0; + signData.bin_len = 0; +} + +uint16_t RsTlvKeySignature::TlvSize() +{ + uint32_t s = 4; /* header + 4 for size */ + + s += GetTlvStringSize(keyId); + s += signData.TlvSize(); + + return s; + +} + +bool RsTlvKeySignature::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + /* must check sizes */ + uint16_t tlvsize = TlvSize(); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) + return false; /* not enough space */ + + bool ok = true; + + /* start at data[offset] */ + /* add mandatory parts first */ + + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEY, tlvsize); + + ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId); + ok &= signData.SetTlv(data, tlvend, offset); + + return ok; + +} + + +bool RsTlvKeySignature::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + if (size < *offset + 4) + return false; + + uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + uint16_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) /* check size */ + return false; /* not enough space */ + + if (tlvtype != TLV_TYPE_SECURITYKEY) /* check type */ + return false; + + bool ok = true; + + /* ready to load */ + TlvClear(); + + /* skip the header */ + (*offset) += 4; + + ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId); + ok &= signData.GetTlv(data, tlvend, offset); + + return ok; + +} + + +std::ostream &RsTlvKeySignature::print(std::ostream &out, uint16_t indent) +{ + printBase(out, "RsTlvKeySignature", indent); + uint16_t int_Indent = indent + 2; + + printIndent(out, int_Indent); + out << "KeyId:" << keyId; + out << std::endl; + + signData.print(out, int_Indent); + + out << std::endl; + + printEnd(out, "RsTlvKeySignature", indent); + return out; +} + diff --git a/libretroshare/src/serialiser/rstlvkeys.h b/libretroshare/src/serialiser/rstlvkeys.h new file mode 100644 index 000000000..327b7cf83 --- /dev/null +++ b/libretroshare/src/serialiser/rstlvkeys.h @@ -0,0 +1,103 @@ +#ifndef RS_TLV_KEY_TYPES_H +#define RS_TLV_KEY_TYPES_H + +/* + * libretroshare/src/serialiser: rstlvkeys.h + * + * RetroShare Serialiser. + * + * Copyright 2008 by Robert Fernie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +/******************************************************************* + * These are the Compound TLV structures that must be (un)packed. + ******************************************************************/ + +#include +#include "serialiser/rstlvtypes.h" + +const uint32_t RSTLV_KEY_TYPE_MASK = 0x000f; +const uint32_t RSTLV_KEY_DISTRIB_MASK = 0x00f0; + +const uint32_t RSTLV_KEY_TYPE_PUBLIC_ONLY = 0x0001; +const uint32_t RSTLV_KEY_TYPE_FULL = 0x0002; +const uint32_t RSTLV_KEY_DISTRIB_PUBLIC = 0x0010; +const uint32_t RSTLV_KEY_DISTRIB_PRIVATE = 0x0020; +const uint32_t RSTLV_KEY_DISTRIB_ADMIN = 0x0040; + + +class RsTlvSecurityKey: public RsTlvItem +{ + public: + RsTlvSecurityKey(); +virtual ~RsTlvSecurityKey() { return; } +virtual uint16_t TlvSize(); +virtual void TlvClear(); +virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ +virtual std::ostream &print(std::ostream &out, uint16_t indent); + + /* clears KeyData - but doesn't delete - to transfer ownership */ + void ShallowClear(); + + std::string keyId; // Mandatory : + uint32_t keyFlags; // Mandatory ; + uint32_t startTS; // Mandatory : + uint32_t endTS; // Mandatory : + RsTlvBinaryData keyData; // Mandatory : +}; + +class RsTlvSecurityKeySet: public RsTlvItem +{ + public: + RsTlvSecurityKeySet() { return; } +virtual ~RsTlvSecurityKeySet() { return; } +virtual uint16_t TlvSize(); +virtual void TlvClear(); +virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ +virtual std::ostream &print(std::ostream &out, uint16_t indent); + + std::string groupId; // Mandatory : + std::map keys; // Mandatory : +}; + + +class RsTlvKeySignature: public RsTlvItem +{ + public: + RsTlvKeySignature(); +virtual ~RsTlvKeySignature() { return; } +virtual uint16_t TlvSize(); +virtual void TlvClear(); +virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ +virtual std::ostream &print(std::ostream &out, uint16_t indent); + + void ShallowClear(); /* clears signData - but doesn't delete */ + + std::string keyId; // Mandatory : + RsTlvBinaryData signData; // Mandatory : +}; + + + +#endif + diff --git a/libretroshare/src/serialiser/rstlvtypes.h b/libretroshare/src/serialiser/rstlvtypes.h index 8949c6d80..a7755afe9 100644 --- a/libretroshare/src/serialiser/rstlvtypes.h +++ b/libretroshare/src/serialiser/rstlvtypes.h @@ -209,6 +209,21 @@ virtual std::ostream &print(std::ostream &out, uint16_t indent); }; +class RsTlvImage: public RsTlvItem +{ + public: + RsTlvImage(); + virtual ~RsTlvImage() { return; } +virtual uint16_t TlvSize(); +virtual void TlvClear(); +virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ +virtual std::ostream &print(std::ostream &out, uint16_t indent); + + uint32_t image_type; // Mandatory: + RsTlvBinaryData binData; // Mandatory: serialised file info +}; + #endif