Skip to content

Commit 6da2741

Browse files
committed
Add test for 2.1
1 parent 78ada7f commit 6da2741

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

src/Servers/Kestrel/Core/src/Internal/HttpsConnectionAdapter.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,7 @@ private async Task<IAdaptedConnection> InnerOnConnectionAsync(ConnectionAdapterC
166166
sslOptions.ApplicationProtocols.Add(SslApplicationProtocol.Http11);
167167
}
168168

169-
try
170-
{
171-
await sslStream.AuthenticateAsServerAsync(sslOptions, CancellationToken.None);
172-
}
173-
catch (AuthenticationException ex)
174-
{
175-
if (_serverCertificate != null &&
176-
CertificateManager.IsHttpsDevelopmentCertificate(_serverCertificate) &&
177-
!CertificateManager.CheckDeveloperCertificateKey(_serverCertificate))
178-
{
179-
_logger.LogError(3, ex, CoreStrings.BadDeveloperCertificateState);
180-
}
181-
throw;
182-
}
169+
await sslStream.AuthenticateAsServerAsync(sslOptions, CancellationToken.None);
183170
#else
184171
var serverCert = _serverCertificate;
185172
if (_serverCertificateSelector != null)
@@ -191,22 +178,9 @@ private async Task<IAdaptedConnection> InnerOnConnectionAsync(ConnectionAdapterC
191178
EnsureCertificateIsAllowedForServerAuth(serverCert);
192179
}
193180
}
194-
try
195-
{
196-
await sslStream.AuthenticateAsServerAsync(serverCert, certificateRequired,
197-
_options.SslProtocols, _options.CheckCertificateRevocation);
198181

199-
}
200-
catch (AuthenticationException ex)
201-
{
202-
if (_serverCertificate != null &&
203-
CertificateManager.IsHttpsDevelopmentCertificate(_serverCertificate) &&
204-
!CertificateManager.CheckDeveloperCertificateKey(_serverCertificate))
205-
{
206-
_logger.LogError(3, ex, CoreStrings.BadDeveloperCertificateState);
207-
}
208-
throw;
209-
}
182+
await sslStream.AuthenticateAsServerAsync(serverCert, certificateRequired,
183+
_options.SslProtocols, _options.CheckCertificateRevocation);
210184
#endif
211185
}
212186
catch (OperationCanceledException)
@@ -223,6 +197,13 @@ await sslStream.AuthenticateAsServerAsync(serverCert, certificateRequired,
223197
}
224198
finally
225199
{
200+
if (!sslStream.IsAuthenticated && (_serverCertificate != null ||
201+
CertificateManager.IsHttpsDevelopmentCertificate(_serverCertificate) ||
202+
!CertificateManager.CheckDeveloperCertificateKey(_serverCertificate)))
203+
{
204+
_logger?.LogError(3, CoreStrings.BadDeveloperCertificateState);
205+
}
206+
226207
timeoutFeature.CancelTimeout();
227208
}
228209

src/Servers/Kestrel/test/FunctionalTests/HttpsTests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -244,6 +244,40 @@ await sslStream.AuthenticateAsClientAsync("127.0.0.1", clientCertificates: null,
244244
Assert.False(loggerProvider.ErrorLogger.ObjectDisposedExceptionLogged);
245245
}
246246

247+
[Fact]
248+
public async Task DevCertWithInvalidPrivateKeyProducesCustomWarning()
249+
{
250+
var loggerProvider = new HandshakeErrorLoggerProvider();
251+
LoggerFactory.AddProvider(loggerProvider);
252+
253+
var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0));
254+
listenOptions.KestrelServerOptions = new KestrelServerOptions();
255+
listenOptions.KestrelServerOptions.ApplicationServices = new ServiceCollection()
256+
.AddSingleton(LoggerFactory)
257+
.BuildServiceProvider();
258+
259+
var serverCertificate = new X509Certificate2(TestResources.GetTestCertificate().Export(X509ContentType.Cert));
260+
listenOptions.UseHttps(serverCertificate);
261+
using (var server = new TestServer(context => Task.CompletedTask,
262+
new TestServiceContext(LoggerFactory),
263+
listenOptions))
264+
{
265+
using (var connection = server.CreateConnection())
266+
using (var sslStream = new SslStream(connection.Stream, true, (sender, certificate, chain, errors) => true))
267+
{
268+
// SslProtocols.Tls is TLS 1.0 which isn't supported by Kestrel by default.
269+
await Assert.ThrowsAsync<IOException>(() =>
270+
sslStream.AuthenticateAsClientAsync("127.0.0.1", clientCertificates: null,
271+
enabledSslProtocols: SslProtocols.Tls,
272+
checkCertificateRevocation: false));
273+
}
274+
}
275+
276+
await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout();
277+
Assert.Equal(3, loggerProvider.FilterLogger.LastEventId);
278+
Assert.Equal(LogLevel.Error, loggerProvider.FilterLogger.LastLogLevel);
279+
}
280+
247281
[Fact]
248282
public async Task DoesNotThrowObjectDisposedExceptionFromWriteAsyncAfterConnectionIsAborted()
249283
{

0 commit comments

Comments
 (0)