Repository navigation
[Experimental] DirectTls transport implementation #67912
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
Merged
wtgodbe
merged 63 commits into
dotnet:main
from
DeagleGross:dmkorolev/net11-aspnetcore-pr
Aug 14, 2026
+7,990
−4
Merged
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
ef06f98
Add experimental DirectTls transport for Kestrel
DeagleGross 953188a
DirectTls: apply API-review-approved cosmetic changes
DeagleGross 0cc6bc9
DirectTls: drop custom ClientHello delegate; move protocols off facto…
DeagleGross fb0a8d6
DirectTls: revert ClientHello callback to Action<ConnectionContext, R…
DeagleGross 85b42a8
DirectTls: move DirectTlsEndpoint(+Options) into Kestrel.Core; socket…
DeagleGross 60d0881
Ship DirectTls transport as a standalone NuGet package
DeagleGross b2b01d7
Add package README and processor-based default WorkerCount for DirectTls
DeagleGross 5fd171e
Remove package README from DirectTls transport
DeagleGross dea9524
Fix DirectTls epoll P/Invokes missing SetLastError=true
DeagleGross 57dd412
log error on epoll closure fail
DeagleGross 8e2fb2c
cleanup tlspumpeventpool ; add per-endpoint workercount
DeagleGross 6399f02
fix renego read/write collapses
DeagleGross 9940470
cover connectionIoState with tests
DeagleGross 86e71f0
comment on dispose/shutdown race condition problem
DeagleGross acbdfd9
Fix DirectTls accept loop spin on listen socket teardown
DeagleGross a615b90
Dispose DirectTls native TLS contexts on listener teardown
DeagleGross b51f8a4
Dispose accepted client certificate on DirectTls connection teardown
DeagleGross 2be824e
defend for linux OS
DeagleGross 5756ed5
Trim unrelated AspNetCore.slnx reordering churn
DeagleGross 4ecbdf3
Disable AIA certificate downloads during client-cert chain build
DeagleGross 635e2a7
Extract ClientCertificateValidator seam and add unit tests
DeagleGross ff076ff
Add DirectTls HandshakeTimeout option and enforce it in the pump
DeagleGross 18f1ad0
cleanup
DeagleGross cbeef60
DirectTls: dispose-race fix, Linux-only test gating, backpressure buf…
BrennanConroy 8d8cf75
DirectTls: de-flake e2e backpressure functional tests
BrennanConroy 4b96398
Fix DirectTls transport concurrency races (pump busy-spin, SSL op ser…
BrennanConroy 4deb145
DirectTls: make _sslLock I/O transitions structurally synchronous
BrennanConroy 6924132
Harden DirectTls resource ownership and pump failures
BrennanConroy 6d5fc02
Address DirectTls review feedback
BrennanConroy bd430b1
Merge upstream/main into dmkorolev/net11-aspnetcore-pr
DeagleGross c1b936a
sync interest for DestinationTooSmall / NeedMoreData
DeagleGross bfb5306
safeguard connectioniostate disposal
DeagleGross da2547d
proper order in complete handshake
DeagleGross abe4846
guard against flushasync backpressure
DeagleGross b62e5f4
arch agnostic epoll native structs
DeagleGross 8402c92
proper LIBC name
DeagleGross 47fe3ff
verify version
DeagleGross cf68430
simplify
DeagleGross 8c12d94
simplify abort
DeagleGross 852d882
drop catching auth exception from session read/write
DeagleGross a681b69
ssl lock on the cancel
DeagleGross 36b3b44
handshake tracker + cap
DeagleGross ffe539c
cache and dispose socket for IConnectionSocketFeature
DeagleGross d8b6da1
refactor to CTS-style pump stop
DeagleGross 3171dc6
react to failed epoll_ctl
DeagleGross ed47653
guard acceptedSocket
DeagleGross 02153ee
fail connection if callback failed
DeagleGross 314149b
update interest on specific closed+armed EPOLLOUT case
DeagleGross 6e229ab
dispose allocated resources on transport.Bind() failures
DeagleGross e3adc22
handle EPOLLERR/EPOLLHUP
DeagleGross 128b15b
accept4 to simplify creation
DeagleGross f6a73af
cap accepting connections in a single epoll wakeup iteration
DeagleGross 5b65042
use min alloc buffer size for memory alloc
DeagleGross b1c56a3
practically select a suitable poll timeout
DeagleGross 593bb16
dont catch object disposed while tls fatal error
DeagleGross 6a6179b
nit
DeagleGross 40a236d
document blocking callback behavior
DeagleGross d97640f
use SOCK_CLOEXEC
DeagleGross 457f618
cap early
DeagleGross cd8fb71
critical epoll error leading to host shutdown
DeagleGross 04d955e
add explicit lock on socket creation / disposal
DeagleGross 3a19a2b
fire and forget conncetion disposal for closed-channel shutdown failure
DeagleGross c585e7f
nit
DeagleGross 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Net; | ||
|
|
||
| namespace Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls; | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="IPEndPoint"/> that opts an endpoint into the DirectTls transport. Bind it with | ||
| /// <c>KestrelServerOptions.Listen(...)</c> (or <c>ListenAnyIP</c>/<c>ListenLocalhost</c>) to serve that | ||
| /// endpoint over the native, file-descriptor-bound OpenSSL TLS session instead of <c>SslStream</c>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// DirectTls is TLS-only. The endpoint's <see cref="Options"/> must supply a server certificate (via | ||
| /// <see cref="DirectTlsEndpointOptions.ServerCertificate"/> or | ||
| /// <see cref="DirectTlsEndpointOptions.ServerCertificateSelector"/>). | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental("ASPNETCORE_DIRECTTLS_001", UrlFormat = "https://aka.ms/aspnetcore/directtls")] | ||
| public sealed class DirectTlsEndpoint : IPEndPoint | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new <see cref="DirectTlsEndpoint"/> with a default <see cref="DirectTlsEndpointOptions"/>. | ||
| /// </summary> | ||
| /// <param name="address">The IP address to listen on.</param> | ||
| /// <param name="port">The port to listen on.</param> | ||
| public DirectTlsEndpoint(IPAddress address, int port) | ||
| : this(address, port, new DirectTlsEndpointOptions()) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new <see cref="DirectTlsEndpoint"/> with the specified TLS options. | ||
| /// </summary> | ||
| /// <param name="address">The IP address to listen on.</param> | ||
| /// <param name="port">The port to listen on.</param> | ||
| /// <param name="options">The per-endpoint TLS configuration.</param> | ||
| public DirectTlsEndpoint(IPAddress address, int port, DirectTlsEndpointOptions options) | ||
| : base(address, port) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(options); | ||
| Options = options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new <see cref="DirectTlsEndpoint"/> from an existing <see cref="IPEndPoint"/> with the specified TLS options. | ||
| /// </summary> | ||
| /// <param name="endpoint">The IP endpoint (address and port) to listen on.</param> | ||
| /// <param name="options">The per-endpoint TLS configuration.</param> | ||
| public DirectTlsEndpoint(IPEndPoint endpoint, DirectTlsEndpointOptions options) | ||
| : this((endpoint ?? throw new ArgumentNullException(nameof(endpoint))).Address, endpoint.Port, options) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The per-endpoint TLS configuration for this endpoint. | ||
| /// </summary> | ||
| public DirectTlsEndpointOptions Options { get; } | ||
| } |
143 changes: 143 additions & 0 deletions
143
src/Servers/Kestrel/Core/src/DirectTlsEndpointOptions.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,143 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Net.Security; | ||
| using System.Security.Authentication; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using Microsoft.AspNetCore.Connections; | ||
| using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
| using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
|
|
||
| namespace Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls; | ||
|
|
||
| /// <summary> | ||
| /// Per-endpoint TLS configuration for an endpoint served by the DirectTls transport. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// DirectTls is TLS-only: every endpoint served by this transport terminates TLS, so a server certificate | ||
| /// (either <see cref="ServerCertificate"/> or <see cref="ServerCertificateSelector"/>) is required. | ||
| /// </remarks> | ||
| [Experimental("ASPNETCORE_DIRECTTLS_001", UrlFormat = "https://aka.ms/aspnetcore/directtls")] | ||
| public sealed class DirectTlsEndpointOptions | ||
| { | ||
| /// <summary> | ||
| /// The server certificate presented during the TLS handshake. Used when | ||
| /// <see cref="ServerCertificateSelector"/> is not set or returns <see langword="null"/>. | ||
| /// </summary> | ||
| public X509Certificate2? ServerCertificate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// A callback that selects the server certificate based on the connection and the SNI host name parsed | ||
| /// from the ClientHello. When set, it is invoked once per connection before the certificate is installed; | ||
| /// returning <see langword="null"/> falls back to <see cref="ServerCertificate"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The first argument is the <see cref="ConnectionContext"/> for the connection being negotiated (already | ||
| /// allocated so it carries the same connection id that will later serve the request); the second is the | ||
| /// requested SNI host name, or <see langword="null"/> when the client did not send one. | ||
| /// <para> | ||
| /// This callback runs synchronously on the epoll worker thread that owns the connection and must not block. | ||
| /// A blocking or long-running callback stalls the handshake and I/O of every connection assigned to that worker. | ||
| /// </para> | ||
| /// </remarks> | ||
| public Func<ConnectionContext?, string?, X509Certificate2?>? ServerCertificateSelector { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The allowable TLS protocol versions. <see cref="SslProtocols.None"/> (the default) lets the operating | ||
| /// system choose an appropriate default set. | ||
| /// </summary> | ||
| public SslProtocols SslProtocols { get; set; } = SslProtocols.None; | ||
|
|
||
| /// <summary> | ||
| /// Whether a client certificate is requested and/or required during the handshake (mutual TLS). | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Defaults to <see cref="ClientCertificateMode.NoCertificate"/>. | ||
| /// <see cref="ClientCertificateMode.DelayCertificate"/> is not supported (see https://github.com/dotnet/aspnetcore/issues/67915) | ||
| /// </remarks> | ||
| public ClientCertificateMode ClientCertificateMode { get; set; } = ClientCertificateMode.NoCertificate; | ||
|
|
||
| /// <summary> | ||
| /// A callback used to validate a client certificate when <see cref="ClientCertificateMode"/> requests one. | ||
| /// Return <see langword="true"/> to accept the certificate. When not set, a certificate is accepted only | ||
| /// when it produced no <see cref="SslPolicyErrors"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This callback runs synchronously on the epoll worker thread that owns the connection and must not block. | ||
| /// A blocking or long-running callback stalls the handshake and I/O of every connection assigned to that worker. | ||
| /// </remarks> | ||
| public Func<X509Certificate2, X509Chain?, SslPolicyErrors, bool>? ClientCertificateValidation { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// An optional callback invoked with the raw ClientHello record bytes as soon as they are parsed, before | ||
| /// the handshake completes. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The <see cref="ReadOnlySequence{T}"/> is only valid for the duration of the callback; copy the bytes | ||
| /// (for example with <c>ToArray()</c>) if they must outlive the call. The first argument is the | ||
| /// <see cref="ConnectionContext"/> for the connection being negotiated. | ||
| /// <para> | ||
| /// This callback runs synchronously on the epoll worker thread that owns the connection and must not block. | ||
| /// A blocking or long-running callback stalls the handshake and I/O of every connection assigned to that worker. | ||
| /// </para> | ||
| /// </remarks> | ||
| public Action<ConnectionContext, ReadOnlySequence<byte>>? TlsClientHelloBytesCallback { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Overrides the transport-wide worker count (<c>DirectTlsTransportOptions.WorkerCount</c>) for this | ||
| /// endpoint. When <see langword="null"/> (the default), the transport-wide worker count is used. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Each DirectTls endpoint runs its own pool of worker threads, so a server's total thread count is the sum | ||
| /// of every bound endpoint's worker count. Set this on individual endpoints to bound threads when hosting | ||
| /// several DirectTls endpoints — for example, a low-traffic management port can use far fewer workers than a | ||
| /// public HTTPS port. Must be greater than zero when set. | ||
| /// </remarks> | ||
| public int? WorkerCount | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| if (value is <= 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(WorkerCount)} must be greater than zero when set."); | ||
| } | ||
|
|
||
| field = value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The maximum amount of time allowed for the TLS handshake to complete on a connection to this endpoint. | ||
| /// A connection whose handshake does not finish within this window is dropped. Defaults to 10 seconds. | ||
| /// Set to <see cref="Timeout.InfiniteTimeSpan"/> to disable the timeout; any other non-positive value is rejected. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This bounds slow or stalled handshakes — for example a client that opens a connection and then dribbles | ||
| /// the ClientHello one byte at a time — which would otherwise keep a file descriptor and its native TLS | ||
| /// session pinned to a worker indefinitely. It mirrors <see cref="HttpsConnectionAdapterOptions.HandshakeTimeout"/>, | ||
| /// which provides the same protection for the <see cref="SslStream"/>-based HTTPS middleware, and shares its | ||
| /// 10-second default. | ||
| /// </remarks> | ||
| public TimeSpan HandshakeTimeout | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| if (value <= TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), CoreStrings.PositiveTimeSpanRequired); | ||
| } | ||
|
|
||
| field = value != Timeout.InfiniteTimeSpan ? value : TimeSpan.MaxValue; | ||
| } | ||
| } = HttpsConnectionAdapterOptions.DefaultHandshakeTimeout; | ||
|
|
||
| /// <summary> | ||
| /// The HTTP protocols (ALPN) advertised for this endpoint, | ||
| /// sourced from <see cref="ListenOptions.Protocols"/> after the endpoint has been configured. | ||
| /// </summary> | ||
| internal HttpProtocols HttpProtocols { get; set; } = HttpProtocols.Http1AndHttp2; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,2 +1,25 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpoint | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpoint.DirectTlsEndpoint(System.Net.IPAddress! address, int port) -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpoint.DirectTlsEndpoint(System.Net.IPAddress! address, int port, Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions! options) -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpoint.DirectTlsEndpoint(System.Net.IPEndPoint! endpoint, Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions! options) -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpoint.Options.get -> Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions! | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.DirectTlsEndpointOptions() -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ClientCertificateMode.get -> Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ClientCertificateMode.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ClientCertificateValidation.get -> System.Func<System.Security.Cryptography.X509Certificates.X509Certificate2!, System.Security.Cryptography.X509Certificates.X509Chain?, System.Net.Security.SslPolicyErrors, bool>? | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ClientCertificateValidation.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.HandshakeTimeout.get -> System.TimeSpan | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.HandshakeTimeout.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ServerCertificate.get -> System.Security.Cryptography.X509Certificates.X509Certificate2? | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ServerCertificate.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ServerCertificateSelector.get -> System.Func<Microsoft.AspNetCore.Connections.ConnectionContext?, string?, System.Security.Cryptography.X509Certificates.X509Certificate2?>? | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.ServerCertificateSelector.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.SslProtocols.get -> System.Security.Authentication.SslProtocols | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.SslProtocols.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Connections.ConnectionContext!, System.Buffers.ReadOnlySequence<byte>>? | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.TlsClientHelloBytesCallback.set -> void | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.WorkerCount.get -> int? | ||
| Microsoft.AspNetCore.Server.Kestrel.Transport.DirectTls.DirectTlsEndpointOptions.WorkerCount.set -> void | ||
| static Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseTlsClientHelloListener(this Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions! listenOptions, System.Action<Microsoft.AspNetCore.Connections.ConnectionContext!, System.Buffers.ReadOnlySequence<byte>>! tlsClientHelloBytesCallback, System.TimeSpan? timeout = null) -> Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions! |
Oops, something went wrong.
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.