9 #ifndef __libutilxx__sella__net__SecureTCPSocket_H__
10 #define __libutilxx__sella__net__SecureTCPSocket_H__
12 #include "../../common.h"
13 #include "TCPSocket.h"
15 #include <openssl/crypto.h>
16 #include <openssl/ssl.h>
17 #include <openssl/err.h>
27 class SecureTCPSocket;
33 typedef std::shared_ptr<SecureTCPSocket> shared;
36 enum Protocol : int8_t { SSLv3, TLSv1_0, TLSv1_1, TLSv1_2 };
38 static const std::set<Protocol> DefaultProtocols;
39 static const std::string DefaultCiphers;
40 static const std::string PerformanceCiphers;
48 SecureTCPSocket(
int family,
const std::string &address,
unsigned short port)
throw (SocketException);
56 void setCertificateFile(
const std::string &certificateFile)
throw ();
57 void setPrivateKeyFile(
const std::string &privateKeyFile)
throw ();
58 void setPasswordFile(
const std::string &passwordFile)
throw ();
59 void setPassword(
const std::string &password)
throw ();
60 void setCertificateChainFile(
const std::string &certificateChainFile)
throw ();
61 void setCACertificateFile(
const std::string &caCertificateFile)
throw ();
63 void setProtocols(
const std::set<Protocol> &protocols = DefaultProtocols)
throw ();
64 void setProtocol(Protocol protocol,
bool on =
true)
throw ();
65 void setCiphers(
const std::string &ciphers = PerformanceCiphers)
throw ();
66 void setPreferServerCiphers(
bool on =
true)
throw ();
68 virtual bool isSecure(
void)
const throw ();
70 virtual TCPSocket::shared accept(
void)
throw (SocketException);
72 const char* getSSLLibraryVersion(
void)
const throw ();
73 const char* getActiveProtocol(
void)
const throw (SocketException);
74 const char* getActiveCipher(
void)
const throw (SocketException);
76 virtual ssize_t send(
const void *data,
size_t len,
int flags)
throw (SocketException);
77 using TCPSocket::send;
78 virtual ssize_t recv(
void *data,
size_t len,
int flags)
throw (SocketException);
79 using TCPSocket::recv;
81 static const SecureTCPSocket::shared shared_ptr(
int family)
throw (SocketException);
82 static const SecureTCPSocket::shared shared_ptr(
int family,
const IPAddressPort &addressport)
throw (SocketException);
83 static const SecureTCPSocket::shared shared_ptr(
int family,
const IPAddress &address,
const Port &port)
throw (SocketException);
84 static const SecureTCPSocket::shared shared_ptr(
int family,
const std::string &address,
unsigned short port)
throw (SocketException);
85 static const SecureTCPSocket::shared shared_ptr(
const IPAddressPort &addressport)
throw (SocketException);
86 static const SecureTCPSocket::shared shared_ptr(
const IPAddress &address,
const Port &port)
throw (SocketException);
93 std::string certificateFile;
94 std::string privateKeyFile;
95 std::string passwordFile;
97 std::string certificateChainFile;
98 std::string caCertificateFile;
100 std::set<Protocol> protocols;
102 bool preferServerCiphers;
106 SecureTCPSocket(
int s,
bool unused __attribute__((unused)))
throw (SocketException);
108 void init(
void)
throw (SocketException);
109 void server_init(
void)
throw (SocketException);
110 void client_init(
void)
throw (SocketException);
111 void shutdown(
void)
throw ();
112 virtual void close(
void)
throw (SocketException);
113 std::vector<std::string> getErrors(
void)
throw (SocketException);
115 static int SSL_password_callback(
char *buf,
int num,
int rwflag,
void *userdata);