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 config = require "core.configmanager";
10 local create_context = require "core.certmanager".create_context;
11 local st = require "util.stanza";
13 local c2s_require_encryption = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
14 local s2s_require_encryption = module:get_option("s2s_require_encryption");
15 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
16 local s2s_secure_auth = module:get_option("s2s_secure_auth");
18 if s2s_secure_auth and s2s_require_encryption == false then
19 module:log("warn", "s2s_secure_auth implies s2s_require_encryption, but s2s_require_encryption is set to false");
20 s2s_require_encryption = true;
23 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
24 local starttls_attr = { xmlns = xmlns_starttls };
25 local starttls_proceed = st.stanza("proceed", starttls_attr);
26 local starttls_failure = st.stanza("failure", starttls_attr);
27 local c2s_feature = st.stanza("starttls", starttls_attr);
28 local s2s_feature = st.stanza("starttls", starttls_attr);
29 if c2s_require_encryption then c2s_feature:tag("required"):up(); end
30 if s2s_require_encryption then s2s_feature:tag("required"):up(); end
32 local hosts = prosody.hosts;
33 local host = hosts[module.host];
35 local ssl_ctx_c2s, ssl_ctx_s2sout, ssl_ctx_s2sin;
37 local function get_ssl_cfg(typ)
38 local cfg_key = (typ and typ.."_" or "").."ssl";
39 local ssl_config = config.rawget(module.host, cfg_key);
40 if not ssl_config then
41 local base_host = module.host:match("%.(.*)");
42 ssl_config = config.get(base_host, cfg_key);
44 return ssl_config or typ and get_ssl_cfg();
47 local ssl_config, err = get_ssl_cfg("c2s");
48 ssl_ctx_c2s, err = create_context(host.host, "server", ssl_config); -- for incoming client connections
49 if err then module:log("error", "Error creating context for c2s: %s", err); end
51 ssl_config = get_ssl_cfg("s2s");
52 ssl_ctx_s2sin, err = create_context(host.host, "server", ssl_config); -- for incoming server connections
53 ssl_ctx_s2sout = create_context(host.host, "client", ssl_config); -- for outgoing server connections
54 if err then module:log("error", "Error creating context for s2s: %s", err); end -- Both would have the same issue
57 local function can_do_tls(session)
58 if not session.conn.starttls then
60 elseif session.ssl_ctx then
63 if session.type == "c2s_unauthed" then
64 session.ssl_ctx = ssl_ctx_c2s;
65 elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
66 session.ssl_ctx = ssl_ctx_s2sin;
67 elseif session.direction == "outgoing" and allow_s2s_tls then
68 session.ssl_ctx = ssl_ctx_s2sout;
72 return session.ssl_ctx;
76 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
77 local origin = event.origin;
78 if can_do_tls(origin) then
79 (origin.sends2s or origin.send)(starttls_proceed);
80 origin:reset_stream();
81 origin.conn:starttls(origin.ssl_ctx);
82 origin.log("debug", "TLS negotiation started for %s...", origin.type);
83 origin.secure = false;
85 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
86 (origin.sends2s or origin.send)(starttls_failure);
92 -- Advertize stream feature
93 module:hook("stream-features", function(event)
94 local origin, features = event.origin, event.features;
95 if can_do_tls(origin) then
96 features:add_child(c2s_feature);
99 module:hook("s2s-stream-features", function(event)
100 local origin, features = event.origin, event.features;
101 if can_do_tls(origin) then
102 features:add_child(s2s_feature);
106 -- For s2sout connections, start TLS if we can
107 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
108 module:log("debug", "Received features element");
109 if can_do_tls(session) and stanza:child_with_ns(xmlns_starttls) then
110 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
111 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
116 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
117 module:log("debug", "Proceeding with TLS on s2sout...");
118 session:reset_stream();
119 session.conn:starttls(session.ssl_ctx);
120 session.secure = false;