engine.ipp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // ssl/detail/impl/engine.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
  11. #define BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/throw_error.hpp>
  17. #include <boost/asio/error.hpp>
  18. #include <boost/asio/ssl/detail/engine.hpp>
  19. #include <boost/asio/ssl/error.hpp>
  20. #include <boost/asio/ssl/verify_context.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ssl {
  25. namespace detail {
  26. engine::engine(SSL_CTX* context)
  27. : ssl_(::SSL_new(context))
  28. {
  29. if (!ssl_)
  30. {
  31. boost::system::error_code ec(
  32. static_cast<int>(::ERR_get_error()),
  33. boost::asio::error::get_ssl_category());
  34. boost::asio::detail::throw_error(ec, "engine");
  35. }
  36. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  37. accept_mutex().init();
  38. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  39. ::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
  40. ::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  41. #if defined(SSL_MODE_RELEASE_BUFFERS)
  42. ::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
  43. #endif // defined(SSL_MODE_RELEASE_BUFFERS)
  44. ::BIO* int_bio = 0;
  45. ::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
  46. ::SSL_set_bio(ssl_, int_bio, int_bio);
  47. }
  48. engine::~engine()
  49. {
  50. if (SSL_get_app_data(ssl_))
  51. {
  52. delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
  53. SSL_set_app_data(ssl_, 0);
  54. }
  55. ::BIO_free(ext_bio_);
  56. ::SSL_free(ssl_);
  57. }
  58. SSL* engine::native_handle()
  59. {
  60. return ssl_;
  61. }
  62. boost::system::error_code engine::set_verify_mode(
  63. verify_mode v, boost::system::error_code& ec)
  64. {
  65. ::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
  66. ec = boost::system::error_code();
  67. return ec;
  68. }
  69. boost::system::error_code engine::set_verify_depth(
  70. int depth, boost::system::error_code& ec)
  71. {
  72. ::SSL_set_verify_depth(ssl_, depth);
  73. ec = boost::system::error_code();
  74. return ec;
  75. }
  76. boost::system::error_code engine::set_verify_callback(
  77. verify_callback_base* callback, boost::system::error_code& ec)
  78. {
  79. if (SSL_get_app_data(ssl_))
  80. delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
  81. SSL_set_app_data(ssl_, callback);
  82. ::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
  83. &engine::verify_callback_function);
  84. ec = boost::system::error_code();
  85. return ec;
  86. }
  87. int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
  88. {
  89. if (ctx)
  90. {
  91. if (SSL* ssl = static_cast<SSL*>(
  92. ::X509_STORE_CTX_get_ex_data(
  93. ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
  94. {
  95. if (SSL_get_app_data(ssl))
  96. {
  97. verify_callback_base* callback =
  98. static_cast<verify_callback_base*>(
  99. SSL_get_app_data(ssl));
  100. verify_context verify_ctx(ctx);
  101. return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
  102. }
  103. }
  104. }
  105. return 0;
  106. }
  107. engine::want engine::handshake(
  108. stream_base::handshake_type type, boost::system::error_code& ec)
  109. {
  110. return perform((type == boost::asio::ssl::stream_base::client)
  111. ? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
  112. }
  113. engine::want engine::shutdown(boost::system::error_code& ec)
  114. {
  115. return perform(&engine::do_shutdown, 0, 0, ec, 0);
  116. }
  117. engine::want engine::write(const boost::asio::const_buffer& data,
  118. boost::system::error_code& ec, std::size_t& bytes_transferred)
  119. {
  120. if (data.size() == 0)
  121. {
  122. ec = boost::system::error_code();
  123. return engine::want_nothing;
  124. }
  125. return perform(&engine::do_write,
  126. const_cast<void*>(data.data()),
  127. data.size(), ec, &bytes_transferred);
  128. }
  129. engine::want engine::read(const boost::asio::mutable_buffer& data,
  130. boost::system::error_code& ec, std::size_t& bytes_transferred)
  131. {
  132. if (data.size() == 0)
  133. {
  134. ec = boost::system::error_code();
  135. return engine::want_nothing;
  136. }
  137. return perform(&engine::do_read, data.data(),
  138. data.size(), ec, &bytes_transferred);
  139. }
  140. boost::asio::mutable_buffer engine::get_output(
  141. const boost::asio::mutable_buffer& data)
  142. {
  143. int length = ::BIO_read(ext_bio_,
  144. data.data(), static_cast<int>(data.size()));
  145. return boost::asio::buffer(data,
  146. length > 0 ? static_cast<std::size_t>(length) : 0);
  147. }
  148. boost::asio::const_buffer engine::put_input(
  149. const boost::asio::const_buffer& data)
  150. {
  151. int length = ::BIO_write(ext_bio_,
  152. data.data(), static_cast<int>(data.size()));
  153. return boost::asio::buffer(data +
  154. (length > 0 ? static_cast<std::size_t>(length) : 0));
  155. }
  156. const boost::system::error_code& engine::map_error_code(
  157. boost::system::error_code& ec) const
  158. {
  159. // We only want to map the error::eof code.
  160. if (ec != boost::asio::error::eof)
  161. return ec;
  162. // If there's data yet to be read, it's an error.
  163. if (BIO_wpending(ext_bio_))
  164. {
  165. ec = boost::asio::ssl::error::stream_truncated;
  166. return ec;
  167. }
  168. // SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
  169. // underlying transport is passed through.
  170. #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
  171. if (SSL_version(ssl_) == SSL2_VERSION)
  172. return ec;
  173. #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
  174. // Otherwise, the peer should have negotiated a proper shutdown.
  175. if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
  176. {
  177. ec = boost::asio::ssl::error::stream_truncated;
  178. }
  179. return ec;
  180. }
  181. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  182. boost::asio::detail::static_mutex& engine::accept_mutex()
  183. {
  184. static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
  185. return mutex;
  186. }
  187. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  188. engine::want engine::perform(int (engine::* op)(void*, std::size_t),
  189. void* data, std::size_t length, boost::system::error_code& ec,
  190. std::size_t* bytes_transferred)
  191. {
  192. std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
  193. ::ERR_clear_error();
  194. int result = (this->*op)(data, length);
  195. int ssl_error = ::SSL_get_error(ssl_, result);
  196. int sys_error = static_cast<int>(::ERR_get_error());
  197. std::size_t pending_output_after = ::BIO_ctrl_pending(ext_bio_);
  198. if (ssl_error == SSL_ERROR_SSL)
  199. {
  200. ec = boost::system::error_code(sys_error,
  201. boost::asio::error::get_ssl_category());
  202. return pending_output_after > pending_output_before
  203. ? want_output : want_nothing;
  204. }
  205. if (ssl_error == SSL_ERROR_SYSCALL)
  206. {
  207. if (sys_error == 0)
  208. {
  209. ec = boost::asio::ssl::error::unspecified_system_error;
  210. }
  211. else
  212. {
  213. ec = boost::system::error_code(sys_error,
  214. boost::asio::error::get_ssl_category());
  215. }
  216. return pending_output_after > pending_output_before
  217. ? want_output : want_nothing;
  218. }
  219. if (result > 0 && bytes_transferred)
  220. *bytes_transferred = static_cast<std::size_t>(result);
  221. if (ssl_error == SSL_ERROR_WANT_WRITE)
  222. {
  223. ec = boost::system::error_code();
  224. return want_output_and_retry;
  225. }
  226. else if (pending_output_after > pending_output_before)
  227. {
  228. ec = boost::system::error_code();
  229. return result > 0 ? want_output : want_output_and_retry;
  230. }
  231. else if (ssl_error == SSL_ERROR_WANT_READ)
  232. {
  233. ec = boost::system::error_code();
  234. return want_input_and_retry;
  235. }
  236. else if (ssl_error == SSL_ERROR_ZERO_RETURN)
  237. {
  238. ec = boost::asio::error::eof;
  239. return want_nothing;
  240. }
  241. else if (ssl_error == SSL_ERROR_NONE)
  242. {
  243. ec = boost::system::error_code();
  244. return want_nothing;
  245. }
  246. else
  247. {
  248. ec = boost::asio::ssl::error::unexpected_result;
  249. return want_nothing;
  250. }
  251. }
  252. int engine::do_accept(void*, std::size_t)
  253. {
  254. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  255. boost::asio::detail::static_mutex::scoped_lock lock(accept_mutex());
  256. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  257. return ::SSL_accept(ssl_);
  258. }
  259. int engine::do_connect(void*, std::size_t)
  260. {
  261. return ::SSL_connect(ssl_);
  262. }
  263. int engine::do_shutdown(void*, std::size_t)
  264. {
  265. int result = ::SSL_shutdown(ssl_);
  266. if (result == 0)
  267. result = ::SSL_shutdown(ssl_);
  268. return result;
  269. }
  270. int engine::do_read(void* data, std::size_t length)
  271. {
  272. return ::SSL_read(ssl_, data,
  273. length < INT_MAX ? static_cast<int>(length) : INT_MAX);
  274. }
  275. int engine::do_write(void* data, std::size_t length)
  276. {
  277. return ::SSL_write(ssl_, data,
  278. length < INT_MAX ? static_cast<int>(length) : INT_MAX);
  279. }
  280. } // namespace detail
  281. } // namespace ssl
  282. } // namespace asio
  283. } // namespace boost
  284. #include <boost/asio/detail/pop_options.hpp>
  285. #endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP