Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SafeMsQuicConfigurationHandle.cs
Go to the documentation of this file.
9
11
13{
14 private static readonly FieldInfo _contextCertificate = typeof(SslStreamCertificateContext).GetField("Certificate", BindingFlags.Instance | BindingFlags.NonPublic);
15
16 private static readonly FieldInfo _contextChain = typeof(SslStreamCertificateContext).GetField("IntermediateCertificates", BindingFlags.Instance | BindingFlags.NonPublic);
17
18 public override bool IsInvalid => handle == IntPtr.Zero;
19
21 : base(IntPtr.Zero, ownsHandle: true)
22 {
23 }
24
25 protected override bool ReleaseHandle()
26 {
27 MsQuicApi.Api.ConfigurationCloseDelegate(handle);
29 return true;
30 }
31
33 {
35 if (options.ClientAuthenticationOptions != null)
36 {
37 if (options.ClientAuthenticationOptions.CipherSuitesPolicy != null)
38 {
40 }
41 if (options.ClientAuthenticationOptions.EncryptionPolicy == EncryptionPolicy.NoEncryption)
42 {
44 }
45 if (options.ClientAuthenticationOptions.ClientCertificates != null)
46 {
47 foreach (X509Certificate clientCertificate in options.ClientAuthenticationOptions.ClientCertificates)
48 {
49 try
50 {
51 if (((X509Certificate2)clientCertificate).HasPrivateKey)
52 {
54 break;
55 }
56 }
57 catch
58 {
59 }
60 }
61 }
62 }
63 return Create(options, QUIC_CREDENTIAL_FLAGS.CLIENT, certificate, null, options.ClientAuthenticationOptions?.ApplicationProtocols);
64 }
65
67 {
71 {
72 if (serverAuthenticationOptions.CipherSuitesPolicy != null)
73 {
75 }
76 if (serverAuthenticationOptions.EncryptionPolicy == EncryptionPolicy.NoEncryption)
77 {
79 }
80 if (serverAuthenticationOptions.ClientCertificateRequired)
81 {
82 qUIC_CREDENTIAL_FLAGS |= QUIC_CREDENTIAL_FLAGS.NO_CERTIFICATE_VALIDATION | QUIC_CREDENTIAL_FLAGS.INDICATE_CERTIFICATE_RECEIVED | QUIC_CREDENTIAL_FLAGS.REQUIRE_CLIENT_AUTHENTICATION;
83 }
84 if (x509Certificate == null && serverAuthenticationOptions != null && serverAuthenticationOptions.ServerCertificateSelectionCallback != null && targetHost != null)
85 {
86 x509Certificate = serverAuthenticationOptions.ServerCertificateSelectionCallback(options, targetHost);
87 }
88 }
90 }
91
93 {
94 if (alpnProtocols == null || alpnProtocols.Count == 0)
95 {
96 throw new Exception("At least one SslApplicationProtocol value must be present in SslClientAuthenticationOptions or SslServerAuthenticationOptions.");
97 }
98 if (options.MaxBidirectionalStreams > 65535)
99 {
100 throw new Exception("MaxBidirectionalStreams overflow.");
101 }
102 if (options.MaxBidirectionalStreams > 65535)
103 {
104 throw new Exception("MaxBidirectionalStreams overflow.");
105 }
106 if ((flags & QUIC_CREDENTIAL_FLAGS.CLIENT) == 0)
107 {
108 if (certificate == null && certificateContext == null)
109 {
110 throw new Exception("Server must provide certificate");
111 }
112 }
113 else
114 {
115 flags |= QUIC_CREDENTIAL_FLAGS.NO_CERTIFICATE_VALIDATION | QUIC_CREDENTIAL_FLAGS.INDICATE_CERTIFICATE_RECEIVED;
116 }
118 {
119 flags |= QUIC_CREDENTIAL_FLAGS.USE_PORTABLE_CERTIFICATES;
120 }
122 quicSettings.IsSetFlags = MsQuicNativeMethods.QuicSettingsIsSetFlags.PeerBidiStreamCount | MsQuicNativeMethods.QuicSettingsIsSetFlags.PeerUnidiStreamCount;
123 quicSettings.PeerBidiStreamCount = (ushort)options.MaxBidirectionalStreams;
124 quicSettings.PeerUnidiStreamCount = (ushort)options.MaxUnidirectionalStreams;
126 if (options.IdleTimeout != Timeout.InfiniteTimeSpan)
127 {
128 if (options.IdleTimeout <= TimeSpan.Zero)
129 {
130 throw new Exception("IdleTimeout must not be negative.");
131 }
132 ulong num = (ulong)options.IdleTimeout.Ticks / 10000uL;
133 if (num > 4611686018427387903L)
134 {
135 throw new Exception("IdleTimeout is too large (max 2^62-1 milliseconds)");
136 }
137 settings.IsSetFlags |= MsQuicNativeMethods.QuicSettingsIsSetFlags.IdleTimeoutMs;
138 settings.IdleTimeoutMs = (ulong)options.IdleTimeout.TotalMilliseconds;
139 }
140 X509Certificate2[] array = null;
141 MemoryHandle[] handles = null;
143 uint status;
144 SafeMsQuicConfigurationHandle configuration;
145 try
146 {
148 status = MsQuicApi.Api.ConfigurationOpenDelegate(MsQuicApi.Api.Registration, (MsQuicNativeMethods.QuicBuffer*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(buffers, 0), (uint)alpnProtocols.Count, ref settings, (uint)sizeof(MsQuicNativeMethods.QuicSettings), IntPtr.Zero, out configuration);
149 }
150 finally
151 {
153 }
154 QuicExceptionHelpers.ThrowIfFailed(status, "ConfigurationOpen failed.");
155 try
156 {
158 credConfig.Flags = flags;
159 if (certificateContext != null)
160 {
163 if (certificate == null || array == null)
164 {
165 throw new ArgumentException("certificateContext");
166 }
167 }
168 if (certificate != null)
169 {
171 {
172 credConfig.Type = QUIC_CREDENTIAL_TYPE.CONTEXT;
173 credConfig.Certificate = certificate.Handle;
174 status = MsQuicApi.Api.ConfigurationLoadCredentialDelegate(configuration, ref credConfig);
175 }
176 else
177 {
178 byte[] array2;
179 if (array != null && array.Length != 0)
180 {
183 for (int i = 0; i < array?.Length; i++)
184 {
186 }
188 }
189 else
190 {
191 array2 = certificate.Export(X509ContentType.Pfx);
192 }
193 fixed (byte* ptr = array2)
194 {
195 void* ptr2 = ptr;
197 credentialConfigCertificatePkcs.Asn1Blob = (IntPtr)ptr2;
198 credentialConfigCertificatePkcs.Asn1BlobLength = (uint)array2.Length;
199 credentialConfigCertificatePkcs.PrivateKeyPassword = IntPtr.Zero;
200 credConfig.Type = QUIC_CREDENTIAL_TYPE.PKCS12;
201 credConfig.Certificate = (IntPtr)(&credentialConfigCertificatePkcs);
202 status = MsQuicApi.Api.ConfigurationLoadCredentialDelegate(configuration, ref credConfig);
203 }
204 }
205 }
206 else
207 {
208 credConfig.Type = QUIC_CREDENTIAL_TYPE.NONE;
209 status = MsQuicApi.Api.ConfigurationLoadCredentialDelegate(configuration, ref credConfig);
210 }
211 QuicExceptionHelpers.ThrowIfFailed(status, "ConfigurationLoadCredential failed.");
212 return configuration;
213 }
214 catch
215 {
216 configuration.Dispose();
217 throw;
218 }
219 }
220}
void Add(TKey key, TValue value)
static unsafe void Prepare(List< SslApplicationProtocol > alpnProtocols, [NotNull] out MemoryHandle[] handles, [NotNull] out MsQuicNativeMethods.QuicBuffer[] buffers)
static void Return(ref MemoryHandle[] handles, ref MsQuicNativeMethods.QuicBuffer[] buffers)
static void ThrowIfFailed(uint status, string message=null, Exception innerException=null)
static unsafe SafeMsQuicConfigurationHandle Create(QuicOptions options, QUIC_CREDENTIAL_FLAGS flags, X509Certificate certificate, SslStreamCertificateContext certificateContext, List< SslApplicationProtocol > alpnProtocols)
static SafeMsQuicConfigurationHandle Create(QuicOptions options, SslServerAuthenticationOptions serverAuthenticationOptions, string targetHost=null)
static SafeMsQuicConfigurationHandle Create(QuicClientConnectionOptions options)
static unsafe IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Definition Marshal.cs:775
static string net_quic_ssl_option
Definition SR.cs:34
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5
static readonly IntPtr Zero
Definition IntPtr.cs:18
static readonly TimeSpan Zero
Definition TimeSpan.cs:21