Merge 0.9->0.10
[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 = 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;
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         depth = 9;
57         protocol = "tlsv1+";
58         verify = (ssl_x509 and { "peer", "client_once", }) or "none";
59         options = {
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;
65         };
66         verifyext = { "lsec_continue", "lsec_ignore_purpose" };
67         curve = "secp384r1";
68         ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
69 }
70 local path_options = { -- These we pass through resolve_path()
71         key = true, certificate = true, cafile = true, capath = true, dhparam = true
72 }
73
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);
78         end
79 end
80
81 function create_context(host, mode, ...)
82         local cfg = new_config();
83         cfg:apply(core_defaults);
84         cfg:apply(global_ssl_config);
85         cfg:apply({
86                 mode = mode,
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;
89         });
90
91         for i = select('#', ...), 1, -1 do
92                 cfg:apply(select(i, ...));
93         end
94         local user_ssl_config = cfg:final();
95
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
99         end
100
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]);
104                 end
105         end
106
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");
114                 f:close();
115                 user_ssl_config.dhparam = function() return dhparam; end
116         end
117
118         local ctx, err = ssl_newcontext(user_ssl_config);
119
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
123                 local success;
124                 success, err = ssl_context.setcipher(ctx, user_ssl_config.ciphers);
125                 if not success then ctx = nil; end
126         end
127
128         if not ctx then
129                 err = err or "invalid ssl config"
130                 local file = err:match("^error loading (.-) %(");
131                 if file then
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";
136                         end
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";
146                         else
147                                 reason = "Reason: "..tostring(reason):lower();
148                         end
149                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
150                 else
151                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
152                 end
153         end
154         return ctx, err, user_ssl_config;
155 end
156
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;
161         end
162 end
163
164 prosody.events.add_handler("config-reloaded", reload_ssl_config);
165
166 return _M;