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:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
48 local origin, stanza = event.origin, event.stanza;
49 if origin.type == "c2s_unauthed" then
50 c2s_starttls_handler(origin, stanza);
51 elseif origin.type == "s2sin_unauthed" then
52 s2s_starttls_handler(origin, stanza);
55 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
61 local starttls_attr = { xmlns = xmlns_starttls };
62 module:add_event_hook("stream-features",
63 function (session, features)
64 if not session.username and session.conn.starttls then
65 features:tag("starttls", starttls_attr);
66 if secure_auth_only then
67 features:tag("required"):up():up();
74 module:hook("s2s-stream-features",
76 local session, features = data.session, data.features;
77 if session.to_host and session.type ~= "s2sin" and session.conn.starttls then
78 features:tag("starttls", starttls_attr):up();
79 if secure_s2s_only then
80 features:tag("required"):up():up();
87 -- For s2sout connections, start TLS if we can
88 module:hook_stanza(xmlns_stream, "features",
89 function (session, stanza)
90 module:log("debug", "Received features element");
91 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then
92 module:log("%s is offering TLS, taking up the offer...", session.to_host);
93 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
98 module:hook_stanza(xmlns_starttls, "proceed",
99 function (session, stanza)
100 module:log("debug", "Proceeding with TLS on s2sout...");
101 session:reset_stream();
102 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
103 session.conn:starttls(ssl_ctx, true);
104 session.secure = false;