engine.ipp 9.0 KB

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