From 0fb433690c0ca5f9561fe9e2e973e2cd61b873ba Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 11 Oct 2016 12:39:05 +0200 Subject: Commit introducing Client keyexchange, triggering bugs. Clients now send their public key to each node and the node calculates the shared secret. The node does not yet respond with it's public key. To keep this commit smaller. Nodes now disconnect from each other in a better way. Getting the relevant crypto api is now done with a generic function. What crypto algorithm and implemenation is beign used can be selected in the cmake cache (use cmake-gui or ccmake) Clients now connect correctly to multiple nodes. --- client/cmixclient.cpp | 82 ++++++++++++++++++++++++++++++++++++++++----------- client/cmixclient.hpp | 13 +++++++- client/main.cpp | 2 +- client/node.cpp | 4 +++ client/node.hpp | 7 +++++ 5 files changed, 89 insertions(+), 19 deletions(-) (limited to 'client') diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp index e3f5ac4..195c5e0 100644 --- a/client/cmixclient.cpp +++ b/client/cmixclient.cpp @@ -1,38 +1,86 @@ #include "cmixclient.hpp" -void CMixClient::initialized() { - BOOST_LOG_TRIVIAL(trace) << "Client connections were made"; - for(auto&& connection : network_connections) { - cmix_proto::Bye bye; - connection.send(bye); - } - io_service.stop(); +void CMixClient::key_exchange(int i) { + shared_keys.resize(network_connections.size()); + + cmix_proto::KeyExchange ke; + ke.set_public_key(keypair.pub, keypair.pub_len); + + network_connections[i].send(ke); + + cmix_proto::Bye bye; + network_connections[i].send(bye); } void CMixClient::initialize_connections() { network_connections.reserve(network_details.size()); - int i = network_details.size(); - auto handler = [this, i]() mutable { - cmix_proto::ImAClient imaclient; - network_connections.at(network_details.size() - i).send(imaclient); + for(int i = 0; i < network_details.size(); ++i) { + auto handler = [this, i]() mutable { + cmix_proto::ImAClient imaclient; + BOOST_LOG_TRIVIAL(trace) << "sending imaclient to node: " << i; + network_connections.at(i).send(imaclient); - if(--i == 0) { - initialized(); - } - }; + key_exchange(i); + }; - for(auto&& details : network_details) { network_connections.emplace_back(boost::asio::ip::tcp::socket(io_service)); - network_connections.back().async_connect(details.host, details.port, handler); + network_connections.back().async_connect(network_details[i].host, network_details[i].port, handler); + + } +} + +cmix_proto::CMixMessage CMixClient::parse_cmix_message(std::vector const& buffer) +{ + cmix_proto::CMixMessage message; + if(!message.ParseFromArray(buffer.data(), buffer.size())) { + BOOST_LOG_TRIVIAL(error) << "Received something which was not a CMixMessage"; + throw std::runtime_error("Network communication was disrupted in a unrecoverable way."); + } + return message; +} + +void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke) +{ + shared_keys[node_id] = api.derive_shared_key(keypair, reinterpret_cast(ke.public_key().c_str()), false); +} + +void CMixClient::handle_message(int node_id, std::vector const& message_buffer) +{ + cmix_proto::CMixMessage message; + try { + message = parse_cmix_message(message_buffer); + } catch(std::runtime_error const& e) { + for(auto&& connection : network_connections) { + connection.close(); + } + io_service.stop(); + return; + } + + switch(message.contents_case()) { + case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: { + handle_key_exchange(node_id, *message.mutable_keyexchange()); + return; + } + default: { + BOOST_LOG_TRIVIAL(error) << "Received unknown message"; + } } + + for(auto&& connection : network_connections) { + connection.close(); + } + io_service.stop(); } CMixClient::CMixClient(std::vector details) : io_service() , network_details(details) , network_connections() +, api(get_implementation()) +, keypair(api.create_key_pair()) { initialize_connections(); } diff --git a/client/cmixclient.hpp b/client/cmixclient.hpp index 10438d1..5c6405a 100644 --- a/client/cmixclient.hpp +++ b/client/cmixclient.hpp @@ -2,6 +2,7 @@ #include "node.hpp" +#include "api.h" #include "logging.hpp" #include "client.hpp" #include "connect.hpp" @@ -22,10 +23,20 @@ class CMixClient { std::vector network_details; std::vector network_connections; - void initialized(); + Api api; + KeyPair keypair; + std::vector shared_keys; + + void key_exchange(int i); void initialize_connections(); + cmix_proto::CMixMessage parse_cmix_message(std::vector const& buffer); + + void handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke); + + void handle_message(int node_id, std::vector const& message_buffer); + public: CMixClient(std::vector details); diff --git a/client/main.cpp b/client/main.cpp index fb05171..bcac249 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char* argv[]) { init_logging(boost::log::trivial::severity_level::trace, "client"); - BOOST_LOG_TRIVIAL(info) << "Started node"; + BOOST_LOG_TRIVIAL(info) << "Started client"; po::options_description desc("Allowed options"); desc.add_options() diff --git a/client/node.cpp b/client/node.cpp index 8fd1dd8..46375f4 100644 --- a/client/node.cpp +++ b/client/node.cpp @@ -13,6 +13,10 @@ void Node::async_connect(std::string next_host, std::string next_port, std::func client.async_connect(next_host, next_port, on_connect); } +void Node::receive(std::function&)> receive_handler) { + client.receive(receive_handler); +} + void Node::close() { client.close(); diff --git a/client/node.hpp b/client/node.hpp index 3719223..afcf56a 100644 --- a/client/node.hpp +++ b/client/node.hpp @@ -22,6 +22,7 @@ inline void message_setter(cmix_proto::CMixMessage& m, cmix_proto::TYPE const& v MESSAGE_SETTER(ImAClient, imaclient) MESSAGE_SETTER(Bye, bye) +MESSAGE_SETTER(KeyExchange, keyexchange) #undef MESSAGE_SETTER @@ -49,6 +50,12 @@ public: client.send(m.SerializeAsString()); } + /*! + * \brief receive + * \param receive_handler + */ + void receive(std::function const&)> receive_handler); + /*! * \brief async_connect * \param next_host The host of the next node. -- cgit v1.2.3-70-g09d2