Troubleshooting
MQTT connection rejected after a successful TLS handshake
Short answer
A completed TLS handshake and an accepted MQTT CONNECT are separate stages. Return code 5 in MQTT 3.1.1, or reason code 135 in MQTT 5.0, after a clean handshake means the broker requires username and password credentials the client never sent. Authorize the client by its certificate on the broker, or run a client mode that carries credentials.

On this page
The broker is not rejecting your certificates. A completed TLS handshake proves that the Root CA, client certificate and private key are correct and that the encrypted transport session is up; authorization happens afterward, when the client sends its MQTT CONNECT. A broker whose policy requires a username and password answers that CONNECT with CONNACK return code 5, Connection Refused, not authorized, in MQTT 3.1.1, or reason code 135 in MQTT 5.0. No certificate, key or port change fixes that, because the certificates already did their job. The fix is in the authentication policy: authorize the client by its certificate on the broker, or run a client mode that carries the credentials. This article shows how to tell the two stages apart, what each rejection code means, and the decision for each case.
Two stages, two different failures #
Treat the connection as two checkpoints. Each one proves something different, and each one fails for different reasons.
| Checkpoint | What passing proves | What passing does not prove |
|---|---|---|
| TLS handshake on the broker port | Server certificate chains to your Root CA, the client certificate and key were accepted, a cipher was agreed | The broker will accept the MQTT CONNECT |
| MQTT CONNECT delivered | Protocol version and client identifier reached the broker | The credentials the broker policy demands were supplied and are valid |
| CONNACK return code 0 | The session is accepted and you can subscribe or publish | Nothing further; this is the finish line |
A rejection after a clean handshake is therefore an authentication-policy mismatch, not a transport fault. The device proved its identity with its certificate; the broker then asked for a second identity, a username and password, that the client never offered.
What the rejection codes tell you #
The MQTT 3.1.1 specification, an OASIS Standard, defines the CONNACK return codes for a refused connection: code 4 means the broker received a username and password and rejected them, and code 5 means the broker refused the connection without accepting any credentials, typically because none were supplied. MQTT 5.0 carries the same meaning in reason codes transmitted as one byte: 0x87 is 8 × 16 + 7 = 135, not authorized, and 0x86 is 8 × 16 + 6 = 134, bad user name or password.
| Your code (MQTT 3.1.1) | Reason code (MQTT 5.0) | Meaning | What to fix |
|---|---|---|---|
| 4 | 0x86 (134) | Credentials were sent and are wrong | The password, or the broker's password file and ACL for that user |
| 5 | 0x87 (135) | The broker refused before any credential check | The authentication policy: supply credentials or authorize by certificate |
Code 4 and code 5 lead to different repairs, so read the code before touching anything.
Worked example: clean handshake, then code 5 #
An edge PC publishes to a Mosquitto 2.0 broker at 192.168.1.50 on port 8883. The client certificate, private key and Root CA are configured, but the command sets no username or password:
mosquitto_pub -h 192.168.1.50 -p 8883 \
--cafile /etc/ssl/plant-root-ca.crt \
--cert /etc/ssl/edge-pc-01.crt \
--key /etc/ssl/edge-pc-01.key \
-t plant/line1/temperature -m "23.4"
The broker listener has client certificates required, allow_anonymous set to false and a password file configured. The sequence runs like this: the TCP connection opens, the TLS 1.2 handshake completes and the broker logs the new connection from 192.168.1.50, the client sends its CONNECT carrying no username, and the broker returns CONNACK code 5 and logs the disconnect as not authorised. On a plant LAN the whole sequence finishes in a fraction of a second, in this example about 0.2 s from the first TCP packet to the CONNACK, so the failure looks instantaneous even though every certificate step was correct.
The reading is unambiguous: transport security succeeded, authorization failed because the policy demands credentials the client cannot supply. Re-issuing certificates, changing ciphers or checking the Root CA again would all be work spent on the stage that already passed.
Confirm the stage before you change anything #
Run the diagnosis in this order so you change one thing, once:
- Confirm TCP reachability of the broker port. If TCP itself fails, credentials are irrelevant; work through the handshake diagnosis in MQTT connect fails when TCP works: diagnose the handshake.
- Run the client with TLS logging on and confirm the handshake completes with no alert, so the certificate path is closed as a cause.
- Read the CONNACK code the client reports. Code 4 means fix the credentials; code 5 means fix the policy.
- Read the broker log for the disconnect reason on that client identifier, and match it to the code.
- Apply exactly one change: a broker policy change or a client credential change, then retest and expect code 0.
If the client-side certificates ever do need work, the certificate setup for a Node-RED client on an edge PC is covered in Configuring MQTT TLS certificates in Node-RED on an edge PC.
Fix one: authorize the client by its certificate #
If you control the broker and your security model accepts certificate identity as the client identity, keep the certificates and drop the username requirement for that listener. On Mosquitto, the documented listener option use_identity_as_username takes the common name of the client certificate as the MQTT username and applies your ACL rules to that name:
listener 8883
cafile /etc/mosquitto/certs/plant-root-ca.crt
certfile /etc/mosquitto/certs/broker.crt
keyfile /etc/mosquitto/certs/broker.key
require_certificate true
use_identity_as_username true
allow_anonymous false
A client whose certificate carries the common name edge-pc-01 then connects with no username or password at all, and the ACL entries you wrote for edge-pc-01 govern what it may publish and subscribe. Set allow_anonymous back to false and leave it there: identity remains mandatory, only its source changes. The background on how broker certificates establish client identity is covered in how TLS certificates secure a dynamic MQTT broker.
Never make this rejection disappear by turning anonymous access on for a listener the plant network can reach. That switch removes authentication for every client on the listener, not just the one you are connecting, and it undoes the security the certificates were providing.
Fix two: use a client mode that carries credentials #
If your security policy demands both a client certificate and a username and password, the client must expose fields for both. Some device profiles are built for one specific cloud broker and offer no username or password setting at all; no configuration effort makes such a mode send credentials it has no field for, and that mode is simply incompatible with a broker that mandates them. The decision in that case: move the device to a manufacturer-documented mode or connection method that supports credential fields, or run the connection from a full client on an edge PC that handles certificates and credentials together. Keep the working certificate configuration unchanged while you make the switch, so the transport stage stays proven while you fix the authorization stage.
The decision #
- CONNACK 4, or reason 134: credentials were sent and are wrong. Correct the password and the broker's password file or ACL for that user. Certificates are not involved.
- CONNACK 5 after a clean handshake, and you own the broker: map the client certificate identity to the MQTT username on that listener, keep anonymous access off, and retest for code 0. No certificates need re-issuing.
- CONNACK 5 and the client has no credential fields: that client mode cannot satisfy this broker. Change to a documented mode or client that carries username and password, or change the broker policy to certificate identity if your security model allows it.
In all three cases the boundary is the same: the handshake certifies the transport, the CONNACK certifies the session, and the code in between tells you which one to repair.
Questions asked
Does a successful TLS handshake mean the MQTT connection is authorized?
No. The handshake covers certificates, keys and the encrypted transport session only. The broker evaluates the MQTT CONNECT separately: it checks the client identifier and whatever credentials its policy demands. A session can pass the entire certificate exchange and still be refused at the CONNECT stage, which is why a certificate change never fixes this particular rejection.
How does Mosquitto accept a client certificate instead of a username and password?
On the TLS listener, set require_certificate to true and add use_identity_as_username. Keep allow_anonymous set to false so identity stays mandatory, and give the certificate's common name the same access rights the old username had.
Can I set the broker to allow anonymous connections just for this one device?
No. Anonymous access is a listener-level switch: it removes authentication for every client that reaches that listener, not only the one you are debugging. On a broker reachable from the plant network that is not an acceptable trade. Authorize the device by its client certificate, or by credentials it is documented to send.
The client does send a username and password and the broker still refuses it. What does that mean?
Then the rejection code changes: MQTT 3.1.1 reports return code 4 and MQTT 5.0 reports reason 134, bad user name or password. That code proves the credentials reached the broker and failed the check. Fix the password or the broker's password file and the ACL entries for that user; the certificates are again not the problem.

