2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "core/socket/stream/SocketConnector.hpp"
43#include "core/socket/stream/tls/SocketConnector.h"
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
47#include "core/socket/stream/tls/ssl_utils.h"
48#include "log/Logger.h"
51#include <openssl/ssl.h>
52#include <openssl/x509v3.h>
59 template <
typename PhysicalClientSocket,
typename Config>
66 const std::shared_ptr<
Config>& config)
69 onConnect(socketConnection);
71 SSL* ssl = socketConnection->startSSL(socketConnection->getFd(),
Super::config->getSslCtx());
73 SSL_set_connect_state(ssl);
74 SSL_set_ex_data(ssl, 1,
Super::config.get());
76 ssl_set_sni(ssl,
Super::config->getSni());
80 LOG(TRACE) << socketConnection->getConnectionName() <<
" SSL/TLS: Start handshake";
81 if (!socketConnection->doSSLHandshake(
82 [socketContextFactory,
85 LOG(DEBUG) << socketConnection->getConnectionName() <<
" SSL/TLS: Handshake success";
87 onConnected(socketConnection);
89 socketConnection->setSocketContext(socketContextFactory);
91 [socketConnection]() {
92 LOG(ERROR) << socketConnection->getConnectionName() <<
" SSL/TLS: Handshake timed out";
94 socketConnection->close();
96 [socketConnection](
int sslErr) {
97 ssl_log(socketConnection->getConnectionName() +
" SSL/TLS: Handshake failed", sslErr);
99 socketConnection->close();
101 LOG(ERROR) << socketConnection->getConnectionName() +
" SSL/TLS: Handshake failed";
103 socketConnection->close();
107 socketConnection->stopSSL();
108 onDisconnect(socketConnection);
114 template <
typename PhysicalSocketServer,
typename Config>
116 :
Super(socketConnector) {
119 template <
typename PhysicalClientSocket,
typename Config>
124 template <
typename PhysicalSocketClient,
typename Config>
127 LOG(TRACE) << config->getInstanceName() <<
" SSL/TLS: SSL_CTX creating ...";
129 if (config->getSslCtx() !=
nullptr) {
130 LOG(DEBUG) << config->getInstanceName() <<
" SSL/TLS: SSL_CTX created";
134 LOG(ERROR) << config->getInstanceName() <<
" SSL/TLS: SSL_CTX creation failed";
136 Super::onStatus(config->Remote::getSocketAddress(), core::socket::STATE_FATAL);
utils::Timeval sslShutdownTimeout
SocketConnection(PhysicalSocket &&physicalSocket, const std::function< void(SocketConnection *)> &onDisconnect, const std::shared_ptr< Config > &config)
typename Super::SocketReader SocketReader
SSL * startSSL(int fd, SSL_CTX *ctx)
core::socket::stream:: SocketConnectionT< PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT > Super
void doWriteShutdown(const std::function< void()> &onShutdown) final
utils::Timeval sslInitTimeout
void onReadShutdown() final
PhysicalSocketT PhysicalSocket
bool doSSLHandshake(const std::function< void()> &onSuccess, const std::function< void()> &onTimeout, const std::function< void(int)> &onStatus) final
typename Super::SocketWriter SocketWriter
core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, core::socket::stream::tls::SocketConnection > Super
typename Super::SocketAddress SocketAddress
void useNextSocketAddress() override
SocketConnector(const SocketConnector &socketConnector)
typename Super::SocketConnection SocketConnection
typename Super::Config Config
SocketConnector(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
static void doHandshake(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)
static void doShutdown(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)