I use Erel's "SetSSLFactory" in the B4A android version and it works great. In the B4J Windows version, there is another name for CA STORE "Windows-ROOT" instead of "AndroidCAStore". It works when I start it from the editor. But after creating the "Build Standalone Package", the error "(NoSuchAlgorithmException) java.security.NoSuchAlgorithmException: Windows-ROOT KeyStore not available" appears. In this part, all existing CA certificates from STORE are loaded.
SetSSLFactory:
Private Sub SetSSLFactory (StoreDir As String, StoreFile As String, StorePassword As String)
Log("SSL start )
Dim hc As OkHttpClient = HttpUtils2Service.hc
Dim builder As JavaObject = hc.As(JavaObject).RunMethod("sharedInit", Array("hc"))
Dim sslfactoryBuilder As JavaObject
sslfactoryBuilder = sslfactoryBuilder.InitializeStatic("nl.altindag.sslcontext.SSLFactory").RunMethod("builder", Null)
Dim in As InputStream = File.OpenInput(StoreDir, StoreFile)
Dim keystore As JavaObject
keystore.InitializeStatic("java.security.KeyStore")
Dim password As Object = StorePassword.As(JavaObject).RunMethod("toCharArray", Null) 'ignore
Dim store As JavaObject = keystore.RunMethodJO("getInstance", Array("pkcs12"))
store.RunMethod("load", Array(in, password)) 'ignore
Log("Store end")
Log("Load (R)CA")
Dim ks As JavaObject
ks.InitializeStatic("java.security.KeyStore")
Dim ks1 As JavaObject=ks.RunMethodJO("getInstance",Array("Windows-ROOT"))'AndroidCAStore
ks1.RunMethod("load",Array( Null))
Dim aliases As JavaObject = ks1.RunMethod("aliases",Null)
Do While aliases.RunMethod("hasMoreElements", Null)
Dim b As String = aliases.RunMethod("nextElement", Null)
'Log("aliases: "&b)
Dim sert As JavaObject
sert.InitializeStatic("java.security.cert.Certificate")
sert=ks1.RunMethodJO("getCertificate",Array (b))
store.RunMethod("setCertificateEntry",Array (b,sert))
Loop
Log("CA end")
sslfactoryBuilder.RunMethod("withIdentityMaterial", Array(store, password))
sslfactoryBuilder.RunMethod("withTrustMaterial", Array(store, password))
'uncomment if need to disable http 2.
Dim protocol As JavaObject
protocol = protocol.InitializeStatic("okhttp3.Protocol").RunMethod("valueOf", Array("HTTP_1_1"))
Dim protocols As List = Array(protocol)
builder.RunMethod("protocols", Array(protocols))
Dim sslfactory As JavaObject = sslfactoryBuilder.RunMethod("build", Null)
Dim socketfactory As JavaObject = sslfactory.RunMethodJO("getSslContext", Null).RunMethod("getSocketFactory", Null)
Dim trustmanager As JavaObject = sslfactory.RunMethodJO("getTrustManager", Null)
builder.RunMethod("sslSocketFactory", Array(socketfactory, trustmanager.RunMethod("get", Null)))
builder.RunMethod("hostnameVerifier", Array(sslfactory.RunMethod("getHostnameVerifier", Null)))
hc.As(JavaObject).SetField("client", builder.RunMethod("build", Null)) 'ignore
Log ("SSL End")
End Sub