Permalink
Browse files

epee: add SSL support

P2P and RPC connections now have optional tranparent SSL.

An optional private key and certificate file can be passed,
using the --{p2p,rpc,daemon}-ssl-private-key and
--{p2p,rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.

SSL can be enabled or disabled using --{p2p,rpc}-ssl, which
accepts autodetect (default), disabled or enabled.

Access can be restricted to particular certificates using the
--{p2p,rpc}-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.

To generate long term certificates:

openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT

/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.

SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
  • Loading branch information...
moneromooo-monero committed Jun 14, 2018
1 parent 31f47d7 commit 535c72d4ae334c12e4a5e681a2133caf0d6883e2
@@ -46,6 +46,7 @@
#include <memory>

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/array.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
@@ -94,12 +95,12 @@ namespace net_utils
typename t_protocol_handler::config_type& config,
std::atomic<long> &ref_sock_count, // the ++/-- counter
std::atomic<long> &sock_number, // the only increasing ++ number generator
i_connection_filter * &pfilter
,t_connection_type connection_type);
i_connection_filter * &pfilter,
t_connection_type connection_type,
epee::net_utils::ssl_support_t ssl_support,
ssl_context_t &ssl_context);

virtual ~connection() noexcept(false);
/// Get the socket associated with the connection.
boost::asio::ip::tcp::socket& socket();

/// Start the first asynchronous operation for the connection.
bool start(bool is_income, bool is_multithreaded);
@@ -129,6 +130,10 @@ namespace net_utils
//------------------------------------------------------
boost::shared_ptr<connection<t_protocol_handler> > safe_shared_from_this();
bool shutdown();
/// Handle completion of a receive operation.
void handle_receive(const boost::system::error_code& e,
std::size_t bytes_transferred);

/// Handle completion of a read operation.
void handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred);
@@ -197,8 +202,8 @@ namespace net_utils
std::map<std::string, t_connection_type> server_type_map;
void create_server_type_map();

bool init_server(uint32_t port, const std::string address = "0.0.0.0");
bool init_server(const std::string port, const std::string& address = "0.0.0.0");
bool init_server(uint32_t port, const std::string address = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect, const std::string &private_key_path = std::string(), const std::string &certificate_path = std::string(), const std::list<std::string> &allowed_certificates = {});
bool init_server(const std::string port, const std::string& address = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect, const std::string &private_key_path = std::string(), const std::string &certificate_path = std::string(), const std::list<std::string> &allowed_certificates = {});

/// Run the server's io_service loop.
bool run_server(size_t threads_count, bool wait = true, const boost::thread::attributes& attrs = boost::thread::attributes());
@@ -219,9 +224,9 @@ namespace net_utils

void set_connection_filter(i_connection_filter* pfilter);

bool connect(const std::string& adr, const std::string& port, uint32_t conn_timeot, t_connection_context& cn, const std::string& bind_ip = "0.0.0.0");
bool connect(const std::string& adr, const std::string& port, uint32_t conn_timeot, t_connection_context& cn, const std::string& bind_ip = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect);
template<class t_callback>
bool connect_async(const std::string& adr, const std::string& port, uint32_t conn_timeot, const t_callback &cb, const std::string& bind_ip = "0.0.0.0");
bool connect_async(const std::string& adr, const std::string& port, uint32_t conn_timeot, const t_callback &cb, const std::string& bind_ip = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect);

typename t_protocol_handler::config_type& get_config_object(){return m_config;}

@@ -329,6 +334,9 @@ namespace net_utils
boost::mutex connections_mutex;
std::set<connection_ptr> connections_;

ssl_context_t m_ssl_context;
std::list<std::string> m_allowed_certificates;

}; // class <>boosted_tcp_server


Large diffs are not rendered by default.

Oops, something went wrong.
@@ -50,6 +50,7 @@
#include <atomic>

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/array.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
@@ -60,6 +61,7 @@
#include <memory>

#include "net/net_utils_base.h"
#include "net/net_ssl.h"
#include "syncobj.h"

namespace epee
@@ -79,7 +81,7 @@ class connection_basic_pimpl; // PIMPL for this class
e_connection_type_RPC = 1, // the rpc commands (probably not rate limited, not chunked, etc)
e_connection_type_P2P = 2 // to other p2p node (probably limited)
};

std::string to_string(t_connection_type type);

class connection_basic { // not-templated base class for rapid developmet of some code parts
@@ -96,15 +98,53 @@ class connection_basic { // not-templated base class for rapid developmet of som
/// Strand to ensure the connection's handlers are not called concurrently.
boost::asio::io_service::strand strand_;
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
ssl_context_t &m_ssl_context;
ssl_support_t m_ssl_support;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;

std::atomic<long> &m_ref_sock_count; // reference to external counter of existing sockets that we will ++/--
public:
// first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
connection_basic(boost::asio::io_service& io_service, std::atomic<long> &ref_sock_count, std::atomic<long> &sock_number);
connection_basic(boost::asio::io_service& io_service, std::atomic<long> &ref_sock_count, std::atomic<long> &sock_number, ssl_support_t ssl, ssl_context_t &ssl_context);

virtual ~connection_basic() noexcept(false);

boost::asio::ip::tcp::socket& socket() { return socket_.next_layer(); }
ssl_support_t get_ssl_support() const { return m_ssl_support; }
void disable_ssl() { m_ssl_support = e_ssl_support_disabled; }

bool handshake(boost::asio::ssl::stream_base::handshake_type type)
{
return ssl_handshake(socket_, type, m_ssl_context);
}

template<typename MutableBufferSequence, typename ReadHandler>
void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
{
if (m_ssl_support == e_ssl_support_enabled)
socket_.async_read_some(buffers, handler);
else
socket().async_read_some(buffers, handler);
}

template<typename ConstBufferSequence, typename WriteHandler>
void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
{
if (m_ssl_support == e_ssl_support_enabled)
socket_.async_write_some(buffers, handler);
else
socket().async_write_some(buffers, handler);
}

template<typename ConstBufferSequence, typename WriteHandler>
void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
{
if (m_ssl_support == e_ssl_support_enabled)
boost::asio::async_write(socket_, buffers, handler);
else
boost::asio::async_write(socket(), buffers, handler);
}

// various handlers to be called from connection class:
void do_send_handler_write(const void * ptr , size_t cb);
void do_send_handler_write_from_queue(const boost::system::error_code& e, size_t cb , int q_len); // from handle_write, sending next part
@@ -275,7 +275,10 @@ namespace net_utils
chunked_state m_chunked_state;
std::string m_chunked_cache;
critical_section m_lock;
bool m_ssl;
epee::net_utils::ssl_support_t m_ssl_support;
std::string m_ssl_private_key;
std::string m_ssl_certificate;
std::list<std::string> m_ssl_allowed_certificates;

public:
explicit http_simple_client_template()
@@ -293,35 +296,39 @@ namespace net_utils
, m_chunked_state()
, m_chunked_cache()
, m_lock()
, m_ssl(false)
, m_ssl_support(epee::net_utils::e_ssl_support_autodetect)
{}

const std::string &get_host() const { return m_host_buff; };
const std::string &get_port() const { return m_port; };

bool set_server(const std::string& address, boost::optional<login> user, bool ssl = false)
bool set_server(const std::string& address, boost::optional<login> user, epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect, const std::string &private_key = std::string(), const std::string &certificate = std::string(), const std::list<std::string> &allowed_ssl_certificates = {})
{
http::url_content parsed{};
const bool r = parse_url(address, parsed);
CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address);
set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), ssl);
set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), ssl_support, private_key, certificate, allowed_ssl_certificates);
return true;
}

void set_server(std::string host, std::string port, boost::optional<login> user, bool ssl = false)
void set_server(std::string host, std::string port, boost::optional<login> user, epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect, const std::string &private_key = std::string(), const std::string &certificate = std::string(), const std::list<std::string> &allowed_ssl_certificates = {})
{
CRITICAL_REGION_LOCAL(m_lock);
disconnect();
m_host_buff = std::move(host);
m_port = std::move(port);
m_auth = user ? http_client_auth{std::move(*user)} : http_client_auth{};
m_ssl = ssl;
m_ssl_support = ssl_support;
m_ssl_private_key = private_key;
m_ssl_certificate = certificate;
m_ssl_allowed_certificates = allowed_ssl_certificates;
m_net_client.set_ssl(m_ssl_support, m_ssl_private_key, m_ssl_certificate, m_ssl_allowed_certificates);
}

bool connect(std::chrono::milliseconds timeout)
{
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.connect(m_host_buff, m_port, timeout, m_ssl);
return m_net_client.connect(m_host_buff, m_port, timeout, "0.0.0.0");
}
//---------------------------------------------------------------------------
bool disconnect()
@@ -330,10 +337,10 @@ namespace net_utils
return m_net_client.disconnect();
}
//---------------------------------------------------------------------------
bool is_connected()
bool is_connected(bool *ssl = NULL)
{
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.is_connected();
return m_net_client.is_connected(ssl);
}
//---------------------------------------------------------------------------
virtual bool handle_target_data(std::string& piece_of_transfer)
@@ -57,7 +57,10 @@ namespace epee

bool init(std::function<void(size_t, uint8_t*)> rng, const std::string& bind_port = "0", const std::string& bind_ip = "0.0.0.0",
std::vector<std::string> access_control_origins = std::vector<std::string>(),
boost::optional<net_utils::http::login> user = boost::none)
boost::optional<net_utils::http::login> user = boost::none,
epee::net_utils::ssl_support_t ssl_support = epee::net_utils::e_ssl_support_autodetect,
const std::string &private_key_path = std::string(), const std::string &certificate_path = std::string(),
const std::list<std::string> &allowed_certificates = std::list<std::string>())
{

//set self as callback handler
@@ -74,7 +77,7 @@ namespace epee
m_net_server.get_config_object().m_user = std::move(user);

MGINFO("Binding on " << bind_ip << ":" << bind_port);
bool res = m_net_server.init_server(bind_port, bind_ip);
bool res = m_net_server.init_server(bind_port, bind_ip, ssl_support, private_key_path, certificate_path, allowed_certificates);
if(!res)
{
LOG_ERROR("Failed to bind server");
Oops, something went wrong.

0 comments on commit 535c72d

Please sign in to comment.