-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Make TLS & QUIC Pay-for-Play #47209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Make TLS & QUIC Pay-for-Play #47209
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1f386d
Make TLS & QUIC Pay-for-Play
amcasey 8041dd1
Drop MultiplexedConnectionMarkerService
amcasey 36e2106
Add support for UseHttps() without explicit opt-in
amcasey 3d6983b
Correct check in ApplyDefaultCertificate
amcasey 91e7e55
Update comments in ListenOptionsHttpsExtensions.cs
amcasey c20c2e1
Retain TLS enablement when KestrelConfigurationLoader is replaced
amcasey a6d600c
Eliminate Action<KestrelConfigurationLoader> in favor of storing the …
amcasey 327ebdb
Fix AltSvc_Http1And2And3EndpointConfigured_NoMultiplexedFactory_NoAlt…
amcasey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel; | ||
|
||
internal interface ITlsConfigurationLoader | ||
{ | ||
void ApplyHttpsDefaults( | ||
KestrelServerOptions serverOptions, | ||
EndpointConfig endpoint, | ||
HttpsConnectionAdapterOptions httpsOptions, | ||
CertificateConfig? defaultCertificateConfig, | ||
ConfigurationReader configurationReader); | ||
|
||
CertificateAndConfig? LoadDefaultCertificate(ConfigurationReader configurationReader); | ||
|
||
void UseHttps(ListenOptions listenOptions, EndpointConfig endpoint, HttpsConnectionAdapterOptions httpsOptions); | ||
} | ||
|
||
internal record CertificateAndConfig(X509Certificate2 Certificate, CertificateConfig CertificateConfig); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
|
||
namespace Microsoft.AspNetCore.Hosting; | ||
|
||
internal interface IUseHttpsHelper | ||
{ | ||
ListenOptions UseHttps(ListenOptions listenOptions); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Servers/Kestrel/Core/src/Internal/Infrastructure/IMultiplexedTransportManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.Net; | ||
using Microsoft.AspNetCore.Connections; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; | ||
|
||
internal interface IMultiplexedTransportManager | ||
{ | ||
bool HasFactories { get; } | ||
|
||
Task<EndPoint> BindAsync(EndPoint endPoint, MultiplexedConnectionDelegate multiplexedConnectionDelegate, ListenOptions listenOptions, CancellationToken cancellationToken); | ||
|
||
Task StopAsync(CancellationToken cancellationToken); | ||
Task StopEndpointsAsync(List<EndpointConfig> endpointsToStop, CancellationToken cancellationToken); | ||
} | ||
19 changes: 19 additions & 0 deletions
19
src/Servers/Kestrel/Core/src/Internal/Infrastructure/ITransportManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.Net; | ||
using Microsoft.AspNetCore.Connections; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; | ||
|
||
internal interface ITransportManager | ||
{ | ||
bool HasFactories { get; } | ||
|
||
Task<EndPoint> BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig? endpointConfig, CancellationToken cancellationToken); | ||
|
||
Task StopAsync(CancellationToken cancellationToken); | ||
Task StopEndpointsAsync(List<EndpointConfig> endpointsToStop, CancellationToken cancellationToken); | ||
} |
148 changes: 148 additions & 0 deletions
148
src/Servers/Kestrel/Core/src/Internal/Infrastructure/MultiplexedTransportManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.IO.Pipelines; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Security; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; | ||
|
||
internal sealed class MultiplexedTransportManager : TransportManagerBase, IMultiplexedTransportManager | ||
{ | ||
private readonly List<IMultiplexedConnectionListenerFactory> _factories; | ||
|
||
public MultiplexedTransportManager( | ||
ServiceContext serviceContext, | ||
IEnumerable<IMultiplexedConnectionListenerFactory> factories) | ||
: base(serviceContext) | ||
{ | ||
_factories = factories.Reverse().ToList(); | ||
} | ||
|
||
public override bool HasFactories => _factories.Count > 0; | ||
|
||
public async Task<EndPoint> BindAsync(EndPoint endPoint, MultiplexedConnectionDelegate multiplexedConnectionDelegate, ListenOptions listenOptions, CancellationToken cancellationToken) | ||
{ | ||
if (!HasFactories) | ||
{ | ||
throw new InvalidOperationException($"Cannot bind with {nameof(MultiplexedConnectionDelegate)} no {nameof(IMultiplexedConnectionListenerFactory)} is registered."); | ||
} | ||
|
||
var features = new FeatureCollection(); | ||
|
||
// HttpsOptions or HttpsCallbackOptions should always be set in production, but it's not set for InMemory tests. | ||
// The QUIC transport will check if TlsConnectionCallbackOptions is missing. | ||
if (listenOptions.HttpsOptions != null) | ||
{ | ||
var sslServerAuthenticationOptions = HttpsConnectionMiddleware.CreateHttp3Options(listenOptions.HttpsOptions); | ||
features.Set(new TlsConnectionCallbackOptions | ||
{ | ||
ApplicationProtocols = sslServerAuthenticationOptions.ApplicationProtocols ?? new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 }, | ||
OnConnection = (context, cancellationToken) => ValueTask.FromResult(sslServerAuthenticationOptions), | ||
OnConnectionState = null, | ||
}); | ||
} | ||
else if (listenOptions.HttpsCallbackOptions != null) | ||
{ | ||
features.Set(new TlsConnectionCallbackOptions | ||
{ | ||
ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 }, | ||
OnConnection = (context, cancellationToken) => | ||
{ | ||
return listenOptions.HttpsCallbackOptions.OnConnection(new TlsHandshakeCallbackContext | ||
{ | ||
ClientHelloInfo = context.ClientHelloInfo, | ||
CancellationToken = cancellationToken, | ||
State = context.State, | ||
Connection = new ConnectionContextAdapter(context.Connection), | ||
}); | ||
}, | ||
OnConnectionState = listenOptions.HttpsCallbackOptions.OnConnectionState, | ||
}); | ||
} | ||
|
||
foreach (var factory in _factories) | ||
{ | ||
var selector = factory as IConnectionListenerFactorySelector; | ||
if (CanBindFactory(endPoint, selector)) | ||
{ | ||
var transport = await factory.BindAsync(endPoint, features, cancellationToken).ConfigureAwait(false); | ||
StartAcceptLoop(new GenericMultiplexedConnectionListener(transport), c => multiplexedConnectionDelegate(c), listenOptions.EndpointConfig); | ||
return transport.EndPoint; | ||
} | ||
} | ||
|
||
throw new InvalidOperationException($"No registered {nameof(IMultiplexedConnectionListenerFactory)} supports endpoint {endPoint.GetType().Name}: {endPoint}"); | ||
} | ||
|
||
/// <summary> | ||
/// TlsHandshakeCallbackContext.Connection is ConnectionContext but QUIC connection only implements BaseConnectionContext. | ||
/// </summary> | ||
private sealed class ConnectionContextAdapter : ConnectionContext | ||
{ | ||
private readonly BaseConnectionContext _inner; | ||
|
||
public ConnectionContextAdapter(BaseConnectionContext inner) => _inner = inner; | ||
|
||
public override IDuplexPipe Transport | ||
{ | ||
get => throw new NotSupportedException("Not supported by HTTP/3 connections."); | ||
set => throw new NotSupportedException("Not supported by HTTP/3 connections."); | ||
} | ||
public override string ConnectionId | ||
{ | ||
get => _inner.ConnectionId; | ||
set => _inner.ConnectionId = value; | ||
} | ||
public override IFeatureCollection Features => _inner.Features; | ||
public override IDictionary<object, object?> Items | ||
{ | ||
get => _inner.Items; | ||
set => _inner.Items = value; | ||
} | ||
public override EndPoint? LocalEndPoint | ||
{ | ||
get => _inner.LocalEndPoint; | ||
set => _inner.LocalEndPoint = value; | ||
} | ||
public override EndPoint? RemoteEndPoint | ||
{ | ||
get => _inner.RemoteEndPoint; | ||
set => _inner.RemoteEndPoint = value; | ||
} | ||
public override CancellationToken ConnectionClosed | ||
{ | ||
get => _inner.ConnectionClosed; | ||
set => _inner.ConnectionClosed = value; | ||
} | ||
public override ValueTask DisposeAsync() => _inner.DisposeAsync(); | ||
} | ||
|
||
private sealed class GenericMultiplexedConnectionListener : IConnectionListener<MultiplexedConnectionContext> | ||
{ | ||
private readonly IMultiplexedConnectionListener _multiplexedConnectionListener; | ||
|
||
public GenericMultiplexedConnectionListener(IMultiplexedConnectionListener multiplexedConnectionListener) | ||
{ | ||
_multiplexedConnectionListener = multiplexedConnectionListener; | ||
} | ||
|
||
public EndPoint EndPoint => _multiplexedConnectionListener.EndPoint; | ||
|
||
public ValueTask<MultiplexedConnectionContext?> AcceptAsync(CancellationToken cancellationToken = default) | ||
=> _multiplexedConnectionListener.AcceptAsync(features: null, cancellationToken); | ||
|
||
public ValueTask UnbindAsync(CancellationToken cancellationToken = default) | ||
=> _multiplexedConnectionListener.UnbindAsync(cancellationToken); | ||
|
||
public ValueTask DisposeAsync() | ||
=> _multiplexedConnectionListener.DisposeAsync(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding
IMultiplexedTransportManager
andITransportManager
, could we registerIMultiplexedHttpsFeatureFactory
? I know it's internal, but it seems like the smaller change unless there's some other benefit to splitting things up.We could have the default implementation throw an
InvalidOperationException
similar toInvalidUseHttpsHelper
.Edit: It might make sense just to combine these:
Or we could add an
ListenOptions.Features
and haveUseHttps(ListenOptions listenOptions)
populate it and keep the one method.I'm a big fan of limiting the number of services even if they're internal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure I understand this suggestion. (I had assumed I could go study
IMultiplexedHttpsFeatureFactory
, but I think you just invented it?) I think you might be suggesting that we add the pay-for-play functionality to a (the?) feature collection, rather than the DI container?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in #47454.