Skip to content
Open
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
12 changes: 8 additions & 4 deletions wvalib/src/main/java/com/digi/wva/internal/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ public final void onJsonParseError(JSONException error, String rawBody) {

public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
private String supportedProtocol;

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
public TLSSocketFactory(String protocol) throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance(protocol);
context.init(null, new X509TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
Expand All @@ -193,6 +194,7 @@ public void checkClientTrusted(X509Certificate[] chain, String authType)
}
}}, null);
internalSSLSocketFactory = context.getSocketFactory();
supportedProtocol = protocol;
}

@Override
Expand Down Expand Up @@ -232,7 +234,7 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre

private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.2"});
((SSLSocket)socket).setEnabledProtocols(new String[] { supportedProtocol });
}
return socket;
}
Expand All @@ -247,7 +249,9 @@ private SSLSocketFactory makeSSLSocketFactory() {
SSLSocketFactory factory = null;

try {
factory = new TLSSocketFactory();
// Enable TLSv1.2 protocol only if the Android API is 16+, otherwise use TLSv1.
int currentAPIVersion = android.os.Build.VERSION.SDK_INT;
factory = (currentAPIVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) ? new TLSSocketFactory("TLSv1.2") : new TLSSocketFactory("TLSv1");
} catch (NoSuchAlgorithmException e) {
} catch (KeyManagementException e) {
}
Expand Down