-
Notifications
You must be signed in to change notification settings - Fork 10.4k
feat(HTTP.SYS): on-demand TLS client hello retrieval #62209
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
DeagleGross
merged 8 commits into
dotnet:main
from
DeagleGross:dmkorolev/httpsys-ondemand-tlsclienthello
Jun 7, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a9b95eb
drop callback / implement on-demand tls client hello access
DeagleGross 7b11c44
fix build, remove tests for callback (no way to test current behavior?)
DeagleGross b2787d5
change message
DeagleGross b431d5d
Merge branch 'main' into dmkorolev/httpsys-ondemand-tlsclienthello
DeagleGross 60f23f0
PR comments & smoke tests
DeagleGross 65fbefe
pr review
DeagleGross 546cb71
doc comments
DeagleGross 18942f2
Merge branch 'main' into dmkorolev/httpsys-ondemand-tlsclienthello
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
There are no files selected for viewing
74 changes: 31 additions & 43 deletions
74
src/Servers/HttpSys/samples/TlsFeaturesObserve/Program.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 |
---|---|---|
@@ -1,60 +1,48 @@ | ||
// 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; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Server.HttpSys; | ||
using Microsoft.Extensions.Hosting; | ||
using TlsFeatureObserve; | ||
using TlsFeaturesObserve.HttpSys; | ||
|
||
HttpSysConfigurator.ConfigureCacheTlsClientHello(); | ||
CreateHostBuilder(args).Build().Run(); | ||
|
||
static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHost(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>() | ||
.UseHttpSys(options => | ||
{ | ||
// If you want to use https locally: https://stackoverflow.com/a/51841893 | ||
options.UrlPrefixes.Add("https://*:6000"); // HTTPS | ||
|
||
options.Authentication.Schemes = AuthenticationSchemes.None; | ||
options.Authentication.AllowAnonymous = true; | ||
|
||
options.TlsClientHelloBytesCallback = ProcessTlsClientHello; | ||
}); | ||
}); | ||
|
||
static void ProcessTlsClientHello(IFeatureCollection features, ReadOnlySpan<byte> tlsClientHelloBytes) | ||
{ | ||
var httpConnectionFeature = features.Get<IHttpConnectionFeature>(); | ||
|
||
var myTlsFeature = new MyTlsFeature( | ||
connectionId: httpConnectionFeature.ConnectionId, | ||
tlsClientHelloLength: tlsClientHelloBytes.Length); | ||
|
||
features.Set<IMyTlsFeature>(myTlsFeature); | ||
} | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
public interface IMyTlsFeature | ||
builder.WebHost.UseHttpSys(options => | ||
{ | ||
string ConnectionId { get; } | ||
int TlsClientHelloLength { get; } | ||
} | ||
options.UrlPrefixes.Add("https://*:6000"); | ||
options.Authentication.Schemes = AuthenticationSchemes.None; | ||
options.Authentication.AllowAnonymous = true; | ||
}); | ||
|
||
public class MyTlsFeature : IMyTlsFeature | ||
var app = builder.Build(); | ||
|
||
app.Use(async (context, next) => | ||
{ | ||
public string ConnectionId { get; } | ||
public int TlsClientHelloLength { get; } | ||
|
||
public MyTlsFeature(string connectionId, int tlsClientHelloLength) | ||
{ | ||
ConnectionId = connectionId; | ||
TlsClientHelloLength = tlsClientHelloLength; | ||
} | ||
} | ||
var connectionFeature = context.Features.GetRequiredFeature<IHttpConnectionFeature>(); | ||
var httpSysPropFeature = context.Features.GetRequiredFeature<IHttpSysRequestPropertyFeature>(); | ||
|
||
// first time invocation to find out required size | ||
var success = httpSysPropFeature.TryGetTlsClientHello(Array.Empty<byte>(), out var bytesReturned); | ||
Debug.Assert(!success); | ||
Debug.Assert(bytesReturned > 0); | ||
|
||
// rent with enough memory span and invoke | ||
var bytes = ArrayPool<byte>.Shared.Rent(bytesReturned); | ||
success = httpSysPropFeature.TryGetTlsClientHello(bytes, out _); | ||
Debug.Assert(success); | ||
|
||
await context.Response.WriteAsync($"[Response] connectionId={connectionFeature.ConnectionId}; tlsClientHello.length={bytesReturned}; tlsclienthello start={string.Join(' ', bytes.AsSpan(0, 30).ToArray())}"); | ||
await next(context); | ||
}); | ||
|
||
app.Run(); |
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Server.HttpSys; | ||
|
||
/// <summary> | ||
/// Provides API to read HTTP_REQUEST_PROPERTY value from the HTTP.SYS request. | ||
/// <see href="https://learn.microsoft.com/windows/win32/api/http/ne-http-http_request_property"/> | ||
/// </summary> | ||
public interface IHttpSysRequestPropertyFeature | ||
{ | ||
/// <summary> | ||
/// Reads the TLS client hello from HTTP.SYS | ||
/// </summary> | ||
/// <param name="tlsClientHelloBytesDestination">Where the raw bytes of the TLS Client Hello message are written.</param> | ||
/// <param name="bytesReturned"> | ||
/// Returns the number of bytes written to <paramref name="tlsClientHelloBytesDestination"/>. | ||
/// Or can return the size of the buffer needed if <paramref name="tlsClientHelloBytesDestination"/> wasn't large enough. | ||
/// </param> | ||
/// <remarks> | ||
/// Works only if <c>HTTP_SERVICE_CONFIG_SSL_FLAG_ENABLE_CACHE_CLIENT_HELLO</c> flag is set on http.sys service configuration. | ||
/// See <see href="https://learn.microsoft.com/windows/win32/api/http/nf-http-httpsetserviceconfiguration"/> | ||
/// and <see href="https://learn.microsoft.com/windows/win32/api/http/ne-http-http_service_config_id"/> | ||
/// <br/><br/> | ||
/// If you don't want to guess the required <paramref name="tlsClientHelloBytesDestination"/> size before first invocation, | ||
/// you should first call with <paramref name="tlsClientHelloBytesDestination"/> set to empty size, so that you can retrieve the required buffer size from <paramref name="bytesReturned"/>, | ||
/// then allocate that amount of memory and retry the query. | ||
/// </remarks> | ||
/// <returns> | ||
/// True, if fetching TLS client hello was successful, false if <paramref name="tlsClientHelloBytesDestination"/> size is not large enough. | ||
/// If unsuccessful for other reason throws an exception. | ||
/// </returns> | ||
/// <exception cref="HttpSysException">Any HttpSys error except for ERROR_INSUFFICIENT_BUFFER or ERROR_MORE_DATA.</exception> | ||
/// <exception cref="InvalidOperationException">If HttpSys does not support querying the TLS Client Hello.</exception> | ||
bool TryGetTlsClientHello(Span<byte> tlsClientHelloBytesDestination, out int bytesReturned); | ||
} |
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,5 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Http.Features.IFeatureCollection!, System.ReadOnlySpan<byte>>? | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.set -> void | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.RequestQueueSecurityDescriptor.get -> System.Security.AccessControl.GenericSecurityDescriptor? | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.RequestQueueSecurityDescriptor.set -> void | ||
Microsoft.AspNetCore.Server.HttpSys.IHttpSysRequestPropertyFeature | ||
Microsoft.AspNetCore.Server.HttpSys.IHttpSysRequestPropertyFeature.TryGetTlsClientHello(System.Span<byte> tlsClientHelloBytesDestination, out int bytesReturned) -> bool |
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
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.