TrinityCore
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 <openssl/crypto.h>
20
21#if OPENSSL_VERSION_NUMBER >= 0x30000000L
22#include <openssl/provider.h>
23OSSL_PROVIDER* LegacyProvider;
24#endif
25
26void OpenSSLCrypto::threadsSetup([[maybe_unused]] boost::filesystem::path const& providerModulePath)
27{
28#ifdef VALGRIND
29 ValgrindRandomSetup();
30#endif
31
32#if OPENSSL_VERSION_NUMBER >= 0x30000000L
33#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
34 OSSL_PROVIDER_set_default_search_path(nullptr, providerModulePath.string().c_str());
35#endif
36 LegacyProvider = OSSL_PROVIDER_try_load(nullptr, "legacy", 1);
37#endif
38}
39
41{
42#if OPENSSL_VERSION_NUMBER >= 0x30000000L
43 OSSL_PROVIDER_unload(LegacyProvider);
44 OSSL_PROVIDER_set_default_search_path(nullptr, nullptr);
45#endif
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
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.