OpenSDN source code
ssl_session.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef SRC_IO_SSL_SESSION_H_
7 #define SRC_IO_SSL_SESSION_H_
8 
9 #include <mutex>
10 
11 #include "io/tcp_session.h"
12 #include "io/ssl_server.h"
13 
14 class SslSession;
15 typedef boost::intrusive_ptr<SslSession> SslSessionPtr;
16 typedef boost::function<void(SslSessionPtr,
17  const boost::system::error_code& error)> SslHandShakeCallbackHandler;
18 
19 class SslSession : public TcpSession {
20 public:
21  typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SslSocket;
22 
23  // SslSession constructor takes ownership of socket.
25  bool async_read_ready = true);
26 
27  virtual Socket *socket() const;
28 
29  // Trigger delayed SslHandShake
31 
32  // Additional states to determine the trigger of SSL handshake
33  bool IsSslDisabled() {
34  return (!ssl_enabled_);
35  }
36 
39  }
40 
42  std::scoped_lock lock(mutex_);
44  }
45 
48  }
49 
51  std::scoped_lock lock(mutex_);
53  }
54 
55  void SetSslHandShakeInProgress(bool state) {
56  std::scoped_lock lock(mutex_);
58  }
59 
60  static bool IsSocketErrorHard(const boost::system::error_code &ec);
61 protected:
62  virtual ~SslSession();
63 
64 private:
65  class SslReader;
66  friend class SslServer;
67 
68  // SslSession do actual ssl socket read for data in this context with
69  // session mutex held, to avoid concurrent read and write operations
70  // on same socket.
71  size_t ReadSome(boost::asio::mutable_buffer buffer,
72  boost::system::error_code *error);
73  void AsyncWrite(const uint8_t *data, std::size_t size);
74 
77 
78  virtual Task* CreateReaderTask(boost::asio::mutable_buffer, size_t);
79 
81  SslSessionPtr, const boost::system::error_code &error);
82 
84  std::scoped_lock lock(mutex_);
86  }
87 
89  std::scoped_lock lock(mutex_);
90  ssl_handshake_success_ = false;
91  }
92  virtual size_t GetReadBufferSize() const;
93  virtual void AsyncReadSome();
94 
95  boost::scoped_ptr<SslSocket> ssl_socket_;
96 
97  /**************** protected by mutex_ *************************/
98  bool ssl_handshake_in_progress_; // ssl handshake ongoing
99  bool ssl_handshake_success_; // ssl handshake success
100  /**************** end protected by mutex_ *********************/
101 
102  /**************** config knobs ********************************/
103  bool ssl_enabled_; // default true
104  bool ssl_handshake_delayed_; // default false
105  /**************************************************************/
106 
107  size_t ssl_last_read_len_; // data len of the last read done
108 
110 };
111 
112 #endif // SRC_IO_SSL_SESSION_H_
void SetSslHandShakeSuccess()
Definition: ssl_session.h:83
static bool IsSocketErrorHard(const boost::system::error_code &ec)
Definition: ssl_session.cc:134
size_t ReadSome(boost::asio::mutable_buffer buffer, boost::system::error_code *error)
Definition: ssl_session.cc:151
bool IsSslHandShakeDelayed()
Definition: ssl_session.h:37
bool IsSslDisabled()
Definition: ssl_session.h:33
virtual Socket * socket() const
Definition: ssl_session.cc:99
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
Definition: ssl_session.h:21
void SetSslHandShakeFailure()
Definition: ssl_session.h:88
virtual void AsyncReadSome()
Definition: ssl_session.cc:109
bool IsSslHandShakeInProgress()
Definition: ssl_session.h:50
void AsyncWrite(const uint8_t *data, std::size_t size)
Definition: ssl_session.cc:160
virtual ~SslSession()
Definition: ssl_session.cc:87
SslSession(SslServer *server, SslSocket *socket, bool async_read_ready=true)
Definition: ssl_session.cc:71
DISALLOW_COPY_AND_ASSIGN(SslSession)
bool ssl_handshake_success_
Definition: ssl_session.h:99
bool ssl_enabled_
Definition: ssl_session.h:103
static void SslHandShakeCallback(SslHandShakeCallbackHandler cb, SslSessionPtr, const boost::system::error_code &error)
Definition: ssl_session.cc:170
size_t ssl_last_read_len_
Definition: ssl_session.h:107
virtual Task * CreateReaderTask(boost::asio::mutable_buffer, size_t)
Definition: ssl_session.cc:90
void SetSslHandShakeInProgress(bool state)
Definition: ssl_session.h:55
boost::scoped_ptr< SslSocket > ssl_socket_
Definition: ssl_session.h:95
bool ssl_handshake_delayed_
Definition: ssl_session.h:104
bool IsSslHandShakeSuccess()
Definition: ssl_session.h:41
virtual size_t GetReadBufferSize() const
Definition: ssl_session.cc:125
void TriggerSslHandShake(SslHandShakeCallbackHandler)
Definition: ssl_session.cc:201
bool IsSslHandShakeSuccessLocked()
Definition: ssl_session.h:46
static void TriggerSslHandShakeInternal(SslSessionPtr ptr, SslHandShakeCallbackHandler cb)
Definition: ssl_session.cc:185
bool ssl_handshake_in_progress_
Definition: ssl_session.h:98
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
TcpServer * server()
Definition: tcp_session.h:85
std::mutex mutex_
Definition: tcp_session.h:222
boost::asio::ip::tcp::socket Socket
Definition: tcp_session.h:57
boost::intrusive_ptr< SslSession > SslSessionPtr
Definition: ssl_session.h:14
boost::function< void(SslSessionPtr, const boost::system::error_code &error)> SslHandShakeCallbackHandler
Definition: ssl_session.h:17