From 28d37c2968b305a2163a11d3efd1690a3ad495d0 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:25:26 +0100 Subject: [PATCH] Added password storing to RsControlModule for GUI benefits --- libresapi/src/api/RsControlModule.cpp | 135 ++++++++++++++++++++++++-- libresapi/src/api/RsControlModule.h | 10 +- 2 files changed, 135 insertions(+), 10 deletions(-) diff --git a/libresapi/src/api/RsControlModule.cpp b/libresapi/src/api/RsControlModule.cpp index 9430df170..228bb691a 100644 --- a/libresapi/src/api/RsControlModule.cpp +++ b/libresapi/src/api/RsControlModule.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "api/ApiServer.h" #include "api/Operators.h" @@ -301,6 +302,7 @@ void RsControlModule::handlePassword(Request &req, Response &resp) mPassword = passwd; mWantPassword = false; mStateTokenServer->replaceToken(mStateToken); + mFixedPassword = passwd; } resp.mDataStream @@ -425,17 +427,17 @@ void RsControlModule::handleCreateLocation(Request &req, Response &resp) RsPeerId ssl_id; std::string err_string; // give the password to the password callback - { - RsStackMutex stack(mDataMtx); // ********** LOCKED ********** - mFixedPassword = pgp_password; - } + { + RsStackMutex stack(mDataMtx); // ********** LOCKED ********** + mFixedPassword = pgp_password; + } bool ssl_ok = RsAccounts::GenerateSSLCertificate(pgp_id, "", ssl_name, "", hidden_port!=0, ssl_password, ssl_id, err_string); // clear fixed password to restore normal password operation - { - RsStackMutex stack(mDataMtx); // ********** LOCKED ********** - mFixedPassword = ""; - } +// { +// RsStackMutex stack(mDataMtx); // ********** LOCKED ********** +// mFixedPassword = ""; +// } if (ssl_ok) { @@ -456,6 +458,123 @@ void RsControlModule::handleCreateLocation(Request &req, Response &resp) resp.setFail("could not create a new location. Error: "+err_string); } +class SignatureEventData +{ + public: + SignatureEventData(const void *_data,int32_t _len,unsigned int _signlen, std::string _reason) + { + // We need a new memory chnk because there's no guarranty _sign nor _signlen are not in the stack + + sign = (unsigned char *)rs_malloc(_signlen); + + if(!sign) + { + signlen = NULL; + signature_result = SELF_SIGNATURE_RESULT_FAILED; + return; + } + + signlen = new unsigned int; + *signlen = _signlen; + signature_result = SELF_SIGNATURE_RESULT_PENDING; + data = rs_malloc(_len); + + if(!data) + { + len = 0; + return; + } + len = _len; + memcpy(data,_data,len); + reason = _reason; + } + + ~SignatureEventData() + { + free(sign); + delete signlen; + free(data); + } + + void performSignature() + { + if(rsPeers->gpgSignData(data,len,sign,signlen,reason)) + signature_result = SELF_SIGNATURE_RESULT_SUCCESS; + else + signature_result = SELF_SIGNATURE_RESULT_FAILED; + } + + void *data; + uint32_t len; + unsigned char *sign; + unsigned int *signlen; + int signature_result; // 0=pending, 1=done, 2=failed/cancelled. + std::string reason; +}; + +bool RsControlModule::askForDeferredSelfSignature(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,int& signature_result, std::string reason /*=""*/) +{ + + { + RsStackMutex stack(mDataMtx); + std::cerr << "NotifyTxt:: deferred signature event requeted. " << std::endl; + + // Look into the queue + + Sha1CheckSum chksum = RsDirUtil::sha1sum((uint8_t*)data,len); + + std::map::iterator it = _deferred_signature_queue.find(chksum.toStdString()); + signature_result = SELF_SIGNATURE_RESULT_PENDING; + + if(it != _deferred_signature_queue.end()) + { + signature_result = it->second->signature_result; + + if(it->second->signature_result != SELF_SIGNATURE_RESULT_PENDING) // found it. Copy the result, and remove from the queue. + { + // We should check for the exact data match, for the sake of being totally secure. + // + std::cerr << "Found into queue: returning it" << std::endl; + + memcpy(sign,it->second->sign,*it->second->signlen); + *signlen = *(it->second->signlen); + + delete it->second; + _deferred_signature_queue.erase(it); + } + + return true; // already registered, but not done yet. + } + + // Not found. Store in the queue and emit a signal. + // + std::cerr << "NotifyTxt:: deferred signature event requeted. Pushing into queue" << std::endl; + + SignatureEventData *edta = new SignatureEventData(data,len,*signlen, reason); + + _deferred_signature_queue[chksum.toStdString()] = edta; + } + handleSignatureEvent(); + return true; +} + +void RsControlModule::handleSignatureEvent() +{ + std::cerr << "NotifyTxt:: performing a deferred signature in the main GUI thread." << std::endl; + + static bool working = false ; + + if(!working) + { + working = true ; + + for(std::map::const_iterator it(_deferred_signature_queue.begin());it!=_deferred_signature_queue.end();++it) + it->second->performSignature() ; + + working = false ; + } +} + void RsControlModule::setRunState(RunState s, std::string errstr) { RsStackMutex stack(mDataMtx); // ********** LOCKED ********** diff --git a/libresapi/src/api/RsControlModule.h b/libresapi/src/api/RsControlModule.h index d3c8ad18b..338027e15 100644 --- a/libresapi/src/api/RsControlModule.h +++ b/libresapi/src/api/RsControlModule.h @@ -9,6 +9,8 @@ namespace resource_api{ class StateTokenServer; class ApiServer; +class SignatureEventData; + // resource api module to control accounts, startup and shutdown of retroshare // - this module handles everything, no things are required from outside // - exception: users of this module have to create an api server and register this module @@ -31,8 +33,9 @@ public: // returns true if the process should terminate bool processShouldExit(); - // from NotifyClient - virtual bool askForPassword(const std::string &title, const std::string& key_details, bool prev_is_bad , std::string& password,bool& canceled); + // from NotifyClient + virtual bool askForPassword(const std::string &title, const std::string& key_details, bool prev_is_bad , std::string& password,bool& canceled) override; + virtual bool askForDeferredSelfSignature(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,int& signature_result, std::string reason = "") override; protected: // from RsThread @@ -84,6 +87,9 @@ private: // we store the password in this variable, it has higher priority than the normal password variable // it is also to avoid a lock, when we make a synchronous call into librs, like in ssl cert generation std::string mFixedPassword; + + void handleSignatureEvent(); + std::map _deferred_signature_queue ; }; } // namespace resource_api