I have a simple web api running on a raspberry pi that sits behind an nginx server on the same pi. I'm using self-signed client certificates to authenticate calls from an android app. This worked completely fine in the past, but I recently came back to this project after rebuilding some hardware, and when I try and use it on my Pixel 2 running Android 8.1, it gives the following exception:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:219)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:268)
...
I generated certs and keys according to: http://nategood.com/client-side-certificate-authentication-in-ngi
Testing with curl
works fine.
I created keystore for the app to use with:
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
I followed the following article to setup the client certs in Android and connect to the server: http://chariotsolutions.com/blog/post/https-with-client-certificates-on/
but I rewrote it in Kotlin, using OkHttp:
private const val SERVER = "https://my.server"
/**
* trustManagers is used to authorize the server's self-signed cert
*/
private val trustManagers by lazy {
val cert = CertificateFactory.getInstance("X.509")
.generateCertificate(appCtx.assets.open("ca.crt")) as X509Certificate
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
load(null, null)
setCertificateEntry(cert.subjectX500Principal.name, cert)
}
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply {
init(trustStore)
}.trustManagers
}
/**
* keyManagers is used to load the client-authentication cert
*/
private val keyManagers by lazy {
// assuming this can only be called after Application is created
val keyStore = KeyStore.getInstance("PKCS12").apply {
load(appCtx.assets.open("client.p12"), "".toCharArray())
}
KeyManagerFactory.getInstance("X509").apply {
init(keyStore, "".toCharArray())
}.keyManagers
}
/**
* sslContext for opening TLS connection to server
*/
private val sslContext by lazy {
SSLContext.getInstance("TLS").apply {
init(keyManagers, trustManagers, null)
}
}
/**
* pass an HTTPS request to server
*/
suspend fun request(url: String): ByteArray? {
return try {
val request = Request.Builder()
.url(url)
.build()
val client = OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManagers[0] as X509TrustManager)
.build()
client.newCall(request).await().body().byteStream().readBytes()
} catch (e: Exception) {
err(e) { "failed to send request" }
null
}
}
This use to work, but now it does not. I spent a day and a half searching for an answer and I've tried the following:
- I've tried using HttpURLConnection instead of OkHttp
- I've tried re-creating all the certs/keys from scratch.
- I've tried using the new "Network Security Configuration":
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<!-- Additionally trust user added CAs -->
<certificates src="user" />
<certificates src="@raw/ca"/>
</trust-anchors>
</base-config>
</network-security-config>
I've read all the examples I can find on creating custom trust managers, and they are all pretty much the same, even https://developer.android.com/training/articles/security-ssl.html#UnknownCa
Everythign I've tried produces the same exception, am I missing something?