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 io_open = io.open;
18 local prosody = prosody;
19 local resolve_path = configmanager.resolve_relative_path;
20 local config_path = prosody.paths.config;
22 local luasec_has_noticket, luasec_has_verifyext, luasec_has_no_compression;
24 local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
25 luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
26 luasec_has_verifyext = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
27 luasec_has_no_compression = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
32 -- Global SSL options if not overridden per-host
33 local default_ssl_config = configmanager.get("*", "ssl");
34 local default_capath = "/etc/ssl/certs";
35 local default_verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
36 local default_options = { "no_sslv2", "no_sslv3", "cipher_server_preference", luasec_has_noticket and "no_ticket" or nil };
37 local default_verifyext = { "lsec_continue", "lsec_ignore_purpose" };
39 if ssl and not luasec_has_verifyext and ssl.x509 then
40 -- COMPAT mw/luasec-hg
41 for i=1,#default_verifyext do -- Remove lsec_ prefix
42 default_verify[#default_verify+1] = default_verifyext[i]:sub(6);
45 if luasec_has_no_compression and configmanager.get("*", "ssl_compression") ~= true then
46 default_options[#default_options+1] = "no_compression";
49 if luasec_has_no_compression then -- Has no_compression? Then it has these too...
50 default_options[#default_options+1] = "single_dh_use";
51 default_options[#default_options+1] = "single_ecdh_use";
54 function create_context(host, mode, user_ssl_config)
55 user_ssl_config = user_ssl_config or default_ssl_config;
57 if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
58 if not user_ssl_config then return nil, "No SSL/TLS configuration present for "..host; end
62 protocol = user_ssl_config.protocol or "sslv23";
63 key = resolve_path(config_path, user_ssl_config.key);
64 password = user_ssl_config.password or function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
65 certificate = resolve_path(config_path, user_ssl_config.certificate);
66 capath = resolve_path(config_path, user_ssl_config.capath or default_capath);
67 cafile = resolve_path(config_path, user_ssl_config.cafile);
68 verify = user_ssl_config.verify or default_verify;
69 verifyext = user_ssl_config.verifyext or default_verifyext;
70 options = user_ssl_config.options or default_options;
71 depth = user_ssl_config.depth;
72 curve = user_ssl_config.curve or "secp384r1";
73 ciphers = user_ssl_config.ciphers or "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
74 dhparam = user_ssl_config.dhparam;
77 -- LuaSec expects dhparam to be a callback that takes two arguments.
78 -- We ignore those because it is mostly used for having a separate
79 -- set of params for EXPORT ciphers, which we don't have by default.
80 if type(ssl_config.dhparam) == "string" then
81 local f, err = io_open(resolve_path(config_path, ssl_config.dhparam));
82 if not f then return nil, "Could not open DH parameters: "..err end
83 local dhparam = f:read("*a");
85 ssl_config.dhparam = function() return dhparam; end
88 local ctx, err = ssl_newcontext(ssl_config);
90 -- COMPAT: LuaSec 0.4.1 ignores the cipher list from the config, so we have to take
91 -- care of it ourselves...
92 if ctx and ssl_config.ciphers then
94 success, err = ssl.context.setcipher(ctx, ssl_config.ciphers);
95 if not success then ctx = nil; end
99 err = err or "invalid ssl config"
100 local file = err:match("^error loading (.-) %(");
102 if file == "private key" then
103 file = ssl_config.key or "your private key";
104 elseif file == "certificate" then
105 file = ssl_config.certificate or "your certificate file";
107 local reason = err:match("%((.+)%)$") or "some reason";
108 if reason == "Permission denied" then
109 reason = "Check that the permissions allow Prosody to read this file.";
110 elseif reason == "No such file or directory" then
111 reason = "Check that the path is correct, and the file exists.";
112 elseif reason == "system lib" then
113 reason = "Previous error (see logs), or other system error.";
114 elseif reason == "(null)" or not reason then
115 reason = "Check that the file exists and the permissions are correct";
117 reason = "Reason: "..tostring(reason):lower();
119 log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
121 log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
127 function reload_ssl_config()
128 default_ssl_config = configmanager.get("*", "ssl");
131 prosody.events.add_handler("config-reloaded", reload_ssl_config);