Skip to content

Commit

Permalink
Added Support Openssl 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
HKevinH committed Aug 5, 2024
1 parent 9339e07 commit df3ff87
Show file tree
Hide file tree
Showing 10 changed files with 931 additions and 206 deletions.
954 changes: 813 additions & 141 deletions cmake/macros/FindOpenSSL.cmake

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/server/authserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include <ace/Sig_Handler.h>
#include <openssl/opensslv.h>
#include <openssl/crypto.h>

#include "OpenSSLCrypto.h"
#include "Common.h"
#include <boost/dll/runtime_symbol_info.hpp>
#include "Database/DatabaseEnv.h"
#include "Configuration/Config.h"
#include "Log.h"
Expand All @@ -40,6 +41,9 @@
#include "RealmList.h"
#include "RealmAcceptor.h"
#include "AppenderDB.h"
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
#include <openssl/provider.h>
#endif

#ifdef __linux__
#include <sched.h>
Expand Down Expand Up @@ -150,7 +154,7 @@ extern int main(int argc, char** argv)

TC_LOG_INFO("server.authserver", "Using configuration file %s.", configFile);

TC_LOG_WARN("server.authserver", "%s (Library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
TC_LOG_WARN("server.authserver", "%s (Library: %s)", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));

#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
ACE_Reactor::instance(new ACE_Reactor(new ACE_Dev_Poll_Reactor(ACE::max_handles(), 1), 1), true);
Expand Down
42 changes: 29 additions & 13 deletions src/server/shared/Cryptography/ARC4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,51 @@

#include "ARC4.h"
#include <openssl/sha.h>
#include "Errors.h"

ARC4::ARC4(uint8 len)
ARC4::ARC4(uint8 len) : _ctx(EVP_CIPHER_CTX_new())
{
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);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
_cipher = EVP_CIPHER_fetch(nullptr, "RC4", nullptr);
#else
EVP_CIPHER const* _cipher = EVP_rc4();
#endif

EVP_CIPHER_CTX_init(_ctx);
EVP_EncryptInit_ex(_ctx, EVP_rc4(), nullptr, nullptr, nullptr);
EVP_CIPHER_CTX_set_key_length(_ctx, len);
}

ARC4::ARC4(uint8 const* seed, size_t len)
ARC4::ARC4(uint8 const* seed, size_t len) : _ctx(EVP_CIPHER_CTX_new())
{
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);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
_cipher = EVP_CIPHER_fetch(nullptr, "RC4", nullptr);
#else
EVP_CIPHER const* _cipher = EVP_rc4();
#endif

EVP_CIPHER_CTX_init(_ctx);
EVP_EncryptInit_ex(_ctx, EVP_rc4(), nullptr, nullptr, nullptr);
EVP_CIPHER_CTX_set_key_length(_ctx, len);
EVP_EncryptInit_ex(_ctx, nullptr, nullptr, seed, nullptr);
}

ARC4::~ARC4()
{
EVP_CIPHER_CTX_cleanup(m_ctx);
EVP_CIPHER_CTX_free(_ctx);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_CIPHER_free(_cipher);
#endif
}

void ARC4::Init(uint8 const* seed)
{
EVP_EncryptInit_ex(m_ctx, NULL, NULL, seed, NULL);
EVP_EncryptInit_ex(_ctx, nullptr, nullptr, seed, nullptr);
}

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);
EVP_EncryptUpdate(_ctx, data, &outlen, data, len);
EVP_EncryptFinal_ex(_ctx, data, &outlen);
}
6 changes: 5 additions & 1 deletion src/server/shared/Cryptography/ARC4.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Define.h"
#include <openssl/evp.h>
#include <array>

class ARC4
{
Expand All @@ -30,7 +31,10 @@ class ARC4
void Init(uint8 const* seed);
void UpdateData(int len, uint8 *data);
private:
EVP_CIPHER_CTX * m_ctx = EVP_CIPHER_CTX_new();
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_CIPHER* _cipher;
#endif
EVP_CIPHER_CTX* _ctx;
};

#endif
49 changes: 19 additions & 30 deletions src/server/shared/Cryptography/OpenSSLCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,29 @@
#include <ace/Thread_Mutex.h>
#include <vector>
#include <ace/Thread.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/provider.h>
OSSL_PROVIDER* LegacyProvider;
OSSL_PROVIDER* DefaultProvider;
#endif

std::vector<ACE_Thread_Mutex*> cryptoLocks;

static void lockingCallback(int mode, int type, const char* /*file*/, int /*line*/)
void OpenSSLCrypto::threadsSetup([[maybe_unused]] boost::filesystem::path const& providerModulePath)
{
if (mode & CRYPTO_LOCK)
cryptoLocks[type]->acquire();
else
cryptoLocks[type]->release();
}

static void threadIdCallback(CRYPTO_THREADID * id)
{
CRYPTO_THREADID_set_numeric(id, ACE_Thread::self());
}

void OpenSSLCrypto::threadsSetup()
{
cryptoLocks.resize(CRYPTO_num_locks());
for(int i = 0 ; i < CRYPTO_num_locks(); ++i)
{
cryptoLocks[i] = new ACE_Thread_Mutex();
}
CRYPTO_THREADID_set_callback(threadIdCallback);
CRYPTO_set_locking_callback(lockingCallback);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#if PLATFORM == PLATFORM_WINDOWS
OSSL_PROVIDER_set_default_search_path(nullptr, providerModulePath.string().c_str());
#endif
LegacyProvider = OSSL_PROVIDER_load(nullptr, "legacy");
DefaultProvider = OSSL_PROVIDER_load(nullptr, "default");
#endif
}

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);
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OSSL_PROVIDER_unload(LegacyProvider);
OSSL_PROVIDER_unload(DefaultProvider);
OSSL_PROVIDER_set_default_search_path(nullptr, nullptr);
#endif
}
8 changes: 4 additions & 4 deletions src/server/shared/Cryptography/OpenSSLCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OPENSSL_CRYPTO_H
#define OPENSSL_CRYPTO_H

#ifndef PD_OPENSSL_CRYPTO_H
#define PD_OPENSSL_CRYPTO_H
#include <boost/filesystem/path.hpp>
/**
* 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();
void threadsSetup(boost::filesystem::path const& providerModulePath);
/// Needs to be called after threads using openssl are despawned
void threadsCleanup();
}
Expand Down
36 changes: 30 additions & 6 deletions src/server/shared/Cryptography/SHA1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,41 @@

SHA1Hash::SHA1Hash()
{
SHA1_Init(&mC);
memset(mDigest, 0, SHA_DIGEST_LENGTH * sizeof(uint8));
m_ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(m_ctx, EVP_sha1(), nullptr);
}

SHA1Hash::SHA1Hash(const SHA1Hash& other) : SHA1Hash() // copy
{
EVP_MD_CTX_copy_ex(m_ctx, other.m_ctx);
std::memcpy(m_digest, other.m_digest, SHA_DIGEST_LENGTH);
}

SHA1Hash::SHA1Hash(SHA1Hash&& other) : SHA1Hash() // move
{
Swap(other);
}

SHA1Hash& SHA1Hash::operator=(SHA1Hash other) // assign
{
Swap(other);
return *this;
}

SHA1Hash::~SHA1Hash()
{
SHA1_Init(&mC);
EVP_MD_CTX_free(m_ctx);
}

void SHA1Hash::Swap(SHA1Hash& other) throw()
{
std::swap(m_ctx, other.m_ctx);
std::swap(m_digest, other.m_digest);
}

void SHA1Hash::UpdateData(const uint8 *dta, int len)
{
SHA1_Update(&mC, dta, len);
EVP_DigestUpdate(m_ctx, dta, len);
}

void SHA1Hash::UpdateData(const std::string &str)
Expand All @@ -57,11 +80,12 @@ void SHA1Hash::UpdateBigNumbers(BigNumber* bn0, ...)

void SHA1Hash::Initialize()
{
SHA1_Init(&mC);
EVP_DigestInit(m_ctx, EVP_sha1());
}

void SHA1Hash::Finalize(void)
{
SHA1_Final(mDigest, &mC);
uint32 length = SHA_DIGEST_LENGTH;
EVP_DigestFinal_ex(m_ctx, m_digest, &length);
}

14 changes: 10 additions & 4 deletions src/server/shared/Cryptography/SHA1.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
#include "Define.h"
#include <string>
#include <openssl/sha.h>
#include <openssl/evp.h>

class BigNumber;

class SHA1Hash
{
public:
SHA1Hash();
SHA1Hash(SHA1Hash const& other); // copy
SHA1Hash(SHA1Hash&& other); // move
SHA1Hash& operator=(SHA1Hash other); // assign
~SHA1Hash();

void Swap(SHA1Hash& other) throw();
friend void Swap(SHA1Hash& left, SHA1Hash& right) { left.Swap(right); }
void UpdateBigNumbers(BigNumber* bn0, ...);

void UpdateData(const uint8 *dta, int len);
Expand All @@ -38,12 +44,12 @@ class SHA1Hash
void Initialize();
void Finalize();

uint8 *GetDigest(void) { return mDigest; };
int GetLength(void) const { return SHA_DIGEST_LENGTH; };
uint8* GetDigest(void) { return m_digest; }
int GetLength() const { return SHA_DIGEST_LENGTH; }

private:
SHA_CTX mC;
uint8 mDigest[SHA_DIGEST_LENGTH];
EVP_MD_CTX* m_ctx;
uint8 m_digest[SHA_DIGEST_LENGTH];
};
#endif

11 changes: 7 additions & 4 deletions src/server/worldserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@

#include <openssl/opensslv.h>
#include <openssl/crypto.h>
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
#include <openssl/provider.h>
#endif

#include <boost/dll/runtime_symbol_info.hpp>
#include <ace/Version.h>

#include "Common.h"
#include "Database/DatabaseEnv.h"
#include "Configuration/Config.h"


#include "Log.h"
#include "Master.h"
#include "World.h"
Expand Down Expand Up @@ -139,7 +145,7 @@ extern int main(int argc, char** argv)

TC_LOG_INFO("server.worldserver", "Using configuration file %s.", cfg_file);

TC_LOG_INFO("server.worldserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
TC_LOG_INFO("server.worldserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));
TC_LOG_INFO("server.worldserver", "Using ACE version: %s", ACE_VERSION);

///- and run the 'Master'
Expand All @@ -151,9 +157,6 @@ extern int main(int argc, char** argv)
// 1 - shutdown at error
// 2 - restart command used, this code can be used by restarter for restart Trinityd


std::this_thread::sleep_for(std::chrono::seconds(20));

return ret;
}

Expand Down
9 changes: 8 additions & 1 deletion src/server/worldserver/Master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#include "Database/DatabaseEnv.h"
#include "Database/DatabaseWorkerPool.h"

#include <openssl/crypto.h>
#include <openssl/opensslv.h>
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
#include <openssl/provider.h>
#endif
#include <boost/dll/runtime_symbol_info.hpp>

#include "CliRunnable.h"
#include "Log.h"
#include "Master.h"
Expand Down Expand Up @@ -161,7 +168,7 @@ void RunAuthserverIfNeed()
/// Main function
int Master::Run()
{
OpenSSLCrypto::threadsSetup();
OpenSSLCrypto::threadsSetup(boost::dll::program_location().remove_filename());
BigNumber seed1;
seed1.SetRand(16 * 8);

Expand Down

0 comments on commit df3ff87

Please sign in to comment.