Skip to content

Can't get SSL/TLS to work #4

@MatthewFrisky

Description

@MatthewFrisky

I am using the library in react-native with SSL/TLS like so:

(async () => {
    try {
      // Paths to your certificate files
      const certPath =
        'client.crt';
      const keyPath =
        'client.key';
      const caPath =
        'ca-root-cert.der';

      const clientCert = await readFile(certPath);
      const clientKey = await readFile(keyPath);

      // Read DER file as a Buffer
      const caDer = await readFile(caPath, 'base64'); // Read DER file as base64

      // Configuration
      const tlsOptions = {
        caDer: Buffer.from(caDer),
        cert: clientCert,
        key: clientKey,
      };

      const options: ConnectionOptions = {
        clientId: 'myClientId',
        cleanSession: true,
        keepAlive: 60,
        timeout: 60,
        maxInFlightMessages: 1,
        autoReconnect: true,
        username: 'user',
        password: 'pass',
        tls: tlsOptions,
        allowUntrustedCA: true,
        enableSsl: true,
        protocol: 'mqtts',
      };

      MqttClient.connect('mqtts://127.0.0.1:8883', options)
        .then(() => console.log('Connected'))
        .catch(error => console.error('Connection failed: ', error));
    } catch (error) {
      console.error('Error loading certificates:', error);
    }
  })();

My Mosquitto config is:

listener 8883
cafile ca-root-cert.crt
certfile server.crt
keyfile server.key
require_certificate true
allow_anonymous true

But I am just getting a protocl error from Mosquitto when trying to connect.

I have tested the certificates using mosquitto_sub and they are working there and also tested that I am correctly reading in the certificate files from the directories so I am not sure what the issue is.

Also the connection works fine if I don't use TLS/SSL and it even works using username/password.

If you have any insight please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions