Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

engine.ipp 8.1KB

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