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 // See the LICENSE file in the project root for more information.
4 
5 using System.Diagnostics.Tracing;
6 using System.Globalization;
7 using System.Net.Security;
8 using System.Security.Authentication;
9 using System.Security.Cryptography.X509Certificates;
10 
11 namespace System.Net
12 {
13     //TODO: If localization resources are not found, logging does not work. Issue #5126.
14     [EventSource(Name = "Microsoft-System-Net-Security", LocalizationResources = "FxResources.System.Net.Security.SR")]
15     internal sealed partial class NetEventSource
16     {
17         private const int EnumerateSecurityPackagesId = NextAvailableEventId;
18         private const int SspiPackageNotFoundId = EnumerateSecurityPackagesId + 1;
19         private const int AcquireDefaultCredentialId = SspiPackageNotFoundId + 1;
20         private const int AcquireCredentialsHandleId = AcquireDefaultCredentialId + 1;
21         private const int SecureChannelCtorId = AcquireCredentialsHandleId + 1;
22         private const int LocatingPrivateKeyId = SecureChannelCtorId + 1;
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 InitializeSecurityContextId = NotFoundCertInStoreId + 1;
27         private const int SecurityContextInputBufferId = InitializeSecurityContextId + 1;
28         private const int SecurityContextInputBuffersId = SecurityContextInputBufferId + 1;
29         private const int AcceptSecuritContextId = SecurityContextInputBuffersId + 1;
30         private const int OperationReturnedSomethingId = AcceptSecuritContextId + 1;
31         private const int RemoteCertificateId = OperationReturnedSomethingId + 1;
32         private const int CertificateFromDelegateId = RemoteCertificateId + 1;
33         private const int NoDelegateNoClientCertId = CertificateFromDelegateId + 1;
34         private const int NoDelegateButClientCertId = NoDelegateNoClientCertId + 1;
35         private const int AttemptingRestartUsingCertId = NoDelegateButClientCertId + 1;
36         private const int NoIssuersTryAllCertsId = AttemptingRestartUsingCertId + 1;
37         private const int LookForMatchingCertsId = NoIssuersTryAllCertsId + 1;
38         private const int SelectedCertId = LookForMatchingCertsId + 1;
39         private const int CertsAfterFilteringId = SelectedCertId + 1;
40         private const int FindingMatchingCertsId = CertsAfterFilteringId + 1;
41         private const int UsingCachedCredentialId = FindingMatchingCertsId + 1;
42         private const int SspiSelectedCipherSuitId = UsingCachedCredentialId + 1;
43         private const int RemoteCertificateErrorId = SspiSelectedCipherSuitId + 1;
44         private const int RemoteVertificateValidId = RemoteCertificateErrorId + 1;
45         private const int RemoteCertificateSuccesId = RemoteVertificateValidId + 1;
46         private const int RemoteCertificateInvalidId = RemoteCertificateSuccesId + 1;
47 
48         [Event(EnumerateSecurityPackagesId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
EnumerateSecurityPackages(string securityPackage)49         public void EnumerateSecurityPackages(string securityPackage)
50         {
51             if (IsEnabled())
52             {
53                 WriteEvent(EnumerateSecurityPackagesId, securityPackage ?? "");
54             }
55         }
56 
57         [Event(SspiPackageNotFoundId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
SspiPackageNotFound(string packageName)58         public void SspiPackageNotFound(string packageName)
59         {
60             if (IsEnabled())
61             {
62                 WriteEvent(SspiPackageNotFoundId, packageName ?? "");
63             }
64         }
65 
66         [NonEvent]
SecureChannelCtor(SecureChannel secureChannel, string hostname, X509CertificateCollection clientCertificates, EncryptionPolicy encryptionPolicy)67         public void SecureChannelCtor(SecureChannel secureChannel, string hostname, X509CertificateCollection clientCertificates, EncryptionPolicy encryptionPolicy)
68         {
69             if (IsEnabled())
70             {
71                 SecureChannelCtor(hostname, GetHashCode(secureChannel), clientCertificates?.Count ?? 0, encryptionPolicy);
72             }
73         }
74         [Event(SecureChannelCtorId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
SecureChannelCtor(string hostname, int secureChannelHash, int clientCertificatesCount, EncryptionPolicy encryptionPolicy)75         private unsafe void SecureChannelCtor(string hostname, int secureChannelHash, int clientCertificatesCount, EncryptionPolicy encryptionPolicy) =>
76             WriteEvent(SecureChannelCtorId, hostname, secureChannelHash, clientCertificatesCount, (int)encryptionPolicy);
77 
78         [NonEvent]
LocatingPrivateKey(X509Certificate x509Certificate, SecureChannel secureChannel)79         public void LocatingPrivateKey(X509Certificate x509Certificate, SecureChannel secureChannel)
80         {
81             if (IsEnabled())
82             {
83                 LocatingPrivateKey(x509Certificate.ToString(true), GetHashCode(secureChannel));
84             }
85         }
86         [Event(LocatingPrivateKeyId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
LocatingPrivateKey(string x509Certificate, int secureChannelHash)87         private void LocatingPrivateKey(string x509Certificate, int secureChannelHash) =>
88             WriteEvent(LocatingPrivateKeyId, x509Certificate, secureChannelHash);
89 
90         [NonEvent]
CertIsType2(SecureChannel secureChannel)91         public void CertIsType2(SecureChannel secureChannel)
92         {
93             if (IsEnabled())
94             {
95                 CertIsType2(GetHashCode(secureChannel));
96             }
97         }
98         [Event(CertIsType2Id, Keywords = Keywords.Default, Level = EventLevel.Informational)]
CertIsType2(int secureChannelHash)99         private void CertIsType2(int secureChannelHash) =>
100             WriteEvent(CertIsType2Id, secureChannelHash);
101 
102         [NonEvent]
FoundCertInStore(bool serverMode, SecureChannel secureChannel)103         public void FoundCertInStore(bool serverMode, SecureChannel secureChannel)
104         {
105             if (IsEnabled())
106             {
107                 FoundCertInStore(serverMode ? "LocalMachine" : "CurrentUser", GetHashCode(secureChannel));
108             }
109         }
110         [Event(FoundCertInStoreId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
FoundCertInStore(string store, int secureChannelHash)111         private void FoundCertInStore(string store, int secureChannelHash) =>
112             WriteEvent(FoundCertInStoreId, store, secureChannelHash);
113 
114         [NonEvent]
NotFoundCertInStore(SecureChannel secureChannel)115         public void NotFoundCertInStore(SecureChannel secureChannel)
116         {
117             if (IsEnabled())
118             {
119                 NotFoundCertInStore(GetHashCode(secureChannel));
120             }
121         }
122         [Event(NotFoundCertInStoreId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
NotFoundCertInStore(int secureChannelHash)123         private void NotFoundCertInStore(int secureChannelHash) =>
124             WriteEvent(NotFoundCertInStoreId, secureChannelHash);
125 
126         [NonEvent]
RemoteCertificate(X509Certificate remoteCertificate)127         public void RemoteCertificate(X509Certificate remoteCertificate)
128         {
129             if (IsEnabled())
130             {
131                 WriteEvent(RemoteCertificateId, remoteCertificate?.ToString(true));
132             }
133         }
134         [Event(RemoteCertificateId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
RemoteCertificate(string remoteCertificate)135         private void RemoteCertificate(string remoteCertificate) =>
136             WriteEvent(RemoteCertificateId, remoteCertificate);
137 
138         [NonEvent]
CertificateFromDelegate(SecureChannel secureChannel)139         public void CertificateFromDelegate(SecureChannel secureChannel)
140         {
141             if (IsEnabled())
142             {
143                 CertificateFromDelegate(GetHashCode(secureChannel));
144             }
145         }
146         [Event(CertificateFromDelegateId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
CertificateFromDelegate(int secureChannelHash)147         private void CertificateFromDelegate(int secureChannelHash) =>
148             WriteEvent(CertificateFromDelegateId, secureChannelHash);
149 
150         [NonEvent]
NoDelegateNoClientCert(SecureChannel secureChannel)151         public void NoDelegateNoClientCert(SecureChannel secureChannel)
152         {
153             if (IsEnabled())
154             {
155                 NoDelegateNoClientCert(GetHashCode(secureChannel));
156             }
157         }
158         [Event(NoDelegateNoClientCertId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
NoDelegateNoClientCert(int secureChannelHash)159         private void NoDelegateNoClientCert(int secureChannelHash) =>
160             WriteEvent(NoDelegateNoClientCertId, secureChannelHash);
161 
162         [NonEvent]
NoDelegateButClientCert(SecureChannel secureChannel)163         public void NoDelegateButClientCert(SecureChannel secureChannel)
164         {
165             if (IsEnabled())
166             {
167                 NoDelegateButClientCert(GetHashCode(secureChannel));
168             }
169         }
170         [Event(NoDelegateButClientCertId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
NoDelegateButClientCert(int secureChannelHash)171         private void NoDelegateButClientCert(int secureChannelHash) =>
172             WriteEvent(NoDelegateButClientCertId, secureChannelHash);
173 
174         [NonEvent]
AttemptingRestartUsingCert(X509Certificate clientCertificate, SecureChannel secureChannel)175         public void AttemptingRestartUsingCert(X509Certificate clientCertificate, SecureChannel secureChannel)
176         {
177             if (IsEnabled())
178             {
179                 AttemptingRestartUsingCert(clientCertificate?.ToString(true), GetHashCode(secureChannel));
180             }
181         }
182         [Event(AttemptingRestartUsingCertId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
AttemptingRestartUsingCert(string clientCertificate, int secureChannelHash)183         private void AttemptingRestartUsingCert(string clientCertificate, int secureChannelHash) =>
184             WriteEvent(AttemptingRestartUsingCertId, clientCertificate, secureChannelHash);
185 
186         [NonEvent]
NoIssuersTryAllCerts(SecureChannel secureChannel)187         public void NoIssuersTryAllCerts(SecureChannel secureChannel)
188         {
189             if (IsEnabled())
190             {
191                 NoIssuersTryAllCerts(GetHashCode(secureChannel));
192             }
193         }
194         [Event(NoIssuersTryAllCertsId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
NoIssuersTryAllCerts(int secureChannelHash)195         private void NoIssuersTryAllCerts(int secureChannelHash) =>
196             WriteEvent(NoIssuersTryAllCertsId, secureChannelHash);
197 
198         [NonEvent]
LookForMatchingCerts(int issuersCount, SecureChannel secureChannel)199         public void LookForMatchingCerts(int issuersCount, SecureChannel secureChannel)
200         {
201             if (IsEnabled())
202             {
203                 LookForMatchingCerts(issuersCount, GetHashCode(secureChannel));
204             }
205         }
206         [Event(LookForMatchingCertsId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
LookForMatchingCerts(int issuersCount, int secureChannelHash)207         private void LookForMatchingCerts(int issuersCount, int secureChannelHash) =>
208             WriteEvent(LookForMatchingCertsId, issuersCount, secureChannelHash);
209 
210         [NonEvent]
SelectedCert(X509Certificate clientCertificate, SecureChannel secureChannel)211         public void SelectedCert(X509Certificate clientCertificate, SecureChannel secureChannel)
212         {
213             if (IsEnabled())
214             {
215                 SelectedCert(clientCertificate?.ToString(true), GetHashCode(secureChannel));
216             }
217         }
218         [Event(SelectedCertId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
SelectedCert(string clientCertificate, int secureChannelHash)219         private void SelectedCert(string clientCertificate, int secureChannelHash) =>
220             WriteEvent(SelectedCertId, clientCertificate, secureChannelHash);
221 
222         [NonEvent]
CertsAfterFiltering(int filteredCertsCount, SecureChannel secureChannel)223         public void CertsAfterFiltering(int filteredCertsCount, SecureChannel secureChannel)
224         {
225             if (IsEnabled())
226             {
227                 CertsAfterFiltering(filteredCertsCount, GetHashCode(secureChannel));
228             }
229         }
230         [Event(CertsAfterFilteringId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
CertsAfterFiltering(int filteredCertsCount, int secureChannelHash)231         private void CertsAfterFiltering(int filteredCertsCount, int secureChannelHash) =>
232             WriteEvent(CertsAfterFilteringId, filteredCertsCount, secureChannelHash);
233 
234         [NonEvent]
FindingMatchingCerts(SecureChannel secureChannel)235         public void FindingMatchingCerts(SecureChannel secureChannel)
236         {
237             if (IsEnabled())
238             {
239                 FindingMatchingCerts(GetHashCode(secureChannel));
240             }
241         }
242         [Event(FindingMatchingCertsId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
FindingMatchingCerts(int secureChannelHash)243         private void FindingMatchingCerts(int secureChannelHash) =>
244             WriteEvent(FindingMatchingCertsId, secureChannelHash);
245 
246         [NonEvent]
UsingCachedCredential(SecureChannel secureChannel)247         public void UsingCachedCredential(SecureChannel secureChannel)
248         {
249             if (IsEnabled())
250             {
251                 WriteEvent(UsingCachedCredentialId, GetHashCode(secureChannel));
252             }
253         }
254         [Event(UsingCachedCredentialId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
UsingCachedCredential(int secureChannelHash)255         private void UsingCachedCredential(int secureChannelHash) =>
256             WriteEvent(UsingCachedCredentialId, secureChannelHash);
257 
258         [Event(SspiSelectedCipherSuitId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
SspiSelectedCipherSuite( string process, SslProtocols sslProtocol, CipherAlgorithmType cipherAlgorithm, int cipherStrength, HashAlgorithmType hashAlgorithm, int hashStrength, ExchangeAlgorithmType keyExchangeAlgorithm, int keyExchangeStrength)259         public unsafe void SspiSelectedCipherSuite(
260             string process,
261             SslProtocols sslProtocol,
262             CipherAlgorithmType cipherAlgorithm,
263             int cipherStrength,
264             HashAlgorithmType hashAlgorithm,
265             int hashStrength,
266             ExchangeAlgorithmType keyExchangeAlgorithm,
267             int keyExchangeStrength)
268         {
269             if (IsEnabled())
270             {
271                 WriteEvent(SspiSelectedCipherSuitId,
272                     process, (int)sslProtocol, (int)cipherAlgorithm, cipherStrength,
273                     (int)hashAlgorithm, hashStrength, (int)keyExchangeAlgorithm, keyExchangeStrength);
274             }
275         }
276 
277         [NonEvent]
RemoteCertificateError(SecureChannel secureChannel, string message)278         public void RemoteCertificateError(SecureChannel secureChannel, string message)
279         {
280             if (IsEnabled())
281             {
282                 RemoteCertificateError(GetHashCode(secureChannel), message);
283             }
284         }
285         [Event(RemoteCertificateErrorId, Keywords = Keywords.Default, Level = EventLevel.Verbose)]
RemoteCertificateError(int secureChannelHash, string message)286         private void RemoteCertificateError(int secureChannelHash, string message) =>
287             WriteEvent(RemoteCertificateErrorId, secureChannelHash, message);
288 
289         [NonEvent]
RemoteCertDeclaredValid(SecureChannel secureChannel)290         public void RemoteCertDeclaredValid(SecureChannel secureChannel)
291         {
292             if (IsEnabled())
293             {
294                 RemoteCertDeclaredValid(GetHashCode(secureChannel));
295             }
296         }
297         [Event(RemoteVertificateValidId, Keywords = Keywords.Default, Level = EventLevel.Verbose)]
RemoteCertDeclaredValid(int secureChannelHash)298         private void RemoteCertDeclaredValid(int secureChannelHash) =>
299             WriteEvent(RemoteVertificateValidId, secureChannelHash);
300 
301         [NonEvent]
RemoteCertHasNoErrors(SecureChannel secureChannel)302         public void RemoteCertHasNoErrors(SecureChannel secureChannel)
303         {
304             if (IsEnabled())
305             {
306                 RemoteCertHasNoErrors(GetHashCode(secureChannel));
307             }
308         }
309         [Event(RemoteCertificateSuccesId, Keywords = Keywords.Default, Level = EventLevel.Verbose)]
RemoteCertHasNoErrors(int secureChannelHash)310         private void RemoteCertHasNoErrors(int secureChannelHash) =>
311             WriteEvent(RemoteCertificateSuccesId, secureChannelHash);
312 
313         [NonEvent]
RemoteCertUserDeclaredInvalid(SecureChannel secureChannel)314         public void RemoteCertUserDeclaredInvalid(SecureChannel secureChannel)
315         {
316             if (IsEnabled())
317             {
318                 RemoteCertUserDeclaredInvalid(GetHashCode(secureChannel));
319             }
320         }
321         [Event(RemoteCertificateInvalidId, Keywords = Keywords.Default, Level = EventLevel.Verbose)]
RemoteCertUserDeclaredInvalid(int secureChannelHash)322         private void RemoteCertUserDeclaredInvalid(int secureChannelHash) =>
323             WriteEvent(RemoteCertificateInvalidId, secureChannelHash);
324 
AdditionalCustomizedToString(T value, ref string result)325         static partial void AdditionalCustomizedToString<T>(T value, ref string result)
326         {
327             X509Certificate cert = value as X509Certificate;
328             if (cert != null)
329             {
330                 result = cert.ToString(fVerbose: true);
331             }
332         }
333 
334         [NonEvent]
WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8)335         private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8)
336         {
337             if (IsEnabled())
338             {
339                 if (arg1 == null) arg1 = "";
340 
341                 fixed (char* arg1Ptr = arg1)
342                 {
343                     const int NumEventDatas = 8;
344                     var descrs = stackalloc EventData[NumEventDatas];
345 
346                     descrs[0].DataPointer = (IntPtr)(arg1Ptr);
347                     descrs[0].Size = (arg1.Length + 1) * sizeof(char);
348 
349                     descrs[1].DataPointer = (IntPtr)(&arg2);
350                     descrs[1].Size = sizeof(int);
351 
352                     descrs[2].DataPointer = (IntPtr)(&arg3);
353                     descrs[2].Size = sizeof(int);
354 
355                     descrs[3].DataPointer = (IntPtr)(&arg4);
356                     descrs[3].Size = sizeof(int);
357 
358                     descrs[4].DataPointer = (IntPtr)(&arg5);
359                     descrs[4].Size = sizeof(int);
360 
361                     descrs[5].DataPointer = (IntPtr)(&arg6);
362                     descrs[5].Size = sizeof(int);
363 
364                     descrs[6].DataPointer = (IntPtr)(&arg7);
365                     descrs[6].Size = sizeof(int);
366 
367                     descrs[7].DataPointer = (IntPtr)(&arg8);
368                     descrs[7].Size = sizeof(int);
369 
370                     WriteEventCore(eventId, NumEventDatas, descrs);
371                 }
372             }
373         }
374     }
375 }
376