diff options
Diffstat (limited to 'app/src')
10 files changed, 243 insertions, 161 deletions
diff --git a/app/src/insecure/java/se/leap/bitmaskclient/ProviderApiManager.java b/app/src/insecure/java/se/leap/bitmaskclient/ProviderApiManager.java index dfc98ffb..d57dfe6d 100644 --- a/app/src/insecure/java/se/leap/bitmaskclient/ProviderApiManager.java +++ b/app/src/insecure/java/se/leap/bitmaskclient/ProviderApiManager.java @@ -171,9 +171,15 @@ public class ProviderApiManager extends ProviderApiManagerBase { eipServiceJsonString = downloadWithProviderCA(provider.getCaCert(), eipServiceUrl, lastDangerOn); JSONObject eipServiceJson = new JSONObject(eipServiceJsonString); - provider.setEipServiceJson(eipServiceJson); - result.putBoolean(BROADCAST_RESULT_KEY, true); + if (eipServiceJson.has(ERRORS)) { + String reasonToFail = pickErrorMessage(eipServiceJsonString); + result.putString(ERRORS, reasonToFail); + result.putBoolean(BROADCAST_RESULT_KEY, false); + } else{ + provider.setEipServiceJson(eipServiceJson); + result.putBoolean(BROADCAST_RESULT_KEY, true); + } } catch (NullPointerException | JSONException e) { String reasonToFail = pickErrorMessage(eipServiceJsonString); result.putString(ERRORS, reasonToFail); @@ -201,7 +207,7 @@ public class ProviderApiManager extends ProviderApiManagerBase { if (certString == null || certString.isEmpty() || ConfigHelper.checkErroneousDownload(certString)) return false; else - return loadCertificate(certString); + return loadCertificate(provider, certString); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java index 238f0ea1..329fd543 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java @@ -56,6 +56,8 @@ import static android.R.attr.name; import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION; import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED; import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; +import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; /** * Stores constants, and implements auxiliary methods used across all Bitmask Android classes. @@ -279,6 +281,8 @@ public class ConfigHelper { provider.setMainUrl(new URL(preferences.getString(Provider.MAIN_URL, ""))); provider.define(new JSONObject(preferences.getString(Provider.KEY, ""))); provider.setCaCert(preferences.getString(Provider.CA_CERT, "")); + provider.setVpnCertificate(preferences.getString(PROVIDER_VPN_CERTIFICATE, "")); + provider.setPrivateKey(preferences.getString(PROVIDER_PRIVATE_KEY, "")); } catch (MalformedURLException | JSONException e) { e.printStackTrace(); } @@ -350,12 +354,15 @@ public class ConfigHelper { } // TODO: replace commit with apply after refactoring EIP + //FIXME: don't save private keys in shared preferences! use the keystore public static void storeProviderInPreferences(SharedPreferences preferences, Provider provider) { preferences.edit().putBoolean(PROVIDER_CONFIGURED, true). putString(Provider.MAIN_URL, provider.getMainUrlString()). putString(Provider.KEY, provider.getDefinitionString()). putString(Provider.CA_CERT, provider.getCaCert()). putString(PROVIDER_EIP_DEFINITION, provider.getEipServiceJsonString()). + putString(PROVIDER_PRIVATE_KEY, provider.getPrivateKey()). + putString(PROVIDER_VPN_CERTIFICATE, provider.getVpnCertificate()). commit(); String providerDomain = provider.getDomain(); @@ -364,6 +371,8 @@ public class ConfigHelper { putString(Provider.KEY + "." + providerDomain, provider.getDefinitionString()). putString(Provider.CA_CERT + "." + providerDomain, provider.getCaCert()). putString(PROVIDER_EIP_DEFINITION + "." + providerDomain, provider.getEipServiceJsonString()). + putString(PROVIDER_PRIVATE_KEY + "." + providerDomain, provider.getPrivateKey()). + putString(PROVIDER_VPN_CERTIFICATE + "." + providerDomain, provider.getVpnCertificate()). apply(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index ceae6706..b4c7a7de 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -29,12 +29,11 @@ import android.content.SharedPreferences; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.os.Bundle; -import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; -import android.os.ResultReceiver; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.widget.AppCompatImageView; import android.util.Log; import android.view.LayoutInflater; @@ -56,13 +55,14 @@ import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.eip.EipStatus; import se.leap.bitmaskclient.eip.VoidVpnService; -import se.leap.bitmaskclient.userstatus.User; import static android.app.Activity.RESULT_OK; +import static android.content.Intent.CATEGORY_DEFAULT; import static android.view.View.GONE; import static android.view.View.VISIBLE; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK; import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; +import static se.leap.bitmaskclient.Constants.BROADCAST_PROVIDER_API_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; @@ -72,14 +72,15 @@ import static se.leap.bitmaskclient.Constants.EIP_ACTION_UPDATE; import static se.leap.bitmaskclient.Constants.EIP_NOTIFICATION; import static se.leap.bitmaskclient.Constants.EIP_REQUEST; import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; -import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOWED_REGISTERED; -import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOW_ANONYMOUS; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; +import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE; +import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE; import static se.leap.bitmaskclient.ProviderAPI.DOWNLOAD_CERTIFICATE; +import static se.leap.bitmaskclient.ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE; +import static se.leap.bitmaskclient.ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE; public class EipFragment extends Fragment implements Observer { @@ -111,12 +112,10 @@ public class EipFragment extends Fragment implements Observer { @InjectView(R.id.vpn_route) TextView vpnRoute; - private EIPReceiver eipReceiver; private EipStatus eipStatus; private boolean wantsToConnect; - private ProviderAPIResultReceiver providerAPIResultReceiver; - private EIPBroadcastReceiver eipBroadcastReceiver; + private EIPFragmentBroadcastReceiver eipFragmentBroadcastReceiver; private IOpenVPNServiceInternal mService; private ServiceConnection openVpnConnection = new ServiceConnection() { @@ -161,9 +160,7 @@ public class EipFragment extends Fragment implements Observer { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); eipStatus = EipStatus.getInstance(); - eipReceiver = new EIPReceiver(new Handler()); - eipBroadcastReceiver = new EIPBroadcastReceiver(); - providerAPIResultReceiver = new ProviderAPIResultReceiver(new Handler(), new EipFragmentReceiver()); + eipFragmentBroadcastReceiver = new EIPFragmentBroadcastReceiver(); Activity activity = getActivity(); if (activity != null) { preferences = getActivity().getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE); @@ -202,7 +199,7 @@ public class EipFragment extends Fragment implements Observer { Activity activity = getActivity(); if (activity != null) { getActivity().unbindService(openVpnConnection); - getActivity().unregisterReceiver(eipBroadcastReceiver); + LocalBroadcastManager.getInstance(activity).unregisterReceiver(eipFragmentBroadcastReceiver); } Log.d(TAG, "broadcast unregistered"); } @@ -246,30 +243,36 @@ public class EipFragment extends Fragment implements Observer { } private void handleSwitchOn() { - if (canStartEIP()) + Context context = getContext(); + if (context == null) { + Log.e(TAG, "context is null when switch turning on"); + return; + } + + if (canStartEIP()) { startEipFromScratch(); - else if (canLogInToStartEIP()) { + } else if (canLogInToStartEIP()) { wantsToConnect = true; Intent intent = new Intent(getContext(), LoginActivity.class); + intent.putExtra(PROVIDER_KEY, provider); Activity activity = getActivity(); if (activity != null) { activity.startActivityForResult(intent, REQUEST_CODE_LOG_IN); } } else { - Log.d(TAG, "WHAT IS GOING ON HERE?!"); - // TODO: implement a fallback: check if vpncertificate was not downloaded properly or give - // a user feedback. A button that does nothing on click is not a good option + // provider has no VpnCertificate but user is logged in + downloadVpnCertificate(); } } private boolean canStartEIP() { - boolean certificateExists = !preferences.getString(PROVIDER_VPN_CERTIFICATE, "").isEmpty(); - boolean isAllowedAnon = preferences.getBoolean(PROVIDER_ALLOW_ANONYMOUS, false); + boolean certificateExists = !provider.hasVpnCertificate(); + boolean isAllowedAnon = provider.allowsAnonymous(); return (isAllowedAnon || certificateExists) && !eipStatus.isConnected() && !eipStatus.isConnecting(); } private boolean canLogInToStartEIP() { - boolean isAllowedRegistered = preferences.getBoolean(PROVIDER_ALLOWED_REGISTERED, false); + boolean isAllowedRegistered = provider.allowsRegistered(); boolean isLoggedIn = !LeapSRPSession.getToken().isEmpty(); return isAllowedRegistered && !isLoggedIn && !eipStatus.isConnecting() && !eipStatus.isConnected(); } @@ -308,7 +311,12 @@ public class EipFragment extends Fragment implements Observer { public void startEipFromScratch() { wantsToConnect = false; saveStatus(true); - EipCommand.startVPN(getContext(), eipReceiver); + Context context = getContext(); + if (context != null) { + EipCommand.startVPN(context); + } else { + Log.e(TAG, "context is null when trying to start VPN"); + } } private void stop() { @@ -344,8 +352,12 @@ public class EipFragment extends Fragment implements Observer { } protected void stopEipIfPossible() { - //FIXME: no need to start a service here! - EipCommand.stopVPN(getContext(), eipReceiver); + Context context = getContext(); + if (context != null) { + EipCommand.stopVPN(getContext()); + } else { + Log.e(TAG, "context is null when trying to stop EIP"); + } } protected void askToStopEIP() { @@ -441,36 +453,26 @@ public class EipFragment extends Fragment implements Observer { } } - protected class EIPReceiver extends ResultReceiver { - - EIPReceiver(Handler handler) { - super(handler); - } - - @Override - protected void onReceiveResult(int resultCode, Bundle resultData) { - super.onReceiveResult(resultCode, resultData); - - handleEIPEvent(resultCode, resultData); - } - } - - private class EIPBroadcastReceiver extends BroadcastReceiver { + private class EIPFragmentBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "received Broadcast"); String action = intent.getAction(); - if (action == null || !action.equalsIgnoreCase(BROADCAST_EIP_EVENT)) { + if (action == null) { return; } int resultCode = intent.getIntExtra(BROADCAST_RESULT_KEY, -1); Bundle resultData = intent.getParcelableExtra(BROADCAST_RESULT_KEY); - Log.d(TAG, "Broadcast resultCode: " + Integer.toString(resultCode)); - - handleEIPEvent(resultCode, resultData); - + switch (action) { + case BROADCAST_EIP_EVENT: + handleEIPEvent(resultCode, resultData); + break; + case BROADCAST_PROVIDER_API_EVENT: + handleProviderApiEvent(resultCode, resultData); + break; + } } } @@ -547,37 +549,45 @@ public class EipFragment extends Fragment implements Observer { background.setImageAlpha(255); } - private class EipFragmentReceiver implements ProviderAPIResultReceiver.Receiver{ + public void handleProviderApiEvent(int resultCode, Bundle resultData) { + Context context = getContext(); + if (context == null) { + return; + } - @Override - public void onReceiveResult(int resultCode, Bundle resultData) { - if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { + // TODO call DOWNLOAD_EIP_SERVICES ore remove respective cases + switch (resultCode) { + case CORRECTLY_DOWNLOADED_EIP_SERVICE: provider = resultData.getParcelable(PROVIDER_KEY); - EipCommand.updateEipService(getContext(), eipReceiver); - } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { + EipCommand.updateEipService(context); + break; + case INCORRECTLY_DOWNLOADED_EIP_SERVICE: //dashboard.setResult(RESULT_CANCELED); - // TODO CATCH ME IF YOU CAN - } + // TODO CATCH ME IF YOU CAN - WHAT DO WE WANT TO DO? + break; + case CORRECTLY_DOWNLOADED_CERTIFICATE: + startEipFromScratch(); + break; + case INCORRECTLY_DOWNLOADED_CERTIFICATE: + // TODO CATCH ME IF YOU CAN - LOGIN? + break; } } private void downloadVpnCertificate() { - boolean is_authenticated = User.loggedIn(); - boolean allowed_anon = preferences.getBoolean(PROVIDER_ALLOW_ANONYMOUS, false); - if (allowed_anon || is_authenticated) { - ProviderAPICommand.execute(getContext(), DOWNLOAD_CERTIFICATE, provider, providerAPIResultReceiver); - } + ProviderAPICommand.execute(getContext(), DOWNLOAD_CERTIFICATE, provider); } private void setUpBroadcastReceiver() { Activity activity = getActivity(); if (activity != null) { IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_EIP_EVENT); - updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); - activity.registerReceiver(eipBroadcastReceiver, updateIntentFilter); + updateIntentFilter.addAction(BROADCAST_PROVIDER_API_EVENT); + updateIntentFilter.addCategory(CATEGORY_DEFAULT); + LocalBroadcastManager.getInstance(activity).registerReceiver(eipFragmentBroadcastReceiver, updateIntentFilter); Log.d(TAG, "broadcast registered"); } else { - Log.e(TAG, "activity null when setting up boradcat receiver"); + Log.e(TAG, "activity null when setting up broadcast receiver"); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 9595b147..4d608222 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -32,6 +32,7 @@ import java.util.Locale; import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOWED_REGISTERED; import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOW_ANONYMOUS; +import static se.leap.bitmaskclient.ProviderAPI.ERRORS; /** * @author Sean Leonard <meanderingcode@aetherislands.net> @@ -48,6 +49,8 @@ public final class Provider implements Parcelable { private String caCert = ""; private String caCertFingerprint = ""; private String apiVerson = ""; + private String privateKey = ""; + private String vpnCertificate = ""; private boolean allowAnonymous; private boolean allowRegistered; @@ -243,7 +246,8 @@ public final class Provider implements Parcelable { } protected boolean hasEIP() { - return getEipServiceJson() != null && getEipServiceJson().length() > 0; + return getEipServiceJson() != null && getEipServiceJson().length() > 0 + && !getEipServiceJson().has(ERRORS); } public boolean allowsRegistration() { @@ -266,6 +270,8 @@ public final class Provider implements Parcelable { parcel.writeString(getCaCert()); parcel.writeString(getCaCertFingerprint()); parcel.writeString(getEipServiceJsonString()); + parcel.writeString(getPrivateKey()); + parcel.writeString(getVpnCertificate()); } @Override @@ -303,22 +309,30 @@ public final class Provider implements Parcelable { private Provider(Parcel in) { try { mainUrl.setUrl(new URL(in.readString())); - String definitionString = in.readString(); - if (!definitionString.isEmpty()) { - definition = new JSONObject((definitionString)); + String tmpString = in.readString(); + if (!tmpString.isEmpty()) { + definition = new JSONObject((tmpString)); parseDefinition(definition); } - String caCert = in.readString(); - if (!caCert.isEmpty()) { - this.caCert = caCert; + tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.caCert = tmpString; } - String caCertFingerprint = in.readString(); - if (!caCertFingerprint.isEmpty()) { - this.caCertFingerprint = caCertFingerprint; + tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.caCertFingerprint = tmpString; } - String eipServiceJson = in.readString(); - if (!eipServiceJson.isEmpty()) { - this.setEipServiceJson(new JSONObject(eipServiceJson)); + tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.setEipServiceJson(new JSONObject(tmpString)); + } + tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.setPrivateKey(tmpString); + } + tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.setVpnCertificate(tmpString); } } catch (MalformedURLException | JSONException e) { e.printStackTrace(); @@ -355,8 +369,12 @@ public final class Provider implements Parcelable { return allowRegistered; } - public void setEipServiceJson(JSONObject eipServiceJson) { + public boolean setEipServiceJson(JSONObject eipServiceJson) { + if (eipServiceJson.has(ERRORS)) { + return false; + } this.eipServiceJson = eipServiceJson; + return true; } public JSONObject getEipServiceJson() { @@ -374,4 +392,23 @@ public final class Provider implements Parcelable { caCert.isEmpty(); } + public String getPrivateKey() { + return privateKey; + } + + public void setPrivateKey(String privateKey) { + this.privateKey = privateKey; + } + + public String getVpnCertificate() { + return vpnCertificate; + } + + public void setVpnCertificate(String vpnCertificate) { + this.vpnCertificate = vpnCertificate; + } + + public boolean hasVpnCertificate() { + return getVpnCertificate() != null && getVpnCertificate().length() >0 ; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java index 4bffd1b2..b3399416 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderAPI.java @@ -20,6 +20,7 @@ import android.annotation.SuppressLint; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; +import android.support.v4.content.LocalBroadcastManager; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; @@ -49,9 +50,8 @@ public class ProviderAPI extends IntentService implements ProviderApiManagerBase ERRORS = "errors", ERRORID = "errorId", UPDATE_PROGRESSBAR = "update_progressbar", - CURRENT_PROGRESS = "current_progress", - DOWNLOAD_EIP_SERVICE = TAG + ".DOWNLOAD_EIP_SERVICE", - PROVIDER_SET_UP = TAG + ".PROVIDER_SET_UP"; + DOWNLOAD_EIP_SERVICE = "ProviderAPI.DOWNLOAD_EIP_SERVICE", + PROVIDER_SET_UP = "ProviderAPI.PROVIDER_SET_UP"; final public static int SUCCESSFUL_LOGIN = 3, @@ -91,7 +91,7 @@ public class ProviderAPI extends IntentService implements ProviderApiManagerBase @Override public void broadcastEvent(Intent intent) { - sendBroadcast(intent); + LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java index f1c0ecef..a9321a9c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java @@ -142,63 +142,72 @@ public abstract class ProviderApiManagerBase { return; } - if (action.equals(UPDATE_PROVIDER_DETAILS)) { - resetProviderDetails(provider); - Bundle task = new Bundle(); - Bundle result = setUpProvider(provider, task); - if (result.getBoolean(BROADCAST_RESULT_KEY)) { - sendToReceiverOrBroadcast(receiver, PROVIDER_OK, result, provider); - } else { - sendToReceiverOrBroadcast(receiver, PROVIDER_NOK, result, provider); - } - } else if (action.equalsIgnoreCase(SET_UP_PROVIDER)) { - Bundle result = setUpProvider(provider, parameters); - if (result.getBoolean(BROADCAST_RESULT_KEY)) { - sendToReceiverOrBroadcast(receiver, PROVIDER_OK, result, provider); - } else { - sendToReceiverOrBroadcast(receiver, PROVIDER_NOK, result, provider); - } - } else if (action.equalsIgnoreCase(SIGN_UP)) { - Bundle result = tryToRegister(parameters); - if (result.getBoolean(BROADCAST_RESULT_KEY)) { - sendToReceiverOrBroadcast(receiver, SUCCESSFUL_SIGNUP, result, provider); - } else { - sendToReceiverOrBroadcast(receiver, FAILED_SIGNUP, result, provider); - } - } else if (action.equalsIgnoreCase(LOG_IN)) { - Bundle result = tryToAuthenticate(provider, parameters); - if (result.getBoolean(BROADCAST_RESULT_KEY)) { - sendToReceiverOrBroadcast(receiver, SUCCESSFUL_LOGIN, result, provider); - } else { - sendToReceiverOrBroadcast(receiver, FAILED_LOGIN, result, provider); - } - } else if (action.equalsIgnoreCase(LOG_OUT)) { - if (logOut(provider)) { - sendToReceiverOrBroadcast(receiver, SUCCESSFUL_LOGOUT, Bundle.EMPTY, provider); - } else { - sendToReceiverOrBroadcast(receiver, LOGOUT_FAILED, Bundle.EMPTY, provider); - } - } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { - if (updateVpnCertificate(provider)) { - sendToReceiverOrBroadcast(receiver, CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY, provider); - } else { - sendToReceiverOrBroadcast(receiver, INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY, provider); - } - } else if (action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { - Bundle result = getAndSetEipServiceJson(provider); - if (result.getBoolean(BROADCAST_RESULT_KEY)) { - sendToReceiverOrBroadcast(receiver, CORRECTLY_DOWNLOADED_EIP_SERVICE, result, provider); - } else { - sendToReceiverOrBroadcast(receiver, INCORRECTLY_DOWNLOADED_EIP_SERVICE, result, provider); - } - } else if (action.equalsIgnoreCase(PROVIDER_SET_UP)) { - if(provider.hasEIP() && provider.hasCaCert() && provider.hasDefinition()) { - if(receiver!= null) { - Bundle result = new Bundle(); - result.putParcelable(PROVIDER_KEY, provider); - receiver.send(PROVIDER_OK, result); + Bundle result = new Bundle(); + switch (action) { + case UPDATE_PROVIDER_DETAILS: + resetProviderDetails(provider); + Bundle task = new Bundle(); + result = setUpProvider(provider, task); + if (result.getBoolean(BROADCAST_RESULT_KEY)) { + sendToReceiverOrBroadcast(receiver, PROVIDER_OK, result, provider); + } else { + sendToReceiverOrBroadcast(receiver, PROVIDER_NOK, result, provider); } - } + break; + case SET_UP_PROVIDER: + result = setUpProvider(provider, parameters); + if (result.getBoolean(BROADCAST_RESULT_KEY)) { + sendToReceiverOrBroadcast(receiver, PROVIDER_OK, result, provider); + } else { + sendToReceiverOrBroadcast(receiver, PROVIDER_NOK, result, provider); + } + break; + case SIGN_UP: + result = tryToRegister(parameters); + if (result.getBoolean(BROADCAST_RESULT_KEY)) { + sendToReceiverOrBroadcast(receiver, SUCCESSFUL_SIGNUP, result, provider); + } else { + sendToReceiverOrBroadcast(receiver, FAILED_SIGNUP, result, provider); + } + break; + case LOG_IN: + result = tryToAuthenticate(provider, parameters); + if (result.getBoolean(BROADCAST_RESULT_KEY)) { + sendToReceiverOrBroadcast(receiver, SUCCESSFUL_LOGIN, result, provider); + } else { + sendToReceiverOrBroadcast(receiver, FAILED_LOGIN, result, provider); + } + break; + case LOG_OUT: + if (logOut(provider)) { + sendToReceiverOrBroadcast(receiver, SUCCESSFUL_LOGOUT, Bundle.EMPTY, provider); + } else { + sendToReceiverOrBroadcast(receiver, LOGOUT_FAILED, Bundle.EMPTY, provider); + } + break; + case DOWNLOAD_CERTIFICATE: + if (updateVpnCertificate(provider)) { + sendToReceiverOrBroadcast(receiver, CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY, provider); + } else { + sendToReceiverOrBroadcast(receiver, INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY, provider); + } + break; + case DOWNLOAD_EIP_SERVICE: + result = getAndSetEipServiceJson(provider); + if (result.getBoolean(BROADCAST_RESULT_KEY)) { + sendToReceiverOrBroadcast(receiver, CORRECTLY_DOWNLOADED_EIP_SERVICE, result, provider); + } else { + sendToReceiverOrBroadcast(receiver, INCORRECTLY_DOWNLOADED_EIP_SERVICE, result, provider); + } + break; + case PROVIDER_SET_UP: + if(provider.hasEIP() && provider.hasCaCert() && provider.hasDefinition()) { + if(receiver!= null) { + result.putParcelable(PROVIDER_KEY, provider); + receiver.send(PROVIDER_OK, result); + } + } + break; } } @@ -824,22 +833,22 @@ public abstract class ProviderApiManagerBase { * Interprets the error message as a JSON object and extract the "errors" keyword pair. * If the error message is not a JSON object, then it is returned untouched. * - * @param string_json_error_message + * @param stringJsonErrorMessage * @return final error message */ - protected String pickErrorMessage(String string_json_error_message) { - String error_message = ""; + protected String pickErrorMessage(String stringJsonErrorMessage) { + String errorMessage = ""; try { - JSONObject json_error_message = new JSONObject(string_json_error_message); - error_message = json_error_message.getString(ERRORS); + JSONObject jsonErrorMessage = new JSONObject(stringJsonErrorMessage); + errorMessage = jsonErrorMessage.getString(ERRORS); } catch (JSONException e) { // TODO Auto-generated catch block - error_message = string_json_error_message; + errorMessage = stringJsonErrorMessage; } catch (NullPointerException e) { //do nothing } - return error_message; + return errorMessage; } @NonNull @@ -867,8 +876,7 @@ public abstract class ProviderApiManagerBase { return false; } - //FIXME: don't save private keys in shared preferences! use the keystore - protected boolean loadCertificate(String cert_string) { + protected boolean loadCertificate(Provider provider, String cert_string) { if (cert_string == null) { return false; } @@ -887,13 +895,13 @@ public abstract class ProviderApiManagerBase { RSAPrivateKey key = ConfigHelper.parseRsaKeyFromString(keyString); keyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT); - preferences.edit().putString(PROVIDER_PRIVATE_KEY, "-----BEGIN RSA PRIVATE KEY-----\n" + keyString + "-----END RSA PRIVATE KEY-----").commit(); + provider.setPrivateKey( "-----BEGIN RSA PRIVATE KEY-----\n" + keyString + "-----END RSA PRIVATE KEY-----"); X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(certificateString); certificateString = Base64.encodeToString(certificate.getEncoded(), Base64.DEFAULT); - preferences.edit().putString(PROVIDER_VPN_CERTIFICATE, "-----BEGIN CERTIFICATE-----\n" + certificateString + "-----END CERTIFICATE-----").commit(); + provider.setVpnCertificate( "-----BEGIN CERTIFICATE-----\n" + certificateString + "-----END CERTIFICATE-----"); return true; - } catch (CertificateException e) { + } catch (CertificateException | NullPointerException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java index a309bdf9..88221007 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java @@ -9,6 +9,7 @@ import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.design.widget.TextInputEditText; import android.support.design.widget.TextInputLayout; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatTextView; import android.text.Editable; @@ -89,7 +90,7 @@ public abstract class ProviderCredentialsBaseActivity extends ConfigWizardBaseAc IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_PROVIDER_API_EVENT); updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); + LocalBroadcastManager.getInstance(this).registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); setUpListeners(); if(savedInstanceState != null) { @@ -147,7 +148,7 @@ public abstract class ProviderCredentialsBaseActivity extends ConfigWizardBaseAc protected void onDestroy() { super.onDestroy(); if (providerAPIBroadcastReceiver != null) - unregisterReceiver(providerAPIBroadcastReceiver); + LocalBroadcastManager.getInstance(this).unregisterReceiver(providerAPIBroadcastReceiver); } @OnClick(R.id.button) diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java index 6fa3b503..1fb54884 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java @@ -26,6 +26,7 @@ import android.os.Handler; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentTransaction; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.Menu; import android.widget.ListView; @@ -188,7 +189,7 @@ public abstract class ProviderListBaseActivity extends ConfigWizardBaseActivity super.onPause(); isActivityShowing = false; if (providerAPIBroadcastReceiver != null) - unregisterReceiver(providerAPIBroadcastReceiver); + LocalBroadcastManager.getInstance(this).unregisterReceiver(providerAPIBroadcastReceiver); } @Override @@ -213,7 +214,7 @@ public abstract class ProviderListBaseActivity extends ConfigWizardBaseActivity IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_PROVIDER_API_EVENT); updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); + LocalBroadcastManager.getInstance(this).registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); } void handleProviderSetUp(Provider handledProvider) { 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 bfecda22..46528b85 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -22,6 +22,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.ResultReceiver; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import org.json.JSONException; @@ -267,7 +268,7 @@ public final class EIP extends IntentService { intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode); intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData); Log.d(TAG, "sending broadcast"); - sendBroadcast(intentUpdate); + LocalBroadcastManager.getInstance(this).sendBroadcast(intentUpdate); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java index 35599ab4..1c778ec7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java @@ -3,10 +3,12 @@ package se.leap.bitmaskclient.eip; import android.content.Context; import android.content.Intent; import android.os.ResultReceiver; +import android.support.annotation.NonNull; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP; import static se.leap.bitmaskclient.Constants.EIP_ACTION_UPDATE; @@ -38,29 +40,36 @@ public class EipCommand { context.startService(vpnIntent); } - public static void updateEipService(Context context, ResultReceiver resultReceiver) { + public static void updateEipService(@NonNull Context context, ResultReceiver resultReceiver) { execute(context, EIP_ACTION_UPDATE, resultReceiver); } - public static void updateEipService(Context context) { + public static void updateEipService(@NonNull Context context) { execute(context, EIP_ACTION_UPDATE); } - public static void startVPN(Context context) { + public static void startVPN(@NonNull Context context) { execute(context, EIP_ACTION_START); } - public static void startVPN(Context context, ResultReceiver resultReceiver) { + public static void startVPN(@NonNull Context context, ResultReceiver resultReceiver) { execute(context, EIP_ACTION_START, resultReceiver); } - - public static void stopVPN(Context context) { + public static void stopVPN(@NonNull Context context) { execute(context, EIP_ACTION_STOP); } - public static void stopVPN(Context context, ResultReceiver resultReceiver) { + public static void stopVPN(@NonNull Context context, ResultReceiver resultReceiver) { execute(context, EIP_ACTION_STOP, resultReceiver); } + public static void checkVpnCertificate(@NonNull Context context) { + execute(context, EIP_ACTION_CHECK_CERT_VALIDITY); + } + + public static void checkVpnCertificate(@NonNull Context context, ResultReceiver resultReceiver) { + execute(context, EIP_ACTION_CHECK_CERT_VALIDITY, resultReceiver); + } + } |