Merge 0.9->0.10
[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_initiate= st.stanza("starttls", starttls_attr);
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
31
32 local hosts = prosody.hosts;
33 local host = hosts[module.host];
34
35 local ssl_ctx_c2s, ssl_ctx_s2sout, ssl_ctx_s2sin;
36 local ssl_cfg_c2s, ssl_cfg_s2sout, ssl_cfg_s2sin;
37 do
38         local NULL, err = {};
39         local global = module:context("*");
40         local parent = module:context(module.host:match("%.(.*)$"));
41
42         local parent_ssl = parent:get_option("ssl");
43         local host_ssl   = module:get_option("ssl", parent_ssl);
44
45         local global_c2s = global:get_option("c2s_ssl", NULL);
46         local parent_c2s = parent:get_option("c2s_ssl", NULL);
47         local host_c2s   = module:get_option("c2s_ssl", parent_c2s);
48
49         local global_s2s = global:get_option("s2s_ssl", NULL);
50         local parent_s2s = parent:get_option("s2s_ssl", NULL);
51         local host_s2s   = module:get_option("s2s_ssl", parent_s2s);
52
53         ssl_ctx_c2s, err, ssl_cfg_c2s = create_context(host.host, "server", host_c2s, host_ssl, global_c2s); -- for incoming client connections
54         if not ssl_ctx_c2s then module:log("error", "Error creating context for c2s: %s", err); end
55
56         ssl_ctx_s2sout, err, ssl_cfg_s2sout = create_context(host.host, "client", host_s2s, host_ssl, global_s2s); -- for outgoing server connections
57         if not ssl_ctx_s2sout then module:log("error", "Error creating contexts for s2sout: %s", err); end
58
59         ssl_ctx_s2sin, err, ssl_cfg_s2sin = create_context(host.host, "server", host_s2s, host_ssl, global_s2s); -- for incoming server connections
60         if not ssl_ctx_s2sin then module:log("error", "Error creating contexts for s2sin: %s", err); end
61 end
62
63 local function can_do_tls(session)
64         if session.ssl_ctx == false or not session.conn.starttls then
65                 return false;
66         elseif session.ssl_ctx then
67                 return true;
68         end
69         if session.type == "c2s_unauthed" then
70                 session.ssl_ctx = ssl_ctx_c2s;
71                 session.ssl_cfg = ssl_cfg_c2s;
72         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
73                 session.ssl_ctx = ssl_ctx_s2sin;
74                 session.ssl_cfg = ssl_cfg_s2sin;
75         elseif session.direction == "outgoing" and allow_s2s_tls then
76                 session.ssl_ctx = ssl_ctx_s2sout;
77                 session.ssl_cfg = ssl_cfg_s2sout;
78         else
79                 return false;
80         end
81         return session.ssl_ctx;
82 end
83
84 -- Hook <starttls/>
85 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
86         local origin = event.origin;
87         if can_do_tls(origin) then
88                 (origin.sends2s or origin.send)(starttls_proceed);
89                 origin:reset_stream();
90                 origin.conn:starttls(origin.ssl_ctx);
91                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
92                 origin.secure = false;
93         else
94                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
95                 (origin.sends2s or origin.send)(starttls_failure);
96                 origin:close();
97         end
98         return true;
99 end);
100
101 -- Advertize stream feature
102 module:hook("stream-features", function(event)
103         local origin, features = event.origin, event.features;
104         if can_do_tls(origin) then
105                 features:add_child(c2s_feature);
106         end
107 end);
108 module:hook("s2s-stream-features", function(event)
109         local origin, features = event.origin, event.features;
110         if can_do_tls(origin) then
111                 features:add_child(s2s_feature);
112         end
113 end);
114
115 -- For s2sout connections, start TLS if we can
116 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
117         module:log("debug", "Received features element");
118         if can_do_tls(session) and stanza:get_child("starttls", xmlns_starttls) then
119                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
120                 session.sends2s(starttls_initiate);
121                 return true;
122         end
123 end, 500);
124
125 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
126         module:log("debug", "Proceeding with TLS on s2sout...");
127         session:reset_stream();
128         session.conn:starttls(session.ssl_ctx);
129         session.secure = false;
130         return true;
131 end);