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
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#include "net/config/ConfigTlsServer.h"
21
22#include "net/config/ConfigSection.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include <functional>
27#include <memory>
28
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace net::config {
32
34 : Super(instance) {
36 ->add_option("--sni-cert",
38 "Server Name Indication (SNI) Certificates:\n"
39 "sni = SNI of the virtual server\n"
40 "<key> = {\n"
41 " Cert -> value:PEM-FILE [\"\"]\n"
42 " CertKey -> value:PEM-FILE [\"\"]\n"
43 " CertKeyPassword -> value:TEXT [\"\"]\n"
44 " CaCert -> value:PEM-FILE [\"\"]\n"
45 " CaCertDir -> value:PEM-CONTAINER-DIR [\"\"]\n"
46 " CaCertUseDefaultDir -> value:BOOLEAN [false]\n"
47 " CipherList -> value:CIPHER [\"\"]\n"
48 " SslOptions -> value:UINT [0]\n"
49 "}") //
50 ->type_name("sni <key> value [<key> value] ... [%% sni <key> value [<key> value] ...]")
51 ->default_str("[\"\" \"\" \"\" \"\"]");
53 sniCertsOpt->group(section->get_formatter()->get_label("Persistent Options"));
54 }
55
58
59 for (const auto& [domain, sniCertConf] : defaultSniCerts) {
60 defaultValue += (!defaultValue.empty() ? "\"%%\" \"" : "\"") + domain + "\" ";
61
62 for (const auto& [key, value] : sniCertConf) {
63 defaultValue += "\"" + key + "\" ";
64
65 if (key == "Cert") {
66 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
67 } else if (key == "CertKey") {
68 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
69 } else if (key == "CertKeyPassword") {
70 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
71 } else if (key == "CaCert") {
72 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
73 } else if (key == "CaCertDir") {
74 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
75 } else if (key == "CaCertUseDefaultDir") {
76 defaultValue += std::get<bool>(value) ? "\"true\" " : "\"false\" ";
77 } else if (key == "CipherList") {
78 defaultValue += "\"" + std::get<std::string>(value) + "\" ";
79 } else if (key == "SslOptions") {
80 defaultValue += "\"" + std::to_string(std::get<ssl_option_t>(value)) + "\" ";
81 }
82 }
83 }
84
86
87 return "[" + defaultValue + " \"\"]";
88 });
89
91 "--force-sni{true}",
92 "Force using of the Server Name Indication",
93 "bool",
94 "false",
95 CLI::IsMember({"true", "false"}));
96
97 section->final_callback([this]() {
98 for (auto& [domain, sniMap] : configuredSniCerts) {
99 if (domain.empty()) {
100 sniCertsOpt //
101 ->clear();
102 sniMap.clear();
103 break;
104 }
105 for (auto& [key, value] : sniMap) {
106 if (key != "Cert" && //
107 key != "CertKey" && //
108 key != "CertKeyPassword" && //
109 key != "CaCert" && //
110 key != "CaCertDir" && //
111 key != "CaCertUseDefaultDir" && //
112 key != "CipherList" && //
113 key != "SslOptions") {
114 throw CLI::ConversionError("'" + key + "' of option '--" + section->get_parent()->get_name() + "." +
115 section->get_name() + ".sni-cert'",
116 "<key>");
117 }
118 }
119 }
120 });
121 }
122
124 forceSniOpt //
125 ->default_val(forceSni ? "true" : "false")
126 ->clear();
127
128 return *this;
129 }
130
132 return forceSniOpt->as<bool>();
133 }
134
142
150
154
155} // namespace net::config