Search in sources :

Example 21 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.

the class PreKeyUtil method generateLastResortKey.

public static PreKeyRecord generateLastResortKey(Context context) {
    PreKeyStore preKeyStore = new SignalPreKeyStore();
    if (preKeyStore.containsPreKey(Medium.MAX_VALUE)) {
        try {
            return preKeyStore.loadPreKey(Medium.MAX_VALUE);
        } catch (InvalidKeyIdException e) {
            LogUtil.exception("Error while generating last resort key", e);
            preKeyStore.removePreKey(Medium.MAX_VALUE);
        }
    }
    ECKeyPair keyPair = Curve.generateKeyPair();
    PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
    preKeyStore.storePreKey(Medium.MAX_VALUE, record);
    return record;
}
Also used : SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore)

Example 22 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.

the class ChatService method registerKeysWithTimestamp.

private Completable registerKeysWithTimestamp(final long timestamp, final IdentityKey identityKey, final PreKeyRecord lastResortKey, final String password, final int registrationId, final String signalingKey, final SignedPreKeyRecord signedPreKey, final List<PreKeyRecord> preKeys) {
    final long startTime = System.currentTimeMillis();
    final List<PreKeyEntity> entities = new LinkedList<>();
    for (PreKeyRecord preKey : preKeys) {
        final PreKeyEntity entity = new PreKeyEntity(preKey.getId(), preKey.getKeyPair().getPublicKey());
        entities.add(entity);
    }
    final PreKeyEntity lastResortEntity = new PreKeyEntity(lastResortKey.getId(), lastResortKey.getKeyPair().getPublicKey());
    final SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(), signedPreKey.getKeyPair().getPublicKey(), signedPreKey.getSignature());
    final long endTime = System.currentTimeMillis();
    final long elapsedSeconds = (endTime - startTime) / 1000;
    final long amendedTimestamp = timestamp + elapsedSeconds;
    final SignalBootstrap payload = new SignalBootstrap(entities, lastResortEntity, password, registrationId, signalingKey, signedPreKeyEntity, identityKey);
    final String payloadForSigning = JsonUtil.toJson(payload);
    return this.chatInterface.register(payloadForSigning, amendedTimestamp).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).toCompletable();
}
Also used : PreKeyEntity(org.whispersystems.signalservice.internal.push.PreKeyEntity) SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) SignalBootstrap(com.toshi.crypto.signal.model.SignalBootstrap) SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) LinkedList(java.util.LinkedList)

Example 23 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class RefreshPreKeysJob method refreshKeys.

/**
 * @return True if we need to clean prekeys, otherwise false.
 */
private boolean refreshKeys(@NonNull ServiceIdType serviceIdType, @NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) throws IOException {
    String logPrefix = "[" + serviceIdType + "] ";
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    int availableKeys = accountManager.getPreKeysCount(serviceIdType);
    log(TAG, logPrefix + "Available keys: " + availableKeys);
    if (availableKeys >= PREKEY_MINIMUM && metadataStore.isSignedPreKeyRegistered()) {
        log(TAG, logPrefix + "Available keys sufficient.");
        return false;
    }
    List<PreKeyRecord> preKeyRecords = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, false);
    IdentityKeyPair identityKey = protocolStore.getIdentityKeyPair();
    log(TAG, logPrefix + "Registering new prekeys...");
    accountManager.setPreKeys(serviceIdType, identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
    metadataStore.setActiveSignedPreKeyId(signedPreKeyRecord.getId());
    metadataStore.setSignedPreKeyRegistered(true);
    log(TAG, logPrefix + "Need to clean prekeys.");
    return true;
}
Also used : SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)

Example 24 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class PniAccountInitializationMigrationJob method performMigration.

@Override
public void performMigration() throws IOException {
    PNI pni = SignalStore.account().getPni();
    if (pni == null || SignalStore.account().getAci() == null || !Recipient.self().isRegistered()) {
        Log.w(TAG, "Not yet registered! No need to perform this migration.");
        return;
    }
    if (!SignalStore.account().hasPniIdentityKey()) {
        Log.i(TAG, "Generating PNI identity.");
        SignalStore.account().generatePniIdentityKeyIfNecessary();
    } else {
        Log.w(TAG, "Already generated the PNI identity. Skipping this step.");
    }
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    SignalProtocolStore protocolStore = ApplicationDependencies.getProtocolStore().pni();
    PreKeyMetadataStore metadataStore = SignalStore.account().pniPreKeys();
    if (!metadataStore.isSignedPreKeyRegistered()) {
        Log.i(TAG, "Uploading signed prekey for PNI.");
        SignedPreKeyRecord signedPreKey = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, true);
        List<PreKeyRecord> oneTimePreKeys = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
        accountManager.setPreKeys(ServiceIdType.PNI, protocolStore.getIdentityKeyPair().getPublicKey(), signedPreKey, oneTimePreKeys);
        metadataStore.setSignedPreKeyRegistered(true);
    } else {
        Log.w(TAG, "Already uploaded signed prekey for PNI. Skipping this step.");
    }
}
Also used : SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) PNI(org.whispersystems.signalservice.api.push.PNI) PreKeyMetadataStore(org.thoughtcrime.securesms.crypto.storage.PreKeyMetadataStore) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 25 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class PreKeyUtil method generateAndStoreOneTimePreKeys.

@NonNull
public static synchronized List<PreKeyRecord> generateAndStoreOneTimePreKeys(@NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) {
    Log.i(TAG, "Generating one-time prekeys...");
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = metadataStore.getNextOneTimePreKeyId();
    for (int i = 0; i < BATCH_SIZE; i++) {
        int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
        protocolStore.storePreKey(preKeyId, record);
        records.add(record);
    }
    metadataStore.setNextOneTimePreKeyId((preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
Also used : ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) LinkedList(java.util.LinkedList) NonNull(androidx.annotation.NonNull)

Aggregations

PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)34 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)33 LinkedList (java.util.LinkedList)9 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)8 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)8 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)6 IOException (java.io.IOException)5 PreKeyStore (org.whispersystems.libsignal.state.PreKeyStore)5 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)5 File (java.io.File)4 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)4 SignedPreKeyEntity (org.whispersystems.signalservice.api.push.SignedPreKeyEntity)4 Cursor (android.database.Cursor)3 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)3 ContentValues (android.content.ContentValues)2 NonNull (androidx.annotation.NonNull)2 SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)2 Element (de.pixart.messenger.xml.Element)2