Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NegotiateStreamPal.cs
Go to the documentation of this file.
7
8namespace System.Net.Security;
9
10internal static class NegotiateStreamPal
11{
12 internal static IIdentity GetIdentity(NTAuthentication context)
13 {
14 IIdentity identity = null;
15 string name = (context.IsServer ? context.AssociatedName : context.Spn);
16 string protocolName = context.ProtocolName;
17 if (context.IsServer)
18 {
19 SecurityContextTokenHandle token = null;
20 try
21 {
22 SecurityStatusPal status;
23 SafeDeleteContext context2 = context.GetContext(out status);
25 {
27 }
28 global::Interop.SECURITY_STATUS sECURITY_STATUS = (global::Interop.SECURITY_STATUS)SSPIWrapper.QuerySecurityContextToken(GlobalSSPI.SSPIAuth, context2, out token);
29 if (sECURITY_STATUS != 0)
30 {
31 throw new Win32Exception((int)sECURITY_STATUS);
32 }
33 string protocolName2 = context.ProtocolName;
34 return new WindowsIdentity(token.DangerousGetHandle(), protocolName2);
35 }
36 catch (SecurityException)
37 {
38 }
39 finally
40 {
41 token?.Dispose();
42 }
43 }
44 return new GenericIdentity(name, protocolName);
45 }
46
47 internal static string QueryContextAssociatedName(SafeDeleteContext securityContext)
48 {
49 return SSPIWrapper.QueryStringContextAttributes(GlobalSSPI.SSPIAuth, securityContext, global::Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES);
50 }
51
52 internal static void ValidateImpersonationLevel(TokenImpersonationLevel impersonationLevel)
53 {
54 if (impersonationLevel != TokenImpersonationLevel.Identification && impersonationLevel != TokenImpersonationLevel.Impersonation && impersonationLevel != TokenImpersonationLevel.Delegation)
55 {
56 throw new ArgumentOutOfRangeException("impersonationLevel", impersonationLevel.ToString(), System.SR.net_auth_supported_impl_levels);
57 }
58 }
59
60 internal static int Encrypt(SafeDeleteContext securityContext, ReadOnlySpan<byte> buffer, bool isConfidential, bool isNtlm, [NotNull] ref byte[] output, uint sequenceNumber)
61 {
62 SecPkgContext_Sizes attribute = default(SecPkgContext_Sizes);
63 bool flag = SSPIWrapper.QueryBlittableContextAttributes(GlobalSSPI.SSPIAuth, securityContext, global::Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES, ref attribute);
64 int num = checked(2147483643 - attribute.cbBlockSize - attribute.cbSecurityTrailer);
65 if (buffer.Length > num)
66 {
68 }
69 int num2 = buffer.Length + attribute.cbSecurityTrailer + attribute.cbBlockSize;
70 if (output == null || output.Length < num2 + 4)
71 {
72 output = new byte[num2 + 4];
73 }
74 buffer.CopyTo(output.AsSpan(4 + attribute.cbSecurityTrailer));
75 ThreeSecurityBuffers threeSecurityBuffers = default(ThreeSecurityBuffers);
76 Span<SecurityBuffer> input = MemoryMarshal.CreateSpan(ref threeSecurityBuffers._item0, 3);
77 input[0] = new SecurityBuffer(output, 4, attribute.cbSecurityTrailer, SecurityBufferType.SECBUFFER_TOKEN);
78 input[1] = new SecurityBuffer(output, 4 + attribute.cbSecurityTrailer, buffer.Length, SecurityBufferType.SECBUFFER_DATA);
79 input[2] = new SecurityBuffer(output, 4 + attribute.cbSecurityTrailer + buffer.Length, attribute.cbBlockSize, SecurityBufferType.SECBUFFER_PADDING);
80 int num3;
81 if (isConfidential)
82 {
83 num3 = SSPIWrapper.EncryptMessage(GlobalSSPI.SSPIAuth, securityContext, input, sequenceNumber);
84 }
85 else
86 {
87 if (isNtlm)
88 {
89 input[1].type |= SecurityBufferType.SECBUFFER_READONLY;
90 }
91 num3 = SSPIWrapper.MakeSignature(GlobalSSPI.SSPIAuth, securityContext, input, 0u);
92 }
93 if (num3 != 0)
94 {
95 Exception ex = new Win32Exception(num3);
96 if (System.Net.NetEventSource.Log.IsEnabled())
97 {
98 System.Net.NetEventSource.Error(null, ex, "Encrypt");
99 }
100 throw ex;
101 }
102 num2 = input[0].size;
103 bool flag2 = false;
104 if (num2 != attribute.cbSecurityTrailer)
105 {
106 flag2 = true;
107 Buffer.BlockCopy(output, input[1].offset, output, 4 + num2, input[1].size);
108 }
109 num2 += input[1].size;
110 if (input[2].size != 0 && (flag2 || num2 != buffer.Length + attribute.cbSecurityTrailer))
111 {
112 Buffer.BlockCopy(output, input[2].offset, output, 4 + num2, input[2].size);
113 }
114 num2 += input[2].size;
115 output[0] = (byte)((uint)num2 & 0xFFu);
116 output[1] = (byte)((uint)(num2 >> 8) & 0xFFu);
117 output[2] = (byte)((uint)(num2 >> 16) & 0xFFu);
118 output[3] = (byte)((uint)(num2 >> 24) & 0xFFu);
119 return num2 + 4;
120 }
121
122 internal static int Decrypt(SafeDeleteContext securityContext, byte[] buffer, int offset, int count, bool isConfidential, bool isNtlm, out int newOffset, uint sequenceNumber)
123 {
124 if (offset < 0 || offset > ((buffer != null) ? buffer.Length : 0))
125 {
126 throw new ArgumentOutOfRangeException("offset");
127 }
128 if (count < 0 || count > ((buffer != null) ? (buffer.Length - offset) : 0))
129 {
130 throw new ArgumentOutOfRangeException("count");
131 }
132 if (isNtlm)
133 {
134 return DecryptNtlm(securityContext, buffer, offset, count, isConfidential, out newOffset, sequenceNumber);
135 }
136 TwoSecurityBuffers twoSecurityBuffers = default(TwoSecurityBuffers);
137 Span<SecurityBuffer> input = MemoryMarshal.CreateSpan(ref twoSecurityBuffers._item0, 2);
138 input[0] = new SecurityBuffer(buffer, offset, count, SecurityBufferType.SECBUFFER_STREAM);
139 input[1] = new SecurityBuffer(0, SecurityBufferType.SECBUFFER_DATA);
140 int num = ((!isConfidential) ? SSPIWrapper.VerifySignature(GlobalSSPI.SSPIAuth, securityContext, input, sequenceNumber) : SSPIWrapper.DecryptMessage(GlobalSSPI.SSPIAuth, securityContext, input, sequenceNumber));
141 if (num != 0)
142 {
143 Exception ex = new Win32Exception(num);
144 if (System.Net.NetEventSource.Log.IsEnabled())
145 {
146 System.Net.NetEventSource.Error(null, ex, "Decrypt");
147 }
148 throw ex;
149 }
150 if (input[1].type != SecurityBufferType.SECBUFFER_DATA)
151 {
152 throw new InternalException(input[1].type);
153 }
154 newOffset = input[1].offset;
155 return input[1].size;
156 }
157
158 private static int DecryptNtlm(SafeDeleteContext securityContext, byte[] buffer, int offset, int count, bool isConfidential, out int newOffset, uint sequenceNumber)
159 {
160 if (count < 16)
161 {
162 throw new ArgumentOutOfRangeException("count");
163 }
164 TwoSecurityBuffers twoSecurityBuffers = default(TwoSecurityBuffers);
165 Span<SecurityBuffer> input = MemoryMarshal.CreateSpan(ref twoSecurityBuffers._item0, 2);
166 input[0] = new SecurityBuffer(buffer, offset, 16, SecurityBufferType.SECBUFFER_TOKEN);
167 input[1] = new SecurityBuffer(buffer, offset + 16, count - 16, SecurityBufferType.SECBUFFER_DATA);
168 SecurityBufferType securityBufferType = SecurityBufferType.SECBUFFER_DATA;
169 int num;
170 if (isConfidential)
171 {
172 num = SSPIWrapper.DecryptMessage(GlobalSSPI.SSPIAuth, securityContext, input, sequenceNumber);
173 }
174 else
175 {
176 securityBufferType |= SecurityBufferType.SECBUFFER_READONLY;
177 input[1].type = securityBufferType;
178 num = SSPIWrapper.VerifySignature(GlobalSSPI.SSPIAuth, securityContext, input, sequenceNumber);
179 }
180 if (num != 0)
181 {
182 Exception message = new Win32Exception(num);
183 if (System.Net.NetEventSource.Log.IsEnabled())
184 {
185 System.Net.NetEventSource.Error(null, message, "DecryptNtlm");
186 }
187 throw new Win32Exception(num);
188 }
189 if (input[1].type != securityBufferType)
190 {
191 throw new InternalException(input[1].type);
192 }
193 newOffset = input[1].offset;
194 return input[1].size;
195 }
196
197 internal static int QueryMaxTokenSize(string package)
198 {
199 return SSPIWrapper.GetVerifyPackageInfo(GlobalSSPI.SSPIAuth, package, throwIfMissing: true).MaxToken;
200 }
201
202 internal static SafeFreeCredentials AcquireDefaultCredential(string package, bool isServer)
203 {
204 return SSPIWrapper.AcquireDefaultCredential(GlobalSSPI.SSPIAuth, package, isServer ? global::Interop.SspiCli.CredentialUse.SECPKG_CRED_INBOUND : global::Interop.SspiCli.CredentialUse.SECPKG_CRED_OUTBOUND);
205 }
206
207 internal static SafeFreeCredentials AcquireCredentialsHandle(string package, bool isServer, NetworkCredential credential)
208 {
209 SafeSspiAuthDataHandle authData = null;
210 try
211 {
212 global::Interop.SECURITY_STATUS sECURITY_STATUS = global::Interop.SspiCli.SspiEncodeStringsAsAuthIdentity(credential.UserName, credential.Domain, credential.Password, out authData);
213 if (sECURITY_STATUS != 0)
214 {
215 if (System.Net.NetEventSource.Log.IsEnabled())
216 {
217 System.Net.NetEventSource.Error(null, System.SR.Format(System.SR.net_log_operation_failed_with_error, "SspiEncodeStringsAsAuthIdentity", $"0x{sECURITY_STATUS:X}"), "AcquireCredentialsHandle");
218 }
219 throw new Win32Exception((int)sECURITY_STATUS);
220 }
221 return SSPIWrapper.AcquireCredentialsHandle(GlobalSSPI.SSPIAuth, package, isServer ? global::Interop.SspiCli.CredentialUse.SECPKG_CRED_INBOUND : global::Interop.SspiCli.CredentialUse.SECPKG_CRED_OUTBOUND, ref authData);
222 }
223 finally
224 {
225 authData?.Dispose();
226 }
227 }
228
229 internal static string QueryContextClientSpecifiedSpn(SafeDeleteContext securityContext)
230 {
231 return SSPIWrapper.QueryStringContextAttributes(GlobalSSPI.SSPIAuth, securityContext, global::Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET);
232 }
233
234 internal static string QueryContextAuthenticationPackage(SafeDeleteContext securityContext)
235 {
237 SafeHandle sspiHandle;
238 bool flag = SSPIWrapper.QueryBlittableContextAttributes(GlobalSSPI.SSPIAuth, securityContext, global::Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO, typeof(SafeFreeContextBuffer), out sspiHandle, ref attribute);
239 using (sspiHandle)
240 {
241 return flag ? NegotiationInfoClass.GetAuthenticationPackageName(sspiHandle, (int)attribute.NegotiationState) : null;
242 }
243 }
244
245 internal static SecurityStatusPal InitializeSecurityContext(ref SafeFreeCredentials credentialsHandle, ref SafeDeleteContext securityContext, string spn, ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref ContextFlagsPal contextFlags)
246 {
247 InputSecurityBuffers inputBuffers = default(InputSecurityBuffers);
248 if (incomingBlob != null)
249 {
250 inputBuffers.SetNextBuffer(new InputSecurityBuffer(incomingBlob, SecurityBufferType.SECBUFFER_TOKEN));
251 }
252 if (channelBinding != null)
253 {
254 inputBuffers.SetNextBuffer(new InputSecurityBuffer(channelBinding));
255 }
256 SecurityBuffer outputBuffer = new SecurityBuffer(resultBlob, SecurityBufferType.SECBUFFER_TOKEN);
257 global::Interop.SspiCli.ContextFlags outFlags = global::Interop.SspiCli.ContextFlags.Zero;
258 SafeDeleteSslContext context = (SafeDeleteSslContext)securityContext;
259 global::Interop.SECURITY_STATUS win32SecurityStatus = (global::Interop.SECURITY_STATUS)SSPIWrapper.InitializeSecurityContext(GlobalSSPI.SSPIAuth, ref credentialsHandle, ref context, spn, ContextFlagsAdapterPal.GetInteropFromContextFlagsPal(requestedContextFlags), global::Interop.SspiCli.Endianness.SECURITY_NETWORK_DREP, inputBuffers, ref outputBuffer, ref outFlags);
260 securityContext = context;
261 resultBlob = outputBuffer.token;
264 }
265
266 internal static SecurityStatusPal CompleteAuthToken(ref SafeDeleteContext securityContext, byte[] incomingBlob)
267 {
268 SafeDeleteSslContext context = (SafeDeleteSslContext)securityContext;
269 SecurityBuffer inputBuffer = new SecurityBuffer(incomingBlob, SecurityBufferType.SECBUFFER_TOKEN);
270 global::Interop.SECURITY_STATUS win32SecurityStatus = (global::Interop.SECURITY_STATUS)SSPIWrapper.CompleteAuthToken(GlobalSSPI.SSPIAuth, ref context, in inputBuffer);
271 securityContext = context;
273 }
274
275 internal static SecurityStatusPal AcceptSecurityContext(SafeFreeCredentials credentialsHandle, ref SafeDeleteContext securityContext, ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref ContextFlagsPal contextFlags)
276 {
277 InputSecurityBuffers inputBuffers = default(InputSecurityBuffers);
278 if (incomingBlob != null)
279 {
280 inputBuffers.SetNextBuffer(new InputSecurityBuffer(incomingBlob, SecurityBufferType.SECBUFFER_TOKEN));
281 }
282 if (channelBinding != null)
283 {
284 inputBuffers.SetNextBuffer(new InputSecurityBuffer(channelBinding));
285 }
286 SecurityBuffer outputBuffer = new SecurityBuffer(resultBlob, SecurityBufferType.SECBUFFER_TOKEN);
287 global::Interop.SspiCli.ContextFlags outFlags = global::Interop.SspiCli.ContextFlags.Zero;
288 SafeDeleteSslContext context = (SafeDeleteSslContext)securityContext;
289 global::Interop.SECURITY_STATUS win32SecurityStatus = (global::Interop.SECURITY_STATUS)SSPIWrapper.AcceptSecurityContext(GlobalSSPI.SSPIAuth, credentialsHandle, ref context, ContextFlagsAdapterPal.GetInteropFromContextFlagsPal(requestedContextFlags), global::Interop.SspiCli.Endianness.SECURITY_NETWORK_DREP, inputBuffers, ref outputBuffer, ref outFlags);
290 resultBlob = outputBuffer.token;
291 securityContext = context;
294 }
295
300}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static System.Net.ContextFlagsPal GetContextFlagsPalFromInterop(global::Interop.SspiCli.ContextFlags win32Flags)
static global::Interop.SspiCli.ContextFlags GetInteropFromContextFlagsPal(System.Net.ContextFlagsPal flags)
static readonly System.Net.SSPIAuthType SSPIAuth
Definition GlobalSSPI.cs:5
System.Net.Security.SafeDeleteContext GetContext(out System.Net.SecurityStatusPal status)
static unsafe string GetAuthenticationPackageName(SafeHandle safeHandle, int negotiationState)
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static int QuerySecurityContextToken(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext context, out System.Net.Security.SecurityContextTokenHandle token)
static System.Net.Security.SafeFreeCredentials AcquireDefaultCredential(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent)
static int DecryptMessage(ISSPIInterface secModule, SafeDeleteContext context, Span< SecurityBuffer > input, uint sequenceNumber)
static int MakeSignature(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext context, Span< System.Net.Security.SecurityBuffer > input, uint sequenceNumber)
static int VerifySignature(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext context, Span< System.Net.Security.SecurityBuffer > input, uint sequenceNumber)
static System.Net.SecurityPackageInfoClass GetVerifyPackageInfo(System.Net.ISSPIInterface secModule, string packageName, bool throwIfMissing)
static int InitializeSecurityContext(System.Net.ISSPIInterface secModule, ref System.Net.Security.SafeFreeCredentials credential, ref System.Net.Security.SafeDeleteSslContext context, string targetName, global::Interop.SspiCli.ContextFlags inFlags, global::Interop.SspiCli.Endianness datarep, System.Net.Security.InputSecurityBuffers inputBuffers, ref System.Net.Security.SecurityBuffer outputBuffer, ref global::Interop.SspiCli.ContextFlags outFlags)
static int EncryptMessage(ISSPIInterface secModule, SafeDeleteContext context, Span< SecurityBuffer > input, uint sequenceNumber)
static int CompleteAuthToken(System.Net.ISSPIInterface secModule, ref System.Net.Security.SafeDeleteSslContext context, in System.Net.Security.SecurityBuffer inputBuffer)
static int AcceptSecurityContext(System.Net.ISSPIInterface secModule, System.Net.Security.SafeFreeCredentials credential, ref System.Net.Security.SafeDeleteSslContext context, global::Interop.SspiCli.ContextFlags inFlags, global::Interop.SspiCli.Endianness datarep, System.Net.Security.InputSecurityBuffers inputBuffers, ref System.Net.Security.SecurityBuffer outputBuffer, ref global::Interop.SspiCli.ContextFlags outFlags)
static string QueryStringContextAttributes(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext securityContext, global::Interop.SspiCli.ContextAttribute contextAttribute)
static System.Net.Security.SafeFreeCredentials AcquireCredentialsHandle(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent, ref System.Net.Security.SafeSspiAuthDataHandle authdata)
static global::Interop.SECURITY_STATUS GetInteropFromSecurityStatusPal(System.Net.SecurityStatusPal status)
static System.Net.SecurityStatusPal GetSecurityStatusPalFromInterop(global::Interop.SECURITY_STATUS win32SecurityStatus, bool attachException=false)
static Win32Exception CreateExceptionFromError(SecurityStatusPal statusCode)
static string QueryContextAssociatedName(SafeDeleteContext securityContext)
static string QueryContextClientSpecifiedSpn(SafeDeleteContext securityContext)
static int DecryptNtlm(SafeDeleteContext securityContext, byte[] buffer, int offset, int count, bool isConfidential, out int newOffset, uint sequenceNumber)
static int Decrypt(SafeDeleteContext securityContext, byte[] buffer, int offset, int count, bool isConfidential, bool isNtlm, out int newOffset, uint sequenceNumber)
static void ValidateImpersonationLevel(TokenImpersonationLevel impersonationLevel)
static IIdentity GetIdentity(NTAuthentication context)
static int Encrypt(SafeDeleteContext securityContext, ReadOnlySpan< byte > buffer, bool isConfidential, bool isNtlm, [NotNull] ref byte[] output, uint sequenceNumber)
static int QueryMaxTokenSize(string package)
static SecurityStatusPal AcceptSecurityContext(SafeFreeCredentials credentialsHandle, ref SafeDeleteContext securityContext, ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref ContextFlagsPal contextFlags)
static SecurityStatusPal InitializeSecurityContext(ref SafeFreeCredentials credentialsHandle, ref SafeDeleteContext securityContext, string spn, ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref ContextFlagsPal contextFlags)
static SecurityStatusPal CompleteAuthToken(ref SafeDeleteContext securityContext, byte[] incomingBlob)
static SafeFreeCredentials AcquireCredentialsHandle(string package, bool isServer, NetworkCredential credential)
static SafeFreeCredentials AcquireDefaultCredential(string package, bool isServer)
static string QueryContextAuthenticationPackage(SafeDeleteContext securityContext)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_log_operation_failed_with_error
Definition SR.cs:154
static string net_auth_supported_impl_levels
Definition SR.cs:64
static string net_io_out_range
Definition SR.cs:30
Definition SR.cs:7
readonly System.Net.SecurityStatusPalErrorCode ErrorCode
void SetNextBuffer(System.Net.Security.InputSecurityBuffer buffer)
System.Net.Security.SecurityBuffer _item0