Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SSPIWrapper.cs
Go to the documentation of this file.
5
6namespace System.Net;
7
8internal static class SSPIWrapper
9{
10 private enum OP
11 {
12 Encrypt = 1,
13 Decrypt,
16 }
17
18 private ref struct ThreeByteArrays
19 {
20 internal byte[] _item0;
21
22 private byte[] _item1;
23
24 private byte[] _item2;
25 }
26
28 {
29 if (secModule.SecurityPackages == null)
30 {
31 lock (secModule)
32 {
33 if (secModule.SecurityPackages == null)
34 {
35 int pkgnum = 0;
37 try
38 {
39 int num = secModule.EnumerateSecurityPackages(out pkgnum, out pkgArray);
40 if (System.Net.NetEventSource.Log.IsEnabled())
41 {
42 System.Net.NetEventSource.Info(null, $"arrayBase: {pkgArray}", "EnumerateSecurityPackages");
43 }
44 if (num != 0)
45 {
46 throw new Win32Exception(num);
47 }
49 for (int i = 0; i < pkgnum; i++)
50 {
51 array[i] = new System.Net.SecurityPackageInfoClass(pkgArray, i);
52 if (System.Net.NetEventSource.Log.IsEnabled())
53 {
54 System.Net.NetEventSource.Log.EnumerateSecurityPackages(array[i].Name);
55 }
56 }
57 secModule.SecurityPackages = array;
58 }
59 finally
60 {
61 pkgArray?.Dispose();
62 }
63 }
64 }
65 }
66 return secModule.SecurityPackages;
67 }
68
69 internal static System.Net.SecurityPackageInfoClass GetVerifyPackageInfo(System.Net.ISSPIInterface secModule, string packageName, bool throwIfMissing)
70 {
72 if (array != null)
73 {
74 for (int i = 0; i < array.Length; i++)
75 {
76 if (string.Equals(array[i].Name, packageName, StringComparison.OrdinalIgnoreCase))
77 {
78 return array[i];
79 }
80 }
81 }
82 if (System.Net.NetEventSource.Log.IsEnabled())
83 {
84 System.Net.NetEventSource.Log.SspiPackageNotFound(packageName);
85 }
86 if (throwIfMissing)
87 {
89 }
90 return null;
91 }
92
93 public static System.Net.Security.SafeFreeCredentials AcquireDefaultCredential(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent)
94 {
95 if (System.Net.NetEventSource.Log.IsEnabled())
96 {
97 System.Net.NetEventSource.Log.AcquireDefaultCredential(package, intent);
98 }
99 System.Net.Security.SafeFreeCredentials outCredential = null;
100 int num = secModule.AcquireDefaultCredential(package, intent, out outCredential);
101 if (num != 0)
102 {
103 if (System.Net.NetEventSource.Log.IsEnabled())
104 {
105 System.Net.NetEventSource.Error(null, System.SR.Format(System.SR.net_log_operation_failed_with_error, "AcquireDefaultCredential", $"0x{num:X}"), "AcquireDefaultCredential");
106 }
107 throw new Win32Exception(num);
108 }
109 return outCredential;
110 }
111
112 public static System.Net.Security.SafeFreeCredentials AcquireCredentialsHandle(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent, ref System.Net.Security.SafeSspiAuthDataHandle authdata)
113 {
114 if (System.Net.NetEventSource.Log.IsEnabled())
115 {
116 System.Net.NetEventSource.Log.AcquireCredentialsHandle(package, intent, authdata);
117 }
118 System.Net.Security.SafeFreeCredentials outCredential = null;
119 int num = secModule.AcquireCredentialsHandle(package, intent, ref authdata, out outCredential);
120 if (num != 0)
121 {
122 if (System.Net.NetEventSource.Log.IsEnabled())
123 {
124 System.Net.NetEventSource.Error(null, System.SR.Format(System.SR.net_log_operation_failed_with_error, "AcquireCredentialsHandle", $"0x{num:X}"), "AcquireCredentialsHandle");
125 }
126 throw new Win32Exception(num);
127 }
128 return outCredential;
129 }
130
131 internal 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)
132 {
133 if (System.Net.NetEventSource.Log.IsEnabled())
134 {
135 System.Net.NetEventSource.Log.InitializeSecurityContext(credential, context, targetName, inFlags);
136 }
137 int num = secModule.InitializeSecurityContext(ref credential, ref context, targetName, inFlags, datarep, inputBuffers, ref outputBuffer, ref outFlags);
138 if (System.Net.NetEventSource.Log.IsEnabled())
139 {
140 System.Net.NetEventSource.Log.SecurityContextInputBuffers("InitializeSecurityContext", inputBuffers.Count, outputBuffer.size, (global::Interop.SECURITY_STATUS)num);
141 }
142 return num;
143 }
144
145 internal 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)
146 {
147 if (System.Net.NetEventSource.Log.IsEnabled())
148 {
149 System.Net.NetEventSource.Log.AcceptSecurityContext(credential, context, inFlags);
150 }
151 int num = secModule.AcceptSecurityContext(credential, ref context, inputBuffers, inFlags, datarep, ref outputBuffer, ref outFlags);
152 if (System.Net.NetEventSource.Log.IsEnabled())
153 {
154 System.Net.NetEventSource.Log.SecurityContextInputBuffers("AcceptSecurityContext", inputBuffers.Count, outputBuffer.size, (global::Interop.SECURITY_STATUS)num);
155 }
156 return num;
157 }
158
160 {
161 int num = secModule.CompleteAuthToken(ref context, in inputBuffer);
162 if (System.Net.NetEventSource.Log.IsEnabled())
163 {
164 System.Net.NetEventSource.Log.OperationReturnedSomething("CompleteAuthToken", (global::Interop.SECURITY_STATUS)num);
165 }
166 return num;
167 }
168
170 {
171 return EncryptDecryptHelper(OP.MakeSignature, secModule, context, input, sequenceNumber);
172 }
173
175 {
176 return EncryptDecryptHelper(OP.VerifySignature, secModule, context, input, sequenceNumber);
177 }
178
180 {
181 global::Interop.SspiCli.SecBufferDesc inputOutput = new global::Interop.SspiCli.SecBufferDesc(input.Length);
182 Span<global::Interop.SspiCli.SecBuffer> span = stackalloc global::Interop.SspiCli.SecBuffer[input.Length];
183 span.Clear();
184 fixed (global::Interop.SspiCli.SecBuffer* pBuffers = span)
185 {
186 fixed (byte* ptr4 = ((input.Length > 0) ? input[0].token : null))
187 {
188 fixed (byte* ptr3 = ((input.Length > 1) ? input[1].token : null))
189 {
190 fixed (byte* ptr2 = ((input.Length > 2) ? input[2].token : null))
191 {
192 inputOutput.pBuffers = pBuffers;
193 ThreeByteArrays threeByteArrays = default(ThreeByteArrays);
194 Span<byte[]> span2 = MemoryMarshal.CreateSpan(ref threeByteArrays._item0, 3).Slice(0, input.Length);
195 for (int i = 0; i < input.Length; i++)
196 {
197 ref System.Net.Security.SecurityBuffer reference = ref input[i];
198 span[i].cbBuffer = reference.size;
199 span[i].BufferType = reference.type;
200 if (reference.token == null || reference.token.Length == 0)
201 {
202 span[i].pvBuffer = IntPtr.Zero;
203 continue;
204 }
205 span[i].pvBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(reference.token, reference.offset);
206 span2[i] = reference.token;
207 }
208 int num = op switch
209 {
210 OP.Encrypt => secModule.EncryptMessage(context, ref inputOutput, sequenceNumber),
211 OP.Decrypt => secModule.DecryptMessage(context, ref inputOutput, sequenceNumber),
212 OP.MakeSignature => secModule.MakeSignature(context, ref inputOutput, sequenceNumber),
213 _ => secModule.VerifySignature(context, ref inputOutput, sequenceNumber),
214 };
215 for (int j = 0; j < input.Length; j++)
216 {
217 ref System.Net.Security.SecurityBuffer reference2 = ref input[j];
218 reference2.size = span[j].cbBuffer;
219 reference2.type = span[j].BufferType;
220 if (reference2.size == 0)
221 {
222 reference2.offset = 0;
223 reference2.token = null;
224 continue;
225 }
226 int k;
227 for (k = 0; k < input.Length; k++)
228 {
229 if (span2[k] != null)
230 {
231 byte* ptr = (byte*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(span2[k], 0);
232 checked
233 {
234 if ((void*)span[j].pvBuffer >= ptr && unchecked((nuint)(void*)span[j].pvBuffer) + unchecked((nuint)reference2.size) <= unchecked((nuint)ptr) + unchecked((nuint)span2[k].Length))
235 {
236 reference2.offset = (int)(unchecked((byte*)(void*)span[j].pvBuffer) - ptr);
237 reference2.token = span2[k];
238 break;
239 }
240 }
241 }
242 }
243 if (k >= input.Length)
244 {
245 reference2.size = 0;
246 reference2.offset = 0;
247 reference2.token = null;
248 }
249 }
250 if (System.Net.NetEventSource.Log.IsEnabled() && num != 0)
251 {
252 System.Net.NetEventSource.Error(null, (num == 590625) ? System.SR.Format(System.SR.event_OperationReturnedSomething, op, "SEC_I_RENEGOTIATE") : System.SR.Format(System.SR.net_log_operation_failed_with_error, op, $"0x{0:X}"), "EncryptDecryptHelper");
253 }
254 return num;
255 }
256 }
257 }
258 }
259 }
260
261 public static bool QueryBlittableContextAttributes<T>(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext securityContext, global::Interop.SspiCli.ContextAttribute contextAttribute, ref T attribute) where T : unmanaged
262 {
263 Span<T> span = MemoryMarshal.CreateSpan(ref attribute, 1);
264 SafeHandle refHandle;
265 int num = secModule.QueryContextAttributes(securityContext, contextAttribute, MemoryMarshal.AsBytes(span), null, out refHandle);
266 using (refHandle)
267 {
268 if (num != 0)
269 {
270 if (System.Net.NetEventSource.Log.IsEnabled())
271 {
272 System.Net.NetEventSource.Error(null, $"ERROR = {ErrorDescription(num)}", "QueryBlittableContextAttributes");
273 }
274 return false;
275 }
276 return true;
277 }
278 }
279
280 public static string ErrorDescription(int errorCode)
281 {
282 if (errorCode == -1)
283 {
284 return "An exception when invoking Win32 API";
285 }
286 return (global::Interop.SECURITY_STATUS)errorCode switch
287 {
288 global::Interop.SECURITY_STATUS.InvalidHandle => "Invalid handle",
289 global::Interop.SECURITY_STATUS.InvalidToken => "Invalid token",
290 global::Interop.SECURITY_STATUS.ContinueNeeded => "Continue needed",
291 global::Interop.SECURITY_STATUS.IncompleteMessage => "Message incomplete",
292 global::Interop.SECURITY_STATUS.WrongPrincipal => "Wrong principal",
293 global::Interop.SECURITY_STATUS.TargetUnknown => "Target unknown",
294 global::Interop.SECURITY_STATUS.PackageNotFound => "Package not found",
295 global::Interop.SECURITY_STATUS.BufferNotEnough => "Buffer not enough",
296 global::Interop.SECURITY_STATUS.MessageAltered => "Message altered",
297 global::Interop.SECURITY_STATUS.UntrustedRoot => "Untrusted root",
298 _ => "0x" + errorCode.ToString("x", NumberFormatInfo.InvariantInfo),
299 };
300 }
301}
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static System.Net.Security.SafeFreeCredentials AcquireDefaultCredential(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent)
static int MakeSignature(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext context, Span< System.Net.Security.SecurityBuffer > input, uint sequenceNumber)
static string ErrorDescription(int errorCode)
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 unsafe int EncryptDecryptHelper(OP op, System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext context, Span< System.Net.Security.SecurityBuffer > input, uint sequenceNumber)
static bool QueryBlittableContextAttributes< T >(System.Net.ISSPIInterface secModule, System.Net.Security.SafeDeleteContext securityContext, global::Interop.SspiCli.ContextAttribute contextAttribute, Type safeHandleType, out SafeHandle sspiHandle, ref T attribute)
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 System.Net.SecurityPackageInfoClass[] EnumerateSecurityPackages(System.Net.ISSPIInterface secModule)
static System.Net.Security.SafeFreeCredentials AcquireCredentialsHandle(System.Net.ISSPIInterface secModule, string package, global::Interop.SspiCli.CredentialUse intent, ref System.Net.Security.SafeSspiAuthDataHandle authdata)
static unsafe int AcquireDefaultCredential(string package, global::Interop.SspiCli.CredentialUse intent, out System.Net.Security.SafeFreeCredentials outCredential)
static unsafe int AcquireCredentialsHandle(string package, global::Interop.SspiCli.CredentialUse intent, ref System.Net.Security.SafeSspiAuthDataHandle authdata, out System.Net.Security.SafeFreeCredentials outCredential)
static unsafe IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Definition Marshal.cs:775
static string net_securitypackagesupport
Definition SR.cs:156
static string event_OperationReturnedSomething
Definition SR.cs:24
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_log_operation_failed_with_error
Definition SR.cs:154
Definition SR.cs:7
static readonly IntPtr Zero
Definition IntPtr.cs:18
unsafe void Clear()
Definition Span.cs:198