2 // ssl/detail/impl/engine.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff 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 BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
12 #define BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
21 # include <boost/asio/detail/throw_error.hpp>
22 # include <boost/asio/error.hpp>
23 # include <boost/asio/ssl/detail/engine.hpp>
24 # include <boost/asio/ssl/error.hpp>
25 # include <boost/asio/ssl/verify_context.hpp>
26 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
28 #include <boost/asio/detail/push_options.hpp>
35 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
37 engine::engine(SSL_CTX* context)
38 : ssl_(::SSL_new(context))
42 boost::system::error_code ec(
43 static_cast<int>(::ERR_get_error()),
44 boost::asio::error::get_ssl_category());
45 boost::asio::detail::throw_error(ec, "engine");
48 accept_mutex().init();
50 ::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
51 ::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
52 #if defined(SSL_MODE_RELEASE_BUFFERS)
53 ::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
54 #endif // defined(SSL_MODE_RELEASE_BUFFERS)
57 ::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
58 ::SSL_set_bio(ssl_, int_bio, int_bio);
63 if (SSL_get_app_data(ssl_))
65 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
66 SSL_set_app_data(ssl_, 0);
73 SSL* engine::native_handle()
78 boost::system::error_code engine::set_verify_mode(
79 verify_mode v, boost::system::error_code& ec)
81 ::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
83 ec = boost::system::error_code();
87 boost::system::error_code engine::set_verify_depth(
88 int depth, boost::system::error_code& ec)
90 ::SSL_set_verify_depth(ssl_, depth);
92 ec = boost::system::error_code();
96 boost::system::error_code engine::set_verify_callback(
97 verify_callback_base* callback, boost::system::error_code& ec)
99 if (SSL_get_app_data(ssl_))
100 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
102 SSL_set_app_data(ssl_, callback);
104 ::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
105 &engine::verify_callback_function);
107 ec = boost::system::error_code();
111 int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
115 if (SSL* ssl = static_cast<SSL*>(
116 ::X509_STORE_CTX_get_ex_data(
117 ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
119 if (SSL_get_app_data(ssl))
121 verify_callback_base* callback =
122 static_cast<verify_callback_base*>(
123 SSL_get_app_data(ssl));
125 verify_context verify_ctx(ctx);
126 return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
134 engine::want engine::handshake(
135 stream_base::handshake_type type, boost::system::error_code& ec)
137 return perform((type == boost::asio::ssl::stream_base::client)
138 ? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
141 engine::want engine::shutdown(boost::system::error_code& ec)
143 return perform(&engine::do_shutdown, 0, 0, ec, 0);
146 engine::want engine::write(const boost::asio::const_buffer& data,
147 boost::system::error_code& ec, std::size_t& bytes_transferred)
149 if (boost::asio::buffer_size(data) == 0)
151 ec = boost::system::error_code();
152 return engine::want_nothing;
155 return perform(&engine::do_write,
156 const_cast<void*>(boost::asio::buffer_cast<const void*>(data)),
157 boost::asio::buffer_size(data), ec, &bytes_transferred);
160 engine::want engine::read(const boost::asio::mutable_buffer& data,
161 boost::system::error_code& ec, std::size_t& bytes_transferred)
163 if (boost::asio::buffer_size(data) == 0)
165 ec = boost::system::error_code();
166 return engine::want_nothing;
169 return perform(&engine::do_read,
170 boost::asio::buffer_cast<void*>(data),
171 boost::asio::buffer_size(data), ec, &bytes_transferred);
174 boost::asio::mutable_buffers_1 engine::get_output(
175 const boost::asio::mutable_buffer& data)
177 int length = ::BIO_read(ext_bio_,
178 boost::asio::buffer_cast<void*>(data),
179 static_cast<int>(boost::asio::buffer_size(data)));
181 return boost::asio::buffer(data,
182 length > 0 ? static_cast<std::size_t>(length) : 0);
185 boost::asio::const_buffer engine::put_input(
186 const boost::asio::const_buffer& data)
188 int length = ::BIO_write(ext_bio_,
189 boost::asio::buffer_cast<const void*>(data),
190 static_cast<int>(boost::asio::buffer_size(data)));
192 return boost::asio::buffer(data +
193 (length > 0 ? static_cast<std::size_t>(length) : 0));
196 const boost::system::error_code& engine::map_error_code(
197 boost::system::error_code& ec) const
199 // We only want to map the error::eof code.
200 if (ec != boost::asio::error::eof)
203 // If there's data yet to be read, it's an error.
204 if (BIO_wpending(ext_bio_))
206 ec = boost::asio::ssl::error::stream_truncated;
210 // SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
211 // underlying transport is passed through.
212 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
213 if (ssl_->version == SSL2_VERSION)
215 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
217 // Otherwise, the peer should have negotiated a proper shutdown.
218 if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
220 ec = boost::asio::ssl::error::stream_truncated;
226 boost::asio::detail::static_mutex& engine::accept_mutex()
228 static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
232 engine::want engine::perform(int (engine::* op)(void*, std::size_t),
233 void* data, std::size_t length, boost::system::error_code& ec,
234 std::size_t* bytes_transferred)
236 std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
238 int result = (this->*op)(data, length);
239 int ssl_error = ::SSL_get_error(ssl_, result);
240 int sys_error = static_cast<int>(::ERR_get_error());
241 std::size_t pending_output_after = ::BIO_ctrl_pending(ext_bio_);
243 if (ssl_error == SSL_ERROR_SSL)
245 ec = boost::system::error_code(sys_error,
246 boost::asio::error::get_ssl_category());
250 if (ssl_error == SSL_ERROR_SYSCALL)
252 ec = boost::system::error_code(sys_error,
253 boost::asio::error::get_system_category());
257 if (result > 0 && bytes_transferred)
258 *bytes_transferred = static_cast<std::size_t>(result);
260 if (ssl_error == SSL_ERROR_WANT_WRITE)
262 ec = boost::system::error_code();
263 return want_output_and_retry;
265 else if (pending_output_after > pending_output_before)
267 ec = boost::system::error_code();
268 return result > 0 ? want_output : want_output_and_retry;
270 else if (ssl_error == SSL_ERROR_WANT_READ)
272 ec = boost::system::error_code();
273 return want_input_and_retry;
275 else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
277 ec = boost::asio::error::eof;
282 ec = boost::system::error_code();
287 int engine::do_accept(void*, std::size_t)
289 boost::asio::detail::static_mutex::scoped_lock lock(accept_mutex());
290 return ::SSL_accept(ssl_);
293 int engine::do_connect(void*, std::size_t)
295 return ::SSL_connect(ssl_);
298 int engine::do_shutdown(void*, std::size_t)
300 int result = ::SSL_shutdown(ssl_);
302 result = ::SSL_shutdown(ssl_);
306 int engine::do_read(void* data, std::size_t length)
308 return ::SSL_read(ssl_, data,
309 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
312 int engine::do_write(void* data, std::size_t length)
314 return ::SSL_write(ssl_, data,
315 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
318 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
320 } // namespace detail
325 #include <boost/asio/detail/pop_options.hpp>
327 #endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP