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