Skip to content

Commit

Permalink
Add basic OpenSSL support to Android (dotnet#35893)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EgorBo authored May 6, 2020
1 parent 21e8298 commit 287a58a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions eng/testing/tests.targets
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
<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
Expand Down
4 changes: 3 additions & 1 deletion src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@
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" />
Expand Down
9 changes: 9 additions & 0 deletions src/mono/netcore/sample/Android/Program.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
<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"
Expand Down

0 comments on commit 287a58a

Please sign in to comment.