Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import java.security.cert.X509Certificate
import java.security.spec.ECGenParameterSpec
import java.security.spec.ECParameterSpec
import java.security.spec.ECPrivateKeySpec
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLEngine
import javax.net.ssl.SSLPeerUnverifiedException
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509ExtendedTrustManager
import kotlin.test.Test
import kotlin.test.assertFailsWith
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -97,17 +100,17 @@ class DistrustedCertificatesTest {
false,
)

assertFailsWith<SSLPeerUnverifiedException> { send(trusting(emSignRoot)) }
assertFailsWith<SSLPeerUnverifiedException> { send(trustingAnyChain()) }

assertThat(server.requestCount).isEqualTo(0)
}

@Test
fun `does not send the envelope when the client completes the chain with a distrusted CA the server left out`() {
// Like Android, which completes the chain from intermediates it cached on earlier connections.
fun `does not send the envelope when the server leaves out the distrusted CA`() {
// Android completes such a chain from intermediates it cached on earlier connections.
server.useHttps(serverCertificates(leafIssuedBy(distrustedCa)), false)

assertFailsWith<SSLPeerUnverifiedException> { send(trusting(distrustedCa.certificate)) }
assertFailsWith<SSLPeerUnverifiedException> { send(trustingAnyChain()) }

assertThat(server.requestCount).isEqualTo(0)
}
Expand All @@ -125,7 +128,7 @@ class DistrustedCertificatesTest {
server.enqueue(MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST))
server.enqueue(MockResponse())

val result = send(trusting(emSignRoot, trustedCa.certificate))
val result = send(trustingAnyChain())

assertThat(result.isSuccess).isFalse()
assertThat(server.requestCount).isEqualTo(1)
Expand Down Expand Up @@ -156,6 +159,13 @@ class DistrustedCertificatesTest {
.build()
.sslSocketFactory()

/**
* Skips the JDK's own chain validation, which on some JDKs rejects the eMudhra CAs too, so that
* only [DistrustedCertificates] can reject the handshake.
*/
private fun trustingAnyChain(): SSLSocketFactory =
SSLContext.getInstance("TLS").apply { init(null, arrayOf(TrustAnyChain), null) }.socketFactory

private fun certificate(resource: String): X509Certificate =
javaClass.classLoader!!.getResourceAsStream(resource)!!.use {
CertificateFactory.getInstance("X.509").generateCertificate(it) as X509Certificate
Expand All @@ -171,6 +181,38 @@ class DistrustedCertificatesTest {
.generatePrivate(ECPrivateKeySpec(BigInteger.valueOf(value), secp384r1))
}

private object TrustAnyChain : X509ExtendedTrustManager() {
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) = Unit

override fun checkClientTrusted(
chain: Array<X509Certificate>,
authType: String,
socket: Socket,
) = Unit

override fun checkClientTrusted(
chain: Array<X509Certificate>,
authType: String,
engine: SSLEngine,
) = Unit

override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) = Unit

override fun checkServerTrusted(
chain: Array<X509Certificate>,
authType: String,
socket: Socket,
) = Unit

override fun checkServerTrusted(
chain: Array<X509Certificate>,
authType: String,
engine: SSLEngine,
) = Unit

override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
}

/** Presents the certificates of [first] on the first connection and of [then] on later ones. */
private class PerConnectionSocketFactory(
private val first: SSLSocketFactory,
Expand Down
Loading