SNode.C
Loading...
Searching...
No Matches
SocketAcceptor.hpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "core/socket/stream/SocketAcceptor.hpp"
43#include "core/socket/stream/tls/SocketAcceptor.h"
44
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
47#include "core/socket/stream/tls/ssl_utils.h"
48#include "log/Logger.h"
49
50#include <algorithm>
51#include <openssl/ssl.h>
52#include <string>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56namespace core::socket::stream::tls {
57
58 template <typename PhysicalServerSocket, typename Config>
59 SocketAcceptor<PhysicalServerSocket, Config>::SocketAcceptor(
60 const std::shared_ptr<SocketContextFactory>& socketContextFactory,
61 const std::function<void(SocketConnection*)>& onConnect,
62 const std::function<void(SocketConnection*)>& onConnected,
63 const std::function<void(SocketConnection*)>& onDisconnect,
64 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
65 const std::shared_ptr<Config>& config)
66 : Super(
67 [onConnect, this](SocketConnection* socketConnection) { // onConnect
68 onConnect(socketConnection);
69
70 SSL* ssl = socketConnection->startSSL(socketConnection->getFd(), Super::config->getSslCtx());
71 if (ssl != nullptr) {
72 SSL_set_accept_state(ssl);
73 SSL_set_ex_data(ssl, 1, Super::config.get());
74 }
75 },
76 [socketContextFactory, onConnected](SocketConnection* socketConnection) { // on Connected
77 LOG(TRACE) << socketConnection->getConnectionName() << " SSL/TLS: Start handshake";
78 if (!socketConnection->doSSLHandshake(
79 [socketContextFactory,
80 onConnected,
81 socketConnection]() { // onSuccess
82 LOG(DEBUG) << socketConnection->getConnectionName() << " SSL/TLS: Handshake success";
83
84 onConnected(socketConnection);
85
86 socketConnection->setSocketContext(socketContextFactory);
87 },
88 [socketConnection]() { // onTimeout
89 LOG(ERROR) << socketConnection->getConnectionName() << "SSL/TLS: Handshake timed out";
90
91 socketConnection->close();
92 },
93 [socketConnection](int sslErr) { //
94 ssl_log(socketConnection->getConnectionName() + " SSL/TLS: Handshake failed", sslErr);
95
96 socketConnection->close();
97 })) {
98 LOG(ERROR) << socketConnection->getConnectionName() + " SSL/TLS: Handshake failed";
99
100 socketConnection->close();
101 }
102 },
103 [onDisconnect, instanceName = config->getInstanceName()](SocketConnection* socketConnection) { // onDisconnect
104 socketConnection->stopSSL();
105 onDisconnect(socketConnection);
106 },
107 onStatus,
108 config) {
109 }
110
111 template <typename PhysicalSocketServer, typename Config>
112 SocketAcceptor<PhysicalSocketServer, Config>::SocketAcceptor(const SocketAcceptor& socketAcceptor)
113 : Super(socketAcceptor) {
114 }
115
116 template <typename PhysicalClientSocket, typename Config>
117 void SocketAcceptor<PhysicalClientSocket, Config>::useNextSocketAddress() {
118 new SocketAcceptor(*this);
119 }
120
121 template <typename PhysicalSocketServer, typename Config>
122 void SocketAcceptor<PhysicalSocketServer, Config>::init() {
123 if (core::eventLoopState() == core::State::RUNNING && !config->getDisabled()) {
124 LOG(TRACE) << config->getInstanceName() << " SSL/TLS: SSL_CTX creating ...";
125 SSL_CTX* sslCtx = config->getSslCtx();
126
127 if (sslCtx != nullptr) {
128 LOG(DEBUG) << config->getInstanceName() << " SSL/TLS: SSL_CTX created";
129
130 SSL_CTX_set_client_hello_cb(sslCtx, clientHelloCallback, nullptr);
131
132 Super::init();
133 } else {
134 LOG(ERROR) << config->getInstanceName() << " SSL/TLS: SSL/TLS creation failed";
135
136 Super::onStatus(Super::config->Local::getSocketAddress(), core::socket::STATE_ERROR);
137 Super::destruct();
138 }
139 } else {
140 Super::init();
141 }
142 }
143
144 template <typename PhysicalSocketServer, typename Config>
145 int SocketAcceptor<PhysicalSocketServer, Config>::clientHelloCallback(SSL* ssl, int* al, [[maybe_unused]] void* arg) {
146 int ret = SSL_CLIENT_HELLO_SUCCESS;
147
148 const std::string connectionName = *static_cast<std::string*>(SSL_get_ex_data(ssl, 0));
149 Config* config = static_cast<Config*>(SSL_get_ex_data(ssl, 1));
150
151 std::string serverNameIndication = core::socket::stream::tls::ssl_get_servername_from_client_hello(ssl);
152
153 if (!serverNameIndication.empty()) {
154 SSL_CTX* sniSslCtx = config->getSniCtx(serverNameIndication);
155
156 if (sniSslCtx != nullptr) {
157 LOG(DEBUG) << connectionName << " SSL/TLS: Setting sni certificate for '" << serverNameIndication << "'";
158 core::socket::stream::tls::ssl_set_ssl_ctx(ssl, sniSslCtx);
159 } else if (config->getForceSni()) {
160 LOG(ERROR) << connectionName << " SSL/TLS: No sni certificate found for '" << serverNameIndication
161 << "' but forceSni set - terminating";
162 ret = SSL_CLIENT_HELLO_ERROR;
163 *al = SSL_AD_UNRECOGNIZED_NAME;
164 } else {
165 LOG(WARNING) << connectionName << " SSL/TLS: No sni certificate found for '" << serverNameIndication
166 << "'. Still using master certificate";
167 }
168 } else {
169 LOG(DEBUG) << connectionName << " SSL/TLS: No sni certificate requested from client. Still using master certificate";
170 }
171
172 return ret;
173 }
174
175} // namespace core::socket::stream::tls
typename Super::SocketAddress SocketAddress
static int clientHelloCallback(SSL *ssl, int *al, void *arg)
typename Super::SocketConnection SocketConnection
SocketAcceptor(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)
SocketAcceptor(const SocketAcceptor &socketAcceptor)
core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, core::socket::stream::tls::SocketConnection > Super
SSL_CTX * ssl_set_ssl_ctx(SSL *ssl, SSL_CTX *sslCtx)
std::string ssl_get_servername_from_client_hello(SSL *ssl)
State
Definition State.h:51
@ RUNNING
Definition State.h:51
State eventLoopState()
Definition State.cpp:52