diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets index 8dea93b9dff256..45b35a8026429e 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets @@ -144,7 +144,12 @@ The .NET Foundation licenses this file to you under the MIT license. - + + + + + + diff --git a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props index 44625877c6561d..277be3b3e06245 100644 --- a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props +++ b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props @@ -91,6 +91,7 @@ + diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index c37e523cf148bd..cd1dbc19239e44 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -176,6 +176,7 @@ else() else() list(APPEND NATIVE_LIBS System.Security.Cryptography.Native.Android-Static + System.Security.Cryptography.Native.Android.JNIExport-Static ) endif() diff --git a/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt index f860bc26f819a2..cfb189ae6006e2 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt @@ -33,7 +33,7 @@ set(NATIVECRYPTO_SOURCES add_library(System.Security.Cryptography.Native.Android SHARED - ${NATIVECRYPTO_SOURCES} pal_jni_onload.c + ${NATIVECRYPTO_SOURCES} pal_jni_onload.c pal_trust_manager_jni_export.c ${VERSION_FILE_PATH} ) @@ -42,7 +42,24 @@ add_library(System.Security.Cryptography.Native.Android-Static ${NATIVECRYPTO_SOURCES} ) -set_target_properties(System.Security.Cryptography.Native.Android-Static PROPERTIES OUTPUT_NAME System.Security.Cryptography.Native.Android CLEAN_DIRECT_OUTPUT 1) +# +# This is necessary so that dynamic linking of the .NET for Android runtime +# can hide all the other symbols in System.Security.Cryptography.Native.Android. +# +# .NET for Android dynamic runtime linking links all the relevant native BCL +# libraries into a single .so, using the .a archives built here. clang allows +# hiding all the symbols in the .a archive, but there's no (working) way to +# exclude just select symbols from hiding. +# +# Java VM requires that all the functions implementing the `native` methods are +# exported from the shared libraries they are implemented in. Therefore it is +# necessary to put this symbol in a separate .a archive so that we can exclude it +# from hiding described above. +# +add_library(System.Security.Cryptography.Native.Android.JNIExport-Static + STATIC + pal_trust_manager_jni_export.c +) target_link_libraries(System.Security.Cryptography.Native.Android PRIVATE @@ -50,14 +67,17 @@ target_link_libraries(System.Security.Cryptography.Native.Android ) set_target_properties(System.Security.Cryptography.Native.Android PROPERTIES OUTPUT_NAME "System.Security.Cryptography.Native.Android") -set_target_properties(System.Security.Cryptography.Native.Android-Static PROPERTIES OUTPUT_NAME "System.Security.Cryptography.Native.Android") +set_target_properties(System.Security.Cryptography.Native.Android-Static PROPERTIES OUTPUT_NAME "System.Security.Cryptography.Native.Android" CLEAN_DIRECT_OUTPUT 1) +set_target_properties(System.Security.Cryptography.Native.Android.JNIExport-Static PROPERTIES OUTPUT_NAME "System.Security.Cryptography.Native.Android.JNIExport" CLEAN_DIRECT_OUTPUT 1) if (GEN_SHARED_LIB) install_with_stripped_symbols (System.Security.Cryptography.Native.Android PROGRAMS .) endif() install (TARGETS System.Security.Cryptography.Native.Android-Static DESTINATION ${STATIC_LIB_DESTINATION} COMPONENT libs) +install (TARGETS System.Security.Cryptography.Native.Android.JNIExport-Static DESTINATION ${STATIC_LIB_DESTINATION} COMPONENT libs) if(CLR_CMAKE_HOST_ANDROID) install (TARGETS System.Security.Cryptography.Native.Android-Static DESTINATION sharedFramework COMPONENT runtime) + install (TARGETS System.Security.Cryptography.Native.Android.JNIExport-Static DESTINATION sharedFramework COMPONENT runtime) endif() diff --git a/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.c b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.c index af87c04a4a031c..86a84320a8c728 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.c +++ b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.c @@ -1,11 +1,11 @@ -#include "pal_trust_manager.h" -#include +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. -static _Atomic RemoteCertificateValidationCallback verifyRemoteCertificate; +#include "pal_trust_manager.h" ARGS_NON_NULL_ALL void AndroidCryptoNative_RegisterRemoteCertificateValidationCallback(RemoteCertificateValidationCallback callback) { - atomic_store(&verifyRemoteCertificate, callback); + StoreRemoteVerificationCallback(callback); } ARGS_NON_NULL_ALL jobjectArray GetTrustManagers(JNIEnv* env, intptr_t sslStreamProxyHandle) @@ -28,10 +28,3 @@ ARGS_NON_NULL_ALL jobjectArray GetTrustManagers(JNIEnv* env, intptr_t sslStreamP return trustManagers; } -ARGS_NON_NULL_ALL jboolean Java_net_dot_android_crypto_DotnetProxyTrustManager_verifyRemoteCertificate( - JNIEnv* env, jobject thisHandle, jlong sslStreamProxyHandle) -{ - RemoteCertificateValidationCallback verify = atomic_load(&verifyRemoteCertificate); - abort_unless(verify, "verifyRemoteCertificate callback has not been registered"); - return verify((intptr_t)sslStreamProxyHandle); -} diff --git a/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.h b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.h index e4f09118492327..fa638408ddb009 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.h +++ b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager.h @@ -1,3 +1,6 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + #include "pal_jni.h" typedef bool (*RemoteCertificateValidationCallback)(intptr_t); @@ -6,5 +9,6 @@ PALEXPORT void AndroidCryptoNative_RegisterRemoteCertificateValidationCallback(R jobjectArray GetTrustManagers(JNIEnv* env, intptr_t sslStreamProxyHandle); +void StoreRemoteVerificationCallback (RemoteCertificateValidationCallback callback); JNIEXPORT jboolean JNICALL Java_net_dot_android_crypto_DotnetProxyTrustManager_verifyRemoteCertificate( JNIEnv *env, jobject thisHandle, jlong sslStreamProxyHandle); diff --git a/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager_jni_export.c b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager_jni_export.c new file mode 100644 index 00000000000000..ac80987d51ce57 --- /dev/null +++ b/src/native/libs/System.Security.Cryptography.Native.Android/pal_trust_manager_jni_export.c @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "pal_trust_manager.h" +#include + +static _Atomic RemoteCertificateValidationCallback verifyRemoteCertificate; + +void StoreRemoteVerificationCallback (RemoteCertificateValidationCallback callback) +{ + atomic_store(&verifyRemoteCertificate, callback); +} + +ARGS_NON_NULL_ALL jboolean Java_net_dot_android_crypto_DotnetProxyTrustManager_verifyRemoteCertificate( + JNIEnv* env, jobject thisHandle, jlong sslStreamProxyHandle) +{ + RemoteCertificateValidationCallback verify = atomic_load(&verifyRemoteCertificate); + abort_unless(verify, "verifyRemoteCertificate callback has not been registered"); + return verify((intptr_t)sslStreamProxyHandle); +}