Skip to content

Commit

Permalink
Enable all available TLS versions (e.g. 1.1,1.2 for old devices)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Bogomolov committed Mar 2, 2023
1 parent edef19e commit c1a3881
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "tech.bogomolov.incomingsmsgateway"
minSdkVersion 14
targetSdkVersion 33
versionCode 10
versionName "2.2.2"
versionCode 11
versionName "2.2.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ public void testHttpsSuccess() throws Exception {
}

@Test
public void testTlsV1Disables() throws Exception {
public void testTlsV1Disabled() throws Exception {
WorkInfo workInfo = this.getWorkInfo("https://wordpress.com", "test", "{}", false);
assertThat(workInfo.getState(), is(WorkInfo.State.SUCCEEDED));
}

@Test
public void testTlsV1DisabledSslIgnore() throws Exception {
WorkInfo workInfo = this.getWorkInfo("https://wordpress.com", "test", "{}", true);
assertThat(workInfo.getState(), is(WorkInfo.State.SUCCEEDED));
}

@Test
public void testHttpSuccess() throws Exception {
WorkInfo workInfo = this.getWorkInfo("http://example.com", "test", "{}", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.bogomolov.incomingsmsgateway;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -76,6 +77,28 @@ private void showList() {

ArrayList<ForwardingConfig> configs = ForwardingConfig.getAll(context);

listAdapter = new ListAdapter(configs, context);
listview.setAdapter(listAdapter);

FloatingActionButton fab = findViewById(R.id.btn_add);
fab.setOnClickListener(this.showAddDialog());

if (!this.isServiceRunning()) {
this.startService();
}
}

private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if(tech.bogomolov.incomingsmsgateway.SmsReceiverService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

private void startService() {
Context appContext = getApplicationContext();
Intent intent = new Intent(this, SmsReceiverService.class);

Expand All @@ -84,13 +107,6 @@ private void showList() {
} else {
appContext.startService(intent);
}

listAdapter = new ListAdapter(configs, context);

listview.setAdapter(listAdapter);

FloatingActionButton fab = findViewById(R.id.btn_add);
fab.setOnClickListener(this.showAddDialog());
}

private void showInfo(String text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package tech.bogomolov.incomingsmsgateway;
package tech.bogomolov.incomingsmsgateway.SSLSocketFactory;

import android.annotation.SuppressLint;
import android.net.SSLCertificateSocketFactory;
import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -10,14 +14,19 @@
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class TLSSocketFactoryKitKat extends SSLSocketFactory {
public class TLSSocketFactory extends SSLSocketFactory {

private final SSLSocketFactory factory;

public TLSSocketFactoryKitKat() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
factory = context.getSocketFactory();
@SuppressLint("SSLCertificateSocketFactoryGetInsecure")
public TLSSocketFactory(boolean ignoreSsl) throws KeyManagementException, NoSuchAlgorithmException {
if (ignoreSsl) {
factory = SSLCertificateSocketFactory.getInsecure(0, null);
} else {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
factory = context.getSocketFactory();
}
}

@Override
Expand Down Expand Up @@ -62,7 +71,8 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre

private Socket enableTLSOnSocket(Socket socket) {
if((socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
String[] supportedProtocols = ((SSLSocket)socket).getSupportedProtocols();
((SSLSocket)socket).setEnabledProtocols(supportedProtocols);
}
return socket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import javax.net.ssl.HttpsURLConnection;

import tech.bogomolov.incomingsmsgateway.SSLSocketFactory.TLSSocketFactory;

public class WebHookWorkRequest extends Worker {

public final static String DATA_URL = "URL";
Expand Down Expand Up @@ -69,26 +72,25 @@ public Result doWork() {
return Result.success();
}

@SuppressLint({"SSLCertificateSocketFactoryGetInsecure", "AllowAllHostnameVerifier"})
@SuppressLint({"AllowAllHostnameVerifier"})
private String makeRequest(String urlString, String text, String headers, boolean ignoreSsl) {
String result = RESULT_SUCCESS;

Log.i("SmsGateway", "request " + urlString);

HttpURLConnection urlConnection = null;

try {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();

if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(
new TLSSocketFactory(ignoreSsl)
);

if (ignoreSsl) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(
SSLCertificateSocketFactory.getInsecure(0, null));
((HttpsURLConnection) urlConnection).setHostnameVerifier(new AllowAllHostnameVerifier());
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(
new TLSSocketFactoryKitKat()
);
}
}

Expand Down

0 comments on commit c1a3881

Please sign in to comment.