]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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-2016 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 | ||
12 | #ifndef BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP | |
13 | #define BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP | |
14 | ||
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
16 | # pragma once | |
17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
18 | ||
19 | #include <boost/asio/detail/config.hpp> | |
20 | #include <vector> | |
21 | #include <boost/asio/detail/assert.hpp> | |
22 | #include <boost/asio/detail/mutex.hpp> | |
23 | #include <boost/asio/detail/tss_ptr.hpp> | |
24 | #include <boost/asio/ssl/detail/openssl_init.hpp> | |
25 | #include <boost/asio/ssl/detail/openssl_types.hpp> | |
26 | ||
27 | #include <boost/asio/detail/push_options.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace asio { | |
31 | namespace ssl { | |
32 | namespace detail { | |
33 | ||
34 | class openssl_init_base::do_init | |
35 | { | |
36 | public: | |
37 | do_init() | |
38 | { | |
39 | ::SSL_library_init(); | |
40 | ::SSL_load_error_strings(); | |
41 | ::OpenSSL_add_all_algorithms(); | |
42 | ||
43 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
44 | mutexes_.resize(::CRYPTO_num_locks()); | |
45 | for (size_t i = 0; i < mutexes_.size(); ++i) | |
46 | mutexes_[i].reset(new boost::asio::detail::mutex); | |
47 | ::CRYPTO_set_locking_callback(&do_init::openssl_locking_func); | |
48 | #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
49 | #if (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
50 | ::CRYPTO_set_id_callback(&do_init::openssl_id_func); | |
51 | #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
52 | ||
53 | #if !defined(SSL_OP_NO_COMPRESSION) \ | |
54 | && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
55 | null_compression_methods_ = sk_SSL_COMP_new_null(); | |
56 | #endif // !defined(SSL_OP_NO_COMPRESSION) | |
57 | // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
58 | } | |
59 | ||
60 | ~do_init() | |
61 | { | |
62 | #if !defined(SSL_OP_NO_COMPRESSION) \ | |
63 | && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
64 | sk_SSL_COMP_free(null_compression_methods_); | |
65 | #endif // !defined(SSL_OP_NO_COMPRESSION) | |
66 | // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
67 | ||
68 | #if (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
69 | ::CRYPTO_set_id_callback(0); | |
70 | #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
71 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
72 | ::CRYPTO_set_locking_callback(0); | |
73 | ::ERR_free_strings(); | |
74 | ::EVP_cleanup(); | |
75 | ::CRYPTO_cleanup_all_ex_data(); | |
76 | #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
77 | #if (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
78 | ::ERR_remove_state(0); | |
79 | #elif (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
80 | ::ERR_remove_thread_state(NULL); | |
81 | #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
82 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) \ | |
83 | && (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
84 | ::SSL_COMP_free_compression_methods(); | |
85 | #endif // (OPENSSL_VERSION_NUMBER >= 0x10002000L) | |
86 | // && (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
87 | #if !defined(OPENSSL_IS_BORINGSSL) | |
88 | ::CONF_modules_unload(1); | |
89 | #endif // !defined(OPENSSL_IS_BORINGSSL) | |
90 | #if !defined(OPENSSL_NO_ENGINE) \ | |
91 | && (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
92 | ::ENGINE_cleanup(); | |
93 | #endif // !defined(OPENSSL_NO_ENGINE) | |
94 | // && (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
95 | } | |
96 | ||
97 | #if !defined(SSL_OP_NO_COMPRESSION) \ | |
98 | && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
99 | STACK_OF(SSL_COMP)* get_null_compression_methods() const | |
100 | { | |
101 | return null_compression_methods_; | |
102 | } | |
103 | #endif // !defined(SSL_OP_NO_COMPRESSION) | |
104 | // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
105 | ||
106 | private: | |
107 | #if (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
108 | static unsigned long openssl_id_func() | |
109 | { | |
110 | #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) | |
111 | return ::GetCurrentThreadId(); | |
112 | #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) | |
113 | void* id = &errno; | |
114 | BOOST_ASIO_ASSERT(sizeof(unsigned long) >= sizeof(void*)); | |
115 | return reinterpret_cast<unsigned long>(id); | |
116 | #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) | |
117 | } | |
118 | #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L) | |
119 | ||
120 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
121 | static void openssl_locking_func(int mode, int n, | |
122 | const char* /*file*/, int /*line*/) | |
123 | { | |
124 | if (mode & CRYPTO_LOCK) | |
125 | instance()->mutexes_[n]->lock(); | |
126 | else | |
127 | instance()->mutexes_[n]->unlock(); | |
128 | } | |
129 | ||
130 | // Mutexes to be used in locking callbacks. | |
131 | std::vector<boost::asio::detail::shared_ptr< | |
132 | boost::asio::detail::mutex> > mutexes_; | |
133 | #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L) | |
134 | ||
135 | #if !defined(SSL_OP_NO_COMPRESSION) \ | |
136 | && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
137 | STACK_OF(SSL_COMP)* null_compression_methods_; | |
138 | #endif // !defined(SSL_OP_NO_COMPRESSION) | |
139 | // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
140 | }; | |
141 | ||
142 | boost::asio::detail::shared_ptr<openssl_init_base::do_init> | |
143 | openssl_init_base::instance() | |
144 | { | |
145 | static boost::asio::detail::shared_ptr<do_init> init(new do_init); | |
146 | return init; | |
147 | } | |
148 | ||
149 | #if !defined(SSL_OP_NO_COMPRESSION) \ | |
150 | && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
151 | STACK_OF(SSL_COMP)* openssl_init_base::get_null_compression_methods() | |
152 | { | |
153 | return instance()->get_null_compression_methods(); | |
154 | } | |
155 | #endif // !defined(SSL_OP_NO_COMPRESSION) | |
156 | // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) | |
157 | ||
158 | } // namespace detail | |
159 | } // namespace ssl | |
160 | } // namespace asio | |
161 | } // namespace boost | |
162 | ||
163 | #include <boost/asio/detail/pop_options.hpp> | |
164 | ||
165 | #endif // BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP |