2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
6 * Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2010 Adam <Adam@anope.org>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
13 * Copyright (C) 2006-2007, 2009-2010 Craig Edwards <brain@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "modules/ssl.h"
31 #include "modules/webirc.h"
32 #include "modules/whois.h"
33 #include "modules/who.h"
38 RPL_WHOISCERTFP = 276,
44 class SSLCertExt : public ExtensionItem
47 SSLCertExt(Module* parent)
48 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
52 ssl_cert* get(const Extensible* item) const
54 return static_cast<ssl_cert*>(get_raw(item));
57 void set(Extensible* item, ssl_cert* value)
59 value->refcount_inc();
60 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
61 if (old && old->refcount_dec())
65 void unset(Extensible* container)
67 free(container, unset_raw(container));
70 std::string ToNetwork(const Extensible* container, void* item) const CXX11_OVERRIDE
72 return static_cast<ssl_cert*>(item)->GetMetaLine();
75 void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE
77 ssl_cert* cert = new ssl_cert;
80 std::stringstream s(value);
84 cert->invalid = (v.find('v') != std::string::npos);
85 cert->trusted = (v.find('T') != std::string::npos);
86 cert->revoked = (v.find('R') != std::string::npos);
87 cert->unknownsigner = (v.find('s') != std::string::npos);
88 if (v.find('E') != std::string::npos)
90 getline(s,cert->error,'\n');
94 getline(s,cert->fingerprint,' ');
95 getline(s,cert->dn,' ');
96 getline(s,cert->issuer,'\n');
100 void free(Extensible* container, void* item) CXX11_OVERRIDE
102 ssl_cert* old = static_cast<ssl_cert*>(item);
103 if (old && old->refcount_dec())
108 class UserCertificateAPIImpl : public UserCertificateAPIBase
111 LocalIntExt nosslext;
114 UserCertificateAPIImpl(Module* mod)
115 : UserCertificateAPIBase(mod)
116 , nosslext("no_ssl_cert", ExtensionItem::EXT_USER, mod)
121 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
123 ssl_cert* cert = sslext.get(user);
127 LocalUser* luser = IS_LOCAL(user);
128 if (!luser || nosslext.get(luser))
131 cert = SSLClientCert::GetCertificate(&luser->eh);
135 SetCertificate(user, cert);
139 void SetCertificate(User* user, ssl_cert* cert) CXX11_OVERRIDE
141 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Setting TLS (SSL) client certificate for %s: %s",
142 user->GetFullHost().c_str(), cert->GetMetaLine().c_str());
143 sslext.set(user, cert);
147 class CommandSSLInfo : public SplitCommand
150 UserCertificateAPIImpl sslapi;
153 CommandSSLInfo(Module* Creator)
154 : SplitCommand(Creator, "SSLINFO", 1)
157 allow_empty_last_param = false;
161 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
163 User* target = ServerInstance->FindNickOnly(parameters[0]);
164 if ((!target) || (target->registered != REG_ALL))
166 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
170 if (operonlyfp && !user->IsOper() && target != user)
172 user->WriteNotice("*** You cannot view TLS (SSL) client certificate information for other users");
176 ssl_cert* cert = sslapi.GetCertificate(target);
179 user->WriteNotice(InspIRCd::Format("*** %s is not connected using TLS (SSL).", target->nick.c_str()));
181 else if (cert->GetError().length())
183 user->WriteNotice(InspIRCd::Format("*** %s is connected using TLS (SSL) but has not specified a valid client certificate (%s).",
184 target->nick.c_str(), cert->GetError().c_str()));
188 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
189 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
190 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
199 , public WebIRC::EventListener
200 , public Whois::EventListener
201 , public Who::EventListener
206 bool MatchFP(ssl_cert* const cert, const std::string& fp) const
208 return irc::spacesepstream(fp).Contains(cert->GetFingerprint());
213 : WebIRC::EventListener(this)
214 , Whois::EventListener(this)
215 , Who::EventListener(this)
220 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
222 ConfigTag* tag = ServerInstance->Config->ConfValue("sslinfo");
223 cmd.operonlyfp = tag->getBool("operonly");
226 Version GetVersion() CXX11_OVERRIDE
228 return Version("Adds user facing TLS (SSL) information, various TLS (SSL) configuration options, and the /SSLINFO command to look up TLS (SSL) certificate information for other users.", VF_VENDOR);
231 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
233 ssl_cert* cert = cmd.sslapi.GetCertificate(whois.GetTarget());
236 whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
237 if ((!cmd.operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
238 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has TLS (SSL) client certificate fingerprint %s", cert->fingerprint.c_str()));
242 ModResult OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE
245 if (!request.GetFieldIndex('f', flag_index))
246 return MOD_RES_PASSTHRU;
248 ssl_cert* cert = cmd.sslapi.GetCertificate(user);
250 numeric.GetParams()[flag_index].push_back('s');
252 return MOD_RES_PASSTHRU;
255 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
257 if ((command == "OPER") && (validated))
259 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
260 if (i != ServerInstance->Config->oper_blocks.end())
262 OperInfo* ifo = i->second;
263 ssl_cert* cert = cmd.sslapi.GetCertificate(user);
265 if (ifo->oper_block->getBool("sslonly") && !cert)
267 user->WriteNumeric(ERR_NOOPERHOST, "Invalid oper credentials");
268 user->CommandFloodPenalty += 10000;
269 ServerInstance->SNO->WriteGlobalSno('o', "WARNING! Failed oper attempt by %s using login '%s': a secure connection is required.", user->GetFullRealHost().c_str(), parameters[0].c_str());
273 std::string fingerprint;
274 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || !MatchFP(cert, fingerprint)))
276 user->WriteNumeric(ERR_NOOPERHOST, "Invalid oper credentials");
277 user->CommandFloodPenalty += 10000;
278 ServerInstance->SNO->WriteGlobalSno('o', "WARNING! Failed oper attempt by %s using login '%s': their TLS (SSL) client certificate fingerprint does not match.", user->GetFullRealHost().c_str(), parameters[0].c_str());
284 // Let core handle it for extra stuff
285 return MOD_RES_PASSTHRU;
288 void OnPostConnect(User* user) CXX11_OVERRIDE
290 LocalUser* const localuser = IS_LOCAL(user);
294 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
295 if (!ssliohook || cmd.sslapi.nosslext.get(localuser))
298 ssl_cert* const cert = ssliohook->GetCertificate();
300 std::string text = "*** You are connected to ";
301 if (!ssliohook->GetServerName(text))
302 text.append(ServerInstance->Config->GetServerName());
303 text.append(" using TLS (SSL) cipher '");
304 ssliohook->GetCiphersuite(text);
305 text.push_back('\'');
306 if (cert && !cert->GetFingerprint().empty())
307 text.append(" and your TLS (SSL) client certificate fingerprint is ").append(cert->GetFingerprint());
308 user->WriteNotice(text);
313 // Find an auto-oper block for this user
314 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
316 OperInfo* ifo = i->second;
317 std::string fp = ifo->oper_block->getString("fingerprint");
318 if (MatchFP(cert, fp) && ifo->oper_block->getBool("autologin"))
323 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
325 ssl_cert* cert = cmd.sslapi.GetCertificate(user);
326 const char* error = NULL;
327 const std::string requiressl = myclass->config->getString("requiressl");
328 if (stdalgo::string::equalsci(requiressl, "trusted"))
330 if (!cert || !cert->IsCAVerified())
331 error = "a trusted TLS (SSL) client certificate";
333 else if (myclass->config->getBool("requiressl"))
336 error = "a TLS (SSL) connection";
341 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "The %s connect class is not suitable as it requires %s",
342 myclass->GetName().c_str(), error);
346 return MOD_RES_PASSTHRU;
349 void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
351 // We are only interested in connection flags. If none have been
352 // given then we have nothing to do.
356 // We only care about the tls connection flag if the connection
357 // between the gateway and the server is secure.
358 if (!cmd.sslapi.GetCertificate(user))
361 WebIRC::FlagMap::const_iterator iter = flags->find("secure");
362 if (iter == flags->end())
364 // If this is not set then the connection between the client and
365 // the gateway is not secure.
366 cmd.sslapi.nosslext.set(user, 1);
367 cmd.sslapi.sslext.unset(user);
371 // Create a fake ssl_cert for the user.
372 ssl_cert* cert = new ssl_cert;
373 cert->error = "WebIRC users can not specify valid certs yet";
374 cert->invalid = true;
375 cert->revoked = true;
376 cert->trusted = false;
377 cert->unknownsigner = true;
378 cmd.sslapi.SetCertificate(user, cert);
382 MODULE_INIT(ModuleSSLInfo)