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