44** Author Francois Michaut
55**
66** Started on Wed Sep 14 20:51:23 2022 Francois Michaut
7- ** Last update Fri Aug 22 21:55:50 2025 Francois Michaut
7+ ** Last update Sun Oct 25 12:00:59 2026 Francois Michaut
88**
99** SecureSocket.hpp : TLS socket wrapper using openssl
1010*/
@@ -20,30 +20,31 @@ namespace CppSockets {
2020 // TODO add more TLS-related functions
2121 class TlsSocket : public Socket {
2222 public:
23- TlsSocket () = default ;
24- // TODO: Constructor allowing application to reuse SSL_CTX objects
25- // (Maybe even a different TLS_CTX class to manage them ?)
23+ TlsSocket () = default ; // TODO: Null-init m_ssl, would segfault on any function call. Find a safer way.
2624 TlsSocket (int domain, int type, int protocol, TlsContext ctx = {});
2725 explicit TlsSocket (Socket &&other, TlsContext ctx = {});
2826 explicit TlsSocket (Socket &&other, SSL_ptr ssl = nullptr );
2927 explicit TlsSocket (RawSocketType fd, SSL_ptr ssl = nullptr );
30- ~TlsSocket () noexcept ;
28+ ~TlsSocket () noexcept override ;
3129
3230 TlsSocket (const TlsSocket &other) = delete ;
3331 TlsSocket (TlsSocket &&other) noexcept ;
3432 auto operator =(const TlsSocket &other) -> TlsSocket & = delete ;
3533 auto operator =(TlsSocket &&other) noexcept -> TlsSocket &;
3634
37- void close ();
35+ void close () override { this ->close (false ); };
36+ bool close (bool force);
3837
39- auto read (std::size_t len = -1 ) -> std::string;
40- auto read (char *buff, std::size_t size) -> std::size_t;
41- auto write (std::string_view buff) -> std::size_t { return this ->write (buff.data (), buff.size ()); };
42- auto write (const char *buff, std::size_t len) -> std::size_t;
38+ // read() with no length reads until the peer closes; use read(N) when reading from untrusted peers
39+ auto read (std::size_t len = -1 ) -> std::string override ;
40+ auto read (char *buff, std::size_t size) -> std::size_t override ;
41+ auto write (std::string_view buff) -> std::size_t override { return this ->write (buff.data (), buff.size ()); };
42+ auto write (const char *buff, std::size_t len) -> std::size_t override ;
4343
44- void set_verify (int mode, SSL_verify_cb verify_callback = nullptr );
44+ void set_verify (int mode, TlsVerifyCallback verify_callback = nullptr );
45+ void set_verify_depth (int depth);
4546 void set_certificate (const std::string &cert_path, const std::string &pkey_path);
46- auto connect (const IEndpoint &endpoint) -> int;
47+ auto connect (const IEndpoint &endpoint) -> int override ;
4748
4849 auto accept (void *addr_out, TlsContext ctx) -> std::unique_ptr<TlsSocket>;
4950 auto accept (void *addr_out = nullptr ) -> std::unique_ptr<TlsSocket> { return accept (addr_out, m_ctx); }
@@ -54,11 +55,18 @@ namespace CppSockets {
5455 [[nodiscard]] auto get_peer_cert () const -> const X509_ptr & { return m_peer_cert; }
5556
5657 [[nodiscard]] auto tls_strerror (int ret) -> std::string;
58+ protected:
59+ auto acceptImpl (void *addr_out) -> TlsSocket * override { return acceptImpl (addr_out, m_ctx); };
60+ auto acceptImpl (void *addr_out, TlsContext ctx) -> TlsSocket *;
61+
62+ void throw_tls_error (std::string message);
63+ void throw_tls_error (std::string message, int ret);
5764 private:
5865 TlsContext m_ctx;
5966 SSL_ptr m_ssl;
6067 X509_ptr m_peer_cert;
6168
62- void check_for_error (const std::string &error_msg, int ret);
69+ bool m_should_shutdown = false ;
70+ void check_for_error (std::string error_msg, int ret);
6371 };
6472}
0 commit comments