2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "modules/ssl.h"
22 #include "modules/webirc.h"
23 #include "modules/whois.h"
28 RPL_WHOISCERTFP = 276,
34 class SSLCertExt : public ExtensionItem {
36 SSLCertExt(Module* parent)
37 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
41 ssl_cert* get(const Extensible* item) const
43 return static_cast<ssl_cert*>(get_raw(item));
45 void set(Extensible* item, ssl_cert* value)
47 value->refcount_inc();
48 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
49 if (old && old->refcount_dec())
53 void unset(Extensible* container)
55 void* old = unset_raw(container);
56 delete static_cast<std::string*>(old);
59 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
61 return static_cast<ssl_cert*>(item)->GetMetaLine();
64 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
66 ssl_cert* cert = new ssl_cert;
69 std::stringstream s(value);
73 cert->invalid = (v.find('v') != std::string::npos);
74 cert->trusted = (v.find('T') != std::string::npos);
75 cert->revoked = (v.find('R') != std::string::npos);
76 cert->unknownsigner = (v.find('s') != std::string::npos);
77 if (v.find('E') != std::string::npos)
79 getline(s,cert->error,'\n');
83 getline(s,cert->fingerprint,' ');
84 getline(s,cert->dn,' ');
85 getline(s,cert->issuer,'\n');
89 void free(Extensible* container, void* item) CXX11_OVERRIDE
91 ssl_cert* old = static_cast<ssl_cert*>(item);
92 if (old && old->refcount_dec())
99 class CommandSSLInfo : public Command
104 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
106 this->syntax = "<nick>";
109 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
111 User* target = ServerInstance->FindNickOnly(parameters[0]);
113 if ((!target) || (target->registered != REG_ALL))
115 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
118 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
119 if (operonlyfp && !user->IsOper() && target != user)
121 user->WriteNotice("*** You cannot view SSL certificate information for other users");
124 ssl_cert* cert = CertExt.get(target);
127 user->WriteNotice("*** No SSL certificate for this user");
129 else if (cert->GetError().length())
131 user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
135 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
136 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
137 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
143 class UserCertificateAPIImpl : public UserCertificateAPIBase
148 UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
149 : UserCertificateAPIBase(mod), ext(certext)
153 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
155 return ext.get(user);
158 void SetCertificate(User* user, ssl_cert* cert) CXX11_OVERRIDE
166 , public WebIRC::EventListener
167 , public Whois::EventListener
170 UserCertificateAPIImpl APIImpl;
174 : WebIRC::EventListener(this)
175 , Whois::EventListener(this)
177 , APIImpl(this, cmd.CertExt)
181 Version GetVersion() CXX11_OVERRIDE
183 return Version("SSL Certificate Utilities", VF_VENDOR);
186 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
188 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
191 whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
192 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
193 if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
194 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
198 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
200 if ((command == "OPER") && (validated))
202 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
203 if (i != ServerInstance->Config->oper_blocks.end())
205 OperInfo* ifo = i->second;
206 ssl_cert* cert = cmd.CertExt.get(user);
208 if (ifo->oper_block->getBool("sslonly") && !cert)
210 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires an SSL connection.");
211 user->CommandFloodPenalty += 10000;
215 std::string fingerprint;
216 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
218 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires a matching SSL certificate fingerprint.");
219 user->CommandFloodPenalty += 10000;
225 // Let core handle it for extra stuff
226 return MOD_RES_PASSTHRU;
229 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
231 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
233 cmd.CertExt.set(user, cert);
236 void OnPostConnect(User* user) CXX11_OVERRIDE
238 LocalUser* const localuser = IS_LOCAL(user);
242 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
246 ssl_cert* const cert = ssliohook->GetCertificate();
249 std::string text = "*** You are connected to ";
250 if (!ssliohook->GetServerName(text))
251 text.append(ServerInstance->Config->ServerName);
252 text.append(" using SSL cipher '");
253 ssliohook->GetCiphersuite(text);
254 text.push_back('\'');
255 if ((cert) && (!cert->GetFingerprint().empty()))
256 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
257 user->WriteNotice(text);
262 // find an auto-oper block for this user
263 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
265 OperInfo* ifo = i->second;
266 std::string fp = ifo->oper_block->getString("fingerprint");
267 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
272 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
274 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
276 if (myclass->config->getString("requiressl") == "trusted")
278 ok = (cert && cert->IsCAVerified());
279 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
281 else if (myclass->config->getBool("requiressl"))
284 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
289 return MOD_RES_PASSTHRU;
292 void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
294 // We are only interested in connection flags. If none have been
295 // given then we have nothing to do.
299 // We only care about the tls connection flag if the connection
300 // between the gateway and the server is secure.
301 if (!cmd.CertExt.get(user))
304 WebIRC::FlagMap::const_iterator iter = flags->find("secure");
305 if (iter == flags->end())
307 // If this is not set then the connection between the client and
308 // the gateway is not secure.
309 cmd.CertExt.unset(user);
313 // Create a fake ssl_cert for the user.
314 ssl_cert* cert = new ssl_cert;
315 cert->error = "WebIRC users can not specify valid certs yet";
316 cert->invalid = true;
317 cert->revoked = true;
318 cert->trusted = false;
319 cert->unknownsigner = true;
320 cmd.CertExt.set(user, cert);
324 MODULE_INIT(ModuleSSLInfo)