engine.hpp 5.7 KB

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