File indexing completed on 2025-01-18 09:29:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
0013 #define BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
0014
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif
0018
0019 #include <boost/asio/detail/config.hpp>
0020 #include <vector>
0021 #include <boost/asio/detail/assert.hpp>
0022 #include <boost/asio/detail/mutex.hpp>
0023 #include <boost/asio/detail/tss_ptr.hpp>
0024 #include <boost/asio/ssl/detail/openssl_init.hpp>
0025 #include <boost/asio/ssl/detail/openssl_types.hpp>
0026
0027 #include <boost/asio/detail/push_options.hpp>
0028
0029 namespace boost {
0030 namespace asio {
0031 namespace ssl {
0032 namespace detail {
0033
0034 class openssl_init_base::do_init
0035 {
0036 public:
0037 do_init()
0038 {
0039 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
0040 ::SSL_library_init();
0041 ::SSL_load_error_strings();
0042 ::OpenSSL_add_all_algorithms();
0043
0044 mutexes_.resize(::CRYPTO_num_locks());
0045 for (size_t i = 0; i < mutexes_.size(); ++i)
0046 mutexes_[i].reset(new boost::asio::detail::mutex);
0047 ::CRYPTO_set_locking_callback(&do_init::openssl_locking_func);
0048 #endif
0049 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0050 ::CRYPTO_set_id_callback(&do_init::openssl_id_func);
0051 #endif
0052
0053 #if !defined(SSL_OP_NO_COMPRESSION) \
0054 && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0055 null_compression_methods_ = sk_SSL_COMP_new_null();
0056 #endif
0057
0058 }
0059
0060 ~do_init()
0061 {
0062 #if !defined(SSL_OP_NO_COMPRESSION) \
0063 && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0064 sk_SSL_COMP_free(null_compression_methods_);
0065 #endif
0066
0067
0068 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0069 ::CRYPTO_set_id_callback(0);
0070 #endif
0071 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
0072 ::CRYPTO_set_locking_callback(0);
0073 ::ERR_free_strings();
0074 ::EVP_cleanup();
0075 ::CRYPTO_cleanup_all_ex_data();
0076 #endif
0077 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0078 ::ERR_remove_state(0);
0079 #elif (OPENSSL_VERSION_NUMBER < 0x10100000L)
0080 ::ERR_remove_thread_state(NULL);
0081 #endif
0082 #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) \
0083 && (OPENSSL_VERSION_NUMBER < 0x10100000L) \
0084 && !defined(SSL_OP_NO_COMPRESSION)
0085 ::SSL_COMP_free_compression_methods();
0086 #endif
0087
0088
0089 #if !defined(OPENSSL_IS_BORINGSSL) \
0090 && !defined(BOOST_ASIO_USE_WOLFSSL) \
0091 && (OPENSSL_VERSION_NUMBER < 0x30000000L)
0092 ::CONF_modules_unload(1);
0093 #endif
0094
0095
0096 #if !defined(OPENSSL_NO_ENGINE) \
0097 && (OPENSSL_VERSION_NUMBER < 0x10100000L)
0098 ::ENGINE_cleanup();
0099 #endif
0100
0101 }
0102
0103 #if !defined(SSL_OP_NO_COMPRESSION) \
0104 && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0105 STACK_OF(SSL_COMP)* get_null_compression_methods() const
0106 {
0107 return null_compression_methods_;
0108 }
0109 #endif
0110
0111
0112 private:
0113 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0114 static unsigned long openssl_id_func()
0115 {
0116 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
0117 return ::GetCurrentThreadId();
0118 #else
0119 void* id = &errno;
0120 BOOST_ASIO_ASSERT(sizeof(unsigned long) >= sizeof(void*));
0121 return reinterpret_cast<unsigned long>(id);
0122 #endif
0123 }
0124 #endif
0125
0126 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
0127 static void openssl_locking_func(int mode, int n,
0128 const char* , int )
0129 {
0130 if (mode & CRYPTO_LOCK)
0131 instance()->mutexes_[n]->lock();
0132 else
0133 instance()->mutexes_[n]->unlock();
0134 }
0135
0136
0137 std::vector<boost::asio::detail::shared_ptr<
0138 boost::asio::detail::mutex>> mutexes_;
0139 #endif
0140
0141 #if !defined(SSL_OP_NO_COMPRESSION) \
0142 && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0143 STACK_OF(SSL_COMP)* null_compression_methods_;
0144 #endif
0145
0146 };
0147
0148 boost::asio::detail::shared_ptr<openssl_init_base::do_init>
0149 openssl_init_base::instance()
0150 {
0151 static boost::asio::detail::shared_ptr<do_init> init(new do_init);
0152 return init;
0153 }
0154
0155 #if !defined(SSL_OP_NO_COMPRESSION) \
0156 && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0157 STACK_OF(SSL_COMP)* openssl_init_base::get_null_compression_methods()
0158 {
0159 return instance()->get_null_compression_methods();
0160 }
0161 #endif
0162
0163
0164 }
0165 }
0166 }
0167 }
0168
0169 #include <boost/asio/detail/pop_options.hpp>
0170
0171 #endif