2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Matthew Martin <phy1729@gmail.com>
5 * Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
13 * This file is part of InspIRCd. InspIRCd is free software: you can
14 * redistribute it and/or modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation, version 2.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "modules/hash.h"
29 #include "modules/ssl.h"
33 #include "treesocket.h"
35 const std::string& TreeSocket::GetOurChallenge()
37 return capab->ourchallenge;
40 void TreeSocket::SetOurChallenge(const std::string &c)
42 capab->ourchallenge = c;
45 const std::string& TreeSocket::GetTheirChallenge()
47 return capab->theirchallenge;
50 void TreeSocket::SetTheirChallenge(const std::string &c)
52 capab->theirchallenge = c;
55 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
57 /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
58 * suggesting the use of HMAC to secure the password against various attacks.
60 * Note: If an sha256 provider is not available, we MUST fall back to plaintext with no
61 * HMAC challenge/response.
63 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
64 if (sha256 && !challenge.empty())
65 return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
67 if (!challenge.empty() && !sha256)
68 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Not authenticating to server using SHA256/HMAC because we don't have an SHA256 provider (e.g. m_sha256) loaded!");
73 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
75 capab->auth_fingerprint = !link.Fingerprint.empty();
76 capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
78 std::string fp = SSLClientCert::GetFingerprint(this);
79 if (capab->auth_fingerprint)
81 /* Require fingerprint to exist and match */
82 if (link.Fingerprint != fp)
84 ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL certificate fingerprint on link %s: need \"%s\" got \"%s\"",
85 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
86 SendError("Invalid SSL certificate fingerprint " + fp + " - expected " + link.Fingerprint);
91 if (capab->auth_challenge)
93 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
95 // Use the timing-safe compare function to compare the hashes
96 if (!InspIRCd::TimingSafeCompare(our_hmac, theirs))
101 // Use the timing-safe compare function to compare the passwords
102 if (!InspIRCd::TimingSafeCompare(link.RecvPass, theirs))
106 // Tell opers to set up fingerprint verification if it's not already set up and the SSL mod gave us a fingerprint
108 if ((!capab->auth_fingerprint) && (!fp.empty()))
110 ServerInstance->SNO->WriteToSnoMask('l', "SSL certificate fingerprint for link %s is \"%s\". "
111 "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());