The issue is I am trying to implement TLS following these two links:
https://www.hivemq.com/blog/end-to-end-encryption-in-the-cloud/
https://www.hivemq.com/blog/mqtt-client-library-mqtt-js/
Here below I have described what I did.
(1) keytool -genkey -keyalg RSA -alias hivemq -keystore hivemq.jks -storepass hello123 -validity 360 -keysize 2048
I created a .jks file using this command. Had the key for <hivemq> alias set to hellokey123
Checked the jks file using command: keytool -list -v -keystore hivemq.jks
and it opened with password "hello123", thus no error in its creation
(2) Exported keystore to a .pem file using: keytool -exportcert -alias hivemq -keystore hivemq.jks -rfc -file server.pem
Checked the .pem file with command: openssl rsa -in server.pem -out private.key
Error given:
unable to load Private Key
15156:error:0909006C:PEM routines:get_name:no start line:../openssl-1.1.1k/crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY
(3) Created .pem based client certificate using: openssl req -x509 -newkey rsa:2048 -keyout mqtt-client-key.pem -out mqtt-client-cert.pem -days 360
Checked the .pem files with command: openssl rsa -in [filename].pem -out private.key
The files were exported with the set pem passphrase
(4) Exported client certificate from the PEM file into an CRT file using command: openssl x509 -outform der -in mqtt-client-cert.pem -out mqtt-client-cert.crt
(5) Imported the certificate into a Java keystore using command: keytool -import -file mqtt-client-cert.crt -alias client -keystore hivemq-trust-store.jks -storepass hellotrust123
Checked the jks file using command: keytool -list -v -keystore hivemq-trust-store.jks
And it opened with password "hellotrust123", thus no error in its creation
/************************************************/
Configured config.xml as
<listeners>
<tcp-listener>
<port>8883</port>
<bind-address>0.0.0.0</bind-address>
<name>my-tcp-listener</name>
</tcp-listener>
<tls-tcp-listener>
<port>1883</port>
<bind-address>0.0.0.0</bind-address>
<proxy-protocol>true</proxy-protocol>
<tls>
<keystore>
<path>../hivemq.jks</path>
<password>hello123</password>
<private-key-password>hellokey123</private-key-password>
</keystore>
<client-authentication-mode>REQUIRED</client-authentication-mode>
<truststore>
<path>../hivemq-trust-store.jks</path>
<password>hellotrust123</password>
</truststore>
</tls>
</tls-tcp-listener>
</listeners>
Now, in nodejs file using mqtt.js,
When I configure to use tcp port 8883,
var options = {
port: 8883,
host: 'localhost'
};
it works,
But when I use configuration for port 1883
var options = {
port: 1883,
host: 'localhost',
keyPath: '../mqtt-client-key.pem',
certPath: '../mqtt-client-cert.pem',
rejectUnauthorized : true,
// The CA list will be used to determine if server is authorized
ca: '../hivemq-server-cert.pem'
};
Nothing happens, no error. I don't understand what is happening. Would be really grateful if someone could help.
Folder strucutre is such:
root
|->Trial
|->|->nodeScript.js
|->mqtt-client-key.pem
|->mqtt-client-cert.pem
|->hivemq-server-cert.pem
|->hivemq-ce-2022.1
|->|->conf
|->|->|->config.xml
|->|->bin
|->|->|->run.sh