5 #ifndef CSOCKET_OPENSSL_H
6 #define CSOCKET_OPENSSL_H
13 #include <openssl/bio.h>
14 #include <openssl/ssl.h>
15 #include <openssl/err.h>
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"
28 eOpenSSL(std::string
const& error) : error(error) {};
30 friend std::ostream& operator<< (std::ostream& os,
eOpenSSL const& e) {
31 os <<
"<eOpenSSL error: " << e.error <<
" >" << std::endl;
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;
73 static bool ssl_initialized;
85 openssl_password_callback(
char *buf,
int size,
int rwflag,
void *userdata);
87 static std::set<csocket_openssl*> openssl_sockets;
90 pthread_rwlock_t ssl_lock;
91 std::list<rofl::cmemory*> txqueue;
93 enum openssl_event_t {
94 EVENT_SEND_TXQUEUE = 0,
95 EVENT_RECV_RXQUEUE = 1,
111 std::string certfile;
113 std::string password;
114 std::string verify_mode;
115 std::string verify_depth;
118 enum openssl_flag_t {
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,
127 std::bitset<64> socket_flags;
213 recv(
void *buf,
size_t count);
216 return recv(buf, count);
244 is_established()
const {
return socket_flags.test(FLAG_SSL_ESTABLISHED); };
250 write_would_block()
const {
return socket.write_would_block(); };
256 get_default_params();
265 set_capath(std::string
const& capath) { this->capath = capath; };
271 get_capath()
const {
return capath; };
277 set_cafile(std::string
const& cafile) { this->cafile = cafile; };
283 get_cafile()
const {
return cafile; };
289 set_certfile(std::string
const& certfile) { this->certfile = certfile; };
295 get_certfile()
const {
return certfile; };
301 set_keyfile(std::string
const& keyfile) { this->keyfile = keyfile; };
307 get_keyfile()
const {
return keyfile; };
313 set_password(std::string
const& password) { this->password = password; };
319 get_password()
const {
return password; };
431 handle_event(
cevent const& ev);
443 openssl_destroy_ctx();
455 openssl_destroy_ssl();
493 os <<
rofl::indent(0) <<
"<csocket_openssl >" << std::endl;
495 if (sock.socket_flags.test(FLAG_SSL_IDLE)) {
498 if (sock.socket_flags.test(FLAG_SSL_CONNECTING)) {
501 if (sock.socket_flags.test(FLAG_SSL_ACCEPTING)) {
504 if (sock.socket_flags.test(FLAG_SSL_ESTABLISHED)) {
505 os <<
"ESTABLISHED ";
507 if (sock.socket_flags.test(FLAG_SSL_CLOSING)) {
510 os <<
">" << std::endl;
virtual void connect(cparams const ¶ms)
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 ¶ms)
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
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