2013-08-13 16:58:55 +00:00
|
|
|
#include <fc/crypto/openssl.hpp>
|
2014-06-02 15:31:28 +00:00
|
|
|
|
|
|
|
|
#include <fc/filesystem.hpp>
|
|
|
|
|
|
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
|
|
2014-06-03 15:03:28 +00:00
|
|
|
#include <cstdlib>
|
2014-06-02 15:31:28 +00:00
|
|
|
#include <string>
|
2014-06-03 15:16:29 +00:00
|
|
|
#include <stdlib.h>
|
2014-06-02 15:31:28 +00:00
|
|
|
|
2013-08-13 16:58:55 +00:00
|
|
|
namespace fc
|
|
|
|
|
{
|
|
|
|
|
struct openssl_scope
|
|
|
|
|
{
|
2014-06-03 15:16:29 +00:00
|
|
|
static path _configurationFilePath;
|
2013-08-13 16:58:55 +00:00
|
|
|
openssl_scope()
|
|
|
|
|
{
|
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
OpenSSL_add_all_algorithms();
|
2014-06-03 15:03:28 +00:00
|
|
|
|
2014-06-02 15:31:28 +00:00
|
|
|
const boost::filesystem::path& boostPath = _configurationFilePath;
|
2014-06-03 15:03:28 +00:00
|
|
|
if(boostPath.empty() == false)
|
2014-06-03 15:16:29 +00:00
|
|
|
{
|
2014-06-05 08:00:50 +00:00
|
|
|
std::string varSetting("OPENSSL_CONF=");
|
|
|
|
|
varSetting += _configurationFilePath.to_native_ansi_path();
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
_putenv((char*)varSetting.c_str());
|
|
|
|
|
#else
|
|
|
|
|
putenv((char*)varSetting.c_str());
|
|
|
|
|
#endif
|
2014-06-03 15:16:29 +00:00
|
|
|
}
|
2018-10-22 12:38:08 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
|
|
|
|
// no longer needed as of OpenSSL 1.1
|
|
|
|
|
// if special initialization is necessary in versions 1.1 and above,
|
|
|
|
|
// use OPENSSL_init_crypto
|
2014-06-03 15:03:28 +00:00
|
|
|
OPENSSL_config(nullptr);
|
2018-10-22 12:38:08 +00:00
|
|
|
#endif
|
2013-08-13 16:58:55 +00:00
|
|
|
}
|
2014-06-02 15:31:28 +00:00
|
|
|
|
2013-08-13 16:58:55 +00:00
|
|
|
~openssl_scope()
|
|
|
|
|
{
|
2019-08-13 15:33:12 +00:00
|
|
|
#if not defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
|
// No FIPS in LibreSSL.
|
|
|
|
|
// https://marc.info/?l=openbsd-misc&m=139819485423701&w=2
|
2019-04-02 10:33:04 +00:00
|
|
|
FIPS_mode_set(0);
|
2019-08-13 15:33:12 +00:00
|
|
|
#endif
|
2019-04-02 10:33:04 +00:00
|
|
|
CONF_modules_unload(1);
|
2013-08-13 16:58:55 +00:00
|
|
|
EVP_cleanup();
|
2019-04-02 10:33:04 +00:00
|
|
|
CRYPTO_cleanup_all_ex_data();
|
2013-08-13 16:58:55 +00:00
|
|
|
ERR_free_strings();
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-06-02 15:31:28 +00:00
|
|
|
|
|
|
|
|
path openssl_scope::_configurationFilePath;
|
|
|
|
|
|
|
|
|
|
void store_configuration_path(const path& filePath)
|
2014-06-03 15:16:29 +00:00
|
|
|
{
|
2014-06-02 15:31:28 +00:00
|
|
|
openssl_scope::_configurationFilePath = filePath;
|
2014-06-03 15:16:29 +00:00
|
|
|
}
|
2013-08-13 16:58:55 +00:00
|
|
|
|
|
|
|
|
int init_openssl()
|
|
|
|
|
{
|
2014-07-22 22:18:09 +00:00
|
|
|
static openssl_scope ossl;
|
|
|
|
|
return 0;
|
2013-08-13 16:58:55 +00:00
|
|
|
}
|
2019-09-16 12:38:45 +00:00
|
|
|
|
|
|
|
|
#define SSL_TYPE_IMPL(name, ssl_type, free_func) \
|
|
|
|
|
name::name( ssl_type* obj ) : ssl_wrapper(obj) {} \
|
|
|
|
|
name::name( name&& move ) : ssl_wrapper( move.obj ) \
|
|
|
|
|
{ \
|
|
|
|
|
move.obj = nullptr; \
|
|
|
|
|
} \
|
|
|
|
|
name::~name() \
|
|
|
|
|
{ \
|
|
|
|
|
if( obj != nullptr ) \
|
|
|
|
|
free_func(obj); \
|
|
|
|
|
} \
|
|
|
|
|
name& name::operator=( name&& move ) \
|
|
|
|
|
{ \
|
|
|
|
|
if( this != &move ) \
|
|
|
|
|
{ \
|
|
|
|
|
if( obj != nullptr ) \
|
|
|
|
|
free_func(obj); \
|
|
|
|
|
obj = move.obj; \
|
|
|
|
|
move.obj = nullptr; \
|
|
|
|
|
} \
|
|
|
|
|
return *this; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSL_TYPE_IMPL(ec_group, EC_GROUP, EC_GROUP_free)
|
|
|
|
|
SSL_TYPE_IMPL(ec_point, EC_POINT, EC_POINT_free)
|
|
|
|
|
SSL_TYPE_IMPL(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free)
|
|
|
|
|
SSL_TYPE_IMPL(bn_ctx, BN_CTX, BN_CTX_free)
|
|
|
|
|
SSL_TYPE_IMPL(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free )
|
|
|
|
|
SSL_TYPE_IMPL(ssl_dh, DH, DH_free)
|
2013-08-13 16:58:55 +00:00
|
|
|
}
|