engine.ipp 9.7 KB

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