Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CertificateValidationPal.cs
Go to the documentation of this file.
8
9namespace System.Net;
10
11internal static class CertificateValidationPal
12{
13 private static readonly object s_syncObject = new object();
14
15 private static volatile X509Store s_myCertStoreEx;
16
17 private static volatile X509Store s_myMachineCertStoreEx;
18
19 internal static X509Store EnsureStoreOpened(bool isMachineStore)
20 {
21 X509Store x509Store = (isMachineStore ? s_myMachineCertStoreEx : s_myCertStoreEx);
22 if (x509Store == null)
23 {
24 StoreLocation storeLocation = ((!isMachineStore) ? StoreLocation.CurrentUser : StoreLocation.LocalMachine);
25 if (1 == 0)
26 {
27 return null;
28 }
29 lock (s_syncObject)
30 {
31 x509Store = (isMachineStore ? s_myMachineCertStoreEx : s_myCertStoreEx);
32 if (x509Store == null)
33 {
34 try
35 {
36 x509Store = OpenStore(storeLocation);
37 if (System.Net.NetEventSource.Log.IsEnabled())
38 {
39 System.Net.NetEventSource.Info(null, $"storeLocation: {storeLocation} returned store {x509Store}", "EnsureStoreOpened");
40 }
41 if (isMachineStore)
42 {
43 s_myMachineCertStoreEx = x509Store;
44 }
45 else
46 {
47 s_myCertStoreEx = x509Store;
48 }
49 }
50 catch (Exception ex)
51 {
53 {
54 return null;
55 }
56 if (System.Net.NetEventSource.Log.IsEnabled())
57 {
58 System.Net.NetEventSource.Error(null, System.SR.Format(System.SR.net_log_open_store_failed, storeLocation, ex), "EnsureStoreOpened");
59 }
60 throw;
61 }
62 }
63 }
64 }
65 return x509Store;
66 }
67
68 internal static SslPolicyErrors VerifyCertificateProperties(SafeDeleteContext securityContext, X509Chain chain, X509Certificate2 remoteCertificate, bool checkCertName, bool isServer, string hostName)
69 {
70 return CertificateValidation.BuildChainAndVerifyProperties(chain, remoteCertificate, checkCertName, isServer, hostName);
71 }
72
74 {
75 X509Certificate2Collection remoteCertificateCollection;
76 return GetRemoteCertificate(securityContext, retrieveCollection: false, out remoteCertificateCollection);
77 }
78
79 internal static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext, out X509Certificate2Collection remoteCertificateCollection)
80 {
81 return GetRemoteCertificate(securityContext, retrieveCollection: true, out remoteCertificateCollection);
82 }
83
84 private static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext, bool retrieveCollection, out X509Certificate2Collection remoteCertificateCollection)
85 {
86 remoteCertificateCollection = null;
87 if (securityContext == null)
88 {
89 return null;
90 }
91 X509Certificate2 x509Certificate = null;
92 SafeFreeCertContext safeFreeCertContext = null;
93 try
94 {
96 if (safeFreeCertContext != null && !safeFreeCertContext.IsInvalid)
97 {
98 x509Certificate = new X509Certificate2(safeFreeCertContext.DangerousGetHandle());
99 }
100 }
101 finally
102 {
103 if (safeFreeCertContext != null && !safeFreeCertContext.IsInvalid)
104 {
105 if (retrieveCollection)
106 {
107 remoteCertificateCollection = UnmanagedCertificateContext.GetRemoteCertificatesFromStoreContext(safeFreeCertContext);
108 }
109 safeFreeCertContext.Dispose();
110 }
111 }
112 if (System.Net.NetEventSource.Log.IsEnabled())
113 {
114 System.Net.NetEventSource.Log.RemoteCertificate(x509Certificate);
115 }
116 return x509Certificate;
117 }
118
119 internal unsafe static string[] GetRequestCertificateAuthorities(SafeDeleteContext securityContext)
120 {
121 global::Interop.SspiCli.SecPkgContext_IssuerListInfoEx ctx = default(global::Interop.SspiCli.SecPkgContext_IssuerListInfoEx);
122 SafeHandle sspiHandle;
123 bool flag = SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_ISSUER_LIST_EX(GlobalSSPI.SSPISecureChannel, securityContext, ref ctx, out sspiHandle);
124 string[] array = Array.Empty<string>();
125 try
126 {
127 if (flag && ctx.cIssuers != 0)
128 {
129 array = new string[ctx.cIssuers];
130 Span<global::Interop.SspiCli.CERT_CHAIN_ELEMENT> span = new Span<global::Interop.SspiCli.CERT_CHAIN_ELEMENT>((void*)sspiHandle.DangerousGetHandle(), array.Length);
131 for (int i = 0; i < span.Length; i++)
132 {
133 if (span[i].cbSize != 0)
134 {
135 byte[] encodedDistinguishedName = new Span<byte>((void*)span[i].pCertContext, checked((int)span[i].cbSize)).ToArray();
136 X500DistinguishedName x500DistinguishedName = new X500DistinguishedName(encodedDistinguishedName);
137 array[i] = x500DistinguishedName.Name;
138 if (System.Net.NetEventSource.Log.IsEnabled())
139 {
140 System.Net.NetEventSource.Info(securityContext, $"IssuerListEx[{array[i]}]", "GetRequestCertificateAuthorities");
141 }
142 }
143 }
144 }
145 }
146 finally
147 {
148 sspiHandle?.Dispose();
149 }
150 return array;
151 }
152
153 internal static X509Store OpenStore(StoreLocation storeLocation)
154 {
155 X509Store store = new X509Store(StoreName.My, storeLocation);
156 try
157 {
159 {
160 store.Open(OpenFlags.OpenExistingOnly);
161 });
162 }
163 catch
164 {
165 throw;
166 }
167 return store;
168 }
169}
static unsafe string[] GetRequestCertificateAuthorities(SafeDeleteContext securityContext)
static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext, out X509Certificate2Collection remoteCertificateCollection)
static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext)
static X509Store OpenStore(StoreLocation storeLocation)
static SslPolicyErrors VerifyCertificateProperties(SafeDeleteContext securityContext, X509Chain chain, X509Certificate2 remoteCertificate, bool checkCertName, bool isServer, string hostName)
static X509Store EnsureStoreOpened(bool isMachineStore)
static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext, bool retrieveCollection, out X509Certificate2Collection remoteCertificateCollection)
static volatile X509Store s_myMachineCertStoreEx
static unsafe SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X509Certificate2 remoteCertificate, bool checkCertName, bool isServer, string hostName)
static readonly System.Net.SSPISecureChannelType SSPISecureChannel
Definition GlobalSSPI.cs:7
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static bool QueryContextAttributes_SECPKG_ATTR_ISSUER_LIST_EX(ISSPIInterface secModule, SafeDeleteContext securityContext, ref global::Interop.SspiCli.SecPkgContext_IssuerListInfoEx ctx, out SafeHandle sspiHandle)
static SafeFreeCertContext QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CONTEXT(ISSPIInterface secModule, SafeDeleteContext securityContext)
static X509Certificate2Collection GetRemoteCertificatesFromStoreContext(SafeFreeCertContext certContext)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_log_open_store_failed
Definition SR.cs:92
Definition SR.cs:7
static void RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
T[] ToArray()
Definition Span.cs:291