Skip to content

Commit

Permalink
fix: #425 plugin crashes on Android SDK levels < 24
Browse files Browse the repository at this point in the history
  • Loading branch information
silkimen committed Aug 20, 2021
1 parent 8031515 commit 16ac763
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.2.1

- Fixed #425: plugin crashes on Android SDK levels < 24

## 3.2.0

- Feature #420: implement blacklist feature to disable SSL/TLS versions on Android (thanks to @MobisysGmbH)
Expand Down
23 changes: 15 additions & 8 deletions src/android/com/silkimen/http/TLSSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.net.Socket;
import java.net.UnknownHostException;

import java.util.Arrays;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
Expand All @@ -15,11 +15,15 @@
public class TLSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory delegate;
private String[] blacklistedProtocols;
private List<String> blacklistedProtocols;

public TLSSocketFactory(SSLContext context, String[] blacklistedProtocols) {
this.delegate = context.getSocketFactory();
this.blacklistedProtocols = Arrays.stream(blacklistedProtocols).map(String::trim).toArray(String[]::new);
this.blacklistedProtocols = new ArrayList();

for (int i = 0; i < blacklistedProtocols.length; ++i) {
this.blacklistedProtocols.add(blacklistedProtocols[i].trim());
}
}

@Override
Expand Down Expand Up @@ -65,12 +69,15 @@ private Socket enableTLSOnSocket(Socket socket) {
}

String[] supported = ((SSLSocket) socket).getSupportedProtocols();
List<String> filtered = new ArrayList();

String[] filtered = Arrays.stream(supported).filter(
val -> Arrays.stream(this.blacklistedProtocols).noneMatch(val::equals)
).toArray(String[]::new);
for (int i = 0; i < supported.length; ++i) {
if (!this.blacklistedProtocols.contains(supported[i])) {
filtered.add(supported[i]);
}
}

((SSLSocket) socket).setEnabledProtocols(filtered);
((SSLSocket) socket).setEnabledProtocols(filtered.toArray(new String[0]));

return socket;
}
Expand Down

0 comments on commit 16ac763

Please sign in to comment.