2 // ssl/detail/engine.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
12 #define BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/buffer.hpp>
21 #include <boost/asio/detail/static_mutex.hpp>
22 #include <boost/asio/ssl/detail/openssl_types.hpp>
23 #include <boost/asio/ssl/detail/verify_callback.hpp>
24 #include <boost/asio/ssl/stream_base.hpp>
25 #include <boost/asio/ssl/verify_mode.hpp>
27 #include <boost/asio/detail/push_options.hpp>
39 // Returned by functions to indicate that the engine wants input. The input
40 // buffer should be updated to point to the data. The engine then needs to
41 // be called again to retry the operation.
42 want_input_and_retry = -2,
44 // Returned by functions to indicate that the engine wants to write output.
45 // The output buffer points to the data to be written. The engine then
46 // needs to be called again to retry the operation.
47 want_output_and_retry = -1,
49 // Returned by functions to indicate that the engine doesn't need input or
53 // Returned by functions to indicate that the engine wants to write output.
54 // The output buffer points to the data to be written. After that the
55 // operation is complete, and the engine does not need to be called again.
59 // Construct a new engine for the specified context.
60 BOOST_ASIO_DECL explicit engine(SSL_CTX* context);
63 BOOST_ASIO_DECL ~engine();
65 // Get the underlying implementation in the native type.
66 BOOST_ASIO_DECL SSL* native_handle();
68 // Set the peer verification mode.
69 BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
70 verify_mode v, boost::system::error_code& ec);
72 // Set the peer verification depth.
73 BOOST_ASIO_DECL boost::system::error_code set_verify_depth(
74 int depth, boost::system::error_code& ec);
76 // Set a peer certificate verification callback.
77 BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
78 verify_callback_base* callback, boost::system::error_code& ec);
80 // Perform an SSL handshake using either SSL_connect (client-side) or
81 // SSL_accept (server-side).
82 BOOST_ASIO_DECL want handshake(
83 stream_base::handshake_type type, boost::system::error_code& ec);
85 // Perform a graceful shutdown of the SSL session.
86 BOOST_ASIO_DECL want shutdown(boost::system::error_code& ec);
88 // Write bytes to the SSL session.
89 BOOST_ASIO_DECL want write(const boost::asio::const_buffer& data,
90 boost::system::error_code& ec, std::size_t& bytes_transferred);
92 // Read bytes from the SSL session.
93 BOOST_ASIO_DECL want read(const boost::asio::mutable_buffer& data,
94 boost::system::error_code& ec, std::size_t& bytes_transferred);
96 // Get output data to be written to the transport.
97 BOOST_ASIO_DECL boost::asio::mutable_buffer get_output(
98 const boost::asio::mutable_buffer& data);
100 // Put input data that was read from the transport.
101 BOOST_ASIO_DECL boost::asio::const_buffer put_input(
102 const boost::asio::const_buffer& data);
104 // Map an error::eof code returned by the underlying transport according to
105 // the type and state of the SSL session. Returns a const reference to the
106 // error code object, suitable for passing to a completion handler.
107 BOOST_ASIO_DECL const boost::system::error_code& map_error_code(
108 boost::system::error_code& ec) const;
111 // Disallow copying and assignment.
112 engine(const engine&);
113 engine& operator=(const engine&);
115 // Callback used when the SSL implementation wants to verify a certificate.
116 BOOST_ASIO_DECL static int verify_callback_function(
117 int preverified, X509_STORE_CTX* ctx);
119 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
120 // The SSL_accept function may not be thread safe. This mutex is used to
121 // protect all calls to the SSL_accept function.
122 BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
123 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
125 // Perform one operation. Returns >= 0 on success or error, want_read if the
126 // operation needs more input, or want_write if it needs to write some output
127 // before the operation can complete.
128 BOOST_ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
129 void* data, std::size_t length, boost::system::error_code& ec,
130 std::size_t* bytes_transferred);
132 // Adapt the SSL_accept function to the signature needed for perform().
133 BOOST_ASIO_DECL int do_accept(void*, std::size_t);
135 // Adapt the SSL_connect function to the signature needed for perform().
136 BOOST_ASIO_DECL int do_connect(void*, std::size_t);
138 // Adapt the SSL_shutdown function to the signature needed for perform().
139 BOOST_ASIO_DECL int do_shutdown(void*, std::size_t);
141 // Adapt the SSL_read function to the signature needed for perform().
142 BOOST_ASIO_DECL int do_read(void* data, std::size_t length);
144 // Adapt the SSL_write function to the signature needed for perform().
145 BOOST_ASIO_DECL int do_write(void* data, std::size_t length);
151 } // namespace detail
156 #include <boost/asio/detail/pop_options.hpp>
158 #if defined(BOOST_ASIO_HEADER_ONLY)
159 # include <boost/asio/ssl/detail/impl/engine.ipp>
160 #endif // defined(BOOST_ASIO_HEADER_ONLY)
162 #endif // BOOST_ASIO_SSL_DETAIL_ENGINE_HPP