]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/hmac.cpp
Improve SSL fingerprint on link message
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / hmac.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 "socket.h"
16 #include "xline.h"
17 #include "../m_hash.h"
18 #include "../ssl.h"
19 #include "socketengine.h"
20
21 #include "main.h"
22 #include "utils.h"
23 #include "treeserver.h"
24 #include "link.h"
25 #include "treesocket.h"
26 #include "resolvers.h"
27
28 const std::string& TreeSocket::GetOurChallenge()
29 {
30         return capab->ourchallenge;
31 }
32
33 void TreeSocket::SetOurChallenge(const std::string &c)
34 {
35         capab->ourchallenge = c;
36 }
37
38 const std::string& TreeSocket::GetTheirChallenge()
39 {
40         return capab->theirchallenge;
41 }
42
43 void TreeSocket::SetTheirChallenge(const std::string &c)
44 {
45         capab->theirchallenge = c;
46 }
47
48 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
49 {
50         /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
51          * suggesting the use of HMAC to secure the password against various attacks.
52          *
53          * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
54          *       HMAC challenge/response.
55          */
56         HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
57         if (Utils->ChallengeResponse && sha256 && !challenge.empty())
58         {
59                 /* This is how HMAC is supposed to be done:
60                  *
61                  * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
62                  *
63                  * 5c and 36 were chosen as part of the HMAC standard, because they
64                  * flip the bits in a way likely to strengthen the function.
65                  */
66                 std::string hmac1, hmac2;
67
68                 for (size_t n = 0; n < password.length(); n++)
69                 {
70                         hmac1.push_back(static_cast<char>(password[n] ^ 0x5C));
71                         hmac2.push_back(static_cast<char>(password[n] ^ 0x36));
72                 }
73
74                 if (proto_version >= 1202)
75                 {
76                         hmac2.append(challenge);
77                         std::string hmac = sha256->hexsum(hmac1 + sha256->sum(hmac2));
78
79                         return "AUTH:" + hmac;
80                 }
81                 else
82                 {
83                         // version 1.2 used a weaker HMAC, using hex output in the intermediate step
84                         hmac2.append(challenge);
85                         hmac2 = sha256->hexsum(hmac2);
86                 
87                         std::string hmac = hmac1 + hmac2;
88                         hmac = sha256->hexsum(hmac);
89
90                         return "HMAC-SHA256:"+ hmac;
91                 }
92         }
93         else if (!challenge.empty() && !sha256)
94                 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
95
96         return password;
97 }
98
99 std::string TreeSocket::RandString(unsigned int ilength)
100 {
101         char* randombuf = new char[ilength+1];
102         std::string out;
103 #ifndef WINDOWS
104         int f = open("/dev/urandom", O_RDONLY, 0);
105
106         if (f >= 0)
107         {
108                 if (read(f, randombuf, ilength) < (int)ilength)
109                         ServerInstance->Logs->Log("m_spanningtree", DEFAULT, "Entropy source has gone predictable (did not return enough data)");
110                 close(f);
111         }
112         else
113 #endif
114         {
115                 for (unsigned int i = 0; i < ilength; i++)
116                         randombuf[i] = rand();
117         }
118
119         for (unsigned int i = 0; i < ilength; i++)
120         {
121                 char randchar = static_cast<char>(0x3F + (randombuf[i] & 0x3F));
122                 out += randchar;
123         }
124
125         delete[] randombuf;
126         return out;
127 }
128
129 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
130 {
131         capab->auth_fingerprint = !link.Fingerprint.empty();
132         capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
133
134         std::string fp;
135         if (GetIOHook())
136         {
137                 SocketCertificateRequest req(this, Utils->Creator, GetIOHook());
138                 if (req.cert)
139                 {
140                         fp = req.cert->GetFingerprint();
141                 }
142         }
143
144         if (capab->auth_challenge)
145         {
146                 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
147
148                 /* Straight string compare of hashes */
149                 if (our_hmac != theirs)
150                         return false;
151         }
152         else
153         {
154                 /* Straight string compare of plaintext */
155                 if (link.RecvPass != theirs)
156                         return false;
157         }
158
159         if (capab->auth_fingerprint)
160         {
161                 /* Require fingerprint to exist and match */
162                 if (link.Fingerprint != fp)
163                 {
164                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need \"%s\" got \"%s\"",
165                                 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
166                         SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
167                         return false;
168                 }
169         }
170         else if (!fp.empty())
171         {
172                 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is \"%s\". "
173                         "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());
174         }
175         return true;
176 }