1 local configmanager = require "core.configmanager";
2 local log = require "util.logger".init("certmanager");
4 local ssl_newcontext = ssl and ssl.newcontext;
6 local setmetatable, tostring = setmetatable, tostring;
8 local prosody = prosody;
12 -- These are the defaults if not overridden in the config
13 local default_ssl_ctx = { mode = "client", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none", options = "no_sslv2"; };
14 local default_ssl_ctx_in = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none", options = "no_sslv2"; };
16 local default_ssl_ctx_mt = { __index = default_ssl_ctx };
17 local default_ssl_ctx_in_mt = { __index = default_ssl_ctx_in };
19 -- Global SSL options if not overridden per-host
20 local default_ssl_config = configmanager.get("*", "core", "ssl");
22 function create_context(host, mode, config)
23 local ssl_config = config and config.core.ssl or default_ssl_config;
24 if ssl and ssl_config then
25 local ctx, err = ssl_newcontext(setmetatable(ssl_config, mode == "client" and default_ssl_ctx_mt or default_ssl_ctx_in_mt));
27 err = err or "invalid ssl config"
28 local file = err:match("^error loading (.-) %(");
30 if file == "private key" then
31 file = ssl_config.key or "your private key";
32 elseif file == "certificate" then
33 file = ssl_config.certificate or "your certificate file";
35 local reason = err:match("%((.+)%)$") or "some reason";
36 if reason == "Permission denied" then
37 reason = "Check that the permissions allow Prosody to read this file.";
38 elseif reason == "No such file or directory" then
39 reason = "Check that the path is correct, and the file exists.";
40 elseif reason == "system lib" then
41 reason = "Previous error (see logs), or other system error.";
42 elseif reason == "(null)" or not reason then
43 reason = "Check that the file exists and the permissions are correct";
45 reason = "Reason: "..tostring(reason):lower();
47 log("error", "SSL/TLS: Failed to load %s: %s", file, reason);
49 log("error", "SSL/TLS: Error initialising for host %s: %s", host, err );
58 function reload_ssl_config()
59 default_ssl_config = configmanager.get("*", "core", "ssl");
62 prosody.events.add_handler("config-reloaded", reload_ssl_config);