Search in sources :

Example 1 with MasterKey

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

use of org.whispersystems.signalservice.api.kbs.MasterKey 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 3 with MasterKey

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

the class KbsValues method setKbsMasterKey.

/**
 * Should only be set by {@link org.thoughtcrime.securesms.pin.PinState}.
 */
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);
    }
    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).commit();
}
Also used : MasterKey(org.whispersystems.signalservice.api.kbs.MasterKey) IOException(java.io.IOException)

Example 4 with MasterKey

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

the class PinHashing_hashPin_Test method argon2_hashed_pin_password.

@Test
public void argon2_hashed_pin_password() throws IOException {
    String pin = "password";
    byte[] backupId = Hex.fromStringCondensed("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
    MasterKey masterKey = new MasterKey(Hex.fromStringCondensed("202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"));
    HashedPin hashedPin = PinHashing.hashPin(pin, () -> backupId);
    KbsData kbsData = hashedPin.createNewKbsData(masterKey);
    assertArrayEquals(hashedPin.getKbsAccessKey(), kbsData.getKbsAccessKey());
    assertArrayEquals(Hex.fromStringCondensed("ab7e8499d21f80a6600b3b9ee349ac6d72c07e3359fe885a934ba7aa844429f8"), kbsData.getKbsAccessKey());
    assertArrayEquals(Hex.fromStringCondensed("3f33ce58eb25b40436592a30eae2a8fabab1899095f4e2fba6e2d0dc43b4a2d9cac5a3931748522393951e0e54dec769"), kbsData.getCipherText());
    assertEquals(masterKey, kbsData.getMasterKey());
    String localPinHash = PinHashing.localPinHash(pin);
    assertTrue(PinHashing.verifyLocalPinHash(localPinHash, pin));
}
Also used : KbsData(org.whispersystems.signalservice.api.kbs.KbsData) MasterKey(org.whispersystems.signalservice.api.kbs.MasterKey) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) Test(org.junit.Test)

Example 5 with MasterKey

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

the class PinHashing_hashPin_Test method argon2_hashed_pin_password_with_just_non_arabic_numerals.

@Test
public void argon2_hashed_pin_password_with_just_non_arabic_numerals() throws IOException {
    String pin = " ६१८ ";
    byte[] backupId = Hex.fromStringCondensed("717dc111a98423a57196512606822fca646c653facd037c10728f14ba0be2ab3");
    MasterKey masterKey = new MasterKey(Hex.fromStringCondensed("0432d735b32f66d0e3a70d4f9cc821a8529521a4937d26b987715d8eff4e4c54"));
    HashedPin hashedPin = PinHashing.hashPin(pin, () -> backupId);
    KbsData kbsData = hashedPin.createNewKbsData(masterKey);
    assertArrayEquals(hashedPin.getKbsAccessKey(), kbsData.getKbsAccessKey());
    assertArrayEquals(Hex.fromStringCondensed("d2fedabd0d4c17a371491c9722578843a26be3b4923e28d452ab2fc5491e794b"), kbsData.getKbsAccessKey());
    assertArrayEquals(Hex.fromStringCondensed("877ef871ef1fc668401c717ef21aa12e8523579fb1ff4474b76f28c2293537c80cc7569996c9e0229bea7f378e3a824e"), kbsData.getCipherText());
    assertEquals(masterKey, kbsData.getMasterKey());
    assertEquals("23a75cb1df1a87df45cc2ed167c2bdc85ab1220b847c88761b0005cac907fce5", kbsData.getMasterKey().deriveRegistrationLock());
    String localPinHash = PinHashing.localPinHash(pin);
    assertTrue(PinHashing.verifyLocalPinHash(localPinHash, pin));
}
Also used : KbsData(org.whispersystems.signalservice.api.kbs.KbsData) MasterKey(org.whispersystems.signalservice.api.kbs.MasterKey) HashedPin(org.whispersystems.signalservice.api.kbs.HashedPin) Test(org.junit.Test)

Aggregations

MasterKey (org.whispersystems.signalservice.api.kbs.MasterKey)14 HashedPin (org.whispersystems.signalservice.api.kbs.HashedPin)12 Test (org.junit.Test)8 KbsData (org.whispersystems.signalservice.api.kbs.KbsData)8 WorkerThread (androidx.annotation.WorkerThread)4 KbsEnclave (org.thoughtcrime.securesms.KbsEnclave)4 KbsValues (org.thoughtcrime.securesms.keyvalue.KbsValues)4 KbsPinData (org.whispersystems.signalservice.api.KbsPinData)4 KeyBackupService (org.whispersystems.signalservice.api.KeyBackupService)4 IOException (java.io.IOException)2