| | | 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.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Net; |
| | | 10 | | |
| | | 11 | | namespace System.Security.Authentication.ExtendedProtection |
| | | 12 | | { |
| | | 13 | | public class ServiceNameCollection : ReadOnlyCollectionBase |
| | | 14 | | { |
| | 0 | 15 | | public ServiceNameCollection(ICollection items) |
| | 0 | 16 | | { |
| | 0 | 17 | | ArgumentNullException.ThrowIfNull(items); |
| | | 18 | | |
| | | 19 | | // Normalize and filter for duplicates. |
| | 0 | 20 | | AddIfNew(items, expectStrings: true); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Merges <paramref name="list"/> and <paramref name="serviceName"/> into a new collection. |
| | | 25 | | /// </summary> |
| | | 26 | | private ServiceNameCollection(IList list, string serviceName) |
| | 0 | 27 | | : this(list, additionalCapacity: 1) |
| | 0 | 28 | | { |
| | 0 | 29 | | AddIfNew(serviceName); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Merges <paramref name="list"/> and <paramref name="serviceNames"/> into a new collection. |
| | | 34 | | /// </summary> |
| | | 35 | | private ServiceNameCollection(IList list, IEnumerable serviceNames) |
| | 0 | 36 | | : this(list, additionalCapacity: GetCountOrOne(serviceNames)) |
| | 0 | 37 | | { |
| | | 38 | | // We have a pretty bad performance here: O(n^2), but since service name lists should |
| | | 39 | | // be small (<<50) and Merge() should not be called frequently, this shouldn't be an issue. |
| | 0 | 40 | | AddIfNew(serviceNames, expectStrings: false); |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | private ServiceNameCollection(IList list, int additionalCapacity) |
| | 0 | 44 | | { |
| | 0 | 45 | | Debug.Assert(list != null); |
| | 0 | 46 | | Debug.Assert(additionalCapacity >= 0); |
| | | 47 | | |
| | 0 | 48 | | foreach (string? item in list) |
| | 0 | 49 | | { |
| | 0 | 50 | | InnerList.Add(item); |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public bool Contains(string? searchServiceName) |
| | 0 | 55 | | { |
| | 0 | 56 | | string? searchName = NormalizeServiceName(searchServiceName); |
| | | 57 | | |
| | 0 | 58 | | foreach (string serviceName in InnerList) |
| | 0 | 59 | | { |
| | 0 | 60 | | if (string.Equals(serviceName, searchName, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 61 | | { |
| | 0 | 62 | | return true; |
| | | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return false; |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | public ServiceNameCollection Merge(string serviceName) => new ServiceNameCollection(InnerList, serviceName); |
| | | 70 | | |
| | 0 | 71 | | public ServiceNameCollection Merge(IEnumerable serviceNames) => new ServiceNameCollection(InnerList, serviceName |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Normalize, check for duplicates, and add each unique value. |
| | | 75 | | /// </summary> |
| | | 76 | | private void AddIfNew(IEnumerable serviceNames, bool expectStrings) |
| | 0 | 77 | | { |
| | 0 | 78 | | List<string>? list = serviceNames as List<string>; |
| | 0 | 79 | | if (list != null) |
| | 0 | 80 | | { |
| | 0 | 81 | | AddIfNew(list); |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | ServiceNameCollection? snc = serviceNames as ServiceNameCollection; |
| | 0 | 86 | | if (snc != null) |
| | 0 | 87 | | { |
| | 0 | 88 | | AddIfNew(snc.InnerList); |
| | 0 | 89 | | return; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // NullReferenceException is thrown when serviceNames is null, |
| | | 93 | | // which is consistent with the behavior of the .NET Framework. |
| | 0 | 94 | | foreach (object item in serviceNames) |
| | 0 | 95 | | { |
| | | 96 | | // To match the behavior of the .NET Framework, when an item |
| | | 97 | | // in the collection is not a string: |
| | | 98 | | // - Throw InvalidCastException when expectStrings is true. |
| | | 99 | | // - Throw ArgumentException when expectStrings is false. |
| | 0 | 100 | | AddIfNew(expectStrings ? (string)item : (item as string)!); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Normalize, check for duplicates, and add each unique value. |
| | | 106 | | /// </summary> |
| | | 107 | | private void AddIfNew(List<string> serviceNames) |
| | 0 | 108 | | { |
| | 0 | 109 | | Debug.Assert(serviceNames != null); |
| | | 110 | | |
| | 0 | 111 | | foreach (string serviceName in serviceNames) |
| | 0 | 112 | | { |
| | 0 | 113 | | AddIfNew(serviceName); |
| | 0 | 114 | | } |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Normalize, check for duplicates, and add each unique value. |
| | | 119 | | /// </summary> |
| | | 120 | | private void AddIfNew(IList serviceNames) |
| | 0 | 121 | | { |
| | 0 | 122 | | Debug.Assert(serviceNames != null); |
| | | 123 | | |
| | 0 | 124 | | foreach (string serviceName in serviceNames) |
| | 0 | 125 | | { |
| | 0 | 126 | | AddIfNew(serviceName); |
| | 0 | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Normalize, check for duplicates, and add if the value is unique. |
| | | 132 | | /// </summary> |
| | | 133 | | private void AddIfNew(string serviceName) |
| | 0 | 134 | | { |
| | 0 | 135 | | ArgumentException.ThrowIfNullOrEmpty(serviceName); |
| | | 136 | | |
| | 0 | 137 | | serviceName = NormalizeServiceName(serviceName); |
| | | 138 | | |
| | 0 | 139 | | if (!Contains(serviceName)) |
| | 0 | 140 | | { |
| | 0 | 141 | | InnerList.Add(serviceName); |
| | 0 | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets the collection Count, if available, otherwise 1. |
| | | 147 | | /// </summary> |
| | | 148 | | private static int GetCountOrOne(IEnumerable collection) |
| | 0 | 149 | | { |
| | 0 | 150 | | ICollection<string>? c = collection as ICollection<string>; |
| | 0 | 151 | | return c != null ? c.Count : 1; |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | // Normalizes any punycode to Unicode in an Service Name (SPN) host. |
| | | 155 | | // If the algorithm fails at any point then the original input is returned. |
| | | 156 | | // ServiceName is in one of the following forms: |
| | | 157 | | // prefix/host |
| | | 158 | | // prefix/host:port |
| | | 159 | | // prefix/host/DistinguishedName |
| | | 160 | | // prefix/host:port/DistinguishedName |
| | | 161 | | [return: NotNullIfNotNull(nameof(inputServiceName))] |
| | | 162 | | private static string? NormalizeServiceName(string? inputServiceName) |
| | 0 | 163 | | { |
| | 0 | 164 | | if (string.IsNullOrWhiteSpace(inputServiceName)) |
| | 0 | 165 | | { |
| | 0 | 166 | | return inputServiceName; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | // Separate out the prefix |
| | 0 | 170 | | int slashIndex = inputServiceName.IndexOf('/'); |
| | 0 | 171 | | if (slashIndex < 0) |
| | 0 | 172 | | { |
| | 0 | 173 | | return inputServiceName; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | ReadOnlySpan<char> prefix = inputServiceName.AsSpan(0, slashIndex + 1); // Includes slash |
| | 0 | 177 | | string hostPortAndDistinguisher = inputServiceName.Substring(slashIndex + 1); // Excludes slash |
| | | 178 | | |
| | 0 | 179 | | if (hostPortAndDistinguisher.Length == 0) |
| | 0 | 180 | | { |
| | 0 | 181 | | return inputServiceName; |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | ReadOnlySpan<char> host = hostPortAndDistinguisher; |
| | 0 | 185 | | ReadOnlySpan<char> port = default; |
| | 0 | 186 | | ReadOnlySpan<char> distinguisher = default; |
| | | 187 | | |
| | | 188 | | // Check for the absence of a port or distinguisher. |
| | 0 | 189 | | UriHostNameType hostType = Uri.CheckHostName(hostPortAndDistinguisher); |
| | 0 | 190 | | if (hostType == UriHostNameType.Unknown) |
| | 0 | 191 | | { |
| | 0 | 192 | | ReadOnlySpan<char> hostAndPort = hostPortAndDistinguisher; |
| | | 193 | | |
| | | 194 | | // Check for distinguisher. |
| | 0 | 195 | | int nextSlashIndex = hostPortAndDistinguisher.IndexOf('/'); |
| | 0 | 196 | | if (nextSlashIndex >= 0) |
| | 0 | 197 | | { |
| | | 198 | | // host:port/distinguisher or host/distinguisher |
| | 0 | 199 | | hostAndPort = hostPortAndDistinguisher.AsSpan(0, nextSlashIndex); // Excludes Slash |
| | 0 | 200 | | distinguisher = hostPortAndDistinguisher.AsSpan(nextSlashIndex); // Includes Slash |
| | 0 | 201 | | host = hostAndPort; // We don't know if there is a port yet. |
| | | 202 | | // No need to validate the distinguisher. |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | // Check for port. |
| | 0 | 206 | | int colonIndex = hostAndPort.LastIndexOf(':'); // Allow IPv6 addresses. |
| | 0 | 207 | | if (colonIndex >= 0) |
| | 0 | 208 | | { |
| | | 209 | | // host:port |
| | 0 | 210 | | host = hostAndPort.Slice(0, colonIndex); // Excludes colon |
| | 0 | 211 | | port = hostAndPort.Slice(colonIndex + 1); // Excludes colon |
| | | 212 | | |
| | | 213 | | // Loosely validate the port just to make sure it was a port and not something else. |
| | 0 | 214 | | if (!ushort.TryParse(port, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)) |
| | 0 | 215 | | { |
| | 0 | 216 | | return inputServiceName; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | // Re-include the colon for the final output. Do not change the port format. |
| | 0 | 220 | | port = hostAndPort.Slice(colonIndex); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | // Re-validate the host. |
| | 0 | 224 | | hostType = Uri.CheckHostName( |
| | 0 | 225 | | host.Length == hostPortAndDistinguisher.Length ? |
| | 0 | 226 | | hostPortAndDistinguisher : |
| | 0 | 227 | | host.ToString()); |
| | 0 | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | if (hostType != UriHostNameType.Dns) |
| | 0 | 231 | | { |
| | | 232 | | // UriHostNameType.IPv4, UriHostNameType.IPv6: Do not normalize IPv4/6 hosts. |
| | | 233 | | // UriHostNameType.Basic: This is never returned by CheckHostName today |
| | | 234 | | // UriHostNameType.Unknown: Nothing recognizable to normalize |
| | | 235 | | // default Some new UriHostNameType? |
| | 0 | 236 | | return inputServiceName; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | // Now we have a valid DNS host, normalize it. |
| | | 240 | | |
| | | 241 | | Uri? constructedUri; |
| | | 242 | | |
| | | 243 | | // We need to avoid any unexpected exceptions on this code path. |
| | | 244 | | const string HttpSchemeAndDelimiter = UriScheme.Http + UriScheme.SchemeDelimiter; |
| | 0 | 245 | | if (!Uri.TryCreate(string.Concat(HttpSchemeAndDelimiter, host), UriKind.Absolute, out constructedUri)) |
| | 0 | 246 | | { |
| | 0 | 247 | | return inputServiceName; |
| | | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | string normalizedHost = constructedUri.GetComponents( |
| | 0 | 251 | | UriComponents.NormalizedHost, UriFormat.SafeUnescaped); |
| | | 252 | | |
| | 0 | 253 | | string normalizedServiceName = string.Concat(prefix, normalizedHost, port, distinguisher); |
| | | 254 | | |
| | | 255 | | // Don't return the new one unless we absolutely have to. It may have only changed casing. |
| | 0 | 256 | | if (string.Equals(inputServiceName, normalizedServiceName, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 257 | | { |
| | 0 | 258 | | return inputServiceName; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | return normalizedServiceName; |
| | 0 | 262 | | } |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | |