| | | 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.CodeAnalysis; |
| | | 5 | | using System.Diagnostics.Tracing; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Net.Security; |
| | | 8 | | using System.Net.Sockets; |
| | | 9 | | using System.Security.Authentication; |
| | | 10 | | using System.Security.Cryptography.X509Certificates; |
| | | 11 | | |
| | | 12 | | namespace System.Net |
| | | 13 | | { |
| | | 14 | | [EventSource(Name = "Private.InternalDiagnostics.System.Net.Security")] |
| | | 15 | | internal sealed partial class NetEventSource |
| | | 16 | | { |
| | | 17 | | #if WINDOWS |
| | | 18 | | // More events are defined in NetEventSource.Security.Windows.cs |
| | | 19 | | private const int LocatingPrivateKeyId = OperationReturnedSomethingId + 1; |
| | | 20 | | #else |
| | | 21 | | private const int LocatingPrivateKeyId = NextAvailableEventId; |
| | | 22 | | #endif |
| | | 23 | | private const int CertIsType2Id = LocatingPrivateKeyId + 1; |
| | | 24 | | private const int FoundCertInStoreId = CertIsType2Id + 1; |
| | | 25 | | private const int NotFoundCertInStoreId = FoundCertInStoreId + 1; |
| | | 26 | | private const int RemoteCertificateId = NotFoundCertInStoreId + 1; |
| | | 27 | | private const int CertificateFromDelegateId = RemoteCertificateId + 1; |
| | | 28 | | private const int NoDelegateNoClientCertId = CertificateFromDelegateId + 1; |
| | | 29 | | private const int NoDelegateButClientCertId = NoDelegateNoClientCertId + 1; |
| | | 30 | | private const int AttemptingRestartUsingCertId = NoDelegateButClientCertId + 1; |
| | | 31 | | private const int NoIssuersTryAllCertsId = AttemptingRestartUsingCertId + 1; |
| | | 32 | | private const int LookForMatchingCertsId = NoIssuersTryAllCertsId + 1; |
| | | 33 | | private const int SelectedCertId = LookForMatchingCertsId + 1; |
| | | 34 | | private const int CertsAfterFilteringId = SelectedCertId + 1; |
| | | 35 | | private const int FindingMatchingCertsId = CertsAfterFilteringId + 1; |
| | | 36 | | private const int UsingCachedCredentialId = FindingMatchingCertsId + 1; |
| | | 37 | | private const int SspiSelectedCipherSuitId = UsingCachedCredentialId + 1; |
| | | 38 | | private const int RemoteCertificateErrorId = SspiSelectedCipherSuitId + 1; |
| | | 39 | | private const int RemoteVertificateValidId = RemoteCertificateErrorId + 1; |
| | | 40 | | private const int RemoteCertificateSuccessId = RemoteVertificateValidId + 1; |
| | | 41 | | private const int RemoteCertificateInvalidId = RemoteCertificateSuccessId + 1; |
| | | 42 | | private const int SslStreamCtorId = RemoteCertificateInvalidId + 1; |
| | | 43 | | private const int SentFrameId = SslStreamCtorId + 1; |
| | | 44 | | private const int ReceivedFrameId = SentFrameId + 1; |
| | | 45 | | private const int CertificateFromCertContextId = ReceivedFrameId + 1; |
| | | 46 | | |
| | | 47 | | [NonEvent] |
| | | 48 | | public void SslStreamCtor(SslStream sslStream, Stream innerStream) |
| | 0 | 49 | | { |
| | 0 | 50 | | string? localId = null; |
| | 0 | 51 | | string? remoteId = null; |
| | | 52 | | |
| | 0 | 53 | | if (innerStream is NetworkStream ns) |
| | 0 | 54 | | { |
| | | 55 | | try |
| | 0 | 56 | | { |
| | 0 | 57 | | localId = ns.Socket.LocalEndPoint?.ToString(); |
| | 0 | 58 | | remoteId = ns.Socket.RemoteEndPoint?.ToString(); |
| | 0 | 59 | | } |
| | 0 | 60 | | catch { }; |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | localId ??= IdOf(innerStream); |
| | | 64 | | |
| | 0 | 65 | | SslStreamCtor(IdOf(sslStream), localId, remoteId); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | [Event(SslStreamCtorId, Level = EventLevel.Informational)] |
| | | 69 | | private void SslStreamCtor(string thisOrContextObject, string? localId, string? remoteId) => |
| | 0 | 70 | | WriteEvent(SslStreamCtorId, thisOrContextObject, localId, remoteId); |
| | | 71 | | |
| | | 72 | | [NonEvent] |
| | | 73 | | public void LocatingPrivateKey(X509Certificate x509Certificate, object instance) => |
| | 0 | 74 | | LocatingPrivateKey(x509Certificate.ToString(fVerbose: true), GetHashCode(instance)); |
| | | 75 | | |
| | | 76 | | [Event(LocatingPrivateKeyId, Level = EventLevel.Informational)] |
| | | 77 | | private void LocatingPrivateKey(string x509Certificate, int sslStreamHash) => |
| | 0 | 78 | | WriteEvent(LocatingPrivateKeyId, x509Certificate, sslStreamHash); |
| | | 79 | | |
| | | 80 | | [NonEvent] |
| | | 81 | | public void CertIsType2(object instance) => |
| | 0 | 82 | | CertIsType2(GetHashCode(instance)); |
| | | 83 | | |
| | | 84 | | [Event(CertIsType2Id, Level = EventLevel.Informational)] |
| | | 85 | | private void CertIsType2(int sslStreamHash) => |
| | 0 | 86 | | WriteEvent(CertIsType2Id, sslStreamHash); |
| | | 87 | | |
| | | 88 | | [NonEvent] |
| | | 89 | | public void FoundCertInStore(bool serverMode, object instance) => |
| | 0 | 90 | | FoundCertInStore(serverMode ? "LocalMachine" : "CurrentUser", GetHashCode(instance)); |
| | | 91 | | |
| | | 92 | | [Event(FoundCertInStoreId, Level = EventLevel.Informational)] |
| | | 93 | | private void FoundCertInStore(string store, int sslStreamHash) => |
| | 0 | 94 | | WriteEvent(FoundCertInStoreId, store, sslStreamHash); |
| | | 95 | | |
| | | 96 | | [NonEvent] |
| | | 97 | | public void NotFoundCertInStore(object instance) => |
| | 0 | 98 | | NotFoundCertInStore(GetHashCode(instance)); |
| | | 99 | | |
| | | 100 | | [Event(NotFoundCertInStoreId, Level = EventLevel.Informational)] |
| | | 101 | | private void NotFoundCertInStore(int sslStreamHash) => |
| | 0 | 102 | | WriteEvent(NotFoundCertInStoreId, sslStreamHash); |
| | | 103 | | |
| | | 104 | | [NonEvent] |
| | | 105 | | public void RemoteCertificate(X509Certificate? remoteCertificate) => |
| | 0 | 106 | | RemoteCertificate(remoteCertificate?.ToString(fVerbose: true)); |
| | | 107 | | |
| | | 108 | | [Event(RemoteCertificateId, Level = EventLevel.Informational)] |
| | | 109 | | private void RemoteCertificate(string? remoteCertificate) => |
| | 0 | 110 | | WriteEvent(RemoteCertificateId, remoteCertificate); |
| | | 111 | | |
| | | 112 | | [NonEvent] |
| | | 113 | | public void CertificateFromDelegate(SslStream SslStream) => |
| | 0 | 114 | | CertificateFromDelegate(GetHashCode(SslStream)); |
| | | 115 | | |
| | | 116 | | [Event(CertificateFromDelegateId, Level = EventLevel.Informational)] |
| | | 117 | | private void CertificateFromDelegate(int sslStreamHash) => |
| | 0 | 118 | | WriteEvent(CertificateFromDelegateId, sslStreamHash); |
| | | 119 | | |
| | | 120 | | [NonEvent] |
| | | 121 | | public void NoDelegateNoClientCert(SslStream SslStream) => |
| | 0 | 122 | | NoDelegateNoClientCert(GetHashCode(SslStream)); |
| | | 123 | | |
| | | 124 | | [Event(NoDelegateNoClientCertId, Level = EventLevel.Informational)] |
| | | 125 | | private void NoDelegateNoClientCert(int sslStreamHash) => |
| | 0 | 126 | | WriteEvent(NoDelegateNoClientCertId, sslStreamHash); |
| | | 127 | | |
| | | 128 | | [NonEvent] |
| | | 129 | | public void NoDelegateButClientCert(SslStream SslStream) => |
| | 0 | 130 | | NoDelegateButClientCert(GetHashCode(SslStream)); |
| | | 131 | | |
| | | 132 | | [Event(NoDelegateButClientCertId, Level = EventLevel.Informational)] |
| | | 133 | | private void NoDelegateButClientCert(int sslStreamHash) => |
| | 0 | 134 | | WriteEvent(NoDelegateButClientCertId, sslStreamHash); |
| | | 135 | | |
| | | 136 | | [NonEvent] |
| | | 137 | | public void AttemptingRestartUsingCert(X509Certificate? clientCertificate, SslStream SslStream) => |
| | 0 | 138 | | AttemptingRestartUsingCert(clientCertificate?.ToString(fVerbose: true), GetHashCode(SslStream)); |
| | | 139 | | |
| | | 140 | | [Event(AttemptingRestartUsingCertId, Level = EventLevel.Informational)] |
| | | 141 | | private void AttemptingRestartUsingCert(string? clientCertificate, int sslStreamHash) => |
| | 0 | 142 | | WriteEvent(AttemptingRestartUsingCertId, clientCertificate, sslStreamHash); |
| | | 143 | | |
| | | 144 | | [NonEvent] |
| | | 145 | | public void NoIssuersTryAllCerts(SslStream SslStream) => |
| | 0 | 146 | | NoIssuersTryAllCerts(GetHashCode(SslStream)); |
| | | 147 | | |
| | | 148 | | [Event(NoIssuersTryAllCertsId, Level = EventLevel.Informational)] |
| | | 149 | | private void NoIssuersTryAllCerts(int sslStreamHash) => |
| | 0 | 150 | | WriteEvent(NoIssuersTryAllCertsId, sslStreamHash); |
| | | 151 | | |
| | | 152 | | [NonEvent] |
| | | 153 | | public void LookForMatchingCerts(int issuersCount, SslStream SslStream) => |
| | 0 | 154 | | LookForMatchingCerts(issuersCount, GetHashCode(SslStream)); |
| | | 155 | | |
| | | 156 | | [Event(LookForMatchingCertsId, Level = EventLevel.Informational)] |
| | | 157 | | private void LookForMatchingCerts(int issuersCount, int sslStreamHash) => |
| | 0 | 158 | | WriteEvent(LookForMatchingCertsId, issuersCount, sslStreamHash); |
| | | 159 | | |
| | | 160 | | [NonEvent] |
| | | 161 | | public void SelectedCert(X509Certificate clientCertificate, SslStream SslStream) => |
| | 0 | 162 | | SelectedCert(clientCertificate?.ToString(fVerbose: true), GetHashCode(SslStream)); |
| | | 163 | | |
| | | 164 | | [Event(SelectedCertId, Level = EventLevel.Informational)] |
| | | 165 | | private void SelectedCert(string? clientCertificate, int sslStreamHash) => |
| | 0 | 166 | | WriteEvent(SelectedCertId, clientCertificate, sslStreamHash); |
| | | 167 | | |
| | | 168 | | [NonEvent] |
| | | 169 | | public void CertsAfterFiltering(int filteredCertsCount, SslStream SslStream) => |
| | 0 | 170 | | CertsAfterFiltering(filteredCertsCount, GetHashCode(SslStream)); |
| | | 171 | | |
| | | 172 | | [Event(CertsAfterFilteringId, Level = EventLevel.Informational)] |
| | | 173 | | private void CertsAfterFiltering(int filteredCertsCount, int sslStreamHash) => |
| | 0 | 174 | | WriteEvent(CertsAfterFilteringId, filteredCertsCount, sslStreamHash); |
| | | 175 | | |
| | | 176 | | [NonEvent] |
| | | 177 | | public void FindingMatchingCerts(SslStream SslStream) => |
| | 0 | 178 | | FindingMatchingCerts(GetHashCode(SslStream)); |
| | | 179 | | |
| | | 180 | | [Event(FindingMatchingCertsId, Level = EventLevel.Informational)] |
| | | 181 | | private void FindingMatchingCerts(int sslStreamHash) => |
| | 0 | 182 | | WriteEvent(FindingMatchingCertsId, sslStreamHash); |
| | | 183 | | |
| | | 184 | | [NonEvent] |
| | | 185 | | public void UsingCachedCredential(SslStream SslStream) => |
| | 0 | 186 | | UsingCachedCredential(GetHashCode(SslStream)); |
| | | 187 | | |
| | | 188 | | [Event(UsingCachedCredentialId, Level = EventLevel.Informational)] |
| | | 189 | | private void UsingCachedCredential(int sslStreamHash) => |
| | 0 | 190 | | WriteEvent(UsingCachedCredentialId, sslStreamHash); |
| | | 191 | | |
| | | 192 | | #pragma warning disable SYSLIB0058 // Use NegotiatedCipherSuite. |
| | | 193 | | [Event(SspiSelectedCipherSuitId, Level = EventLevel.Informational)] |
| | | 194 | | public void SspiSelectedCipherSuite( |
| | | 195 | | string process, |
| | | 196 | | SslProtocols sslProtocol, |
| | | 197 | | CipherAlgorithmType cipherAlgorithm, |
| | | 198 | | int cipherStrength, |
| | | 199 | | HashAlgorithmType hashAlgorithm, |
| | | 200 | | int hashStrength, |
| | | 201 | | ExchangeAlgorithmType keyExchangeAlgorithm, |
| | | 202 | | int keyExchangeStrength) |
| | 0 | 203 | | { |
| | 0 | 204 | | WriteEvent(SspiSelectedCipherSuitId, |
| | 0 | 205 | | process, (int)sslProtocol, (int)cipherAlgorithm, cipherStrength, |
| | 0 | 206 | | (int)hashAlgorithm, hashStrength, (int)keyExchangeAlgorithm, keyExchangeStrength); |
| | 0 | 207 | | } |
| | | 208 | | #pragma warning restore SYSLIB0058 // Use NegotiatedCipherSuite. |
| | | 209 | | |
| | | 210 | | [NonEvent] |
| | | 211 | | public void RemoteCertificateError(object sender, string message) => |
| | 0 | 212 | | RemoteCertificateError(GetHashCode(sender), message); |
| | | 213 | | |
| | | 214 | | [Event(RemoteCertificateErrorId, Level = EventLevel.Verbose)] |
| | | 215 | | private void RemoteCertificateError(int sslStreamHash, string message) => |
| | 0 | 216 | | WriteEvent(RemoteCertificateErrorId, sslStreamHash, message); |
| | | 217 | | |
| | | 218 | | [NonEvent] |
| | | 219 | | public void RemoteCertDeclaredValid(object sender) => |
| | 0 | 220 | | RemoteCertDeclaredValid(GetHashCode(sender)); |
| | | 221 | | |
| | | 222 | | [Event(RemoteVertificateValidId, Level = EventLevel.Verbose)] |
| | | 223 | | private void RemoteCertDeclaredValid(int sslStreamHash) => |
| | 0 | 224 | | WriteEvent(RemoteVertificateValidId, sslStreamHash); |
| | | 225 | | |
| | | 226 | | [NonEvent] |
| | | 227 | | public void RemoteCertHasNoErrors(object sender) => |
| | 0 | 228 | | RemoteCertHasNoErrors(GetHashCode(sender)); |
| | | 229 | | |
| | | 230 | | [Event(RemoteCertificateSuccessId, Level = EventLevel.Verbose)] |
| | | 231 | | private void RemoteCertHasNoErrors(int sslStreamHash) => |
| | 0 | 232 | | WriteEvent(RemoteCertificateSuccessId, sslStreamHash); |
| | | 233 | | |
| | | 234 | | [NonEvent] |
| | | 235 | | public void RemoteCertUserDeclaredInvalid(object sender) => |
| | 0 | 236 | | RemoteCertUserDeclaredInvalid(GetHashCode(sender)); |
| | | 237 | | |
| | | 238 | | [Event(RemoteCertificateInvalidId, Level = EventLevel.Verbose)] |
| | | 239 | | private void RemoteCertUserDeclaredInvalid(int sslStreamHash) => |
| | 0 | 240 | | WriteEvent(RemoteCertificateInvalidId, sslStreamHash); |
| | | 241 | | |
| | | 242 | | [NonEvent] |
| | | 243 | | public void SentFrame(SslStream sslStream, ReadOnlySpan<byte> frame) |
| | 0 | 244 | | { |
| | 0 | 245 | | TlsFrameHelper.TlsFrameInfo info = default; |
| | 0 | 246 | | bool isComplete = TlsFrameHelper.TryGetFrameInfo(frame, ref info); |
| | 0 | 247 | | SentFrame(IdOf(sslStream), info.ToString(), isComplete ? 1 : 0); |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | [Event(SentFrameId, Level = EventLevel.Verbose)] |
| | | 251 | | private void SentFrame(string sslStream, string tlsFrame, int isComplete) => |
| | 0 | 252 | | WriteEvent(SentFrameId, sslStream, tlsFrame, isComplete); |
| | | 253 | | |
| | | 254 | | [NonEvent] |
| | | 255 | | public void ReceivedFrame(SslStream sslStream, TlsFrameHelper.TlsFrameInfo frameInfo) => |
| | 0 | 256 | | ReceivedFrame(IdOf(sslStream), frameInfo.ToString(), 1); |
| | | 257 | | |
| | | 258 | | [NonEvent] |
| | | 259 | | public void ReceivedFrame(SslStream sslStream, ReadOnlySpan<byte> frame) |
| | | 260 | | { |
| | | 261 | | TlsFrameHelper.TlsFrameInfo info = default; |
| | | 262 | | bool isComplete = TlsFrameHelper.TryGetFrameInfo(frame, ref info); |
| | | 263 | | ReceivedFrame(IdOf(sslStream), info.ToString(), isComplete ? 1 : 0); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | [Event(ReceivedFrameId, Level = EventLevel.Verbose)] |
| | | 267 | | private void ReceivedFrame(string sslStream, string tlsFrame, int isComplete) => |
| | 0 | 268 | | WriteEvent(ReceivedFrameId, sslStream, tlsFrame, isComplete); |
| | | 269 | | |
| | | 270 | | [NonEvent] |
| | | 271 | | public void CertificateFromCertContext(SslStream sslStream) => |
| | 0 | 272 | | CertificateFromCertContext(GetHashCode(sslStream)); |
| | | 273 | | |
| | | 274 | | [Event(CertificateFromCertContextId, Level = EventLevel.Informational)] |
| | | 275 | | public void CertificateFromCertContext(int sslStreamHash) => |
| | 0 | 276 | | WriteEvent(CertificateFromCertContextId, sslStreamHash); |
| | | 277 | | |
| | | 278 | | static partial void AdditionalCustomizedToString(object value, ref string? result) |
| | 0 | 279 | | { |
| | 0 | 280 | | if (value is X509Certificate cert) |
| | 0 | 281 | | { |
| | 0 | 282 | | result = cert.ToString(fVerbose: true); |
| | 0 | 283 | | } |
| | 0 | 284 | | } |
| | | 285 | | |
| | | 286 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", |
| | | 287 | | Justification = EventSourceSuppressMessage)] |
| | | 288 | | [NonEvent] |
| | | 289 | | private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int a |
| | 0 | 290 | | { |
| | 0 | 291 | | arg1 ??= ""; |
| | | 292 | | |
| | 0 | 293 | | fixed (char* arg1Ptr = arg1) |
| | 0 | 294 | | { |
| | | 295 | | const int NumEventDatas = 8; |
| | 0 | 296 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 297 | | |
| | 0 | 298 | | descrs[0] = new EventData |
| | 0 | 299 | | { |
| | 0 | 300 | | DataPointer = (IntPtr)(arg1Ptr), |
| | 0 | 301 | | Size = (arg1.Length + 1) * sizeof(char) |
| | 0 | 302 | | }; |
| | 0 | 303 | | descrs[1] = new EventData |
| | 0 | 304 | | { |
| | 0 | 305 | | DataPointer = (IntPtr)(&arg2), |
| | 0 | 306 | | Size = sizeof(int) |
| | 0 | 307 | | }; |
| | 0 | 308 | | descrs[2] = new EventData |
| | 0 | 309 | | { |
| | 0 | 310 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 311 | | Size = sizeof(int) |
| | 0 | 312 | | }; |
| | 0 | 313 | | descrs[3] = new EventData |
| | 0 | 314 | | { |
| | 0 | 315 | | DataPointer = (IntPtr)(&arg4), |
| | 0 | 316 | | Size = sizeof(int) |
| | 0 | 317 | | }; |
| | 0 | 318 | | descrs[4] = new EventData |
| | 0 | 319 | | { |
| | 0 | 320 | | DataPointer = (IntPtr)(&arg5), |
| | 0 | 321 | | Size = sizeof(int) |
| | 0 | 322 | | }; |
| | 0 | 323 | | descrs[5] = new EventData |
| | 0 | 324 | | { |
| | 0 | 325 | | DataPointer = (IntPtr)(&arg6), |
| | 0 | 326 | | Size = sizeof(int) |
| | 0 | 327 | | }; |
| | 0 | 328 | | descrs[6] = new EventData |
| | 0 | 329 | | { |
| | 0 | 330 | | DataPointer = (IntPtr)(&arg7), |
| | 0 | 331 | | Size = sizeof(int) |
| | 0 | 332 | | }; |
| | 0 | 333 | | descrs[7] = new EventData |
| | 0 | 334 | | { |
| | 0 | 335 | | DataPointer = (IntPtr)(&arg8), |
| | 0 | 336 | | Size = sizeof(int) |
| | 0 | 337 | | }; |
| | | 338 | | |
| | 0 | 339 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 340 | | } |
| | 0 | 341 | | } |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | |