2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local configmanager = require "core.configmanager";
10 local log = require "util.logger".init("certmanager");
12 local ssl_newcontext = ssl and ssl.newcontext;
14 local tostring = tostring;
17 local io_open = io.open;
19 local prosody = prosody;
20 local resolve_path = configmanager.resolve_relative_path;
21 local config_path = prosody.paths.config;
23 local luasec_has_noticket, luasec_has_verifyext, luasec_has_no_compression;
25 local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
26 luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
27 luasec_has_verifyext = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
28 luasec_has_no_compression = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
33 -- Global SSL options if not overridden per-host
34 local global_ssl_config = configmanager.get("*", "ssl");
36 local core_defaults = {
37 capath = "/etc/ssl/certs";
39 verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
40 options = { "no_sslv2", luasec_has_noticket and "no_ticket" or nil };
41 verifyext = { "lsec_continue", "lsec_ignore_purpose" };
43 ciphers = "HIGH:!DSS:!aNULL@STRENGTH";
45 local path_options = { -- These we pass through resolve_path()
46 key = true, certificate = true, cafile = true, capath = true, dhparam = true
49 if ssl and not luasec_has_verifyext and ssl.x509 then
50 -- COMPAT mw/luasec-hg
51 for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
52 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
56 if luasec_has_no_compression and configmanager.get("*", "ssl_compression") ~= true then
57 core_defaults.options[#core_defaults.options+1] = "no_compression";
60 function create_context(host, mode, user_ssl_config)
61 user_ssl_config = user_ssl_config or {}
62 user_ssl_config.mode = mode;
64 if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
66 if global_ssl_config then
67 for option,default_value in pairs(global_ssl_config) do
68 if not user_ssl_config[option] then
69 user_ssl_config[option] = default_value;
73 for option,default_value in pairs(core_defaults) do
74 if not user_ssl_config[option] then
75 user_ssl_config[option] = default_value;
78 user_ssl_config.password = user_ssl_config.password or function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
79 for option in pairs(path_options) do
80 if type(user_ssl_config[option]) == "string" then
81 user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
85 if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
86 if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
88 -- LuaSec expects dhparam to be a callback that takes two arguments.
89 -- We ignore those because it is mostly used for having a separate
90 -- set of params for EXPORT ciphers, which we don't have by default.
91 if type(user_ssl_config.dhparam) == "string" then
92 local f, err = io_open(user_ssl_config.dhparam);
93 if not f then return nil, "Could not open DH parameters: "..err end
94 local dhparam = f:read("*a");
96 user_ssl_config.dhparam = function() return dhparam; end
99 local ctx, err = ssl_newcontext(user_ssl_config);
101 -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
102 -- of it ourselves (W/A for #x)
103 if ctx and user_ssl_config.ciphers then
105 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
106 if not success then ctx = nil; end
110 err = err or "invalid ssl config"
111 local file = err:match("^error loading (.-) %(");
113 if file == "private key" then
114 file = user_ssl_config.key or "your private key";
115 elseif file == "certificate" then
116 file = user_ssl_config.certificate or "your certificate file";
118 local reason = err:match("%((.+)%)$") or "some reason";
119 if reason == "Permission denied" then
120 reason = "Check that the permissions allow Prosody to read this file.";
121 elseif reason == "No such file or directory" then
122 reason = "Check that the path is correct, and the file exists.";
123 elseif reason == "system lib" then
124 reason = "Previous error (see logs), or other system error.";
125 elseif reason == "(null)" or not reason then
126 reason = "Check that the file exists and the permissions are correct";
128 reason = "Reason: "..tostring(reason):lower();
130 log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
132 log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
138 function reload_ssl_config()
139 global_ssl_config = configmanager.get("*", "ssl");
142 prosody.events.add_handler("config-reloaded", reload_ssl_config);