engine.ipp 7.9 KB

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