Skip to content
Merged
Changes from 2 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
39 changes: 19 additions & 20 deletions src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ internal async Task OnTlsClientHelloAsync(ConnectionContext connection)
// no data is consumed, it will be processed by the follow-up middlewares
input.AdvanceTo(buffer.Start);

switch (parseState)
if (parseState == ClientHelloParseState.NotEnoughData)
{
case ClientHelloParseState.NotEnoughData:
continue;

case ClientHelloParseState.NotTlsClientHello:
await _next(connection);
return;
continue;
}

case ClientHelloParseState.ValidTlsClientHello:
_tlsClientHelloBytesCallback(connection, clientHelloBytes);
await _next(connection);
return;
if (parseState == ClientHelloParseState.ValidTlsClientHello)
{
_tlsClientHelloBytesCallback(connection, clientHelloBytes);
}

// Here either it's a valid TLS client hello or definitely not a TLS client hello.
// Anyway we can continue with the middleware pipeline
break;
}

await _next(connection);
Expand All @@ -78,7 +77,7 @@ private static ClientHelloParseState TryParseClientHello(ReadOnlySequence<byte>
}

// Protocol version
if (!reader.TryReadBigEndian(out short version) || IsValidProtocolVersion(version) == false)
if (!reader.TryReadBigEndian(out short version) || !IsValidProtocolVersion(version))
{
return ClientHelloParseState.NotTlsClientHello;
}
Expand Down Expand Up @@ -109,14 +108,14 @@ private static ClientHelloParseState TryParseClientHello(ReadOnlySequence<byte>
}

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

private enum ClientHelloParseState
=> 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,
Expand Down
Loading