Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NTAuthentication.cs
Go to the documentation of this file.
5
6namespace System.Net;
7
8[UnsupportedOSPlatform("tvos")]
9internal sealed class NTAuthentication
10{
11 private bool _isServer;
12
14
16
17 private string _spn;
18
19 private int _tokenSize;
20
22
24
25 private bool _isCompleted;
26
27 private string _package;
28
30
31 internal bool IsCompleted => _isCompleted;
32
33 internal bool IsValidContext
34 {
35 get
36 {
37 if (_securityContext != null)
38 {
39 return !_securityContext.IsInvalid;
40 }
41 return false;
42 }
43 }
44
45 internal NTAuthentication(bool isServer, string package, NetworkCredential credential, string spn, System.Net.ContextFlagsPal requestedContextFlags, ChannelBinding channelBinding)
46 {
47 Initialize(isServer, package, credential, spn, requestedContextFlags, channelBinding);
48 }
49
50 [MemberNotNull("_package")]
51 private void Initialize(bool isServer, string package, NetworkCredential credential, string spn, System.Net.ContextFlagsPal requestedContextFlags, ChannelBinding channelBinding)
52 {
53 if (System.Net.NetEventSource.Log.IsEnabled())
54 {
55 System.Net.NetEventSource.Info(this, $"package={package}, spn={spn}, requestedContextFlags={requestedContextFlags}", "Initialize");
56 }
58 _isServer = isServer;
59 _spn = spn;
60 _securityContext = null;
61 _requestedContextFlags = requestedContextFlags;
62 _package = package;
63 _channelBinding = channelBinding;
64 if (System.Net.NetEventSource.Log.IsEnabled())
65 {
66 System.Net.NetEventSource.Info(this, $"Peer SPN-> '{_spn}'", "Initialize");
67 }
68 if (credential == CredentialCache.DefaultCredentials)
69 {
70 if (System.Net.NetEventSource.Log.IsEnabled())
71 {
72 System.Net.NetEventSource.Info(this, "using DefaultCredentials", "Initialize");
73 }
75 }
76 else
77 {
79 }
80 }
81
82 internal void CloseContext()
83 {
84 if (_securityContext != null && !_securityContext.IsClosed)
85 {
86 _securityContext.Dispose();
87 }
88 }
89
90 internal string GetOutgoingBlob(string incomingBlob)
91 {
92 byte[] array = null;
93 if (incomingBlob != null && incomingBlob.Length > 0)
94 {
95 array = Convert.FromBase64String(incomingBlob);
96 }
97 byte[] array2 = null;
98 if ((IsValidContext || IsCompleted) && array == null)
99 {
100 _isCompleted = true;
101 }
102 else
103 {
104 array2 = GetOutgoingBlob(array, throwOnError: true, out var _);
105 }
106 string result = null;
107 if (array2 != null && array2.Length != 0)
108 {
109 result = Convert.ToBase64String(array2);
110 }
111 if (IsCompleted)
112 {
113 CloseContext();
114 }
115 return result;
116 }
117
118 internal byte[] GetOutgoingBlob(byte[] incomingBlob, bool throwOnError, out System.Net.SecurityStatusPal statusCode)
119 {
120 byte[] resultBlob = new byte[_tokenSize];
121 bool flag = _securityContext == null;
122 try
123 {
124 if (!_isServer)
125 {
127 if (System.Net.NetEventSource.Log.IsEnabled())
128 {
129 System.Net.NetEventSource.Info(this, $"SSPIWrapper.InitializeSecurityContext() returns statusCode:0x{(int)statusCode.ErrorCode:x8} ({statusCode})", "GetOutgoingBlob");
130 }
131 if (statusCode.ErrorCode == System.Net.SecurityStatusPalErrorCode.CompleteNeeded)
132 {
134 if (System.Net.NetEventSource.Log.IsEnabled())
135 {
136 System.Net.NetEventSource.Info(this, $"SSPIWrapper.CompleteAuthToken() returns statusCode:0x{(int)statusCode.ErrorCode:x8} ({statusCode})", "GetOutgoingBlob");
137 }
138 resultBlob = null;
139 }
140 }
141 else
142 {
144 if (System.Net.NetEventSource.Log.IsEnabled())
145 {
146 System.Net.NetEventSource.Info(this, $"SSPIWrapper.AcceptSecurityContext() returns statusCode:0x{(int)statusCode.ErrorCode:x8} ({statusCode})", "GetOutgoingBlob");
147 }
148 }
149 }
150 finally
151 {
152 if (flag)
153 {
154 _credentialsHandle?.Dispose();
155 }
156 }
157 if (statusCode.ErrorCode >= System.Net.SecurityStatusPalErrorCode.OutOfMemory)
158 {
159 CloseContext();
160 _isCompleted = true;
161 if (throwOnError)
162 {
164 }
165 return null;
166 }
167 if (flag && _credentialsHandle != null)
168 {
170 }
171 if (statusCode.ErrorCode == System.Net.SecurityStatusPalErrorCode.OK || (_isServer && statusCode.ErrorCode == System.Net.SecurityStatusPalErrorCode.CompleteNeeded))
172 {
173 _isCompleted = true;
174 }
175 else if (System.Net.NetEventSource.Log.IsEnabled())
176 {
177 System.Net.NetEventSource.Info(this, $"need continue statusCode:0x{(int)statusCode.ErrorCode:x8} ({statusCode}) _securityContext:{_securityContext}", "GetOutgoingBlob");
178 }
179 return resultBlob;
180 }
181}
static string ToBase64String(byte[] inArray)
Definition Convert.cs:2675
static unsafe byte[] FromBase64String(string s)
Definition Convert.cs:2904
static ICredentials DefaultCredentials
void Initialize(bool isServer, string package, NetworkCredential credential, string spn, System.Net.ContextFlagsPal requestedContextFlags, ChannelBinding channelBinding)
string GetOutgoingBlob(string incomingBlob)
System.Net.ContextFlagsPal _contextFlags
NTAuthentication(bool isServer, string package, NetworkCredential credential, string spn, System.Net.ContextFlagsPal requestedContextFlags, ChannelBinding channelBinding)
System.Net.Security.SafeFreeCredentials _credentialsHandle
System.Net.ContextFlagsPal _requestedContextFlags
byte[] GetOutgoingBlob(byte[] incomingBlob, bool throwOnError, out System.Net.SecurityStatusPal statusCode)
System.Net.Security.SafeDeleteContext _securityContext
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static System.Net.SecurityStatusPal AcceptSecurityContext(System.Net.Security.SafeFreeCredentials credentialsHandle, ref System.Net.Security.SafeDeleteContext securityContext, System.Net.ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref System.Net.ContextFlagsPal contextFlags)
static System.Net.SecurityStatusPal InitializeSecurityContext(ref System.Net.Security.SafeFreeCredentials credentialsHandle, ref System.Net.Security.SafeDeleteContext securityContext, string spn, System.Net.ContextFlagsPal requestedContextFlags, byte[] incomingBlob, ChannelBinding channelBinding, ref byte[] resultBlob, ref System.Net.ContextFlagsPal contextFlags)
static System.Net.Security.SafeFreeCredentials AcquireDefaultCredential(string package, bool isServer)
static System.Net.SecurityStatusPal CompleteAuthToken(ref System.Net.Security.SafeDeleteContext securityContext, byte[] incomingBlob)
static System.Net.Security.SafeFreeCredentials AcquireCredentialsHandle(string package, bool isServer, NetworkCredential credential)
static int QueryMaxTokenSize(string package)
static Win32Exception CreateExceptionFromError(System.Net.SecurityStatusPal statusCode)
static void CacheCredential(System.Net.Security.SafeFreeCredentials newHandle)