engine.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // ssl/detail/engine.hpp
  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 BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
  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/buffer.hpp>
  18. # include <boost/asio/detail/static_mutex.hpp>
  19. # include <boost/asio/ssl/detail/openssl_types.hpp>
  20. # include <boost/asio/ssl/detail/verify_callback.hpp>
  21. # include <boost/asio/ssl/stream_base.hpp>
  22. # include <boost/asio/ssl/verify_mode.hpp>
  23. #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ssl {
  28. namespace detail {
  29. #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  30. class engine
  31. {
  32. public:
  33. enum want
  34. {
  35. // Returned by functions to indicate that the engine wants input. The input
  36. // buffer should be updated to point to the data. The engine then needs to
  37. // be called again to retry the operation.
  38. want_input_and_retry = -2,
  39. // Returned by functions to indicate that the engine wants to write output.
  40. // The output buffer points to the data to be written. The engine then
  41. // needs to be called again to retry the operation.
  42. want_output_and_retry = -1,
  43. // Returned by functions to indicate that the engine doesn't need input or
  44. // output.
  45. want_nothing = 0,
  46. // Returned by functions to indicate that the engine wants to write output.
  47. // The output buffer points to the data to be written. After that the
  48. // operation is complete, and the engine does not need to be called again.
  49. want_output = 1
  50. };
  51. // Construct a new engine for the specified context.
  52. BOOST_ASIO_DECL explicit engine(SSL_CTX* context);
  53. // Destructor.
  54. BOOST_ASIO_DECL ~engine();
  55. // Get the underlying implementation in the native type.
  56. BOOST_ASIO_DECL SSL* native_handle();
  57. // Set the peer verification mode.
  58. BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
  59. verify_mode v, boost::system::error_code& ec);
  60. // Set the peer verification depth.
  61. BOOST_ASIO_DECL boost::system::error_code set_verify_depth(
  62. int depth, boost::system::error_code& ec);
  63. // Set a peer certificate verification callback.
  64. BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
  65. verify_callback_base* callback, boost::system::error_code& ec);
  66. // Perform an SSL handshake using either SSL_connect (client-side) or
  67. // SSL_accept (server-side).
  68. BOOST_ASIO_DECL want handshake(
  69. stream_base::handshake_type type, boost::system::error_code& ec);
  70. // Perform a graceful shutdown of the SSL session.
  71. BOOST_ASIO_DECL want shutdown(boost::system::error_code& ec);
  72. // Write bytes to the SSL session.
  73. BOOST_ASIO_DECL want write(const boost::asio::const_buffer& data,
  74. boost::system::error_code& ec, std::size_t& bytes_transferred);
  75. // Read bytes from the SSL session.
  76. BOOST_ASIO_DECL want read(const boost::asio::mutable_buffer& data,
  77. boost::system::error_code& ec, std::size_t& bytes_transferred);
  78. // Get output data to be written to the transport.
  79. BOOST_ASIO_DECL boost::asio::mutable_buffers_1 get_output(
  80. const boost::asio::mutable_buffer& data);
  81. // Put input data that was read from the transport.
  82. BOOST_ASIO_DECL boost::asio::const_buffer put_input(
  83. const boost::asio::const_buffer& data);
  84. // Map an error::eof code returned by the underlying transport according to
  85. // the type and state of the SSL session. Returns a const reference to the
  86. // error code object, suitable for passing to a completion handler.
  87. BOOST_ASIO_DECL const boost::system::error_code& map_error_code(
  88. boost::system::error_code& ec) const;
  89. private:
  90. // Disallow copying and assignment.
  91. engine(const engine&);
  92. engine& operator=(const engine&);
  93. // Callback used when the SSL implementation wants to verify a certificate.
  94. BOOST_ASIO_DECL static int verify_callback_function(
  95. int preverified, X509_STORE_CTX* ctx);
  96. // The SSL_accept function may not be thread safe. This mutex is used to
  97. // protect all calls to the SSL_accept function.
  98. BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
  99. // Perform one operation. Returns >= 0 on success or error, want_read if the
  100. // operation needs more input, or want_write if it needs to write some output
  101. // before the operation can complete.
  102. BOOST_ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
  103. void* data, std::size_t length, boost::system::error_code& ec,
  104. std::size_t* bytes_transferred);
  105. // Adapt the SSL_accept function to the signature needed for perform().
  106. BOOST_ASIO_DECL int do_accept(void*, std::size_t);
  107. // Adapt the SSL_connect function to the signature needed for perform().
  108. BOOST_ASIO_DECL int do_connect(void*, std::size_t);
  109. // Adapt the SSL_shutdown function to the signature needed for perform().
  110. BOOST_ASIO_DECL int do_shutdown(void*, std::size_t);
  111. // Adapt the SSL_read function to the signature needed for perform().
  112. BOOST_ASIO_DECL int do_read(void* data, std::size_t length);
  113. // Adapt the SSL_write function to the signature needed for perform().
  114. BOOST_ASIO_DECL int do_write(void* data, std::size_t length);
  115. SSL* ssl_;
  116. BIO* ext_bio_;
  117. };
  118. #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
  119. } // namespace detail
  120. } // namespace ssl
  121. } // namespace asio
  122. } // namespace boost
  123. #include <boost/asio/detail/pop_options.hpp>
  124. #if defined(BOOST_ASIO_HEADER_ONLY)
  125. # include <boost/asio/ssl/detail/impl/engine.ipp>
  126. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  127. #endif // BOOST_ASIO_SSL_DETAIL_ENGINE_HPP