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