Implement keys migration

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-01-11 20:55:16 +01:00 committed by tobiasKaminsky
parent d56f53bb11
commit ee7d4ed56c
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
4 changed files with 38 additions and 1 deletions

View file

@ -99,6 +99,7 @@ public class PushUtils {
}
private static int generateRsa2048KeyPair() {
MainApp.migratePushKeys();
String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator
+ KEYPAIR_FOLDER;

View file

@ -33,7 +33,6 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.annotation.StringRes;
import android.support.multidex.MultiDexApplication;
@ -66,6 +65,7 @@ import com.owncloud.android.utils.FilesSyncHelper;
import com.owncloud.android.utils.PermissionUtil;
import com.owncloud.android.utils.ReceiversHelper;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
@ -238,6 +238,31 @@ public class MainApp extends MultiDexApplication {
}
}
public static void migratePushKeys() {
Context context = getAppContext();
if (!PreferenceManager.getKeysMigration(context)) {
String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair";
File oldPrivateKey = new File(oldKeyPath + File.separator + "push_key.priv");
File oldPublicKey = new File(oldKeyPath + File.separator + "push_key.pub");
String keyPath = getAppContext().getFilesDir().getAbsolutePath() +
File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair";
File privateKey = new File(keyPath + File.separator + "push_key.priv");
File publicKey = new File(keyPath + File.separator + "push_key.pub");
if (oldPrivateKey.exists() && oldPublicKey.exists()) {
if (oldPrivateKey.renameTo(privateKey) && oldPublicKey.renameTo(publicKey)) {
PreferenceManager.setKeysMigration(context, true);
} else {
PreferenceManager.setKeysMigration(context, false);
}
} else {
PreferenceManager.setKeysMigration(context, true);
}
}
}
public static void notificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && getAppContext() != null) {
Context context = getAppContext();

View file

@ -53,6 +53,7 @@ public abstract class PreferenceManager {
private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS
= "instant_video_upload_path_use_subfolders";
private static final String PREF__LEGACY_CLEAN = "legacyClean";
private static final String PREF__KEYS_MIGRATION = "keysMigration";
private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate";
private static final String PREF__PUSH_TOKEN = "pushToken";
private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut";
@ -286,6 +287,11 @@ public abstract class PreferenceManager {
return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
}
public static boolean getKeysMigration(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
}
/**
* Gets the auto upload paths flag last set.
*
@ -316,6 +322,10 @@ public abstract class PreferenceManager {
saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
}
public static void setKeysMigration(Context context, boolean keysMigration) {
saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
}
public static void setAutoUploadInit(Context context, boolean autoUploadInit) {
saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit);
}

View file

@ -99,6 +99,7 @@ public class PushUtils {
}
private static int generateRsa2048KeyPair() {
MainApp.migratePushKeys();
String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator
+ KEYPAIR_FOLDER;