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 xmlns_stream = 'http://etherx.jabber.org/streams';
12 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
14 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
15 local secure_s2s_only = module:get_option("s2s_require_encryption");
17 local global_ssl_ctx = prosody.global_ssl_ctx;
19 function c2s_starttls_handler(session, stanza)
20 if session.conn.starttls then
21 session.send(st.stanza("proceed", { xmlns = xmlns_starttls }));
22 session:reset_stream();
23 local ssl_ctx = session.host and hosts[session.host].ssl_ctx_in or global_ssl_ctx;
24 session.conn:starttls(ssl_ctx);
25 session.log("info", "TLS negotiation started...");
26 session.secure = false;
29 session.log("warn", "Attempt to start TLS, but TLS is not available on this connection");
33 function s2s_starttls_handler(session, stanza)
34 if session.conn.starttls then
35 session.sends2s(st.stanza("proceed", { xmlns = xmlns_starttls }));
36 session:reset_stream();
37 local ssl_ctx = session.to_host and hosts[session.to_host].ssl_ctx_in or global_ssl_ctx;
38 session.conn:starttls(ssl_ctx);
39 session.log("info", "TLS negotiation started for incoming s2s...");
40 session.secure = false;
43 session.log("warn", "Attempt to start TLS, but TLS is not available on this s2s connection");
47 module:add_handler("c2s_unauthed", "starttls", xmlns_starttls, c2s_starttls_handler);
48 module:add_handler("s2sin_unauthed", "starttls", xmlns_starttls, s2s_starttls_handler);
51 local starttls_attr = { xmlns = xmlns_starttls };
52 module:add_event_hook("stream-features",
53 function (session, features)
54 if not session.username and session.conn.starttls then
55 features:tag("starttls", starttls_attr);
56 if secure_auth_only then
57 features:tag("required"):up():up();
64 module:hook("s2s-stream-features",
66 local session, features = data.session, data.features;
67 if session.to_host and session.type ~= "s2sin" and session.conn.starttls then
68 features:tag("starttls", starttls_attr):up();
69 if secure_s2s_only then
70 features:tag("required"):up():up();
77 -- For s2sout connections, start TLS if we can
78 module:hook_stanza(xmlns_stream, "features",
79 function (session, stanza)
80 module:log("debug", "Received features element");
81 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then
82 module:log("%s is offering TLS, taking up the offer...", session.to_host);
83 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
88 module:hook_stanza(xmlns_starttls, "proceed",
89 function (session, stanza)
90 module:log("debug", "Proceeding with TLS on s2sout...");
91 session:reset_stream();
92 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
93 session.conn:starttls(ssl_ctx, true);
94 session.secure = false;