2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local st = require "util.stanza";
11 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
12 local secure_s2s_only = module:get_option("s2s_require_encryption");
14 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
15 local starttls_attr = { xmlns = xmlns_starttls };
16 local starttls_proceed = st.stanza("proceed", starttls_attr);
17 local starttls_failure = st.stanza("failure", starttls_attr);
18 local c2s_feature = st.stanza("starttls", starttls_attr);
19 local s2s_feature = st.stanza("starttls", starttls_attr);
20 if secure_auth_only then c2s_feature:tag("required"):up(); end
21 if secure_s2s_only then s2s_feature:tag("required"):up(); end
23 local global_ssl_ctx = prosody.global_ssl_ctx;
26 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
27 local origin = event.origin;
28 if origin.conn.starttls then
29 (origin.sends2s or origin.send)(starttls_proceed);
30 origin:reset_stream();
31 local host = origin.to_host or origin.host;
32 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx;
33 origin.conn:starttls(ssl_ctx);
34 origin.log("info", "TLS negotiation started for %s...", origin.type);
35 origin.secure = false;
37 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
38 (origin.sends2s or origin.send)(starttls_failure);
44 -- Advertize stream feature
45 module:add_event_hook("stream-features", function(session, features)
46 if not session.username and session.conn.starttls then
47 features:add_child(c2s_feature);
50 module:hook("s2s-stream-features", function(event)
51 local session, features = event.session, event.features;
52 if session.to_host and session.type ~= "s2sin" and session.conn.starttls then
53 features:add_child(s2s_feature);
57 -- For s2sout connections, start TLS if we can
58 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
59 module:log("debug", "Received features element");
60 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then
61 module:log("%s is offering TLS, taking up the offer...", session.to_host);
62 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
66 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
67 module:log("debug", "Proceeding with TLS on s2sout...");
68 session:reset_stream();
69 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
70 session.conn:starttls(ssl_ctx, true);
71 session.secure = false;