Merge with daurnimator
[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 local t_insert = table.insert;
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 local set_options = {
59         options = true, verify = true, verifyext = true
60 }
61
62 if ssl and not luasec_has_verifyext and ssl.x509 then
63         -- COMPAT mw/luasec-hg
64         for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
65                 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
66         end
67 end
68
69 local function merge_set(t, o)
70         if type(t) ~= "table" then t = { t } end
71         for k,v in pairs(t) do
72                 if v == true or v == false then
73                         o[k] = v;
74                 else
75                         o[v] = true;
76                 end
77         end
78         return o;
79 end
80
81 local protocols = { "sslv2", "sslv3", "tlsv1", "tlsv1_1", "tlsv1_2" };
82 for i = 1, #protocols do protocols[protocols[i] .. "+"] = i - 1; end
83
84 function create_context(host, mode, user_ssl_config)
85         user_ssl_config = user_ssl_config or {}
86         user_ssl_config.mode = mode;
87
88         if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
89
90         if global_ssl_config then
91                 for option,default_value in pairs(global_ssl_config) do
92                         if user_ssl_config[option] == nil then
93                                 user_ssl_config[option] = default_value;
94                         end
95                 end
96         end
97
98         for option,default_value in pairs(core_defaults) do
99                 if user_ssl_config[option] == nil then
100                         user_ssl_config[option] = default_value;
101                 end
102         end
103
104         for option in pairs(set_options) do
105                 local merged = {};
106                 merge_set(core_defaults[option], merged);
107                 if global_ssl_config then
108                         merge_set(global_ssl_config[option], merged);
109                 end
110                 merge_set(user_ssl_config[option], merged);
111                 local final_array = {};
112                 for opt, enable in pairs(merged) do
113                         if enable then
114                                 final_array[#final_array+1] = opt;
115                         end
116                 end
117                 user_ssl_config[option] = final_array;
118         end
119
120         local min_protocol = protocols[user_ssl_config.protocol];
121         if min_protocol then
122                 user_ssl_config.protocol = "sslv23";
123                 for i = 1, min_protocol do
124                         t_insert(user_ssl_config.options, "no_"..protocols[i]);
125                 end
126         end
127
128         -- We can't read the password interactively when daemonized
129         user_ssl_config.password = user_ssl_config.password or
130                 function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
131
132         for option in pairs(path_options) do
133                 if type(user_ssl_config[option]) == "string" then
134                         user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
135                 end
136         end
137
138         -- Allow the cipher list to be a table
139         if type(user_ssl_config.ciphers) == "table" then
140                 user_ssl_config.ciphers = t_concat(user_ssl_config.ciphers, ":")
141         end
142
143         if mode == "server" then
144                 if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
145                 if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
146         end
147
148         -- LuaSec expects dhparam to be a callback that takes two arguments.
149         -- We ignore those because it is mostly used for having a separate
150         -- set of params for EXPORT ciphers, which we don't have by default.
151         if type(user_ssl_config.dhparam) == "string" then
152                 local f, err = io_open(user_ssl_config.dhparam);
153                 if not f then return nil, "Could not open DH parameters: "..err end
154                 local dhparam = f:read("*a");
155                 f:close();
156                 user_ssl_config.dhparam = function() return dhparam; end
157         end
158
159         local ctx, err = ssl_newcontext(user_ssl_config);
160
161         -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
162         -- of it ourselves (W/A for #x)
163         if ctx and user_ssl_config.ciphers then
164                 local success;
165                 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
166                 if not success then ctx = nil; end
167         end
168
169         if not ctx then
170                 err = err or "invalid ssl config"
171                 local file = err:match("^error loading (.-) %(");
172                 if file then
173                         if file == "private key" then
174                                 file = user_ssl_config.key or "your private key";
175                         elseif file == "certificate" then
176                                 file = user_ssl_config.certificate or "your certificate file";
177                         end
178                         local reason = err:match("%((.+)%)$") or "some reason";
179                         if reason == "Permission denied" then
180                                 reason = "Check that the permissions allow Prosody to read this file.";
181                         elseif reason == "No such file or directory" then
182                                 reason = "Check that the path is correct, and the file exists.";
183                         elseif reason == "system lib" then
184                                 reason = "Previous error (see logs), or other system error.";
185                         elseif reason == "(null)" or not reason then
186                                 reason = "Check that the file exists and the permissions are correct";
187                         else
188                                 reason = "Reason: "..tostring(reason):lower();
189                         end
190                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
191                 else
192                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
193                 end
194         end
195         return ctx, err;
196 end
197
198 function reload_ssl_config()
199         global_ssl_config = configmanager.get("*", "ssl");
200         if luasec_has_no_compression then
201                 core_defaults.options.no_compression = configmanager.get("*", "ssl_compression") ~= true;
202         end
203 end
204
205 prosody.events.add_handler("config-reloaded", reload_ssl_config);
206
207 return _M;