Security Concerns
See original GitHub issueHello and thanks for all the great work! I am trying to get to grips with creating an encrypted MQTTnet server and there is barely anything in the GitHub wiki documentation on the proper way to do so. Nevertheless, I’ve scraped the issues section and managed to cobble together the following:
_MQTTCertificate =
new X509Certificate2(path, password, X509KeyStorageFlags.Exportable);
var optionsBuilder = new MqttServerOptionsBuilder()
.WithEncryptedEndpoint()
.WithEncryptionCertificate(_MQTTCertificate.Export(X509ContentType.Pfx))
.WithSubscriptionInterceptor(SubscriptionInterceptor);
await _MQTTServer.StartAsync(optionsBuilder.Build());
which allows the MQTT server to start - so far, so well.
If a connection is then made using mosquitto_sub client without specifying any certificate, CA nor key:
mosquitto_sub -h MQTTNet.tld -p 8883 -t 'sometopic' -v
then MQTTnet will gladly accept the unencrypted connection and not even warn that the connection is not encrypted.
This is quite the security hazard given that all data flowing between the subscriber and the broker is very much plaintext and, in some cases, may contain sensitive information that can be sniffed right off the network - both the subscription topic and payload data are exposed.
Am I using MQTTnet correctly? If so, then why is MQTTnet accepting an unencrypted connection when it has been explicitly told not to do so? Is there some way to prevent this behaviour?
I can work around the issue by creating an SSL encrypted stream on top of all communication but shouldn’t encryption work with MQTTnet without having to resort to SslStream?
Furthermore, connecting with mosquitto_sub and specifying a CA, client certificate and a certificate key simply leads to mosquitto_sub bailing out with:
Error: A TLS error occurred.
I understand that the latter is a mosquitto_sub issue but I am unsure how to debug given that the connection is handled by MQTTnet internally. MQTTnet does not throw any elucidating errors when mosquitto_sub fails to connect.
Finally, I do not see a way to set the desired protocol version with MQTTnet - does it default to Tls1.2? mosquitto_sub is capable of Tls1, Tls1.1 and Tls1.2 - which one is then implied by MQTTnet if there is no way of selecting?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5

Top Related StackOverflow Question
I’ll just post the rest here in case other people have the same issue with TLS and MQTTnet - perhaps someone will add the details to the wiki for others.
Generate the MQTTnet server certificates using OwnTracks:
You may need to comment out
subjectAltNameon certain OpenSSL versions:Run:
where
SERVER_HOSTNAMEMUST be the hostname of the server where the application using MQTTnet runs on.This will generate
ca.crt,ca.key,SERVER_HOSTNAME.crtandSERVER_HOSTNAME.key. Now convert the certificates to PFX with OpenSSL:It will prompt for a password, so be creative - let’s say it’s going to be
somepassword.Now transfer
server.pfxto the application using MQTTnet and initialise the MQTTnet server as follows:This will force MQTTnet to refuse insecure connections.
Now, for the client, generate client certificates [1]:
where, again,
CLIENT_HOSTNAMEMUST be the hostname of the machine where your MQTT client resides.Finally, you can test with
mosquitto_sub:where
SERVER_HOSTNAMEis the hostname of the application running MQTTnet,PORTis the port you chose to initialise MQTTnet andca.crtwas generated in the first steps.[1] http://rockingdlabs.dunmire.org/exercises-experiments/ssl-client-certs-to-secure-mqtt
I’m closing this now. Feel free to open this if you still see any problems.