engine.ipp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // ssl/detail/impl/engine.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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. #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  17. # include <boost/asio/detail/throw_error.hpp>
  18. # include <boost/asio/error.hpp>
  19. # include <boost/asio/ssl/detail/engine.hpp>
  20. # include <boost/asio/ssl/error.hpp>
  21. # include <boost/asio/ssl/verify_context.hpp>
  22. #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ssl {
  27. namespace detail {
  28. #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  29. engine::engine(SSL_CTX* context)
  30. : ssl_(::SSL_new(context))
  31. {
  32. if (!ssl_)
  33. {
  34. boost::system::error_code ec(
  35. static_cast<int>(::ERR_get_error()),
  36. boost::asio::error::get_ssl_category());
  37. boost::asio::detail::throw_error(ec, "engine");
  38. }
  39. accept_mutex().init();
  40. ::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
  41. ::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  42. #if defined(SSL_MODE_RELEASE_BUFFERS)
  43. ::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
  44. #endif // defined(SSL_MODE_RELEASE_BUFFERS)
  45. ::BIO* int_bio = 0;
  46. ::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
  47. ::SSL_set_bio(ssl_, int_bio, int_bio);
  48. }
  49. engine::~engine()
  50. {
  51. if (SSL_get_app_data(ssl_))
  52. {
  53. delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
  54. SSL_set_app_data(ssl_, 0);
  55. }
  56. ::BIO_free(ext_bio_);
  57. ::SSL_free(ssl_);
  58. }
  59. SSL* engine::native_handle()
  60. {
  61. return ssl_;
  62. }
  63. boost::system::error_code engine::set_verify_mode(
  64. verify_mode v, boost::system::error_code& ec)
  65. {
  66. ::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
  67. ec = boost::system::error_code();
  68. return ec;
  69. }
  70. boost::system::error_code engine::set_verify_depth(
  71. int depth, boost::system::error_code& ec)
  72. {
  73. ::SSL_set_verify_depth(ssl_, depth);
  74. ec = boost::system::error_code();
  75. return ec;
  76. }
  77. boost::system::error_code engine::set_verify_callback(
  78. verify_callback_base* callback, boost::system::error_code& ec)
  79. {
  80. if (SSL_get_app_data(ssl_))
  81. delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
  82. SSL_set_app_data(ssl_, callback);
  83. ::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
  84. &engine::verify_callback_function);
  85. ec = boost::system::error_code();
  86. return ec;
  87. }
  88. int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
  89. {
  90. if (ctx)
  91. {
  92. if (SSL* ssl = static_cast<SSL*>(
  93. ::X509_STORE_CTX_get_ex_data(
  94. ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
  95. {
  96. if (SSL_get_app_data(ssl))
  97. {
  98. verify_callback_base* callback =
  99. static_cast<verify_callback_base*>(
  100. SSL_get_app_data(ssl));
  101. verify_context verify_ctx(ctx);
  102. return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
  103. }
  104. }
  105. }
  106. return 0;
  107. }
  108. engine::want engine::handshake(
  109. stream_base::handshake_type type, boost::system::error_code& ec)
  110. {
  111. return perform((type == boost::asio::ssl::stream_base::client)
  112. ? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
  113. }
  114. engine::want engine::shutdown(boost::system::error_code& ec)
  115. {
  116. return perform(&engine::do_shutdown, 0, 0, ec, 0);
  117. }
  118. engine::want engine::write(const boost::asio::const_buffer& data,
  119. boost::system::error_code& ec, std::size_t& bytes_transferred)
  120. {
  121. if (boost::asio::buffer_size(data) == 0)
  122. {
  123. ec = boost::system::error_code();
  124. return engine::want_nothing;
  125. }
  126. return perform(&engine::do_write,
  127. const_cast<void*>(boost::asio::buffer_cast<const void*>(data)),
  128. boost::asio::buffer_size(data), ec, &bytes_transferred);
  129. }
  130. engine::want engine::read(const boost::asio::mutable_buffer& data,
  131. boost::system::error_code& ec, std::size_t& bytes_transferred)
  132. {
  133. if (boost::asio::buffer_size(data) == 0)
  134. {
  135. ec = boost::system::error_code();
  136. return engine::want_nothing;
  137. }
  138. return perform(&engine::do_read,
  139. boost::asio::buffer_cast<void*>(data),
  140. boost::asio::buffer_size(data), ec, &bytes_transferred);
  141. }
  142. boost::asio::mutable_buffers_1 engine::get_output(
  143. const boost::asio::mutable_buffer& data)
  144. {
  145. int length = ::BIO_read(ext_bio_,
  146. boost::asio::buffer_cast<void*>(data),
  147. static_cast<int>(boost::asio::buffer_size(data)));
  148. return boost::asio::buffer(data,
  149. length > 0 ? static_cast<std::size_t>(length) : 0);
  150. }
  151. boost::asio::const_buffer engine::put_input(
  152. const boost::asio::const_buffer& data)
  153. {
  154. int length = ::BIO_write(ext_bio_,
  155. boost::asio::buffer_cast<const void*>(data),
  156. static_cast<int>(boost::asio::buffer_size(data)));
  157. return boost::asio::buffer(data +
  158. (length > 0 ? static_cast<std::size_t>(length) : 0));
  159. }
  160. const boost::system::error_code& engine::map_error_code(
  161. boost::system::error_code& ec) const
  162. {
  163. // We only want to map the error::eof code.
  164. if (ec != boost::asio::error::eof)
  165. return ec;
  166. // If there's data yet to be read, it's an error.
  167. if (BIO_wpending(ext_bio_))
  168. {
  169. ec = boost::system::error_code(
  170. ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
  171. boost::asio::error::get_ssl_category());
  172. return ec;
  173. }
  174. // SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
  175. // underlying transport is passed through.
  176. if (ssl_ && ssl_->version == SSL2_VERSION)
  177. return ec;
  178. // Otherwise, the peer should have negotiated a proper shutdown.
  179. if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
  180. {
  181. ec = boost::system::error_code(
  182. ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
  183. boost::asio::error::get_ssl_category());
  184. }
  185. return ec;
  186. }
  187. boost::asio::detail::static_mutex& engine::accept_mutex()
  188. {
  189. static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
  190. return mutex;
  191. }
  192. engine::want engine::perform(int (engine::* op)(void*, std::size_t),
  193. void* data, std::size_t length, boost::system::error_code& ec,
  194. std::size_t* bytes_transferred)
  195. {
  196. std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
  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 = boost::system::error_code(sys_error,
  204. boost::asio::error::get_ssl_category());
  205. return want_nothing;
  206. }
  207. if (ssl_error == SSL_ERROR_SYSCALL)
  208. {
  209. ec = boost::system::error_code(sys_error,
  210. boost::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 = boost::system::error_code();
  218. return want_output_and_retry;
  219. }
  220. else if (pending_output_after > pending_output_before)
  221. {
  222. ec = boost::system::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 = boost::system::error_code();
  228. return want_input_and_retry;
  229. }
  230. else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
  231. {
  232. ec = boost::asio::error::eof;
  233. return want_nothing;
  234. }
  235. else
  236. {
  237. ec = boost::system::error_code();
  238. return want_nothing;
  239. }
  240. }
  241. int engine::do_accept(void*, std::size_t)
  242. {
  243. boost::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(BOOST_ASIO_ENABLE_OLD_SSL)
  268. } // namespace detail
  269. } // namespace ssl
  270. } // namespace asio
  271. } // namespace boost
  272. #include <boost/asio/detail/pop_options.hpp>
  273. #endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP