B4J Question MQTT connecting into SSL:// throws an error

quique

Member
Licensed User
Longtime User
Hi There! I got a question regarding MQTT connecting thru SSL into my mosquitto broker.

The SSL certificate I got installed on my server is from https://letsencrypt.org/ ...

I already got a B4A app running with MQTT using this connection. In B4A I got no error...

But on B4J, I get the following error: (SunCertPathBuilderException) sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

What can I do to remediate this ?

Regards,
Enrique
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try to update Java. The newer version might include the required certificates.

It is also possible to trust all certificates.

Try this:
B4X:
    Dim mo As MqttConnectOptions
   mo.Initialize("aa", "bb")
   Dim jo As JavaObject = mo
   jo.RunMethod("setSocketFactory", Array(CreateTrustAllSSLSocketFactory))
 'Use Mqtt.Connect2(mo)

Sub CreateTrustAllSSLSocketFactory As Object
   Dim tm As CustomTrustManager
   tm.InitializeAcceptAll
   Dim SSLContext As JavaObject
   SSLContext = SSLContext.InitializeStatic("javax.net.ssl.SSLContext").RunMethod("getInstance", Array("TLS"))
   SSLContext.RunMethod("init", Array(Null, tm, Null))
   Dim Factory As JavaObject = SSLContext.RunMethod("getSocketFactory", Null)
   Return Factory
End Sub

Depends on jNet library.
 
Last edited:
Upvote 0

quique

Member
Licensed User
Longtime User
Thank you Erel,

Yes! Both of your suggestions worked fine. I updated Java SDK into version 8 update 181, and it cleared ok the root certificate from letsencrypt.

Also, before trying to update the Java SDK, i tried your code, and it also worked fine (side note: in Sub CreateTrustAllSSLSocketFactory, the return line at the end says Factrory (extra 'r').

I decided not to use your code, but just trust the java updated certificate thing, which sounds like the most secure / judicious thing to do :)
 
Upvote 0
Top