]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/hmac.cpp
Add a CapReference class for the message-tags capability.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / hmac.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Matthew Martin <phy1729@gmail.com>
5  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "modules/hash.h"
29 #include "modules/ssl.h"
30
31 #include "main.h"
32 #include "link.h"
33 #include "treesocket.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 an sha256 provider is not available, we MUST fall back to plaintext with no
61          *       HMAC challenge/response.
62          */
63         HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
64         if (sha256 && !challenge.empty())
65                 return "AUTH:" + BinToBase64(sha256->hmac(password, challenge));
66
67         if (!challenge.empty() && !sha256)
68                 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!");
69
70         return password;
71 }
72
73 bool TreeSocket::ComparePass(const Link& link, const std::string &theirs)
74 {
75         capab->auth_fingerprint = !link.Fingerprint.empty();
76         capab->auth_challenge = !capab->ourchallenge.empty() && !capab->theirchallenge.empty();
77
78         std::string fp = SSLClientCert::GetFingerprint(this);
79         if (capab->auth_fingerprint)
80         {
81                 /* Require fingerprint to exist and match */
82                 if (link.Fingerprint != fp)
83                 {
84                         ServerInstance->SNO->WriteToSnoMask('l',"Invalid SSL certificate fingerprint on link %s: need \"%s\" got \"%s\"",
85                                 link.Name.c_str(), link.Fingerprint.c_str(), fp.c_str());
86                         SendError("Invalid SSL certificate fingerprint " + fp + " - expected " + link.Fingerprint);
87                         return false;
88                 }
89         }
90
91         if (capab->auth_challenge)
92         {
93                 std::string our_hmac = MakePass(link.RecvPass, capab->ourchallenge);
94
95                 // Use the timing-safe compare function to compare the hashes
96                 if (!InspIRCd::TimingSafeCompare(our_hmac, theirs))
97                         return false;
98         }
99         else
100         {
101                 // Use the timing-safe compare function to compare the passwords
102                 if (!InspIRCd::TimingSafeCompare(link.RecvPass, theirs))
103                         return false;
104         }
105
106         // Tell opers to set up fingerprint verification if it's not already set up and the SSL mod gave us a fingerprint
107         // this time
108         if ((!capab->auth_fingerprint) && (!fp.empty()))
109         {
110                 ServerInstance->SNO->WriteToSnoMask('l', "SSL certificate fingerprint for link %s is \"%s\". "
111                         "You can improve security by specifying this in <link:fingerprint>.", link.Name.c_str(), fp.c_str());
112         }
113
114         return true;
115 }