From d523eb98316caaa60a6512ae6f45300205ad5abf Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 3 Aug 2022 15:05:18 +0200 Subject: Disable profile encryption upon error --- .../de/blinkt/openvpn/core/ProfileManager.java | 31 +++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java index 1185a697..b46a8f10 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Set; +import java.util.Vector; import de.blinkt.openvpn.VpnProfile; @@ -35,7 +36,8 @@ public class ProfileManager { private static VpnProfile mLastConnectedVpn = null; private static VpnProfile tmpprofile = null; private HashMap profiles = new HashMap<>(); - + /* We got an error trying to save profiles, do not try encryption anymore */ + private static boolean encryptionBroken = false; private ProfileManager() { } @@ -107,6 +109,8 @@ public class ProfileManager { public static void saveProfile(Context context, VpnProfile profile) { SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context); boolean preferEncryption = prefs.getBoolean("preferencryption", true); + if (encryptionBroken) + preferEncryption = false; profile.mVersion += 1; ObjectOutputStream vpnFile; @@ -136,10 +140,19 @@ public class ProfileManager { VpnStatus.logInfo("Cannot rename " + encryptedFile); } } - vpnFileOut = ProfileEncryption.getEncryptedVpOutput(context, encryptedFile); - deleteIfExists = filename + ".vp"; - if (encryptedFileOld.exists()) { - encryptedFileOld.delete(); + try { + vpnFileOut = ProfileEncryption.getEncryptedVpOutput(context, encryptedFile); + deleteIfExists = filename + ".vp"; + if (encryptedFileOld.exists()) { + encryptedFileOld.delete(); + } + } catch (IOException ioe) + { + VpnStatus.logException(VpnStatus.LogLevel.INFO, "Error trying to write an encrypted VPN profile, disabling " + + "encryption", ioe); + encryptionBroken = true; + saveProfile(context, profile); + return; } } else { @@ -257,10 +270,16 @@ public class ProfileManager { if (!profiles.containsKey(vpnentry)) loadVpnEntry(context, vpnentry); } + + Vector removeUuids = new Vector<>(); for (String profileuuid:profiles.keySet()) { if (!vlist.contains(profileuuid)) - profiles.remove(profileuuid); + removeUuids.add(profileuuid); + } + for (String uuid: removeUuids) + { + profiles.remove(uuid); } } -- cgit v1.2.3