Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ListenerClientCertAsyncResult.cs
Go to the documentation of this file.
6
7namespace System.Net;
8
10{
12
14
15 private byte[] _backingBuffer;
16
17 private unsafe global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO* _memoryBlob;
18
19 private uint _size;
20
21 private unsafe static readonly IOCompletionCallback s_IOCallback = WaitCallback;
22
24
25 internal unsafe global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO* RequestBlob => _memoryBlob;
26
27 internal ListenerClientCertAsyncResult(ThreadPoolBoundHandle boundHandle, object asyncObject, object userState, AsyncCallback callback, uint size)
28 : base(asyncObject, userState, callback)
29 {
30 _boundHandle = boundHandle;
31 Reset(size);
32 }
33
34 internal unsafe void Reset(uint size)
35 {
36 if (size != _size)
37 {
38 if (_size != 0)
39 {
41 }
42 _size = size;
43 if (size == 0)
44 {
45 _pOverlapped = null;
46 _memoryBlob = null;
47 _backingBuffer = null;
48 }
49 else
50 {
51 _backingBuffer = new byte[checked((int)size)];
53 _memoryBlob = (global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(_backingBuffer, 0);
54 }
55 }
56 }
57
58 internal void IOCompleted(uint errorCode, uint numBytes)
59 {
60 IOCompleted(this, errorCode, numBytes);
61 }
62
63 private unsafe static void IOCompleted(ListenerClientCertAsyncResult asyncResult, uint errorCode, uint numBytes)
64 {
65 HttpListenerRequest httpListenerRequest = (HttpListenerRequest)asyncResult.AsyncObject;
66 object result = null;
67 try
68 {
69 if (errorCode == 234)
70 {
71 global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO* requestBlob = asyncResult.RequestBlob;
72 asyncResult.Reset(numBytes + requestBlob->CertEncodedSize);
73 uint num = 0u;
74 errorCode = global::Interop.HttpApi.HttpReceiveClientCertificate(httpListenerRequest.HttpListenerContext.RequestQueueHandle, httpListenerRequest._connectionId, 0u, asyncResult._memoryBlob, asyncResult._size, &num, asyncResult._pOverlapped);
75 switch (errorCode)
76 {
77 case 0u:
79 {
80 return;
81 }
82 break;
83 case 997u:
84 return;
85 }
86 }
87 if (errorCode != 0)
88 {
89 asyncResult.ErrorCode = (int)errorCode;
90 result = new HttpListenerException((int)errorCode);
91 }
92 else
93 {
94 global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO* memoryBlob = asyncResult._memoryBlob;
95 if (memoryBlob != null)
96 {
97 if (System.Net.NetEventSource.Log.IsEnabled())
98 {
99 System.Net.NetEventSource.Info(null, $"pClientCertInfo:{(IntPtr)memoryBlob} pClientCertInfo->CertFlags: {memoryBlob->CertFlags} pClientCertInfo->CertEncodedSize: {memoryBlob->CertEncodedSize} pClientCertInfo->pCertEncoded: {(IntPtr)memoryBlob->pCertEncoded} pClientCertInfo->Token: {(IntPtr)memoryBlob->Token} pClientCertInfo->CertDeniedByMapper: {memoryBlob->CertDeniedByMapper}", "IOCompleted");
100 }
101 if (memoryBlob->pCertEncoded != null)
102 {
103 try
104 {
105 byte[] array = new byte[memoryBlob->CertEncodedSize];
106 Marshal.Copy((IntPtr)memoryBlob->pCertEncoded, array, 0, array.Length);
107 X509Certificate2 x509Certificate2 = (httpListenerRequest.ClientCertificate = new X509Certificate2(array));
108 result = x509Certificate2;
109 }
110 catch (CryptographicException ex)
111 {
112 if (System.Net.NetEventSource.Log.IsEnabled())
113 {
114 System.Net.NetEventSource.Info(null, $"HttpListenerRequest: {httpListenerRequest} caught CryptographicException: {ex}", "IOCompleted");
115 }
116 result = ex;
117 }
118 catch (SecurityException ex2)
119 {
120 if (System.Net.NetEventSource.Log.IsEnabled())
121 {
122 System.Net.NetEventSource.Info(null, $"HttpListenerRequest: {httpListenerRequest} caught SecurityException: {ex2}", "IOCompleted");
123 }
124 result = ex2;
125 }
126 }
127 httpListenerRequest.SetClientCertificateError((int)memoryBlob->CertFlags);
128 }
129 }
130 if (System.Net.NetEventSource.Log.IsEnabled())
131 {
132 System.Net.NetEventSource.Info(null, "Calling Complete()", "IOCompleted");
133 }
134 }
135 catch (Exception ex3) when (!System.Net.ExceptionCheck.IsFatal(ex3))
136 {
137 result = ex3;
138 }
139 finally
140 {
141 if (errorCode != 997)
142 {
143 httpListenerRequest.ClientCertState = ListenerClientCertState.Completed;
144 }
145 }
146 asyncResult.InvokeCallback(result);
147 }
148
149 private unsafe static void WaitCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
150 {
152 if (System.Net.NetEventSource.Log.IsEnabled())
153 {
154 System.Net.NetEventSource.Info(null, $"errorCode:[{errorCode}] numBytes:[{numBytes}] nativeOverlapped:[{(long)nativeOverlapped}]", "WaitCallback");
155 }
156 IOCompleted(asyncResult, errorCode, numBytes);
157 }
158
159 protected unsafe override void Cleanup()
160 {
161 if (_pOverlapped != null)
162 {
163 _memoryBlob = null;
165 _pOverlapped = null;
166 _boundHandle = null;
167 }
168 GC.SuppressFinalize(this);
169 base.Cleanup();
170 }
171
181}
static bool HasShutdownStarted
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static bool IsFatal(Exception exception)
void SetClientCertificateError(int clientCertificateError)
static readonly bool SkipIOCPCallbackOnSuccess
static unsafe void WaitCallback(uint errorCode, uint numBytes, NativeOverlapped *nativeOverlapped)
ListenerClientCertAsyncResult(ThreadPoolBoundHandle boundHandle, object asyncObject, object userState, AsyncCallback callback, uint size)
unsafe global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO * _memoryBlob
void IOCompleted(uint errorCode, uint numBytes)
unsafe global::Interop.HttpApi.HTTP_SSL_CLIENT_CERT_INFO * RequestBlob
static unsafe readonly IOCompletionCallback s_IOCallback
static unsafe void IOCompleted(ListenerClientCertAsyncResult asyncResult, uint errorCode, uint numBytes)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static unsafe IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Definition Marshal.cs:775
unsafe void FreeNativeOverlapped(NativeOverlapped *overlapped)
unsafe NativeOverlapped * AllocateNativeOverlapped(IOCompletionCallback callback, object? state, object? pinData)
static unsafe? object GetNativeOverlappedState(NativeOverlapped *overlapped)
unsafe delegate void IOCompletionCallback(uint errorCode, uint numBytes, NativeOverlapped *pOVERLAP)