refactoring PICrypt, add PIStreamPackerConfig, delete piclientserver_config
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -25,7 +25,6 @@
|
||||
#ifndef piclientserver_client_base_H
|
||||
#define piclientserver_client_base_H
|
||||
|
||||
#include "piclientserver_config.h"
|
||||
#include "pidiagnostics.h"
|
||||
#include "pip_client_server_export.h"
|
||||
#include "pistreampacker.h"
|
||||
@@ -36,8 +35,10 @@ namespace PIClientServer {
|
||||
|
||||
class Server;
|
||||
|
||||
class ClientInterface {};
|
||||
|
||||
// template<bool EnableDiagnostics = false>
|
||||
class PIP_CLIENT_SERVER_EXPORT ClientBase {
|
||||
friend class Config;
|
||||
friend class Server;
|
||||
NO_COPY_CLASS(ClientBase);
|
||||
|
||||
@@ -57,7 +58,9 @@ public:
|
||||
PIDiagnostics::State diagnostics() const;
|
||||
int receivePacketProgress() const;
|
||||
|
||||
Config & configuration() { return config; }
|
||||
const PIStreamPackerConfig & configuration() const { return stream.configuration(); }
|
||||
PIStreamPackerConfig & configuration() { return stream.configuration(); }
|
||||
void setConfiguration(const PIStreamPackerConfig & config) { stream.setConfiguration(config); }
|
||||
|
||||
protected:
|
||||
virtual void readed(PIByteArray data) {}
|
||||
@@ -71,7 +74,6 @@ protected:
|
||||
bool own_tcp = false;
|
||||
std::atomic_bool can_write = {true};
|
||||
PIEthernet * tcp = nullptr;
|
||||
Config config;
|
||||
|
||||
private:
|
||||
void destroy();
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
/*! \file piclientserver_config.h
|
||||
* \ingroup ClientServer
|
||||
* \~\brief
|
||||
* \~english
|
||||
* \~russian
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef piclientserver_config_H
|
||||
#define piclientserver_config_H
|
||||
|
||||
#include "pibytearray.h"
|
||||
#include "pip_client_server_export.h"
|
||||
|
||||
|
||||
namespace PIClientServer {
|
||||
|
||||
class Server;
|
||||
class Client;
|
||||
class ClientBase;
|
||||
|
||||
class PIP_CLIENT_SERVER_EXPORT Config {
|
||||
friend class Server;
|
||||
friend class Client;
|
||||
|
||||
public:
|
||||
void setPacketSign(ushort sign);
|
||||
void setPacketSize(int bytes);
|
||||
|
||||
void enableSymmetricEncryption(const PIByteArray & key);
|
||||
|
||||
protected:
|
||||
void apply(ClientBase * client);
|
||||
|
||||
PIByteArray crypt_key;
|
||||
ushort packet_sign = 0xAFBE;
|
||||
int packet_size = 1400;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace PIClientServer
|
||||
|
||||
#endif
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -25,10 +25,10 @@
|
||||
#ifndef piclientserver_server_H
|
||||
#define piclientserver_server_H
|
||||
|
||||
#include "piclientserver_config.h"
|
||||
#include "pimutex.h"
|
||||
#include "pinetworkaddress.h"
|
||||
#include "pip_client_server_export.h"
|
||||
#include "pistreampacker.h"
|
||||
#include "pithreadnotifier.h"
|
||||
|
||||
class PIEthernet;
|
||||
@@ -38,7 +38,7 @@ namespace PIClientServer {
|
||||
|
||||
class ServerClient;
|
||||
|
||||
class PIP_CLIENT_SERVER_EXPORT Server {
|
||||
class PIP_CLIENT_SERVER_EXPORT Server: public PIStreamPackerConfig {
|
||||
friend class ServerClient;
|
||||
NO_COPY_CLASS(Server);
|
||||
|
||||
@@ -58,8 +58,6 @@ public:
|
||||
|
||||
void setClientFactory(std::function<ServerClient *()> f) { client_factory = f; }
|
||||
|
||||
Config & configuration() { return config; }
|
||||
|
||||
private:
|
||||
void stopServer();
|
||||
void newClient(ServerClient * c);
|
||||
@@ -70,7 +68,6 @@ private:
|
||||
PIEthernet * tcp_server = nullptr;
|
||||
PIThread * clean_thread = nullptr;
|
||||
PIThreadNotifier clean_notifier;
|
||||
Config config;
|
||||
PIVector<ServerClient *> clients;
|
||||
mutable PIMutex clients_mutex;
|
||||
|
||||
|
||||
@@ -5,22 +5,22 @@
|
||||
* \~russian Шифрование с помощью libsodium
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Cryptographic class using lib Sodium
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Cryptographic class using lib Sodium
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PICRYPT_H
|
||||
@@ -40,6 +40,8 @@ public:
|
||||
//! \~russian Конструктор, генерирующий случайный ключ
|
||||
PICrypt();
|
||||
|
||||
~PICrypt();
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Set key to "key", key size must be a \a sizeKey()
|
||||
//! \~russian Установить ключ "key", размер ключа должен быть равен \a sizeKey()
|
||||
@@ -48,7 +50,7 @@ public:
|
||||
//! \~\brief
|
||||
//! \~english Generate and set key from keyphrase "secret"
|
||||
//! \~russian Генерировать и установить ключ из ключевой фразы "secret"
|
||||
PIByteArray setKey(const PIString & secret);
|
||||
bool setKey(const PIString & secret);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Returns current key
|
||||
@@ -123,12 +125,12 @@ public:
|
||||
//! \~\brief
|
||||
//! \~english Function randomly generates a secret key and a corresponding public key for digital signature
|
||||
//! \~russian Функция случайным образом генерирует секретный ключ и соответствующий ему открытый ключ для цифровой подписи
|
||||
static void generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key);
|
||||
static bool generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Function generates a secret key from input data and a corresponding public key for digital signature
|
||||
//! \~russian Функция генерирует секретный ключ из входных данных и соответствующий ему открытый ключ для цифровой подписи
|
||||
static void generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
|
||||
static bool generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Function extract sign public key from sing secret key
|
||||
@@ -138,24 +140,24 @@ public:
|
||||
//! \~\brief
|
||||
//! \~english Calculate digital signature for data
|
||||
//! \~russian Вычислить цифровую подпись для данных
|
||||
PIByteArray signMessage(const PIByteArray & data, PIByteArray secret_key);
|
||||
PIByteArray signMessage(const PIByteArray & data, const PIByteArray & secret_key);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Verify digital signature of signed message
|
||||
//! \~russian Проверить цифровую подпись подписанного сообщения
|
||||
bool verifySign(const PIByteArray & data, const PIByteArray & signature, PIByteArray public_key);
|
||||
bool verifySign(const PIByteArray & data, const PIByteArray & signature, const PIByteArray & public_key);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Function randomly generates a secret key and a corresponding public key for authenticated encryption
|
||||
//! \~russian Функция случайным образом генерирует секретный ключ и соответствующий ему открытый ключ для аутентифицированного
|
||||
//! шифрования
|
||||
static void generateKeypair(PIByteArray & public_key, PIByteArray & secret_key);
|
||||
static bool generateKeypair(PIByteArray & public_key, PIByteArray & secret_key);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Function generates a secret key from input data and a corresponding public key for authenticated encryption
|
||||
//! \~russian Функция генерирует секретный ключ из входных данных и соответствующий ему открытый ключ для аутентифицированного
|
||||
//! шифрования
|
||||
static void generateKeypair(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
|
||||
static bool generateKeypair(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
|
||||
|
||||
//! \~\brief
|
||||
//! \~english Encrypt given data "data"
|
||||
|
||||
@@ -57,6 +57,9 @@ public:
|
||||
//! Returns crypt layer key
|
||||
PIByteArray cryptKey() const;
|
||||
|
||||
//! \brief Returns addition size for crypted data.
|
||||
static size_t cryptSizeAddition();
|
||||
|
||||
protected:
|
||||
/*! \brief Returns encrypted data if layer enabled,
|
||||
* otherwise returns unchanged \"data\" */
|
||||
|
||||
@@ -33,15 +33,17 @@
|
||||
|
||||
class PIIODevice;
|
||||
|
||||
class PIP_IO_UTILS_EXPORT PIStreamPacker
|
||||
: public PIObject
|
||||
, public PIEthUtilBase {
|
||||
PIOBJECT(PIStreamPacker)
|
||||
class PIStreamPackerConfig: public PIEthUtilBase {
|
||||
friend class PIStreamPacker;
|
||||
|
||||
public:
|
||||
//! Contructs packer and try to assign \"dev\"
|
||||
PIStreamPacker(PIIODevice * dev = 0);
|
||||
|
||||
PIStreamPackerConfig() {
|
||||
crypt_frag = crypt_size = false;
|
||||
aggressive_optimization = true;
|
||||
crypt_frag_size = 1 * 1024 * 1024;
|
||||
max_packet_size = 1400;
|
||||
packet_sign = 0xAFBE;
|
||||
}
|
||||
|
||||
//! Set maximum size of single packet
|
||||
void setMaxPacketSize(int max_size) { max_packet_size = max_size; }
|
||||
@@ -49,17 +51,12 @@ public:
|
||||
//! Returns maximum size of single packet, default 1400 bytes
|
||||
int maxPacketSize() const { return max_packet_size; }
|
||||
|
||||
|
||||
//! Set packet sinature
|
||||
void setPacketSign(ushort sign_) { packet_sign = sign_; }
|
||||
|
||||
//! Returns packet sinature, default 0xAFBE
|
||||
ushort packetSign() const { return packet_sign; }
|
||||
|
||||
//! Returns progress of current packet receive in bytes
|
||||
int receivePacketProgress() const { return packet.size_s(); }
|
||||
|
||||
|
||||
//! Set receive aggressive optimization. If yes then %PIStreamPacker doesn`t
|
||||
//! check every byte in incoming stream but check only begin of each read()
|
||||
//! result. Default is \b true.
|
||||
@@ -68,17 +65,42 @@ public:
|
||||
//! Returns aggressive optimization
|
||||
bool aggressiveOptimization() const { return aggressive_optimization; }
|
||||
|
||||
|
||||
bool cryptFragmentationEnabled() const { return crypt_frag; }
|
||||
void setCryptFragmentationEnabled(bool on) { crypt_frag = on; }
|
||||
int cryptFragmentationSize() const { return crypt_frag_size; }
|
||||
void setCryptFragmentationSize(int size_) { crypt_frag_size = size_; }
|
||||
bool cryptSizeEnabled() const { return crypt_size; }
|
||||
void setCryptSizeEnabled(bool on);
|
||||
void setCryptSizeEnabled(bool on) { crypt_size = on; }
|
||||
|
||||
//! Get configuration
|
||||
const PIStreamPackerConfig & configuration() const { return *this; }
|
||||
PIStreamPackerConfig & configuration() { return *this; }
|
||||
|
||||
//! Apply configuration
|
||||
void setConfiguration(const PIStreamPackerConfig & config) { *this = config; }
|
||||
|
||||
private:
|
||||
bool crypt_frag, crypt_size, aggressive_optimization;
|
||||
int crypt_frag_size;
|
||||
ushort packet_sign;
|
||||
int max_packet_size;
|
||||
};
|
||||
|
||||
|
||||
class PIP_IO_UTILS_EXPORT PIStreamPacker
|
||||
: public PIObject
|
||||
, public PIStreamPackerConfig {
|
||||
PIOBJECT(PIStreamPacker)
|
||||
|
||||
public:
|
||||
//! Contructs packer and try to assign \"dev\"
|
||||
PIStreamPacker(PIIODevice * dev = nullptr);
|
||||
|
||||
//! Returns progress of current packet receive in bytes
|
||||
int receivePacketProgress() const { return packet.size_s(); }
|
||||
|
||||
void clear();
|
||||
|
||||
|
||||
//! Prepare data for send and raise \a sendRequest() events
|
||||
void send(const PIByteArray & data);
|
||||
|
||||
@@ -130,11 +152,10 @@ protected:
|
||||
virtual void packetReceived(PIByteArray data) {}
|
||||
|
||||
private:
|
||||
uint sizeCryptedSize();
|
||||
|
||||
PIByteArray stream, packet;
|
||||
bool crypt_frag, crypt_size, aggressive_optimization;
|
||||
int packet_size, crypt_frag_size;
|
||||
ushort packet_sign;
|
||||
int max_packet_size, size_crypted_size;
|
||||
int packet_size;
|
||||
mutable PIMutex prog_s_mutex, prog_r_mutex;
|
||||
};
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ class PIStringList;
|
||||
//! \~russian Класс строки.
|
||||
class PIP_EXPORT PIString {
|
||||
BINARY_STREAM_FRIEND(PIString);
|
||||
friend class PICrypt;
|
||||
|
||||
public:
|
||||
typedef PIDeque<PIChar>::iterator iterator;
|
||||
|
||||
Reference in New Issue
Block a user