forked from mono/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNegotiateStreamKerberosTest.cs
190 lines (157 loc) · 7.78 KB
/
NegotiateStreamKerberosTest.cs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Security;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.Net.Security.Tests
{
using Configuration = System.Net.Test.Common.Configuration;
public class NegotiateStreamKerberosTest
{
public static bool IsServerAndDomainAvailable =>
Capability.IsDomainAvailable() && Capability.IsNegotiateServerAvailable();
public static IEnumerable<object[]> GoodCredentialsData
{
get
{
yield return new object[] { CredentialCache.DefaultNetworkCredentials };
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
Configuration.Security.ActiveDirectoryUserPassword,
Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
AsSecureString(Configuration.Security.ActiveDirectoryUserPassword),
Configuration.Security.ActiveDirectoryName) };
// Anonymous (with domain name).
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
(string)null,
Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
(SecureString)null,
Configuration.Security.ActiveDirectoryName) };
// Anonymous (without domain).
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
(string)null,
null) };
yield return new object[] { new NetworkCredential(
Configuration.Security.ActiveDirectoryUserName,
(SecureString)null,
null) };
}
}
public static IEnumerable<object[]> BadCredentialsData
{
get
{
yield return new object[] { new NetworkCredential(null, (string)null, Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(null, (SecureString)null, Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(null, (string)null, null) };
yield return new object[] { new NetworkCredential(null, (SecureString)null, null) };
yield return new object[] { new NetworkCredential(
"baduser",
(string)null,
Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(
"baduser",
(SecureString)null,
Configuration.Security.ActiveDirectoryName) };
yield return new object[] { new NetworkCredential(
"baduser",
AsSecureString("badpassword"),
Configuration.Security.ActiveDirectoryName) };
}
}
[OuterLoop]
[ConditionalTheory(nameof(IsServerAndDomainAvailable))]
[MemberData(nameof(GoodCredentialsData))]
public async Task NegotiateStream_AuthenticationRemote_Success(object credentialObject)
{
var credential = (NetworkCredential)credentialObject;
await VerifyAuthentication(credential);
}
[OuterLoop]
[ConditionalTheory(nameof(IsServerAndDomainAvailable))]
[MemberData(nameof(BadCredentialsData))]
public async Task NegotiateStream_AuthenticationRemote_Fails(object credentialObject)
{
var credential = (NetworkCredential)credentialObject;
await Assert.ThrowsAsync<AuthenticationException>(() => VerifyAuthentication(credential));
}
private async Task VerifyAuthentication(NetworkCredential credential)
{
string serverName = Configuration.Security.NegotiateServer.Host;
int port = Configuration.Security.NegotiateServer.Port;
string serverSPN = "HOST/" + serverName;
bool isLocalhost = await IsLocalHost(serverName);
string expectedAuthenticationType = "Kerberos";
bool mutualAuthenitcated = true;
if (credential == CredentialCache.DefaultNetworkCredentials && isLocalhost)
{
expectedAuthenticationType = "NTLM";
}
else if (credential != CredentialCache.DefaultNetworkCredentials &&
(string.IsNullOrEmpty(credential.UserName) || string.IsNullOrEmpty(credential.Password)))
{
// Anonymous authentication.
expectedAuthenticationType = "NTLM";
mutualAuthenitcated = false;
}
using (var client = new TcpClient())
{
await client.ConnectAsync(serverName, port);
NetworkStream clientStream = client.GetStream();
using (var auth = new NegotiateStream(clientStream, leaveInnerStreamOpen:false))
{
await auth.AuthenticateAsClientAsync(
credential,
serverSPN,
ProtectionLevel.EncryptAndSign,
System.Security.Principal.TokenImpersonationLevel.Identification);
Assert.Equal(expectedAuthenticationType, auth.RemoteIdentity.AuthenticationType);
Assert.Equal(serverSPN, auth.RemoteIdentity.Name);
Assert.Equal(true, auth.IsAuthenticated);
Assert.Equal(true, auth.IsEncrypted);
Assert.Equal(mutualAuthenitcated, auth.IsMutuallyAuthenticated);
Assert.Equal(true, auth.IsSigned);
// Send a message to the server. Encode the test data into a byte array.
byte[] message = Encoding.UTF8.GetBytes("Hello from the client.");
await auth.WriteAsync(message, 0, message.Length);
}
}
}
private async static Task<bool> IsLocalHost(string hostname)
{
IPAddress[] hostAddresses = await Dns.GetHostAddressesAsync(hostname);
IPAddress[] localHostAddresses = await Dns.GetHostAddressesAsync(Dns.GetHostName());
// Note: This returns 127.0.0.1 and ::1 but 127.0.0.0/8 which most systems consider localhost.
IPAddress[] loopbackAddresses = await Dns.GetHostAddressesAsync("localhost");
int isLocalHost = hostAddresses.Intersect(localHostAddresses).Count();
int isLoopback = hostAddresses.Intersect(loopbackAddresses).Count();
return isLocalHost + isLoopback > 0;
}
private static SecureString AsSecureString(string str)
{
SecureString secureString = new SecureString();
if (string.IsNullOrEmpty(str))
{
return secureString;
}
foreach (char ch in str)
{
secureString.AppendChar(ch);
}
return secureString;
}
}
}