Skip to content

Commit b3160b0

Browse files
authored
Remove conscrypt (#77)
* Remove conscrypt dependency * Initialize conscrypt in OkHttpTest
1 parent 46cdd7a commit b3160b0

File tree

7 files changed

+62
-62
lines changed

7 files changed

+62
-62
lines changed

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ dependencies {
100100
implementation(libs.androidx.appcompat)
101101
implementation(libs.androidx.lifecycle.livedata)
102102
implementation(libs.androidx.lifecycle.viewmodel)
103-
implementation(libs.conscrypt)
104103

105104
// Jetpack Compose
106105
implementation(libs.androidx.activityCompose)
@@ -113,6 +112,7 @@ dependencies {
113112
androidTestImplementation(libs.androidx.test.core)
114113
androidTestImplementation(libs.androidx.test.runner)
115114
androidTestImplementation(libs.androidx.test.rules)
115+
androidTestImplementation(libs.conscrypt)
116116
androidTestImplementation(libs.okhttp3.mockwebserver)
117117
androidTestImplementation(libs.mockk.android)
118118

lib/src/main/java/at/bitfire/cert4android/ConscryptIntegration.kt renamed to lib/src/androidTest/java/at/bitfire/cert4android/ConscryptIntegration.kt

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package at.bitfire.cert4android
2+
3+
import okhttp3.internal.tls.BasicCertificateChainCleaner
4+
import okhttp3.internal.tls.BasicTrustRootIndex
5+
import org.junit.Before
6+
import org.junit.Test
7+
8+
class ConscryptIntegrationTest {
9+
10+
@Before
11+
fun setUp() {
12+
// initialize Conscrypt
13+
ConscryptIntegration.initialize()
14+
}
15+
16+
17+
/**
18+
* Test for https://github.com/google/conscrypt/issues/1268.
19+
*
20+
* See also https://github.com/bitfireAT/cert4android/pull/48.
21+
*/
22+
@Test
23+
fun test_X509Certificate_toString() {
24+
val testCert = TestCertificates.crashCert()
25+
26+
// Crashes with Conscrypt 2.5.3
27+
// Uncomment with Conscrypt >2.5.3
28+
// System.err.println(testCert.toString())
29+
}
30+
31+
@Test
32+
fun testBasicCertificateChainCleaner() {
33+
val cleaner = BasicCertificateChainCleaner(BasicTrustRootIndex())
34+
35+
// See https://github.com/bitfireAT/cert4android/issues/72
36+
// CRASHES with Conscrypt 2.5.3:
37+
// cleaner.clean(listOf(TestCertificates.crashCert()), "doesn't matter")
38+
39+
// This is relevant, because okhttp creates such a BasicCertificateChainManager
40+
// when using a custom X509TrustManager. However when the trust manager extends
41+
// X509ExtendedTrustManager, AndroidCertificateChainManager is used on Android.
42+
}
43+
44+
}

lib/src/androidTest/java/at/bitfire/cert4android/ConscryptTest.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/src/androidTest/java/at/bitfire/cert4android/OkhttpTest.kt

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import okhttp3.Cache
55
import okhttp3.CacheControl
66
import okhttp3.OkHttpClient
77
import okhttp3.Request
8-
import okhttp3.internal.tls.BasicCertificateChainCleaner
9-
import okhttp3.internal.tls.BasicTrustRootIndex
108
import okhttp3.internal.tls.OkHostnameVerifier
119
import org.junit.Assert.assertEquals
10+
import org.junit.Before
1211
import org.junit.Test
1312
import javax.net.ssl.SSLContext
1413

1514
class OkhttpTest {
1615

17-
private val context by lazy { InstrumentationRegistry.getInstrumentation().targetContext }
18-
19-
init {
16+
@Before
17+
fun setUp() {
18+
// initialize Conscrypt
2019
ConscryptIntegration.initialize()
2120
}
2221

22+
private val context by lazy { InstrumentationRegistry.getInstrumentation().targetContext }
23+
2324
@Test
2425
fun testAccessICloudComWithCache() {
2526
// See https://github.com/bitfireAT/davx5/issues/713 and
@@ -42,20 +43,6 @@ class OkhttpTest {
4243
}
4344
}
4445

45-
@Test
46-
fun testBasicCertificateChainCleaner() {
47-
val cleaner = BasicCertificateChainCleaner(BasicTrustRootIndex())
48-
49-
// See https://github.com/bitfireAT/cert4android/issues/72
50-
// CRASHES with Conscrypt 2.5.3:
51-
// cleaner.clean(listOf(TestCertificates.crashCert()), "doesn't matter")
52-
53-
// This is relevant, because okhttp creates such a BasicCertificateChainManager
54-
// when using a custom X509TrustManager. However when the trust manager extends
55-
// X509ExtendedTrustManager, AndroidCertificateChainManager is used on Android.
56-
}
57-
58-
5946
fun buildClient(useCache: Boolean): OkHttpClient {
6047
val builder = OkHttpClient.Builder()
6148

lib/src/main/java/at/bitfire/cert4android/CustomCertManager.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import javax.net.ssl.X509TrustManager
1616
/**
1717
* TrustManager to handle custom certificates.
1818
*
19-
* Initializes Conscrypt when it is first loaded.
20-
*
2119
* @param trustSystemCerts whether system certificates will be trusted
2220
* @param appInForeground - `true`: if needed, directly launches [TrustCertificateActivity] and shows notification (if possible)
2321
* - `false`: if needed, shows notification (if possible)
@@ -84,14 +82,4 @@ class CustomCertManager @JvmOverloads constructor(
8482

8583
}
8684

87-
88-
companion object {
89-
90-
init {
91-
// On first load of this class, initialize Conscrypt.
92-
ConscryptIntegration.initialize()
93-
}
94-
95-
}
96-
9785
}

lib/src/main/java/at/bitfire/cert4android/CustomCertStore.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import kotlinx.coroutines.TimeoutCancellationException
1111
import kotlinx.coroutines.flow.StateFlow
1212
import kotlinx.coroutines.runBlocking
1313
import kotlinx.coroutines.withTimeout
14-
import org.conscrypt.Conscrypt
1514
import java.io.File
1615
import java.io.FileInputStream
1716
import java.io.FileOutputStream
@@ -20,6 +19,8 @@ import java.security.cert.CertificateException
2019
import java.security.cert.X509Certificate
2120
import java.util.logging.Level
2221
import java.util.logging.Logger
22+
import javax.net.ssl.TrustManagerFactory
23+
import javax.net.ssl.X509TrustManager
2324

2425
class CustomCertStore internal constructor(
2526
private val context: Context,
@@ -51,7 +52,15 @@ class CustomCertStore internal constructor(
5152
get() = Logger.getLogger(javaClass.name)
5253

5354
/** system default TrustStore */
54-
private val systemKeyStore by lazy { Conscrypt.getDefaultX509TrustManager() }
55+
private val systemKeyStore by lazy {
56+
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).run {
57+
init(null as KeyStore?) // null means "use system default trust store"
58+
trustManagers
59+
.filterIsInstance<X509TrustManager>()
60+
.firstOrNull()
61+
?: throw IllegalStateException("No X509TrustManager found")
62+
}
63+
}
5564

5665
/** custom TrustStore */
5766
@VisibleForTesting

0 commit comments

Comments
 (0)