1 #ifndef __NGINX_SSL_UTIL_HPP
2 #define __NGINX_SSL_UTIL_HPP
10 #include "regex-pcre.hpp"
13 #include "nginx-util.hpp"
14 #include "px5g-openssl.hpp"
18 static constexpr auto UBUS_TIMEOUT = 1000;
22 static constexpr auto CRON_INTERVAL = std::string_view{"3 3 12 12 *"};
24 static constexpr auto LAN_SSL_LISTEN =
25 std::string_view{"/var/lib/nginx/lan_ssl.listen"};
27 static constexpr auto LAN_SSL_LISTEN_DEFAULT =
28 std::string_view{"/var/lib/nginx/lan_ssl.listen.default"};
30 static constexpr auto ADD_SSL_FCT = std::string_view{"add_ssl"};
32 static constexpr auto SSL_SESSION_CACHE_ARG =
33 [](const std::string_view & /*name*/) -> std::string
34 { return "shared:SSL:32k"; };
36 static constexpr auto SSL_SESSION_TIMEOUT_ARG = std::string_view{"64m"};
40 std::array< std::string (*)(const std::string &, const std::string &), 2 >;
50 explicit Line(const _Line & line) noexcept : _line{line} {}
52 template<const _Line & ...xn>
53 static auto build() noexcept -> Line
56 [](const std::string & p, const std::string & b) -> std::string
57 { return (... + xn[0](p, b)); },
58 [](const std::string & p, const std::string & b) -> std::string
59 { return (... + xn[1](p, b)); }
64 [[nodiscard]] auto STR(const std::string & param, const std::string & begin)
66 { return _line[0](param, begin); }
69 [[nodiscard]] auto RGX() const -> rgx::regex
70 { return rgx::regex{_line[1]("", "")}; }
75 auto get_if_missed(const std::string & conf, const Line & LINE,
76 const std::string & val,
77 const std::string & indent="\n ", bool compare=true)
81 auto delete_if(const std::string & conf, const rgx::regex & rgx,
82 const std::string & val="", bool compare=false)
86 void add_ssl_directives_to(const std::string & name, bool isdefault);
89 void create_ssl_certificate(const std::string & crtpath,
90 const std::string & keypath,
94 void use_cron_to_recreate_certificate(const std::string & name);
97 void add_ssl_if_needed(const std::string & name);
100 void del_ssl_directives_from(const std::string & name, bool isdefault);
103 void del_ssl(const std::string & name);
106 constexpr auto _begin = _Line{
107 [](const std::string & /*param*/, const std::string & begin) -> std::string
110 [](const std::string & /*param*/, const std::string & /*begin*/)
112 { return R"([{;](?:\s*#[^\n]*(?=\n))*(\s*))"; }
116 constexpr auto _space = _Line{
117 [](const std::string & /*param*/, const std::string & /*begin*/)
119 { return std::string{" "}; },
121 [](const std::string & /*param*/, const std::string & /*begin*/)
127 constexpr auto _newline = _Line{
128 [](const std::string & /*param*/, const std::string & /*begin*/)
130 { return std::string{"\n"}; },
132 [](const std::string & /*param*/, const std::string & /*begin*/)
134 { return std::string{"\n"}; }
138 constexpr auto _end = _Line{
139 [](const std::string & /*param*/, const std::string & /*begin*/)
141 { return std::string{";"}; },
143 [](const std::string & /*param*/, const std::string & /*begin*/)
145 { return std::string{R"(\s*;(?:[\t ]*#[^\n]*)?)"}; }
149 template<char clim='\0'>
150 constexpr auto _capture = _Line{
151 [](const std::string & param, const std::string & /*begin*/) -> std::string
152 { return '\'' + param + '\''; },
154 [](const std::string & /*param*/, const std::string & /*begin*/)
157 const auto lim = clim=='\0' ? std::string{"\\s"} : std::string{clim};
158 return std::string{R"(((?:(?:"[^"]*")|(?:[^'")"} +
159 lim + "][^" + lim + "]*)|(?:'[^']*'))+)";
164 template<const std::string_view & strptr, char clim='\0'>
165 constexpr auto _escape = _Line{
166 [](const std::string & /*param*/, const std::string & /*begin*/)
170 std::string{strptr.data()} :
171 clim + std::string{strptr.data()} + clim;
174 [](const std::string & /*param*/, const std::string & /*begin*/)
178 for (char c : strptr) {
180 case '^': ret += '\\'; [[fallthrough]];
181 case '_': [[fallthrough]];
185 if ((isalpha(c)!=0) || (isdigit(c)!=0)) { ret += c; }
186 else { ret += std::string{"["}+c+"]"; }
189 return "(?:"+ret+"|'"+ret+"'"+"|\""+ret+"\""+")";
194 constexpr std::string_view _server_name = "server_name";
196 constexpr std::string_view _include = "include";
198 constexpr std::string_view _ssl_certificate = "ssl_certificate";
200 constexpr std::string_view _ssl_certificate_key = "ssl_certificate_key";
202 constexpr std::string_view _ssl_session_cache = "ssl_session_cache";
204 constexpr std::string_view _ssl_session_timeout = "ssl_session_timeout";
207 // For a compile time regex lib, this must be fixed, use one of these options:
208 // * Hand craft or macro concat them (loosing more or less flexibility).
209 // * Use Macro concatenation of __VA_ARGS__ with the help of:
210 // https://p99.gforge.inria.fr/p99-html/group__preprocessor__for.html
211 // * Use constexpr---not available for strings or char * for now---look at lib.
213 static const auto CRON_CMD = Line::build
214 < _space, _escape<NGINX_UTIL>, _space, _escape<ADD_SSL_FCT,'\''>, _space,
215 _capture<>, _newline >();
217 static const auto NGX_SERVER_NAME =
218 Line::build<_begin, _escape<_server_name>, _space, _capture<';'>, _end>();
220 static const auto NGX_INCLUDE_LAN_LISTEN = Line::build
221 <_begin, _escape<_include>, _space, _escape<LAN_LISTEN,'\''>, _end>();
223 static const auto NGX_INCLUDE_LAN_LISTEN_DEFAULT = Line::build
224 < _begin, _escape<_include>, _space,
225 _escape<LAN_LISTEN_DEFAULT, '\''>, _end >();
227 static const auto NGX_INCLUDE_LAN_SSL_LISTEN = Line::build
228 <_begin, _escape<_include>, _space, _escape<LAN_SSL_LISTEN, '\''>, _end>();
230 static const auto NGX_INCLUDE_LAN_SSL_LISTEN_DEFAULT = Line::build
231 < _begin, _escape<_include>, _space,
232 _escape<LAN_SSL_LISTEN_DEFAULT, '\''>, _end >();
234 static const auto NGX_SSL_CRT = Line::build
235 <_begin, _escape<_ssl_certificate>, _space, _capture<';'>, _end>();
237 static const auto NGX_SSL_KEY = Line::build
238 <_begin, _escape<_ssl_certificate_key>, _space, _capture<';'>, _end>();
240 static const auto NGX_SSL_SESSION_CACHE = Line::build
241 <_begin, _escape<_ssl_session_cache>, _space, _capture<';'>, _end>();
243 static const auto NGX_SSL_SESSION_TIMEOUT = Line::build
244 <_begin, _escape<_ssl_session_timeout>, _space, _capture<';'>, _end>();
247 auto get_if_missed(const std::string & conf, const Line & LINE,
248 const std::string & val,
249 const std::string & indent, bool compare)
252 if (!compare || val.empty()) {
253 return rgx::regex_search(conf, LINE.RGX()) ? "" : LINE.STR(val, indent);
256 rgx::smatch match; // assuming last capture has the value!
258 for (auto pos = conf.begin();
259 rgx::regex_search(pos, conf.end(), match, LINE.RGX());
260 pos += match.position(0) + match.length(0))
262 const std::string value = match.str(match.size() - 1);
264 if (value==val || value=="'"+val+"'" || value=='"'+val+'"') {
269 return LINE.STR(val, indent);
273 auto delete_if(const std::string & conf, const rgx::regex & rgx,
274 const std::string & val, const bool compare)
278 auto pos = conf.begin();
280 for (rgx::smatch match;
281 rgx::regex_search(pos, conf.end(), match, rgx);
282 pos += match.position(0) + match.length(0))
284 const std::string value = match.str(match.size() - 1);
285 auto len = match.position(1);
286 if (compare && value!=val && value!="'"+val+"'" && value!='"'+val+'"') {
287 len = match.position(0) + match.length(0);
289 ret.append(pos, pos + len);
292 ret.append(pos, conf.end());
297 void add_ssl_directives_to(const std::string & name, const bool isdefault)
299 const std::string prefix = std::string{CONF_DIR} + name;
301 std::string conf = read_file(prefix+".conf");
303 const std::string & const_conf = conf; // iteration needs const string.
304 rgx::smatch match; // captures str(1)=indentation spaces, str(2)=server name
305 for (auto pos = const_conf.begin();
306 rgx::regex_search(pos, const_conf.end(), match, NGX_SERVER_NAME.RGX());
307 pos += match.position(0) + match.length(0))
309 if (match.str(2).find(name) == std::string::npos) { continue; }
311 const std::string indent = match.str(1);
313 std::string adds = isdefault ?
314 get_if_missed(conf, NGX_INCLUDE_LAN_SSL_LISTEN_DEFAULT,"",indent) :
315 get_if_missed(conf, NGX_INCLUDE_LAN_SSL_LISTEN, "", indent);
317 adds += get_if_missed(conf, NGX_SSL_CRT, prefix+".crt", indent);
319 adds += get_if_missed(conf, NGX_SSL_KEY, prefix+".key", indent);
321 adds += get_if_missed(conf, NGX_SSL_SESSION_CACHE,
322 SSL_SESSION_CACHE_ARG(name), indent, false);
324 adds += get_if_missed(conf, NGX_SSL_SESSION_TIMEOUT,
325 std::string{SSL_SESSION_TIMEOUT_ARG}, indent, false);
327 if (adds.length() > 0) {
328 pos += match.position(0) + match.length(0);
330 conf = std::string(const_conf.begin(), pos) + adds +
331 std::string(pos, const_conf.end());
334 delete_if(conf, NGX_INCLUDE_LAN_LISTEN_DEFAULT.RGX()) :
335 delete_if(conf, NGX_INCLUDE_LAN_LISTEN.RGX());
337 write_file(prefix+".conf", conf);
339 std::cerr<<"Added SSL directives to "<<prefix<<".conf: ";
340 std::cerr<<adds<<std::endl;
346 auto errmsg = std::string{"add_ssl_directives_to error: "};
347 errmsg += "cannot add SSL directives to " + name + ".conf, missing: ";
348 errmsg += NGX_SERVER_NAME.STR(name, "\n ") + "\n";
349 throw std::runtime_error(errmsg.c_str());
354 inline auto num2hex(T bytes) -> std::array<char, 2*sizeof(bytes)+1>
356 constexpr auto n = 2*sizeof(bytes);
357 std::array<char, n+1> str{};
359 for (size_t i=0; i<n; ++i) {
360 static const std::array<char, 17> hex{"0123456789ABCDEF"};
361 static constexpr auto get = 0x0fU;
362 str.at(i) = hex.at(bytes & get);
364 static constexpr auto move = 4U;
374 inline auto get_nonce(const T salt=0) -> T
378 std::ifstream urandom{"/dev/urandom"};
380 static constexpr auto move = 6U;
382 constexpr size_t steps = (sizeof(nonce)*8 - 1)/move + 1;
384 for (size_t i=0; i<steps; ++i) {
385 if (!urandom.good()) { throw std::runtime_error("get_nonce error"); }
386 nonce = (nonce << move) + static_cast<unsigned>(urandom.get());
395 void create_ssl_certificate(const std::string & crtpath,
396 const std::string & keypath,
401 try { nonce = get_nonce(nonce); }
403 catch (...) { // the address of a variable should be random enough:
404 auto addr = &crtpath;
405 auto addrptr = static_cast<const size_t *>(
406 static_cast<const void *>(&addr) );
410 auto noncestr = num2hex(nonce);
412 const auto tmpcrtpath = crtpath + ".new-" + noncestr.data();
413 const auto tmpkeypath = keypath + ".new-" + noncestr.data();
416 auto pkey = gen_eckey(NID_secp384r1);
418 write_key(pkey, tmpkeypath);
420 std::string subject {"/C=ZZ/ST=Somewhere/L=None/CN=OpenWrt/O=OpenWrt"};
421 subject += noncestr.data();
423 selfsigned(pkey, days, subject, tmpcrtpath);
425 static constexpr auto to_seconds = 24*60*60;
426 static constexpr auto leeway = 42;
427 if (!checkend(tmpcrtpath, days*to_seconds - leeway)) {
428 throw std::runtime_error("bug: created certificate is not valid!!");
432 std::cerr<<"create_ssl_certificate error: ";
433 std::cerr<<"cannot create selfsigned certificate, ";
434 std::cerr<<"removing temporary files ..."<<std::endl;
436 if (remove(tmpcrtpath.c_str())!=0) {
437 auto errmsg = "\t cannot remove "+tmpcrtpath;
438 perror(errmsg.c_str());
441 if (remove(tmpkeypath.c_str())!=0) {
442 auto errmsg = "\t cannot remove "+tmpkeypath;
443 perror(errmsg.c_str());
449 if ( rename(tmpcrtpath.c_str(), crtpath.c_str())!=0 ||
450 rename(tmpkeypath.c_str(), keypath.c_str())!=0 )
452 auto errmsg = std::string{"create_ssl_certificate warning: "};
453 errmsg += "cannot move "+tmpcrtpath+" to "+crtpath;
454 errmsg += " or "+tmpkeypath+" to "+keypath+", continuing ... ";
455 perror(errmsg.c_str());
461 void use_cron_to_recreate_certificate(const std::string & name)
463 static const char * filename = "/etc/crontabs/root";
466 try { conf = read_file(filename); }
467 catch (const std::ifstream::failure &) { /* is ok if not found, create. */ }
469 const std::string add = get_if_missed(conf, CRON_CMD, name);
471 if (add.length() > 0) {
473 auto service = ubus::call("service","list",UBUS_TIMEOUT).filter("cron");
476 std::string errmsg{"use_cron_to_recreate_certificate error: "};
477 errmsg += "Cron unavailable to re-create the ssl certificate for ";
478 errmsg += name + "\n";
479 throw std::runtime_error(errmsg.c_str());
480 } // else active with or without instances:
483 write_file(filename, std::string{CRON_INTERVAL}+add, std::ios::app);
486 call("/etc/init.d/cron", "reload");
489 std::cerr<<"Rebuild the ssl certificate for '";
490 std::cerr<<name<<"' annually with cron."<<std::endl;
495 void add_ssl_if_needed(const std::string & name)
497 add_ssl_directives_to(name, name==LAN_NAME); // let it throw.
499 const auto crtpath = std::string{CONF_DIR} + name + ".crt";
500 const auto keypath = std::string{CONF_DIR} + name + ".key";
501 constexpr auto remaining_seconds = (365 + 32)*24*60*60;
502 constexpr auto validity_days = 3*(365 + 31);
504 bool is_valid = true;
506 if (access(keypath.c_str(), R_OK) != 0 ||
507 access(crtpath.c_str(), R_OK) != 0)
508 { is_valid = false; }
512 if (!checkend(crtpath, remaining_seconds)) {
516 catch (...) { // something went wrong, maybe it is in DER format:
518 if (!checkend(crtpath, remaining_seconds, false)) {
522 catch (...) { // it has neither DER nor PEM format, rebuild.
528 if (!is_valid) { create_ssl_certificate(crtpath, keypath, validity_days); }
530 try { use_cron_to_recreate_certificate(name); }
532 std::cerr<<"add_ssl_if_needed warning: ";
533 std::cerr<<"cannot use cron to rebuild certificate for "<<name<<"\n";
538 void del_ssl_directives_from(const std::string & name, const bool isdefault)
540 const std::string prefix = std::string{CONF_DIR} + name;
542 std::string conf = read_file(prefix+".conf");
544 const std::string & const_conf = conf; // iteration needs const string.
545 rgx::smatch match; // captures str(1)=indentation spaces, str(2)=server name
546 for (auto pos = const_conf.begin();
547 rgx::regex_search(pos, const_conf.end(), match, NGX_SERVER_NAME.RGX());
548 pos += match.position(0) + match.length(0))
550 if (match.str(2).find(name) == std::string::npos) { continue; }
552 const std::string indent = match.str(1);
554 std::string adds = isdefault ?
555 get_if_missed(conf, NGX_INCLUDE_LAN_LISTEN_DEFAULT,"",indent) :
556 get_if_missed(conf, NGX_INCLUDE_LAN_LISTEN, "", indent);
558 if (adds.length() > 0) {
559 pos += match.position(1);
561 conf = std::string(const_conf.begin(), pos) + adds
562 + std::string(pos, const_conf.end());
565 delete_if(conf, NGX_INCLUDE_LAN_SSL_LISTEN_DEFAULT.RGX())
566 : delete_if(conf, NGX_INCLUDE_LAN_SSL_LISTEN.RGX());
568 const auto crtpath = prefix+".crt";
569 conf = delete_if(conf, NGX_SSL_CRT.RGX(), crtpath, true);
571 const auto keypath = prefix+".key";
572 conf = delete_if(conf, NGX_SSL_KEY.RGX(), keypath, true);
574 conf = delete_if(conf, NGX_SSL_SESSION_CACHE.RGX());
576 conf = delete_if(conf, NGX_SSL_SESSION_TIMEOUT.RGX());
578 write_file(prefix+".conf", conf);
580 std::cerr<<"Deleted SSL directives from "<<prefix<<".conf\n";
586 auto errmsg = std::string{"del_ssl_directives_from error: "};
587 errmsg += "cannot delete SSL directives from " + name + ".conf, missing: ";
588 errmsg += NGX_SERVER_NAME.STR(name, "\n ") + "\n";
589 throw std::runtime_error(errmsg.c_str());
593 void del_ssl(const std::string & name)
595 static const char * filename = "/etc/crontabs/root";
598 const auto const_conf = read_file(filename);
600 bool changed = false;
601 auto conf = std::string{};
605 while ((curr=const_conf.find('\n', prev)) != std::string::npos) {
607 auto line = const_conf.substr(prev, curr-prev+1);
609 if (line==delete_if(line,CRON_CMD.RGX(),std::string{name},true)) {
611 } else { changed = true; }
617 write_file(filename, conf);
619 std::cerr<<"Do not rebuild the ssl certificate for '";
620 std::cerr<<name<<"' annually with cron anymore."<<std::endl;
623 if (ubus::call("service", "list", UBUS_TIMEOUT).filter("cron"))
624 { call("/etc/init.d/cron", "reload"); }
629 std::cerr<<"del_ssl warning: ";
630 std::cerr<<"cannot delete cron job for "<<name<<" in "<<filename<<"\n";
633 try { del_ssl_directives_from(name, name==LAN_NAME); }
635 std::cerr<<"del_ssl error: ";
636 std::cerr<<"cannot delete SSL directives from "<<name<<".conf\n";
640 const auto crtpath = std::string{CONF_DIR} + name + ".crt";
642 if (remove(crtpath.c_str())!=0) {
643 auto errmsg = "del_ssl warning: cannot remove "+crtpath;
644 perror(errmsg.c_str());
647 const auto keypath = std::string{CONF_DIR} + name + ".key";
649 if (remove(keypath.c_str())!=0) {
650 auto errmsg = "del_ssl warning: cannot remove "+keypath;
651 perror(errmsg.c_str());