send/recv PGP public key and short invite

This commit is contained in:
csoler 2021-11-01 09:16:41 +01:00
parent e058b3a35f
commit 5da31592f2
5 changed files with 107 additions and 9 deletions

View File

@ -33,10 +33,20 @@ bool FsClient::requestFriends(const std::string& address,uint16_t port,uint32_t
RsFriendServerClientPublishItem *pitem = new RsFriendServerClientPublishItem();
pitem->n_requested_friends = reqs;
pitem->long_invite = rsPeers->GetRetroshareInvite();
std::string pgp_base64_string,pgp_base64_checksum,short_invite;
rsPeers->GetPGPBase64StringAndCheckSum(rsPeers->getGPGOwnId(),pgp_base64_string,pgp_base64_checksum);
if(!rsPeers->getShortInvite(short_invite,RsPeerId(),RetroshareInviteFlags::RADIX_FORMAT | RetroshareInviteFlags::DNS))
{
RsErr() << "Cannot request own short invite! Something's very wrong." ;
return false;
}
pitem->pgp_public_key_b64 = pgp_base64_string;
pitem->short_invite = short_invite;
std::list<RsItem*> response;
sendItem(address,port,pitem,response);
// now decode the response

View File

@ -26,6 +26,7 @@
#include "serialiser/rsserializer.h"
#include "rsitems/rsitem.h"
#include "serialiser/rstlvbinary.h"
#include "rsitems/rsserviceids.h"
#include "rsitems/itempriorities.h"
@ -53,18 +54,21 @@ public:
void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) override
{
RS_SERIAL_PROCESS(n_requested_friends);
RS_SERIAL_PROCESS(long_invite);
RS_SERIAL_PROCESS(short_invite);
RS_SERIAL_PROCESS(pgp_public_key_b64);
}
virtual void clear() override
{
long_invite = std::string();
pgp_public_key_b64.clear();
short_invite.clear();
n_requested_friends=0;
}
// specific members for that item
uint32_t n_requested_friends;
std::string long_invite;
std::string short_invite;
std::string pgp_public_key_b64;
};
class RsFriendServerClientRemoveItem: public RsFriendServerItem

View File

@ -1,4 +1,8 @@
#include "util/rsdebug.h"
#include "util/rsprint.h"
#include "util/rsdir.h"
#include "util/rsbase64.h"
#include "util/radix64.h"
#include "friendserver.h"
#include "friend_server/fsitem.h"
@ -58,9 +62,68 @@ void FriendServer::threadTick()
void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *item)
{
RsDbg() << "Received a client publish item from " << item->PeerId() << ":" << *item ;
try
{
RsDbg() << "Received a client publish item from " << item->PeerId() ;
RsDbg() << *item ;
// Respond with a list of potential friends
// First of all, read PGP key and short invites, parse them, and check that they contain the same information
RsDbg() << " Checking item data...";
std::string error_string;
RsPgpId pgp_id ;
std::vector<uint8_t> key_binary_data ;
key_binary_data = Radix64::decode(item->pgp_public_key_b64);
if(key_binary_data.empty())
throw std::runtime_error(" Cannot decode client pgp public key: \"" + item->pgp_public_key_b64 + "\". Wrong format??");
// if(!RsBase64::decode(item->pgp_public_key_b64,key_binary_data))
// throw std::runtime_error(" Cannot decode client pgp public key: \"" + item->pgp_public_key_b64 + "\". Wrong format??");
RsDbg() << " Public key radix is fine." ;
if(!mPgpHandler->LoadCertificateFromBinaryData(key_binary_data.data(),key_binary_data.size(), pgp_id, error_string))
throw std::runtime_error("Cannot load client's pgp public key into keyring: " + error_string) ;
RsDbg() << " Public key added to keyring.";
RsPeerDetails shortInviteDetails;
uint32_t errorCode = 0;
if(item->short_invite.empty() || !rsPeers->parseShortInvite(item->short_invite, shortInviteDetails,errorCode ))
throw std::runtime_error("Could not parse short certificate. Error = " + RsUtil::NumberToString(errorCode));
RsDbg() << " Short invite is fine. PGP fingerprint: " << shortInviteDetails.fpr ;
RsPgpFingerprint fpr_test;
if(!mPgpHandler->getKeyFingerprint(pgp_id,fpr_test))
throw std::runtime_error("Cannot get fingerprint from keyring for client public key. Something's really wrong.") ;
if(fpr_test != shortInviteDetails.fpr)
throw std::runtime_error("Cannot get fingerprint from keyring for client public key. Something's really wrong.") ;
RsDbg() << " Short invite PGP fingerprint matches the public key fingerprint." ;
// Check the item's data signature
// All good.
#warning TODO
// Store/update the peer info
auto& pi(mCurrentClientPeers[shortInviteDetails.id]);
pi.short_certificate = item->short_invite;
pi.last_connection_TS = time(nullptr);
// Respond with a list of potential friends
}
catch(std::exception& e)
{
RsErr() << e.what() ;
}
// Close client connection from server side, to tell the client that nothing more is coming.
@ -68,6 +131,7 @@ void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *it
mni->closeConnection(item->PeerId());
}
void FriendServer::handleClientRemove(const RsFriendServerClientRemoveItem *item)
{
RsDbg() << "Received a client remove item:" << *item ;
@ -76,6 +140,16 @@ FriendServer::FriendServer(const std::string& base_dir)
{
RsDbg() << "Creating friend server." ;
mBaseDirectory = base_dir;
// Create a PGP Handler
std::string pgp_public_keyring_path = RsDirUtil::makePath(base_dir,"pgp_public_keyring") ;
std::string pgp_lock_path = RsDirUtil::makePath(base_dir,"pgp_lock") ;
std::string pgp_private_keyring_path = RsDirUtil::makePath(base_dir,"pgp_private_keyring") ; // not used.
std::string pgp_trustdb_path = RsDirUtil::makePath(base_dir,"pgp_trustdb") ; // not used.
mPgpHandler = new PGPHandler(pgp_public_keyring_path,pgp_private_keyring_path,pgp_trustdb_path,pgp_lock_path);
}
void FriendServer::run()

View File

@ -23,6 +23,7 @@
#include "util/rsthreads.h"
#include "pqi/pqistreamer.h"
#include "pgp/pgphandler.h"
#include "network.h"
@ -57,6 +58,7 @@ private:
// Local members
FsNetworkInterface *mni;
PGPHandler *mPgpHandler;
std::string mBaseDirectory;

View File

@ -20,6 +20,7 @@
*/
#include "util/stacktrace.h"
#include "util/rsdir.h"
#include "util/argstream.h"
#include "util/rstime.h"
#include "util/rsdebug.h"
@ -43,15 +44,22 @@ int main(int argc, char* argv[])
//RsInit::InitRsConfig();
//RsControl::earlyInitNotificationSystem();
std::string base_directory;
std::string base_directory = "FSData";
argstream as(argc,argv);
as >> parameter( 'c',"base-dir", base_directory, "directory", "Set base directory.", false )
as >> parameter( 'c',"base-dir", base_directory, "set base directory to store data files (keys, etc)", false )
>> help( 'h', "help", "Display this Help" );
as.defaultErrorHandling(true, true);
// Create the base directory if needed
if(!RsDirUtil::checkCreateDirectory(base_directory))
{
RsErr() << "Cannot create base directory \"" << base_directory << "\". Check permissions, paths, etc." ;
return 1;
}
// Now start the real thing.
FriendServer fs(base_directory);