Skip to content

Commit 11c01fb

Browse files
authored
Merge pull request dotnet#25637 from Priya91/fixalpnlinux
Add logic to skip alpn tests where unsupported.
2 parents 939ffe6 + 939e22c commit 11c01fb

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal static partial class Interop
9+
{
10+
internal static partial class OpenSsl
11+
{
12+
private static Version s_opensslVersion;
13+
14+
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_SSLEayVersion")]
15+
private static extern string OpenSslVersionDescription();
16+
17+
internal static Version OpenSslVersion
18+
{
19+
get
20+
{
21+
if (s_opensslVersion == null)
22+
{
23+
const string OpenSSL = "OpenSSL ";
24+
25+
// Skip OpenSSL part, and get the version string of format x.y.z
26+
if (!Version.TryParse(OpenSslVersionDescription().AsReadOnlySpan().Slice(OpenSSL.Length, 5), out s_opensslVersion))
27+
{
28+
s_opensslVersion = new Version(0, 0, 0);
29+
}
30+
}
31+
32+
return s_opensslVersion;
33+
}
34+
}
35+
}
36+
}

src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static partial class PlatformDetection
7777
public static bool IsNotRedHatFamily6 { get { throw null; } }
7878
public static bool IsUap { get { throw null; } }
7979
public static Version ICUVersion { get { return null; } }
80+
public static Version OpenSslVersion { get { return null; } }
8081
public static bool IsUbuntu { get { throw null; } }
8182
public static bool IsUbuntu1404 { get { throw null; } }
8283
public static bool IsUbuntu1604 { get { throw null; } }

src/CoreFx.Private.TestUtilities/src/CoreFx.Private.TestUtilities.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Compile Include="System\TheoryExtensions.cs" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Reference Include="System.Memory" />
4546
<Reference Include="Microsoft.Win32.Registry" />
4647
<Reference Include="System.Runtime" />
4748
<Reference Include="System.IO.FileSystem" />
@@ -93,6 +94,9 @@
9394
<Compile Include="System\AdminHelpers.Windows.cs" />
9495
</ItemGroup>
9596
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
97+
<Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs">
98+
<Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs</Link>
99+
</Compile>
96100
<Compile Include="System\AdminHelpers.Unix.cs" />
97101
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetEUid.cs">
98102
<Link>Common\Interop\Unix\Interop.GetEUid.cs</Link>
@@ -115,4 +119,4 @@
115119
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
116120
</ItemGroup>
117121
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
118-
</Project>
122+
</Project>

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.Unix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static partial class PlatformDetection
5252

5353
public static Version OSXVersion { get; } = ToVersion(Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion);
5454

55+
public static Version OpenSslVersion => RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? Interop.OpenSsl.OpenSslVersion : throw new PlatformNotSupportedException();
56+
5557
public static string GetDistroVersionString()
5658
{
5759
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.Windows.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace System
1515
public static partial class PlatformDetection
1616
{
1717
public static Version OSXVersion => throw new PlatformNotSupportedException();
18+
public static Version OpenSslVersion => throw new PlatformNotSupportedException();
1819
public static bool IsSuperUser => throw new PlatformNotSupportedException();
1920
public static bool IsCentos6 => false;
2021
public static bool IsOpenSUSE => false;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,3 +1389,18 @@ extern "C" int32_t CryptoNative_EnsureOpenSslInitialized()
13891389
pthread_mutex_unlock(&g_initLock);
13901390
return ret;
13911391
}
1392+
1393+
/*
1394+
Function:
1395+
SSLEayVersion
1396+
1397+
Gets the version of openssl library.
1398+
1399+
Return values:
1400+
Textual description of the version on success.
1401+
"not available" string on failure.
1402+
*/
1403+
extern "C" char* CryptoNative_SSLEayVersion()
1404+
{
1405+
return strdup(SSLeay_version(SSLEAY_VERSION));
1406+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <openssl/asn1.h>
1515
#include <openssl/bio.h>
1616
#include <openssl/bn.h>
17+
#include <openssl/crypto.h>
1718
#include <openssl/dsa.h>
1819
#include <openssl/ecdsa.h>
1920
#include <openssl/ec.h>
@@ -297,6 +298,7 @@ void SSL_get0_alpn_selected(const SSL* ssl, const unsigned char** protocol, unsi
297298
PER_FUNCTION_BLOCK(SSL_set_connect_state, true) \
298299
PER_FUNCTION_BLOCK(SSL_shutdown, true) \
299300
PER_FUNCTION_BLOCK(SSL_state, true) \
301+
PER_FUNCTION_BLOCK(SSLeay_version, true) \
300302
PER_FUNCTION_BLOCK(SSLv23_method, true) \
301303
PER_FUNCTION_BLOCK(SSL_write, true) \
302304
PER_FUNCTION_BLOCK(TLSv1_1_method, true) \
@@ -590,6 +592,7 @@ FOR_ALL_OPENSSL_FUNCTIONS
590592
#define SSL_set_connect_state SSL_set_connect_state_ptr
591593
#define SSL_shutdown SSL_shutdown_ptr
592594
#define SSL_state SSL_state_ptr
595+
#define SSLeay_version SSLeay_version_ptr
593596
#define SSLv23_method SSLv23_method_ptr
594597
#define SSL_write SSL_write_ptr
595598
#define TLSv1_1_method TLSv1_1_method_ptr

src/System.Net.Security/tests/FunctionalTests/SslStreamAlpnTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ namespace System.Net.Security.Tests
2222

2323
public class SslStreamAlpnTests
2424
{
25+
// Windows - Schannel supports alpn from win8 and higher.
26+
// Linux - OpenSsl supports alpn from openssl 1.0.2 and higher.
27+
// OSX - SecureTransport doesn't expose alpn APIs.
28+
private static bool BackendSupportsAlpn => (PlatformDetection.IsWindows && !PlatformDetection.IsWindows7) ||
29+
(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) &&
30+
(PlatformDetection.OpenSslVersion.Major >= 1 && (PlatformDetection.OpenSslVersion.Minor >= 1 || PlatformDetection.OpenSslVersion.Build >= 2)));
31+
2532
private async Task DoHandshakeWithOptions(SslStream clientSslStream, SslStream serverSslStream, SslClientAuthenticationOptions clientOptions, SslServerAuthenticationOptions serverOptions)
2633
{
2734
using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
@@ -147,8 +154,7 @@ public async Task SslStream_StreamToStream_Alpn_NonMatchingProtocols_Fail()
147154
};
148155

149156
// Test alpn failure only on platforms that supports ALPN.
150-
if ((RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !(PlatformDetection.IsUbuntu1404 || PlatformDetection.IsDebian8 || PlatformDetection.IsCentos6)) ||
151-
(PlatformDetection.IsWindows && !PlatformDetection.IsWindows7))
157+
if (BackendSupportsAlpn)
152158
{
153159
Task t1 = Assert.ThrowsAsync<IOException>(() => clientStream.AuthenticateAsClientAsync(clientOptions, CancellationToken.None));
154160
try
@@ -193,14 +199,9 @@ internal static IEnumerable<object[]> Alpn_TestData()
193199
}
194200
else
195201
{
196-
// Works on linux distros with openssl 1.0.2, CI machines Ubuntu14.04 and Debian 87 don't have openssl 1.0.2
197-
// Works on Windows OSes > 7.0
198-
bool featureWorks = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !(PlatformDetection.IsUbuntu1404 || PlatformDetection.IsDebian8 || PlatformDetection.IsCentos6)) ||
199-
(PlatformDetection.IsWindows && !PlatformDetection.IsWindows7);
200-
201-
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http2 }, featureWorks ? SslApplicationProtocol.Http2 : default };
202-
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, featureWorks ? SslApplicationProtocol.Http11 : default };
203-
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, featureWorks ? SslApplicationProtocol.Http11 : default };
202+
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http2 }, BackendSupportsAlpn ? SslApplicationProtocol.Http2 : default };
203+
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, BackendSupportsAlpn ? SslApplicationProtocol.Http11 : default };
204+
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, BackendSupportsAlpn ? SslApplicationProtocol.Http11 : default };
204205
yield return new object[] { null, new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, default(SslApplicationProtocol) };
205206
yield return new object[] { new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 }, null, default(SslApplicationProtocol) };
206207
yield return new object[] { null, null, default(SslApplicationProtocol) };

0 commit comments

Comments
 (0)