fixed compilation and several problems following separation of public/private keys

This commit is contained in:
csoler 2016-06-04 21:39:40 -04:00
parent 590be092e5
commit a2ca0385db
16 changed files with 291 additions and 523 deletions

View File

@ -406,7 +406,7 @@ class RsChatDHPublicKeyItem: public RsChatItem
BIGNUM *public_key ; BIGNUM *public_key ;
RsTlvKeySignature signature ; // signs the public key in a row. RsTlvKeySignature signature ; // signs the public key in a row.
RsTlvSecurityKey_deprecated gxs_key ; // public key of the signer RsTlvPublicRSAKey gxs_key ; // public key of the signer
private: private:
RsChatDHPublicKeyItem(const RsChatDHPublicKeyItem&) : RsChatItem(RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY) {} // make the object non copy-able RsChatDHPublicKeyItem(const RsChatDHPublicKeyItem&) : RsChatItem(RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY) {} // make the object non copy-able

View File

@ -1958,8 +1958,6 @@ bool p3GRouter::signDataItem(RsGRouterAbstractMsgItem *item,const RsGxsId& signi
{ {
try try
{ {
RsTlvSecurityKey_deprecated signature_key ;
#ifdef GROUTER_DEBUG #ifdef GROUTER_DEBUG
std::cerr << "p3GRouter::signDataItem()" << std::endl; std::cerr << "p3GRouter::signDataItem()" << std::endl;
std::cerr << " Key ID = " << signing_id << std::endl; std::cerr << " Key ID = " << signing_id << std::endl;

View File

@ -174,17 +174,6 @@ bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key)
return true ; return true ;
} }
static void setRSAPrivateKeyData(RsTlvSecurityKey_deprecated & key, RSA *rsa_priv)
{
unsigned char *data = NULL ;
int reqspace = i2d_RSAPrivateKey(rsa_priv, &data);
key.keyData.setBinData(data, reqspace);
key.keyId = getRsaKeyFingerprint(rsa_priv);
free(data) ;
}
bool GxsSecurity::generateKeyPair(RsTlvPublicRSAKey& public_key,RsTlvPrivateRSAKey& private_key) bool GxsSecurity::generateKeyPair(RsTlvPublicRSAKey& public_key,RsTlvPrivateRSAKey& private_key)
{ {
// admin keys // admin keys
@ -991,8 +980,7 @@ bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& s
#endif #endif
/* extract admin key */ /* extract admin key */
#warning Souldn't need to do that HERE!! RSA *rsakey = d2i_RSAPublicKey(NULL, &(keyptr), keylen);
RSA *rsakey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)? d2i_RSAPrivateKey(NULL, &(keyptr), keylen): d2i_RSAPublicKey(NULL, &(keyptr), keylen);
if (!rsakey) if (!rsakey)
{ {
@ -1067,4 +1055,21 @@ bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& s
return false; return false;
} }
void GxsSecurity::createPublicKeysFromPrivateKeys(RsTlvSecurityKeySet& keyset)
{
for( std::map<RsGxsId, RsTlvPrivateRSAKey>::const_iterator it = keyset.private_keys.begin(); it != keyset.private_keys.end() ; ++it)
if(keyset.public_keys.find(it->second.keyId) == keyset.public_keys.end())
{
RsTlvPublicRSAKey pub_key ;
if(!extractPublicKey(it->second,pub_key))
{
std::cerr << "(EE) ERROR when trying to generate public key from private key for ID " << it->second.keyId << ". This is probably a bug with security implications." << std::endl;
continue ;
}
keyset.public_keys[it->second.keyId] = pub_key ;
std::cerr << "(II) Generated missing public key for ID " << it->second.keyId << " from private key." << std::endl;
}
}

View File

@ -125,6 +125,15 @@ class GxsSecurity
static bool checkPublicKey(const RsTlvPublicRSAKey &key); static bool checkPublicKey(const RsTlvPublicRSAKey &key);
static bool checkPrivateKey(const RsTlvPrivateRSAKey &key); static bool checkPrivateKey(const RsTlvPrivateRSAKey &key);
/*!
* Adds possibly missing public keys when private keys are present.
*
* \brief createPublicKeysForPrivateKeys
* \param set set of keys to consider
* \return
*/
static void createPublicKeysFromPrivateKeys(RsTlvSecurityKeySet& set) ;
}; };
#endif // GXSSECURITY_H #endif // GXSSECURITY_H

View File

@ -373,20 +373,6 @@ void RsGenExchange::generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPubli
} }
} }
void RsGenExchange::generatePublicFromPrivateKeys(RsTlvSecurityKeySet& keySet)
{
// actually just copy settings of one key except mark its key flags public
keySet.public_keys.clear() ;
for(std::map<RsGxsId, RsTlvPrivateRSAKey>::const_iterator cit=keySet.private_keys.begin(); cit != keySet.private_keys.end(); ++cit)
{
RsTlvPublicRSAKey pubkey ;
if(GxsSecurity::extractPublicKey(cit->second,pubkey))
keySet.public_keys.insert(std::make_pair(pubkey.keyId, pubkey));
}
}
uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& keySet) uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& keySet)
{ {
#ifdef GEN_EXCH_DEBUG #ifdef GEN_EXCH_DEBUG
@ -2166,7 +2152,9 @@ void RsGenExchange::processGroupUpdatePublish()
if(checkKeys(meta->keys)) if(checkKeys(meta->keys))
{ {
ggps.mKeys = meta->keys; ggps.mKeys = meta->keys;
generatePublicFromPrivateKeys(ggps.mKeys);
GxsSecurity::createPublicKeysFromPrivateKeys(ggps.mKeys) ;
ggps.mHaveKeys = true; ggps.mHaveKeys = true;
ggps.mStartTS = time(NULL); ggps.mStartTS = time(NULL);
ggps.mLastAttemptTS = 0; ggps.mLastAttemptTS = 0;
@ -2581,29 +2569,30 @@ RsGeneralDataService* RsGenExchange::getDataStore()
bool RsGenExchange::getGroupKeys(const RsGxsGroupId &grpId, RsTlvSecurityKeySet &keySet) bool RsGenExchange::getGroupKeys(const RsGxsGroupId &grpId, RsTlvSecurityKeySet &keySet)
{ {
if(grpId.isNull()) if(grpId.isNull())
return false; return false;
RS_STACK_MUTEX(mGenMtx) ; RS_STACK_MUTEX(mGenMtx) ;
std::map<RsGxsGroupId, RsGxsGrpMetaData*> grpMeta; std::map<RsGxsGroupId, RsGxsGrpMetaData*> grpMeta;
grpMeta[grpId] = NULL; grpMeta[grpId] = NULL;
mDataStore->retrieveGxsGrpMetaData(grpMeta); mDataStore->retrieveGxsGrpMetaData(grpMeta);
if(grpMeta.empty()) if(grpMeta.empty())
return false; return false;
RsGxsGrpMetaData* meta = grpMeta[grpId]; RsGxsGrpMetaData* meta = grpMeta[grpId];
if(meta == NULL) if(meta == NULL)
return false; return false;
keySet = meta->keys; keySet = meta->keys;
GxsSecurity::createPublicKeysFromPrivateKeys(keySet) ;
for(std::map<RsGxsGroupId, RsGxsGrpMetaData*>::iterator it=grpMeta.begin();it!=grpMeta.end();++it) for(std::map<RsGxsGroupId, RsGxsGrpMetaData*>::iterator it=grpMeta.begin();it!=grpMeta.end();++it)
delete it->second ; delete it->second ;
return true; return true;
} }
void RsGenExchange::shareGroupPublishKey(const RsGxsGroupId& grpId,const std::set<RsPeerId>& peers) void RsGenExchange::shareGroupPublishKey(const RsGxsGroupId& grpId,const std::set<RsPeerId>& peers)

View File

@ -743,15 +743,6 @@ private:
*/ */
void generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPublishKeys); void generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPublishKeys);
/*!
* Generate public set of keys from their private counterparts
* No keys will be generated if one fails
* @param privatekeySet contains private generated keys
* @param publickeySet contains public generated keys (counterpart of private)
* @return false if key gen failed for a key set
*/
void generatePublicFromPrivateKeys(RsTlvSecurityKeySet& keySet);
/*! /*!
* Attempts to validate msg signatures * Attempts to validate msg signatures
* @param msg message to be validated * @param msg message to be validated

View File

@ -4877,7 +4877,7 @@ void RsGxsNetService::sharePublishKeysPending()
publishKeyItem->clear(); publishKeyItem->clear();
publishKeyItem->grpId = mit->first; publishKeyItem->grpId = mit->first;
publishKeyItem->key = publishKey ; publishKeyItem->private_key = publishKey ;
publishKeyItem->PeerId(*it); publishKeyItem->PeerId(*it);
sendItem(publishKeyItem); sendItem(publishKeyItem);
@ -4932,8 +4932,8 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item)
GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key received: " << std::endl; GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key received: " << std::endl;
#endif #endif
bool admin = (item->key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; bool admin = (item->private_key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (item->private_key.keyFlags & RSTLV_KEY_TYPE_FULL) ;
bool publi = (item->key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; bool publi = (item->private_key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (item->private_key.keyFlags & RSTLV_KEY_TYPE_FULL) ;
#ifdef NXS_NET_DEBUG_3 #ifdef NXS_NET_DEBUG_3
GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key id = " << item->key.keyId << " admin=" << admin << ", publish=" << publi << " ts=" << item->key.endTS << std::endl; GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key id = " << item->key.keyId << " admin=" << admin << ", publish=" << publi << " ts=" << item->key.endTS << std::endl;
@ -4946,13 +4946,13 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item)
} }
// Also check that we don't already have full keys for that group. // Also check that we don't already have full keys for that group.
if(grpMeta->keys.public_keys.find(item->key.keyId) == grpMeta->keys.public_keys.end()) if(grpMeta->keys.public_keys.find(item->private_key.keyId) == grpMeta->keys.public_keys.end())
{ {
std::cerr << " (EE) Key not found in known group keys. This is an inconsistency." << std::endl; std::cerr << " (EE) Key not found in known group keys. This is an inconsistency." << std::endl;
return ; return ;
} }
if(grpMeta->keys.private_keys.find(item->key.keyId) != grpMeta->keys.private_keys.end()) if(grpMeta->keys.private_keys.find(item->private_key.keyId) != grpMeta->keys.private_keys.end())
{ {
#ifdef NXS_NET_DEBUG_3 #ifdef NXS_NET_DEBUG_3
GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " (EE) Publish key already present in database. Discarding message." << std::endl; GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " (EE) Publish key already present in database. Discarding message." << std::endl;
@ -4962,7 +4962,7 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item)
// Store/update the info. // Store/update the info.
grpMeta->keys.private_keys[item->key.keyId] = item->key ; grpMeta->keys.private_keys[item->private_key.keyId] = item->private_key ;
bool ret = mDataStore->updateGroupKeys(item->grpId,grpMeta->keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ; bool ret = mDataStore->updateGroupKeys(item->grpId,grpMeta->keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ;

View File

@ -117,7 +117,7 @@ virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0); std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvServiceIdSet signing_classes; RsTlvServiceIdSet signing_classes;
RsTlvSecurityKey_deprecated key; // has from->to, and flags. RsTlvPublicRSAKey key; // has from->to, and flags.
RsTlvKeySignature sign; RsTlvKeySignature sign;
}; };

View File

@ -401,7 +401,7 @@ bool RsNxsGroupPublishKeyItem::serialise(void *data, uint32_t& size) const
return false ; return false ;
ok &= grpId.serialise(data, size, offset) ; ok &= grpId.serialise(data, size, offset) ;
ok &= key.SetTlv(data, size, &offset) ; ok &= private_key.SetTlv(data, size, &offset) ;
if(offset != tlvsize) if(offset != tlvsize)
{ {
@ -811,7 +811,7 @@ RsNxsGroupPublishKeyItem* RsNxsSerialiser::deserialNxsGroupPublishKeyItem(void *
RsNxsGroupPublishKeyItem* item = new RsNxsGroupPublishKeyItem(SERVICE_TYPE); RsNxsGroupPublishKeyItem* item = new RsNxsGroupPublishKeyItem(SERVICE_TYPE);
ok &= item->grpId.deserialise(data, *size, offset); ok &= item->grpId.deserialise(data, *size, offset);
ok &= item->key.GetTlv(data, *size, &offset) ; ok &= item->private_key.GetTlv(data, *size, &offset) ;
if (offset != *size) if (offset != *size)
{ {
@ -957,7 +957,7 @@ uint32_t RsNxsGroupPublishKeyItem::serial_size() const
uint32_t s = 8; // header size uint32_t s = 8; // header size
s += grpId.serial_size() ; s += grpId.serial_size() ;
s += key.TlvSize(); s += private_key.TlvSize();
return s; return s;
} }
@ -1091,7 +1091,7 @@ void RsNxsSyncGrpReqItem::clear()
} }
void RsNxsGroupPublishKeyItem::clear() void RsNxsGroupPublishKeyItem::clear()
{ {
key.TlvClear(); private_key.TlvClear();
} }
void RsNxsSyncMsgReqItem::clear() void RsNxsSyncMsgReqItem::clear()
{ {
@ -1163,7 +1163,7 @@ std::ostream& RsNxsGroupPublishKeyItem::print(std::ostream &out, uint16_t indent
printIndent(out , int_Indent); printIndent(out , int_Indent);
out << "GroupId: " << grpId << std::endl; out << "GroupId: " << grpId << std::endl;
printIndent(out , int_Indent); printIndent(out , int_Indent);
out << "keyId: " << key.keyId << std::endl; out << "keyId: " << private_key.keyId << std::endl;
printRsItemEnd(out ,"RsNxsGroupPublishKeyItem", indent); printRsItemEnd(out ,"RsNxsGroupPublishKeyItem", indent);

View File

@ -157,8 +157,7 @@ public:
virtual uint32_t serial_size() const; virtual uint32_t serial_size() const;
RsGxsGroupId grpId ; RsGxsGroupId grpId ;
#warning should be renamed private_key RsTlvPrivateRSAKey private_key ;
RsTlvPrivateRSAKey key ;
}; };

View File

@ -207,14 +207,16 @@ const uint16_t TLV_TYPE_GXSCIRCLEIDSET= 0x1026;
const uint16_t TLV_TYPE_SERVICESET = 0x1030; const uint16_t TLV_TYPE_SERVICESET = 0x1030;
const uint16_t TLV_TYPE_SECURITYKEY_deprecated = 0x1040; // *_deprectate should not be used anymore!!
const uint16_t TLV_TYPE_SECURITYKEYSET = 0x1041; // We use 1040 for both public and private keys, so that transmitting them still works (backward compatibility), and so that
const uint16_t TLV_TYPE_RSA_KEY_PUBLIC = 0x1042; // signatures are kept. But the two different classes will check that the flags are correct when deserialising.
const uint16_t TLV_TYPE_RSA_KEY_PRIVATE= 0x1043;
const uint16_t TLV_TYPE_KEYSIGNATURE = 0x1050; const uint16_t TLV_TYPE_SECURITY_KEY = 0x1040;
const uint16_t TLV_TYPE_SECURITYKEYSET = 0x1041;
const uint16_t TLV_TYPE_KEYSIGNATURE = 0x1050;
const uint16_t TLV_TYPE_KEYSIGNATURESET = 0x1051; const uint16_t TLV_TYPE_KEYSIGNATURESET = 0x1051;
const uint16_t TLV_TYPE_KEYSIGNATURETYPE = 0x1052; const uint16_t TLV_TYPE_KEYSIGNATURETYPE = 0x1052;
const uint16_t TLV_TYPE_IMAGE = 0x1060; const uint16_t TLV_TYPE_IMAGE = 0x1060;

View File

@ -38,13 +38,13 @@
/************************************* RsTlvSecurityKey ************************************/ /************************************* RsTlvSecurityKey ************************************/
RsTlvSecurityKey_deprecated::RsTlvSecurityKey_deprecated() RsTlvRSAKey::RsTlvRSAKey()
:RsTlvItem(), keyFlags(0), startTS(0), endTS(0), keyData(TLV_TYPE_KEY_EVP_PKEY) :RsTlvItem(), keyFlags(0), startTS(0), endTS(0), keyData(TLV_TYPE_KEY_EVP_PKEY)
{ {
return; return;
} }
void RsTlvSecurityKey_deprecated::TlvClear() void RsTlvRSAKey::TlvClear()
{ {
keyId.clear(); keyId.clear();
keyFlags = 0; keyFlags = 0;
@ -54,7 +54,7 @@ void RsTlvSecurityKey_deprecated::TlvClear()
} }
/* clears keyData - but doesn't delete */ /* clears keyData - but doesn't delete */
void RsTlvSecurityKey_deprecated::ShallowClear() void RsTlvRSAKey::ShallowClear()
{ {
keyId.clear(); keyId.clear();
keyFlags = 0; keyFlags = 0;
@ -70,21 +70,6 @@ uint32_t RsTlvRSAKey::TlvSize() const
/* now add comment and title length of this tlv object */ /* now add comment and title length of this tlv object */
s += keyId.serial_size();
s += 4;
s += 4;
s += 4;
s += keyData.TlvSize();
return s;
}
uint32_t RsTlvSecurityKey_deprecated::TlvSize() const
{
uint32_t s = TLV_HEADER_SIZE; /* header + 4 for size */
/* now add comment and title length of this tlv object */
#ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT #ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT
s += GetTlvStringSize(keyId.toStdString()) ; s += GetTlvStringSize(keyId.toStdString()) ;
#else #else
@ -116,44 +101,7 @@ bool RsTlvRSAKey::SetTlv(void *data, uint32_t size, uint32_t *offset) const
/* start at data[offset] */ /* start at data[offset] */
/* add mandatory parts first */ /* add mandatory parts first */
ok &= SetTlvBase(data, tlvend, offset, tlvType(), tlvsize); ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITY_KEY, tlvsize);
ok &= keyId.serialise(data, tlvend, *offset) ;
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_deprecated::SetTlv(void *data, uint32_t size, uint32_t *offset) const
{
std::cerr << "(EE) Serialisation of an old security key format. Will not be done! callstack is:" << std::cerr << std::endl;
print_stacktrace() ;
#warning REMOVE THIS CODE BELOW WHEN IT IS NOT CALLED ANYMORE
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
{
//#ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::SetTlv() Failed not enough space";
std::cerr << std::endl;
//#endif
return false; /* not enough space */
}
bool ok = true;
/* start at data[offset] */
/* add mandatory parts first */
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEY_deprecated, tlvsize);
#ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT #ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT
ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId.toStdString()); ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId.toStdString());
#else #else
@ -186,7 +134,7 @@ bool RsTlvRSAKey::GetTlv(void *data, uint32_t size, uint32_t *offset)
return false; /* not enough space */ return false; /* not enough space */
} }
if (tlvtype != tlvType()) /* check type */ if (tlvtype != TLV_TYPE_SECURITY_KEY) /* check type */
{ {
#ifdef TLV_DEBUG #ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::GetTlv() Fail, wrong type"; std::cerr << "RsTlvSecurityKey::GetTlv() Fail, wrong type";
@ -202,75 +150,6 @@ bool RsTlvRSAKey::GetTlv(void *data, uint32_t size, uint32_t *offset)
/* skip the header */ /* skip the header */
(*offset) += TLV_HEADER_SIZE; (*offset) += TLV_HEADER_SIZE;
ok &= keyId.deserialise(data, tlvend, *offset) ;
ok &= getRawUInt32(data, tlvend, offset, &(keyFlags));
ok &= getRawUInt32(data, tlvend, offset, &(startTS));
ok &= getRawUInt32(data, tlvend, offset, &(endTS));
ok &= keyData.GetTlv(data, tlvend, offset);
ok &= checkFlags(keyFlags) ;
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
if (!ok)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::GetTlv() Failed somewhere ok == false";
std::cerr << std::endl;
#endif
}
return ok;
}
bool RsTlvSecurityKey_deprecated::GetTlv(void *data, uint32_t size, uint32_t *offset)
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::GetTlv() Fail, not enough space";
std::cerr << std::endl;
#endif
return false; /* not enough space */
}
if (tlvtype != TLV_TYPE_SECURITYKEY_deprecated) /* check type */
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvSecurityKey::GetTlv() Fail, wrong type";
std::cerr << std::endl;
#endif
return false;
}
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
#ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT #ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT
std::string s ; std::string s ;
ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, s); ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, s);
@ -282,7 +161,6 @@ bool RsTlvSecurityKey_deprecated::GetTlv(void *data, uint32_t size, uint32_t *o
ok &= getRawUInt32(data, tlvend, offset, &(startTS)); ok &= getRawUInt32(data, tlvend, offset, &(startTS));
ok &= getRawUInt32(data, tlvend, offset, &(endTS)); ok &= getRawUInt32(data, tlvend, offset, &(endTS));
ok &= keyData.GetTlv(data, tlvend, offset); ok &= keyData.GetTlv(data, tlvend, offset);
/*************************************************************************** /***************************************************************************
* NB: extra components could be added (for future expansion of the type). * NB: extra components could be added (for future expansion of the type).
@ -306,12 +184,10 @@ bool RsTlvSecurityKey_deprecated::GetTlv(void *data, uint32_t size, uint32_t *o
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
} }
return ok && checkFlags(keyFlags) ;
return ok;
} }
std::ostream& RsTlvRSAKey::print(std::ostream &out, uint16_t indent) const
std::ostream &RsTlvSecurityKey_deprecated::print(std::ostream &out, uint16_t indent) const
{ {
printBase(out, "RsTlvSecurityKey", indent); printBase(out, "RsTlvSecurityKey", indent);
uint16_t int_Indent = indent + 2; uint16_t int_Indent = indent + 2;
@ -435,54 +311,30 @@ bool RsTlvSecurityKeySet::GetTlv(void *data, uint32_t size, uint32_t *offset)
/* get the next type */ /* get the next type */
uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
// Security key set can be composed of public or private keys. We sort them into the correct bins after deserialisation
switch(tlvsubtype) switch(tlvsubtype)
{ {
case TLV_TYPE_SECURITYKEY_deprecated: case TLV_TYPE_SECURITY_KEY:
{ {
RsTlvSecurityKey_deprecated key; uint32_t offset_save = *offset ;
ok &= key.GetTlv(data, size, offset); RsTlvPublicRSAKey public_key;
if (ok) if(public_key.GetTlv(data, tlvend, offset))
public_keys[public_key.keyId] = public_key;
else
{ {
std::cerr << "(WW) read RSA key with old format. Will be converted to new format. Flags=" << std::hex << key.keyFlags << std::dec << std::endl; *offset = offset_save ;
if(key.keyFlags & RSTLV_KEY_TYPE_FULL) RsTlvPrivateRSAKey private_key;
private_keys[key.keyId] = RsTlvPrivateRSAKey(key);
else
public_keys[key.keyId] = RsTlvPublicRSAKey(key);
key.TlvClear(); /* so that the Map can get control - should be ref counted*/ if(private_key.GetTlv(data, tlvend, offset))
private_keys[private_key.keyId] = private_key;
} }
} }
break ; break ;
case TLV_TYPE_RSA_KEY_PRIVATE:
{
RsTlvPrivateRSAKey key;
if(key.GetTlv(data, size, offset))
private_keys[key.keyId] = key;
else
ok = false ;
key.TlvClear(); /* so that the Map can get control - should be ref counted*/
}
break;
case TLV_TYPE_RSA_KEY_PUBLIC:
{
RsTlvPublicRSAKey key;
if(key.GetTlv(data, size, offset))
public_keys[key.keyId] = key;
else
ok = false ;
key.TlvClear(); /* so that the Map can get control - should be ref counted*/
}
break;
default: default:
ok &= SkipUnknownTlv(data, tlvend, offset); ok &= SkipUnknownTlv(data, tlvend, offset);
break; break;
@ -513,6 +365,7 @@ bool RsTlvSecurityKeySet::GetTlv(void *data, uint32_t size, uint32_t *offset)
return ok; return ok;
} }
// prints out contents of RsTlvSecurityKeySet // prints out contents of RsTlvSecurityKeySet
std::ostream &RsTlvSecurityKeySet::print(std::ostream &out, uint16_t indent) const std::ostream &RsTlvSecurityKeySet::print(std::ostream &out, uint16_t indent) const
{ {
@ -855,3 +708,23 @@ uint32_t RsTlvKeySignatureSet::TlvSize() const
return s; return s;
} }

View File

@ -49,39 +49,17 @@ const uint32_t RSTLV_KEY_DISTRIB_MASK = 0x00f0;
// Old class for RsTlvSecurityKey. Is kept for backward compatibility, but should not be serialised anymore // Old class for RsTlvSecurityKey. Is kept for backward compatibility, but should not be serialised anymore
class RsTlvSecurityKey_deprecated: public RsTlvItem class RsTlvRSAKey: public RsTlvItem
{
public:
RsTlvSecurityKey_deprecated();
virtual ~RsTlvSecurityKey_deprecated() {}
virtual uint32_t TlvSize() const;
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const;
/* clears KeyData - but doesn't delete - to transfer ownership */
void ShallowClear();
RsGxsId keyId; // Mandatory :
uint32_t keyFlags; // Mandatory ;
uint32_t startTS; // Mandatory :
uint32_t endTS; // Mandatory :
RsTlvBinaryData keyData; // Mandatory :
};
class RsTlvRSAKey
{ {
public: public:
virtual uint32_t tlvType() const = 0 ; RsTlvRSAKey();
virtual bool checkFlags(uint32_t flags) const = 0 ; virtual bool checkFlags(uint32_t flags) const = 0 ; // this pure virtual forces people to explicitly declare if they use a public or a private key.
virtual uint32_t TlvSize() const; virtual uint32_t TlvSize() const;
virtual void TlvClear(); virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const; virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const; virtual std::ostream& print(std::ostream &out, uint16_t indent) const;
/* clears KeyData - but doesn't delete - to transfer ownership */ /* clears KeyData - but doesn't delete - to transfer ownership */
void ShallowClear(); void ShallowClear();
@ -100,24 +78,16 @@ public:
class RsTlvPrivateRSAKey: public RsTlvRSAKey class RsTlvPrivateRSAKey: public RsTlvRSAKey
{ {
public: public:
RsTlvPrivateRSAKey();
explicit RsTlvPrivateRSAKey(RsTlvSecurityKey_deprecated& key); // convertion tool. Should be called explicitely and performs some checks
virtual ~RsTlvPrivateRSAKey() {} virtual ~RsTlvPrivateRSAKey() {}
virtual bool checkFlags(uint32_t flags) const { return bool(flags & RSTLV_KEY_TYPE_FULL) && !bool(flags & RSTLV_KEY_TYPE_PUBLIC_ONLY) ; } virtual bool checkFlags(uint32_t flags) const { return bool(flags & RSTLV_KEY_TYPE_FULL) && !bool(flags & RSTLV_KEY_TYPE_PUBLIC_ONLY) ; }
virtual uint32_t tlvType() const { return TLV_TYPE_RSA_KEY_PRIVATE ; }
}; };
class RsTlvPublicRSAKey: public RsTlvRSAKey class RsTlvPublicRSAKey: public RsTlvRSAKey
{ {
#warning TEST IF WE CAN CONVERT PUBLIC TO PRIVATE AND VIS VERSA. NORMALLY WE SHOULDNT
public: public:
RsTlvPublicRSAKey();
virtual ~RsTlvPublicRSAKey() {} virtual ~RsTlvPublicRSAKey() {}
explicit RsTlvPublicRSAKey(RsTlvSecurityKey_deprecated& key); // convertion tool. Should be called explicitely and performs some checks
virtual bool checkFlags(uint32_t flags) const { return bool(flags & RSTLV_KEY_TYPE_PUBLIC_ONLY) && !bool(flags & RSTLV_KEY_TYPE_FULL) ; } virtual bool checkFlags(uint32_t flags) const { return bool(flags & RSTLV_KEY_TYPE_PUBLIC_ONLY) && !bool(flags & RSTLV_KEY_TYPE_FULL) ; }
virtual uint32_t tlvType() const { return TLV_TYPE_RSA_KEY_PUBLIC ; }
}; };
class RsTlvSecurityKeySet: public RsTlvItem class RsTlvSecurityKeySet: public RsTlvItem
@ -126,7 +96,9 @@ public:
RsTlvSecurityKeySet() { return; } RsTlvSecurityKeySet() { return; }
virtual ~RsTlvSecurityKeySet() { return; } virtual ~RsTlvSecurityKeySet() { return; }
void initFromPublicKeys(const RsTlvSecurityKeySet& skset) ; // creates the public keys that are possible missing although the private keys are present.
void createPublicFromPrivateKeys();
virtual uint32_t TlvSize() const; virtual uint32_t TlvSize() const;
virtual void TlvClear(); virtual void TlvClear();
@ -137,10 +109,6 @@ public:
std::string groupId; // Mandatory : std::string groupId; // Mandatory :
std::map<RsGxsId, RsTlvPublicRSAKey> public_keys; // Mandatory : std::map<RsGxsId, RsTlvPublicRSAKey> public_keys; // Mandatory :
std::map<RsGxsId, RsTlvPrivateRSAKey> private_keys; // Mandatory : std::map<RsGxsId, RsTlvPrivateRSAKey> private_keys; // Mandatory :
//private:
// RsTlvSecurityKeySet& operator=(const RsTlvSecurityKeySet&) { std::cerr << "(EE) " <<__PRETTY_FUNCTION__ << " shouldn't be called!" << std::endl; return *this;}
// RsTlvSecurityKeySet(const RsTlvSecurityKeySet&) { std::cerr << "(EE) " <<__PRETTY_FUNCTION__ << " shouldn't be called!" << std::endl;}
}; };

View File

@ -147,8 +147,7 @@ RsIdentity *rsIdentity = NULL;
p3IdService::p3IdService(RsGeneralDataService *gds, RsNetworkExchangeService *nes, PgpAuxUtils *pgpUtils) p3IdService::p3IdService(RsGeneralDataService *gds, RsNetworkExchangeService *nes, PgpAuxUtils *pgpUtils)
: RsGxsIdExchange(gds, nes, new RsGxsIdSerialiser(), RS_SERVICE_GXS_TYPE_GXSID, idAuthenPolicy()), : RsGxsIdExchange(gds, nes, new RsGxsIdSerialiser(), RS_SERVICE_GXS_TYPE_GXSID, idAuthenPolicy()),
RsIdentity(this), GxsTokenQueue(this), RsTickEvent(), RsIdentity(this), GxsTokenQueue(this), RsTickEvent(),
mPublicKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdPublicKeyCache"), mKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdKeyCache"),
mPrivateKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdPrivateKeyCache"),
mIdMtx("p3IdService"), mNes(nes), mIdMtx("p3IdService"), mNes(nes),
mPgpUtils(pgpUtils) mPgpUtils(pgpUtils)
{ {
@ -445,49 +444,31 @@ bool p3IdService:: getIdDetails(const RsGxsId &id, RsIdentityDetails &details)
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
{
RsGxsIdCache<RsTlvPublicRSAKey> data;
if (mPublicKeyCache.fetch(id, data))
{
details = data.details;
details.mLastUsageTS = locked_getLastUsageTS(id) ;
if(mContacts.find(id) != mContacts.end())
details.mFlags |= RS_IDENTITY_FLAGS_IS_A_CONTACT ;
// one utf8 symbol can be at most 4 bytes long - would be better to measure real unicode length !!! RsGxsIdCache data;
if(details.mNickname.length() > RSID_MAXIMUM_NICKNAME_SIZE*4)
details.mNickname = "[too long a name]" ;
rsReputations->getReputationInfo(id,details.mReputation) ;
return true; if (mKeyCache.fetch(id, data))
} {
} details = data.details;
details.mLastUsageTS = locked_getLastUsageTS(id) ;
RsGxsIdCache<RsTlvPrivateRSAKey> data; if(mContacts.find(id) != mContacts.end())
details.mFlags |= RS_IDENTITY_FLAGS_IS_A_CONTACT ;
/* try private cache too */
if (mPrivateKeyCache.fetch(id, data))
{
details = data.details;
details.mLastUsageTS = locked_getLastUsageTS(id) ;
if(mContacts.find(id) != mContacts.end())
details.mFlags |= RS_IDENTITY_FLAGS_IS_A_CONTACT ;
rsReputations->getReputationInfo(id,details.mReputation) ; // one utf8 symbol can be at most 4 bytes long - would be better to measure real unicode length !!!
if(details.mNickname.length() > RSID_MAXIMUM_NICKNAME_SIZE*4)
return true; details.mNickname = "[too long a name]" ;
}
} rsReputations->getReputationInfo(id,details.mReputation) ;
return true;
}
}
/* it isn't there - add to public requests */ /* it isn't there - add to public requests */
cache_request_load(id); cache_request_load(id);
return false; return false;
} }
@ -598,36 +579,24 @@ bool p3IdService::getRecognTagRequest(const RsGxsId &id, const std::string &comm
std::cerr << "p3IdService::getRecognTagRequest()"; std::cerr << "p3IdService::getRecognTagRequest()";
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
if(!isOwnId(id))
if (!havePrivateKey(id))
{ {
#ifdef DEBUG_RECOGN std::cerr << "(EE) cannot retrieve own key to create tag request. KeyId=" << id << std::endl;
std::cerr << "p3IdService::getRecognTagRequest() Dont have private key"; return false ;
std::cerr << std::endl;
#endif
// attempt to load it.
cache_request_load(id);
return false;
} }
RsTlvPrivateRSAKey key; RsTlvPrivateRSAKey key;
std::string nickname; std::string nickname;
RsGxsIdCache data ;
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RsGxsIdCache<RsTlvPrivateRSAKey> data;
if (!mPrivateKeyCache.fetch(id, data))
{
#ifdef DEBUG_RECOGN
std::cerr << "p3IdService::getRecognTagRequest() Cache failure";
std::cerr << std::endl;
#endif
return false;
}
key = data.pubkey; if(!mKeyCache.fetch(id, data))
nickname = data.details.mNickname; return false ;
nickname = data.details.mNickname ;
key = data.priv_key ;
} }
return RsRecogn::createTagRequest(key, id, nickname, tag_class, tag_type, comment, tag); return RsRecogn::createTagRequest(key, id, nickname, tag_class, tag_type, comment, tag);
@ -642,13 +611,16 @@ bool p3IdService::getRecognTagRequest(const RsGxsId &id, const std::string &comm
bool p3IdService::haveKey(const RsGxsId &id) bool p3IdService::haveKey(const RsGxsId &id)
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
return mPublicKeyCache.is_cached(id); return mKeyCache.is_cached(id);
} }
bool p3IdService::havePrivateKey(const RsGxsId &id) bool p3IdService::havePrivateKey(const RsGxsId &id)
{ {
if(! isOwnId(id))
return false ;
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
return mPrivateKeyCache.is_cached(id); return mKeyCache.is_cached(id) ;
} }
bool p3IdService::requestKey(const RsGxsId &id, const std::list<PeerId> &peers) bool p3IdService::requestKey(const RsGxsId &id, const std::list<PeerId> &peers)
@ -678,21 +650,21 @@ bool p3IdService::isPendingNetworkRequest(const RsGxsId& gxsId)
bool p3IdService::getKey(const RsGxsId &id, RsTlvPublicRSAKey &key) bool p3IdService::getKey(const RsGxsId &id, RsTlvPublicRSAKey &key)
{ {
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RsGxsIdCache<RsTlvPublicRSAKey> data;
if (mPublicKeyCache.fetch(id, data))
{ {
key = data.pubkey; RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
return true; RsGxsIdCache data;
}
}
cache_request_load(id); if (mKeyCache.fetch(id, data))
{
key = data.pub_key;
return true;
}
}
key.keyId.clear() ; cache_request_load(id);
return false;
key.keyId.clear() ;
return false;
} }
bool p3IdService::requestPrivateKey(const RsGxsId &id) bool p3IdService::requestPrivateKey(const RsGxsId &id)
@ -705,21 +677,21 @@ bool p3IdService::requestPrivateKey(const RsGxsId &id)
bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey &key) bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey &key)
{ {
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RsGxsIdCache<RsTlvPrivateRSAKey> data; RsGxsIdCache data;
if (mPrivateKeyCache.fetch(id, data))
{
key = data.pubkey;
return true;
}
}
key.keyId.clear() ; if (mKeyCache.fetch(id, data))
cache_request_load(id); {
key = data.priv_key;
return true;
}
}
return false ; key.keyId.clear() ;
cache_request_load(id);
return false ;
} }
@ -888,9 +860,9 @@ bool p3IdService::getReputation(const RsGxsId &id, GixsReputation &rep)
/* this is the key part for accepting messages */ /* this is the key part for accepting messages */
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RsGxsIdCache<RsTlvPublicRSAKey> data; RsGxsIdCache data;
if (mPublicKeyCache.fetch(id, data)) if (mKeyCache.fetch(id, data))
{ {
rep.id = id; rep.id = id;
rep.score = 0;//data.details.mReputation.mOverallScore; rep.score = 0;//data.details.mReputation.mOverallScore;
@ -1155,24 +1127,24 @@ bool p3IdService::createGroup(uint32_t& token, RsGxsIdGroup &group)
return true; return true;
} }
bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group) bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group)
{ {
RsGxsId id(group.mMeta.mGroupId); RsGxsId id(group.mMeta.mGroupId);
RsGxsIdGroupItem* item = new RsGxsIdGroupItem(); RsGxsIdGroupItem* item = new RsGxsIdGroupItem();
item->fromGxsIdGroup(group,false) ; item->fromGxsIdGroup(group,false) ;
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::updateGroup() Updating RsGxsId: " << id; std::cerr << "p3IdService::updateGroup() Updating RsGxsId: " << id;
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
RsGenExchange::updateGroup(token, item); RsGenExchange::updateGroup(token, item);
// if its in the cache - clear it. // if its in the cache - clear it.
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
if (mPublicKeyCache.erase(id)) if (mKeyCache.erase(id))
{ {
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::updateGroup() Removed from PublicKeyCache"; std::cerr << "p3IdService::updateGroup() Removed from PublicKeyCache";
@ -1184,21 +1156,6 @@ bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group)
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::updateGroup() Not in PublicKeyCache"; std::cerr << "p3IdService::updateGroup() Not in PublicKeyCache";
std::cerr << std::endl; std::cerr << std::endl;
#endif
}
if (mPrivateKeyCache.erase(id))
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::updateGroup() Removed from PrivateKeyCache";
std::cerr << std::endl;
#endif
}
else
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::updateGroup() Not in PrivateKeyCache";
std::cerr << std::endl;
#endif #endif
} }
} }
@ -1223,7 +1180,7 @@ bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group)
// if its in the cache - clear it. // if its in the cache - clear it.
{ {
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
if (mPublicKeyCache.erase(id)) if (mKeyCache.erase(id))
{ {
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Removed from PublicKeyCache"; std::cerr << "p3IdService::deleteGroup() Removed from PublicKeyCache";
@ -1238,21 +1195,6 @@ bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group)
#endif #endif
} }
if (mPrivateKeyCache.erase(id))
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Removed from PrivateKeyCache";
std::cerr << std::endl;
#endif
}
else
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Not in PrivateKeyCache";
std::cerr << std::endl;
#endif
}
std::list<RsGxsId>::iterator lit = std::find( mOwnIds.begin(), mOwnIds.end(), id); std::list<RsGxsId>::iterator lit = std::find( mOwnIds.begin(), mOwnIds.end(), id);
if (lit != mOwnIds.end()) if (lit != mOwnIds.end())
{ {
@ -1642,6 +1584,59 @@ std::string SSGxsIdGroup::save() const
* *
*/ */
RsGxsIdCache::RsGxsIdCache() {}
RsGxsIdCache::RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const std::list<RsRecognTag> &tagList)
{
init(item,in_pkey,RsTlvPrivateRSAKey(),tagList) ;
}
RsGxsIdCache::RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const RsTlvPrivateRSAKey& privkey, const std::list<RsRecognTag> &tagList)
{
init(item,in_pkey,privkey,tagList) ;
}
void RsGxsIdCache::init(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pub_key, const RsTlvPrivateRSAKey& in_priv_key,const std::list<RsRecognTag> &tagList)
{
// Save Keys.
pub_key = in_pub_key;
priv_key = in_priv_key;
// Save Time for ServiceString comparisions.
mPublishTs = item->meta.mPublishTs;
// Save RecognTags.
mRecognTags = tagList;
details.mAvatar.copy((uint8_t *) item->mImage.binData.bin_data, item->mImage.binData.bin_len);
// Fill in Details.
details.mNickname = item->meta.mGroupName;
details.mId = RsGxsId(item->meta.mGroupId);
#ifdef DEBUG_IDS
std::cerr << "RsGxsIdCache::RsGxsIdCache() for: " << details.mId;
std::cerr << std::endl;
#endif // DEBUG_IDS
details.mFlags = 0 ;
if(item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) details.mFlags |= RS_IDENTITY_FLAGS_IS_OWN_ID;
if(item->meta.mGroupFlags & RSGXSID_GROUPFLAG_REALID) details.mFlags |= RS_IDENTITY_FLAGS_PGP_LINKED;
// do some tests
if(details.mFlags & RS_IDENTITY_FLAGS_IS_OWN_ID)
{
if(!priv_key.check())
std::cerr << "(EE) Private key missing for own identity " << pub_key.keyId << std::endl;
}
if(!pub_key.check())
std::cerr << "(EE) Public key missing for identity " << pub_key.keyId << std::endl;
/* rest must be retrived from ServiceString */
updateServiceString(item->meta.mServiceString);
}
void RsGxsIdCache::updateServiceString(std::string serviceString) void RsGxsIdCache::updateServiceString(std::string serviceString)
{ {
details.mRecognTags.clear(); details.mRecognTags.clear();
@ -1853,6 +1848,7 @@ bool p3IdService::cache_process_recogntaginfo(const RsGxsIdGroupItem *item, std:
return true; return true;
} }
// Loads in the cache the group data from the given group item, retrieved from sqlite storage.
bool p3IdService::cache_store(const RsGxsIdGroupItem *item) bool p3IdService::cache_store(const RsGxsIdGroupItem *item)
{ {
@ -1865,56 +1861,42 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item)
/* extract key from keys */ /* extract key from keys */
RsTlvSecurityKeySet keySet; RsTlvSecurityKeySet keySet;
RsTlvSecurityKey_deprecated pubkey;
RsTlvSecurityKey_deprecated fullkey; RsTlvPublicRSAKey pubkey;
RsTlvPrivateRSAKey fullkey;
bool pub_key_ok = false; bool pub_key_ok = false;
bool full_key_ok = false; bool full_key_ok = false;
RsGxsId id (item->meta.mGroupId.toStdString()); RsGxsId id (item->meta.mGroupId.toStdString());
if (!getGroupKeys(RsGxsGroupId(id.toStdString()), keySet)) if (!getGroupKeys(RsGxsGroupId(id.toStdString()), keySet))
{ {
std::cerr << "p3IdService::cache_store() ERROR getting GroupKeys for: "; std::cerr << "p3IdService::cache_store() ERROR getting GroupKeys for: "<< item->meta.mGroupId << std::endl;
std::cerr << item->meta.mGroupId;
std::cerr << std::endl;
return false; return false;
} }
std::map<RsGxsId, RsTlvSecurityKey_deprecated>::iterator kit; for (std::map<RsGxsId, RsTlvPrivateRSAKey>::iterator kit = keySet.private_keys.begin(); kit != keySet.private_keys.end(); ++kit)
//std::cerr << "p3IdService::cache_store() KeySet is:";
//keySet.print(std::cerr, 10);
for (kit = keySet.keys.begin(); kit != keySet.keys.end(); ++kit)
{
if (kit->second.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) if (kit->second.keyFlags & RSTLV_KEY_DISTRIB_ADMIN)
{ {
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::cache_store() Found Admin Key"; std::cerr << "p3IdService::cache_store() Found Admin Key" << std::endl;
std::cerr << std::endl; #endif
#endif // DEBUG_IDS fullkey = kit->second;
full_key_ok = true;
/* save full key - if we have it */
if (kit->second.keyFlags & RSTLV_KEY_TYPE_FULL)
{
fullkey = kit->second;
full_key_ok = true;
if(GxsSecurity::extractPublicKey(fullkey,pubkey))
pub_key_ok = true ;
}
else
{
pubkey = kit->second;
pub_key_ok = true ;
}
/* cache public key always
* we don't need to check the keyFlags,
* as both FULL and PUBLIC_ONLY keys contain the PUBLIC key
*/
} }
} for (std::map<RsGxsId, RsTlvPublicRSAKey>::iterator kit = keySet.public_keys.begin(); kit != keySet.public_keys.end(); ++kit)
if (kit->second.keyFlags & RSTLV_KEY_DISTRIB_ADMIN)
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::cache_store() Found Admin public Key" << std::endl;
#endif
pubkey = kit->second;
pub_key_ok = true ;
}
assert(!( pubkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ;
assert(!full_key_ok || (fullkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ;
if (!pub_key_ok) if (!pub_key_ok)
{ {
@ -1929,19 +1911,11 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item)
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
assert(!(pubkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ;
// Create Cache Data. // Create Cache Data.
RsGxsIdCache pubcache(item, pubkey, tagList); RsGxsIdCache keycache(item, pubkey, fullkey,tagList);
mPublicKeyCache.store(id, pubcache);
mPublicKeyCache.resize(); mKeyCache.store(id, keycache);
mKeyCache.resize();
if (full_key_ok)
{
RsGxsIdCache fullcache(item, fullkey, tagList);
mPrivateKeyCache.store(id, fullcache);
mPrivateKeyCache.resize();
}
return true; return true;
} }
@ -2145,31 +2119,18 @@ bool p3IdService::cache_update_if_cached(const RsGxsId &id, std::string serviceS
/* retrieve - update, save */ /* retrieve - update, save */
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RsGxsIdCache pub_data; RsGxsIdCache updated_data;
if (mPublicKeyCache.fetch(id, pub_data))
if(mKeyCache.fetch(id, updated_data))
{ {
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
std::cerr << "p3IdService::cache_update_if_cached() Updating Public Cache"; std::cerr << "p3IdService::cache_update_if_cached() Updating Public Cache";
std::cerr << std::endl; std::cerr << std::endl;
#endif // DEBUG_IDS #endif // DEBUG_IDS
assert(!(pub_data.pubkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ; updated_data.updateServiceString(serviceString);
pub_data.updateServiceString(serviceString); mKeyCache.store(id, updated_data);
mPublicKeyCache.store(id, pub_data);
}
RsGxsIdCache priv_data;
if (mPrivateKeyCache.fetch(id, priv_data))
{
#ifdef DEBUG_IDS
std::cerr << "p3IdService::cache_update_if_cached() Updating Private Cache";
std::cerr << std::endl;
#endif // DEBUG_IDS
priv_data.updateServiceString(serviceString);
mPrivateKeyCache.store(id, priv_data);
} }
return true; return true;
@ -2338,7 +2299,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token)
} }
else else
{ {
RsTlvSecurityKey_deprecated seckey; RsTlvPublicRSAKey seckey;
if (getKey(*vit, seckey)) if (getKey(*vit, seckey))
{ {
#ifdef DEBUG_IDS #ifdef DEBUG_IDS
@ -2370,7 +2331,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token)
} }
else else
{ {
RsTlvSecurityKey_deprecated seckey; RsTlvPrivateRSAKey seckey;
if (getPrivateKey(*vit, seckey)) if (getPrivateKey(*vit, seckey))
{ {
// success! // success!
@ -2509,22 +2470,19 @@ RsGenExchange::ServiceCreate_Return p3IdService::service_CreateGroup(RsGxsGrpIte
item->print(std::cerr); item->print(std::cerr);
std::cerr << std::endl; std::cerr << std::endl;
#endif // DEBUG_IDS #endif // DEBUG_IDS
item->meta.mGroupId.clear();
/********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/ /********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/
// find private admin key // find private admin key
std::map<RsGxsId, RsTlvSecurityKey_deprecated>::iterator mit = keySet.keys.begin(); for(std::map<RsGxsId, RsTlvPrivateRSAKey>::iterator mit = keySet.private_keys.begin();mit != keySet.private_keys.end(); ++mit)
for(; mit != keySet.keys.end(); ++mit) if(mit->second.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
{
RsTlvSecurityKey_deprecated& pk = mit->second;
if(pk.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
{ {
item->meta.mGroupId = RsGxsGroupId(pk.keyId); item->meta.mGroupId = RsGxsGroupId(mit->second.keyId);
break; break;
} }
}
if(mit == keySet.keys.end()) if(item->meta.mGroupId.isNull())
{ {
std::cerr << "p3IdService::service_CreateGroup() ERROR no admin key"; std::cerr << "p3IdService::service_CreateGroup() ERROR no admin key";
std::cerr << std::endl; std::cerr << std::endl;

View File

@ -190,40 +190,13 @@ virtual std::string save() const;
class RsGxsIdGroupItem; class RsGxsIdGroupItem;
template<class KeyClass> class RsGxsIdCache class RsGxsIdCache
{ {
public: public:
RsGxsIdCache(); RsGxsIdCache();
RsGxsIdCache(const RsGxsIdGroupItem *item, const KeyClass& in_pkey, const std::list<RsRecognTag> &tagList) RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const RsTlvPrivateRSAKey& privkey, const std::list<RsRecognTag> &tagList);
{ RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const std::list<RsRecognTag> &tagList);
// Save Keys.
pubkey = in_pkey;
// Save Time for ServiceString comparisions.
mPublishTs = item->meta.mPublishTs;
// Save RecognTags.
mRecognTags = tagList;
details.mAvatar.copy((uint8_t *) item->mImage.binData.bin_data, item->mImage.binData.bin_len);
// Fill in Details.
details.mNickname = item->meta.mGroupName;
details.mId = RsGxsId(item->meta.mGroupId);
#ifdef DEBUG_IDS
std::cerr << "RsGxsIdCache::RsGxsIdCache() for: " << details.mId;
std::cerr << std::endl;
#endif // DEBUG_IDS
details.mFlags = 0 ;
if(item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) details.mFlags |= RS_IDENTITY_FLAGS_IS_OWN_ID;
if(item->meta.mGroupFlags & RSGXSID_GROUPFLAG_REALID) details.mFlags |= RS_IDENTITY_FLAGS_PGP_LINKED;
/* rest must be retrived from ServiceString */
updateServiceString(item->meta.mServiceString);
}
void updateServiceString(std::string serviceString); void updateServiceString(std::string serviceString);
@ -231,8 +204,12 @@ public:
std::list<RsRecognTag> mRecognTags; // Only partially validated. std::list<RsRecognTag> mRecognTags; // Only partially validated.
RsIdentityDetails details; RsIdentityDetails details;
#warning why the "pub" here??
KeyClass pubkey; RsTlvPublicRSAKey pub_key;
RsTlvPrivateRSAKey priv_key;
private:
void init(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pub_key, const RsTlvPrivateRSAKey& in_priv_key,const std::list<RsRecognTag> &tagList);
}; };
@ -380,8 +357,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
std::map<RsGxsId, std::list<RsPeerId> > mCacheLoad_ToCache, mPendingCache; std::map<RsGxsId, std::list<RsPeerId> > mCacheLoad_ToCache, mPendingCache;
// Switching to RsMemCache for Key Caching. // Switching to RsMemCache for Key Caching.
RsMemCache<RsGxsId, RsGxsIdCache<RsTlvPublicRSAKey> > mPublicKeyCache; RsMemCache<RsGxsId, RsGxsIdCache> mKeyCache;
RsMemCache<RsGxsId, RsGxsIdCache<RsTlvPrivateRSAKey> > mPrivateKeyCache;
/************************************************************************ /************************************************************************
* Refreshing own Ids. * Refreshing own Ids.

View File

@ -143,7 +143,7 @@ template<class Key, class Value> template<class ClientClass> bool RsMemCache<Key
return res ; return res ;
} }
template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &key, Value &data) template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &key, Value& data)
{ {
#ifdef DEBUG_RSMEMCACHE #ifdef DEBUG_RSMEMCACHE
std::cerr << "RsMemCache::fetch()"; std::cerr << "RsMemCache::fetch()";