From e30c56041d5b885ddac21b15579a7d100577ae01 Mon Sep 17 00:00:00 2001 From: malloc Date: Thu, 20 Sep 2018 19:01:17 -0500 Subject: [PATCH] securer boob --- src/server/sock/tcpsock.hpp | 4 +++ src/server/sock/tcpsock_bsd.cpp | 37 ++++++++++++++++++----- src/server/sock/tcpsock_win.cpp | 52 +++++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/server/sock/tcpsock.hpp b/src/server/sock/tcpsock.hpp index 2bf9c4d..191bad4 100644 --- a/src/server/sock/tcpsock.hpp +++ b/src/server/sock/tcpsock.hpp @@ -59,6 +59,10 @@ public: // TODO consider changing this return this->sock_open; } + + inline bool IsSecure() const { + return this->ssl != nullptr; + } inline net::IpAddress GetIpAddress() const { return this->ip; diff --git a/src/server/sock/tcpsock_bsd.cpp b/src/server/sock/tcpsock_bsd.cpp index 42a1ff8..61f8f70 100644 --- a/src/server/sock/tcpsock_bsd.cpp +++ b/src/server/sock/tcpsock_bsd.cpp @@ -55,7 +55,7 @@ bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) { else { _ssl_ctx.client_mtx.lock(); this->ssl = SSL_new(_ssl_ctx.client); - _ssl_ctx.client_mtx.lock(); + _ssl_ctx.client_mtx.unlock(); SSL_set_fd(this->ssl, this->sock); if(SSL_connect(this->ssl) != 1) { @@ -76,8 +76,20 @@ void sosc::TcpClient::Open this->sock = sock; this->sock_open = true; + if(!secure) this->ssl = nullptr; + else { + _ssl_ctx.client_mtx.lock(); + this->ssl = SSL_new(_ssl_ctx.server); + _ssl_ctx.client_mtx.unlock(); + + SSL_set_fd(this->ssl, this->sock); + if(SSL_accept(this->ssl) <= 0) { + this->Close(); + return; + } + } this->addr = addr; this->addr_len = addr_len; @@ -99,7 +111,10 @@ int sosc::TcpClient::Receive(std::string* str, int flags) { while(block ? (first_recv ? true : this->IsDataReady()) : this->IsDataReady()) { - int length = recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0); + int length = (this->ssl == nullptr) + ? (int)recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0) + : (int)SSL_read(this->ssl, this->buffer, SOSC_TCP_BUFLEN); + if(length <= 0) { this->Close(); return -1; @@ -123,12 +138,17 @@ bool sosc::TcpClient::Send(const std::string& str) { std::string::size_type total_sent = 0; while(total_sent < str.length()) { - int sent = total_sent == 0 - ? send(this->sock, str.c_str(), str.length(), 0) - : send(this->sock, str.substr(total_sent).c_str(), - str.length() - total_sent, 0); + int sent = (total_sent == 0) + ? (this->ssl == nullptr) + ? (int)send(this->sock, str.c_str(), str.length(), 0) + : (int)SSL_write(this->ssl, str.c_str(), str.length()) + : (this->ssl == nullptr) + ? (int)send(this->sock, str.c_str() + total_sent, + str.length() - total_sent, 0) + : (int)SSL_write(this->ssl, str.c_str() + total_sent, + str.length() - total_sent); - if(sent == -1) { + if(sent < 0) { this->Close(); return false; } else @@ -175,6 +195,9 @@ void sosc::TcpClient::Close() { this->sock_open = false; shutdown(this->sock, SHUT_RDWR); close(this->sock); + + if(this->ssl != nullptr) + SSL_free(this->ssl); } /****************************/ diff --git a/src/server/sock/tcpsock_win.cpp b/src/server/sock/tcpsock_win.cpp index 158c674..dcb6e26 100644 --- a/src/server/sock/tcpsock_win.cpp +++ b/src/server/sock/tcpsock_win.cpp @@ -64,17 +64,49 @@ bool sosc::TcpClient::Open(std::string host, std::uint16_t port, bool secure) { this->ip = net::IpAddress(); this->sock_open = true; + + if(!secure) + this->ssl = nullptr; + else { + _ssl_ctx.client_mtx.lock(); + this->ssl = SSL_new(_ssl_ctx.client); + _ssl_ctx.client_mtx.unlock(); + + SSL_set_fd(this->ssl, this->sock); + if(SSL_connect(this->ssl) != 1) { + SSL_free(this->ssl); + this->Close(); + return false; + } + } + return true; } void sosc::TcpClient::Open - (SOSC_SOCK_T sock, SOSC_ADDR_T addr, int addr_len) + (SOSC_SOCK_T sock, SOSC_ADDR_T addr, int addr_len, bool secure) { + if(secure && !ssl_init()) + return false; if(this->sock_open) return; this->sock = sock; this->sock_open = true; + + if(!secure) + this->ssl = nullptr; + else { + _ssl_ctx.client_mtx.lock(); + this->ssl = SSL_new(_ssl_ctx.server); + _ssl_ctx.client_mtx.unlock(); + + SSL_set_fd(this->ssl, this->sock); + if(SSL_accept(this->ssl) <= 0) { + this->Close(); + return; + } + } this->addr = addr; this->addr_len = addr_len; @@ -96,7 +128,10 @@ int sosc::TcpClient::Receive(std::string* str, int flags) { while(block ? (first_recv ? true : this->IsDataReady()) : this->IsDataReady()) { - int length = recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0); + int length = (this->ssl == nullptr) + ? (int)recv(this->sock, this->buffer, SOSC_TCP_BUFLEN, 0) + : (int)SSL_read(this->ssl, this->buffer, SOSC_TCP_BUFLEN); + if(length <= 0) { this->Close(); return -1; @@ -120,10 +155,15 @@ bool sosc::TcpClient::Send(const std::string& str) { std::string::size_type total_sent = 0; while(total_sent < str.length()) { - int sent = total_sent == 0 - ? send(this->sock, str.c_str(), str.length(), 0) - : send(this->sock, str.substr(total_sent).c_str(), - str.length() - total_sent, 0); + int sent = (total_sent == 0) + ? (this->ssl == nullptr) + ? (int)send(this->sock, str.c_str(), str.length(), 0) + : (int)SSL_write(this->ssl, str.c_str(), str.length()) + : (this->ssl == nullptr) + ? (int)send(this->sock, str.c_str() + total_sent, + str.length() - total_sent, 0) + : (int)SSL_write(this->ssl, str.c_str() + total_sent, + str.length() - total_sent); if(sent == SOCKET_ERROR) { this->Close();