]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/hmac.cpp
e1d46d0d974da1ce9d33f6d5999f98d88b1fc219
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / hmac.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "../transport.h"
20 #include "../m_hash.h"
21 #include "socketengine.h"
22
23 #include "main.h"
24 #include "utils.h"
25 #include "treeserver.h"
26 #include "link.h"
27 #include "treesocket.h"
28 #include "resolvers.h"
29 #include "handshaketimer.h"
30
31 /* $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 */
32
33 const std::string& TreeSocket::GetOurChallenge()
34 {
35         return this->ourchallenge;
36 }
37
38 void TreeSocket::SetOurChallenge(const std::string &c)
39 {
40         this->ourchallenge = c;
41 }
42
43 const std::string& TreeSocket::GetTheirChallenge()
44 {
45         return this->theirchallenge;
46 }
47
48 void TreeSocket::SetTheirChallenge(const std::string &c)
49 {
50         this->theirchallenge = c;
51 }
52
53 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
54 {
55         /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
56          * suggesting the use of HMAC to secure the password against various attacks.
57          *
58          * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
59          *       HMAC challenge/response.
60          */
61         Module* sha256 = ServerInstance->Modules->Find("m_sha256.so");
62         if (Utils->ChallengeResponse && sha256 && !challenge.empty())
63         {
64                 /* XXX: This is how HMAC is supposed to be done:
65                  *
66                  * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
67                  *
68                  * Note that we are encoding the hex hash, not the binary
69                  * output of the hash which is slightly different to standard.
70                  *
71                  * 5c and 36 were chosen as part of the HMAC standard, because they
72                  * flip the bits in a way likely to strengthen the function.
73                  */
74                 std::string hmac1, hmac2;
75
76                 for (size_t n = 0; n < password.length(); n++)
77                 {
78                         hmac1 += static_cast<char>(password[n] ^ 0x5C);
79                         hmac2 += static_cast<char>(password[n] ^ 0x36);
80                 }
81
82                 hmac2 += challenge;
83                 HashResetRequest(Utils->Creator, sha256).Send();
84                 hmac2 = HashSumRequest(Utils->Creator, sha256, hmac2).Send();
85
86                 HashResetRequest(Utils->Creator, sha256).Send();
87                 std::string hmac = hmac1 + hmac2;
88                 hmac = HashSumRequest(Utils->Creator, sha256, hmac).Send();
89
90                 return "HMAC-SHA256:"+ hmac;
91         }
92         else if (!challenge.empty() && !sha256)
93                 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
94
95         return password;
96 }
97
98 std::string TreeSocket::RandString(unsigned int ilength)
99 {
100         char* randombuf = new char[ilength+1];
101         std::string out;
102 #ifndef WINDOWS
103         int f = open("/dev/urandom", O_RDONLY, 0);
104
105         if (f >= 0)
106         {
107                 if (read(f, randombuf, ilength) < ilength)
108                         ServerInstance->Logs->Log("m_spanningtree", DEFAULT, "Entropy source has gone predictable (did not return enough data)");
109                 close(f);
110         }
111         else
112 #endif
113         {
114                 for (unsigned int i = 0; i < ilength; i++)
115                         randombuf[i] = rand();
116         }
117
118         for (unsigned int i = 0; i < ilength; i++)
119         {
120                 char randchar = static_cast<char>(0x3F + (randombuf[i] & 0x3F));
121                 out += randchar;
122         }
123
124         delete[] randombuf;
125         return out;
126 }
127
128 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
129 {
130         this->auth_fingerprint = !link.Fingerprint.empty();
131         this->auth_challenge = !ourchallenge.empty() && !theirchallenge.empty();
132
133         const char* fp = NULL;
134         if (GetHook())
135                 fp = BufferedSocketFingerprintRequest(this, Utils->Creator, GetHook()).Send();
136
137         if (fp)
138                 ServerInstance->Logs->Log("m_spanningtree", DEFAULT, std::string("Server SSL fingerprint ") + fp);
139
140         if (auth_fingerprint)
141         {
142                 /* Require fingerprint to exist and match */
143                 if (!fp || link.Fingerprint != std::string(fp))
144                         return false;
145         }
146
147         if (auth_challenge)
148         {
149                 std::string our_hmac = MakePass(link.RecvPass, ourchallenge);
150
151                 /* Straight string compare of hashes */
152                 return our_hmac == theirs;
153         }
154
155         /* Straight string compare of plaintext */
156         return link.RecvPass == theirs;
157 }