The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

hmac.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
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 /* $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 */
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         /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
00056          * suggesting the use of HMAC to secure the password against various attacks.
00057          *
00058          * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
00059          *       HMAC challenge/response.
00060          */
00061         Module* sha256 = ServerInstance->Modules->Find("m_sha256.so");
00062         if (Utils->ChallengeResponse && sha256 && !challenge.empty())
00063         {
00064                 /* XXX: This is how HMAC is supposed to be done:
00065                  *
00066                  * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
00067                  *
00068                  * Note that we are encoding the hex hash, not the binary
00069                  * output of the hash which is slightly different to standard.
00070                  *
00071                  * Don't ask me why its always 0x5c and 0x36... it just is.
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                 /* One or both of us specified hmac sha256, but we don't have sha256 module loaded!
00136                  * We can't allow this password as valid.
00137                   */
00138                 if (!ServerInstance->Modules->Find("m_sha256.so") || !Utils->ChallengeResponse)
00139                         return false;
00140                 else
00141                         /* Straight string compare of hashes */
00142                         return ours == theirs;
00143         }
00144         else
00145                 /* Straight string compare of plaintext */
00146                 return ours == theirs;
00147 }