SNode.C
Loading...
Searching...
No Matches
ConfigTlsServer.cpp
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, 2026
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 "net/config/ConfigTlsServer.h"
43
44#include "ConfigTls.hpp"
45
46namespace net::config {
47 class ConfigInstance;
48}
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52#include <functional>
53
54#endif // DOXYGEN_SHOULD_SKIP_THIS
55
56namespace net::config {
57
59 : ConfigTls(instance, this) {
62 "Server Name Indication (SNI) Certificates:\n"
63 "sni = SNI of the virtual server\n"
64 "<key> = {\n"
65 " Cert -> value:PEM-FILE [\"\"]\n"
66 " CertKey -> value:PEM-FILE [\"\"]\n"
67 " CertKeyPassword -> value:TEXT [\"\"]\n"
68 " CaCert -> value:PEM-FILE [\"\"]\n"
69 " CaCertDir -> value:PEM-CONTAINER-DIR [\"\"]\n"
70 " CaCertUseDefaultDir -> value:BOOLEAN [false]\n"
71 " CipherList -> value:CIPHER [\"\"]\n"
72 " SslOptions -> value:UINT [0]\n"
73 "}",
74 "sni <key> value [<key> value] ... [%% sni <key> value [<key> value] ...]",
75 CLI::TypeValidator<std::string>());
76
77 sniCertsOpt->default_function([this]() -> std::string {
78 std::string defaultValue;
79
80 for (const auto& [domain, sniCertConf] : defaultSniCerts) {
81 defaultValue += (!defaultValue.empty() ? "%% " : "") + domain + " ";
82
83 for (const auto& [key, value] : sniCertConf) {
84 defaultValue += key + " ";
85
86 if (key == "Cert") {
87 defaultValue += std::get<std::string>(value) + " ";
88 } else if (key == "CertKey") {
89 defaultValue += std::get<std::string>(value) + " ";
90 } else if (key == "CertKeyPassword") {
91 defaultValue += std::get<std::string>(value) + " ";
92 } else if (key == "CaCert") {
93 defaultValue += std::get<std::string>(value) + " ";
94 } else if (key == "CaCertDir") {
95 defaultValue += std::get<std::string>(value) + " ";
96 } else if (key == "CaCertUseDefaultDir") {
97 defaultValue += std::get<bool>(value) ? "true " : "false ";
98 } else if (key == "CipherList") {
99 defaultValue += std::get<std::string>(value) + " ";
100 } else if (key == "SslOptions") {
101 defaultValue += std::to_string(std::get<ssl_option_t>(value)) + " ";
102 }
103 }
104 }
105
106 defaultValue.pop_back();
107
108 return "[" + defaultValue + "]";
109 });
110
112 "--force-sni{true}",
113 "Force using of the Server Name Indication",
114 "bool",
115 "false",
116 CLI::IsMember({"true", "false"}));
117
118 finalCallback([this]() {
119 for (auto& [domain, sniMap] : configuredSniCerts) {
120 if (domain.empty()) {
121 sniCertsOpt //
122 ->clear();
123 sniMap.clear();
124 break;
125 }
126 for (auto& [key, value] : sniMap) {
127 if (key != "Cert" && //
128 key != "CertKey" && //
129 key != "CertKeyPassword" && //
130 key != "CaCert" && //
131 key != "CaCertDir" && //
132 key != "CaCertUseDefaultDir" && //
133 key != "CipherList" && //
134 key != "SslOptions") {
135 throw CLI::ConversionError("'" + key + "' of option '--" + getParent()->getName() + "." + getName() + ".sni-cert'",
136 "<key>");
137 }
138 }
139 }
140 });
141 }
142
145
147 setDefaultValue(forceSniOpt, forceSni ? "true" : "false");
148
149 return *this;
150 }
151
153 return forceSniOpt->as<bool>();
154 }
155
157 const std::map<std::string, std::map<std::string, std::variant<std::string, bool, ssl_option_t>>>& sniCerts) {
158 defaultSniCerts.insert(sniCerts.begin(), sniCerts.end());
159 sniCertsOpt->capture_default_str();
160
161 return *this;
162 }
163
164 ConfigTlsServer& ConfigTlsServer::addSniCert(const std::string& domain,
165 const std::map<std::string, std::variant<std::string, bool, ssl_option_t>>& sniCert) {
166 defaultSniCerts[domain] = sniCert;
167 sniCertsOpt->capture_default_str();
168
169 return *this;
170 }
171
172 const std::map<std::string, std::map<std::string, std::variant<std::string, bool, ssl_option_t>>>&
176
177} // namespace net::config
std::map< std::string, std::map< std::string, std::variant< std::string, bool, ssl_option_t > > > configuredSniCerts
ConfigTlsServer & addSniCerts(const std::map< std::string, std::map< std::string, std::variant< std::string, bool, ssl_option_t > > > &sniCerts)
ConfigTlsServer(ConfigInstance *instance)
ConfigTlsServer & setForceSni(bool forceSni=true)
const std::map< std::string, std::map< std::string, std::variant< std::string, bool, ssl_option_t > > > & getSniCerts() const
std::map< std::string, std::map< std::string, std::variant< std::string, bool, ssl_option_t > > > defaultSniCerts
ConfigTlsServer & addSniCert(const std::string &domain, const std::map< std::string, std::variant< std::string, bool, ssl_option_t > > &sniCert)
ConfigTls(ConfigInstance *instance, ConcretConfigTls section)
Definition ConfigTls.hpp:52
SubCommand * getParent()
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &validator) const
Definition SubCommand.h:328
CLI::Option * setDefaultValue(CLI::Option *option, const ValueTypeT &value, bool clear=true) const
Definition SubCommand.h:337
SubCommand * finalCallback(const std::function< void()> &finalCallback)
std::string getName() const
CLI::Option * addOptionVariable(const std::string &name, ValueTypeT &variable, const std::string &description, const std::string &typeName, const CLI::Validator &additionalValidator) const
Definition SubCommand.h:298