From 590be092e5b5533cb5bd6f717c4c29d5a2387cac Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 2 Jun 2016 23:47:57 -0400 Subject: [PATCH 01/13] separated RsTlvSecurityKey into two incompatible classes to enforce the correct usage of private vs. public keys --- libretroshare/src/chat/rschatitems.h | 2 +- libretroshare/src/grouter/p3grouter.cc | 2 +- libretroshare/src/gxs/gxssecurity.cc | 62 +++-- libretroshare/src/gxs/gxssecurity.h | 24 +- libretroshare/src/gxs/rsgenexchange.cc | 201 +++++++------- libretroshare/src/gxs/rsgenexchange.h | 9 +- libretroshare/src/gxs/rsgixs.h | 4 +- libretroshare/src/gxs/rsgxsnetservice.cc | 94 +++---- libretroshare/src/gxs/rsgxsnetservice.h | 2 +- libretroshare/src/gxstunnel/p3gxstunnel.cc | 8 +- .../src/gxstunnel/rsgxstunnelitems.h | 2 +- .../src/serialiser/rsgxsrecognitems.h | 2 +- libretroshare/src/serialiser/rsnxsitems.cc | 2 +- libretroshare/src/serialiser/rsnxsitems.h | 8 +- libretroshare/src/serialiser/rstlvbase.h | 6 +- libretroshare/src/serialiser/rstlvkeys.cc | 256 ++++++++++++++---- libretroshare/src/serialiser/rstlvkeys.h | 88 +++++- libretroshare/src/services/p3idservice.cc | 87 ++---- libretroshare/src/services/p3idservice.h | 58 +++- libretroshare/src/util/rsrecogn.cc | 6 +- libretroshare/src/util/rsrecogn.h | 2 +- .../gxs/gen_exchange/rsdummyservices.h | 4 +- .../gxs/security/gxssecurity_test.cc | 6 +- .../libretroshare/serialiser/support.cc | 8 +- .../libretroshare/serialiser/support.h | 4 +- .../serialiser/tlvrandom_test.cc | 4 +- 26 files changed, 600 insertions(+), 351 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.h b/libretroshare/src/chat/rschatitems.h index 8a21cb404..ab75f8cec 100644 --- a/libretroshare/src/chat/rschatitems.h +++ b/libretroshare/src/chat/rschatitems.h @@ -406,7 +406,7 @@ class RsChatDHPublicKeyItem: public RsChatItem BIGNUM *public_key ; RsTlvKeySignature signature ; // signs the public key in a row. - RsTlvSecurityKey gxs_key ; // public key of the signer + RsTlvSecurityKey_deprecated gxs_key ; // public key of the signer private: RsChatDHPublicKeyItem(const RsChatDHPublicKeyItem&) : RsChatItem(RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY) {} // make the object non copy-able diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 306f72d74..9d72bca49 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -1958,7 +1958,7 @@ bool p3GRouter::signDataItem(RsGRouterAbstractMsgItem *item,const RsGxsId& signi { try { - RsTlvSecurityKey signature_key ; + RsTlvSecurityKey_deprecated signature_key ; #ifdef GROUTER_DEBUG std::cerr << "p3GRouter::signDataItem()" << std::endl; diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 7440a9b40..5042267ef 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -59,7 +59,7 @@ static RsGxsId getRsaKeyFingerprint(RSA *pubkey) return RsGxsId(s.toStdString().substr(0,2*CERTSIGNLEN)); } -static RSA *extractPrivateKey(const RsTlvSecurityKey & key) +static RSA *extractPrivateKey(const RsTlvPrivateRSAKey& key) { assert(key.keyFlags & RSTLV_KEY_TYPE_FULL) ; @@ -72,7 +72,7 @@ static RSA *extractPrivateKey(const RsTlvSecurityKey & key) return rsakey; } -static RSA *extractPublicKey(const RsTlvSecurityKey& key) +static RSA *extractPublicKey(const RsTlvPublicRSAKey& key) { assert(!(key.keyFlags & RSTLV_KEY_TYPE_FULL)) ; @@ -84,18 +84,30 @@ static RSA *extractPublicKey(const RsTlvSecurityKey& key) return rsakey; } -static void setRSAPublicKeyData(RsTlvSecurityKey & key, RSA *rsa_pub) +static void setRSAPublicKeyData(RsTlvPublicRSAKey& key, RSA *rsa_pub) { + assert(!(key.keyFlags & RSTLV_KEY_TYPE_FULL)) ; unsigned char *data = NULL ; // this works for OpenSSL > 0.9.7 int reqspace = i2d_RSAPublicKey(rsa_pub, &data); key.keyData.setBinData(data, reqspace); key.keyId = getRsaKeyFingerprint(rsa_pub); - free(data) ; + free(data) ; } -bool GxsSecurity::checkPrivateKey(const RsTlvSecurityKey& key) +static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_pub) +{ + assert(key.keyFlags & RSTLV_KEY_TYPE_FULL) ; + unsigned char *data = NULL ; // this works for OpenSSL > 0.9.7 + int reqspace = i2d_RSAPublicKey(rsa_pub, &data); + + key.keyData.setBinData(data, reqspace); + key.keyId = getRsaKeyFingerprint(rsa_pub); + + free(data) ; +} +bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) { #ifdef GXS_SECURITY_DEBUG std::cerr << "Checking private key " << key.keyId << " ..." << std::endl; @@ -132,7 +144,7 @@ bool GxsSecurity::checkPrivateKey(const RsTlvSecurityKey& key) return true ; } -bool GxsSecurity::checkPublicKey(const RsTlvSecurityKey& key) +bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) { #ifdef GXS_SECURITY_DEBUG std::cerr << "Checking public key " << key.keyId << " ..." << std::endl; @@ -162,7 +174,7 @@ bool GxsSecurity::checkPublicKey(const RsTlvSecurityKey& key) return true ; } -static void setRSAPrivateKeyData(RsTlvSecurityKey & key, RSA *rsa_priv) +static void setRSAPrivateKeyData(RsTlvSecurityKey_deprecated & key, RSA *rsa_priv) { unsigned char *data = NULL ; int reqspace = i2d_RSAPrivateKey(rsa_priv, &data); @@ -173,7 +185,7 @@ static void setRSAPrivateKeyData(RsTlvSecurityKey & key, RSA *rsa_priv) free(data) ; } -bool GxsSecurity::generateKeyPair(RsTlvSecurityKey& public_key,RsTlvSecurityKey& private_key) +bool GxsSecurity::generateKeyPair(RsTlvPublicRSAKey& public_key,RsTlvPrivateRSAKey& private_key) { // admin keys RSA *rsa = RSA_generate_key(2048, 65537, NULL, NULL); @@ -194,10 +206,16 @@ bool GxsSecurity::generateKeyPair(RsTlvSecurityKey& public_key,RsTlvSecurityKey& RSA_free(rsa); RSA_free(rsa_pub); - return true ; + if(!(private_key.check() && public_key.check())) + { + std::cerr << "(EE) ERROR while generating keys. Something inconsistent in flags. This is probably a bad sign!" << std::endl; + return false ; + } + + return true ; } -bool GxsSecurity::extractPublicKey(const RsTlvSecurityKey& private_key,RsTlvSecurityKey& public_key) +bool GxsSecurity::extractPublicKey(const RsTlvPrivateRSAKey &private_key, RsTlvPublicRSAKey &public_key) { public_key.TlvClear() ; @@ -241,7 +259,7 @@ bool GxsSecurity::extractPublicKey(const RsTlvSecurityKey& private_key,RsTlvSecu return true ; } -bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& privKey, RsTlvKeySignature& sign) +bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvPrivateRSAKey &privKey, RsTlvKeySignature& sign) { RSA* rsa_pub = extractPrivateKey(privKey); @@ -272,12 +290,11 @@ bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvS return ok; } -bool GxsSecurity::validateSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& key, const RsTlvKeySignature& signature) +bool GxsSecurity::validateSignature(const char *data, uint32_t data_len, const RsTlvPublicRSAKey &key, const RsTlvKeySignature& signature) { - RSA *tmpkey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)?(::extractPrivateKey(key)):(::extractPublicKey(key)) ; - - RSA *rsakey = RSAPublicKey_dup(tmpkey) ; // always extract public key - RSA_free(tmpkey) ; + assert(!(key.keyFlags & RSTLV_KEY_TYPE_FULL)) ; + + RSA *rsakey = ::extractPublicKey(key) ; if(!rsakey) { @@ -303,7 +320,7 @@ bool GxsSecurity::validateSignature(const char *data, uint32_t data_len, const R return signOk; } -bool GxsSecurity::validateNxsMsg(const RsNxsMsg& msg, const RsTlvKeySignature& sign, const RsTlvSecurityKey& key) +bool GxsSecurity::validateNxsMsg(const RsNxsMsg& msg, const RsTlvKeySignature& sign, const RsTlvPublicRSAKey& key) { #ifdef GXS_SECURITY_DEBUG std::cerr << "GxsSecurity::validateNxsMsg()"; @@ -417,7 +434,7 @@ bool GxsSecurity::validateNxsMsg(const RsNxsMsg& msg, const RsTlvKeySignature& s return false; } -bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvSecurityKey& key) +bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvPublicRSAKey& key) { #ifdef DISTRIB_DEBUG std::cerr << "GxsSecurity::encrypt() " << std::endl; @@ -532,7 +549,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u return true; } -bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys) +bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const std::vector &keys) { #ifdef DISTRIB_DEBUG std::cerr << "GxsSecurity::encrypt() " << std::endl; @@ -685,7 +702,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u } } -bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const RsTlvSecurityKey& key) +bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const RsTlvPrivateRSAKey &key) { // Decrypts (in,inlen) into (out,outlen) using the given RSA public key. // The format of the encrypted data (in) is: @@ -791,7 +808,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, return true; } -bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys) +bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const std::vector &keys) { // Decrypts (in,inlen) into (out,outlen) using one of the given RSA public keys, trying them all in a row. // The format of the encrypted data is: @@ -936,7 +953,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, return false; } } -bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& sign, const RsTlvSecurityKey& key) +bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& sign, const RsTlvPublicRSAKey &key) { #ifdef GXS_SECURITY_DEBUG std::cerr << "GxsSecurity::validateNxsGrp()"; @@ -974,6 +991,7 @@ bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& s #endif /* extract admin key */ +#warning Souldn't need to do that HERE!! RSA *rsakey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)? d2i_RSAPrivateKey(NULL, &(keyptr), keylen): d2i_RSAPublicKey(NULL, &(keyptr), keylen); if (!rsakey) diff --git a/libretroshare/src/gxs/gxssecurity.h b/libretroshare/src/gxs/gxssecurity.h index 22ac03b98..4642cc512 100644 --- a/libretroshare/src/gxs/gxssecurity.h +++ b/libretroshare/src/gxs/gxssecurity.h @@ -45,7 +45,7 @@ class GxsSecurity /*! * Extracts a public key from a private key. */ - static bool extractPublicKey(const RsTlvSecurityKey& private_key,RsTlvSecurityKey& public_key) ; + static bool extractPublicKey(const RsTlvPrivateRSAKey& private_key,RsTlvPublicRSAKey& public_key) ; /*! * Generates a public/private RSA keypair. To be used for all GXS purposes. @@ -53,7 +53,7 @@ class GxsSecurity * @param RsTlvSecurityKey private RSA key * @return true if the generate was successful, false otherwise. */ - static bool generateKeyPair(RsTlvSecurityKey& public_key,RsTlvSecurityKey& private_key) ; + static bool generateKeyPair(RsTlvPublicRSAKey &public_key, RsTlvPrivateRSAKey &private_key) ; /*! * Encrypts data using envelope encryption (taken from open ssl's evp_sealinit ) @@ -63,8 +63,8 @@ class GxsSecurity *@param in *@param inlen */ - static bool encrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvSecurityKey& key) ; - static bool encrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys) ; + static bool encrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvPublicRSAKey& key) ; + static bool encrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys) ; /** * Decrypts data using evelope decryption (taken from open ssl's evp_sealinit ) @@ -75,8 +75,8 @@ class GxsSecurity * @param inlen * @return false if encryption failed */ - static bool decrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvSecurityKey& key) ; - static bool decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys); + static bool decrypt(uint8_t *&out, uint32_t &outlen, const uint8_t *in, uint32_t inlen, const RsTlvPrivateRSAKey& key) ; + static bool decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, uint32_t inlen, const std::vector& keys); /*! * uses grp signature to check if group has been @@ -86,7 +86,7 @@ class GxsSecurity * @param key the public key to use to check signature * @return true if group valid false otherwise */ - static bool validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& sign, const RsTlvSecurityKey& key); + static bool validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& sign, const RsTlvPublicRSAKey& key); /*! * Validate a msg's signature using the given public key @@ -95,7 +95,7 @@ class GxsSecurity * @param key the public key to use to check signature * @return false if verfication of signature is not passed */ - static bool validateNxsMsg(const RsNxsMsg& msg, const RsTlvKeySignature& sign, const RsTlvSecurityKey& key); + static bool validateNxsMsg(const RsNxsMsg& msg, const RsTlvKeySignature& sign, const RsTlvPublicRSAKey &key); /*! @@ -105,7 +105,7 @@ class GxsSecurity * @param sign the signature is stored here * @return false if signature creation failed, true is signature created */ - static bool getSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& privKey, RsTlvKeySignature& sign); + static bool getSignature(const char *data, uint32_t data_len, const RsTlvPrivateRSAKey& privKey, RsTlvKeySignature& sign); /*! * @param data data that has been signed @@ -114,7 +114,7 @@ class GxsSecurity * @param sign Signature for the data * @return true if signature checks */ - static bool validateSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& pubKey, const RsTlvKeySignature& sign); + static bool validateSignature(const char *data, uint32_t data_len, const RsTlvPublicRSAKey& pubKey, const RsTlvKeySignature& sign); /*! * Checks that the public key has correct fingerprint and correct flags. @@ -123,8 +123,8 @@ class GxsSecurity * @return false if the key is invalid. */ - static bool checkPublicKey(const RsTlvSecurityKey &key); - static bool checkPrivateKey(const RsTlvSecurityKey &key); + static bool checkPublicKey(const RsTlvPublicRSAKey &key); + static bool checkPrivateKey(const RsTlvPrivateRSAKey &key); }; #endif // GXSSECURITY_H diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 2f06786ec..3b512c90d 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -341,47 +341,53 @@ bool RsGenExchange::acknowledgeTokenGrp(const uint32_t& token, RsGxsGroupId& grp return true; } -void RsGenExchange::generateGroupKeys(RsTlvSecurityKeySet& privatekeySet, RsTlvSecurityKeySet& publickeySet, bool genPublishKeys) +void RsGenExchange::generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPublishKeys) { - /* create Keys */ - RsTlvSecurityKey adminKey, privAdminKey; - GxsSecurity::generateKeyPair(adminKey,privAdminKey) ; + /* create Keys */ + RsTlvPublicRSAKey pubAdminKey ; + RsTlvPrivateRSAKey privAdminKey; - // 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; + GxsSecurity::generateKeyPair(pubAdminKey,privAdminKey) ; - publickeySet.keys[adminKey.keyId] = adminKey; - privatekeySet.keys[privAdminKey.keyId] = privAdminKey; + // for now all public + pubAdminKey.keyFlags |= RSTLV_KEY_DISTRIB_ADMIN ; + privAdminKey.keyFlags |= RSTLV_KEY_DISTRIB_ADMIN ; - if(genPublishKeys) + keySet.public_keys[pubAdminKey.keyId] = pubAdminKey; + keySet.private_keys[privAdminKey.keyId] = privAdminKey; + + if(genPublishKeys) + { + /* set publish keys */ + RsTlvPublicRSAKey pubPublishKey ; + RsTlvPrivateRSAKey privPublishKey; + + GxsSecurity::generateKeyPair(pubPublishKey,privPublishKey) ; + + // for now all public + pubPublishKey.keyFlags |= RSTLV_KEY_DISTRIB_PUBLISH ; + privPublishKey.keyFlags |= RSTLV_KEY_DISTRIB_PUBLISH ; + + keySet.public_keys[pubPublishKey.keyId] = pubPublishKey; + keySet.private_keys[privPublishKey.keyId] = privPublishKey; + } +} + +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::const_iterator cit=keySet.private_keys.begin(); cit != keySet.private_keys.end(); ++cit) { - /* set publish keys */ - RsTlvSecurityKey publishKey, privPublishKey; - GxsSecurity::generateKeyPair(publishKey,privPublishKey) ; - - // for now all public - publishKey.keyFlags = RSTLV_KEY_DISTRIB_PUBLISH | RSTLV_KEY_TYPE_PUBLIC_ONLY; - privPublishKey.keyFlags = RSTLV_KEY_DISTRIB_PUBLISH | RSTLV_KEY_TYPE_FULL; - - publickeySet.keys[publishKey.keyId] = publishKey; - privatekeySet.keys[privPublishKey.keyId] = privPublishKey; + RsTlvPublicRSAKey pubkey ; + if(GxsSecurity::extractPublicKey(cit->second,pubkey)) + keySet.public_keys.insert(std::make_pair(pubkey.keyId, pubkey)); } } -void RsGenExchange::generatePublicFromPrivateKeys(const RsTlvSecurityKeySet &privatekeySet, RsTlvSecurityKeySet &publickeySet) -{ - // actually just copy settings of one key except mark its key flags public - - publickeySet = RsTlvSecurityKeySet() ; - RsTlvSecurityKey pubkey ; - - for(std::map::const_iterator cit=privatekeySet.keys.begin(); cit != privatekeySet.keys.end(); ++cit) - if(GxsSecurity::extractPublicKey(cit->second,pubkey)) - publickeySet.keys.insert(std::make_pair(pubkey.keyId, pubkey)); -} - -uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& privateKeySet, RsTlvSecurityKeySet& publicKeySet) +uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& keySet) { #ifdef GEN_EXCH_DEBUG std::cerr << "RsGenExchange::createGroup()"; @@ -393,19 +399,18 @@ uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& privateKe /* add public admin and publish keys to grp */ // find private admin key - RsTlvSecurityKey privAdminKey; - std::map::iterator mit = privateKeySet.keys.begin(); - + RsTlvPrivateRSAKey privAdminKey; bool privKeyFound = false; // private admin key - for(; mit != privateKeySet.keys.end(); ++mit) + + for( std::map::iterator mit = keySet.private_keys.begin(); mit != keySet.private_keys.end(); ++mit) { - RsTlvSecurityKey& key = mit->second; + RsTlvPrivateRSAKey& key = mit->second; if((key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (key.keyFlags & RSTLV_KEY_TYPE_FULL)) { privAdminKey = key; privKeyFound = true; - break ; + break ; } } @@ -417,13 +422,17 @@ uint8_t RsGenExchange::createGroup(RsNxsGrp *grp, RsTlvSecurityKeySet& privateKe return false; } - meta->keys = publicKeySet; // only public keys are included to be transported + // only public keys are included to be transported. The 2nd line below is very important. + + meta->keys = keySet; + meta->keys.private_keys.clear() ; // group is self signing // for the creation of group signature // only public admin and publish keys are present in meta uint32_t metaDataLen = meta->serial_size(RS_GXS_GRP_META_DATA_CURRENT_API_VERSION); uint32_t allGrpDataLen = metaDataLen + grp->grp.bin_len; + char* metaData = new char[metaDataLen]; char* allGrpData = new char[allGrpDataLen]; // msgData + metaData @@ -503,12 +512,11 @@ int RsGenExchange::createGroupSignatures(RsTlvKeySignatureSet& signSet, RsTlvBin if(haveKey) { - RsTlvSecurityKey authorKey; + RsTlvPrivateRSAKey authorKey; mGixs->getPrivateKey(grpMeta.mAuthorId, authorKey); RsTlvKeySignature sign; - if(GxsSecurity::getSignature((char*)grpData.bin_data, grpData.bin_len, - authorKey, sign)) + if(GxsSecurity::getSignature((char*)grpData.bin_data, grpData.bin_len, authorKey, sign)) { id_ret = SIGN_SUCCESS; mGixs->timeStampKey(grpMeta.mAuthorId) ; @@ -623,12 +631,12 @@ int RsGenExchange::createMsgSignatures(RsTlvKeySignatureSet& signSet, RsTlvBinar if(needPublishSign) { // public and shared is publish key - RsTlvSecurityKeySet& keys = grpMeta.keys; - RsTlvSecurityKey* publishKey; + const RsTlvSecurityKeySet& keys = grpMeta.keys; + const RsTlvPrivateRSAKey *publishKey; - std::map::iterator mit = - keys.keys.begin(), mit_end = keys.keys.end(); + std::map::const_iterator mit = keys.private_keys.begin(), mit_end = keys.private_keys.end(); bool publish_key_found = false; + for(; mit != mit_end; ++mit) { @@ -672,7 +680,7 @@ int RsGenExchange::createMsgSignatures(RsTlvKeySignatureSet& signSet, RsTlvBinar if(haveKey) { - RsTlvSecurityKey authorKey; + RsTlvPrivateRSAKey authorKey; mGixs->getPrivateKey(msgMeta.mAuthorId, authorKey); RsTlvKeySignature sign; @@ -841,13 +849,13 @@ int RsGenExchange::validateMsg(RsNxsMsg *msg, const uint32_t& grpFlag, const uin { RsTlvKeySignature sign = metaData.signSet.keySignSet[INDEX_AUTHEN_PUBLISH]; - std::map& keys = grpKeySet.keys; - std::map::iterator mit = keys.begin(); + std::map& keys = grpKeySet.public_keys; + std::map::iterator mit = keys.begin(); RsGxsId keyId; for(; mit != keys.end() ; ++mit) { - RsTlvSecurityKey& key = mit->second; + RsTlvPublicRSAKey& key = mit->second; if(key.keyFlags & RSTLV_KEY_DISTRIB_PUBLIC_deprecated) { @@ -865,7 +873,7 @@ int RsGenExchange::validateMsg(RsNxsMsg *msg, const uint32_t& grpFlag, const uin if(!keyId.isNull()) { - RsTlvSecurityKey& key = keys[keyId]; + RsTlvPublicRSAKey& key = keys[keyId]; publishValidate &= GxsSecurity::validateNxsMsg(*msg, sign, key); } else @@ -889,7 +897,7 @@ int RsGenExchange::validateMsg(RsNxsMsg *msg, const uint32_t& grpFlag, const uin if(haveKey) { - RsTlvSecurityKey authorKey; + RsTlvPublicRSAKey authorKey; bool auth_key_fetched = mGixs->getKey(metaData.mAuthorId, authorKey) ; if (auth_key_fetched) @@ -1008,7 +1016,7 @@ int RsGenExchange::validateGrp(RsNxsGrp* grp) std::cerr << " have ID key in cache: yes" << std::endl; #endif - RsTlvSecurityKey authorKey; + RsTlvPublicRSAKey authorKey; bool auth_key_fetched = mGixs->getKey(metaData.mAuthorId, authorKey) ; if (auth_key_fetched) @@ -2155,20 +2163,21 @@ void RsGenExchange::processGroupUpdatePublish() //gup.grpItem->meta = *meta; GxsGrpPendingSign ggps(gup.grpItem, gup.mToken); - bool publishAndAdminPrivatePresent = checkKeys(meta->keys); - - if(publishAndAdminPrivatePresent) + if(checkKeys(meta->keys)) { - ggps.mPrivateKeys = meta->keys; - generatePublicFromPrivateKeys(ggps.mPrivateKeys, ggps.mPublicKeys); + ggps.mKeys = meta->keys; + generatePublicFromPrivateKeys(ggps.mKeys); ggps.mHaveKeys = true; ggps.mStartTS = time(NULL); ggps.mLastAttemptTS = 0; ggps.mIsUpdate = true; ggps.mToken = gup.mToken; mGrpsToPublish.push_back(ggps); - }else + } + else { + std::cerr << "(EE) publish group fails because RS cannot find the private publish and author keys" << std::endl; + delete gup.grpItem; mDataAccess->updatePublicRequestStatus(gup.mToken, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED); } @@ -2291,14 +2300,15 @@ void RsGenExchange::processMessageDelete() bool RsGenExchange::checkKeys(const RsTlvSecurityKeySet& keySet) { - typedef std::map keyMap; - const keyMap& allKeys = keySet.keys; + typedef std::map keyMap; + const keyMap& allKeys = keySet.private_keys; keyMap::const_iterator cit = allKeys.begin(); + bool adminFound = false, publishFound = false; for(; cit != allKeys.end(); ++cit) { - const RsTlvSecurityKey& key = cit->second; - if(key.keyFlags & RSTLV_KEY_TYPE_FULL) + const RsTlvPrivateRSAKey& key = cit->second; + if(key.keyFlags & RSTLV_KEY_TYPE_FULL) // this one is not useful. Just a security. { if(key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) adminFound = true; @@ -2307,6 +2317,11 @@ bool RsGenExchange::checkKeys(const RsTlvSecurityKeySet& keySet) publishFound = true; } + else if(key.keyFlags & RSTLV_KEY_TYPE_PUBLIC_ONLY) // this one is not useful. Just a security. + { + std::cerr << "(EE) found a public only key in the private key list" << std::endl; + return false ; + } } // user must have both private and public parts of publish and admin keys @@ -2345,29 +2360,23 @@ void RsGenExchange::publishGrps() RsNxsGrp* grp = new RsNxsGrp(mServType); RsGxsGrpItem* grpItem = ggps.mItem; - RsTlvSecurityKeySet privatekeySet, publicKeySet; + RsTlvSecurityKeySet fullKeySet; if(!(ggps.mHaveKeys)) { - generateGroupKeys(privatekeySet, publicKeySet, true); + generateGroupKeys(fullKeySet, true); ggps.mHaveKeys = true; - ggps.mPrivateKeys = privatekeySet; - ggps.mPublicKeys = publicKeySet; + ggps.mKeys = fullKeySet; } else - { - privatekeySet = ggps.mPrivateKeys; - publicKeySet = ggps.mPublicKeys; - } - - // find private admin key - RsTlvSecurityKey privAdminKey; - std::map::iterator mit_keys = privatekeySet.keys.begin(); + fullKeySet = ggps.mKeys; + // find private admin key + RsTlvPrivateRSAKey privAdminKey; bool privKeyFound = false; - for(; mit_keys != privatekeySet.keys.end(); ++mit_keys) + for(std::map::iterator mit_keys = fullKeySet.private_keys.begin(); mit_keys != fullKeySet.private_keys.end(); ++mit_keys) { - RsTlvSecurityKey& key = mit_keys->second; + RsTlvPrivateRSAKey& key = mit_keys->second; if(key.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL)) { @@ -2383,8 +2392,7 @@ void RsGenExchange::publishGrps() // get group id from private admin key id grpItem->meta.mGroupId = grp->grpId = RsGxsGroupId(privAdminKey.keyId); - ServiceCreate_Return ret = service_CreateGroup(grpItem, privatekeySet); - + ServiceCreate_Return ret = service_CreateGroup(grpItem, fullKeySet); bool serialOk = false, servCreateOk; @@ -2412,7 +2420,7 @@ void RsGenExchange::publishGrps() grp->metaData->mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_ADMIN | GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH; - create = createGroup(grp, privatekeySet, publicKeySet); + create = createGroup(grp, fullKeySet); #ifdef GEN_EXCH_DEBUG std::cerr << "RsGenExchange::publishGrps() "; @@ -2424,15 +2432,22 @@ void RsGenExchange::publishGrps() if(create == CREATE_SUCCESS) { + // Here we need to make sure that no private keys are included. This is very important since private keys + // can be used to modify the group. Normally the private key set is whiped out by createGroup, but + grp->metaData->keys.private_keys.clear() ; + uint32_t mdSize = grp->metaData->serial_size(RS_GXS_GRP_META_DATA_CURRENT_API_VERSION); - char* metaData = new char[mdSize]; - serialOk = grp->metaData->serialise(metaData, mdSize,RS_GXS_GRP_META_DATA_CURRENT_API_VERSION); - grp->meta.setBinData(metaData, mdSize); - delete[] metaData; + + { + RsTemporaryMemory metaData(mdSize); + serialOk = grp->metaData->serialise(metaData, mdSize,RS_GXS_GRP_META_DATA_CURRENT_API_VERSION); +#warning TODO: grp->meta should be renamed grp->public_meta ! + grp->meta.setBinData(metaData, mdSize); + } - // place back private keys for publisher - grp->metaData->keys = privatekeySet; + // Place back private keys for publisher and database storage + grp->metaData->keys.private_keys = fullKeySet.private_keys; if(mDataStore->validSize(grp) && serialOk) { @@ -2443,10 +2458,10 @@ void RsGenExchange::publishGrps() if(ggps.mIsUpdate) mDataAccess->updateGroupData(grp); else - mDataAccess->addGroupData(grp); - - if(mNetService!=NULL) - mNetService->subscribeStatusChanged(grpId,true) ; + mDataAccess->addGroupData(grp); +#warning this is bad: addGroupData/updateGroupData actially deletes grp. But it may be used below? grp should be a class object and not deleted manually! + if(mNetService!=NULL) + mNetService->subscribeStatusChanged(grpId,true) ; } else { @@ -3074,8 +3089,8 @@ bool RsGenExchange::updateValid(RsGxsGrpMetaData& oldGrpMeta, RsNxsGrp& newGrp) RsTlvKeySignature adminSign = mit->second; - std::map& keys = oldGrpMeta.keys.keys; - std::map::iterator keyMit = keys.find(RsGxsId(oldGrpMeta.mGroupId)); + std::map& keys = oldGrpMeta.keys.public_keys; + std::map::iterator keyMit = keys.find(RsGxsId(oldGrpMeta.mGroupId)); if(keyMit == keys.end()) { diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index 4707f8ee2..2b72c9d5e 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -79,8 +79,7 @@ public: RsGxsGrpItem* mItem; bool mHaveKeys; // mKeys->first == true if key present bool mIsUpdate; - RsTlvSecurityKeySet mPrivateKeys; - RsTlvSecurityKeySet mPublicKeys; + RsTlvSecurityKeySet mKeys; }; typedef std::map > GxsMsgDataMap; @@ -694,7 +693,7 @@ private: * @return CREATE_SUCCESS for success, CREATE_FAIL for fail, * CREATE_FAIL_TRY_LATER for Id sign key not avail (but requested) */ - uint8_t createGroup(RsNxsGrp* grp, RsTlvSecurityKeySet& privateKeySet, RsTlvSecurityKeySet& publicKeySet); + uint8_t createGroup(RsNxsGrp* grp, RsTlvSecurityKeySet& keySet); /*! * This completes the creation of an instance on RsNxsMsg @@ -742,7 +741,7 @@ private: * @param publickeySet contains public generated keys (counterpart of private) * @param genPublicKeys should publish key pair also be generated */ - void generateGroupKeys(RsTlvSecurityKeySet& privatekeySet, RsTlvSecurityKeySet& publickeySet, bool genPublishKeys); + void generateGroupKeys(RsTlvSecurityKeySet& keySet, bool genPublishKeys); /*! * Generate public set of keys from their private counterparts @@ -751,7 +750,7 @@ private: * @param publickeySet contains public generated keys (counterpart of private) * @return false if key gen failed for a key set */ - void generatePublicFromPrivateKeys(const RsTlvSecurityKeySet& privatekeySet, RsTlvSecurityKeySet& publickeySet); + void generatePublicFromPrivateKeys(RsTlvSecurityKeySet& keySet); /*! * Attempts to validate msg signatures diff --git a/libretroshare/src/gxs/rsgixs.h b/libretroshare/src/gxs/rsgixs.h index 5c19f6067..bdb58337c 100644 --- a/libretroshare/src/gxs/rsgixs.h +++ b/libretroshare/src/gxs/rsgixs.h @@ -160,8 +160,8 @@ public: * @return a pointer to a valid profile if successful, otherwise NULL * */ - virtual bool getKey(const RsGxsId &id, RsTlvSecurityKey &key) = 0; - virtual bool getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key) = 0; // For signing outgoing messages. + virtual bool getKey(const RsGxsId &id, RsTlvPublicRSAKey& key) = 0; + virtual bool getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey& key) = 0; // For signing outgoing messages. virtual bool getIdDetails(const RsGxsId& id, RsIdentityDetails& details) = 0 ; // Proxy function so that we get p3Identity info from Gxs }; diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index eb8fc6f3a..dae7b5089 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -3297,6 +3297,7 @@ void RsGxsNetService::locked_genSendGrpsTransaction(NxsTransaction* tr) RsPeerId peerId = tr->mTransaction->PeerId(); for(;mit != grps.end(); ++mit) { +#warning Should make sure that no private key information is sneaked in here for the grp mit->second->PeerId(peerId); // set so it gets sent to right peer mit->second->transactionNumber = transN; newTr->mItems.push_back(mit->second); @@ -3694,11 +3695,11 @@ bool RsGxsNetService::encryptSingleNxsItem(RsNxsItem *item, const RsGxsCircleId& #ifdef NXS_NET_DEBUG_7 GXSNETDEBUG_P_ (item->PeerId()) << " Dest Ids: " << std::endl; #endif - std::vector recipient_keys ; + std::vector recipient_keys ; for(std::list::const_iterator it(recipients.begin());it!=recipients.end();++it) { - RsTlvSecurityKey pkey ; + RsTlvPublicRSAKey pkey ; if(!mGixs->getKey(*it,pkey)) { @@ -3770,7 +3771,7 @@ bool RsGxsNetService::processTransactionForDecryption(NxsTransaction *tr) #endif std::list decrypted_items ; - std::vector private_keys ; + std::vector private_keys ; // get all private keys. Normally we should look into the circle name and only supply the keys that we have @@ -3806,13 +3807,13 @@ bool RsGxsNetService::processTransactionForDecryption(NxsTransaction *tr) return true ; } -bool RsGxsNetService::decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypted_item, RsNxsItem *& nxsitem,std::vector *pprivate_keys) +bool RsGxsNetService::decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypted_item, RsNxsItem *& nxsitem,std::vector *pprivate_keys) { // if private_keys storage is supplied use/update them, otherwise, find which key should be used, and store them in a local std::vector. nxsitem = NULL ; - std::vector local_keys ; - std::vector& private_keys = pprivate_keys?(*pprivate_keys):local_keys ; + std::vector local_keys ; + std::vector& private_keys = pprivate_keys?(*pprivate_keys):local_keys ; // we need the private keys to decrypt the item. First load them in! bool key_loading_failed = false ; @@ -3828,7 +3829,7 @@ bool RsGxsNetService::decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypt for(std::list::const_iterator it(own_keys.begin());it!=own_keys.end();++it) { - RsTlvSecurityKey private_key ; + RsTlvPrivateRSAKey private_key ; if(mGixs->getPrivateKey(*it,private_key)) { @@ -4848,9 +4849,9 @@ void RsGxsNetService::sharePublishKeysPending() const RsTlvSecurityKeySet& keys = grpMeta->keys; - std::map::const_iterator kit = keys.keys.begin(), kit_end = keys.keys.end(); + std::map::const_iterator kit = keys.private_keys.begin(), kit_end = keys.private_keys.end(); bool publish_key_found = false; - RsTlvSecurityKey publishKey ; + RsTlvPrivateRSAKey publishKey ; for(; kit != kit_end && !publish_key_found; ++kit) { @@ -4900,7 +4901,7 @@ void RsGxsNetService::sharePublishKeysPending() void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) { #ifdef NXS_NET_DEBUG_3 - GXSNETDEBUG_PG(item->PeerId(),item->grpId) << "RsGxsNetService::sharePublishKeys() " << std::endl; + GXSNETDEBUG_PG(item->PeerId(),item->grpId) << "RsGxsNetService::sharePublishKeys() " << std::endl; #endif if (!item) @@ -4909,62 +4910,61 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) RS_STACK_MUTEX(mNxsMutex) ; #ifdef NXS_NET_DEBUG_3 - GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " PeerId : " << item->PeerId() << std::endl; - GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " GrpId: " << item->grpId << std::endl; - GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " Got key Item: " << item->key.keyId << std::endl; + GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " PeerId : " << item->PeerId() << std::endl; + GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " GrpId: " << item->grpId << std::endl; + GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " Got key Item: " << item->key.keyId << std::endl; #endif - // Get the meta data for this group Id - // - RsGxsMetaDataTemporaryMap grpMetaMap; - grpMetaMap[item->grpId] = NULL; - - mDataStore->retrieveGxsGrpMetaData(grpMetaMap); + // Get the meta data for this group Id + // + RsGxsMetaDataTemporaryMap grpMetaMap; + grpMetaMap[item->grpId] = NULL; - // update the publish keys in this group meta info + mDataStore->retrieveGxsGrpMetaData(grpMetaMap); - RsGxsGrpMetaData *grpMeta = grpMetaMap[item->grpId] ; + // update the publish keys in this group meta info - // Check that the keys correspond, and that FULL keys are supplied, etc. + RsGxsGrpMetaData *grpMeta = grpMetaMap[item->grpId] ; + + // Check that the keys correspond, and that FULL keys are supplied, etc. #ifdef NXS_NET_DEBUG_3 - GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key received: " << std::endl; + GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key received: " << std::endl; #endif - bool admin = (item->key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; - bool publi = (item->key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; + bool admin = (item->key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; + bool publi = (item->key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; #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; #endif - if(!(!admin && publi)) - { - std::cerr << " Key is not a publish private key. Discarding!" << std::endl; - return ; - } - // Also check that we don't already have full keys for that group. - - std::map::iterator it = grpMeta->keys.keys.find(item->key.keyId) ; + if(!(!admin && publi)) + { + std::cerr << " Key is not a publish private key. Discarding!" << std::endl; + return ; + } + // Also check that we don't already have full keys for that group. - if(it == grpMeta->keys.keys.end()) - { - std::cerr << " (EE) Key not found in known group keys. This is an inconsistency." << std::endl; - return ; - } + if(grpMeta->keys.public_keys.find(item->key.keyId) == grpMeta->keys.public_keys.end()) + { + std::cerr << " (EE) Key not found in known group keys. This is an inconsistency." << std::endl; + return ; + } - if((it->second.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (it->second.keyFlags & RSTLV_KEY_TYPE_FULL)) - { + if(grpMeta->keys.private_keys.find(item->key.keyId) != grpMeta->keys.private_keys.end()) + { #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; #endif - return ; - } + return ; + } - // Store/update the info. + // Store/update the info. - it->second = item->key ; - bool ret = mDataStore->updateGroupKeys(item->grpId,grpMeta->keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ; + grpMeta->keys.private_keys[item->key.keyId] = item->key ; + + bool ret = mDataStore->updateGroupKeys(item->grpId,grpMeta->keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ; if(ret) { diff --git a/libretroshare/src/gxs/rsgxsnetservice.h b/libretroshare/src/gxs/rsgxsnetservice.h index e9556701c..3ed83960a 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.h +++ b/libretroshare/src/gxs/rsgxsnetservice.h @@ -455,7 +455,7 @@ private: * encrypts/decrypts the transaction for the destination circle id. */ bool encryptSingleNxsItem(RsNxsItem *item, const RsGxsCircleId& destination_circle, const RsGxsGroupId &destination_group, RsNxsItem *& encrypted_item, uint32_t &status) ; - bool decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypted_item, RsNxsItem *&nxsitem, std::vector *private_keys=NULL); + bool decryptSingleNxsItem(const RsNxsEncryptedDataItem *encrypted_item, RsNxsItem *&nxsitem, std::vector *private_keys=NULL); bool processTransactionForDecryption(NxsTransaction *tr); // return false when the keys are not loaded => need retry later void cleanRejectedMessages(); diff --git a/libretroshare/src/gxstunnel/p3gxstunnel.cc b/libretroshare/src/gxstunnel/p3gxstunnel.cc index eb0c32812..38d2b8525 100644 --- a/libretroshare/src/gxstunnel/p3gxstunnel.cc +++ b/libretroshare/src/gxstunnel/p3gxstunnel.cc @@ -890,7 +890,7 @@ void p3GxsTunnelService::handleRecvDHPublicKey(RsGxsTunnelDHPublicKeyItem *item) RsTemporaryMemory data(pubkey_size) ; BN_bn2bin(item->public_key, data) ; - RsTlvSecurityKey signature_key ; + RsTlvPublicRSAKey signature_key ; // We need to get the key of the sender, but if the key is not cached, we // need to get it first. So we let the system work for 2-3 seconds before @@ -1059,9 +1059,9 @@ bool p3GxsTunnelService::locked_sendDHPublicKey(const DH *dh,const RsGxsId& own_ // we should also sign the data and check the signature on the other end. // - RsTlvKeySignature signature ; - RsTlvSecurityKey signature_key ; - RsTlvSecurityKey signature_key_public ; + RsTlvKeySignature signature ; + RsTlvPrivateRSAKey signature_key ; + RsTlvPublicRSAKey signature_key_public ; uint32_t error_status ; diff --git a/libretroshare/src/gxstunnel/rsgxstunnelitems.h b/libretroshare/src/gxstunnel/rsgxstunnelitems.h index dac8ab60a..c23a8b9a1 100644 --- a/libretroshare/src/gxstunnel/rsgxstunnelitems.h +++ b/libretroshare/src/gxstunnel/rsgxstunnelitems.h @@ -146,7 +146,7 @@ class RsGxsTunnelDHPublicKeyItem: public RsGxsTunnelItem BIGNUM *public_key ; RsTlvKeySignature signature ; // signs the public key in a row. - RsTlvSecurityKey gxs_key ; // public key of the signer + RsTlvPublicRSAKey gxs_key ; // public key of the signer private: // make the object non copy-able diff --git a/libretroshare/src/serialiser/rsgxsrecognitems.h b/libretroshare/src/serialiser/rsgxsrecognitems.h index 5d75db34a..fedc7eafb 100644 --- a/libretroshare/src/serialiser/rsgxsrecognitems.h +++ b/libretroshare/src/serialiser/rsgxsrecognitems.h @@ -117,7 +117,7 @@ virtual void clear(); std::ostream &print(std::ostream &out, uint16_t indent = 0); RsTlvServiceIdSet signing_classes; - RsTlvSecurityKey key; // has from->to, and flags. + RsTlvSecurityKey_deprecated key; // has from->to, and flags. RsTlvKeySignature sign; }; diff --git a/libretroshare/src/serialiser/rsnxsitems.cc b/libretroshare/src/serialiser/rsnxsitems.cc index 1a7cf45b4..55501195d 100644 --- a/libretroshare/src/serialiser/rsnxsitems.cc +++ b/libretroshare/src/serialiser/rsnxsitems.cc @@ -1237,7 +1237,7 @@ RsNxsGrp* RsNxsGrp::clone() const { if(this->metaData) { grp->metaData = new RsGxsGrpMetaData(); - *(grp->metaData) = *(this->metaData); +// *(grp->metaData) = *(this->metaData); } return grp; diff --git a/libretroshare/src/serialiser/rsnxsitems.h b/libretroshare/src/serialiser/rsnxsitems.h index 6be81ecbe..8bc19d501 100644 --- a/libretroshare/src/serialiser/rsnxsitems.h +++ b/libretroshare/src/serialiser/rsnxsitems.h @@ -157,7 +157,8 @@ public: virtual uint32_t serial_size() const; RsGxsGroupId grpId ; - RsTlvSecurityKey key ; +#warning should be renamed private_key + RsTlvPrivateRSAKey key ; }; @@ -330,9 +331,12 @@ public: * This should contains all data * which is not specific to the Gxs service data */ + // This is the binary data for the group meta that is sent to friends. It *should not* contain any private + // key parts. This is ensured in RsGenExchange + RsTlvBinaryData meta; - // deserialised metaData, this is not serialised + // Deserialised metaData, this is not serialised by the serialize() method. So it may contain private key parts in some cases. RsGxsGrpMetaData* metaData; }; diff --git a/libretroshare/src/serialiser/rstlvbase.h b/libretroshare/src/serialiser/rstlvbase.h index 41dd344e5..8e463214f 100644 --- a/libretroshare/src/serialiser/rstlvbase.h +++ b/libretroshare/src/serialiser/rstlvbase.h @@ -207,8 +207,10 @@ const uint16_t TLV_TYPE_GXSCIRCLEIDSET= 0x1026; const uint16_t TLV_TYPE_SERVICESET = 0x1030; -const uint16_t TLV_TYPE_SECURITYKEY = 0x1040; -const uint16_t TLV_TYPE_SECURITYKEYSET= 0x1041; +const uint16_t TLV_TYPE_SECURITYKEY_deprecated = 0x1040; +const uint16_t TLV_TYPE_SECURITYKEYSET = 0x1041; +const uint16_t TLV_TYPE_RSA_KEY_PUBLIC = 0x1042; +const uint16_t TLV_TYPE_RSA_KEY_PRIVATE= 0x1043; const uint16_t TLV_TYPE_KEYSIGNATURE = 0x1050; const uint16_t TLV_TYPE_KEYSIGNATURESET = 0x1051; diff --git a/libretroshare/src/serialiser/rstlvkeys.cc b/libretroshare/src/serialiser/rstlvkeys.cc index 968b09d83..302951f9f 100644 --- a/libretroshare/src/serialiser/rstlvkeys.cc +++ b/libretroshare/src/serialiser/rstlvkeys.cc @@ -27,6 +27,7 @@ #include "rstlvkeys.h" #include "rstlvbase.h" #include "rsbaseserial.h" +#include "util/stacktrace.h" #include @@ -37,13 +38,13 @@ /************************************* RsTlvSecurityKey ************************************/ -RsTlvSecurityKey::RsTlvSecurityKey() +RsTlvSecurityKey_deprecated::RsTlvSecurityKey_deprecated() :RsTlvItem(), keyFlags(0), startTS(0), endTS(0), keyData(TLV_TYPE_KEY_EVP_PKEY) { return; } -void RsTlvSecurityKey::TlvClear() +void RsTlvSecurityKey_deprecated::TlvClear() { keyId.clear(); keyFlags = 0; @@ -53,7 +54,7 @@ void RsTlvSecurityKey::TlvClear() } /* clears keyData - but doesn't delete */ -void RsTlvSecurityKey::ShallowClear() +void RsTlvSecurityKey_deprecated::ShallowClear() { keyId.clear(); keyFlags = 0; @@ -63,8 +64,22 @@ void RsTlvSecurityKey::ShallowClear() keyData.bin_len = 0; } +uint32_t RsTlvRSAKey::TlvSize() const +{ + uint32_t s = TLV_HEADER_SIZE; /* header + 4 for size */ -uint32_t RsTlvSecurityKey::TlvSize() const + /* 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 */ @@ -81,15 +96,48 @@ uint32_t RsTlvSecurityKey::TlvSize() const s += keyData.TlvSize(); return s; - } -bool RsTlvSecurityKey::SetTlv(void *data, uint32_t size, uint32_t *offset) const +bool RsTlvRSAKey::SetTlv(void *data, uint32_t size, uint32_t *offset) const { /* must check sizes */ uint32_t tlvsize = TlvSize(); uint32_t tlvend = *offset + tlvsize; + if (size < tlvend) + { + std::cerr << "RsTlvSecurityKey::SetTlv() Failed not enough space"; + std::cerr << std::endl; + return false; /* not enough space */ + } + + bool ok = checkFlags(keyFlags); // check before serialise, just in case + + /* start at data[offset] */ + /* add mandatory parts first */ + + ok &= SetTlvBase(data, tlvend, offset, tlvType(), 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 @@ -104,7 +152,7 @@ bool RsTlvSecurityKey::SetTlv(void *data, uint32_t size, uint32_t *offset) cons /* start at data[offset] */ /* add mandatory parts first */ - ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEY, tlvsize); + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITYKEY_deprecated, tlvsize); #ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId.toStdString()); @@ -120,8 +168,7 @@ bool RsTlvSecurityKey::SetTlv(void *data, uint32_t size, uint32_t *offset) cons } - -bool RsTlvSecurityKey::GetTlv(void *data, uint32_t size, uint32_t *offset) +bool RsTlvRSAKey::GetTlv(void *data, uint32_t size, uint32_t *offset) { if (size < *offset + TLV_HEADER_SIZE) return false; @@ -139,7 +186,75 @@ bool RsTlvSecurityKey::GetTlv(void *data, uint32_t size, uint32_t *offset) return false; /* not enough space */ } - if (tlvtype != TLV_TYPE_SECURITYKEY) /* check type */ + if (tlvtype != tlvType()) /* 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; + + 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"; @@ -196,7 +311,7 @@ bool RsTlvSecurityKey::GetTlv(void *data, uint32_t size, uint32_t *offset) } -std::ostream &RsTlvSecurityKey::print(std::ostream &out, uint16_t indent) const +std::ostream &RsTlvSecurityKey_deprecated::print(std::ostream &out, uint16_t indent) const { printBase(out, "RsTlvSecurityKey", indent); uint16_t int_Indent = indent + 2; @@ -233,7 +348,10 @@ std::ostream &RsTlvSecurityKey::print(std::ostream &out, uint16_t indent) const void RsTlvSecurityKeySet::TlvClear() { groupId.clear(); - keys.clear(); //empty list +#ifdef TODO + public_keys.clear(); //empty list + private_keys.clear(); //empty list +#endif } uint32_t RsTlvSecurityKeySet::TlvSize() const @@ -241,17 +359,13 @@ uint32_t RsTlvSecurityKeySet::TlvSize() const uint32_t s = TLV_HEADER_SIZE; /* header */ - std::map::const_iterator it; - s += GetTlvStringSize(groupId); - if(!keys.empty()) - { - - for(it = keys.begin(); it != keys.end() ; ++it) - s += (it->second).TlvSize(); - - } + for(std::map::const_iterator it = public_keys.begin(); it != public_keys.end() ; ++it) + s += (it->second).TlvSize(); + + for(std::map::const_iterator it = private_keys.begin(); it != private_keys.end() ; ++it) + s += (it->second).TlvSize(); return s; } @@ -279,17 +393,13 @@ bool RsTlvSecurityKeySet::SetTlv(void *data, uint32_t size, uint32_t *offset) c /* groupId */ ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_GROUPID, groupId); - if(!keys.empty()) - { - std::map::const_iterator it; - - for(it = keys.begin(); it != keys.end() ; ++it) + for(std::map::const_iterator it = public_keys.begin(); it != public_keys.end() ; ++it) + ok &= (it->second).SetTlv(data, size, offset); + + for(std::map::const_iterator it = private_keys.begin(); it != private_keys.end() ; ++it) ok &= (it->second).SetTlv(data, size, offset); - } - - -return ok; + return ok; } @@ -319,37 +429,72 @@ bool RsTlvSecurityKeySet::GetTlv(void *data, uint32_t size, uint32_t *offset) /* groupId */ ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_GROUPID, groupId); - /* while there is TLV */ - while((*offset) + 2 < tlvend) - { - /* get the next type */ - uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + /* while there is TLV */ + while((*offset) + 2 < tlvend) + { + /* get the next type */ + uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); - switch(tlvsubtype) - { - case TLV_TYPE_SECURITYKEY: + switch(tlvsubtype) + { + case TLV_TYPE_SECURITYKEY_deprecated: + { + RsTlvSecurityKey_deprecated key; + + ok &= key.GetTlv(data, size, offset); + + if (ok) { - RsTlvSecurityKey key; - ok &= key.GetTlv(data, size, offset); - if (ok) - { - keys[key.keyId] = key; - key.TlvClear(); /* so that the Map can get control - should be ref counted*/ - } + std::cerr << "(WW) read RSA key with old format. Will be converted to new format. Flags=" << std::hex << key.keyFlags << std::dec << std::endl; + + if(key.keyFlags & RSTLV_KEY_TYPE_FULL) + 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*/ } - break; - default: - ok &= SkipUnknownTlv(data, tlvend, offset); - break; + } + break ; - } + case TLV_TYPE_RSA_KEY_PRIVATE: + { + RsTlvPrivateRSAKey key; - if (!ok) + 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: + ok &= SkipUnknownTlv(data, tlvend, offset); + break; + + } + + if (!ok) break; } - - + + /*************************************************************************** * NB: extra components could be added (for future expansion of the type). * or be present (if this code is reading an extended version). @@ -378,8 +523,9 @@ std::ostream &RsTlvSecurityKeySet::print(std::ostream &out, uint16_t indent) con out << "GroupId: " << groupId; out << std::endl; - std::map::const_iterator it; - for(it = keys.begin(); it != keys.end() ; ++it) + for( std::map::const_iterator it = public_keys.begin(); it != public_keys.end() ; ++it) + (it->second).print(out, int_Indent); + for( std::map::const_iterator it = private_keys.begin(); it != private_keys.end() ; ++it) (it->second).print(out, int_Indent); printEnd(out, "RsTlvSecurityKeySet", indent); diff --git a/libretroshare/src/serialiser/rstlvkeys.h b/libretroshare/src/serialiser/rstlvkeys.h index 7e78e1a94..d65f92b2d 100644 --- a/libretroshare/src/serialiser/rstlvkeys.h +++ b/libretroshare/src/serialiser/rstlvkeys.h @@ -31,6 +31,7 @@ #include "serialiser/rstlvitem.h" #include "serialiser/rstlvbinary.h" +#include "serialiser/rstlvbase.h" #include "retroshare/rsgxsifacetypes.h" #include @@ -46,11 +47,13 @@ const uint32_t RSTLV_KEY_DISTRIB_ADMIN = 0x0040; const uint32_t RSTLV_KEY_DISTRIB_IDENTITY = 0x0080; const uint32_t RSTLV_KEY_DISTRIB_MASK = 0x00f0; -class RsTlvSecurityKey: public RsTlvItem +// Old class for RsTlvSecurityKey. Is kept for backward compatibility, but should not be serialised anymore + +class RsTlvSecurityKey_deprecated: public RsTlvItem { public: - RsTlvSecurityKey(); - virtual ~RsTlvSecurityKey() {} + RsTlvSecurityKey_deprecated(); + virtual ~RsTlvSecurityKey_deprecated() {} virtual uint32_t TlvSize() const; virtual void TlvClear(); @@ -61,26 +64,83 @@ class RsTlvSecurityKey: public RsTlvItem /* clears KeyData - but doesn't delete - to transfer ownership */ void ShallowClear(); - RsGxsId keyId; // Mandatory : + RsGxsId keyId; // Mandatory : uint32_t keyFlags; // Mandatory ; uint32_t startTS; // Mandatory : uint32_t endTS; // Mandatory : RsTlvBinaryData keyData; // Mandatory : }; +class RsTlvRSAKey +{ +public: + virtual uint32_t tlvType() const = 0 ; + virtual bool checkFlags(uint32_t flags) const = 0 ; + + 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(); + + bool check() const { return checkFlags(keyFlags) && (!keyId.isNull()) ; } + + RsGxsId keyId; // Mandatory : + uint32_t keyFlags; // Mandatory ; + uint32_t startTS; // Mandatory : + uint32_t endTS; // Mandatory : + RsTlvBinaryData keyData; // Mandatory : +}; + +// The two classes below are by design incompatible, making it impossible to pass a private key as a public key + +class RsTlvPrivateRSAKey: public RsTlvRSAKey +{ + public: + RsTlvPrivateRSAKey(); + explicit RsTlvPrivateRSAKey(RsTlvSecurityKey_deprecated& key); // convertion tool. Should be called explicitely and performs some checks + + virtual ~RsTlvPrivateRSAKey() {} + + 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 +{ +#warning TEST IF WE CAN CONVERT PUBLIC TO PRIVATE AND VIS VERSA. NORMALLY WE SHOULDNT + public: + 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 uint32_t tlvType() const { return TLV_TYPE_RSA_KEY_PUBLIC ; } +}; + class RsTlvSecurityKeySet: public RsTlvItem { - public: - RsTlvSecurityKeySet() { return; } -virtual ~RsTlvSecurityKeySet() { return; } -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; +public: + RsTlvSecurityKeySet() { return; } + virtual ~RsTlvSecurityKeySet() { return; } + + void initFromPublicKeys(const RsTlvSecurityKeySet& skset) ; + + 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; - std::string groupId; // Mandatory : - std::map keys; // Mandatory : + std::string groupId; // Mandatory : + std::map public_keys; // Mandatory : + std::map 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;} }; diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 0e2769205..a9e38b02c 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -447,7 +447,10 @@ bool p3IdService:: getIdDetails(const RsGxsId &id, RsIdentityDetails &details) { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + + { + RsGxsIdCache data; + if (mPublicKeyCache.fetch(id, data)) { details = data.details; @@ -464,7 +467,10 @@ bool p3IdService:: getIdDetails(const RsGxsId &id, RsIdentityDetails &details) return true; } + } + RsGxsIdCache data; + /* try private cache too */ if (mPrivateKeyCache.fetch(id, data)) { @@ -604,12 +610,13 @@ bool p3IdService::getRecognTagRequest(const RsGxsId &id, const std::string &comm return false; } - RsTlvSecurityKey key; + RsTlvPrivateRSAKey key; std::string nickname; { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + RsGxsIdCache data; + if (!mPrivateKeyCache.fetch(id, data)) { #ifdef DEBUG_RECOGN @@ -669,11 +676,12 @@ bool p3IdService::isPendingNetworkRequest(const RsGxsId& gxsId) return false; } -bool p3IdService::getKey(const RsGxsId &id, RsTlvSecurityKey &key) +bool p3IdService::getKey(const RsGxsId &id, RsTlvPublicRSAKey &key) { { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + RsGxsIdCache data; + if (mPublicKeyCache.fetch(id, data)) { key = data.pubkey; @@ -695,11 +703,12 @@ bool p3IdService::requestPrivateKey(const RsGxsId &id) return cache_request_load(id); } -bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key) +bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey &key) { { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + RsGxsIdCache data; + if (mPrivateKeyCache.fetch(id, data)) { key = data.pubkey; @@ -716,10 +725,7 @@ bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key) bool p3IdService::signData(const uint8_t *data,uint32_t data_size,const RsGxsId& own_gxs_id,RsTlvKeySignature& signature,uint32_t& error_status) { - //RsIdentityDetails details ; - RsTlvSecurityKey signature_key ; - - //getIdDetails(own_gxs_id,details); + RsTlvPrivateRSAKey signature_key ; int i ; for(i=0;i<6;++i) @@ -758,7 +764,7 @@ bool p3IdService::validateData(const uint8_t *data,uint32_t data_size,const RsTl { // RsIdentityDetails details ; // getIdDetails(signature.keyId,details); - RsTlvSecurityKey signature_key ; + RsTlvPublicRSAKey signature_key ; for(int i=0;i< (force_load?6:1);++i) if(!getKey(signature.keyId,signature_key) || signature_key.keyData.bin_data == NULL) @@ -793,7 +799,7 @@ bool p3IdService::validateData(const uint8_t *data,uint32_t data_size,const RsTl } bool p3IdService::encryptData(const uint8_t *decrypted_data,uint32_t decrypted_data_size,uint8_t *& encrypted_data,uint32_t& encrypted_data_size,const RsGxsId& encryption_key_id,bool force_load,uint32_t& error_status) { - RsTlvSecurityKey encryption_key ; + RsTlvPublicRSAKey encryption_key ; // get the key, and let the cache find it. for(int i=0;i<(force_load?6:1);++i) @@ -823,7 +829,7 @@ bool p3IdService::encryptData(const uint8_t *decrypted_data,uint32_t decrypted_d bool p3IdService::decryptData(const uint8_t *encrypted_data,uint32_t encrypted_data_size,uint8_t *& decrypted_data,uint32_t& decrypted_size,const RsGxsId& key_id,uint32_t& error_status) { - RsTlvSecurityKey encryption_key ; + RsTlvPrivateRSAKey encryption_key ; // Get the key, and let the cache find it. It's our own key, so we should be able to find it, even if it takes // some seconds. @@ -882,7 +888,8 @@ bool p3IdService::getReputation(const RsGxsId &id, GixsReputation &rep) /* this is the key part for accepting messages */ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + RsGxsIdCache data; + if (mPublicKeyCache.fetch(id, data)) { rep.id = id; @@ -1635,42 +1642,6 @@ std::string SSGxsIdGroup::save() const * */ -RsGxsIdCache::RsGxsIdCache() -{ - return; -} - -RsGxsIdCache::RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvSecurityKey &in_pkey, const std::list &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 RsGxsIdCache::updateServiceString(std::string serviceString) { details.mRecognTags.clear(); @@ -1894,8 +1865,8 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item) /* extract key from keys */ RsTlvSecurityKeySet keySet; - RsTlvSecurityKey pubkey; - RsTlvSecurityKey fullkey; + RsTlvSecurityKey_deprecated pubkey; + RsTlvSecurityKey_deprecated fullkey; bool pub_key_ok = false; bool full_key_ok = false; @@ -1908,7 +1879,7 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item) return false; } - std::map::iterator kit; + std::map::iterator kit; //std::cerr << "p3IdService::cache_store() KeySet is:"; //keySet.print(std::cerr, 10); @@ -2367,7 +2338,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token) } else { - RsTlvSecurityKey seckey; + RsTlvSecurityKey_deprecated seckey; if (getKey(*vit, seckey)) { #ifdef DEBUG_IDS @@ -2399,7 +2370,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token) } else { - RsTlvSecurityKey seckey; + RsTlvSecurityKey_deprecated seckey; if (getPrivateKey(*vit, seckey)) { // success! @@ -2541,10 +2512,10 @@ RsGenExchange::ServiceCreate_Return p3IdService::service_CreateGroup(RsGxsGrpIte /********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/ // find private admin key - std::map::iterator mit = keySet.keys.begin(); + std::map::iterator mit = keySet.keys.begin(); for(; mit != keySet.keys.end(); ++mit) { - RsTlvSecurityKey& pk = mit->second; + RsTlvSecurityKey_deprecated& pk = mit->second; if(pk.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL)) { diff --git a/libretroshare/src/services/p3idservice.h b/libretroshare/src/services/p3idservice.h index 72e11bd74..e64b931ff 100644 --- a/libretroshare/src/services/p3idservice.h +++ b/libretroshare/src/services/p3idservice.h @@ -32,6 +32,7 @@ #include "gxs/rsgixs.h" // Internal Interfaces. #include "gxs/gxstokenqueue.h" +#include "serialiser/rsgxsiditems.h" #include #include @@ -189,20 +190,49 @@ virtual std::string save() const; class RsGxsIdGroupItem; -class RsGxsIdCache +template class RsGxsIdCache { - public: - RsGxsIdCache(); - RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvSecurityKey &in_pkey, - const std::list &tagList); +public: + RsGxsIdCache(); -void updateServiceString(std::string serviceString); + RsGxsIdCache(const RsGxsIdGroupItem *item, const KeyClass& in_pkey, const std::list &tagList) + { + // Save Keys. + pubkey = in_pkey; + // Save Time for ServiceString comparisions. + mPublishTs = item->meta.mPublishTs; - time_t mPublishTs; - std::list mRecognTags; // Only partially validated. + // Save RecognTags. + mRecognTags = tagList; - RsIdentityDetails details; - RsTlvSecurityKey pubkey; + 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); + + time_t mPublishTs; + std::list mRecognTags; // Only partially validated. + + RsIdentityDetails details; +#warning why the "pub" here?? + KeyClass pubkey; }; @@ -286,8 +316,8 @@ virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) ; virtual bool haveKey(const RsGxsId &id); virtual bool havePrivateKey(const RsGxsId &id); - virtual bool getKey(const RsGxsId &id, RsTlvSecurityKey &key); - virtual bool getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key); + virtual bool getKey(const RsGxsId &id, RsTlvPublicRSAKey &key); + virtual bool getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey &key); virtual bool requestKey(const RsGxsId &id, const std::list &peers); virtual bool requestPrivateKey(const RsGxsId &id); @@ -350,8 +380,8 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel); std::map > mCacheLoad_ToCache, mPendingCache; // Switching to RsMemCache for Key Caching. - RsMemCache mPublicKeyCache; - RsMemCache mPrivateKeyCache; + RsMemCache > mPublicKeyCache; + RsMemCache > mPrivateKeyCache; /************************************************************************ * Refreshing own Ids. diff --git a/libretroshare/src/util/rsrecogn.cc b/libretroshare/src/util/rsrecogn.cc index 4ce5f5ef9..17f16d480 100644 --- a/libretroshare/src/util/rsrecogn.cc +++ b/libretroshare/src/util/rsrecogn.cc @@ -327,6 +327,7 @@ bool rsa_sanity_check(RSA *rsa) } +#warning this code should be using GxsSecurity signature code. Not some own made signature call. bool RsRecogn::signTag(EVP_PKEY *signKey, RsGxsRecognTagItem *item) { @@ -374,6 +375,8 @@ bool RsRecogn::signTag(EVP_PKEY *signKey, RsGxsRecognTagItem *item) return true; } +#warning this code should be using GxsSecurity signature code. Not some own made signature call. + bool RsRecogn::signSigner(EVP_PKEY *signKey, RsGxsRecognSignerItem *item) { std::cerr << "RsRecogn::signSigner()"; @@ -429,6 +432,7 @@ bool RsRecogn::signSigner(EVP_PKEY *signKey, RsGxsRecognSignerItem *item) return true; } +#warning this code should be using GxsSecurity signature code. Not some own made signature call. bool RsRecogn::signTagRequest(EVP_PKEY *signKey, RsGxsRecognReqItem *item) { @@ -560,7 +564,7 @@ RsGxsRecognTagItem *RsRecogn::extractTag(const std::string &encoded) } -bool RsRecogn::createTagRequest(const RsTlvSecurityKey &key, const RsGxsId &id, const std::string &nickname, uint16_t tag_class, uint16_t tag_type, const std::string &comment, std::string &tag) +bool RsRecogn::createTagRequest(const RsTlvPrivateRSAKey &key, const RsGxsId &id, const std::string &nickname, uint16_t tag_class, uint16_t tag_type, const std::string &comment, std::string &tag) { EVP_PKEY *signKey = EVP_PKEY_new(); RSA *rsakey = d2i_RSAPrivateKey(NULL, (const unsigned char **)&key.keyData.bin_data, key.keyData.bin_len); diff --git a/libretroshare/src/util/rsrecogn.h b/libretroshare/src/util/rsrecogn.h index 25507bfff..6909a34ce 100644 --- a/libretroshare/src/util/rsrecogn.h +++ b/libretroshare/src/util/rsrecogn.h @@ -52,7 +52,7 @@ std::string getRsaKeyId(RSA *pubkey); RsGxsRecognTagItem *extractTag(const std::string &encoded); -bool createTagRequest(const RsTlvSecurityKey &key, +bool createTagRequest(const RsTlvPrivateRSAKey &key, const RsGxsId &id, const std::string &nickname, uint16_t tag_class, uint16_t tag_type, const std::string &comment, std::string &tag); diff --git a/tests/unittests/libretroshare/gxs/gen_exchange/rsdummyservices.h b/tests/unittests/libretroshare/gxs/gen_exchange/rsdummyservices.h index ac4dbc243..d1ce8f0a9 100644 --- a/tests/unittests/libretroshare/gxs/gen_exchange/rsdummyservices.h +++ b/tests/unittests/libretroshare/gxs/gen_exchange/rsdummyservices.h @@ -155,8 +155,8 @@ public: * @return a pointer to a valid profile if successful, otherwise NULL * */ - bool getKey(const RsGxsId &id, RsTlvSecurityKey &key){ return false; } - bool getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key){ return false; } // For signing outgoing messages. + bool getKey(const RsGxsId &id, RsTlvPrublicRSAKey& key){ return false; } + bool getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey& key){ return false; } // For signing outgoing messages. private: diff --git a/tests/unittests/libretroshare/gxs/security/gxssecurity_test.cc b/tests/unittests/libretroshare/gxs/security/gxssecurity_test.cc index 62e0f8d93..238f2dc50 100644 --- a/tests/unittests/libretroshare/gxs/security/gxssecurity_test.cc +++ b/tests/unittests/libretroshare/gxs/security/gxssecurity_test.cc @@ -35,8 +35,8 @@ TEST(libretroshare_gxs, GxsSecurity) { - RsTlvSecurityKey pub_key ; - RsTlvSecurityKey priv_key ; + RsTlvSecurityKey_deprecated pub_key ; + RsTlvSecurityKey_deprecated priv_key ; EXPECT_TRUE(GxsSecurity::generateKeyPair(pub_key,priv_key)) ; @@ -45,7 +45,7 @@ TEST(libretroshare_gxs, GxsSecurity) EXPECT_TRUE( pub_key.keyId == priv_key.keyId ); EXPECT_TRUE( pub_key.startTS == priv_key.startTS ); - RsTlvSecurityKey pub_key2 ; + RsTlvSecurityKey_deprecated pub_key2 ; EXPECT_TRUE(GxsSecurity::extractPublicKey(priv_key,pub_key2)) ; EXPECT_TRUE( pub_key.keyId == pub_key2.keyId ); diff --git a/tests/unittests/libretroshare/serialiser/support.cc b/tests/unittests/libretroshare/serialiser/support.cc index 99a642c12..a46689255 100644 --- a/tests/unittests/libretroshare/serialiser/support.cc +++ b/tests/unittests/libretroshare/serialiser/support.cc @@ -70,7 +70,7 @@ void init_item(RsTlvSecurityKeySet& ks) RsGxsId a_str; a_str = RsGxsId::random(); - RsTlvSecurityKey& a_key = ks.keys[a_str]; + RsTlvSecurityKey_deprecated& a_key = ks.keys[a_str]; init_item(a_key); a_key.keyId = a_str; } @@ -81,7 +81,7 @@ bool operator==(const RsTlvSecurityKeySet& l, const RsTlvSecurityKeySet& r) if(l.groupId != r.groupId) return false; - std::map::const_iterator l_cit = l.keys.begin(), + std::map::const_iterator l_cit = l.keys.begin(), r_cit = r.keys.begin(); for(; l_cit != l.keys.end(); l_cit++, r_cit++){ @@ -94,7 +94,7 @@ bool operator==(const RsTlvSecurityKeySet& l, const RsTlvSecurityKeySet& r) -bool operator==(const RsTlvSecurityKey& sk1, const RsTlvSecurityKey& sk2) +bool operator==(const RsTlvSecurityKey_deprecated& sk1, const RsTlvSecurityKey_deprecated& sk2) { if(sk1.startTS != sk2.startTS) return false; @@ -183,7 +183,7 @@ bool operator==(const RsTlvBinaryData& bd1, const RsTlvBinaryData& bd2) } -void init_item(RsTlvSecurityKey& sk) +void init_item(RsTlvSecurityKey_deprecated& sk) { int randnum = rand()%313131; diff --git a/tests/unittests/libretroshare/serialiser/support.h b/tests/unittests/libretroshare/serialiser/support.h index 5841d22f5..0af3a3ebf 100644 --- a/tests/unittests/libretroshare/serialiser/support.h +++ b/tests/unittests/libretroshare/serialiser/support.h @@ -58,7 +58,7 @@ void randString(const uint32_t, std::wstring&); /* for testing compound tlv items */ -void init_item(RsTlvSecurityKey&); +void init_item(RsTlvSecurityKey_deprecated&); void init_item(RsTlvSecurityKeySet&); void init_item(RsTlvKeySignature&); void init_item(RsTlvKeySignatureSet&); @@ -70,7 +70,7 @@ void init_item(RsTlvPeerIdSet&); void init_item(RsTlvImage&); void init_item(RsTlvPeerIdSet&); -bool operator==(const RsTlvSecurityKey&, const RsTlvSecurityKey& ); +bool operator==(const RsTlvSecurityKey_deprecated&, const RsTlvSecurityKey_deprecated& ); bool operator==(const RsTlvSecurityKeySet&, const RsTlvSecurityKeySet& ); bool operator==(const RsTlvKeySignature&, const RsTlvKeySignature& ); bool operator==(const RsTlvBinaryData&, const RsTlvBinaryData&); diff --git a/tests/unittests/libretroshare/serialiser/tlvrandom_test.cc b/tests/unittests/libretroshare/serialiser/tlvrandom_test.cc index 3a087d1f8..60483e5e3 100644 --- a/tests/unittests/libretroshare/serialiser/tlvrandom_test.cc +++ b/tests/unittests/libretroshare/serialiser/tlvrandom_test.cc @@ -68,7 +68,7 @@ int test_TlvRandom(void *data, uint32_t len, uint32_t offset) uint32_t tmpoffset = 0; /* List of all the TLV types it could be! */ - RsTlvSecurityKey skey; + RsTlvSecurityKey_deprecated skey; RsTlvSecurityKeySet skeyset; RsTlvKeySignature keysign; @@ -114,7 +114,7 @@ int test_TlvRandom(void *data, uint32_t len, uint32_t offset) EXPECT_TRUE(test_SetTlvItem(&kv, TLV_TYPE_KEYVALUE, data, len, offset)); EXPECT_TRUE(test_SetTlvItem(&kvset, TLV_TYPE_KEYVALUESET, data, len, offset)); std::cerr << "test_TlvRandom:: Testing Keys (TYPESET)" << std::endl; - EXPECT_TRUE(test_SetTlvItem(&skey, TLV_TYPE_SECURITYKEY, data, len, offset)); + EXPECT_TRUE(test_SetTlvItem(&skey, TLV_TYPE_SECURITYKEY_deprecated, data, len, offset)); EXPECT_TRUE(test_SetTlvItem(&skeyset, TLV_TYPE_SECURITYKEYSET, data, len, offset)); EXPECT_TRUE(test_SetTlvItem(&keysign, TLV_TYPE_KEYSIGNATURE, data, len, offset)); From a2ca0385db59a648c37757cd5d18710b1ad3c87e Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 4 Jun 2016 21:39:40 -0400 Subject: [PATCH 02/13] fixed compilation and several problems following separation of public/private keys --- libretroshare/src/chat/rschatitems.h | 2 +- libretroshare/src/grouter/p3grouter.cc | 2 - libretroshare/src/gxs/gxssecurity.cc | 33 +- libretroshare/src/gxs/gxssecurity.h | 9 + libretroshare/src/gxs/rsgenexchange.cc | 49 +-- libretroshare/src/gxs/rsgenexchange.h | 9 - libretroshare/src/gxs/rsgxsnetservice.cc | 12 +- .../src/serialiser/rsgxsrecognitems.h | 2 +- libretroshare/src/serialiser/rsnxsitems.cc | 10 +- libretroshare/src/serialiser/rsnxsitems.h | 3 +- libretroshare/src/serialiser/rstlvbase.h | 14 +- libretroshare/src/serialiser/rstlvkeys.cc | 207 ++-------- libretroshare/src/serialiser/rstlvkeys.h | 46 +-- libretroshare/src/services/p3idservice.cc | 370 ++++++++---------- libretroshare/src/services/p3idservice.h | 44 +-- libretroshare/src/util/rsmemcache.h | 2 +- 16 files changed, 291 insertions(+), 523 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.h b/libretroshare/src/chat/rschatitems.h index ab75f8cec..13b42c18f 100644 --- a/libretroshare/src/chat/rschatitems.h +++ b/libretroshare/src/chat/rschatitems.h @@ -406,7 +406,7 @@ class RsChatDHPublicKeyItem: public RsChatItem BIGNUM *public_key ; 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: RsChatDHPublicKeyItem(const RsChatDHPublicKeyItem&) : RsChatItem(RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY) {} // make the object non copy-able diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 9d72bca49..9d79920e0 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -1958,8 +1958,6 @@ bool p3GRouter::signDataItem(RsGRouterAbstractMsgItem *item,const RsGxsId& signi { try { - RsTlvSecurityKey_deprecated signature_key ; - #ifdef GROUTER_DEBUG std::cerr << "p3GRouter::signDataItem()" << std::endl; std::cerr << " Key ID = " << signing_id << std::endl; diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 5042267ef..65d093959 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -174,17 +174,6 @@ bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) 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) { // admin keys @@ -991,8 +980,7 @@ bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& s #endif /* extract admin key */ -#warning Souldn't need to do that HERE!! - RSA *rsakey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)? d2i_RSAPrivateKey(NULL, &(keyptr), keylen): d2i_RSAPublicKey(NULL, &(keyptr), keylen); + RSA *rsakey = d2i_RSAPublicKey(NULL, &(keyptr), keylen); if (!rsakey) { @@ -1067,4 +1055,21 @@ bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& s return false; } - +void GxsSecurity::createPublicKeysFromPrivateKeys(RsTlvSecurityKeySet& keyset) +{ + for( std::map::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; + } +} diff --git a/libretroshare/src/gxs/gxssecurity.h b/libretroshare/src/gxs/gxssecurity.h index 4642cc512..126c496d1 100644 --- a/libretroshare/src/gxs/gxssecurity.h +++ b/libretroshare/src/gxs/gxssecurity.h @@ -125,6 +125,15 @@ class GxsSecurity static bool checkPublicKey(const RsTlvPublicRSAKey &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 diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 3b512c90d..a8b847481 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -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::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) { #ifdef GEN_EXCH_DEBUG @@ -2166,7 +2152,9 @@ void RsGenExchange::processGroupUpdatePublish() if(checkKeys(meta->keys)) { ggps.mKeys = meta->keys; - generatePublicFromPrivateKeys(ggps.mKeys); + + GxsSecurity::createPublicKeysFromPrivateKeys(ggps.mKeys) ; + ggps.mHaveKeys = true; ggps.mStartTS = time(NULL); ggps.mLastAttemptTS = 0; @@ -2581,29 +2569,30 @@ RsGeneralDataService* RsGenExchange::getDataStore() bool RsGenExchange::getGroupKeys(const RsGxsGroupId &grpId, RsTlvSecurityKeySet &keySet) { - if(grpId.isNull()) - return false; + if(grpId.isNull()) + return false; - RS_STACK_MUTEX(mGenMtx) ; + RS_STACK_MUTEX(mGenMtx) ; - std::map grpMeta; - grpMeta[grpId] = NULL; - mDataStore->retrieveGxsGrpMetaData(grpMeta); + std::map grpMeta; + grpMeta[grpId] = NULL; + mDataStore->retrieveGxsGrpMetaData(grpMeta); - if(grpMeta.empty()) - return false; + if(grpMeta.empty()) + return false; - RsGxsGrpMetaData* meta = grpMeta[grpId]; + RsGxsGrpMetaData* meta = grpMeta[grpId]; - if(meta == NULL) - return false; + if(meta == NULL) + return false; - keySet = meta->keys; + keySet = meta->keys; + GxsSecurity::createPublicKeysFromPrivateKeys(keySet) ; - for(std::map::iterator it=grpMeta.begin();it!=grpMeta.end();++it) - delete it->second ; + for(std::map::iterator it=grpMeta.begin();it!=grpMeta.end();++it) + delete it->second ; - return true; + return true; } void RsGenExchange::shareGroupPublishKey(const RsGxsGroupId& grpId,const std::set& peers) diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index 2b72c9d5e..6535ef510 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -743,15 +743,6 @@ private: */ 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 * @param msg message to be validated diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index dae7b5089..2e15206fa 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -4877,7 +4877,7 @@ void RsGxsNetService::sharePublishKeysPending() publishKeyItem->clear(); publishKeyItem->grpId = mit->first; - publishKeyItem->key = publishKey ; + publishKeyItem->private_key = publishKey ; publishKeyItem->PeerId(*it); sendItem(publishKeyItem); @@ -4932,8 +4932,8 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) GXSNETDEBUG_PG(item->PeerId(),item->grpId)<< " Key received: " << std::endl; #endif - bool admin = (item->key.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) && (item->key.keyFlags & RSTLV_KEY_TYPE_FULL) ; - bool publi = (item->key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (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->private_key.keyFlags & RSTLV_KEY_DISTRIB_PUBLISH) && (item->private_key.keyFlags & RSTLV_KEY_TYPE_FULL) ; #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; @@ -4946,13 +4946,13 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) } // 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; 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 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. - 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) ; diff --git a/libretroshare/src/serialiser/rsgxsrecognitems.h b/libretroshare/src/serialiser/rsgxsrecognitems.h index fedc7eafb..a8aad7c31 100644 --- a/libretroshare/src/serialiser/rsgxsrecognitems.h +++ b/libretroshare/src/serialiser/rsgxsrecognitems.h @@ -117,7 +117,7 @@ virtual void clear(); std::ostream &print(std::ostream &out, uint16_t indent = 0); RsTlvServiceIdSet signing_classes; - RsTlvSecurityKey_deprecated key; // has from->to, and flags. + RsTlvPublicRSAKey key; // has from->to, and flags. RsTlvKeySignature sign; }; diff --git a/libretroshare/src/serialiser/rsnxsitems.cc b/libretroshare/src/serialiser/rsnxsitems.cc index 55501195d..2c8ef9baa 100644 --- a/libretroshare/src/serialiser/rsnxsitems.cc +++ b/libretroshare/src/serialiser/rsnxsitems.cc @@ -401,7 +401,7 @@ bool RsNxsGroupPublishKeyItem::serialise(void *data, uint32_t& size) const return false ; ok &= grpId.serialise(data, size, offset) ; - ok &= key.SetTlv(data, size, &offset) ; + ok &= private_key.SetTlv(data, size, &offset) ; if(offset != tlvsize) { @@ -811,7 +811,7 @@ RsNxsGroupPublishKeyItem* RsNxsSerialiser::deserialNxsGroupPublishKeyItem(void * RsNxsGroupPublishKeyItem* item = new RsNxsGroupPublishKeyItem(SERVICE_TYPE); 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) { @@ -957,7 +957,7 @@ uint32_t RsNxsGroupPublishKeyItem::serial_size() const uint32_t s = 8; // header size s += grpId.serial_size() ; - s += key.TlvSize(); + s += private_key.TlvSize(); return s; } @@ -1091,7 +1091,7 @@ void RsNxsSyncGrpReqItem::clear() } void RsNxsGroupPublishKeyItem::clear() { - key.TlvClear(); + private_key.TlvClear(); } void RsNxsSyncMsgReqItem::clear() { @@ -1163,7 +1163,7 @@ std::ostream& RsNxsGroupPublishKeyItem::print(std::ostream &out, uint16_t indent printIndent(out , int_Indent); out << "GroupId: " << grpId << std::endl; printIndent(out , int_Indent); - out << "keyId: " << key.keyId << std::endl; + out << "keyId: " << private_key.keyId << std::endl; printRsItemEnd(out ,"RsNxsGroupPublishKeyItem", indent); diff --git a/libretroshare/src/serialiser/rsnxsitems.h b/libretroshare/src/serialiser/rsnxsitems.h index 8bc19d501..2f4f855f6 100644 --- a/libretroshare/src/serialiser/rsnxsitems.h +++ b/libretroshare/src/serialiser/rsnxsitems.h @@ -157,8 +157,7 @@ public: virtual uint32_t serial_size() const; RsGxsGroupId grpId ; -#warning should be renamed private_key - RsTlvPrivateRSAKey key ; + RsTlvPrivateRSAKey private_key ; }; diff --git a/libretroshare/src/serialiser/rstlvbase.h b/libretroshare/src/serialiser/rstlvbase.h index 8e463214f..73e0112f4 100644 --- a/libretroshare/src/serialiser/rstlvbase.h +++ b/libretroshare/src/serialiser/rstlvbase.h @@ -207,14 +207,16 @@ const uint16_t TLV_TYPE_GXSCIRCLEIDSET= 0x1026; const uint16_t TLV_TYPE_SERVICESET = 0x1030; -const uint16_t TLV_TYPE_SECURITYKEY_deprecated = 0x1040; -const uint16_t TLV_TYPE_SECURITYKEYSET = 0x1041; -const uint16_t TLV_TYPE_RSA_KEY_PUBLIC = 0x1042; -const uint16_t TLV_TYPE_RSA_KEY_PRIVATE= 0x1043; +// *_deprectate should not be used anymore!! +// We use 1040 for both public and private keys, so that transmitting them still works (backward compatibility), and so that +// signatures are kept. But the two different classes will check that the flags are correct when deserialising. -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_KEYSIGNATURETYPE = 0x1052; +const uint16_t TLV_TYPE_KEYSIGNATURETYPE = 0x1052; const uint16_t TLV_TYPE_IMAGE = 0x1060; diff --git a/libretroshare/src/serialiser/rstlvkeys.cc b/libretroshare/src/serialiser/rstlvkeys.cc index 302951f9f..d50747cb6 100644 --- a/libretroshare/src/serialiser/rstlvkeys.cc +++ b/libretroshare/src/serialiser/rstlvkeys.cc @@ -38,13 +38,13 @@ /************************************* RsTlvSecurityKey ************************************/ -RsTlvSecurityKey_deprecated::RsTlvSecurityKey_deprecated() +RsTlvRSAKey::RsTlvRSAKey() :RsTlvItem(), keyFlags(0), startTS(0), endTS(0), keyData(TLV_TYPE_KEY_EVP_PKEY) { return; } -void RsTlvSecurityKey_deprecated::TlvClear() +void RsTlvRSAKey::TlvClear() { keyId.clear(); keyFlags = 0; @@ -54,7 +54,7 @@ void RsTlvSecurityKey_deprecated::TlvClear() } /* clears keyData - but doesn't delete */ -void RsTlvSecurityKey_deprecated::ShallowClear() +void RsTlvRSAKey::ShallowClear() { keyId.clear(); keyFlags = 0; @@ -70,21 +70,6 @@ uint32_t RsTlvRSAKey::TlvSize() const /* 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 s += GetTlvStringSize(keyId.toStdString()) ; #else @@ -116,44 +101,7 @@ bool RsTlvRSAKey::SetTlv(void *data, uint32_t size, uint32_t *offset) const /* start at data[offset] */ /* add mandatory parts first */ - ok &= SetTlvBase(data, tlvend, offset, tlvType(), 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); - + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_SECURITY_KEY, tlvsize); #ifdef KEEP_OLD_SIGNATURE_SERIALISE_FORMAT ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_KEYID, keyId.toStdString()); #else @@ -186,7 +134,7 @@ bool RsTlvRSAKey::GetTlv(void *data, uint32_t size, uint32_t *offset) return false; /* not enough space */ } - if (tlvtype != tlvType()) /* check type */ + if (tlvtype != TLV_TYPE_SECURITY_KEY) /* check type */ { #ifdef TLV_DEBUG 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 */ (*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 std::string 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, &(endTS)); ok &= keyData.GetTlv(data, tlvend, offset); - /*************************************************************************** * 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; #endif } - - return ok; + return ok && checkFlags(keyFlags) ; } - -std::ostream &RsTlvSecurityKey_deprecated::print(std::ostream &out, uint16_t indent) const +std::ostream& RsTlvRSAKey::print(std::ostream &out, uint16_t indent) const { printBase(out, "RsTlvSecurityKey", indent); 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 */ 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) { - 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) - private_keys[key.keyId] = RsTlvPrivateRSAKey(key); - else - public_keys[key.keyId] = RsTlvPublicRSAKey(key); + RsTlvPrivateRSAKey private_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 ; - 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: ok &= SkipUnknownTlv(data, tlvend, offset); break; @@ -513,6 +365,7 @@ bool RsTlvSecurityKeySet::GetTlv(void *data, uint32_t size, uint32_t *offset) return ok; } + // prints out contents of RsTlvSecurityKeySet std::ostream &RsTlvSecurityKeySet::print(std::ostream &out, uint16_t indent) const { @@ -855,3 +708,23 @@ uint32_t RsTlvKeySignatureSet::TlvSize() const return s; } + + + + + + + + + + + + + + + + + + + + diff --git a/libretroshare/src/serialiser/rstlvkeys.h b/libretroshare/src/serialiser/rstlvkeys.h index d65f92b2d..92e09b281 100644 --- a/libretroshare/src/serialiser/rstlvkeys.h +++ b/libretroshare/src/serialiser/rstlvkeys.h @@ -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 -class RsTlvSecurityKey_deprecated: 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 +class RsTlvRSAKey: public RsTlvItem { public: - virtual uint32_t tlvType() const = 0 ; - virtual bool checkFlags(uint32_t flags) const = 0 ; + RsTlvRSAKey(); + 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 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; + virtual std::ostream& print(std::ostream &out, uint16_t indent) const; /* clears KeyData - but doesn't delete - to transfer ownership */ void ShallowClear(); @@ -100,24 +78,16 @@ public: class RsTlvPrivateRSAKey: public RsTlvRSAKey { public: - RsTlvPrivateRSAKey(); - explicit RsTlvPrivateRSAKey(RsTlvSecurityKey_deprecated& key); // convertion tool. Should be called explicitely and performs some checks - virtual ~RsTlvPrivateRSAKey() {} 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 { -#warning TEST IF WE CAN CONVERT PUBLIC TO PRIVATE AND VIS VERSA. NORMALLY WE SHOULDNT public: - 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 uint32_t tlvType() const { return TLV_TYPE_RSA_KEY_PUBLIC ; } }; class RsTlvSecurityKeySet: public RsTlvItem @@ -126,7 +96,9 @@ public: 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 void TlvClear(); @@ -137,10 +109,6 @@ public: std::string groupId; // Mandatory : std::map public_keys; // Mandatory : std::map 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;} }; diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index a9e38b02c..52735980f 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -147,8 +147,7 @@ RsIdentity *rsIdentity = NULL; p3IdService::p3IdService(RsGeneralDataService *gds, RsNetworkExchangeService *nes, PgpAuxUtils *pgpUtils) : RsGxsIdExchange(gds, nes, new RsGxsIdSerialiser(), RS_SERVICE_GXS_TYPE_GXSID, idAuthenPolicy()), RsIdentity(this), GxsTokenQueue(this), RsTickEvent(), - mPublicKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdPublicKeyCache"), - mPrivateKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdPrivateKeyCache"), + mKeyCache(GXSID_MAX_CACHE_SIZE, "GxsIdKeyCache"), mIdMtx("p3IdService"), mNes(nes), mPgpUtils(pgpUtils) { @@ -445,49 +444,31 @@ bool p3IdService:: getIdDetails(const RsGxsId &id, RsIdentityDetails &details) std::cerr << std::endl; #endif - { - RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - - { - RsGxsIdCache 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 ; + { + RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - // 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) - details.mNickname = "[too long a name]" ; - - rsReputations->getReputationInfo(id,details.mReputation) ; + RsGxsIdCache data; - return true; - } - } + if (mKeyCache.fetch(id, data)) + { + details = data.details; + details.mLastUsageTS = locked_getLastUsageTS(id) ; - RsGxsIdCache data; - - /* 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 ; + if(mContacts.find(id) != mContacts.end()) + details.mFlags |= RS_IDENTITY_FLAGS_IS_A_CONTACT ; - rsReputations->getReputationInfo(id,details.mReputation) ; - - return true; - } - } + // 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) + details.mNickname = "[too long a name]" ; + + rsReputations->getReputationInfo(id,details.mReputation) ; + + return true; + } + } /* it isn't there - add to public requests */ - cache_request_load(id); + cache_request_load(id); return false; } @@ -598,36 +579,24 @@ bool p3IdService::getRecognTagRequest(const RsGxsId &id, const std::string &comm std::cerr << "p3IdService::getRecognTagRequest()"; std::cerr << std::endl; #endif - - if (!havePrivateKey(id)) + if(!isOwnId(id)) { -#ifdef DEBUG_RECOGN - std::cerr << "p3IdService::getRecognTagRequest() Dont have private key"; - std::cerr << std::endl; -#endif - // attempt to load it. - cache_request_load(id); - return false; + std::cerr << "(EE) cannot retrieve own key to create tag request. KeyId=" << id << std::endl; + return false ; } RsTlvPrivateRSAKey key; std::string nickname; + RsGxsIdCache data ; { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache 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; - nickname = data.details.mNickname; + if(!mKeyCache.fetch(id, data)) + return false ; + + nickname = data.details.mNickname ; + key = data.priv_key ; } 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) { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - return mPublicKeyCache.is_cached(id); + return mKeyCache.is_cached(id); } bool p3IdService::havePrivateKey(const RsGxsId &id) { + if(! isOwnId(id)) + return false ; + 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 &peers) @@ -678,21 +650,21 @@ bool p3IdService::isPendingNetworkRequest(const RsGxsId& gxsId) bool p3IdService::getKey(const RsGxsId &id, RsTlvPublicRSAKey &key) { - { - RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; - - if (mPublicKeyCache.fetch(id, data)) { - key = data.pubkey; - return true; - } - } + RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ + RsGxsIdCache data; - cache_request_load(id); + if (mKeyCache.fetch(id, data)) + { + key = data.pub_key; + return true; + } + } - key.keyId.clear() ; - return false; + cache_request_load(id); + + key.keyId.clear() ; + return false; } bool p3IdService::requestPrivateKey(const RsGxsId &id) @@ -705,21 +677,21 @@ bool p3IdService::requestPrivateKey(const RsGxsId &id) bool p3IdService::getPrivateKey(const RsGxsId &id, RsTlvPrivateRSAKey &key) { - { - RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; - - if (mPrivateKeyCache.fetch(id, data)) - { - key = data.pubkey; - return true; - } - } + { + RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ + RsGxsIdCache data; - key.keyId.clear() ; - cache_request_load(id); + if (mKeyCache.fetch(id, data)) + { + 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 */ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache data; + RsGxsIdCache data; - if (mPublicKeyCache.fetch(id, data)) + if (mKeyCache.fetch(id, data)) { rep.id = id; rep.score = 0;//data.details.mReputation.mOverallScore; @@ -1155,24 +1127,24 @@ bool p3IdService::createGroup(uint32_t& token, RsGxsIdGroup &group) return true; } -bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group) +bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group) { - RsGxsId id(group.mMeta.mGroupId); - RsGxsIdGroupItem* item = new RsGxsIdGroupItem(); + RsGxsId id(group.mMeta.mGroupId); + RsGxsIdGroupItem* item = new RsGxsIdGroupItem(); - item->fromGxsIdGroup(group,false) ; + item->fromGxsIdGroup(group,false) ; #ifdef DEBUG_IDS std::cerr << "p3IdService::updateGroup() Updating RsGxsId: " << id; std::cerr << std::endl; #endif - - RsGenExchange::updateGroup(token, item); + + RsGenExchange::updateGroup(token, item); // if its in the cache - clear it. { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - if (mPublicKeyCache.erase(id)) + if (mKeyCache.erase(id)) { #ifdef DEBUG_IDS std::cerr << "p3IdService::updateGroup() Removed from PublicKeyCache"; @@ -1184,21 +1156,6 @@ bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group) #ifdef DEBUG_IDS std::cerr << "p3IdService::updateGroup() Not in PublicKeyCache"; 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 } } @@ -1223,7 +1180,7 @@ bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group) // if its in the cache - clear it. { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - if (mPublicKeyCache.erase(id)) + if (mKeyCache.erase(id)) { #ifdef DEBUG_IDS std::cerr << "p3IdService::deleteGroup() Removed from PublicKeyCache"; @@ -1238,21 +1195,6 @@ bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group) #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::iterator lit = std::find( mOwnIds.begin(), mOwnIds.end(), id); 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 &tagList) +{ + init(item,in_pkey,RsTlvPrivateRSAKey(),tagList) ; +} + +RsGxsIdCache::RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const RsTlvPrivateRSAKey& privkey, const std::list &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 &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) { details.mRecognTags.clear(); @@ -1853,6 +1848,7 @@ bool p3IdService::cache_process_recogntaginfo(const RsGxsIdGroupItem *item, std: 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) { @@ -1865,56 +1861,42 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item) /* extract key from keys */ RsTlvSecurityKeySet keySet; - RsTlvSecurityKey_deprecated pubkey; - RsTlvSecurityKey_deprecated fullkey; + + RsTlvPublicRSAKey pubkey; + RsTlvPrivateRSAKey fullkey; + bool pub_key_ok = false; bool full_key_ok = false; RsGxsId id (item->meta.mGroupId.toStdString()); + if (!getGroupKeys(RsGxsGroupId(id.toStdString()), keySet)) { - std::cerr << "p3IdService::cache_store() ERROR getting GroupKeys for: "; - std::cerr << item->meta.mGroupId; - std::cerr << std::endl; + std::cerr << "p3IdService::cache_store() ERROR getting GroupKeys for: "<< item->meta.mGroupId << std::endl; return false; } - std::map::iterator kit; - - //std::cerr << "p3IdService::cache_store() KeySet is:"; - //keySet.print(std::cerr, 10); - - for (kit = keySet.keys.begin(); kit != keySet.keys.end(); ++kit) - { + for (std::map::iterator kit = keySet.private_keys.begin(); kit != keySet.private_keys.end(); ++kit) if (kit->second.keyFlags & RSTLV_KEY_DISTRIB_ADMIN) { #ifdef DEBUG_IDS - std::cerr << "p3IdService::cache_store() Found Admin Key"; - std::cerr << std::endl; -#endif // DEBUG_IDS - - /* 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 - */ - + std::cerr << "p3IdService::cache_store() Found Admin Key" << std::endl; +#endif + fullkey = kit->second; + full_key_ok = true; } - } + for (std::map::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) { @@ -1929,19 +1911,11 @@ bool p3IdService::cache_store(const RsGxsIdGroupItem *item) RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - assert(!(pubkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ; - // Create Cache Data. - RsGxsIdCache pubcache(item, pubkey, tagList); - mPublicKeyCache.store(id, pubcache); - mPublicKeyCache.resize(); - - if (full_key_ok) - { - RsGxsIdCache fullcache(item, fullkey, tagList); - mPrivateKeyCache.store(id, fullcache); - mPrivateKeyCache.resize(); - } + RsGxsIdCache keycache(item, pubkey, fullkey,tagList); + + mKeyCache.store(id, keycache); + mKeyCache.resize(); return true; } @@ -2145,31 +2119,18 @@ bool p3IdService::cache_update_if_cached(const RsGxsId &id, std::string serviceS /* retrieve - update, save */ RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ - RsGxsIdCache pub_data; - if (mPublicKeyCache.fetch(id, pub_data)) + RsGxsIdCache updated_data; + + if(mKeyCache.fetch(id, updated_data)) { #ifdef DEBUG_IDS std::cerr << "p3IdService::cache_update_if_cached() Updating Public Cache"; std::cerr << std::endl; #endif // DEBUG_IDS - assert(!(pub_data.pubkey.keyFlags & RSTLV_KEY_TYPE_FULL)) ; - - pub_data.updateServiceString(serviceString); - 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); + updated_data.updateServiceString(serviceString); + + mKeyCache.store(id, updated_data); } return true; @@ -2338,7 +2299,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token) } else { - RsTlvSecurityKey_deprecated seckey; + RsTlvPublicRSAKey seckey; if (getKey(*vit, seckey)) { #ifdef DEBUG_IDS @@ -2370,7 +2331,7 @@ bool p3IdService::cachetest_handlerequest(uint32_t token) } else { - RsTlvSecurityKey_deprecated seckey; + RsTlvPrivateRSAKey seckey; if (getPrivateKey(*vit, seckey)) { // success! @@ -2509,22 +2470,19 @@ RsGenExchange::ServiceCreate_Return p3IdService::service_CreateGroup(RsGxsGrpIte item->print(std::cerr); std::cerr << std::endl; #endif // DEBUG_IDS + + item->meta.mGroupId.clear(); /********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/ // find private admin key - std::map::iterator mit = keySet.keys.begin(); - for(; mit != keySet.keys.end(); ++mit) - { - RsTlvSecurityKey_deprecated& pk = mit->second; - - if(pk.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL)) + for(std::map::iterator mit = keySet.private_keys.begin();mit != keySet.private_keys.end(); ++mit) + if(mit->second.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL)) { - item->meta.mGroupId = RsGxsGroupId(pk.keyId); + item->meta.mGroupId = RsGxsGroupId(mit->second.keyId); break; } - } - if(mit == keySet.keys.end()) + if(item->meta.mGroupId.isNull()) { std::cerr << "p3IdService::service_CreateGroup() ERROR no admin key"; std::cerr << std::endl; diff --git a/libretroshare/src/services/p3idservice.h b/libretroshare/src/services/p3idservice.h index e64b931ff..316fad106 100644 --- a/libretroshare/src/services/p3idservice.h +++ b/libretroshare/src/services/p3idservice.h @@ -190,40 +190,13 @@ virtual std::string save() const; class RsGxsIdGroupItem; -template class RsGxsIdCache +class RsGxsIdCache { public: RsGxsIdCache(); - RsGxsIdCache(const RsGxsIdGroupItem *item, const KeyClass& in_pkey, const std::list &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); - } + RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const RsTlvPrivateRSAKey& privkey, const std::list &tagList); + RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pkey, const std::list &tagList); void updateServiceString(std::string serviceString); @@ -231,8 +204,12 @@ public: std::list mRecognTags; // Only partially validated. 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 &tagList); }; @@ -380,8 +357,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel); std::map > mCacheLoad_ToCache, mPendingCache; // Switching to RsMemCache for Key Caching. - RsMemCache > mPublicKeyCache; - RsMemCache > mPrivateKeyCache; + RsMemCache mKeyCache; /************************************************************************ * Refreshing own Ids. diff --git a/libretroshare/src/util/rsmemcache.h b/libretroshare/src/util/rsmemcache.h index 612fe5f04..ae383b3c3 100644 --- a/libretroshare/src/util/rsmemcache.h +++ b/libretroshare/src/util/rsmemcache.h @@ -143,7 +143,7 @@ template template bool RsMemCache bool RsMemCache::fetch(const Key &key, Value &data) +template bool RsMemCache::fetch(const Key &key, Value& data) { #ifdef DEBUG_RSMEMCACHE std::cerr << "RsMemCache::fetch()"; From 927f782bef4ee4f9f2df04e1808da6dbd756129e Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 5 Jun 2016 10:43:57 -0400 Subject: [PATCH 03/13] pass over compiler warnings --- libresapi/src/api/IdentityHandler.cpp | 2 +- libresapi/src/api/ResourceRouter.cpp | 2 +- libresapi/src/api/RsControlModule.cpp | 2 +- libresapi/src/api/TransfersHandler.cpp | 4 +-- libretroshare/src/pqi/pqibin.cc | 2 +- libretroshare/src/pqi/pqistreamer.cc | 4 +-- libretroshare/src/rsserver/p3peers.cc | 6 ++-- libretroshare/src/rsserver/rsloginhandler.cc | 15 +++++---- libretroshare/src/services/p3gxscircles.cc | 32 ++++++++++--------- .../FeedReader/services/p3FeedReaderThread.cc | 6 ++-- .../src/gui/FileTransfer/xprogressbar.cpp | 2 +- retroshare-gui/src/gui/Identity/IdDialog.cpp | 2 +- retroshare-gui/src/gui/NewsFeed.cpp | 6 ++-- retroshare-gui/src/gui/Posted/PostedItem.cpp | 3 +- retroshare-gui/src/gui/common/AvatarDefs.cpp | 2 +- .../src/gui/common/RSGraphWidget.cpp | 4 +-- retroshare-gui/src/gui/gxs/GxsIdLabel.cpp | 1 + .../src/gui/msgs/MessageComposer.cpp | 2 ++ retroshare-gui/src/gui/msgs/MessageComposer.h | 2 ++ .../gui/settings/RSPermissionMatrixWidget.cpp | 32 +++---------------- .../src/gui/settings/ServerPage.cpp | 2 +- .../gui/statistics/GlobalRouterStatistics.cpp | 2 +- .../src/gui/statistics/TurtleRouterDialog.cpp | 4 +-- 23 files changed, 66 insertions(+), 73 deletions(-) diff --git a/libresapi/src/api/IdentityHandler.cpp b/libresapi/src/api/IdentityHandler.cpp index 2b0b41421..6d017fad8 100644 --- a/libresapi/src/api/IdentityHandler.cpp +++ b/libresapi/src/api/IdentityHandler.cpp @@ -218,7 +218,7 @@ ResponseTask* IdentityHandler::handleOwn(Request & /* req */, Response &resp) return 0; } -ResponseTask* IdentityHandler::handleCreateIdentity(Request &req, Response &resp) +ResponseTask* IdentityHandler::handleCreateIdentity(Request & /* req */, Response & /* resp */) { return new CreateIdentityTask(mRsIdentity); } diff --git a/libresapi/src/api/ResourceRouter.cpp b/libresapi/src/api/ResourceRouter.cpp index 67898c3a8..a13201a27 100644 --- a/libresapi/src/api/ResourceRouter.cpp +++ b/libresapi/src/api/ResourceRouter.cpp @@ -12,7 +12,7 @@ public: { addResourceHandler("eins", this, &TestResource::eins); } - ResponseTask* eins(Request& req, Response& resp) + ResponseTask* eins(Request& /* req */, Response& /* resp */) { return 0; } diff --git a/libresapi/src/api/RsControlModule.cpp b/libresapi/src/api/RsControlModule.cpp index 35a41987a..9dfe55aad 100644 --- a/libresapi/src/api/RsControlModule.cpp +++ b/libresapi/src/api/RsControlModule.cpp @@ -55,7 +55,7 @@ bool RsControlModule::processShouldExit() return mProcessShouldExit; } -bool RsControlModule::askForPassword(const std::string &key_details, bool prev_is_bad, std::string &password, bool& cancelled) +bool RsControlModule::askForPassword(const std::string &key_details, bool /* prev_is_bad */, std::string &password, bool& cancelled) { cancelled = false ; { diff --git a/libresapi/src/api/TransfersHandler.cpp b/libresapi/src/api/TransfersHandler.cpp index 2368cb67d..2b936e032 100644 --- a/libresapi/src/api/TransfersHandler.cpp +++ b/libresapi/src/api/TransfersHandler.cpp @@ -42,7 +42,7 @@ void TransfersHandler::tick() } } -void TransfersHandler::handleWildcard(Request &req, Response &resp) +void TransfersHandler::handleWildcard(Request & /*req*/, Response & /*resp*/) { } @@ -108,7 +108,7 @@ void TransfersHandler::handleControlDownload(Request &req, Response &resp) resp.setFail("error: action not handled"); } -void TransfersHandler::handleDownloads(Request &req, Response &resp) +void TransfersHandler::handleDownloads(Request & /* req */, Response &resp) { tick(); resp.mStateToken = mStateToken; diff --git a/libretroshare/src/pqi/pqibin.cc b/libretroshare/src/pqi/pqibin.cc index dd76c1f20..172a23b14 100644 --- a/libretroshare/src/pqi/pqibin.cc +++ b/libretroshare/src/pqi/pqibin.cc @@ -266,7 +266,7 @@ int BinEncryptedFileInterface::readdata(void* data, int len) } - if(len <= sizeData) + if((uint64_t)len <= sizeData) { memcpy(data, this->data, len); cpyCount += len; diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index 4a4db5c63..bb924a792 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -671,7 +671,7 @@ int pqistreamer::handleincoming_locked() allocate_rpend_locked(); // enough space to read any packet. - int maxlen = mPkt_rpend_size; + uint32_t maxlen = mPkt_rpend_size; void *block = mPkt_rpending; // initial read size: basic packet. @@ -787,7 +787,7 @@ continue_packet: std::cerr << "[" << (void*)pthread_self() << "] " << "continuing packet state=" << mReading_state << std::endl ; std::cerr << "[" << (void*)pthread_self() << "] " << "block 1 : " << RsUtil::BinToHex(block,8) << std::endl; #endif - if (extralen > maxlen - blen) + if (extralen + (uint32_t)blen > maxlen) { pqioutput(PQL_ALERT, pqistreamerzone, "ERROR: Read Packet too Big!"); diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index dc86e8194..e516b2f28 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -1381,8 +1381,10 @@ RsPeerDetails::RsPeerDetails() trustLvl(0), validLvl(0),ownsign(false), hasSignedMe(false),accept_connection(false), state(0),localAddr(""),localPort(0),extAddr(""),extPort(0),netMode(0),vs_disc(0), vs_dht(0), - lastConnect(0),connectState(0),connectStateString(""),connectPeriod(0), - hiddenType(RS_HIDDEN_TYPE_NONE), foundDHT(false), wasDeniedConnection(false), deniedTS(0) + lastConnect(0),connectState(0),connectStateString(""), + hiddenType(RS_HIDDEN_TYPE_NONE), + connectPeriod(0), + foundDHT(false), wasDeniedConnection(false), deniedTS(0) { } diff --git a/libretroshare/src/rsserver/rsloginhandler.cc b/libretroshare/src/rsserver/rsloginhandler.cc index e5d346b61..a62381432 100644 --- a/libretroshare/src/rsserver/rsloginhandler.cc +++ b/libretroshare/src/rsserver/rsloginhandler.cc @@ -7,12 +7,15 @@ #if defined(HAS_GNOME_KEYRING) || defined(__FreeBSD__) || defined(__OpenBSD__) #include - GnomeKeyringPasswordSchema my_schema = { - GNOME_KEYRING_ITEM_ENCRYPTION_KEY_PASSWORD, - { - { "RetroShare SSL Id", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, - { NULL, (GnomeKeyringAttributeType)0 } - } +GnomeKeyringPasswordSchema my_schema = { + GNOME_KEYRING_ITEM_ENCRYPTION_KEY_PASSWORD, + { + { "RetroShare SSL Id", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, + { NULL, (GnomeKeyringAttributeType)0 } + }, + NULL, + NULL, + NULL }; #endif diff --git a/libretroshare/src/services/p3gxscircles.cc b/libretroshare/src/services/p3gxscircles.cc index a27497dd4..60db7f0b9 100644 --- a/libretroshare/src/services/p3gxscircles.cc +++ b/libretroshare/src/services/p3gxscircles.cc @@ -1060,6 +1060,7 @@ bool p3GxsCircles::locked_processLoadingCacheEntry(RsGxsCircleCache& cache) /* check cache */ if(!(pit->second.subscription_flags & GXS_EXTERNAL_CIRCLE_FLAGS_KEY_AVAILABLE)) + { if(mIdentities->haveKey(pit->first)) { pit->second.subscription_flags |= GXS_EXTERNAL_CIRCLE_FLAGS_KEY_AVAILABLE; @@ -1068,26 +1069,27 @@ bool p3GxsCircles::locked_processLoadingCacheEntry(RsGxsCircleCache& cache) #endif } else - { - std::list peers; + { + std::list peers; - if(!cache.mOriginator.isNull()) - { - peers.push_back(cache.mOriginator) ; + if(!cache.mOriginator.isNull()) + { + peers.push_back(cache.mOriginator) ; #ifdef DEBUG_CIRCLES - std::cerr << " Requesting unknown/unloaded identity: " << pit->first << " to originator " << cache.mOriginator << std::endl; + std::cerr << " Requesting unknown/unloaded identity: " << pit->first << " to originator " << cache.mOriginator << std::endl; #endif - } - else - { - std::cerr << " (WW) cache entry for circle " << cache.mCircleId << " has empty originator. Asking info for GXS id " << pit->first << " to all connected friends." << std::endl; + } + else + { + std::cerr << " (WW) cache entry for circle " << cache.mCircleId << " has empty originator. Asking info for GXS id " << pit->first << " to all connected friends." << std::endl; - rsPeers->getOnlineList(peers) ; - } + rsPeers->getOnlineList(peers) ; + } - mIdentities->requestKey(pit->first, peers); - //isUnprocessedPeers = true; - } + mIdentities->requestKey(pit->first, peers); + //isUnprocessedPeers = true; + } + } #ifdef DEBUG_CIRCLES else std::cerr << " Key is available. Nothing to process." << std::endl; diff --git a/plugins/FeedReader/services/p3FeedReaderThread.cc b/plugins/FeedReader/services/p3FeedReaderThread.cc index 0a91a6a97..8ac64c920 100644 --- a/plugins/FeedReader/services/p3FeedReaderThread.cc +++ b/plugins/FeedReader/services/p3FeedReaderThread.cc @@ -1007,7 +1007,7 @@ std::string p3FeedReaderThread::getProxyForFeed(const RsFeedReaderFeed &feed) RsFeedReaderErrorState p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMsg *msg, std::string &errorString) { - long todo_fill_errorString; + //long todo_fill_errorString; if (!msg) { return RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR; @@ -1073,7 +1073,7 @@ RsFeedReaderErrorState p3FeedReaderThread::processMsg(const RsFeedReaderFeed &fe if (isRunning()) { /* process description */ - long todo; // encoding + //long todo; // encoding HTMLWrapper html; if (html.readHTML(msg->description.c_str(), url.c_str())) { xmlNodePtr root = html.getRootElement(); @@ -1527,7 +1527,7 @@ RsFeedReaderErrorState p3FeedReaderThread::processXslt(const std::string &xslt, RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK; /* process description */ - long todo; // encoding + //long todo; // encoding HTMLWrapper html; if (html.readHTML(description.c_str(), "")) { xmlNodePtr root = html.getRootElement(); diff --git a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp index 44491c539..a4dd414da 100644 --- a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp +++ b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp @@ -227,7 +227,7 @@ void xProgressBar::paint() linearGrad.setColorAt(1.00, gradColor1); painter->setPen(gradBorderColor); - int width = static_cast(rect.width()-1-2*hSpan) ; + uint32_t width = static_cast(rect.width()-1-2*hSpan) ; painter->setBrush(linearGrad); diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 39e751213..98d2cfff8 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -561,7 +561,7 @@ void IdDialog::loadCircleGroupMeta(const uint32_t &token) // remove any identity that has an item, but no subscription flag entry std::vector to_delete ; - for(uint32_t k=0;kchildCount();++k) + for(int k=0;kchildCount();++k) if(details.mSubscriptionFlags.find(RsGxsId(item->child(k)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString().toStdString())) == details.mSubscriptionFlags.end()) to_delete.push_back(item->child(k)); diff --git a/retroshare-gui/src/gui/NewsFeed.cpp b/retroshare-gui/src/gui/NewsFeed.cpp index 5af5bf071..76756d061 100644 --- a/retroshare-gui/src/gui/NewsFeed.cpp +++ b/retroshare-gui/src/gui/NewsFeed.cpp @@ -624,8 +624,9 @@ void NewsFeed::loadChannelPublishKey(const uint32_t &token) std::cerr << std::endl; return; } - +#ifdef UNUSED_CODE MessageComposer::sendChannelPublishKey(groups[0]); +#endif } void NewsFeed::loadForumGroup(const uint32_t &token) @@ -717,8 +718,9 @@ void NewsFeed::loadForumPublishKey(const uint32_t &token) std::cerr << std::endl; return; } - +#ifdef UNUSED_CODE MessageComposer::sendForumPublishKey(groups[0]); +#endif } void NewsFeed::loadPostedGroup(const uint32_t &token) diff --git a/retroshare-gui/src/gui/Posted/PostedItem.cpp b/retroshare-gui/src/gui/Posted/PostedItem.cpp index 5949fd2a2..314ec0df4 100644 --- a/retroshare-gui/src/gui/Posted/PostedItem.cpp +++ b/retroshare-gui/src/gui/Posted/PostedItem.cpp @@ -295,9 +295,10 @@ void PostedItem::fill() ui->voteDownButton->setEnabled(false); } - uint32_t up, down, nComments; #if 0 + uint32_t up, down, nComments; + bool ok = rsPosted->retrieveScores(mPost.mMeta.mServiceString, up, down, nComments); if(ok) diff --git a/retroshare-gui/src/gui/common/AvatarDefs.cpp b/retroshare-gui/src/gui/common/AvatarDefs.cpp index 213d67bf3..f31599189 100644 --- a/retroshare-gui/src/gui/common/AvatarDefs.cpp +++ b/retroshare-gui/src/gui/common/AvatarDefs.cpp @@ -65,7 +65,7 @@ void AvatarDefs::getAvatarFromSslId(const RsPeerId& sslId, QPixmap &avatar, cons } void AvatarDefs::getAvatarFromGxsId(const RsGxsId& gxsId, QPixmap &avatar, const QString& defaultImage) { - int size = 0; + //int size = 0; /* get avatar */ RsIdentityDetails details ; diff --git a/retroshare-gui/src/gui/common/RSGraphWidget.cpp b/retroshare-gui/src/gui/common/RSGraphWidget.cpp index bcb365baf..02cccda5a 100644 --- a/retroshare-gui/src/gui/common/RSGraphWidget.cpp +++ b/retroshare-gui/src/gui/common/RSGraphWidget.cpp @@ -524,7 +524,7 @@ void RSGraphWidget::paintTotals() float FS = QFontMetricsF(font()).height(); float fact = FS/14.0 ; - int x = SCALE_WIDTH*fact + FS, y = 0; + //int x = SCALE_WIDTH*fact + FS, y = 0; int rowHeight = FS; #if !defined(Q_OS_MAC) @@ -626,7 +626,7 @@ void RSGraphWidget::wheelEvent(QWheelEvent *e) void RSGraphWidget::paintLegend() { - int bottom = _rec.height(); + //int bottom = _rec.height(); std::vector vals ; _source->getCurrentValues(vals) ; diff --git a/retroshare-gui/src/gui/gxs/GxsIdLabel.cpp b/retroshare-gui/src/gui/gxs/GxsIdLabel.cpp index 4ec26d47f..8cae5bce0 100644 --- a/retroshare-gui/src/gui/gxs/GxsIdLabel.cpp +++ b/retroshare-gui/src/gui/gxs/GxsIdLabel.cpp @@ -45,6 +45,7 @@ static void fillLabelCallback(GxsIdDetailsType type, const RsIdentityDetails &de case GXS_ID_DETAILS_TYPE_EMPTY: case GXS_ID_DETAILS_TYPE_LOADING: case GXS_ID_DETAILS_TYPE_FAILED: + case GXS_ID_DETAILS_TYPE_BANNED: break; case GXS_ID_DETAILS_TYPE_DONE: diff --git a/retroshare-gui/src/gui/msgs/MessageComposer.cpp b/retroshare-gui/src/gui/msgs/MessageComposer.cpp index 752cb4086..04a09f9e5 100644 --- a/retroshare-gui/src/gui/msgs/MessageComposer.cpp +++ b/retroshare-gui/src/gui/msgs/MessageComposer.cpp @@ -627,6 +627,7 @@ void MessageComposer::sendConnectAttemptMsg(const RsPgpId &gpgId, const RsPeerId rsMail->SystemMessage(title.toUtf8().constData(), msgText.toUtf8().constData(), RS_MSG_USER_REQUEST); } +#ifdef UNUSED_CODE void MessageComposer::sendChannelPublishKey(RsGxsChannelGroup &group) { // QString channelName = QString::fromUtf8(group.mMeta.mGroupName.c_str()); @@ -658,6 +659,7 @@ void MessageComposer::sendForumPublishKey(RsGxsForumGroup &group) // QString msgText = tr("... %1 ...
%2").arg(forumName, link.toHtml()); // rsMail->SystemMessage(title.toUtf8().constData(), msgText.toUtf8().constData(), RS_MSG_PUBLISH_KEY); } +#endif void MessageComposer::closeEvent (QCloseEvent * event) { diff --git a/retroshare-gui/src/gui/msgs/MessageComposer.h b/retroshare-gui/src/gui/msgs/MessageComposer.h index ad286d858..f13ca7b06 100644 --- a/retroshare-gui/src/gui/msgs/MessageComposer.h +++ b/retroshare-gui/src/gui/msgs/MessageComposer.h @@ -64,8 +64,10 @@ public: static QString recommendMessage(); static void recommendFriend(const std::set &sslIds, const RsPeerId &to = RsPeerId(), const QString &msg = "", bool autoSend = false); static void sendConnectAttemptMsg(const RsPgpId &gpgId, const RsPeerId &sslId, const QString &sslName); +#ifdef UNUSED_CODE static void sendChannelPublishKey(RsGxsChannelGroup &group); static void sendForumPublishKey(RsGxsForumGroup &group); +#endif static MessageComposer *newMsg(const std::string &msgId = ""); static MessageComposer *replyMsg(const std::string &msgId, bool all); diff --git a/retroshare-gui/src/gui/settings/RSPermissionMatrixWidget.cpp b/retroshare-gui/src/gui/settings/RSPermissionMatrixWidget.cpp index cd2ffcf72..93110bb36 100644 --- a/retroshare-gui/src/gui/settings/RSPermissionMatrixWidget.cpp +++ b/retroshare-gui/src/gui/settings/RSPermissionMatrixWidget.cpp @@ -304,7 +304,7 @@ void RSPermissionMatrixWidget::paintEvent(QPaintEvent *) _painter->setPen(pen) ; int i=0; - int x=5/14.0*S ; + //int x=5/14.0*S ; int y=S*fMATRIX_START_Y ; for(std::list::const_iterator it(ssllist.begin());it!=ssllist.end();++it,++i) @@ -383,7 +383,7 @@ void RSPermissionMatrixWidget::paintEvent(QPaintEvent *) static const std::string global_switch[2] = { ":/icons/global_switch_off_128.png", ":/icons/global_switch_on_128.png" } ; - for(int i=0;igetServicePermissions(service_ids[i],serv_perm) ; @@ -450,7 +450,7 @@ void RSPermissionMatrixWidget::paintEvent(QPaintEvent *) // now display some info about current node. - if(n_row_selected < peer_ids.size() && n_col_selected < service_ids.size()) + if(n_row_selected < (int)peer_ids.size() && n_col_selected < (int)service_ids.size()) { QRect position = computeNodePosition(n_row_selected,n_col_selected,false) ; @@ -592,36 +592,14 @@ bool RSPermissionMatrixWidget::computeServiceGlobalSwitch(int x,int y,uint32_t& return true ; } -void RSPermissionMatrixWidget::defaultPermissionSwitched(uint32_t ServiceId,bool b) +void RSPermissionMatrixWidget::defaultPermissionSwitched(uint32_t /* ServiceId */,bool /* b */) { NOT_IMPLEMENTED ; } -void RSPermissionMatrixWidget::userPermissionSwitched(uint32_t ServiceId,const RsPeerId& friend_id,bool b) +void RSPermissionMatrixWidget::userPermissionSwitched(uint32_t /* ServiceId */,const RsPeerId& /* friend_id */,bool /* b */) { NOT_IMPLEMENTED ; } -// void RSGraphWidget::paintLegend() -// { -// int bottom = _rec.height(); -// -// std::vector vals ; -// _source->getCurrentValues(vals) ; -// -// for(uint i=0;ilegend(i,vals[i]) ; -// -// QPen oldPen = _painter->pen(); -// _painter->setPen(QPen(getColor(i), Qt::SolidLine)); -// _painter->drawLine(QPointF(SCALE_WIDTH+10.0, pos), QPointF(SCALE_WIDTH+30.0, pos)); -// _painter->setPen(oldPen); -// -// _painter->setPen(SCALE_COLOR); -// _painter->drawText(QPointF(SCALE_WIDTH + 40,pos + 0.5*FONT_SIZE), text) ; -// } -// } diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 45e317d84..521df3450 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -594,7 +594,7 @@ void ServerPage::moveToWhiteList2() rsBanList->addIpRange(addr,2,RSBANLIST_TYPE_WHITELIST, tr("Added by you").toStdString()); } -void ServerPage::ipWhiteListContextMenu(const QPoint& point) +void ServerPage::ipWhiteListContextMenu(const QPoint& /* point */) { QMenu contextMenu(this) ; int row = ui.whiteListIpsTable->currentRow(); diff --git a/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp b/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp index e5a35b28c..1f4bcb968 100644 --- a/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp +++ b/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp @@ -167,7 +167,7 @@ void GlobalRouterStatistics::updateContent() groupBox->setTitle(tr("Pending packets")+": " + QString::number(cache_infos.size()) ); - for(int i=0;iaddTopLevelItem(item); diff --git a/retroshare-gui/src/gui/statistics/TurtleRouterDialog.cpp b/retroshare-gui/src/gui/statistics/TurtleRouterDialog.cpp index a216e2102..d3de0e9e1 100644 --- a/retroshare-gui/src/gui/statistics/TurtleRouterDialog.cpp +++ b/retroshare-gui/src/gui/statistics/TurtleRouterDialog.cpp @@ -227,8 +227,8 @@ GxsTunnelsDialog::GxsTunnelsDialog(QWidget *parent) m_bProcessSettings = false; - float fontHeight = QFontMetricsF(font()).height(); - float fact = fontHeight/14.0; + //float fontHeight = QFontMetricsF(font()).height(); + //float fact = fontHeight/14.0; maxWidth = 200 ; maxHeight = 200 ; From f55b2838875f867a0cbafc772ecdf58d37343371 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 5 Jun 2016 11:05:52 -0400 Subject: [PATCH 04/13] second pass over compilation warnings --- libresapi/src/api/ApiServer.cpp | 2 +- libresapi/src/api/ApiServerMHD.cpp | 2 +- libresapi/src/api/PeersHandler.cpp | 2 +- libresapi/src/api/json.cpp | 2 +- retroshare-gui/src/gui/Posted/PostedListWidget.cpp | 2 +- retroshare-gui/src/gui/common/PictureFlow.cpp | 4 ++-- retroshare-gui/src/gui/common/RSGraphWidget.cpp | 12 ++++++------ retroshare-gui/src/gui/connect/PGPKeyDialog.cpp | 2 +- retroshare-gui/src/gui/msgs/MessageComposer.cpp | 2 +- retroshare-gui/src/gui/settings/ServerPage.cpp | 2 +- retroshare-gui/src/gui/statistics/BWGraph.cpp | 2 +- .../src/gui/statistics/BandwidthStatsWidget.cpp | 2 +- retroshare-gui/src/gui/statistics/BwCtrlWindow.cpp | 6 +++--- retroshare-gui/src/gui/statistics/DhtWindow.cpp | 4 ++-- .../src/gui/statistics/GlobalRouterStatistics.cpp | 2 +- retroshare-gui/src/gui/statistics/RttStatistics.cpp | 2 +- retroshare-nogui/src/notifytxt.cc | 6 +++--- 17 files changed, 28 insertions(+), 28 deletions(-) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index e434fe391..6db36f96f 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -316,7 +316,7 @@ std::string ApiServer::handleRequest(Request &request) task = mRouter.handleRequest(request, resp); } - time_t start = time(NULL); + //time_t start = time(NULL); bool morework = true; while(task && morework) { diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 55c055eb2..2ee6af149 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -424,7 +424,7 @@ static void sendMessage(MHD_Connection *connection, unsigned int status, std::st static std::string escape_html(std::string in) { std::string out; - for(int i = 0; i < in.size(); i++) + for(uint32_t i = 0; i < in.size(); i++) { char a = (in[i]&0xF0)>>4; a = a < 10? a+'0': a-10+'A'; diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index 9894836e8..0f35e59e9 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -72,7 +72,7 @@ PeersHandler::~PeersHandler() mStateTokenServer->unregisterTickClient(this); } -void PeersHandler::notifyListChange(int list, int type) +void PeersHandler::notifyListChange(int list, int /* type */) { RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ if(list == NOTIFY_LIST_FRIENDS) diff --git a/libresapi/src/api/json.cpp b/libresapi/src/api/json.cpp index 9068d7a06..d921e6b80 100644 --- a/libresapi/src/api/json.cpp +++ b/libresapi/src/api/json.cpp @@ -525,7 +525,7 @@ std::string SerializeValue(const Value& v) // json expets decimal points, so replace all commas with decimal points if(v.GetType() == FloatVal || v.GetType() == DoubleVal) { - for(int i = 0; i < str.size(); i++) + for(unsigned int i = 0; i < str.size(); i++) if(str[i] == ',') str[i] = '.'; } diff --git a/retroshare-gui/src/gui/Posted/PostedListWidget.cpp b/retroshare-gui/src/gui/Posted/PostedListWidget.cpp index 586b673ed..a333d3fbd 100644 --- a/retroshare-gui/src/gui/Posted/PostedListWidget.cpp +++ b/retroshare-gui/src/gui/Posted/PostedListWidget.cpp @@ -464,7 +464,7 @@ void PostedListWidget::clearPosts() mPosts.clear(); } -bool PostedListWidget::navigatePostItem(const RsGxsMessageId &msgId) +bool PostedListWidget::navigatePostItem(const RsGxsMessageId & /*msgId*/) { //TODO return false; diff --git a/retroshare-gui/src/gui/common/PictureFlow.cpp b/retroshare-gui/src/gui/common/PictureFlow.cpp index 9087ad3a5..0dab2341b 100644 --- a/retroshare-gui/src/gui/common/PictureFlow.cpp +++ b/retroshare-gui/src/gui/common/PictureFlow.cpp @@ -293,7 +293,7 @@ PictureFlowState::PictureFlowState(): PictureFlowState::~PictureFlowState() { - for(uint i=0;istate->slideImages.size();++i) + for(int i=0;istate->slideImages.size();++i) delete d->state->slideImages[i] ; d->state->slideImages.clear() ; diff --git a/retroshare-gui/src/gui/common/RSGraphWidget.cpp b/retroshare-gui/src/gui/common/RSGraphWidget.cpp index 02cccda5a..fa7d00bdc 100644 --- a/retroshare-gui/src/gui/common/RSGraphWidget.cpp +++ b/retroshare-gui/src/gui/common/RSGraphWidget.cpp @@ -332,10 +332,10 @@ void RSGraphWidget::paintEvent(QPaintEvent *) _painter->end(); } -QSizeF RSGraphWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +QSizeF RSGraphWidget::sizeHint(Qt::SizeHint which, const QSizeF& /* constraint */) const { float FS = QFontMetricsF(font()).height(); - float fact = FS/14.0 ; + //float fact = FS/14.0 ; switch(which) { @@ -408,7 +408,7 @@ void RSGraphWidget::pointsFromData(const std::vector& values,QVector& points, QColor color, Qt:: void RSGraphWidget::paintTotals() { float FS = QFontMetricsF(font()).height(); - float fact = FS/14.0 ; + //float fact = FS/14.0 ; //int x = SCALE_WIDTH*fact + FS, y = 0; int rowHeight = FS; @@ -593,12 +593,12 @@ void RSGraphWidget::paintScale2() float FS = QFontMetricsF(font()).height(); float fact = FS/14.0 ; - int bottom = _rec.height(); + //int bottom = _rec.height(); static const int npix = 100*fact ; for(int i=_rec.width();i>SCALE_WIDTH*fact;i-=npix) { - qreal pos = bottom - FS; + //qreal pos = bottom - FS; int seconds = (_rec.width()-i)/_time_scale ; // pixels / (pixels per second) => seconds QString text = QString::number(seconds)+ " secs"; diff --git a/retroshare-gui/src/gui/connect/PGPKeyDialog.cpp b/retroshare-gui/src/gui/connect/PGPKeyDialog.cpp index beb8d420c..91c666296 100644 --- a/retroshare-gui/src/gui/connect/PGPKeyDialog.cpp +++ b/retroshare-gui/src/gui/connect/PGPKeyDialog.cpp @@ -340,7 +340,7 @@ void PGPKeyDialog::applyDialog() } //check the GPG trustlvl - if(ui.trustlevel_CB->currentIndex() != detail.trustLvl) + if(ui.trustlevel_CB->currentIndex() != (int)detail.trustLvl) rsPeers->trustGPGCertificate(pgpId, ui.trustlevel_CB->currentIndex()); //setServiceFlags() ; diff --git a/retroshare-gui/src/gui/msgs/MessageComposer.cpp b/retroshare-gui/src/gui/msgs/MessageComposer.cpp index 04a09f9e5..0a3bf70af 100644 --- a/retroshare-gui/src/gui/msgs/MessageComposer.cpp +++ b/retroshare-gui/src/gui/msgs/MessageComposer.cpp @@ -1534,7 +1534,7 @@ QString MessageComposer::getRecipientEmailAddress(const RsGxsId& id,const RsIden return (QString("%2 <")+tr("Distant identity:")+" %2@%1>").arg(QString::fromStdString(id.toStdString())).arg(QString::fromUtf8(detail.mNickname.c_str())) ; } -QString MessageComposer::getRecipientEmailAddress(const RsPeerId& id,const RsPeerDetails& detail) +QString MessageComposer::getRecipientEmailAddress(const RsPeerId& /* id */,const RsPeerDetails& detail) { QString location_name = detail.location.empty()?tr("[Missing]"):QString::fromUtf8(detail.location.c_str()) ; diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 521df3450..b600db1da 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -496,7 +496,7 @@ void ServerPage::ipFilterContextMenu(const QPoint& point) if(item == NULL) return ; - bool status = item->data(Qt::UserRole).toBool(); + //bool status = item->data(Qt::UserRole).toBool(); uint32_t reason = ui.filteredIpsTable->item(row,COLUMN_REASON)->data(Qt::UserRole).toUInt(); diff --git a/retroshare-gui/src/gui/statistics/BWGraph.cpp b/retroshare-gui/src/gui/statistics/BWGraph.cpp index 2f352aa85..b3d7f64b5 100644 --- a/retroshare-gui/src/gui/statistics/BWGraph.cpp +++ b/retroshare-gui/src/gui/statistics/BWGraph.cpp @@ -411,7 +411,7 @@ void BWGraphSource::setSelector(int selector_type,int graph_type,const std::stri { if(graph_type == GRAPH_TYPE_SINGLE) { - bool ok = false ; + //bool ok = false ; int tmp = QString::fromStdString(selector_client_string).toInt() ; if(tmp > 0 && tmp < 0x10000) diff --git a/retroshare-gui/src/gui/statistics/BandwidthStatsWidget.cpp b/retroshare-gui/src/gui/statistics/BandwidthStatsWidget.cpp index 5ef4a9d68..3d29f2cc9 100644 --- a/retroshare-gui/src/gui/statistics/BandwidthStatsWidget.cpp +++ b/retroshare-gui/src/gui/statistics/BandwidthStatsWidget.cpp @@ -62,7 +62,7 @@ void BandwidthStatsWidget::updateComboBoxes() // Setup button/combobox info - uint32_t indx = 2 ; + int indx = 2 ; //RsPeerDetails details ; RsPeerId current_friend_id(ui.friend_CB->itemData(ui.friend_CB->currentIndex()).toString().toStdString()) ; diff --git a/retroshare-gui/src/gui/statistics/BwCtrlWindow.cpp b/retroshare-gui/src/gui/statistics/BwCtrlWindow.cpp index 45d5da8c6..980025a03 100644 --- a/retroshare-gui/src/gui/statistics/BwCtrlWindow.cpp +++ b/retroshare-gui/src/gui/statistics/BwCtrlWindow.cpp @@ -171,7 +171,7 @@ void BWListDelegate::paint(QPainter * painter, const QStyleOptionViewItem & opti QSize BWListDelegate::sizeHint(const QStyleOptionViewItem & option/*option*/, const QModelIndex & index) const { float FS = QFontMetricsF(option.font).height(); - float fact = FS/14.0 ; + //float fact = FS/14.0 ; float w = QFontMetricsF(option.font).width(index.data(Qt::DisplayRole).toString()); @@ -187,8 +187,8 @@ BwCtrlWindow::BwCtrlWindow(QWidget *parent) BWDelegate = new BWListDelegate(); bwTreeWidget->setItemDelegate(BWDelegate); - float FS = QFontMetricsF(font()).height(); - float fact = FS/14.0 ; + //float FS = QFontMetricsF(font()).height(); + //float fact = FS/14.0 ; /* Set header resize modes and initial section sizes Peer TreeView*/ QHeaderView * _header = bwTreeWidget->header () ; diff --git a/retroshare-gui/src/gui/statistics/DhtWindow.cpp b/retroshare-gui/src/gui/statistics/DhtWindow.cpp index 4dcb031bd..a6406743d 100644 --- a/retroshare-gui/src/gui/statistics/DhtWindow.cpp +++ b/retroshare-gui/src/gui/statistics/DhtWindow.cpp @@ -279,7 +279,7 @@ void DhtWindow::updateNetStatus() void DhtWindow::updateNetPeers() { - QTreeWidget *peerTreeWidget = ui.peerTreeWidget; + //QTreeWidget *peerTreeWidget = ui.peerTreeWidget; std::list peerIds; std::list::iterator it; @@ -654,7 +654,7 @@ void DhtWindow::updateDhtPeers() } } - QTreeWidget *dhtTreeWidget = ui.dhtTreeWidget; + //QTreeWidget *dhtTreeWidget = ui.dhtTreeWidget; ui.dhtTreeWidget->clear(); diff --git a/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp b/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp index 1f4bcb968..a309717b8 100644 --- a/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp +++ b/retroshare-gui/src/gui/statistics/GlobalRouterStatistics.cpp @@ -324,7 +324,7 @@ void GlobalRouterStatisticsWidget::updateContent() // oy += celly ; // oy += celly ; - static const int MaxKeySize = 20*fact ; + //static const int MaxKeySize = 20*fact ; painter.setFont(monospace_f) ; int n=0; diff --git a/retroshare-gui/src/gui/statistics/RttStatistics.cpp b/retroshare-gui/src/gui/statistics/RttStatistics.cpp index 45ab5bdcc..9690b07a4 100644 --- a/retroshare-gui/src/gui/statistics/RttStatistics.cpp +++ b/retroshare-gui/src/gui/statistics/RttStatistics.cpp @@ -34,7 +34,7 @@ #include "gui/settings/rsharesettings.h" -RttStatistics::RttStatistics(QWidget *parent) +RttStatistics::RttStatistics(QWidget * /*parent*/) { setupUi(this) ; diff --git a/retroshare-nogui/src/notifytxt.cc b/retroshare-nogui/src/notifytxt.cc index 07aa53463..4327700ff 100644 --- a/retroshare-nogui/src/notifytxt.cc +++ b/retroshare-nogui/src/notifytxt.cc @@ -74,7 +74,7 @@ char *getpass (const char *prompt) } #endif -void NotifyTxt::notifyErrorMsg(int list, int type, std::string msg) +void NotifyTxt::notifyErrorMsg(int /* list */, int /* type */, std::string /* msg */) { return; } @@ -101,7 +101,7 @@ bool NotifyTxt::askForPluginConfirmation(const std::string& plugin_file_name, co return a == 'y' ; } -bool NotifyTxt::askForPassword(const std::string& question, bool prev_is_bad, std::string& password,bool& cancel) +bool NotifyTxt::askForPassword(const std::string& question, bool /* prev_is_bad */, std::string& password,bool& cancel) { std::string question1="Please enter your PGP password for key:\n " + question + " :"; char *passwd = getpass(question1.c_str()) ; @@ -112,7 +112,7 @@ bool NotifyTxt::askForPassword(const std::string& question, bool prev_is_bad, st } -void NotifyTxt::notifyListChange(int list, int type) +void NotifyTxt::notifyListChange(int list, int /* type */) { //std::cerr << "NotifyTxt::notifyListChange()" << std::endl; switch(list) From e2e9edecb94efdb2c1073ff78762ff55e09e4212 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 5 Jun 2016 11:51:47 -0400 Subject: [PATCH 05/13] fixed initialization of memebrs in peerDetails --- libretroshare/src/rsserver/p3peers.cc | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index e516b2f28..74e9f270f 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -1376,15 +1376,20 @@ FileSearchFlags p3Peers::computePeerPermissionFlags(const RsPeerId& peer_ssl_id, RsPeerDetails::RsPeerDetails() :isOnlyGPGdetail(false), - name(""),email(""),location(""), - org(""),authcode(""), - trustLvl(0), validLvl(0),ownsign(false), - hasSignedMe(false),accept_connection(false), - state(0),localAddr(""),localPort(0),extAddr(""),extPort(0),netMode(0),vs_disc(0), vs_dht(0), - lastConnect(0),connectState(0),connectStateString(""), - hiddenType(RS_HIDDEN_TYPE_NONE), - connectPeriod(0), - foundDHT(false), wasDeniedConnection(false), deniedTS(0) + name(""),email(""),location(""), + org(""),authcode(""), + trustLvl(0), validLvl(0),ownsign(false), + hasSignedMe(false),accept_connection(false), + state(0),actAsServer(false), + connectPort(0), + isHiddenNode(false), + hiddenNodePort(0), + hiddenType(RS_HIDDEN_TYPE_NONE), + localAddr(""),localPort(0),extAddr(""),extPort(0),netMode(0),vs_disc(0), vs_dht(0), + lastConnect(0),lastUsed(0),connectState(0),connectStateString(""), + connectPeriod(0), + foundDHT(false), wasDeniedConnection(false), deniedTS(0), + linkType ( RS_NET_CONN_TRANS_TCP_UNKNOWN) { } From 8d54603b02c9fcd9b724de784dc74604b7f40445 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 16 Jun 2016 21:27:28 -0400 Subject: [PATCH 06/13] fixed bug in private key extraction --- libretroshare/src/gxs/gxssecurity.cc | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 65d093959..4b9ab5938 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -96,14 +96,15 @@ static void setRSAPublicKeyData(RsTlvPublicRSAKey& key, RSA *rsa_pub) free(data) ; } -static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_pub) +static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_priv) { assert(key.keyFlags & RSTLV_KEY_TYPE_FULL) ; + unsigned char *data = NULL ; // this works for OpenSSL > 0.9.7 - int reqspace = i2d_RSAPublicKey(rsa_pub, &data); + int reqspace = i2d_RSAPrivateKey(rsa_priv, &data); key.keyData.setBinData(data, reqspace); - key.keyId = getRsaKeyFingerprint(rsa_pub); + key.keyId = getRsaKeyFingerprint(rsa_priv); free(data) ; } @@ -179,17 +180,18 @@ bool GxsSecurity::generateKeyPair(RsTlvPublicRSAKey& public_key,RsTlvPrivateRSAK // admin keys RSA *rsa = RSA_generate_key(2048, 65537, NULL, NULL); RSA *rsa_pub = RSAPublicKey_dup(rsa); + + public_key.keyFlags = RSTLV_KEY_TYPE_PUBLIC_ONLY ; + private_key.keyFlags = RSTLV_KEY_TYPE_FULL ; setRSAPublicKeyData(public_key, rsa_pub); setRSAPrivateKeyData(private_key, rsa); public_key.startTS = time(NULL); public_key.endTS = public_key.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */ - public_key.keyFlags = RSTLV_KEY_TYPE_PUBLIC_ONLY ; private_key.startTS = public_key.startTS; private_key.endTS = 0; /* no end */ - private_key.keyFlags = RSTLV_KEY_TYPE_FULL ; // clean up RSA_free(rsa); @@ -250,28 +252,28 @@ bool GxsSecurity::extractPublicKey(const RsTlvPrivateRSAKey &private_key, RsTlvP bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvPrivateRSAKey &privKey, RsTlvKeySignature& sign) { - RSA* rsa_pub = extractPrivateKey(privKey); + RSA* rsa_priv = extractPrivateKey(privKey); - if(!rsa_pub) + if(!rsa_priv) { std::cerr << "GxsSecurity::getSignature(): Cannot create signature. Keydata is incomplete." << std::endl; return false ; } - EVP_PKEY *key_pub = EVP_PKEY_new(); - EVP_PKEY_assign_RSA(key_pub, rsa_pub); + EVP_PKEY *key_priv = EVP_PKEY_new(); + EVP_PKEY_assign_RSA(key_priv, rsa_priv); /* calc and check signature */ EVP_MD_CTX *mdctx = EVP_MD_CTX_create(); bool ok = EVP_SignInit(mdctx, EVP_sha1()) == 1; ok &= EVP_SignUpdate(mdctx, data, data_len) == 1; - unsigned int siglen = EVP_PKEY_size(key_pub); + unsigned int siglen = EVP_PKEY_size(key_priv); unsigned char sigbuf[siglen]; - ok &= EVP_SignFinal(mdctx, sigbuf, &siglen, key_pub) == 1; + ok &= EVP_SignFinal(mdctx, sigbuf, &siglen, key_priv) == 1; // clean up EVP_MD_CTX_destroy(mdctx); - EVP_PKEY_free(key_pub); + EVP_PKEY_free(key_priv); sign.signData.setBinData(sigbuf, siglen); sign.keyId = RsGxsId(privKey.keyId); From b9ba51f2ba3b25605fa172ffb5723af30c187fe6 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 16 Jun 2016 22:23:27 -0400 Subject: [PATCH 07/13] improved naming of publish/admin rights in GroupTreeWidget --- libretroshare/src/retroshare/rsgxsflags.h | 4 +- .../src/gui/common/GroupTreeWidget.cpp | 196 +++++++++--------- .../src/gui/common/GroupTreeWidget.h | 5 +- .../src/gui/gxs/GxsGroupFrameDialog.cpp | 7 +- .../src/gui/gxs/GxsGroupFrameDialog.h | 2 +- .../src/gui/gxs/GxsGroupShareKey.cpp | 4 +- 6 files changed, 110 insertions(+), 108 deletions(-) diff --git a/libretroshare/src/retroshare/rsgxsflags.h b/libretroshare/src/retroshare/rsgxsflags.h index 0b2d78a08..c9b92c6f1 100644 --- a/libretroshare/src/retroshare/rsgxsflags.h +++ b/libretroshare/src/retroshare/rsgxsflags.h @@ -102,6 +102,7 @@ namespace GXS_SERV { // GENERIC GXS MACROS #define IS_MSG_NEW(status) (status & GXS_SERV::GXS_MSG_STATUS_GUI_NEW) #define IS_MSG_UNREAD(status) (status & GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD) +#define IS_MSG_UNPROCESSED(status) (status & GXS_SERV::GXS_MSG_STATUS_UNPROCESSED) #define IS_GROUP_PGP_AUTHED(signFlags) (signFlags & GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_GPG) #define IS_GROUP_MESSAGE_TRACKING(signFlags) (signFlags & GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_TRACK_MESSAGES) @@ -111,7 +112,4 @@ namespace GXS_SERV { #define IS_GROUP_SUBSCRIBED(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED) #define IS_GROUP_NOT_SUBSCRIBED(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED) -#define IS_MSG_UNPROCESSED(status) (status & GXS_SERV::GXS_MSG_STATUS_UNPROCESSED) - - #endif // RSGXSFLAGS_H diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp index 1bef06098..b396083b5 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp @@ -334,112 +334,114 @@ QString GroupTreeWidget::itemIdAt(QPoint &point) void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList &itemList) { - if (categoryItem == NULL) { - return; - } - - QString filterText = ui->filterLineEdit->text(); - - /* Iterate all items */ - QList::const_iterator it; - for (it = itemList.begin(); it != itemList.end(); ++it) { - const GroupItemInfo &itemInfo = *it; - - QTreeWidgetItem *item = NULL; - - /* Search exisiting item */ - int childCount = categoryItem->childCount(); - for (int child = 0; child < childCount; ++child) { - QTreeWidgetItem *childItem = categoryItem->child(child); - if (childItem->data(COLUMN_DATA, ROLE_ID).toString() == itemInfo.id) { - /* Found child */ - item = childItem; - break; - } - } - - if (item == NULL) { - item = new RSTreeWidgetItem(compareRole); - item->setData(COLUMN_DATA, ROLE_ID, itemInfo.id); - categoryItem->addChild(item); - } - - item->setText(COLUMN_NAME, itemInfo.name); - item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name); - item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description); - - /* Set last post */ - qlonglong lastPost = itemInfo.lastpost.toTime_t(); - item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting - - /* Set visible posts */ - item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting - - /* Set icon */ - if (ui->treeWidget->itemWidget(item, COLUMN_NAME)) { - /* Item is waiting, save icon in role */ - item->setData(COLUMN_DATA, ROLE_SAVED_ICON, itemInfo.icon); - } else { - item->setIcon(COLUMN_NAME, itemInfo.icon); - } - - /* Set popularity */ - QString tooltip = PopularityDefs::tooltip(itemInfo.popularity); - - item->setIcon(COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity)); - item->setData(COLUMN_DATA, ROLE_POPULARITY, -itemInfo.popularity); // negative for correct sorting - - /* Set tooltip */ - if (itemInfo.privatekey) { - tooltip += "\n" + tr("You have admin rights"); - } - if(!IS_GROUP_SUBSCRIBED(itemInfo.subscribeFlags)) - { - tooltip += "\n" + QString::number(itemInfo.max_visible_posts) + " messages available" ; - tooltip += "\n" + tr("Subscribe to download and read messages") ; + if (categoryItem == NULL) { + return; } - item->setToolTip(COLUMN_NAME, tooltip); - item->setToolTip(COLUMN_POPULARITY, tooltip); + QString filterText = ui->filterLineEdit->text(); - item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags); + /* Iterate all items */ + QList::const_iterator it; + for (it = itemList.begin(); it != itemList.end(); ++it) { + const GroupItemInfo &itemInfo = *it; - /* Set color */ - QBrush brush; - if (itemInfo.privatekey) { - brush = QBrush(textColorPrivateKey()); - item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_PRIVATEKEY); - } else { - brush = ui->treeWidget->palette().color(QPalette::Text); - item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_STANDARD); - } - item->setForeground(COLUMN_NAME, brush); + QTreeWidgetItem *item = NULL; - /* Calculate score */ - calculateScore(item, filterText); - } + /* Search exisiting item */ + int childCount = categoryItem->childCount(); + for (int child = 0; child < childCount; ++child) { + QTreeWidgetItem *childItem = categoryItem->child(child); + if (childItem->data(COLUMN_DATA, ROLE_ID).toString() == itemInfo.id) { + /* Found child */ + item = childItem; + break; + } + } - /* Remove all items not in list */ - int child = 0; - int childCount = categoryItem->childCount(); - while (child < childCount) { - QString id = categoryItem->child(child)->data(COLUMN_DATA, ROLE_ID).toString(); + if (item == NULL) { + item = new RSTreeWidgetItem(compareRole); + item->setData(COLUMN_DATA, ROLE_ID, itemInfo.id); + categoryItem->addChild(item); + } - for (it = itemList.begin(); it != itemList.end(); ++it) { - if (it->id == id) { - break; - } - } + item->setText(COLUMN_NAME, itemInfo.name); + item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name); + item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description); - if (it == itemList.end()) { - delete(categoryItem->takeChild(child)); - childCount = categoryItem->childCount(); - } else { - ++child; - } - } + /* Set last post */ + qlonglong lastPost = itemInfo.lastpost.toTime_t(); + item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting - resort(categoryItem); + /* Set visible posts */ + item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting + + /* Set icon */ + if (ui->treeWidget->itemWidget(item, COLUMN_NAME)) { + /* Item is waiting, save icon in role */ + item->setData(COLUMN_DATA, ROLE_SAVED_ICON, itemInfo.icon); + } else { + item->setIcon(COLUMN_NAME, itemInfo.icon); + } + + /* Set popularity */ + QString tooltip = PopularityDefs::tooltip(itemInfo.popularity); + + item->setIcon(COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity)); + item->setData(COLUMN_DATA, ROLE_POPULARITY, -itemInfo.popularity); // negative for correct sorting + + /* Set tooltip */ + if (itemInfo.adminKey) + tooltip += "\n" + tr("You are admin (modify names and description using Edit menu)"); + else if (itemInfo.publishKey) + tooltip += "\n" + tr("You have been granted as publisher (you can post here!)"); + + if(!IS_GROUP_SUBSCRIBED(itemInfo.subscribeFlags)) + { + tooltip += "\n" + QString::number(itemInfo.max_visible_posts) + " messages available" ; + tooltip += "\n" + tr("Subscribe to download and read messages") ; + } + + item->setToolTip(COLUMN_NAME, tooltip); + item->setToolTip(COLUMN_POPULARITY, tooltip); + + item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags); + + /* Set color */ + QBrush brush; + if (itemInfo.publishKey) { + brush = QBrush(textColorPrivateKey()); + item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_PRIVATEKEY); + } else { + brush = ui->treeWidget->palette().color(QPalette::Text); + item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_STANDARD); + } + item->setForeground(COLUMN_NAME, brush); + + /* Calculate score */ + calculateScore(item, filterText); + } + + /* Remove all items not in list */ + int child = 0; + int childCount = categoryItem->childCount(); + while (child < childCount) { + QString id = categoryItem->child(child)->data(COLUMN_DATA, ROLE_ID).toString(); + + for (it = itemList.begin(); it != itemList.end(); ++it) { + if (it->id == id) { + break; + } + } + + if (it == itemList.end()) { + delete(categoryItem->takeChild(child)); + childCount = categoryItem->childCount(); + } else { + ++child; + } + } + + resort(categoryItem); } void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount) diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.h b/retroshare-gui/src/gui/common/GroupTreeWidget.h index 0283a2ac8..f88b79de1 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.h +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.h @@ -47,7 +47,7 @@ public: GroupItemInfo() { popularity = 0; - privatekey = false; + publishKey = false; subscribeFlags = 0; max_visible_posts =0; } @@ -59,7 +59,8 @@ public: int popularity; QDateTime lastpost; QIcon icon; - bool privatekey; + bool publishKey; + bool adminKey; quint32 subscribeFlags; quint32 max_visible_posts ; }; diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp index 5785d68fb..0ab1f6f33 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp @@ -286,7 +286,7 @@ void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) action->setEnabled (!mGroupId.isNull() && isAdmin); if (shareKeyType()) { - action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share admin permissions"), this, SLOT(shareKey())); + action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share publish permissions"), this, SLOT(sharePublishKey())); action->setEnabled(!mGroupId.isNull() && isPublisher); } @@ -429,7 +429,7 @@ void GxsGroupFrameDialog::markMsgAsUnread() } } -void GxsGroupFrameDialog::shareKey() +void GxsGroupFrameDialog::sharePublishKey() { if (mGroupId.isNull()) { return; @@ -673,7 +673,8 @@ void GxsGroupFrameDialog::groupInfoToGroupItemInfo(const RsGroupMetaData &groupI groupItemInfo.popularity = groupInfo.mPop; groupItemInfo.lastpost = QDateTime::fromTime_t(groupInfo.mLastPost); groupItemInfo.subscribeFlags = groupInfo.mSubscribeFlags; - groupItemInfo.privatekey = IS_GROUP_PUBLISHER(groupInfo.mSubscribeFlags) ; + groupItemInfo.publishKey = IS_GROUP_PUBLISHER(groupInfo.mSubscribeFlags) ; + groupItemInfo.adminKey = IS_GROUP_ADMIN(groupInfo.mSubscribeFlags) ; groupItemInfo.max_visible_posts = groupInfo.mVisibleMsgCount ; #if TOGXS diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h index db6e2bd38..fa5e19582 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h @@ -122,7 +122,7 @@ private slots: void markMsgAsRead(); void markMsgAsUnread(); - void shareKey(); + void sharePublishKey(); void loadComment(const RsGxsGroupId &grpId, const RsGxsMessageId &msgId, const QString &title); diff --git a/retroshare-gui/src/gui/gxs/GxsGroupShareKey.cpp b/retroshare-gui/src/gui/gxs/GxsGroupShareKey.cpp index 8db2edf47..4dc2c44e8 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupShareKey.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupShareKey.cpp @@ -78,8 +78,8 @@ void GroupShareKey::setTyp() return; ui->headerFrame->setHeaderImage(QPixmap(":/images/channels.png")); - ui->headerFrame->setHeaderText(tr("Share channel admin permissions")); - ui->sharekeyinfo_label->setText(tr("You can allow your friends to publish in your channel and to modify the description. Or you can send the admin permissions to another Retroshare instance. Select the friends which you want to be allowed to publish in this channel. Note: it is not possible to revoke channel admin permissions.")); + ui->headerFrame->setHeaderText(tr("Share channel publish permissions")); + ui->sharekeyinfo_label->setText(tr("You can allow your friends to publish in your channel, or send the publish permissions to another Retroshare instance of yours. Select the friends which you want to be allowed to publish in this channel. Note: it is currently not possible to revoke channel publish permissions.")); } else if(mGrpType == FORUM_KEY_SHARE) { From 25c0c9d4ce9d2c0a8044cbeda2047ce23853b8ba Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 16 Jun 2016 23:06:07 -0400 Subject: [PATCH 08/13] added systematic consistency checking of public/private RSA keys at deserialisation time --- libretroshare/src/gxs/gxssecurity.cc | 23 ++++++++++++++++++----- libretroshare/src/serialiser/rstlvkeys.cc | 13 +++++++++++-- libretroshare/src/serialiser/rstlvkeys.h | 8 +++----- libretroshare/src/services/p3idservice.cc | 4 ++-- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 4b9ab5938..4e086ac7a 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -110,9 +110,9 @@ static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_priv) } bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) { -#ifdef GXS_SECURITY_DEBUG +//#ifdef GXS_SECURITY_DEBUG std::cerr << "Checking private key " << key.keyId << " ..." << std::endl; -#endif +//#endif if( (key.keyFlags & RSTLV_KEY_TYPE_MASK) != RSTLV_KEY_TYPE_FULL) { @@ -147,15 +147,28 @@ bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) } bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) { -#ifdef GXS_SECURITY_DEBUG +//#ifdef GXS_SECURITY_DEBUG std::cerr << "Checking public key " << key.keyId << " ..." << std::endl; -#endif +//#endif if( (key.keyFlags & RSTLV_KEY_TYPE_MASK) != RSTLV_KEY_TYPE_PUBLIC_ONLY) { std::cerr << "(WW) GxsSecurity::checkPublicKey(): public key has wrong flags " << std::hex << (key.keyFlags & RSTLV_KEY_TYPE_MASK) << std::dec << ". This is unexpected." << std::endl; return false ; } + + // try to extract private key + const unsigned char *keyptr = (const unsigned char *) key.keyData.bin_data; + long keylen = key.keyData.bin_len; + RSA *rsa_prv = d2i_RSAPrivateKey(NULL, &(keyptr), keylen); + + if(rsa_prv != NULL) + { + std::cerr << "(SS) GxsSecurity::checkPublicKey(): public key with ID " << key.keyId << " actually is a Private key!!!" << std::endl; + RSA_free(rsa_prv) ; + return false ; + } + RSA *rsa_pub = ::extractPublicKey(key) ; if(rsa_pub == NULL) @@ -197,7 +210,7 @@ bool GxsSecurity::generateKeyPair(RsTlvPublicRSAKey& public_key,RsTlvPrivateRSAK RSA_free(rsa); RSA_free(rsa_pub); - if(!(private_key.check() && public_key.check())) + if(!(private_key.checkKey() && public_key.checkKey())) { std::cerr << "(EE) ERROR while generating keys. Something inconsistent in flags. This is probably a bad sign!" << std::endl; return false ; diff --git a/libretroshare/src/serialiser/rstlvkeys.cc b/libretroshare/src/serialiser/rstlvkeys.cc index d50747cb6..5cb06f3ff 100644 --- a/libretroshare/src/serialiser/rstlvkeys.cc +++ b/libretroshare/src/serialiser/rstlvkeys.cc @@ -28,6 +28,7 @@ #include "rstlvbase.h" #include "rsbaseserial.h" #include "util/stacktrace.h" +#include "gxs/gxssecurity.h" #include @@ -96,7 +97,7 @@ bool RsTlvRSAKey::SetTlv(void *data, uint32_t size, uint32_t *offset) const return false; /* not enough space */ } - bool ok = checkFlags(keyFlags); // check before serialise, just in case + bool ok = checkKey(); // check before serialise, just in case /* start at data[offset] */ /* add mandatory parts first */ @@ -184,7 +185,7 @@ bool RsTlvRSAKey::GetTlv(void *data, uint32_t size, uint32_t *offset) std::cerr << std::endl; #endif } - return ok && checkFlags(keyFlags) ; + return ok && checkKey() ; } std::ostream& RsTlvRSAKey::print(std::ostream &out, uint16_t indent) const @@ -217,7 +218,15 @@ std::ostream& RsTlvRSAKey::print(std::ostream &out, uint16_t indent) const } +bool RsTlvPrivateRSAKey::checkKey() const +{ + return bool(keyFlags & RSTLV_KEY_TYPE_FULL) && !bool(keyFlags & RSTLV_KEY_TYPE_PUBLIC_ONLY) && GxsSecurity::checkPrivateKey(*this) ; +} +bool RsTlvPublicRSAKey::checkKey() const +{ + return bool(keyFlags & RSTLV_KEY_TYPE_PUBLIC_ONLY) && !bool(keyFlags & RSTLV_KEY_TYPE_FULL) && GxsSecurity::checkPublicKey(*this) ; +} /************************************* RsTlvSecurityKeySet ************************************/ diff --git a/libretroshare/src/serialiser/rstlvkeys.h b/libretroshare/src/serialiser/rstlvkeys.h index 92e09b281..03b41c06d 100644 --- a/libretroshare/src/serialiser/rstlvkeys.h +++ b/libretroshare/src/serialiser/rstlvkeys.h @@ -53,7 +53,7 @@ class RsTlvRSAKey: public RsTlvItem { public: RsTlvRSAKey(); - 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 bool checkKey() 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 void TlvClear(); @@ -64,8 +64,6 @@ public: /* clears KeyData - but doesn't delete - to transfer ownership */ void ShallowClear(); - bool check() const { return checkFlags(keyFlags) && (!keyId.isNull()) ; } - RsGxsId keyId; // Mandatory : uint32_t keyFlags; // Mandatory ; uint32_t startTS; // Mandatory : @@ -80,14 +78,14 @@ class RsTlvPrivateRSAKey: public RsTlvRSAKey public: 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 checkKey() const ; }; class RsTlvPublicRSAKey: public RsTlvRSAKey { public: virtual ~RsTlvPublicRSAKey() {} - virtual bool checkFlags(uint32_t flags) const { return bool(flags & RSTLV_KEY_TYPE_PUBLIC_ONLY) && !bool(flags & RSTLV_KEY_TYPE_FULL) ; } + virtual bool checkKey() const ; }; class RsTlvSecurityKeySet: public RsTlvItem diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 1c467a137..22fb8da49 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -1699,10 +1699,10 @@ void RsGxsIdCache::init(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& i // do some tests if(details.mFlags & RS_IDENTITY_FLAGS_IS_OWN_ID) { - if(!priv_key.check()) + if(!priv_key.checkKey()) std::cerr << "(EE) Private key missing for own identity " << pub_key.keyId << std::endl; } - if(!pub_key.check()) + if(!pub_key.checkKey()) std::cerr << "(EE) Public key missing for identity " << pub_key.keyId << std::endl; /* rest must be retrived from ServiceString */ From c25b65074a77367b6261c16e89e324ef3fa7ab92 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 16 Jun 2016 23:10:13 -0400 Subject: [PATCH 09/13] removed check key debug info --- libretroshare/src/gxs/gxssecurity.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 4e086ac7a..53293b0e5 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -110,9 +110,9 @@ static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_priv) } bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) { -//#ifdef GXS_SECURITY_DEBUG +#ifdef GXS_SECURITY_DEBUG std::cerr << "Checking private key " << key.keyId << " ..." << std::endl; -//#endif +#endif if( (key.keyFlags & RSTLV_KEY_TYPE_MASK) != RSTLV_KEY_TYPE_FULL) { @@ -147,9 +147,9 @@ bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) } bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) { -//#ifdef GXS_SECURITY_DEBUG +#ifdef GXS_SECURITY_DEBUG std::cerr << "Checking public key " << key.keyId << " ..." << std::endl; -//#endif +#endif if( (key.keyFlags & RSTLV_KEY_TYPE_MASK) != RSTLV_KEY_TYPE_PUBLIC_ONLY) { From 886d896151bda7b009ff6f0ac248ae60acf51514 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 17 Jun 2016 20:46:24 -0400 Subject: [PATCH 10/13] fixed warning about old fingerprint for own keys. --- libretroshare/src/gxs/gxssecurity.cc | 42 +++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 53293b0e5..bb5607432 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -39,6 +39,20 @@ static const uint32_t MULTI_ENCRYPTION_FORMAT_v001_HEADER_SIZE = 2 ; static const uint32_t MULTI_ENCRYPTION_FORMAT_v001_NUMBER_OF_KEYS_SIZE = 2 ; static const uint32_t MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE = 256 ; +static RsGxsId getRsaKeyFingerprint_old_insecure_method(RSA *pubkey) +{ + int lenn = BN_num_bytes(pubkey -> n); + + RsTemporaryMemory tmp(lenn) ; + BN_bn2bin(pubkey -> n, tmp); + + // Copy first CERTSIGNLEN bytes from the hash of the public modulus and exponent + // We should not be using strings here, but a real ID. To be done later. + + assert(lenn >= CERTSIGNLEN) ; + + return RsGxsId((unsigned char*)tmp) ; +} static RsGxsId getRsaKeyFingerprint(RSA *pubkey) { int lenn = BN_num_bytes(pubkey -> n); @@ -135,14 +149,23 @@ bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) return false ; } RsGxsId recomputed_key_id = getRsaKeyFingerprint(rsa_pub) ; - RSA_free(rsa_pub) ; if(recomputed_key_id != key.keyId) { - std::cerr << "(WW) GxsSecurity::checkPrivateKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << "! This is unexpected." << std::endl; + std::cerr << "(WW) GxsSecurity::checkPrivateKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << std::endl; + + if(key.keyId == getRsaKeyFingerprint_old_insecure_method(rsa_pub)) + { + std::cerr << "(WW) fingerprint was derived using old---insecure---format. It can be faked easily. You should get rid of it!" << std::endl; + RSA_free(rsa_pub) ; + return true ; + } + + RSA_free(rsa_pub) ; return false ; } + RSA_free(rsa_pub) ; return true ; } bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) @@ -177,14 +200,25 @@ bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) return false ; } RsGxsId recomputed_key_id = getRsaKeyFingerprint(rsa_pub) ; - RSA_free(rsa_pub) ; if(recomputed_key_id != key.keyId) { - std::cerr << "(WW) GxsSecurity::checkPublicKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << "! This is unexpected." << std::endl; + if(key.keyId == getRsaKeyFingerprint_old_insecure_method(rsa_pub)) + { +#ifdef GXS_SECURITY_DEBUG + std::cerr << "(WW) fingerprint was derived using old---insecure---format. It can be faked easily." << std::endl; +#endif + RSA_free(rsa_pub) ; + return true ; + } + else + std::cerr << "(WW) GxsSecurity::checkPublicKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << "! Key will be discarded." << std::endl; + + RSA_free(rsa_pub) ; return false ; } + RSA_free(rsa_pub) ; return true ; } From 4df4bb638f78774dc0288384d7106b0c47ed0029 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 17 Jun 2016 20:46:58 -0400 Subject: [PATCH 11/13] fixed removal of messages in unsubscribed groups --- libretroshare/src/gxs/rsgxsutil.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/gxs/rsgxsutil.cc b/libretroshare/src/gxs/rsgxsutil.cc index 9e7d4c5a8..0187e9cb4 100644 --- a/libretroshare/src/gxs/rsgxsutil.cc +++ b/libretroshare/src/gxs/rsgxsutil.cc @@ -89,11 +89,14 @@ bool RsGxsMessageCleanUp::clean() remove &= !(meta->mMsgStatus & GXS_SERV::GXS_MSG_STATUS_KEEP); // if not subscribed remove messages (can optimise this really) - remove = remove || (grpMeta->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED); + remove = remove || (grpMeta->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED); + remove = remove || !(grpMeta->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED); if( remove ) { req[grpId].push_back(meta->mMsgId); + + std::cerr << "Scheduling msg id " << meta->mMsgId << " in grp " << grpId << " for removal." << std::endl; } delete meta; From eb05922cd1d3b60cfbc2c92c0fecf65677f40a1b Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 17 Jun 2016 22:21:06 -0400 Subject: [PATCH 12/13] added a tooltip and read color for unsecure GXS identities. These are still supported for a few weeks at most. So get rid of them --- libretroshare/src/gxs/gxssecurity.cc | 30 ++++++++++++---- libretroshare/src/gxs/gxssecurity.h | 1 + libretroshare/src/retroshare/rsidentity.h | 1 + libretroshare/src/services/p3idservice.cc | 4 +++ retroshare-gui/src/gui/Identity/IdDialog.cpp | 38 +++++++++++++------- 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index bb5607432..a1de27d89 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -122,6 +122,14 @@ static void setRSAPrivateKeyData(RsTlvPrivateRSAKey& key, RSA *rsa_priv) free(data) ; } +bool GxsSecurity::checkFingerprint(const RsTlvPublicRSAKey& key) +{ + RSA *rsa_pub = ::extractPublicKey(key) ; + bool res = (key.keyId == getRsaKeyFingerprint(rsa_pub)) ; + RSA_free(rsa_pub) ; + return res ; +} + bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) { #ifdef GXS_SECURITY_DEBUG @@ -152,22 +160,29 @@ bool GxsSecurity::checkPrivateKey(const RsTlvPrivateRSAKey& key) if(recomputed_key_id != key.keyId) { - std::cerr << "(WW) GxsSecurity::checkPrivateKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << std::endl; - if(key.keyId == getRsaKeyFingerprint_old_insecure_method(rsa_pub)) { - std::cerr << "(WW) fingerprint was derived using old---insecure---format. It can be faked easily. You should get rid of it!" << std::endl; +#ifdef GXS_SECURITY_DEBUG + std::cerr << "(WW) fingerprint of key " << key.keyId << " was derived using old---insecure---format. It can be faked easily. You should get rid of this key!" << std::endl; +#endif RSA_free(rsa_pub) ; + + // The policy is to *accept* these private keys, but the public key that corresponds will be rejected anyway, as it can easily be faked. return true ; } - - RSA_free(rsa_pub) ; - return false ; + else + { + std::cerr << "(WW) GxsSecurity::checkPrivateKey(): key " << key.keyId << " has wrong fingerprint " << recomputed_key_id << std::endl; + + RSA_free(rsa_pub) ; + return false ; + } } RSA_free(rsa_pub) ; return true ; } + bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) { #ifdef GXS_SECURITY_DEBUG @@ -209,6 +224,9 @@ bool GxsSecurity::checkPublicKey(const RsTlvPublicRSAKey &key) std::cerr << "(WW) fingerprint was derived using old---insecure---format. It can be faked easily." << std::endl; #endif RSA_free(rsa_pub) ; + + // The policy is to accept these public keys, but warn the owner, since they might be fake keys. They will be soon rejected here, by replacing + // the return value by false. return true ; } else diff --git a/libretroshare/src/gxs/gxssecurity.h b/libretroshare/src/gxs/gxssecurity.h index 126c496d1..b19f81302 100644 --- a/libretroshare/src/gxs/gxssecurity.h +++ b/libretroshare/src/gxs/gxssecurity.h @@ -125,6 +125,7 @@ class GxsSecurity static bool checkPublicKey(const RsTlvPublicRSAKey &key); static bool checkPrivateKey(const RsTlvPrivateRSAKey &key); + static bool checkFingerprint(const RsTlvPublicRSAKey& key); // helper function to only check the fingerprint /*! * Adds possibly missing public keys when private keys are present. diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index 7965da100..e57d43162 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -69,6 +69,7 @@ static const uint32_t RS_IDENTITY_FLAGS_IS_A_CONTACT = 0x0001; static const uint32_t RS_IDENTITY_FLAGS_PGP_LINKED = 0x0002; static const uint32_t RS_IDENTITY_FLAGS_PGP_KNOWN = 0x0004; static const uint32_t RS_IDENTITY_FLAGS_IS_OWN_ID = 0x0008; +static const uint32_t RS_IDENTITY_FLAGS_IS_DEPRECATED= 0x0010; // used to denote keys with deprecated fingerprint format. class GxsReputation { diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 22fb8da49..d436f07af 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -1701,9 +1701,13 @@ void RsGxsIdCache::init(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& i { if(!priv_key.checkKey()) std::cerr << "(EE) Private key missing for own identity " << pub_key.keyId << std::endl; + } if(!pub_key.checkKey()) std::cerr << "(EE) Public key missing for identity " << pub_key.keyId << std::endl; + + if(!GxsSecurity::checkFingerprint(pub_key)) + details.mFlags |= RS_IDENTITY_FLAGS_IS_DEPRECATED; /* rest must be retrived from ServiceString */ updateServiceString(item->meta.mServiceString); diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 28103738e..120435be1 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -1373,19 +1373,33 @@ bool IdDialog::fillIdListItem(const RsGxsIdGroup& data, QTreeWidgetItem *&item, item->setTextAlignment(RSID_COL_VOTES, Qt::AlignRight); item->setData(RSID_COL_VOTES,Qt::DisplayRole, QString::number(info.mOverallReputationScore - 1.0f,'f',3)); - if(isOwnId) - { - QFont font = item->font(RSID_COL_NICKNAME) ; - font.setBold(true) ; - item->setFont(RSID_COL_NICKNAME,font) ; - item->setFont(RSID_COL_IDTYPE,font) ; - item->setFont(RSID_COL_KEYID,font) ; + if(isOwnId) + { + RsIdentityDetails idd ; + rsIdentity->getIdDetails(RsGxsId(data.mMeta.mGroupId),idd) ; - QString tooltip = tr("This identity is owned by you"); - item->setToolTip(RSID_COL_NICKNAME, tooltip) ; - item->setToolTip(RSID_COL_KEYID, tooltip) ; - item->setToolTip(RSID_COL_IDTYPE, tooltip) ; - } + QFont font = item->font(RSID_COL_NICKNAME) ; + + font.setBold(true) ; + item->setFont(RSID_COL_NICKNAME,font) ; + item->setFont(RSID_COL_IDTYPE,font) ; + item->setFont(RSID_COL_KEYID,font) ; + + QString tooltip = tr("This identity is owned by you"); + + if(idd.mFlags & RS_IDENTITY_FLAGS_IS_DEPRECATED) + { + item->setForeground(RSID_COL_NICKNAME,QBrush(Qt::red)); + item->setForeground(RSID_COL_KEYID,QBrush(Qt::red)); + item->setForeground(RSID_COL_IDTYPE,QBrush(Qt::red)); + + tooltip += tr("\nThis identity has a unsecure fingerprint (It's probably quite old).\nYou should get rid of it now and use a new one.\nThese identities will soon be not supported anymore.") ; + } + + item->setToolTip(RSID_COL_NICKNAME, tooltip) ; + item->setToolTip(RSID_COL_KEYID, tooltip) ; + item->setToolTip(RSID_COL_IDTYPE, tooltip) ; + } QPixmap pixmap ; From f57f620111a3d0207b26640abd443ceebe81e364 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 17 Jun 2016 22:27:00 -0400 Subject: [PATCH 13/13] reduced database testing/cleaning frequency to 31/59 mins. --- libretroshare/src/gxs/rsgenexchange.cc | 39 ++------------------------ 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 0783424cb..43db2abd3 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -63,8 +63,8 @@ static const uint32_t INDEX_AUTHEN_ADMIN = 0x00000040; // admin key //#define GEN_EXCH_DEBUG 1 -#define MSG_CLEANUP_PERIOD 60*5 // 5 minutes -#define INTEGRITY_CHECK_PERIOD 60*30 // 30 minutes +#define MSG_CLEANUP_PERIOD 60*59 // 59 minutes +#define INTEGRITY_CHECK_PERIOD 60*31 // 31 minutes RsGenExchange::RsGenExchange(RsGeneralDataService *gds, RsNetworkExchangeService *ns, RsSerialType *serviceSerialiser, uint16_t servType, RsGixs* gixs, @@ -108,41 +108,6 @@ void RsGenExchange::setNetworkExchangeService(RsNetworkExchangeService *ns) mNetService = ns ; } -#ifdef TO_BE_DELETED_IF_NOT_USEFUL -// This class has been tested so as to see where the database gets modified. -class RsDataBaseTester -{ -public: - RsDataBaseTester(RsGeneralDataService *store,const RsGxsGroupId& grpId,const std::string& info) - :_grpId(grpId),_store(store),_info(info) - { - //std::cerr << "RsDataBaseTester: (" << _info << ") retrieving messages for group " << grpId << std::endl; - _store->retrieveMsgIds(_grpId, _msgIds1) ; - } - - ~RsDataBaseTester() - { - //std::cerr << "RsDataBaseTester: (" << _info << ") testing messages for group " << _grpId << std::endl; - _store->retrieveMsgIds(_grpId, _msgIds2) ; - - bool all_idendical = true ; - std::cerr << std::dec ; - - if(_msgIds1.size() != _msgIds2.size()) - std::cerr << " " << _info << " (EE) The two arrays are different (size1=" << _msgIds1.size() << ", size2=" << _msgIds2.size() << ") !!" << std::endl; - else - for(uint32_t i=0;i<_msgIds1.size();++i) - if(_msgIds1[i] != _msgIds2[i]) - std::cerr << " " << _info << " (EE) The two arrays are different for i=" << i << " !!" << std::endl; - } - RsGxsGroupId _grpId ; - RsGeneralDataService *_store ; - std::vector _msgIds1 ; - std::vector _msgIds2 ; - std::string _info ; -}; -#endif - RsGenExchange::~RsGenExchange() { // need to destruct in a certain order (bad thing, TODO: put down instance ownership rules!)