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#ifndef APPS_ECHO_MODEL_SERVER_H
21#define APPS_ECHO_MODEL_SERVER_H
22
23#include "log/Logger.h"
24
25#define QUOTE_INCLUDE(a) STR_INCLUDE(a)
26#define STR_INCLUDE(a) #a
27
28// clang-format off
29#define SOCKETSERVER_INCLUDE QUOTE_INCLUDE(net/NET/stream/STREAM/SocketServer.h)
30// clang-format on
31
32#include SOCKETSERVER_INCLUDE // IWYU pragma: export
33
35
36#ifndef DOXYGEN_SHOULD_SKIP_THIS
37
38#include <string>
39
40#if (STREAM_TYPE == TLS) // tls
41#include <cstddef>
42#include <openssl/ssl.h>
43#include <openssl/x509v3.h>
44#endif
45
46#endif /* DOXYGEN_SHOULD_SKIP_THIS */
47
48#if (STREAM_TYPE == LEGACY) // legacy
49
50namespace apps::echo::model::legacy {
51
53
55 return EchoSocketServer("echoserver");
56 }
57
58} // namespace apps::echo::model::legacy
59
60#elif (STREAM_TYPE == TLS) // tls
61
62namespace apps::echo::model::tls {
63
64 using EchoSocketServer = net::NET::stream::tls::SocketServer<EchoServerSocketContextFactory>;
65 using SocketConnection = EchoSocketServer::SocketConnection;
66
67 EchoSocketServer getServer() {
68 EchoSocketServer server("echoserver");
69
70 server.setOnConnect([&server](SocketConnection* socketConnection) { // onConnect
71 VLOG(1) << "OnConnect " << server.getConfig().getInstanceName();
72
73 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
74 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
75
76 /* Enable automatic hostname checks */
77 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
78
79 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
80 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
81 // // handle error
82 // socketConnection->close();
83 // }
84 });
85
86 server.setOnConnected([&server](SocketConnection* socketConnection) { // onConnected
87 VLOG(1) << "OnConnected " << server.getConfig().getInstanceName();
88
89 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
90 if (server_cert != nullptr) {
91 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
92
93 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
94 std::string(X509_verify_cert_error_string(verifyErr));
95
96 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
97 VLOG(1) << "\t Subject: " + std::string(str);
98 OPENSSL_free(str);
99
100 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
101 VLOG(1) << "\t Issuer: " + std::string(str);
102 OPENSSL_free(str);
103
104 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
105
106 GENERAL_NAMES* subjectAltNames =
107 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
108#ifdef __GNUC__
109#pragma GCC diagnostic push
110#ifdef __has_warning
111#if __has_warning("-Wused-but-marked-unused")
112#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
113#endif
114#endif
115#endif
116 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
117#ifdef __GNUC_
118#pragma GCC diagnostic pop
119#endif
120 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
121 for (int32_t i = 0; i < altNameCount; ++i) {
122#ifdef __GNUC__
123#pragma GCC diagnostic push
124#ifdef __has_warning
125#if __has_warning("-Wused-but-marked-unused")
126#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
127#endif
128#endif
129#endif
130 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
131#ifdef __GNUC_
132#pragma GCC diagnostic pop
133#endif
134 if (generalName->type == GEN_URI) {
135 std::string subjectAltName =
136 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
137 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
138 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
139 } else if (generalName->type == GEN_DNS) {
140 std::string subjectAltName =
141 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
142 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
143 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
144 } else {
145 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
146 }
147 }
148#ifdef __GNUC__
149#pragma GCC diagnostic push
150#ifdef __has_warning
151#if __has_warning("-Wused-but-marked-unused")
152#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
153#endif
154#endif
155#endif
156 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
157#ifdef __GNUC_
158#pragma GCC diagnostic pop
159#endif
160 X509_free(server_cert);
161 } else {
162 VLOG(1) << "\tPeer certificate: no certificate";
163 }
164 });
165
166 server.setOnDisconnect([&server](SocketConnection* socketConnection) { // onDisconnect
167 VLOG(1) << "OnDisconnect " << server.getConfig().getInstanceName();
168
169 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
170 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
171 });
172
173 return server;
174 }
175
176} // namespace apps::echo::model::tls
177
178#endif
179
180#endif // APPS_ECHO_MODEL_SERVER_H
int main(int argc, char *argv[])
#define QUOTE_INCLUDE(a)
Definition clients.h:25
#define STR_INCLUDE(a)
Definition servers.h:26
EchoSocketServer getServer()
Definition servers.h:54
Definition clients.h:50