2 // ssl/detail/impl/engine.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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 #include <boost/asio/detail/throw_error.hpp>
21 #include <boost/asio/error.hpp>
22 #include <boost/asio/ssl/detail/engine.hpp>
23 #include <boost/asio/ssl/error.hpp>
24 #include <boost/asio/ssl/verify_context.hpp>
26 #include <boost/asio/detail/push_options.hpp>
33 engine::engine(SSL_CTX* context)
34 : ssl_(::SSL_new(context))
38 boost::system::error_code ec(
39 static_cast<int>(::ERR_get_error()),
40 boost::asio::error::get_ssl_category());
41 boost::asio::detail::throw_error(ec, "engine");
44 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
45 accept_mutex().init();
46 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
48 ::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
49 ::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
50 #if defined(SSL_MODE_RELEASE_BUFFERS)
51 ::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
52 #endif // defined(SSL_MODE_RELEASE_BUFFERS)
55 ::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
56 ::SSL_set_bio(ssl_, int_bio, int_bio);
61 if (SSL_get_app_data(ssl_))
63 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
64 SSL_set_app_data(ssl_, 0);
71 SSL* engine::native_handle()
76 boost::system::error_code engine::set_verify_mode(
77 verify_mode v, boost::system::error_code& ec)
79 ::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
81 ec = boost::system::error_code();
85 boost::system::error_code engine::set_verify_depth(
86 int depth, boost::system::error_code& ec)
88 ::SSL_set_verify_depth(ssl_, depth);
90 ec = boost::system::error_code();
94 boost::system::error_code engine::set_verify_callback(
95 verify_callback_base* callback, boost::system::error_code& ec)
97 if (SSL_get_app_data(ssl_))
98 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
100 SSL_set_app_data(ssl_, callback);
102 ::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
103 &engine::verify_callback_function);
105 ec = boost::system::error_code();
109 int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
113 if (SSL* ssl = static_cast<SSL*>(
114 ::X509_STORE_CTX_get_ex_data(
115 ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
117 if (SSL_get_app_data(ssl))
119 verify_callback_base* callback =
120 static_cast<verify_callback_base*>(
121 SSL_get_app_data(ssl));
123 verify_context verify_ctx(ctx);
124 return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
132 engine::want engine::handshake(
133 stream_base::handshake_type type, boost::system::error_code& ec)
135 return perform((type == boost::asio::ssl::stream_base::client)
136 ? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
139 engine::want engine::shutdown(boost::system::error_code& ec)
141 return perform(&engine::do_shutdown, 0, 0, ec, 0);
144 engine::want engine::write(const boost::asio::const_buffer& data,
145 boost::system::error_code& ec, std::size_t& bytes_transferred)
147 if (data.size() == 0)
149 ec = boost::system::error_code();
150 return engine::want_nothing;
153 return perform(&engine::do_write,
154 const_cast<void*>(data.data()),
155 data.size(), ec, &bytes_transferred);
158 engine::want engine::read(const boost::asio::mutable_buffer& data,
159 boost::system::error_code& ec, std::size_t& bytes_transferred)
161 if (data.size() == 0)
163 ec = boost::system::error_code();
164 return engine::want_nothing;
167 return perform(&engine::do_read, data.data(),
168 data.size(), ec, &bytes_transferred);
171 boost::asio::mutable_buffer engine::get_output(
172 const boost::asio::mutable_buffer& data)
174 int length = ::BIO_read(ext_bio_,
175 data.data(), static_cast<int>(data.size()));
177 return boost::asio::buffer(data,
178 length > 0 ? static_cast<std::size_t>(length) : 0);
181 boost::asio::const_buffer engine::put_input(
182 const boost::asio::const_buffer& data)
184 int length = ::BIO_write(ext_bio_,
185 data.data(), static_cast<int>(data.size()));
187 return boost::asio::buffer(data +
188 (length > 0 ? static_cast<std::size_t>(length) : 0));
191 const boost::system::error_code& engine::map_error_code(
192 boost::system::error_code& ec) const
194 // We only want to map the error::eof code.
195 if (ec != boost::asio::error::eof)
198 // If there's data yet to be read, it's an error.
199 if (BIO_wpending(ext_bio_))
201 ec = boost::asio::ssl::error::stream_truncated;
205 // SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
206 // underlying transport is passed through.
207 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
208 if (SSL_version(ssl_) == SSL2_VERSION)
210 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
212 // Otherwise, the peer should have negotiated a proper shutdown.
213 if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
215 ec = boost::asio::ssl::error::stream_truncated;
221 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
222 boost::asio::detail::static_mutex& engine::accept_mutex()
224 static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
227 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
229 engine::want engine::perform(int (engine::* op)(void*, std::size_t),
230 void* data, std::size_t length, boost::system::error_code& ec,
231 std::size_t* bytes_transferred)
233 std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
235 int result = (this->*op)(data, length);
236 int ssl_error = ::SSL_get_error(ssl_, result);
237 int sys_error = static_cast<int>(::ERR_get_error());
238 std::size_t pending_output_after = ::BIO_ctrl_pending(ext_bio_);
240 if (ssl_error == SSL_ERROR_SSL)
242 ec = boost::system::error_code(sys_error,
243 boost::asio::error::get_ssl_category());
247 if (ssl_error == SSL_ERROR_SYSCALL)
249 ec = boost::system::error_code(sys_error,
250 boost::asio::error::get_system_category());
254 if (result > 0 && bytes_transferred)
255 *bytes_transferred = static_cast<std::size_t>(result);
257 if (ssl_error == SSL_ERROR_WANT_WRITE)
259 ec = boost::system::error_code();
260 return want_output_and_retry;
262 else if (pending_output_after > pending_output_before)
264 ec = boost::system::error_code();
265 return result > 0 ? want_output : want_output_and_retry;
267 else if (ssl_error == SSL_ERROR_WANT_READ)
269 ec = boost::system::error_code();
270 return want_input_and_retry;
272 else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
274 ec = boost::asio::error::eof;
279 ec = boost::system::error_code();
284 int engine::do_accept(void*, std::size_t)
286 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
287 boost::asio::detail::static_mutex::scoped_lock lock(accept_mutex());
288 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
289 return ::SSL_accept(ssl_);
292 int engine::do_connect(void*, std::size_t)
294 return ::SSL_connect(ssl_);
297 int engine::do_shutdown(void*, std::size_t)
299 int result = ::SSL_shutdown(ssl_);
301 result = ::SSL_shutdown(ssl_);
305 int engine::do_read(void* data, std::size_t length)
307 return ::SSL_read(ssl_, data,
308 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
311 int engine::do_write(void* data, std::size_t length)
313 return ::SSL_write(ssl_, data,
314 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
317 } // namespace detail
322 #include <boost/asio/detail/pop_options.hpp>
324 #endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP