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/>.
26 #include "socketengine.h"
30 #include "treeserver.h"
32 #include "treesocket.h"
33 #include "resolvers.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 m_sha256.so is not loaded, we MUST fall back to plaintext with no
61 * HMAC challenge/response.
63 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
64 if (Utils->ChallengeResponse && sha256 && !challenge.empty())
66 if (proto_version < 1202)
68 /* This is how HMAC is done in InspIRCd 1.2:
70 * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
72 * 5c and 36 were chosen as part of the HMAC standard, because they
73 * flip the bits in a way likely to strengthen the function.
75 std::string hmac1, hmac2;
77 for (size_t n = 0; n < password.length(); n++)
79 hmac1.push_back(static_cast<char>(password[n] ^ 0x5C));
80 hmac2.push_back(static_cast<char>(password[n] ^ 0x36));
83 hmac2.append(challenge);
84 hmac2 = sha256->hexsum(hmac2);
86 std::string hmac = hmac1 + hmac2;
87 hmac = sha256->hexsum(hmac);
89 return "HMAC-SHA256:"+ hmac;
93 return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
96 else if (!challenge.empty() && !sha256)
97 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
102 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
104 capab->auth_fingerprint = !link.Fingerprint.empty();
105 capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
110 SocketCertificateRequest req(this, Utils->Creator);
113 fp = req.cert->GetFingerprint();
117 if (capab->auth_challenge)
119 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
121 /* Straight string compare of hashes */
122 if (our_hmac != theirs)
127 /* Straight string compare of plaintext */
128 if (link.RecvPass != theirs)
132 if (capab->auth_fingerprint)
134 /* Require fingerprint to exist and match */
135 if (link.Fingerprint != fp)
137 ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need \"%s\" got \"%s\"",
138 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
139 SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
143 else if (!fp.empty())
145 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is \"%s\". "
146 "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());