certmanager: Options that appear to be available since LuaSec 0.2
[prosody.git] / core / certmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local softreq = require"util.dependencies".softreq;
10 local ssl = softreq"ssl";
11 if not ssl then
12         return {
13                 create_context = function ()
14                         return nil, "LuaSec (required for encryption) was not found";
15                 end;
16                 reload_ssl_config = function () end;
17         }
18 end
19
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;
26
27 local tostring = tostring;
28 local pairs = pairs;
29 local type = type;
30 local io_open = io.open;
31 local select = select;
32
33 local prosody = prosody;
34 local resolve_path = require"util.paths".resolve_relative_path;
35 local config_path = prosody.paths.config;
36
37 local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
38 local luasec_version = luasec_major * 100 + luasec_minor;
39 local luasec_has = {
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;
46 };
47
48 module "certmanager"
49
50 -- Global SSL options if not overridden per-host
51 local global_ssl_config = configmanager.get("*", "ssl");
52
53 -- Built-in defaults
54 local core_defaults = {
55         capath = "/etc/ssl/certs";
56         protocol = "tlsv1+";
57         verify = (ssl_x509 and { "peer", "client_once", }) or "none";
58         options = {
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;
64         };
65         verifyext = { "lsec_continue", "lsec_ignore_purpose" };
66         curve = "secp384r1";
67         ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
68 }
69 local path_options = { -- These we pass through resolve_path()
70         key = true, certificate = true, cafile = true, capath = true, dhparam = true
71 }
72
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);
77         end
78 end
79
80 function create_context(host, mode, ...)
81         local cfg = new_config();
82         cfg:apply(core_defaults);
83         cfg:apply(global_ssl_config);
84         cfg:apply({
85                 mode = mode,
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;
88         });
89
90         for i = select('#', ...), 1, -1 do
91                 cfg:apply(select(i, ...));
92         end
93         local user_ssl_config = cfg:final();
94
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
98         end
99
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]);
103                 end
104         end
105
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");
113                 f:close();
114                 user_ssl_config.dhparam = function() return dhparam; end
115         end
116
117         local ctx, err = ssl_newcontext(user_ssl_config);
118
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
122                 local success;
123                 success, err = ssl_context.setcipher(ctx, user_ssl_config.ciphers);
124                 if not success then ctx = nil; end
125         end
126
127         if not ctx then
128                 err = err or "invalid ssl config"
129                 local file = err:match("^error loading (.-) %(");
130                 if file then
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";
135                         end
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";
145                         else
146                                 reason = "Reason: "..tostring(reason):lower();
147                         end
148                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
149                 else
150                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
151                 end
152         end
153         return ctx, err, user_ssl_config;
154 end
155
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;
160         end
161 end
162
163 prosody.events.add_handler("config-reloaded", reload_ssl_config);
164
165 return _M;