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 configmanager = require "core.configmanager";
10 local log = require "util.logger".init("certmanager");
11 local ssl = _G.ssl;
12 local ssl_newcontext = ssl and ssl.newcontext;
13 local new_config = require"util.sslconfig".new;
14
15 local tostring = tostring;
16 local pairs = pairs;
17 local type = type;
18 local io_open = io.open;
19 local select = select;
20
21 local prosody = prosody;
22 local resolve_path = require"util.paths".resolve_relative_path;
23 local config_path = prosody.paths.config;
24
25 local luasec_has_noticket, luasec_has_verifyext, luasec_has_no_compression;
26 if ssl then
27         local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
28         luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
29         luasec_has_verifyext = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
30         luasec_has_no_compression = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
31 end
32
33 module "certmanager"
34
35 -- Global SSL options if not overridden per-host
36 local global_ssl_config = configmanager.get("*", "ssl");
37
38 -- Built-in defaults
39 local core_defaults = {
40         capath = "/etc/ssl/certs";
41         protocol = "tlsv1+";
42         verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
43         options = {
44                 cipher_server_preference = true;
45                 no_ticket = luasec_has_noticket;
46                 no_compression = luasec_has_no_compression and configmanager.get("*", "ssl_compression") ~= true;
47                 -- Has no_compression? Then it has these too...
48                 single_dh_use = luasec_has_no_compression;
49                 single_ecdh_use = luasec_has_no_compression;
50         };
51         verifyext = { "lsec_continue", "lsec_ignore_purpose" };
52         curve = "secp384r1";
53         ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
54 }
55 local path_options = { -- These we pass through resolve_path()
56         key = true, certificate = true, cafile = true, capath = true, dhparam = true
57 }
58
59 if ssl and not luasec_has_verifyext and ssl.x509 then
60         -- COMPAT mw/luasec-hg
61         for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
62                 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
63         end
64 end
65
66 function create_context(host, mode, ...)
67         if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
68
69         local cfg = new_config();
70         cfg:apply(core_defaults);
71         cfg:apply(global_ssl_config);
72         cfg:apply({
73                 mode = mode,
74                 -- We can't read the password interactively when daemonized
75                 password = function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
76         });
77
78         for i = select('#', ...), 1, -1 do
79                 cfg:apply(select(i, ...));
80         end
81         local user_ssl_config = cfg:final();
82
83         if mode == "server" then
84                 if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
85                 if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
86         end
87
88         for option in pairs(path_options) do
89                 if type(user_ssl_config[option]) == "string" then
90                         user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
91                 end
92         end
93
94         -- LuaSec expects dhparam to be a callback that takes two arguments.
95         -- We ignore those because it is mostly used for having a separate
96         -- set of params for EXPORT ciphers, which we don't have by default.
97         if type(user_ssl_config.dhparam) == "string" then
98                 local f, err = io_open(user_ssl_config.dhparam);
99                 if not f then return nil, "Could not open DH parameters: "..err end
100                 local dhparam = f:read("*a");
101                 f:close();
102                 user_ssl_config.dhparam = function() return dhparam; end
103         end
104
105         local ctx, err = ssl_newcontext(user_ssl_config);
106
107         -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
108         -- of it ourselves (W/A for #x)
109         if ctx and user_ssl_config.ciphers then
110                 local success;
111                 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
112                 if not success then ctx = nil; end
113         end
114
115         if not ctx then
116                 err = err or "invalid ssl config"
117                 local file = err:match("^error loading (.-) %(");
118                 if file then
119                         if file == "private key" then
120                                 file = user_ssl_config.key or "your private key";
121                         elseif file == "certificate" then
122                                 file = user_ssl_config.certificate or "your certificate file";
123                         end
124                         local reason = err:match("%((.+)%)$") or "some reason";
125                         if reason == "Permission denied" then
126                                 reason = "Check that the permissions allow Prosody to read this file.";
127                         elseif reason == "No such file or directory" then
128                                 reason = "Check that the path is correct, and the file exists.";
129                         elseif reason == "system lib" then
130                                 reason = "Previous error (see logs), or other system error.";
131                         elseif reason == "(null)" or not reason then
132                                 reason = "Check that the file exists and the permissions are correct";
133                         else
134                                 reason = "Reason: "..tostring(reason):lower();
135                         end
136                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
137                 else
138                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
139                 end
140         end
141         return ctx, err, user_ssl_config;
142 end
143
144 function reload_ssl_config()
145         global_ssl_config = configmanager.get("*", "ssl");
146         if luasec_has_no_compression then
147                 core_defaults.options.no_compression = configmanager.get("*", "ssl_compression") ~= true;
148         end
149 end
150
151 prosody.events.add_handler("config-reloaded", reload_ssl_config);
152
153 return _M;