2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local configmanager = require "core.configmanager";
10 local log = require "util.logger".init("certmanager");
12 local ssl_newcontext = ssl and ssl.newcontext;
14 local tostring = tostring;
16 local prosody = prosody;
17 local resolve_path = configmanager.resolve_relative_path;
18 local config_path = prosody.paths.config;
20 local luasec_has_noticket, luasec_has_verifyext;
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;
29 -- Global SSL options if not overridden per-host
30 local default_ssl_config = configmanager.get("*", "ssl");
31 local default_capath = "/etc/ssl/certs";
32 local default_verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
33 local default_options = { "no_sslv2", luasec_has_noticket and "no_ticket" or nil };
34 local default_verifyext = { "lsec_continue", "lsec_ignore_purpose" };
36 if ssl and not luasec_has_verifyext and ssl.x509 then
37 -- COMPAT mw/luasec-hg
38 for i=1,#default_verifyext do -- Remove lsec_ prefix
39 default_verify[#default_verify+1] = default_verifyext[i]:sub(6);
43 function create_context(host, mode, user_ssl_config)
44 user_ssl_config = user_ssl_config or default_ssl_config;
46 if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
47 if not user_ssl_config then return nil, "No SSL/TLS configuration present for "..host; end
51 protocol = user_ssl_config.protocol or "sslv23";
52 key = resolve_path(config_path, user_ssl_config.key);
53 password = user_ssl_config.password or function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
54 certificate = resolve_path(config_path, user_ssl_config.certificate);
55 capath = resolve_path(config_path, user_ssl_config.capath or default_capath);
56 cafile = resolve_path(config_path, user_ssl_config.cafile);
57 verify = user_ssl_config.verify or default_verify;
58 verifyext = user_ssl_config.verifyext or default_verifyext;
59 options = user_ssl_config.options or default_options;
60 depth = user_ssl_config.depth;
63 local ctx, err = ssl_newcontext(ssl_config);
65 -- LuaSec ignores the cipher list from the config, so we have to take care
66 -- of it ourselves (W/A for #x)
67 if ctx and user_ssl_config.ciphers then
69 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
70 if not success then ctx = nil; end
74 err = err or "invalid ssl config"
75 local file = err:match("^error loading (.-) %(");
77 if file == "private key" then
78 file = ssl_config.key or "your private key";
79 elseif file == "certificate" then
80 file = ssl_config.certificate or "your certificate file";
82 local reason = err:match("%((.+)%)$") or "some reason";
83 if reason == "Permission denied" then
84 reason = "Check that the permissions allow Prosody to read this file.";
85 elseif reason == "No such file or directory" then
86 reason = "Check that the path is correct, and the file exists.";
87 elseif reason == "system lib" then
88 reason = "Previous error (see logs), or other system error.";
89 elseif reason == "(null)" or not reason then
90 reason = "Check that the file exists and the permissions are correct";
92 reason = "Reason: "..tostring(reason):lower();
94 log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
96 log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
102 function reload_ssl_config()
103 default_ssl_config = configmanager.get("*", "ssl");
106 prosody.events.add_handler("config-reloaded", reload_ssl_config);