diff --git a/src/common/crypto/cipher.cpp b/src/common/crypto/cipher.cpp deleted file mode 100644 index a6ab929..0000000 --- a/src/common/crypto/cipher.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "cipher.hpp" - -template -static void _swap(T& a, T& b) { - T tmp = a; - a = b; - b = tmp; -} - -sosc::cgc::Cipher::Cipher(const KeyExchange& key) { - std::string key_raw - = key.GetPrivateKey().ToRawString((uint64_t)key.key_size_bytes); - - for(int i = 0; i < this->state_size; ++i) - this->state[i] = (uint8_t)i; - - int i = 0, j = 0; - for(i = 0; i < this->state_size; ++i) - j = (j + this->state[i] + key_raw[i % key_raw.length()]) % 256; - - GenerateStream(1024); -} - -void sosc::cgc::Cipher::Parse - (std::string* data, std::string::size_type offset) -{ - std::string stream = this->GenerateStream(data->length() - offset); - for(std::string::size_type i = offset; i < data->length(); ++i) - (*data)[i] ^= stream[i]; -} - -std::string sosc::cgc::Cipher::GenerateStream(uint64_t length) { - std::string stream(length, 0); - int i = 0, j = 0; - - for(uint64_t x = 0; x < length; ++x) { - i = (i + 1) % 256; - j = (j + this->state[i]) % 256; - _swap(this->state[i], this->state[j]); - - stream[x] = - this->state[(this->state[i] + this->state[j]) % 256]; - } - - return stream; -} diff --git a/src/common/crypto/cipher.hpp b/src/common/crypto/cipher.hpp deleted file mode 100644 index 4ead268..0000000 --- a/src/common/crypto/cipher.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef SOSC_CRYPTO_CIPHER_H -#define SOSC_CRYPTO_CIPHER_H - -#include "utils/bigint.hpp" -#include "keyex.hpp" - -namespace sosc { -namespace cgc { -class Cipher { -public: - Cipher() = default; - explicit Cipher(const KeyExchange& key); - - void Parse(std::string* data, std::string::size_type offset = 0); -private: - std::string GenerateStream(uint64_t length); - - static const int state_size = 256; - uint8_t state[state_size]; -}; -}} - -#endif diff --git a/src/common/crypto/keyex.cpp b/src/common/crypto/keyex.cpp deleted file mode 100644 index 9a4ca5c..0000000 --- a/src/common/crypto/keyex.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "keyex.hpp" - -sosc::BigUInt sosc::cgc::KeyExchange::secret; - -sosc::cgc::KeyExchange::KeyExchange() { - if(this->secret.IsZero()) - this->secret = FastRandomPrime(); - - this->modulus = FastRandomPrime(); -} - -sosc::Packet sosc::cgc::KeyExchange::GenerateRequest() const { - return Packet(1, { - this->generator.ToString(), - this->modulus.ToString(), - BigUInt::ModPow(this->generator, this->secret, this->modulus) - }); -} - -bool sosc::cgc::KeyExchange::ParseRequest - (const Packet& request, Packet* response, uint8_t id) -{ - if(request.GetId() != 1 || request.RegionCount() != 3) - return false; - - BigUInt generator, modulus, public_key; - bool check = generator.Parse(request[0]); - check = check || modulus.Parse(request[1]); - check = check || public_key.Parse(request[2]); - - if(!check) - return false; - - this->private_key = BigUInt::ModPow(public_key, this->secret, modulus); - public_key = BigUInt::ModPow(generator, this->secret, modulus); - *response = Packet(id, { public_key.ToString() }); - - return true; -} - -bool sosc::cgc::KeyExchange::ParseResponse(const Packet& response) { - if(response.GetId() != 1 || response.RegionCount() != 1) - return false; - - BigUInt public_key; - if(!public_key.Parse(response[0])) - return false; - - this->private_key = - BigUInt::ModPow(public_key, this->secret, this->modulus); - - return true; -} - -sosc::BigUInt sosc::cgc::KeyExchange::FastRandomPrime() { - BigUInt prime; - for(int i = 0; i < this->key_size_bytes; i += 16) - prime += BigUInt::GenerateRandomPrime(16) << (i * 8); - - return prime; -} - diff --git a/src/common/crypto/keyex.hpp b/src/common/crypto/keyex.hpp deleted file mode 100644 index d1f8810..0000000 --- a/src/common/crypto/keyex.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef SOSC_CRYPTO_KEYEX_H -#define SOSC_CRYPTO_KEYEX_H - -#include "utils/bigint.hpp" -#include "sock/packet.hpp" - -namespace sosc { -namespace cgc { -class KeyExchange { -public: - KeyExchange(); - - Packet GenerateRequest() const; - bool ParseRequest(const Packet& request, Packet* response, uint8_t id); - bool ParseResponse(const Packet& response); - - inline bool Succeeded() const { - return !this->private_key.IsZero(); - } - - inline const BigUInt& GetPrivateKey() const { - return this->private_key; - } - - const int key_size = 512; - const int key_size_bytes = key_size / 8; -private: - BigUInt FastRandomPrime(); - - const BigUInt generator = BigUInt(2u); - static BigUInt secret; - BigUInt modulus; - - BigUInt private_key; -}; -}} - -#endif diff --git a/src/common/utils/net.cpp b/src/common/utils/net.cpp index bb71280..0c9157a 100644 --- a/src/common/utils/net.cpp +++ b/src/common/utils/net.cpp @@ -8,7 +8,7 @@ sosc::net::IpAddress::IpAddress() { void sosc::net::IpAddress::Reset() { for(int i = 0; i < 8; ++i) - this->parts[i] = std::make_pair(0, 0); + this->parts[i] = std::make_pair(0, 0); } bool sosc::net::IpAddress::ParseError() { @@ -161,11 +161,8 @@ bool sosc::net::IpAddress::IsIPv4() const { for(int i = 0; i < 5; ++i) if(this->parts[i] != blank) return false; - - if(this->parts[5] != std::make_pair(0xFFFF, 0)) - return false; - - return true; + + return this->parts[5] == std::make_pair(0xFFFF, 0); } // END IPADDRESS CLASS diff --git a/src/common/utils/net.hpp b/src/common/utils/net.hpp index 1f6730c..c9e0d0d 100644 --- a/src/common/utils/net.hpp +++ b/src/common/utils/net.hpp @@ -1,6 +1,7 @@ #ifndef SOSC_UTIL_NET_H #define SOSC_UTIL_NET_H +#include #include #include #include diff --git a/src/server/hosts/master.hpp b/src/server/hosts/master.hpp index 5b57e6e..1ccfba0 100644 --- a/src/server/hosts/master.hpp +++ b/src/server/hosts/master.hpp @@ -5,9 +5,6 @@ #include "sock/scapesock.hpp" #include "sock/pool.hpp" -#include "crypto/keyex.hpp" -#include "crypto/cipher.hpp" - #include "db/database.hpp" #include "ctx/master.hpp" @@ -22,8 +19,6 @@ public: private: ScapeConnection sock; - cgc::KeyExchange key; - cgc::Cipher cipher; }; class MasterClientPool : public Pool { @@ -44,7 +39,7 @@ protected: class MasterIntra { public: explicit MasterIntra(const IntraClient& client); - bool Process(const Pool::Queries* queries); + bool Process(const Queries* queries); bool Close(); bool Close(const Packet& message); @@ -72,8 +67,6 @@ private: }; IntraClient sock; - cgc::KeyExchange key; - cgc::Cipher cipher; bool authed; int auth_attempts; @@ -82,7 +75,7 @@ private: int32_t server_id; std::string license; - const Pool::Queries* queries; + const Queries* queries; }; class MasterIntraPool : public Pool { diff --git a/src/server/hosts/master_intra.cpp b/src/server/hosts/master_intra.cpp index 48a521e..c4310eb 100644 --- a/src/server/hosts/master_intra.cpp +++ b/src/server/hosts/master_intra.cpp @@ -83,7 +83,7 @@ sosc::MasterIntra::MasterIntra(const IntraClient& client) { this->auth_attempts = 0; } -bool sosc::MasterIntra::Process(const Pool::Queries* queries) { +bool sosc::MasterIntra::Process(const Queries* queries) { Packet pck; int status = this->sock.Receive(&pck); if(status == PCK_ERR) @@ -105,18 +105,7 @@ bool sosc::MasterIntra::Process(const Pool::Queries* queries) { } bool sosc::MasterIntra::InitAttempt(sosc::Packet& pck) { - if(!pck.Check(1, key.key_size_bytes)) - return this->Close( - Packet(kEncryptionError, { net::htonv(0x100) })); - - Packet response; - if(!this->key.ParseRequest(pck, &response, kKeyExchange)) - return this->Close( - Packet(kEncryptionError, { net::htonv(0x101) })); - - this->cipher = cgc::Cipher(this->key); - this->sock.Send(response); - this->sock.SetCipher(&this->cipher); + } bool sosc::MasterIntra::Authentication(sosc::Packet& pck) { diff --git a/src/server/sock/intrasock.cpp b/src/server/sock/intrasock.cpp index b84acdb..3372d58 100644 --- a/src/server/sock/intrasock.cpp +++ b/src/server/sock/intrasock.cpp @@ -6,11 +6,12 @@ sosc::IntraClient::IntraClient() { this->client_open = false; - this->cipher = nullptr; } -bool sosc::IntraClient::Open(const std::string& host, uint16_t port) { - if(!this->client.Open(host, port)) +bool sosc::IntraClient::Open + (const std::string& host, uint16_t port, bool secure) +{ + if(!this->client.Open(host, port, secure)) return false; this->client_open = true; @@ -22,15 +23,6 @@ void sosc::IntraClient::Open(const TcpClient& client) { this->client_open = true; } -bool sosc::IntraClient::IsCiphered() const { - return this->cipher != nullptr; -} - -void sosc::IntraClient::SetCipher(cgc::Cipher *cipher) { - this->cipher = cipher; - cipher->Parse(&this->buffer); -} - int sosc::IntraClient::Receive(Packet* packet, bool block) { if(!this->client_open) return PCK_ERR; @@ -46,8 +38,6 @@ int sosc::IntraClient::Receive(Packet* packet, bool block) { std::string::size_type offset = this->buffer.size(); status = this->client.Receive (&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0)); - if(this->IsCiphered()) - this->cipher->Parse(&this->buffer, offset); if(status == -1) return PCK_ERR; @@ -60,13 +50,8 @@ int sosc::IntraClient::Receive(Packet* packet, bool block) { bool sosc::IntraClient::Send(const Packet& packet) { if(!this->client_open) return false; - - std::string packet_raw; - packet.ToString(&packet_raw); - if(this->IsCiphered()) - this->cipher->Parse(&packet_raw); - return this->client.Send(packet_raw); + return this->client.Send(packet.ToString()); } /****************************/ diff --git a/src/server/sock/intrasock.hpp b/src/server/sock/intrasock.hpp index 58fa9bb..c88f7c2 100644 --- a/src/server/sock/intrasock.hpp +++ b/src/server/sock/intrasock.hpp @@ -3,16 +3,12 @@ #include "tcpsock.hpp" #include "packet.hpp" -#include "crypto/cipher.hpp" namespace sosc { class IntraClient { public: IntraClient(); - bool Open(const std::string& host, uint16_t port); - - bool IsCiphered() const; - void SetCipher(cgc::Cipher* cipher); + bool Open(const std::string& host, uint16_t port, bool secure = false); int Receive(Packet* packet, bool block = false); bool Send(const Packet& packet); @@ -35,7 +31,6 @@ private: bool client_open; TcpClient client; std::string buffer; - cgc::Cipher* cipher; friend class IntraServer; }; diff --git a/src/server/sock/pool.hpp b/src/server/sock/pool.hpp index c10aafa..a8c1549 100644 --- a/src/server/sock/pool.hpp +++ b/src/server/sock/pool.hpp @@ -8,6 +8,8 @@ #include "db/database.hpp" namespace sosc { +typedef std::vector Queries; + typedef struct { // amount of threads to start with (never close) int initial_count = 3; @@ -42,8 +44,6 @@ public: } virtual void Stop(); - - typedef std::vector Queries; protected: virtual void SetupQueries(Queries* queries) {}; virtual bool ProcessClient diff --git a/src/server/sock/scapesock.cpp b/src/server/sock/scapesock.cpp index 71c10ac..53f4bf0 100644 --- a/src/server/sock/scapesock.cpp +++ b/src/server/sock/scapesock.cpp @@ -7,7 +7,6 @@ sosc::ScapeConnection::ScapeConnection() { this->client_open = false; this->handshaked = false; - this->cipher = nullptr; } void sosc::ScapeConnection::Open(const TcpClient& client) { @@ -15,15 +14,6 @@ void sosc::ScapeConnection::Open(const TcpClient& client) { this->client_open = true; } -bool sosc::ScapeConnection::IsCiphered() const { - return this->cipher != nullptr; -} - -void sosc::ScapeConnection::SetCipher(cgc::Cipher* cipher) { - this->cipher = cipher; - cipher->Parse(&this->buffer); -} - int sosc::ScapeConnection::Handshake() { if(this->handshaked) return SOSC_SHAKE_DONE; @@ -98,9 +88,6 @@ int sosc::ScapeConnection::Receive(Packet* packet, bool block) { if(status == -1) return PCK_ERR; - if(this->IsCiphered()) - this->cipher->Parse(&this->buffer, bufferSize); - first_recv = false; } @@ -124,8 +111,6 @@ bool sosc::ScapeConnection::Send(const Packet& packet) { std::string packet_raw; packet.ToString(&packet_raw); - if(this->IsCiphered()) - this->cipher->Parse(&packet_raw); return this->client.Send(packet_raw); } @@ -140,12 +125,12 @@ sosc::ScapeServer::ScapeServer() { this->server_open = false; } -bool sosc::ScapeServer::Listen(uint16_t port) { +bool sosc::ScapeServer::Listen(uint16_t port, bool secure) { if(this->server_open) return false; this->server = TcpServer(); - this->server.Listen(port); + this->server.Listen(port, secure); this->server_open = true; return true; } @@ -158,3 +143,7 @@ bool sosc::ScapeServer::Accept(ScapeConnection* client) { client->Open(raw_client); return true; } + +/****************************/ +/* END SCAPESERVER CODE */ +/****************************/ diff --git a/src/server/sock/scapesock.hpp b/src/server/sock/scapesock.hpp index 49f9f6c..2d366c6 100644 --- a/src/server/sock/scapesock.hpp +++ b/src/server/sock/scapesock.hpp @@ -4,7 +4,6 @@ #include #include "crypto/sha1.hpp" #include "crypto/base64.hpp" -#include "crypto/cipher.hpp" #include "frame.hpp" #include "packet.hpp" #include "tcpsock.hpp" @@ -18,9 +17,6 @@ class ScapeConnection { public: ScapeConnection(); - bool IsCiphered() const; - void SetCipher(cgc::Cipher* cipher); - int Handshake(); int Receive(Packet* packet, bool block = false); bool Send(const Packet& packet); @@ -47,7 +43,6 @@ private: bool client_open; bool handshaked; TcpClient client; - cgc::Cipher* cipher; std::string buffer; std::string pck_frames; @@ -59,7 +54,7 @@ class ScapeServer { public: ScapeServer(); - bool Listen(uint16_t port); + bool Listen(uint16_t port, bool secure = false); bool Accept(ScapeConnection* client); inline bool IsOpen() const {