MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

Unable to connect over TLS

Open PierreLoupG opened this issue 4 years ago • 4 comments

Hi, I'm trying to connect to a broker over TLS in react-native. Based on the doc I tried this code:

const options = {
  port: PORT,
  host: HOST,
  protocol: 'mqtts',
  secureProtocol: 'TLS_method',
  username: USERNAME,
  password: PASSWORD,
  ca: CA_FILE
}

const client = mqtt.connect(options);

client.on('error', (error) => {
  console.log("error", error);
});

client.on('connect', () => {
  console.log("connected");
});

The options object will be passed through tls.connect(), so I specified the certificate file.

The connection doesn't return an error, but the client is not connected. What did I do wrong?

Thanks in advance for your help

PierreLoupG avatar Jul 17 '20 09:07 PierreLoupG

What is CA_FILE? It can't be a path to file--you need to load it yourself and pass in the file's content as a Buffer or string.

Try running with env var DEBUG=mqttjs,mqttjs:* to see if there is any useful output.

jdiamond avatar Jul 17 '20 22:07 jdiamond

CA_FILE is the file content as a string, not the path.

The broker log show:

1595238085: OpenSSL Error: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
1595238085: Socket error on client <unknown>, disconnecting.

I checked, and the cafile in the mosquitto.conf is the same one I use. What could cause this error ?

PierreLoupG avatar Jul 20 '20 09:07 PierreLoupG

I think the certificate in mosquitto.conf and the CA file you use with MQTT.js would only be the same if the certificate was self-signed.

Your error message makes me think mosquitto is expecting a client certificate. Are you setting the require_certificate and cafile options in mosquitto.conf? If so, remove those. You should probably only be setting certfile and keyfile. But if you are trying to use client certificates, you need to also set the cert and key options with MQTT.js.

jdiamond avatar Jul 20 '20 17:07 jdiamond

How did you solve it ?

YassineChe avatar Jul 06 '22 00:07 YassineChe

This is an automated message to let you know that this issue has gone 365 days without any activity. In order to ensure that we work on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a "bump" to keep it open.

Thank you for your contribution.

github-actions[bot] avatar Jul 06 '23 02:07 github-actions[bot]

This issue was automatically closed due to inactivity.

github-actions[bot] avatar Jul 21 '23 01:07 github-actions[bot]