Fix BigNumber::AsByteArray function by returning Auto_Ptr.

Remove mutex from BigNumber class - it didn't do what it was advertised to do - consider using the "locked" array outside of the function in which it was "locked".
This commit is contained in:
QAston
2013-08-18 16:06:29 +02:00
parent 8160633e12
commit 2a3370929d
9 changed files with 38 additions and 47 deletions

View File

@@ -267,12 +267,12 @@ void AuthSocket::_SetVSFields(const std::string& rI)
uint8 mDigest[SHA_DIGEST_LENGTH];
memset(mDigest, 0, SHA_DIGEST_LENGTH);
if (I.GetNumBytes() <= SHA_DIGEST_LENGTH)
memcpy(mDigest, I.AsByteArray(), I.GetNumBytes());
memcpy(mDigest, I.AsByteArray().get(), I.GetNumBytes());
std::reverse(mDigest, mDigest + SHA_DIGEST_LENGTH);
SHA1Hash sha;
sha.UpdateData(s.AsByteArray(), s.GetNumBytes());
sha.UpdateData(s.AsByteArray().get(), s.GetNumBytes());
sha.UpdateData(mDigest, SHA_DIGEST_LENGTH);
sha.Finalize();
BigNumber x;
@@ -484,13 +484,13 @@ bool AuthSocket::_HandleLogonChallenge()
pkt << uint8(WOW_FAIL_VERSION_INVALID);
// B may be calculated < 32B so we force minimal length to 32B
pkt.append(B.AsByteArray(32), 32); // 32 bytes
pkt.append(B.AsByteArray(32).get(), 32); // 32 bytes
pkt << uint8(1);
pkt.append(g.AsByteArray(), 1);
pkt.append(g.AsByteArray().get(), 1);
pkt << uint8(32);
pkt.append(N.AsByteArray(32), 32);
pkt.append(s.AsByteArray(), s.GetNumBytes()); // 32 bytes
pkt.append(unk3.AsByteArray(16), 16);
pkt.append(N.AsByteArray(32).get(), 32);
pkt.append(s.AsByteArray().get(), s.GetNumBytes()); // 32 bytes
pkt.append(unk3.AsByteArray(16).get(), 16);
uint8 securityFlags = 0;
pkt << uint8(securityFlags); // security flags (0x0...0x04)
@@ -574,7 +574,7 @@ bool AuthSocket::_HandleLogonProof()
uint8 t[32];
uint8 t1[16];
uint8 vK[40];
memcpy(t, S.AsByteArray(32), 32);
memcpy(t, S.AsByteArray(32).get(), 32);
for (int i = 0; i < 16; ++i)
t1[i] = t[i * 2];
@@ -629,7 +629,7 @@ bool AuthSocket::_HandleLogonProof()
M.SetBinary(sha.GetDigest(), 20);
// Check if SRP6 results match (password is correct), else send an error
if (!memcmp(M.AsByteArray(), lp.M1, 20))
if (!memcmp(M.AsByteArray().get(), lp.M1, 20))
{
TC_LOG_DEBUG(LOG_FILTER_AUTHSERVER, "'%s:%d' User '%s' successfully authenticated", socket().getRemoteAddress().c_str(), socket().getRemotePort(), _login.c_str());
@@ -800,7 +800,7 @@ bool AuthSocket::_HandleReconnectChallenge()
pkt << uint8(AUTH_RECONNECT_CHALLENGE);
pkt << uint8(0x00);
_reconnectProof.SetRand(16 * 8);
pkt.append(_reconnectProof.AsByteArray(16), 16); // 16 bytes random
pkt.append(_reconnectProof.AsByteArray(16).get(), 16); // 16 bytes random
pkt << uint64(0x00) << uint64(0x00); // 16 bytes zeros
socket().send((char const*)pkt.contents(), pkt.size());
return true;

View File

@@ -730,11 +730,11 @@ int WorldSocket::HandleSendAuthSession()
BigNumber seed1;
seed1.SetRand(16 * 8);
packet.append(seed1.AsByteArray(16), 16); // new encryption seeds
packet.append(seed1.AsByteArray(16).get(), 16); // new encryption seeds
BigNumber seed2;
seed2.SetRand(16 * 8);
packet.append(seed2.AsByteArray(16), 16); // new encryption seeds
packet.append(seed2.AsByteArray(16).get(), 16); // new encryption seeds
return SendPacket(packet);
}

View File

@@ -94,7 +94,7 @@ void WardenCheckMgr::LoadWardenChecks()
{
uint8 temp[24];
memset(temp, 0, len);
memcpy(temp, wardenCheck->Data.AsByteArray(), wardenCheck->Data.GetNumBytes());
memcpy(temp, wardenCheck->Data.AsByteArray().get(), wardenCheck->Data.GetNumBytes());
std::reverse(temp, temp + len);
wardenCheck->Data.SetBinary((uint8*)temp, len);
}
@@ -126,7 +126,7 @@ void WardenCheckMgr::LoadWardenChecks()
{
uint8 *temp = new uint8[len];
memset(temp, 0, len);
memcpy(temp, wr->Result.AsByteArray(), wr->Result.GetNumBytes());
memcpy(temp, wr->Result.AsByteArray().get(), wr->Result.GetNumBytes());
std::reverse(temp, temp + len);
wr->Result.SetBinary((uint8*)temp, len);
delete [] temp;

View File

@@ -42,7 +42,7 @@ void WardenMac::Init(WorldSession* pClient, BigNumber* K)
{
_session = pClient;
// Generate Warden Key
SHA1Randx WK(K->AsByteArray(), K->GetNumBytes());
SHA1Randx WK(K->AsByteArray().get(), K->GetNumBytes());
WK.Generate(_inputKey, 16);
WK.Generate(_outputKey, 16);
/*

View File

@@ -46,7 +46,7 @@ void WardenWin::Init(WorldSession* session, BigNumber* k)
{
_session = session;
// Generate Warden Key
SHA1Randx WK(k->AsByteArray(), k->GetNumBytes());
SHA1Randx WK(k->AsByteArray().get(), k->GetNumBytes());
WK.Generate(_inputKey, 16);
WK.Generate(_outputKey, 16);
@@ -266,7 +266,7 @@ void WardenWin::RequestData()
case PAGE_CHECK_A:
case PAGE_CHECK_B:
{
buff.append(wd->Data.AsByteArray(0, false), wd->Data.GetNumBytes());
buff.append(wd->Data.AsByteArray(0, false).get(), wd->Data.GetNumBytes());
buff << uint32(wd->Address);
buff << uint8(wd->Length);
break;
@@ -279,7 +279,7 @@ void WardenWin::RequestData()
}
case DRIVER_CHECK:
{
buff.append(wd->Data.AsByteArray(0, false), wd->Data.GetNumBytes());
buff.append(wd->Data.AsByteArray(0, false).get(), wd->Data.GetNumBytes());
buff << uint8(index++);
break;
}
@@ -295,7 +295,7 @@ void WardenWin::RequestData()
}
/*case PROC_CHECK:
{
buff.append(wd->i.AsByteArray(0, false), wd->i.GetNumBytes());
buff.append(wd->i.AsByteArray(0, false).get(), wd->i.GetNumBytes());
buff << uint8(index++);
buff << uint8(index++);
buff << uint32(wd->Address);
@@ -395,7 +395,7 @@ void WardenWin::HandleData(ByteBuffer &buff)
continue;
}
if (memcmp(buff.contents() + buff.rpos(), rs->Result.AsByteArray(0, false), rd->Length) != 0)
if (memcmp(buff.contents() + buff.rpos(), rs->Result.AsByteArray(0, false).get(), rd->Length) != 0)
{
TC_LOG_DEBUG(LOG_FILTER_WARDEN, "RESULT MEM_CHECK fail CheckId %u account Id %u", *itr, _session->GetAccountId());
checkFailed = *itr;
@@ -474,7 +474,7 @@ void WardenWin::HandleData(ByteBuffer &buff)
continue;
}
if (memcmp(buff.contents() + buff.rpos(), rs->Result.AsByteArray(0, false), 20) != 0) // SHA1
if (memcmp(buff.contents() + buff.rpos(), rs->Result.AsByteArray(0, false).get(), 20) != 0) // SHA1
{
TC_LOG_DEBUG(LOG_FILTER_WARDEN, "RESULT MPQ_CHECK fail, CheckId %u account Id %u", *itr, _session->GetAccountId());
checkFailed = *itr;

View File

@@ -22,17 +22,18 @@
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <algorithm>
#include <ace/Auto_Ptr.h>
BigNumber::BigNumber()
: _bn(BN_new()), _array(NULL)
: _bn(BN_new())
{ }
BigNumber::BigNumber(BigNumber const& bn)
: _bn(BN_dup(bn._bn)), _array(NULL)
: _bn(BN_dup(bn._bn))
{ }
BigNumber::BigNumber(uint32 val)
: _bn(BN_new()), _array(NULL)
: _bn(BN_new())
{
BN_set_word(_bn, val);
}
@@ -40,7 +41,6 @@ BigNumber::BigNumber(uint32 val)
BigNumber::~BigNumber()
{
BN_free(_bn);
delete[] _array;
}
void BigNumber::SetDword(uint32 val)
@@ -170,29 +170,23 @@ bool BigNumber::isZero() const
return BN_is_zero(_bn);
}
uint8* BigNumber::AsByteArray(int32 minSize, bool reverse)
ACE_Auto_Array_Ptr<uint8> BigNumber::AsByteArray(int32 minSize, bool littleEndian)
{
int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
ACE_GUARD_RETURN(ACE_Mutex, g, _lock, 0);
if (_array)
{
delete[] _array;
_array = NULL;
}
_array = new uint8[length];
uint8* array = new uint8[length];
// If we need more bytes than length of BigNumber set the rest to 0
if (length > GetNumBytes())
memset((void*)_array, 0, length);
memset((void*)array, 0, length);
BN_bn2bin(_bn, (unsigned char *)_array);
BN_bn2bin(_bn, (unsigned char *)array);
if (reverse)
std::reverse(_array, _array + length);
// openssl's BN stores data internally in big endian format, reverse if little endian desired
if (littleEndian)
std::reverse(array, array + length);
return _array;
return ACE_Auto_Array_Ptr<uint8>(array);
}
char * BigNumber::AsHexStr() const

View File

@@ -20,7 +20,7 @@
#define _AUTH_BIGNUMBER_H
#include "Define.h"
#include <ace/Mutex.h>
#include <ace/Auto_Ptr.h>
struct bignum_st;
@@ -86,17 +86,14 @@ class BigNumber
struct bignum_st *BN() { return _bn; }
uint32 AsDword();
uint8* AsByteArray(int32 minSize = 0, bool reverse = true);
ACE_Auto_Array_Ptr<uint8> AsByteArray(int32 minSize = 0, bool littleEndian = true);
char * AsHexStr() const;
char * AsDecStr() const;
private:
struct bignum_st *_bn;
uint8 *_array;
// This mutex only controls thread-safe access to AsByteArray() and should be replaced with a thread-safe implementation of BigNumber
ACE_Mutex _lock;
};
#endif

View File

@@ -45,7 +45,7 @@ void HmacHash::Finalize()
uint8 *HmacHash::ComputeHash(BigNumber* bn)
{
HMAC_Update(&m_ctx, bn->AsByteArray(), bn->GetNumBytes());
HMAC_Update(&m_ctx, bn->AsByteArray().get(), bn->GetNumBytes());
Finalize();
return (uint8*)m_digest;
}

View File

@@ -50,7 +50,7 @@ void SHA1Hash::UpdateBigNumbers(BigNumber* bn0, ...)
bn = bn0;
while (bn)
{
UpdateData(bn->AsByteArray(), bn->GetNumBytes());
UpdateData(bn->AsByteArray().get(), bn->GetNumBytes());
bn = va_arg(v, BigNumber*);
}
va_end(v);