| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Diagnostics.Tracing; |
| | | 7 | | using System.Security.Authentication; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Security |
| | | 11 | | { |
| | | 12 | | [EventSource(Name = "System.Net.Security")] |
| | | 13 | | internal sealed partial class NetSecurityTelemetry : EventSource |
| | | 14 | | { |
| | | 15 | | private const string ActivitySourceName = "Experimental.System.Net.Security"; |
| | | 16 | | private const string ActivityName = ActivitySourceName + ".TlsHandshake"; |
| | | 17 | | |
| | 1 | 18 | | private static readonly ActivitySource s_activitySource = new ActivitySource(ActivitySourceName); |
| | | 19 | | |
| | | 20 | | private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe" |
| | 1 | 21 | | public static readonly NetSecurityTelemetry Log = new NetSecurityTelemetry(); |
| | | 22 | | |
| | | 23 | | private IncrementingPollingCounter? _tlsHandshakeRateCounter; |
| | | 24 | | private PollingCounter? _totalTlsHandshakesCounter; |
| | | 25 | | private PollingCounter? _currentTlsHandshakesCounter; |
| | | 26 | | private PollingCounter? _failedTlsHandshakesCounter; |
| | | 27 | | private PollingCounter? _sessionsOpenCounter; |
| | | 28 | | private PollingCounter? _sessionsOpenTls10Counter; |
| | | 29 | | private PollingCounter? _sessionsOpenTls11Counter; |
| | | 30 | | private PollingCounter? _sessionsOpenTls12Counter; |
| | | 31 | | private PollingCounter? _sessionsOpenTls13Counter; |
| | | 32 | | private EventCounter? _handshakeDurationCounter; |
| | | 33 | | private EventCounter? _handshakeDurationTls10Counter; |
| | | 34 | | private EventCounter? _handshakeDurationTls11Counter; |
| | | 35 | | private EventCounter? _handshakeDurationTls12Counter; |
| | | 36 | | private EventCounter? _handshakeDurationTls13Counter; |
| | | 37 | | |
| | | 38 | | private long _finishedTlsHandshakes; // Successfully and failed |
| | | 39 | | private long _startedTlsHandshakes; |
| | | 40 | | private long _failedTlsHandshakes; |
| | | 41 | | private long _sessionsOpen; |
| | | 42 | | private long _sessionsOpenTls10; |
| | | 43 | | private long _sessionsOpenTls11; |
| | | 44 | | private long _sessionsOpenTls12; |
| | | 45 | | private long _sessionsOpenTls13; |
| | | 46 | | |
| | 31 | 47 | | public static bool AnyTelemetryEnabled() => Log.IsEnabled() || s_activitySource.HasListeners(); |
| | | 48 | | |
| | | 49 | | protected override void OnEventCommand(EventCommandEventArgs command) |
| | 0 | 50 | | { |
| | 0 | 51 | | if (command.Command == EventCommand.Enable) |
| | 0 | 52 | | { |
| | 0 | 53 | | _tlsHandshakeRateCounter ??= new IncrementingPollingCounter("tls-handshake-rate", this, () => Interlocke |
| | 0 | 54 | | { |
| | 0 | 55 | | DisplayName = "TLS handshakes completed", |
| | 0 | 56 | | DisplayRateTimeScale = TimeSpan.FromSeconds(1) |
| | 0 | 57 | | }; |
| | | 58 | | |
| | 0 | 59 | | _totalTlsHandshakesCounter ??= new PollingCounter("total-tls-handshakes", this, () => Interlocked.Read(r |
| | 0 | 60 | | { |
| | 0 | 61 | | DisplayName = "Total TLS handshakes completed" |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | _currentTlsHandshakesCounter ??= new PollingCounter("current-tls-handshakes", this, () => -Interlocked.R |
| | 0 | 65 | | { |
| | 0 | 66 | | DisplayName = "Current TLS handshakes" |
| | 0 | 67 | | }; |
| | | 68 | | |
| | 0 | 69 | | _failedTlsHandshakesCounter ??= new PollingCounter("failed-tls-handshakes", this, () => Interlocked.Read |
| | 0 | 70 | | { |
| | 0 | 71 | | DisplayName = "Total TLS handshakes failed" |
| | 0 | 72 | | }; |
| | | 73 | | |
| | 0 | 74 | | _sessionsOpenCounter ??= new PollingCounter("all-tls-sessions-open", this, () => Interlocked.Read(ref _s |
| | 0 | 75 | | { |
| | 0 | 76 | | DisplayName = "All TLS Sessions Active" |
| | 0 | 77 | | }; |
| | | 78 | | |
| | 0 | 79 | | _sessionsOpenTls10Counter ??= new PollingCounter("tls10-sessions-open", this, () => Interlocked.Read(ref |
| | 0 | 80 | | { |
| | 0 | 81 | | DisplayName = "TLS 1.0 Sessions Active" |
| | 0 | 82 | | }; |
| | | 83 | | |
| | 0 | 84 | | _sessionsOpenTls11Counter ??= new PollingCounter("tls11-sessions-open", this, () => Interlocked.Read(ref |
| | 0 | 85 | | { |
| | 0 | 86 | | DisplayName = "TLS 1.1 Sessions Active" |
| | 0 | 87 | | }; |
| | | 88 | | |
| | 0 | 89 | | _sessionsOpenTls12Counter ??= new PollingCounter("tls12-sessions-open", this, () => Interlocked.Read(ref |
| | 0 | 90 | | { |
| | 0 | 91 | | DisplayName = "TLS 1.2 Sessions Active" |
| | 0 | 92 | | }; |
| | | 93 | | |
| | 0 | 94 | | _sessionsOpenTls13Counter ??= new PollingCounter("tls13-sessions-open", this, () => Interlocked.Read(ref |
| | 0 | 95 | | { |
| | 0 | 96 | | DisplayName = "TLS 1.3 Sessions Active" |
| | 0 | 97 | | }; |
| | | 98 | | |
| | 0 | 99 | | _handshakeDurationCounter ??= new EventCounter("all-tls-handshake-duration", this) |
| | 0 | 100 | | { |
| | 0 | 101 | | DisplayName = "TLS Handshake Duration", |
| | 0 | 102 | | DisplayUnits = "ms" |
| | 0 | 103 | | }; |
| | | 104 | | |
| | 0 | 105 | | _handshakeDurationTls10Counter ??= new EventCounter("tls10-handshake-duration", this) |
| | 0 | 106 | | { |
| | 0 | 107 | | DisplayName = "TLS 1.0 Handshake Duration", |
| | 0 | 108 | | DisplayUnits = "ms" |
| | 0 | 109 | | }; |
| | | 110 | | |
| | 0 | 111 | | _handshakeDurationTls11Counter ??= new EventCounter("tls11-handshake-duration", this) |
| | 0 | 112 | | { |
| | 0 | 113 | | DisplayName = "TLS 1.1 Handshake Duration", |
| | 0 | 114 | | DisplayUnits = "ms" |
| | 0 | 115 | | }; |
| | | 116 | | |
| | 0 | 117 | | _handshakeDurationTls12Counter ??= new EventCounter("tls12-handshake-duration", this) |
| | 0 | 118 | | { |
| | 0 | 119 | | DisplayName = "TLS 1.2 Handshake Duration", |
| | 0 | 120 | | DisplayUnits = "ms" |
| | 0 | 121 | | }; |
| | | 122 | | |
| | 0 | 123 | | _handshakeDurationTls13Counter ??= new EventCounter("tls13-handshake-duration", this) |
| | 0 | 124 | | { |
| | 0 | 125 | | DisplayName = "TLS 1.3 Handshake Duration", |
| | 0 | 126 | | DisplayUnits = "ms" |
| | 0 | 127 | | }; |
| | 0 | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | |
| | | 132 | | [Event(1, Level = EventLevel.Informational)] |
| | | 133 | | public void HandshakeStart(bool isServer, string targetHost) |
| | 0 | 134 | | { |
| | 0 | 135 | | Interlocked.Increment(ref _startedTlsHandshakes); |
| | | 136 | | |
| | 0 | 137 | | if (IsEnabled(EventLevel.Informational, EventKeywords.None)) |
| | 0 | 138 | | { |
| | 0 | 139 | | WriteEvent(eventId: 1, isServer, targetHost); |
| | 0 | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | [Event(2, Level = EventLevel.Informational)] |
| | | 144 | | private void HandshakeStop(SslProtocols protocol) |
| | 0 | 145 | | { |
| | 0 | 146 | | if (IsEnabled(EventLevel.Informational, EventKeywords.None)) |
| | 0 | 147 | | { |
| | 0 | 148 | | Debug.Assert(sizeof(SslProtocols) == 4); |
| | 0 | 149 | | WriteEvent(eventId: 2, (int)protocol); |
| | 0 | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | [Event(3, Level = EventLevel.Error)] |
| | | 154 | | private void HandshakeFailed(bool isServer, double elapsedMilliseconds, string exceptionMessage) |
| | 0 | 155 | | { |
| | 0 | 156 | | WriteEvent(eventId: 3, isServer, elapsedMilliseconds, exceptionMessage); |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | |
| | | 160 | | [NonEvent] |
| | | 161 | | public void HandshakeFailed(bool isServer, long startingTimestamp, string exceptionMessage) |
| | 0 | 162 | | { |
| | 0 | 163 | | Interlocked.Increment(ref _finishedTlsHandshakes); |
| | 0 | 164 | | Interlocked.Increment(ref _failedTlsHandshakes); |
| | | 165 | | |
| | 0 | 166 | | if (IsEnabled(EventLevel.Error, EventKeywords.None)) |
| | 0 | 167 | | { |
| | 0 | 168 | | HandshakeFailed(isServer, Stopwatch.GetElapsedTime(startingTimestamp).TotalMilliseconds, exceptionMessag |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | HandshakeStop(SslProtocols.None); |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | [NonEvent] |
| | | 175 | | public void HandshakeCompleted(SslProtocols protocol, long startingTimestamp, bool connectionOpen) |
| | 0 | 176 | | { |
| | 0 | 177 | | Interlocked.Increment(ref _finishedTlsHandshakes); |
| | | 178 | | |
| | 0 | 179 | | long dummy = 0; |
| | 0 | 180 | | ref long protocolSessionsOpen = ref dummy; |
| | 0 | 181 | | EventCounter? handshakeDurationCounter = null; |
| | | 182 | | |
| | 0 | 183 | | Debug.Assert(Enum.GetValues<SslProtocols>()[^1] == SslProtocols.Tls13, "Make sure to add a counter for new S |
| | | 184 | | |
| | 0 | 185 | | switch (protocol) |
| | | 186 | | { |
| | | 187 | | #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete |
| | | 188 | | case SslProtocols.Tls: |
| | 0 | 189 | | protocolSessionsOpen = ref _sessionsOpenTls10; |
| | 0 | 190 | | handshakeDurationCounter = _handshakeDurationTls10Counter; |
| | 0 | 191 | | break; |
| | | 192 | | |
| | | 193 | | case SslProtocols.Tls11: |
| | 0 | 194 | | protocolSessionsOpen = ref _sessionsOpenTls11; |
| | 0 | 195 | | handshakeDurationCounter = _handshakeDurationTls11Counter; |
| | 0 | 196 | | break; |
| | | 197 | | #pragma warning restore SYSLIB0039 |
| | | 198 | | |
| | | 199 | | case SslProtocols.Tls12: |
| | 0 | 200 | | protocolSessionsOpen = ref _sessionsOpenTls12; |
| | 0 | 201 | | handshakeDurationCounter = _handshakeDurationTls12Counter; |
| | 0 | 202 | | break; |
| | | 203 | | |
| | | 204 | | case SslProtocols.Tls13: |
| | 0 | 205 | | protocolSessionsOpen = ref _sessionsOpenTls13; |
| | 0 | 206 | | handshakeDurationCounter = _handshakeDurationTls13Counter; |
| | 0 | 207 | | break; |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | if (connectionOpen) |
| | 0 | 211 | | { |
| | 0 | 212 | | Interlocked.Increment(ref protocolSessionsOpen); |
| | 0 | 213 | | Interlocked.Increment(ref _sessionsOpen); |
| | 0 | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | double duration = Stopwatch.GetElapsedTime(startingTimestamp).TotalMilliseconds; |
| | 0 | 217 | | handshakeDurationCounter?.WriteMetric(duration); |
| | 0 | 218 | | _handshakeDurationCounter?.WriteMetric(duration); |
| | | 219 | | |
| | 0 | 220 | | HandshakeStop(protocol); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | [NonEvent] |
| | | 224 | | public void ConnectionClosed(SslProtocols protocol) |
| | 0 | 225 | | { |
| | 0 | 226 | | long count = 0; |
| | | 227 | | |
| | 0 | 228 | | switch (protocol) |
| | | 229 | | { |
| | | 230 | | #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete |
| | | 231 | | case SslProtocols.Tls: |
| | 0 | 232 | | count = Interlocked.Decrement(ref _sessionsOpenTls10); |
| | 0 | 233 | | break; |
| | | 234 | | |
| | | 235 | | case SslProtocols.Tls11: |
| | 0 | 236 | | count = Interlocked.Decrement(ref _sessionsOpenTls11); |
| | 0 | 237 | | break; |
| | | 238 | | #pragma warning restore SYSLIB0039 |
| | | 239 | | |
| | | 240 | | case SslProtocols.Tls12: |
| | 0 | 241 | | count = Interlocked.Decrement(ref _sessionsOpenTls12); |
| | 0 | 242 | | break; |
| | | 243 | | |
| | | 244 | | case SslProtocols.Tls13: |
| | 0 | 245 | | count = Interlocked.Decrement(ref _sessionsOpenTls13); |
| | 0 | 246 | | break; |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | Debug.Assert(count >= 0); |
| | | 250 | | |
| | 0 | 251 | | count = Interlocked.Decrement(ref _sessionsOpen); |
| | 0 | 252 | | Debug.Assert(count >= 0); |
| | 0 | 253 | | } |
| | | 254 | | |
| | | 255 | | |
| | | 256 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", |
| | | 257 | | Justification = EventSourceSuppressMessage)] |
| | | 258 | | [NonEvent] |
| | | 259 | | private unsafe void WriteEvent(int eventId, bool arg1, string? arg2) |
| | 0 | 260 | | { |
| | 0 | 261 | | arg2 ??= string.Empty; |
| | | 262 | | |
| | 0 | 263 | | fixed (char* arg2Ptr = arg2) |
| | 0 | 264 | | { |
| | | 265 | | const int NumEventDatas = 2; |
| | 0 | 266 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 267 | | |
| | 0 | 268 | | descrs[0] = new EventData |
| | 0 | 269 | | { |
| | 0 | 270 | | DataPointer = (IntPtr)(&arg1), |
| | 0 | 271 | | Size = sizeof(int) // EventSource defines bool as a 32-bit type |
| | 0 | 272 | | }; |
| | 0 | 273 | | descrs[1] = new EventData |
| | 0 | 274 | | { |
| | 0 | 275 | | DataPointer = (IntPtr)(arg2Ptr), |
| | 0 | 276 | | Size = (arg2.Length + 1) * sizeof(char) |
| | 0 | 277 | | }; |
| | | 278 | | |
| | 0 | 279 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 280 | | } |
| | 0 | 281 | | } |
| | | 282 | | |
| | | 283 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", |
| | | 284 | | Justification = EventSourceSuppressMessage)] |
| | | 285 | | [NonEvent] |
| | | 286 | | private unsafe void WriteEvent(int eventId, bool arg1, double arg2, string? arg3) |
| | 0 | 287 | | { |
| | 0 | 288 | | arg3 ??= string.Empty; |
| | | 289 | | |
| | 0 | 290 | | fixed (char* arg3Ptr = arg3) |
| | 0 | 291 | | { |
| | | 292 | | const int NumEventDatas = 3; |
| | 0 | 293 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 294 | | |
| | 0 | 295 | | descrs[0] = new EventData |
| | 0 | 296 | | { |
| | 0 | 297 | | DataPointer = (IntPtr)(&arg1), |
| | 0 | 298 | | Size = sizeof(int) // EventSource defines bool as a 32-bit type |
| | 0 | 299 | | }; |
| | 0 | 300 | | descrs[1] = new EventData |
| | 0 | 301 | | { |
| | 0 | 302 | | DataPointer = (IntPtr)(&arg2), |
| | 0 | 303 | | Size = sizeof(double) |
| | 0 | 304 | | }; |
| | 0 | 305 | | descrs[2] = new EventData |
| | 0 | 306 | | { |
| | 0 | 307 | | DataPointer = (IntPtr)(arg3Ptr), |
| | 0 | 308 | | Size = (arg3.Length + 1) * sizeof(char) |
| | 0 | 309 | | }; |
| | | 310 | | |
| | 0 | 311 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | [NonEvent] |
| | | 316 | | public static Activity? StartActivity(SslStream stream) |
| | 0 | 317 | | { |
| | 0 | 318 | | using Activity? activity = s_activitySource.StartActivity(ActivityName); |
| | 0 | 319 | | if (activity is not null) |
| | 0 | 320 | | { |
| | 0 | 321 | | activity.DisplayName = stream.IsServer ? "TLS server handshake" : $"TLS client handshake {stream.TargetH |
| | 0 | 322 | | if (activity.IsAllDataRequested && !stream.IsServer) |
| | 0 | 323 | | { |
| | 0 | 324 | | activity.SetTag("server.address", stream.TargetHostName); |
| | 0 | 325 | | } |
| | 0 | 326 | | } |
| | 0 | 327 | | return activity; |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | [NonEvent] |
| | | 331 | | public static void StopActivity(Activity? activity, Exception? exception, SslStream stream) |
| | 0 | 332 | | { |
| | 0 | 333 | | if (activity?.IsAllDataRequested != true) return; |
| | | 334 | | |
| | 0 | 335 | | SslProtocols protocol = stream.GetSslProtocolInternal(); |
| | 0 | 336 | | (string? protocolName, string? protocolVersion) = GetNameAndVersionString(protocol); |
| | | 337 | | |
| | 0 | 338 | | if (protocolName is not null) |
| | 0 | 339 | | { |
| | 0 | 340 | | Debug.Assert(protocolVersion is not null); |
| | 0 | 341 | | activity.SetTag("tls.protocol.name", protocolName); |
| | 0 | 342 | | activity.SetTag("tls.protocol.version", protocolVersion); |
| | 0 | 343 | | } |
| | | 344 | | |
| | 0 | 345 | | if (exception is not null) |
| | 0 | 346 | | { |
| | 0 | 347 | | activity.SetStatus(ActivityStatusCode.Error); |
| | 0 | 348 | | activity.SetTag("error.type", exception.GetType().FullName); |
| | 0 | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | static (string?, string?) GetNameAndVersionString(SslProtocols protocol) => protocol switch |
| | 0 | 352 | | { |
| | 0 | 353 | | #pragma warning disable 0618 // Ssl2, Ssl3 are deprecated. |
| | 0 | 354 | | SslProtocols.Ssl2 => ("ssl", "2"), |
| | 0 | 355 | | SslProtocols.Ssl3 => ("ssl", "3"), |
| | 0 | 356 | | #pragma warning restore 0618 |
| | 0 | 357 | | #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete. |
| | 0 | 358 | | SslProtocols.Tls => ("tls", "1"), |
| | 0 | 359 | | SslProtocols.Tls12 => ("tls", "1.2"), |
| | 0 | 360 | | #pragma warning restore SYSLIB0039 |
| | 0 | 361 | | SslProtocols.Tls13 => ("tls", "1.3"), |
| | 0 | 362 | | _ => (null, null) |
| | 0 | 363 | | }; |
| | 0 | 364 | | } |
| | | 365 | | } |
| | | 366 | | } |
| | | 367 | | |