SNode.C
Loading...
Searching...
No Matches
servers.h
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#ifndef APPS_HTTP_MODEL_SERVERS_H
43#define APPS_HTTP_MODEL_SERVERS_H
44
45#define QUOTE_INCLUDE(a) STR_INCLUDE(a)
46#define STR_INCLUDE(a) #a
47
48// clang-format off
49#define WEBAPP_INCLUDE QUOTE_INCLUDE(express/STREAM/NET/WebApp.h)
50// clang-format on
51
52#include WEBAPP_INCLUDE // IWYU pragma: export
53
54#include "express/middleware/VerboseRequest.h"
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58#include "log/Logger.h"
59
60#if (STREAM_TYPE == TLS) // tls
61#include <cstddef>
62#include <openssl/ssl.h>
63#include <openssl/x509v3.h>
64#endif
65
66#endif /* DOXYGEN_SHOULD_SKIP_THIS */
67
68static express::Router getRouter() {
69 return express::middleware::VerboseRequest();
70}
71
72#if (STREAM_TYPE == LEGACY) // legacy
73
74namespace apps::http::legacy {
75
78
79 static WebApp getWebApp(const std::string& name) {
80 WebApp webApp = WebApp(name, getRouter());
81
82 return webApp;
83 }
84
85} // namespace apps::http::legacy
86
87#endif // (STREAM_TYPE == LEGACY) // legacy
88
89#if (STREAM_TYPE == TLS) // tls
90
91namespace apps::http::tls {
92
95
96 static WebApp getWebApp(const std::string& name) {
97 WebApp webApp(name, getRouter());
98
99 webApp.setOnConnect([webApp](SocketConnection* socketConnection) { // onConnect
100 VLOG(1) << "OnConnect " << webApp.getConfig().getInstanceName();
101
102 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
103 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
104
105 /* Enable automatic hostname checks */
106 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
107
108 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
109 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
110 // // handle error
111 // socketConnection->close();
112 // }
113 });
114
115 webApp.setOnConnected([webApp](SocketConnection* socketConnection) { // onConnected
116 VLOG(1) << "OnConnected " << webApp.getConfig().getInstanceName();
117
118 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
119 if (server_cert != nullptr) {
120 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
121
122 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
123 std::string(X509_verify_cert_error_string(verifyErr));
124
125 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
126 VLOG(1) << "\t Subject: " + std::string(str);
127 OPENSSL_free(str);
128
129 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
130 VLOG(1) << "\t Issuer: " + std::string(str);
131 OPENSSL_free(str);
132
133 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
134
135 GENERAL_NAMES* subjectAltNames =
136 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
137#ifdef __GNUC__
138#pragma GCC diagnostic push
139#ifdef __has_warning
140#if __has_warning("-Wused-but-marked-unused")
141#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
142#endif
143#endif
144#endif
145 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
146#ifdef __GNUC_
147#pragma GCC diagnostic pop
148#endif
149 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
150 for (int32_t i = 0; i < altNameCount; ++i) {
151#ifdef __GNUC__
152#pragma GCC diagnostic push
153#ifdef __has_warning
154#if __has_warning("-Wused-but-marked-unused")
155#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
156#endif
157#endif
158#endif
159 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
160#ifdef __GNUC_
161#pragma GCC diagnostic pop
162#endif
163 if (generalName->type == GEN_URI) {
164 std::string subjectAltName =
165 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
166 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
167 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
168 } else if (generalName->type == GEN_DNS) {
169 std::string subjectAltName =
170 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
171 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
172 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
173 } else {
174 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
175 }
176 }
177#ifdef __GNUC__
178#pragma GCC diagnostic push
179#ifdef __has_warning
180#if __has_warning("-Wused-but-marked-unused")
181#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
182#endif
183#endif
184#endif
185 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
186#ifdef __GNUC_
187#pragma GCC diagnostic pop
188#endif
189 X509_free(server_cert);
190 } else {
191 LOG(WARNING) << "\tPeer certificate: no certificate";
192 }
193 });
194
195 webApp.setOnDisconnect([webApp](SocketConnection* socketConnection) { // onDisconnect
196 VLOG(1) << "OnDisconnect " << webApp.getConfig().getInstanceName();
197
198 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
199 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
200 });
201
202 return webApp;
203 }
204
205} // namespace apps::http::tls
206
207#endif // (STREAM_TYPE == TLS) // tls
208
209#endif // APPS_HTTP_MODEL_SERVERS_H
#define QUOTE_INCLUDE(a)
Definition clients.h:47
#define STR_INCLUDE(a)
Definition servers.h:48
static express::Router getRouter()
Definition servers.h:68
static WebApp getWebApp(const std::string &name)
Definition servers.h:79
static WebApp getWebApp(const std::string &name)
Definition servers.h:96
int main(int argc, char *argv[])