chore(Core/Authserver): restyle authserver library with astyle (#3464)

This commit is contained in:
Kargatum
2020-09-11 09:55:28 +07:00
committed by GitHub
parent c197181468
commit 3a8ca806c5
4 changed files with 70 additions and 69 deletions
+13 -13
View File
@@ -54,10 +54,10 @@ public:
{ {
switch (sigNum) switch (sigNum)
{ {
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
stopEvent = true; stopEvent = true;
break; break;
} }
} }
}; };
@@ -66,8 +66,8 @@ public:
void usage(const char* prog) void usage(const char* prog)
{ {
sLog->outString("Usage: \n %s [<options>]\n" sLog->outString("Usage: \n %s [<options>]\n"
" -c config_file use config_file as configuration file\n\r", " -c config_file use config_file as configuration file\n\r",
prog); prog);
} }
/// Launch the auth server /// Launch the auth server
@@ -100,16 +100,16 @@ extern int main(int argc, char** argv)
sLog->outString("%s (authserver)", GitRevision::GetFullVersion()); sLog->outString("%s (authserver)", GitRevision::GetFullVersion());
sLog->outString("<Ctrl-C> to stop.\n"); sLog->outString("<Ctrl-C> to stop.\n");
sLog->outString(" █████╗ ███████╗███████╗██████╗ ██████╗ ████████╗██╗ ██╗"); sLog->outString(" █████╗ ███████╗███████╗██████╗ ██████╗ ████████╗██╗ ██╗");
sLog->outString(" ██╔══██╗╚══███╔╝██╔════╝██╔══██╗██╔═══██╗╚══██╔══╝██║ ██║"); sLog->outString(" ██╔══██╗╚══███╔╝██╔════╝██╔══██╗██╔═══██╗╚══██╔══╝██║ ██║");
sLog->outString(" ███████║ ███╔╝ █████╗ ██████╔╝██║ ██║ ██║ ███████║"); sLog->outString(" ███████║ ███╔╝ █████╗ ██████╔╝██║ ██║ ██║ ███████║");
sLog->outString(" ██╔══██║ ███╔╝ ██╔══╝ ██╔══██╗██║ ██║ ██║ ██╔══██║"); sLog->outString(" ██╔══██║ ███╔╝ ██╔══╝ ██╔══██╗██║ ██║ ██║ ██╔══██║");
sLog->outString(" ██║ ██║███████╗███████╗██║ ██║╚██████╔╝ ██║ ██║ ██║"); sLog->outString(" ██║ ██║███████╗███████╗██║ ██║╚██████╔╝ ██║ ██║ ██║");
sLog->outString(" ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝"); sLog->outString(" ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝");
sLog->outString(" ██████╗ ██████╗ ██████╗ ███████╗"); sLog->outString(" ██████╗ ██████╗ ██████╗ ███████╗");
sLog->outString(" ██╔════╝██╔═══██╗██╔══██╗██╔═══╝"); sLog->outString(" ██╔════╝██╔═══██╗██╔══██╗██╔═══╝");
sLog->outString(" ██║ ██║ ██║██████╔╝█████╗"); sLog->outString(" ██║ ██║ ██║██████╔╝█████╗");
sLog->outString(" ██║ ██║ ██║██╔══██╗██╔══╝"); sLog->outString(" ██║ ██║ ██║██╔══██╗██╔══╝");
sLog->outString(" ╚██████╗╚██████╔╝██║ ██║███████╗"); sLog->outString(" ╚██████╗╚██████╔╝██║ ██║███████╗");
sLog->outString(" ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝\n"); sLog->outString(" ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝\n");
+44 -43
View File
@@ -154,7 +154,7 @@ public:
Patches::const_iterator begin() const { return _patches.begin(); } Patches::const_iterator begin() const { return _patches.begin(); }
Patches::const_iterator end() const { return _patches.end(); } Patches::const_iterator end() const { return _patches.end(); }
void LoadPatchMD5(char*); void LoadPatchMD5(char*);
bool GetHash(char * pat, uint8 mymd5[16]); bool GetHash(char* pat, uint8 mymd5[16]);
private: private:
void LoadPatchesInfo(); void LoadPatchesInfo();
@@ -206,16 +206,16 @@ void AuthSocket::OnClose(void)
// Read the packet from the client // Read the packet from the client
void AuthSocket::OnRead() void AuthSocket::OnRead()
{ {
#define MAX_AUTH_LOGON_CHALLENGES_IN_A_ROW 3 #define MAX_AUTH_LOGON_CHALLENGES_IN_A_ROW 3
uint32 challengesInARow = 0; uint32 challengesInARow = 0;
#define MAX_AUTH_GET_REALM_LIST 10 #define MAX_AUTH_GET_REALM_LIST 10
uint32 challengesInARowRealmList = 0; uint32 challengesInARowRealmList = 0;
uint8 _cmd; uint8 _cmd;
while (1) while (1)
{ {
if (!socket().recv_soft((char *)&_cmd, 1)) if (!socket().recv_soft((char*)&_cmd, 1))
return; return;
if (_cmd == AUTH_LOGON_CHALLENGE) if (_cmd == AUTH_LOGON_CHALLENGE)
@@ -228,14 +228,15 @@ void AuthSocket::OnRead()
return; return;
} }
} }
else if (_cmd == REALM_LIST) { else if (_cmd == REALM_LIST)
challengesInARowRealmList++; {
if (challengesInARowRealmList == MAX_AUTH_GET_REALM_LIST) challengesInARowRealmList++;
{ if (challengesInARowRealmList == MAX_AUTH_GET_REALM_LIST)
sLog->outString("Got %u REALM_LIST in a row from '%s', possible ongoing DoS", challengesInARowRealmList, socket().getRemoteAddress().c_str()); {
socket().shutdown(); sLog->outString("Got %u REALM_LIST in a row from '%s', possible ongoing DoS", challengesInARowRealmList, socket().getRemoteAddress().c_str());
return; socket().shutdown();
} return;
}
} }
size_t i; size_t i;
@@ -297,7 +298,7 @@ void AuthSocket::_SetVSFields(const std::string& rI)
v = g.ModExp(x, N); v = g.ModExp(x, N);
// No SQL injection (username escaped) // No SQL injection (username escaped)
char *v_hex, *s_hex; char* v_hex, *s_hex;
v_hex = v.AsHexStr(); v_hex = v.AsHexStr();
s_hex = s.AsHexStr(); s_hex = s.AsHexStr();
@@ -342,7 +343,7 @@ bool AuthSocket::_HandleLogonChallenge()
socket().send((char const*)pkt.contents(), pkt.size()); socket().send((char const*)pkt.contents(), pkt.size());
return true; return true;
} }
if (LastLoginAttemptCleanTime+60 < currTime) if (LastLoginAttemptCleanTime + 60 < currTime)
{ {
LastLoginAttemptTimeForIP.clear(); LastLoginAttemptTimeForIP.clear();
LastLoginAttemptCleanTime = currTime; LastLoginAttemptCleanTime = currTime;
@@ -355,11 +356,11 @@ bool AuthSocket::_HandleLogonChallenge()
std::vector<uint8> buf; std::vector<uint8> buf;
buf.resize(4); buf.resize(4);
socket().recv((char *)&buf[0], 4); socket().recv((char*)&buf[0], 4);
EndianConvertPtr<uint16>(&buf[0]); EndianConvertPtr<uint16>(&buf[0]);
uint16 remaining = ((sAuthLogonChallenge_C *)&buf[0])->size; uint16 remaining = ((sAuthLogonChallenge_C*)&buf[0])->size;
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] got header, body is %#04x bytes", remaining); sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] got header, body is %#04x bytes", remaining);
#endif #endif
@@ -370,10 +371,10 @@ bool AuthSocket::_HandleLogonChallenge()
//No big fear of memory outage (size is int16, i.e. < 65536) //No big fear of memory outage (size is int16, i.e. < 65536)
buf.resize(remaining + buf.size() + 1); buf.resize(remaining + buf.size() + 1);
buf[buf.size() - 1] = 0; buf[buf.size() - 1] = 0;
sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0]; sAuthLogonChallenge_C* ch = (sAuthLogonChallenge_C*)&buf[0];
// Read the remaining of the packet // Read the remaining of the packet
socket().recv((char *)&buf[4], remaining); socket().recv((char*)&buf[4], remaining);
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] got full packet, %#04x bytes", ch->size); sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] got full packet, %#04x bytes", ch->size);
sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] name(%d): '%s'", ch->I_len, ch->I); sLog->outDebug(LOG_FILTER_NETWORKIO, "[AuthChallenge] name(%d): '%s'", ch->I_len, ch->I);
@@ -416,7 +417,7 @@ bool AuthSocket::_HandleLogonChallenge()
{ {
pkt << uint8(WOW_FAIL_BANNED); pkt << uint8(WOW_FAIL_BANNED);
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] Banned ip tries to login!",socket().getRemoteAddress().c_str(), socket().getRemotePort()); sLog->outDebug(LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] Banned ip tries to login!", socket().getRemoteAddress().c_str(), socket().getRemotePort());
#endif #endif
} }
else else
@@ -546,12 +547,12 @@ bool AuthSocket::_HandleLogonChallenge()
pkt.append(s.AsByteArray().get(), s.GetNumBytes()); // 32 bytes pkt.append(s.AsByteArray().get(), s.GetNumBytes()); // 32 bytes
pkt.append(unk3.AsByteArray(16).get(), 16); pkt.append(unk3.AsByteArray(16).get(), 16);
uint8 securityFlags = 0; uint8 securityFlags = 0;
// Check if token is used // Check if token is used
_tokenKey = fields[8].GetString(); _tokenKey = fields[8].GetString();
if (!_tokenKey.empty()) if (!_tokenKey.empty())
securityFlags = 4; securityFlags = 4;
pkt << uint8(securityFlags); // security flags (0x0...0x04) pkt << uint8(securityFlags); // security flags (0x0...0x04)
if (securityFlags & 0x01) // PIN input if (securityFlags & 0x01) // PIN input
@@ -577,11 +578,11 @@ bool AuthSocket::_HandleLogonChallenge()
_localizationName.resize(4); _localizationName.resize(4);
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
_localizationName[i] = ch->country[4-i-1]; _localizationName[i] = ch->country[4 - i - 1];
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug( LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] account %s is using '%c%c%c%c' locale (%u)", socket().getRemoteAddress().c_str(), socket().getRemotePort(), _login.c_str (), ch->country[3], ch->country[2], ch->country[1], ch->country[0], GetLocaleByName(_localizationName) ); sLog->outDebug( LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] account %s is using '%c%c%c%c' locale (%u)", socket().getRemoteAddress().c_str(), socket().getRemotePort(), _login.c_str (), ch->country[3], ch->country[2], ch->country[1], ch->country[0], GetLocaleByName(_localizationName) );
#endif #endif
///- All good, await client's proof ///- All good, await client's proof
@@ -606,7 +607,7 @@ bool AuthSocket::_HandleLogonProof()
// Read the packet // Read the packet
sAuthLogonProof_C lp; sAuthLogonProof_C lp;
if (!socket().recv((char *)&lp, sizeof(sAuthLogonProof_C))) if (!socket().recv((char*)&lp, sizeof(sAuthLogonProof_C)))
return false; return false;
_status = STATUS_CLOSED; _status = STATUS_CLOSED;
@@ -707,9 +708,9 @@ bool AuthSocket::_HandleLogonProof()
// Update the sessionkey, last_ip, last login time and reset number of failed logins in the account table for this account // Update the sessionkey, last_ip, last login time and reset number of failed logins in the account table for this account
// No SQL injection (escaped user name) and IP address as received by socket // No SQL injection (escaped user name) and IP address as received by socket
const char *K_hex = K.AsHexStr(); const char* K_hex = K.AsHexStr();
PreparedStatement *stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGONPROOF); PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGONPROOF);
stmt->setString(0, K_hex); stmt->setString(0, K_hex);
stmt->setString(1, socket().getRemoteAddress().c_str()); stmt->setString(1, socket().getRemoteAddress().c_str());
stmt->setUInt32(2, GetLocaleByName(_localizationName)); stmt->setUInt32(2, GetLocaleByName(_localizationName));
@@ -724,7 +725,7 @@ bool AuthSocket::_HandleLogonProof()
sha.UpdateBigNumbers(&A, &M, &K, nullptr); sha.UpdateBigNumbers(&A, &M, &K, nullptr);
sha.Finalize(); sha.Finalize();
// Check auth token // Check auth token
if ((lp.securityFlags & 0x04) || !_tokenKey.empty()) if ((lp.securityFlags & 0x04) || !_tokenKey.empty())
{ {
uint8 size; uint8 size;
@@ -742,7 +743,7 @@ bool AuthSocket::_HandleLogonProof()
return false; return false;
} }
} }
if (_expversion & POST_BC_EXP_FLAG) // 2.x and 3.x clients if (_expversion & POST_BC_EXP_FLAG) // 2.x and 3.x clients
{ {
sAuthLogonProof_S proof; sAuthLogonProof_S proof;
@@ -752,7 +753,7 @@ bool AuthSocket::_HandleLogonProof()
proof.unk1 = 0x00800000; // Accountflags. 0x01 = GM, 0x08 = Trial, 0x00800000 = Pro pass (arena tournament) proof.unk1 = 0x00800000; // Accountflags. 0x01 = GM, 0x08 = Trial, 0x00800000 = Pro pass (arena tournament)
proof.unk2 = 0x00; // SurveyId proof.unk2 = 0x00; // SurveyId
proof.unk3 = 0x00; proof.unk3 = 0x00;
socket().send((char *)&proof, sizeof(proof)); socket().send((char*)&proof, sizeof(proof));
} }
else else
{ {
@@ -761,7 +762,7 @@ bool AuthSocket::_HandleLogonProof()
proof.cmd = AUTH_LOGON_PROOF; proof.cmd = AUTH_LOGON_PROOF;
proof.error = 0; proof.error = 0;
proof.unk2 = 0x00; proof.unk2 = 0x00;
socket().send((char *)&proof, sizeof(proof)); socket().send((char*)&proof, sizeof(proof));
} }
///- Set _status to authed! ///- Set _status to authed!
@@ -829,7 +830,7 @@ bool AuthSocket::_HandleLogonProof()
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] IP %s got banned for '%u' seconds because account %s failed to authenticate '%u' times", sLog->outDebug(LOG_FILTER_NETWORKIO, "'%s:%d' [AuthChallenge] IP %s got banned for '%u' seconds because account %s failed to authenticate '%u' times",
socket().getRemoteAddress().c_str(), socket().getRemotePort(), socket().getRemoteAddress().c_str(), WrongPassBanTime, _login.c_str(), failed_logins); socket().getRemoteAddress().c_str(), socket().getRemotePort(), socket().getRemoteAddress().c_str(), WrongPassBanTime, _login.c_str(), failed_logins);
#endif #endif
} }
} }
@@ -853,11 +854,11 @@ bool AuthSocket::_HandleReconnectChallenge()
std::vector<uint8> buf; std::vector<uint8> buf;
buf.resize(4); buf.resize(4);
socket().recv((char *)&buf[0], 4); socket().recv((char*)&buf[0], 4);
EndianConvertPtr<uint16>(&buf[0]); EndianConvertPtr<uint16>(&buf[0]);
uint16 remaining = ((sAuthLogonChallenge_C *)&buf[0])->size; uint16 remaining = ((sAuthLogonChallenge_C*)&buf[0])->size;
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] got header, body is %#04x bytes", remaining); sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] got header, body is %#04x bytes", remaining);
#endif #endif
@@ -871,10 +872,10 @@ bool AuthSocket::_HandleReconnectChallenge()
// No big fear of memory outage (size is int16, i.e. < 65536) // No big fear of memory outage (size is int16, i.e. < 65536)
buf.resize(remaining + buf.size() + 1); buf.resize(remaining + buf.size() + 1);
buf[buf.size() - 1] = 0; buf[buf.size() - 1] = 0;
sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0]; sAuthLogonChallenge_C* ch = (sAuthLogonChallenge_C*)&buf[0];
// Read the remaining of the packet // Read the remaining of the packet
socket().recv((char *)&buf[4], remaining); socket().recv((char*)&buf[4], remaining);
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS) #if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] got full packet, %#04x bytes", ch->size); sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] got full packet, %#04x bytes", ch->size);
sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] name(%d): '%s'", ch->I_len, ch->I); sLog->outDebug(LOG_FILTER_NETWORKIO, "[ReconnectChallenge] name(%d): '%s'", ch->I_len, ch->I);
@@ -933,7 +934,7 @@ bool AuthSocket::_HandleReconnectProof()
#endif #endif
// Read the packet // Read the packet
sAuthReconnectProof_C lp; sAuthReconnectProof_C lp;
if (!socket().recv((char *)&lp, sizeof(sAuthReconnectProof_C))) if (!socket().recv((char*)&lp, sizeof(sAuthReconnectProof_C)))
return false; return false;
_status = STATUS_CLOSED; _status = STATUS_CLOSED;
@@ -1032,7 +1033,7 @@ bool AuthSocket::_HandleRealmList()
size_t RealmListSize = 0; size_t RealmListSize = 0;
for (RealmList::RealmMap::const_iterator i = sRealmList->begin(); i != sRealmList->end(); ++i) for (RealmList::RealmMap::const_iterator i = sRealmList->begin(); i != sRealmList->end(); ++i)
{ {
const Realm &realm = i->second; const Realm& realm = i->second;
// don't work with realms which not compatible with the client // don't work with realms which not compatible with the client
bool okBuild = ((_expversion & POST_BC_EXP_FLAG) && realm.gamebuild == _build) || ((_expversion & PRE_BC_EXP_FLAG) && !AuthHelper::IsPreBCAcceptedClientBuild(realm.gamebuild)); bool okBuild = ((_expversion & POST_BC_EXP_FLAG) && realm.gamebuild == _build) || ((_expversion & PRE_BC_EXP_FLAG) && !AuthHelper::IsPreBCAcceptedClientBuild(realm.gamebuild));
@@ -1199,8 +1200,8 @@ void PatcherRunnable::run() { }
#include <errno.h> #include <errno.h>
void Patcher::LoadPatchesInfo() void Patcher::LoadPatchesInfo()
{ {
DIR *dirp; DIR* dirp;
struct dirent *dp; struct dirent* dp;
dirp = opendir("./patches/"); dirp = opendir("./patches/");
if (!dirp) if (!dirp)
@@ -1248,7 +1249,7 @@ void Patcher::LoadPatchesInfo()
#endif #endif
// Calculate and store MD5 hash for a given patch file // Calculate and store MD5 hash for a given patch file
void Patcher::LoadPatchMD5(char *szFileName) void Patcher::LoadPatchMD5(char* szFileName)
{ {
// Try to open the patch file // Try to open the patch file
std::string path = "./patches/"; std::string path = "./patches/";
@@ -1280,11 +1281,11 @@ void Patcher::LoadPatchMD5(char *szFileName)
// Store the result in the internal patch hash map // Store the result in the internal patch hash map
_patches[path] = new PATCH_INFO; _patches[path] = new PATCH_INFO;
MD5_Final((uint8 *)&_patches[path]->md5, &ctx); MD5_Final((uint8*)&_patches[path]->md5, &ctx);
} }
// Get cached MD5 hash for a given patch file // Get cached MD5 hash for a given patch file
bool Patcher::GetHash(char * pat, uint8 mymd5[16]) bool Patcher::GetHash(char* pat, uint8 mymd5[16])
{ {
for (Patches::iterator i = _patches.begin(); i != _patches.end(); ++i) for (Patches::iterator i = _patches.begin(); i != _patches.end(); ++i)
if (!stricmp(pat, i->first.c_str())) if (!stricmp(pat, i->first.c_str()))
+1 -1
View File
@@ -24,7 +24,7 @@ public:
} }
protected: protected:
virtual int make_svc_handler(RealmSocket* &sh) virtual int make_svc_handler(RealmSocket*& sh)
{ {
if (sh == 0) if (sh == 0)
ACE_NEW_RETURN(sh, RealmSocket, -1); ACE_NEW_RETURN(sh, RealmSocket, -1);
+12 -12
View File
@@ -26,27 +26,27 @@ int base32_decode(const char* encoded, char* result, int bufSize)
int buffer = 0; int buffer = 0;
int bitsLeft = 0; int bitsLeft = 0;
int count = 0; int count = 0;
for (const char *ptr = encoded; count < bufSize && *ptr; ++ptr) for (const char* ptr = encoded; count < bufSize && *ptr; ++ptr)
{ {
char ch = *ptr; char ch = *ptr;
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-')
continue; continue;
buffer <<= 5; buffer <<= 5;
// Deal with commonly mistyped characters // Deal with commonly mistyped characters
if (ch == '0') if (ch == '0')
ch = 'O'; ch = 'O';
else if (ch == '1') else if (ch == '1')
ch = 'L'; ch = 'L';
else if (ch == '8') else if (ch == '8')
ch = 'B'; ch = 'B';
// Look up one base32 digit // Look up one base32 digit
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
ch = (ch & 0x1F) - 1; ch = (ch & 0x1F) - 1;
else if (ch >= '2' && ch <= '7') else if (ch >= '2' && ch <= '7')
ch -= '2' - 26; ch -= '2' - 26;
else else
return -1; return -1;
buffer |= ch; buffer |= ch;
bitsLeft += 5; bitsLeft += 5;
if (bitsLeft >= 8) if (bitsLeft >= 8)
{ {
@@ -54,7 +54,7 @@ int base32_decode(const char* encoded, char* result, int bufSize)
bitsLeft -= 8; bitsLeft -= 8;
} }
} }
if (count < bufSize) if (count < bufSize)
result[count] = '\000'; result[count] = '\000';
return count; return count;
} }
@@ -66,21 +66,21 @@ namespace TOTP
unsigned int GenerateToken(const char* b32key) unsigned int GenerateToken(const char* b32key)
{ {
size_t keySize = strlen(b32key); size_t keySize = strlen(b32key);
int bufsize = (keySize + 7)/8*5; int bufsize = (keySize + 7) / 8 * 5;
char* encoded = new char[bufsize]; char* encoded = new char[bufsize];
memset(encoded, 0, bufsize); memset(encoded, 0, bufsize);
unsigned int hmacResSize = HMAC_RES_SIZE; unsigned int hmacResSize = HMAC_RES_SIZE;
unsigned char hmacRes[HMAC_RES_SIZE]; unsigned char hmacRes[HMAC_RES_SIZE];
unsigned long timestamp = time(nullptr)/30; unsigned long timestamp = time(nullptr) / 30;
unsigned char challenge[8]; unsigned char challenge[8];
for (int i = 8; i--;timestamp >>= 8) for (int i = 8; i--; timestamp >>= 8)
challenge[i] = timestamp; challenge[i] = timestamp;
base32_decode(b32key, encoded, bufsize); base32_decode(b32key, encoded, bufsize);
HMAC(EVP_sha1(), encoded, bufsize, challenge, 8, hmacRes, &hmacResSize); HMAC(EVP_sha1(), encoded, bufsize, challenge, 8, hmacRes, &hmacResSize);
unsigned int offset = hmacRes[19] & 0xF; unsigned int offset = hmacRes[19] & 0xF;
unsigned int truncHash = (hmacRes[offset] << 24) | (hmacRes[offset+1] << 16 )| (hmacRes[offset+2] << 8) | (hmacRes[offset+3]); unsigned int truncHash = (hmacRes[offset] << 24) | (hmacRes[offset + 1] << 16 ) | (hmacRes[offset + 2] << 8) | (hmacRes[offset + 3]);
truncHash &= 0x7FFFFFFF; truncHash &= 0x7FFFFFFF;
delete[] encoded; delete[] encoded;
return truncHash % 1000000; return truncHash % 1000000;
} }
} }