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 <openssl/crypto.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#endif
35 LegacyProvider = OSSL_PROVIDER_try_load(nullptr, "legacy", 1);
36}
37
39{
40 OSSL_PROVIDER_unload(LegacyProvider);
41 OSSL_PROVIDER_set_default_search_path(nullptr, nullptr);
42}
43
44#ifdef VALGRIND
45#include <openssl/rand.h>
46
47RAND_METHOD const* default_rand;
48
49static int Valgrind_RAND_seed(const void* buf, int num)
50{
51 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
52 return default_rand->seed(buf, num);
53}
54
55static int Valgrind_RAND_bytes(unsigned char* buf, int num)
56{
57 int ret = default_rand->bytes(buf, num);
58 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
59 return ret;
60}
61
62static void Valgrind_RAND_cleanup(void)
63{
64 default_rand->cleanup();
65}
66
67static int Valgrind_RAND_add(const void* buf, int num, double randomness)
68{
69 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
70 return default_rand->add(buf, num, randomness);
71}
72
73static int Valgrind_RAND_pseudorand(unsigned char* buf, int num)
74{
75 int ret = default_rand->pseudorand(buf, num);
76 VALGRIND_DISCARD(VALGRIND_MAKE_MEM_DEFINED(buf, num));
77 return ret;
78}
79
80static int Valgrind_RAND_status(void)
81{
82 return default_rand->status();
83}
84
85static RAND_METHOD valgrind_rand;
86
87void ValgrindRandomSetup()
88{
89 memset(&valgrind_rand, 0, sizeof(RAND_METHOD));
90 default_rand = RAND_get_rand_method();
91 if (default_rand->seed)
92 valgrind_rand.seed = &Valgrind_RAND_seed;
93 if (default_rand->bytes)
94 valgrind_rand.bytes = &Valgrind_RAND_bytes;
95 if (default_rand->cleanup)
96 valgrind_rand.cleanup = &Valgrind_RAND_cleanup;
97 if (default_rand->add)
98 valgrind_rand.add = &Valgrind_RAND_add;
99 if (default_rand->pseudorand)
100 valgrind_rand.pseudorand = &Valgrind_RAND_pseudorand;
101 if (default_rand->status)
102 valgrind_rand.status = &Valgrind_RAND_status;
103 RAND_set_rand_method(&valgrind_rand);
104}
105#endif
OSSL_PROVIDER * LegacyProvider
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.