forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientAsyncAuthenticateTest.cs
More file actions
177 lines (151 loc) · 7.82 KB
/
ClientAsyncAuthenticateTest.cs
File metadata and controls
177 lines (151 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Security.Tests
{
public class ClientAsyncAuthenticateTest
{
private readonly ITestOutputHelper _log;
public ClientAsyncAuthenticateTest(ITestOutputHelper output)
{
_log = output;
}
[Fact]
public async Task ClientAsyncAuthenticate_ServerRequireEncryption_ConnectWithEncryption()
{
await ClientAsyncSslHelper(EncryptionPolicy.RequireEncryption);
}
[Fact]
public async Task ClientAsyncAuthenticate_ConnectionInfoInCallback_DoesNotThrow()
{
await ClientAsyncSslHelper(EncryptionPolicy.RequireEncryption, SslProtocols.Tls12, SslProtocolSupport.DefaultSslProtocols, AllowAnyServerCertificateAndVerifyConnectionInfo);
}
[Fact]
public async Task ClientAsyncAuthenticate_ServerNoEncryption_NoConnect()
{
// Don't use Tls13 since we are trying to use NullEncryption
await Assert.ThrowsAsync<AuthenticationException>(
() => ClientAsyncSslHelper(
EncryptionPolicy.NoEncryption,
SslProtocolSupport.DefaultSslProtocols, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12));
}
[Theory]
[ClassData(typeof(SslProtocolSupport.SupportedSslProtocolsTestData))]
public async Task ClientAsyncAuthenticate_EachSupportedProtocol_Success(SslProtocols protocol)
{
await ClientAsyncSslHelper(protocol, protocol);
}
[Theory]
[MemberData(nameof(ProtocolMismatchData))]
public async Task ClientAsyncAuthenticate_MismatchProtocols_Fails(
SslProtocols serverProtocol,
SslProtocols clientProtocol,
Type expectedException)
{
Exception e = await Record.ExceptionAsync(() => ClientAsyncSslHelper(serverProtocol, clientProtocol));
Assert.NotNull(e);
Assert.IsAssignableFrom(expectedException, e);
}
[Fact]
public async Task ClientAsyncAuthenticate_AllServerAllClient_Success()
{
await ClientAsyncSslHelper(
SslProtocolSupport.SupportedSslProtocols,
SslProtocolSupport.SupportedSslProtocols);
}
[Theory]
[ClassData(typeof(SslProtocolSupport.SupportedSslProtocolsTestData))]
public async Task ClientAsyncAuthenticate_AllServerVsIndividualClientSupportedProtocols_Success(
SslProtocols clientProtocol)
{
await ClientAsyncSslHelper(clientProtocol, SslProtocolSupport.SupportedSslProtocols);
}
[Theory]
[ClassData(typeof(SslProtocolSupport.SupportedSslProtocolsTestData))]
public async Task ClientAsyncAuthenticate_IndividualServerVsAllClientSupportedProtocols_Success(
SslProtocols serverProtocol)
{
await ClientAsyncSslHelper(SslProtocolSupport.SupportedSslProtocols, serverProtocol);
// Cached Tls creds fail when used against Tls servers of higher versions.
// Servers are not expected to dynamically change versions.
}
public static IEnumerable<object[]> ProtocolMismatchData()
{
#pragma warning disable 0618
yield return new object[] { SslProtocols.Ssl2, SslProtocols.Ssl3, typeof(Exception) };
yield return new object[] { SslProtocols.Ssl2, SslProtocols.Tls12, typeof(Exception) };
yield return new object[] { SslProtocols.Ssl3, SslProtocols.Tls12, typeof(Exception) };
#pragma warning restore 0618
yield return new object[] { SslProtocols.Tls, SslProtocols.Tls11, typeof(AuthenticationException) };
yield return new object[] { SslProtocols.Tls, SslProtocols.Tls12, typeof(AuthenticationException) };
yield return new object[] { SslProtocols.Tls11, SslProtocols.Tls, typeof(AuthenticationException) };
yield return new object[] { SslProtocols.Tls12, SslProtocols.Tls, typeof(AuthenticationException) };
yield return new object[] { SslProtocols.Tls12, SslProtocols.Tls11, typeof(AuthenticationException) };
yield return new object[] { SslProtocols.Tls11, SslProtocols.Tls12, typeof(AuthenticationException) };
}
#region Helpers
private Task ClientAsyncSslHelper(EncryptionPolicy encryptionPolicy)
{
return ClientAsyncSslHelper(encryptionPolicy, SslProtocolSupport.DefaultSslProtocols, SslProtocolSupport.DefaultSslProtocols);
}
private Task ClientAsyncSslHelper(SslProtocols clientSslProtocols, SslProtocols serverSslProtocols)
{
return ClientAsyncSslHelper(EncryptionPolicy.RequireEncryption, clientSslProtocols, serverSslProtocols);
}
private async Task ClientAsyncSslHelper(
EncryptionPolicy encryptionPolicy,
SslProtocols clientSslProtocols,
SslProtocols serverSslProtocols,
RemoteCertificateValidationCallback certificateCallback = null)
{
_log.WriteLine("Server: " + serverSslProtocols + "; Client: " + clientSslProtocols);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);
using (var server = new DummyTcpServer(endPoint, encryptionPolicy))
using (var client = new TcpClient())
{
server.SslProtocols = serverSslProtocols;
// Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378
string serverName = TestHelper.GetTestSNIName(nameof(ClientAsyncSslHelper), clientSslProtocols, serverSslProtocols);
await client.ConnectAsync(server.RemoteEndPoint.Address, server.RemoteEndPoint.Port);
using (SslStream sslStream = new SslStream(client.GetStream(), false, certificateCallback != null ? certificateCallback : AllowAnyServerCertificate, null))
{
Task clientAuthTask = sslStream.AuthenticateAsClientAsync(serverName, null, clientSslProtocols, false);
await clientAuthTask.WaitAsync(TestConfiguration.PassingTestTimeout);
_log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength",
server.RemoteEndPoint, sslStream.CipherAlgorithm, sslStream.CipherStrength);
Assert.True(sslStream.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
Assert.True(sslStream.CipherStrength > 0, "Cipher strength should be greater than 0");
}
}
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
private bool AllowAnyServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true; // allow everything
}
private bool AllowAnyServerCertificateAndVerifyConnectionInfo(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
SslStream stream = (SslStream)sender;
Assert.NotEqual(SslProtocols.None, stream.SslProtocol);
Assert.NotEqual(CipherAlgorithmType.None, stream.CipherAlgorithm);
return true; // allow everything
}
#endregion Helpers
}
}