Signal-Android/app/src/main/java/org/thoughtcrime/securesms/keyvalue/KbsValues.java

176 lines
5.7 KiB
Java
Raw Normal View History

2020-01-17 17:14:54 +01:00
package org.thoughtcrime.securesms.keyvalue;
import androidx.annotation.NonNull;
2020-01-17 17:14:54 +01:00
import androidx.annotation.Nullable;
2020-05-30 00:16:38 +02:00
import org.thoughtcrime.securesms.lock.PinHashing;
2020-01-17 17:14:54 +01:00
import org.thoughtcrime.securesms.util.JsonUtils;
import org.whispersystems.signalservice.api.KbsPinData;
2020-01-17 19:31:30 +01:00
import org.whispersystems.signalservice.api.kbs.MasterKey;
2020-01-17 17:14:54 +01:00
import org.whispersystems.signalservice.internal.contacts.entities.TokenResponse;
import java.io.IOException;
import java.security.SecureRandom;
2020-01-17 17:14:54 +01:00
2020-06-11 00:24:27 +02:00
public final class KbsValues extends SignalStoreValues {
2020-01-17 17:14:54 +01:00
2020-05-09 18:02:18 +02:00
public static final String V2_LOCK_ENABLED = "kbs.v2_lock_enabled";
private static final String MASTER_KEY = "kbs.registration_lock_master_key";
private static final String TOKEN_RESPONSE = "kbs.token_response";
2020-05-30 00:16:38 +02:00
private static final String PIN = "kbs.pin";
2020-05-09 18:02:18 +02:00
private static final String LOCK_LOCAL_PIN_HASH = "kbs.registration_lock_local_pin_hash";
private static final String LAST_CREATE_FAILED_TIMESTAMP = "kbs.last_create_failed_timestamp";
2020-07-10 01:04:30 +02:00
public static final String OPTED_OUT = "kbs.opted_out";
2020-01-17 17:14:54 +01:00
KbsValues(KeyValueStore store) {
2020-06-11 00:24:27 +02:00
super(store);
}
@Override
void onFirstEverAppLaunch() {
2020-01-17 17:14:54 +01:00
}
/**
* Deliberately does not clear the {@link #MASTER_KEY}.
*
* Should only be called by {@link org.thoughtcrime.securesms.pin.PinState}
*/
public void clearRegistrationLockAndPin() {
2020-06-11 00:24:27 +02:00
getStore().beginWrite()
.remove(V2_LOCK_ENABLED)
.remove(TOKEN_RESPONSE)
.remove(LOCK_LOCAL_PIN_HASH)
.remove(PIN)
.remove(LAST_CREATE_FAILED_TIMESTAMP)
2020-07-10 01:04:30 +02:00
.remove(OPTED_OUT)
2020-06-11 00:24:27 +02:00
.commit();
}
2020-01-17 17:14:54 +01:00
/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
2020-05-30 00:16:38 +02:00
public synchronized void setKbsMasterKey(@NonNull KbsPinData pinData, @NonNull String pin) {
MasterKey masterKey = pinData.getMasterKey();
String tokenResponse;
try {
tokenResponse = JsonUtils.toJson(pinData.getTokenResponse());
} catch (IOException e) {
throw new AssertionError(e);
2020-01-17 17:14:54 +01:00
}
2020-06-11 00:24:27 +02:00
getStore().beginWrite()
.putString(TOKEN_RESPONSE, tokenResponse)
.putBlob(MASTER_KEY, masterKey.serialize())
.putString(LOCK_LOCAL_PIN_HASH, PinHashing.localPinHash(pin))
.putString(PIN, pin)
.putLong(LAST_CREATE_FAILED_TIMESTAMP, -1)
.putBoolean(OPTED_OUT, false)
2020-06-11 00:24:27 +02:00
.commit();
2020-01-17 17:14:54 +01:00
}
2020-05-30 00:16:38 +02:00
synchronized void setPinIfNotPresent(@NonNull String pin) {
2020-06-11 00:24:27 +02:00
if (getStore().getString(PIN, null) == null) {
getStore().beginWrite().putString(PIN, pin).commit();
2020-05-30 00:16:38 +02:00
}
}
/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
public synchronized void setV2RegistrationLockEnabled(boolean enabled) {
2020-06-11 00:24:27 +02:00
putBoolean(V2_LOCK_ENABLED, enabled);
}
2020-05-09 18:02:18 +02:00
/**
* Whether or not registration lock V2 is enabled.
*/
public synchronized boolean isV2RegistrationLockEnabled() {
2020-06-11 00:24:27 +02:00
return getBoolean(V2_LOCK_ENABLED, false);
}
2020-05-09 18:02:18 +02:00
/** Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}. */
public synchronized void onPinCreateFailure() {
2020-06-11 00:24:27 +02:00
putLong(LAST_CREATE_FAILED_TIMESTAMP, System.currentTimeMillis());
2020-05-09 18:02:18 +02:00
}
/**
* Whether or not the last time the user attempted to create a PIN, it failed.
*/
public synchronized boolean lastPinCreateFailed() {
2020-06-11 00:24:27 +02:00
return getLong(LAST_CREATE_FAILED_TIMESTAMP, -1) > 0;
2020-05-09 18:02:18 +02:00
}
/**
* Finds or creates the master key. Therefore this will always return a master key whether backed
* up or not.
* <p>
* If you only want a key when it's backed up, use {@link #getPinBackedMasterKey()}.
*/
public synchronized @NonNull MasterKey getOrCreateMasterKey() {
2020-06-11 00:24:27 +02:00
byte[] blob = getStore().getBlob(MASTER_KEY, null);
if (blob == null) {
2020-06-11 00:24:27 +02:00
getStore().beginWrite()
.putBlob(MASTER_KEY, MasterKey.createNew(new SecureRandom()).serialize())
.commit();
blob = getBlob(MASTER_KEY, null);
2020-01-17 19:31:30 +01:00
}
return new MasterKey(blob);
}
/**
* Returns null if master key is not backed up by a pin.
*/
public synchronized @Nullable MasterKey getPinBackedMasterKey() {
if (!isV2RegistrationLockEnabled()) return null;
return getMasterKey();
}
private synchronized @Nullable MasterKey getMasterKey() {
2020-06-11 00:24:27 +02:00
byte[] blob = getBlob(MASTER_KEY, null);
return blob != null ? new MasterKey(blob) : null;
2020-01-17 17:14:54 +01:00
}
public @Nullable String getRegistrationLockToken() {
MasterKey masterKey = getPinBackedMasterKey();
if (masterKey == null) {
return null;
} else {
return masterKey.deriveRegistrationLock();
}
2020-01-17 17:14:54 +01:00
}
public synchronized @Nullable String getLocalPinHash() {
2020-06-11 00:24:27 +02:00
return getString(LOCK_LOCAL_PIN_HASH, null);
2020-01-17 17:14:54 +01:00
}
public synchronized boolean hasPin() {
return getLocalPinHash() != null;
2020-01-17 17:14:54 +01:00
}
/** Should only be called by {@link org.thoughtcrime.securesms.pin.PinState}. */
2020-07-10 01:04:30 +02:00
public synchronized void optOut() {
getStore().beginWrite()
.putBoolean(OPTED_OUT, true)
.remove(TOKEN_RESPONSE)
.putBlob(MASTER_KEY, MasterKey.createNew(new SecureRandom()).serialize())
.remove(LOCK_LOCAL_PIN_HASH)
.remove(PIN)
.putLong(LAST_CREATE_FAILED_TIMESTAMP, -1)
.commit();
2020-07-10 01:04:30 +02:00
}
public synchronized boolean hasOptedOut() {
return getBoolean(OPTED_OUT, false);
}
public synchronized @Nullable TokenResponse getRegistrationLockTokenResponse() {
2020-06-11 00:24:27 +02:00
String token = getStore().getString(TOKEN_RESPONSE, null);
2020-01-17 17:14:54 +01:00
if (token == null) return null;
try {
return JsonUtils.fromJson(token, TokenResponse.class);
} catch (IOException e) {
throw new AssertionError(e);
}
}
}