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

mosquitto allows cafile but doesn't go into TLS mode, creating protocol errors. #3130

Open
gbronner opened this issue Oct 8, 2024 · 2 comments

Comments

@gbronner
Copy link

gbronner commented Oct 8, 2024

I was getting a lot of Error: Protocol error issues when attempting to use SSL. I believe that the source of the issue is that mosquitto_pub assumes TLS/SSL when you use the --cafile argument, while mosquitto never calls net__tls_server_ctx(listener) unless the certfile and keyfile arguments are passed.

Details: Version 2.0.18

Mosquitto is started with

allow_anonymous true
cafile <path>/root2.crt
connection_messages true
listener 8883 localhost
log_dest stderr
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S
log_type all
socket_domain ipv4
tls_version tlsv1.2

It will start correctly. However, the function net__tls_server_ctx never gets called, so the ssl_ctx in the listener never gets initialized.

mosquitto_pub -p 8883 --id "asdf" -m "blah" -d -t "asdf" --cafile <path>/root2.crt -h localhost -V mqttv5

It will generate a connect packet with the correct CONNECT_CMD, and then a 2 byte value of '4' for the protocol length. I believe that it will then SSL encode this.

However, on receipt, mosquitto will attempt to decode the connection message without using SSL The test
if(slen != 4 /* MQTT */ && slen != 6 /* MQIsdp */){ will fail (I got a value of 256 or 257 in my tests) and the rest of the packet looks like garbage.

It seems like this configuration should be explicitly banned or handled correctly, as the current situation creates a lot of confusion.

Note that this is related to a prior bug that was marked as done, but not actually done.

@gbronner
Copy link
Author

gbronner commented Oct 8, 2024

Prior bug was 1836

@ralight
Copy link
Contributor

ralight commented Oct 8, 2024

@gbronner As it mentions on that other issue, you need certfile and keyfile to enable TLS mode for a listener:

listener 8883
certfile <server.pem>
keyfile <server.key>

On the broker side, the cafile option is only needed if client certificates are in use. On the client side, cafile is often needed to verify the server certificate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants