Remove all trailing whitespace
[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
17 local prosody = prosody;
18 local resolve_path = configmanager.resolve_relative_path;
19 local config_path = prosody.paths.config;
20
21 local luasec_has_noticket, luasec_has_verifyext, luasec_has_no_compression;
22 if ssl then
23         local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
24         luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
25         luasec_has_verifyext = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
26         luasec_has_no_compression = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
27 end
28
29 module "certmanager"
30
31 -- Global SSL options if not overridden per-host
32 local global_ssl_config = configmanager.get("*", "ssl");
33
34 local core_defaults = {
35         capath = "/etc/ssl/certs";
36         protocol = "sslv23";
37         verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
38         options = { "no_sslv2", luasec_has_noticket and "no_ticket" or nil };
39         verifyext = { "lsec_continue", "lsec_ignore_purpose" };
40         curve = "secp384r1";
41         ciphers = "HIGH:!DSS:!aNULL@STRENGTH";
42 }
43 local path_options = { -- These we pass through resolve_path()
44         key = true, certificate = true, cafile = true, capath = true
45 }
46
47 if ssl and not luasec_has_verifyext and ssl.x509 then
48         -- COMPAT mw/luasec-hg
49         for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
50                 core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
51         end
52 end
53
54 if luasec_has_no_compression and configmanager.get("*", "ssl_compression") ~= true then
55         core_defaults.options[#core_defaults.options+1] = "no_compression";
56 end
57
58 function create_context(host, mode, user_ssl_config)
59         user_ssl_config = user_ssl_config or {}
60         user_ssl_config.mode = mode;
61
62         if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
63
64         if global_ssl_config then
65                 for option,default_value in pairs(global_ssl_config) do
66                         if not user_ssl_config[option] then
67                                 user_ssl_config[option] = default_value;
68                         end
69                 end
70         end
71         for option,default_value in pairs(core_defaults) do
72                 if not user_ssl_config[option] then
73                         user_ssl_config[option] = default_value;
74                 end
75         end
76         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;
77         for option in pairs(path_options) do
78                 user_ssl_config[option] = user_ssl_config[option] and resolve_path(config_path, user_ssl_config[option]);
79         end
80
81         if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
82         if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
83
84         local ctx, err = ssl_newcontext(user_ssl_config);
85
86         -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
87         -- of it ourselves (W/A for #x)
88         if ctx and user_ssl_config.ciphers then
89                 local success;
90                 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
91                 if not success then ctx = nil; end
92         end
93
94         if not ctx then
95                 err = err or "invalid ssl config"
96                 local file = err:match("^error loading (.-) %(");
97                 if file then
98                         if file == "private key" then
99                                 file = user_ssl_config.key or "your private key";
100                         elseif file == "certificate" then
101                                 file = user_ssl_config.certificate or "your certificate file";
102                         end
103                         local reason = err:match("%((.+)%)$") or "some reason";
104                         if reason == "Permission denied" then
105                                 reason = "Check that the permissions allow Prosody to read this file.";
106                         elseif reason == "No such file or directory" then
107                                 reason = "Check that the path is correct, and the file exists.";
108                         elseif reason == "system lib" then
109                                 reason = "Previous error (see logs), or other system error.";
110                         elseif reason == "(null)" or not reason then
111                                 reason = "Check that the file exists and the permissions are correct";
112                         else
113                                 reason = "Reason: "..tostring(reason):lower();
114                         end
115                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
116                 else
117                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
118                 end
119         end
120         return ctx, err;
121 end
122
123 function reload_ssl_config()
124         global_ssl_config = configmanager.get("*", "ssl");
125 end
126
127 prosody.events.add_handler("config-reloaded", reload_ssl_config);
128
129 return _M;