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