Migrate profile key into database.

This commit is contained in:
Alan Evans
2020-02-10 10:06:12 -05:00
committed by Greyson Parrelli
parent b92c389a5b
commit f10d1eac61
11 changed files with 87 additions and 57 deletions

View File

@@ -34,8 +34,8 @@ import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.util.Pair;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.api.storage.SignalContactRecord;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.io.Closeable;
import java.io.IOException;

View File

@@ -9,6 +9,7 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import androidx.annotation.NonNull;
@@ -47,10 +48,12 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.SqlUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import java.io.File;
import java.util.List;
@@ -105,8 +108,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int STICKER_PACK_ORDER = 44;
private static final int MEGAPHONES = 45;
private static final int MEGAPHONE_FIRST_APPEARANCE = 46;
private static final int PROFILE_KEY_TO_DB = 47;
private static final int DATABASE_VERSION = 46;
private static final int DATABASE_VERSION = 47;
private static final String DATABASE_NAME = "signal.db";
private final Context context;
@@ -724,6 +728,21 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE megaphone ADD COLUMN first_visible INTEGER DEFAULT 0");
}
if (oldVersion < PROFILE_KEY_TO_DB) {
String localNumber = TextSecurePreferences.getLocalNumber(context);
if (!TextUtils.isEmpty(localNumber)) {
String encodedProfileKey = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_profile_key", null);
byte[] profileKey = encodedProfileKey != null ? Base64.decodeOrThrow(encodedProfileKey) : Util.getSecretBytes(32);
ContentValues values = new ContentValues(1);
values.put("profile_key", Base64.encodeBytes(profileKey));
if (db.update("recipient", values, "phone = ?", new String[]{localNumber}) == 0) {
throw new AssertionError("No rows updated!");
}
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();