Add locking to SecureTransport P/Invokes (dotnet/corefx#24833)
authorJeremy Barton <jbarton@microsoft.com>
Tue, 24 Oct 2017 19:13:44 +0000 (12:13 -0700)
committerStephen Toub <stoub@microsoft.com>
Tue, 24 Oct 2017 19:13:44 +0000 (15:13 -0400)
Makes the macOS version of SslStream capable of doing one read and one write
"at the same time" without taking down the process.

Commit migrated from https://github.com/dotnet/corefx/commit/debf4bcaf4cd8e200c88c605b612cb4fa4f9150b

src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.OSX.cs

index 5006b1f..54f8303 100644 (file)
@@ -131,7 +131,16 @@ namespace System.Net.Security
                     MemoryHandle memHandle = input.Retain(pin: true);
                     try
                     {
-                        PAL_TlsIo status = Interop.AppleCrypto.SslWrite(sslHandle, (byte*)memHandle.Pointer, input.Length, out int written);
+                        PAL_TlsIo status;
+
+                        lock (sslHandle)
+                        {
+                            status = Interop.AppleCrypto.SslWrite(
+                                sslHandle,
+                                (byte*)memHandle.Pointer,
+                                input.Length,
+                                out int written);
+                        }
 
                         if (status < 0)
                         {
@@ -191,7 +200,12 @@ namespace System.Net.Security
                     fixed (byte* offsetInput = &buffer[offset])
                     {
                         int written;
-                        PAL_TlsIo status = Interop.AppleCrypto.SslRead(sslHandle, offsetInput, count, out written);
+                        PAL_TlsIo status;
+
+                        lock (sslHandle)
+                        {
+                            status = Interop.AppleCrypto.SslRead(sslHandle, offsetInput, count, out written);
+                        }
 
                         if (status < 0)
                         {
@@ -290,7 +304,13 @@ namespace System.Net.Security
                     sslContext.Write(inputBuffer.token, inputBuffer.offset, inputBuffer.size);
                 }
 
-                SecurityStatusPal status = PerformHandshake(sslContext.SslContext);
+                SafeSslHandle sslHandle = sslContext.SslContext;
+                SecurityStatusPal status;
+
+                lock (sslHandle)
+                {
+                    status = PerformHandshake(sslHandle);
+                }
 
                 byte[] output = sslContext.ReadPendingWrites();
                 outputBuffer.offset = 0;
@@ -351,7 +371,13 @@ namespace System.Net.Security
             SafeDeleteContext securityContext)
         {
             SafeDeleteSslContext sslContext = ((SafeDeleteSslContext)securityContext);
-            int osStatus = Interop.AppleCrypto.SslShutdown(sslContext.SslContext);
+            SafeSslHandle sslHandle = sslContext.SslContext;
+            int osStatus;
+
+            lock (sslHandle)
+            {
+                osStatus = Interop.AppleCrypto.SslShutdown(sslHandle);
+            }
 
             if (osStatus == 0)
             {