Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SslStreamCertificateContext.cs
Go to the documentation of this file.
3
4namespace System.Net.Security;
5
7{
8 internal readonly X509Certificate2 Certificate;
9
11
12 internal readonly SslCertificateTrust Trust;
13
14 [EditorBrowsable(EditorBrowsableState.Never)]
15 public static SslStreamCertificateContext Create(X509Certificate2 target, X509Certificate2Collection? additionalCertificates, bool offline)
16 {
17 return Create(target, additionalCertificates, offline, null);
18 }
19
20 public static SslStreamCertificateContext Create(X509Certificate2 target, X509Certificate2Collection? additionalCertificates, bool offline = false, SslCertificateTrust? trust = null)
21 {
22 if (!target.HasPrivateKey)
23 {
25 }
27 using (X509Chain x509Chain = new X509Chain())
28 {
29 if (additionalCertificates != null)
30 {
31 foreach (X509Certificate2 additionalCertificate in additionalCertificates)
32 {
33 x509Chain.ChainPolicy.ExtraStore.Add((X509Certificate)additionalCertificate);
34 }
35 }
36 x509Chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
37 x509Chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
38 x509Chain.ChainPolicy.DisableCertificateDownloads = offline;
39 if (!x509Chain.Build(target) && System.Net.NetEventSource.Log.IsEnabled())
40 {
41 System.Net.NetEventSource.Error(null, $"Failed to build chain for {target.Subject}", "Create");
42 }
43 int num = x509Chain.ChainElements.Count - 1;
44 if (num >= 0)
45 {
46 if (num > 0 && x509Chain.ChainElements.Count > 1)
47 {
48 array = new X509Certificate2[num];
49 for (int i = 0; i < num; i++)
50 {
51 array[i] = x509Chain.ChainElements[i + 1].Certificate;
52 }
53 }
54 x509Chain.ChainElements[0].Certificate.Dispose();
55 for (int j = num + 1; j < x509Chain.ChainElements.Count; j++)
56 {
57 x509Chain.ChainElements[j].Certificate.Dispose();
58 }
59 }
60 }
61 return new SslStreamCertificateContext(target, array, trust);
62 }
63
65 {
66 return new SslStreamCertificateContext(target, Array.Empty<X509Certificate2>(), null);
67 }
68
70 {
71 if (intermediates.Length != 0)
72 {
73 using X509Chain x509Chain = new X509Chain();
74 x509Chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
75 x509Chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
76 x509Chain.ChainPolicy.DisableCertificateDownloads = true;
77 bool flag = x509Chain.Build(target);
78 int num = 0;
79 X509ChainStatus[] chainStatus = x509Chain.ChainStatus;
80 for (int i = 0; i < chainStatus.Length; i++)
81 {
82 X509ChainStatus x509ChainStatus = chainStatus[i];
83 if (x509ChainStatus.Status.HasFlag(X509ChainStatusFlags.PartialChain) || x509ChainStatus.Status.HasFlag(X509ChainStatusFlags.NotSignatureValid))
84 {
85 flag = false;
86 break;
87 }
88 num++;
89 }
90 if (!flag)
91 {
92 X509Store x509Store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);
93 try
94 {
95 x509Store.Open(OpenFlags.ReadWrite);
96 }
97 catch
98 {
99 x509Store.Dispose();
100 x509Store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser);
101 try
102 {
103 x509Store.Open(OpenFlags.ReadWrite);
104 }
105 catch
106 {
107 x509Store.Dispose();
108 x509Store = null;
109 if (System.Net.NetEventSource.Log.IsEnabled())
110 {
111 System.Net.NetEventSource.Error(this, $"Failed to open certificate store for intermediates.", ".ctor");
112 }
113 }
114 }
115 if (x509Store != null)
116 {
117 using (x509Store)
118 {
119 for (int j = num; j < intermediates.Length - 1; j++)
120 {
121 x509Store.Add(intermediates[j]);
122 }
123 flag = x509Chain.Build(target);
124 X509ChainStatus[] chainStatus2 = x509Chain.ChainStatus;
125 for (int k = 0; k < chainStatus2.Length; k++)
126 {
127 X509ChainStatus x509ChainStatus2 = chainStatus2[k];
128 if (x509ChainStatus2.Status.HasFlag(X509ChainStatusFlags.PartialChain) || x509ChainStatus2.Status.HasFlag(X509ChainStatusFlags.NotSignatureValid))
129 {
130 flag = false;
131 break;
132 }
133 }
134 if (!flag)
135 {
136 x509Store.Add(intermediates[^1]);
137 }
138 }
139 }
140 }
141 }
142 Certificate = target;
143 IntermediateCertificates = intermediates;
144 Trust = trust;
145 }
146}
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static SslStreamCertificateContext Create(X509Certificate2 target)
SslStreamCertificateContext(X509Certificate2 target, X509Certificate2[] intermediates, SslCertificateTrust trust)
static SslStreamCertificateContext Create(X509Certificate2 target, X509Certificate2Collection? additionalCertificates, bool offline=false, SslCertificateTrust? trust=null)
static SslStreamCertificateContext Create(X509Certificate2 target, X509Certificate2Collection? additionalCertificates, bool offline)
static string net_ssl_io_no_server_cert
Definition SR.cs:52
Definition SR.cs:7