Skip to content

Commit 3d23ac7

Browse files
committed
Don't try to use unsupported TLS versions. Closes #1458
1 parent 070344d commit 3d23ac7

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

acra-http/src/main/java/org/acra/security/ProtocolSocketFactoryWrapper.kt

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,16 @@ import javax.net.ssl.SSLSocket
2424
import javax.net.ssl.SSLSocketFactory
2525

2626
class ProtocolSocketFactoryWrapper(private val delegate: SSLSocketFactory, protocols: List<TLS>) : SSLSocketFactory() {
27-
private val protocols: List<String>
28-
29-
init {
30-
val list = protocols.toMutableList()
31-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
32-
list.remove(TLS.V1_3)
33-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
34-
list.remove(TLS.V1_2)
35-
list.remove(TLS.V1_1)
36-
}
37-
}
38-
this.protocols = list.map { it.id }
39-
}
27+
private val protocols: List<String> = protocols.filter { Build.VERSION.SDK_INT >= it.minSdk }.map { it.id }
4028

4129
private fun setProtocols(socket: Socket): Socket {
42-
if (socket is SSLSocket && isTLSServerEnabled(socket)) {
43-
socket.enabledProtocols = protocols.toTypedArray()
44-
}
45-
return socket
46-
}
47-
48-
private fun isTLSServerEnabled(sslSocket: SSLSocket): Boolean {
49-
for (protocol in sslSocket.supportedProtocols) {
50-
if (protocols.contains(protocol)) {
51-
return true
30+
if (socket is SSLSocket) {
31+
val wantedProtocols = protocols intersect socket.supportedProtocols.toSet()
32+
if (wantedProtocols.isNotEmpty()) {
33+
socket.enabledProtocols = wantedProtocols.toTypedArray()
5234
}
5335
}
54-
return false
36+
return socket
5537
}
5638

5739
override fun getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites

acra-http/src/main/java/org/acra/security/TLS.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package org.acra.security
1717

18-
enum class TLS(val id: String) {
19-
V1("TLSv1"), V1_1("TLSv1.1"), V1_2("TLSv1.2"), V1_3("TLSv1.3");
18+
import android.os.Build
19+
20+
enum class TLS(val id: String, val minSdk: Int) {
21+
V1("TLSv1", Build.VERSION_CODES.BASE),
22+
V1_1("TLSv1.1", Build.VERSION_CODES.JELLY_BEAN),
23+
V1_2("TLSv1.2", Build.VERSION_CODES.JELLY_BEAN),
24+
V1_3("TLSv1.3", Build.VERSION_CODES.Q);
2025
}

0 commit comments

Comments
 (0)