2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/gxs: rsgenexchange.cc
|
|
|
|
*
|
|
|
|
* RetroShare Gxs exchange interface.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 by Christopher Evi-Parker, 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-08-12 16:46:21 -04:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/evp.h>
|
2012-08-19 18:15:37 -04:00
|
|
|
#include <openssl/rsa.h>
|
2012-08-12 16:46:21 -04:00
|
|
|
|
2012-10-01 16:57:56 -04:00
|
|
|
#include "pqi/pqihash.h"
|
2012-05-21 18:07:43 -04:00
|
|
|
#include "rsgenexchange.h"
|
2012-08-12 16:46:21 -04:00
|
|
|
#include "gxssecurity.h"
|
2012-08-21 17:32:07 -04:00
|
|
|
#include "util/contentvalue.h"
|
2012-09-04 18:32:52 -04:00
|
|
|
#include "rsgxsflags.h"
|
2012-11-10 18:42:38 -05:00
|
|
|
#include "rsgixs.h"
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
|
|
|
|
#define PUB_GRP_MASK 0x000f
|
|
|
|
#define RESTR_GRP_MASK 0x00f0
|
|
|
|
#define PRIV_GRP_MASK 0x0f00
|
|
|
|
#define GRP_OPTIONS_MASK 0xf000
|
|
|
|
|
|
|
|
#define PUB_GRP_OFFSET 0
|
|
|
|
#define RESTR_GRP_OFFSET 8
|
|
|
|
#define PRIV_GRP_OFFSET 16
|
|
|
|
#define GRP_OPTIONS_OFFSET 24
|
|
|
|
|
|
|
|
RsGenExchange::RsGenExchange(RsGeneralDataService *gds, RsNetworkExchangeService *ns,
|
|
|
|
RsSerialType *serviceSerialiser, uint16_t servType, RsGixs* gixs, uint32_t authenPolicy)
|
|
|
|
: mGenMtx("GenExchange"), mDataStore(gds), mNetService(ns), mSerialiser(serviceSerialiser),
|
|
|
|
mServType(servType), mGixs(gixs), mAuthenPolicy(authenPolicy)
|
2012-05-21 18:07:43 -04:00
|
|
|
{
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-07-18 17:35:41 -04:00
|
|
|
mDataAccess = new RsGxsDataAccess(gds);
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGenExchange::~RsGenExchange()
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
// need to destruct in a certain order (prob a bad thing!)
|
2012-07-05 17:26:14 -04:00
|
|
|
delete mNetService;
|
|
|
|
|
|
|
|
delete mDataAccess;
|
|
|
|
mDataAccess = NULL;
|
|
|
|
|
|
|
|
delete mDataStore;
|
|
|
|
mDataStore = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
void RsGenExchange::run()
|
|
|
|
{
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
double timeDelta = 0.1; // slow tick
|
2012-11-03 21:38:48 -04:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
tick();
|
|
|
|
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
usleep((int) (timeDelta * 1000000));
|
|
|
|
#else
|
|
|
|
Sleep((int) (timeDelta * 1000));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
void RsGenExchange::tick()
|
|
|
|
{
|
2012-07-18 17:35:41 -04:00
|
|
|
mDataAccess->processRequests();
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
publishGrps();
|
|
|
|
|
|
|
|
publishMsgs();
|
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
processGrpMetaChanges();
|
|
|
|
|
|
|
|
processMsgMetaChanges();
|
|
|
|
|
2012-09-25 17:04:04 -04:00
|
|
|
processRecvdData();
|
|
|
|
|
|
|
|
if(!mNotifications.empty())
|
|
|
|
{
|
|
|
|
notifyChanges(mNotifications);
|
|
|
|
mNotifications.clear();
|
|
|
|
}
|
2012-08-06 17:00:38 -04:00
|
|
|
|
2012-10-15 16:31:56 -04:00
|
|
|
// implemented service tick function
|
|
|
|
service_tick();
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
2012-08-18 06:01:35 -04:00
|
|
|
bool RsGenExchange::acknowledgeTokenMsg(const uint32_t& token,
|
2012-08-21 17:32:07 -04:00
|
|
|
RsGxsGrpMsgIdPair& msgId)
|
2012-08-18 06:01:35 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
std::map<uint32_t, RsGxsGrpMsgIdPair >::iterator mit =
|
|
|
|
mMsgNotify.find(token);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
if(mit == mMsgNotify.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-18 06:01:35 -04:00
|
|
|
|
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
msgId = mit->second;
|
|
|
|
|
|
|
|
// no dump token as client has ackowledged its completion
|
|
|
|
mDataAccess->disposeOfPublicToken(token);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool RsGenExchange::acknowledgeTokenGrp(const uint32_t& token,
|
|
|
|
RsGxsGroupId& grpId)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::map<uint32_t, RsGxsGroupId >::iterator mit =
|
2012-08-21 17:32:07 -04:00
|
|
|
mGrpNotify.find(token);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
if(mit == mGrpNotify.end())
|
2012-08-18 06:01:35 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
grpId = mit->second;
|
|
|
|
|
|
|
|
// no dump token as client has ackowledged its completion
|
|
|
|
mDataAccess->disposeOfPublicToken(token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
void RsGenExchange::generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPublishKeys)
|
2012-08-12 16:46:21 -04:00
|
|
|
{
|
|
|
|
/* create Keys */
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
// admin keys
|
2012-08-12 16:46:21 -04:00
|
|
|
RSA *rsa_admin = RSA_generate_key(2048, 65537, NULL, NULL);
|
|
|
|
RSA *rsa_admin_pub = RSAPublicKey_dup(rsa_admin);
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
/* set admin keys */
|
2012-08-18 06:01:35 -04:00
|
|
|
RsTlvSecurityKey adminKey, privAdminKey;
|
|
|
|
|
2012-08-12 16:46:21 -04:00
|
|
|
GxsSecurity::setRSAPublicKey(adminKey, rsa_admin_pub);
|
2012-08-18 06:01:35 -04:00
|
|
|
GxsSecurity::setRSAPrivateKey(privAdminKey, rsa_admin);
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
adminKey.startTS = time(NULL);
|
|
|
|
adminKey.endTS = 0; /* no end */
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
privAdminKey.startTS = adminKey.startTS;
|
2012-11-03 21:38:48 -04:00
|
|
|
privAdminKey.endTS = 0; /* no end */
|
|
|
|
|
2012-08-18 06:01:35 -04:00
|
|
|
// for now all public
|
|
|
|
adminKey.keyFlags = RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_PUBLIC_ONLY;
|
|
|
|
privAdminKey.keyFlags = RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL;
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
keySet.keys[adminKey.keyId] = adminKey;
|
|
|
|
keySet.keys[privAdminKey.keyId] = privAdminKey;
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
RSA_free(rsa_admin);
|
|
|
|
RSA_free(rsa_admin_pub);
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
if(genPublishKeys)
|
|
|
|
{
|
|
|
|
// publish keys
|
|
|
|
RSA *rsa_publish = RSA_generate_key(2048, 65537, NULL, NULL);
|
|
|
|
RSA *rsa_publish_pub = RSAPublicKey_dup(rsa_publish);
|
|
|
|
|
|
|
|
/* set publish keys */
|
|
|
|
RsTlvSecurityKey pubKey, privPubKey;
|
|
|
|
|
|
|
|
GxsSecurity::setRSAPublicKey(pubKey, rsa_publish_pub);
|
|
|
|
GxsSecurity::setRSAPrivateKey(privPubKey, rsa_publish);
|
|
|
|
|
|
|
|
pubKey.startTS = adminKey.startTS;
|
|
|
|
pubKey.endTS = pubKey.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */
|
|
|
|
|
|
|
|
privPubKey.startTS = adminKey.startTS;
|
|
|
|
privPubKey.endTS = 0; /* no end */
|
|
|
|
|
|
|
|
// for now all public
|
|
|
|
pubKey.keyFlags = RSTLV_KEY_DISTRIB_PUBLIC | RSTLV_KEY_TYPE_PUBLIC_ONLY;
|
|
|
|
privPubKey.keyFlags = RSTLV_KEY_DISTRIB_PRIVATE | RSTLV_KEY_TYPE_FULL;
|
|
|
|
|
|
|
|
keySet.keys[pubKey.keyId] = pubKey;
|
|
|
|
keySet.keys[privPubKey.keyId] = privPubKey;
|
|
|
|
|
|
|
|
RSA_free(rsa_publish);
|
|
|
|
RSA_free(rsa_publish_pub);
|
|
|
|
}
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& keySet)
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
std::cerr << "RsGenExchange::createGroup()";
|
|
|
|
std::cerr << std::endl;
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-08-12 16:46:21 -04:00
|
|
|
RsGxsGrpMetaData* meta = grp->metaData;
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
/* add public admin and publish keys to grp */
|
2012-11-03 21:38:48 -04:00
|
|
|
|
|
|
|
// find private admin key
|
|
|
|
RsTlvSecurityKey privAdminKey;
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator mit = keySet.keys.begin();
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
bool privKeyFound = false;
|
2012-11-03 21:38:48 -04:00
|
|
|
for(; mit != keySet.keys.end(); mit++)
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
RsTlvSecurityKey& key = mit->second;
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// add public admin key
|
|
|
|
if(key.keyFlags & (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_PUBLIC_ONLY))
|
|
|
|
meta->keys.keys.insert(std::make_pair(key.keyId, key));
|
|
|
|
|
|
|
|
// add public publish key
|
|
|
|
if(key.keyFlags & (RSTLV_KEY_DISTRIB_PUBLIC | RSTLV_KEY_TYPE_PUBLIC_ONLY))
|
|
|
|
meta->keys.keys.insert(std::make_pair(key.keyId, key));
|
|
|
|
|
|
|
|
if(key.keyFlags & (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
|
2012-11-03 21:38:48 -04:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
privAdminKey = key;
|
|
|
|
privKeyFound = true;
|
2012-11-03 21:38:48 -04:00
|
|
|
}
|
|
|
|
}
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
if(!privKeyFound)
|
2012-11-08 19:56:07 -05:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
std::cerr << "RsGenExchange::createGroup() Missing private ADMIN Key";
|
2012-11-08 19:56:07 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
return false;
|
2012-11-08 19:56:07 -05:00
|
|
|
}
|
2012-11-02 19:35:10 -04:00
|
|
|
|
|
|
|
// group is self signing
|
|
|
|
// for the creation of group signature
|
2012-11-11 18:45:22 -05:00
|
|
|
// only public admin and publish keys are present in meta
|
2012-11-02 19:35:10 -04:00
|
|
|
uint32_t metaDataLen = meta->serial_size();
|
2012-11-03 21:38:48 -04:00
|
|
|
uint32_t allGrpDataLen = metaDataLen + grp->grp.bin_len;
|
|
|
|
char* metaData = new char[metaDataLen];
|
|
|
|
char* allGrpData = new char[allGrpDataLen]; // msgData + metaData
|
2012-11-02 19:35:10 -04:00
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
meta->serialise(metaData, metaDataLen);
|
2012-11-02 19:35:10 -04:00
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
// copy msg data and meta in allMsgData buffer
|
|
|
|
memcpy(allGrpData, grp->grp.bin_data, grp->grp.bin_len);
|
|
|
|
memcpy(allGrpData+(grp->grp.bin_len), metaData, metaDataLen);
|
2012-11-02 19:35:10 -04:00
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
RsTlvKeySignature adminSign;
|
|
|
|
bool ok = GxsSecurity::getSignature(allGrpData, allGrpDataLen, &privAdminKey, adminSign);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-02 19:35:10 -04:00
|
|
|
// add admin sign to grpMeta
|
|
|
|
meta->signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_ADMIN] = adminSign;
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// set meta to be transported as meta without private
|
|
|
|
// key components
|
|
|
|
grp->meta.setBinData(metaData, metaDataLen);
|
|
|
|
|
|
|
|
// but meta that is stored locally
|
|
|
|
// has all keys
|
|
|
|
// nxs net transports only bin data
|
|
|
|
meta->keys = keySet;
|
2012-08-12 16:46:21 -04:00
|
|
|
|
2012-11-02 19:35:10 -04:00
|
|
|
// clean up
|
|
|
|
delete[] allGrpData;
|
|
|
|
delete[] metaData;
|
|
|
|
|
2012-11-08 19:56:07 -05:00
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGenExchange::createGroup() ERROR !okay (getSignature error)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:35:10 -04:00
|
|
|
return ok;
|
2012-08-18 06:01:35 -04:00
|
|
|
}
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
bool RsGenExchange::createMsgSignatures(RsTlvKeySignatureSet& signSet, RsTlvBinaryData& msgData,
|
|
|
|
const RsGxsMsgMetaData& msgMeta, RsGxsGrpMetaData& grpMeta)
|
|
|
|
{
|
|
|
|
bool isParent = false;
|
|
|
|
bool needPublishSign, needIdentitySign;
|
|
|
|
bool ok = true;
|
|
|
|
uint32_t grpFlag = grpMeta.mGroupFlags;
|
|
|
|
|
|
|
|
// publish signature is determined by whether group is public or not
|
|
|
|
// for private group signature is not needed as it needs decrypting with
|
|
|
|
// the private publish key anyways
|
|
|
|
|
|
|
|
// restricted is a special case which heeds whether publish sign needs to be checked or not
|
|
|
|
// one may or may not want
|
|
|
|
|
|
|
|
if(msgMeta.mParentId.empty())
|
|
|
|
{
|
|
|
|
isParent = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isParent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isParent)
|
|
|
|
{
|
|
|
|
needIdentitySign = false;
|
|
|
|
needPublishSign = false;
|
|
|
|
|
|
|
|
if(grpFlag & GXS_SERV::FLAG_PRIVACY_PUBLIC)
|
|
|
|
{
|
|
|
|
needPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PUBLIC_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_RESTRICTED)
|
|
|
|
{
|
|
|
|
needPublishSign = true;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_PRIVATE)
|
|
|
|
{
|
|
|
|
needPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PRIVATE_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if(grpFlag & GXS_SERV::FLAG_PRIVACY_PUBLIC)
|
|
|
|
{
|
|
|
|
needPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PUBLIC_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_RESTRICTED)
|
|
|
|
{
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_PUBLISH_SIGN))
|
|
|
|
needPublishSign = true;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_PRIVATE)
|
|
|
|
{
|
|
|
|
needPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PRIVATE_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
needIdentitySign = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(needPublishSign)
|
|
|
|
{
|
|
|
|
|
|
|
|
// public and shared is publish key
|
|
|
|
RsTlvSecurityKeySet& keys = grpMeta.keys;
|
|
|
|
RsTlvSecurityKey* pubKey;
|
|
|
|
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator mit =
|
|
|
|
keys.keys.begin(), mit_end = keys.keys.end();
|
|
|
|
bool pub_key_found = false;
|
|
|
|
for(; mit != mit_end; mit++)
|
|
|
|
{
|
|
|
|
|
|
|
|
pub_key_found = mit->second.keyFlags & (RSTLV_KEY_DISTRIB_PRIVATE | RSTLV_KEY_TYPE_FULL);
|
|
|
|
if(pub_key_found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// private publish key
|
|
|
|
pubKey = &(mit->second);
|
|
|
|
|
|
|
|
RsTlvKeySignature pubSign = signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_PUBLISH];
|
|
|
|
|
|
|
|
ok &= GxsSecurity::getSignature((char*)msgData.bin_data, msgData.bin_len, pubKey, pubSign);
|
|
|
|
|
|
|
|
//place signature in msg meta
|
|
|
|
signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_PUBLISH] = pubSign;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(needIdentitySign)
|
|
|
|
{
|
|
|
|
if(mGixs)
|
|
|
|
{
|
|
|
|
bool haveKey = mGixs->havePrivateKey(msgMeta.mAuthorId);
|
|
|
|
|
|
|
|
if(haveKey)
|
|
|
|
{
|
|
|
|
mGixs->requestPrivateKey(msgMeta.mAuthorId);
|
|
|
|
|
|
|
|
RsTlvSecurityKey authorKey;
|
|
|
|
|
|
|
|
double timeDelta = 0.002; // fast polling
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
// poll immediately but, don't spend more than a second polling
|
|
|
|
while( (mGixs->getPrivateKey(msgMeta.mAuthorId, authorKey) == -1) &&
|
|
|
|
((now + 1) >> time(NULL))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
usleep((int) (timeDelta * 1000000));
|
|
|
|
#else
|
|
|
|
Sleep((int) (timeDelta * 1000));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RsTlvKeySignature sign;
|
|
|
|
ok &= GxsSecurity::getSignature((char*)msgData.bin_data, msgData.bin_len,
|
|
|
|
&authorKey, sign);
|
|
|
|
signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_IDENTITY] = sign;
|
|
|
|
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef GEN_EXHANGE_DEBUG
|
|
|
|
std::cerr << "Gixs not enabled while request identity signature validation!" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-18 06:01:35 -04:00
|
|
|
bool RsGenExchange::createMessage(RsNxsMsg* msg)
|
|
|
|
{
|
|
|
|
const RsGxsGroupId& id = msg->grpId;
|
|
|
|
|
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*> metaMap;
|
|
|
|
|
|
|
|
metaMap.insert(std::make_pair(id, (RsGxsGrpMetaData*)(NULL)));
|
|
|
|
mDataStore->retrieveGxsGrpMetaData(metaMap);
|
2012-11-11 18:45:22 -05:00
|
|
|
bool ok = true;
|
|
|
|
RsGxsMsgMetaData &meta = *(msg->metaData);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
|
|
|
if(!metaMap[id])
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
return false;
|
2012-08-18 06:01:35 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
// get publish key
|
|
|
|
RsGxsGrpMetaData* grpMeta = metaMap[id];
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
uint32_t metaDataLen = meta.serial_size();
|
|
|
|
uint32_t allMsgDataLen = metaDataLen + msg->msg.bin_len;
|
|
|
|
char* metaData = new char[metaDataLen];
|
|
|
|
char* allMsgData = new char[allMsgDataLen]; // msgData + metaData
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
meta.serialise(metaData, &metaDataLen);
|
2012-10-23 17:52:51 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// copy msg data and meta in allmsgData buffer
|
|
|
|
memcpy(allMsgData, msg->msg.bin_data, msg->msg.bin_len);
|
|
|
|
memcpy(allMsgData+(msg->msg.bin_len), metaData, metaDataLen);
|
2012-10-23 17:52:51 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsTlvBinaryData msgData(0);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
msgData.setBinData(allMsgData, allMsgDataLen);
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// create signatures
|
|
|
|
ok &= createMsgSignatures(meta.signSet, msgData, meta, *grpMeta);
|
2012-10-23 17:52:51 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// get hash of msg data to create msg id
|
|
|
|
pqihash hash;
|
|
|
|
hash.addData(allMsgData, allMsgDataLen);
|
|
|
|
hash.Complete(msg->msgId);
|
2012-10-01 16:57:56 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// assign msg id to msg meta
|
|
|
|
msg->metaData->mMsgId = msg->msgId;
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
delete[] metaData;
|
|
|
|
delete[] allMsgData;
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
delete grpMeta;
|
2012-08-18 06:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2012-08-12 16:46:21 -04:00
|
|
|
}
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
bool RsGenExchange::validateMsg(RsNxsMsg *msg, const uint32_t& grpFlag, RsTlvSecurityKeySet& grpKeySet)
|
|
|
|
{
|
|
|
|
bool isParent = false;
|
|
|
|
bool checkPublishSign, checkIdentitySign;
|
|
|
|
bool valid = true;
|
|
|
|
|
|
|
|
// publish signature is determined by whether group is public or not
|
|
|
|
// for private group signature is not needed as it needs decrypting with
|
|
|
|
// the private publish key anyways
|
|
|
|
|
|
|
|
// restricted is a special case which heeds whether publish sign needs to be checked or not
|
|
|
|
// one may or may not want
|
|
|
|
|
|
|
|
if(msg->metaData->mParentId.empty())
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
isParent = true;
|
2012-11-10 18:42:38 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
isParent = false;
|
2012-11-10 18:42:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isParent)
|
|
|
|
{
|
|
|
|
checkIdentitySign = false;
|
|
|
|
checkPublishSign = false;
|
|
|
|
|
|
|
|
if(grpFlag & GXS_SERV::FLAG_PRIVACY_PUBLIC)
|
|
|
|
{
|
|
|
|
checkPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PUBLIC_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_RESTRICTED)
|
|
|
|
{
|
|
|
|
checkPublishSign = true;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_PRIVATE)
|
|
|
|
{
|
|
|
|
checkPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PRIVATE_GRP_BITS, GXS_SERV::MSG_AUTHEN_ROOT_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if(grpFlag & GXS_SERV::FLAG_PRIVACY_PUBLIC)
|
|
|
|
{
|
|
|
|
checkPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PUBLIC_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_RESTRICTED)
|
|
|
|
{
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_PUBLISH_SIGN))
|
|
|
|
checkPublishSign = true;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(RESTRICTED_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
}
|
|
|
|
else if(grpFlag & GXS_SERV::FLAG_PRIVACY_PRIVATE)
|
|
|
|
{
|
|
|
|
checkPublishSign = false;
|
|
|
|
|
|
|
|
if(checkMsgAuthenFlag(PRIVATE_GRP_BITS, GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN))
|
|
|
|
checkIdentitySign = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGxsMsgMetaData& metaData = *(msg->metaData);
|
|
|
|
|
|
|
|
if(checkPublishSign)
|
|
|
|
{
|
|
|
|
RsTlvKeySignature sign = metaData.signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_PUBLISH];
|
|
|
|
|
|
|
|
if(grpKeySet.keys.find(sign.keyId) != grpKeySet.keys.end())
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey publishKey = grpKeySet.keys[sign.keyId];
|
|
|
|
valid &= GxsSecurity::validateNxsMsg(*msg, sign, publishKey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
if(checkIdentitySign)
|
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
if(mGixs)
|
2012-11-10 18:42:38 -05:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
bool haveKey = mGixs->haveKey(metaData.mAuthorId);
|
|
|
|
|
|
|
|
if(haveKey)
|
|
|
|
{
|
|
|
|
std::list<std::string> peers;
|
|
|
|
mGixs->requestKey(metaData.mAuthorId, peers);
|
2012-11-10 18:42:38 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsTlvSecurityKey authorKey;
|
2012-11-10 18:42:38 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
double timeDelta = 0.002; // fast polling
|
|
|
|
time_t now = time(NULL);
|
|
|
|
// poll immediately but, don't spend more than a second polling
|
|
|
|
while( (mGixs->getKey(metaData.mAuthorId, authorKey) == -1) &&
|
|
|
|
((now + 1) >> time(NULL))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
usleep((int) (timeDelta * 1000000));
|
|
|
|
#else
|
|
|
|
Sleep((int) (timeDelta * 1000));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RsTlvKeySignature sign = metaData.signSet.keySignSet[GXS_SERV::FLAG_AUTHEN_PUBLISH];
|
|
|
|
valid &= GxsSecurity::validateNxsMsg(*msg, sign, authorKey);
|
|
|
|
}else
|
2012-11-10 18:42:38 -05:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
valid = false;
|
2012-11-10 18:42:38 -05:00
|
|
|
}
|
2012-11-11 18:45:22 -05:00
|
|
|
}
|
|
|
|
else
|
2012-11-10 18:42:38 -05:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
#ifdef GEN_EXHANGE_DEBUG
|
|
|
|
std::cerr << "Gixs not enabled while request identity signature validation!" << std::endl;
|
|
|
|
#endif
|
2012-11-10 18:42:38 -05:00
|
|
|
valid = false;
|
|
|
|
}
|
2012-11-11 18:45:22 -05:00
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::checkMsgAuthenFlag(const PrivacyBitPos& pos, const uint8_t& flag) const
|
|
|
|
{
|
|
|
|
switch(pos)
|
|
|
|
{
|
|
|
|
case PUBLIC_GRP_BITS:
|
|
|
|
return mAuthenPolicy & flag;
|
|
|
|
break;
|
|
|
|
case RESTRICTED_GRP_BITS:
|
|
|
|
return flag & (mAuthenPolicy >> RESTR_GRP_OFFSET);
|
|
|
|
break;
|
|
|
|
case PRIVATE_GRP_BITS:
|
|
|
|
return flag & (mAuthenPolicy >> PRIV_GRP_OFFSET);
|
|
|
|
break;
|
|
|
|
case GRP_OPTION_BITS:
|
|
|
|
return flag & (mAuthenPolicy >> GRP_OPTIONS_OFFSET);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cerr << "pos option not recognised";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
bool RsGenExchange::getGroupList(const uint32_t &token, std::list<RsGxsGroupId> &groupIds)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
return mDataAccess->getGroupList(token, groupIds);
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getMsgList(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgIdResult &msgIds)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
return mDataAccess->getMsgList(token, msgIds);
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getGroupMeta(const uint32_t &token, std::list<RsGroupMetaData> &groupInfo)
|
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
std::list<RsGxsGrpMetaData*> metaL;
|
|
|
|
bool ok = mDataAccess->getGroupSummary(token, metaL);
|
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
std::list<RsGxsGrpMetaData*>::iterator lit = metaL.begin();
|
2012-07-28 16:01:30 -04:00
|
|
|
RsGroupMetaData m;
|
2012-07-26 17:48:54 -04:00
|
|
|
for(; lit != metaL.end(); lit++)
|
|
|
|
{
|
2012-07-28 16:01:30 -04:00
|
|
|
RsGxsGrpMetaData& gMeta = *(*lit);
|
|
|
|
m = gMeta;
|
2012-07-26 17:48:54 -04:00
|
|
|
groupInfo.push_back(m);
|
2012-07-28 16:01:30 -04:00
|
|
|
delete (*lit);
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
bool RsGenExchange::getMsgMeta(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgMetaMap &msgInfo)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
std::list<RsGxsMsgMetaData*> metaL;
|
|
|
|
GxsMsgMetaResult result;
|
|
|
|
bool ok = mDataAccess->getMsgSummary(token, result);
|
|
|
|
|
|
|
|
GxsMsgMetaResult::iterator mit = result.begin();
|
|
|
|
|
|
|
|
for(; mit != result.end(); mit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgMetaData*>& metaV = mit->second;
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
msgInfo[mit->first] = std::vector<RsMsgMetaData>();
|
|
|
|
std::vector<RsMsgMetaData>& msgInfoV = msgInfo[mit->first];
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
std::vector<RsGxsMsgMetaData*>::iterator vit = metaV.begin();
|
|
|
|
RsMsgMetaData meta;
|
2012-07-22 17:38:15 -04:00
|
|
|
for(; vit != metaV.end(); vit++)
|
|
|
|
{
|
2012-08-06 17:00:38 -04:00
|
|
|
RsGxsMsgMetaData& m = *(*vit);
|
|
|
|
meta = m;
|
|
|
|
msgInfoV.push_back(meta);
|
2012-07-22 17:38:15 -04:00
|
|
|
delete *vit;
|
|
|
|
}
|
2012-08-06 17:00:38 -04:00
|
|
|
metaV.clear();
|
2012-07-22 17:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
2012-08-12 16:46:21 -04:00
|
|
|
bool RsGenExchange::getGroupData(const uint32_t &token, std::vector<RsGxsGrpItem *>& grpItem)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
|
|
|
|
std::list<RsNxsGrp*> nxsGrps;
|
|
|
|
bool ok = mDataAccess->getGroupData(token, nxsGrps);
|
|
|
|
|
|
|
|
std::list<RsNxsGrp*>::iterator lit = nxsGrps.begin();
|
|
|
|
|
2012-11-08 19:56:07 -05:00
|
|
|
std::cerr << "RsGenExchange::getGroupData() RsNxsGrp::len: " << nxsGrps.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
if(ok)
|
2012-07-22 17:38:15 -04:00
|
|
|
{
|
|
|
|
for(; lit != nxsGrps.end(); lit++)
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
RsTlvBinaryData& data = (*lit)->grp;
|
2012-07-22 17:38:15 -04:00
|
|
|
RsItem* item = mSerialiser->deserialise(data.bin_data, &data.bin_len);
|
2012-08-06 17:00:38 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
if(item != NULL)
|
|
|
|
{
|
2012-08-06 17:00:38 -04:00
|
|
|
RsGxsGrpItem* gItem = dynamic_cast<RsGxsGrpItem*>(item);
|
|
|
|
gItem->meta = *((*lit)->metaData);
|
|
|
|
grpItem.push_back(gItem);
|
|
|
|
delete *lit;
|
|
|
|
}
|
2012-11-08 19:56:07 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "RsGenExchange::getGroupData() ERROR deserialising item";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2012-07-22 17:38:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getMsgData(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgDataMap &msgItems)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
NxsMsgDataResult msgResult;
|
|
|
|
bool ok = mDataAccess->getMsgData(token, msgResult);
|
|
|
|
NxsMsgDataResult::iterator mit = msgResult.begin();
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
for(; mit != msgResult.end(); mit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgItem*> gxsMsgItems;
|
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
|
|
|
std::vector<RsNxsMsg*>& nxsMsgsV = mit->second;
|
|
|
|
std::vector<RsNxsMsg*>::iterator vit
|
|
|
|
= nxsMsgsV.begin();
|
|
|
|
for(; vit != nxsMsgsV.end(); vit++)
|
|
|
|
{
|
|
|
|
RsNxsMsg*& msg = *vit;
|
|
|
|
|
|
|
|
RsItem* item = mSerialiser->deserialise(msg->msg.bin_data,
|
|
|
|
&msg->msg.bin_len);
|
|
|
|
RsGxsMsgItem* mItem = dynamic_cast<RsGxsMsgItem*>(item);
|
|
|
|
mItem->meta = *((*vit)->metaData); // get meta info from nxs msg
|
|
|
|
gxsMsgItems.push_back(mItem);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
msgItems[grpId] = gxsMsgItems;
|
|
|
|
}
|
|
|
|
}
|
2012-07-22 17:38:15 -04:00
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-05 14:12:52 -04:00
|
|
|
RsTokenService* RsGenExchange::getTokenService()
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
|
|
|
return mDataAccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
bool RsGenExchange::setAuthenPolicyFlag(const uint8_t &msgFlag, uint32_t& authenFlag, const PrivacyBitPos &pos)
|
|
|
|
{
|
|
|
|
uint32_t temp = 0;
|
|
|
|
temp = msgFlag;
|
|
|
|
|
|
|
|
switch(pos)
|
|
|
|
{
|
|
|
|
case PUBLIC_GRP_BITS:
|
|
|
|
authenFlag &= ~PUB_GRP_MASK;
|
|
|
|
authenFlag |= temp;
|
|
|
|
break;
|
|
|
|
case RESTRICTED_GRP_BITS:
|
|
|
|
authenFlag &= ~RESTR_GRP_MASK;
|
|
|
|
authenFlag |= (temp << RESTR_GRP_OFFSET);
|
|
|
|
break;
|
|
|
|
case PRIVATE_GRP_BITS:
|
|
|
|
authenFlag &= ~PRIV_GRP_MASK;
|
|
|
|
authenFlag |= (temp << PRIV_GRP_OFFSET);
|
|
|
|
break;
|
|
|
|
case GRP_OPTION_BITS:
|
|
|
|
authenFlag &= ~GRP_OPTIONS_MASK;
|
|
|
|
authenFlag |= (temp << GRP_OPTIONS_OFFSET);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cerr << "pos option not recognised";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
void RsGenExchange::notifyNewGroups(std::vector<RsNxsGrp *> &groups)
|
|
|
|
{
|
|
|
|
std::vector<RsNxsGrp*>::iterator vit = groups.begin();
|
|
|
|
|
|
|
|
// store these for tick() to pick them up
|
|
|
|
for(; vit != groups.end(); vit++)
|
|
|
|
mReceivedGrps.push_back(*vit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::notifyNewMessages(std::vector<RsNxsMsg *>& messages)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
|
|
|
std::vector<RsNxsMsg*>::iterator vit = messages.begin();
|
|
|
|
|
|
|
|
// store these for tick() to pick them up
|
|
|
|
for(; vit != messages.end(); vit++)
|
|
|
|
mReceivedMsgs.push_back(*vit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
void RsGenExchange::publishGroup(uint32_t& token, RsGxsGrpItem *grpItem)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
|
|
|
|
2012-08-18 06:01:35 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
token = mDataAccess->generatePublicToken();
|
|
|
|
mGrpsToPublish.insert(std::make_pair(token, grpItem));
|
2012-08-21 17:32:07 -04:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
void RsGenExchange::publishMsg(uint32_t& token, RsGxsMsgItem *msgItem)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
token = mDataAccess->generatePublicToken();
|
|
|
|
mMsgsToPublish.insert(std::make_pair(token, msgItem));
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
void RsGenExchange::setGroupSubscribeFlags(uint32_t& token, const RsGxsGroupId& grpId, const uint32_t& flag, const uint32_t& mask)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
/* TODO APPLY MASK TO FLAGS */
|
2012-08-21 17:32:07 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
token = mDataAccess->generatePublicToken();
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
GrpLocMetaData g;
|
|
|
|
g.grpId = grpId;
|
|
|
|
g.val.put(RsGeneralDataService::GRP_META_SUBSCRIBE_FLAG, (int32_t)flag);
|
|
|
|
mGrpLocMetaMap.insert(std::make_pair(token, g));
|
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
void RsGenExchange::setGroupStatusFlags(uint32_t& token, const RsGxsGroupId& grpId, const uint32_t& status, const uint32_t& mask)
|
2012-08-21 17:32:07 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
/* TODO APPLY MASK TO FLAGS */
|
2012-08-21 17:32:07 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
token = mDataAccess->generatePublicToken();
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
GrpLocMetaData g;
|
|
|
|
g.grpId = grpId;
|
|
|
|
g.val.put(RsGeneralDataService::GRP_META_STATUS, (int32_t)status);
|
|
|
|
mGrpLocMetaMap.insert(std::make_pair(token, g));
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
void RsGenExchange::setGroupServiceString(uint32_t& token, const RsGxsGroupId& grpId, const std::string& servString)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
token = mDataAccess->generatePublicToken();
|
|
|
|
|
|
|
|
GrpLocMetaData g;
|
|
|
|
g.grpId = grpId;
|
|
|
|
g.val.put(RsGeneralDataService::GRP_META_SERV_STRING, servString);
|
|
|
|
mGrpLocMetaMap.insert(std::make_pair(token, g));
|
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
void RsGenExchange::setMsgStatusFlags(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, const uint32_t& status, const uint32_t& mask)
|
2012-08-21 17:32:07 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
/* TODO APPLY MASK TO FLAGS */
|
2012-08-21 17:32:07 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
token = mDataAccess->generatePublicToken();
|
|
|
|
|
|
|
|
MsgLocMetaData m;
|
|
|
|
m.val.put(RsGeneralDataService::MSG_META_STATUS, (int32_t)status);
|
|
|
|
m.msgId = msgId;
|
|
|
|
mMsgLocMetaMap.insert(std::make_pair(token, m));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsGenExchange::setMsgServiceString(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, const std::string& servString )
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
token = mDataAccess->generatePublicToken();
|
|
|
|
|
|
|
|
MsgLocMetaData m;
|
|
|
|
m.val.put(RsGeneralDataService::MSG_META_SERV_STRING, servString);
|
|
|
|
m.msgId = msgId;
|
|
|
|
mMsgLocMetaMap.insert(std::make_pair(token, m));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsGenExchange::processMsgMetaChanges()
|
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::map<uint32_t, MsgLocMetaData>::iterator mit = mMsgLocMetaMap.begin(),
|
|
|
|
mit_end = mMsgLocMetaMap.end();
|
|
|
|
|
|
|
|
for(; mit != mit_end; mit++)
|
|
|
|
{
|
|
|
|
MsgLocMetaData& m = mit->second;
|
|
|
|
bool ok = mDataStore->updateMessageMetaData(m) == 1;
|
|
|
|
uint32_t token = mit->first;
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
2012-10-05 14:12:52 -04:00
|
|
|
mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE);
|
2012-08-21 17:32:07 -04:00
|
|
|
}else
|
|
|
|
{
|
2012-10-05 14:12:52 -04:00
|
|
|
mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED);
|
2012-08-21 17:32:07 -04:00
|
|
|
}
|
|
|
|
mMsgNotify.insert(std::make_pair(token, m.msgId));
|
|
|
|
}
|
|
|
|
|
|
|
|
mMsgLocMetaMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsGenExchange::processGrpMetaChanges()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::map<uint32_t, GrpLocMetaData>::iterator mit = mGrpLocMetaMap.begin(),
|
|
|
|
mit_end = mGrpLocMetaMap.end();
|
|
|
|
|
|
|
|
for(; mit != mit_end; mit++)
|
|
|
|
{
|
|
|
|
GrpLocMetaData& g = mit->second;
|
|
|
|
uint32_t token = mit->first;
|
|
|
|
bool ok = mDataStore->updateGroupMetaData(g) == 1;
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
2012-10-05 14:12:52 -04:00
|
|
|
mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE);
|
2012-08-21 17:32:07 -04:00
|
|
|
}else
|
|
|
|
{
|
2012-10-05 14:12:52 -04:00
|
|
|
mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED);
|
2012-08-21 17:32:07 -04:00
|
|
|
}
|
|
|
|
mGrpNotify.insert(std::make_pair(token, g.grpId));
|
|
|
|
}
|
|
|
|
|
|
|
|
mGrpLocMetaMap.clear();
|
|
|
|
}
|
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
void RsGenExchange::publishMsgs()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
2012-08-18 06:01:35 -04:00
|
|
|
std::map<uint32_t, RsGxsMsgItem*>::iterator mit = mMsgsToPublish.begin();
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
for(; mit != mMsgsToPublish.end(); mit++)
|
2012-07-26 17:48:54 -04:00
|
|
|
{
|
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
RsNxsMsg* msg = new RsNxsMsg(mServType);
|
|
|
|
RsGxsMsgItem* msgItem = mit->second;
|
|
|
|
|
|
|
|
msg->grpId = msgItem->meta.mGroupId;
|
|
|
|
|
|
|
|
uint32_t size = mSerialiser->size(msgItem);
|
|
|
|
char* mData = new char[size];
|
|
|
|
bool serialOk = false;
|
|
|
|
bool createOk = false;
|
|
|
|
serialOk = mSerialiser->serialise(msgItem, mData, &size);
|
|
|
|
|
|
|
|
if(serialOk)
|
|
|
|
{
|
|
|
|
msg->msg.setBinData(mData, size);
|
|
|
|
|
|
|
|
// now create meta
|
|
|
|
msg->metaData = new RsGxsMsgMetaData();
|
|
|
|
*(msg->metaData) = msgItem->meta;
|
|
|
|
|
|
|
|
// assign time stamp
|
|
|
|
msg->metaData->mPublishTs = time(NULL);
|
|
|
|
|
|
|
|
// now intialise msg (sign it)
|
|
|
|
createOk = createMessage(msg);
|
|
|
|
RsGxsMessageId msgId;
|
|
|
|
RsGxsGroupId grpId = msgItem->meta.mGroupId;
|
|
|
|
|
|
|
|
bool msgDoesnExist = false;
|
|
|
|
|
|
|
|
if(createOk)
|
|
|
|
{
|
|
|
|
|
|
|
|
GxsMsgReq req;
|
|
|
|
std::vector<RsGxsMessageId> msgV;
|
|
|
|
msgV.push_back(msg->msgId);
|
|
|
|
GxsMsgMetaResult res;
|
|
|
|
req.insert(std::make_pair(msg->grpId, msgV));
|
|
|
|
mDataStore->retrieveGxsMsgMetaData(req, res);
|
|
|
|
msgDoesnExist = res[grpId].empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(createOk && msgDoesnExist)
|
|
|
|
{
|
|
|
|
// empty orig msg id means this is the original
|
|
|
|
// msg
|
|
|
|
// TODO: a non empty msgid means one should at least
|
|
|
|
// have the msg on disk, after which this msg is signed
|
|
|
|
// based on the security settings
|
|
|
|
// public grp (sign by grp public pub key, private/id: signed by
|
|
|
|
// id
|
|
|
|
if(msg->metaData->mOrigMsgId.empty())
|
|
|
|
{
|
|
|
|
msg->metaData->mOrigMsgId = msg->metaData->mMsgId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now serialise meta data
|
|
|
|
size = msg->metaData->serial_size();
|
|
|
|
char metaDataBuff[size];
|
|
|
|
msg->metaData->serialise(metaDataBuff, &size);
|
|
|
|
msg->meta.setBinData(metaDataBuff, size);
|
|
|
|
|
|
|
|
msgId = msg->msgId;
|
|
|
|
grpId = msg->grpId;
|
|
|
|
mDataAccess->addMsgData(msg);
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
// add to published to allow acknowledgement
|
|
|
|
mMsgNotify.insert(std::make_pair(mit->first, std::make_pair(grpId, msgId)));
|
|
|
|
mDataAccess->updatePublicRequestStatus(mit->first, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// delete msg if created wasn't ok
|
|
|
|
delete msg;
|
|
|
|
mDataAccess->updatePublicRequestStatus(mit->first, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED);
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
2012-10-23 17:52:51 -04:00
|
|
|
std::cerr << "RsGenExchange::publishMsgs() failed to publish msg " << std::endl;
|
2012-07-26 17:48:54 -04:00
|
|
|
#endif
|
2012-10-23 17:52:51 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
delete[] mData;
|
|
|
|
delete msgItem;
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
// clear msg list as we're done publishing them and entries
|
|
|
|
// are invalid
|
|
|
|
mMsgsToPublish.clear();
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
2012-11-03 21:38:48 -04:00
|
|
|
void RsGenExchange::service_CreateGroup(RsGxsGrpItem* grpItem, RsTlvSecurityKeySet& keySet)
|
|
|
|
{
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
|
|
|
std::cerr << "RsGenExchange::service_CreateGroup(): Does nothing"
|
|
|
|
<< std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define GEN_EXCH_GRP_CHUNK 3
|
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
void RsGenExchange::publishGrps()
|
|
|
|
{
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsStackMutex stack(mGenMtx);
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
std::map<uint32_t, RsGxsGrpItem*>::iterator mit = mGrpsToPublish.begin();
|
|
|
|
std::vector<uint32_t> toRemove;
|
|
|
|
int i = 0;
|
|
|
|
for(; mit != mGrpsToPublish.end(); mit++)
|
|
|
|
{
|
|
|
|
toRemove.push_back(mit->first);
|
|
|
|
i++;
|
|
|
|
if(i > GEN_EXCH_GRP_CHUNK) break;
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsNxsGrp* grp = new RsNxsGrp(mServType);
|
|
|
|
RsGxsGrpItem* grpItem = mit->second;
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsTlvSecurityKeySet keySet;
|
|
|
|
generateGroupKeys(keySet,
|
|
|
|
!(grpItem->meta.mGroupFlags & GXS_SERV::FLAG_PRIVACY_PUBLIC));
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// find private admin key
|
|
|
|
RsTlvSecurityKey privAdminKey;
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator mit_keys = keySet.keys.begin();
|
2012-11-08 19:56:07 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
bool privKeyFound = false;
|
|
|
|
for(; mit_keys != keySet.keys.end(); mit_keys++)
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey& key = mit_keys->second;
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
if(key.keyFlags & (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
|
2012-11-03 21:38:48 -04:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
privAdminKey = key;
|
|
|
|
privKeyFound = true;
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 19:56:07 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
bool ok = true;
|
2012-11-08 19:56:07 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
if(privKeyFound)
|
|
|
|
{
|
|
|
|
// get group id from private admin key id
|
|
|
|
grpItem->meta.mGroupId = grp->grpId = privAdminKey.keyId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
}
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-11-08 19:56:07 -05:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
service_CreateGroup(grpItem, keySet);
|
|
|
|
|
|
|
|
uint32_t size = mSerialiser->size(grpItem);
|
|
|
|
char gData[size];
|
|
|
|
ok = mSerialiser->serialise(grpItem, gData, &size);
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGenExchange::publishGrps() !ok ERROR After First Serialise" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
grp->grp.setBinData(gData, size);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
grp->metaData = new RsGxsGrpMetaData();
|
|
|
|
grpItem->meta.mPublishTs = time(NULL);
|
|
|
|
*(grp->metaData) = grpItem->meta;
|
|
|
|
grp->metaData->mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_ADMIN;
|
|
|
|
|
|
|
|
ok &= createGroup(grp, keySet);
|
|
|
|
|
|
|
|
if (!ok)
|
2012-11-03 21:38:48 -04:00
|
|
|
{
|
2012-11-11 18:45:22 -05:00
|
|
|
std::cerr << "RsGenExchange::publishGrps() !ok ERROR After createGroup" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGxsGroupId grpId = grp->grpId;
|
|
|
|
mDataAccess->addGroupData(grp);
|
|
|
|
|
|
|
|
std::cerr << "RsGenExchange::publishGrps() ok -> pushing to notifies" << std::endl;
|
|
|
|
|
|
|
|
// add to published to allow acknowledgement
|
|
|
|
mGrpNotify.insert(std::make_pair(mit->first, grpId));
|
|
|
|
mDataAccess->updatePublicRequestStatus(mit->first, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ok)
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
|
|
|
#endif
|
2012-11-11 18:45:22 -05:00
|
|
|
std::cerr << "RsGenExchange::publishGrps() failed to publish grp " << std::endl;
|
|
|
|
delete grp;
|
2012-08-18 06:01:35 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// add to published to allow acknowledgement, grpid is empty as grp creation failed
|
|
|
|
mGrpNotify.insert(std::make_pair(mit->first, RsGxsGroupId("")));
|
|
|
|
mDataAccess->updatePublicRequestStatus(mit->first, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED);
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
delete grpItem;
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
// clear grp list as we're done publishing them and entries
|
|
|
|
// are invalid
|
2012-11-03 21:38:48 -04:00
|
|
|
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
for(int i = 0; i < toRemove.size(); i++)
|
|
|
|
mGrpsToPublish.erase(toRemove[i]);
|
2012-08-06 17:00:38 -04:00
|
|
|
}
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsGenExchange::generatePublicToken()
|
|
|
|
{
|
|
|
|
return mDataAccess->generatePublicToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::updatePublicRequestStatus(const uint32_t &token, const uint32_t &status)
|
|
|
|
{
|
|
|
|
return mDataAccess->updatePublicRequestStatus(token, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::disposeOfPublicToken(const uint32_t &token)
|
|
|
|
{
|
|
|
|
return mDataAccess->disposeOfPublicToken(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGeneralDataService* RsGenExchange::getDataStore()
|
|
|
|
{
|
|
|
|
return mDataStore;
|
|
|
|
}
|
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
bool RsGenExchange::getGroupKeys(const RsGxsGroupId &grpId, RsTlvSecurityKeySet &keySet)
|
|
|
|
{
|
|
|
|
if(grpId.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*> grpMeta;
|
|
|
|
grpMeta[grpId] = NULL;
|
|
|
|
mDataStore->retrieveGxsGrpMetaData(grpMeta);
|
|
|
|
|
|
|
|
if(grpMeta.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RsGxsGrpMetaData* meta = grpMeta[grpId];
|
|
|
|
|
|
|
|
if(meta == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
keySet = meta->keys;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:04:04 -04:00
|
|
|
void RsGenExchange::createDummyGroup(RsGxsGrpItem *grpItem)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
RsNxsGrp* grp = new RsNxsGrp(mServType);
|
|
|
|
uint32_t size = mSerialiser->size(grpItem);
|
|
|
|
char gData[size];
|
|
|
|
bool ok = mSerialiser->serialise(grpItem, gData, &size);
|
|
|
|
grp->grp.setBinData(gData, size);
|
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
RsTlvSecurityKeySet keySet;
|
|
|
|
generateGroupKeys(keySet,
|
|
|
|
!(grpItem->meta.mGroupFlags & GXS_SERV::FLAG_PRIVACY_PUBLIC));
|
|
|
|
|
|
|
|
// find private admin key
|
|
|
|
RsTlvSecurityKey privAdminKey;
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator mit_keys = keySet.keys.begin();
|
|
|
|
|
|
|
|
bool privKeyFound = false;
|
|
|
|
for(; mit_keys != keySet.keys.end(); mit_keys++)
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey& key = mit_keys->second;
|
|
|
|
|
|
|
|
if(key.keyFlags & (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
|
|
|
|
{
|
|
|
|
privAdminKey = key;
|
|
|
|
privKeyFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(privKeyFound)
|
|
|
|
{
|
|
|
|
// get group id from private admin key id
|
|
|
|
grpItem->meta.mGroupId = grp->grpId = privAdminKey.keyId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
}
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-11-11 18:45:22 -05:00
|
|
|
service_CreateGroup(grpItem, keySet);
|
2012-11-03 21:38:48 -04:00
|
|
|
|
2012-09-25 17:04:04 -04:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
grp->metaData = new RsGxsGrpMetaData();
|
|
|
|
grpItem->meta.mPublishTs = time(NULL);
|
|
|
|
*(grp->metaData) = grpItem->meta;
|
2012-11-11 18:45:22 -05:00
|
|
|
grp->metaData->mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_ADMIN;
|
2012-11-03 21:38:48 -04:00
|
|
|
createGroup(grp, keySet);
|
2012-09-25 17:04:04 -04:00
|
|
|
|
2012-09-27 17:42:00 -04:00
|
|
|
mDataAccess->addGroupData(grp);
|
2012-09-25 17:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!ok)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
|
|
|
#endif
|
2012-11-08 19:56:07 -05:00
|
|
|
std::cerr << "RsGenExchange::createDummyGroup(); failed to publish grp " << std::endl;
|
2012-09-25 17:04:04 -04:00
|
|
|
delete grp;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete grpItem;
|
|
|
|
}
|
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::processRecvdData()
|
|
|
|
{
|
2012-09-25 17:04:04 -04:00
|
|
|
processRecvdGroups();
|
2012-09-30 10:21:17 -04:00
|
|
|
|
|
|
|
processRecvdMessages();
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::processRecvdMessages()
|
|
|
|
{
|
2012-09-30 10:21:17 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::vector<RsNxsMsg*>::iterator vit = mReceivedMsgs.begin();
|
|
|
|
GxsMsgReq msgIds;
|
|
|
|
std::map<RsNxsMsg*, RsGxsMsgMetaData*> msgs;
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*> grpMetas;
|
|
|
|
|
|
|
|
// coalesce group meta retrieval for performance
|
2012-09-30 10:21:17 -04:00
|
|
|
for(; vit != mReceivedMsgs.end(); vit++)
|
2012-11-10 18:42:38 -05:00
|
|
|
{
|
|
|
|
RsNxsMsg* msg = *vit;
|
|
|
|
grpMetas.insert(std::make_pair(msg->grpId, (RsGxsGrpMetaData*)NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
mDataStore->retrieveGxsGrpMetaData(grpMetas);
|
|
|
|
|
|
|
|
for(vit = mReceivedMsgs.begin(); vit != mReceivedMsgs.end(); vit++)
|
2012-09-30 10:21:17 -04:00
|
|
|
{
|
|
|
|
RsNxsMsg* msg = *vit;
|
|
|
|
RsGxsMsgMetaData* meta = new RsGxsMsgMetaData();
|
|
|
|
bool ok = meta->deserialise(msg->meta.bin_data, &(msg->meta.bin_len));
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
2012-11-10 18:42:38 -05:00
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*>::iterator mit = grpMetas.find(msg->grpId);
|
|
|
|
|
|
|
|
// validate msg
|
|
|
|
if(mit != grpMetas.end()){
|
|
|
|
RsGxsGrpMetaData* grpMeta = mit->second;
|
|
|
|
ok = true;
|
|
|
|
//&= validateMsg(msg, grpMeta->mGroupFlags, grpMeta->keys);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ok = false;
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
msgs.insert(std::make_pair(msg, meta));
|
|
|
|
msgIds[msg->grpId].push_back(msg->msgId);
|
|
|
|
}
|
2012-09-30 10:21:17 -04:00
|
|
|
}
|
2012-11-10 18:42:38 -05:00
|
|
|
|
|
|
|
if(!ok)
|
2012-09-30 10:21:17 -04:00
|
|
|
{
|
|
|
|
#ifdef GXS_GENX_DEBUG
|
|
|
|
std::cerr << "failed to deserialise incoming meta, grpId: "
|
|
|
|
<< msg->grpId << ", msgId: " << msg->msgId << std::endl;
|
|
|
|
#endif
|
|
|
|
delete msg;
|
|
|
|
delete meta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-10 18:42:38 -05:00
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*>::iterator mit = grpMetas.begin();
|
|
|
|
|
|
|
|
// clean up resources
|
|
|
|
for(; mit != grpMetas.end(); mit++)
|
|
|
|
{
|
|
|
|
delete mit->second;
|
|
|
|
}
|
|
|
|
|
2012-09-30 10:21:17 -04:00
|
|
|
if(!msgIds.empty())
|
|
|
|
{
|
|
|
|
mDataStore->storeMessage(msgs);
|
|
|
|
RsGxsMsgChange* c = new RsGxsMsgChange();
|
|
|
|
c->msgChangeMap = msgIds;
|
|
|
|
mNotifications.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
mReceivedMsgs.clear();
|
2012-08-06 17:00:38 -04:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::processRecvdGroups()
|
|
|
|
{
|
2012-09-25 17:04:04 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::vector<RsNxsGrp*>::iterator vit = mReceivedGrps.begin();
|
|
|
|
std::map<RsNxsGrp*, RsGxsGrpMetaData*> grps;
|
|
|
|
|
|
|
|
std::list<RsGxsGroupId> grpIds;
|
|
|
|
|
|
|
|
for(; vit != mReceivedGrps.end(); vit++)
|
|
|
|
{
|
|
|
|
RsNxsGrp* grp = *vit;
|
|
|
|
RsGxsGrpMetaData* meta = new RsGxsGrpMetaData();
|
2012-09-30 10:21:17 -04:00
|
|
|
bool ok = meta->deserialise(grp->meta.bin_data, grp->meta.bin_len);
|
2012-09-25 17:04:04 -04:00
|
|
|
|
2012-09-30 10:21:17 -04:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
grps.insert(std::make_pair(grp, meta));
|
|
|
|
grpIds.push_back(grp->grpId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef GXS_GENX_DEBUG
|
|
|
|
std::cerr << "failed to deserialise incoming meta, grpId: "
|
|
|
|
<< grp->grpId << std::endl;
|
|
|
|
#endif
|
|
|
|
delete grp;
|
|
|
|
delete meta;
|
|
|
|
}
|
2012-09-25 17:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!grpIds.empty())
|
|
|
|
{
|
|
|
|
RsGxsGroupChange* c = new RsGxsGroupChange();
|
|
|
|
c->grpIdList = grpIds;
|
|
|
|
mNotifications.push_back(c);
|
|
|
|
mDataStore->storeGroup(grps);
|
|
|
|
}
|
|
|
|
|
|
|
|
mReceivedGrps.clear();
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|