]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/hmac.cpp
Merge pull request #109 from Justasic/insp20
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / hmac.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "socket.h"
23 #include "xline.h"
24 #include "../hash.h"
25 #include "../ssl.h"
26 #include "socketengine.h"
27
28 #include "main.h"
29 #include "utils.h"
30 #include "treeserver.h"
31 #include "link.h"
32 #include "treesocket.h"
33 #include "resolvers.h"
34
35 const std::string& TreeSocket::GetOurChallenge()
36 {
37         return capab->ourchallenge;
38 }
39
40 void TreeSocket::SetOurChallenge(const std::string &c)
41 {
42         capab->ourchallenge = c;
43 }
44
45 const std::string& TreeSocket::GetTheirChallenge()
46 {
47         return capab->theirchallenge;
48 }
49
50 void TreeSocket::SetTheirChallenge(const std::string &c)
51 {
52         capab->theirchallenge = c;
53 }
54
55 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
56 {
57         /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
58          * suggesting the use of HMAC to secure the password against various attacks.
59          *
60          * Note: If m_sha256.so is not loaded, we MUST fall back to plaintext with no
61          *       HMAC challenge/response.
62          */
63         HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
64         if (Utils->ChallengeResponse && sha256 && !challenge.empty())
65         {
66                 if (proto_version < 1202)
67                 {
68                         /* This is how HMAC is done in InspIRCd 1.2:
69                          *
70                          * sha256( (pass xor 0x5c) + sha256((pass xor 0x36) + m) )
71                          *
72                          * 5c and 36 were chosen as part of the HMAC standard, because they
73                          * flip the bits in a way likely to strengthen the function.
74                          */
75                         std::string hmac1, hmac2;
76
77                         for (size_t n = 0; n < password.length(); n++)
78                         {
79                                 hmac1.push_back(static_cast<char>(password[n] ^ 0x5C));
80                                 hmac2.push_back(static_cast<char>(password[n] ^ 0x36));
81                         }
82
83                         hmac2.append(challenge);
84                         hmac2 = sha256->hexsum(hmac2);
85                 
86                         std::string hmac = hmac1 + hmac2;
87                         hmac = sha256->hexsum(hmac);
88
89                         return "HMAC-SHA256:"+ hmac;
90                 }
91                 else
92                 {
93                         return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
94                 }
95         }
96         else if (!challenge.empty() && !sha256)
97                 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Not authenticating to server using SHA256/HMAC because we don't have m_sha256 loaded!");
98
99         return password;
100 }
101
102 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
103 {
104         capab->auth_fingerprint = !link.Fingerprint.empty();
105         capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
106
107         std::string fp;
108         if (GetIOHook())
109         {
110                 SocketCertificateRequest req(this, Utils->Creator);
111                 if (req.cert)
112                 {
113                         fp = req.cert->GetFingerprint();
114                 }
115         }
116
117         if (capab->auth_challenge)
118         {
119                 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
120
121                 /* Straight string compare of hashes */
122                 if (our_hmac != theirs)
123                         return false;
124         }
125         else
126         {
127                 /* Straight string compare of plaintext */
128                 if (link.RecvPass != theirs)
129                         return false;
130         }
131
132         if (capab->auth_fingerprint)
133         {
134                 /* Require fingerprint to exist and match */
135                 if (link.Fingerprint != fp)
136                 {
137                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need \"%s\" got \"%s\"",
138                                 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
139                         SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
140                         return false;
141                 }
142         }
143         else if (!fp.empty())
144         {
145                 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is \"%s\". "
146                         "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());
147         }
148         return true;
149 }