2#if defined(HAVE_CONFIG_H)
10#include "../AsyncTlsSocketBase.hxx"
11#include <openssl/ssl.h>
13#include "../ReTurnSubsystem.hxx"
15#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
23static asio::ssl::context
26 asio::ssl::context context(asio::ssl::context::sslv23);
31 context.set_verify_mode(asio::ssl::context::verify_peer |
32 asio::ssl::context::verify_fail_if_no_peer_cert);
36 context.load_verify_file(rootCertificatesFile, ec);
39 ErrLog(<<
"Unable to load TLS root certificates from " << rootCertificatesFile <<
", error=" << ec.value() <<
"(" << ec.message() <<
")");
45 const asio::ip::address& address,
47 const std::string& rootCertificatesFile) :
50 mSocket(new
asio::ssl::stream<
asio::ip::tcp::socket>(mIOService, mSslContext)),
51 mValidateServerCertificateHostname(validateServerCertificateHostname)
63 asio::error_code errorCode;
64 asio::ip::tcp::resolver::results_type results = resolver.resolve(
protocol(), address, service.
c_str(), errorCode);
69 asio::ip::tcp::resolver::results_type::const_iterator endpoint_iterator = results.begin();
70 asio::ip::tcp::resolver::results_type::const_iterator end = results.end();
73 errorCode = asio::error::host_not_found;
74 while (errorCode && endpoint_iterator != end)
78 mSocket.reset(
new asio::ssl::stream<asio::ip::tcp::socket>(
mIOService, mSslContext));
83 return asio::error::invalid_argument;
87 const std::function<void()> closeSocket = [
this]() { asio::error_code ignored; mSocket->lowest_layer().close(ignored); };
91 asio::error_code result = asio::error::would_block;
92 mSocket->lowest_layer().async_connect(*endpoint_iterator, [&result](
const asio::error_code& ec) { result = ec; });
98 asio::error_code result = asio::error::would_block;
99 mSocket->async_handshake(asio::ssl::stream_base::client, [&result](
const asio::error_code& ec) { result = ec; });
123 asio::error_code errorCode;
124 asio::write(*mSocket, asio::buffer(buffer, size), asio::transfer_all(), errorCode);
131 asio::error_code errorCode;
132 asio::write(*mSocket, buffers, asio::transfer_all(), errorCode);
139 asio::async_read(*mSocket, asio::buffer(
mReadBuffer, 4),
146 asio::async_read(*mSocket, asio::buffer(&
mReadBuffer[4], len),
154 mSocket->lowest_layer().cancel(ec);
static bool setupClientSsl(SSL *ssl, const std::string &hostname, bool validateHostname)
Sets up a client SSL to connect to hostname (a host name or IP address): SNI for a host name,...
static void logClientHandshakeResult(SSL *ssl, const std::string &hostname, const asio::error_code &ec)
Logs a client handshake's outcome: the TLS version and cipher, or why the server certificate was reje...
void setPort(unsigned int port)
void setAddress(const asio::ip::address &address)
void setTransportType(TransportType transport)
void handleRawRead(const asio::error_code &errorCode, size_t bytesRead)
asio::io_context mIOService
StunTuple mConnectedTuple
asio::error_code waitForConnectStep(asio::error_code &result, const std::function< void()> &cancel)
For a step of connect() started as an asynchronous operation that sets result (initially asio::error:...
virtual void handleReadHeader(const asio::error_code &e)
Handle completion of a read operation for first 32 bits.
asio::ip::tcp protocol() const
The protocol (IPv4 or IPv6) of the local binding.
asio::error_code openAndBind(asio::ip::tcp::socket &socket)
Opens socket and binds it to the local binding (address and port), for a connection attempt.
virtual void cancelSocket()
virtual asio::error_code rawWrite(const char *buffer, unsigned int size)
virtual void readBody(unsigned int len)
TurnTlsSocket(bool validateServerCertificateHostname, const asio::ip::address &address=UnspecifiedIpAddress, unsigned short port=0, const std::string &rootCertificatesFile="ca.pem")
validateServerCertificateHostname: check the server certificate is for the host name (or IP address) ...
virtual asio::error_code connect(const std::string &address, unsigned short port)
virtual void readHeader()
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
const char * c_str() const
Returns a null-terminated string representing.
static asio::ssl::context makeClientContext(const std::string &rootCertificatesFile)