Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
csocket_openssl.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CSOCKET_OPENSSL_H
6 #define CSOCKET_OPENSSL_H
7 
8 #include <set>
9 #include <list>
10 #include <bitset>
11 #include <stdio.h>
12 
13 #include <openssl/bio.h>
14 #include <openssl/ssl.h>
15 #include <openssl/err.h>
16 
17 #include "rofl/common/ciosrv.h"
18 #include "rofl/common/csocket.h"
19 #include "rofl/common/csocket_plain.h"
20 #include "rofl/common/logging.h"
21 #include "rofl/common/croflexception.h"
22 
23 namespace rofl {
24 
25 class eOpenSSL : public RoflException {
26  std::string error;
27 public:
28  eOpenSSL(std::string const& error) : error(error) {};
29  virtual ~eOpenSSL() throw() {};
30  friend std::ostream& operator<< (std::ostream& os, eOpenSSL const& e) {
31  os << "<eOpenSSL error: " << e.error << " >" << std::endl;
32  return os;
33  };
34 };
35 
36 class eOpenSSLVerify : public eOpenSSL {};
37 
38 
59  public csocket,
60  public csocket_env
61 {
62  //Defaults
63  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_CA_PATH;
64  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_CA_FILE;
65  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_CERT;
66  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_PRIVATE_KEY;
67  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_PRIVATE_KEY_PASSWORD;
68  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_VERIFY_MODE;
69  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_VERIFY_DEPTH;
70  static std::string const PARAM_DEFAULT_VALUE_SSL_KEY_CIPHERS;
71 
72 
73  static bool ssl_initialized;
74 
78  static void
79  openssl_init();
80 
84  static int
85  openssl_password_callback(char *buf, int size, int rwflag, void *userdata);
86 
87  static std::set<csocket_openssl*> openssl_sockets;
88 
89  csocket_plain socket;
90  pthread_rwlock_t ssl_lock;
91  std::list<rofl::cmemory*> txqueue;
92 
93  enum openssl_event_t {
94  EVENT_SEND_TXQUEUE = 0,
95  EVENT_RECV_RXQUEUE = 1,
96  EVENT_CONN_RESET = 2,
97  };
98 
99  /*
100  * OpenSSL related structures
101  */
102  SSL_CTX *ctx;
103  SSL *ssl;
104  BIO *bio;
105 
106  /*
107  * socket parameters
108  */
109  std::string capath;
110  std::string cafile;
111  std::string certfile;
112  std::string keyfile;
113  std::string password;
114  std::string verify_mode;
115  std::string verify_depth;
116  std::string ciphers;
117 
118  enum openssl_flag_t {
119  FLAG_SSL_IDLE = 0,
120  FLAG_SSL_CONNECTING = 1,
121  FLAG_SSL_ACCEPTING = 2,
122  FLAG_SSL_ESTABLISHED = 3,
123  FLAG_SSL_CLOSING = 4,
124  FLAG_ACTIVE_SOCKET = 5,
125  };
126 
127  std::bitset<64> socket_flags;
128 
129 public:
130 
131 
132 
139 
140 
141 
142 
147  virtual
149 
150 
159  virtual void
160  listen(
161  cparams const& params);
162 
163 
167  virtual void
168  accept(
169  cparams const& socket_params, int sd);
170 
171 
172 
180  virtual void
181  connect(
182  cparams const& params);
183 
184 
193  virtual void
194  reconnect();
195 
196 
204  virtual void
205  close();
206 
207 
212  virtual ssize_t
213  recv(void *buf, size_t count);
214  virtual ssize_t
215  recv(void *buf, size_t count, int flags, rofl::csockaddr& from) {
216  return recv(buf, count); // TODO: OpenSSL with DTLS on datagram sockets
217  };
218 
219 
236  virtual void
237  send(cmemory *mem, csockaddr const& dest = csockaddr());
238 
239 
243  virtual bool
244  is_established() const { return socket_flags.test(FLAG_SSL_ESTABLISHED); };
245 
249  virtual bool
250  write_would_block() const { return socket.write_would_block(); };
251 
255  static cparams
256  get_default_params();
257 
258 
259 public:
260 
264  void
265  set_capath(std::string const& capath) { this->capath = capath; };
266 
270  std::string const&
271  get_capath() const { return capath; };
272 
276  void
277  set_cafile(std::string const& cafile) { this->cafile = cafile; };
278 
282  std::string const&
283  get_cafile() const { return cafile; };
284 
288  void
289  set_certfile(std::string const& certfile) { this->certfile = certfile; };
290 
294  std::string const&
295  get_certfile() const { return certfile; };
296 
300  void
301  set_keyfile(std::string const& keyfile) { this->keyfile = keyfile; };
302 
306  std::string const&
307  get_keyfile() const { return keyfile; };
308 
312  void
313  set_password(std::string const& password) { this->password = password; };
314 
318  std::string const&
319  get_password() const { return password; };
320 
321 
322 
323 
324 
325 protected:
326 
327  //
328  // socket specific methods, must be overloaded in derived class
329  //
330 
338  virtual void
340 
348  virtual void
350 
358  virtual void
360 
368  virtual void
370 
378  virtual void
380 
390  virtual void
392  rofl::csocket& socket,
393  int newsd);
394 
401  virtual void
402  handle_closed(rofl::csocket& socket);
403 
412  virtual void
413  handle_read(rofl::csocket& socket);
414 
423  virtual void
424  handle_write(rofl::csocket& socket);
425 
426 
427 
428 private:
429 
430  virtual void
431  handle_event(cevent const& ev);
432 
436  void
437  openssl_init_ctx();
438 
442  void
443  openssl_destroy_ctx();
444 
448  void
449  openssl_init_ssl();
450 
454  void
455  openssl_destroy_ssl();
456 
460  void
461  openssl_connect();
462 
466  void
467  openssl_accept();
468 
472  bool
473  openssl_verify_ok();
474 
475 
476 protected:
477 
478 
485  virtual void
486  dequeue_packet();
487 
488 
489 public:
490 
491  friend std::ostream&
492  operator<< (std::ostream& os, csocket_openssl const& sock) {
493  os << rofl::indent(0) << "<csocket_openssl >" << std::endl;
494  os << rofl::indent(2) << "<flags: ";
495  if (sock.socket_flags.test(FLAG_SSL_IDLE)) {
496  os << "IDLE ";
497  }
498  if (sock.socket_flags.test(FLAG_SSL_CONNECTING)) {
499  os << "CONNECTING ";
500  }
501  if (sock.socket_flags.test(FLAG_SSL_ACCEPTING)) {
502  os << "ACCEPTING ";
503  }
504  if (sock.socket_flags.test(FLAG_SSL_ESTABLISHED)) {
505  os << "ESTABLISHED ";
506  }
507  if (sock.socket_flags.test(FLAG_SSL_CLOSING)) {
508  os << "CLOSING ";
509  }
510  os << ">" << std::endl;
511  rofl::indent i(2);
512  os << sock.socket;
513  return os;
514  };
515 
516 };
517 
518 }; // end of namespace
519 
520 #endif
Definition: cparams.h:20
virtual void connect(cparams const &params)
Open socket and connect to peer entity (client side).
Definition: csocket_openssl.cc:279
A single socket.
Definition: csocket.h:182
Definition: csockaddr.h:38
A single unencrypted socket.This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_plain_owner.
Definition: csocket_plain.h:51
virtual void handle_accepted(rofl::csocket &socket)
Definition: csocket_openssl.cc:255
virtual void reconnect()
Reconnect this socket.
Definition: csocket_openssl.cc:517
virtual void close()
Closes this socket.
Definition: csocket_openssl.cc:531
virtual void handle_write(rofl::csocket &socket)
Definition: csocket_openssl.cc:420
virtual void handle_connected(rofl::csocket &socket)
Definition: csocket_openssl.cc:304
virtual void handle_connect_refused(rofl::csocket &socket)
Definition: csocket_openssl.cc:318
Definition: csocket_openssl.h:36
cparams socket_params
Definition: csocket.h:488
virtual void listen(cparams const &params)
Open socket in listening mode (server side).
Definition: csocket_openssl.cc:215
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual void handle_accept_refused(rofl::csocket &socket)
Definition: csocket_openssl.cc:269
Single event used internally by class crofl::cioloop.
Definition: cevent.h:20
virtual void send(cmemory *mem, csockaddr const &dest=csockaddr())
Store a packet for transmission.
Definition: csocket_openssl.cc:449
virtual void handle_closed(rofl::csocket &socket)
Definition: csocket_openssl.cc:346
An abstract interface defining the consumer side of a csocket.
Definition: csocket.h:58
int sd
Definition: csocket.h:481
virtual void handle_read(rofl::csocket &socket)
Definition: csocket_openssl.cc:355
Definition: logging.h:76
csocket_openssl(csocket_env *owner)
Constructor for new empty csocket_impl instances.
Definition: csocket_openssl.cc:57
A single TLS encrypted socket.This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_impl_owner.
Definition: csocket_openssl.h:58
virtual ~csocket_openssl()
Destructor.
Definition: csocket_openssl.cc:78
virtual void handle_listen(rofl::csocket &socket, int newsd)
Definition: csocket_openssl.cc:338
virtual void accept(cparams const &socket_params, int sd)
Handle accepted socket descriptor obtained from external listening socket.
Definition: csocket_openssl.cc:235
virtual ssize_t recv(void *buf, size_t count)
Reads bytes from socket.
Definition: csocket_openssl.cc:385
virtual void dequeue_packet()
Definition: csocket_openssl.cc:483
Definition: csocket_openssl.h:25
Definition: croflexception.h:27
virtual void handle_connect_failed(rofl::csocket &socket)
Definition: csocket_openssl.cc:328