1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
20 #include "transport.h"
22 #include "socketengine.h"
24 #include "m_spanningtree/main.h"
25 #include "m_spanningtree/utils.h"
26 #include "m_spanningtree/treeserver.h"
27 #include "m_spanningtree/link.h"
28 #include "m_spanningtree/treesocket.h"
29 #include "m_spanningtree/resolvers.h"
30 #include "m_spanningtree/handshaketimer.h"
32 /* $ModDep: m_spanningtree/timesynctimer.h 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 */
34 const std::string& TreeSocket::GetOurChallenge()
36 return this->ourchallenge;
39 void TreeSocket::SetOurChallenge(const std::string &c)
41 this->ourchallenge = c;
44 const std::string& TreeSocket::GetTheirChallenge()
46 return this->theirchallenge;
49 void TreeSocket::SetTheirChallenge(const std::string &c)
51 this->theirchallenge = c;
54 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
56 /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
57 * suggesting the use of HMAC to secure the password against various attacks.
59 * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
60 * HMAC challenge/response.
62 Module* sha256 = Instance->Modules->Find("m_sha256.so");
63 if (Utils->ChallengeResponse && sha256 && !challenge.empty())
65 /* XXX: This is how HMAC is supposed to be done:
67 * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
69 * Note that we are encoding the hex hash, not the binary
70 * output of the hash which is slightly different to standard.
72 * Don't ask me why its always 0x5c and 0x36... it just is.
74 std::string hmac1, hmac2;
76 for (size_t n = 0; n < password.length(); n++)
78 hmac1 += static_cast<char>(password[n] ^ 0x5C);
79 hmac2 += static_cast<char>(password[n] ^ 0x36);
83 HashResetRequest(Utils->Creator, sha256).Send();
84 hmac2 = HashSumRequest(Utils->Creator, sha256, hmac2).Send();
86 HashResetRequest(Utils->Creator, sha256).Send();
87 std::string hmac = hmac1 + hmac2;
88 hmac = HashSumRequest(Utils->Creator, sha256, hmac).Send();
90 return "HMAC-SHA256:"+ hmac;
92 else if (!challenge.empty() && !sha256)
93 Instance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
98 std::string TreeSocket::RandString(unsigned int ilength)
100 char* randombuf = new char[ilength+1];
105 int f = open("/dev/urandom", O_RDONLY, 0);
111 if (read(f, randombuf, ilength) < 1)
112 Instance->Logs->Log("m_spanningtree", DEFAULT, "There are crack smoking monkeys in your kernel (in other words, nonblocking /dev/urandom blocked.)");
118 for (unsigned int i = 0; i < ilength; i++)
119 randombuf[i] = rand();
122 for (unsigned int i = 0; i < ilength; i++)
124 char randchar = static_cast<char>((randombuf[i] & 0x7F) | 0x21);
125 out += (randchar == '=' ? '_' : randchar);
132 bool TreeSocket::ComparePass(const std::string &ours, const std::string &theirs)
134 if ((!strncmp(ours.c_str(), "HMAC-SHA256:", 12)) || (!strncmp(theirs.c_str(), "HMAC-SHA256:", 12)))
136 /* One or both of us specified hmac sha256, but we don't have sha256 module loaded!
137 * We can't allow this password as valid.
139 if (!Instance->Modules->Find("m_sha256.so") || !Utils->ChallengeResponse)
142 /* Straight string compare of hashes */
143 return ours == theirs;
146 /* Straight string compare of plaintext */
147 return ours == theirs;