Search code examples
node.jssslmqttmosquittopem

Sending a message to a remote broker using a pem-certificate


I'm writing a script to get some sensor-data using MQTT, transform the incoming value is a specific way and forward it to a cloud which uses an encrypted communication. The script runs on RaspberryPi 3 (Raspbian) where Mosquitto runs as MQTT-broker ( The sensors send the data to this broker)

So far I get the information from the the sensors and I can transform the content like desired.

The problem is when I try to connect to the remote broker, I got following error-message

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Object.createSecureContext (_tls_common.js:67:17)
at Object.TLSSocket._init.ssl.onclienthello.ssl.oncertcb.exports.connect (_tls_wrap.js:1015:46)
at Object.buildBuilder (/home/pi/node_modules/mqtt/lib/connect/tls.js:13:20)
at MqttClient.wrapper [as streamBuilder] (/home/pi/node_modules/mqtt/lib/connect/index.js:135:36)
at MqttClient._setupStream (/home/pi/node_modules/mqtt/lib/client.js:246:22)
at new MqttClient (/home/pi/node_modules/mqtt/lib/client.js:227:8)
at Object.connect (/home/pi/node_modules/mqtt/lib/connect/index.js:138:10)
at Object.<anonymous> (/home/pi/GATT_server/MQTT_module.js:3:25)

I have tried to configure Mosquitto as MQTT-bridge to use the config-file and it looks like it'd work (There are no error messages when the service is restarted).

The problem when I use Mosquitto as a bridge the script can't subscribe to the topics published by the sensors.

I'm not familiar with those certificates but from reading the doc of the file 'client-options.cs' I wrote following lines:

var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://localhost', 'port:1883');
var remoteBroker = mqtt.connect('mqtts://xxx.xxx.io', 
	{port:8883,
	protocol:'ssl', 
	username:'username', 
	password:'123password123', 
	cert:'/etc/mosquitto/certs/ca.pem'} );
                
/*....
.... many lines later
....*/
remoteBroker.publish(topicToUseOnRemoteMqttBroker, PayloadToForward); 

Am I doing it right? Or did I miss something?

If the certificate is OK for the Mosquitto-Bridge it should be OK for the remote broker, too. - Is that right?

If you have additional links/papers about this topic - I like to learn ;P Google gave me so many information - I don't know where to start :(


Solution

  • The entry for the cert should not be the path to the certificate file, but the actual cert it's self. You need to read the file in and pass that.

    Somethings like this:

    var remoteBroker = mqtt.connect('mqtts://xxx.xxx.io', 
        {port:8883,
        protocol: 'ssl', 
        username: 'username', 
        password: '123password123', 
        cert: fs.readFileSync('/etc/mosquitto/certs/ca.pem')} );