2008-01-27 23:39:50 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2008-2018, Arvid Norberg
|
2008-01-27 23:39:50 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_SSL_STREAM_HPP_INCLUDED
|
|
|
|
#define TORRENT_SSL_STREAM_HPP_INCLUDED
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2015-04-19 08:28:21 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/io_service.hpp"
|
2015-09-07 23:00:27 +02:00
|
|
|
#include "libtorrent/aux_/openssl.hpp"
|
2015-04-19 08:28:21 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/ssl.hpp>
|
2015-08-08 22:19:44 +02:00
|
|
|
#if defined TORRENT_BUILD_SIMULATOR
|
|
|
|
#include "simulator/simulator.hpp"
|
|
|
|
#endif
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-04-19 08:28:21 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2015-08-08 22:19:44 +02:00
|
|
|
namespace ssl {
|
2017-04-12 19:00:57 +02:00
|
|
|
|
2015-08-08 22:19:44 +02:00
|
|
|
#if defined TORRENT_BUILD_SIMULATOR
|
|
|
|
using sim::asio::ssl::context;
|
|
|
|
using sim::asio::ssl::stream_base;
|
|
|
|
using sim::asio::ssl::stream;
|
|
|
|
#else
|
|
|
|
using boost::asio::ssl::context;
|
|
|
|
using boost::asio::ssl::stream_base;
|
|
|
|
using boost::asio::ssl::stream;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
template <class Stream>
|
|
|
|
class ssl_stream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-08-08 22:19:44 +02:00
|
|
|
explicit ssl_stream(io_service& io_service, ssl::context& ctx)
|
2010-10-12 10:57:43 +02:00
|
|
|
: m_sock(io_service, ctx)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using sock_type = typename boost::asio::ssl::stream<Stream>;
|
|
|
|
using next_layer_type = typename sock_type::next_layer_type;
|
|
|
|
using lowest_layer_type = typename Stream::lowest_layer_type;
|
|
|
|
using endpoint_type = typename Stream::endpoint_type;
|
|
|
|
using protocol_type = typename Stream::protocol_type;
|
2017-12-14 01:37:27 +01:00
|
|
|
#if BOOST_VERSION >= 106600
|
2018-03-22 17:01:38 +01:00
|
|
|
using executor_type = typename sock_type::executor_type;
|
2017-12-14 01:37:27 +01:00
|
|
|
executor_type get_executor() { return m_sock.get_executor(); }
|
|
|
|
#endif
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
void set_host_name(std::string name)
|
2012-04-06 06:11:04 +02:00
|
|
|
{
|
2015-09-07 23:00:27 +02:00
|
|
|
aux::openssl_set_tlsext_hostname(m_sock.native_handle(), name.c_str());
|
2012-04-06 06:11:04 +02:00
|
|
|
}
|
2012-01-14 17:04:25 +01:00
|
|
|
|
2012-01-29 03:27:55 +01:00
|
|
|
template <class T>
|
|
|
|
void set_verify_callback(T const& fun, error_code& ec)
|
|
|
|
{ m_sock.set_verify_callback(fun, ec); }
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
SSL* native_handle() { return m_sock.native_handle(); }
|
|
|
|
|
2016-11-17 20:34:49 +01:00
|
|
|
using handler_type = std::function<void(error_code const&)>;
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
template <class Handler>
|
|
|
|
void async_connect(endpoint_type const& endpoint, Handler const& handler)
|
|
|
|
{
|
|
|
|
// the connect is split up in the following steps:
|
|
|
|
// 1. connect to peer
|
|
|
|
// 2. perform SSL client handshake
|
|
|
|
|
|
|
|
// to avoid unnecessary copying of the handler,
|
2008-05-03 18:05:42 +02:00
|
|
|
// store it in a shared_ptr
|
2016-11-17 20:34:49 +01:00
|
|
|
auto h = std::make_shared<handler_type>(handler);
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using std::placeholders::_1;
|
2008-01-27 23:39:50 +01:00
|
|
|
m_sock.next_layer().async_connect(endpoint
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&ssl_stream::connected, this, _1, h));
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
2011-09-10 07:52:07 +02:00
|
|
|
template <class Handler>
|
|
|
|
void async_accept_handshake(Handler const& handler)
|
|
|
|
{
|
|
|
|
// this is used for accepting SSL connections
|
2016-11-17 20:34:49 +01:00
|
|
|
auto h = std::make_shared<handler_type>(handler);
|
2016-05-25 06:31:52 +02:00
|
|
|
using std::placeholders::_1;
|
2015-08-08 22:19:44 +02:00
|
|
|
m_sock.async_handshake(ssl::stream_base::server
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&ssl_stream::handshake, this, _1, h));
|
2011-09-10 07:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void accept_handshake(error_code& ec)
|
|
|
|
{
|
|
|
|
// this is used for accepting SSL connections
|
2015-08-08 22:19:44 +02:00
|
|
|
m_sock.handshake(ssl::stream_base::server, ec);
|
2011-09-10 07:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Handler>
|
|
|
|
void async_shutdown(Handler const& handler)
|
|
|
|
{
|
2014-10-06 05:03:01 +02:00
|
|
|
error_code ec;
|
|
|
|
m_sock.next_layer().cancel(ec);
|
2012-01-14 17:04:25 +01:00
|
|
|
m_sock.async_shutdown(handler);
|
2011-09-10 07:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown(error_code& ec)
|
|
|
|
{
|
|
|
|
m_sock.shutdown(ec);
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
template <class Mutable_Buffers, class Handler>
|
|
|
|
void async_read_some(Mutable_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
m_sock.async_read_some(buffers, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers, error_code& ec)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
return m_sock.read_some(buffers, ec);
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class SettableSocketOption>
|
|
|
|
void set_option(SettableSocketOption const& opt)
|
|
|
|
{
|
|
|
|
m_sock.next_layer().set_option(opt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
|
|
|
error_code set_option(SettableSocketOption const& opt, error_code& ec)
|
|
|
|
{
|
|
|
|
return m_sock.next_layer().set_option(opt, ec);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:02:24 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class GettableSocketOption>
|
|
|
|
void get_option(GettableSocketOption& opt)
|
|
|
|
{
|
2013-09-03 20:14:17 +02:00
|
|
|
m_sock.next_layer().get_option(opt);
|
2013-09-03 18:02:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class GettableSocketOption>
|
|
|
|
error_code get_option(GettableSocketOption& opt, error_code& ec)
|
|
|
|
{
|
2013-09-03 20:14:17 +02:00
|
|
|
return m_sock.next_layer().get_option(opt, ec);
|
2013-09-03 18:02:24 +02:00
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class Mutable_Buffers>
|
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers)
|
|
|
|
{
|
|
|
|
return m_sock.read_some(buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IO_Control_Command>
|
|
|
|
void io_control(IO_Control_Command& ioc)
|
|
|
|
{
|
|
|
|
m_sock.next_layer().io_control(ioc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class IO_Control_Command>
|
2008-05-03 18:05:42 +02:00
|
|
|
void io_control(IO_Control_Command& ioc, error_code& ec)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
m_sock.next_layer().io_control(ioc, ec);
|
|
|
|
}
|
|
|
|
|
2017-12-14 01:37:27 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
void non_blocking(bool b) { m_sock.next_layer().non_blocking(b); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
error_code non_blocking(bool b, error_code& ec)
|
|
|
|
{ return m_sock.next_layer().non_blocking(b, ec); }
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
template <class Const_Buffers, class Handler>
|
|
|
|
void async_write_some(Const_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
m_sock.async_write_some(buffers, handler);
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
template <class Const_Buffers>
|
|
|
|
std::size_t write_some(Const_Buffers const& buffers, error_code& ec)
|
|
|
|
{
|
|
|
|
return m_sock.write_some(buffers, ec);
|
|
|
|
}
|
|
|
|
|
2014-10-08 17:27:52 +02:00
|
|
|
// the SSL stream may cache 17 kiB internally, and there's no way of
|
|
|
|
// asking how large its buffer is. 17 kiB isn't very much though, so it
|
|
|
|
// seems fine to potentially over-estimate the number of bytes available.
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
std::size_t available() const
|
2014-10-08 17:27:52 +02:00
|
|
|
{ return 17 * 1024 + const_cast<sock_type&>(m_sock).next_layer().available(); }
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::size_t available(error_code& ec) const
|
2014-10-08 17:27:52 +02:00
|
|
|
{ return 17 * 1024 + const_cast<sock_type&>(m_sock).next_layer().available(ec); }
|
2010-10-12 10:57:43 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
void bind(endpoint_type const& endpoint)
|
|
|
|
{
|
|
|
|
m_sock.next_layer().bind(endpoint);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void bind(endpoint_type const& endpoint, error_code& ec)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
m_sock.next_layer().bind(endpoint, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
void open(protocol_type const& p)
|
|
|
|
{
|
|
|
|
m_sock.next_layer().open(p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void open(protocol_type const& p, error_code& ec)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
m_sock.next_layer().open(p, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_open() const
|
|
|
|
{
|
|
|
|
return const_cast<sock_type&>(m_sock).next_layer().is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
m_sock.next_layer().close();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void close(error_code& ec)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
m_sock.next_layer().close(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
endpoint_type remote_endpoint() const
|
|
|
|
{
|
|
|
|
return const_cast<sock_type&>(m_sock).next_layer().remote_endpoint();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
endpoint_type remote_endpoint(error_code& ec) const
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
return const_cast<sock_type&>(m_sock).next_layer().remote_endpoint(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
endpoint_type local_endpoint() const
|
|
|
|
{
|
|
|
|
return const_cast<sock_type&>(m_sock).next_layer().local_endpoint();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
endpoint_type local_endpoint(error_code& ec) const
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
return const_cast<sock_type&>(m_sock).next_layer().local_endpoint(ec);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& get_io_service()
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
return m_sock.get_io_service();
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lowest_layer_type& lowest_layer()
|
|
|
|
{
|
|
|
|
return m_sock.lowest_layer();
|
|
|
|
}
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
next_layer_type& next_layer()
|
|
|
|
{
|
2011-09-10 07:52:07 +02:00
|
|
|
return m_sock.next_layer();
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-08-31 18:45:45 +02:00
|
|
|
void connected(error_code const& e, std::shared_ptr<handler_type> h)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
(*h)(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using std::placeholders::_1;
|
2015-08-08 22:19:44 +02:00
|
|
|
m_sock.async_handshake(ssl::stream_base::client
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&ssl_stream::handshake, this, _1, h));
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 18:45:45 +02:00
|
|
|
void handshake(error_code const& e, std::shared_ptr<handler_type> h)
|
2008-01-27 23:39:50 +01:00
|
|
|
{
|
|
|
|
(*h)(e);
|
|
|
|
}
|
|
|
|
|
2015-08-08 22:19:44 +02:00
|
|
|
ssl::stream<Stream> m_sock;
|
2008-01-27 23:39:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif // TORRENT_USE_OPENSSL
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#endif
|