engine.hpp 5.9 KB

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