mod_tls: Refactor to allow separate SSL configuration for c2s and s2s connections
[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 config = require "core.configmanager";
10 local create_context = require "core.certmanager".create_context;
11 local st = require "util.stanza";
12
13 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
14 local secure_s2s_only = module:get_option("s2s_require_encryption");
15 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
16
17 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
18 local starttls_attr = { xmlns = xmlns_starttls };
19 local starttls_proceed = st.stanza("proceed", starttls_attr);
20 local starttls_failure = st.stanza("failure", starttls_attr);
21 local c2s_feature = st.stanza("starttls", starttls_attr);
22 local s2s_feature = st.stanza("starttls", starttls_attr);
23 if secure_auth_only then c2s_feature:tag("required"):up(); end
24 if secure_s2s_only then s2s_feature:tag("required"):up(); end
25
26 local hosts = prosody.hosts;
27 local host = hosts[module.host];
28
29 local ssl_ctx_c2s, ssl_ctx_s2sout, ssl_ctx_s2sin;
30 do
31         local function get_ssl_cfg(typ)
32                 local cfg_key = (typ and typ.."_" or "").."ssl";
33                 local ssl_config = config.rawget(module.host, cfg_key);
34                 if not ssl_config then
35                         local base_host = module.host:match("%.(.*)");
36                         ssl_config = config.get(base_host, cfg_key);
37                 end
38                 return ssl_config or typ and get_ssl_cfg();
39         end
40
41         local ssl_config, err = get_ssl_cfg("c2s");
42         ssl_ctx_c2s, err = create_context(host.host, "server", ssl_config); -- for incoming client connections
43         if err then module:log("error", "Error creating context for c2s: %s", err); end
44
45         ssl_config = get_ssl_cfg("s2s");
46         ssl_ctx_s2sin, err = create_context(host.host, "server", ssl_config); -- for incoming server connections
47         ssl_ctx_s2sout = create_context(host.host, "client", ssl_config); -- for outgoing server connections
48         if err then module:log("error", "Error creating context for s2s: %s", err); end -- Both would have the same issue
49 end
50
51 local function can_do_tls(session)
52         if not session.conn.starttls then
53                 return false;
54         elseif session.ssl_ctx then
55                 return true;
56         end
57         if session.type == "c2s_unauthed" then
58                 module:log("debug", "session.ssl_ctx = ssl_ctx_c2s;")
59                 session.ssl_ctx = ssl_ctx_c2s;
60         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
61                 session.ssl_ctx = ssl_ctx_s2sin;
62         elseif session.direction == "outgoing" and allow_s2s_tls then
63                 session.ssl_ctx = ssl_ctx_s2sout;
64         else
65                 return false;
66         end
67         return session.ssl_ctx;
68 end
69
70 -- Hook <starttls/>
71 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
72         local origin = event.origin;
73         if can_do_tls(origin) then
74                 (origin.sends2s or origin.send)(starttls_proceed);
75                 origin:reset_stream();
76                 origin.conn:starttls(origin.ssl_ctx);
77                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
78                 origin.secure = false;
79         else
80                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
81                 (origin.sends2s or origin.send)(starttls_failure);
82                 origin:close();
83         end
84         return true;
85 end);
86
87 -- Advertize stream feature
88 module:hook("stream-features", function(event)
89         local origin, features = event.origin, event.features;
90         if can_do_tls(origin) then
91                 features:add_child(c2s_feature);
92         end
93 end);
94 module:hook("s2s-stream-features", function(event)
95         local origin, features = event.origin, event.features;
96         if can_do_tls(origin) then
97                 features:add_child(s2s_feature);
98         end
99 end);
100
101 -- For s2sout connections, start TLS if we can
102 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
103         module:log("debug", "Received features element");
104         if can_do_tls(session) and stanza:child_with_ns(xmlns_starttls) then
105                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
106                 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
107                 return true;
108         end
109 end, 500);
110
111 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
112         module:log("debug", "Proceeding with TLS on s2sout...");
113         session:reset_stream();
114         session.conn:starttls(session.ssl_ctx);
115         session.secure = false;
116         return true;
117 end);