From b24b5a1346e1ab0ed11a117200a8fdc97ca08da4 Mon Sep 17 00:00:00 2001 From: peri4 Date: Wed, 11 Sep 2024 15:44:02 +0300 Subject: [PATCH] add encryption --- libs/client_server/piclient_server_client.cpp | 9 +++++++++ libs/client_server/piclient_server_server.cpp | 8 +++++++- libs/main/client_server/piclient_server_client.h | 2 ++ libs/main/client_server/piclient_server_server.h | 3 ++- main.cpp | 9 +++++---- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/libs/client_server/piclient_server_client.cpp b/libs/client_server/piclient_server_client.cpp index 2b97a01c..d5b51ee2 100644 --- a/libs/client_server/piclient_server_client.cpp +++ b/libs/client_server/piclient_server_client.cpp @@ -71,6 +71,15 @@ int PIClientServer::Client::write(const void * d, const size_t s) { } +void PIClientServer::Client::enableSymmetricEncryption(const PIByteArray & key) { + if (key.isNotEmpty()) { + stream.setCryptEnabled(true); + stream.setCryptKey(key); + } else + stream.setCryptEnabled(false); +} + + void PIClientServer::Client::createForServer(PIEthernet * tcp_) { tcp = tcp_; tcp->setParameter(PIEthernet::KeepConnection, false); diff --git a/libs/client_server/piclient_server_server.cpp b/libs/client_server/piclient_server_server.cpp index cb1d4aa3..09260ebd 100644 --- a/libs/client_server/piclient_server_server.cpp +++ b/libs/client_server/piclient_server_server.cpp @@ -91,6 +91,11 @@ void PIClientServer::Server::setMaxClients(int new_max_clients) { } +void PIClientServer::Server::enableSymmetricEncryption(const PIByteArray & key) { + crypt_key = key; +} + + void PIClientServer::Server::stopServer() { if (!tcp_server) return; tcp_server->stopAndWait(); @@ -98,8 +103,9 @@ void PIClientServer::Server::stopServer() { void PIClientServer::Server::newClient(Client * c) { - c->readed_func = [this, c](PIByteArray ba) { readed(c, ba); }; clients << c; + c->enableSymmetricEncryption(crypt_key); + c->readed_func = [this, c](PIByteArray ba) { readed(c, ba); }; c->tcp->startThreadedRead(); c->connected(); piCout << "New client"; diff --git a/libs/main/client_server/piclient_server_client.h b/libs/main/client_server/piclient_server_client.h index 660e5539..3181523f 100644 --- a/libs/main/client_server/piclient_server_client.h +++ b/libs/main/client_server/piclient_server_client.h @@ -54,6 +54,8 @@ public: int write(const void * d, const size_t s); int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); } + void enableSymmetricEncryption(const PIByteArray & key); + protected: virtual void readed(PIByteArray data) {} virtual void connected() {} diff --git a/libs/main/client_server/piclient_server_server.h b/libs/main/client_server/piclient_server_server.h index 070c8b0d..7143db71 100644 --- a/libs/main/client_server/piclient_server_server.h +++ b/libs/main/client_server/piclient_server_server.h @@ -49,7 +49,7 @@ public: void setClientFactory(std::function f) { client_factory = f; } - void write(Client * c, const PIByteArray & data); + void enableSymmetricEncryption(const PIByteArray & key); protected: virtual void readed(Client * c, PIByteArray data) {} @@ -61,6 +61,7 @@ private: std::function client_factory; PIEthernet * tcp_server = nullptr; PIThread * clean_thread = nullptr; + PIByteArray crypt_key; PIVector clients; PIMutex clients_mutex; diff --git a/main.cpp b/main.cpp index 5f62cd3e..dd59b5b6 100644 --- a/main.cpp +++ b/main.cpp @@ -28,7 +28,7 @@ protected: send_thread.start( [this] { // write((PIString::fromNumber(++counter)).toUTF8()); - PIByteArray ba(64_MiB); + PIByteArray ba(64_KiB); write(ba); }, 2_Hz); @@ -41,20 +41,21 @@ protected: int main(int argc, char * argv[]) { kbd.enableExitCapture(); - piCout << argc; PIClientServer::Server * s = nullptr; PIClientServer::Client * c = nullptr; if (argc > 1) { - // server + piCout << "Server"; s = new PIClientServer::Server(); s->setClientFactory([] { return new MyClient(); }); + s->enableSymmetricEncryption("1122334455667788"_hex); s->listenAll(12345); } else { - // client + piCout << "Client"; c = new MyClient(); c->createNew(); + c->enableSymmetricEncryption("1122334455667788"_hex); c->connect(PINetworkAddress::resolve("127.0.0.1", 12345)); }