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
37 SSLCertExt(Module* parent)
38 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
42 ssl_cert* get(const Extensible* item) const
44 return static_cast<ssl_cert*>(get_raw(item));
47 void set(Extensible* item, ssl_cert* value)
49 value->refcount_inc();
50 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
51 if (old && old->refcount_dec())
55 void unset(Extensible* container)
57 free(container, unset_raw(container));
60 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
62 return static_cast<ssl_cert*>(item)->GetMetaLine();
65 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
67 ssl_cert* cert = new ssl_cert;
70 std::stringstream s(value);
74 cert->invalid = (v.find('v') != std::string::npos);
75 cert->trusted = (v.find('T') != std::string::npos);
76 cert->revoked = (v.find('R') != std::string::npos);
77 cert->unknownsigner = (v.find('s') != std::string::npos);
78 if (v.find('E') != std::string::npos)
80 getline(s,cert->error,'\n');
84 getline(s,cert->fingerprint,' ');
85 getline(s,cert->dn,' ');
86 getline(s,cert->issuer,'\n');
90 void free(Extensible* container, void* item) CXX11_OVERRIDE
92 ssl_cert* old = static_cast<ssl_cert*>(item);
93 if (old && old->refcount_dec())
98 class UserCertificateAPIImpl : public UserCertificateAPIBase
101 LocalIntExt nosslext;
104 UserCertificateAPIImpl(Module* mod)
105 : UserCertificateAPIBase(mod)
106 , nosslext("no_ssl_cert", ExtensionItem::EXT_USER, mod)
111 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
113 ssl_cert* cert = sslext.get(user);
117 LocalUser* luser = IS_LOCAL(user);
118 if (!luser || nosslext.get(luser))
121 cert = SSLClientCert::GetCertificate(&luser->eh);
125 SetCertificate(user, cert);
129 void SetCertificate(User* user, ssl_cert* cert) CXX11_OVERRIDE
131 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Setting SSL certificate for %s: %s",
132 user->GetFullHost().c_str(), cert->GetMetaLine().c_str());
133 sslext.set(user, cert);
137 class CommandSSLInfo : public Command
140 UserCertificateAPIImpl sslapi;
142 CommandSSLInfo(Module* Creator)
143 : Command(Creator, "SSLINFO", 1)
146 this->syntax = "<nick>";
149 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
151 User* target = ServerInstance->FindNickOnly(parameters[0]);
153 if ((!target) || (target->registered != REG_ALL))
155 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
158 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
159 if (operonlyfp && !user->IsOper() && target != user)
161 user->WriteNotice("*** You cannot view SSL certificate information for other users");
164 ssl_cert* cert = sslapi.GetCertificate(target);
167 user->WriteNotice("*** No SSL certificate for this user");
169 else if (cert->GetError().length())
171 user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
175 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
176 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
177 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
185 , public WebIRC::EventListener
186 , public Whois::EventListener
191 bool MatchFP(ssl_cert* const cert, const std::string& fp) const
193 return irc::spacesepstream(fp).Contains(cert->GetFingerprint());
198 : WebIRC::EventListener(this)
199 , Whois::EventListener(this)
204 Version GetVersion() CXX11_OVERRIDE
206 return Version("SSL Certificate Utilities", VF_VENDOR);
209 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
211 ssl_cert* cert = cmd.sslapi.GetCertificate(whois.GetTarget());
214 whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
215 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
216 if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
217 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
221 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
223 if ((command == "OPER") && (validated))
225 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
226 if (i != ServerInstance->Config->oper_blocks.end())
228 OperInfo* ifo = i->second;
229 ssl_cert* cert = cmd.sslapi.GetCertificate(user);
231 if (ifo->oper_block->getBool("sslonly") && !cert)
233 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires an SSL connection.");
234 user->CommandFloodPenalty += 10000;
238 std::string fingerprint;
239 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || !MatchFP(cert, fingerprint)))
241 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires a matching SSL certificate fingerprint.");
242 user->CommandFloodPenalty += 10000;
248 // Let core handle it for extra stuff
249 return MOD_RES_PASSTHRU;
252 void OnPostConnect(User* user) CXX11_OVERRIDE
254 LocalUser* const localuser = IS_LOCAL(user);
258 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
259 if (!ssliohook || cmd.sslapi.nosslext.get(localuser))
262 ssl_cert* const cert = ssliohook->GetCertificate();
265 std::string text = "*** You are connected to ";
266 if (!ssliohook->GetServerName(text))
267 text.append(ServerInstance->Config->ServerName);
268 text.append(" using SSL cipher '");
269 ssliohook->GetCiphersuite(text);
270 text.push_back('\'');
271 if ((cert) && (!cert->GetFingerprint().empty()))
272 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
273 user->WriteNotice(text);
278 // find an auto-oper block for this user
279 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
281 OperInfo* ifo = i->second;
282 std::string fp = ifo->oper_block->getString("fingerprint");
283 if (MatchFP(cert, fp) && ifo->oper_block->getBool("autologin"))
288 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
290 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
292 if (myclass->config->getString("requiressl") == "trusted")
294 ok = (cert && cert->IsCAVerified());
295 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
297 else if (myclass->config->getBool("requiressl"))
300 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
305 return MOD_RES_PASSTHRU;
308 void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
310 // We are only interested in connection flags. If none have been
311 // given then we have nothing to do.
315 // We only care about the tls connection flag if the connection
316 // between the gateway and the server is secure.
317 if (!cmd.sslapi.GetCertificate(user))
320 WebIRC::FlagMap::const_iterator iter = flags->find("secure");
321 if (iter == flags->end())
323 // If this is not set then the connection between the client and
324 // the gateway is not secure.
325 cmd.sslapi.nosslext.set(user, 1);
326 cmd.sslapi.sslext.unset(user);
330 // Create a fake ssl_cert for the user.
331 ssl_cert* cert = new ssl_cert;
332 cert->error = "WebIRC users can not specify valid certs yet";
333 cert->invalid = true;
334 cert->revoked = true;
335 cert->trusted = false;
336 cert->unknownsigner = true;
337 cmd.sslapi.SetCertificate(user, cert);
341 MODULE_INIT(ModuleSSLInfo)