summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorlalottacontinua <loddatio@posteo.de>2018-06-27 22:44:33 +0200
committerlalottacontinua <loddatio@posteo.de>2018-06-27 22:44:33 +0200
commitd63dd4b36701f2f993e671f8cc3c5424b787e43a (patch)
tree486a8c3a1a688e76c7da7f5d9f8dfddbcc0b58aa /app/src/main/java
parent277f0f12ca505797daec42a5211885ac07b909f9 (diff)
parent8c9879145e588e985e5305ee2f88dfc1b6bf1d10 (diff)
Merge branch 'master' into add_provider
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Constants.java1
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipFragment.java38
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java14
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/MainActivity.java9
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Provider.java4
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java16
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderManager.java47
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/StartActivity.java10
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/TLSCompatSocketFactory.java1
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java19
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java2
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java4
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java2
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java3
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java170
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java29
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java22
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java21
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java78
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java (renamed from app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java)230
20 files changed, 436 insertions, 284 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java
index 7fa09441..2efc2c1f 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Constants.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java
@@ -100,6 +100,7 @@ public interface Constants {
String DONATION_URL = TextUtils.isEmpty(BuildConfig.donation_url) ?
BuildConfig.default_donation_url : BuildConfig.donation_url;
String LAST_DONATION_REMINDER_DATE = "last_donation_reminder_date";
+ String FIRST_TIME_USER_DATE = "first_time_user_date";
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
index 535322e5..51f787b7 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
@@ -58,6 +58,7 @@ import de.blinkt.openvpn.core.OpenVPNService;
import de.blinkt.openvpn.core.ProfileManager;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.eip.EipStatus;
+import se.leap.bitmaskclient.utils.DateHelper;
import se.leap.bitmaskclient.views.VpnStateImage;
import static android.view.View.GONE;
@@ -65,6 +66,7 @@ import static android.view.View.VISIBLE;
import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK;
import static se.leap.bitmaskclient.Constants.DONATION_REMINDER_DURATION;
import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
+import static se.leap.bitmaskclient.Constants.FIRST_TIME_USER_DATE;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER;
@@ -530,7 +532,8 @@ public class EipFragment extends Fragment implements Observer {
}).setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
- saveLastDonationReminderDate();
+ preferences.edit().putString(LAST_DONATION_REMINDER_DATE,
+ DateHelper.getCurrentDateString()).apply();
}
}).show();
}
@@ -545,29 +548,32 @@ public class EipFragment extends Fragment implements Observer {
return false;
}
- String lastDonationReminderDate = preferences.getString(LAST_DONATION_REMINDER_DATE, null);
- if (lastDonationReminderDate == null) {
- return true;
+ String firstTimeUserDate = preferences.getString(FIRST_TIME_USER_DATE, null);
+ if (firstTimeUserDate == null) {
+ preferences.edit().putString(FIRST_TIME_USER_DATE, DateHelper.getCurrentDateString()).apply();
+ return false;
}
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
- Date lastDate;
try {
- lastDate = sdf.parse(lastDonationReminderDate);
+ long diffDays;
+
+ diffDays = DateHelper.getDateDiffToCurrentDateInDays(firstTimeUserDate);
+ if (diffDays < 1) {
+ return false;
+ }
+
+ String lastDonationReminderDate = preferences.getString(LAST_DONATION_REMINDER_DATE, null);
+ if (lastDonationReminderDate == null) {
+ return true;
+ }
+ diffDays = DateHelper.getDateDiffToCurrentDateInDays(lastDonationReminderDate);
+ return diffDays >= DONATION_REMINDER_DURATION;
+
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
return false;
}
-
- Date currentDate = new Date();
- long diffDays = (currentDate.getTime() - lastDate.getTime()) / ONE_DAY;
- return diffDays >= DONATION_REMINDER_DURATION;
}
- private void saveLastDonationReminderDate() {
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
- Date lastDate = new Date();
- preferences.edit().putString(LAST_DONATION_REMINDER_DATE, sdf.format(lastDate)).apply();
- }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java
index 3a1fd6e0..d1f1ed21 100644
--- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java
+++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java
@@ -17,12 +17,16 @@
package se.leap.bitmaskclient;
-import org.jboss.security.srp.*;
+import org.jboss.security.srp.SRPParameters;
-import java.io.*;
-import java.math.*;
-import java.security.*;
-import java.util.*;
+import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.Arrays;
+
+import se.leap.bitmaskclient.utils.ConfigHelper;
/**
* Implements all SRP algorithm logic.
diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
index 868d2876..c44e8a3e 100644
--- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
+++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
@@ -38,6 +38,7 @@ import org.json.JSONObject;
import se.leap.bitmaskclient.drawer.NavigationDrawerFragment;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.fragments.LogFragment;
+import se.leap.bitmaskclient.utils.ConfigHelper;
import static android.content.Intent.CATEGORY_DEFAULT;
import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT;
@@ -61,6 +62,8 @@ import static se.leap.bitmaskclient.ProviderAPI.INCORRECTLY_UPDATED_INVALID_VPN_
import static se.leap.bitmaskclient.ProviderAPI.USER_MESSAGE;
import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed;
import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.storeProviderInPreferences;
public class MainActivity extends AppCompatActivity {
@@ -92,7 +95,7 @@ public class MainActivity extends AppCompatActivity {
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
- provider = ConfigHelper.getSavedProviderFromSharedPreferences(preferences);
+ provider = getSavedProviderFromSharedPreferences(preferences);
// Set up the drawer.
navigationDrawerFragment.setUp(
@@ -179,7 +182,7 @@ public class MainActivity extends AppCompatActivity {
return;
}
- ConfigHelper.storeProviderInPreferences(preferences, provider);
+ storeProviderInPreferences(preferences, provider);
navigationDrawerFragment.refresh();
switch (requestCode) {
@@ -296,7 +299,7 @@ public class MainActivity extends AppCompatActivity {
case CORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE:
provider = resultData.getParcelable(PROVIDER_KEY);
- ConfigHelper.storeProviderInPreferences(preferences, provider);
+ storeProviderInPreferences(preferences, provider);
EipCommand.startVPN(this, true);
break;
case INCORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE:
diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java
index fd067bf9..5e5a3a62 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Provider.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java
@@ -140,7 +140,7 @@ public final class Provider implements Parcelable {
return definition;
}
- String getDefinitionString() {
+ public String getDefinitionString() {
return getDefinition().toString();
}
@@ -148,7 +148,7 @@ public final class Provider implements Parcelable {
return mainUrl.getDomain();
}
- String getMainUrlString() {
+ public String getMainUrlString() {
return getMainUrl().toString();
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java
index 753172e6..8f3acf1d 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java
+++ b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java
@@ -52,8 +52,9 @@ import javax.net.ssl.SSLHandshakeException;
import okhttp3.OkHttpClient;
import se.leap.bitmaskclient.Constants.CREDENTIAL_ERRORS;
+import se.leap.bitmaskclient.utils.ConfigHelper;
-import static se.leap.bitmaskclient.ConfigHelper.getFingerprintFromCertificate;
+import static se.leap.bitmaskclient.utils.ConfigHelper.getFingerprintFromCertificate;
import static se.leap.bitmaskclient.Constants.BROADCAST_PROVIDER_API_EVENT;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
@@ -106,6 +107,9 @@ import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid;
import static se.leap.bitmaskclient.R.string.warning_corrupted_provider_cert;
import static se.leap.bitmaskclient.R.string.warning_corrupted_provider_details;
import static se.leap.bitmaskclient.R.string.warning_expired_provider_cert;
+import static se.leap.bitmaskclient.utils.ConfigHelper.parseRsaKeyFromString;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.deleteProviderDetailsFromPreferences;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getFromPersistedProvider;
/**
* Implements the logic of the http api calls. The methods of this class needs to be called from
@@ -229,7 +233,7 @@ public abstract class ProviderApiManagerBase {
void resetProviderDetails(Provider provider) {
provider.reset();
- ConfigHelper.deleteProviderDetailsFromPreferences(preferences, provider.getDomain());
+ deleteProviderDetailsFromPreferences(preferences, provider.getDomain());
}
String formatErrorMessage(final int toastStringId) {
@@ -765,16 +769,16 @@ public abstract class ProviderApiManagerBase {
}
protected String getPersistedPrivateKey(String providerDomain) {
- return ConfigHelper.getFromPersistedProvider(PROVIDER_PRIVATE_KEY, providerDomain, preferences);
+ return getFromPersistedProvider(PROVIDER_PRIVATE_KEY, providerDomain, preferences);
}
protected String getPersistedVPNCertificate(String providerDomain) {
- return ConfigHelper.getFromPersistedProvider(PROVIDER_VPN_CERTIFICATE, providerDomain, preferences);
+ return getFromPersistedProvider(PROVIDER_VPN_CERTIFICATE, providerDomain, preferences);
}
protected JSONObject getPersistedProviderDefinition(String providerDomain) {
try {
- return new JSONObject(ConfigHelper.getFromPersistedProvider(Provider.KEY, providerDomain, preferences));
+ return new JSONObject(getFromPersistedProvider(Provider.KEY, providerDomain, preferences));
} catch (JSONException e) {
e.printStackTrace();
return new JSONObject();
@@ -860,7 +864,7 @@ public abstract class ProviderApiManagerBase {
}
}
- RSAPrivateKey key = ConfigHelper.parseRsaKeyFromString(keyString);
+ RSAPrivateKey key = parseRsaKeyFromString(keyString);
keyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);
provider.setPrivateKey( "-----BEGIN RSA PRIVATE KEY-----\n" + keyString + "-----END RSA PRIVATE KEY-----");
diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
index 97ba3b98..a36c2dec 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
@@ -1,6 +1,7 @@
package se.leap.bitmaskclient;
import android.content.res.AssetManager;
+import android.support.annotation.VisibleForTesting;
import com.pedrogomez.renderers.AdapteeCollection;
@@ -8,9 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@@ -22,6 +21,11 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import static se.leap.bitmaskclient.utils.FileHelper.createFile;
+import static se.leap.bitmaskclient.utils.FileHelper.persistFile;
+import static se.leap.bitmaskclient.utils.InputStreamHelper.getInputStreamFrom;
+import static se.leap.bitmaskclient.utils.InputStreamHelper.loadInputStreamAsString;
+
/**
* Created by parmegv on 4/12/14.
*/
@@ -37,6 +41,8 @@ public class ProviderManager implements AdapteeCollection<Provider> {
private static ProviderManager instance;
final private static String URLS = "urls";
+ final private static String EXT_JSON = ".json";
+ final private static String EXT_PEM = ".pem";
public static ProviderManager getInstance(AssetManager assetsManager, File externalFilesDir) {
if (instance == null)
@@ -45,6 +51,11 @@ public class ProviderManager implements AdapteeCollection<Provider> {
return instance;
}
+ @VisibleForTesting
+ static void reset() {
+ instance = null;
+ }
+
private ProviderManager(AssetManager assetManager, File externalFilesDir) {
this.assetsManager = assetManager;
addDefaultProviders(assetManager);
@@ -79,8 +90,8 @@ public class ProviderManager implements AdapteeCollection<Provider> {
String provider = file.substring(0, file.length() - ".url".length());
InputStream provider_file = assetsManager.open(directory + "/" + file);
mainUrl = extractMainUrlFromInputStream(provider_file);
- certificate = ConfigHelper.loadInputStreamAsString(assetsManager.open(provider + ".pem"));
- providerDefinition = ConfigHelper.loadInputStreamAsString(assetsManager.open(provider + ".json"));
+ certificate = loadInputStreamAsString(assetsManager.open(provider + EXT_PEM));
+ providerDefinition = loadInputStreamAsString(assetsManager.open(provider + EXT_JSON));
} catch (IOException e) {
e.printStackTrace();
}
@@ -107,7 +118,7 @@ public class ProviderManager implements AdapteeCollection<Provider> {
Set<Provider> providers = new HashSet<>();
try {
for (String file : files) {
- String mainUrl = extractMainUrlFromInputStream(ConfigHelper.getInputStreamFrom(externalFilesDir.getAbsolutePath() + "/" + file));
+ String mainUrl = extractMainUrlFromInputStream(getInputStreamFrom(externalFilesDir.getAbsolutePath() + "/" + file));
providers.add(new Provider(new URL(mainUrl)));
}
} catch (MalformedURLException | FileNotFoundException e) {
@@ -219,19 +230,33 @@ public class ProviderManager implements AdapteeCollection<Provider> {
defaultProviderURLs.clear();
}
- //FIXME: removed custom providers should be deleted here as well
void saveCustomProvidersToFile() {
try {
+ deleteLegacyCustomProviders();
+
for (Provider provider : customProviders) {
- File providerFile = new File(externalFilesDir, provider.getName() + ".json");
+ File providerFile = createFile(externalFilesDir, provider.getName() + EXT_JSON);
if (!providerFile.exists()) {
- FileWriter writer = new FileWriter(providerFile);
- writer.write(provider.toJson().toString());
- writer.close();
+ persistFile(providerFile, provider.toJson().toString());
}
}
- } catch (IOException e) {
+ } catch (IOException | SecurityException e) {
e.printStackTrace();
}
}
+
+ /**
+ * Deletes persisted custom providers from from internal storage that are not in customProviders list anymore
+ */
+ private void deleteLegacyCustomProviders() throws IOException, SecurityException {
+ Set<Provider> persistedCustomProviders = externalFilesDir != null && externalFilesDir.isDirectory() ?
+ providersFromFiles(externalFilesDir.list()) : new HashSet<Provider>();
+ persistedCustomProviders.removeAll(customProviders);
+ for (Provider providerToDelete : persistedCustomProviders) {
+ File providerFile = createFile(externalFilesDir, providerToDelete.getName() + EXT_JSON);
+ if (providerFile.exists()) {
+ providerFile.delete();
+ }
+ }
+ }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/StartActivity.java b/app/src/main/java/se/leap/bitmaskclient/StartActivity.java
index 6bbdeb4f..33c13b90 100644
--- a/app/src/main/java/se/leap/bitmaskclient/StartActivity.java
+++ b/app/src/main/java/se/leap/bitmaskclient/StartActivity.java
@@ -15,6 +15,7 @@ import java.lang.annotation.RetentionPolicy;
import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.userstatus.User;
+import se.leap.bitmaskclient.utils.ConfigHelper;
import static se.leap.bitmaskclient.Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE;
import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
@@ -24,6 +25,9 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.MainActivity.ACTION_SHOW_VPN_FRAGMENT;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.providerInSharedPreferences;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.storeProviderInPreferences;
/**
* Activity shown at startup. Evaluates if App is started for the first time or has been upgraded
@@ -154,9 +158,9 @@ public class StartActivity extends Activity{
}
private void prepareEIP() {
- boolean provider_exists = ConfigHelper.providerInSharedPreferences(preferences);
+ boolean provider_exists = providerInSharedPreferences(preferences);
if (provider_exists) {
- Provider provider = ConfigHelper.getSavedProviderFromSharedPreferences(preferences);
+ Provider provider = getSavedProviderFromSharedPreferences(preferences);
if(!provider.isConfigured()) {
configureLeapProvider();
} else {
@@ -186,7 +190,7 @@ public class StartActivity extends Activity{
if (requestCode == REQUEST_CODE_CONFIGURE_LEAP) {
if (resultCode == RESULT_OK && data != null && data.hasExtra(Provider.KEY)) {
Provider provider = data.getParcelableExtra(Provider.KEY);
- ConfigHelper.storeProviderInPreferences(preferences, provider);
+ storeProviderInPreferences(preferences, provider);
EipCommand.startVPN(this, false);
showMainActivity();
} else if (resultCode == RESULT_CANCELED) {
diff --git a/app/src/main/java/se/leap/bitmaskclient/TLSCompatSocketFactory.java b/app/src/main/java/se/leap/bitmaskclient/TLSCompatSocketFactory.java
index 76d38447..cca75bdf 100644
--- a/app/src/main/java/se/leap/bitmaskclient/TLSCompatSocketFactory.java
+++ b/app/src/main/java/se/leap/bitmaskclient/TLSCompatSocketFactory.java
@@ -22,6 +22,7 @@ import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
+import se.leap.bitmaskclient.utils.ConfigHelper;
/**
* Created by cyberta on 24.10.17.
diff --git a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java
index be4bdf99..6e9879dd 100644
--- a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java
@@ -50,7 +50,7 @@ import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
-import se.leap.bitmaskclient.ConfigHelper;
+import se.leap.bitmaskclient.utils.ConfigHelper;
import se.leap.bitmaskclient.DrawerSettingsAdapter;
import se.leap.bitmaskclient.DrawerSettingsAdapter.DrawerSettingsItem;
import se.leap.bitmaskclient.EipFragment;
@@ -64,8 +64,6 @@ import se.leap.bitmaskclient.fragments.LogFragment;
import static android.content.Context.MODE_PRIVATE;
import static se.leap.bitmaskclient.BitmaskApp.getRefWatcher;
-import static se.leap.bitmaskclient.ConfigHelper.getSaveBattery;
-import static se.leap.bitmaskclient.ConfigHelper.getShowAlwaysOnDialog;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
@@ -82,6 +80,11 @@ import static se.leap.bitmaskclient.DrawerSettingsAdapter.SWITCH_PROVIDER;
import static se.leap.bitmaskclient.R.string.about_fragment_title;
import static se.leap.bitmaskclient.R.string.log_fragment_title;
import static se.leap.bitmaskclient.R.string.switch_provider_menu_option;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getProviderName;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getSaveBattery;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.getShowAlwaysOnDialog;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.saveBattery;
import static se.leap.bitmaskclient.R.string.donate_title;
/**
@@ -383,7 +386,7 @@ public class NavigationDrawerFragment extends Fragment {
DrawerSettingsItem item = settingsListAdapter.getDrawerItem(BATTERY_SAVER);
item.setChecked(true);
settingsListAdapter.notifyDataSetChanged();
- ConfigHelper.saveBattery(getContext(), item.isChecked());
+ saveBattery(getContext(), item.isChecked());
}
})
.setNegativeButton(activity.getString(android.R.string.no), new DialogInterface.OnClickListener() {
@@ -467,14 +470,14 @@ public class NavigationDrawerFragment extends Fragment {
private void onSwitchItemSelected(int elementType, boolean newStateIsChecked) {
switch (elementType) {
case BATTERY_SAVER:
- if (ConfigHelper.getSaveBattery(getContext()) == newStateIsChecked) {
+ if (getSaveBattery(getContext()) == newStateIsChecked) {
//initial ui setup, ignore
return;
}
if (newStateIsChecked) {
showExperimentalFeatureAlert();
} else {
- ConfigHelper.saveBattery(this.getContext(), false);
+ saveBattery(this.getContext(), false);
disableSwitch(BATTERY_SAVER);
}
break;
@@ -500,7 +503,7 @@ public class NavigationDrawerFragment extends Fragment {
fragment = new EipFragment();
fragmentTag = EipFragment.TAG;
Bundle arguments = new Bundle();
- Provider currentProvider = ConfigHelper.getSavedProviderFromSharedPreferences(preferences);
+ Provider currentProvider = getSavedProviderFromSharedPreferences(preferences);
arguments.putParcelable(PROVIDER_KEY, currentProvider);
fragment.setArguments(arguments);
} else {
@@ -562,7 +565,7 @@ public class NavigationDrawerFragment extends Fragment {
private void refreshAccountListAdapter() {
accountListAdapter.clear();
- String providerName = ConfigHelper.getProviderName(preferences);
+ String providerName = getProviderName(preferences);
if (providerName == null) {
//TODO: ADD A header to the ListView containing a useful message.
//TODO 2: disable switchProvider
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
index 159bc9a7..971d973f 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
@@ -53,7 +53,6 @@ import se.leap.bitmaskclient.OnBootReceiver;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.content.Intent.CATEGORY_DEFAULT;
-import static se.leap.bitmaskclient.ConfigHelper.ensureNotOnMainThread;
import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
@@ -71,6 +70,7 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.ERROR_INVALID_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid;
+import static se.leap.bitmaskclient.utils.ConfigHelper.ensureNotOnMainThread;
/**
* EIP is the abstract base class for interacting with and managing the Encrypted
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
index a04ede08..5b4db5af 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
@@ -35,8 +35,8 @@ import java.util.List;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.Connection;
import de.blinkt.openvpn.core.ProfileManager;
-import se.leap.bitmaskclient.ConfigHelper;
import se.leap.bitmaskclient.Provider;
+import se.leap.bitmaskclient.utils.PreferenceHelper;
import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY;
import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
@@ -216,7 +216,7 @@ public class GatewaysManager {
// to add all gateways from prefs without duplicates, but this should be refactored.
clearGatewaysAndProfiles();
fromEipServiceJson(
- ConfigHelper.getEipDefinitionFromPreferences(preferences)
+ PreferenceHelper.getEipDefinitionFromPreferences(preferences)
);
}
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java
index 03dd9d05..83904729 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java
@@ -22,7 +22,7 @@ import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Date;
-import se.leap.bitmaskclient.ConfigHelper;
+import se.leap.bitmaskclient.utils.ConfigHelper;
public class VpnCertificateValidator {
public final static String TAG = VpnCertificateValidator.class.getSimpleName();
diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java b/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
index 3558f378..e3d004f5 100644
--- a/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
+++ b/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
@@ -17,7 +17,8 @@ import butterknife.InjectView;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.views.IconTextView;
-import static se.leap.bitmaskclient.ConfigHelper.saveShowAlwaysOnDialog;
+import static se.leap.bitmaskclient.utils.PreferenceHelper.saveShowAlwaysOnDialog;
+
/**
* Created by cyberta on 25.02.18.
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java
new file mode 100644
index 00000000..5bb637b7
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java
@@ -0,0 +1,170 @@
+/**
+ * Copyright (c) 2013 LEAP Encryption Access Project and contributers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package se.leap.bitmaskclient.utils;
+
+import android.content.Context;
+import android.os.Looper;
+import android.support.annotation.NonNull;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.spongycastle.util.encoders.Base64;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+import java.security.KeyFactory;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+
+import se.leap.bitmaskclient.ProviderAPI;
+
+/**
+ * Stores constants, and implements auxiliary methods used across all Bitmask Android classes.
+ *
+ * @author parmegv
+ * @author MeanderingCode
+ */
+public class ConfigHelper {
+ final public static String NG_1024 =
+ "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
+ final public static BigInteger G = new BigInteger("2");
+
+ public static boolean checkErroneousDownload(String downloadedString) {
+ try {
+ if (downloadedString == null || downloadedString.isEmpty() || new JSONObject(downloadedString).has(ProviderAPI.ERRORS) || new JSONObject(downloadedString).has(ProviderAPI.BACKEND_ERROR_KEY)) {
+ return true;
+ } else {
+ return false;
+ }
+ } catch (NullPointerException | JSONException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Treat the input as the MSB representation of a number,
+ * and lop off leading zero elements. For efficiency, the
+ * input is simply returned if no leading zeroes are found.
+ *
+ * @param in array to be trimmed
+ */
+ public static byte[] trim(byte[] in) {
+ if (in.length == 0 || in[0] != 0)
+ return in;
+
+ int len = in.length;
+ int i = 1;
+ while (in[i] == 0 && i < len)
+ ++i;
+ byte[] ret = new byte[len - i];
+ System.arraycopy(in, i, ret, 0, len - i);
+ return ret;
+ }
+
+ public static X509Certificate parseX509CertificateFromString(String certificateString) {
+ java.security.cert.Certificate certificate = null;
+ CertificateFactory cf;
+ try {
+ cf = CertificateFactory.getInstance("X.509");
+
+ certificateString = certificateString.replaceFirst("-----BEGIN CERTIFICATE-----", "").replaceFirst("-----END CERTIFICATE-----", "").trim();
+ byte[] cert_bytes = Base64.decode(certificateString);
+ InputStream caInput = new ByteArrayInputStream(cert_bytes);
+ try {
+ certificate = cf.generateCertificate(caInput);
+ System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN());
+ } finally {
+ caInput.close();
+ }
+ } catch (NullPointerException | CertificateException | IOException | IllegalArgumentException e) {
+ return null;
+ }
+ return (X509Certificate) certificate;
+ }
+
+ public static RSAPrivateKey parseRsaKeyFromString(String rsaKeyString) {
+ RSAPrivateKey key;
+ try {
+ KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
+ rsaKeyString = rsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", "");
+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(rsaKeyString));
+ key = (RSAPrivateKey) kf.generatePrivate(keySpec);
+ } catch (InvalidKeySpecException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ } catch (NoSuchAlgorithmException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ } catch (NoSuchProviderException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ } catch (NullPointerException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ return key;
+ }
+
+ private static String byteArrayToHex(byte[] input) {
+ int readBytes = input.length;
+ StringBuffer hexData = new StringBuffer();
+ int onebyte;
+ for (int i = 0; i < readBytes; i++) {
+ onebyte = ((0x000000ff & input[i]) | 0xffffff00);
+ hexData.append(Integer.toHexString(onebyte).substring(6));
+ }
+ return hexData.toString();
+ }
+
+ /**
+ * Calculates the hexadecimal representation of a sha256/sha1 fingerprint of a certificate
+ *
+ * @param certificate
+ * @param encoding
+ * @return
+ * @throws NoSuchAlgorithmException
+ * @throws CertificateEncodingException
+ */
+ @NonNull
+ public static String getFingerprintFromCertificate(X509Certificate certificate, String encoding) throws NoSuchAlgorithmException, CertificateEncodingException /*, UnsupportedEncodingException*/ {
+ byte[] byteArray = MessageDigest.getInstance(encoding).digest(certificate.getEncoded());
+ return byteArrayToHex(byteArray);
+ }
+
+ public static void ensureNotOnMainThread(@NonNull Context context) throws IllegalStateException{
+ Looper looper = Looper.myLooper();
+ if (looper != null && looper == context.getMainLooper()) {
+ throw new IllegalStateException(
+ "calling this from your main thread can lead to deadlock");
+ }
+ }
+
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java
new file mode 100644
index 00000000..523c8c4c
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java
@@ -0,0 +1,29 @@
+package se.leap.bitmaskclient.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+/**
+ * Contains helper methods related to date manipulation.
+ *
+ * @author Janak
+ */
+public class DateHelper {
+ private static final String DATE_PATTERN = "dd/MM/yyyy";
+ private static final int ONE_DAY = 86400000; //1000*60*60*24
+
+ public static long getDateDiffToCurrentDateInDays(String startDate) throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
+ Date lastDate = sdf.parse(startDate);
+ Date currentDate = new Date();
+ return (currentDate.getTime() - lastDate.getTime()) / ONE_DAY;
+ }
+
+ public static String getCurrentDateString() {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
+ Date lastDate = new Date();
+ return sdf.format(lastDate);
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java
new file mode 100644
index 00000000..1c3e1ebb
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java
@@ -0,0 +1,22 @@
+package se.leap.bitmaskclient.utils;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * Created by cyberta on 18.03.18.
+ */
+
+public class FileHelper {
+ public static File createFile(File dir, String fileName) {
+ return new File(dir, fileName);
+ }
+
+ public static void persistFile(File file, String content) throws IOException {
+ FileWriter writer = new FileWriter(file);
+ writer.write(content);
+ writer.close();
+ }
+
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java
new file mode 100644
index 00000000..87996615
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java
@@ -0,0 +1,21 @@
+package se.leap.bitmaskclient.utils;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+/**
+ * Created by cyberta on 18.03.18.
+ */
+
+public class InputStreamHelper {
+ //allows us to mock FileInputStream
+ public static InputStream getInputStreamFrom(String filePath) throws FileNotFoundException {
+ return new FileInputStream(filePath);
+ }
+
+ public static String loadInputStreamAsString(InputStream is) {
+ java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
+ return s.hasNext() ? s.next() : "";
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java
new file mode 100644
index 00000000..48d4cbad
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java
@@ -0,0 +1,78 @@
+package se.leap.bitmaskclient.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+
+/**
+ * Created by cyberta on 18.03.18.
+ */
+
+public class KeyStoreHelper {
+ private static KeyStore trustedKeystore;
+
+ /**
+ * Adds a new X509 certificate given its input stream and its provider name
+ *
+ * @param provider used to store the certificate in the keystore
+ * @param inputStream from which X509 certificate must be generated.
+ */
+ public static void addTrustedCertificate(String provider, InputStream inputStream) {
+ CertificateFactory cf;
+ try {
+ cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert =
+ (X509Certificate) cf.generateCertificate(inputStream);
+ trustedKeystore.setCertificateEntry(provider, cert);
+ } catch (CertificateException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (KeyStoreException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Adds a new X509 certificate given in its string from and using its provider name
+ *
+ * @param provider used to store the certificate in the keystore
+ * @param certificate
+ */
+ public static void addTrustedCertificate(String provider, String certificate) {
+
+ try {
+ X509Certificate cert = ConfigHelper.parseX509CertificateFromString(certificate);
+ if (trustedKeystore == null) {
+ trustedKeystore = KeyStore.getInstance("BKS");
+ trustedKeystore.load(null);
+ }
+ trustedKeystore.setCertificateEntry(provider, cert);
+ } catch (KeyStoreException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (NoSuchAlgorithmException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (CertificateException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * @return class wide keystore
+ */
+ public static KeyStore getKeystore() {
+ return trustedKeystore;
+ }
+
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
index bfc77261..12015dfb 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
@@ -1,20 +1,4 @@
-/**
- * Copyright (c) 2013 LEAP Encryption Access Project and contributers
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package se.leap.bitmaskclient;
+package se.leap.bitmaskclient.utils;
import android.content.Context;
import android.content.SharedPreferences;
@@ -25,7 +9,6 @@ import android.support.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
-import org.spongycastle.util.encoders.Base64;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
@@ -35,24 +18,13 @@ import java.io.InputStream;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
-import java.security.KeyFactory;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.security.cert.CertificateEncodingException;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.security.interfaces.RSAPrivateKey;
-import java.security.spec.InvalidKeySpecException;
-import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import se.leap.bitmaskclient.Provider;
+
import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG;
import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER;
import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION;
@@ -63,195 +35,10 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
/**
- * Stores constants, and implements auxiliary methods used across all Bitmask Android classes.
- *
- * @author parmegv
- * @author MeanderingCode
+ * Created by cyberta on 18.03.18.
*/
-public class ConfigHelper {
- private static final String TAG = ConfigHelper.class.getName();
- private static KeyStore keystore_trusted;
-
- final public static String NG_1024 =
- "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
- final public static BigInteger G = new BigInteger("2");
-
- public static boolean checkErroneousDownload(String downloadedString) {
- try {
- if (downloadedString == null || downloadedString.isEmpty() || new JSONObject(downloadedString).has(ProviderAPI.ERRORS) || new JSONObject(downloadedString).has(ProviderAPI.BACKEND_ERROR_KEY)) {
- return true;
- } else {
- return false;
- }
- } catch (NullPointerException | JSONException e) {
- return false;
- }
- }
-
- /**
- * Treat the input as the MSB representation of a number,
- * and lop off leading zero elements. For efficiency, the
- * input is simply returned if no leading zeroes are found.
- *
- * @param in array to be trimmed
- */
- public static byte[] trim(byte[] in) {
- if (in.length == 0 || in[0] != 0)
- return in;
-
- int len = in.length;
- int i = 1;
- while (in[i] == 0 && i < len)
- ++i;
- byte[] ret = new byte[len - i];
- System.arraycopy(in, i, ret, 0, len - i);
- return ret;
- }
-
- public static X509Certificate parseX509CertificateFromString(String certificateString) {
- java.security.cert.Certificate certificate = null;
- CertificateFactory cf;
- try {
- cf = CertificateFactory.getInstance("X.509");
-
- certificateString = certificateString.replaceFirst("-----BEGIN CERTIFICATE-----", "").replaceFirst("-----END CERTIFICATE-----", "").trim();
- byte[] cert_bytes = Base64.decode(certificateString);
- InputStream caInput = new ByteArrayInputStream(cert_bytes);
- try {
- certificate = cf.generateCertificate(caInput);
- System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN());
- } finally {
- caInput.close();
- }
- } catch (NullPointerException | CertificateException | IOException | IllegalArgumentException e) {
- return null;
- }
- return (X509Certificate) certificate;
- }
-
- public static String loadInputStreamAsString(java.io.InputStream is) {
- java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
- return s.hasNext() ? s.next() : "";
- }
-
- //allows us to mock FileInputStream
- public static InputStream getInputStreamFrom(String filePath) throws FileNotFoundException {
- return new FileInputStream(filePath);
- }
-
- protected static RSAPrivateKey parseRsaKeyFromString(String rsaKeyString) {
- RSAPrivateKey key;
- try {
- KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
- rsaKeyString = rsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", "");
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(rsaKeyString));
- key = (RSAPrivateKey) kf.generatePrivate(keySpec);
- } catch (InvalidKeySpecException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- } catch (NoSuchAlgorithmException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- } catch (NoSuchProviderException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- } catch (NullPointerException e) {
- e.printStackTrace();
- return null;
- }
-
- return key;
- }
-
- private static String byteArrayToHex(byte[] input) {
- int readBytes = input.length;
- StringBuffer hexData = new StringBuffer();
- int onebyte;
- for (int i = 0; i < readBytes; i++) {
- onebyte = ((0x000000ff & input[i]) | 0xffffff00);
- hexData.append(Integer.toHexString(onebyte).substring(6));
- }
- return hexData.toString();
- }
-
- /**
- * Calculates the hexadecimal representation of a sha256/sha1 fingerprint of a certificate
- *
- * @param certificate
- * @param encoding
- * @return
- * @throws NoSuchAlgorithmException
- * @throws CertificateEncodingException
- */
- @NonNull
- public static String getFingerprintFromCertificate(X509Certificate certificate, String encoding) throws NoSuchAlgorithmException, CertificateEncodingException /*, UnsupportedEncodingException*/ {
- byte[] byteArray = MessageDigest.getInstance(encoding).digest(certificate.getEncoded());
- return byteArrayToHex(byteArray);
- }
-
- /**
- * Adds a new X509 certificate given its input stream and its provider name
- *
- * @param provider used to store the certificate in the keystore
- * @param inputStream from which X509 certificate must be generated.
- */
- public static void addTrustedCertificate(String provider, InputStream inputStream) {
- CertificateFactory cf;
- try {
- cf = CertificateFactory.getInstance("X.509");
- X509Certificate cert =
- (X509Certificate) cf.generateCertificate(inputStream);
- keystore_trusted.setCertificateEntry(provider, cert);
- } catch (CertificateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (KeyStoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /**
- * Adds a new X509 certificate given in its string from and using its provider name
- *
- * @param provider used to store the certificate in the keystore
- * @param certificate
- */
- public static void addTrustedCertificate(String provider, String certificate) {
-
- try {
- X509Certificate cert = ConfigHelper.parseX509CertificateFromString(certificate);
- if (keystore_trusted == null) {
- keystore_trusted = KeyStore.getInstance("BKS");
- keystore_trusted.load(null);
- }
- keystore_trusted.setCertificateEntry(provider, cert);
- } catch (KeyStoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (CertificateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /**
- * @return class wide keystore
- */
- public static KeyStore getKeystore() {
- return keystore_trusted;
- }
-
+public class PreferenceHelper {
public static boolean providerInSharedPreferences(@NonNull SharedPreferences preferences) {
return preferences.getBoolean(PROVIDER_CONFIGURED, false);
}
@@ -452,11 +239,4 @@ public class ConfigHelper {
return result;
}
- public static void ensureNotOnMainThread(@NonNull Context context) throws IllegalStateException{
- Looper looper = Looper.myLooper();
- if (looper != null && looper == context.getMainLooper()) {
- throw new IllegalStateException(
- "calling this from your main thread can lead to deadlock");
- }
- }
}