2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/hash.h"
23 #include "modules/ssl.h"
27 #include "treesocket.h"
29 const std::string& TreeSocket::GetOurChallenge()
31 return capab->ourchallenge;
34 void TreeSocket::SetOurChallenge(const std::string &c)
36 capab->ourchallenge = c;
39 const std::string& TreeSocket::GetTheirChallenge()
41 return capab->theirchallenge;
44 void TreeSocket::SetTheirChallenge(const std::string &c)
46 capab->theirchallenge = c;
49 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
51 /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
52 * suggesting the use of HMAC to secure the password against various attacks.
54 * Note: If an sha256 provider is not available, we MUST fall back to plaintext with no
55 * HMAC challenge/response.
57 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
58 if (sha256 && !challenge.empty())
59 return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
61 if (!challenge.empty() && !sha256)
62 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!");
67 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
69 capab->auth_fingerprint = !link.Fingerprint.empty();
70 capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
72 if (capab->auth_challenge)
74 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
76 /* Straight string compare of hashes */
77 if (our_hmac != theirs)
82 /* Straight string compare of plaintext */
83 if (link.RecvPass != theirs)
87 std::string fp = SSLClientCert::GetFingerprint(this);
88 if (capab->auth_fingerprint)
90 /* Require fingerprint to exist and match */
91 if (link.Fingerprint != fp)
93 ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need \"%s\" got \"%s\"",
94 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
95 SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
101 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is \"%s\". "
102 "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());