aboutsummaryrefslogtreecommitdiffstats
path: root/modules/ircv3_sts.cpp
blob: f63ed68ec13f20da3bfc073b484c1dee7d758969 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2017, 2019-2023 Sadie Powell <sadie@sadiepowell.dev>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"
#include "modules/cap.h"
#include "modules/tls.h"

class STSCap final
	: public Cap::Capability
{
private:
	std::string host;
	std::string plaintextpolicy;
	std::string plaintextwspolicy;
	std::string securepolicy;
	TLS::API tlsapi;

	bool OnList(LocalUser* user) override
	{
		// Don't send the cap to clients that only support cap-3.1.
		if (GetProtocol(user) == Cap::CAP_LEGACY)
			return false;

		// Don't send the cap to clients in a class which has STS disabled.
		if (!user->GetClass()->config->getBool("usests", true))
			return false;

		// Check whether we have a value for this hook type.
		auto* value = GetValue(user);
		if (!value || value->empty())
			return false; // No sts for this (probably websocket) hook.

		// Plaintext listeners have their own policy.
		const auto* tlshook = TLS::GetHook(user->io->GetSocket());
		if (!tlshook)
			return true;

		// If no hostname has been provided for the connection, an STS persistence policy SHOULD NOT be advertised.
		std::string snihost;
		if (!tlshook->GetServerName(snihost))
			return false;

		// Before advertising an STS persistence policy over a secure connection, servers SHOULD verify whether the
		// hostname provided by clients, for example, via TLS Server Name Indication (SNI), has been whitelisted by
		// administrators in the server configuration.
		return InspIRCd::Match(snihost, host, ascii_case_insensitive_map);
	}

	bool OnRequest(LocalUser* user, bool adding) override
	{
		// Clients MUST NOT request this capability with CAP REQ. Servers MAY reply with a CAP NAK message if a
		// client requests this capability.
		return false;
	}

	const std::string* GetValue(LocalUser* user) const override
	{
		auto* ios = user->io->GetSocket();
		if (TLS::GetHook(ios))
			return &securepolicy; // Normal SSL connection.

		if (tlsapi && tlsapi->GetCertificate(user))
			return &securepolicy; // Proxied SSL connection.

		auto* ioh = ios->GetIOHook();
		return ioh && insp::casemapped_equals(ioh->GetHookProvider()->service_name, "websocket")
			? &plaintextwspolicy // Plain text websocket connection.
			: &plaintextpolicy;  // Plain text connection.
	}

public:
	STSCap(const WeakModulePtr& mod)
		: Cap::Capability(mod, "sts")
		, tlsapi(mod)
	{
		DisableAutoRegister();
	}

	~STSCap() override
	{
		// TODO: Send duration=0 when STS vanishes.
	}

	void SetPolicy(const std::string& newhost, unsigned long duration, in_port_t port, in_port_t wsport, bool preload)
	{
		// To enforce an STS upgrade policy, servers MUST send this key to insecurely connected clients. Servers
		// MAY send this key to securely connected clients, but it will be ignored.
		std::string newplaintextpolicy("port=");
		newplaintextpolicy.append(ConvToStr(port));

		std::string newplaintextwspolicy;
		if (wsport)
			newplaintextwspolicy.append("port=").append(ConvToStr(wsport));

		// To enforce an STS persistence policy, servers MUST send this key to securely connected clients. Servers
		// MAY send this key to all clients, but insecurely connected clients MUST ignore it.
		std::string newsecurepolicy("duration=");
		newsecurepolicy.append(ConvToStr(duration));

		// Servers MAY send this key to all clients, but insecurely connected clients MUST ignore it.
		if (preload)
			newsecurepolicy.append(",preload");

		// Apply the new policy.
		bool changed = false;
		if (!insp::casemapped_equals(host, newhost))
		{
			ServerInstance->Logs.Debug(MODNAME, "Changing STS SNI hostname from \"{}\" to \"{}\"", host, newhost);
			host = newhost;
			changed = true;
		}

		if (plaintextpolicy != newplaintextpolicy)
		{
			ServerInstance->Logs.Debug(MODNAME, "Changing plaintext STS policy from \"{}\" to \"{}\"", plaintextpolicy, newplaintextpolicy);
			plaintextpolicy.swap(newplaintextpolicy);
			changed = true;
		}

		if (plaintextwspolicy != newplaintextwspolicy)
		{
			ServerInstance->Logs.Debug(MODNAME, "Changing plaintext WebSocket STS policy from \"{}\" to \"{}\"", plaintextwspolicy, newplaintextwspolicy);
			plaintextwspolicy.swap(newplaintextwspolicy);
			changed = true;
		}

		if (securepolicy != newsecurepolicy)
		{
			ServerInstance->Logs.Debug(MODNAME, "Changing secure STS policy from \"{}\" to \"{}\"", securepolicy, newsecurepolicy);
			securepolicy.swap(newsecurepolicy);
			changed = true;
		}

		// If the policy has changed then notify all clients via cap-notify.
		if (changed)
			NotifyValueChange();
	}
};

class ModuleIRCv3STS final
	: public Module
{
private:
	STSCap cap;

	// The IRCv3 STS specification requires that the server is listening using TLS using a valid certificate.
	static bool HasValidSSLPort(in_port_t port, bool websocket)
	{
		for (const auto* ls : ServerInstance->Ports)
		{
			ServerInstance->Logs.Debug(MODNAME, "HasValidSSLPort({}, {}): checking {} at {}",
				port, websocket, ls->bind_sa.str(), ls->bind_tag->source.str());

			// Is this listener on the right port?
			const auto saport = ls->bind_sa.port();
			if (saport != port)
			{
				ServerInstance->Logs.Debug(MODNAME, "BAD: wrong port.");
				continue;
			}

			const auto bindhook = ls->bind_tag->getString("hook");
			if (insp::casemapped_equals(bindhook, "websocket"))
			{
				if (!websocket)
				{
					ServerInstance->Logs.Debug(MODNAME, "BAD: websocket hook when we want a non-websocket hook.");
					continue; // Not a websocket connection.
				}

				// Port has a websocket hook and we want a websocket hook.
			}
			else if (websocket)
			{
				ServerInstance->Logs.Debug(MODNAME, "BAD: non-websocket hook when we want a websocket hook.");
				continue;
			}
			else if (!bindhook.empty())
			{
				if (!ls->bind_tag->getBool("sslhook"))
				{
					ServerInstance->Logs.Debug(MODNAME, "BAD: {} hook and sslhook is not set.", bindhook);
					continue; // Not explicitly marked as a SSL hook.
				}

				ServerInstance->Logs.Debug(MODNAME, "GOOD: {} hook and sslhook is set.", bindhook);
				return true; // Listener is marked as providing SSL via a proxy like HAProxy.
			}

			// Is this listener using TLS?
			if (ls->bind_tag->getString("sslprofile").empty())
			{
				ServerInstance->Logs.Debug(MODNAME, "BAD: no sslprofile.");
				continue;
			}

			// TODO: Add a way to check if a listener's TLS cert is CA-verified.
			ServerInstance->Logs.Debug(MODNAME, "GOOD: passed all checks.");
			return true;
		}

		ServerInstance->Logs.Debug(MODNAME, "BAD: nothing passed checks.");
		return false;
	}

public:
	ModuleIRCv3STS()
		: Module(VF_VENDOR | VF_OPTCOMMON, "Adds support for the IRCv3 Strict Transport Security specification.")
		, cap(weak_from_this())
	{
	}

	void ReadConfig(ConfigStatus& status) override
	{
		// TODO: Multiple SNI profiles
		const auto& tag = ServerInstance->Config->ConfValue("sts");
		if (tag == ServerInstance->Config->EmptyTag)
			throw ModuleException(weak_from_this(), "You must define a STS policy!");

		const std::string host = tag->getString("host");
		if (host.empty())
			throw ModuleException(weak_from_this(), "<sts:host> must contain a hostname, at " + tag->source.str());

		const auto port = tag->getNum<in_port_t>("port", 6697, 1);
		if (!HasValidSSLPort(port, false))
			throw ModuleException(weak_from_this(), "<sts:port> must be a TLS port, at " + tag->source.str());

		const auto wsport = tag->getNum<in_port_t>("wsport", 0);
		if (wsport && !HasValidSSLPort(wsport, true))
			throw ModuleException(weak_from_this(), "<sts:wsport> must be a TLS WebSocket port, at " + tag->source.str());

		unsigned long duration = tag->getDuration("duration", 5*60, 60);
		bool preload = tag->getBool("preload");
		cap.SetPolicy(host, duration, port, wsport, preload);

		if (!cap.IsRegistered())
			ServerInstance->Modules.AddService(cap);
	}
};

MODULE_INIT(ModuleIRCv3STS)