/
ServiceNameCollection.cs
259 lines (217 loc) · 9.51 KB
/
ServiceNameCollection.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
namespace System.Security.Authentication.ExtendedProtection
{
public class ServiceNameCollection : ReadOnlyCollectionBase
{
public ServiceNameCollection(ICollection items)
{
ArgumentNullException.ThrowIfNull(items);
// Normalize and filter for duplicates.
AddIfNew(items, expectStrings: true);
}
/// <summary>
/// Merges <paramref name="list"/> and <paramref name="serviceName"/> into a new collection.
/// </summary>
private ServiceNameCollection(IList list, string serviceName)
: this(list, additionalCapacity: 1)
{
AddIfNew(serviceName);
}
/// <summary>
/// Merges <paramref name="list"/> and <paramref name="serviceNames"/> into a new collection.
/// </summary>
private ServiceNameCollection(IList list, IEnumerable serviceNames)
: this(list, additionalCapacity: GetCountOrOne(serviceNames))
{
// We have a pretty bad performance here: O(n^2), but since service name lists should
// be small (<<50) and Merge() should not be called frequently, this shouldn't be an issue.
AddIfNew(serviceNames, expectStrings: false);
}
private ServiceNameCollection(IList list, int additionalCapacity)
{
Debug.Assert(list != null);
Debug.Assert(additionalCapacity >= 0);
foreach (string? item in list)
{
InnerList.Add(item);
}
}
public bool Contains(string? searchServiceName)
{
string? searchName = NormalizeServiceName(searchServiceName);
foreach (string serviceName in InnerList)
{
if (string.Equals(serviceName, searchName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
public ServiceNameCollection Merge(string serviceName) => new ServiceNameCollection(InnerList, serviceName);
public ServiceNameCollection Merge(IEnumerable serviceNames) => new ServiceNameCollection(InnerList, serviceNames);
/// <summary>
/// Normalize, check for duplicates, and add each unique value.
/// </summary>
private void AddIfNew(IEnumerable serviceNames, bool expectStrings)
{
List<string>? list = serviceNames as List<string>;
if (list != null)
{
AddIfNew(list);
return;
}
ServiceNameCollection? snc = serviceNames as ServiceNameCollection;
if (snc != null)
{
AddIfNew(snc.InnerList);
return;
}
// NullReferenceException is thrown when serviceNames is null,
// which is consistent with the behavior of the .NET Framework.
foreach (object item in serviceNames)
{
// To match the behavior of the .NET Framework, when an item
// in the collection is not a string:
// - Throw InvalidCastException when expectStrings is true.
// - Throw ArgumentException when expectStrings is false.
AddIfNew(expectStrings ? (string)item : (item as string)!);
}
}
/// <summary>
/// Normalize, check for duplicates, and add each unique value.
/// </summary>
private void AddIfNew(List<string> serviceNames)
{
Debug.Assert(serviceNames != null);
foreach (string serviceName in serviceNames)
{
AddIfNew(serviceName);
}
}
/// <summary>
/// Normalize, check for duplicates, and add each unique value.
/// </summary>
private void AddIfNew(IList serviceNames)
{
Debug.Assert(serviceNames != null);
foreach (string serviceName in serviceNames)
{
AddIfNew(serviceName);
}
}
/// <summary>
/// Normalize, check for duplicates, and add if the value is unique.
/// </summary>
private void AddIfNew(string serviceName)
{
ArgumentException.ThrowIfNullOrEmpty(serviceName);
serviceName = NormalizeServiceName(serviceName);
if (!Contains(serviceName))
{
InnerList.Add(serviceName);
}
}
/// <summary>
/// Gets the collection Count, if available, otherwise 1.
/// </summary>
private static int GetCountOrOne(IEnumerable collection)
{
ICollection<string>? c = collection as ICollection<string>;
return c != null ? c.Count : 1;
}
// Normalizes any punycode to Unicode in an Service Name (SPN) host.
// If the algorithm fails at any point then the original input is returned.
// ServiceName is in one of the following forms:
// prefix/host
// prefix/host:port
// prefix/host/DistinguishedName
// prefix/host:port/DistinguishedName
[return: NotNullIfNotNull(nameof(inputServiceName))]
private static string? NormalizeServiceName(string? inputServiceName)
{
if (string.IsNullOrWhiteSpace(inputServiceName))
{
return inputServiceName;
}
// Separate out the prefix
int slashIndex = inputServiceName.IndexOf('/');
if (slashIndex < 0)
{
return inputServiceName;
}
string prefix = inputServiceName.Substring(0, slashIndex + 1); // Includes slash
string hostPortAndDistinguisher = inputServiceName.Substring(slashIndex + 1); // Excludes slash
if (string.IsNullOrWhiteSpace(hostPortAndDistinguisher))
{
return inputServiceName;
}
string host = hostPortAndDistinguisher;
string port = string.Empty;
string distinguisher = string.Empty;
// Check for the absence of a port or distinguisher.
UriHostNameType hostType = Uri.CheckHostName(hostPortAndDistinguisher);
if (hostType == UriHostNameType.Unknown)
{
string hostAndPort = hostPortAndDistinguisher;
// Check for distinguisher.
int nextSlashIndex = hostPortAndDistinguisher.IndexOf('/');
if (nextSlashIndex >= 0)
{
// host:port/distinguisher or host/distinguisher
hostAndPort = hostPortAndDistinguisher.Substring(0, nextSlashIndex); // Excludes Slash
distinguisher = hostPortAndDistinguisher.Substring(nextSlashIndex); // Includes Slash
host = hostAndPort; // We don't know if there is a port yet.
// No need to validate the distinguisher.
}
// Check for port.
int colonIndex = hostAndPort.LastIndexOf(':'); // Allow IPv6 addresses.
if (colonIndex >= 0)
{
// host:port
host = hostAndPort.Substring(0, colonIndex); // Excludes colon
port = hostAndPort.Substring(colonIndex + 1); // Excludes colon
// Loosely validate the port just to make sure it was a port and not something else.
if (!ushort.TryParse(port, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
{
return inputServiceName;
}
// Re-include the colon for the final output. Do not change the port format.
port = hostAndPort.Substring(colonIndex);
}
hostType = Uri.CheckHostName(host); // Re-validate the host.
}
if (hostType != UriHostNameType.Dns)
{
// UriHostNameType.IPv4, UriHostNameType.IPv6: Do not normalize IPv4/6 hosts.
// UriHostNameType.Basic: This is never returned by CheckHostName today
// UriHostNameType.Unknown: Nothing recognizable to normalize
// default Some new UriHostNameType?
return inputServiceName;
}
// Now we have a valid DNS host, normalize it.
Uri? constructedUri;
// We need to avoid any unexpected exceptions on this code path.
if (!Uri.TryCreate(UriScheme.Http + UriScheme.SchemeDelimiter + host, UriKind.Absolute, out constructedUri))
{
return inputServiceName;
}
string normalizedHost = constructedUri.GetComponents(
UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
string normalizedServiceName = prefix + normalizedHost + port + distinguisher;
// Don't return the new one unless we absolutely have to. It may have only changed casing.
if (string.Equals(inputServiceName, normalizedServiceName, StringComparison.OrdinalIgnoreCase))
{
return inputServiceName;
}
return normalizedServiceName;
}
}
}