engine.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // ssl/detail/engine.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. // Construct a new engine for an existing native SSL implementation.
  51. BOOST_ASIO_DECL explicit engine(SSL* ssl_impl);
  52. // Move construct from another engine.
  53. BOOST_ASIO_DECL engine(engine&& other) noexcept;
  54. // Destructor.
  55. BOOST_ASIO_DECL ~engine();
  56. // Move assign from another engine.
  57. BOOST_ASIO_DECL engine& operator=(engine&& other) noexcept;
  58. // Get the underlying implementation in the native type.
  59. BOOST_ASIO_DECL SSL* native_handle();
  60. // Set the peer verification mode.
  61. BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
  62. verify_mode v, boost::system::error_code& ec);
  63. // Set the peer verification depth.
  64. BOOST_ASIO_DECL boost::system::error_code set_verify_depth(
  65. int depth, boost::system::error_code& ec);
  66. // Set a peer certificate verification callback.
  67. BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
  68. verify_callback_base* callback, boost::system::error_code& ec);
  69. // Perform an SSL handshake using either SSL_connect (client-side) or
  70. // SSL_accept (server-side).
  71. BOOST_ASIO_DECL want handshake(
  72. stream_base::handshake_type type, boost::system::error_code& ec);
  73. // Perform a graceful shutdown of the SSL session.
  74. BOOST_ASIO_DECL want shutdown(boost::system::error_code& ec);
  75. // Write bytes to the SSL session.
  76. BOOST_ASIO_DECL want write(const boost::asio::const_buffer& data,
  77. boost::system::error_code& ec, std::size_t& bytes_transferred);
  78. // Read bytes from the SSL session.
  79. BOOST_ASIO_DECL want read(const boost::asio::mutable_buffer& data,
  80. boost::system::error_code& ec, std::size_t& bytes_transferred);
  81. // Get output data to be written to the transport.
  82. BOOST_ASIO_DECL boost::asio::mutable_buffer get_output(
  83. const boost::asio::mutable_buffer& data);
  84. // Put input data that was read from the transport.
  85. BOOST_ASIO_DECL boost::asio::const_buffer put_input(
  86. const boost::asio::const_buffer& data);
  87. // Map an error::eof code returned by the underlying transport according to
  88. // the type and state of the SSL session. Returns a const reference to the
  89. // error code object, suitable for passing to a completion handler.
  90. BOOST_ASIO_DECL const boost::system::error_code& map_error_code(
  91. boost::system::error_code& ec) const;
  92. private:
  93. // Disallow copying and assignment.
  94. engine(const engine&);
  95. engine& operator=(const engine&);
  96. // Callback used when the SSL implementation wants to verify a certificate.
  97. BOOST_ASIO_DECL static int verify_callback_function(
  98. int preverified, X509_STORE_CTX* ctx);
  99. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  100. // The SSL_accept function may not be thread safe. This mutex is used to
  101. // protect all calls to the SSL_accept function.
  102. BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
  103. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  104. // Perform one operation. Returns >= 0 on success or error, want_read if the
  105. // operation needs more input, or want_write if it needs to write some output
  106. // before the operation can complete.
  107. BOOST_ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
  108. void* data, std::size_t length, boost::system::error_code& ec,
  109. std::size_t* bytes_transferred);
  110. // Adapt the SSL_accept function to the signature needed for perform().
  111. BOOST_ASIO_DECL int do_accept(void*, std::size_t);
  112. // Adapt the SSL_connect function to the signature needed for perform().
  113. BOOST_ASIO_DECL int do_connect(void*, std::size_t);
  114. // Adapt the SSL_shutdown function to the signature needed for perform().
  115. BOOST_ASIO_DECL int do_shutdown(void*, std::size_t);
  116. // Adapt the SSL_read function to the signature needed for perform().
  117. BOOST_ASIO_DECL int do_read(void* data, std::size_t length);
  118. // Adapt the SSL_write function to the signature needed for perform().
  119. BOOST_ASIO_DECL int do_write(void* data, std::size_t length);
  120. SSL* ssl_;
  121. BIO* ext_bio_;
  122. };
  123. } // namespace detail
  124. } // namespace ssl
  125. } // namespace asio
  126. } // namespace boost
  127. #include <boost/asio/detail/pop_options.hpp>
  128. #if defined(BOOST_ASIO_HEADER_ONLY)
  129. # include <boost/asio/ssl/detail/impl/engine.ipp>
  130. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  131. #endif // BOOST_ASIO_SSL_DETAIL_ENGINE_HPP