Added Support Openssl 3.0.x

This commit is contained in:
HKevinH 2024-08-05 00:46:01 -05:00
parent 9339e072af
commit df3ff87f65
10 changed files with 935 additions and 210 deletions

File diff suppressed because it is too large Load diff

View file

@ -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"
@ -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>
@ -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);

View file

@ -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);
}

View file

@ -20,6 +20,7 @@
#include "Define.h"
#include <openssl/evp.h>
#include <array>
class ARC4
{
@ -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

View file

@ -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
}

View file

@ -15,9 +15,9 @@
* 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
@ -25,7 +25,7 @@
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();
}

View file

@ -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)
@ -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);
}

View file

@ -21,6 +21,7 @@
#include "Define.h"
#include <string>
#include <openssl/sha.h>
#include <openssl/evp.h>
class BigNumber;
@ -28,8 +29,13 @@ 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);
@ -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

View file

@ -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"
@ -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'
@ -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;
}

View file

@ -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"
@ -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);