Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:01

0001 //
0002 // ssl/detail/engine.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
0012 #define BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #include <boost/asio/buffer.hpp>
0021 #include <boost/asio/detail/static_mutex.hpp>
0022 #include <boost/asio/ssl/detail/openssl_types.hpp>
0023 #include <boost/asio/ssl/detail/verify_callback.hpp>
0024 #include <boost/asio/ssl/stream_base.hpp>
0025 #include <boost/asio/ssl/verify_mode.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace ssl {
0032 namespace detail {
0033 
0034 class engine
0035 {
0036 public:
0037   enum want
0038   {
0039     // Returned by functions to indicate that the engine wants input. The input
0040     // buffer should be updated to point to the data. The engine then needs to
0041     // be called again to retry the operation.
0042     want_input_and_retry = -2,
0043 
0044     // Returned by functions to indicate that the engine wants to write output.
0045     // The output buffer points to the data to be written. The engine then
0046     // needs to be called again to retry the operation.
0047     want_output_and_retry = -1,
0048 
0049     // Returned by functions to indicate that the engine doesn't need input or
0050     // output.
0051     want_nothing = 0,
0052 
0053     // Returned by functions to indicate that the engine wants to write output.
0054     // The output buffer points to the data to be written. After that the
0055     // operation is complete, and the engine does not need to be called again.
0056     want_output = 1
0057   };
0058 
0059   // Construct a new engine for the specified context.
0060   BOOST_ASIO_DECL explicit engine(SSL_CTX* context);
0061 
0062   // Construct a new engine for an existing native SSL implementation.
0063   BOOST_ASIO_DECL explicit engine(SSL* ssl_impl);
0064 
0065   // Move construct from another engine.
0066   BOOST_ASIO_DECL engine(engine&& other) noexcept;
0067 
0068   // Destructor.
0069   BOOST_ASIO_DECL ~engine();
0070 
0071   // Move assign from another engine.
0072   BOOST_ASIO_DECL engine& operator=(engine&& other) noexcept;
0073 
0074   // Get the underlying implementation in the native type.
0075   BOOST_ASIO_DECL SSL* native_handle();
0076 
0077   // Set the peer verification mode.
0078   BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
0079       verify_mode v, boost::system::error_code& ec);
0080 
0081   // Set the peer verification depth.
0082   BOOST_ASIO_DECL boost::system::error_code set_verify_depth(
0083       int depth, boost::system::error_code& ec);
0084 
0085   // Set a peer certificate verification callback.
0086   BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
0087       verify_callback_base* callback, boost::system::error_code& ec);
0088 
0089   // Perform an SSL handshake using either SSL_connect (client-side) or
0090   // SSL_accept (server-side).
0091   BOOST_ASIO_DECL want handshake(
0092       stream_base::handshake_type type, boost::system::error_code& ec);
0093 
0094   // Perform a graceful shutdown of the SSL session.
0095   BOOST_ASIO_DECL want shutdown(boost::system::error_code& ec);
0096 
0097   // Write bytes to the SSL session.
0098   BOOST_ASIO_DECL want write(const boost::asio::const_buffer& data,
0099       boost::system::error_code& ec, std::size_t& bytes_transferred);
0100 
0101   // Read bytes from the SSL session.
0102   BOOST_ASIO_DECL want read(const boost::asio::mutable_buffer& data,
0103       boost::system::error_code& ec, std::size_t& bytes_transferred);
0104 
0105   // Get output data to be written to the transport.
0106   BOOST_ASIO_DECL boost::asio::mutable_buffer get_output(
0107       const boost::asio::mutable_buffer& data);
0108 
0109   // Put input data that was read from the transport.
0110   BOOST_ASIO_DECL boost::asio::const_buffer put_input(
0111       const boost::asio::const_buffer& data);
0112 
0113   // Map an error::eof code returned by the underlying transport according to
0114   // the type and state of the SSL session. Returns a const reference to the
0115   // error code object, suitable for passing to a completion handler.
0116   BOOST_ASIO_DECL const boost::system::error_code& map_error_code(
0117       boost::system::error_code& ec) const;
0118 
0119 private:
0120   // Disallow copying and assignment.
0121   engine(const engine&);
0122   engine& operator=(const engine&);
0123 
0124   // Callback used when the SSL implementation wants to verify a certificate.
0125   BOOST_ASIO_DECL static int verify_callback_function(
0126       int preverified, X509_STORE_CTX* ctx);
0127 
0128 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0129   // The SSL_accept function may not be thread safe. This mutex is used to
0130   // protect all calls to the SSL_accept function.
0131   BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
0132 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
0133 
0134   // Perform one operation. Returns >= 0 on success or error, want_read if the
0135   // operation needs more input, or want_write if it needs to write some output
0136   // before the operation can complete.
0137   BOOST_ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
0138       void* data, std::size_t length, boost::system::error_code& ec,
0139       std::size_t* bytes_transferred);
0140 
0141   // Adapt the SSL_accept function to the signature needed for perform().
0142   BOOST_ASIO_DECL int do_accept(void*, std::size_t);
0143 
0144   // Adapt the SSL_connect function to the signature needed for perform().
0145   BOOST_ASIO_DECL int do_connect(void*, std::size_t);
0146 
0147   // Adapt the SSL_shutdown function to the signature needed for perform().
0148   BOOST_ASIO_DECL int do_shutdown(void*, std::size_t);
0149 
0150   // Adapt the SSL_read function to the signature needed for perform().
0151   BOOST_ASIO_DECL int do_read(void* data, std::size_t length);
0152 
0153   // Adapt the SSL_write function to the signature needed for perform().
0154   BOOST_ASIO_DECL int do_write(void* data, std::size_t length);
0155 
0156   SSL* ssl_;
0157   BIO* ext_bio_;
0158 };
0159 
0160 } // namespace detail
0161 } // namespace ssl
0162 } // namespace asio
0163 } // namespace boost
0164 
0165 #include <boost/asio/detail/pop_options.hpp>
0166 
0167 #if defined(BOOST_ASIO_HEADER_ONLY)
0168 # include <boost/asio/ssl/detail/impl/engine.ipp>
0169 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0170 
0171 #endif // BOOST_ASIO_SSL_DETAIL_ENGINE_HPP