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 socketContextFactory,
68 [onConnect, this](SocketConnection* socketConnection) { // onConnect
69 onConnect(socketConnection);
70
71 SSL* ssl = socketConnection->startSSL(socketConnection->getFd(),
72 Super::config->getSslCtx(),
73 Super::config->getInitTimeout(),
74 Super::config->getShutdownTimeout(),
75 !Super::config->getNoCloseNotifyIsEOF());
76 if (ssl != nullptr) {
77 SSL_set_accept_state(ssl);
78 SSL_set_ex_data(ssl, 1, Super::config.get());
79 }
80 },
81 [socketContextFactory, onConnected](SocketConnection* socketConnection) { // on Connected
82 LOG(TRACE) << socketConnection->getConnectionName() << " SSL/TLS: Start handshake";
83 if (!socketConnection->doSSLHandshake(
84 [socketContextFactory,
85 onConnected,
86 socketConnection]() { // onSuccess
87 LOG(DEBUG) << socketConnection->getConnectionName() << " SSL/TLS: Handshake success";
88
89 onConnected(socketConnection);
90
91 socketConnection->connectSocketContext(socketContextFactory);
92 },
93 [socketConnection]() { // onTimeout
94 LOG(ERROR) << socketConnection->getConnectionName() << "SSL/TLS: Handshake timed out";
95
96 socketConnection->close();
97 },
98 [socketConnection](int sslErr) { //
99 ssl_log(socketConnection->getConnectionName() + " SSL/TLS: Handshake failed", sslErr);
100
101 socketConnection->close();
102 })) {
103 LOG(ERROR) << socketConnection->getConnectionName() + " SSL/TLS: Handshake failed";
104
105 socketConnection->close();
106 }
107 },
108 [onDisconnect, instanceName = config->getInstanceName()](SocketConnection* socketConnection) { // onDisconnect
109 socketConnection->stopSSL();
110 onDisconnect(socketConnection);
111 },
112 onStatus,
113 config) {
114 }
115
116 template <typename PhysicalSocketServer, typename Config>
117 SocketAcceptor<PhysicalSocketServer, Config>::SocketAcceptor(const SocketAcceptor& socketAcceptor)
118 : Super(socketAcceptor) {
119 }
120
121 template <typename PhysicalClientSocket, typename Config>
122 void SocketAcceptor<PhysicalClientSocket, Config>::useNextSocketAddress() {
123 new SocketAcceptor(*this);
124 }
125
126 template <typename PhysicalSocketServer, typename Config>
127 void SocketAcceptor<PhysicalSocketServer, Config>::init() {
128 if (core::eventLoopState() == core::State::RUNNING && !config->getDisabled()) {
129 LOG(TRACE) << config->getInstanceName() << " SSL/TLS: SSL_CTX creating ...";
130 SSL_CTX* sslCtx = config->getSslCtx();
131
132 if (sslCtx != nullptr) {
133 LOG(DEBUG) << config->getInstanceName() << " SSL/TLS: SSL_CTX created";
134
135 SSL_CTX_set_client_hello_cb(sslCtx, clientHelloCallback, nullptr);
136
137 Super::init();
138 } else {
139 LOG(ERROR) << config->getInstanceName() << " SSL/TLS: SSL/TLS creation failed";
140
141 Super::onStatus(Super::config->Local::getSocketAddress(), core::socket::STATE_ERROR);
142 Super::destruct();
143 }
144 } else {
145 Super::init();
146 }
147 }
148
149 template <typename PhysicalSocketServer, typename Config>
150 int SocketAcceptor<PhysicalSocketServer, Config>::clientHelloCallback(SSL* ssl, int* al, [[maybe_unused]] void* arg) {
151 int ret = SSL_CLIENT_HELLO_SUCCESS;
152
153 const std::string connectionName = *static_cast<std::string*>(SSL_get_ex_data(ssl, 0));
154 Config* config = static_cast<Config*>(SSL_get_ex_data(ssl, 1));
155
156 std::string serverNameIndication = core::socket::stream::tls::ssl_get_servername_from_client_hello(ssl);
157
158 if (!serverNameIndication.empty()) {
159 SSL_CTX* sniSslCtx = config->getSniCtx(serverNameIndication);
160
161 if (sniSslCtx != nullptr) {
162 LOG(DEBUG) << connectionName << " SSL/TLS: Setting sni certificate for '" << serverNameIndication << "'";
163 core::socket::stream::tls::ssl_set_ssl_ctx(ssl, sniSslCtx);
164 } else if (config->getForceSni()) {
165 LOG(ERROR) << connectionName << " SSL/TLS: No sni certificate found for '" << serverNameIndication
166 << "' but forceSni set - terminating";
167 ret = SSL_CLIENT_HELLO_ERROR;
168 *al = SSL_AD_UNRECOGNIZED_NAME;
169 } else {
170 LOG(WARNING) << connectionName << " SSL/TLS: No sni certificate found for '" << serverNameIndication
171 << "'. Still using master certificate";
172 }
173 } else {
174 LOG(DEBUG) << connectionName << " SSL/TLS: No sni certificate requested from client. Still using master certificate";
175 }
176
177 return ret;
178 }
179
180} // namespace core::socket::stream::tls
static int clientHelloCallback(SSL *ssl, int *al, void *arg)
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)
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
State eventLoopState()
Definition State.cpp:52