improve correlation for EventSource events in SslStream (#36633)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Tue, 16 Jun 2020 02:10:26 +0000 (19:10 -0700)
committerGitHub <noreply@github.com>
Tue, 16 Jun 2020 02:10:26 +0000 (19:10 -0700)
* improve corelation for EventSource events in SslStream

* fix UnitTests build

* feedback from review

* add line

* add missing reference

Co-authored-by: Tomas Weinfurt <furt@Shining.local>
src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs
src/libraries/System.Net.Security/src/System.Net.Security.csproj
src/libraries/System.Net.Security/src/System/Net/Security/NetEventSource.Security.cs
src/libraries/System.Net.Security/src/System/Net/Security/SecureChannel.cs
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs
src/libraries/System.Net.Security/tests/UnitTests/System.Net.Security.Unit.Tests.csproj

index f476d85..fa06462 100644 (file)
@@ -740,6 +740,109 @@ namespace System.Net
                 }
             }
         }
+
+        [NonEvent]
+        private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8)
+        {
+            if (IsEnabled())
+            {
+                if (arg1 == null) arg1 = "";
+
+                fixed (char* arg1Ptr = arg1)
+                {
+                    const int NumEventDatas = 8;
+                    var descrs = stackalloc EventData[NumEventDatas];
+
+                    descrs[0] = new EventData
+                    {
+                        DataPointer = (IntPtr)(arg1Ptr),
+                        Size = (arg1.Length + 1) * sizeof(char)
+                    };
+                    descrs[1] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg2),
+                        Size = sizeof(int)
+                    };
+                    descrs[2] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg3),
+                        Size = sizeof(int)
+                    };
+                    descrs[3] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg4),
+                        Size = sizeof(int)
+                    };
+                    descrs[4] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg5),
+                        Size = sizeof(int)
+                    };
+                    descrs[5] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg6),
+                        Size = sizeof(int)
+                    };
+                    descrs[6] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg7),
+                        Size = sizeof(int)
+                    };
+                    descrs[7] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg8),
+                        Size = sizeof(int)
+                    };
+
+                    WriteEventCore(eventId, NumEventDatas, descrs);
+                }
+            }
+        }
+
+        [NonEvent]
+        private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3, int arg4, int arg5)
+        {
+            if (IsEnabled())
+            {
+                if (arg1 == null) arg1 = "";
+                if (arg2 == null) arg2 = "";
+
+                fixed (char* arg1Ptr = arg1)
+                fixed (char* arg2Ptr = arg2)
+                {
+                    const int NumEventDatas = 5;
+                    var descrs = stackalloc EventData[NumEventDatas];
+
+                    descrs[0] = new EventData
+                    {
+                        DataPointer = (IntPtr)(arg1Ptr),
+                        Size = (arg1.Length + 1) * sizeof(char)
+                    };
+                    descrs[1] = new EventData
+                    {
+                        DataPointer = (IntPtr)(arg2Ptr),
+                        Size = (arg2.Length + 1) * sizeof(char)
+                    };
+                    descrs[2] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg3),
+                        Size = sizeof(int)
+                    };
+                    descrs[3] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg4),
+                        Size = sizeof(int)
+                    };
+                    descrs[4] = new EventData
+                    {
+                        DataPointer = (IntPtr)(&arg5),
+                        Size = sizeof(int)
+                    };
+
+                    WriteEventCore(eventId, NumEventDatas, descrs);
+                }
+            }
+        }
         #endregion
     }
 }
index d91dfa7..a4a3356 100644 (file)
     <Reference Include="System.Linq" />
     <Reference Include="System.Memory" />
     <Reference Include="System.Net.Primitives" />
+    <Reference Include="System.Net.Sockets" />
     <Reference Include="System.Runtime" />
     <Reference Include="System.Runtime.Extensions" />
     <Reference Include="System.Runtime.InteropServices" />
index d05d8c8..e21c04c 100644 (file)
@@ -4,7 +4,9 @@
 
 using System.Diagnostics.Tracing;
 using System.Globalization;
+using System.IO;
 using System.Net.Security;
+using System.Net.Sockets;
 using System.Security.Authentication;
 using System.Security.Cryptography.X509Certificates;
 
@@ -34,7 +36,8 @@ namespace System.Net
         private const int RemoteVertificateValidId = RemoteCertificateErrorId + 1;
         private const int RemoteCertificateSuccesId = RemoteVertificateValidId + 1;
         private const int RemoteCertificateInvalidId = RemoteCertificateSuccesId + 1;
-        private const int SentFrameId = RemoteCertificateInvalidId + 1;
+        private const int SslStreamCtorId = RemoteCertificateInvalidId + 1;
+        private const int SentFrameId = SslStreamCtorId + 1;
         private const int ReceivedFrameId = SentFrameId + 1;
 
         [Event(EnumerateSecurityPackagesId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
@@ -56,16 +59,50 @@ namespace System.Net
         }
 
         [NonEvent]
-        public void SecureChannelCtor(SecureChannel secureChannel, string hostname, X509CertificateCollection? clientCertificates, EncryptionPolicy encryptionPolicy)
+        public void SslStreamCtor(SslStream sslStream, Stream innerStream)
         {
             if (IsEnabled())
             {
-                SecureChannelCtor(hostname, GetHashCode(secureChannel), clientCertificates?.Count ?? 0, encryptionPolicy);
+                string? localId = null;
+                string? remoteId = null;
+
+                NetworkStream? ns = innerStream as NetworkStream;
+                if (ns != null)
+                {
+                    try
+                    {
+                        localId = ns.Socket.LocalEndPoint?.ToString();
+                        remoteId = ns.Socket.RemoteEndPoint?.ToString();
+
+                    }
+                    catch { };
+                }
+
+                if (localId == null)
+                {
+                    localId = IdOf(innerStream);
+                }
+
+                SslStreamCtor(IdOf(sslStream), localId, remoteId);
             }
         }
+
+        [Event(SslStreamCtorId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
+        private unsafe void SslStreamCtor(string thisOrContextObject, string? localId, string? remoteId) =>
+              WriteEvent(SslStreamCtorId, thisOrContextObject, localId, remoteId);
+
+        [NonEvent]
+        public void SecureChannelCtor(SecureChannel secureChannel, SslStream sslStream, string hostname, X509CertificateCollection? clientCertificates, EncryptionPolicy encryptionPolicy)
+        {
+            if (IsEnabled())
+            {
+                SecureChannelCtor(IdOf(secureChannel), hostname, GetHashCode(secureChannel), clientCertificates?.Count ?? 0, encryptionPolicy);
+            }
+        }
+
         [Event(SecureChannelCtorId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
-        private unsafe void SecureChannelCtor(string hostname, int secureChannelHash, int clientCertificatesCount, EncryptionPolicy encryptionPolicy) =>
-            WriteEvent(SecureChannelCtorId, hostname, secureChannelHash, clientCertificatesCount, (int)encryptionPolicy);
+        private unsafe void SecureChannelCtor(string sslStream, string hostname, int secureChannelHash, int clientCertificatesCount, EncryptionPolicy encryptionPolicy) =>
+            WriteEvent(SecureChannelCtorId, sslStream, hostname, secureChannelHash, clientCertificatesCount, (int)encryptionPolicy);
 
         [NonEvent]
         public void LocatingPrivateKey(X509Certificate x509Certificate, SecureChannel secureChannel)
@@ -358,63 +395,5 @@ namespace System.Net
                 result = cert.ToString(fVerbose: true);
             }
         }
-
-        [NonEvent]
-        private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8)
-        {
-            if (IsEnabled())
-            {
-                if (arg1 == null) arg1 = "";
-
-                fixed (char* arg1Ptr = arg1)
-                {
-                    const int NumEventDatas = 8;
-                    var descrs = stackalloc EventData[NumEventDatas];
-
-                    descrs[0] = new EventData
-                    {
-                        DataPointer = (IntPtr)(arg1Ptr),
-                        Size = (arg1.Length + 1) * sizeof(char)
-                    };
-                    descrs[1] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg2),
-                        Size = sizeof(int)
-                    };
-                    descrs[2] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg3),
-                        Size = sizeof(int)
-                    };
-                    descrs[3] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg4),
-                        Size = sizeof(int)
-                    };
-                    descrs[4] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg5),
-                        Size = sizeof(int)
-                    };
-                    descrs[5] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg6),
-                        Size = sizeof(int)
-                    };
-                    descrs[6] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg7),
-                        Size = sizeof(int)
-                    };
-                    descrs[7] = new EventData
-                    {
-                        DataPointer = (IntPtr)(&arg8),
-                        Size = sizeof(int)
-                    };
-
-                    WriteEventCore(eventId, NumEventDatas, descrs);
-                }
-            }
-        }
     }
 }
index b770ef2..8b7e39f 100644 (file)
@@ -41,12 +41,12 @@ namespace System.Net.Security
         private static readonly Oid s_serverAuthOid = new Oid("1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.1");
         private static readonly Oid s_clientAuthOid = new Oid("1.3.6.1.5.5.7.3.2", "1.3.6.1.5.5.7.3.2");
 
-        internal SecureChannel(SslAuthenticationOptions sslAuthenticationOptions)
+        internal SecureChannel(SslAuthenticationOptions sslAuthenticationOptions, SslStream sslStream)
         {
             if (NetEventSource.IsEnabled)
             {
                 NetEventSource.Enter(this, sslAuthenticationOptions.TargetHost, sslAuthenticationOptions.ClientCertificates);
-                NetEventSource.Log.SecureChannelCtor(this, sslAuthenticationOptions.TargetHost!, sslAuthenticationOptions.ClientCertificates, sslAuthenticationOptions.EncryptionPolicy);
+                NetEventSource.Log.SecureChannelCtor(this, sslStream, sslAuthenticationOptions.TargetHost!, sslAuthenticationOptions.ClientCertificates, sslAuthenticationOptions.EncryptionPolicy);
             }
 
             SslStreamPal.VerifyPackageInfo();
index e50be3b..ef52738 100644 (file)
@@ -71,7 +71,7 @@ namespace System.Net.Security
                 {
                     _sslAuthenticationOptions.TargetHost = "?" + Interlocked.Increment(ref s_uniqueNameInteger).ToString(NumberFormatInfo.InvariantInfo);
                 }
-                _context = new SecureChannel(_sslAuthenticationOptions);
+                _context = new SecureChannel(_sslAuthenticationOptions, this);
             }
             catch (Win32Exception e)
             {
@@ -98,7 +98,7 @@ namespace System.Net.Security
 
             try
             {
-                _context = new SecureChannel(_sslAuthenticationOptions);
+                _context = new SecureChannel(_sslAuthenticationOptions, this);
             }
             catch (Win32Exception e)
             {
@@ -129,6 +129,8 @@ namespace System.Net.Security
         //
         private void CloseInternal()
         {
+            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
+
             _exception = s_disposedSentinel;
             _context?.Close();
 
@@ -153,6 +155,8 @@ namespace System.Net.Security
                 // Suppress finalizer if the read buffer was returned.
                 GC.SuppressFinalize(this);
             }
+
+            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
         }
 
         private SecurityStatusPal EncryptData(ReadOnlyMemory<byte> buffer, ref byte[] outBuffer, out int outSize)
index 132ad45..61011fd 100644 (file)
@@ -109,6 +109,8 @@ namespace System.Net.Security
             _certSelectionDelegate = userCertificateSelectionCallback == null ? null : new LocalCertSelectionCallback(UserCertSelectionCallbackWrapper);
 
             _innerStream = innerStream;
+
+            if (NetEventSource.IsEnabled) NetEventSource.Log.SslStreamCtor(this, innerStream);
         }
 
         public SslApplicationProtocol NegotiatedApplicationProtocol
index 50d702a..fd76bb9 100644 (file)
@@ -32,6 +32,8 @@
   </ItemGroup>
   <ItemGroup>
     <!-- Production code references -->
+    <Compile Include="..\..\src\System\Net\Security\NetEventSource.Security.cs"
+             Link="ProductionCode\System\Net\Security\NetEventSource.Security.cs" />
     <Compile Include="..\..\src\System\Net\Security\SslStream.cs"
              Link="ProductionCode\System\Net\Security\SslStream.cs" />
     <Compile Include="..\..\src\System\Net\Security\SslClientAuthenticationOptions.cs"
@@ -54,6 +56,8 @@
              Link="ProductionCode\Common\System\Net\TlsAlertType.cs" />
     <Compile Include="..\..\src\System\Net\Security\TlsAlertMessage.cs"
              Link="ProductionCode\Common\System\Net\TlsAlertMessage.cs" />
+    <Compile Include="..\..\src\System\Net\Security\TlsFrameHelper.cs"
+             Link="ProductionCode\Common\System\Net\TlsFrameHelper.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\SChannel\Interop.Alerts.cs"
              Link="Common\Interop\Windows\SChannel\Interop.Alerts.cs" />
     <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
@@ -71,4 +75,4 @@
     <Reference Include="System.Runtime.InteropServices" />
     <Reference Include="System.Threading" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>