0

What's wrong with this code, I cannot open the https url from WebView. I added the certificate to src/res/raw with the name "my_ca.crt".

error

I/X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

code

val cf = CertificateFactory.getInstance("X.509")
        val inputS = context?.resources?.openRawResource(R.raw.my_ca)
        val caInput = BufferedInputStream(inputS)
        val ca = cf.generateCertificate(caInput)

        // Create a KeyStore containing our trusted CAs
        val keyStoreType = KeyStore.getDefaultType()
        val trustedKeyStore = KeyStore.getInstance(keyStoreType)
        trustedKeyStore.load(null, null)
        trustedKeyStore.setCertificateEntry("my_ca", ca)


        // Create a TrustManager that trusts the CAs in our KeyStore
        val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
        tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
        tmf?.init(trustedKeyStore)

        // Create an SSLContext that uses our TrustManager
        val sslContext = SSLContext.getInstance("TLS") //"SSL" is also not working
        sslContext.init(null, tmf?.getTrustManagers(), null);

        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)

        web_view.loadUrl(url) 

0 Answers0