mirror of
https://github.com/monero-project/monero.git
synced 2025-06-19 05:44:12 -04:00
epee: add SSL support
RPC connections now have optional tranparent SSL. An optional private key and certificate file can be passed, using the --{rpc,daemon}-ssl-private-key and --{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 --{rpc}-ssl, which accepts autodetect (default), disabled or enabled. Access can be restricted to particular certificates using the --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.
This commit is contained in:
parent
31bdf7bd11
commit
2456945408
25 changed files with 1065 additions and 239 deletions
|
@ -47,8 +47,10 @@
|
|||
#include <memory>
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
|
||||
#include "net/net_utils_base.h"
|
||||
#include "net/net_ssl.h"
|
||||
#include "syncobj.h"
|
||||
|
||||
namespace epee
|
||||
|
@ -95,16 +97,56 @@ 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_;
|
||||
|
||||
public:
|
||||
// first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
|
||||
connection_basic(boost::asio::ip::tcp::socket&& socket, boost::shared_ptr<socket_stats> stats);
|
||||
connection_basic(boost::asio::ip::tcp::socket&& socket, boost::shared_ptr<socket_stats> stats, ssl_support_t ssl_support, ssl_context_t &ssl_context);
|
||||
connection_basic(boost::asio::io_service &io_service, boost::shared_ptr<socket_stats> stats, ssl_support_t ssl_support, ssl_context_t &ssl_context);
|
||||
|
||||
virtual ~connection_basic() noexcept(false);
|
||||
|
||||
//! \return `socket_stats` object passed in construction (ptr never changes).
|
||||
socket_stats& get_stats() noexcept { return *m_stats; /* verified in constructor */ }
|
||||
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);
|
||||
|
||||
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 = epee::net_utils::ssl_support_t::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 == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
||||
socket_.async_read_some(buffers, std::forward<ReadHandler>(handler));
|
||||
else
|
||||
socket().async_read_some(buffers, std::forward<ReadHandler>(handler));
|
||||
}
|
||||
|
||||
template<typename ConstBufferSequence, typename WriteHandler>
|
||||
void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
|
||||
{
|
||||
if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
||||
socket_.async_write_some(buffers, std::forward<WriteHandler>(handler));
|
||||
else
|
||||
socket().async_write_some(buffers, std::forward<WriteHandler>(handler));
|
||||
}
|
||||
|
||||
template<typename ConstBufferSequence, typename WriteHandler>
|
||||
void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
|
||||
{
|
||||
if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
||||
boost::asio::async_write(socket_, buffers, std::forward<WriteHandler>(handler));
|
||||
else
|
||||
boost::asio::async_write(socket(), buffers, std::forward<WriteHandler>(handler));
|
||||
}
|
||||
|
||||
// various handlers to be called from connection class:
|
||||
void do_send_handler_write(const void * ptr , size_t cb);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue