Merge 0.10->trunk
[prosody.git] / plugins / mod_tls.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local create_context = require "core.certmanager".create_context;
10 local st = require "util.stanza";
11
12 local c2s_require_encryption = module:get_option("c2s_require_encryption", module:get_option("require_encryption"));
13 local s2s_require_encryption = module:get_option("s2s_require_encryption");
14 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
15 local s2s_secure_auth = module:get_option("s2s_secure_auth");
16
17 if s2s_secure_auth and s2s_require_encryption == false then
18         module:log("warn", "s2s_secure_auth implies s2s_require_encryption, but s2s_require_encryption is set to false");
19         s2s_require_encryption = true;
20 end
21
22 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
23 local starttls_attr = { xmlns = xmlns_starttls };
24 local starttls_proceed = st.stanza("proceed", starttls_attr);
25 local starttls_failure = st.stanza("failure", starttls_attr);
26 local c2s_feature = st.stanza("starttls", starttls_attr);
27 local s2s_feature = st.stanza("starttls", starttls_attr);
28 if c2s_require_encryption then c2s_feature:tag("required"):up(); end
29 if s2s_require_encryption then s2s_feature:tag("required"):up(); end
30
31 local hosts = prosody.hosts;
32 local host = hosts[module.host];
33
34 local ssl_ctx_c2s, ssl_ctx_s2sout, ssl_ctx_s2sin;
35 local ssl_cfg_c2s, ssl_cfg_s2sout, ssl_cfg_s2sin;
36 do
37         local NULL = {};
38         local global = module:context("*");
39         local parent = module:context(module.host:match("%.(.*)$"));
40
41         local parent_ssl = parent:get_option("ssl");
42         local host_ssl   = module:get_option("ssl", parent_ssl);
43
44         local global_c2s = global:get_option("c2s_ssl", NULL);
45         local parent_c2s = parent:get_option("c2s_ssl", NULL);
46         local host_c2s   = module:get_option("c2s_ssl", parent_c2s);
47
48         local global_s2s = global:get_option("s2s_ssl", NULL);
49         local parent_s2s = parent:get_option("s2s_ssl", NULL);
50         local host_s2s   = module:get_option("s2s_ssl", parent_s2s);
51
52         ssl_ctx_c2s, ssl_cfg_c2s = create_context(host.host, "server", host_c2s, host_ssl, global_c2s); -- for incoming client connections
53         if not ssl_ctx_c2s then module:log("error", "Error creating context for c2s: %s", ssl_cfg_c2s); end
54
55         ssl_ctx_s2sout, ssl_cfg_s2sout = create_context(host.host, "client", host_s2s, host_ssl, global_s2s); -- for outgoing server connections
56         if not ssl_ctx_s2sout then module:log("error", "Error creating contexts for s2sout: %s", ssl_cfg_s2sin); end
57
58         ssl_ctx_s2sin, ssl_cfg_s2sin = create_context(host.host, "server", host_s2s, host_ssl, global_s2s); -- for incoming server connections
59         if not ssl_ctx_s2sin then module:log("error", "Error creating contexts for s2sin: %s", ssl_cfg_s2sin); end
60 end
61
62 local function can_do_tls(session)
63         if not session.conn.starttls then
64                 return false;
65         elseif session.ssl_ctx then
66                 return true;
67         end
68         if session.type == "c2s_unauthed" then
69                 session.ssl_ctx = ssl_ctx_c2s;
70                 session.ssl_cfg = ssl_cfg_c2s;
71         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
72                 session.ssl_ctx = ssl_ctx_s2sin;
73                 session.ssl_cfg = ssl_cfg_s2sin;
74         elseif session.direction == "outgoing" and allow_s2s_tls then
75                 session.ssl_ctx = ssl_ctx_s2sout;
76                 session.ssl_cfg = ssl_cfg_s2sout;
77         else
78                 return false;
79         end
80         return session.ssl_ctx;
81 end
82
83 -- Hook <starttls/>
84 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
85         local origin = event.origin;
86         if can_do_tls(origin) then
87                 (origin.sends2s or origin.send)(starttls_proceed);
88                 origin:reset_stream();
89                 origin.conn:starttls(origin.ssl_ctx);
90                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
91                 origin.secure = false;
92         else
93                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
94                 (origin.sends2s or origin.send)(starttls_failure);
95                 origin:close();
96         end
97         return true;
98 end);
99
100 -- Advertize stream feature
101 module:hook("stream-features", function(event)
102         local origin, features = event.origin, event.features;
103         if can_do_tls(origin) then
104                 features:add_child(c2s_feature);
105         end
106 end);
107 module:hook("s2s-stream-features", function(event)
108         local origin, features = event.origin, event.features;
109         if can_do_tls(origin) then
110                 features:add_child(s2s_feature);
111         end
112 end);
113
114 -- For s2sout connections, start TLS if we can
115 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
116         module:log("debug", "Received features element");
117         if can_do_tls(session) and stanza:get_child("starttls", xmlns_starttls) then
118                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
119                 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
120                 return true;
121         end
122 end, 500);
123
124 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
125         module:log("debug", "Proceeding with TLS on s2sout...");
126         session:reset_stream();
127         session.conn:starttls(session.ssl_ctx);
128         session.secure = false;
129         return true;
130 end);