dotnet / dotnet/MQTTnet

Server error when using TLS 1.2: CONNECT packet must have at least 7 bytes

Open
#1,897 6 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

When connecting to the MQTT Server with TLS, the following error is thrown:

```
[20:57:49 Debug Apm-Trace: Apm-Transaction:] Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
Connection id "0HMVPO9R8V5EA" communication error.
MQTTnet.Exceptions.MqttProtocolViolationException: **CONNECT packet must have at least 7 bytes.**
at MQTTnet.Formatter.MqttPacketFormatterAdapter.ParseProtocolVersion(ReceivedMqttPacket receivedMqttPacket)
at MQTTnet.Formatter.MqttPacketFormatterAdapter.DetectProtocolVersion(ReceivedMqttPacket receivedMqttPacket)
at MQTTnet.AspNetCore.ReaderExtensions.TryDecode(MqttPacketFormatterAdapter formatter, ReadOnlySequence`1& input, MqttPacket& packet, SequencePosition& consumed, SequencePosition& observed, Int32& bytesRead)
at MQTTnet.AspNetCore.MqttConnectionContext.ReceivePacketAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoSend()
at MQTTnet.Server.MqttClientSessionsManager.ReceiveConnectPacket(IMqttChannelAdapter channelAdapter, CancellationToken cancellationToken)
at MQTTnet.Server.MqttClientSessionsManager.HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter, CancellationToken cancellationToken)

[20:57:49 Debug Apm-Trace: Apm-Transaction:] Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
Connection id "0HMVPO9R8V5EA" sending RST because: "CONNECT packet must have at least 7 bytes."
```

### Which component is your bug related to?
- .net8.0
- Client - MQTTnet 4.3.3.952
- Server - MQTTnet 4.3.3.952 and MQTTnet.AspNetCore 4.3.3.952
Note: this works with a server that is version 3.x

### Client Code example
```
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var certificate = new X509Certificate2(Path.Combine(currentPath, "certificate.pfx"), "password-redacted", X509KeyStorageFlags.Exportable);

// Create a new MQTT client.
var factory = new MqttFactory();
mqttClient = factory.CreateMqttClient();

var options = new MqttClientOptionsBuilder()
.WithClientId(clientId)
.WithTcpServer("localhost", 8883)
.WithTlsOptions(o =>
{
o.UseTls(true);
o.WithClientCertificates(new List { certificate });
o.WithCertificateValidationHandler(_ => true);
o.WithAllowUntrustedCertificates(true);
o.WithIgnoreCertificateChainErrors(true);
o.WithIgnoreCertificateRevocationErrors(true);

// the default value is determined by the os. set manually to force version.
o.WithSslProtocols(SslProtocols.Tls12);
})
.WithCredentials(username, password)
.WithCleanSession()
.Build();

await mqttClient.ConnectAsync(options, CancellationToken.None); //server crashes when the client reaches this line
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.