|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Buffers; |
| 5 | +using System.Diagnostics; |
| 6 | +using Microsoft.AspNetCore.Connections; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.Server.Kestrel.Core.Middleware; |
| 9 | + |
| 10 | +internal sealed class TlsListenerMiddleware |
| 11 | +{ |
| 12 | + private readonly ConnectionDelegate _next; |
| 13 | + private readonly Action<ConnectionContext, ReadOnlySequence<byte>> _tlsClientHelloBytesCallback; |
| 14 | + |
| 15 | + public TlsListenerMiddleware(ConnectionDelegate next, Action<ConnectionContext, ReadOnlySequence<byte>> tlsClientHelloBytesCallback) |
| 16 | + { |
| 17 | + _next = next; |
| 18 | + _tlsClientHelloBytesCallback = tlsClientHelloBytesCallback; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Sniffs the TLS Client Hello message, and invokes a callback if found. |
| 23 | + /// </summary> |
| 24 | + internal async Task OnTlsClientHelloAsync(ConnectionContext connection) |
| 25 | + { |
| 26 | + var input = connection.Transport.Input; |
| 27 | + ClientHelloParseState parseState = ClientHelloParseState.NotEnoughData; |
| 28 | + |
| 29 | + while (true) |
| 30 | + { |
| 31 | + var result = await input.ReadAsync(); |
| 32 | + var buffer = result.Buffer; |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + // If the buffer length is less than 6 bytes (handshake + version + length + client-hello byte) |
| 37 | + // and no more data is coming, we can't block in a loop here because we will not get more data |
| 38 | + if (result.IsCompleted && buffer.Length < 6) |
| 39 | + { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + parseState = TryParseClientHello(buffer, out var clientHelloBytes); |
| 44 | + if (parseState == ClientHelloParseState.NotEnoughData) |
| 45 | + { |
| 46 | + // if no data will be added, and we still lack enough bytes |
| 47 | + // we can't block in a loop, so just exit |
| 48 | + if (result.IsCompleted) |
| 49 | + { |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (parseState == ClientHelloParseState.ValidTlsClientHello) |
| 57 | + { |
| 58 | + _tlsClientHelloBytesCallback(connection, clientHelloBytes); |
| 59 | + } |
| 60 | + |
| 61 | + Debug.Assert(parseState is ClientHelloParseState.ValidTlsClientHello or ClientHelloParseState.NotTlsClientHello); |
| 62 | + break; // We can continue with the middleware pipeline |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + if (parseState is ClientHelloParseState.NotEnoughData) |
| 67 | + { |
| 68 | + input.AdvanceTo(buffer.Start, buffer.End); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + // ready to continue middleware pipeline, reset the buffer to initial state |
| 73 | + input.AdvanceTo(buffer.Start); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + await _next(connection); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// RFCs |
| 83 | + /// ---- |
| 84 | + /// TLS 1.1: https://datatracker.ietf.org/doc/html/rfc4346#section-6.2 |
| 85 | + /// TLS 1.2: https://datatracker.ietf.org/doc/html/rfc5246#section-6.2 |
| 86 | + /// TLS 1.3: https://datatracker.ietf.org/doc/html/rfc8446#section-5.1 |
| 87 | + /// </summary> |
| 88 | + private static ClientHelloParseState TryParseClientHello(ReadOnlySequence<byte> buffer, out ReadOnlySequence<byte> clientHelloBytes) |
| 89 | + { |
| 90 | + clientHelloBytes = default; |
| 91 | + |
| 92 | + if (buffer.Length < 6) |
| 93 | + { |
| 94 | + return ClientHelloParseState.NotEnoughData; |
| 95 | + } |
| 96 | + |
| 97 | + var reader = new SequenceReader<byte>(buffer); |
| 98 | + |
| 99 | + // Content type must be 0x16 for TLS Handshake |
| 100 | + if (!reader.TryRead(out byte contentType) || contentType != 0x16) |
| 101 | + { |
| 102 | + return ClientHelloParseState.NotTlsClientHello; |
| 103 | + } |
| 104 | + |
| 105 | + // Protocol version |
| 106 | + if (!reader.TryReadBigEndian(out short version) || !IsValidProtocolVersion(version)) |
| 107 | + { |
| 108 | + return ClientHelloParseState.NotTlsClientHello; |
| 109 | + } |
| 110 | + |
| 111 | + // Record length |
| 112 | + if (!reader.TryReadBigEndian(out short recordLength)) |
| 113 | + { |
| 114 | + return ClientHelloParseState.NotTlsClientHello; |
| 115 | + } |
| 116 | + |
| 117 | + // byte 6: handshake message type (must be 0x01 for ClientHello) |
| 118 | + if (!reader.TryRead(out byte handshakeType) || handshakeType != 0x01) |
| 119 | + { |
| 120 | + return ClientHelloParseState.NotTlsClientHello; |
| 121 | + } |
| 122 | + |
| 123 | + // 5 bytes are |
| 124 | + // 1) Handshake (1 byte) |
| 125 | + // 2) Protocol version (2 bytes) |
| 126 | + // 3) Record length (2 bytes) |
| 127 | + if (buffer.Length < 5 + recordLength) |
| 128 | + { |
| 129 | + return ClientHelloParseState.NotEnoughData; |
| 130 | + } |
| 131 | + |
| 132 | + clientHelloBytes = buffer.Slice(0, 5 + recordLength); |
| 133 | + return ClientHelloParseState.ValidTlsClientHello; |
| 134 | + } |
| 135 | + |
| 136 | + private static bool IsValidProtocolVersion(short version) |
| 137 | + => version is 0x0300 // SSL 3.0 (0x0300) |
| 138 | + or 0x0301 // TLS 1.0 (0x0301) |
| 139 | + or 0x0302 // TLS 1.1 (0x0302) |
| 140 | + or 0x0303 // TLS 1.2 (0x0303) |
| 141 | + or 0x0304; // TLS 1.3 (0x0304) |
| 142 | + |
| 143 | + private enum ClientHelloParseState : byte |
| 144 | + { |
| 145 | + NotEnoughData, |
| 146 | + NotTlsClientHello, |
| 147 | + ValidTlsClientHello |
| 148 | + } |
| 149 | +} |
0 commit comments