Skip to content
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

Error while authenticating. Unexpected Maximum QoS value: 2 #2152

Open
jaimestuardo opened this issue Feb 9, 2025 · 1 comment
Open

Error while authenticating. Unexpected Maximum QoS value: 2 #2152

jaimestuardo opened this issue Feb 9, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@jaimestuardo
Copy link

jaimestuardo commented Feb 9, 2025

Hello.

I have these options to connect to my own broker:

_options = new MqttClientOptionsBuilder()
                    .WithClientId(ClientID)
                    .WithTcpServer(Globals.BROKER_ADDRESS, Globals.BROKER_PORT)
                    .WithCredentials(Globals.BROKER_USER, Globals.BROKER_PASSWORD) 
                    .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500) // Use MQTT version 5.0
                    .WithTlsOptions(o =>
                    {
                        o.UseTls(true);
                        o.WithAllowUntrustedCertificates(false);
                        o.WithIgnoreCertificateChainErrors(false);
                        o.WithIgnoreCertificateRevocationErrors(false);

                        o.WithCertificateValidationHandler(_ => true);

                        Assembly assembly = Assembly.GetExecutingAssembly();

                        byte[]? certPubicKeyData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.client.key")?.CopyTo(mem);

                            certPubicKeyData = mem.ToArray();
                        }

                        byte[]? certClientData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.client.crt")?.CopyTo(mem);

                            certClientData = mem.ToArray();
                        }

                        byte[]? certRootData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.root.crt")?.CopyTo(mem);

                            certRootData = mem.ToArray();
                        }

                        var rootCert = new X509Certificate2(certRootData);
                        var clientCert = new X509Certificate2(certClientData);
                        //var clientKey= new X509Certificate2(certPubicKeyData);
                        o.WithClientCertificates([rootCert, clientCert]);
                        o.WithSslProtocols(System.Security.Authentication.SslProtocols.Tls12);
                    })
                    .WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
                    .WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
                    .WithCleanStart(true)
                    .WithSessionExpiryInterval(Globals.BROKER_SESSION_INTERVAL)
                    .Build();

When I try to connect the Disconnected handler is called with this exception:

**

Error while authenticating. Unexpected Maximum QoS value: 2

**

I tried even with MqttQualityOfServiceLevel.AtLeastOnce but no avail. Always the same error with the same number.

Is there a way to solve this without editing MQTTnet source code??

Digging inside the source code, I have found:

public MqttQualityOfServiceLevel ReadMaximumQoS()
{
	var value = _body.ReadByte();
	if (value > 1)
	{
		throw new MqttProtocolViolationException($"Unexpected Maximum QoS value: {value}");
	}

	return (MqttQualityOfServiceLevel)value;
}

I think this if (value > 1) is wrong. It should be if (value > 2)

Thanks
Jaime

@jaimestuardo jaimestuardo added the bug Something isn't working label Feb 9, 2025
@MattBrittan
Copy link

MattBrittan commented Feb 10, 2025

Responded to OP on stackoverflow (this issue is a copy/paste from there). Server in use is CrystalMQ and this server appears to breach the MQTTV5 spec requirement that Maximum QoS valid values are 0 or 1 only (it sends 2 - verified via wireshark). I believe that MQTTNet is behaving correctly when it rejects this.

Also appears to be a duplicate of #1056.

Update from OP on stackoverflow: "I have contacted CrystalMQ provider, and in fact, they are fixing the issue now."

Further update: the authors of crystalmq have acknowledged the issue and are realising a fix (very quick response!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants