Search in sources :

Example 1 with KbsPinData

use of org.whispersystems.signalservice.api.KbsPinData 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 2 with KbsPinData

use of org.whispersystems.signalservice.api.KbsPinData 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 KbsPinData

use of org.whispersystems.signalservice.api.KbsPinData 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 KbsPinData

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

the class PinRestoreRepository method submitPin.

void submitPin(@NonNull String pin, @NonNull TokenData tokenData, @NonNull Callback<PinResultData> callback) {
    executor.execute(() -> {
        try {
            Stopwatch stopwatch = new Stopwatch("PinSubmission");
            KbsPinData kbsData = KbsRepository.restoreMasterKey(pin, tokenData.getEnclave(), tokenData.getBasicAuth(), tokenData.getTokenResponse());
            PinState.onSignalPinRestore(ApplicationDependencies.getApplication(), Objects.requireNonNull(kbsData), pin);
            stopwatch.split("MasterKey");
            ApplicationDependencies.getJobManager().runSynchronously(new StorageAccountRestoreJob(), StorageAccountRestoreJob.LIFESPAN);
            stopwatch.split("AccountRestore");
            ApplicationDependencies.getJobManager().runSynchronously(new StorageSyncJob(), TimeUnit.SECONDS.toMillis(10));
            stopwatch.split("ContactRestore");
            stopwatch.stop(TAG);
            callback.onComplete(new PinResultData(PinResult.SUCCESS, tokenData));
        } catch (IOException e) {
            callback.onComplete(new PinResultData(PinResult.NETWORK_ERROR, tokenData));
        } catch (KeyBackupSystemNoDataException e) {
            callback.onComplete(new PinResultData(PinResult.LOCKED, tokenData));
        } catch (KeyBackupSystemWrongPinException e) {
            callback.onComplete(new PinResultData(PinResult.INCORRECT, TokenData.withResponse(tokenData, e.getTokenResponse())));
        }
    });
}
Also used : StorageSyncJob(org.thoughtcrime.securesms.jobs.StorageSyncJob) StorageAccountRestoreJob(org.thoughtcrime.securesms.jobs.StorageAccountRestoreJob) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) KbsPinData(org.whispersystems.signalservice.api.KbsPinData) IOException(java.io.IOException) KeyBackupSystemNoDataException(org.whispersystems.signalservice.api.KeyBackupSystemNoDataException)

Example 5 with KbsPinData

use of org.whispersystems.signalservice.api.KbsPinData 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

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