Skip to content

feat: support tls client hello bytes callback in Kestrel #61631

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
merged 20 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -96,6 +97,12 @@ public void AllowAnyClientCertificate()
/// </summary>
public Action<ConnectionContext, SslServerAuthenticationOptions>? OnAuthenticate { get; set; }

/// <summary>
/// A callback to be invoked to get the TLS client hello bytes.
/// Null by default.
/// </summary>
public Action<ConnectionContext, ReadOnlySequence<byte>>? TlsClientHelloBytesCallback { get; set; }

/// <summary>
/// Specifies the maximum amount of time allowed for the TLS/SSL handshake. This must be positive
/// or <see cref="Timeout.InfiniteTimeSpan"/>. Defaults to 10 seconds.
Expand Down
19 changes: 19 additions & 0 deletions src/Servers/Kestrel/Core/src/ListenOptionsHttpsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Core.Middleware;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -197,6 +198,15 @@ public static ListenOptions UseHttps(this ListenOptions listenOptions, HttpsConn
listenOptions.IsTls = true;
listenOptions.HttpsOptions = httpsOptions;

if (httpsOptions.TlsClientHelloBytesCallback is not null)
{
listenOptions.Use(next =>
{
var middleware = new TlsListenerMiddleware(next, httpsOptions.TlsClientHelloBytesCallback);
return middleware.OnTlsClientHelloAsync;
});
}

listenOptions.Use(next =>
{
var middleware = new HttpsConnectionMiddleware(next, httpsOptions, listenOptions.Protocols, loggerFactory, metrics);
Expand Down Expand Up @@ -260,6 +270,15 @@ public static ListenOptions UseHttps(this ListenOptions listenOptions, TlsHandsh
listenOptions.IsTls = true;
listenOptions.HttpsCallbackOptions = callbackOptions;

if (listenOptions.HttpsOptions?.TlsClientHelloBytesCallback is not null)
{
listenOptions.Use(next =>
{
var middleware = new TlsListenerMiddleware(next, listenOptions.HttpsOptions.TlsClientHelloBytesCallback);
return middleware.OnTlsClientHelloAsync;
});
}

listenOptions.Use(next =>
{
// Set the list of protocols from listen options.
Expand Down
150 changes: 150 additions & 0 deletions src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 Microsoft.AspNetCore.Connections;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Middleware;

internal sealed class TlsListenerMiddleware
{
private readonly ConnectionDelegate _next;
private readonly Action<ConnectionContext, ReadOnlySequence<byte>> _tlsClientHelloBytesCallback;

public TlsListenerMiddleware(ConnectionDelegate next, Action<ConnectionContext, ReadOnlySequence<byte>> tlsClientHelloBytesCallback)
{
_next = next;
_tlsClientHelloBytesCallback = tlsClientHelloBytesCallback;
}

/// <summary>
/// Sniffs the TLS Client Hello message, and invokes a callback if found.
/// </summary>
internal async Task OnTlsClientHelloAsync(ConnectionContext connection)
{
var input = connection.Transport.Input;
ClientHelloParseState parseState = ClientHelloParseState.NotEnoughData;

while (true)
{
var result = await input.ReadAsync();
var buffer = result.Buffer;

try
{
// If the buffer length is less than 6 bytes (handshake + version + length + client-hello byte)
// and no more data is coming, we can't block in a loop here because we will not get more data
if (result.IsCompleted && buffer.Length < 6)
{
break;
}

parseState = TryParseClientHello(buffer, out var clientHelloBytes);
if (parseState == ClientHelloParseState.NotEnoughData)
{
// if no data will be added, and we still lack enough bytes
// we can't block in a loop, so just exit
if (result.IsCompleted)
{
break;
}

continue;
}

if (parseState == ClientHelloParseState.ValidTlsClientHello)
{
_tlsClientHelloBytesCallback(connection, clientHelloBytes);
}

Debug.Assert(parseState is ClientHelloParseState.ValidTlsClientHello or ClientHelloParseState.NotTlsClientHello);
break; // We can continue with the middleware pipeline
}
finally
{
if (parseState is ClientHelloParseState.NotEnoughData)
{
input.AdvanceTo(buffer.Start, buffer.End);
}
else
{
// ready to continue middleware pipeline, reset the buffer to initial state
input.AdvanceTo(buffer.Start);
}
}
}

await _next(connection);
}

/// <summary>
/// RFCs
/// ----
/// TLS 1.1: https://datatracker.ietf.org/doc/html/rfc4346#section-6.2
/// TLS 1.2: https://datatracker.ietf.org/doc/html/rfc5246#section-6.2
/// TLS 1.3: https://datatracker.ietf.org/doc/html/rfc8446#section-5.1
/// </summary>
private static ClientHelloParseState TryParseClientHello(ReadOnlySequence<byte> buffer, out ReadOnlySequence<byte> clientHelloBytes)
{
clientHelloBytes = default;

if (buffer.Length < 6)
{
return ClientHelloParseState.NotEnoughData;
}

var reader = new SequenceReader<byte>(buffer);

// Content type must be 0x16 for TLS Handshake
if (!reader.TryRead(out byte contentType) || contentType != 0x16)
{
return ClientHelloParseState.NotTlsClientHello;
}

// Protocol version
if (!reader.TryReadBigEndian(out short version) || !IsValidProtocolVersion(version))
{
return ClientHelloParseState.NotTlsClientHello;
}

// Record length
if (!reader.TryReadBigEndian(out short recordLength))
{
return ClientHelloParseState.NotTlsClientHello;
}

// 5 bytes are
// 1) Handshake (1 byte)
// 2) Protocol version (2 bytes)
// 3) Record length (2 bytes)
if (buffer.Length < 5 + recordLength)
{
return ClientHelloParseState.NotEnoughData;
}

// byte 6: handshake message type (must be 0x01 for ClientHello)
if (!reader.TryRead(out byte handshakeType) || handshakeType != 0x01)
{
return ClientHelloParseState.NotTlsClientHello;
}

clientHelloBytes = buffer.Slice(0, 5 + recordLength);
return ClientHelloParseState.ValidTlsClientHello;
}

private static bool IsValidProtocolVersion(short version)
=> version is 0x0002 // SSL 2.0 (0x0002)
or 0x0300 // SSL 3.0 (0x0300)
or 0x0301 // TLS 1.0 (0x0301)
or 0x0302 // TLS 1.1 (0x0302)
or 0x0303 // TLS 1.2 (0x0303)
or 0x0304; // TLS 1.3 (0x0304)

private enum ClientHelloParseState : byte
{
NotEnoughData,
NotTlsClientHello,
ValidTlsClientHello
}
}
2 changes: 2 additions & 0 deletions src/Servers/Kestrel/Core/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Connections.ConnectionContext!, System.Buffers.ReadOnlySequence<byte>>?
Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions.TlsClientHelloBytesCallback.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override async ValueTask DisposeAsync()
// This piece of code allows us to wait until the PipeReader has been awaited on.
// We need to wrap lots of layers (including the ValueTask) to gain visiblity into when
// the machinery for the await happens
private class ObservableDuplexPipe : IDuplexPipe
internal class ObservableDuplexPipe : IDuplexPipe
{
private readonly ObservablePipeReader _reader;

Expand All @@ -110,11 +110,14 @@ public ObservableDuplexPipe(IDuplexPipe duplexPipe)

public PipeWriter Output { get; }

public int ReadAsyncCounter => _reader.ReadAsyncCounter;

private class ObservablePipeReader : PipeReader
{
private readonly PipeReader _reader;
private readonly TaskCompletionSource _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

public int ReadAsyncCounter { get; private set; } = 0;
public Task WaitForReadTask => _tcs.Task;

public ObservablePipeReader(PipeReader reader)
Expand Down Expand Up @@ -144,6 +147,7 @@ public override void Complete(Exception exception = null)

public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
{
ReadAsyncCounter++;
var task = _reader.ReadAsync(cancellationToken);

if (_tcs.Task.IsCompleted)
Expand All @@ -152,7 +156,7 @@ public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationTo
}

return new ValueTask<ReadResult>(new ObservableValueTask<ReadResult>(task, _tcs), 0);
}
}

public override bool TryRead(out ReadResult result)
{
Expand Down
Loading
Loading