Always return passphrase without spaces.

This commit is contained in:
Alan Evans 2020-02-04 08:10:58 -05:00
parent e551ea8bd9
commit 36a4225858

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.backup;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -12,26 +13,30 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences;
/** /**
* Allows the getting and setting of the backup passphrase, which is stored encrypted on API >= 23. * Allows the getting and setting of the backup passphrase, which is stored encrypted on API >= 23.
*/ */
public class BackupPassphrase { public final class BackupPassphrase {
private BackupPassphrase() {
}
private static final String TAG = BackupPassphrase.class.getSimpleName(); private static final String TAG = BackupPassphrase.class.getSimpleName();
public static String get(@NonNull Context context) { public static @Nullable String get(@NonNull Context context) {
String passphrase = TextSecurePreferences.getBackupPassphrase(context); String passphrase = TextSecurePreferences.getBackupPassphrase(context);
String encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context); String encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context);
if (Build.VERSION.SDK_INT < 23 || (passphrase == null && encryptedPassphrase == null)) { if (Build.VERSION.SDK_INT < 23 || (passphrase == null && encryptedPassphrase == null)) {
return passphrase; return stripSpaces(passphrase);
} }
if (encryptedPassphrase == null) { if (encryptedPassphrase == null) {
Log.i(TAG, "Migrating to encrypted passphrase."); Log.i(TAG, "Migrating to encrypted passphrase.");
set(context, passphrase); set(context, passphrase);
encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context); encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context);
if (encryptedPassphrase == null) throw new AssertionError("Passphrase migration failed");
} }
KeyStoreHelper.SealedData data = KeyStoreHelper.SealedData.fromString(encryptedPassphrase); KeyStoreHelper.SealedData data = KeyStoreHelper.SealedData.fromString(encryptedPassphrase);
return new String(KeyStoreHelper.unseal(data)); return stripSpaces(new String(KeyStoreHelper.unseal(data)));
} }
public static void set(@NonNull Context context, @Nullable String passphrase) { public static void set(@NonNull Context context, @Nullable String passphrase) {
@ -44,4 +49,8 @@ public class BackupPassphrase {
TextSecurePreferences.setBackupPassphrase(context, null); TextSecurePreferences.setBackupPassphrase(context, null);
} }
} }
private static String stripSpaces(@Nullable String passphrase) {
return passphrase != null ? passphrase.replace(" ", "") : null;
}
} }