-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathCertificateValidationRemoteServer.cs
416 lines (366 loc) · 17.7 KB
/
CertificateValidationRemoteServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.X509Certificates.Tests.Common;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Microsoft.Win32.SafeHandles;
using Xunit;
namespace System.Net.Security.Tests
{
using Configuration = System.Net.Test.Common.Configuration;
public class CertificateValidationRemoteServer
{
[OuterLoop("Uses external servers")]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.SupportsTls12))] // external server does not support TLS1.1 and below
[InlineData(false)]
[InlineData(true)]
public async Task CertificateValidationRemoteServer_EndToEnd_Ok(bool useAsync)
{
using (var client = new TcpClient(AddressFamily.InterNetwork))
{
try
{
await client.ConnectAsync(Configuration.Security.TlsServer.IdnHost, Configuration.Security.TlsServer.Port);
}
catch (Exception ex)
{
// if we cannot connect, skip the test instead of failing.
// This test is not trying to test networking.
throw new SkipTestException($"Unable to connect to '{Configuration.Security.TlsServer.IdnHost}': {ex.Message}");
}
using (SslStream sslStream = new SslStream(client.GetStream(), false, RemoteHttpsCertValidation, null))
{
try
{
if (useAsync)
{
await sslStream.AuthenticateAsClientAsync(Configuration.Security.TlsServer.IdnHost);
}
else
{
sslStream.AuthenticateAsClient(Configuration.Security.TlsServer.IdnHost);
}
}
catch (IOException ex) when (ex.InnerException is SocketException &&
((SocketException)ex.InnerException).SocketErrorCode == SocketError.ConnectionReset)
{
// Since we try to verify certificate validation, ignore IO errors
// caused most likely by environmental failures.
throw new SkipTestException($"Unable to connect to '{Configuration.Security.TlsServer.IdnHost}': {ex.InnerException.Message}");
}
}
}
}
// MacOS has special validation rules for apple.com and icloud.com
[ConditionalTheory]
[OuterLoop("Uses external servers")]
[InlineData("www.apple.com")]
[InlineData("www.icloud.com")]
[PlatformSpecific(TestPlatforms.OSX)]
public Task CertificateValidationApple_EndToEnd_Ok(string host)
{
return EndToEndHelper(host);
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.SupportsTls12))]
[OuterLoop("Uses external servers")]
[InlineData("api.nuget.org")]
[InlineData("www.github.com.")]
[InlineData("")]
public async Task DefaultConnect_EndToEnd_Ok(string host)
{
if (string.IsNullOrEmpty(host))
{
host = Configuration.Security.TlsServer.IdnHost;
}
await EndToEndHelper(host);
// Second try may change the handshake because of TLS resume.
await EndToEndHelper(host);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/70981", TestPlatforms.OSX)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/68206", TestPlatforms.Android)]
public Task ConnectWithRevocation_WithCallback(bool checkRevocation)
{
X509RevocationMode mode = checkRevocation ? X509RevocationMode.Online : X509RevocationMode.NoCheck;
return ConnectWithRevocation_WithCallback_Core(mode);
}
[PlatformSpecific(TestPlatforms.Linux)]
[ConditionalTheory]
[OuterLoop("Subject to system load race conditions")]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public Task ConnectWithRevocation_StapledOcsp(bool offlineContext, bool noIntermediates)
{
// Offline will only work if
// a) the revocation has been checked recently enough that it is cached, or
// b) the server stapled the response
//
// At high load, the server's background fetch might not have completed before
// this test runs.
return ConnectWithRevocation_WithCallback_Core(X509RevocationMode.Offline, offlineContext, noIntermediates);
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public Task ConnectWithRevocation_ServerCertWithoutContext_NoStapledOcsp()
{
// When using specific certificate, OCSP is disabled e.g. when SslStreamCertificateContext is passed in explicitly.
return ConnectWithRevocation_WithCallback_Core(X509RevocationMode.Offline, offlineContext: null);
}
#if WINDOWS
[ConditionalTheory]
[OuterLoop("Uses external servers")]
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData(X509RevocationMode.Offline)]
[InlineData(X509RevocationMode.Online)]
[InlineData(X509RevocationMode.NoCheck)]
public Task ConnectWithRevocation_RemoteServer_StapledOcsp_FromWindows(X509RevocationMode revocationMode)
{
// This test could ideally end at the Client Hello, because it really only wants to
// ensure that the status_request extension was asserted. Since the SslStream tests
// do not currently attempt to intercept and inspect the Client Hello, this test
// obtains the data indirectly: by talking to a host known to do OCSP Server Stapling
// with revocation in Offline mode.
// Unfortunately, this test will fail if the remote host stops doing server stapling,
// but it's the best we can do right now.
string serverName = Configuration.Http.Http2Host;
SslClientAuthenticationOptions clientOpts = new SslClientAuthenticationOptions
{
TargetHost = serverName,
RemoteCertificateValidationCallback = CertificateValidationCallback,
CertificateRevocationCheckMode = revocationMode,
};
return EndToEndHelper(clientOpts);
static bool CertificateValidationCallback(
object sender,
X509Certificate? certificate,
X509Chain? chain,
SslPolicyErrors sslPolicyErrors)
{
Assert.NotNull(certificate);
using (SafeCertContextHandle ctx = new SafeCertContextHandle(certificate.Handle, ownsHandle: false))
{
bool hasStapledOcsp =
ctx.CertHasProperty(Interop.Crypt32.CertContextPropId.CERT_OCSP_RESPONSE_PROP_ID);
if (((SslStream)sender).CheckCertRevocationStatus)
{
Assert.True(hasStapledOcsp, "Cert has stapled OCSP data");
}
else
{
Assert.False(hasStapledOcsp, "Cert has stapled OCSP data");
}
}
return true;
}
}
#endif
private async Task ConnectWithRevocation_WithCallback_Core(
X509RevocationMode revocationMode,
bool? offlineContext = false,
bool noIntermediates = false)
{
string offlinePart = offlineContext.HasValue ? offlineContext.GetValueOrDefault().ToString().ToLower() : "null";
string serverName = $"{revocationMode.ToString().ToLower()}.{offlinePart}.server.example";
(Stream clientStream, Stream serverStream) = TestHelper.GetConnectedStreams();
CertificateAuthority.BuildPrivatePki(
PkiOptions.EndEntityRevocationViaOcsp | PkiOptions.CrlEverywhere,
out RevocationResponder responder,
out CertificateAuthority rootAuthority,
out CertificateAuthority[] intermediateAuthorities,
out X509Certificate2 serverCert,
intermediateAuthorityCount: noIntermediates ? 0 : 1,
subjectName: serverName,
keySize: 2048,
extensions: Configuration.Certificates.BuildTlsServerCertExtensions(serverName));
CertificateAuthority issuingAuthority = noIntermediates ? rootAuthority : intermediateAuthorities[0];
X509Certificate2 issuerCert = issuingAuthority.CloneIssuerCert();
X509Certificate2 rootCert = rootAuthority.CloneIssuerCert();
SslClientAuthenticationOptions clientOpts = new SslClientAuthenticationOptions
{
TargetHost = serverName,
RemoteCertificateValidationCallback = CertificateValidationCallback,
CertificateChainPolicy = new X509ChainPolicy
{
RevocationMode = revocationMode,
TrustMode = X509ChainTrustMode.CustomRootTrust,
// The offline test will not know about revocation for the intermediate,
// so change the policy to only check the end certificate.
RevocationFlag = X509RevocationFlag.EndCertificateOnly,
ExtraStore =
{
issuerCert,
},
CustomTrustStore =
{
rootCert,
},
},
};
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
X509Certificate2 temp = new X509Certificate2(serverCert.Export(X509ContentType.Pkcs12));
serverCert.Dispose();
serverCert = temp;
}
try
{
await using (clientStream)
await using (serverStream)
using (responder)
using (rootAuthority)
using (serverCert)
using (issuerCert)
using (rootCert)
await using (SslStream tlsClient = new SslStream(clientStream))
await using (SslStream tlsServer = new SslStream(serverStream))
{
issuingAuthority.Revoke(serverCert, serverCert.NotBefore);
SslServerAuthenticationOptions serverOpts = new SslServerAuthenticationOptions();
if (offlineContext.HasValue)
{
serverOpts.ServerCertificateContext = SslStreamCertificateContext.Create(
serverCert,
new X509Certificate2Collection(issuerCert),
offlineContext.GetValueOrDefault());
if (revocationMode == X509RevocationMode.Offline)
{
if (offlineContext.GetValueOrDefault(false))
{
// Add a delay just to show we're not winning because of race conditions.
await Task.Delay(200);
}
else
{
if (!OperatingSystem.IsLinux())
{
throw new InvalidOperationException(
"This test configuration uses reflection and is only defined for Linux.");
}
FieldInfo pendingDownloadTaskField = typeof(SslStreamCertificateContext).GetField(
"_pendingDownload",
BindingFlags.Instance | BindingFlags.NonPublic);
if (pendingDownloadTaskField is null)
{
throw new InvalidOperationException("Cannot find the pending download field.");
}
Task download = (Task)pendingDownloadTaskField.GetValue(serverOpts.ServerCertificateContext);
// If it's null, it should mean it has already finished. If not, it might not have.
if (download is not null)
{
await download;
}
}
}
}
else
{
serverOpts.ServerCertificate = serverCert;
}
Task serverTask = tlsServer.AuthenticateAsServerAsync(serverOpts);
Task clientTask = tlsClient.AuthenticateAsClientAsync(clientOpts);
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(clientTask, serverTask);
}
}
finally
{
foreach (CertificateAuthority intermediateAuthority in intermediateAuthorities)
{
intermediateAuthority.Dispose();
}
}
static bool CertificateValidationCallback(
object sender,
X509Certificate? certificate,
X509Chain? chain,
SslPolicyErrors sslPolicyErrors)
{
Assert.NotNull(certificate);
Assert.NotNull(chain);
if (chain.ChainPolicy.RevocationMode == X509RevocationMode.NoCheck)
{
X509ChainStatusFlags chainFlags = 0;
foreach (X509ChainStatus status in chain.ChainStatus)
{
chainFlags |= status.Status;
}
Assert.Equal(X509ChainStatusFlags.NoError, chainFlags);
// The call didn't request revocation, so the chain should have been trusted.
Assert.Equal(SslPolicyErrors.None, sslPolicyErrors);
}
else if ((certificate.Subject.Contains(".true.server.") || certificate.Subject.Contains(".null.server.")) &&
chain.ChainPolicy.RevocationMode == X509RevocationMode.Offline)
{
// In an Offline chain with an offline context the revocation still shouldn't
// process, because there's no OCSP data.
Assert.Equal(SslPolicyErrors.RemoteCertificateChainErrors, sslPolicyErrors);
X509ChainStatusFlags[] flags = chain.ChainElements[0].ChainElementStatus.Select(cs => cs.Status).ToArray();
Assert.Contains(X509ChainStatusFlags.RevocationStatusUnknown, flags);
}
else
{
// Revocation was requested, and the cert is revoked, so the callback should
// say the chain isn't happy.
Assert.Equal(SslPolicyErrors.RemoteCertificateChainErrors, sslPolicyErrors);
X509ChainStatusFlags[] flags = chain.ChainElements[0].ChainElementStatus.Select(cs => cs.Status).ToArray();
Assert.Contains(X509ChainStatusFlags.Revoked, flags);
}
return true;
}
}
private async Task EndToEndHelper(string host)
{
using (var client = new TcpClient())
{
try
{
await client.ConnectAsync(host, 443);
}
catch (Exception ex)
{
// if we cannot connect skip the test instead of failing.
throw new SkipTestException($"Unable to connect to '{host}': {ex.Message}");
}
using (SslStream sslStream = new SslStream(client.GetStream(), false, RemoteHttpsCertValidation, null))
{
await sslStream.AuthenticateAsClientAsync(host);
}
}
}
private async Task EndToEndHelper(SslClientAuthenticationOptions clientOptions)
{
using (var client = new TcpClient())
{
try
{
await client.ConnectAsync(clientOptions.TargetHost, 443);
}
catch (Exception ex)
{
// if we cannot connect skip the test instead of failing.
throw new SkipTestException($"Unable to connect to '{clientOptions.TargetHost}': {ex.Message}");
}
using (SslStream sslStream = new SslStream(client.GetStream()))
{
await sslStream.AuthenticateAsClientAsync(clientOptions);
}
}
}
private bool RemoteHttpsCertValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Assert.Equal(SslPolicyErrors.None, sslPolicyErrors);
return true;
}
}
}