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 softreq = require"util.dependencies".softreq;
10 local ssl = softreq"ssl";
13 create_context = function ()
14 return nil, "LuaSec (required for encryption) was not found";
16 reload_ssl_config = function () end;
20 local configmanager = require "core.configmanager";
21 local log = require "util.logger".init("certmanager");
22 local ssl_context = ssl.context or softreq"ssl.context";
23 local ssl_x509 = ssl.x509 or softreq"ssl.x509";
24 local ssl_newcontext = ssl.newcontext;
25 local new_config = require"util.sslconfig".new;
27 local tostring = tostring;
30 local io_open = io.open;
31 local select = select;
33 local prosody = prosody;
34 local resolve_path = require"util.paths".resolve_relative_path;
35 local config_path = prosody.paths.config;
37 local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
38 local luasec_version = luasec_major * 100 + luasec_minor;
40 -- TODO If LuaSec ever starts exposing these things itself, use that instead
41 cipher_server_preference = luasec_version >= 2;
42 no_ticket = luasec_version >= 4;
43 no_compression = luasec_version >= 5;
44 single_dh_use = luasec_version >= 2;
45 single_ecdh_use = luasec_version >= 2;
50 -- Global SSL options if not overridden per-host
51 local global_ssl_config = configmanager.get("*", "ssl");
54 local core_defaults = {
55 capath = "/etc/ssl/certs";
58 verify = (ssl_x509 and { "peer", "client_once", }) or "none";
60 cipher_server_preference = luasec_has.cipher_server_preference;
61 no_ticket = luasec_has.no_ticket;
62 no_compression = luasec_has.no_compression and configmanager.get("*", "ssl_compression") ~= true;
63 single_dh_use = luasec_has.single_dh_use;
64 single_ecdh_use = luasec_has.single_ecdh_use;
66 verifyext = { "lsec_continue", "lsec_ignore_purpose" };
68 ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
70 local path_options = { -- These we pass through resolve_path()
71 key = true, certificate = true, cafile = true, capath = true, dhparam = true
74 if luasec_version < 5 and ssl_x509 then
75 -- COMPAT mw/luasec-hg
76 for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
77 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
81 function create_context(host, mode, ...)
82 local cfg = new_config();
83 cfg:apply(core_defaults);
84 cfg:apply(global_ssl_config);
87 -- We can't read the password interactively when daemonized
88 password = function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
91 for i = select('#', ...), 1, -1 do
92 cfg:apply(select(i, ...));
94 local user_ssl_config = cfg:final();
96 if mode == "server" then
97 if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
98 if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
101 for option in pairs(path_options) do
102 if type(user_ssl_config[option]) == "string" then
103 user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
107 -- LuaSec expects dhparam to be a callback that takes two arguments.
108 -- We ignore those because it is mostly used for having a separate
109 -- set of params for EXPORT ciphers, which we don't have by default.
110 if type(user_ssl_config.dhparam) == "string" then
111 local f, err = io_open(user_ssl_config.dhparam);
112 if not f then return nil, "Could not open DH parameters: "..err end
113 local dhparam = f:read("*a");
115 user_ssl_config.dhparam = function() return dhparam; end
118 local ctx, err = ssl_newcontext(user_ssl_config);
120 -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
121 -- of it ourselves (W/A for #x)
122 if ctx and user_ssl_config.ciphers then
124 success, err = ssl_context.setcipher(ctx, user_ssl_config.ciphers);
125 if not success then ctx = nil; end
129 err = err or "invalid ssl config"
130 local file = err:match("^error loading (.-) %(");
132 if file == "private key" then
133 file = user_ssl_config.key or "your private key";
134 elseif file == "certificate" then
135 file = user_ssl_config.certificate or "your certificate file";
137 local reason = err:match("%((.+)%)$") or "some reason";
138 if reason == "Permission denied" then
139 reason = "Check that the permissions allow Prosody to read this file.";
140 elseif reason == "No such file or directory" then
141 reason = "Check that the path is correct, and the file exists.";
142 elseif reason == "system lib" then
143 reason = "Previous error (see logs), or other system error.";
144 elseif reason == "(null)" or not reason then
145 reason = "Check that the file exists and the permissions are correct";
147 reason = "Reason: "..tostring(reason):lower();
149 log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
151 log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
154 return ctx, err, user_ssl_config;
157 function reload_ssl_config()
158 global_ssl_config = configmanager.get("*", "ssl");
159 if luasec_has.no_compression then
160 core_defaults.options.no_compression = configmanager.get("*", "ssl_compression") ~= true;
164 prosody.events.add_handler("config-reloaded", reload_ssl_config);