openssl_init.ipp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // ssl/detail/impl/openssl_init.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
  12. #define BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <vector>
  18. #include <boost/asio/detail/assert.hpp>
  19. #include <boost/asio/detail/mutex.hpp>
  20. #include <boost/asio/detail/tss_ptr.hpp>
  21. #include <boost/asio/ssl/detail/openssl_init.hpp>
  22. #include <boost/asio/ssl/detail/openssl_types.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ssl {
  27. namespace detail {
  28. class openssl_init_base::do_init
  29. {
  30. public:
  31. do_init()
  32. {
  33. ::SSL_library_init();
  34. ::SSL_load_error_strings();
  35. ::OpenSSL_add_all_algorithms();
  36. mutexes_.resize(::CRYPTO_num_locks());
  37. for (size_t i = 0; i < mutexes_.size(); ++i)
  38. mutexes_[i].reset(new boost::asio::detail::mutex);
  39. ::CRYPTO_set_locking_callback(&do_init::openssl_locking_func);
  40. ::CRYPTO_set_id_callback(&do_init::openssl_id_func);
  41. #if !defined(SSL_OP_NO_COMPRESSION) \
  42. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  43. null_compression_methods_ = sk_SSL_COMP_new_null();
  44. #endif // !defined(SSL_OP_NO_COMPRESSION)
  45. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  46. }
  47. ~do_init()
  48. {
  49. #if !defined(SSL_OP_NO_COMPRESSION) \
  50. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  51. sk_SSL_COMP_free(null_compression_methods_);
  52. #endif // !defined(SSL_OP_NO_COMPRESSION)
  53. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  54. ::CRYPTO_set_id_callback(0);
  55. ::CRYPTO_set_locking_callback(0);
  56. ::ERR_free_strings();
  57. ::ERR_remove_state(0);
  58. ::EVP_cleanup();
  59. ::CRYPTO_cleanup_all_ex_data();
  60. ::CONF_modules_unload(1);
  61. #if !defined(OPENSSL_NO_ENGINE)
  62. ::ENGINE_cleanup();
  63. #endif // !defined(OPENSSL_NO_ENGINE)
  64. }
  65. #if !defined(SSL_OP_NO_COMPRESSION) \
  66. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  67. STACK_OF(SSL_COMP)* get_null_compression_methods() const
  68. {
  69. return null_compression_methods_;
  70. }
  71. #endif // !defined(SSL_OP_NO_COMPRESSION)
  72. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  73. private:
  74. static unsigned long openssl_id_func()
  75. {
  76. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  77. return ::GetCurrentThreadId();
  78. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  79. void* id = instance()->thread_id_;
  80. if (id == 0)
  81. instance()->thread_id_ = id = &id; // Ugh.
  82. BOOST_ASIO_ASSERT(sizeof(unsigned long) >= sizeof(void*));
  83. return reinterpret_cast<unsigned long>(id);
  84. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  85. }
  86. static void openssl_locking_func(int mode, int n,
  87. const char* /*file*/, int /*line*/)
  88. {
  89. if (mode & CRYPTO_LOCK)
  90. instance()->mutexes_[n]->lock();
  91. else
  92. instance()->mutexes_[n]->unlock();
  93. }
  94. // Mutexes to be used in locking callbacks.
  95. std::vector<boost::asio::detail::shared_ptr<
  96. boost::asio::detail::mutex> > mutexes_;
  97. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  98. // The thread identifiers to be used by openssl.
  99. boost::asio::detail::tss_ptr<void> thread_id_;
  100. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  101. #if !defined(SSL_OP_NO_COMPRESSION) \
  102. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  103. STACK_OF(SSL_COMP)* null_compression_methods_;
  104. #endif // !defined(SSL_OP_NO_COMPRESSION)
  105. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  106. };
  107. boost::asio::detail::shared_ptr<openssl_init_base::do_init>
  108. openssl_init_base::instance()
  109. {
  110. static boost::asio::detail::shared_ptr<do_init> init(new do_init);
  111. return init;
  112. }
  113. #if !defined(SSL_OP_NO_COMPRESSION) \
  114. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  115. STACK_OF(SSL_COMP)* openssl_init_base::get_null_compression_methods()
  116. {
  117. return instance()->get_null_compression_methods();
  118. }
  119. #endif // !defined(SSL_OP_NO_COMPRESSION)
  120. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  121. } // namespace detail
  122. } // namespace ssl
  123. } // namespace asio
  124. } // namespace boost
  125. #include <boost/asio/detail/pop_options.hpp>
  126. #endif // BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP