From 6e92ffd95cab814aee1488ddf91792b0a5925b1d Mon Sep 17 00:00:00 2001 From: Pierre Wielders Date: Sun, 8 Dec 2024 11:56:58 +0100 Subject: [PATCH] [REFACTOR] The constructors are fixed, use them fixed. --- Source/core/SocketServer.h | 2 +- Source/cryptalgo/SecureSocketPort.cpp | 62 ++++++++++++++----- Source/cryptalgo/SecureSocketPort.h | 87 +++++++++++++-------------- 3 files changed, 90 insertions(+), 61 deletions(-) diff --git a/Source/core/SocketServer.h b/Source/core/SocketServer.h index 2a2230e4a..b76926190 100644 --- a/Source/core/SocketServer.h +++ b/Source/core/SocketServer.h @@ -281,7 +281,7 @@ namespace Core { _lock.Unlock(); } - virtual void Accept(SOCKET& newClient, const NodeId& remoteId) + void Accept(SOCKET& newClient, const NodeId& remoteId) override { ProxyType client = ProxyType::Create(newClient, remoteId, &_parent); diff --git a/Source/cryptalgo/SecureSocketPort.cpp b/Source/cryptalgo/SecureSocketPort.cpp index ac241e1b0..0b5379583 100644 --- a/Source/cryptalgo/SecureSocketPort.cpp +++ b/Source/cryptalgo/SecureSocketPort.cpp @@ -305,22 +305,52 @@ void CertificateStore::Add(const Certificate& certificate) { // ----------------------------------------------------------------------------- // class SecureSocketPort::Handler // ----------------------------------------------------------------------------- +SecureSocketPort::Handler::Handler(SecureSocketPort& parent, + const enumType socketType, + const Core::NodeId& localNode, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize, + const uint32_t socketSendBufferSize, + const uint32_t socketReceiveBufferSize) + : SocketPort(socketType, localNode, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) + , _parent(parent) + , _callback(nullptr) + , _handShaking(EXCHANGE) { + CreateContext(TLS_method()); +} + +SecureSocketPort::Handler::Handler(SecureSocketPort& parent, + const enumType socketType, + const SOCKET& connector, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize, + const uint32_t socketSendBufferSize, + const uint32_t socketReceiveBufferSize) + : SocketPort(socketType, connector, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) + , _parent(parent) + , _callback(nullptr) + , _handShaking(EXCHANGE) { + CreateContext(TLS_server_method()); +} + SecureSocketPort::Handler::~Handler() { ASSERT(IsClosed() == true); Close(0); if (_ssl != nullptr) { - SSL_free(static_cast(_ssl)); + SSL_free(_ssl); _ssl = nullptr; } if (_context != nullptr) { - SSL_CTX_free(static_cast(_context)); + SSL_CTX_free(_context); _context = nullptr; } } -void SecureSocketPort::Handler::CreateContext(const bool server) { - _context = SSL_CTX_new(server ? TLS_server_method() : TLS_method()); +void SecureSocketPort::Handler::CreateContext(const struct ssl_method_st* method) { + _context = SSL_CTX_new(method); if (_context != nullptr) { _ssl = SSL_new(_context); @@ -334,13 +364,6 @@ void SecureSocketPort::Handler::CreateContext(const bool server) { VARIABLE_IS_NOT_USED unsigned long bitmask = SSL_CTX_set_options(_context, options); ASSERT((bitmask & options) == options); - - if (server == true) { - SSL_set_accept_state(_ssl); - } - else { - SSL_set_connect_state(_ssl); - } } } } @@ -351,8 +374,15 @@ uint32_t SecureSocketPort::Handler::Initialize() { ASSERT(_context != nullptr); ASSERT(_ssl != nullptr); - if (SSL_set_fd(static_cast(_ssl), static_cast(*this).Descriptor()) == 1) { + if (SSL_set_fd(_ssl, static_cast(*this).Descriptor()) == 1) { SSL_set_tlsext_host_name(_ssl, RemoteNode().HostName().c_str()); + if (IsOpen() == true) { + SSL_set_accept_state(_ssl); + } + else { + SSL_set_connect_state(_ssl); + } + initialized = Core::SocketPort::Initialize(); } @@ -367,18 +397,20 @@ int32_t SecureSocketPort::Handler::Read(uint8_t buffer[], const uint16_t length) const_cast(*this).Update(); } - return (SSL_read(static_cast(_ssl), buffer, length)); + return (SSL_read(_ssl, buffer, length)); } int32_t SecureSocketPort::Handler::Write(const uint8_t buffer[], const uint16_t length) { ASSERT(_handShaking != ERROR); + uint32_t result = SSL_write(_ssl, buffer, length); + if (_handShaking != OPEN) { Update(); } - return (SSL_write(_ssl, buffer, length)); + return (result); } uint32_t SecureSocketPort::Handler::Open(const uint32_t waitTime) { @@ -387,7 +419,7 @@ uint32_t SecureSocketPort::Handler::Open(const uint32_t waitTime) { uint32_t SecureSocketPort::Handler::Close(const uint32_t waitTime) { ASSERT(_ssl != nullptr); - SSL_shutdown(static_cast(_ssl)); + SSL_shutdown(_ssl); return(Core::SocketPort::Close(waitTime)); } diff --git a/Source/cryptalgo/SecureSocketPort.h b/Source/cryptalgo/SecureSocketPort.h index 1482219bf..c1ff30fb8 100644 --- a/Source/cryptalgo/SecureSocketPort.h +++ b/Source/cryptalgo/SecureSocketPort.h @@ -23,6 +23,7 @@ struct ssl_st; struct ssl_ctx_st; +struct ssl_method_st; struct x509_st; struct evp_pkey_st; struct x509_store_st; @@ -89,9 +90,8 @@ namespace Crypto { ~CertificateStore(); public: - static CertificateStore& Default() { - static CertificateStore defaultStore(_default); - return (defaultStore); + static CertificateStore Default() { + return (CertificateStore(_default)); } void Add(const Certificate& cert); inline operator const x509_store_st* () const { @@ -128,43 +128,13 @@ namespace Crypto { Handler& operator=(const Handler&) = delete; Handler(SecureSocketPort& parent, - const enumType socketType, - const Core::NodeId& localNode, - const Core::NodeId& remoteNode, - const uint16_t sendBufferSize, - const uint16_t receiveBufferSize) - : SocketPort(socketType, localNode, remoteNode, sendBufferSize, receiveBufferSize) - , _parent(parent) - , _callback(nullptr) - , _handShaking(EXCHANGE) { - CreateContext(false); - } - Handler(SecureSocketPort& parent, const enumType socketType, const Core::NodeId& localNode, const Core::NodeId& remoteNode, const uint16_t sendBufferSize, const uint16_t receiveBufferSize, const uint32_t socketSendBufferSize, - const uint32_t socketReceiveBufferSize) - : SocketPort(socketType, localNode, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) - , _parent(parent) - , _callback(nullptr) - , _handShaking(EXCHANGE) { - CreateContext(false); - } - Handler(SecureSocketPort& parent, - const enumType socketType, - const SOCKET& connector, - const Core::NodeId& remoteNode, - const uint16_t sendBufferSize, - const uint16_t receiveBufferSize) - : SocketPort(socketType, connector, remoteNode, sendBufferSize, receiveBufferSize) - , _parent(parent) - , _callback(nullptr) - , _handShaking(EXCHANGE) { - CreateContext(true); - } + const uint32_t socketReceiveBufferSize); Handler(SecureSocketPort& parent, const enumType socketType, const SOCKET& connector, @@ -172,13 +142,7 @@ namespace Crypto { const uint16_t sendBufferSize, const uint16_t receiveBufferSize, const uint32_t socketSendBufferSize, - const uint32_t socketReceiveBufferSize) - : SocketPort(socketType, connector, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) - , _parent(parent) - , _callback(nullptr) - , _handShaking(EXCHANGE) { - CreateContext(true); - } + const uint32_t socketReceiveBufferSize); ~Handler(); public: @@ -217,7 +181,7 @@ namespace Crypto { private: void Update(); void ValidateHandShake(); - void CreateContext(const bool server); + void CreateContext(const struct ssl_method_st* method); private: SecureSocketPort& _parent; @@ -233,10 +197,43 @@ namespace Crypto { SecureSocketPort& operator=(SecureSocketPort&&) = delete; SecureSocketPort& operator=(const SecureSocketPort&) = delete; - template - SecureSocketPort(Args&&... args) - : _handler(*this, args...) { + SecureSocketPort( + const Core::SocketPort::enumType socketType, + const Core::NodeId& localNode, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize) + : _handler(*this, socketType, localNode, remoteNode, sendBufferSize, receiveBufferSize, sendBufferSize, receiveBufferSize) { } + SecureSocketPort( + const Core::SocketPort::enumType socketType, + const Core::NodeId& localNode, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize, + const uint32_t socketSendBufferSize, + const uint32_t socketReceiveBufferSize) + : _handler(*this, socketType, localNode, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) { + } + SecureSocketPort( + const Core::SocketPort::enumType socketType, + const SOCKET& connector, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize) + : _handler(*this, socketType, connector, remoteNode, sendBufferSize, receiveBufferSize, sendBufferSize, receiveBufferSize) { + } + SecureSocketPort( + const Core::SocketPort::enumType socketType, + const SOCKET& connector, + const Core::NodeId& remoteNode, + const uint16_t sendBufferSize, + const uint16_t receiveBufferSize, + const uint32_t socketSendBufferSize, + const uint32_t socketReceiveBufferSize) + : _handler(*this, socketType, connector, remoteNode, sendBufferSize, receiveBufferSize, socketSendBufferSize, socketReceiveBufferSize) { + } + ~SecureSocketPort() override; public: