2
// openssl_operation.hpp
3
// ~~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11
#ifndef ASIO_SSL_DETAIL_OPENSSL_OPERATION_HPP
12
#define ASIO_SSL_DETAIL_OPENSSL_OPERATION_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
21
#include <boost/function.hpp>
22
#include <boost/assert.hpp>
23
#include <boost/bind.hpp>
24
#include "asio/detail/pop_options.hpp"
26
#include "asio/buffer.hpp"
27
#include "asio/placeholders.hpp"
28
#include "asio/write.hpp"
29
#include "asio/detail/socket_ops.hpp"
30
#include "asio/ssl/detail/openssl_types.hpp"
36
typedef boost::function<int (::SSL*)> ssl_primitive_func;
37
typedef boost::function<void (const asio::error_code&, int)>
40
// Network send_/recv buffer implementation
45
static const int NET_BUF_SIZE = 16*1024 + 256; // SSL record size + spare
47
unsigned char buf_[NET_BUF_SIZE];
48
unsigned char* data_start_;
49
unsigned char* data_end_;
54
data_start_ = data_end_ = buf_;
56
unsigned char* get_unused_start() { return data_end_; }
57
unsigned char* get_data_start() { return data_start_; }
58
size_t get_unused_len() { return (NET_BUF_SIZE - (data_end_ - buf_)); }
59
size_t get_data_len() { return (data_end_ - data_start_); }
60
void data_added(size_t count)
63
data_end_ = data_end_ > (buf_ + NET_BUF_SIZE)?
64
(buf_ + NET_BUF_SIZE):
67
void data_removed(size_t count)
70
if (data_start_ >= data_end_) reset();
72
void reset() { data_start_ = buf_; data_end_ = buf_; }
73
bool has_data() { return (data_start_ < data_end_); }
74
}; // class net_buffer
80
template <typename Stream>
81
class openssl_operation
85
// Constructor for asynchronous operations
86
openssl_operation(ssl_primitive_func primitive,
91
user_handler_func handler,
92
asio::io_service::strand& strand
94
: primitive_(primitive)
95
, user_handler_(handler)
102
write_ = boost::bind(
103
&openssl_operation::do_async_write,
104
this, boost::arg<1>(), boost::arg<2>()
107
&openssl_operation::do_async_read,
110
handler_= boost::bind(
111
&openssl_operation::async_user_handler,
112
this, boost::arg<1>(), boost::arg<2>()
116
// Constructor for synchronous operations
117
openssl_operation(ssl_primitive_func primitive,
119
net_buffer& recv_buf,
122
: primitive_(primitive)
124
, recv_buf_(recv_buf)
129
write_ = boost::bind(
130
&openssl_operation::do_sync_write,
131
this, boost::arg<1>(), boost::arg<2>()
134
&openssl_operation::do_sync_read,
137
handler_ = boost::bind(
138
&openssl_operation::sync_user_handler,
139
this, boost::arg<1>(), boost::arg<2>()
144
// In case of asynchronous it returns 0, in sync mode returns success code
145
// or throws an error...
148
int rc = primitive_( session_ );
150
bool is_operation_done = (rc > 0);
151
// For connect/accept/shutdown, the operation
152
// is done, when return code is 1
153
// for write, it is done, when is retcode > 0
154
// for read, is is done when retcode > 0
156
int error_code = !is_operation_done ?
157
::SSL_get_error( session_, rc ) :
159
int sys_error_code = ERR_get_error();
161
bool is_read_needed = (error_code == SSL_ERROR_WANT_READ);
162
bool is_write_needed = (error_code == SSL_ERROR_WANT_WRITE ||
163
::BIO_ctrl_pending( ssl_bio_ ));
164
bool is_shut_down_received =
165
((::SSL_get_shutdown( session_ ) & SSL_RECEIVED_SHUTDOWN) ==
166
SSL_RECEIVED_SHUTDOWN);
167
bool is_shut_down_sent =
168
((::SSL_get_shutdown( session_ ) & SSL_SENT_SHUTDOWN) ==
171
if (is_shut_down_sent && is_shut_down_received && is_operation_done && !is_write_needed)
172
// SSL connection is shut down cleanly
173
return handler_(asio::error_code(), 1);
175
if (is_shut_down_received && !is_operation_done)
176
// Shutdown has been requested, while we were reading or writing...
177
// abort our action...
178
return handler_(asio::error::shut_down, 0);
180
if (!is_operation_done && !is_read_needed && !is_write_needed
181
&& !is_shut_down_sent)
183
// The operation has failed... It is not completed and does
184
// not want network communication nor does want to send shutdown out...
185
if (error_code == SSL_ERROR_SYSCALL)
187
return handler_(asio::error_code(
188
sys_error_code, asio::error::system_category), rc);
192
return handler_(asio::error_code(
193
error_code, asio::error::get_ssl_category()), rc);
197
if (!is_operation_done && !is_write_needed)
199
// We may have left over data that we can pass to SSL immediately
200
if (recv_buf_.get_data_len() > 0)
202
// Pass the buffered data to SSL
203
int written = ::BIO_write
206
recv_buf_.get_data_start(),
207
recv_buf_.get_data_len()
212
recv_buf_.data_removed(written);
214
else if (written < 0)
216
if (!BIO_should_retry(ssl_bio_))
218
// Some serios error with BIO....
219
return handler_(asio::error::no_recovery, 0);
225
else if (is_read_needed || (is_shut_down_sent && !is_shut_down_received))
231
// Continue with operation, flush any SSL data out to network...
232
return write_(is_operation_done, rc);
235
// Private implementation
237
typedef boost::function<int (const asio::error_code&, int)>
239
typedef boost::function<int (bool, int)> write_func;
240
typedef boost::function<int ()> read_func;
242
ssl_primitive_func primitive_;
243
user_handler_func user_handler_;
244
asio::io_service::strand* strand_;
247
int_handler_func handler_;
249
net_buffer send_buf_; // buffers for network IO
251
// The recv buffer is owned by the stream, not the operation, since there can
252
// be left over bytes after passing the data up to the application, and these
253
// bytes need to be kept around for the next read operation issued by the
255
net_buffer& recv_buf_;
262
int sync_user_handler(const asio::error_code& error, int rc)
267
throw asio::system_error(error);
270
int async_user_handler(asio::error_code error, int rc)
275
error = asio::error::no_recovery;
279
user_handler_(error, rc);
283
// Writes bytes asynchronously from SSL to NET
284
int do_async_write(bool is_operation_done, int rc)
286
int len = ::BIO_ctrl_pending( ssl_bio_ );
289
// There is something to write into net, do it...
290
len = (int)send_buf_.get_unused_len() > len?
292
send_buf_.get_unused_len();
296
// In case our send buffer is full, we have just to wait until
297
// previous send to complete...
301
// Read outgoing data from bio
302
len = ::BIO_read( ssl_bio_, send_buf_.get_unused_start(), len);
306
unsigned char *data_start = send_buf_.get_unused_start();
307
send_buf_.data_added(len);
309
BOOST_ASSERT(strand_);
313
asio::buffer(data_start, len),
318
&openssl_operation::async_write_handler,
322
asio::placeholders::error,
323
asio::placeholders::bytes_transferred
330
else if (!BIO_should_retry(ssl_bio_))
332
// Seems like fatal error
333
// reading from SSL BIO has failed...
334
handler_(asio::error::no_recovery, 0);
339
if (is_operation_done)
341
// Finish the operation, with success
342
handler_(asio::error_code(), rc);
346
// OPeration is not done and writing to net has been made...
347
// start operation again
353
void async_write_handler(bool is_operation_done, int rc,
354
const asio::error_code& error, size_t bytes_sent)
358
// Remove data from send buffer
359
send_buf_.data_removed(bytes_sent);
361
if (is_operation_done)
362
handler_(asio::error_code(), rc);
364
// Since the operation was not completed, try it again...
374
BOOST_ASSERT(strand_);
375
socket_.async_read_some
377
asio::buffer(recv_buf_.get_unused_start(),
378
recv_buf_.get_unused_len()),
383
&openssl_operation::async_read_handler,
385
asio::placeholders::error,
386
asio::placeholders::bytes_transferred
393
void async_read_handler(const asio::error_code& error,
398
recv_buf_.data_added(bytes_recvd);
400
// Pass the received data to SSL
401
int written = ::BIO_write
404
recv_buf_.get_data_start(),
405
recv_buf_.get_data_len()
410
recv_buf_.data_removed(written);
412
else if (written < 0)
414
if (!BIO_should_retry(ssl_bio_))
416
// Some serios error with BIO....
417
handler_(asio::error::no_recovery, 0);
422
// and try the SSL primitive again
427
// Error in network level...
428
// SSL can't continue either...
433
// Syncronous functions...
434
int do_sync_write(bool is_operation_done, int rc)
436
int len = ::BIO_ctrl_pending( ssl_bio_ );
439
// There is something to write into net, do it...
440
len = (int)send_buf_.get_unused_len() > len?
442
send_buf_.get_unused_len();
444
// Read outgoing data from bio
445
len = ::BIO_read( ssl_bio_, send_buf_.get_unused_start(), len);
449
size_t sent_len = asio::write(
451
asio::buffer(send_buf_.get_unused_start(), len)
454
send_buf_.data_added(len);
455
send_buf_.data_removed(sent_len);
457
else if (!BIO_should_retry(ssl_bio_))
459
// Seems like fatal error
460
// reading from SSL BIO has failed...
461
throw asio::system_error(asio::error::no_recovery);
465
if (is_operation_done)
466
// Finish the operation, with success
469
// Operation is not finished, start again.
475
size_t len = socket_.read_some
477
asio::buffer(recv_buf_.get_unused_start(),
478
recv_buf_.get_unused_len())
482
recv_buf_.data_added(len);
484
// Pass the received data to SSL
485
int written = ::BIO_write
488
recv_buf_.get_data_start(),
489
recv_buf_.get_data_len()
494
recv_buf_.data_removed(written);
496
else if (written < 0)
498
if (!BIO_should_retry(ssl_bio_))
500
// Some serios error with BIO....
501
throw asio::system_error(asio::error::no_recovery);
505
// Try the operation again
508
}; // class openssl_operation
510
} // namespace detail
514
#include "asio/detail/pop_options.hpp"
516
#endif // ASIO_SSL_DETAIL_OPENSSL_OPERATION_HPP