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