Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
OpenSSLProviders.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "OpenSSLProviders.h"
7
8#include "Define.h"
9#include "Log.h"
10
11#include <openssl/provider.h>
12
13#if PLATFORM == PLATFORM_WINDOWS
14#include <windows.h>
15
16#include <cstring>
17#include <string>
18#endif
19
20namespace
21{
22 bool IsProviderAvailable(OSSL_PROVIDER* provider, char const* providerName)
23 {
24 return provider != NULL && OSSL_PROVIDER_available(NULL, providerName);
25 }
26
27#if PLATFORM == PLATFORM_WINDOWS
28 std::string GetExecutableDirectory()
29 {
30 char path[SKYFIRE_PATH_MAX];
31 DWORD length = GetModuleFileNameA(NULL, path, SKYFIRE_PATH_MAX);
32
33 if (length == 0 || length >= SKYFIRE_PATH_MAX)
34 return std::string();
35
36 char* lastSlash = strrchr(path, '\\');
37 if (lastSlash == NULL)
38 return std::string();
39
40 *lastSlash = '\0';
41 return path;
42 }
43
44 bool SetProviderSearchPathToExecutableDirectory(char const* logFilter)
45 {
46 std::string const executableDirectory = GetExecutableDirectory();
47 if (executableDirectory.empty())
48 return false;
49
50 if (OSSL_PROVIDER_set_default_search_path(NULL, executableDirectory.c_str()) != 1)
51 return false;
52
53 SF_LOG_INFO(logFilter, "OpenSSL provider search path set to %s.", executableDirectory.c_str());
54 return true;
55 }
56#endif
57}
58
59bool Skyfire::LoadOpenSSLProviders(char const* logFilter)
60{
61 OSSL_PROVIDER* defaultProvider = OSSL_PROVIDER_try_load(NULL, "default", 1);
62 OSSL_PROVIDER* legacyProvider = OSSL_PROVIDER_try_load(NULL, "legacy", 1);
63
64#if PLATFORM == PLATFORM_WINDOWS
65 if (!IsProviderAvailable(legacyProvider, "legacy"))
66 {
67 SF_LOG_INFO(logFilter, "Failed loading legacy provider. Trying executable directory as OpenSSL provider path.");
68
69 if (legacyProvider != NULL)
70 {
71 OSSL_PROVIDER_unload(legacyProvider);
72 legacyProvider = NULL;
73 }
74
75 if (SetProviderSearchPathToExecutableDirectory(logFilter))
76 legacyProvider = OSSL_PROVIDER_try_load(NULL, "legacy", 1);
77 }
78#endif
79
80 SF_LOG_INFO(logFilter, "Loading default provider: (%s)",
81 IsProviderAvailable(defaultProvider, "default") ? "succeeded" : "failed");
82 SF_LOG_INFO(logFilter, "Loading legacy provider: (%s)",
83 IsProviderAvailable(legacyProvider, "legacy") ? "succeeded" : "failed");
84
85 bool const loadedRequiredProvider = IsProviderAvailable(legacyProvider, "legacy");
86
87 if (legacyProvider != NULL)
88 OSSL_PROVIDER_unload(legacyProvider);
89
90 return loadedRequiredProvider;
91}
#define SKYFIRE_PATH_MAX
Definition Define.h:35
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
bool LoadOpenSSLProviders(char const *logFilter)