Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LetsEncrypt certificate returns CertPathValidatorException #818

Open
Alphexoed opened this issue Nov 7, 2021 · 0 comments
Open

LetsEncrypt certificate returns CertPathValidatorException #818

Alphexoed opened this issue Nov 7, 2021 · 0 comments

Comments

@Alphexoed
Copy link

Good evening everyone,

I'm trying to do do SSL Pinning for my app and the Lets Encrypt certificate (ISRG Root X1) is returning CertPathValidatorException: Trust anchor for certification path not found.

I've tried changing certificates but no luck, I've read on one of these issues that its the key store but still didn't work out.

I'm on Android Studios using Kotlin, using Fuel for the web requests, here is the code I have, thanks!

fun pinning(ctx: Context): Pair<SSLSocketFactory, KeyStore> {
    val cf: CertificateFactory = CertificateFactory.getInstance("X.509")
    val caInput: InputStream = BufferedInputStream(ctx.resources.openRawResource(R.raw.isrgrootx1))
    val ca: X509Certificate = caInput.use {
        cf.generateCertificate(it) as X509Certificate
    }
    Log.println(Log.DEBUG, "SYSTEM-CA", ca.subjectDN.toString())

    // Create a KeyStore containing our trusted CAs
    val keyStoreType = KeyStore.getDefaultType()
    val keyStore = KeyStore.getInstance(keyStoreType).apply {
        load(null, null)
        setCertificateEntry("ca", ca)
    }

    // Create a TrustManager that trusts the CAs inputStream our KeyStore
    val tmfAlgorithm: String = TrustManagerFactory.getDefaultAlgorithm()
    val tmf: TrustManagerFactory = TrustManagerFactory.getInstance(tmfAlgorithm).apply {
        init(keyStore)
    }

    // Create an SSLContext that uses our TrustManager
    val context: SSLContext = SSLContext.getInstance("TLS").apply {
        init(null, tmf.trustManagers, null)
    }

    return Pair(context.socketFactory, keyStore)
}


val (pin, key) = pinning(this)
FuelManager.instance.socketFactory = pin
FuelManager.instance.keystore = key
Log.println(Log.INFO, "FUEL-MANAGER", "Imported instances successfully")

The FuelManager is run as soon as the app starts (override fun onStart())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant