Add basic OpenSSL support to Android (#35893)
authorEgor Bogatov <egorbo@gmail.com>
Wed, 6 May 2020 18:13:33 +0000 (21:13 +0300)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 18:13:33 +0000 (20:13 +0200)
Temp solution to enable `System.Security.Cryptography.Native` for Android while we figure out how to build/consume it properly.

#### Steps to enable:
1) Download and unzip `https://maven.google.com/com/android/ndk/thirdparty/openssl/1.1.1g-alpha-1/openssl-1.1.1g-alpha-1.aar`
2) Set these env variables:
```
export AndroidOpenSslHeaders="/Users/egorbo/prj/openssl-1.1.1g-alpha-1.aar/prefab/modules/ssl/include"
export AndroidOpenSslCryptoLib="/Users/egorbo/prj/openssl-1.1.1g-alpha-1.aar/prefab/modules/crypto/libs/android.x86_64/libcrypto.so"
export AndroidOpenSslLib="/Users/egorbo/prj/openssl-1.1.1g-alpha-1.aar/prefab/modules/ssl/libs/android.x86_64/libssl.so"
```
(make sure you use the right ABI in the paths, e.g. `android.x86_64` for `-arch x64` or `android.arm64-v8a` for `-arch arm64`)

3) build repo normally, e.g.
```
./build.sh -os Android -subset Mono+Libs
```

I am preparing a PR to update the docs and will include these temp hints.

eng/testing/tests.targets
src/libraries/Native/Unix/CMakeLists.txt
src/libraries/Native/Unix/System.Security.Cryptography.Native/CMakeLists.txt
src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
src/mono/netcore/sample/Android/Program.csproj

index faaffc4..473b631 100644 (file)
     <Copy SourceFiles="@(AndroidTestRunnerBinaries)" DestinationFolder="$(BundleDir)\%(RecursiveDir)" SkipUnchangedFiles="true"/>
     <Copy SourceFiles="@(BclBinaries)" DestinationFolder="$(BundleDir)\%(RecursiveDir)" SkipUnchangedFiles="true"/>
 
+    <!-- TEMP: consume OpenSSL binaries from external sources via env. variables -->
+    <Copy Condition="'$(AndroidOpenSslCryptoLib)' != ''"
+          SourceFiles="$(AndroidOpenSslCryptoLib)"
+          DestinationFolder="$(BundleDir)" SkipUnchangedFiles="true"/>
+    <Copy Condition="'$(AndroidOpenSslLib)' != ''"
+          SourceFiles="$(AndroidOpenSslLib)"
+          DestinationFolder="$(BundleDir)" SkipUnchangedFiles="true"/>
+
     <WriteLinesToFile File="$(BundleDir)\xunit-excludes.txt" Lines="$(_withoutCategories.Replace(';', '%0dcategory='))" />
 
     <AndroidAppBuilderTask 
index f14ada6..143c990 100644 (file)
@@ -215,7 +215,9 @@ elseif(CLR_CMAKE_TARGET_TVOS)
 elseif(CLR_CMAKE_TARGET_ANDROID AND NOT CROSS_ROOTFS)
     add_subdirectory(System.Globalization.Native)
     #add_subdirectory(System.Net.Security.Native) # TODO: reenable
-    #add_subdirectory(System.Security.Cryptography.Native) # TODO: reenable
+    if (NOT "$ENV{AndroidOpenSslHeaders}" STREQUAL "")
+        add_subdirectory(System.Security.Cryptography.Native)
+    endif()
 else()
     add_subdirectory(System.Globalization.Native)
     add_subdirectory(System.Net.Security.Native)
index 7d895aa..60d933d 100644 (file)
@@ -12,7 +12,16 @@ if(CMAKE_STATIC_LIB_LINK)
    set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
 endif(CMAKE_STATIC_LIB_LINK)
 
-find_package(OpenSSL)
+if(CLR_CMAKE_TARGET_ANDROID AND NOT CROSS_ROOTFS)
+    # TEMP: consume OpenSSL dependencies from external sources via env. variables
+    set(OPENSSL_FOUND 1)
+    set(OPENSSL_INCLUDE_DIR $ENV{AndroidOpenSslHeaders})
+    set(OPENSSL_CRYPTO_LIBRARY $ENV{AndroidOpenSslCryptoLib})
+    set(OPENSSL_SSL_LIBRARY $ENV{AndroidOpenSslLib})
+else()
+    find_package(OpenSSL)
+endif()
+
 if(NOT OPENSSL_FOUND)
     message(FATAL_ERROR "!!! Cannot find libssl and System.Security.Cryptography.Native cannot build without it. Try installing libssl-dev (or the appropriate package for your platform) !!!. See the requirements document for your specific operating system: https://github.com/dotnet/runtime/tree/master/docs/workflow/requirements.")
 endif(NOT OPENSSL_FOUND)
index 3b2959c..0a126fc 100644 (file)
              Link="Common\System\Security\Cryptography\RSAOpenSsl.cs" />
     <Compile Include="Internal\Cryptography\AesImplementation.Unix.cs" />
     <Compile Include="Internal\Cryptography\DesImplementation.Unix.cs" />
-    <Compile Condition="'$(TargetsAndroid)' != 'true'" Include="Internal\Cryptography\HashProviderDispenser.Unix.cs" />
-    <Compile Condition="'$(TargetsAndroid)' == 'true'" Include="Internal\Cryptography\HashProviderDispenser.Android.cs" />
+    <Compile Condition="'$(TargetsAndroid)' != 'true' or '$(AndroidOpenSslHeaders)' != ''" Include="Internal\Cryptography\HashProviderDispenser.Unix.cs" />
+    <Compile Condition="'$(TargetsAndroid)' == 'true' and '$(AndroidOpenSslHeaders)' == ''" Include="Internal\Cryptography\HashProviderDispenser.Android.cs" />
     <Compile Include="Internal\Cryptography\OpenSslCipher.cs" />
     <Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.Unix.cs" />
     <Compile Include="Internal\Cryptography\RC2Implementation.Unix.cs" />
index 5c22424..51a153d 100644 (file)
     <RemoveDir Directories="$(BundleDir)" />
     <Copy SourceFiles="@(AppBinaries)" DestinationFolder="$(BundleDir)" SkipUnchangedFiles="true"/>
     <Copy SourceFiles="@(BclBinaries)" DestinationFolder="$(BundleDir)\%(RecursiveDir)" SkipUnchangedFiles="true"/>
+
+    <!-- TEMP: consume OpenSSL binaries from external sources via env. variables -->
+    <Copy Condition="'$(AndroidOpenSslCryptoLib)' != ''"
+          SourceFiles="$(AndroidOpenSslCryptoLib)"
+          DestinationFolder="$(BundleDir)" SkipUnchangedFiles="true"/>
+    <Copy Condition="'$(AndroidOpenSslLib)' != ''"
+          SourceFiles="$(AndroidOpenSslLib)"
+          DestinationFolder="$(BundleDir)" SkipUnchangedFiles="true"/>
+
     <AndroidAppBuilderTask 
         Abi="$(AndroidAbi)"
         ProjectName="HelloAndroid"