refactor(Core): remove ace_autoptr, cleanup (#3276)

This commit is contained in:
Viste
2020-08-25 13:52:22 +03:00
committed by GitHub
parent 208c257b99
commit a9b90c9a07
7 changed files with 46 additions and 42 deletions

View File

@@ -10,7 +10,6 @@
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <algorithm>
#include <ace/Auto_Ptr.h>
BigNumber::BigNumber()
: _bn(BN_new())
@@ -158,23 +157,24 @@ bool BigNumber::isZero() const
return BN_is_zero(_bn);
}
ACE_Auto_Array_Ptr<uint8> BigNumber::AsByteArray(int32 minSize, bool littleEndian)
std::unique_ptr<uint8[]> BigNumber::AsByteArray(int32 minSize, bool littleEndian)
{
int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
int numBytes = GetNumBytes();
int length = (minSize >= numBytes) ? minSize : numBytes;
uint8* array = new uint8[length];
// If we need more bytes than length of BigNumber set the rest to 0
if (length > GetNumBytes())
if (length > numBytes)
memset((void*)array, 0, length);
BN_bn2bin(_bn, (unsigned char *)array);
BN_bn2bin(_bn, (unsigned char*)array);
// openssl's BN stores data internally in big endian format, reverse if little endian desired
if (littleEndian)
std::reverse(array, array + length);
ACE_Auto_Array_Ptr<uint8> ret(array);
std::unique_ptr<uint8[]> ret(array);
return ret;
}

View File

@@ -8,7 +8,10 @@
#define _AUTH_BIGNUMBER_H
#include "Define.h"
#include <ace/Auto_Ptr.h>
#include "Errors.h"
#include <array>
#include <memory>
#include <string>
struct bignum_st;
@@ -75,7 +78,7 @@ class BigNumber
uint32 AsDword();
ACE_Auto_Array_Ptr<uint8> AsByteArray(int32 minSize = 0, bool littleEndian = true);
std::unique_ptr<uint8[]> AsByteArray(int32 minSize = 0, bool littleEndian = true);
char * AsHexStr() const;
char * AsDecStr() const;

View File

@@ -6,7 +6,8 @@
#include "HMACSHA1.h"
#include "BigNumber.h"
#include "Common.h"
#include "Errors.h"
#include <cstring>
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX* HMAC_CTX_new()
@@ -23,8 +24,7 @@ void HMAC_CTX_free(HMAC_CTX* ctx)
}
#endif
HmacHash::HmacHash(uint32 len, uint8 *seed)
HmacHash::HmacHash(uint32 len, uint8* seed)
{
m_ctx = HMAC_CTX_new();
HMAC_Init_ex(m_ctx, seed, len, EVP_sha1(), nullptr);
@@ -36,14 +36,14 @@ HmacHash::~HmacHash()
HMAC_CTX_free(m_ctx);
}
void HmacHash::UpdateData(const std::string &str)
void HmacHash::UpdateData(std::string const& str)
{
HMAC_Update(m_ctx, reinterpret_cast<uint8 const*>(str.c_str()), str.length());
}
void HmacHash::UpdateData(const uint8* data, size_t len)
void HmacHash::UpdateData(uint8 const* data, size_t len)
{
HMAC_Update(m_ctx, data, len);
HMAC_Update(m_ctx, data, len);
}
void HmacHash::Finalize()

View File

@@ -19,10 +19,10 @@ class BigNumber;
class HmacHash
{
public:
HmacHash(uint32 len, uint8 *seed);
HmacHash(uint32 len, uint8* seed);
~HmacHash();
void UpdateData(const std::string &str);
void UpdateData(const uint8* data, size_t len);
void UpdateData(std::string const& str);
void UpdateData(uint8 const* data, size_t len);
void Finalize();
uint8* ComputeHash(BigNumber* bn);
uint8* GetDigest() { return m_digest; }

View File

@@ -6,52 +6,44 @@
#include <OpenSSLCrypto.h>
#include <openssl/crypto.h>
#include <ace/Thread_Mutex.h>
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x1010000fL
#include <vector>
#include <ace/Thread.h>
std::vector<ACE_Thread_Mutex*> cryptoLocks;
static void lockingCallback(int mode, int type, const char* /*file*/, int /*line*/)
#include <thread>
#include <mutex>
std::vector<std::mutex*> cryptoLocks;
static void lockingCallback(int mode, int type, char const* /*file*/, int /*line*/)
{
if (mode & CRYPTO_LOCK)
cryptoLocks[type]->acquire();
cryptoLocks[type]->lock();
else
cryptoLocks[type]->release();
cryptoLocks[type]->unlock();
}
static void threadIdCallback(CRYPTO_THREADID * id)
{
(void)id;
/// ACE_thread_t turns out to be a struct under Mac OS.
#ifndef __APPLE__
CRYPTO_THREADID_set_numeric(id, ACE_Thread::self());
#else
CRYPTO_THREADID_set_pointer(id, ACE_Thread::self());
#endif
CRYPTO_THREADID_set_numeric(id, std::hash<std::thread::id>()(std::this_thread::get_id()));
}
void OpenSSLCrypto::threadsSetup()
{
cryptoLocks.resize(CRYPTO_num_locks());
for(int i = 0 ; i < CRYPTO_num_locks(); ++i)
{
cryptoLocks[i] = new ACE_Thread_Mutex();
cryptoLocks[i] = new std::mutex();
}
(void)&threadIdCallback;
CRYPTO_THREADID_set_callback(threadIdCallback);
(void)&lockingCallback;
CRYPTO_set_locking_callback(lockingCallback);
}
void OpenSSLCrypto::threadsCleanup()
{
CRYPTO_set_locking_callback(NULL);
CRYPTO_THREADID_set_callback(NULL);
CRYPTO_set_locking_callback(nullptr);
CRYPTO_THREADID_set_callback(nullptr);
for(int i = 0 ; i < CRYPTO_num_locks(); ++i)
{
delete cryptoLocks[i];
}
cryptoLocks.resize(0);
}
}
#endif

View File

@@ -7,16 +7,26 @@
#ifndef OPENSSL_CRYPTO_H
#define OPENSSL_CRYPTO_H
#include "Define.h"
#include <openssl/opensslv.h>
/**
* A group of functions which setup openssl crypto module to work properly in multithreaded enviroment
* If not setup properly - it will crash
*/
namespace OpenSSLCrypto
{
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x1010000fL
/// Needs to be called before threads using openssl are spawned
void threadsSetup();
/// Needs to be called after threads using openssl are despawned
void threadsCleanup();
#else
void threadsSetup() { };
void threadsCleanup() { };
#endif
}
#endif
#endif

View File

@@ -13,7 +13,6 @@
#include <ace/os_include/sys/os_socket.h>
#include <ace/OS_NS_string.h>
#include <ace/Reactor.h>
#include <ace/Auto_Ptr.h>
#include "WorldSocket.h"
#include "Common.h"
@@ -660,7 +659,7 @@ int WorldSocket::ProcessIncoming(WorldPacket* new_pct)
ACE_ASSERT (new_pct);
// manage memory ;)
ACE_Auto_Ptr<WorldPacket> aptr (new_pct);
std::unique_ptr<WorldPacket> aptr (new_pct);
const uint16 opcode = new_pct->GetOpcode();