Skip to content

Commit

Permalink
[REFACTOR] The constructors are fixed, use them fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwielders committed Dec 8, 2024
1 parent 7b93e65 commit 6e92ffd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Source/core/SocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HANDLECLIENT> client = ProxyType<HANDLECLIENT>::Create(newClient, remoteId, &_parent);

Expand Down
62 changes: 47 additions & 15 deletions Source/cryptalgo/SecureSocketPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
SSL_free(_ssl);
_ssl = nullptr;
}
if (_context != nullptr) {
SSL_CTX_free(static_cast<SSL_CTX*>(_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);

Expand All @@ -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);
}
}
}
}
Expand All @@ -351,8 +374,15 @@ uint32_t SecureSocketPort::Handler::Initialize() {
ASSERT(_context != nullptr);
ASSERT(_ssl != nullptr);

if (SSL_set_fd(static_cast<SSL*>(_ssl), static_cast<Core::IResource&>(*this).Descriptor()) == 1) {
if (SSL_set_fd(_ssl, static_cast<Core::IResource&>(*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();
}

Expand All @@ -367,18 +397,20 @@ int32_t SecureSocketPort::Handler::Read(uint8_t buffer[], const uint16_t length)
const_cast<Handler&>(*this).Update();
}

return (SSL_read(static_cast<SSL*>(_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) {
Expand All @@ -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));
SSL_shutdown(_ssl);

return(Core::SocketPort::Close(waitTime));
}
Expand Down
87 changes: 42 additions & 45 deletions Source/cryptalgo/SecureSocketPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -128,57 +128,21 @@ 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,
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(true);
}
const uint32_t socketReceiveBufferSize);
~Handler();

public:
Expand Down Expand Up @@ -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;
Expand All @@ -233,10 +197,43 @@ namespace Crypto {
SecureSocketPort& operator=(SecureSocketPort&&) = delete;
SecureSocketPort& operator=(const SecureSocketPort&) = delete;

template <typename... Args>
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:
Expand Down

0 comments on commit 6e92ffd

Please sign in to comment.