1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "../m_hash.h"
19 #include "socketengine.h"
23 #include "treeserver.h"
25 #include "treesocket.h"
26 #include "resolvers.h"
28 /* $ModDep: m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_hash.h */
30 const std::string& TreeSocket::GetOurChallenge()
32 return this->ourchallenge;
35 void TreeSocket::SetOurChallenge(const std::string &c)
37 this->ourchallenge = c;
40 const std::string& TreeSocket::GetTheirChallenge()
42 return this->theirchallenge;
45 void TreeSocket::SetTheirChallenge(const std::string &c)
47 this->theirchallenge = c;
50 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
52 /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
53 * suggesting the use of HMAC to secure the password against various attacks.
55 * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
56 * HMAC challenge/response.
58 Module* sha256 = ServerInstance->Modules->Find("m_sha256.so");
59 if (Utils->ChallengeResponse && sha256 && !challenge.empty())
61 /* XXX: This is how HMAC is supposed to be done:
63 * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
65 * Note that we are encoding the hex hash, not the binary
66 * output of the hash which is slightly different to standard.
68 * 5c and 36 were chosen as part of the HMAC standard, because they
69 * flip the bits in a way likely to strengthen the function.
71 std::string hmac1, hmac2;
73 for (size_t n = 0; n < password.length(); n++)
75 hmac1 += static_cast<char>(password[n] ^ 0x5C);
76 hmac2 += static_cast<char>(password[n] ^ 0x36);
80 hmac2 = HashRequest(Utils->Creator, sha256, hmac2).hex();
82 std::string hmac = hmac1 + hmac2;
83 hmac = HashRequest(Utils->Creator, sha256, hmac).hex();
85 return "HMAC-SHA256:"+ hmac;
87 else if (!challenge.empty() && !sha256)
88 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
93 std::string TreeSocket::RandString(unsigned int ilength)
95 char* randombuf = new char[ilength+1];
98 int f = open("/dev/urandom", O_RDONLY, 0);
102 if (read(f, randombuf, ilength) < (int)ilength)
103 ServerInstance->Logs->Log("m_spanningtree", DEFAULT, "Entropy source has gone predictable (did not return enough data)");
109 for (unsigned int i = 0; i < ilength; i++)
110 randombuf[i] = rand();
113 for (unsigned int i = 0; i < ilength; i++)
115 char randchar = static_cast<char>(0x3F + (randombuf[i] & 0x3F));
123 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
125 this->auth_fingerprint = !link.Fingerprint.empty();
126 this->auth_challenge = !ourchallenge.empty() && !theirchallenge.empty();
131 SSLCertificateRequest req(this, Utils->Creator);
134 fp = req.cert->GetFingerprint();
140 std::string our_hmac = MakePass(link.RecvPass, ourchallenge);
142 /* Straight string compare of hashes */
143 if (our_hmac != theirs)
148 /* Straight string compare of plaintext */
149 if (link.RecvPass != theirs)
153 if (auth_fingerprint)
155 /* Require fingerprint to exist and match */
156 if (link.Fingerprint != fp)
158 ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need '%s' got '%s'",
159 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
160 SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
164 else if (!fp.empty())
166 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is %s", link.Name.c_str(), fp.c_str());