Search in sources :

Example 1 with KeyBackupService

use of org.whispersystems.signalservice.api.KeyBackupService in project Signal-Android by WhisperSystems.

the class KbsRepository method getTokenSync.

@NonNull
private TokenData getTokenSync(@Nullable String authorization) throws IOException {
    TokenData firstKnownTokenData = null;
    for (KbsEnclave enclave : KbsEnclaves.all()) {
        KeyBackupService kbs = ApplicationDependencies.getKeyBackupService(enclave);
        authorization = authorization == null ? kbs.getAuthorization() : authorization;
        TokenResponse token = kbs.getToken(authorization);
        TokenData tokenData = new TokenData(enclave, authorization, token);
        if (tokenData.getTriesRemaining() > 0) {
            Log.i(TAG, "Found data! " + enclave.getEnclaveName());
            return tokenData;
        } else if (firstKnownTokenData == null) {
            Log.i(TAG, "No data, but storing as the first response. " + enclave.getEnclaveName());
            firstKnownTokenData = tokenData;
        } else {
            Log.i(TAG, "No data, and we already have a 'first response'. " + enclave.getEnclaveName());
        }
    }
    return Objects.requireNonNull(firstKnownTokenData);
}
Also used : KbsEnclave(org.thoughtcrime.securesms.KbsEnclave) TokenResponse(org.whispersystems.signalservice.internal.contacts.entities.TokenResponse) KeyBackupService(org.whispersystems.signalservice.api.KeyBackupService) NonNull(androidx.annotation.NonNull)

Example 2 with KeyBackupService

use of org.whispersystems.signalservice.api.KeyBackupService in project Signal-Android by WhisperSystems.

the class KbsRepository method restoreMasterKeyFromEnclave.

@NonNull
private static KbsPinData restoreMasterKeyFromEnclave(@NonNull KbsEnclave enclave, @NonNull String pin, @NonNull String basicStorageCredentials, @NonNull TokenResponse tokenResponse) throws IOException, KeyBackupSystemWrongPinException, KeyBackupSystemNoDataException {
    KeyBackupService keyBackupService = ApplicationDependencies.getKeyBackupService(enclave);
    KeyBackupService.RestoreSession session = keyBackupService.newRegistrationSession(basicStorageCredentials, tokenResponse);
    try {
        Log.i(TAG, "Restoring pin from KBS");
        HashedPin hashedPin = PinHashing.hashPin(pin, session);
        KbsPinData kbsData = session.restorePin(hashedPin);
        if (kbsData != null) {
            Log.i(TAG, "Found registration lock token on KBS.");
        } else {
            throw new AssertionError("Null not expected");
        }
        return kbsData;
    } catch (UnauthenticatedResponseException | InvalidKeyException e) {
        Log.w(TAG, "Failed to restore key", e);
        throw new IOException(e);
    } catch (KeyBackupServicePinException e) {
        Log.w(TAG, "Incorrect pin", e);
        throw new KeyBackupSystemWrongPinException(e.getToken());
    }
}
Also used : KeyBackupService(org.whispersystems.signalservice.api.KeyBackupService) UnauthenticatedResponseException(org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException) KeyBackupServicePinException(org.whispersystems.signalservice.api.KeyBackupServicePinException) KbsPinData(org.whispersystems.signalservice.api.KbsPinData) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) NonNull(androidx.annotation.NonNull)

Example 3 with KeyBackupService

use of org.whispersystems.signalservice.api.KeyBackupService in project Signal-Android by WhisperSystems.

the class PinState method onMigrateToRegistrationLockV2.

/**
 * Should only be called by {@link org.thoughtcrime.securesms.migrations.RegistrationPinV2MigrationJob}.
 */
@WorkerThread
public static synchronized void onMigrateToRegistrationLockV2(@NonNull Context context, @NonNull String pin) throws IOException, UnauthenticatedResponseException, InvalidKeyException {
    Log.i(TAG, "onMigrateToRegistrationLockV2()");
    KbsEnclave kbsEnclave = KbsEnclaves.current();
    Log.i(TAG, "Enclave: " + kbsEnclave.getEnclaveName());
    KbsValues kbsValues = SignalStore.kbsValues();
    MasterKey masterKey = kbsValues.getOrCreateMasterKey();
    KeyBackupService keyBackupService = ApplicationDependencies.getKeyBackupService(kbsEnclave);
    KeyBackupService.PinChangeSession pinChangeSession = keyBackupService.newPinChangeSession();
    HashedPin hashedPin = PinHashing.hashPin(pin, pinChangeSession);
    KbsPinData kbsData = pinChangeSession.setPin(hashedPin, masterKey);
    pinChangeSession.enableRegistrationLock(masterKey);
    kbsValues.setKbsMasterKey(kbsData, pin);
    TextSecurePreferences.clearRegistrationLockV1(context);
    updateState(buildInferredStateFromOtherFields());
}
Also used : KbsEnclave(org.thoughtcrime.securesms.KbsEnclave) KeyBackupService(org.whispersystems.signalservice.api.KeyBackupService) MasterKey(org.whispersystems.signalservice.api.kbs.MasterKey) KbsValues(org.thoughtcrime.securesms.keyvalue.KbsValues) KbsPinData(org.whispersystems.signalservice.api.KbsPinData) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) WorkerThread(androidx.annotation.WorkerThread)

Example 4 with KeyBackupService

use of org.whispersystems.signalservice.api.KeyBackupService in project Signal-Android by WhisperSystems.

the class PinState method onPinChangedOrCreated.

/**
 * Invoked whenever the Signal PIN is changed or created.
 */
@WorkerThread
public static synchronized void onPinChangedOrCreated(@NonNull Context context, @NonNull String pin, @NonNull PinKeyboardType keyboard) throws IOException, UnauthenticatedResponseException, InvalidKeyException {
    Log.i(TAG, "onPinChangedOrCreated()");
    KbsEnclave kbsEnclave = KbsEnclaves.current();
    KbsValues kbsValues = SignalStore.kbsValues();
    boolean isFirstPin = !kbsValues.hasPin() || kbsValues.hasOptedOut();
    MasterKey masterKey = kbsValues.getOrCreateMasterKey();
    KeyBackupService keyBackupService = ApplicationDependencies.getKeyBackupService(kbsEnclave);
    KeyBackupService.PinChangeSession pinChangeSession = keyBackupService.newPinChangeSession();
    HashedPin hashedPin = PinHashing.hashPin(pin, pinChangeSession);
    KbsPinData kbsData = pinChangeSession.setPin(hashedPin, masterKey);
    kbsValues.setKbsMasterKey(kbsData, pin);
    TextSecurePreferences.clearRegistrationLockV1(context);
    SignalStore.pinValues().setKeyboardType(keyboard);
    SignalStore.pinValues().resetPinReminders();
    ApplicationDependencies.getMegaphoneRepository().markFinished(Megaphones.Event.PINS_FOR_ALL);
    if (isFirstPin) {
        Log.i(TAG, "First time setting a PIN. Refreshing attributes to set the 'storage' capability. Enclave: " + kbsEnclave.getEnclaveName());
        bestEffortRefreshAttributes();
    } else {
        Log.i(TAG, "Not the first time setting a PIN. Enclave: " + kbsEnclave.getEnclaveName());
    }
    updateState(buildInferredStateFromOtherFields());
}
Also used : KbsEnclave(org.thoughtcrime.securesms.KbsEnclave) KeyBackupService(org.whispersystems.signalservice.api.KeyBackupService) MasterKey(org.whispersystems.signalservice.api.kbs.MasterKey) KbsValues(org.thoughtcrime.securesms.keyvalue.KbsValues) KbsPinData(org.whispersystems.signalservice.api.KbsPinData) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) WorkerThread(androidx.annotation.WorkerThread)

Example 5 with KeyBackupService

use of org.whispersystems.signalservice.api.KeyBackupService in project Signal-Android by WhisperSystems.

the class PinState method setPinOnEnclave.

@WorkerThread
@NonNull
private static KbsPinData setPinOnEnclave(@NonNull KbsEnclave enclave, @NonNull String pin, @NonNull MasterKey masterKey) throws IOException, UnauthenticatedResponseException {
    Log.i(TAG, "Setting PIN on enclave: " + enclave.getEnclaveName());
    KeyBackupService kbs = ApplicationDependencies.getKeyBackupService(enclave);
    KeyBackupService.PinChangeSession pinChangeSession = kbs.newPinChangeSession();
    HashedPin hashedPin = PinHashing.hashPin(pin, pinChangeSession);
    KbsPinData newData = pinChangeSession.setPin(hashedPin, masterKey);
    SignalStore.kbsValues().setKbsMasterKey(newData, pin);
    return newData;
}
Also used : KeyBackupService(org.whispersystems.signalservice.api.KeyBackupService) KbsPinData(org.whispersystems.signalservice.api.KbsPinData) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

KeyBackupService (org.whispersystems.signalservice.api.KeyBackupService)5 KbsPinData (org.whispersystems.signalservice.api.KbsPinData)4 HashedPin (org.whispersystems.signalservice.api.kbs.HashedPin)4 NonNull (androidx.annotation.NonNull)3 WorkerThread (androidx.annotation.WorkerThread)3 KbsEnclave (org.thoughtcrime.securesms.KbsEnclave)3 KbsValues (org.thoughtcrime.securesms.keyvalue.KbsValues)2 MasterKey (org.whispersystems.signalservice.api.kbs.MasterKey)2 IOException (java.io.IOException)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1 KeyBackupServicePinException (org.whispersystems.signalservice.api.KeyBackupServicePinException)1 UnauthenticatedResponseException (org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException)1 TokenResponse (org.whispersystems.signalservice.internal.contacts.entities.TokenResponse)1