Troubleshooting
Why an MQTT client connects without its TLS client certificate
Short answer
An mqtts branch that assigns Certificates = null enables TLS but presents no client certificate, so AWS IoT's mutual TLS check ends the handshake before CONNECT. Build a certificate object from the thing certificate and its private key, assign it to the TLS options, keep chain validation on, and confirm with a captured CONNECT result.

On this page
The mqtts branch in this MQTT publisher enables TLS but never attaches a client certificate: it assigns an empty certificate list, so the client presents no X.509 identity during the handshake. AWS IoT requires mutual TLS — it requests a client certificate and validates it against its registry — so the server ends the handshake before MQTT CONNECT ever runs. The fix is to build a certificate object from the thing certificate and its private key, assign it to the TLS options' client certificate list, keep chain validation enabled, and prove the result with a captured handshake and a CONNACK.
What the mqtts branch actually sets #
The connect logic builds its TLS options in one place, shown here exactly as the code path defines them:
if (brokerUrl.Scheme == Utils.UriSchemeMqtts)
{
builder = builder.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = null,
SslProtocol = System.Security.Authentication.SslProtocols.None,
AllowUntrustedCertificates = false,
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false,
CertificateValidationHandler = Client_MqttClientCertificateValidation
});
}
Each field does one job:
UseTls = true— TLS is enabled for the mqtts URL, so the client opens a TLS session.Certificates = null— the client certificate list is empty. No identity, no private key, nothing to authenticate the client.SslProtocols.None— the operating system negotiates the TLS protocol version.- The three
falseflags keep untrusted certificates, chain errors and revocation errors rejected. CertificateValidationHandlerroutes server certificate validation intoClient_MqttClientCertificateValidation.
This snippet proves what this one code path does, not that the whole MqttAgent lacks another loading route. Before changing source, check the documented fields of publisher.json for certificate, private-key and CA inputs, and trace where the configuration values land in the options builder. Undocumented field names do not count as a loading path.
Why AWS IoT closes the handshake #
AWS IoT documents TLS client authentication as a requirement: the device presents an X.509 client certificate, and AWS IoT validates the certificate's status and account against its registry. A TLS server that requests a client certificate and receives none sends an alert and tears down the session, so the failure shows up in your client log as a TLS error, never as an MQTT return code.
Worked example — count what the handshake carries:
- Client certificates AWS IoT asks for during TLS client authentication: 1
- Client certificates the
Certificates = nullpath presents: 0 - Missing identity: 1 − 0 = 1
The same count applies to the three files AWS creates with the thing — the certificate, the private key and the Amazon root CA. This code path loads 0 of those 3 files. TLS turns on, but the session carries no client identity, so mutual TLS fails by arithmetic, not by chance.
Load the client certificate and its private key #
The client certificate list must contain a certificate object that carries the private key. A minimal change to the same branch looks like this — the file names and the passphrase argument are examples, and the builder member names follow the MQTTnet version the snippet came from:
var thingCertificate = new X509Certificate2(
"thing-certificate.pem.crt",
"the private key passphrase if the key file is encrypted");
builder = builder.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = new[] { thingCertificate },
SslProtocol = System.Security.Authentication.SslProtocols.None,
AllowUntrustedCertificates = false,
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false,
CertificateValidationHandler = Client_MqttClientCertificateValidation
});
Check two things before you run it. First, the object must actually hold the key: a constructor that accepted the certificate but silently dropped the key gives you a certificate without an identity. If your .NET version will not build the object from a PEM certificate and PEM key directly, combine the two into a PFX file with a documented conversion tool and load that. Second, changing only the Certificates line is enough only because the other fields were already correct — the validation flags stay false.
If the key file exists but the client still reports it cannot load the private key, the cause is usually the file format or an unreadable key, not the TLS options; the article on fixing an edge gateway MQTT TLS private key load failure walks that fault. In MQTTnet version 4 the builder methods are named differently — TLS setup and client certificates moved to separate builder members — so match your patch to the version installed on the target, not to a snippet copied from another major version.
Keep the root CA and validation where they belong #
The Amazon root CA belongs to server certificate validation, not to the client certificate list. With a plain WithTls setup, the operating system and .NET validate the server chain themselves; in this branch, Client_MqttClientCertificateValidation is the hook that decides, so give it the Amazon root CA to trust or let the OS trust store confirm the chain. Loading the root CA into Certificates sends it as a client identity, which authenticates nothing.
Do not set AllowUntrustedCertificates or IgnoreCertificateChainErrors to true to make the connection succeed. AWS IoT serves a valid chain, so those flags would not fix a real fault — they would hide it and let an intercepting endpoint impersonate the broker. If the chain is correct and validation still fails, check the device clock: a wrong time makes a valid certificate look expired, the same combination of clock and certificate trust described in the article on checking device clock and certificate trust when MQTT TLS fails.
Verify with a captured handshake and CONNACK #
Work through the checks in order; each one rules out a different cause:
- Confirm the certificate object holds the private key — a certificate-only object passes construction but fails authentication.
- Capture the TLS handshake. The client's Certificate message must now list one certificate, up from the zero the old path sent; 1 presented against 1 required means the identity question is closed.
- Confirm the handshake completes and the client receives a CONNACK. If TLS finishes but no CONNACK arrives, the fault is one hop further on, and the article on finding the failing hop when MQTT TLS connects but no CONNACK returns takes you through it.
- Confirm in AWS that the certificate is active and registered to the thing, since AWS IoT checks certificate status against its registry during authentication.
- Publish one known message and read it back on the receiving side. A completed handshake plus a confirmed publish is the only full proof of the certificate-loading path.
Decision #
- If
publisher.jsondocuments certificate, key and CA fields and you can trace them into the TLS options builder, configure the connection — no source change is needed. - If no documented fields reach the builder, patch the mqtts branch: build a certificate object that includes the private key and assign it to
Certificates. Changing only that one line is the correct fix here because the validation flags are already right. - If the certificate loads and the handshake still fails on the server chain or on validity dates, the fault is trust or time, not the client identity; fix the trust chain or the clock.
- A release statement that promises AWS verification is a plan, not a shipped feature. Verify the installed build's behavior with a capture before relying on it, and re-run the five checks above after any version change.
Questions asked
Does setting UseTls to true attach the AWS IoT client certificate?
No. UseTls only turns TLS on for an mqtts URL. The client certificate list stays empty until the code or configuration assigns a certificate object built from the thing certificate together with its private key. With an empty list, AWS IoT receives no client identity during the handshake and closes the connection before MQTT CONNECT.
Where does the Amazon root CA file go?
Into server certificate validation, not into the client certificate list. Either let the operating system trust store validate the server chain, or load the root CA inside the certificate validation handler the TLS options expose. The root CA never authenticates the client; only the thing certificate with its private key does.
Can I disable certificate validation to make the connection work?
No. AWS IoT presents a valid server chain, so clearing the validation flags only hides the real error and exposes the session to interception. If validation fails, fix the trust chain or the device clock. A wrong clock makes a valid certificate look expired and produces the same handshake failure.
Does the same fix apply to newer MQTTnet versions?
The member names change but the requirement does not. MQTTnet version 4 moved TLS setup to different builder methods and a separate provider for client certificates. Whatever the version, the TLS options must carry a client certificate that includes its private key. Check the builder members documented for the exact version you deploy instead of reusing an older snippet.
The certificate and key are set and TLS completes, but no CONNACK arrives. What next?
The identity problem is solved; the fault has moved to the broker address, port or MQTT layer. Capture the traffic to find the hop where the exchange stops, and confirm the certificate is active and registered to the thing in AWS. A completed handshake with no CONNACK is a different failure than a rejected client certificate.


