IMD SolutionComputing LibraryShop main store

Troubleshooting

mosquitto_sub TLS error when no cafile or capath is set

Edge computing and IIoT Published 8 min read By IMD Solution engineering team

Short answer

The error is raised by mosquitto_sub itself: the receiving client was started with no cafile, capath or psk, so it has nothing to validate the broker certificate with. The publishing controller is not the fault. Give the subscriber the Amazon root CA for AWS IoT Core on port 8883, plus its own client certificate and key, and verify the chain with openssl before blaming either side.

On this page
  1. Why does mosquitto_sub report MOSQ_OPT_SSL_CTX_WITH_DEFAULTS used without specifying cafile, capath or psk?
  2. Does that error mean the PLC failed to provide a CA file?
  3. Is the default /etc/ssl/certs/ca-certificates.crt bundle enough?
  4. Which files does mosquitto_sub need against AWS IoT Core on port 8883?
  5. How do you check the certificate chain before changing anything?
  6. What command should a working subscriber use?
  7. When is the same error the publisher's fault?
  8. What is the fix order from here?
  9. Questions asked

The MOSQ_OPT_SSL_CTX_WITH_DEFAULTS message comes from mosquitto_sub on the receiving machine, not from the broker and not from the controller that publishes: the client was started with no cafile, no capath and no psk, so it holds no trust material with which to check the broker's certificate. It does not mean the controller failed to provide a CA file, because CA material never travels with MQTT messages; in the reported session the controller already connects and publishes over TLS on port 8883, which proves its side of the handshake. The fix belongs on the subscriber: give mosquitto_sub the root CA that signed the broker's certificate chain and, when the broker authenticates clients by certificate, the subscriber's own certificate and private key. The worked commands below use the AWS IoT Core broker from that session, but the client-side logic is identical against any MQTT broker.

Why does mosquitto_sub report MOSQ_OPT_SSL_CTX_WITH_DEFAULTS used without specifying cafile, capath or psk? #

The message comes from the receiving client, not from the broker or the controller: mosquitto_sub was asked to build a TLS session but was given none of the three inputs that carry trust material — a cafile, a capath or a psk.

The literal string, as documented in the mosquitto issue tracker, is MOSQ_OPT_SSL_CTX_WITH_DEFAULTS used without specifying cafile, capath or psk., and the client library emits it whenever a TLS context is requested with none of the three trust inputs set. In client terms:

  • --cafile points at one PEM file holding the root CA certificate or certificates the client should trust.
  • --capath points at a directory of CA certificates stored under OpenSSL hashed filenames.
  • --psk selects a pre-shared key for cipher suites that skip certificates entirely.

A CA file is local trust material. During the handshake the broker presents its certificate chain, and the client checks that chain against its own anchors; nothing crosses the connection that could substitute for a locally configured anchor. The same message is reported by people testing against Azure IoT Hub, which is a useful hint that the fault belongs to the client configuration rather than to one broker. And when a test targets a port that serves plain MQTT, the documented exit is --no-tls, not a trust file — decide first whether that port needs TLS at all.

Does that error mean the PLC failed to provide a CA file? #

No — in this session the controller already completes its TLS handshake and publishes, which proves its side of the trust setup works, and the error text names only the client that was started without cafile, capath or psk.

Trust is checked independently at each end of the connection. The subscriber never receives the publisher's CA, and the publisher cannot configure the subscriber on its behalf, so a healthy publishing side neither causes nor cures this error. Verify two separate results and keep them separate in your notes: the controller completes TLS negotiation on its connection and delivers messages, and mosquitto_sub completes TLS negotiation on its own connection and prints them.

The split matters because a sibling fault looks similar and is fixed at the other end: a device that connected for months and stops after a power cut has usually lost its own trust file at boot, which is the situation covered in MQTT connect fails after power loss when the CA certificate is missing.

Is the default /etc/ssl/certs/ca-certificates.crt bundle enough? #

It is enough only when that bundle holds the root CA that signed the broker's certificate chain, so for an AWS IoT Core endpoint the documented route is to download the Amazon root CA and pass it explicitly with --cafile.

AWS documents downloading the root CA that signs its server certificates and storing it in a file named AmazonRootCA1.pem; for an AWS IoT Core endpoint, treat that file as the default --cafile value and fall back to the system bundle only if the check in the next section returns 0 against it. A Linux bundle such as /etc/ssl/certs/ca-certificates.crt carries a large set of public roots, so it may well pass — but whether it holds this particular root is a property of the distribution build, not a given, and the openssl check settles it in seconds.

The same requirement exists on the other side of the connection: the broker configuration documentation states that a cafile or capath is required to enable TLS on a listener. Configuring that listener is a separate job with separate errors, covered in fixing a Mosquitto MQTT TLS listener that returns an unknown error.

Which files does mosquitto_sub need against AWS IoT Core on port 8883? #

Three: the Amazon root CA as --cafile, plus the subscriber's own certificate as --cert and its private key as --key, because AWS IoT Core authenticates clients on port 8883 with a certificate and private key, not with a password.

That trio mirrors what the publishing controller already carries. AWS documents that MQTT over TLS on 8883 requires a valid certificate and private key for authentication, so on a standard AWS IoT Core endpoint the certificate inputs are not optional for any client, subscriber included. Among the three trust inputs from the first section, cafile (or capath) is therefore the right choice and psk is the wrong one: a pre-shared key only fits brokers explicitly configured for PSK cipher suites.

Provision a dedicated client certificate for the subscribing machine rather than reusing the controller's certificate and key — both ends present certificates on this broker, and each identity should be its own, independently revocable, object. The identical rule set governs a PC-based SCADA client against the same cloud broker, as laid out in why a PC SCADA MQTT client fails against AWS IoT Core.

How do you check the certificate chain before changing anything? #

Run openssl s_client against the broker endpoint with the candidate trust file and read the verify return code: 0 means the file contains the needed root, and 20 means the chain's issuer is missing from that file.

Run the check on the machine that runs mosquitto_sub, because that machine's trust material is what you are testing. The endpoint below is an example; substitute the one your broker's console shows:

openssl s_client -connect a1b2c3d4e5f6-ats.iot.us-west-2.amazonaws.com:8883 -CAfile AmazonRootCA1.pem

Read two things from the output. The depth lines count from zero: depth 0 the server certificate, depth 1 its issuer, depth 2 the root, so 2 + 1 = 3 certificates ride in the chain. The decisive last line should read:

Verify return code: 0 (ok)

Repeat the command with the system bundle in place of the downloaded root:

openssl s_client -connect a1b2c3d4e5f6-ats.iot.us-west-2.amazonaws.com:8883 -CAfile /etc/ssl/certs/ca-certificates.crt

Return code 0 on both runs means the default bundle suffices and no extra file is needed. Verify return code: 20 (unable to get local issuer certificate) on the bundle run means the bundle lacks the root this chain needs, and the downloaded Amazon root CA file is the value to pass. That is the whole decision: one number selects the file, and the same number both times means either file works.

What command should a working subscriber use? #

Start mosquitto_sub with the broker endpoint as -h, 8883 as -p, the Amazon root CA as --cafile, and the client certificate and key as --cert and --key, then leave it running while the controller publishes.

mosquitto_sub -h a1b2c3d4e5f6-ats.iot.us-west-2.amazonaws.com -p 8883 \
  --cafile AmazonRootCA1.pem \
  --cert subscriber-cert.pem \
  --key subscriber-key.pem \
  -t plant/line1/temperature -v

The endpoint, the topic and the two filenames are examples; use the endpoint string your console shows, the topic the controller publishes to, and the certificate pair you provisioned for this machine. Success is two results at once: no trust error at startup, and a payload line such as plant/line1/temperature 23.4 printed at each publish. If the trust error reappears with these options, suspect the cafile path first — a relative path resolves against the directory the command runs from, and an empty or truncated file fails exactly like a missing one.

When is the same error the publisher's fault? #

Blame the controller only when the controller itself cannot connect: then the missing trust material is on the publishing device, the fix is its CA file, client certificate and key, and the subscriber needs nothing.

Split the decision by symptom rather than by which process printed first. A subscriber that reports the MOSQ_OPT_SSL_CTX_WITH_DEFAULTS message while the publisher delivers messages is a subscriber problem, full stop. A controller that fails during its own TLS handshake — its logs show certificate or handshake errors — has the gap, and the work happens in its certificate store at the path its web management interface exposes; move files by the transfer method the controller's documentation describes, and match the replacement bundle's contents to the broker's chain with the openssl check above. If both ends negotiate TLS yet no payloads arrive, neither trust store is the suspect: topic name, broker authorization or access rules are, and that is a configuration audit rather than a certificate fix.

What is the fix order from here? #

Fix the subscriber first, the controller second: give mosquitto_sub one trust input — cafile or capath, never psk on this broker — then its own certificate and key, and only revisit the controller if the publisher itself stops connecting.

  1. Confirm the controller still connects and publishes; if it does, leave its configuration untouched.
  2. On the subscriber machine, run the openssl check with the downloaded Amazon root CA file.
  3. Pass that file as --cafile — or pass the system bundle instead if the check against it returned 0.
  4. Add --cert and --key with the certificate pair provisioned for the subscribing machine.
  5. Restart the subscriber and confirm the two success signs: no trust error, payloads printing at the publish rate.

One line not to cross: do not reach for an option that skips server-certificate verification to make the message disappear — the check it disables is the one that proves the connection landed on the real broker. The end state, case by case: subscriber errors while the publisher delivers — fix the subscriber's trust inputs; the publisher cannot complete its handshake — fix the controller's certificate store; the bundle verifies at return code 0 — no extra CA file is needed at all.

Questions asked

Where does capath point, and how must its files be named?

capath names a directory of CA certificates rather than one file. The files inside must carry OpenSSL hashed filenames, so after copying a new CA into the directory run the c_rehash utility against it; without that step the client cannot match the broker's certificate to any certificate there and reports the same trust failure.

Does mosquitto_pub hit the same error?

Yes. mosquitto_pub and mosquitto_sub share the same TLS options, so a publishing test client started without cafile, capath or psk prints the identical message. The remedy is identical as well: hand that client the root CA that signed the broker's certificate, and its own certificate and key when the broker requires mutual TLS.

What permissions should the certificate files have?

The CA file and the client certificate can be world readable, typically mode 644. The private key must be readable only by the account that runs the client: set mode 600 and ownership to that account. On a shared edge PC, a readable key lets anyone on the machine impersonate the device against the broker.

The controller stopped connecting after a power cut — is that the same fault?

No. A client that worked before a power cut and fails after one usually lost its trust or identity files at boot, meaning a corrupted or missing CA file on that device. That is a different failure from a first-time configuration error, and it is fixed on the device that lost the file, not on the receiving client.

Can the error mean TLS was never wanted at all?

It can. If you pointed the client at a port that serves plain MQTT and passed no TLS options, recent client versions can still raise this message while building the security context; the documented exit for that case is the client's no-TLS option on the command line. Decide first whether the port needs TLS; if it does, supply the CA.

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

  • Troubleshooting

    Why the Flexy AWS IoT script fails at MQTT CONNECT with error 28

    Error 28 at MQTT CONNECT does not mean the WAN is down: the script prints its WAN message for any LSTERR value 28. An SSL/TLS configuration event in the event log puts the fault at the TLS hop, so check the CA, certificate and key files, then port 8883 and the Flexy clock, before touching the AWS device policy.

    Edge computing and IIoT

  • Troubleshooting

    Flexy AWS IoT MQTT: fix the CA file and the event callback

    A Flexy running Java ETK 1.4.4 fails its MQTT connection to AWS IoT for two separate reasons. The cafile option names /usr/root-CA.crt while the stored file is CA.crt, so rename the file to match. The old callMqttEvent callback calls readMessage() on event 1, a status event, which makes the gateway unreachable after the JVM starts; handle the status event first and read only on the other events.

    Edge computing and IIoT

Where point history goes when an edge historian is set to MySQL