IMD SolutionComputing LibraryShop main store

Troubleshooting

MQTT TLS connects but no CONNACK: find the failing hop

Edge computing and IIoT Published 8 min read

Short answer

The connect callback fires only when the broker returns a CONNACK, so with TCP ESTABLISHED and no callback the failure is in the TLS handshake or between TLS and MQTT. Run openssl s_client against the listener, then mosquitto_sub -d with the same CA file and credentials; wrong credentials are not the cause, because the broker answers those with a CONNACK that fires the callback with a return code.

On this page
  1. What the client log actually proves
  2. Six hops, one observable each
  3. Test one: openssl s_client against the listener
  4. Test two: mosquitto_sub -d outside the application
  5. Test three: a broker you control
  6. Working example: a spBv1.0/# subscription that stalls after sending CONNECT
  7. Checking the client's TLS calls
  8. The one request that gets broker logs
  9. Where credentials and ACLs actually fail
  10. Questions asked

An ESTABLISHED line in netstat and a client log that ends with sending CONNECT prove exactly two things: the TCP three-way handshake completed, and the client queued its packets. Neither line came from the broker. In libmosquitto the connect callback runs only when the broker sends a CONNACK, so a connection that stalls silently with TCP up has failed in the TLS handshake or between TLS and the MQTT layer, and wrong credentials are not the cause, because the broker answers those with a CONNACK carrying return code 4 or 5, which fires the callback with that code. Treat the path as six hops, prove each one with an observable you can capture without the server team, and stop at the first hop that has no proof.

What the client log actually proves #

The lines Client ... sending CONNECT and sending SUBSCRIBE (Mid: 1, Topic: spBv1.0/#, QoS: 0) record packets the client handed to its own stack. The number after Mid: is the message identifier the client assigned to its own SUBSCRIBE packet; no byte of either line originated at the broker. libmosquitto dispatches the connect callback when the broker sends a CONNACK in response to the connection, and it dispatches every callback from the network loop, started with mosquitto_loop_start() or driven by mosquitto_loop_forever().

Rule out two client-side causes first, because each reproduces the same silence and costs nothing to test:

  • The loop is not running. A program that calls the connect function and then waits without the network loop never dispatches any callback, whatever the broker did.
  • TLS is pointed at a plaintext listener. A TLS client on a plaintext port, or a plaintext client on a TLS port, sends bytes the peer never answers, and the client waits for a CONNACK without printing an error. The mosquitto command-line tools behave the same way when TLS is misconfigured: they sit quietly instead of failing loudly.

Six hops, one observable each #

Split the path and demand one observable per hop. The first hop without proof is where the fault lives.

Hop What happens What proves it passed What failure looks like
1. Name and route The client resolves the host and routes to the listener The name resolves and the first packet leaves Resolve error, no route
2. TCP to the listener port SYN, SYN-ACK, ACK through every firewall and NAT ESTABLISHED in netstat, already captured Refused, timed out
3. TLS handshake ClientHello, ServerHello, certificate chain, verification, optional client certificate openssl s_client prints the chain and Verify return code: 0 (ok) Silent hang, TLS alert, nonzero verify code
4. MQTT CONNECT to CONNACK The broker checks the username and password A received CONNACK line and the callback with return code 0 No CONNACK, or a nonzero return code
5. SUBSCRIBE to SUBACK The broker applies the topic ACL SUBACK with a granted QoS SUBACK failure code, or a grant with no data
6. Topic and payload The Sparkplug namespace and payload reach the subscriber Messages arrive under spBv1.0/ Wrong group or node id, undecoded binary

This symptom, TCP up and the callback silent, puts the fault at hop 3 or 4. If the same silence appears with TLS out of the path entirely, start with diagnosing the MQTT connect handshake when TCP already works.

Test one: openssl s_client against the listener #

This needs nothing from the server team and proves or breaks hop 3. Use the same CA file the application loads:

openssl s_client -connect <host>:<port> -servername <host> -showcerts -status -CAfile /etc/mosquitto/ca_certificates/<ca>.pem -verify_return_error

Read four things in the output: the chain order and the issuer of the top certificate; the Verify return code: line; the negotiated protocol and cipher; and whether the listener lists acceptable client certificate CA names, which means it requests a client certificate. Then map what you saw:

Observation Meaning Action
Nothing prints, or the output hangs before a certificate appears The port is not a TLS listener, or a filter drops the ClientHello Confirm the TLS port number against the plaintext port
Verify return code: 20 or 21 The CA file lacks the issuing intermediate or root Concatenate the full chain into the PEM
Verify return code: 10 The certificate is expired, or the client clock is wrong Check the validity dates and the system time
Verify return code: 62 Hostname mismatch: connected by IP or a name absent from the certificate Connect with a name the certificate covers
Clean handshake, client certificate CA names listed The listener requests mutual TLS Obtain a client certificate and key and load them in the client
Verify return code: 0 (ok) Hop 3 is proven Move to test two

Test two: mosquitto_sub -d outside the application #

Run the broker's own client with the application's exact inputs, same host, port, CA file, username and password:

mosquitto_sub -d -h <host> -p <port> --cafile /etc/mosquitto/ca_certificates/<ca>.pem -u <user> -P <pwd> -t 'spBv1.0/#' -v

The -d output prints sending CONNECT, then either a received CONNACK line with its return code, or nothing more. This removes your application code from the path while changing nothing else, which is the whole value of the test:

  • A received CONNACK with return code 0 proves hop 4 and moves the fault to hop 5 or 6, the ACL or the topic namespace, not the connection.
  • A stop after sending CONNECT fails hop 4 with the application excluded, which puts the fault on the server side of the MQTT layer.

Test three: a broker you control #

Stand up a TLS broker of your own and point the application at it: a pass there shifts suspicion to the remote policy, a fail points at your code. Running a secure Mosquitto lab broker with Node-RED is the fastest way to get a known-good listener on an edge PC. A public encrypted listener such as test.mosquitto.org on port 8883 checks the client stack the same way, though it proves nothing about the target's policy.

Working example: a spBv1.0/# subscription that stalls after sending CONNECT #

Inputs:

  • The application log ends with sending CONNECT, then sending SUBSCRIBE (Mid: 1, Topic: spBv1.0/#, QoS: 0), from the client's own debug output.
  • netstat, captured on the server side, shows the connection ESTABLISHED.
  • openssl s_client with the application's CA file completes and prints Verify return code: 0 (ok).
  • mosquitto_sub -d with the same host, port, CA file and account prints sending CONNECT and nothing after.

Rule: each of the six hops is closed by exactly one observable, and the fault sits at the first hop after the last proven hop that has no passing observable.

Arithmetic: the path has 6 hops. The ESTABLISHED entry closes hops 1 and 2, so 6 - 2 = 4 hops remain open. The verify code 0 closes hop 3, so 4 - 1 = 3 hops remain open: hops 4, 5 and 6. The mosquitto_sub result fails hop 4, and because a client cannot subscribe before its CONNECT is accepted, hops 5 and 6 are unreachable until hop 4 is fixed: 3 - 1 = 2 hops no test can reach yet.

Result: hop 4 fails with the application excluded. TLS terminates cleanly but no CONNACK returns, which points at a TLS-terminating proxy in front of the broker that does not forward the MQTT stream, or a broker that accepts the socket and never answers. If the s_client run also listed acceptable client certificate CA names, the cause is already named: the listener wants mutual TLS and the application presents no certificate, so the fix is a client certificate and key, not debugging.

Decision: work hop 4 and nothing else. Send the administrator the one request below, and spend no further time on usernames, passwords or topic ACLs; those act on the CONNACK or the SUBACK, and both would have produced a callback, not silence.

Checking the client's TLS calls #

mosquitto_tls_set takes five inputs after the handle: cafile, capath, certfile, keyfile and the key-password callback. Passing NULL for the certificate and key is correct for server-authenticated TLS only; it cannot satisfy a listener that demands a client certificate.

struct mosquitto *m_client = NULL;                         /* handle created earlier by the application */
const char *usr = "edge-gateway";                           /* illustrative account name */
const char *pwd = "illustrative-never-a-real-secret";       /* illustrative */
const char *ca  = "/etc/mosquitto/ca_certificates/ca.pem"; /* illustrative CA path */

mosquitto_username_pw_set(m_client, usr, pwd);
mosquitto_tls_set(m_client, ca, NULL, NULL, NULL, NULL);   /* cafile, capath, certfile, keyfile, pw_callback */
mosquitto_connect_callback_set(m_client, on_connect);      /* illustrative callback names */
mosquitto_log_callback_set(m_client, on_log);
mosquitto_loop_start(m_client);

The test that proves this block: run mosquitto_sub -d with the same four strings. If it prints received CONNACK, the file and the account are good and any remaining difference is in the code. Then check four things in the application:

  • mosquitto_username_pw_set and mosquitto_tls_set are called before the connect call, and every return value is checked.
  • The PEM is readable by the account that runs the application; files under /etc/mosquitto/ca_certificates/ are often owned by the broker's service account.
  • The log callback is registered before connecting, because it surfaces TLS errors the application otherwise discards.
  • The network loop is started before anything waits on a callback.

One safety note: mosquitto_tls_insecure_set(m_client, true) is a diagnostic, not a setting. It disables hostname verification so you can tell a verify code 62 name mismatch from a chain failure; remove it before the link carries plant data. When you later add a certificate and key and the key will not load, the failure pattern is covered in fixing an MQTT TLS private key load failure on an edge gateway.

The one request that gets broker logs #

Administrators respond to precise requests, not to a request to check the logs. Send the client id exactly as it appears next to sending CONNECT, the timestamp window of your last test, and ask for three things: any TLS alert lines for that client, any CONNACK decision for that client, and the name of the product that terminates TLS on that port. An alert line repeats what s_client already told you, a missing CONNACK decision confirms the CONNECT never reached the broker, and a TLS terminator in front of the broker explains a clean handshake with no MQTT answer.

Where credentials and ACLs actually fail #

Both announce themselves after the connection exists. The broker rejects a bad username or password with a CONNACK return code 4, and an unauthorized client with return code 5; the callback fires with that code either way. A topic ACL acts one hop later: the SUBSCRIBE is answered by a SUBACK with a failure code, or granted silently with no data ever arriving. Neither pattern is a silent stall, which is why the six-hop walk starts at TLS and not at the password. Once a CONNACK does arrive and data still fails to appear, the remaining suspect is hop 6: the Sparkplug group and node ids in the topic namespace, which must match what the publishing side uses.

Questions asked

Does an ESTABLISHED socket mean the broker accepted my MQTT client?

No. ESTABLISHED proves the TCP three-way handshake completed through every firewall and NAT in the path. TLS, the MQTT CONNECT and the CONNACK all travel over that socket afterwards, so the state says nothing about them. An accepted client appears in the broker's own application log, not in netstat.

Why would wrong credentials still fire the connect callback?

Because the broker answers a rejected login with a CONNACK: return code 4 for a bad username or password, return code 5 for a client that is not authorized. The callback runs with that code, then the broker closes the socket. You see a rejection, never silence; silence means no CONNACK was sent at all.

What does Mid: 1 mean in the sending SUBSCRIBE log line?

It is the message identifier the client assigned to its own SUBSCRIBE packet, numbered in the order the client queues its commands, with 1 as the first. The line records what the client sent, not what the broker granted. The matching proof is a SUBACK carrying the granted QoS, which can only exist after a CONNACK.

Can a public broker prove my client is set up correctly?

It proves the client stack, nothing more. Point mosquitto_sub -d at the encrypted listener at test.mosquitto.org, port 8883: a received CONNACK shows your tools and certificate handling work against a known TLS setup, including variants that request client certificates. It says nothing about the target broker's policy, ACLs or listeners.

mosquitto_sub connects but my application still stalls. What does that change?

It removes the server and the inputs from the suspect list: the same host, port, CA file and account worked outside your code. Compare the order of calls, with username and TLS set before connect, callbacks registered before connecting, and the network loop started before anything waits, because the difference is now in the application, not the network.

Keep reading

  • Troubleshooting

    Fixing an edge gateway MQTT TLS private key load failure

    The errors error:0906D06C PEM routines:PEM_read_bio:no start line and error:140B0009 SSL routines:SSL_CTX_use_PrivateKey_file mean the gateway cannot parse its own private-key file, so the failure is local and happens before the broker sees anything. On a Flexy 205 running firmware 15.0s1 the confirmed correction is the 15.0s2 firmware upgrade with the same certificate files, not new keys or AWS IoT policy edits.

    Edge computing and IIoT

  • Explainer

    Securing ctrlX MQTT data paths to AWS or GCP

    Yes — Node-RED plus Mosquitto can carry ctrlX Data Layer values under X.509 authentication and TLS 1.2, but each of the three connections (publisher to broker, device to broker, uploader to cloud) needs its own trust and client-certificate configuration. DeviceBridge joins only when the chosen AWS or GCP service's protocol calls for it.

    Edge computing and IIoT

  • How-to

    Configuring MQTT TLS certificates in Node-RED on an edge PC

    Configure the MQTT node's own TLS inputs: the broker's CA chain always, the client certificate and private key when the broker requires mutual TLS. A platform certificate store is invisible to Node-RED until the installed integration exposes it, so use persistent files unless store support is verified, and renew in a staged 30-day window.

    Edge computing and IIoT