engine.ipp 9.5 KB

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