| | | 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.ComponentModel; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Net.Security; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | namespace System.Net |
| | | 11 | | { |
| | | 12 | | internal static class SSPIWrapper |
| | | 13 | | { |
| | | 14 | | internal static SecurityPackageInfoClass[] EnumerateSecurityPackages(ISSPIInterface secModule) |
| | 0 | 15 | | { |
| | 0 | 16 | | if (secModule.SecurityPackages == null) |
| | 0 | 17 | | { |
| | 0 | 18 | | lock (secModule) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (secModule.SecurityPackages == null) |
| | 0 | 21 | | { |
| | 0 | 22 | | int moduleCount = 0; |
| | 0 | 23 | | SafeFreeContextBuffer? arrayBaseHandle = null; |
| | | 24 | | try |
| | 0 | 25 | | { |
| | 0 | 26 | | int errorCode = secModule.EnumerateSecurityPackages(out moduleCount, out arrayBaseHandle); |
| | 0 | 27 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"arrayBase: {arrayBaseHandle} |
| | 0 | 28 | | if (errorCode != 0) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new Win32Exception(errorCode); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | var securityPackages = new SecurityPackageInfoClass[moduleCount]; |
| | | 34 | | |
| | | 35 | | int i; |
| | 0 | 36 | | for (i = 0; i < moduleCount; i++) |
| | 0 | 37 | | { |
| | 0 | 38 | | securityPackages[i] = new SecurityPackageInfoClass(arrayBaseHandle, i); |
| | 0 | 39 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.EnumerateSecurityPackages(securit |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | secModule.SecurityPackages = securityPackages; |
| | 0 | 43 | | } |
| | | 44 | | finally |
| | 0 | 45 | | { |
| | 0 | 46 | | arrayBaseHandle?.Dispose(); |
| | 0 | 47 | | } |
| | 0 | 48 | | } |
| | 0 | 49 | | } |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return secModule.SecurityPackages; |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static SecurityPackageInfoClass? GetVerifyPackageInfo(ISSPIInterface secModule, string packageName, boo |
| | 0 | 56 | | { |
| | 0 | 57 | | SecurityPackageInfoClass[] supportedSecurityPackages = EnumerateSecurityPackages(secModule); |
| | 0 | 58 | | if (supportedSecurityPackages != null) |
| | 0 | 59 | | { |
| | 0 | 60 | | for (int i = 0; i < supportedSecurityPackages.Length; i++) |
| | 0 | 61 | | { |
| | 0 | 62 | | if (string.Equals(supportedSecurityPackages[i].Name, packageName, StringComparison.OrdinalIgnoreCase |
| | 0 | 63 | | { |
| | 0 | 64 | | return supportedSecurityPackages[i]; |
| | | 65 | | } |
| | 0 | 66 | | } |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.SspiPackageNotFound(packageName); |
| | | 70 | | |
| | 0 | 71 | | if (throwIfMissing) |
| | 0 | 72 | | { |
| | 0 | 73 | | throw new NotSupportedException(SR.net_securitypackagesupport); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return null; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public static SafeFreeCredentials AcquireDefaultCredential(ISSPIInterface secModule, string package, Interop.Ssp |
| | 0 | 80 | | { |
| | 0 | 81 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.AcquireDefaultCredential(package, intent); |
| | | 82 | | |
| | 0 | 83 | | SafeFreeCredentials? outCredential = null; |
| | 0 | 84 | | int errorCode = secModule.AcquireDefaultCredential(package, intent, out outCredential); |
| | | 85 | | |
| | 0 | 86 | | if (errorCode != 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_wit |
| | 0 | 89 | | throw new Win32Exception(errorCode); |
| | | 90 | | } |
| | 0 | 91 | | return outCredential; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public static SafeFreeCredentials AcquireCredentialsHandle(ISSPIInterface secModule, string package, Interop.Ssp |
| | 0 | 95 | | { |
| | 0 | 96 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.AcquireCredentialsHandle(package, intent, authdata); |
| | | 97 | | |
| | 0 | 98 | | SafeFreeCredentials? credentialsHandle = null; |
| | 0 | 99 | | int errorCode = secModule.AcquireCredentialsHandle(package, intent, ref authdata, out credentialsHandle); |
| | | 100 | | |
| | 0 | 101 | | if (errorCode != 0) |
| | 0 | 102 | | { |
| | 0 | 103 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_wit |
| | 0 | 104 | | throw new Win32Exception(errorCode); |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | return credentialsHandle; |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | public static unsafe SafeFreeCredentials AcquireCredentialsHandle(ISSPIInterface secModule, string package, Inte |
| | 0 | 111 | | { |
| | 0 | 112 | | int errorCode = secModule.AcquireCredentialsHandle( |
| | 0 | 113 | | package, |
| | 0 | 114 | | intent, |
| | 0 | 115 | | scc, |
| | 0 | 116 | | out SafeFreeCredentials outCredential); |
| | | 117 | | |
| | 0 | 118 | | if (errorCode != 0) |
| | 0 | 119 | | { |
| | 0 | 120 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_wit |
| | 0 | 121 | | throw new Win32Exception(errorCode); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | return outCredential; |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | public static unsafe SafeFreeCredentials AcquireCredentialsHandle(ISSPIInterface secModule, string package, Inte |
| | 0 | 128 | | { |
| | 0 | 129 | | int errorCode = secModule.AcquireCredentialsHandle( |
| | 0 | 130 | | package, |
| | 0 | 131 | | intent, |
| | 0 | 132 | | scc, |
| | 0 | 133 | | out SafeFreeCredentials outCredential); |
| | | 134 | | |
| | 0 | 135 | | if (errorCode != 0) |
| | 0 | 136 | | { |
| | 0 | 137 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_wit |
| | 0 | 138 | | throw new Win32Exception(errorCode); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | return outCredential; |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | internal static int InitializeSecurityContext(ISSPIInterface secModule, ref SafeFreeCredentials? credential, ref |
| | 0 | 145 | | { |
| | 0 | 146 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.InitializeSecurityContext(credential, context, target |
| | | 147 | | |
| | 0 | 148 | | int errorCode = secModule.InitializeSecurityContext(ref credential, ref context, targetName, inFlags, datare |
| | | 149 | | |
| | 0 | 150 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.SecurityContextInputBuffers(nameof(InitializeSecurity |
| | | 151 | | |
| | 0 | 152 | | return errorCode; |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | internal static int AcceptSecurityContext(ISSPIInterface secModule, SafeFreeCredentials? credential, ref SafeDel |
| | 0 | 156 | | { |
| | 0 | 157 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.AcceptSecurityContext(credential, context, inFlags); |
| | | 158 | | |
| | 0 | 159 | | int errorCode = secModule.AcceptSecurityContext(credential, ref context, ref inputBuffers, inFlags, datarep, |
| | | 160 | | |
| | 0 | 161 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.SecurityContextInputBuffers(nameof(AcceptSecurityCont |
| | | 162 | | |
| | 0 | 163 | | return errorCode; |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | internal static int CompleteAuthToken(ISSPIInterface secModule, ref SafeDeleteSslContext? context, in InputSecur |
| | 0 | 167 | | { |
| | 0 | 168 | | int errorCode = secModule.CompleteAuthToken(ref context, in inputBuffer); |
| | | 169 | | |
| | 0 | 170 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.OperationReturnedSomething(nameof(CompleteAuthToken), |
| | | 171 | | |
| | 0 | 172 | | return errorCode; |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | internal static int ApplyControlToken(ISSPIInterface secModule, ref SafeDeleteSslContext? context, in SecurityBu |
| | 0 | 176 | | { |
| | 0 | 177 | | int errorCode = secModule.ApplyControlToken(ref context, in inputBuffer); |
| | | 178 | | |
| | 0 | 179 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.OperationReturnedSomething(nameof(ApplyControlToken), |
| | | 180 | | |
| | 0 | 181 | | return errorCode; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | public static int QuerySecurityContextToken(ISSPIInterface secModule, SafeDeleteContext context, out SecurityCon |
| | 0 | 185 | | { |
| | 0 | 186 | | return secModule.QuerySecurityContextToken(context, out token); |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | public static SafeFreeContextBufferChannelBinding? QueryContextChannelBinding(ISSPIInterface secModule, SafeDele |
| | 0 | 190 | | { |
| | | 191 | | SafeFreeContextBufferChannelBinding result; |
| | 0 | 192 | | int errorCode = secModule.QueryContextChannelBinding(securityContext, contextAttribute, out result); |
| | 0 | 193 | | if (errorCode != 0) |
| | 0 | 194 | | { |
| | 0 | 195 | | result.Dispose(); |
| | 0 | 196 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode)}") |
| | 0 | 197 | | return null; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | return result; |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | public static bool QueryBlittableContextAttributes<T>(ISSPIInterface secModule, SafeDeleteContext securityContex |
| | 0 | 204 | | { |
| | 0 | 205 | | Span<T> span = new Span<T>(ref attribute); |
| | 0 | 206 | | int errorCode = secModule.QueryContextAttributes( |
| | 0 | 207 | | securityContext, contextAttribute, |
| | 0 | 208 | | MemoryMarshal.AsBytes(span), |
| | 0 | 209 | | null, |
| | 0 | 210 | | out SafeHandle? sspiHandle); |
| | | 211 | | |
| | 0 | 212 | | using (sspiHandle) |
| | 0 | 213 | | { |
| | 0 | 214 | | if (errorCode != 0) |
| | 0 | 215 | | { |
| | 0 | 216 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode |
| | 0 | 217 | | return false; |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | return true; |
| | | 221 | | } |
| | 0 | 222 | | } |
| | | 223 | | |
| | | 224 | | public static bool QueryBlittableContextAttributes<T>(ISSPIInterface secModule, SafeDeleteContext securityContex |
| | 0 | 225 | | { |
| | 0 | 226 | | Span<T> span = new Span<T>(ref attribute); |
| | 0 | 227 | | int errorCode = secModule.QueryContextAttributes( |
| | 0 | 228 | | securityContext, contextAttribute, |
| | 0 | 229 | | MemoryMarshal.AsBytes(span), |
| | 0 | 230 | | safeHandleType, |
| | 0 | 231 | | out sspiHandle); |
| | | 232 | | |
| | 0 | 233 | | if (errorCode != 0) |
| | 0 | 234 | | { |
| | 0 | 235 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode)}") |
| | 0 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | return true; |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | public static unsafe string? QueryStringContextAttributes(ISSPIInterface secModule, SafeDeleteContext securityCo |
| | 0 | 243 | | { |
| | 0 | 244 | | Debug.Assert( |
| | 0 | 245 | | contextAttribute == Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES || |
| | 0 | 246 | | contextAttribute == Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET); |
| | | 247 | | |
| | | 248 | | |
| | 0 | 249 | | Span<byte> buffer = stackalloc byte[IntPtr.Size]; |
| | 0 | 250 | | int errorCode = secModule.QueryContextAttributes( |
| | 0 | 251 | | securityContext, |
| | 0 | 252 | | contextAttribute, |
| | 0 | 253 | | buffer, |
| | 0 | 254 | | typeof(SafeFreeContextBuffer), |
| | 0 | 255 | | out SafeHandle? sspiHandle); |
| | | 256 | | |
| | 0 | 257 | | Debug.Assert(sspiHandle != null); |
| | | 258 | | |
| | 0 | 259 | | using (sspiHandle) |
| | 0 | 260 | | { |
| | 0 | 261 | | if (errorCode != 0) |
| | 0 | 262 | | { |
| | 0 | 263 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode |
| | 0 | 264 | | return null; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | string? result = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle()); |
| | 0 | 268 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, result); |
| | 0 | 269 | | return result; |
| | | 270 | | } |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | private static unsafe bool QueryCertContextAttribute(ISSPIInterface secModule, SafeDeleteContext securityContext |
| | 0 | 274 | | { |
| | 0 | 275 | | IntPtr handle = IntPtr.Zero; |
| | 0 | 276 | | certContext = null; |
| | | 277 | | |
| | | 278 | | try |
| | 0 | 279 | | { |
| | 0 | 280 | | int errorCode = secModule.QueryContextAttributes( |
| | 0 | 281 | | securityContext, |
| | 0 | 282 | | attribute, |
| | 0 | 283 | | &handle); |
| | | 284 | | |
| | | 285 | | // certificate is not always present (e.g. on server when querying client certificate) |
| | | 286 | | // but we still want to consider such case as a success. |
| | 0 | 287 | | bool success = errorCode == 0 || errorCode == (int)Interop.SECURITY_STATUS.NoCredentials; |
| | | 288 | | |
| | 0 | 289 | | if (errorCode == 0 && handle != IntPtr.Zero) |
| | 0 | 290 | | { |
| | 0 | 291 | | certContext = new SafeFreeCertContext(); |
| | 0 | 292 | | certContext.Set(handle); |
| | | 293 | | // Handle was successfully transferred to SafeHandle |
| | 0 | 294 | | handle = IntPtr.Zero; |
| | 0 | 295 | | } |
| | 0 | 296 | | if (!success) |
| | 0 | 297 | | { |
| | 0 | 298 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode |
| | 0 | 299 | | } |
| | 0 | 300 | | return success; |
| | | 301 | | } |
| | | 302 | | finally |
| | 0 | 303 | | { |
| | 0 | 304 | | if (handle != IntPtr.Zero) |
| | 0 | 305 | | { |
| | 0 | 306 | | Interop.Crypt32.CertFreeCertificateContext(handle); |
| | 0 | 307 | | } |
| | 0 | 308 | | } |
| | 0 | 309 | | } |
| | | 310 | | |
| | | 311 | | public static bool QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CONTEXT(ISSPIInterface secModule, SafeDeleteCo |
| | 0 | 312 | | => QueryCertContextAttribute(secModule, securityContext, Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE |
| | | 313 | | |
| | | 314 | | public static bool QueryContextAttributes_SECPKG_ATTR_LOCAL_CERT_CONTEXT(ISSPIInterface secModule, SafeDeleteCon |
| | 0 | 315 | | => QueryCertContextAttribute(secModule, securityContext, Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_ |
| | | 316 | | |
| | | 317 | | public static bool QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CHAIN(ISSPIInterface secModule, SafeDeleteCont |
| | 0 | 318 | | => QueryCertContextAttribute(secModule, securityContext, Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE |
| | | 319 | | |
| | | 320 | | public static bool QueryContextAttributes_SECPKG_ATTR_ISSUER_LIST_EX(ISSPIInterface secModule, SafeDeleteContext |
| | 0 | 321 | | { |
| | 0 | 322 | | Span<Interop.SspiCli.SecPkgContext_IssuerListInfoEx> buffer = new Span<Interop.SspiCli.SecPkgContext_IssuerL |
| | 0 | 323 | | int errorCode = secModule.QueryContextAttributes( |
| | 0 | 324 | | securityContext, |
| | 0 | 325 | | Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX, |
| | 0 | 326 | | MemoryMarshal.AsBytes(buffer), |
| | 0 | 327 | | typeof(SafeFreeContextBuffer), |
| | 0 | 328 | | out sspiHandle); |
| | | 329 | | |
| | 0 | 330 | | if (errorCode != 0) |
| | 0 | 331 | | { |
| | 0 | 332 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, $"ERROR = {ErrorDescription(errorCode)}") |
| | 0 | 333 | | return false; |
| | | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | return true; |
| | 0 | 337 | | } |
| | | 338 | | |
| | | 339 | | public static string ErrorDescription(int errorCode) |
| | 0 | 340 | | { |
| | 0 | 341 | | if (errorCode == -1) |
| | 0 | 342 | | { |
| | 0 | 343 | | return "An exception when invoking Win32 API"; |
| | | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | return (Interop.SECURITY_STATUS)errorCode switch |
| | 0 | 347 | | { |
| | 0 | 348 | | Interop.SECURITY_STATUS.InvalidHandle => "Invalid handle", |
| | 0 | 349 | | Interop.SECURITY_STATUS.InvalidToken => "Invalid token", |
| | 0 | 350 | | Interop.SECURITY_STATUS.ContinueNeeded => "Continue needed", |
| | 0 | 351 | | Interop.SECURITY_STATUS.IncompleteMessage => "Message incomplete", |
| | 0 | 352 | | Interop.SECURITY_STATUS.WrongPrincipal => "Wrong principal", |
| | 0 | 353 | | Interop.SECURITY_STATUS.TargetUnknown => "Target unknown", |
| | 0 | 354 | | Interop.SECURITY_STATUS.PackageNotFound => "Package not found", |
| | 0 | 355 | | Interop.SECURITY_STATUS.BufferNotEnough => "Buffer not enough", |
| | 0 | 356 | | Interop.SECURITY_STATUS.MessageAltered => "Message altered", |
| | 0 | 357 | | Interop.SECURITY_STATUS.UntrustedRoot => "Untrusted root", |
| | 0 | 358 | | _ => "0x" + errorCode.ToString("x", NumberFormatInfo.InvariantInfo), |
| | 0 | 359 | | }; |
| | 0 | 360 | | } |
| | | 361 | | } // class SSPIWrapper |
| | | 362 | | } |
| | | 363 | | |