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 = setmetatable;
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.";
43 reason = "Reason: "..tostring(reason or "unknown"):lower();
45 log("error", "SSL/TLS: Failed to load %s: %s", file, reason);
47 log("error", "SSL/TLS: Error initialising for host %s: %s", host, err );
56 function reload_ssl_config()
57 default_ssl_config = config.get("*", "core", "ssl");
60 prosody.events.add_handler("config-reloaded", reload_ssl_config);