Search in sources :

Example 1 with SignalProtocolStore

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

the class PushDecryptJob method handleMessage.

private void handleMessage(MasterSecretUnion masterSecret, SignalServiceEnvelope envelope, Optional<Long> smsMessageId) {
    try {
        GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
        SignalProtocolStore axolotlStore = new SignalProtocolStoreImpl(context);
        SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
        SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore);
        SignalServiceContent content = cipher.decrypt(envelope);
        if (content.getDataMessage().isPresent()) {
            SignalServiceDataMessage message = content.getDataMessage().get();
            if (message.isEndSession())
                handleEndSessionMessage(masterSecret, envelope, message, smsMessageId);
            else if (message.isGroupUpdate())
                handleGroupMessage(masterSecret, envelope, message, smsMessageId);
            else if (message.isExpirationUpdate())
                handleExpirationUpdate(masterSecret, envelope, message, smsMessageId);
            else if (message.getAttachments().isPresent())
                handleMediaMessage(masterSecret, envelope, message, smsMessageId);
            else
                handleTextMessage(masterSecret, envelope, message, smsMessageId);
            if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(message.getGroupInfo().get().getGroupId())) {
                handleUnknownGroupMessage(envelope, message.getGroupInfo().get());
            }
        } else if (content.getSyncMessage().isPresent()) {
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(masterSecret, envelope, syncMessage.getSent().get(), smsMessageId);
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(masterSecret, syncMessage.getRequest().get());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(masterSecret, syncMessage.getRead().get(), envelope.getTimestamp());
            else
                Log.w(TAG, "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            Log.w(TAG, "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(envelope, message.getOfferMessage().get(), smsMessageId);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(envelope, message.getAnswerMessage().get());
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(envelope, message.getIceUpdateMessages().get());
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(envelope, message.getHangupMessage().get(), smsMessageId);
        } else {
            Log.w(TAG, "Got unrecognized message...");
        }
        if (envelope.isPreKeySignalMessage()) {
            ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
        }
    } catch (InvalidVersionException e) {
        Log.w(TAG, e);
        handleInvalidVersionMessage(masterSecret, envelope, smsMessageId);
    } catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
        Log.w(TAG, e);
        handleCorruptMessage(masterSecret, envelope, smsMessageId);
    } catch (NoSessionException e) {
        Log.w(TAG, e);
        handleNoSessionMessage(masterSecret, envelope, smsMessageId);
    } catch (LegacyMessageException e) {
        Log.w(TAG, e);
        handleLegacyMessage(masterSecret, envelope, smsMessageId);
    } catch (DuplicateMessageException e) {
        Log.w(TAG, e);
        handleDuplicateMessage(masterSecret, envelope, smsMessageId);
    } catch (UntrustedIdentityException e) {
        Log.w(TAG, e);
        handleUntrustedIdentityMessage(masterSecret, envelope, smsMessageId);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) SignalServiceContent(org.whispersystems.signalservice.api.messages.SignalServiceContent) NoSessionException(org.whispersystems.libsignal.NoSessionException) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) MmsException(ws.com.google.android.mms.MmsException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) SignalProtocolStoreImpl(org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException)

Example 2 with SignalProtocolStore

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

the class RefreshPreKeysJob method onRun.

@Override
public void onRun() throws IOException {
    if (!SignalStore.account().isRegistered() || SignalStore.account().getAci() == null || SignalStore.account().getPni() == null) {
        Log.w(TAG, "Not registered. Skipping.");
        return;
    }
    SignalProtocolStore aciProtocolStore = ApplicationDependencies.getProtocolStore().aci();
    PreKeyMetadataStore aciPreKeyStore = SignalStore.account().aciPreKeys();
    SignalProtocolStore pniProtocolStore = ApplicationDependencies.getProtocolStore().pni();
    PreKeyMetadataStore pniPreKeyStore = SignalStore.account().pniPreKeys();
    if (refreshKeys(ServiceIdType.ACI, aciProtocolStore, aciPreKeyStore)) {
        PreKeyUtil.cleanSignedPreKeys(aciProtocolStore, aciPreKeyStore);
    }
    if (refreshKeys(ServiceIdType.PNI, pniProtocolStore, pniPreKeyStore)) {
        PreKeyUtil.cleanSignedPreKeys(pniProtocolStore, pniPreKeyStore);
    }
    SignalStore.misc().setLastPrekeyRefreshTime(System.currentTimeMillis());
    Log.i(TAG, "Successfully refreshed prekeys.");
}
Also used : SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) PreKeyMetadataStore(org.thoughtcrime.securesms.crypto.storage.PreKeyMetadataStore)

Example 3 with SignalProtocolStore

use of org.whispersystems.libsignal.state.SignalProtocolStore in project Signal-Android by signalapp.

the class PushDecryptJob method handleMessage.

private void handleMessage(SignalServiceEnvelope envelope, Optional<Long> smsMessageId) {
    try {
        GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
        SignalProtocolStore axolotlStore = new SignalProtocolStoreImpl(context);
        SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
        SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore);
        SignalServiceContent content = cipher.decrypt(envelope);
        if (content.getDataMessage().isPresent()) {
            SignalServiceDataMessage message = content.getDataMessage().get();
            if (message.isEndSession())
                handleEndSessionMessage(envelope, message, smsMessageId);
            else if (message.isGroupUpdate())
                handleGroupMessage(envelope, message, smsMessageId);
            else if (message.isExpirationUpdate())
                handleExpirationUpdate(envelope, message, smsMessageId);
            else if (message.getAttachments().isPresent())
                handleMediaMessage(envelope, message, smsMessageId);
            else if (message.getBody().isPresent())
                handleTextMessage(envelope, message, smsMessageId);
            if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(GroupUtil.getEncodedId(message.getGroupInfo().get().getGroupId(), false))) {
                handleUnknownGroupMessage(envelope, message.getGroupInfo().get());
            }
            if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
                handleProfileKey(envelope, message);
            }
        } else if (content.getSyncMessage().isPresent()) {
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(envelope, syncMessage.getSent().get());
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(syncMessage.getRequest().get());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(syncMessage.getRead().get(), envelope.getTimestamp());
            else if (syncMessage.getVerified().isPresent())
                handleSynchronizeVerifiedMessage(syncMessage.getVerified().get());
            else
                Log.w(TAG, "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            Log.w(TAG, "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(envelope, message.getOfferMessage().get(), smsMessageId);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(envelope, message.getAnswerMessage().get());
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(envelope, message.getIceUpdateMessages().get());
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(envelope, message.getHangupMessage().get(), smsMessageId);
            else if (message.getBusyMessage().isPresent())
                handleCallBusyMessage(envelope, message.getBusyMessage().get());
        } else if (content.getReceiptMessage().isPresent()) {
            SignalServiceReceiptMessage message = content.getReceiptMessage().get();
            if (message.isReadReceipt())
                handleReadReceipt(envelope, message);
            else if (message.isDeliveryReceipt())
                handleDeliveryReceipt(envelope, message);
        } else {
            Log.w(TAG, "Got unrecognized message...");
        }
        if (envelope.isPreKeySignalMessage()) {
            ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
        }
    } catch (InvalidVersionException e) {
        Log.w(TAG, e);
        handleInvalidVersionMessage(envelope, smsMessageId);
    } catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
        Log.w(TAG, e);
        handleCorruptMessage(envelope, smsMessageId);
    } catch (NoSessionException e) {
        Log.w(TAG, e);
        handleNoSessionMessage(envelope, smsMessageId);
    } catch (LegacyMessageException e) {
        Log.w(TAG, e);
        handleLegacyMessage(envelope, smsMessageId);
    } catch (DuplicateMessageException e) {
        Log.w(TAG, e);
        handleDuplicateMessage(envelope, smsMessageId);
    } catch (UntrustedIdentityException e) {
        Log.w(TAG, e);
        handleUntrustedIdentityMessage(envelope, smsMessageId);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) SignalServiceReceiptMessage(org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) SignalServiceContent(org.whispersystems.signalservice.api.messages.SignalServiceContent) NoSessionException(org.whispersystems.libsignal.NoSessionException) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) MmsException(org.thoughtcrime.securesms.mms.MmsException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) SignalProtocolStoreImpl(org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException)

Example 4 with SignalProtocolStore

use of org.whispersystems.libsignal.state.SignalProtocolStore 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 5 with SignalProtocolStore

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

the class PreKeyUtil method cleanSignedPreKeys.

/**
 * Finds all of the signed prekeys that are older than the archive age, and archive all but the youngest of those.
 */
public static synchronized void cleanSignedPreKeys(@NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) {
    Log.i(TAG, "Cleaning signed prekeys...");
    int activeSignedPreKeyId = metadataStore.getActiveSignedPreKeyId();
    if (activeSignedPreKeyId < 0) {
        return;
    }
    try {
        long now = System.currentTimeMillis();
        SignedPreKeyRecord currentRecord = protocolStore.loadSignedPreKey(activeSignedPreKeyId);
        List<SignedPreKeyRecord> allRecords = protocolStore.loadSignedPreKeys();
        allRecords.stream().filter(r -> r.getId() != currentRecord.getId()).filter(r -> (now - r.getTimestamp()) > ARCHIVE_AGE).sorted(Comparator.comparingLong(SignedPreKeyRecord::getTimestamp).reversed()).skip(1).forEach(record -> {
            Log.i(TAG, "Removing signed prekey record: " + record.getId() + " with timestamp: " + record.getTimestamp());
            protocolStore.removeSignedPreKey(record.getId());
        });
    } catch (InvalidKeyIdException e) {
        Log.w(TAG, e);
    }
}
Also used : SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) NonNull(androidx.annotation.NonNull) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) PreKeyMetadataStore(org.thoughtcrime.securesms.crypto.storage.PreKeyMetadataStore) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) Medium(org.whispersystems.libsignal.util.Medium) Comparator(java.util.Comparator) LinkedList(java.util.LinkedList) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) Curve(org.whispersystems.libsignal.ecc.Curve) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Aggregations

SignalProtocolStore (org.whispersystems.libsignal.state.SignalProtocolStore)5 PreKeyMetadataStore (org.thoughtcrime.securesms.crypto.storage.PreKeyMetadataStore)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)3 SignalProtocolStoreImpl (org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 DuplicateMessageException (org.whispersystems.libsignal.DuplicateMessageException)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)2 LegacyMessageException (org.whispersystems.libsignal.LegacyMessageException)2 NoSessionException (org.whispersystems.libsignal.NoSessionException)2 UntrustedIdentityException (org.whispersystems.libsignal.UntrustedIdentityException)2 PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)2 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)2 SignalServiceCipher (org.whispersystems.signalservice.api.crypto.SignalServiceCipher)2 SignalServiceContent (org.whispersystems.signalservice.api.messages.SignalServiceContent)2 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)2 SignalServiceCallMessage (org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage)2 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)2 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)2