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 = >= 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";
57 verify = (ssl_x509 and { "peer", "client_once", }) or "none";
59 cipher_server_preference = luasec_has.cipher_server_preference;
60 no_ticket = luasec_has.no_ticket;
61 no_compression = luasec_has.no_compression and configmanager.get("*", "ssl_compression") ~= true;
62 single_dh_use = luasec_has.single_dh_use;
63 single_ecdh_use = luasec_has.single_ecdh_use;
65 verifyext = { "lsec_continue", "lsec_ignore_purpose" };
67 ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
69 local path_options = { -- These we pass through resolve_path()
70 key = true, certificate = true, cafile = true, capath = true, dhparam = true
73 if not luasec_has_verifyext and ssl_x509 then
74 -- COMPAT mw/luasec-hg
75 for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
76 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
80 function create_context(host, mode, ...)
81 local cfg = new_config();
82 cfg:apply(core_defaults);
83 cfg:apply(global_ssl_config);
86 -- We can't read the password interactively when daemonized
87 password = function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
90 for i = select('#', ...), 1, -1 do
91 cfg:apply(select(i, ...));
93 local user_ssl_config = cfg:final();
95 if mode == "server" then
96 if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
97 if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
100 for option in pairs(path_options) do
101 if type(user_ssl_config[option]) == "string" then
102 user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
106 -- LuaSec expects dhparam to be a callback that takes two arguments.
107 -- We ignore those because it is mostly used for having a separate
108 -- set of params for EXPORT ciphers, which we don't have by default.
109 if type(user_ssl_config.dhparam) == "string" then
110 local f, err = io_open(user_ssl_config.dhparam);
111 if not f then return nil, "Could not open DH parameters: "..err end
112 local dhparam = f:read("*a");
114 user_ssl_config.dhparam = function() return dhparam; end
117 local ctx, err = ssl_newcontext(user_ssl_config);
119 -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
120 -- of it ourselves (W/A for #x)
121 if ctx and user_ssl_config.ciphers then
123 success, err = ssl_context.setcipher(ctx, user_ssl_config.ciphers);
124 if not success then ctx = nil; end
128 err = err or "invalid ssl config"
129 local file = err:match("^error loading (.-) %(");
131 if file == "private key" then
132 file = user_ssl_config.key or "your private key";
133 elseif file == "certificate" then
134 file = user_ssl_config.certificate or "your certificate file";
136 local reason = err:match("%((.+)%)$") or "some reason";
137 if reason == "Permission denied" then
138 reason = "Check that the permissions allow Prosody to read this file.";
139 elseif reason == "No such file or directory" then
140 reason = "Check that the path is correct, and the file exists.";
141 elseif reason == "system lib" then
142 reason = "Previous error (see logs), or other system error.";
143 elseif reason == "(null)" or not reason then
144 reason = "Check that the file exists and the permissions are correct";
146 reason = "Reason: "..tostring(reason):lower();
148 log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
150 log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
153 return ctx, err, user_ssl_config;
156 function reload_ssl_config()
157 global_ssl_config = configmanager.get("*", "ssl");
158 if luasec_has.no_compression then
159 core_defaults.options.no_compression = configmanager.get("*", "ssl_compression") ~= true;
163 prosody.events.add_handler("config-reloaded", reload_ssl_config);