Skip to content

Commit e04a1e7

Browse files
author
yostyle
committed
Limit supported cipher suites
1 parent 3a387c5 commit e04a1e7

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.matrix.android.sdk.api
1818

19+
import okhttp3.ConnectionSpec
1920
import org.matrix.android.sdk.api.crypto.MXCryptoConfig
2021
import java.net.Proxy
2122

@@ -44,6 +45,10 @@ data class MatrixConfiguration(
4445
* You can create one using for instance Proxy(proxyType, InetSocketAddress.createUnresolved(hostname, port).
4546
*/
4647
val proxy: Proxy? = null,
48+
/**
49+
* TLS versions and cipher suites limitation for unauthenticated requests
50+
*/
51+
val connectionSpec: ConnectionSpec = ConnectionSpec.RESTRICTED_TLS,
4752
/**
4853
* True to advertise support for call transfers to other parties on Matrix calls.
4954
*/

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/HomeServerConnectionConfig.kt

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.matrix.android.sdk.api.auth.data
1919
import android.net.Uri
2020
import com.squareup.moshi.JsonClass
2121
import okhttp3.CipherSuite
22+
import okhttp3.ConnectionSpec
2223
import okhttp3.TlsVersion
2324
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig.Builder
2425
import org.matrix.android.sdk.internal.network.ssl.Fingerprint
@@ -191,13 +192,14 @@ data class HomeServerConnectionConfig(
191192
/**
192193
* Convenient method to limit the TLS versions and cipher suites for this Builder
193194
* Ref:
194-
* - https://www.ssi.gouv.fr/uploads/2017/02/security-recommendations-for-tls_v1.1.pdf
195+
* - https://www.ssi.gouv.fr/uploads/2017/07/anssi-guide-recommandations_de_securite_relatives_a_tls-v1.2.pdf
195196
* - https://developer.android.com/reference/javax/net/ssl/SSLEngine
196197
*
197198
* @param tlsLimitations true to use Tls limitations
198199
* @param enableCompatibilityMode set to true for Android < 20
199200
* @return this builder
200201
*/
202+
@Deprecated("TLS versions and cipher suites are limited by default")
201203
fun withTlsLimitations(tlsLimitations: Boolean, enableCompatibilityMode: Boolean): Builder {
202204
if (tlsLimitations) {
203205
withShouldAcceptTlsExtensions(false)
@@ -209,14 +211,7 @@ data class HomeServerConnectionConfig(
209211
forceUsageOfTlsVersions(enableCompatibilityMode)
210212

211213
// Cipher suites
212-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
213-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256)
214-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256)
215-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256)
216-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
217-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384)
218-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256)
219-
addAcceptedTlsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
214+
ConnectionSpec.RESTRICTED_TLS.cipherSuites?.let { this.tlsCipherSuites.addAll(it) }
220215

221216
if (enableCompatibilityMode) {
222217
// Adopt some preceding cipher suites for Android < 20 to be able to negotiate

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/NetworkModule.kt

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.facebook.stetho.okhttp3.StethoInterceptor
2020
import com.squareup.moshi.Moshi
2121
import dagger.Module
2222
import dagger.Provides
23+
import okhttp3.ConnectionSpec
2324
import okhttp3.OkHttpClient
2425
import okhttp3.logging.HttpLoggingInterceptor
2526
import org.matrix.android.sdk.BuildConfig
@@ -29,6 +30,7 @@ import org.matrix.android.sdk.internal.network.TimeOutInterceptor
2930
import org.matrix.android.sdk.internal.network.UserAgentInterceptor
3031
import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingInterceptor
3132
import org.matrix.android.sdk.internal.network.interceptors.FormattedJsonHttpLogger
33+
import java.util.Collections
3234
import java.util.concurrent.TimeUnit
3335

3436
@Module
@@ -66,6 +68,8 @@ internal object NetworkModule {
6668
httpLoggingInterceptor: HttpLoggingInterceptor,
6769
curlLoggingInterceptor: CurlLoggingInterceptor,
6870
apiInterceptor: ApiInterceptor): OkHttpClient {
71+
val spec = ConnectionSpec.Builder(matrixConfiguration.connectionSpec).build()
72+
6973
return OkHttpClient.Builder()
7074
.connectTimeout(30, TimeUnit.SECONDS)
7175
.readTimeout(60, TimeUnit.SECONDS)
@@ -87,6 +91,7 @@ internal object NetworkModule {
8791
proxy(it)
8892
}
8993
}
94+
.connectionSpecs(Collections.singletonList(spec))
9095
.build()
9196
}
9297

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/ssl/CertUtil.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,13 @@ internal object CertUtil {
177177

178178
val trustPinned = arrayOf<TrustManager>(PinnedTrustManagerProvider.provide(hsConfig.allowedFingerprints, defaultTrustManager))
179179

180-
val sslSocketFactory: SSLSocketFactory
181-
182-
if (hsConfig.forceUsageTlsVersions && hsConfig.tlsVersions != null) {
180+
val sslSocketFactory = if (hsConfig.forceUsageTlsVersions && !hsConfig.tlsVersions.isNullOrEmpty()) {
183181
// Force usage of accepted Tls Versions for Android < 20
184-
sslSocketFactory = TLSSocketFactory(trustPinned, hsConfig.tlsVersions)
182+
TLSSocketFactory(trustPinned, hsConfig.tlsVersions)
185183
} else {
186184
val sslContext = SSLContext.getInstance("TLS")
187185
sslContext.init(null, trustPinned, java.security.SecureRandom())
188-
sslSocketFactory = sslContext.socketFactory
186+
sslContext.socketFactory
189187
}
190188

191189
return PinnedSSLSocketFactory(sslSocketFactory, defaultTrustManager!!)
@@ -237,14 +235,14 @@ internal object CertUtil {
237235
* @return a list of accepted TLS specifications.
238236
*/
239237
fun newConnectionSpecs(hsConfig: HomeServerConnectionConfig): List<ConnectionSpec> {
240-
val builder = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
238+
val builder = ConnectionSpec.Builder(ConnectionSpec.RESTRICTED_TLS)
241239
val tlsVersions = hsConfig.tlsVersions
242-
if (null != tlsVersions && tlsVersions.isNotEmpty()) {
240+
if (!tlsVersions.isNullOrEmpty()) {
243241
builder.tlsVersions(*tlsVersions.toTypedArray())
244242
}
245243

246244
val tlsCipherSuites = hsConfig.tlsCipherSuites
247-
if (null != tlsCipherSuites && tlsCipherSuites.isNotEmpty()) {
245+
if (!tlsCipherSuites.isNullOrEmpty()) {
248246
builder.cipherSuites(*tlsCipherSuites.toTypedArray())
249247
}
250248

0 commit comments

Comments
 (0)