Android security implementation Phase 1 finalization part 1
This commit is contained in:
@@ -21,12 +21,11 @@ internal object AndroidAccountVault {
|
||||
private const val ENCRYPTED_VALUE = "encrypted-data"
|
||||
private const val GCM_TAG_BITS = 128
|
||||
|
||||
@Volatile private var appContext: Context? = null
|
||||
|
||||
fun initialize(context: Context) {
|
||||
appContext = context.applicationContext
|
||||
secretKey()
|
||||
}
|
||||
@Volatile private var appContext: Context? = null
|
||||
|
||||
fun initialize(context: Context) {
|
||||
appContext = context.applicationContext
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun read(): JSONObject {
|
||||
@@ -598,10 +597,15 @@ internal object AndroidAccountVault {
|
||||
appContext ?: throw IllegalStateException("Encrypted account storage is not initialized.")
|
||||
|
||||
@Synchronized
|
||||
private fun secretKey(): SecretKey {
|
||||
val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
|
||||
(keyStore.getKey(KEY_ALIAS, null) as? SecretKey)?.let { return it }
|
||||
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
|
||||
private fun secretKey(): SecretKey {
|
||||
val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
|
||||
(keyStore.getKey(KEY_ALIAS, null) as? SecretKey)?.let { return it }
|
||||
val hasEncryptedData = requireContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.contains(ENCRYPTED_VALUE)
|
||||
check(AndroidVaultKeyPolicy.shouldCreateKey(hasEncryptedData)) {
|
||||
AndroidVaultKeyPolicy.MISSING_KEY_MESSAGE
|
||||
}
|
||||
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
|
||||
generator.init(
|
||||
KeyGenParameterSpec.Builder(
|
||||
KEY_ALIAS,
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package studio.daybreak.mail
|
||||
|
||||
internal object AndroidVaultKeyPolicy {
|
||||
fun shouldCreateKey(hasEncryptedData: Boolean): Boolean = !hasEncryptedData
|
||||
|
||||
const val MISSING_KEY_MESSAGE =
|
||||
"The Android Keystore key for saved app data is missing. Existing encrypted data was not reset or replaced."
|
||||
}
|
||||
@@ -22,8 +22,8 @@ import javax.mail.internet.InternetAddress
|
||||
class EpMailPlugin : Plugin() {
|
||||
private var syncReceiver: BroadcastReceiver? = null
|
||||
|
||||
override fun load() {
|
||||
AndroidAccountVault.initialize(context)
|
||||
override fun load() {
|
||||
AndroidAccountVault.initialize(context)
|
||||
val filter = IntentFilter(MailIdleService.ACTION_MAIL_SYNC)
|
||||
val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
@@ -38,12 +38,21 @@ class EpMailPlugin : Plugin() {
|
||||
@Suppress("DEPRECATION")
|
||||
context.registerReceiver(receiver, filter)
|
||||
}
|
||||
MailIdleService.start(context)
|
||||
}
|
||||
|
||||
override fun handleOnResume() {
|
||||
MailIdleService.start(context)
|
||||
}
|
||||
ensureVaultAvailableAndStartSync()
|
||||
}
|
||||
|
||||
override fun handleOnResume() {
|
||||
ensureVaultAvailableAndStartSync()
|
||||
}
|
||||
|
||||
private fun ensureVaultAvailableAndStartSync() {
|
||||
try {
|
||||
AndroidAccountVault.read()
|
||||
MailIdleService.start(context)
|
||||
} catch (error: Exception) {
|
||||
android.util.Log.e("EpMailStorage", "Secure local storage is unavailable (${error.javaClass.simpleName})")
|
||||
}
|
||||
}
|
||||
|
||||
override fun handleOnDestroy() {
|
||||
syncReceiver?.let {
|
||||
|
||||
@@ -6,11 +6,13 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
@@ -27,12 +29,21 @@ public class MainActivity extends BridgeActivity {
|
||||
private boolean appLocked;
|
||||
private boolean awaitingUnlockResult;
|
||||
private boolean activityResumed;
|
||||
private boolean vaultRecoveryRequired;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
registerPlugin(EpMailPlugin.class);
|
||||
super.onCreate(savedInstanceState);
|
||||
hardenWebView();
|
||||
AndroidAccountVault.INSTANCE.initialize(getApplicationContext());
|
||||
try {
|
||||
AndroidAccountVault.INSTANCE.read();
|
||||
} catch (Exception storageError) {
|
||||
vaultRecoveryRequired = true;
|
||||
createVaultRecoveryOverlay();
|
||||
return;
|
||||
}
|
||||
createAppLockOverlay();
|
||||
if (isAppLockEnabled()) lockWebView();
|
||||
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
@@ -43,6 +54,7 @@ public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (vaultRecoveryRequired) return;
|
||||
activityResumed = true;
|
||||
if (isAppLockEnabled() && appLocked) {
|
||||
lockWebView();
|
||||
@@ -53,7 +65,7 @@ public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onPause() {
|
||||
activityResumed = false;
|
||||
if (isAppLockEnabled()) lockWebView();
|
||||
if (!vaultRecoveryRequired && isAppLockEnabled()) lockWebView();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@@ -88,6 +100,77 @@ public class MainActivity extends BridgeActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void hardenWebView() {
|
||||
if (getBridge() == null || getBridge().getWebView() == null) return;
|
||||
WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG);
|
||||
WebView webView = getBridge().getWebView();
|
||||
android.webkit.WebSettings settings = webView.getSettings();
|
||||
settings.setAllowFileAccess(false);
|
||||
settings.setAllowContentAccess(false);
|
||||
settings.setAllowFileAccessFromFileURLs(false);
|
||||
settings.setAllowUniversalAccessFromFileURLs(false);
|
||||
settings.setGeolocationEnabled(false);
|
||||
settings.setJavaScriptCanOpenWindowsAutomatically(false);
|
||||
settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_NEVER_ALLOW);
|
||||
}
|
||||
|
||||
private void createVaultRecoveryOverlay() {
|
||||
View content = findViewById(android.R.id.content);
|
||||
if (!(content instanceof FrameLayout)) return;
|
||||
FrameLayout overlay = new FrameLayout(this);
|
||||
overlay.setBackgroundColor(Color.rgb(25, 21, 18));
|
||||
overlay.setClickable(true);
|
||||
overlay.setFocusable(true);
|
||||
|
||||
LinearLayout panel = new LinearLayout(this);
|
||||
panel.setOrientation(LinearLayout.VERTICAL);
|
||||
panel.setGravity(Gravity.CENTER);
|
||||
panel.setPadding(dp(28), dp(28), dp(28), dp(28));
|
||||
|
||||
TextView title = new TextView(this);
|
||||
title.setText("Saved data could not be opened");
|
||||
title.setTextColor(Color.rgb(238, 230, 215));
|
||||
title.setTextSize(21);
|
||||
title.setGravity(Gravity.CENTER);
|
||||
panel.addView(title, new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
|
||||
TextView details = new TextView(this);
|
||||
details.setText("ep-mail could not decrypt its saved local data. It has not been reset or replaced. Try restoring the same device/app state if available. If you cannot recover it, open Android App info and clear ep-mail data to start fresh; this permanently removes saved accounts, cached mail, drafts, queued sends, DAV settings, and OpenPGP keys. Attachments already saved to Downloads remain there.");
|
||||
details.setTextColor(Color.rgb(210, 199, 181));
|
||||
details.setTextSize(14);
|
||||
details.setGravity(Gravity.CENTER);
|
||||
LinearLayout.LayoutParams detailsParams = new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
detailsParams.topMargin = dp(16);
|
||||
panel.addView(details, detailsParams);
|
||||
|
||||
Button settingsButton = new Button(this);
|
||||
settingsButton.setText("Open Android App info");
|
||||
settingsButton.setOnClickListener(view -> {
|
||||
try {
|
||||
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||
Uri.parse("package:" + getPackageName())));
|
||||
} catch (Exception ignored) {
|
||||
// Keep the recovery instructions visible if Settings cannot be launched.
|
||||
}
|
||||
});
|
||||
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
buttonParams.gravity = Gravity.CENTER_HORIZONTAL;
|
||||
buttonParams.topMargin = dp(24);
|
||||
panel.addView(settingsButton, buttonParams);
|
||||
|
||||
overlay.addView(panel, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
|
||||
((FrameLayout) content).addView(overlay, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
if (getBridge() != null && getBridge().getWebView() != null) {
|
||||
getBridge().getWebView().getSettings().setJavaScriptEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void createAppLockOverlay() {
|
||||
View content = findViewById(android.R.id.content);
|
||||
if (!(content instanceof FrameLayout)) return;
|
||||
@@ -178,6 +261,7 @@ public class MainActivity extends BridgeActivity {
|
||||
|
||||
private void lockWebView() {
|
||||
appLocked = true;
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
if (appLockOverlay != null) appLockOverlay.setVisibility(View.VISIBLE);
|
||||
if (getBridge() != null) {
|
||||
WebView webView = getBridge().getWebView();
|
||||
@@ -188,6 +272,7 @@ public class MainActivity extends BridgeActivity {
|
||||
|
||||
private void unlockWebView(boolean reload) {
|
||||
appLocked = false;
|
||||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
if (getBridge() != null) {
|
||||
WebView webView = getBridge().getWebView();
|
||||
webView.getSettings().setJavaScriptEnabled(true);
|
||||
|
||||
Reference in New Issue
Block a user