2014-04-13 11:04:56 +00:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "Authenticator.h"
|
2014-04-14 20:52:59 +00:00
|
|
|
#include "../OSSupport/BlockingTCPLink.h"
|
|
|
|
#include "../Root.h"
|
|
|
|
#include "../Server.h"
|
|
|
|
#include "../ClientHandle.h"
|
2014-04-13 11:04:56 +00:00
|
|
|
|
|
|
|
#include "inifile/iniFile.h"
|
|
|
|
#include "json/json.h"
|
|
|
|
|
|
|
|
#include "polarssl/config.h"
|
|
|
|
#include "polarssl/net.h"
|
|
|
|
#include "polarssl/ssl.h"
|
|
|
|
#include "polarssl/ctr_drbg.h"
|
|
|
|
#include "polarssl/error.h"
|
2014-04-24 19:51:19 +00:00
|
|
|
|
|
|
|
#include "PolarSSL++/EntropyContext.h"
|
|
|
|
#include "PolarSSL++/CtrDrbgContext.h"
|
|
|
|
#include "PolarSSL++/X509Cert.h"
|
2014-04-13 11:04:56 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_AUTH_SERVER "sessionserver.mojang.com"
|
|
|
|
#define DEFAULT_AUTH_ADDRESS "/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cAuthenticator::cAuthenticator(void) :
|
2014-04-14 20:52:59 +00:00
|
|
|
super("cAuthenticator"),
|
|
|
|
m_Server(DEFAULT_AUTH_SERVER),
|
|
|
|
m_Address(DEFAULT_AUTH_ADDRESS),
|
|
|
|
m_ShouldAuthenticate(true)
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cAuthenticator::~cAuthenticator()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cAuthenticator::ReadINI(cIniFile & IniFile)
|
|
|
|
{
|
|
|
|
m_Server = IniFile.GetValueSet("Authentication", "Server", DEFAULT_AUTH_SERVER);
|
|
|
|
m_Address = IniFile.GetValueSet("Authentication", "Address", DEFAULT_AUTH_ADDRESS);
|
|
|
|
m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash)
|
|
|
|
{
|
|
|
|
if (!m_ShouldAuthenticate)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
cRoot::Get()->AuthenticateUser(a_ClientID, a_UserName, cClientHandle::GenerateOfflineUUID(a_UserName));
|
2014-04-13 11:04:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCSLock LOCK(m_CS);
|
|
|
|
m_Queue.push_back(cUser(a_ClientID, a_UserName, a_ServerHash));
|
|
|
|
m_QueueNonempty.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cAuthenticator::Start(cIniFile & IniFile)
|
|
|
|
{
|
|
|
|
ReadINI(IniFile);
|
|
|
|
m_ShouldTerminate = false;
|
|
|
|
super::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cAuthenticator::Stop(void)
|
|
|
|
{
|
|
|
|
m_ShouldTerminate = true;
|
|
|
|
m_QueueNonempty.Set();
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cAuthenticator::Execute(void)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (!m_ShouldTerminate && (m_Queue.size() == 0))
|
|
|
|
{
|
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
m_QueueNonempty.Wait();
|
|
|
|
}
|
|
|
|
if (m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT(!m_Queue.empty());
|
|
|
|
|
2014-04-18 19:44:58 +00:00
|
|
|
cAuthenticator::cUser & User = m_Queue.front();
|
|
|
|
int ClientID = User.m_ClientID;
|
|
|
|
AString UserName = User.m_Name;
|
|
|
|
AString ServerID = User.m_ServerID;
|
2014-04-13 11:04:56 +00:00
|
|
|
m_Queue.pop_front();
|
|
|
|
Lock.Unlock();
|
|
|
|
|
|
|
|
AString NewUserName = UserName;
|
|
|
|
AString UUID;
|
|
|
|
if (AuthWithYggdrasil(NewUserName, ServerID, UUID))
|
|
|
|
{
|
|
|
|
LOGINFO("User %s authenticated with UUID '%s'", NewUserName.c_str(), UUID.c_str());
|
|
|
|
cRoot::Get()->AuthenticateUser(ClientID, NewUserName, UUID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cRoot::Get()->KickUser(ClientID, "Failed to authenticate account!");
|
|
|
|
}
|
|
|
|
} // for (-ever)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_ServerId, AString & a_UUID)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGD("Trying to auth user %s", a_UserName.c_str());
|
|
|
|
|
2014-04-13 13:32:15 +00:00
|
|
|
int ret, server_fd = -1;
|
2014-04-13 14:15:57 +00:00
|
|
|
unsigned char buf[1024];
|
2014-04-24 19:51:19 +00:00
|
|
|
const char pers[] = "cAuthenticator";
|
2014-04-13 11:04:56 +00:00
|
|
|
|
2014-04-24 19:51:19 +00:00
|
|
|
cCtrDrbgContext CtrDrbg;
|
2014-04-13 11:04:56 +00:00
|
|
|
ssl_context ssl;
|
2014-04-24 19:51:19 +00:00
|
|
|
cX509Cert CACert;
|
2014-04-13 11:04:56 +00:00
|
|
|
|
|
|
|
/* Initialize the RNG and the session data */
|
|
|
|
memset(&ssl, 0, sizeof(ssl_context));
|
|
|
|
|
2014-04-24 19:51:19 +00:00
|
|
|
if ((ret = CtrDrbg.Initialize(pers, sizeof(pers))) != 0)
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
2014-04-24 19:51:19 +00:00
|
|
|
LOGWARNING("cAuthenticator: CtrDrbg.Initialize() returned %d", ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize certificates */
|
2014-04-24 19:51:19 +00:00
|
|
|
// This is the root cert for Starfield Technologies, the CA that signed sessionserver.mojang.com's cert:
|
|
|
|
static const char StarfieldCACert[] =
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
|
|
"MIID3TCCAsWgAwIBAgIBADANBgkqhkiG9w0BAQsFADCBjzELMAkGA1UEBhMCVVMx\n"
|
|
|
|
"EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAjBgNVBAoT\n"
|
|
|
|
"HFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xMjAwBgNVBAMTKVN0YXJmaWVs\n"
|
|
|
|
"ZCBSb290IENlcnRpZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTA5MDkwMTAwMDAw\n"
|
|
|
|
"MFoXDTM3MTIzMTIzNTk1OVowgY8xCzAJBgNVBAYTAlVTMRAwDgYDVQQIEwdBcml6\n"
|
|
|
|
"b25hMRMwEQYDVQQHEwpTY290dHNkYWxlMSUwIwYDVQQKExxTdGFyZmllbGQgVGVj\n"
|
|
|
|
"aG5vbG9naWVzLCBJbmMuMTIwMAYDVQQDEylTdGFyZmllbGQgUm9vdCBDZXJ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n"
|
|
|
|
"-----END CERTIFICATE-----";
|
|
|
|
// Parse the Starfield CA and add it to "trusted root certs"
|
|
|
|
ret = CACert.Parse(StarfieldCACert, sizeof(StarfieldCACert) - 1);
|
2014-04-13 11:04:56 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-04-24 19:51:19 +00:00
|
|
|
LOGWARNING("cAuthenticator: CACert.Parse returned -0x%x", -ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect */
|
|
|
|
if ((ret = net_connect(&server_fd, m_Server.c_str(), 443)) != 0)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: Can't connect to %s: %d", m_Server.c_str(), ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup */
|
|
|
|
if ((ret = ssl_init(&ssl)) != 0)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: ssl_init returned %d", ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ssl_set_endpoint(&ssl, SSL_IS_CLIENT);
|
|
|
|
ssl_set_authmode(&ssl, SSL_VERIFY_OPTIONAL);
|
2014-04-24 19:51:19 +00:00
|
|
|
ssl_set_ca_chain(&ssl, CACert.Get(), NULL, "PolarSSL Server 1");
|
|
|
|
ssl_set_rng(&ssl, ctr_drbg_random, CtrDrbg.Get());
|
2014-04-13 11:04:56 +00:00
|
|
|
ssl_set_bio(&ssl, net_recv, &server_fd, net_send, &server_fd);
|
|
|
|
|
|
|
|
/* Handshake */
|
|
|
|
while ((ret = ssl_handshake(&ssl)) != 0)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
if ((ret != POLARSSL_ERR_NET_WANT_READ) && (ret != POLARSSL_ERR_NET_WANT_WRITE))
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: ssl_handshake returned -0x%x", -ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the GET request */
|
|
|
|
AString ActualAddress = m_Address;
|
|
|
|
ReplaceString(ActualAddress, "%USERNAME%", a_UserName);
|
|
|
|
ReplaceString(ActualAddress, "%SERVERID%", a_ServerId);
|
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
AString Request;
|
|
|
|
Request += "GET " + ActualAddress + " HTTP/1.1\r\n";
|
|
|
|
Request += "Host: " + m_Server + "\r\n";
|
|
|
|
Request += "User-Agent: MCServer\r\n";
|
|
|
|
Request += "Connection: close\r\n";
|
|
|
|
Request += "\r\n";
|
2014-04-13 11:04:56 +00:00
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
ret = ssl_write(&ssl, (const unsigned char *)Request.c_str(), Request.size());
|
|
|
|
if (ret <= 0)
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: ssl_write returned %d", ret);
|
|
|
|
return false;
|
2014-04-13 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the HTTP response */
|
2014-04-14 20:52:59 +00:00
|
|
|
std::string Response;
|
2014-04-13 11:04:56 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2014-04-14 20:52:59 +00:00
|
|
|
ret = ssl_read(&ssl, buf, sizeof(buf));
|
2014-04-13 11:04:56 +00:00
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
if ((ret == POLARSSL_ERR_NET_WANT_READ) || (ret == POLARSSL_ERR_NET_WANT_WRITE))
|
|
|
|
{
|
2014-04-13 11:04:56 +00:00
|
|
|
continue;
|
2014-04-14 20:52:59 +00:00
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
if (ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
|
2014-04-14 20:52:59 +00:00
|
|
|
{
|
2014-04-13 11:04:56 +00:00
|
|
|
break;
|
2014-04-14 20:52:59 +00:00
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: ssl_read returned %d", ret);
|
2014-04-13 11:04:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGWARNING("cAuthenticator: EOF");
|
2014-04-13 11:04:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
Response.append((const char *)buf, ret);
|
2014-04-24 19:51:19 +00:00
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
|
|
|
|
ssl_close_notify(&ssl);
|
|
|
|
net_close(server_fd);
|
|
|
|
ssl_free(&ssl);
|
|
|
|
memset(&ssl, 0, sizeof(ssl));
|
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
// Check the HTTP status line:
|
|
|
|
AString prefix("HTTP/1.1 200 OK");
|
|
|
|
AString HexDump;
|
|
|
|
if (Response.compare(0, prefix.size(), prefix))
|
|
|
|
{
|
|
|
|
LOGINFO("User \"%s\" failed to auth, bad http status line received", a_UserName.c_str());
|
|
|
|
LOG("Response: \n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str());
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
2014-04-14 20:52:59 +00:00
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
// Erase the HTTP headers from the response:
|
|
|
|
size_t idxHeadersEnd = Response.find("\r\n\r\n");
|
|
|
|
if (idxHeadersEnd == AString::npos)
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
2014-04-14 20:52:59 +00:00
|
|
|
LOGINFO("User \"%s\" failed to authenticate, bad http response header received", a_UserName.c_str());
|
|
|
|
LOG("Response: \n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str());
|
|
|
|
return false;
|
2014-04-13 11:04:56 +00:00
|
|
|
}
|
2014-04-14 20:52:59 +00:00
|
|
|
Response.erase(0, idxHeadersEnd + 4);
|
2014-04-13 11:04:56 +00:00
|
|
|
|
2014-04-14 20:52:59 +00:00
|
|
|
// Parse the Json response:
|
|
|
|
if (Response.empty())
|
|
|
|
{
|
2014-04-13 11:04:56 +00:00
|
|
|
return false;
|
2014-04-14 20:52:59 +00:00
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
Json::Value root;
|
|
|
|
Json::Reader reader;
|
2014-04-14 20:52:59 +00:00
|
|
|
if (!reader.parse(Response, root, false))
|
2014-04-13 11:04:56 +00:00
|
|
|
{
|
|
|
|
LOGWARNING("cAuthenticator: Cannot parse Received Data to json!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
a_UserName = root.get("name", "Unknown").asString();
|
|
|
|
a_UUID = root.get("id", "").asString();
|
2014-04-14 20:52:59 +00:00
|
|
|
|
|
|
|
// If the UUID doesn't contain the hashes, insert them at the proper places:
|
|
|
|
if (a_UUID.size() == 32)
|
|
|
|
{
|
|
|
|
a_UUID.insert(8, "-");
|
|
|
|
a_UUID.insert(13, "-");
|
|
|
|
a_UUID.insert(18, "-");
|
|
|
|
a_UUID.insert(23, "-");
|
|
|
|
}
|
2014-04-13 11:04:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|