]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ssl/detail/impl/engine.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / ssl / detail / impl / engine.ipp
1 //
2 // ssl/detail/impl/engine.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_IMPL_ENGINE_IPP
12 #define BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
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 #include <boost/asio/detail/throw_error.hpp>
21 #include <boost/asio/error.hpp>
22 #include <boost/asio/ssl/detail/engine.hpp>
23 #include <boost/asio/ssl/error.hpp>
24 #include <boost/asio/ssl/verify_context.hpp>
25
26 #include <boost/asio/detail/push_options.hpp>
27
28 namespace boost {
29 namespace asio {
30 namespace ssl {
31 namespace detail {
32
33 engine::engine(SSL_CTX* context)
34 : ssl_(::SSL_new(context))
35 {
36 if (!ssl_)
37 {
38 boost::system::error_code ec(
39 static_cast<int>(::ERR_get_error()),
40 boost::asio::error::get_ssl_category());
41 boost::asio::detail::throw_error(ec, "engine");
42 }
43
44 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
45 accept_mutex().init();
46 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
47
48 ::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
49 ::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
50 #if defined(SSL_MODE_RELEASE_BUFFERS)
51 ::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
52 #endif // defined(SSL_MODE_RELEASE_BUFFERS)
53
54 ::BIO* int_bio = 0;
55 ::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
56 ::SSL_set_bio(ssl_, int_bio, int_bio);
57 }
58
59 engine::~engine()
60 {
61 if (SSL_get_app_data(ssl_))
62 {
63 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
64 SSL_set_app_data(ssl_, 0);
65 }
66
67 ::BIO_free(ext_bio_);
68 ::SSL_free(ssl_);
69 }
70
71 SSL* engine::native_handle()
72 {
73 return ssl_;
74 }
75
76 boost::system::error_code engine::set_verify_mode(
77 verify_mode v, boost::system::error_code& ec)
78 {
79 ::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
80
81 ec = boost::system::error_code();
82 return ec;
83 }
84
85 boost::system::error_code engine::set_verify_depth(
86 int depth, boost::system::error_code& ec)
87 {
88 ::SSL_set_verify_depth(ssl_, depth);
89
90 ec = boost::system::error_code();
91 return ec;
92 }
93
94 boost::system::error_code engine::set_verify_callback(
95 verify_callback_base* callback, boost::system::error_code& ec)
96 {
97 if (SSL_get_app_data(ssl_))
98 delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
99
100 SSL_set_app_data(ssl_, callback);
101
102 ::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
103 &engine::verify_callback_function);
104
105 ec = boost::system::error_code();
106 return ec;
107 }
108
109 int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
110 {
111 if (ctx)
112 {
113 if (SSL* ssl = static_cast<SSL*>(
114 ::X509_STORE_CTX_get_ex_data(
115 ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
116 {
117 if (SSL_get_app_data(ssl))
118 {
119 verify_callback_base* callback =
120 static_cast<verify_callback_base*>(
121 SSL_get_app_data(ssl));
122
123 verify_context verify_ctx(ctx);
124 return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
125 }
126 }
127 }
128
129 return 0;
130 }
131
132 engine::want engine::handshake(
133 stream_base::handshake_type type, boost::system::error_code& ec)
134 {
135 return perform((type == boost::asio::ssl::stream_base::client)
136 ? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
137 }
138
139 engine::want engine::shutdown(boost::system::error_code& ec)
140 {
141 return perform(&engine::do_shutdown, 0, 0, ec, 0);
142 }
143
144 engine::want engine::write(const boost::asio::const_buffer& data,
145 boost::system::error_code& ec, std::size_t& bytes_transferred)
146 {
147 if (data.size() == 0)
148 {
149 ec = boost::system::error_code();
150 return engine::want_nothing;
151 }
152
153 return perform(&engine::do_write,
154 const_cast<void*>(data.data()),
155 data.size(), ec, &bytes_transferred);
156 }
157
158 engine::want engine::read(const boost::asio::mutable_buffer& data,
159 boost::system::error_code& ec, std::size_t& bytes_transferred)
160 {
161 if (data.size() == 0)
162 {
163 ec = boost::system::error_code();
164 return engine::want_nothing;
165 }
166
167 return perform(&engine::do_read, data.data(),
168 data.size(), ec, &bytes_transferred);
169 }
170
171 boost::asio::mutable_buffer engine::get_output(
172 const boost::asio::mutable_buffer& data)
173 {
174 int length = ::BIO_read(ext_bio_,
175 data.data(), static_cast<int>(data.size()));
176
177 return boost::asio::buffer(data,
178 length > 0 ? static_cast<std::size_t>(length) : 0);
179 }
180
181 boost::asio::const_buffer engine::put_input(
182 const boost::asio::const_buffer& data)
183 {
184 int length = ::BIO_write(ext_bio_,
185 data.data(), static_cast<int>(data.size()));
186
187 return boost::asio::buffer(data +
188 (length > 0 ? static_cast<std::size_t>(length) : 0));
189 }
190
191 const boost::system::error_code& engine::map_error_code(
192 boost::system::error_code& ec) const
193 {
194 // We only want to map the error::eof code.
195 if (ec != boost::asio::error::eof)
196 return ec;
197
198 // If there's data yet to be read, it's an error.
199 if (BIO_wpending(ext_bio_))
200 {
201 ec = boost::asio::ssl::error::stream_truncated;
202 return ec;
203 }
204
205 // SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
206 // underlying transport is passed through.
207 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
208 if (SSL_version(ssl_) == SSL2_VERSION)
209 return ec;
210 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
211
212 // Otherwise, the peer should have negotiated a proper shutdown.
213 if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
214 {
215 ec = boost::asio::ssl::error::stream_truncated;
216 }
217
218 return ec;
219 }
220
221 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
222 boost::asio::detail::static_mutex& engine::accept_mutex()
223 {
224 static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
225 return mutex;
226 }
227 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
228
229 engine::want engine::perform(int (engine::* op)(void*, std::size_t),
230 void* data, std::size_t length, boost::system::error_code& ec,
231 std::size_t* bytes_transferred)
232 {
233 std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
234 ::ERR_clear_error();
235 int result = (this->*op)(data, length);
236 int ssl_error = ::SSL_get_error(ssl_, result);
237 int sys_error = static_cast<int>(::ERR_get_error());
238 std::size_t pending_output_after = ::BIO_ctrl_pending(ext_bio_);
239
240 if (ssl_error == SSL_ERROR_SSL)
241 {
242 ec = boost::system::error_code(sys_error,
243 boost::asio::error::get_ssl_category());
244 return want_nothing;
245 }
246
247 if (ssl_error == SSL_ERROR_SYSCALL)
248 {
249 ec = boost::system::error_code(sys_error,
250 boost::asio::error::get_system_category());
251 return want_nothing;
252 }
253
254 if (result > 0 && bytes_transferred)
255 *bytes_transferred = static_cast<std::size_t>(result);
256
257 if (ssl_error == SSL_ERROR_WANT_WRITE)
258 {
259 ec = boost::system::error_code();
260 return want_output_and_retry;
261 }
262 else if (pending_output_after > pending_output_before)
263 {
264 ec = boost::system::error_code();
265 return result > 0 ? want_output : want_output_and_retry;
266 }
267 else if (ssl_error == SSL_ERROR_WANT_READ)
268 {
269 ec = boost::system::error_code();
270 return want_input_and_retry;
271 }
272 else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
273 {
274 ec = boost::asio::error::eof;
275 return want_nothing;
276 }
277 else
278 {
279 ec = boost::system::error_code();
280 return want_nothing;
281 }
282 }
283
284 int engine::do_accept(void*, std::size_t)
285 {
286 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
287 boost::asio::detail::static_mutex::scoped_lock lock(accept_mutex());
288 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
289 return ::SSL_accept(ssl_);
290 }
291
292 int engine::do_connect(void*, std::size_t)
293 {
294 return ::SSL_connect(ssl_);
295 }
296
297 int engine::do_shutdown(void*, std::size_t)
298 {
299 int result = ::SSL_shutdown(ssl_);
300 if (result == 0)
301 result = ::SSL_shutdown(ssl_);
302 return result;
303 }
304
305 int engine::do_read(void* data, std::size_t length)
306 {
307 return ::SSL_read(ssl_, data,
308 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
309 }
310
311 int engine::do_write(void* data, std::size_t length)
312 {
313 return ::SSL_write(ssl_, data,
314 length < INT_MAX ? static_cast<int>(length) : INT_MAX);
315 }
316
317 } // namespace detail
318 } // namespace ssl
319 } // namespace asio
320 } // namespace boost
321
322 #include <boost/asio/detail/pop_options.hpp>
323
324 #endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP