Skip to content

Commit 189105a

Browse files
Detect presence of OpenSSL for AEAD algorithms on macOS
This makes the IsSupported values for AesCcm and AesGcm return false on macOS when the OpenSSL shim library can't find a suitable OpenSSL, and makes AesCcm/AesGcm throw an informative exception in those cases. Additionally it fixes the IsSupported on ChaCha/Poly to not tear down the process when OpenSSL isn't found on macOS. Co-authored-by: Filip Navara <filip.navara@gmail.com>
1 parent a1517c5 commit 189105a

File tree

21 files changed

+125
-16
lines changed

21 files changed

+125
-16
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static class OpenSslNoInit
10+
{
11+
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_OpenSslAvailable")]
12+
private static extern int OpenSslAvailable();
13+
14+
private static readonly Lazy<bool> s_openSslAvailable =
15+
new Lazy<bool>(() => OpenSslAvailable() != 0);
16+
17+
internal static bool OpenSslIsAvailable => s_openSslAvailable.Value;
18+
}
19+
}

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public static string LibcVersion
101101
}
102102
}
103103

104+
public static bool OpenSslPresentOnSystem => !IsBrowser && Interop.OpenSslNoInit.OpenSslIsAvailable;
105+
104106
private static Version s_opensslVersion;
105107
private static Version GetOpenSslVersion()
106108
{

src/libraries/Common/tests/TestUtilities/TestUtilities.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
<ItemGroup>
6565
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs"
6666
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs" />
67+
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs"
68+
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs" />
6769
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs"
6870
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs" />
6971
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslGetProtocolSupport.cs"

src/libraries/Native/Unix/System.Security.Cryptography.Native/entrypoints.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ static const Entry s_cryptoNative[] =
213213
DllImportEntry(CryptoNative_ObjTxt2Obj)
214214
DllImportEntry(CryptoNative_OcspRequestDestroy)
215215
DllImportEntry(CryptoNative_OcspResponseDestroy)
216+
DllImportEntry(CryptoNative_OpenSslAvailable)
216217
DllImportEntry(CryptoNative_Pbkdf2)
217218
DllImportEntry(CryptoNative_PemReadBioPkcs7)
218219
DllImportEntry(CryptoNative_PemReadBioX509Crl)

src/libraries/Native/Unix/System.Security.Cryptography.Native/openssl.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,17 @@ static int32_t EnsureOpenSsl11Initialized()
13191319

13201320
#endif
13211321

1322+
int32_t CryptoNative_OpenSslAvailable()
1323+
{
1324+
#ifdef FEATURE_DISTRO_AGNOSTIC_SSL
1325+
// OpenLibrary will attempt to open libssl. DlOpen will handle
1326+
// the case of it already being open and dlclose the duplicate
1327+
return OpenLibrary();
1328+
#else
1329+
return 1;
1330+
#endif
1331+
}
1332+
13221333
int32_t CryptoNative_EnsureOpenSslInitialized()
13231334
{
13241335
// If portable then decide which OpenSSL we are, and call the right one.

src/libraries/Native/Unix/System.Security.Cryptography.Native/openssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ PALEXPORT int32_t CryptoNative_EnsureOpenSslInitialized(void);
7474
PALEXPORT int64_t CryptoNative_OpenSslVersionNumber(void);
7575

7676
PALEXPORT void CryptoNative_RegisterLegacyAlgorithms(void);
77+
78+
PALEXPORT int32_t CryptoNative_OpenSslAvailable(void);

src/libraries/Native/Unix/System.Security.Cryptography.Native/opensslshim.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <assert.h>
66
#include <dlfcn.h>
77
#include <stdio.h>
8-
#include <stdbool.h>
98
#include <string.h>
109

1110
#include "opensslshim.h"
@@ -51,7 +50,7 @@ static void DlOpen(const char* libraryName)
5150
}
5251
}
5352

54-
static bool OpenLibrary()
53+
int OpenLibrary()
5554
{
5655
// If there is an override of the version specified using the CLR_OPENSSL_VERSION_OVERRIDE
5756
// env variable, try to load that first.
@@ -124,7 +123,14 @@ static bool OpenLibrary()
124123
DlOpen(MAKELIB("8"));
125124
}
126125

127-
return libssl != NULL;
126+
if (libssl != NULL)
127+
{
128+
return 1;
129+
}
130+
else
131+
{
132+
return 0;
133+
}
128134
}
129135

130136
void InitializeOpenSSLShim(void)

src/libraries/Native/Unix/System.Security.Cryptography.Native/opensslshim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void ERR_put_error(int32_t lib, int32_t func, int32_t reason, const char* file,
9595
#define NEED_OPENSSL_1_1 true
9696
#define NEED_OPENSSL_3_0 true
9797

98+
int OpenLibrary(void);
9899
void InitializeOpenSSLShim(void);
99100

100101
#if !HAVE_OPENSSL_EC2M

src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@
586586
Link="Common\System\Security\Cryptography\ECDiffieHellmanDerivation.cs" />
587587
</ItemGroup>
588588
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true'">
589+
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs"
590+
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs" />
589591
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.EVP.Cipher.cs"
590592
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.EVP.Cipher.cs" />
591593
<Compile Include="System\Security\Cryptography\AesCcm.Unix.cs" />

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.Android.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public sealed partial class AesCcm
1111
{
1212
private byte[] _key;
1313

14+
public static bool IsSupported => true;
15+
1416
[MemberNotNull(nameof(_key))]
1517
private void ImportKey(ReadOnlySpan<byte> key)
1618
{

0 commit comments

Comments
 (0)