| | | 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.Diagnostics; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Net |
| | | 9 | | { |
| | | 10 | | // Authentication SSPI (Kerberos, NTLM, Negotiate and WDigest): |
| | | 11 | | internal sealed class SSPIAuthType : ISSPIInterface |
| | | 12 | | { |
| | | 13 | | private static volatile SecurityPackageInfoClass[]? s_securityPackages; |
| | | 14 | | |
| | | 15 | | public SecurityPackageInfoClass[]? SecurityPackages |
| | | 16 | | { |
| | | 17 | | get |
| | 0 | 18 | | { |
| | 0 | 19 | | return s_securityPackages; |
| | 0 | 20 | | } |
| | | 21 | | set |
| | 0 | 22 | | { |
| | 0 | 23 | | s_securityPackages = value; |
| | 0 | 24 | | } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public int EnumerateSecurityPackages(out int pkgnum, out SafeFreeContextBuffer pkgArray) |
| | 0 | 28 | | { |
| | 0 | 29 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this); |
| | 0 | 30 | | return SafeFreeContextBuffer.EnumeratePackages(out pkgnum, out pkgArray); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, ref SafeSspiAuthData |
| | 0 | 34 | | { |
| | 0 | 35 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, ref authdata, out outCredential); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public int AcquireDefaultCredential(string moduleName, Interop.SspiCli.CredentialUse usage, out SafeFreeCredenti |
| | 0 | 39 | | { |
| | 0 | 40 | | return SafeFreeCredentials.AcquireDefaultCredential(moduleName, usage, out outCredential); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public unsafe int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, Interop.SspiC |
| | 0 | 44 | | { |
| | 0 | 45 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, authdata, out outCredential); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public unsafe int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, Interop.SspiC |
| | 0 | 49 | | { |
| | 0 | 50 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, authdata, out outCredential); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public int AcceptSecurityContext(SafeFreeCredentials? credential, ref SafeDeleteSslContext? context, ref InputSe |
| | 0 | 54 | | { |
| | 0 | 55 | | return SafeDeleteContext.AcceptSecurityContext(ref credential, ref context, inFlags, endianness, ref inputBu |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public int InitializeSecurityContext(ref SafeFreeCredentials? credential, ref SafeDeleteSslContext? context, str |
| | 0 | 59 | | { |
| | 0 | 60 | | return SafeDeleteContext.InitializeSecurityContext(ref credential, ref context, targetName, inFlags, endiann |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public int EncryptMessage(SafeDeleteContext context, ref Interop.SspiCli.SecBufferDesc inputOutput, uint qop) |
| | 0 | 64 | | { |
| | 0 | 65 | | bool mustRelease = false; |
| | | 66 | | try |
| | 0 | 67 | | { |
| | 0 | 68 | | context.DangerousAddRef(ref mustRelease); |
| | 0 | 69 | | return Interop.SspiCli.EncryptMessage(ref context._handle, qop, ref inputOutput, 0); |
| | | 70 | | } |
| | | 71 | | finally |
| | 0 | 72 | | { |
| | 0 | 73 | | if (mustRelease) |
| | 0 | 74 | | { |
| | 0 | 75 | | context.DangerousRelease(); |
| | 0 | 76 | | } |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public unsafe int DecryptMessage(SafeDeleteContext context, ref Interop.SspiCli.SecBufferDesc inputOutput, out u |
| | 0 | 81 | | { |
| | 0 | 82 | | int status = (int)Interop.SECURITY_STATUS.InvalidHandle; |
| | 0 | 83 | | uint qopTemp = 0; |
| | | 84 | | |
| | 0 | 85 | | bool mustRelease = false; |
| | | 86 | | try |
| | 0 | 87 | | { |
| | 0 | 88 | | context.DangerousAddRef(ref mustRelease); |
| | 0 | 89 | | status = Interop.SspiCli.DecryptMessage(ref context._handle, ref inputOutput, 0, &qopTemp); |
| | 0 | 90 | | } |
| | | 91 | | finally |
| | 0 | 92 | | { |
| | 0 | 93 | | if (mustRelease) |
| | 0 | 94 | | { |
| | 0 | 95 | | context.DangerousRelease(); |
| | 0 | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | qop = qopTemp; |
| | 0 | 100 | | return status; |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public int QueryContextChannelBinding(SafeDeleteContext context, Interop.SspiCli.ContextAttribute attribute, out |
| | 0 | 104 | | { |
| | | 105 | | // Querying an auth SSP for a CBT is not supported. |
| | 0 | 106 | | throw new NotSupportedException(); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public unsafe int QueryContextAttributes(SafeDeleteContext context, Interop.SspiCli.ContextAttribute attribute, |
| | 0 | 110 | | { |
| | 0 | 111 | | return SafeFreeContextBuffer.QueryContextAttributes(context, attribute, refHandle); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public unsafe int QueryContextAttributes(SafeDeleteContext context, Interop.SspiCli.ContextAttribute attribute, |
| | 0 | 115 | | { |
| | 0 | 116 | | refHandle = null; |
| | 0 | 117 | | if (handleType != null) |
| | 0 | 118 | | { |
| | 0 | 119 | | if (handleType == typeof(SafeFreeContextBuffer)) |
| | 0 | 120 | | { |
| | 0 | 121 | | refHandle = SafeFreeContextBuffer.CreateEmptyHandle(); |
| | 0 | 122 | | } |
| | | 123 | | else |
| | 0 | 124 | | { |
| | 0 | 125 | | throw new ArgumentException(SR.Format(SR.SSPIInvalidHandleType, handleType.FullName), nameof(handleT |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | fixed (byte* bufferPtr = buffer) |
| | 0 | 130 | | { |
| | 0 | 131 | | return SafeFreeContextBuffer.QueryContextAttributes(context, attribute, bufferPtr, refHandle); |
| | | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public int QuerySecurityContextToken(SafeDeleteContext phContext, out SecurityContextTokenHandle phToken) |
| | 0 | 136 | | { |
| | 0 | 137 | | return GetSecurityContextToken(phContext, out phToken); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | public int CompleteAuthToken(ref SafeDeleteSslContext? refContext, in InputSecurityBuffer inputBuffer) |
| | 0 | 141 | | { |
| | 0 | 142 | | return SafeDeleteContext.CompleteAuthToken(ref refContext, in inputBuffer); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static int GetSecurityContextToken(SafeDeleteContext phContext, out SecurityContextTokenHandle safeHandl |
| | 0 | 146 | | { |
| | 0 | 147 | | bool mustRelease = false; |
| | | 148 | | try |
| | 0 | 149 | | { |
| | 0 | 150 | | phContext.DangerousAddRef(ref mustRelease); |
| | 0 | 151 | | return Interop.SspiCli.QuerySecurityContextToken(ref phContext._handle, out safeHandle); |
| | | 152 | | } |
| | | 153 | | finally |
| | 0 | 154 | | { |
| | 0 | 155 | | if (mustRelease) |
| | 0 | 156 | | { |
| | 0 | 157 | | phContext.DangerousRelease(); |
| | 0 | 158 | | } |
| | 0 | 159 | | } |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | public int ApplyControlToken(ref SafeDeleteSslContext? refContext, in SecurityBuffer inputBuffers) |
| | 0 | 163 | | { |
| | 0 | 164 | | throw new NotSupportedException(); |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |