libutil++  1.9.3
 All Classes Functions Variables
SecureTCPSocket.h
1 /*
2 ** libutil++
3 ** $Id: SecureTCPSocket.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__net__SecureTCPSocket_H__
10 #define __libutilxx__sella__net__SecureTCPSocket_H__
11 
12 #include "../../common.h"
13 #include "TCPSocket.h"
14 
15 #include <openssl/crypto.h>
16 #include <openssl/ssl.h>
17 #include <openssl/err.h>
18 
19 #include <set>
20 #include <mutex>
21 #include <vector>
22 #include <memory>
23 #include <string>
24 
25 namespace sella {
26  namespace net {
27  class SecureTCPSocket;
28  }
29 }
30 
32  public:
33  typedef std::shared_ptr<SecureTCPSocket> shared;
34  typedef SecureTCPSocket *ptr;
35 
36  enum Protocol : int8_t { SSLv3, TLSv1_0, TLSv1_1, TLSv1_2 };
37 
38  static const std::set<Protocol> DefaultProtocols; /* { SSLv3, TLSv1_0, TLSv1_1, TLSv1_2 } */
39  static const std::string DefaultCiphers; /* ALL:!aNULL:!eNULL */
40  static const std::string PerformanceCiphers; /* AES128-SHA:RC4-MD5:RC4-SHA:AES256-SHA:HIGH:!DSS:!aNULL:!eNULL */
41 
42  /* Cipher Performance: http://zombe.es/post/4078724716/openssl-cipher-selection */
43 
44  public:
45  SecureTCPSocket(int family) throw (SocketException);
46  SecureTCPSocket(int family, const IPAddressPort &addressport) throw (SocketException);
47  SecureTCPSocket(int family, const IPAddress &address, const Port &port) throw (SocketException);
48  SecureTCPSocket(int family, const std::string &address, unsigned short port) throw (SocketException);
49  SecureTCPSocket(const IPAddressPort &addressport) throw (SocketException);
50  SecureTCPSocket(const IPAddress &address, const Port &port) throw (SocketException);
51  SecureTCPSocket(const SecureTCPSocket &sock) = delete;
52  virtual ~SecureTCPSocket();
53  void operator=(const SecureTCPSocket &sock) = delete;
54 
55  /* Server-side Options */
56  void setCertificateFile(const std::string &certificateFile) throw ();
57  void setPrivateKeyFile(const std::string &privateKeyFile) throw ();
58  void setPasswordFile(const std::string &passwordFile) throw (); /* Preferred over configured password */
59  void setPassword(const std::string &password) throw ();
60  void setCertificateChainFile(const std::string &certificateChainFile) throw ();
61  void setCACertificateFile(const std::string &caCertificateFile) throw (); /* Set to perform certificate verification */
62 
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 (); /* To see chosen ciphers run: openssl ciphers -v 'AES128-SHA:RC4-MD5' */
66  void setPreferServerCiphers(bool on = true) throw (); /* Prefer the server's ciphers and ordering. */
67 
68  virtual bool isSecure(void) const throw ();
69 
70  virtual TCPSocket::shared accept(void) throw (SocketException);
71 
72  const char* getSSLLibraryVersion(void) const throw ();
73  const char* getActiveProtocol(void) const throw (SocketException);
74  const char* getActiveCipher(void) const throw (SocketException);
75 
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;
80 
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);
87 
88  protected:
89  SSL_CTX *ctx;
90  SSL *ssl;
91  bool server;
92 
93  std::string certificateFile;
94  std::string privateKeyFile;
95  std::string passwordFile;
96  std::string password;
97  std::string certificateChainFile;
98  std::string caCertificateFile;
99 
100  std::set<Protocol> protocols;
101  std::string ciphers;
102  bool preferServerCiphers;
103 
104  std::mutex mutex;
105 
106  SecureTCPSocket(int s, bool unused __attribute__((unused))) throw (SocketException);
107 
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);
114 
115  static int SSL_password_callback(char *buf, int num, int rwflag, void *userdata);
116 };
117 
118 #endif
119 
120 /*
121 ** vim: noet ts=3 sw=3
122 */