hmac.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015 #include "commands/cmd_whois.h"
00016 #include "commands/cmd_stats.h"
00017 #include "socket.h"
00018 #include "xline.h"
00019 #include "transport.h"
00020 #include "m_hash.h"
00021 #include "socketengine.h"
00022
00023 #include "m_spanningtree/main.h"
00024 #include "m_spanningtree/utils.h"
00025 #include "m_spanningtree/treeserver.h"
00026 #include "m_spanningtree/link.h"
00027 #include "m_spanningtree/treesocket.h"
00028 #include "m_spanningtree/resolvers.h"
00029 #include "m_spanningtree/handshaketimer.h"
00030
00031
00032
00033 const std::string& TreeSocket::GetOurChallenge()
00034 {
00035 return this->ourchallenge;
00036 }
00037
00038 void TreeSocket::SetOurChallenge(const std::string &c)
00039 {
00040 this->ourchallenge = c;
00041 }
00042
00043 const std::string& TreeSocket::GetTheirChallenge()
00044 {
00045 return this->theirchallenge;
00046 }
00047
00048 void TreeSocket::SetTheirChallenge(const std::string &c)
00049 {
00050 this->theirchallenge = c;
00051 }
00052
00053 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
00054 {
00055
00056
00057
00058
00059
00060
00061 Module* sha256 = ServerInstance->Modules->Find("m_sha256.so");
00062 if (Utils->ChallengeResponse && sha256 && !challenge.empty())
00063 {
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 std::string hmac1, hmac2;
00074
00075 for (size_t n = 0; n < password.length(); n++)
00076 {
00077 hmac1 += static_cast<char>(password[n] ^ 0x5C);
00078 hmac2 += static_cast<char>(password[n] ^ 0x36);
00079 }
00080
00081 hmac2 += challenge;
00082 HashResetRequest(Utils->Creator, sha256).Send();
00083 hmac2 = HashSumRequest(Utils->Creator, sha256, hmac2).Send();
00084
00085 HashResetRequest(Utils->Creator, sha256).Send();
00086 std::string hmac = hmac1 + hmac2;
00087 hmac = HashSumRequest(Utils->Creator, sha256, hmac).Send();
00088
00089 return "HMAC-SHA256:"+ hmac;
00090 }
00091 else if (!challenge.empty() && !sha256)
00092 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
00093
00094 return password;
00095 }
00096
00097 std::string TreeSocket::RandString(unsigned int ilength)
00098 {
00099 char* randombuf = new char[ilength+1];
00100 std::string out;
00101 #ifdef WINDOWS
00102 int f = -1;
00103 #else
00104 int f = open("/dev/urandom", O_RDONLY, 0);
00105 #endif
00106
00107 if (f >= 0)
00108 {
00109 #ifndef WINDOWS
00110 if (read(f, randombuf, ilength) < 1)
00111 ServerInstance->Logs->Log("m_spanningtree", DEFAULT, "There are crack smoking monkeys in your kernel (in other words, nonblocking /dev/urandom blocked.)");
00112 close(f);
00113 #endif
00114 }
00115 else
00116 {
00117 for (unsigned int i = 0; i < ilength; i++)
00118 randombuf[i] = rand();
00119 }
00120
00121 for (unsigned int i = 0; i < ilength; i++)
00122 {
00123 char randchar = static_cast<char>((randombuf[i] & 0x7F) | 0x21);
00124 out += (randchar == '=' ? '_' : randchar);
00125 }
00126
00127 delete[] randombuf;
00128 return out;
00129 }
00130
00131 bool TreeSocket::ComparePass(const std::string &ours, const std::string &theirs)
00132 {
00133 if ((!strncmp(ours.c_str(), "HMAC-SHA256:", 12)) || (!strncmp(theirs.c_str(), "HMAC-SHA256:", 12)))
00134 {
00135
00136
00137
00138 if (!ServerInstance->Modules->Find("m_sha256.so") || !Utils->ChallengeResponse)
00139 return false;
00140 else
00141
00142 return ours == theirs;
00143 }
00144 else
00145
00146 return ours == theirs;
00147 }