116 lines
4.4 KiB
Java
116 lines
4.4 KiB
Java
package org.thoughtcrime.securesms.migrations;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
|
import org.thoughtcrime.securesms.jobs.BaseJob;
|
|
import org.thoughtcrime.securesms.keyvalue.KbsValues;
|
|
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
|
import org.thoughtcrime.securesms.lock.PinHashing;
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
import org.whispersystems.signalservice.api.KeyBackupService;
|
|
import org.whispersystems.signalservice.api.KeyBackupServicePinException;
|
|
import org.whispersystems.signalservice.api.RegistrationLockData;
|
|
import org.whispersystems.signalservice.api.kbs.HashedPin;
|
|
import org.whispersystems.signalservice.api.kbs.MasterKey;
|
|
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* Deliberately not a {@link MigrationJob} because it is not something that needs to run at app start.
|
|
* This migration can run at anytime.
|
|
*/
|
|
public final class RegistrationPinV2MigrationJob extends BaseJob {
|
|
|
|
private static final String TAG = Log.tag(RegistrationPinV2MigrationJob.class);
|
|
|
|
public static final String KEY = "RegistrationPinV2MigrationJob";
|
|
|
|
public RegistrationPinV2MigrationJob() {
|
|
this(new Parameters.Builder()
|
|
.setQueue(KEY)
|
|
.setMaxInstances(1)
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
.setLifespan(Job.Parameters.IMMORTAL)
|
|
.setMaxAttempts(Job.Parameters.UNLIMITED)
|
|
.setMaxBackoff(TimeUnit.HOURS.toMillis(2))
|
|
.build());
|
|
}
|
|
|
|
private RegistrationPinV2MigrationJob(@NonNull Parameters parameters) {
|
|
super(parameters);
|
|
}
|
|
|
|
@Override
|
|
public @NonNull Data serialize() {
|
|
return Data.EMPTY;
|
|
}
|
|
|
|
@Override
|
|
protected void onRun() throws IOException, UnauthenticatedResponseException, KeyBackupServicePinException {
|
|
if (!TextSecurePreferences.isV1RegistrationLockEnabled(context)) {
|
|
Log.i(TAG, "Registration lock disabled");
|
|
return;
|
|
}
|
|
|
|
//noinspection deprecation Only acceptable place to read the old pin.
|
|
String pinValue = TextSecurePreferences.getDeprecatedV1RegistrationLockPin(context);
|
|
|
|
if (pinValue == null | TextUtils.isEmpty(pinValue)) {
|
|
Log.i(TAG, "No old pin to migrate");
|
|
return;
|
|
}
|
|
|
|
Log.i(TAG, "Migrating pin to Key Backup Service");
|
|
|
|
KbsValues kbsValues = SignalStore.kbsValues();
|
|
MasterKey masterKey = kbsValues.getOrCreateMasterKey();
|
|
KeyBackupService keyBackupService = ApplicationDependencies.getKeyBackupService();
|
|
KeyBackupService.PinChangeSession pinChangeSession = keyBackupService.newPinChangeSession();
|
|
HashedPin hashedPin = PinHashing.hashPin(pinValue, pinChangeSession);
|
|
RegistrationLockData kbsData = pinChangeSession.setPin(hashedPin, masterKey);
|
|
RegistrationLockData restoredData = keyBackupService.newRestoreSession(kbsData.getTokenResponse())
|
|
.restorePin(hashedPin);
|
|
|
|
if (!restoredData.getMasterKey().equals(masterKey)) {
|
|
throw new RuntimeException("Failed to migrate the pin correctly");
|
|
} else {
|
|
Log.i(TAG, "Set and retrieved pin on KBS successfully");
|
|
}
|
|
|
|
kbsValues.setRegistrationLockMasterKey(restoredData, PinHashing.localPinHash(pinValue));
|
|
TextSecurePreferences.clearOldRegistrationLockPin(context);
|
|
|
|
Log.i(TAG, "Pin migrated to Key Backup Service");
|
|
}
|
|
|
|
@Override
|
|
protected boolean onShouldRetry(@NonNull Exception e) {
|
|
return e instanceof IOException;
|
|
}
|
|
|
|
@Override
|
|
public @NonNull String getFactoryKey() {
|
|
return KEY;
|
|
}
|
|
|
|
@Override
|
|
public void onFailure() {
|
|
}
|
|
|
|
public static class Factory implements Job.Factory<RegistrationPinV2MigrationJob> {
|
|
@Override
|
|
public @NonNull RegistrationPinV2MigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
return new RegistrationPinV2MigrationJob(parameters);
|
|
}
|
|
}
|
|
}
|