Skip to content

Commit 9ffb4b9

Browse files
authored
TLS Fixes (#6)
- Fixed TLS Socket destructor hanging if peer doesnt respond - Fixed Tls Socket move CTOR freeing the TlsContext unexpectedly
1 parent edd6d57 commit 9ffb4b9

File tree

9 files changed

+91
-29
lines changed

9 files changed

+91
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ compile_commands.json
55

66
*.tmp
77
*.gch
8+
*.pch
89
vgcore.*
910

1011
.vscode/

include/CppSockets/Tls/Context.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Aug 20 14:13:44 2025 Francois Michaut
7-
** Last update Thu Aug 21 14:14:45 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:43:02 2025 Francois Michaut
88
**
99
** Context.hpp : Context for TLS sockets
1010
*/
@@ -23,9 +23,9 @@ namespace CppSockets {
2323
TlsContext(SSL_CTX *ptr, bool own = true);
2424

2525
TlsContext(const TlsContext &other) { *this = other; }
26-
TlsContext(TlsContext &&other) noexcept = default;
26+
TlsContext(TlsContext &&other) noexcept;
2727
auto operator=(const TlsContext &other) -> TlsContext &;
28-
auto operator=(TlsContext &&other) noexcept -> TlsContext & = default;
28+
auto operator=(TlsContext &&other) noexcept -> TlsContext &;
2929

3030
~TlsContext();
3131

include/CppSockets/Tls/Socket.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Sep 14 20:51:23 2022 Francois Michaut
7-
** Last update Wed Aug 20 23:11:28 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:55:50 2025 Francois Michaut
88
**
99
** SecureSocket.hpp : TLS socket wrapper using openssl
1010
*/
@@ -34,6 +34,8 @@ namespace CppSockets {
3434
auto operator=(const TlsSocket &other) -> TlsSocket & = delete;
3535
auto operator=(TlsSocket &&other) noexcept -> TlsSocket &;
3636

37+
void close();
38+
3739
auto read(std::size_t len = -1) -> std::string;
3840
auto read(char *buff, std::size_t size) -> std::size_t;
3941
auto write(std::string_view buff) -> std::size_t { return this->write(buff.data(), buff.size()); };

private/CppSockets/SslMacros.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Aug 20 16:54:02 2025 Francois Michaut
7-
** Last update Wed Aug 20 18:59:18 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:46:55 2025 Francois Michaut
88
**
99
** SslMacros.hpp : Private Macros to define SSL wrappers
1010
*/
@@ -22,7 +22,7 @@
2222
type *dup = type##_dup(other.m_ptr.get()); \
2323
\
2424
if (dup == nullptr) { \
25-
throw std::runtime_error("Failed to dup ##type##"); \
25+
throw std::runtime_error("Failed to dup " #type); \
2626
} \
2727
if (!this->m_own) { \
2828
(void)this->m_ptr.release(); \

source/Tls/Context.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Aug 20 14:40:41 2025 Francois Michaut
7-
** Last update Wed Aug 20 18:58:53 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:46:12 2025 Francois Michaut
88
**
99
** Context.cpp : Implementation of the Context for TLS sockets
1010
*/
@@ -63,10 +63,22 @@ namespace CppSockets {
6363
TLS_CONTEXT_CONSTRUCTOR_BODY;
6464
}
6565

66+
TlsContext::TlsContext(TlsContext &&other) noexcept {
67+
*this = other;
68+
}
69+
6670
auto TlsContext::operator=(const TlsContext &other) -> TlsContext & {
6771
UP_REF_ASSIGNMENT_OPERATOR(SSL_CTX)
6872
}
6973

74+
auto TlsContext::operator=(TlsContext &&other) noexcept -> TlsContext & {
75+
std::swap(m_ptr, other.m_ptr);
76+
std::swap(m_own, other.m_own);
77+
78+
m_verify_callback = std::move(other.m_verify_callback);
79+
return *this;
80+
}
81+
7082
MAKE_DESTRUCTOR(TlsContext)
7183

7284
void TlsContext::set_min_proto_version(int version) {

source/Tls/Socket.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Sep 14 21:04:42 2022 Francois Michaut
7-
** Last update Wed Aug 20 23:12:24 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:57:23 2025 Francois Michaut
88
**
99
** SecureSocket.cpp : TLS socket wrapper implementation
1010
*/
@@ -77,36 +77,45 @@ namespace CppSockets {
7777

7878
TlsSocket::~TlsSocket() noexcept {
7979
if (m_ssl && this->connected()) {
80-
int ret = SSL_shutdown(m_ssl.get()); // TODO: log failure
81-
82-
if (ret == 0) {
83-
try {
84-
while (this->connected()) {
85-
this->read();
86-
}
87-
} catch (std::runtime_error &e) {
88-
// TODO: What ?
89-
}
90-
SSL_shutdown(m_ssl.get()); // TODO: log failure
91-
}
80+
// TODO: Better shutdown mecanics
81+
int ret = SSL_shutdown(m_ssl.get());
82+
83+
// if (ret == 1) {
84+
// // Peer also closed -> We can leave.
85+
// } else if (ret == 0) {
86+
// // Peer didn't send, but we can't wait in the Destructor
87+
// } else {
88+
// // TODO: log failure
89+
// }
9290
}
9391
}
9492

95-
TlsSocket::TlsSocket(TlsSocket &&other) noexcept :
96-
Socket(std::move(other)), m_ctx(std::move(other.m_ctx)),
97-
m_ssl(std::move(other.m_ssl)), m_peer_cert(std::move(other.m_peer_cert))
98-
{}
93+
TlsSocket::TlsSocket(TlsSocket &&other) noexcept {
94+
*this = std::move(other);
95+
}
9996

10097
auto TlsSocket::operator=(TlsSocket &&other) noexcept -> TlsSocket & {
101-
std::swap(m_ssl, other.m_ssl);
102-
98+
m_ssl = std::move(other.m_ssl);
10399
m_ctx = std::move(other.m_ctx);
104100
m_peer_cert = std::move(other.m_peer_cert);
105101

106102
Socket::operator=(std::move(other));
107103
return *this;
108104
}
109105

106+
void TlsSocket::close() {
107+
int ret = SSL_shutdown(m_ssl.get());
108+
109+
if (ret == 1) {
110+
return Socket::close();
111+
}
112+
// if (ret == 0) {
113+
// // TODO: wait for peer
114+
// } else {
115+
// // TODO: Log failure
116+
// }
117+
}
118+
110119
void TlsSocket::set_verify(int mode, SSL_verify_cb verify_callback) {
111120
// TODO: While setting it on the CTX makes sense imo (since accepted sockets will inherit this), an application
112121
// might not want that behavior. Need to provide alertnate ways to set verify on CTX vs SSL

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
## Author Francois Michaut
55
##
66
## Started on Mon Feb 14 19:35:41 2022 Francois Michaut
7-
## Last update Sat Aug 2 18:06:42 2025 Francois Michaut
7+
## Last update Fri Aug 22 21:12:37 2025 Francois Michaut
88
##
99
## CMakeLists.txt : CMake building and running tests for CppSockets
1010
##
1111

1212
include(CTest)
1313

1414
create_test_sourcelist(TestFiles test_driver.cpp
15+
Tls/TestContext.cpp
16+
1517
TestSockets.cpp
1618
)
1719

tests/TestSockets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Mon Feb 14 21:17:55 2022 Francois Michaut
7-
** Last update Tue Aug 5 11:11:27 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:11:25 2025 Francois Michaut
88
**
99
** TestSockets.cpp : Socket tests
1010
*/
@@ -23,7 +23,7 @@
2323

2424
using namespace CppSockets;
2525

26-
int TestSockets(int /* ac */, char ** const /* av */)
26+
auto TestSockets(int /* ac */, char ** const /* av */) -> int
2727
{
2828
#ifdef OS_WINDOWS
2929
// TODO

tests/Tls/TestContext.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
** Project FileShare-Tests, 2025
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Fri Aug 22 21:09:12 2025 Francois Michaut
7+
** Last update Fri Aug 22 21:36:06 2025 Francois Michaut
8+
**
9+
** TestContext.cpp : TlsContext tests
10+
*/
11+
12+
#include "CppSockets/Tls/Context.hpp"
13+
#include "CppSockets/Tls/Socket.hpp"
14+
15+
void TestConfigCopyCtor() {
16+
CppSockets::TlsContext ctx;
17+
CppSockets::TlsSocket soc;
18+
19+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
20+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
21+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
22+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
23+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
24+
25+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
26+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
27+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
28+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
29+
soc = CppSockets::TlsSocket(AF_INET, SOCK_STREAM, 0, ctx);
30+
}
31+
32+
auto Tls_TestContext(int /* ac */, char ** const /* av */) -> int
33+
{
34+
TestConfigCopyCtor();
35+
return 0;
36+
}

0 commit comments

Comments
 (0)