Boost C++ Libraries: Ticket #13128: boost asio does not initialize libressl library https://svn.boost.org/trac10/ticket/13128 <p> When using the <code>libressl</code> library with boost asio, there is some library initialization code that depends on <code>OPENSSL_VERSION_NUMBER</code> that should be executed but it is not because <code>libressl</code> identifies itself with <code>OPENSSL_VERSION_NUMBER 0x20000000L</code> and the code in question is inside an <code>#if (OPENSSL_VERSION_NUMBER &lt; 0x10100000L)</code>or an <code>#if (OPENSSL_VERSION_NUMBER &lt; 0x10000000L)</code> </p> <p> The code I'm referring to is in <code>boost/asio/ssl/detail/impl/openssl_init.ipp</code> as follows: </p> <ul><li>in <code>openssl_init_base::do_init</code> method there is the following code: <pre class="wiki">#if (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) ::SSL_library_init(); ::SSL_load_error_strings(); ::OpenSSL_add_all_algorithms(); mutexes_.resize(::CRYPTO_num_locks()); for (size_t i = 0; i &lt; mutexes_.size(); ++i) mutexes_[i].reset(new boost::asio::detail::mutex); ::CRYPTO_set_locking_callback(&amp;do_init::openssl_locking_func); #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) #if (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) ::CRYPTO_set_id_callback(&amp;do_init::openssl_id_func); #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) </pre></li></ul><ul><li>similarly, in <code>~do_init()</code> the following sequence should also execute for libressl: <pre class="wiki">#if (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) ::CRYPTO_set_id_callback(0); #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) #if (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) ::CRYPTO_set_locking_callback(0); ::ERR_free_strings(); ::EVP_cleanup(); ::CRYPTO_cleanup_all_ex_data(); #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) </pre></li><li>also in <code>~do_init()</code> the <code>ERR_remove_thread_state</code> and <code>ENGINE_cleanup</code> functions should be called <pre class="wiki">#elif (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) ::ERR_remove_thread_state(NULL); #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) </pre><pre class="wiki">#if !defined(OPENSSL_NO_ENGINE) \ &amp;&amp; (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) ::ENGINE_cleanup(); #endif // !defined(OPENSSL_NO_ENGINE) // &amp;&amp; (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) </pre></li><li>the following <code>openssl_id_func</code> and <code>openssl_locking_func</code> should also be available for <code>libressl</code>, along with the vector of mutexes: <pre class="wiki">#if (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) static unsigned long openssl_id_func() { #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) return ::GetCurrentThreadId(); #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) void* id = &amp;errno; BOOST_ASIO_ASSERT(sizeof(unsigned long) &gt;= sizeof(void*)); return reinterpret_cast&lt;unsigned long&gt;(id); #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) } #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10000000L) #if (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) static void openssl_locking_func(int mode, int n, const char* /*file*/, int /*line*/) { if (mode &amp; CRYPTO_LOCK) instance()-&gt;mutexes_[n]-&gt;lock(); else instance()-&gt;mutexes_[n]-&gt;unlock(); } // Mutexes to be used in locking callbacks. std::vector&lt;boost::asio::detail::shared_ptr&lt; boost::asio::detail::mutex&gt; &gt; mutexes_; #endif // (OPENSSL_VERSION_NUMBER &lt; 0x10100000L) </pre></li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13128 Trac 1.4.3 anonymous Tue, 15 May 2018 19:48:43 GMT <link>https://svn.boost.org/trac10/ticket/13128#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13128#comment:1</guid> <description> <p> You should not attempt to initialize OpenSSL from boost code at all -- doing so conflicts with other initializations made by user code (or other libraries). These initialization (and un-initialization) routines aren't thread-safe. You also can't just stomp in and replace locks provided by user with those of your choosing. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 15 May 2018 20:06:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13128#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13128#comment:2</guid> <description> <p> FYI: <a class="ext-link" href="https://wiki.openssl.org/index.php/Library_Initialization"><span class="icon">​</span>https://wiki.openssl.org/index.php/Library_Initialization</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>