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 7.4KB

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