77// Not present in openssl 1.1 headers
88#define SSL_CTRL_OPTIONS 32
99
10+ static bool TryOpenLibraries (const char *sslName, LibraryLoader::handle *& sslHandle, const char *cryptoName, LibraryLoader::handle *&cryptoHandle)
11+ {
12+ sslHandle = LibraryLoader::OpenLibrary (sslName);
13+ cryptoHandle = LibraryLoader::OpenLibrary (cryptoName);
14+
15+ if (sslHandle && cryptoHandle)
16+ return true ;
17+
18+ if (sslHandle)
19+ LibraryLoader::CloseLibrary (sslHandle);
20+ if (cryptoHandle)
21+ LibraryLoader::CloseLibrary (cryptoHandle);
22+ return false ;
23+ }
24+
1025OpenSSLConnection::SSLFuncs::SSLFuncs ()
1126{
1227 using namespace LibraryLoader ;
1328
14- valid = false ;
29+ handle *sslhandle = nullptr ;
30+ handle *cryptohandle = nullptr ;
1531
16- // Try OpenSSL 1.1
17- handle *sslhandle = OpenLibrary (" libssl.so.1.1" );
18- handle *cryptohandle = OpenLibrary (" libcrypto.so.1.1" );
19- // Try OpenSSL 1.0
20- if (!sslhandle || !cryptohandle)
21- {
22- sslhandle = OpenLibrary (" libssl.so.1.0.0" );
23- cryptohandle = OpenLibrary (" libcrypto.so.1.0.0" );
24- }
25- // Try OpenSSL without version
26- if (!sslhandle || !cryptohandle)
27- {
28- sslhandle = OpenLibrary (" libssl.so" );
29- cryptohandle = OpenLibrary (" libcrypto.so" );
30- }
31- // Give up
32- if (!sslhandle || !cryptohandle)
32+ valid = TryOpenLibraries (" libssl.so.3" , sslhandle, " libcrypto.so.3" , cryptohandle)
33+ || TryOpenLibraries (" libssl.so.1.1" , sslhandle, " libcrypto.so.1.1" , cryptohandle)
34+ || TryOpenLibraries (" libssl.so.1.0.0" , sslhandle, " libcrypto.so.1.0.0" , cryptohandle)
35+ // Try the version-less name last, it may not be compatible or tested
36+ || TryOpenLibraries (" libssl.so" , sslhandle, " libcrypto.so" , cryptohandle);
37+ if (!valid)
3338 return ;
3439
3540 valid = true ;
36- valid = valid && (LoadSymbol (library_init, sslhandle, " SSL_library_init" ) ||
37- LoadSymbol (init_ssl, sslhandle, " OPENSSL_init_ssl" ));
41+ valid = valid && (
42+ LoadSymbol (init_ssl, sslhandle, " OPENSSL_init_ssl" ) ||
43+ LoadSymbol (library_init, sslhandle, " SSL_library_init" ));
3844
3945 valid = valid && LoadSymbol (CTX_new, sslhandle, " SSL_CTX_new" );
4046 valid = valid && LoadSymbol (CTX_ctrl, sslhandle, " SSL_CTX_ctrl" );
47+ if (valid)
48+ LoadSymbol (CTX_set_options, sslhandle, " SSL_CTX_set_options" );
4149 valid = valid && LoadSymbol (CTX_set_verify, sslhandle, " SSL_CTX_set_verify" );
4250 valid = valid && LoadSymbol (CTX_set_default_verify_paths, sslhandle, " SSL_CTX_set_default_verify_paths" );
4351 valid = valid && LoadSymbol (CTX_free, sslhandle, " SSL_CTX_free" );
@@ -50,12 +58,16 @@ OpenSSLConnection::SSLFuncs::SSLFuncs()
5058 valid = valid && LoadSymbol (write, sslhandle, " SSL_write" );
5159 valid = valid && LoadSymbol (shutdown, sslhandle, " SSL_shutdown" );
5260 valid = valid && LoadSymbol (get_verify_result, sslhandle, " SSL_get_verify_result" );
53- valid = valid && LoadSymbol (get_peer_certificate, sslhandle, " SSL_get_peer_certificate" );
61+ valid = valid && (LoadSymbol (get_peer_certificate, sslhandle, " SSL_get1_peer_certificate" ) ||
62+ LoadSymbol (get_peer_certificate, sslhandle, " SSL_get_peer_certificate" ));
5463
55- valid = valid && (LoadSymbol (SSLv23_method, sslhandle, " SSLv23_method" ) ||
56- LoadSymbol (SSLv23_method, sslhandle, " TLS_method" ));
64+ valid = valid && (
65+ LoadSymbol (SSLv23_method, sslhandle, " TLS_client_method" ) ||
66+ LoadSymbol (SSLv23_method, sslhandle, " TLS_method" ) ||
67+ LoadSymbol (SSLv23_method, sslhandle, " SSLv23_method" ));
5768
5869 valid = valid && LoadSymbol (check_host, cryptohandle, " X509_check_host" );
70+ valid = valid && LoadSymbol (X509_free, cryptohandle, " X509_free" );
5971
6072 if (library_init)
6173 library_init ();
@@ -76,7 +88,10 @@ OpenSSLConnection::OpenSSLConnection()
7688 if (!context)
7789 return ;
7890
79- ssl.CTX_ctrl (context, SSL_CTRL_OPTIONS, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3, nullptr );
91+ if (ssl.CTX_set_options )
92+ ssl.CTX_set_options (context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
93+ else
94+ ssl.CTX_ctrl (context, SSL_CTRL_OPTIONS, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3, nullptr );
8095 ssl.CTX_set_verify (context, SSL_VERIFY_PEER, nullptr );
8196 ssl.CTX_set_default_verify_paths (context);
8297}
@@ -118,6 +133,7 @@ bool OpenSSLConnection::connect(const std::string &hostname, uint16_t port)
118133 close ();
119134 return false ;
120135 }
136+ ssl.X509_free (cert);
121137
122138 return true ;
123139}
0 commit comments