Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:01

0001 //
0002 // ssl/detail/impl/openssl_init.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
0006 // Copyright (c) 2005-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // defined(_MSC_VER) && (_MSC_VER >= 1200)
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 // (OPENSSL_VERSION_NUMBER < 0x10100000L)
0049 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0050     ::CRYPTO_set_id_callback(&do_init::openssl_id_func);
0051 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
0052 
0053 #if !defined(SSL_OP_NO_COMPRESSION) \
0054   && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0055     null_compression_methods_ = sk_SSL_COMP_new_null();
0056 #endif // !defined(SSL_OP_NO_COMPRESSION)
0057        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
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 // !defined(SSL_OP_NO_COMPRESSION)
0066        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0067 
0068 #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
0069     ::CRYPTO_set_id_callback(0);
0070 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
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 // (OPENSSL_VERSION_NUMBER < 0x10100000L)
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 // (OPENSSL_VERSION_NUMBER < 0x10000000L)
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 // (OPENSSL_VERSION_NUMBER >= 0x10002000L)
0087        // && (OPENSSL_VERSION_NUMBER < 0x10100000L)
0088        // && !defined(SSL_OP_NO_COMPRESSION)
0089 #if !defined(OPENSSL_IS_BORINGSSL) \
0090     && !defined(BOOST_ASIO_USE_WOLFSSL) \
0091     && (OPENSSL_VERSION_NUMBER < 0x30000000L)
0092     ::CONF_modules_unload(1);
0093 #endif // !defined(OPENSSL_IS_BORINGSSL)
0094        //   && !defined(BOOST_ASIO_USE_WOLFSSL)
0095        //   && (OPENSSL_VERSION_NUMBER < 0x30000000L)
0096 #if !defined(OPENSSL_NO_ENGINE) \
0097   && (OPENSSL_VERSION_NUMBER < 0x10100000L)
0098     ::ENGINE_cleanup();
0099 #endif // !defined(OPENSSL_NO_ENGINE)
0100        // && (OPENSSL_VERSION_NUMBER < 0x10100000L)
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 // !defined(SSL_OP_NO_COMPRESSION)
0110        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
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 // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
0119     void* id = &errno;
0120     BOOST_ASIO_ASSERT(sizeof(unsigned long) >= sizeof(void*));
0121     return reinterpret_cast<unsigned long>(id);
0122 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
0123   }
0124 #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
0125 
0126 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
0127   static void openssl_locking_func(int mode, int n, 
0128     const char* /*file*/, int /*line*/)
0129   {
0130     if (mode & CRYPTO_LOCK)
0131       instance()->mutexes_[n]->lock();
0132     else
0133       instance()->mutexes_[n]->unlock();
0134   }
0135 
0136   // Mutexes to be used in locking callbacks.
0137   std::vector<boost::asio::detail::shared_ptr<
0138         boost::asio::detail::mutex>> mutexes_;
0139 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
0140 
0141 #if !defined(SSL_OP_NO_COMPRESSION) \
0142   && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0143   STACK_OF(SSL_COMP)* null_compression_methods_;
0144 #endif // !defined(SSL_OP_NO_COMPRESSION)
0145        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
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 // !defined(SSL_OP_NO_COMPRESSION)
0162        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0163 
0164 } // namespace detail
0165 } // namespace ssl
0166 } // namespace asio
0167 } // namespace boost
0168 
0169 #include <boost/asio/detail/pop_options.hpp>
0170 
0171 #endif // BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP