Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit 7a9e727

Browse files
bartonjsdanmoseley
authored andcommitted
Always build the OpenSSL shim in portable mode on macOS. (#42809)
1 parent dac9eef commit 7a9e727

File tree

3 files changed

+51
-56
lines changed

3 files changed

+51
-56
lines changed

src/Native/Unix/System.Security.Cryptography.Native/CMakeLists.txt

+21-27
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ set(NATIVECRYPTO_SOURCES
4646
pal_x509ext.cpp
4747
)
4848

49-
if (FEATURE_DISTRO_AGNOSTIC_SSL)
50-
if (NOT CMAKE_SYSTEM_NAME STREQUAL Linux)
51-
message(FATAL_ERROR "FEATURE_DISTRO_AGNOSTIC_SSL can only be enabled for Linux")
52-
endif()
49+
# Always build portable on macOS because OpenSSL is not a system component
50+
# and our prebuilts should not assume a specific ABI version for the types
51+
# that use OpenSSL at runtime.
52+
if (APPLE)
53+
set(FEATURE_DISTRO_AGNOSTIC_SSL True)
54+
endif()
5355

56+
if (FEATURE_DISTRO_AGNOSTIC_SSL)
5457
list(APPEND NATIVECRYPTO_SOURCES
5558
opensslshim.cpp
5659
)
@@ -77,13 +80,20 @@ set_target_properties(System.Security.Cryptography.Native.OpenSsl-Static PROPERT
7780
set_target_properties(System.Security.Cryptography.Native.OpenSsl-Static PROPERTIES OUTPUT_NAME System.Security.Cryptography.Native.OpenSsl CLEAN_DIRECT_OUTPUT 1)
7881

7982
if (FEATURE_DISTRO_AGNOSTIC_SSL)
80-
add_custom_command(TARGET System.Security.Cryptography.Native.OpenSsl POST_BUILD
81-
COMMENT "Verifying System.Security.Cryptography.Native.OpenSsl.so dependencies"
82-
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-so.sh
83-
$<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl>
84-
"Verification failed. System.Security.Cryptography.Native.OpenSsl.so has undefined dependencies. These are likely OpenSSL APIs that need to be added to opensslshim.h"
85-
VERBATIM
86-
)
83+
# on macOS the link step fails with undefined symbols, and the script doesn't run.
84+
# if the build succeeds, the script would succeed, except it uses a Linux-only command.
85+
#
86+
# on Linux, the build will succeed with undefined symbols, then the script reports them
87+
# and fails the build for us.
88+
if (NOT APPLE)
89+
add_custom_command(TARGET System.Security.Cryptography.Native.OpenSsl POST_BUILD
90+
COMMENT "Verifying System.Security.Cryptography.Native.OpenSsl.so dependencies"
91+
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-so.sh
92+
$<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl>
93+
"Verification failed. System.Security.Cryptography.Native.OpenSsl.so has undefined dependencies. These are likely OpenSSL APIs that need to be added to opensslshim.h"
94+
VERBATIM
95+
)
96+
endif()
8797

8898
# Link with libdl.so to get the dlopen / dlsym / dlclose
8999
target_link_libraries(System.Security.Cryptography.Native.OpenSsl
@@ -94,22 +104,6 @@ else()
94104
${OPENSSL_CRYPTO_LIBRARY}
95105
${OPENSSL_SSL_LIBRARY}
96106
)
97-
98-
# On OS X every library emits the manner in which it should be referenced.
99-
# All of our libraries are referenced via @rpath, which is similar to how Linux and Windows
100-
# libraries are loaded. The homebrew installation of OpenSSL (libcrypto, libssl) uses the
101-
# full path to the library installation. This means that this library is not flexible to
102-
# users installing newer libcrypto in the working directory, or to systems which do not
103-
# install to the same path as homebrew does.
104-
#
105-
# So, after compiling, rewrite the references to libcrypto to be more flexible.
106-
if (APPLE)
107-
add_custom_command(TARGET System.Security.Cryptography.Native.OpenSsl POST_BUILD
108-
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -change /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib @rpath/libcrypto.1.0.0.dylib $<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl>
109-
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -change /usr/local/opt/openssl/lib/libssl.1.0.0.dylib @rpath/libssl.1.0.0.dylib $<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl>
110-
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath @loader_path $<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl>
111-
)
112-
endif()
113107
endif()
114108

115109
include(configure.cmake)

src/Native/Unix/System.Security.Cryptography.Native/openssl.cpp

-23
Original file line numberDiff line numberDiff line change
@@ -1332,24 +1332,6 @@ static void LockingCallback(int mode, int n, const char* file, int line)
13321332
#pragma clang diagnostic pop
13331333
}
13341334

1335-
#ifdef __APPLE__
1336-
/*
1337-
Function:
1338-
GetCurrentThreadId
1339-
1340-
Called back by OpenSSL to get the current thread id.
1341-
1342-
This is necessary because OSX uses an earlier version of
1343-
OpenSSL, which requires setting the CRYPTO_set_id_callback.
1344-
*/
1345-
static unsigned long GetCurrentThreadId()
1346-
{
1347-
uint64_t tid;
1348-
pthread_threadid_np(pthread_self(), &tid);
1349-
return tid;
1350-
}
1351-
#endif // __APPLE__
1352-
13531335
/*
13541336
Function:
13551337
EnsureOpenSslInitialized
@@ -1405,11 +1387,6 @@ static int32_t EnsureOpenSsl10Initialized()
14051387
// Initialize the callback
14061388
CRYPTO_set_locking_callback(LockingCallback);
14071389

1408-
#ifdef __APPLE__
1409-
// OSX uses an earlier version of OpenSSL which requires setting the CRYPTO_set_id_callback
1410-
CRYPTO_set_id_callback(GetCurrentThreadId);
1411-
#endif
1412-
14131390
// Initialize the random number generator seed
14141391
randPollResult = RAND_poll();
14151392
if (randPollResult < 1)

src/Native/Unix/System.Security.Cryptography.Native/opensslshim.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44
//
55

6+
#include <assert.h>
67
#include <dlfcn.h>
78
#include <stdio.h>
89
#include <stdbool.h>
@@ -27,10 +28,24 @@ FOR_ALL_OPENSSL_FUNCTIONS
2728

2829
// x.x.x, considering the max number of decimal digits for each component
2930
static const int MaxVersionStringLength = 32;
30-
#define SONAME_BASE "libssl.so."
3131

3232
static void* libssl = nullptr;
3333

34+
#ifdef __APPLE__
35+
#define DYLIBNAME_PREFIX "libssl."
36+
#define DYLIBNAME_SUFFIX ".dylib"
37+
#define MAKELIB(v) DYLIBNAME_PREFIX v DYLIBNAME_SUFFIX
38+
#else
39+
#define SONAME_BASE "libssl.so."
40+
#define MAKELIB(v) SONAME_BASE v
41+
#endif
42+
43+
static void DlOpen(const char* libraryName)
44+
{
45+
assert(libssl == nullptr);
46+
libssl = dlopen(libraryName, RTLD_LAZY);
47+
}
48+
3449
bool OpenLibrary()
3550
{
3651
// If there is an override of the version specified using the CLR_OPENSSL_VERSION_OVERRIDE
@@ -41,35 +56,44 @@ bool OpenLibrary()
4156

4257
if ((versionOverride != nullptr) && strnlen(versionOverride, MaxVersionStringLength + 1) <= MaxVersionStringLength)
4358
{
59+
#ifdef __APPLE__
60+
char soName[sizeof(DYLIBNAME_PREFIX) + MaxVersionStringLength + sizeof(DYLIBNAME_SUFFIX)] =
61+
DYLIBNAME_PREFIX;
62+
63+
strcat(soName, versionOverride);
64+
strcat(soName, DYLIBNAME_SUFFIX);
65+
#else
4466
char soName[sizeof(SONAME_BASE) + MaxVersionStringLength] = SONAME_BASE;
4567

4668
strcat(soName, versionOverride);
47-
libssl = dlopen(soName, RTLD_LAZY);
69+
#endif
70+
71+
DlOpen(soName);
4872
}
4973

5074
if (libssl == nullptr)
5175
{
5276
// Debian 9 has dropped support for SSLv3 and so they have bumped their soname. Let's try it
5377
// before trying the version 1.0.0 to make it less probable that some of our other dependencies
5478
// end up loading conflicting version of libssl.
55-
libssl = dlopen("libssl.so.1.0.2", RTLD_LAZY);
79+
DlOpen(MAKELIB("1.0.2"));
5680
}
5781

5882
if (libssl == nullptr)
5983
{
6084
// Now try the default versioned so naming as described in the OpenSSL doc
61-
libssl = dlopen("libssl.so.1.0.0", RTLD_LAZY);
85+
DlOpen(MAKELIB("1.0.0"));
6286
}
6387

6488
if (libssl == nullptr)
6589
{
6690
// Fedora derived distros use different naming for the version 1.0.0
67-
libssl = dlopen("libssl.so.10", RTLD_LAZY);
91+
DlOpen(MAKELIB("10"));
6892
}
6993

7094
if (libssl == nullptr)
7195
{
72-
libssl = dlopen("libssl.so.1.1", RTLD_LAZY);
96+
DlOpen(MAKELIB("1.1"));
7397
}
7498

7599
return libssl != nullptr;

0 commit comments

Comments
 (0)