44** Author Francois Michaut
55**
66** Started on Wed Sep 14 20:51:23 2022 Francois Michaut
7- ** Last update Tue May 9 23:29:53 2023 Francois Michaut
7+ ** Last update Sat Jul 22 22:45:28 2023 Francois Michaut
88**
99** SecureSocket.hpp : TLS socket wrapper using openssl
1010*/
2323#include < functional>
2424
2525// TODO: find a better way do to this
26+ using BIO = struct bio_st ;
2627using SSL = struct ssl_st ;
2728using SSL_METHOD = struct ssl_method_st ;
2829using SSL_CTX = struct ssl_ctx_st ;
@@ -33,6 +34,7 @@ using EVP_MD = struct evp_md_st;
3334using EVP_MD_CTX = struct evp_md_ctx_st ;
3435
3536namespace CppSockets {
37+ using BIO_ptr=std::unique_ptr<BIO, std::function<void (BIO *)>>;
3638 using SSL_CTX_ptr=std::unique_ptr<SSL_CTX, std::function<void (SSL_CTX *)>>;
3739 using SSL_ptr=std::unique_ptr<SSL, std::function<void (SSL *)>>;
3840 using X509_ptr=std::unique_ptr<X509, std::function<void (X509 *)>>;
@@ -44,8 +46,10 @@ namespace CppSockets {
4446 // TODO add more TLS-related functions
4547 class TlsSocket : public Socket {
4648 public:
47- explicit TlsSocket (Socket &&other) ;
49+ TlsSocket () = default ;
4850 TlsSocket (int domain, int type, int protocol);
51+ TlsSocket (Socket &&other, SSL_ptr ssl = nullptr );
52+ TlsSocket (RawSocketType fd, SSL_ptr ssl = nullptr );
4953 ~TlsSocket ();
5054
5155 TlsSocket (const TlsSocket &other) = delete ;
@@ -58,9 +62,10 @@ namespace CppSockets {
5862 std::size_t write (const std::string &buff);
5963 std::size_t write (const char *buff, std::size_t len);
6064
65+ void set_certificate (std::string cert_path, std::string pkey_path);
6166 int connect (const IEndpoint &endpoint);
6267
63- std::shared_ptr<TlsSocket> accept (void *addr_out);
68+ std::shared_ptr<TlsSocket> accept (void *addr_out = nullptr );
6469
6570 [[nodiscard]]
6671 const SSL_CTX_ptr &get_ssl_ctx () const ;
@@ -72,26 +77,30 @@ namespace CppSockets {
7277 [[nodiscard]]
7378 const std::string tls_strerror (int ret);
7479 private:
75- SSL_CTX_ptr ctx;
76- SSL_ptr ssl;
77- X509_ptr peer_cert;
78- bool do_shutdown = true ;
80+ SSL_CTX_ptr m_ctx;
81+ SSL_ptr m_ssl;
82+ X509_ptr m_peer_cert;
83+ X509_ptr m_cert;
84+ EVP_PKEY_ptr m_pkey;
85+ bool m_do_shutdown = true ;
86+
87+ void check_for_error (std::string error_msg, int ret);
7988 };
8089
8190 inline std::size_t TlsSocket::write (const std::string &buff) {
82- return write (buff.data (), buff.size ());
91+ return write (buff.c_str (), buff.size ());
8392 }
8493
8594 inline const SSL_CTX_ptr &TlsSocket::get_ssl_ctx () const {
86- return ctx ;
95+ return m_ctx ;
8796 }
8897
8998 inline const SSL_ptr &TlsSocket::get_ssl () const {
90- return ssl ;
99+ return m_ssl ;
91100 }
92101
93102 inline const X509_ptr &TlsSocket::get_client_cert () const {
94- return peer_cert ;
103+ return m_peer_cert ;
95104 }
96105}
97106#endif
0 commit comments