diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 64 | ||||
-rw-r--r-- | src/common/Collision/Management/MMapManager.cpp | 2 | ||||
-rw-r--r-- | src/common/Collision/Maps/MapDefines.h | 36 | ||||
-rw-r--r-- | src/common/Cryptography/ARC4.cpp | 51 | ||||
-rw-r--r-- | src/common/Cryptography/ARC4.h | 37 | ||||
-rw-r--r-- | src/common/Cryptography/Authentication/AuthCrypt.cpp | 75 | ||||
-rw-r--r-- | src/common/Cryptography/Authentication/AuthCrypt.h | 42 | ||||
-rw-r--r-- | src/common/Cryptography/BigNumber.cpp | 201 | ||||
-rw-r--r-- | src/common/Cryptography/BigNumber.h | 100 | ||||
-rw-r--r-- | src/common/Cryptography/HMACSHA1.cpp | 57 | ||||
-rw-r--r-- | src/common/Cryptography/HMACSHA1.h | 47 | ||||
-rw-r--r-- | src/common/Cryptography/OpenSSLCrypto.cpp | 59 | ||||
-rw-r--r-- | src/common/Cryptography/OpenSSLCrypto.h | 33 | ||||
-rw-r--r-- | src/common/Cryptography/SHA1.cpp | 69 | ||||
-rw-r--r-- | src/common/Cryptography/SHA1.h | 50 | ||||
-rw-r--r-- | src/common/Cryptography/WardenKeyGeneration.h | 81 | ||||
-rw-r--r-- | src/common/GitRevision.cpp | 78 | ||||
-rw-r--r-- | src/common/GitRevision.h | 40 |
18 files changed, 1080 insertions, 42 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 010a4f9ac43..2339399e022 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -18,6 +18,9 @@ file(GLOB_RECURSE sources_Threading Threading/*.cpp Threading/*.h) file(GLOB_RECURSE sources_Utilities Utilities/*.cpp Utilities/*.h) file(GLOB_RECURSE sources_Configuration Configuration/*.cpp Configuration/*.h) file(GLOB_RECURSE sources_Logging Logging/*.cpp Logging/*.h) +if (SERVERS) + file(GLOB_RECURSE sources_Cryptography Cryptography/*.cpp Cryptography/*.h) +endif (SERVERS) # Manually set sources for Debugging directory as we don't want to include WheatyExceptionReport in common project # It needs to be included both in authserver and worldserver for the static global variable to be properly initialized @@ -28,7 +31,7 @@ file(GLOB sources_localdir *.cpp *.h) if (USE_COREPCH) set(common_STAT_PCH_HDR PrecompiledHeaders/commonPCH.h) set(common_STAT_PCH_SRC PrecompiledHeaders/commonPCH.cpp) -endif () +endif (USE_COREPCH) set(common_STAT_SRCS ${common_STAT_SRCS} @@ -39,58 +42,37 @@ set(common_STAT_SRCS ${sources_Debugging} ${sources_Configuration} ${sources_Logging} + ${sources_Cryptography} ${sources_localdir} ) +# Do NOT add any extra include directory here, as we don't want the common +# library to depend on anything else than TC deps, and itself. +# This way we ensure that if either a PR does that without modifying this file, +# a compile error will be generated, either this file will be modified so it +# is detected more easily. +# While it is OK to include files from other libs as long as they don't require +# linkage (enums, defines...) it is discouraged to do so unless necessary, as it will pullute +# include_directories leading to further unnoticed dependency aditions +# Linker Depencency requirements: none include_directories( ${CMAKE_BINARY_DIR} - ${CMAKE_SOURCE_DIR}/dep/utf8cpp - ${CMAKE_SOURCE_DIR}/dep/SFMT - ${CMAKE_SOURCE_DIR}/dep/g3dlite/include - ${CMAKE_SOURCE_DIR}/dep/recastnavigation/Detour - ${CMAKE_SOURCE_DIR}/dep/recastnavigation/Detour/Include - ${CMAKE_SOURCE_DIR}/dep/cppformat - ${CMAKE_SOURCE_DIR}/src/server/database - ${CMAKE_SOURCE_DIR}/src/server/database/Database - ${CMAKE_SOURCE_DIR}/src/server/shared - ${CMAKE_SOURCE_DIR}/src/server/shared/Dynamic - ${CMAKE_SOURCE_DIR}/src/server/shared/Dynamic/LinkedReference - ${CMAKE_SOURCE_DIR}/src/server/shared/Packets - ${CMAKE_SOURCE_DIR}/src/server/shared/DataStores - ${CMAKE_SOURCE_DIR}/src/server/game/Addons - ${CMAKE_SOURCE_DIR}/src/server/game/Conditions - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/Item - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/GameObject - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/Creature - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/Object - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/Object/Updates - ${CMAKE_SOURCE_DIR}/src/server/game/Entities/Unit - ${CMAKE_SOURCE_DIR}/src/server/game/Combat - ${CMAKE_SOURCE_DIR}/src/server/game/Loot - ${CMAKE_SOURCE_DIR}/src/server/game/Miscellaneous - ${CMAKE_SOURCE_DIR}/src/server/game/Grids - ${CMAKE_SOURCE_DIR}/src/server/game/Grids/Cells - ${CMAKE_SOURCE_DIR}/src/server/game/Grids/Notifiers - ${CMAKE_SOURCE_DIR}/src/server/game/Maps - ${CMAKE_SOURCE_DIR}/src/server/game/DataStores - ${CMAKE_SOURCE_DIR}/src/server/game/Movement/Waypoints - ${CMAKE_SOURCE_DIR}/src/server/game/Movement/Spline - ${CMAKE_SOURCE_DIR}/src/server/game/Movement - ${CMAKE_SOURCE_DIR}/src/server/game/Server - ${CMAKE_SOURCE_DIR}/src/server/game/Server/Protocol - ${CMAKE_SOURCE_DIR}/src/server/game/World - ${CMAKE_SOURCE_DIR}/src/server/game/Spells - ${CMAKE_SOURCE_DIR}/src/server/game/Spells/Auras ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/Threading - ${CMAKE_CURRENT_SOURCE_DIR}/Debugging ${CMAKE_CURRENT_SOURCE_DIR}/Collision ${CMAKE_CURRENT_SOURCE_DIR}/Collision/Management ${CMAKE_CURRENT_SOURCE_DIR}/Collision/Maps ${CMAKE_CURRENT_SOURCE_DIR}/Collision/Models - ${CMAKE_CURRENT_SOURCE_DIR}/Utilities ${CMAKE_CURRENT_SOURCE_DIR}/Configuration + ${CMAKE_CURRENT_SOURCE_DIR}/Cryptography + ${CMAKE_CURRENT_SOURCE_DIR}/Debugging ${CMAKE_CURRENT_SOURCE_DIR}/Logging + ${CMAKE_CURRENT_SOURCE_DIR}/Utilities + ${CMAKE_SOURCE_DIR}/dep/cppformat + ${CMAKE_SOURCE_DIR}/dep/g3dlite/include + ${CMAKE_SOURCE_DIR}/dep/recastnavigation/Detour/Include + ${CMAKE_SOURCE_DIR}/dep/SFMT + ${CMAKE_SOURCE_DIR}/dep/utf8cpp + ${OPENSSL_INCLUDE_DIR} ${VALGRIND_INCLUDE_DIR} ) diff --git a/src/common/Collision/Management/MMapManager.cpp b/src/common/Collision/Management/MMapManager.cpp index c93d4e0e751..a5685c65d9c 100644 --- a/src/common/Collision/Management/MMapManager.cpp +++ b/src/common/Collision/Management/MMapManager.cpp @@ -19,7 +19,7 @@ #include "MMapManager.h" #include "Log.h" #include "Config.h" -#include "SharedDefines.h" +#include "MapDefines.h" namespace MMAP { diff --git a/src/common/Collision/Maps/MapDefines.h b/src/common/Collision/Maps/MapDefines.h new file mode 100644 index 00000000000..538a78a38d4 --- /dev/null +++ b/src/common/Collision/Maps/MapDefines.h @@ -0,0 +1,36 @@ +#ifndef _MAPDEFINES_H +#define _MAPDEFINES_H + +#include "Define.h" +#include "DetourNavMesh.h" + +const uint32 MMAP_MAGIC = 0x4d4d4150; // 'MMAP' +#define MMAP_VERSION 5 + +struct MmapTileHeader +{ + uint32 mmapMagic; + uint32 dtVersion; + uint32 mmapVersion; + uint32 size; + bool usesLiquids : 1; + + MmapTileHeader() : mmapMagic(MMAP_MAGIC), dtVersion(DT_NAVMESH_VERSION), + mmapVersion(MMAP_VERSION), size(0), usesLiquids(true) { } +}; + +enum NavTerrain +{ + NAV_EMPTY = 0x00, + NAV_GROUND = 0x01, + NAV_MAGMA = 0x02, + NAV_SLIME = 0x04, + NAV_WATER = 0x08, + NAV_UNUSED1 = 0x10, + NAV_UNUSED2 = 0x20, + NAV_UNUSED3 = 0x40, + NAV_UNUSED4 = 0x80 + // we only have 8 bits +}; + +#endif diff --git a/src/common/Cryptography/ARC4.cpp b/src/common/Cryptography/ARC4.cpp new file mode 100644 index 00000000000..d1082b39347 --- /dev/null +++ b/src/common/Cryptography/ARC4.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ARC4.h" + +ARC4::ARC4(uint8 len) : m_ctx() +{ + EVP_CIPHER_CTX_init(&m_ctx); + EVP_EncryptInit_ex(&m_ctx, EVP_rc4(), NULL, NULL, NULL); + EVP_CIPHER_CTX_set_key_length(&m_ctx, len); +} + +ARC4::ARC4(uint8 *seed, uint8 len) : m_ctx() +{ + EVP_CIPHER_CTX_init(&m_ctx); + EVP_EncryptInit_ex(&m_ctx, EVP_rc4(), NULL, NULL, NULL); + EVP_CIPHER_CTX_set_key_length(&m_ctx, len); + EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL); +} + +ARC4::~ARC4() +{ + EVP_CIPHER_CTX_cleanup(&m_ctx); +} + +void ARC4::Init(uint8 *seed) +{ + EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL); +} + +void ARC4::UpdateData(int len, uint8 *data) +{ + int outlen = 0; + EVP_EncryptUpdate(&m_ctx, data, &outlen, data, len); + EVP_EncryptFinal_ex(&m_ctx, data, &outlen); +} diff --git a/src/common/Cryptography/ARC4.h b/src/common/Cryptography/ARC4.h new file mode 100644 index 00000000000..16a0cb92eb9 --- /dev/null +++ b/src/common/Cryptography/ARC4.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _AUTH_SARC4_H +#define _AUTH_SARC4_H + +#include <openssl/evp.h> +#include "Define.h" + +class ARC4 +{ + public: + ARC4(uint8 len); + ARC4(uint8 *seed, uint8 len); + ~ARC4(); + void Init(uint8 *seed); + void UpdateData(int len, uint8 *data); + private: + EVP_CIPHER_CTX m_ctx; +}; + +#endif diff --git a/src/common/Cryptography/Authentication/AuthCrypt.cpp b/src/common/Cryptography/Authentication/AuthCrypt.cpp new file mode 100644 index 00000000000..153726e6950 --- /dev/null +++ b/src/common/Cryptography/Authentication/AuthCrypt.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "AuthCrypt.h" +#include "Cryptography/HMACSHA1.h" +#include "Cryptography/BigNumber.h" + +#include <cstring> + +AuthCrypt::AuthCrypt() : + _clientDecrypt(SHA_DIGEST_LENGTH), _serverEncrypt(SHA_DIGEST_LENGTH), + _initialized(false) +{ } + +void AuthCrypt::Init(BigNumber* K) +{ + uint8 ServerEncryptionKey[SEED_KEY_SIZE] = { 0xCC, 0x98, 0xAE, 0x04, 0xE8, 0x97, 0xEA, 0xCA, 0x12, 0xDD, 0xC0, 0x93, 0x42, 0x91, 0x53, 0x57 }; + HmacHash serverEncryptHmac(SEED_KEY_SIZE, (uint8*)ServerEncryptionKey); + uint8 *encryptHash = serverEncryptHmac.ComputeHash(K); + + uint8 ServerDecryptionKey[SEED_KEY_SIZE] = { 0xC2, 0xB3, 0x72, 0x3C, 0xC6, 0xAE, 0xD9, 0xB5, 0x34, 0x3C, 0x53, 0xEE, 0x2F, 0x43, 0x67, 0xCE }; + HmacHash clientDecryptHmac(SEED_KEY_SIZE, (uint8*)ServerDecryptionKey); + uint8 *decryptHash = clientDecryptHmac.ComputeHash(K); + + //ARC4 _serverDecrypt(encryptHash); + _clientDecrypt.Init(decryptHash); + _serverEncrypt.Init(encryptHash); + //ARC4 _clientEncrypt(decryptHash); + + // Drop first 1024 bytes, as WoW uses ARC4-drop1024. + uint8 syncBuf[1024]; + memset(syncBuf, 0, 1024); + + _serverEncrypt.UpdateData(1024, syncBuf); + //_clientEncrypt.UpdateData(1024, syncBuf); + + memset(syncBuf, 0, 1024); + + //_serverDecrypt.UpdateData(1024, syncBuf); + _clientDecrypt.UpdateData(1024, syncBuf); + + _initialized = true; +} + +void AuthCrypt::DecryptRecv(uint8 *data, size_t len) +{ + if (!_initialized) + return; + + _clientDecrypt.UpdateData(len, data); +} + +void AuthCrypt::EncryptSend(uint8 *data, size_t len) +{ + if (!_initialized) + return; + + _serverEncrypt.UpdateData(len, data); +} + diff --git a/src/common/Cryptography/Authentication/AuthCrypt.h b/src/common/Cryptography/Authentication/AuthCrypt.h new file mode 100644 index 00000000000..b8913c95a2c --- /dev/null +++ b/src/common/Cryptography/Authentication/AuthCrypt.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _AUTHCRYPT_H +#define _AUTHCRYPT_H + +#include "Cryptography/ARC4.h" + +class BigNumber; + +class AuthCrypt +{ + public: + AuthCrypt(); + + void Init(BigNumber* K); + void DecryptRecv(uint8 *, size_t); + void EncryptSend(uint8 *, size_t); + + bool IsInitialized() const { return _initialized; } + + private: + ARC4 _clientDecrypt; + ARC4 _serverEncrypt; + bool _initialized; +}; +#endif diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp new file mode 100644 index 00000000000..720e8e30441 --- /dev/null +++ b/src/common/Cryptography/BigNumber.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "Cryptography/BigNumber.h" +#include <openssl/bn.h> +#include <cstring> +#include <algorithm> +#include <memory> + +BigNumber::BigNumber() + : _bn(BN_new()) +{ } + +BigNumber::BigNumber(BigNumber const& bn) + : _bn(BN_dup(bn._bn)) +{ } + +BigNumber::BigNumber(uint32 val) + : _bn(BN_new()) +{ + BN_set_word(_bn, val); +} + +BigNumber::~BigNumber() +{ + BN_free(_bn); +} + +void BigNumber::SetDword(uint32 val) +{ + BN_set_word(_bn, val); +} + +void BigNumber::SetQword(uint64 val) +{ + BN_set_word(_bn, (uint32)(val >> 32)); + BN_lshift(_bn, _bn, 32); + BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF)); +} + +void BigNumber::SetBinary(uint8 const* bytes, int32 len) +{ + uint8* array = new uint8[len]; + + for (int i = 0; i < len; i++) + array[i] = bytes[len - 1 - i]; + + BN_bin2bn(array, len, _bn); + + delete[] array; +} + +void BigNumber::SetHexStr(char const* str) +{ + BN_hex2bn(&_bn, str); +} + +void BigNumber::SetRand(int32 numbits) +{ + BN_rand(_bn, numbits, 0, 1); +} + +BigNumber& BigNumber::operator=(BigNumber const& bn) +{ + if (this == &bn) + return *this; + + BN_copy(_bn, bn._bn); + return *this; +} + +BigNumber BigNumber::operator+=(BigNumber const& bn) +{ + BN_add(_bn, _bn, bn._bn); + return *this; +} + +BigNumber BigNumber::operator-=(BigNumber const& bn) +{ + BN_sub(_bn, _bn, bn._bn); + return *this; +} + +BigNumber BigNumber::operator*=(BigNumber const& bn) +{ + BN_CTX *bnctx; + + bnctx = BN_CTX_new(); + BN_mul(_bn, _bn, bn._bn, bnctx); + BN_CTX_free(bnctx); + + return *this; +} + +BigNumber BigNumber::operator/=(BigNumber const& bn) +{ + BN_CTX *bnctx; + + bnctx = BN_CTX_new(); + BN_div(_bn, NULL, _bn, bn._bn, bnctx); + BN_CTX_free(bnctx); + + return *this; +} + +BigNumber BigNumber::operator%=(BigNumber const& bn) +{ + BN_CTX *bnctx; + + bnctx = BN_CTX_new(); + BN_mod(_bn, _bn, bn._bn, bnctx); + BN_CTX_free(bnctx); + + return *this; +} + +BigNumber BigNumber::Exp(BigNumber const& bn) +{ + BigNumber ret; + BN_CTX *bnctx; + + bnctx = BN_CTX_new(); + BN_exp(ret._bn, _bn, bn._bn, bnctx); + BN_CTX_free(bnctx); + + return ret; +} + +BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) +{ + BigNumber ret; + BN_CTX *bnctx; + + bnctx = BN_CTX_new(); + BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx); + BN_CTX_free(bnctx); + + return ret; +} + +int32 BigNumber::GetNumBytes(void) +{ + return BN_num_bytes(_bn); +} + +uint32 BigNumber::AsDword() +{ + return (uint32)BN_get_word(_bn); +} + +bool BigNumber::isZero() const +{ + return BN_is_zero(_bn); +} + +std::unique_ptr<uint8[]> BigNumber::AsByteArray(int32 minSize, bool littleEndian) +{ + 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 > numBytes) + memset((void*)array, 0, length); + + 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 + numBytes); + + std::unique_ptr<uint8[]> ret(array); + return ret; +} + +char * BigNumber::AsHexStr() const +{ + return BN_bn2hex(_bn); +} + +char * BigNumber::AsDecStr() const +{ + return BN_bn2dec(_bn); +} + diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h new file mode 100644 index 00000000000..e6a056b5baa --- /dev/null +++ b/src/common/Cryptography/BigNumber.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _AUTH_BIGNUMBER_H +#define _AUTH_BIGNUMBER_H + +#include <memory> +#include "Define.h" + +struct bignum_st; + +class BigNumber +{ + public: + BigNumber(); + BigNumber(BigNumber const& bn); + BigNumber(uint32); + ~BigNumber(); + + void SetDword(uint32); + void SetQword(uint64); + void SetBinary(uint8 const* bytes, int32 len); + void SetHexStr(char const* str); + + void SetRand(int32 numbits); + + BigNumber& operator=(BigNumber const& bn); + + BigNumber operator+=(BigNumber const& bn); + BigNumber operator+(BigNumber const& bn) + { + BigNumber t(*this); + return t += bn; + } + + BigNumber operator-=(BigNumber const& bn); + BigNumber operator-(BigNumber const& bn) + { + BigNumber t(*this); + return t -= bn; + } + + BigNumber operator*=(BigNumber const& bn); + BigNumber operator*(BigNumber const& bn) + { + BigNumber t(*this); + return t *= bn; + } + + BigNumber operator/=(BigNumber const& bn); + BigNumber operator/(BigNumber const& bn) + { + BigNumber t(*this); + return t /= bn; + } + + BigNumber operator%=(BigNumber const& bn); + BigNumber operator%(BigNumber const& bn) + { + BigNumber t(*this); + return t %= bn; + } + + bool isZero() const; + + BigNumber ModExp(BigNumber const& bn1, BigNumber const& bn2); + BigNumber Exp(BigNumber const&); + + int32 GetNumBytes(void); + + struct bignum_st *BN() { return _bn; } + + uint32 AsDword(); + + std::unique_ptr<uint8[]> AsByteArray(int32 minSize = 0, bool littleEndian = true); + + char * AsHexStr() const; + char * AsDecStr() const; + + private: + struct bignum_st *_bn; + +}; +#endif + diff --git a/src/common/Cryptography/HMACSHA1.cpp b/src/common/Cryptography/HMACSHA1.cpp new file mode 100644 index 00000000000..304633cbc03 --- /dev/null +++ b/src/common/Cryptography/HMACSHA1.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "HMACSHA1.h" +#include "BigNumber.h" +#include "Common.h" + +HmacHash::HmacHash(uint32 len, uint8 *seed) +{ + HMAC_CTX_init(&m_ctx); + HMAC_Init_ex(&m_ctx, seed, len, EVP_sha1(), NULL); + memset(m_digest, 0, sizeof(m_digest)); +} + +HmacHash::~HmacHash() +{ + HMAC_CTX_cleanup(&m_ctx); +} + +void HmacHash::UpdateData(const std::string &str) +{ + HMAC_Update(&m_ctx, (uint8 const*)str.c_str(), str.length()); +} + +void HmacHash::UpdateData(const uint8* data, size_t len) +{ + HMAC_Update(&m_ctx, data, len); +} + +void HmacHash::Finalize() +{ + uint32 length = 0; + HMAC_Final(&m_ctx, (uint8*)m_digest, &length); + ASSERT(length == SHA_DIGEST_LENGTH); +} + +uint8 *HmacHash::ComputeHash(BigNumber* bn) +{ + HMAC_Update(&m_ctx, bn->AsByteArray().get(), bn->GetNumBytes()); + Finalize(); + return (uint8*)m_digest; +} diff --git a/src/common/Cryptography/HMACSHA1.h b/src/common/Cryptography/HMACSHA1.h new file mode 100644 index 00000000000..66cf214d9b1 --- /dev/null +++ b/src/common/Cryptography/HMACSHA1.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _AUTH_HMAC_H +#define _AUTH_HMAC_H + +#include "Define.h" +#include <string> +#include <openssl/hmac.h> +#include <openssl/sha.h> + +class BigNumber; + +#define SEED_KEY_SIZE 16 + +class HmacHash +{ + public: + HmacHash(uint32 len, uint8 *seed); + ~HmacHash(); + void UpdateData(const std::string &str); + void UpdateData(const uint8* data, size_t len); + void Finalize(); + uint8 *ComputeHash(BigNumber* bn); + uint8 *GetDigest() { return (uint8*)m_digest; } + int GetLength() const { return SHA_DIGEST_LENGTH; } + private: + HMAC_CTX m_ctx; + uint8 m_digest[SHA_DIGEST_LENGTH]; +}; +#endif + diff --git a/src/common/Cryptography/OpenSSLCrypto.cpp b/src/common/Cryptography/OpenSSLCrypto.cpp new file mode 100644 index 00000000000..f122888292f --- /dev/null +++ b/src/common/Cryptography/OpenSSLCrypto.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <OpenSSLCrypto.h> +#include <openssl/crypto.h> +#include <vector> +#include <thread> +#include <mutex> + +std::vector<std::mutex*> cryptoLocks; + +static void lockingCallback(int mode, int type, const char* /*file*/, int /*line*/) +{ + if (mode & CRYPTO_LOCK) + cryptoLocks[type]->lock(); + else + cryptoLocks[type]->unlock(); +} + +static void threadIdCallback(CRYPTO_THREADID * id) +{ + 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 std::mutex; + } + CRYPTO_THREADID_set_callback(threadIdCallback); + CRYPTO_set_locking_callback(lockingCallback); +} + +void OpenSSLCrypto::threadsCleanup() +{ + CRYPTO_set_locking_callback(NULL); + CRYPTO_THREADID_set_callback(NULL); + for(int i = 0 ; i < CRYPTO_num_locks(); ++i) + { + delete cryptoLocks[i]; + } + cryptoLocks.resize(0); +} diff --git a/src/common/Cryptography/OpenSSLCrypto.h b/src/common/Cryptography/OpenSSLCrypto.h new file mode 100644 index 00000000000..0daa20c4780 --- /dev/null +++ b/src/common/Cryptography/OpenSSLCrypto.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef OPENSSL_CRYPTO_H +#define OPENSSL_CRYPTO_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 +{ + /// Needs to be called before threads using openssl are spawned + void threadsSetup(); + /// Needs to be called after threads using openssl are despawned + void threadsCleanup(); +} + +#endif
\ No newline at end of file diff --git a/src/common/Cryptography/SHA1.cpp b/src/common/Cryptography/SHA1.cpp new file mode 100644 index 00000000000..bd7101075de --- /dev/null +++ b/src/common/Cryptography/SHA1.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "SHA1.h" +#include "BigNumber.h" +#include <cstring> +#include <stdarg.h> + +SHA1Hash::SHA1Hash() +{ + SHA1_Init(&mC); + memset(mDigest, 0, SHA_DIGEST_LENGTH * sizeof(uint8)); +} + +SHA1Hash::~SHA1Hash() +{ + SHA1_Init(&mC); +} + +void SHA1Hash::UpdateData(const uint8 *dta, int len) +{ + SHA1_Update(&mC, dta, len); +} + +void SHA1Hash::UpdateData(const std::string &str) +{ + UpdateData((uint8 const*)str.c_str(), str.length()); +} + +void SHA1Hash::UpdateBigNumbers(BigNumber* bn0, ...) +{ + va_list v; + BigNumber* bn; + + va_start(v, bn0); + bn = bn0; + while (bn) + { + UpdateData(bn->AsByteArray().get(), bn->GetNumBytes()); + bn = va_arg(v, BigNumber*); + } + va_end(v); +} + +void SHA1Hash::Initialize() +{ + SHA1_Init(&mC); +} + +void SHA1Hash::Finalize(void) +{ + SHA1_Final(mDigest, &mC); +} + diff --git a/src/common/Cryptography/SHA1.h b/src/common/Cryptography/SHA1.h new file mode 100644 index 00000000000..f59bdc25556 --- /dev/null +++ b/src/common/Cryptography/SHA1.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _AUTH_SHA1_H +#define _AUTH_SHA1_H + +#include "Define.h" +#include <string> +#include <openssl/sha.h> + +class BigNumber; + +class SHA1Hash +{ + public: + SHA1Hash(); + ~SHA1Hash(); + + void UpdateBigNumbers(BigNumber* bn0, ...); + + void UpdateData(const uint8 *dta, int len); + void UpdateData(const std::string &str); + + void Initialize(); + void Finalize(); + + uint8 *GetDigest(void) { return mDigest; } + int GetLength(void) const { return SHA_DIGEST_LENGTH; } + + private: + SHA_CTX mC; + uint8 mDigest[SHA_DIGEST_LENGTH]; +}; +#endif + diff --git a/src/common/Cryptography/WardenKeyGeneration.h b/src/common/Cryptography/WardenKeyGeneration.h new file mode 100644 index 00000000000..bfa0337d347 --- /dev/null +++ b/src/common/Cryptography/WardenKeyGeneration.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "SHA1.h" + +#include <cstring> + +#ifndef _WARDEN_KEY_GENERATION_H +#define _WARDEN_KEY_GENERATION_H + +class SHA1Randx +{ +public: + SHA1Randx(uint8* buff, uint32 size) + { + uint32 halfSize = size / 2; + + sh.Initialize(); + sh.UpdateData(buff, halfSize); + sh.Finalize(); + + memcpy(o1, sh.GetDigest(), 20); + + sh.Initialize(); + sh.UpdateData(buff + halfSize, size - halfSize); + sh.Finalize(); + + memcpy(o2, sh.GetDigest(), 20); + + memset(o0, 0x00, 20); + + FillUp(); + } + + void Generate(uint8* buf, uint32 sz) + { + for (uint32 i = 0; i < sz; ++i) + { + if (taken == 20) + FillUp(); + + buf[i] = o0[taken]; + taken++; + } + } + +private: + void FillUp() + { + sh.Initialize(); + sh.UpdateData(o1, 20); + sh.UpdateData(o0, 20); + sh.UpdateData(o2, 20); + sh.Finalize(); + + memcpy(o0, sh.GetDigest(), 20); + + taken = 0; + } + + SHA1Hash sh; + uint32 taken; + uint8 o0[20], o1[20], o2[20]; +}; + +#endif diff --git a/src/common/GitRevision.cpp b/src/common/GitRevision.cpp new file mode 100644 index 00000000000..d0719c09959 --- /dev/null +++ b/src/common/GitRevision.cpp @@ -0,0 +1,78 @@ +#include "GitRevision.h" +#include "CompilerDefs.h" +#include "revision_data.h" + +char const* GitRevision::GetHash() +{ + return _HASH; +} + +char const* GitRevision::GetDate() +{ + return _DATE; +} + +char const* GitRevision::GetBranch() +{ + return _BRANCH; +} + +char const* GitRevision::GetSourceDirectory() +{ + return _SOURCE_DIRECTORY; +} + +char const* GitRevision::GetMySQLExecutable() +{ + return _MYSQL_EXECUTABLE; +} + +char const* GitRevision::GetFullDatabase() +{ + return _FULL_DATABASE; +} + +#define _PACKAGENAME "TrinityCore" + +char const* GitRevision::GetFullVersion() +{ +#if PLATFORM == PLATFORM_WINDOWS +# ifdef _WIN64 + return _PACKAGENAME " rev. " VER_PRODUCTVERSION_STR " (Win64, " _BUILD_DIRECTIVE ")"; +# else + return _PACKAGENAME " rev. " VER_PRODUCTVERSION_STR " (Win32, " _BUILD_DIRECTIVE ")"; +# endif +#else + return _PACKAGENAME " rev. " VER_PRODUCTVERSION_STR " (Unix, " _BUILD_DIRECTIVE ")"; +#endif +} + +char const* GitRevision::GetCompanyNameStr() +{ + return VER_COMPANYNAME_STR; +} + +char const* GitRevision::GetLegalCopyrightStr() +{ + return VER_LEGALCOPYRIGHT_STR; +} + +char const* GitRevision::GetFileVersionStr() +{ + return VER_FILEVERSION_STR; +} + +char const* GitRevision::GetProductVersionStr() +{ + return VER_PRODUCTVERSION_STR; +} + +char const* GitRevision::GetCompilerCFlags() +{ + return COMPILER_C_FLAGS; +} + +char const* GitRevision::GetCompilerCXXFlags() +{ + return COMPILER_CXX_FLAGS; +} diff --git a/src/common/GitRevision.h b/src/common/GitRevision.h new file mode 100644 index 00000000000..d9b292031f6 --- /dev/null +++ b/src/common/GitRevision.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __GITREVISION_H__ +#define __GITREVISION_H__ + +#include <string> + +namespace GitRevision +{ + char const* GetHash(); + char const* GetDate(); + char const* GetBranch(); + char const* GetSourceDirectory(); + char const* GetMySQLExecutable(); + char const* GetFullDatabase(); + char const* GetFullVersion(); + char const* GetCompanyNameStr(); + char const* GetLegalCopyrightStr(); + char const* GetFileVersionStr(); + char const* GetProductVersionStr(); + char const* GetCompilerCFlags(); + char const* GetCompilerCXXFlags(); +} + +#endif |