]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/hmac.cpp
Check fingerprint before checking password (server linking)
[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 "modules/hash.h"
23 #include "modules/ssl.h"
24
25 #include "main.h"
26 #include "link.h"
27 #include "treesocket.h"
28
29 const std::string& TreeSocket::GetOurChallenge()
30 {
31         return capab->ourchallenge;
32 }
33
34 void TreeSocket::SetOurChallenge(const std::string &c)
35 {
36         capab->ourchallenge = c;
37 }
38
39 const std::string& TreeSocket::GetTheirChallenge()
40 {
41         return capab->theirchallenge;
42 }
43
44 void TreeSocket::SetTheirChallenge(const std::string &c)
45 {
46         capab->theirchallenge = c;
47 }
48
49 std::string TreeSocket::MakePass(const std::string &password, const std::string &challenge)
50 {
51         /* This is a simple (maybe a bit hacky?) HMAC algorithm, thanks to jilles for
52          * suggesting the use of HMAC to secure the password against various attacks.
53          *
54          * Note: If an sha256 provider is not available, we MUST fall back to plaintext with no
55          *       HMAC challenge/response.
56          */
57         HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
58         if (sha256 && !challenge.empty())
59                 return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
60
61         if (!challenge.empty() && !sha256)
62                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Not authenticating to server using SHA256/HMAC because we don't have an SHA256 provider (e.g. m_sha256) loaded!");
63
64         return password;
65 }
66
67 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
68 {
69         capab->auth_fingerprint = !link.Fingerprint.empty();
70         capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
71
72         std::string fp = SSLClientCert::GetFingerprint(this);
73         if (capab->auth_fingerprint)
74         {
75                 /* Require fingerprint to exist and match */
76                 if (link.Fingerprint != fp)
77                 {
78                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL fingerprint on link %s: need \"%s\" got \"%s\"",
79                                 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
80                         SendError("Provided invalid SSL fingerprint " + fp + " - expected " + link.Fingerprint);
81                         return false;
82                 }
83         }
84         else if (!fp.empty())
85         {
86                 ServerInstance->SNO->WriteToSnoMask('l', "SSL fingerprint for link %s is \"%s\". "
87                         "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());
88         }
89
90         if (capab->auth_challenge)
91         {
92                 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
93
94                 /* Straight string compare of hashes */
95                 if (our_hmac != theirs)
96                         return false;
97         }
98         else
99         {
100                 /* Straight string compare of plaintext */
101                 if (link.RecvPass != theirs)
102                         return false;
103         }
104         return true;
105 }