OpenSDN source code
ssl_session.cc
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 #include "io/ssl_session.h"
7 
8 #include <string>
9 
10 #include <boost/asio.hpp>
11 #include <boost/bind/bind.hpp>
12 
13 #include "io/event_manager.h"
14 #include "io/io_log.h"
15 #include "io/io_utils.h"
16 
17 using boost::asio::async_write;
18 using boost::asio::buffer;
19 using boost::asio::buffer_cast;
20 using boost::asio::mutable_buffer;
21 using boost::asio::mutable_buffers_1;
22 using boost::asio::null_buffers;
23 using boost::asio::placeholders::error;
24 using boost::asio::placeholders::bytes_transferred;
25 using boost::asio::ssl::stream_base;
26 using boost::bind;
27 using boost::function;
28 using boost::system::error_code;
29 using std::size_t;
30 using std::srand;
31 using std::string;
32 using std::time;
33 using namespace boost::placeholders;
34 
35 class SslSession::SslReader : public Task {
36 public:
37  typedef function<void(Buffer)> ReadHandler;
38 
39  SslReader(int task_id, SslSessionPtr session, ReadHandler read_fn,
40  Buffer buffer)
41  : Task(task_id, session->GetSessionInstance()),
42  session_(session), read_fn_(read_fn), buffer_(buffer) {
43  }
44  virtual bool Run() {
45  if (session_->IsEstablished()) {
46  session_->ssl_last_read_len_ = BufferSize(buffer_);
47  read_fn_(buffer_);
48  if (session_->IsReaderDeferred()) {
49  // Update socket read block count.
50  session_->stats_.read_block_start_time = UTCTimestampUsec();
51  session_->stats_.read_blocked++;
52  session_->server()->stats_.read_blocked++;
53  } else {
54  if (session_->IsSslDisabled()) {
55  session_->AsyncReadStart();
56  } else if (!session_->IsSslHandShakeInProgress()) {
57  session_->AsyncReadStart();
58  }
59  }
60  }
61  return true;
62  }
63  string Description() const { return "SslSession::SslReader"; }
64 
65 private:
69 };
70 
72  bool async_read_ready)
73  : TcpSession(server, NULL, async_read_ready),
74  ssl_socket_(ssl_socket),
75  ssl_handshake_in_progress_(false),
76  ssl_handshake_success_(false),
77  ssl_enabled_(true),
78  ssl_handshake_delayed_(false),
79  ssl_last_read_len_(0) {
80 
81  if (server) {
82  ssl_enabled_ = server->ssl_enabled_;
83  ssl_handshake_delayed_ = server->ssl_handshake_delayed_;
84  }
85 }
86 
88 }
89 
90 Task* SslSession::CreateReaderTask(mutable_buffer buffer,
91  size_t bytes_transferred) {
92  Buffer rdbuf(buffer_cast<const uint8_t *>(buffer), bytes_transferred);
93  SslReader *task = new SslReader(this->reader_task_id(),
94  SslSessionPtr(this), bind(&SslSession::OnRead, this, _1), rdbuf);
95  return (task);
96 }
97 
98 
100  if (ssl_socket_) {
101  // return tcp socket
102  return &ssl_socket_->next_layer();
103  }
104  return NULL;
105 }
106 
107 // Register for data read notification from the tcp socket or from the ssl
108 // socket, as appropriate.
110  if (established()) {
111  if (ssl_last_read_len_ == 0) {
112  // we have drained the read buffer of the socket
113  // register for a read notification from the tcp socket
115  } else {
116  // trigger Async Read Handler for immediate read
118  }
119  }
120 }
121 
122 // Tests with large data have consistently shown 16K as the maximum read data
123 // size even though when a lot more data was available in the underlying socket.
124 // Also, there is no available() api for the ssl socket.
126  return kDefaultBufferSize;
127 }
128 
129 //
130 // Check if a socker error is hard and fatal. Only then should we close the
131 // socket. Soft errors like EINTR and EAGAIN should be ignored or properly
132 // handled with retries
133 //
134 bool SslSession::IsSocketErrorHard(const error_code &ec) {
135 
136  bool error;
137  error = TcpSession::IsSocketErrorHard(ec);
138 #if defined(SSL_R_SHORT_READ) // openssl 1.0
139  if (ec.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)) {
140  error = false;
141  }
142 #elif defined(BOOST_ASIO_SSL_ERROR_STREAM_TRUNCATED) // new openssl and boost
143  if (ec.value() == boost::asio::ssl::error::stream_truncated) {
144  error = false;
145  }
146 #endif
147 
148  return error;
149 }
150 
151 size_t SslSession::ReadSome(mutable_buffer buffer, error_code *error) {
152  // Read data from the tcp socket or from the ssl socket, as appropriate.
155  return TcpSession::ReadSome(buffer, error);
156 
157  return ssl_socket_->read_some(mutable_buffers_1(buffer), *error);
158 }
159 
160 void SslSession::AsyncWrite(const uint8_t *data, size_t size) {
162  async_write(*ssl_socket_.get(), buffer(data, size),
164  TcpSessionPtr(this), error, bytes_transferred));
165  } else {
166  return (TcpSession::AsyncWrite(data, size));
167  }
168 }
169 
171  SslSessionPtr session,
172  const error_code &error) {
173  session->ssl_handshake_in_progress_ = false;
174  if (!error) {
175  session->SetSslHandShakeSuccess();
176  } else {
177  session->SetSslHandShakeFailure();
178  }
179 
180  if (session->socket() != NULL && !(session->IsClosed())) {
181  cb(session, error);
182  }
183 }
184 
187  srand(static_cast<unsigned>(time(0)));
188  error_code ec;
189  session->ssl_handshake_in_progress_ = true;
190  if (session->IsServerSession()) {
191  session->ssl_socket_->async_handshake(stream_base::server,
192  bind(&SslSession::SslHandShakeCallback, cb, session,
193  error));
194  } else {
195  session->ssl_socket_->async_handshake(stream_base::client,
196  bind(&SslSession::SslHandShakeCallback, cb, session,
197  error));
198  }
199 }
200 
202  server()->event_manager()->io_service()->post(
203  bind(&TriggerSslHandShakeInternal, SslSessionPtr(this), cb));
204 }
boost::asio::io_context * io_service()
Definition: event_manager.h:43
SslSessionPtr session_
Definition: ssl_session.cc:66
SslReader(int task_id, SslSessionPtr session, ReadHandler read_fn, Buffer buffer)
Definition: ssl_session.cc:39
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
Definition: ssl_session.cc:44
ReadHandler read_fn_
Definition: ssl_session.cc:67
string Description() const
Gives a description of the task.
Definition: ssl_session.cc:63
function< void(Buffer)> ReadHandler
Definition: ssl_session.cc:37
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
virtual Socket * socket() const
Definition: ssl_session.cc:99
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
Definition: ssl_session.h:21
virtual void AsyncReadSome()
Definition: ssl_session.cc:109
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
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
boost::scoped_ptr< SslSocket > ssl_socket_
Definition: ssl_session.h:95
bool ssl_handshake_delayed_
Definition: ssl_session.h:104
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
EventManager * event_manager()
Definition: tcp_server.h:77
virtual int reader_task_id() const
Definition: tcp_session.h:205
TcpServer * server()
Definition: tcp_session.h:85
virtual void AsyncWrite(const uint8_t *data, std::size_t size)
Definition: tcp_session.cc:201
static bool IsSocketErrorHard(const boost::system::error_code &ec)
Definition: tcp_session.cc:744
void TriggerAsyncReadHandler()
Definition: tcp_session.cc:348
bool established() const
Definition: tcp_session.h:209
virtual void AsyncReadSome()
Definition: tcp_session.cc:194
virtual void OnRead(Buffer buffer)=0
static void AsyncWriteHandler(TcpSessionPtr session, const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: tcp_session.cc:380
boost::asio::const_buffer Buffer
Definition: tcp_session.h:61
boost::intrusive_ptr< TcpSession > TcpSessionPtr
Definition: tcp_session.h:181
static const int kDefaultBufferSize
Definition: tcp_session.h:41
boost::asio::ip::tcp::socket Socket
Definition: tcp_session.h:57
virtual size_t ReadSome(boost::asio::mutable_buffer buffer, boost::system::error_code *error)
Definition: tcp_session.cc:470
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
Definition: task_int.h:11
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14