Skip to content

Commit 601111c

Browse files
committed
[unitytls] Mono convention compliance fixes
1 parent 3843d8e commit 601111c

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

mcs/class/System/Mono.UnityTls/UnityTlsContext.cs

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ unsafe internal class UnityTlsContext : MobileTlsContext
3131
private const bool ActivateTracing = false;
3232

3333
// Native UnityTls objects
34-
private UnityTls.unitytls_tlsctx* m_TlsContext = null;
35-
private UnityTls.unitytls_x509list* m_RequestedClientCertChain = null;
36-
private UnityTls.unitytls_key* m_RequestedClientKey = null;
34+
UnityTls.unitytls_tlsctx* tlsContext = null;
35+
UnityTls.unitytls_x509list* requestedClientCertChain = null;
36+
UnityTls.unitytls_key* requestedClientKey = null;
3737

3838
// States and certificates
39-
X509Certificate m_LocalClientCertificate;
40-
X509Certificate m_RemoteCertificate;
41-
MonoTlsConnectionInfo m_Connectioninfo;
42-
bool m_IsAuthenticated = false;
43-
bool m_HasContext = false;
39+
X509Certificate localClientCertificate;
40+
X509Certificate remoteCertificate;
41+
MonoTlsConnectionInfo connectioninfo;
42+
bool isAuthenticated = false;
43+
bool hasContext = false;
4444

4545
// Memory-buffer
46-
byte [] m_WriteBuffer;
47-
byte [] m_ReadBuffer;
46+
byte [] writeBuffer;
47+
byte [] readBuffer;
4848

49-
GCHandle m_handle;
49+
GCHandle handle;
5050
Exception lastException;
5151

5252
public UnityTlsContext (
@@ -57,7 +57,7 @@ public UnityTlsContext (
5757
: base (parent, serverMode, targetHost, enabledProtocols, serverCertificate, clientCertificates, askForClientCert)
5858
{
5959
// Need GCHandle to get a consistent pointer to this instance
60-
m_handle = GCHandle.Alloc (this);
60+
handle = GCHandle.Alloc (this);
6161

6262
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
6363

@@ -70,7 +70,7 @@ public UnityTlsContext (
7070
UnityTls.unitytls_tlsctx_callbacks callbacks = new UnityTls.unitytls_tlsctx_callbacks {
7171
write = WriteCallback,
7272
read = ReadCallback,
73-
data = (void*)(IntPtr)m_handle,
73+
data = (void*)(IntPtr)handle,
7474
};
7575

7676
if (serverMode) {
@@ -80,14 +80,14 @@ public UnityTlsContext (
8080
var serverKeyRef = UnityTls.NativeInterface.unitytls_key_get_ref (serverPrivateKey, &errorState);
8181
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to parse server key/certificate");
8282

83-
m_TlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
83+
tlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
8484

8585
if (askForClientCert) {
8686
UnityTls.unitytls_x509list* clientAuthCAList = null;
8787
try {
8888
clientAuthCAList = UnityTls.NativeInterface.unitytls_x509list_create (&errorState);
8989
var clientAuthCAListRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (clientAuthCAList, &errorState);
90-
UnityTls.NativeInterface.unitytls_tlsctx_server_require_client_authentication (m_TlsContext, clientAuthCAListRef, &errorState);
90+
UnityTls.NativeInterface.unitytls_tlsctx_server_require_client_authentication (tlsContext, clientAuthCAListRef, &errorState);
9191
} finally {
9292
UnityTls.NativeInterface.unitytls_x509list_free (clientAuthCAList);
9393
}
@@ -100,22 +100,22 @@ public UnityTlsContext (
100100
else {
101101
byte [] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
102102
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
103-
m_TlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
103+
tlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
104104
}
105105

106-
UnityTls.NativeInterface.unitytls_tlsctx_set_certificate_callback (m_TlsContext, CertificateCallback, (void*)(IntPtr)m_handle, &errorState);
106+
UnityTls.NativeInterface.unitytls_tlsctx_set_certificate_callback (tlsContext, CertificateCallback, (void*)(IntPtr)handle, &errorState);
107107
}
108108

109-
UnityTls.NativeInterface.unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
109+
UnityTls.NativeInterface.unitytls_tlsctx_set_x509verify_callback (tlsContext, VerifyCallback, (void*)(IntPtr)handle, &errorState);
110110

111111
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to create UnityTls context");
112112

113113
if (ActivateTracing) {
114-
UnityTls.NativeInterface.unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
114+
UnityTls.NativeInterface.unitytls_tlsctx_set_trace_callback (tlsContext, TraceCallback, null, &errorState);
115115
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to set trace callback");
116116
}
117117

118-
m_HasContext = true;
118+
hasContext = true;
119119
}
120120

121121
static private void ExtractNativeKeyAndChainFromManagedCertificate(X509Certificate cert, UnityTls.unitytls_errorstate* errorState, out UnityTls.unitytls_x509list* nativeCertChain, out UnityTls.unitytls_key* nativeKey)
@@ -144,22 +144,22 @@ static private void ExtractNativeKeyAndChainFromManagedCertificate(X509Certifica
144144
}
145145

146146
public override bool HasContext {
147-
get { return m_HasContext; }
147+
get { return hasContext; }
148148
}
149149
public override bool IsAuthenticated {
150-
get { return m_IsAuthenticated; }
150+
get { return isAuthenticated; }
151151
}
152152
public override MonoTlsConnectionInfo ConnectionInfo {
153-
get { return m_Connectioninfo; }
153+
get { return connectioninfo; }
154154
}
155155
internal override bool IsRemoteCertificateAvailable {
156-
get { return m_RemoteCertificate != null; }
156+
get { return remoteCertificate != null; }
157157
}
158158
internal override X509Certificate LocalClientCertificate {
159-
get { return m_LocalClientCertificate; }
159+
get { return localClientCertificate; }
160160
}
161161
public override X509Certificate RemoteCertificate {
162-
get { return m_RemoteCertificate; }
162+
get { return remoteCertificate; }
163163
}
164164
public override TlsProtocols NegotiatedProtocol {
165165
get { return ConnectionInfo.ProtocolVersion; }
@@ -178,7 +178,7 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
178178
lastException = null;
179179
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
180180
fixed (byte* bufferPtr = buffer) {
181-
numBytesRead = UnityTls.NativeInterface.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
181+
numBytesRead = UnityTls.NativeInterface.unitytls_tlsctx_read (tlsContext, bufferPtr + offset, count, &errorState);
182182
}
183183
if (lastException != null)
184184
throw lastException;
@@ -199,7 +199,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
199199
lastException = null;
200200
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
201201
fixed (byte* bufferPtr = buffer) {
202-
numBytesWritten = UnityTls.NativeInterface.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
202+
numBytesWritten = UnityTls.NativeInterface.unitytls_tlsctx_write (tlsContext, bufferPtr + offset, count, &errorState);
203203
}
204204
if (lastException != null)
205205
throw lastException;
@@ -215,12 +215,12 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
215215
public override void Shutdown ()
216216
{
217217
// Destroy native UnityTls objects
218-
UnityTls.NativeInterface.unitytls_x509list_free (m_RequestedClientCertChain);
219-
UnityTls.NativeInterface.unitytls_key_free (m_RequestedClientKey);
220-
UnityTls.NativeInterface.unitytls_tlsctx_free (m_TlsContext);
221-
m_TlsContext = null;
218+
UnityTls.NativeInterface.unitytls_x509list_free (requestedClientCertChain);
219+
UnityTls.NativeInterface.unitytls_key_free (requestedClientKey);
220+
UnityTls.NativeInterface.unitytls_tlsctx_free (tlsContext);
221+
tlsContext = null;
222222

223-
m_HasContext = false;
223+
hasContext = false;
224224
}
225225

226226
protected override void Dispose (bool disposing)
@@ -231,24 +231,24 @@ protected override void Dispose (bool disposing)
231231
Shutdown();
232232

233233
// reset states
234-
m_LocalClientCertificate = null;
235-
m_RemoteCertificate = null;
234+
localClientCertificate = null;
235+
remoteCertificate = null;
236236

237-
if (m_LocalClientCertificate != null) {
238-
m_LocalClientCertificate.Dispose ();
239-
m_LocalClientCertificate = null;
237+
if (localClientCertificate != null) {
238+
localClientCertificate.Dispose ();
239+
localClientCertificate = null;
240240
}
241-
if (m_RemoteCertificate != null) {
242-
m_RemoteCertificate.Dispose ();
243-
m_RemoteCertificate = null;
241+
if (remoteCertificate != null) {
242+
remoteCertificate.Dispose ();
243+
remoteCertificate = null;
244244
}
245245

246-
m_Connectioninfo = null;
247-
m_IsAuthenticated = false;
248-
m_HasContext = false;
246+
connectioninfo = null;
247+
isAuthenticated = false;
248+
hasContext = false;
249249
}
250250

251-
m_handle.Free();
251+
handle.Free();
252252

253253
} finally {
254254
base.Dispose (disposing);
@@ -264,7 +264,7 @@ public override void StartHandshake ()
264264

265265
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
266266
fixed (UnityTls.unitytls_ciphersuite* ciphersPtr = ciphers)
267-
UnityTls.NativeInterface.unitytls_tlsctx_set_supported_ciphersuites (m_TlsContext, ciphersPtr, ciphers.Length, &errorState);
267+
UnityTls.NativeInterface.unitytls_tlsctx_set_supported_ciphersuites (tlsContext, ciphersPtr, ciphers.Length, &errorState);
268268
Unity.Debug.CheckAndThrow (errorState, "Failed to set list of supported ciphers", AlertDescription.HandshakeFailure);
269269
}
270270
}
@@ -273,7 +273,7 @@ public override bool ProcessHandshake ()
273273
{
274274
lastException = null;
275275
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
276-
var result = UnityTls.NativeInterface.unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
276+
var result = UnityTls.NativeInterface.unitytls_tlsctx_process_handshake (tlsContext, &errorState);
277277
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
278278
return false;
279279
if (lastException != null)
@@ -299,10 +299,10 @@ public override void FinishHandshake ()
299299
{
300300
// Query some data. Ignore errors on the way since failure is not crucial.
301301
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
302-
var cipherSuite = UnityTls.NativeInterface.unitytls_tlsctx_get_ciphersuite(m_TlsContext, &errorState);
303-
var protocolVersion = UnityTls.NativeInterface.unitytls_tlsctx_get_protocol(m_TlsContext, &errorState);
302+
var cipherSuite = UnityTls.NativeInterface.unitytls_tlsctx_get_ciphersuite(tlsContext, &errorState);
303+
var protocolVersion = UnityTls.NativeInterface.unitytls_tlsctx_get_protocol(tlsContext, &errorState);
304304

305-
m_Connectioninfo = new MonoTlsConnectionInfo () {
305+
connectioninfo = new MonoTlsConnectionInfo () {
306306
CipherSuiteCode = (CipherSuiteCode)cipherSuite,
307307
ProtocolVersion = UnityTlsConversions.ConvertProtocolVersion(protocolVersion),
308308
PeerDomainName = ServerName
@@ -315,7 +315,7 @@ public override void FinishHandshake ()
315315
//HashAlgorithmType
316316
//ExchangeAlgorithmType
317317
};
318-
m_IsAuthenticated = true;
318+
isAuthenticated = true;
319319
}
320320

321321
[MonoPInvokeCallback (typeof (UnityTls.unitytls_tlsctx_write_callback))]
@@ -329,11 +329,11 @@ static private size_t WriteCallback (void* userData, byte* data, size_t bufferLe
329329
private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_errorstate* errorState)
330330
{
331331
try {
332-
if (m_WriteBuffer == null || m_WriteBuffer.Length < bufferLen)
333-
m_WriteBuffer = new byte[bufferLen];
334-
Marshal.Copy ((IntPtr)data, m_WriteBuffer, 0, bufferLen);
332+
if (writeBuffer == null || writeBuffer.Length < bufferLen)
333+
writeBuffer = new byte[bufferLen];
334+
Marshal.Copy ((IntPtr)data, writeBuffer, 0, bufferLen);
335335

336-
if (!Parent.InternalWrite (m_WriteBuffer, 0, bufferLen)) {
336+
if (!Parent.InternalWrite (writeBuffer, 0, bufferLen)) {
337337
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
338338
return 0;
339339
}
@@ -358,11 +358,11 @@ static private size_t ReadCallback (void* userData, byte* buffer, size_t bufferL
358358
private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_errorstate* errorState)
359359
{
360360
try {
361-
if (m_ReadBuffer == null || m_ReadBuffer.Length < bufferLen)
362-
m_ReadBuffer = new byte [bufferLen];
361+
if (readBuffer == null || readBuffer.Length < bufferLen)
362+
readBuffer = new byte [bufferLen];
363363

364364
bool wouldBlock;
365-
int numBytesRead = Parent.InternalRead (m_ReadBuffer, 0, bufferLen, out wouldBlock);
365+
int numBytesRead = Parent.InternalRead (readBuffer, 0, bufferLen, out wouldBlock);
366366
if (wouldBlock) {
367367
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
368368
return 0;
@@ -372,7 +372,7 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
372372
return 0;
373373
}
374374

375-
Marshal.Copy (m_ReadBuffer, 0, (IntPtr)buffer, bufferLen);
375+
Marshal.Copy (readBuffer, 0, (IntPtr)buffer, bufferLen);
376376
return numBytesRead;
377377
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
378378
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
@@ -394,7 +394,7 @@ private UnityTls.unitytls_x509verify_result VerifyCallback (UnityTls.unitytls_x5
394394
{
395395
try {
396396
X509CertificateCollection certificates = CertHelper.NativeChainToManagedCollection (chain, errorState);
397-
m_RemoteCertificate = new X509Certificate (certificates [0]);
397+
remoteCertificate = new X509Certificate (certificates [0]);
398398

399399
if (ValidateCertificate (certificates))
400400
return UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_SUCCESS;
@@ -419,23 +419,23 @@ static private void CertificateCallback (void* userData, UnityTls.unitytls_tlsct
419419
private void CertificateCallback (UnityTls.unitytls_tlsctx* ctx, Int8* cn, size_t cnLen, UnityTls.unitytls_x509name* caList, size_t caListLen, UnityTls.unitytls_x509list_ref* chain, UnityTls.unitytls_key_ref* key, UnityTls.unitytls_errorstate* errorState)
420420
{
421421
try {
422-
if (m_RemoteCertificate == null)
422+
if (remoteCertificate == null)
423423
throw new TlsException (AlertDescription.InternalError, "Cannot request client certificate before receiving one from the server.");
424424

425-
m_LocalClientCertificate = SelectClientCertificate (m_RemoteCertificate, null);
425+
localClientCertificate = SelectClientCertificate (remoteCertificate, null);
426426

427-
if (m_LocalClientCertificate == null) {
427+
if (localClientCertificate == null) {
428428
*chain = new UnityTls.unitytls_x509list_ref { handle = UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE };
429429
*key = new UnityTls.unitytls_key_ref { handle = UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE };
430430
} else {
431431
// Need to create native objects for client chain/key. Need to keep them cached.
432432
// Make sure we don't have old native objects still around.
433-
UnityTls.NativeInterface.unitytls_x509list_free (m_RequestedClientCertChain);
434-
UnityTls.NativeInterface.unitytls_key_free (m_RequestedClientKey);
433+
UnityTls.NativeInterface.unitytls_x509list_free (requestedClientCertChain);
434+
UnityTls.NativeInterface.unitytls_key_free (requestedClientKey);
435435

436-
ExtractNativeKeyAndChainFromManagedCertificate(m_LocalClientCertificate, errorState, out m_RequestedClientCertChain, out m_RequestedClientKey);
437-
*chain = UnityTls.NativeInterface.unitytls_x509list_get_ref (m_RequestedClientCertChain, errorState);
438-
*key = UnityTls.NativeInterface.unitytls_key_get_ref (m_RequestedClientKey, errorState);
436+
ExtractNativeKeyAndChainFromManagedCertificate(localClientCertificate, errorState, out requestedClientCertChain, out requestedClientKey);
437+
*chain = UnityTls.NativeInterface.unitytls_x509list_get_ref (requestedClientCertChain, errorState);
438+
*key = UnityTls.NativeInterface.unitytls_key_get_ref (requestedClientKey, errorState);
439439
}
440440

441441
Unity.Debug.CheckAndThrow (*errorState, "Failed to retrieve certificates on request.", AlertDescription.HandshakeFailure);

0 commit comments

Comments
 (0)