-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Open
DeagleGross
wants to merge
16
commits into
dotnet:main
Choose a base branch
from
DeagleGross:dmkorolev/kestrel-tls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ecbc54d
support tls client hello bytes callback in Kestrel
DeagleGross c2a1d9e
prettify
DeagleGross 5a91164
address comments
DeagleGross 8201939
fix duplicate next middleware
DeagleGross 8e5ed75
nit
DeagleGross 48e07f8
nit nit
DeagleGross 1fcffb8
comments 1
DeagleGross 0581163
exit even if not enough data
DeagleGross 2bbd480
dont re-read the same data
DeagleGross 6f9a6de
increase delays
DeagleGross 4f6bc2b
await the middleware
DeagleGross ab06730
another test fix
DeagleGross 898c415
move tls client hello check
DeagleGross 4f87edd
move tests
DeagleGross 7856693
dont register callback from `TlsClientHelloBytesCallback` to not have…
DeagleGross d64fb45
tests
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
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
149 changes: 149 additions & 0 deletions
149
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.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,149 @@ | ||
// 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) | ||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var result = await input.ReadAsync(); | ||
var buffer = result.Buffer; | ||
|
||
try | ||
{ | ||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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) | ||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
|
||
// byte 6: handshake message type (must be 0x01 for ClientHello) | ||
if (!reader.TryRead(out byte handshakeType) || handshakeType != 0x01) | ||
{ | ||
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; | ||
} | ||
|
||
clientHelloBytes = buffer.Slice(0, 5 + recordLength); | ||
return ClientHelloParseState.ValidTlsClientHello; | ||
} | ||
|
||
private static bool IsValidProtocolVersion(short version) | ||
=> version is 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 | ||
} | ||
} |
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 +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 |
47 changes: 47 additions & 0 deletions
47
src/Servers/Kestrel/Core/test/TestHelpers/ObservablePipeReader.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,47 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO.Pipelines; | ||
using System.Text; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests.TestHelpers; | ||
|
||
internal class ObservablePipeReader : PipeReader | ||
{ | ||
private readonly PipeReader _inner; | ||
|
||
public ObservablePipeReader(PipeReader reader) | ||
{ | ||
_inner = reader; | ||
} | ||
|
||
/// <summary> | ||
/// Number of times <see cref="ReadAsync(CancellationToken)"/> was called. | ||
/// </summary> | ||
public int ReadAsyncCounter { get; private set; } | ||
|
||
public override void AdvanceTo(SequencePosition consumed) | ||
=> _inner.AdvanceTo(consumed); | ||
|
||
public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) | ||
=> _inner.AdvanceTo(consumed, examined); | ||
|
||
public override void CancelPendingRead() | ||
=> _inner.CancelPendingRead(); | ||
|
||
public override void Complete(Exception exception = null) | ||
=> _inner.Complete(exception); | ||
|
||
public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) | ||
{ | ||
ReadAsyncCounter++; | ||
return _inner.ReadAsync(cancellationToken); | ||
} | ||
|
||
public override bool TryRead(out ReadResult result) | ||
{ | ||
return _inner.TryRead(out result); | ||
} | ||
} |
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.
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.
Need to add this for the other UseHttps overload at the bottom of the file?
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.
I think it has a different use case (allows setup via
TlsHandshakeCallbackOptions
), but I added a checkif (listenOptions.HttpsOptions?.TlsClientHelloBytesCallback is not null)
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.
Probably shouldn't add it then.
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.
But it does raise the question about client hello in that case. It also shows we have a client hello API already (although not very good) https://source.dot.net/#Microsoft.AspNetCore.Server.Kestrel.Core/TlsHandshakeCallbackContext.cs,97120719b6888c33,references
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.
I tried to move the callback to
TlsHandshakeCallbackOptions
, but it requires setting OnConnection, and I dont think it should be required for TLS client hello.I personally would leave it where we designed it - in
HttpsConnectionAdapterOptions
. Let me know what you think!(talked with @halter73 about it as well)
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.
Actually, I removed registration from overload using
TlsHandshakeCallbackOptions
, because it can result in 2 registrations of the middleware