Unable to connect hivemq via TLS V1.2 Certifcate Authentication using mqtt-spy

Hi Team,

On trying to connect to hivemq via Certificate Authentication method using mqtt-spy
we are getting the below mentioned error:

2022-09-20 16:39:04,886 DEBUG [FileUtils ] - Trying to read C:\Users\NeKhan\Desktop\Certificate\NewFolder\mqtt-client-cert.pem from filesystem
2022-09-20 16:39:04,907 DEBUG [SecureSocketUtils ] - Reading PEM file C:\Users\NeKhan\Desktop\Certificate\NewFolder\mqtt-client-key.pem, type = ENCRYPTED PRIVATE KEY
2022-09-20 16:39:04,934 ERROR [MqttConnectionViewManager ] - Cannot create connection properties
pl.baczkowicz.spy.exceptions.ConfigurationException: Invalid configuration parameters
at pl.baczkowicz.mqttspy.connectivity.MqttConnectionDetailsWithOptions.(MqttConnectionDetailsWithOptions.java:91)
at pl.baczkowicz.mqttspy.connectivity.MqttRuntimeConnectionProperties.(MqttRuntimeConnectionProperties.java:34)
at pl.baczkowicz.mqttspy.ui.MqttConnectionViewManager.openConnection(MqttConnectionViewManager.java:152)
at pl.baczkowicz.mqttspy.connectivity.MqttConnectionFactory$2.handle(MqttConnectionFactory.java:330)
at pl.baczkowicz.mqttspy.connectivity.MqttConnectionFactory$2.handle(MqttConnectionFactory.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:750)
Caused by: pl.baczkowicz.spy.exceptions.SpyException: Cannot create TLS/SSL connection
at pl.baczkowicz.spy.security.SecureSocketFactoryBuilder.getSocketFactory(SecureSocketFactoryBuilder.java:106)
at pl.baczkowicz.mqttspy.connectivity.MqttConnectionDetailsWithOptions.populateMqttConnectOptions(MqttConnectionDetailsWithOptions.java:175)
at pl.baczkowicz.mqttspy.connectivity.MqttConnectionDetailsWithOptions.(MqttConnectionDetailsWithOptions.java:87)
… 59 more
Caused by: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DER input, Integer tag error
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:252)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
at pl.baczkowicz.spy.security.SecureSocketUtils.loadPrivateKeyFromPemFile(SecureSocketUtils.java:103)
at pl.baczkowicz.spy.security.SecureSocketUtils.getKeyManagerFactory(SecureSocketUtils.java:192)
at pl.baczkowicz.spy.security.SecureSocketFactoryBuilder.getSocketFactory(SecureSocketFactoryBuilder.java:99)
… 61 more
Caused by: java.security.InvalidKeyException: IOException : DER input, Integer tag error
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:351)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
at sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:130)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:80)
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:357)
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:248)
… 65 more

For creating required certificate files, truststore and keystore we have taken reference from Setting up TLS for your cloud-based MQTT broker (hivemq.com)

Hi @neha.khan ,

From the error it seems that the MQTT client application cannot read or properly decode the certificate file. It could be either an issue with the client app or the certificate file itself.
Try to test the same but using some other MQTT client, for example MQTT CLI. With MQTT CLI the publish command should look like:

mqtt-cli publish --topic Test --message Hello --host <your host> --port <your TLS port> --debug \
 --cafile <your cafile> --cert <your client cert file> --key <your client key file> \
 --tls-version 'TLSv.1.2' \
 --identifier <my client id>

I hope this helps,
Dasha from HiveMQ team