BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
openssl.cpp
Go to the documentation of this file.
1 #include <fc/crypto/openssl.hpp>
2 
3 #include <fc/filesystem.hpp>
4 
5 #include <boost/filesystem/path.hpp>
6 
7 #include <cstdlib>
8 #include <string>
9 #include <stdlib.h>
10 
11 namespace fc
12 {
14  {
17  {
18  ERR_load_crypto_strings();
19  OpenSSL_add_all_algorithms();
20 
21  const boost::filesystem::path& boostPath = _configurationFilePath;
22  if(boostPath.empty() == false)
23  {
24  std::string varSetting("OPENSSL_CONF=");
25  varSetting += _configurationFilePath.to_native_ansi_path();
26 #if defined(WIN32)
27  _putenv((char*)varSetting.c_str());
28 #else
29  putenv((char*)varSetting.c_str());
30 #endif
31  }
32 #if OPENSSL_VERSION_NUMBER < 0x10100000L
33  // no longer needed as of OpenSSL 1.1
34  // if special initialization is necessary in versions 1.1 and above,
35  // use OPENSSL_init_crypto
36  OPENSSL_config(nullptr);
37 #endif
38  }
39 
41  {
42 #if not defined(LIBRESSL_VERSION_NUMBER)
43  // No FIPS in LibreSSL.
44  // https://marc.info/?l=openbsd-misc&m=139819485423701&w=2
45  FIPS_mode_set(0);
46 #endif
47  CONF_modules_unload(1);
48  EVP_cleanup();
49  CRYPTO_cleanup_all_ex_data();
50  ERR_free_strings();
51  }
52  };
53 
55 
56  void store_configuration_path(const path& filePath)
57  {
58  openssl_scope::_configurationFilePath = filePath;
59  }
60 
62  {
63  static openssl_scope ossl;
64  return 0;
65  }
66 
67  #define SSL_TYPE_IMPL(name, ssl_type, free_func) \
68  name::name( ssl_type* obj ) : ssl_wrapper(obj) {} \
69  name::name( name&& move ) : ssl_wrapper( move.obj ) \
70  { \
71  move.obj = nullptr; \
72  } \
73  name::~name() \
74  { \
75  if( obj != nullptr ) \
76  free_func(obj); \
77  } \
78  name& name::operator=( name&& move ) \
79  { \
80  if( this != &move ) \
81  { \
82  if( obj != nullptr ) \
83  free_func(obj); \
84  obj = move.obj; \
85  move.obj = nullptr; \
86  } \
87  return *this; \
88  }
89 
90  SSL_TYPE_IMPL(ec_group, EC_GROUP, EC_GROUP_free)
91  SSL_TYPE_IMPL(ec_point, EC_POINT, EC_POINT_free)
92  SSL_TYPE_IMPL(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free)
93  SSL_TYPE_IMPL(bn_ctx, BN_CTX, BN_CTX_free)
94  SSL_TYPE_IMPL(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free )
95  SSL_TYPE_IMPL(ssl_dh, DH, DH_free)
96 }
std::string to_native_ansi_path() const
Definition: filesystem.cpp:119
int init_openssl()
Definition: openssl.cpp:61
void store_configuration_path(const path &filePath)
Definition: openssl.cpp:56
#define SSL_TYPE_IMPL(name, ssl_type, free_func)
Definition: openssl.cpp:67
Definition: api.hpp:15
static path _configurationFilePath
Definition: openssl.cpp:15
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28