TrinityCore
Loading...
Searching...
No Matches
OpenSSLCrypto.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "OpenSSLCrypto.h"
19#include "Errors.h"
20#include <openssl/provider.h>
21#include <cstdlib>
22
23OSSL_PROVIDER* LegacyProvider;
24
25void OpenSSLCrypto::threadsSetup([[maybe_unused]] boost::filesystem::path const& providerModulePath)
26{
27#ifdef VALGRIND
28 ValgrindRandomSetup();
29#endif
30
31#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
32 if (!std::getenv("OPENSSL_MODULES"))
33 OSSL_PROVIDER_set_default_search_path(nullptr, providerModulePath.string().c_str());
34#define OPENSSL_LEGACY_PROVIDER_FILENAME "legacy.dll"
35#else
36#define OPENSSL_LEGACY_PROVIDER_FILENAME "legacy.so"
37#endif
38 LegacyProvider = OSSL_PROVIDER_try_load(nullptr, "legacy", 1);
39 WPWarning(LegacyProvider != nullptr, "OpenSSL failed to load " OPENSSL_LEGACY_PROVIDER_FILENAME);
40}
41
43{
44 OSSL_PROVIDER_unload(LegacyProvider);
45 OSSL_PROVIDER_set_default_search_path(nullptr, nullptr);
46}
47
48#ifdef VALGRIND
49#include <openssl/rand.h>
50
51RAND_METHOD const* default_rand;
52
53static int Valgrind_RAND_seed(const void* buf, int num)
54{
55 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
56 return default_rand->seed(buf, num);
57}
58
59static int Valgrind_RAND_bytes(unsigned char* buf, int num)
60{
61 int ret = default_rand->bytes(buf, num);
62 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
63 return ret;
64}
65
66static void Valgrind_RAND_cleanup(void)
67{
68 default_rand->cleanup();
69}
70
71static int Valgrind_RAND_add(const void* buf, int num, double randomness)
72{
73 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
74 return default_rand->add(buf, num, randomness);
75}
76
77static int Valgrind_RAND_pseudorand(unsigned char* buf, int num)
78{
79 int ret = default_rand->pseudorand(buf, num);
80 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
81 return ret;
82}
83
84static int Valgrind_RAND_status(void)
85{
86 return default_rand->status();
87}
88
89static RAND_METHOD valgrind_rand;
90
91void ValgrindRandomSetup()
92{
93 memset(&valgrind_rand, 0, sizeof(RAND_METHOD));
94 default_rand = RAND_get_rand_method();
95 if (default_rand->seed)
96 valgrind_rand.seed = &Valgrind_RAND_seed;
97 if (default_rand->bytes)
98 valgrind_rand.bytes = &Valgrind_RAND_bytes;
99 if (default_rand->cleanup)
100 valgrind_rand.cleanup = &Valgrind_RAND_cleanup;
101 if (default_rand->add)
102 valgrind_rand.add = &Valgrind_RAND_add;
103 if (default_rand->pseudorand)
104 valgrind_rand.pseudorand = &Valgrind_RAND_pseudorand;
105 if (default_rand->status)
106 valgrind_rand.status = &Valgrind_RAND_status;
107 RAND_set_rand_method(&valgrind_rand);
108}
109#endif
#define WPWarning(cond, msg)
Definition Errors.h:63
OSSL_PROVIDER * LegacyProvider
#define OPENSSL_LEGACY_PROVIDER_FILENAME
TC_COMMON_API void threadsSetup(boost::filesystem::path const &providerModulePath)
Needs to be called before threads using openssl are spawned.
TC_COMMON_API void threadsCleanup()
Needs to be called after threads using openssl are despawned.